readonly_dynrelocs
[external/binutils.git] / bfd / elf32-nds32.c
1 /* NDS32-specific support for 32-bit ELF.
2    Copyright (C) 2012-2017 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 bfd_boolean nds32_elf_ex9_build_hash_table
66 (bfd *, asection *, struct bfd_link_info *);
67 static bfd_boolean nds32_elf_ex9_itb_base (struct bfd_link_info *);
68 static void nds32_elf_ex9_import_table (struct bfd_link_info *);
69 static void nds32_elf_ex9_finish (struct bfd_link_info *);
70 static void nds32_elf_ex9_reloc_jmp (struct bfd_link_info *);
71 static void nds32_elf_get_insn_with_reg
72   (Elf_Internal_Rela *, uint32_t, uint32_t *);
73 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
74                                  Elf_Internal_Sym **);
75 static bfd_boolean nds32_elf_ex9_replace_instruction
76   (struct bfd_link_info *, bfd *, asection *);
77 static bfd_boolean nds32_elf_ifc_calc (struct bfd_link_info *, bfd *,
78                                        asection *);
79 static bfd_boolean nds32_elf_ifc_finish (struct bfd_link_info *);
80 static bfd_boolean nds32_elf_ifc_replace (struct bfd_link_info *);
81 static bfd_boolean nds32_elf_ifc_reloc (void);
82 static bfd_boolean  nds32_relax_fp_as_gp
83   (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
84    Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
85    Elf_Internal_Sym *isymbuf);
86 static bfd_boolean nds32_fag_remove_unused_fpbase
87   (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
88    Elf_Internal_Rela *irelend);
89
90 enum
91 {
92   MACH_V1 = bfd_mach_n1h,
93   MACH_V2 = bfd_mach_n1h_v2,
94   MACH_V3 = bfd_mach_n1h_v3,
95   MACH_V3M = bfd_mach_n1h_v3m
96 };
97
98 #define MIN(a, b) ((a) > (b) ? (b) : (a))
99 #define MAX(a, b) ((a) > (b) ? (a) : (b))
100
101 /* The name of the dynamic interpreter.  This is put in the .interp
102    section.  */
103 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
104
105 /* The nop opcode we use.  */
106 #define NDS32_NOP32 0x40000009
107 #define NDS32_NOP16 0x9200
108
109 /* The size in bytes of an entry in the procedure linkage table.  */
110 #define PLT_ENTRY_SIZE 24
111 #define PLT_HEADER_SIZE 24
112
113 /* The first entry in a procedure linkage table are reserved,
114    and the initial contents are unimportant (we zero them out).
115    Subsequent entries look like this.  */
116 #define PLT0_ENTRY_WORD0  0x46f00000            /* sethi   r15, HI20(.got+4)      */
117 #define PLT0_ENTRY_WORD1  0x58f78000            /* ori     r15, r25, LO12(.got+4) */
118 #define PLT0_ENTRY_WORD2  0x05178000            /* lwi     r17, [r15+0]           */
119 #define PLT0_ENTRY_WORD3  0x04f78001            /* lwi     r15, [r15+4]           */
120 #define PLT0_ENTRY_WORD4  0x4a003c00            /* jr      r15                    */
121
122 /* $ta is change to $r15 (from $r25).  */
123 #define PLT0_PIC_ENTRY_WORD0  0x46f00000        /* sethi   r15, HI20(got[1]@GOT)  */
124 #define PLT0_PIC_ENTRY_WORD1  0x58f78000        /* ori     r15, r15, LO12(got[1]@GOT) */
125 #define PLT0_PIC_ENTRY_WORD2  0x40f7f400        /* add     r15, gp, r15           */
126 #define PLT0_PIC_ENTRY_WORD3  0x05178000        /* lwi     r17, [r15+0]           */
127 #define PLT0_PIC_ENTRY_WORD4  0x04f78001        /* lwi     r15, [r15+4]           */
128 #define PLT0_PIC_ENTRY_WORD5  0x4a003c00        /* jr      r15                    */
129
130 #define PLT_ENTRY_WORD0  0x46f00000             /* sethi   r15, HI20(&got[n+3])      */
131 #define PLT_ENTRY_WORD1  0x04f78000             /* lwi     r15, r15, LO12(&got[n+3]) */
132 #define PLT_ENTRY_WORD2  0x4a003c00             /* jr      r15                       */
133 #define PLT_ENTRY_WORD3  0x45000000             /* movi    r16, sizeof(RELA) * n     */
134 #define PLT_ENTRY_WORD4  0x48000000             /* j      .plt0.                     */
135
136 #define PLT_PIC_ENTRY_WORD0  0x46f00000         /* sethi  r15, HI20(got[n+3]@GOT)    */
137 #define PLT_PIC_ENTRY_WORD1  0x58f78000         /* ori    r15, r15,    LO12(got[n+3]@GOT) */
138 #define PLT_PIC_ENTRY_WORD2  0x38febc02         /* lw     r15, [gp+r15]              */
139 #define PLT_PIC_ENTRY_WORD3  0x4a003c00         /* jr     r15                        */
140 #define PLT_PIC_ENTRY_WORD4  0x45000000         /* movi   r16, sizeof(RELA) * n      */
141 #define PLT_PIC_ENTRY_WORD5  0x48000000         /* j      .plt0                      */
142
143 /* These are macros used to get the relocation accurate value.  */
144 #define ACCURATE_8BIT_S1        (0x100)
145 #define ACCURATE_U9BIT_S1       (0x400)
146 #define ACCURATE_12BIT_S1       (0x2000)
147 #define ACCURATE_14BIT_S1       (0x4000)
148 #define ACCURATE_19BIT          (0x40000)
149
150 /* These are macros used to get the relocation conservative value.  */
151 #define CONSERVATIVE_8BIT_S1    (0x100 - 4)
152 #define CONSERVATIVE_14BIT_S1   (0x4000 - 4)
153 #define CONSERVATIVE_16BIT_S1   (0x10000 - 4)
154 #define CONSERVATIVE_24BIT_S1   (0x1000000 - 4)
155 /* These must be more conservative because the address may be in
156    different segment.  */
157 #define CONSERVATIVE_15BIT      (0x4000 - 0x1000)
158 #define CONSERVATIVE_15BIT_S1   (0x8000 - 0x1000)
159 #define CONSERVATIVE_15BIT_S2   (0x10000 - 0x1000)
160 #define CONSERVATIVE_19BIT      (0x40000 - 0x1000)
161 #define CONSERVATIVE_20BIT      (0x80000 - 0x1000)
162
163 /* Size of small data/bss sections, used to calculate SDA_BASE.  */
164 static long got_size = 0;
165 static int is_SDA_BASE_set = 0;
166 static int is_ITB_BASE_set = 0;
167
168 /* Convert ELF-VER in eflags to string for debugging purpose.  */
169 static const char *const nds32_elfver_strtab[] =
170 {
171   "ELF-1.2",
172   "ELF-1.3",
173   "ELF-1.4",
174 };
175
176 /* The nds32 linker needs to keep track of the number of relocs that it
177    decides to copy in check_relocs for each symbol.  This is so that
178    it can discard PC relative relocs if it doesn't need them when
179    linking with -Bsymbolic.  We store the information in a field
180    extending the regular ELF linker hash table.  */
181
182 /* This structure keeps track of the number of PC relative relocs we
183    have copied for a given symbol.  */
184
185 struct elf_nds32_pcrel_relocs_copied
186 {
187   /* Next section.  */
188   struct elf_nds32_pcrel_relocs_copied *next;
189   /* A section in dynobj.  */
190   asection *section;
191   /* Number of relocs copied in this section.  */
192   bfd_size_type count;
193 };
194
195 /* The sh linker needs to keep track of the number of relocs that it
196    decides to copy as dynamic relocs in check_relocs for each symbol.
197    This is so that it can later discard them if they are found to be
198    unnecessary.  We store the information in a field extending the
199    regular ELF linker hash table.  */
200
201 struct elf_nds32_dyn_relocs
202 {
203   struct elf_nds32_dyn_relocs *next;
204
205   /* The input section of the reloc.  */
206   asection *sec;
207
208   /* Total number of relocs copied for the input section.  */
209   bfd_size_type count;
210
211   /* Number of pc-relative relocs copied for the input section.  */
212   bfd_size_type pc_count;
213 };
214
215 /* Nds32 ELF linker hash entry.  */
216
217 struct elf_nds32_link_hash_entry
218 {
219   struct elf_link_hash_entry root;
220
221   /* Track dynamic relocs copied for this symbol.  */
222   struct elf_nds32_dyn_relocs *dyn_relocs;
223
224   /* For checking relocation type.  */
225 #define GOT_UNKNOWN     0
226 #define GOT_NORMAL      1
227 #define GOT_TLS_IE      2
228   unsigned int tls_type;
229 };
230
231 /* Get the nds32 ELF linker hash table from a link_info structure.  */
232
233 #define FP_BASE_NAME "_FP_BASE_"
234 static int check_start_export_sym = 0;
235 static size_t ex9_relax_size = 0;               /* Save ex9 predicted reducing size.  */
236
237 /* The offset for executable tls relaxation.  */
238 #define TP_OFFSET 0x0
239
240 struct elf_nds32_obj_tdata
241 {
242   struct elf_obj_tdata root;
243
244   /* tls_type for each local got entry.  */
245   char *local_got_tls_type;
246 };
247
248 #define elf_nds32_tdata(bfd) \
249   ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
250
251 #define elf32_nds32_local_got_tls_type(bfd) \
252   (elf_nds32_tdata (bfd)->local_got_tls_type)
253
254 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
255
256 static bfd_boolean
257 nds32_elf_mkobject (bfd *abfd)
258 {
259   return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
260                                   NDS32_ELF_DATA);
261 }
262
263 /* Relocations used for relocation.  */
264 static reloc_howto_type nds32_elf_howto_table[] =
265 {
266   /* This reloc does nothing.  */
267   HOWTO (R_NDS32_NONE,          /* type */
268          0,                     /* rightshift */
269          3,                     /* size (0 = byte, 1 = short, 2 = long) */
270          0,                     /* bitsize */
271          FALSE,                 /* pc_relative */
272          0,                     /* bitpos */
273          complain_overflow_dont,        /* complain_on_overflow */
274          bfd_elf_generic_reloc, /* special_function */
275          "R_NDS32_NONE",        /* name */
276          FALSE,                 /* partial_inplace */
277          0,                     /* src_mask */
278          0,                     /* dst_mask */
279          FALSE),                /* pcrel_offset */
280
281   /* A 16 bit absolute relocation.  */
282   HOWTO (R_NDS32_16,            /* type */
283          0,                     /* rightshift */
284          1,                     /* size (0 = byte, 1 = short, 2 = long) */
285          16,                    /* bitsize */
286          FALSE,                 /* pc_relative */
287          0,                     /* bitpos */
288          complain_overflow_bitfield,    /* complain_on_overflow */
289          nds32_elf_generic_reloc,       /* special_function */
290          "R_NDS32_16",          /* name */
291          FALSE,                 /* partial_inplace */
292          0xffff,                /* src_mask */
293          0xffff,                /* dst_mask */
294          FALSE),                /* pcrel_offset */
295
296   /* A 32 bit absolute relocation.  */
297   HOWTO (R_NDS32_32,            /* type */
298          0,                     /* rightshift */
299          2,                     /* size (0 = byte, 1 = short, 2 = long) */
300          32,                    /* bitsize */
301          FALSE,                 /* pc_relative */
302          0,                     /* bitpos */
303          complain_overflow_bitfield,    /* complain_on_overflow */
304          nds32_elf_generic_reloc,       /* special_function */
305          "R_NDS32_32",          /* name */
306          FALSE,                 /* partial_inplace */
307          0xffffffff,            /* src_mask */
308          0xffffffff,            /* dst_mask */
309          FALSE),                /* pcrel_offset */
310
311   /* A 20 bit address.  */
312   HOWTO (R_NDS32_20,            /* type */
313          0,                     /* rightshift */
314          2,                     /* size (0 = byte, 1 = short, 2 = long) */
315          20,                    /* bitsize */
316          FALSE,                 /* pc_relative */
317          0,                     /* bitpos */
318          complain_overflow_unsigned,    /* complain_on_overflow */
319          nds32_elf_generic_reloc,       /* special_function */
320          "R_NDS32_20",          /* name */
321          FALSE,                 /* partial_inplace */
322          0xfffff,               /* src_mask */
323          0xfffff,               /* dst_mask */
324          FALSE),                /* pcrel_offset */
325
326   /* An PC Relative 9-bit relocation, shifted by 2.
327      This reloc is complicated because relocations are relative to pc & -4.
328      i.e. branches in the right insn slot use the address of the left insn
329      slot for pc.  */
330   /* ??? It's not clear whether this should have partial_inplace set or not.
331      Branch relaxing in the assembler can store the addend in the insn,
332      and if bfd_install_relocation gets called the addend may get added
333      again.  */
334   HOWTO (R_NDS32_9_PCREL,       /* type */
335          1,                     /* rightshift */
336          1,                     /* size (0 = byte, 1 = short, 2 = long) */
337          8,                     /* bitsize */
338          TRUE,                  /* pc_relative */
339          0,                     /* bitpos */
340          complain_overflow_signed,      /* complain_on_overflow */
341          nds32_elf_9_pcrel_reloc,       /* special_function */
342          "R_NDS32_9_PCREL",     /* name */
343          FALSE,                 /* partial_inplace */
344          0xff,                  /* src_mask */
345          0xff,                  /* dst_mask */
346          TRUE),                 /* pcrel_offset */
347
348   /* A relative 15 bit relocation, right shifted by 1.  */
349   HOWTO (R_NDS32_15_PCREL,      /* type */
350          1,                     /* rightshift */
351          2,                     /* size (0 = byte, 1 = short, 2 = long) */
352          14,                    /* bitsize */
353          TRUE,                  /* pc_relative */
354          0,                     /* bitpos */
355          complain_overflow_signed,      /* complain_on_overflow */
356          bfd_elf_generic_reloc, /* special_function */
357          "R_NDS32_15_PCREL",    /* name */
358          FALSE,                 /* partial_inplace */
359          0x3fff,                /* src_mask */
360          0x3fff,                /* dst_mask */
361          TRUE),                 /* pcrel_offset */
362
363   /* A relative 17 bit relocation, right shifted by 1.  */
364   HOWTO (R_NDS32_17_PCREL,      /* type */
365          1,                     /* rightshift */
366          2,                     /* size (0 = byte, 1 = short, 2 = long) */
367          16,                    /* bitsize */
368          TRUE,                  /* pc_relative */
369          0,                     /* bitpos */
370          complain_overflow_signed,      /* complain_on_overflow */
371          bfd_elf_generic_reloc, /* special_function */
372          "R_NDS32_17_PCREL",    /* name */
373          FALSE,                 /* partial_inplace */
374          0xffff,                /* src_mask */
375          0xffff,                /* dst_mask */
376          TRUE),                 /* pcrel_offset */
377
378   /* A relative 25 bit relocation, right shifted by 1.  */
379   /* ??? It's not clear whether this should have partial_inplace set or not.
380      Branch relaxing in the assembler can store the addend in the insn,
381      and if bfd_install_relocation gets called the addend may get added
382      again.  */
383   HOWTO (R_NDS32_25_PCREL,      /* type */
384          1,                     /* rightshift */
385          2,                     /* size (0 = byte, 1 = short, 2 = long) */
386          24,                    /* bitsize */
387          TRUE,                  /* pc_relative */
388          0,                     /* bitpos */
389          complain_overflow_signed,      /* complain_on_overflow */
390          bfd_elf_generic_reloc, /* special_function */
391          "R_NDS32_25_PCREL",    /* name */
392          FALSE,                 /* partial_inplace */
393          0xffffff,              /* src_mask */
394          0xffffff,              /* dst_mask */
395          TRUE),                 /* pcrel_offset */
396
397   /* High 20 bits of address when lower 12 is or'd in.  */
398   HOWTO (R_NDS32_HI20,          /* type */
399          12,                    /* rightshift */
400          2,                     /* size (0 = byte, 1 = short, 2 = long) */
401          20,                    /* bitsize */
402          FALSE,                 /* pc_relative */
403          0,                     /* bitpos */
404          complain_overflow_dont,/* complain_on_overflow */
405          nds32_elf_hi20_reloc,  /* special_function */
406          "R_NDS32_HI20",        /* name */
407          FALSE,                 /* partial_inplace */
408          0x000fffff,            /* src_mask */
409          0x000fffff,            /* dst_mask */
410          FALSE),                /* pcrel_offset */
411
412   /* Lower 12 bits of address.  */
413   HOWTO (R_NDS32_LO12S3,        /* type */
414          3,                     /* rightshift */
415          2,                     /* size (0 = byte, 1 = short, 2 = long) */
416          9,                     /* bitsize */
417          FALSE,                 /* pc_relative */
418          0,                     /* bitpos */
419          complain_overflow_dont,/* complain_on_overflow */
420          nds32_elf_lo12_reloc,  /* special_function */
421          "R_NDS32_LO12S3",      /* name */
422          FALSE,                 /* partial_inplace */
423          0x000001ff,            /* src_mask */
424          0x000001ff,            /* dst_mask */
425          FALSE),                /* pcrel_offset */
426
427   /* Lower 12 bits of address.  */
428   HOWTO (R_NDS32_LO12S2,        /* type */
429          2,                     /* rightshift */
430          2,                     /* size (0 = byte, 1 = short, 2 = long) */
431          10,                    /* bitsize */
432          FALSE,                 /* pc_relative */
433          0,                     /* bitpos */
434          complain_overflow_dont,/* complain_on_overflow */
435          nds32_elf_lo12_reloc,  /* special_function */
436          "R_NDS32_LO12S2",      /* name */
437          FALSE,                 /* partial_inplace */
438          0x000003ff,            /* src_mask */
439          0x000003ff,            /* dst_mask */
440          FALSE),                /* pcrel_offset */
441
442   /* Lower 12 bits of address.  */
443   HOWTO (R_NDS32_LO12S1,        /* type */
444          1,                     /* rightshift */
445          2,                     /* size (0 = byte, 1 = short, 2 = long) */
446          11,                    /* bitsize */
447          FALSE,                 /* pc_relative */
448          0,                     /* bitpos */
449          complain_overflow_dont,/* complain_on_overflow */
450          nds32_elf_lo12_reloc,  /* special_function */
451          "R_NDS32_LO12S1",      /* name */
452          FALSE,                 /* partial_inplace */
453          0x000007ff,            /* src_mask */
454          0x000007ff,            /* dst_mask */
455          FALSE),                /* pcrel_offset */
456
457   /* Lower 12 bits of address.  */
458   HOWTO (R_NDS32_LO12S0,        /* type */
459          0,                     /* rightshift */
460          2,                     /* size (0 = byte, 1 = short, 2 = long) */
461          12,                    /* bitsize */
462          FALSE,                 /* pc_relative */
463          0,                     /* bitpos */
464          complain_overflow_dont,/* complain_on_overflow */
465          nds32_elf_lo12_reloc,  /* special_function */
466          "R_NDS32_LO12S0",      /* name */
467          FALSE,                 /* partial_inplace */
468          0x00000fff,            /* src_mask */
469          0x00000fff,            /* dst_mask */
470          FALSE),                /* pcrel_offset */
471
472   /* Small data area 15 bits offset.  */
473   HOWTO (R_NDS32_SDA15S3,       /* type */
474          3,                     /* rightshift */
475          2,                     /* size (0 = byte, 1 = short, 2 = long) */
476          15,                    /* bitsize */
477          FALSE,                 /* pc_relative */
478          0,                     /* bitpos */
479          complain_overflow_signed,      /* complain_on_overflow */
480          nds32_elf_sda15_reloc, /* special_function */
481          "R_NDS32_SDA15S3",     /* name */
482          FALSE,                 /* partial_inplace */
483          0x00007fff,            /* src_mask */
484          0x00007fff,            /* dst_mask */
485          FALSE),                /* pcrel_offset */
486
487   /* Small data area 15 bits offset.  */
488   HOWTO (R_NDS32_SDA15S2,       /* type */
489          2,                     /* rightshift */
490          2,                     /* size (0 = byte, 1 = short, 2 = long) */
491          15,                    /* bitsize */
492          FALSE,                 /* pc_relative */
493          0,                     /* bitpos */
494          complain_overflow_signed,      /* complain_on_overflow */
495          nds32_elf_sda15_reloc, /* special_function */
496          "R_NDS32_SDA15S2",     /* name */
497          FALSE,                 /* partial_inplace */
498          0x00007fff,            /* src_mask */
499          0x00007fff,            /* dst_mask */
500          FALSE),                /* pcrel_offset */
501
502   /* Small data area 15 bits offset.  */
503   HOWTO (R_NDS32_SDA15S1,       /* type */
504          1,                     /* rightshift */
505          2,                     /* size (0 = byte, 1 = short, 2 = long) */
506          15,                    /* bitsize */
507          FALSE,                 /* pc_relative */
508          0,                     /* bitpos */
509          complain_overflow_signed,      /* complain_on_overflow */
510          nds32_elf_sda15_reloc, /* special_function */
511          "R_NDS32_SDA15S1",     /* name */
512          FALSE,                 /* partial_inplace */
513          0x00007fff,            /* src_mask */
514          0x00007fff,            /* dst_mask */
515          FALSE),                /* pcrel_offset */
516
517   /* Small data area 15 bits offset.  */
518   HOWTO (R_NDS32_SDA15S0,       /* type */
519          0,                     /* rightshift */
520          2,                     /* size (0 = byte, 1 = short, 2 = long) */
521          15,                    /* bitsize */
522          FALSE,                 /* pc_relative */
523          0,                     /* bitpos */
524          complain_overflow_signed,      /* complain_on_overflow */
525          nds32_elf_sda15_reloc, /* special_function */
526          "R_NDS32_SDA15S0",     /* name */
527          FALSE,                 /* partial_inplace */
528          0x00007fff,            /* src_mask */
529          0x00007fff,            /* dst_mask */
530          FALSE),                /* pcrel_offset */
531
532   /* GNU extension to record C++ vtable hierarchy */
533   HOWTO (R_NDS32_GNU_VTINHERIT, /* type */
534          0,                     /* rightshift */
535          2,                     /* size (0 = byte, 1 = short, 2 = long) */
536          0,                     /* bitsize */
537          FALSE,                 /* pc_relative */
538          0,                     /* bitpos */
539          complain_overflow_dont,/* complain_on_overflow */
540          NULL,                  /* special_function */
541          "R_NDS32_GNU_VTINHERIT",       /* name */
542          FALSE,                 /* partial_inplace */
543          0,                     /* src_mask */
544          0,                     /* dst_mask */
545          FALSE),                /* pcrel_offset */
546
547   /* GNU extension to record C++ vtable member usage */
548   HOWTO (R_NDS32_GNU_VTENTRY,   /* type */
549          0,                     /* rightshift */
550          2,                     /* size (0 = byte, 1 = short, 2 = long) */
551          0,                     /* bitsize */
552          FALSE,                 /* pc_relative */
553          0,                     /* bitpos */
554          complain_overflow_dont,/* complain_on_overflow */
555          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
556          "R_NDS32_GNU_VTENTRY", /* name */
557          FALSE,                 /* partial_inplace */
558          0,                     /* src_mask */
559          0,                     /* dst_mask */
560          FALSE),                /* pcrel_offset */
561
562   /* A 16 bit absolute relocation.  */
563   HOWTO (R_NDS32_16_RELA,       /* type */
564          0,                     /* rightshift */
565          1,                     /* size (0 = byte, 1 = short, 2 = long) */
566          16,                    /* bitsize */
567          FALSE,                 /* pc_relative */
568          0,                     /* bitpos */
569          complain_overflow_bitfield,    /* complain_on_overflow */
570          bfd_elf_generic_reloc, /* special_function */
571          "R_NDS32_16_RELA",     /* name */
572          FALSE,                 /* partial_inplace */
573          0xffff,                /* src_mask */
574          0xffff,                /* dst_mask */
575          FALSE),                /* pcrel_offset */
576
577   /* A 32 bit absolute relocation.  */
578   HOWTO (R_NDS32_32_RELA,       /* type */
579          0,                     /* rightshift */
580          2,                     /* size (0 = byte, 1 = short, 2 = long) */
581          32,                    /* bitsize */
582          FALSE,                 /* pc_relative */
583          0,                     /* bitpos */
584          complain_overflow_bitfield,    /* complain_on_overflow */
585          bfd_elf_generic_reloc, /* special_function */
586          "R_NDS32_32_RELA",     /* name */
587          FALSE,                 /* partial_inplace */
588          0xffffffff,            /* src_mask */
589          0xffffffff,            /* dst_mask */
590          FALSE),                /* pcrel_offset */
591
592   /* A 20 bit address.  */
593   HOWTO (R_NDS32_20_RELA,       /* type */
594          0,                     /* rightshift */
595          2,                     /* size (0 = byte, 1 = short, 2 = long) */
596          20,                    /* bitsize */
597          FALSE,                 /* pc_relative */
598          0,                     /* bitpos */
599          complain_overflow_signed,      /* complain_on_overflow */
600          bfd_elf_generic_reloc, /* special_function */
601          "R_NDS32_20_RELA",     /* name */
602          FALSE,                 /* partial_inplace */
603          0xfffff,               /* src_mask */
604          0xfffff,               /* dst_mask */
605          FALSE),                /* pcrel_offset */
606
607   HOWTO (R_NDS32_9_PCREL_RELA,  /* type */
608          1,                     /* rightshift */
609          1,                     /* size (0 = byte, 1 = short, 2 = long) */
610          8,                     /* bitsize */
611          TRUE,                  /* pc_relative */
612          0,                     /* bitpos */
613          complain_overflow_signed,      /* complain_on_overflow */
614          bfd_elf_generic_reloc, /* special_function */
615          "R_NDS32_9_PCREL_RELA",/* name */
616          FALSE,                 /* partial_inplace */
617          0xff,                  /* src_mask */
618          0xff,                  /* dst_mask */
619          TRUE),                 /* pcrel_offset */
620
621   /* A relative 15 bit relocation, right shifted by 1.  */
622   HOWTO (R_NDS32_15_PCREL_RELA, /* type */
623          1,                     /* rightshift */
624          2,                     /* size (0 = byte, 1 = short, 2 = long) */
625          14,                    /* bitsize */
626          TRUE,                  /* pc_relative */
627          0,                     /* bitpos */
628          complain_overflow_signed,      /* complain_on_overflow */
629          bfd_elf_generic_reloc, /* special_function */
630          "R_NDS32_15_PCREL_RELA",       /* name */
631          FALSE,                 /* partial_inplace */
632          0x3fff,                /* src_mask */
633          0x3fff,                /* dst_mask */
634          TRUE),                 /* pcrel_offset */
635
636   /* A relative 17 bit relocation, right shifted by 1.  */
637   HOWTO (R_NDS32_17_PCREL_RELA, /* type */
638          1,                     /* rightshift */
639          2,                     /* size (0 = byte, 1 = short, 2 = long) */
640          16,                    /* bitsize */
641          TRUE,                  /* pc_relative */
642          0,                     /* bitpos */
643          complain_overflow_signed,      /* complain_on_overflow */
644          bfd_elf_generic_reloc, /* special_function */
645          "R_NDS32_17_PCREL_RELA",       /* name */
646          FALSE,                 /* partial_inplace */
647          0xffff,                /* src_mask */
648          0xffff,                /* dst_mask */
649          TRUE),                 /* pcrel_offset */
650
651   /* A relative 25 bit relocation, right shifted by 2.  */
652   HOWTO (R_NDS32_25_PCREL_RELA, /* type */
653          1,                     /* rightshift */
654          2,                     /* size (0 = byte, 1 = short, 2 = long) */
655          24,                    /* bitsize */
656          TRUE,                  /* pc_relative */
657          0,                     /* bitpos */
658          complain_overflow_signed,      /* complain_on_overflow */
659          bfd_elf_generic_reloc, /* special_function */
660          "R_NDS32_25_PCREL_RELA",       /* name */
661          FALSE,                 /* partial_inplace */
662          0xffffff,              /* src_mask */
663          0xffffff,              /* dst_mask */
664          TRUE),                 /* pcrel_offset */
665
666   /* High 20 bits of address when lower 16 is or'd in.  */
667   HOWTO (R_NDS32_HI20_RELA,     /* type */
668          12,                    /* rightshift */
669          2,                     /* size (0 = byte, 1 = short, 2 = long) */
670          20,                    /* bitsize */
671          FALSE,                 /* pc_relative */
672          0,                     /* bitpos */
673          complain_overflow_dont,/* complain_on_overflow */
674          bfd_elf_generic_reloc, /* special_function */
675          "R_NDS32_HI20_RELA",   /* name */
676          FALSE,                 /* partial_inplace */
677          0x000fffff,            /* src_mask */
678          0x000fffff,            /* dst_mask */
679          FALSE),                /* pcrel_offset */
680
681   /* Lower 12 bits of address.  */
682   HOWTO (R_NDS32_LO12S3_RELA,   /* type */
683          3,                     /* rightshift */
684          2,                     /* size (0 = byte, 1 = short, 2 = long) */
685          9,                     /* bitsize */
686          FALSE,                 /* pc_relative */
687          0,                     /* bitpos */
688          complain_overflow_dont,/* complain_on_overflow */
689          bfd_elf_generic_reloc, /* special_function */
690          "R_NDS32_LO12S3_RELA", /* name */
691          FALSE,                 /* partial_inplace */
692          0x000001ff,            /* src_mask */
693          0x000001ff,            /* dst_mask */
694          FALSE),                /* pcrel_offset */
695
696   /* Lower 12 bits of address.  */
697   HOWTO (R_NDS32_LO12S2_RELA,   /* type */
698          2,                     /* rightshift */
699          2,                     /* size (0 = byte, 1 = short, 2 = long) */
700          10,                    /* bitsize */
701          FALSE,                 /* pc_relative */
702          0,                     /* bitpos */
703          complain_overflow_dont,/* complain_on_overflow */
704          bfd_elf_generic_reloc, /* special_function */
705          "R_NDS32_LO12S2_RELA", /* name */
706          FALSE,                 /* partial_inplace */
707          0x000003ff,            /* src_mask */
708          0x000003ff,            /* dst_mask */
709          FALSE),                /* pcrel_offset */
710
711   /* Lower 12 bits of address.  */
712   HOWTO (R_NDS32_LO12S1_RELA,   /* type */
713          1,                     /* rightshift */
714          2,                     /* size (0 = byte, 1 = short, 2 = long) */
715          11,                    /* bitsize */
716          FALSE,                 /* pc_relative */
717          0,                     /* bitpos */
718          complain_overflow_dont,/* complain_on_overflow */
719          bfd_elf_generic_reloc, /* special_function */
720          "R_NDS32_LO12S1_RELA", /* name */
721          FALSE,                 /* partial_inplace */
722          0x000007ff,            /* src_mask */
723          0x000007ff,            /* dst_mask */
724          FALSE),                /* pcrel_offset */
725
726   /* Lower 12 bits of address.  */
727   HOWTO (R_NDS32_LO12S0_RELA,   /* type */
728          0,                     /* rightshift */
729          2,                     /* size (0 = byte, 1 = short, 2 = long) */
730          12,                    /* bitsize */
731          FALSE,                 /* pc_relative */
732          0,                     /* bitpos */
733          complain_overflow_dont,/* complain_on_overflow */
734          bfd_elf_generic_reloc, /* special_function */
735          "R_NDS32_LO12S0_RELA", /* name */
736          FALSE,                 /* partial_inplace */
737          0x00000fff,            /* src_mask */
738          0x00000fff,            /* dst_mask */
739          FALSE),                /* pcrel_offset */
740
741   /* Small data area 15 bits offset.  */
742   HOWTO (R_NDS32_SDA15S3_RELA,  /* type */
743          3,                     /* rightshift */
744          2,                     /* size (0 = byte, 1 = short, 2 = long) */
745          15,                    /* bitsize */
746          FALSE,                 /* pc_relative */
747          0,                     /* bitpos */
748          complain_overflow_signed,      /* complain_on_overflow */
749          bfd_elf_generic_reloc, /* special_function */
750          "R_NDS32_SDA15S3_RELA",/* name */
751          FALSE,                 /* partial_inplace */
752          0x00007fff,            /* src_mask */
753          0x00007fff,            /* dst_mask */
754          FALSE),                /* pcrel_offset */
755
756   /* Small data area 15 bits offset.  */
757   HOWTO (R_NDS32_SDA15S2_RELA,  /* type */
758          2,                     /* rightshift */
759          2,                     /* size (0 = byte, 1 = short, 2 = long) */
760          15,                    /* bitsize */
761          FALSE,                 /* pc_relative */
762          0,                     /* bitpos */
763          complain_overflow_signed,      /* complain_on_overflow */
764          bfd_elf_generic_reloc, /* special_function */
765          "R_NDS32_SDA15S2_RELA",/* name */
766          FALSE,                 /* partial_inplace */
767          0x00007fff,            /* src_mask */
768          0x00007fff,            /* dst_mask */
769          FALSE),                /* pcrel_offset */
770
771   HOWTO (R_NDS32_SDA15S1_RELA,  /* type */
772          1,                     /* rightshift */
773          2,                     /* size (0 = byte, 1 = short, 2 = long) */
774          15,                    /* bitsize */
775          FALSE,                 /* pc_relative */
776          0,                     /* bitpos */
777          complain_overflow_signed,      /* complain_on_overflow */
778          bfd_elf_generic_reloc, /* special_function */
779          "R_NDS32_SDA15S1_RELA",/* name */
780          FALSE,                 /* partial_inplace */
781          0x00007fff,            /* src_mask */
782          0x00007fff,            /* dst_mask */
783          FALSE),                /* pcrel_offset */
784
785   HOWTO (R_NDS32_SDA15S0_RELA,  /* type */
786          0,                     /* rightshift */
787          2,                     /* size (0 = byte, 1 = short, 2 = long) */
788          15,                    /* bitsize */
789          FALSE,                 /* pc_relative */
790          0,                     /* bitpos */
791          complain_overflow_signed,      /* complain_on_overflow */
792          bfd_elf_generic_reloc, /* special_function */
793          "R_NDS32_SDA15S0_RELA",/* name */
794          FALSE,                 /* partial_inplace */
795          0x00007fff,            /* src_mask */
796          0x00007fff,            /* dst_mask */
797          FALSE),                /* pcrel_offset */
798
799   /* GNU extension to record C++ vtable hierarchy */
800   HOWTO (R_NDS32_RELA_GNU_VTINHERIT,    /* type */
801          0,                     /* rightshift */
802          2,                     /* size (0 = byte, 1 = short, 2 = long) */
803          0,                     /* bitsize */
804          FALSE,                 /* pc_relative */
805          0,                     /* bitpos */
806          complain_overflow_dont,/* complain_on_overflow */
807          NULL,                  /* special_function */
808          "R_NDS32_RELA_GNU_VTINHERIT",  /* name */
809          FALSE,                 /* partial_inplace */
810          0,                     /* src_mask */
811          0,                     /* dst_mask */
812          FALSE),                /* pcrel_offset */
813
814   /* GNU extension to record C++ vtable member usage */
815   HOWTO (R_NDS32_RELA_GNU_VTENTRY,      /* type */
816          0,                     /* rightshift */
817          2,                     /* size (0 = byte, 1 = short, 2 = long) */
818          0,                     /* bitsize */
819          FALSE,                 /* pc_relative */
820          0,                     /* bitpos */
821          complain_overflow_dont,/* complain_on_overflow */
822          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
823          "R_NDS32_RELA_GNU_VTENTRY",    /* name */
824          FALSE,                 /* partial_inplace */
825          0,                     /* src_mask */
826          0,                     /* dst_mask */
827          FALSE),                /* pcrel_offset */
828
829   /* Like R_NDS32_20, but referring to the GOT table entry for
830      the symbol.  */
831   HOWTO (R_NDS32_GOT20,         /* type */
832          0,                     /* rightshift */
833          2,                     /* size (0 = byte, 1 = short, 2 = long) */
834          20,                    /* bitsize */
835          FALSE,                 /* pc_relative */
836          0,                     /* bitpos */
837          complain_overflow_signed,      /* complain_on_overflow */
838          bfd_elf_generic_reloc, /* special_function */
839          "R_NDS32_GOT20",       /* name */
840          FALSE,                 /* partial_inplace */
841          0xfffff,               /* src_mask */
842          0xfffff,               /* dst_mask */
843          FALSE),                /* pcrel_offset */
844
845   /* Like R_NDS32_PCREL, but referring to the procedure linkage table
846      entry for the symbol.  */
847   HOWTO (R_NDS32_25_PLTREL,     /* type */
848          1,                     /* rightshift */
849          2,                     /* size (0 = byte, 1 = short, 2 = long) */
850          24,                    /* bitsize */
851          TRUE,                  /* pc_relative */
852          0,                     /* bitpos */
853          complain_overflow_signed,      /* complain_on_overflow */
854          bfd_elf_generic_reloc, /* special_function */
855          "R_NDS32_25_PLTREL",   /* name */
856          FALSE,                 /* partial_inplace */
857          0xffffff,              /* src_mask */
858          0xffffff,              /* dst_mask */
859          TRUE),                 /* pcrel_offset */
860
861   /* This is used only by the dynamic linker.  The symbol should exist
862      both in the object being run and in some shared library.  The
863      dynamic linker copies the data addressed by the symbol from the
864      shared library into the object, because the object being
865      run has to have the data at some particular address.  */
866   HOWTO (R_NDS32_COPY,          /* type */
867          0,                     /* rightshift */
868          2,                     /* size (0 = byte, 1 = short, 2 = long) */
869          32,                    /* bitsize */
870          FALSE,                 /* pc_relative */
871          0,                     /* bitpos */
872          complain_overflow_bitfield,    /* complain_on_overflow */
873          bfd_elf_generic_reloc, /* special_function */
874          "R_NDS32_COPY",        /* name */
875          FALSE,                 /* partial_inplace */
876          0xffffffff,            /* src_mask */
877          0xffffffff,            /* dst_mask */
878          FALSE),                /* pcrel_offset */
879
880   /* Like R_NDS32_20, but used when setting global offset table
881      entries.  */
882   HOWTO (R_NDS32_GLOB_DAT,      /* type */
883          0,                     /* rightshift */
884          2,                     /* size (0 = byte, 1 = short, 2 = long) */
885          32,                    /* bitsize */
886          FALSE,                 /* pc_relative */
887          0,                     /* bitpos */
888          complain_overflow_bitfield,    /* complain_on_overflow */
889          bfd_elf_generic_reloc, /* special_function */
890          "R_NDS32_GLOB_DAT",    /* name */
891          FALSE,                 /* partial_inplace */
892          0xffffffff,            /* src_mask */
893          0xffffffff,            /* dst_mask */
894          FALSE),                /* pcrel_offset */
895
896   /* Marks a procedure linkage table entry for a symbol.  */
897   HOWTO (R_NDS32_JMP_SLOT,      /* type */
898          0,                     /* rightshift */
899          2,                     /* size (0 = byte, 1 = short, 2 = long) */
900          32,                    /* bitsize */
901          FALSE,                 /* pc_relative */
902          0,                     /* bitpos */
903          complain_overflow_bitfield,    /* complain_on_overflow */
904          bfd_elf_generic_reloc, /* special_function */
905          "R_NDS32_JMP_SLOT",    /* name */
906          FALSE,                 /* partial_inplace */
907          0xffffffff,            /* src_mask */
908          0xffffffff,            /* dst_mask */
909          FALSE),                /* pcrel_offset */
910
911   /* Used only by the dynamic linker.  When the object is run, this
912      longword is set to the load address of the object, plus the
913      addend.  */
914   HOWTO (R_NDS32_RELATIVE,      /* type */
915          0,                     /* rightshift */
916          2,                     /* size (0 = byte, 1 = short, 2 = long) */
917          32,                    /* bitsize */
918          FALSE,                 /* pc_relative */
919          0,                     /* bitpos */
920          complain_overflow_bitfield,    /* complain_on_overflow */
921          bfd_elf_generic_reloc, /* special_function */
922          "R_NDS32_RELATIVE",    /* name */
923          FALSE,                 /* partial_inplace */
924          0xffffffff,            /* src_mask */
925          0xffffffff,            /* dst_mask */
926          FALSE),                /* pcrel_offset */
927
928   HOWTO (R_NDS32_GOTOFF,        /* type */
929          0,                     /* rightshift */
930          2,                     /* size (0 = byte, 1 = short, 2 = long) */
931          20,                    /* bitsize */
932          FALSE,                 /* pc_relative */
933          0,                     /* bitpos */
934          complain_overflow_signed,      /* complain_on_overflow */
935          bfd_elf_generic_reloc, /* special_function */
936          "R_NDS32_GOTOFF",      /* name */
937          FALSE,                 /* partial_inplace */
938          0xfffff,               /* src_mask */
939          0xfffff,               /* dst_mask */
940          FALSE),                /* pcrel_offset */
941
942   /* An PC Relative 20-bit relocation used when setting PIC offset
943      table register.  */
944   HOWTO (R_NDS32_GOTPC20,       /* type */
945          0,                     /* rightshift */
946          2,                     /* size (0 = byte, 1 = short, 2 = long) */
947          20,                    /* bitsize */
948          TRUE,                  /* pc_relative */
949          0,                     /* bitpos */
950          complain_overflow_signed,      /* complain_on_overflow */
951          bfd_elf_generic_reloc, /* special_function */
952          "R_NDS32_GOTPC20",     /* name */
953          FALSE,                 /* partial_inplace */
954          0xfffff,               /* src_mask */
955          0xfffff,               /* dst_mask */
956          TRUE),                 /* pcrel_offset */
957
958   /* Like R_NDS32_HI20, but referring to the GOT table entry for
959      the symbol.  */
960   HOWTO (R_NDS32_GOT_HI20,      /* type */
961          12,                    /* rightshift */
962          2,                     /* size (0 = byte, 1 = short, 2 = long) */
963          20,                    /* bitsize */
964          FALSE,                 /* pc_relative */
965          0,                     /* bitpos */
966          complain_overflow_dont,/* complain_on_overflow */
967          bfd_elf_generic_reloc, /* special_function */
968          "R_NDS32_GOT_HI20",    /* name */
969          FALSE,                 /* partial_inplace */
970          0x000fffff,            /* src_mask */
971          0x000fffff,            /* dst_mask */
972          FALSE),                /* pcrel_offset */
973   HOWTO (R_NDS32_GOT_LO12,      /* type */
974          0,                     /* rightshift */
975          2,                     /* size (0 = byte, 1 = short, 2 = long) */
976          12,                    /* bitsize */
977          FALSE,                 /* pc_relative */
978          0,                     /* bitpos */
979          complain_overflow_dont,/* complain_on_overflow */
980          bfd_elf_generic_reloc, /* special_function */
981          "R_NDS32_GOT_LO12",    /* name */
982          FALSE,                 /* partial_inplace */
983          0x00000fff,            /* src_mask */
984          0x00000fff,            /* dst_mask */
985          FALSE),                /* pcrel_offset */
986
987   /* An PC Relative relocation used when setting PIC offset table register.
988      Like R_NDS32_HI20, but referring to the GOT table entry for
989      the symbol.  */
990   HOWTO (R_NDS32_GOTPC_HI20,    /* type */
991          12,                    /* rightshift */
992          2,                     /* size (0 = byte, 1 = short, 2 = long) */
993          20,                    /* bitsize */
994          FALSE,                 /* pc_relative */
995          0,                     /* bitpos */
996          complain_overflow_dont,/* complain_on_overflow */
997          bfd_elf_generic_reloc, /* special_function */
998          "R_NDS32_GOTPC_HI20",  /* name */
999          FALSE,                 /* partial_inplace */
1000          0x000fffff,            /* src_mask */
1001          0x000fffff,            /* dst_mask */
1002          TRUE),                 /* pcrel_offset */
1003   HOWTO (R_NDS32_GOTPC_LO12,    /* type */
1004          0,                     /* rightshift */
1005          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1006          12,                    /* bitsize */
1007          FALSE,                 /* pc_relative */
1008          0,                     /* bitpos */
1009          complain_overflow_dont,        /* complain_on_overflow */
1010          bfd_elf_generic_reloc, /* special_function */
1011          "R_NDS32_GOTPC_LO12",  /* name */
1012          FALSE,                 /* partial_inplace */
1013          0x00000fff,            /* src_mask */
1014          0x00000fff,            /* dst_mask */
1015          TRUE),                 /* pcrel_offset */
1016
1017   HOWTO (R_NDS32_GOTOFF_HI20,   /* type */
1018          12,                    /* rightshift */
1019          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1020          20,                    /* bitsize */
1021          FALSE,                 /* pc_relative */
1022          0,                     /* bitpos */
1023          complain_overflow_dont,/* complain_on_overflow */
1024          bfd_elf_generic_reloc, /* special_function */
1025          "R_NDS32_GOTOFF_HI20", /* name */
1026          FALSE,                 /* partial_inplace */
1027          0x000fffff,            /* src_mask */
1028          0x000fffff,            /* dst_mask */
1029          FALSE),                /* pcrel_offset */
1030   HOWTO (R_NDS32_GOTOFF_LO12,   /* type */
1031          0,                     /* rightshift */
1032          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1033          12,                    /* bitsize */
1034          FALSE,                 /* pc_relative */
1035          0,                     /* bitpos */
1036          complain_overflow_dont,/* complain_on_overflow */
1037          bfd_elf_generic_reloc, /* special_function */
1038          "R_NDS32_GOTOFF_LO12", /* name */
1039          FALSE,                 /* partial_inplace */
1040          0x00000fff,            /* src_mask */
1041          0x00000fff,            /* dst_mask */
1042          FALSE),                /* pcrel_offset */
1043
1044   /* Alignment hint for relaxable instruction.  This is used with
1045      R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
1046      in order to make next label aligned on word boundary.  */
1047   HOWTO (R_NDS32_INSN16,        /* type */
1048          0,                     /* rightshift */
1049          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1050          32,                    /* bitsize */
1051          FALSE,                 /* pc_relative */
1052          0,                     /* bitpos */
1053          complain_overflow_dont,/* complain_on_overflow */
1054          nds32_elf_ignore_reloc,/* special_function */
1055          "R_NDS32_INSN16",      /* name */
1056          FALSE,                 /* partial_inplace */
1057          0x00000fff,            /* src_mask */
1058          0x00000fff,            /* dst_mask */
1059          FALSE),                /* pcrel_offset */
1060
1061   /* Alignment hint for label.  */
1062   HOWTO (R_NDS32_LABEL,         /* type */
1063          0,                     /* rightshift */
1064          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1065          32,                    /* bitsize */
1066          FALSE,                 /* pc_relative */
1067          0,                     /* bitpos */
1068          complain_overflow_dont,/* complain_on_overflow */
1069          nds32_elf_ignore_reloc,/* special_function */
1070          "R_NDS32_LABEL",       /* name */
1071          FALSE,                 /* partial_inplace */
1072          0xffffffff,            /* src_mask */
1073          0xffffffff,            /* dst_mask */
1074          FALSE),                /* pcrel_offset */
1075
1076   /* Relax hint for unconditional call sequence  */
1077   HOWTO (R_NDS32_LONGCALL1,     /* type */
1078          0,                     /* rightshift */
1079          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1080          32,                    /* bitsize */
1081          FALSE,                 /* pc_relative */
1082          0,                     /* bitpos */
1083          complain_overflow_dont,/* complain_on_overflow */
1084          nds32_elf_ignore_reloc,/* special_function */
1085          "R_NDS32_LONGCALL1",   /* name */
1086          FALSE,                 /* partial_inplace */
1087          0xffffffff,            /* src_mask */
1088          0xffffffff,            /* dst_mask */
1089          FALSE),                /* pcrel_offset */
1090
1091   /* Relax hint for conditional call sequence.  */
1092   HOWTO (R_NDS32_LONGCALL2,     /* type */
1093          0,                     /* rightshift */
1094          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1095          32,                    /* bitsize */
1096          FALSE,                 /* pc_relative */
1097          0,                     /* bitpos */
1098          complain_overflow_dont,/* complain_on_overflow */
1099          nds32_elf_ignore_reloc,/* special_function */
1100          "R_NDS32_LONGCALL2",   /* name */
1101          FALSE,                 /* partial_inplace */
1102          0xffffffff,            /* src_mask */
1103          0xffffffff,            /* dst_mask */
1104          FALSE),                /* pcrel_offset */
1105
1106   /* Relax hint for conditional call sequence.  */
1107   HOWTO (R_NDS32_LONGCALL3,     /* type */
1108          0,                     /* rightshift */
1109          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1110          32,                    /* bitsize */
1111          FALSE,                 /* pc_relative */
1112          0,                     /* bitpos */
1113          complain_overflow_dont,/* complain_on_overflow */
1114          nds32_elf_ignore_reloc,/* special_function */
1115          "R_NDS32_LONGCALL3",   /* name */
1116          FALSE,                 /* partial_inplace */
1117          0xffffffff,            /* src_mask */
1118          0xffffffff,            /* dst_mask */
1119          FALSE),                /* pcrel_offset */
1120
1121   /* Relax hint for unconditional branch sequence.  */
1122   HOWTO (R_NDS32_LONGJUMP1,     /* type */
1123          0,                     /* rightshift */
1124          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1125          32,                    /* bitsize */
1126          FALSE,                 /* pc_relative */
1127          0,                     /* bitpos */
1128          complain_overflow_dont,/* complain_on_overflow */
1129          nds32_elf_ignore_reloc,/* special_function */
1130          "R_NDS32_LONGJUMP1",   /* name */
1131          FALSE,                 /* partial_inplace */
1132          0xffffffff,            /* src_mask */
1133          0xffffffff,            /* dst_mask */
1134          FALSE),                /* pcrel_offset */
1135
1136   /* Relax hint for conditional branch sequence.  */
1137   HOWTO (R_NDS32_LONGJUMP2,     /* type */
1138          0,                     /* rightshift */
1139          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1140          32,                    /* bitsize */
1141          FALSE,                 /* pc_relative */
1142          0,                     /* bitpos */
1143          complain_overflow_dont,/* complain_on_overflow */
1144          nds32_elf_ignore_reloc,/* special_function */
1145          "R_NDS32_LONGJUMP2",   /* name */
1146          FALSE,                 /* partial_inplace */
1147          0xffffffff,            /* src_mask */
1148          0xffffffff,            /* dst_mask */
1149          FALSE),                /* pcrel_offset */
1150
1151   /* Relax hint for conditional branch sequence.  */
1152   HOWTO (R_NDS32_LONGJUMP3,     /* type */
1153          0,                     /* rightshift */
1154          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1155          32,                    /* bitsize */
1156          FALSE,                 /* pc_relative */
1157          0,                     /* bitpos */
1158          complain_overflow_dont,/* complain_on_overflow */
1159          nds32_elf_ignore_reloc,/* special_function */
1160          "R_NDS32_LONGJUMP3",   /* name */
1161          FALSE,                 /* partial_inplace */
1162          0xffffffff,            /* src_mask */
1163          0xffffffff,            /* dst_mask */
1164          FALSE),                /* pcrel_offset */
1165
1166   /* Relax hint for load/store sequence.   */
1167   HOWTO (R_NDS32_LOADSTORE,     /* type */
1168          0,                     /* rightshift */
1169          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1170          32,                    /* bitsize */
1171          FALSE,                 /* pc_relative */
1172          0,                     /* bitpos */
1173          complain_overflow_dont,/* complain_on_overflow */
1174          nds32_elf_ignore_reloc,/* special_function */
1175          "R_NDS32_LOADSTORE",   /* name */
1176          FALSE,                 /* partial_inplace */
1177          0xffffffff,            /* src_mask */
1178          0xffffffff,            /* dst_mask */
1179          FALSE),                /* pcrel_offset */
1180
1181   /* Relax hint for load/store sequence.  */
1182   HOWTO (R_NDS32_9_FIXED_RELA,  /* type */
1183          0,                     /* rightshift */
1184          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1185          16,                    /* bitsize */
1186          FALSE,                 /* pc_relative */
1187          0,                     /* bitpos */
1188          complain_overflow_dont,/* complain_on_overflow */
1189          nds32_elf_ignore_reloc,/* special_function */
1190          "R_NDS32_9_FIXED_RELA",/* name */
1191          FALSE,                 /* partial_inplace */
1192          0x000000ff,            /* src_mask */
1193          0x000000ff,            /* dst_mask */
1194          FALSE),                /* pcrel_offset */
1195
1196   /* Relax hint for load/store sequence.  */
1197   HOWTO (R_NDS32_15_FIXED_RELA, /* type */
1198          0,                     /* rightshift */
1199          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1200          32,                    /* bitsize */
1201          FALSE,                 /* pc_relative */
1202          0,                     /* bitpos */
1203          complain_overflow_dont,/* complain_on_overflow */
1204          nds32_elf_ignore_reloc,/* special_function */
1205          "R_NDS32_15_FIXED_RELA",       /* name */
1206          FALSE,                 /* partial_inplace */
1207          0x00003fff,            /* src_mask */
1208          0x00003fff,            /* dst_mask */
1209          FALSE),                /* pcrel_offset */
1210
1211   /* Relax hint for load/store sequence.  */
1212   HOWTO (R_NDS32_17_FIXED_RELA, /* type */
1213          0,                     /* rightshift */
1214          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1215          32,                    /* bitsize */
1216          FALSE,                 /* pc_relative */
1217          0,                     /* bitpos */
1218          complain_overflow_dont,/* complain_on_overflow */
1219          nds32_elf_ignore_reloc,/* special_function */
1220          "R_NDS32_17_FIXED_RELA",       /* name */
1221          FALSE,                 /* partial_inplace */
1222          0x0000ffff,            /* src_mask */
1223          0x0000ffff,            /* dst_mask */
1224          FALSE),                /* pcrel_offset */
1225
1226   /* Relax hint for load/store sequence.  */
1227   HOWTO (R_NDS32_25_FIXED_RELA, /* type */
1228          0,                     /* rightshift */
1229          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1230          32,                    /* bitsize */
1231          FALSE,                 /* pc_relative */
1232          0,                     /* bitpos */
1233          complain_overflow_dont,/* complain_on_overflow */
1234          nds32_elf_ignore_reloc,/* special_function */
1235          "R_NDS32_25_FIXED_RELA",       /* name */
1236          FALSE,                 /* partial_inplace */
1237          0x00ffffff,            /* src_mask */
1238          0x00ffffff,            /* dst_mask */
1239          FALSE),                /* pcrel_offset */
1240
1241   /* High 20 bits of PLT symbol offset relative to PC.  */
1242   HOWTO (R_NDS32_PLTREL_HI20,   /* type */
1243          12,                    /* rightshift */
1244          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1245          20,                    /* bitsize */
1246          FALSE,                 /* pc_relative */
1247          0,                     /* bitpos */
1248          complain_overflow_dont,/* complain_on_overflow */
1249          bfd_elf_generic_reloc, /* special_function */
1250          "R_NDS32_PLTREL_HI20", /* name */
1251          FALSE,                 /* partial_inplace */
1252          0x000fffff,            /* src_mask */
1253          0x000fffff,            /* dst_mask */
1254          FALSE),                /* pcrel_offset */
1255
1256   /* Low 12 bits of PLT symbol offset relative to PC.  */
1257   HOWTO (R_NDS32_PLTREL_LO12,   /* type */
1258          0,                     /* rightshift */
1259          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1260          12,                    /* bitsize */
1261          FALSE,                 /* pc_relative */
1262          0,                     /* bitpos */
1263          complain_overflow_dont,/* complain_on_overflow */
1264          bfd_elf_generic_reloc, /* special_function */
1265          "R_NDS32_PLTREL_LO12", /* name */
1266          FALSE,                 /* partial_inplace */
1267          0x00000fff,            /* src_mask */
1268          0x00000fff,            /* dst_mask */
1269          FALSE),                /* pcrel_offset */
1270
1271   /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
1272   HOWTO (R_NDS32_PLT_GOTREL_HI20,       /* type */
1273          12,                    /* rightshift */
1274          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1275          20,                    /* bitsize */
1276          FALSE,                 /* pc_relative */
1277          0,                     /* bitpos */
1278          complain_overflow_dont,/* complain_on_overflow */
1279          bfd_elf_generic_reloc, /* special_function */
1280          "R_NDS32_PLT_GOTREL_HI20",     /* name */
1281          FALSE,                 /* partial_inplace */
1282          0x000fffff,            /* src_mask */
1283          0x000fffff,            /* dst_mask */
1284          FALSE),                /* pcrel_offset */
1285
1286   /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
1287   HOWTO (R_NDS32_PLT_GOTREL_LO12,       /* type */
1288          0,                     /* rightshift */
1289          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1290          12,                    /* bitsize */
1291          FALSE,                 /* pc_relative */
1292          0,                     /* bitpos */
1293          complain_overflow_dont,/* complain_on_overflow */
1294          bfd_elf_generic_reloc, /* special_function */
1295          "R_NDS32_PLT_GOTREL_LO12",     /* name */
1296          FALSE,                 /* partial_inplace */
1297          0x00000fff,            /* src_mask */
1298          0x00000fff,            /* dst_mask */
1299          FALSE),                /* pcrel_offset */
1300
1301   /* Small data area 12 bits offset.  */
1302   HOWTO (R_NDS32_SDA12S2_DP_RELA,       /* type */
1303          2,                     /* rightshift */
1304          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1305          12,                    /* bitsize */
1306          FALSE,                 /* pc_relative */
1307          0,                     /* bitpos */
1308          complain_overflow_signed,      /* complain_on_overflow */
1309          bfd_elf_generic_reloc, /* special_function */
1310          "R_NDS32_SDA12S2_DP_RELA",     /* name */
1311          FALSE,                 /* partial_inplace */
1312          0x00000fff,            /* src_mask */
1313          0x00000fff,            /* dst_mask */
1314          FALSE),                /* pcrel_offset */
1315
1316   /* Small data area 12 bits offset.  */
1317   HOWTO (R_NDS32_SDA12S2_SP_RELA,       /* type */
1318          2,                     /* rightshift */
1319          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1320          12,                    /* bitsize */
1321          FALSE,                 /* pc_relative */
1322          0,                     /* bitpos */
1323          complain_overflow_signed,      /* complain_on_overflow */
1324          bfd_elf_generic_reloc, /* special_function */
1325          "R_NDS32_SDA12S2_SP_RELA",     /* name */
1326          FALSE,                 /* partial_inplace */
1327          0x00000fff,            /* src_mask */
1328          0x00000fff,            /* dst_mask */
1329          FALSE),                /* pcrel_offset */
1330   /* Lower 12 bits of address.  */
1331
1332   HOWTO (R_NDS32_LO12S2_DP_RELA,        /* type */
1333          2,                     /* rightshift */
1334          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1335          10,                    /* bitsize */
1336          FALSE,                 /* pc_relative */
1337          0,                     /* bitpos */
1338          complain_overflow_dont,/* complain_on_overflow */
1339          bfd_elf_generic_reloc, /* special_function */
1340          "R_NDS32_LO12S2_DP_RELA",      /* name */
1341          FALSE,                 /* partial_inplace */
1342          0x000003ff,            /* src_mask */
1343          0x000003ff,            /* dst_mask */
1344          FALSE),                /* pcrel_offset */
1345
1346   /* Lower 12 bits of address.  */
1347   HOWTO (R_NDS32_LO12S2_SP_RELA,/* type */
1348          2,                     /* rightshift */
1349          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1350          10,                    /* bitsize */
1351          FALSE,                 /* pc_relative */
1352          0,                     /* bitpos */
1353          complain_overflow_dont,/* complain_on_overflow */
1354          bfd_elf_generic_reloc, /* special_function */
1355          "R_NDS32_LO12S2_SP_RELA",      /* name */
1356          FALSE,                 /* partial_inplace */
1357          0x000003ff,            /* src_mask */
1358          0x000003ff,            /* dst_mask */
1359          FALSE),                /* pcrel_offset */
1360   /* Lower 12 bits of address.  Special identity for or case.  */
1361   HOWTO (R_NDS32_LO12S0_ORI_RELA,       /* type */
1362          0,                     /* rightshift */
1363          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1364          12,                    /* bitsize */
1365          FALSE,                 /* pc_relative */
1366          0,                     /* bitpos */
1367          complain_overflow_dont,/* complain_on_overflow */
1368          bfd_elf_generic_reloc, /* special_function */
1369          "R_NDS32_LO12S0_ORI_RELA",     /* name */
1370          FALSE,                 /* partial_inplace */
1371          0x00000fff,            /* src_mask */
1372          0x00000fff,            /* dst_mask */
1373          FALSE),                /* pcrel_offset */
1374   /* Small data area 19 bits offset.  */
1375   HOWTO (R_NDS32_SDA16S3_RELA,  /* type */
1376          3,                     /* rightshift */
1377          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1378          16,                    /* bitsize */
1379          FALSE,                 /* pc_relative */
1380          0,                     /* bitpos */
1381          complain_overflow_signed,      /* complain_on_overflow */
1382          bfd_elf_generic_reloc, /* special_function */
1383          "R_NDS32_SDA16S3_RELA",/* name */
1384          FALSE,                 /* partial_inplace */
1385          0x0000ffff,            /* src_mask */
1386          0x0000ffff,            /* dst_mask */
1387          FALSE),                /* pcrel_offset */
1388
1389   /* Small data area 15 bits offset.  */
1390   HOWTO (R_NDS32_SDA17S2_RELA,  /* type */
1391          2,                     /* rightshift */
1392          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1393          17,                    /* bitsize */
1394          FALSE,                 /* pc_relative */
1395          0,                     /* bitpos */
1396          complain_overflow_signed,      /* complain_on_overflow */
1397          bfd_elf_generic_reloc, /* special_function */
1398          "R_NDS32_SDA17S2_RELA",/* name */
1399          FALSE,                 /* partial_inplace */
1400          0x0001ffff,            /* src_mask */
1401          0x0001ffff,            /* dst_mask */
1402          FALSE),                /* pcrel_offset */
1403
1404   HOWTO (R_NDS32_SDA18S1_RELA,  /* type */
1405          1,                     /* rightshift */
1406          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1407          18,                    /* bitsize */
1408          FALSE,                 /* pc_relative */
1409          0,                     /* bitpos */
1410          complain_overflow_signed,      /* complain_on_overflow */
1411          bfd_elf_generic_reloc, /* special_function */
1412          "R_NDS32_SDA18S1_RELA",/* name */
1413          FALSE,                 /* partial_inplace */
1414          0x0003ffff,            /* src_mask */
1415          0x0003ffff,            /* dst_mask */
1416          FALSE),                /* pcrel_offset */
1417
1418   HOWTO (R_NDS32_SDA19S0_RELA,  /* type */
1419          0,                     /* rightshift */
1420          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1421          19,                    /* bitsize */
1422          FALSE,                 /* pc_relative */
1423          0,                     /* bitpos */
1424          complain_overflow_signed,      /* complain_on_overflow */
1425          bfd_elf_generic_reloc, /* special_function */
1426          "R_NDS32_SDA19S0_RELA",/* name */
1427          FALSE,                 /* partial_inplace */
1428          0x0007ffff,            /* src_mask */
1429          0x0007ffff,            /* dst_mask */
1430          FALSE),                /* pcrel_offset */
1431   HOWTO (R_NDS32_DWARF2_OP1_RELA,       /* type */
1432          0,                     /* rightshift */
1433          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1434          8,                     /* bitsize */
1435          FALSE,                 /* pc_relative */
1436          0,                     /* bitpos */
1437          complain_overflow_dont,/* complain_on_overflow */
1438          nds32_elf_ignore_reloc,/* special_function */
1439          "R_NDS32_DWARF2_OP1_RELA",     /* name */
1440          FALSE,                 /* partial_inplace */
1441          0xff,                  /* src_mask */
1442          0xff,                  /* dst_mask */
1443          FALSE),                /* pcrel_offset */
1444   HOWTO (R_NDS32_DWARF2_OP2_RELA,       /* type */
1445          0,                     /* rightshift */
1446          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1447          16,                    /* bitsize */
1448          FALSE,                 /* pc_relative */
1449          0,                     /* bitpos */
1450          complain_overflow_dont,/* complain_on_overflow */
1451          nds32_elf_ignore_reloc,/* special_function */
1452          "R_NDS32_DWARF2_OP2_RELA",     /* name */
1453          FALSE,                 /* partial_inplace */
1454          0xffff,                /* src_mask */
1455          0xffff,                /* dst_mask */
1456          FALSE),                /* pcrel_offset */
1457   HOWTO (R_NDS32_DWARF2_LEB_RELA,       /* type */
1458          0,                     /* rightshift */
1459          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1460          32,                    /* bitsize */
1461          FALSE,                 /* pc_relative */
1462          0,                     /* bitpos */
1463          complain_overflow_dont,/* complain_on_overflow */
1464          nds32_elf_ignore_reloc,/* special_function */
1465          "R_NDS32_DWARF2_LEB_RELA",     /* name */
1466          FALSE,                 /* partial_inplace */
1467          0xffffffff,            /* src_mask */
1468          0xffffffff,            /* dst_mask */
1469          FALSE),                /* pcrel_offset */
1470   HOWTO (R_NDS32_UPDATE_TA_RELA,/* type */
1471          0,                     /* rightshift */
1472          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1473          16,                    /* bitsize */
1474          FALSE,                 /* pc_relative */
1475          0,                     /* bitpos */
1476          complain_overflow_dont,/* complain_on_overflow */
1477          nds32_elf_ignore_reloc,/* special_function */
1478          "R_NDS32_UPDATE_TA_RELA",      /* name */
1479          FALSE,                 /* partial_inplace */
1480          0xffff,                /* src_mask */
1481          0xffff,                /* dst_mask */
1482          FALSE),                /* pcrel_offset */
1483   /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1484      entry for the symbol.  */
1485   HOWTO (R_NDS32_9_PLTREL,      /* type */
1486          1,                     /* rightshift */
1487          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1488          8,                     /* bitsize */
1489          TRUE,                  /* pc_relative */
1490          0,                     /* bitpos */
1491          complain_overflow_signed,      /* complain_on_overflow */
1492          bfd_elf_generic_reloc, /* special_function */
1493          "R_NDS32_9_PLTREL",    /* name */
1494          FALSE,                 /* partial_inplace */
1495          0xff,                  /* src_mask */
1496          0xff,                  /* dst_mask */
1497          TRUE),                 /* pcrel_offset */
1498   /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
1499   HOWTO (R_NDS32_PLT_GOTREL_LO20,       /* type */
1500          0,                     /* rightshift */
1501          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1502          20,                    /* bitsize */
1503          FALSE,                 /* pc_relative */
1504          0,                     /* bitpos */
1505          complain_overflow_dont,/* complain_on_overflow */
1506          bfd_elf_generic_reloc, /* special_function */
1507          "R_NDS32_PLT_GOTREL_LO20",     /* name */
1508          FALSE,                 /* partial_inplace */
1509          0x000fffff,            /* src_mask */
1510          0x000fffff,            /* dst_mask */
1511          FALSE),                /* pcrel_offset */
1512   /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1513   HOWTO (R_NDS32_PLT_GOTREL_LO15,       /* type */
1514          0,                     /* rightshift */
1515          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1516          15,                    /* bitsize */
1517          FALSE,                 /* pc_relative */
1518          0,                     /* bitpos */
1519          complain_overflow_dont,/* complain_on_overflow */
1520          bfd_elf_generic_reloc, /* special_function */
1521          "R_NDS32_PLT_GOTREL_LO15",     /* name */
1522          FALSE,                 /* partial_inplace */
1523          0x00007fff,            /* src_mask */
1524          0x00007fff,            /* dst_mask */
1525          FALSE),                /* pcrel_offset */
1526   /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
1527   HOWTO (R_NDS32_PLT_GOTREL_LO19,       /* type */
1528          0,                     /* rightshift */
1529          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1530          19,                    /* bitsize */
1531          FALSE,                 /* pc_relative */
1532          0,                     /* bitpos */
1533          complain_overflow_dont,/* complain_on_overflow */
1534          bfd_elf_generic_reloc, /* special_function */
1535          "R_NDS32_PLT_GOTREL_LO19",     /* name */
1536          FALSE,                 /* partial_inplace */
1537          0x0007ffff,            /* src_mask */
1538          0x0007ffff,            /* dst_mask */
1539          FALSE),                /* pcrel_offset */
1540   HOWTO (R_NDS32_GOT_LO15,      /* type */
1541          0,                     /* rightshift */
1542          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1543          15,                    /* bitsize */
1544          FALSE,                 /* pc_relative */
1545          0,                     /* bitpos */
1546          complain_overflow_dont,/* complain_on_overflow */
1547          bfd_elf_generic_reloc, /* special_function */
1548          "R_NDS32_GOT_LO15",    /* name */
1549          FALSE,                 /* partial_inplace */
1550          0x00007fff,            /* src_mask */
1551          0x00007fff,            /* dst_mask */
1552          FALSE),                /* pcrel_offset */
1553   HOWTO (R_NDS32_GOT_LO19,      /* type */
1554          0,                     /* rightshift */
1555          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1556          19,                    /* bitsize */
1557          FALSE,                 /* pc_relative */
1558          0,                     /* bitpos */
1559          complain_overflow_dont,/* complain_on_overflow */
1560          bfd_elf_generic_reloc, /* special_function */
1561          "R_NDS32_GOT_LO19",    /* name */
1562          FALSE,                 /* partial_inplace */
1563          0x0007ffff,            /* src_mask */
1564          0x0007ffff,            /* dst_mask */
1565          FALSE),                /* pcrel_offset */
1566   HOWTO (R_NDS32_GOTOFF_LO15,   /* type */
1567          0,                     /* rightshift */
1568          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1569          15,                    /* bitsize */
1570          FALSE,                 /* pc_relative */
1571          0,                     /* bitpos */
1572          complain_overflow_dont,/* complain_on_overflow */
1573          bfd_elf_generic_reloc, /* special_function */
1574          "R_NDS32_GOTOFF_LO15", /* name */
1575          FALSE,                 /* partial_inplace */
1576          0x00007fff,            /* src_mask */
1577          0x00007fff,            /* dst_mask */
1578          FALSE),                /* pcrel_offset */
1579   HOWTO (R_NDS32_GOTOFF_LO19,   /* type */
1580          0,                     /* rightshift */
1581          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1582          19,                    /* bitsize */
1583          FALSE,                 /* pc_relative */
1584          0,                     /* bitpos */
1585          complain_overflow_dont,/* complain_on_overflow */
1586          bfd_elf_generic_reloc, /* special_function */
1587          "R_NDS32_GOTOFF_LO19", /* name */
1588          FALSE,                 /* partial_inplace */
1589          0x0007ffff,            /* src_mask */
1590          0x0007ffff,            /* dst_mask */
1591          FALSE),                /* pcrel_offset */
1592   /* GOT 15 bits offset.  */
1593   HOWTO (R_NDS32_GOT15S2_RELA,  /* type */
1594          2,                     /* rightshift */
1595          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1596          15,                    /* bitsize */
1597          FALSE,                 /* pc_relative */
1598          0,                     /* bitpos */
1599          complain_overflow_signed,      /* complain_on_overflow */
1600          bfd_elf_generic_reloc, /* special_function */
1601          "R_NDS32_GOT15S2_RELA",/* name */
1602          FALSE,                 /* partial_inplace */
1603          0x00007fff,            /* src_mask */
1604          0x00007fff,            /* dst_mask */
1605          FALSE),                /* pcrel_offset */
1606   /* GOT 17 bits offset.  */
1607   HOWTO (R_NDS32_GOT17S2_RELA,  /* type */
1608          2,                     /* rightshift */
1609          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1610          17,                    /* bitsize */
1611          FALSE,                 /* pc_relative */
1612          0,                     /* bitpos */
1613          complain_overflow_signed,      /* complain_on_overflow */
1614          bfd_elf_generic_reloc, /* special_function */
1615          "R_NDS32_GOT17S2_RELA",/* name */
1616          FALSE,                 /* partial_inplace */
1617          0x0001ffff,            /* src_mask */
1618          0x0001ffff,            /* dst_mask */
1619          FALSE),                /* pcrel_offset */
1620   /* A 5 bit address.  */
1621   HOWTO (R_NDS32_5_RELA,        /* type */
1622          0,                     /* rightshift */
1623          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1624          5,                     /* bitsize */
1625          FALSE,                 /* pc_relative */
1626          0,                     /* bitpos */
1627          complain_overflow_signed,      /* complain_on_overflow */
1628          bfd_elf_generic_reloc, /* special_function */
1629          "R_NDS32_5_RELA",      /* name */
1630          FALSE,                 /* partial_inplace */
1631          0x1f,                  /* src_mask */
1632          0x1f,                  /* dst_mask */
1633          FALSE),                /* pcrel_offset */
1634   HOWTO (R_NDS32_10_UPCREL_RELA,/* type */
1635          1,                     /* rightshift */
1636          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1637          9,                     /* bitsize */
1638          TRUE,                  /* pc_relative */
1639          0,                     /* bitpos */
1640          complain_overflow_unsigned,    /* complain_on_overflow */
1641          bfd_elf_generic_reloc, /* special_function */
1642          "R_NDS32_10_UPCREL_RELA",      /* name */
1643          FALSE,                 /* partial_inplace */
1644          0x1ff,                 /* src_mask */
1645          0x1ff,                 /* dst_mask */
1646          TRUE),                 /* pcrel_offset */
1647   HOWTO (R_NDS32_SDA_FP7U2_RELA,/* type */
1648          2,                     /* rightshift */
1649          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1650          7,                     /* bitsize */
1651          FALSE,                 /* pc_relative */
1652          0,                     /* bitpos */
1653          complain_overflow_unsigned,    /* complain_on_overflow */
1654          bfd_elf_generic_reloc, /* special_function */
1655          "R_NDS32_SDA_FP7U2_RELA",      /* name */
1656          FALSE,                 /* partial_inplace */
1657          0x0000007f,            /* src_mask */
1658          0x0000007f,            /* dst_mask */
1659          FALSE),                /* pcrel_offset */
1660   HOWTO (R_NDS32_WORD_9_PCREL_RELA,     /* type */
1661          1,                     /* rightshift */
1662          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1663          8,                     /* bitsize */
1664          TRUE,                  /* pc_relative */
1665          0,                     /* bitpos */
1666          complain_overflow_signed,      /* complain_on_overflow */
1667          bfd_elf_generic_reloc, /* special_function */
1668          "R_NDS32_WORD_9_PCREL_RELA",   /* name */
1669          FALSE,                 /* partial_inplace */
1670          0xff,                  /* src_mask */
1671          0xff,                  /* dst_mask */
1672          TRUE),                 /* pcrel_offset */
1673   HOWTO (R_NDS32_25_ABS_RELA,   /* type */
1674          1,                     /* rightshift */
1675          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1676          24,                    /* bitsize */
1677          FALSE,                 /* pc_relative */
1678          0,                     /* bitpos */
1679          complain_overflow_dont,/* complain_on_overflow */
1680          bfd_elf_generic_reloc, /* special_function */
1681          "R_NDS32_25_ABS_RELA", /* name */
1682          FALSE,                 /* partial_inplace */
1683          0xffffff,              /* src_mask */
1684          0xffffff,              /* dst_mask */
1685          FALSE),                /* pcrel_offset */
1686
1687   /* A relative 17 bit relocation for ifc, right shifted by 1.  */
1688   HOWTO (R_NDS32_17IFC_PCREL_RELA,      /* type */
1689          1,                     /* rightshift */
1690          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1691          16,                    /* bitsize */
1692          TRUE,                  /* pc_relative */
1693          0,                     /* bitpos */
1694          complain_overflow_signed,      /* complain_on_overflow */
1695          bfd_elf_generic_reloc, /* special_function */
1696          "R_NDS32_17IFC_PCREL_RELA",    /* name */
1697          FALSE,                 /* partial_inplace */
1698          0xffff,                /* src_mask */
1699          0xffff,                /* dst_mask */
1700          TRUE),                 /* pcrel_offset */
1701
1702   /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
1703   HOWTO (R_NDS32_10IFCU_PCREL_RELA,     /* type */
1704          1,                     /* rightshift */
1705          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1706          9,                     /* bitsize */
1707          TRUE,                  /* pc_relative */
1708          0,                     /* bitpos */
1709          complain_overflow_unsigned,    /* complain_on_overflow */
1710          bfd_elf_generic_reloc, /* special_function */
1711          "R_NDS32_10IFCU_PCREL_RELA",   /* name */
1712          FALSE,                 /* partial_inplace */
1713          0x1ff,                 /* src_mask */
1714          0x1ff,                 /* dst_mask */
1715          TRUE),                 /* pcrel_offset */
1716
1717   /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol.  */
1718   HOWTO (R_NDS32_TLS_LE_HI20,   /* type */
1719          12,                    /* rightshift */
1720          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1721          20,                    /* bitsize */
1722          FALSE,                 /* pc_relative */
1723          0,                     /* bitpos */
1724          complain_overflow_dont,        /* complain_on_overflow */
1725          bfd_elf_generic_reloc, /* special_function */
1726          "R_NDS32_TLS_LE_HI20", /* name */
1727          FALSE,                 /* partial_inplace */
1728          0x000fffff,            /* src_mask */
1729          0x000fffff,            /* dst_mask */
1730          FALSE),                /* pcrel_offset */
1731   HOWTO (R_NDS32_TLS_LE_LO12,   /* type */
1732          0,                     /* rightshift */
1733          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1734          12,                    /* bitsize */
1735          FALSE,                 /* pc_relative */
1736          0,                     /* bitpos */
1737          complain_overflow_dont,        /* complain_on_overflow */
1738          bfd_elf_generic_reloc, /* special_function */
1739          "R_NDS32_TLS_LE_LO12", /* name */
1740          FALSE,                 /* partial_inplace */
1741          0x00000fff,            /* src_mask */
1742          0x00000fff,            /* dst_mask */
1743          FALSE),                /* pcrel_offset */
1744
1745   /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol.  */
1746   HOWTO (R_NDS32_TLS_IE_HI20,   /* type */
1747          12,                    /* rightshift */
1748          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1749          20,                    /* bitsize */
1750          FALSE,                 /* pc_relative */
1751          0,                     /* bitpos */
1752          complain_overflow_dont,        /* complain_on_overflow */
1753          bfd_elf_generic_reloc, /* special_function */
1754          "R_NDS32_TLS_IE_HI20", /* name */
1755          FALSE,                 /* partial_inplace */
1756          0x000fffff,            /* src_mask */
1757          0x000fffff,            /* dst_mask */
1758          FALSE),                /* pcrel_offset */
1759   HOWTO (R_NDS32_TLS_IE_LO12S2, /* type */
1760          2,                     /* rightshift */
1761          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1762          10,                    /* bitsize */
1763          FALSE,                 /* pc_relative */
1764          0,                     /* bitpos */
1765          complain_overflow_dont,        /* complain_on_overflow */
1766          bfd_elf_generic_reloc, /* special_function */
1767          "R_NDS32_TLS_IE_LO12S2",       /* name */
1768          FALSE,                 /* partial_inplace */
1769          0x000003ff,            /* src_mask */
1770          0x000003ff,            /* dst_mask */
1771          FALSE),                /* pcrel_offset */
1772   /* Mark a TLS IE entry in GOT.  */
1773   HOWTO (R_NDS32_TLS_TPOFF,     /* type */
1774          0,                     /* rightshift */
1775          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1776          32,                    /* bitsize */
1777          FALSE,                 /* pc_relative */
1778          0,                     /* bitpos */
1779          complain_overflow_bitfield,    /* complain_on_overflow */
1780          bfd_elf_generic_reloc, /* special_function */
1781          "R_NDS32_TLS_TPOFF",   /* name */
1782          FALSE,                 /* partial_inplace */
1783          0xffffffff,            /* src_mask */
1784          0xffffffff,            /* dst_mask */
1785          FALSE),                /* pcrel_offset */
1786   /* A 20 bit address.  */
1787   HOWTO (R_NDS32_TLS_LE_20,     /* type */
1788          0,                     /* rightshift */
1789          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1790          20,                    /* bitsize */
1791          FALSE,         /* pc_relative */
1792          0,                     /* bitpos */
1793          complain_overflow_signed,      /* complain_on_overflow */
1794          bfd_elf_generic_reloc, /* special_function */
1795          "R_NDS32_TLS_LE_20",   /* name */
1796          FALSE,         /* partial_inplace */
1797          0xfffff,               /* src_mask */
1798          0xfffff,               /* dst_mask */
1799          FALSE),                /* pcrel_offset */
1800   HOWTO (R_NDS32_TLS_LE_15S0,   /* type */
1801          0,                     /* rightshift */
1802          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1803          15,                    /* 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_15S0", /* name */
1809          FALSE,         /* partial_inplace */
1810          0x7fff,                /* src_mask */
1811          0x7fff,                /* dst_mask */
1812          FALSE),                /* pcrel_offset */
1813   HOWTO (R_NDS32_TLS_LE_15S1,   /* type */
1814          1,                     /* rightshift */
1815          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1816          15,                    /* bitsize */
1817          FALSE,         /* pc_relative */
1818          0,                     /* bitpos */
1819          complain_overflow_signed,      /* complain_on_overflow */
1820          bfd_elf_generic_reloc, /* special_function */
1821          "R_NDS32_TLS_LE_15S1", /* name */
1822          FALSE,         /* partial_inplace */
1823          0x7fff,                /* src_mask */
1824          0x7fff,                /* dst_mask */
1825          FALSE),                /* pcrel_offset */
1826   HOWTO (R_NDS32_TLS_LE_15S2,   /* type */
1827          2,                     /* rightshift */
1828          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1829          15,                    /* bitsize */
1830          FALSE,         /* pc_relative */
1831          0,                     /* bitpos */
1832          complain_overflow_signed,      /* complain_on_overflow */
1833          bfd_elf_generic_reloc, /* special_function */
1834          "R_NDS32_TLS_LE_15S2", /* name */
1835          FALSE,         /* partial_inplace */
1836          0x7fff,                /* src_mask */
1837          0x7fff,                /* dst_mask */
1838          FALSE),                /* pcrel_offset */
1839
1840   /* Relax hint for unconditional call sequence  */
1841   HOWTO (R_NDS32_LONGCALL4,     /* type */
1842          0,                     /* rightshift */
1843          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1844          32,                    /* bitsize */
1845          FALSE,                 /* pc_relative */
1846          0,                     /* bitpos */
1847          complain_overflow_dont,        /* complain_on_overflow */
1848          nds32_elf_ignore_reloc,        /* special_function */
1849          "R_NDS32_LONGCALL4",   /* name */
1850          FALSE,                 /* partial_inplace */
1851          0xffffffff,            /* src_mask */
1852          0xffffffff,            /* dst_mask */
1853          FALSE),                /* pcrel_offset */
1854
1855   /* Relax hint for conditional call sequence.  */
1856   HOWTO (R_NDS32_LONGCALL5,     /* type */
1857          0,                     /* rightshift */
1858          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1859          32,                    /* bitsize */
1860          FALSE,                 /* pc_relative */
1861          0,                     /* bitpos */
1862          complain_overflow_dont,        /* complain_on_overflow */
1863          nds32_elf_ignore_reloc,        /* special_function */
1864          "R_NDS32_LONGCALL5",   /* name */
1865          FALSE,                 /* partial_inplace */
1866          0xffffffff,            /* src_mask */
1867          0xffffffff,            /* dst_mask */
1868          FALSE),                /* pcrel_offset */
1869
1870   /* Relax hint for conditional call sequence.  */
1871   HOWTO (R_NDS32_LONGCALL6,     /* type */
1872          0,                     /* rightshift */
1873          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1874          32,                    /* bitsize */
1875          FALSE,                 /* pc_relative */
1876          0,                     /* bitpos */
1877          complain_overflow_dont,        /* complain_on_overflow */
1878          nds32_elf_ignore_reloc,        /* special_function */
1879          "R_NDS32_LONGCALL6",   /* name */
1880          FALSE,                 /* partial_inplace */
1881          0xffffffff,            /* src_mask */
1882          0xffffffff,            /* dst_mask */
1883          FALSE),                /* pcrel_offset */
1884
1885   /* Relax hint for unconditional branch sequence.  */
1886   HOWTO (R_NDS32_LONGJUMP4,     /* type */
1887          0,                     /* rightshift */
1888          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1889          32,                    /* bitsize */
1890          FALSE,                 /* pc_relative */
1891          0,                     /* bitpos */
1892          complain_overflow_dont,        /* complain_on_overflow */
1893          nds32_elf_ignore_reloc,        /* special_function */
1894          "R_NDS32_LONGJUMP4",   /* name */
1895          FALSE,                 /* partial_inplace */
1896          0xffffffff,            /* src_mask */
1897          0xffffffff,            /* dst_mask */
1898          FALSE),                /* pcrel_offset */
1899
1900   /* Relax hint for conditional branch sequence.  */
1901   HOWTO (R_NDS32_LONGJUMP5,     /* type */
1902          0,                     /* rightshift */
1903          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1904          32,                    /* bitsize */
1905          FALSE,                 /* pc_relative */
1906          0,                     /* bitpos */
1907          complain_overflow_dont,        /* complain_on_overflow */
1908          nds32_elf_ignore_reloc,        /* special_function */
1909          "R_NDS32_LONGJUMP5",   /* name */
1910          FALSE,                 /* partial_inplace */
1911          0xffffffff,            /* src_mask */
1912          0xffffffff,            /* dst_mask */
1913          FALSE),                /* pcrel_offset */
1914
1915   /* Relax hint for conditional branch sequence.  */
1916   HOWTO (R_NDS32_LONGJUMP6,     /* type */
1917          0,                     /* rightshift */
1918          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1919          32,                    /* bitsize */
1920          FALSE,                 /* pc_relative */
1921          0,                     /* bitpos */
1922          complain_overflow_dont,        /* complain_on_overflow */
1923          nds32_elf_ignore_reloc,        /* special_function */
1924          "R_NDS32_LONGJUMP6",   /* name */
1925          FALSE,                 /* partial_inplace */
1926          0xffffffff,            /* src_mask */
1927          0xffffffff,            /* dst_mask */
1928          FALSE),                /* pcrel_offset */
1929
1930   /* Relax hint for conditional branch sequence.  */
1931   HOWTO (R_NDS32_LONGJUMP7,     /* type */
1932          0,                     /* rightshift */
1933          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1934          32,                    /* bitsize */
1935          FALSE,                 /* pc_relative */
1936          0,                     /* bitpos */
1937          complain_overflow_dont,        /* complain_on_overflow */
1938          nds32_elf_ignore_reloc,        /* special_function */
1939          "R_NDS32_LONGJUMP7",   /* name */
1940          FALSE,                 /* partial_inplace */
1941          0xffffffff,            /* src_mask */
1942          0xffffffff,            /* dst_mask */
1943          FALSE),                /* pcrel_offset */
1944 };
1945
1946 /* Relocations used for relaxation.  */
1947 static reloc_howto_type nds32_elf_relax_howto_table[] =
1948 {
1949   HOWTO (R_NDS32_RELAX_ENTRY,   /* type */
1950          0,                     /* rightshift */
1951          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1952          32,                    /* bitsize */
1953          FALSE,                 /* pc_relative */
1954          0,                     /* bitpos */
1955          complain_overflow_dont,/* complain_on_overflow */
1956          nds32_elf_ignore_reloc,/* special_function */
1957          "R_NDS32_RELAX_ENTRY", /* name */
1958          FALSE,                 /* partial_inplace */
1959          0xffffffff,            /* src_mask */
1960          0xffffffff,            /* dst_mask */
1961          FALSE),                /* pcrel_offset */
1962   HOWTO (R_NDS32_GOT_SUFF,      /* type */
1963          0,                     /* rightshift */
1964          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1965          32,                    /* bitsize */
1966          FALSE,                 /* pc_relative */
1967          0,                     /* bitpos */
1968          complain_overflow_dont,/* complain_on_overflow */
1969          nds32_elf_ignore_reloc,/* special_function */
1970          "R_NDS32_GOT_SUFF",    /* name */
1971          FALSE,                 /* partial_inplace */
1972          0xffffffff,            /* src_mask */
1973          0xffffffff,            /* dst_mask */
1974          FALSE),                /* pcrel_offset */
1975   HOWTO (R_NDS32_GOTOFF_SUFF,   /* type */
1976          0,                     /* rightshift */
1977          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1978          32,                    /* bitsize */
1979          FALSE,                 /* pc_relative */
1980          0,                     /* bitpos */
1981          complain_overflow_bitfield,    /* complain_on_overflow */
1982          nds32_elf_ignore_reloc,/* special_function */
1983          "R_NDS32_GOTOFF_SUFF", /* name */
1984          FALSE,                 /* partial_inplace */
1985          0xffffffff,            /* src_mask */
1986          0xffffffff,            /* dst_mask */
1987          FALSE),                /* pcrel_offset */
1988   HOWTO (R_NDS32_PLT_GOT_SUFF,  /* type */
1989          0,                     /* rightshift */
1990          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1991          32,                    /* bitsize */
1992          FALSE,                 /* pc_relative */
1993          0,                     /* bitpos */
1994          complain_overflow_dont,/* complain_on_overflow */
1995          nds32_elf_ignore_reloc,/* special_function */
1996          "R_NDS32_PLT_GOT_SUFF",/* name */
1997          FALSE,                 /* partial_inplace */
1998          0xffffffff,            /* src_mask */
1999          0xffffffff,            /* dst_mask */
2000          FALSE),                /* pcrel_offset */
2001   HOWTO (R_NDS32_MULCALL_SUFF,  /* type */
2002          0,                     /* rightshift */
2003          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2004          32,                    /* bitsize */
2005          FALSE,                 /* pc_relative */
2006          0,                     /* bitpos */
2007          complain_overflow_dont,/* complain_on_overflow */
2008          nds32_elf_ignore_reloc,/* special_function */
2009          "R_NDS32_MULCALL_SUFF",/* name */
2010          FALSE,                 /* partial_inplace */
2011          0xffffffff,            /* src_mask */
2012          0xffffffff,            /* dst_mask */
2013          FALSE),                /* pcrel_offset */
2014   HOWTO (R_NDS32_PTR,           /* type */
2015          0,                     /* rightshift */
2016          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2017          32,                    /* bitsize */
2018          FALSE,                 /* pc_relative */
2019          0,                     /* bitpos */
2020          complain_overflow_dont,/* complain_on_overflow */
2021          nds32_elf_ignore_reloc,/* special_function */
2022          "R_NDS32_PTR",         /* name */
2023          FALSE,                 /* partial_inplace */
2024          0xffffffff,            /* src_mask */
2025          0xffffffff,            /* dst_mask */
2026          FALSE),                /* pcrel_offset */
2027   HOWTO (R_NDS32_PTR_COUNT,     /* type */
2028          0,                     /* rightshift */
2029          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2030          32,                    /* bitsize */
2031          FALSE,                 /* pc_relative */
2032          0,                     /* bitpos */
2033          complain_overflow_dont,/* complain_on_overflow */
2034          nds32_elf_ignore_reloc,/* special_function */
2035          "R_NDS32_PTR_COUNT",   /* name */
2036          FALSE,                 /* partial_inplace */
2037          0xffffffff,            /* src_mask */
2038          0xffffffff,            /* dst_mask */
2039          FALSE),                /* pcrel_offset */
2040   HOWTO (R_NDS32_PTR_RESOLVED,  /* type */
2041          0,                     /* rightshift */
2042          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2043          32,                    /* bitsize */
2044          FALSE,                 /* pc_relative */
2045          0,                     /* bitpos */
2046          complain_overflow_dont,/* complain_on_overflow */
2047          nds32_elf_ignore_reloc,/* special_function */
2048          "R_NDS32_PTR_RESOLVED",/* name */
2049          FALSE,                 /* partial_inplace */
2050          0xffffffff,            /* src_mask */
2051          0xffffffff,            /* dst_mask */
2052          FALSE),                /* pcrel_offset */
2053   HOWTO (R_NDS32_PLTBLOCK,      /* type */
2054          0,                     /* rightshift */
2055          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2056          32,                    /* bitsize */
2057          FALSE,                 /* pc_relative */
2058          0,                     /* bitpos */
2059          complain_overflow_dont,/* complain_on_overflow */
2060          nds32_elf_ignore_reloc,/* special_function */
2061          "R_NDS32_PLTBLOCK",    /* name */
2062          FALSE,                 /* partial_inplace */
2063          0xffffffff,            /* src_mask */
2064          0xffffffff,            /* dst_mask */
2065          FALSE),                /* pcrel_offset */
2066   HOWTO (R_NDS32_RELAX_REGION_BEGIN,    /* type */
2067          0,                     /* rightshift */
2068          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2069          32,                    /* bitsize */
2070          FALSE,                 /* pc_relative */
2071          0,                     /* bitpos */
2072          complain_overflow_dont,/* complain_on_overflow */
2073          nds32_elf_ignore_reloc,/* special_function */
2074          "R_NDS32_RELAX_REGION_BEGIN",  /* name */
2075          FALSE,                 /* partial_inplace */
2076          0xffffffff,            /* src_mask */
2077          0xffffffff,            /* dst_mask */
2078          FALSE),                /* pcrel_offset */
2079   HOWTO (R_NDS32_RELAX_REGION_END,      /* type */
2080          0,                     /* rightshift */
2081          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2082          32,                    /* bitsize */
2083          FALSE,                 /* pc_relative */
2084          0,                     /* bitpos */
2085          complain_overflow_dont,/* complain_on_overflow */
2086          nds32_elf_ignore_reloc,/* special_function */
2087          "R_NDS32_RELAX_REGION_END",    /* name */
2088          FALSE,                 /* partial_inplace */
2089          0xffffffff,            /* src_mask */
2090          0xffffffff,            /* dst_mask */
2091          FALSE),                /* pcrel_offset */
2092   HOWTO (R_NDS32_MINUEND,       /* type */
2093          0,                     /* rightshift */
2094          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2095          32,                    /* bitsize */
2096          FALSE,                 /* pc_relative */
2097          0,                     /* bitpos */
2098          complain_overflow_dont,/* complain_on_overflow */
2099          nds32_elf_ignore_reloc,/* special_function */
2100          "R_NDS32_MINUEND",     /* name */
2101          FALSE,                 /* partial_inplace */
2102          0xffffffff,            /* src_mask */
2103          0xffffffff,            /* dst_mask */
2104          FALSE),                /* pcrel_offset */
2105   HOWTO (R_NDS32_SUBTRAHEND,    /* type */
2106          0,                     /* rightshift */
2107          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2108          32,                    /* bitsize */
2109          FALSE,                 /* pc_relative */
2110          0,                     /* bitpos */
2111          complain_overflow_dont,/* complain_on_overflow */
2112          nds32_elf_ignore_reloc,/* special_function */
2113          "R_NDS32_SUBTRAHEND",  /* name */
2114          FALSE,                 /* partial_inplace */
2115          0xffffffff,            /* src_mask */
2116          0xffffffff,            /* dst_mask */
2117          FALSE),                /* pcrel_offset */
2118   HOWTO (R_NDS32_DIFF8,         /* type */
2119          0,                     /* rightshift */
2120          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2121          8,                     /* bitsize */
2122          FALSE,                 /* pc_relative */
2123          0,                     /* bitpos */
2124          complain_overflow_dont,/* complain_on_overflow */
2125          nds32_elf_ignore_reloc,/* special_function */
2126          "R_NDS32_DIFF8",       /* name */
2127          FALSE,                 /* partial_inplace */
2128          0x000000ff,            /* src_mask */
2129          0x000000ff,            /* dst_mask */
2130          FALSE),                /* pcrel_offset */
2131   HOWTO (R_NDS32_DIFF16,        /* type */
2132          0,                     /* rightshift */
2133          1,                     /* size (0 = byte, 1 = short, 2 = long) */
2134          16,                    /* bitsize */
2135          FALSE,                 /* pc_relative */
2136          0,                     /* bitpos */
2137          complain_overflow_dont,/* complain_on_overflow */
2138          nds32_elf_ignore_reloc,/* special_function */
2139          "R_NDS32_DIFF16",      /* name */
2140          FALSE,                 /* partial_inplace */
2141          0x0000ffff,            /* src_mask */
2142          0x0000ffff,            /* dst_mask */
2143          FALSE),                /* pcrel_offset */
2144   HOWTO (R_NDS32_DIFF32,        /* type */
2145          0,                     /* rightshift */
2146          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2147          32,                    /* bitsize */
2148          FALSE,                 /* pc_relative */
2149          0,                     /* bitpos */
2150          complain_overflow_dont,/* complain_on_overflow */
2151          nds32_elf_ignore_reloc,/* special_function */
2152          "R_NDS32_DIFF32",      /* name */
2153          FALSE,                 /* partial_inplace */
2154          0xffffffff,            /* src_mask */
2155          0xffffffff,            /* dst_mask */
2156          FALSE),                /* pcrel_offset */
2157   HOWTO (R_NDS32_DIFF_ULEB128,  /* type */
2158          0,                     /* rightshift */
2159          0,                     /* size (0 = byte, 1 = short, 2 = long) */
2160          0,                     /* bitsize */
2161          FALSE,                 /* pc_relative */
2162          0,                     /* bitpos */
2163          complain_overflow_dont,/* complain_on_overflow */
2164          nds32_elf_ignore_reloc,/* special_function */
2165          "R_NDS32_DIFF_ULEB128",/* name */
2166          FALSE,                 /* partial_inplace */
2167          0xffffffff,            /* src_mask */
2168          0xffffffff,            /* dst_mask */
2169          FALSE),                /* pcrel_offset */
2170   HOWTO (R_NDS32_DATA,          /* type */
2171          0,                     /* rightshift */
2172          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2173          32,                    /* bitsize */
2174          FALSE,                 /* pc_relative */
2175          0,                     /* bitpos */
2176          complain_overflow_dont,/* complain_on_overflow */
2177          nds32_elf_ignore_reloc,/* special_function */
2178          "R_NDS32_DATA",        /* name */
2179          FALSE,                 /* partial_inplace */
2180          0xffffffff,            /* src_mask */
2181          0xffffffff,            /* dst_mask */
2182          FALSE),                /* pcrel_offset */
2183   HOWTO (R_NDS32_TRAN,          /* type */
2184          0,                     /* rightshift */
2185          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2186          32,                    /* bitsize */
2187          FALSE,                 /* pc_relative */
2188          0,                     /* bitpos */
2189          complain_overflow_dont,/* complain_on_overflow */
2190          nds32_elf_ignore_reloc,/* special_function */
2191          "R_NDS32_TRAN",        /* name */
2192          FALSE,                 /* partial_inplace */
2193          0xffffffff,            /* src_mask */
2194          0xffffffff,            /* dst_mask */
2195          FALSE),                /* pcrel_offset */
2196   HOWTO (R_NDS32_TLS_LE_ADD,    /* type */
2197          0,                     /* rightshift */
2198          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2199          32,                    /* bitsize */
2200          FALSE,                 /* pc_relative */
2201          0,                     /* bitpos */
2202          complain_overflow_dont,        /* complain_on_overflow */
2203          nds32_elf_ignore_reloc,        /* special_function */
2204          "R_NDS32_TLS_LE_ADD",  /* name */
2205          FALSE,                 /* partial_inplace */
2206          0xffffffff,            /* src_mask */
2207          0xffffffff,            /* dst_mask */
2208          FALSE),                /* pcrel_offset */
2209   HOWTO (R_NDS32_TLS_LE_LS,     /* type */
2210          0,                     /* rightshift */
2211          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2212          32,                    /* bitsize */
2213          FALSE,                 /* pc_relative */
2214          0,                     /* bitpos */
2215          complain_overflow_dont,        /* complain_on_overflow */
2216          nds32_elf_ignore_reloc,        /* special_function */
2217          "R_NDS32_TLS_LE_LS",   /* name */
2218          FALSE,                 /* partial_inplace */
2219          0xffffffff,            /* src_mask */
2220          0xffffffff,            /* dst_mask */
2221          FALSE),                /* pcrel_offset */
2222   HOWTO (R_NDS32_EMPTY,         /* type */
2223          0,                     /* rightshift */
2224          2,                     /* size (0 = byte, 1 = short, 2 = long) */
2225          32,                    /* bitsize */
2226          FALSE,                 /* pc_relative */
2227          0,                     /* bitpos */
2228          complain_overflow_dont,        /* complain_on_overflow */
2229          nds32_elf_ignore_reloc,        /* special_function */
2230          "R_NDS32_EMPTY",               /* name */
2231          FALSE,                 /* partial_inplace */
2232          0xffffffff,            /* src_mask */
2233          0xffffffff,            /* dst_mask */
2234          FALSE),                /* pcrel_offset */
2235 };
2236
2237 \f
2238 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2239    This prototype is the same as qsort ().  */
2240
2241 void
2242 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2243                       int (*compar) (const void *lhs, const void *rhs))
2244 {
2245   char *ptr = (char *) base;
2246   int i, j;
2247   char *tmp = xmalloc (size);
2248
2249   /* If i is less than j, i is inserted before j.
2250
2251      |---- j ----- i --------------|
2252       \          / \              /
2253          sorted         unsorted
2254    */
2255
2256   for (i = 1; i < (int) nmemb; i++)
2257     {
2258       for (j = (i - 1); j >= 0; j--)
2259         if (compar (ptr + i * size, ptr + j * size) >= 0)
2260           break;
2261
2262       j++;
2263
2264       if (i == j)
2265         continue; /* i is in order.  */
2266
2267       memcpy (tmp, ptr + i * size, size);
2268       memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2269       memcpy (ptr + j * size, tmp, size);
2270     }
2271   free (tmp);
2272 }
2273
2274 /* Sort relocation by r_offset.
2275
2276    We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2277    algorithm.  Relocations at the same r_offset must keep their order.
2278    For example, RELAX_ENTRY must be the very first relocation entry.
2279
2280    Currently, this function implements insertion-sort.
2281
2282    FIXME: If we already sort them in assembler, why bother sort them
2283           here again?  */
2284
2285 static int
2286 compar_reloc (const void *lhs, const void *rhs)
2287 {
2288   const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2289   const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2290
2291   if (l->r_offset > r->r_offset)
2292     return 1;
2293   else if (l->r_offset == r->r_offset)
2294     return 0;
2295   else
2296     return -1;
2297 }
2298
2299 /* Functions listed below are only used for old relocs.
2300    * nds32_elf_9_pcrel_reloc
2301    * nds32_elf_do_9_pcrel_reloc
2302    * nds32_elf_hi20_reloc
2303    * nds32_elf_relocate_hi20
2304    * nds32_elf_lo12_reloc
2305    * nds32_elf_sda15_reloc
2306    * nds32_elf_generic_reloc
2307    */
2308
2309 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
2310
2311 static bfd_reloc_status_type
2312 nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2313                          void *data, asection *input_section, bfd *output_bfd,
2314                          char **error_message ATTRIBUTE_UNUSED)
2315 {
2316   /* This part is from bfd_elf_generic_reloc.  */
2317   if (output_bfd != (bfd *) NULL
2318       && (symbol->flags & BSF_SECTION_SYM) == 0
2319       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2320     {
2321       reloc_entry->address += input_section->output_offset;
2322       return bfd_reloc_ok;
2323     }
2324
2325   if (output_bfd != NULL)
2326     {
2327       /* FIXME: See bfd_perform_relocation.  Is this right?  */
2328       return bfd_reloc_continue;
2329     }
2330
2331   return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2332                                      input_section,
2333                                      data, reloc_entry->address,
2334                                      symbol->section,
2335                                      (symbol->value
2336                                       + symbol->section->output_section->vma
2337                                       + symbol->section->output_offset),
2338                                      reloc_entry->addend);
2339 }
2340
2341 /* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
2342 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2343
2344 static bfd_reloc_status_type
2345 nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
2346                             asection *input_section, bfd_byte *data,
2347                             bfd_vma offset,
2348                             asection *symbol_section ATTRIBUTE_UNUSED,
2349                             bfd_vma symbol_value, bfd_vma addend)
2350 {
2351   bfd_signed_vma relocation;
2352   unsigned short x;
2353   bfd_reloc_status_type status;
2354
2355   /* Sanity check the address (offset in section).  */
2356   if (offset > bfd_get_section_limit (abfd, input_section))
2357     return bfd_reloc_outofrange;
2358
2359   relocation = symbol_value + addend;
2360   /* Make it pc relative.  */
2361   relocation -= (input_section->output_section->vma
2362                  + input_section->output_offset);
2363   /* These jumps mask off the lower two bits of the current address
2364      before doing pcrel calculations.  */
2365   relocation -= (offset & -(bfd_vma) 2);
2366
2367   if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2368     status = bfd_reloc_overflow;
2369   else
2370     status = bfd_reloc_ok;
2371
2372   x = bfd_getb16 (data + offset);
2373
2374   relocation >>= howto->rightshift;
2375   relocation <<= howto->bitpos;
2376   x = (x & ~howto->dst_mask)
2377       | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2378
2379   bfd_putb16 ((bfd_vma) x, data + offset);
2380
2381   return status;
2382 }
2383
2384 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2385    HI20_SLO is for the add3 and load/store with displacement instructions.
2386    HI20 is for the or3 instruction.
2387    For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2388    the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2389    we must add one to the high 16 bytes (which will get subtracted off when
2390    the low 16 bits are added).
2391    These relocs have to be done in combination with an R_NDS32_LO12 reloc
2392    because there is a carry from the LO12 to the HI20.  Here we just save
2393    the information we need; we do the actual relocation when we see the LO12.
2394    This code is copied from the elf32-mips.c.  We also support an arbitrary
2395    number of HI20 relocs to be associated with a single LO12 reloc.  The
2396    assembler sorts the relocs to ensure each HI20 immediately precedes its
2397    LO12.  However if there are multiple copies, the assembler may not find
2398    the real LO12 so it picks the first one it finds.  */
2399
2400 struct nds32_hi20
2401 {
2402   struct nds32_hi20 *next;
2403   bfd_byte *addr;
2404   bfd_vma addend;
2405 };
2406
2407 static struct nds32_hi20 *nds32_hi20_list;
2408
2409 static bfd_reloc_status_type
2410 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2411                       asymbol *symbol, void *data, asection *input_section,
2412                       bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2413 {
2414   bfd_reloc_status_type ret;
2415   bfd_vma relocation;
2416   struct nds32_hi20 *n;
2417
2418   /* This part is from bfd_elf_generic_reloc.
2419      If we're relocating, and this an external symbol, we don't want
2420      to change anything.  */
2421   if (output_bfd != (bfd *) NULL
2422       && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2423     {
2424       reloc_entry->address += input_section->output_offset;
2425       return bfd_reloc_ok;
2426     }
2427
2428   /* Sanity check the address (offset in section).  */
2429   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2430     return bfd_reloc_outofrange;
2431
2432   ret = bfd_reloc_ok;
2433   if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2434     ret = bfd_reloc_undefined;
2435
2436   if (bfd_is_com_section (symbol->section))
2437     relocation = 0;
2438   else
2439     relocation = symbol->value;
2440
2441   relocation += symbol->section->output_section->vma;
2442   relocation += symbol->section->output_offset;
2443   relocation += reloc_entry->addend;
2444
2445   /* Save the information, and let LO12 do the actual relocation.  */
2446   n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2447   if (n == NULL)
2448     return bfd_reloc_outofrange;
2449
2450   n->addr = (bfd_byte *) data + reloc_entry->address;
2451   n->addend = relocation;
2452   n->next = nds32_hi20_list;
2453   nds32_hi20_list = n;
2454
2455   if (output_bfd != (bfd *) NULL)
2456     reloc_entry->address += input_section->output_offset;
2457
2458   return ret;
2459 }
2460
2461 /* Handle an NDS32 ELF HI20 reloc.  */
2462
2463 static void
2464 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2465                          int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
2466                          Elf_Internal_Rela *rello, bfd_byte *contents,
2467                          bfd_vma addend)
2468 {
2469   unsigned long insn;
2470   bfd_vma addlo;
2471
2472   insn = bfd_getb32 (contents + relhi->r_offset);
2473
2474   addlo = bfd_getb32 (contents + rello->r_offset);
2475   addlo &= 0xfff;
2476
2477   addend += ((insn & 0xfffff) << 20) + addlo;
2478
2479   insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2480   bfd_putb32 (insn, contents + relhi->r_offset);
2481 }
2482
2483 /* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
2484    inplace relocation; this function exists in order to do the
2485    R_NDS32_HI20_[SU]LO relocation described above.  */
2486
2487 static bfd_reloc_status_type
2488 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2489                       void *data, asection *input_section, bfd *output_bfd,
2490                       char **error_message)
2491 {
2492   /* This part is from bfd_elf_generic_reloc.
2493      If we're relocating, and this an external symbol, we don't want
2494      to change anything.  */
2495   if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2496       && reloc_entry->addend == 0)
2497     {
2498       reloc_entry->address += input_section->output_offset;
2499       return bfd_reloc_ok;
2500     }
2501
2502   if (nds32_hi20_list != NULL)
2503     {
2504       struct nds32_hi20 *l;
2505
2506       l = nds32_hi20_list;
2507       while (l != NULL)
2508         {
2509           unsigned long insn;
2510           unsigned long val;
2511           unsigned long vallo;
2512           struct nds32_hi20 *next;
2513
2514           /* Do the HI20 relocation.  Note that we actually don't need
2515              to know anything about the LO12 itself, except where to
2516              find the low 12 bits of the addend needed by the LO12.  */
2517           insn = bfd_getb32 (l->addr);
2518           vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2519           vallo &= 0xfff;
2520           switch (reloc_entry->howto->type)
2521             {
2522             case R_NDS32_LO12S3:
2523               vallo <<= 3;
2524               break;
2525
2526             case R_NDS32_LO12S2:
2527               vallo <<= 2;
2528               break;
2529
2530             case R_NDS32_LO12S1:
2531               vallo <<= 1;
2532               break;
2533
2534             case R_NDS32_LO12S0:
2535               vallo <<= 0;
2536               break;
2537             }
2538
2539           val = ((insn & 0xfffff) << 12) + vallo;
2540           val += l->addend;
2541
2542           insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2543           bfd_putb32 ((bfd_vma) insn, l->addr);
2544
2545           next = l->next;
2546           free (l);
2547           l = next;
2548         }
2549
2550       nds32_hi20_list = NULL;
2551     }
2552
2553   /* Now do the LO12 reloc in the usual way.
2554      ??? It would be nice to call bfd_elf_generic_reloc here,
2555      but we have partial_inplace set.  bfd_elf_generic_reloc will
2556      pass the handling back to bfd_install_relocation which will install
2557      a section relative addend which is wrong.  */
2558   return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2559                                   input_section, output_bfd, error_message);
2560 }
2561
2562 /* Do generic partial_inplace relocation.
2563    This is a local replacement for bfd_elf_generic_reloc.  */
2564
2565 static bfd_reloc_status_type
2566 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2567                          asymbol *symbol, void *data, asection *input_section,
2568                          bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2569 {
2570   bfd_reloc_status_type ret;
2571   bfd_vma relocation;
2572   bfd_byte *inplace_address;
2573
2574   /* This part is from bfd_elf_generic_reloc.
2575      If we're relocating, and this an external symbol, we don't want
2576      to change anything.  */
2577   if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2578       && reloc_entry->addend == 0)
2579     {
2580       reloc_entry->address += input_section->output_offset;
2581       return bfd_reloc_ok;
2582     }
2583
2584   /* Now do the reloc in the usual way.
2585      ??? It would be nice to call bfd_elf_generic_reloc here,
2586      but we have partial_inplace set.  bfd_elf_generic_reloc will
2587      pass the handling back to bfd_install_relocation which will install
2588      a section relative addend which is wrong.  */
2589
2590   /* Sanity check the address (offset in section).  */
2591   if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2592     return bfd_reloc_outofrange;
2593
2594   ret = bfd_reloc_ok;
2595   if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2596     ret = bfd_reloc_undefined;
2597
2598   if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2599     relocation = 0;
2600   else
2601     relocation = symbol->value;
2602
2603   /* Only do this for a final link.  */
2604   if (output_bfd == (bfd *) NULL)
2605     {
2606       relocation += symbol->section->output_section->vma;
2607       relocation += symbol->section->output_offset;
2608     }
2609
2610   relocation += reloc_entry->addend;
2611   switch (reloc_entry->howto->type)
2612     {
2613     case R_NDS32_LO12S3:
2614       relocation >>= 3;
2615       break;
2616
2617     case R_NDS32_LO12S2:
2618       relocation >>= 2;
2619       break;
2620
2621     case R_NDS32_LO12S1:
2622       relocation >>= 1;
2623       break;
2624
2625     case R_NDS32_LO12S0:
2626     default:
2627       relocation >>= 0;
2628       break;
2629     }
2630
2631   inplace_address = (bfd_byte *) data + reloc_entry->address;
2632
2633 #define DOIT(x)                                         \
2634   x = ((x & ~reloc_entry->howto->dst_mask) |            \
2635   (((x & reloc_entry->howto->src_mask) +  relocation) & \
2636   reloc_entry->howto->dst_mask))
2637
2638   switch (reloc_entry->howto->size)
2639     {
2640     case 1:
2641       {
2642         short x = bfd_getb16 (inplace_address);
2643
2644         DOIT (x);
2645         bfd_putb16 ((bfd_vma) x, inplace_address);
2646       }
2647       break;
2648     case 2:
2649       {
2650         unsigned long x = bfd_getb32 (inplace_address);
2651
2652         DOIT (x);
2653         bfd_putb32 ((bfd_vma) x, inplace_address);
2654       }
2655       break;
2656     default:
2657       BFD_ASSERT (0);
2658     }
2659
2660   if (output_bfd != (bfd *) NULL)
2661     reloc_entry->address += input_section->output_offset;
2662
2663   return ret;
2664 }
2665
2666 /* Handle the R_NDS32_SDA15 reloc.
2667    This reloc is used to compute the address of objects in the small data area
2668    and to perform loads and stores from that area.
2669    The lower 15 bits are sign extended and added to the register specified
2670    in the instruction, which is assumed to point to _SDA_BASE_.
2671
2672    Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2673    the access size, this must be taken care of.  */
2674
2675 static bfd_reloc_status_type
2676 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2677                        asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2678                        asection *input_section, bfd *output_bfd,
2679                        char **error_message ATTRIBUTE_UNUSED)
2680 {
2681   /* This part is from bfd_elf_generic_reloc.  */
2682   if (output_bfd != (bfd *) NULL
2683       && (symbol->flags & BSF_SECTION_SYM) == 0
2684       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2685     {
2686       reloc_entry->address += input_section->output_offset;
2687       return bfd_reloc_ok;
2688     }
2689
2690   if (output_bfd != NULL)
2691     {
2692       /* FIXME: See bfd_perform_relocation.  Is this right?  */
2693       return bfd_reloc_continue;
2694     }
2695
2696   /* FIXME: not sure what to do here yet.  But then again, the linker
2697      may never call us.  */
2698   abort ();
2699 }
2700
2701 /* nds32_elf_ignore_reloc is the special function for
2702    relocation types which don't need to be relocated
2703    like relaxation relocation types.
2704    This function simply return bfd_reloc_ok when it is
2705    invoked.  */
2706
2707 static bfd_reloc_status_type
2708 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2709                         asymbol *symbol ATTRIBUTE_UNUSED,
2710                         void *data ATTRIBUTE_UNUSED, asection *input_section,
2711                         bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2712 {
2713   if (output_bfd != NULL)
2714     reloc_entry->address += input_section->output_offset;
2715
2716   return bfd_reloc_ok;
2717 }
2718 \f
2719
2720 /* Map BFD reloc types to NDS32 ELF reloc types.  */
2721
2722 struct nds32_reloc_map_entry
2723 {
2724   bfd_reloc_code_real_type bfd_reloc_val;
2725   unsigned char elf_reloc_val;
2726 };
2727
2728 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
2729 {
2730   {BFD_RELOC_NONE, R_NDS32_NONE},
2731   {BFD_RELOC_16, R_NDS32_16_RELA},
2732   {BFD_RELOC_32, R_NDS32_32_RELA},
2733   {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
2734   {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
2735   {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
2736   {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
2737   {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
2738   {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
2739   {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
2740   {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
2741   {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
2742   {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
2743   {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
2744   {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
2745   {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
2746   {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
2747   {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
2748   {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
2749   {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
2750   {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
2751   {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
2752   {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
2753
2754   {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
2755   {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
2756   {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
2757   {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
2758   {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
2759   {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
2760   {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
2761   {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
2762   {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
2763   {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
2764   {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
2765   {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
2766   {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
2767   {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
2768   {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
2769   {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
2770   {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
2771   {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
2772   {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
2773   {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
2774   {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
2775   {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
2776   {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
2777   {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
2778   {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
2779   {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
2780   {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
2781   {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
2782   {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
2783   {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
2784   {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
2785   {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
2786   {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
2787   {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
2788   {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
2789   {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
2790   {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
2791   {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
2792   {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
2793   {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
2794   {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
2795   {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
2796   {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
2797   {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
2798   {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
2799   {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
2800   {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
2801   {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
2802   {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
2803   {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
2804   {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
2805   {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
2806   {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
2807   {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
2808   {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
2809   {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
2810   {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
2811   {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
2812   {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
2813   {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
2814   {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
2815   {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
2816   {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
2817   {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
2818   {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
2819   {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
2820   {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
2821   {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
2822   {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
2823   {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
2824   {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
2825   {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
2826   {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
2827   {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
2828   {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
2829
2830   {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
2831   {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
2832   {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
2833   {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
2834   {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
2835   {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
2836   {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
2837   {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
2838   {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
2839   {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
2840   {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
2841   {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
2842   {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
2843   {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
2844   {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
2845   {BFD_RELOC_NDS32_TLS_TPOFF, R_NDS32_TLS_TPOFF},
2846   {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
2847   {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
2848   {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
2849   {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
2850 };
2851
2852 /* Patch tag.  */
2853
2854 static reloc_howto_type *
2855 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2856                                  const char *r_name)
2857 {
2858   unsigned int i;
2859
2860   for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
2861     if (nds32_elf_howto_table[i].name != NULL
2862         && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
2863       return &nds32_elf_howto_table[i];
2864
2865   for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
2866     if (nds32_elf_relax_howto_table[i].name != NULL
2867         && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
2868       return &nds32_elf_relax_howto_table[i];
2869
2870   return NULL;
2871 }
2872
2873 static reloc_howto_type *
2874 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
2875 {
2876   if (code < R_NDS32_RELAX_ENTRY)
2877     {
2878       BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
2879       return &nds32_elf_howto_table[code];
2880     }
2881   else
2882     {
2883       BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
2884                   < ARRAY_SIZE (nds32_elf_relax_howto_table));
2885       return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
2886     }
2887 }
2888
2889 static reloc_howto_type *
2890 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2891                                  bfd_reloc_code_real_type code)
2892 {
2893   unsigned int i;
2894
2895   for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
2896     {
2897       if (nds32_reloc_map[i].bfd_reloc_val == code)
2898         return bfd_elf32_bfd_reloc_type_table_lookup
2899                  (nds32_reloc_map[i].elf_reloc_val);
2900     }
2901
2902   return NULL;
2903 }
2904
2905 /* Set the howto pointer for an NDS32 ELF reloc.  */
2906
2907 static void
2908 nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2909                          Elf_Internal_Rela *dst)
2910 {
2911   enum elf_nds32_reloc_type r_type;
2912
2913   r_type = ELF32_R_TYPE (dst->r_info);
2914   if (r_type > R_NDS32_GNU_VTENTRY)
2915     {
2916       /* xgettext:c-format */
2917       _bfd_error_handler (_("%B: invalid NDS32 reloc number: %d"), abfd, r_type);
2918       r_type = 0;
2919     }
2920   cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
2921 }
2922
2923 static void
2924 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2925                      Elf_Internal_Rela *dst)
2926 {
2927   BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
2928               || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
2929                   && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
2930   cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
2931 }
2932
2933 /* Support for core dump NOTE sections.
2934    Reference to include/linux/elfcore.h in Linux.  */
2935
2936 static bfd_boolean
2937 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2938 {
2939   int offset;
2940   size_t size;
2941
2942   switch (note->descsz)
2943     {
2944     case 0x114:
2945       /* Linux/NDS32 32-bit, ABI1 */
2946
2947       /* pr_cursig */
2948       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2949
2950       /* pr_pid */
2951       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
2952
2953       /* pr_reg */
2954       offset = 72;
2955       size = 200;
2956       break;
2957
2958     case 0xfc:
2959       /* Linux/NDS32 32-bit */
2960
2961       /* pr_cursig */
2962       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2963
2964       /* pr_pid */
2965       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
2966
2967       /* pr_reg */
2968       offset = 72;
2969       size = 176;
2970       break;
2971
2972     default:
2973       return FALSE;
2974     }
2975
2976   /* Make a ".reg" section.  */
2977   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2978                                           size, note->descpos + offset);
2979 }
2980
2981 static bfd_boolean
2982 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2983 {
2984   switch (note->descsz)
2985     {
2986     case 124:
2987       /* Linux/NDS32 */
2988
2989       /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
2990       elf_tdata (abfd)->core->program =
2991         _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
2992       elf_tdata (abfd)->core->command =
2993         _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2994       break;
2995
2996     default:
2997       return FALSE;
2998     }
2999
3000   /* Note that for some reason, a spurious space is tacked
3001      onto the end of the args in some (at least one anyway)
3002      implementations, so strip it off if it exists.  */
3003   {
3004     char *command = elf_tdata (abfd)->core->command;
3005     int n = strlen (command);
3006
3007     if (0 < n && command[n - 1] == ' ')
3008       command[n - 1] = '\0';
3009   }
3010
3011   return TRUE;
3012 }
3013
3014 /* Hook called by the linker routine which adds symbols from an object
3015    file.  We must handle the special NDS32 section numbers here.
3016    We also keep watching for whether we need to create the sdata special
3017    linker sections.  */
3018
3019 static bfd_boolean
3020 nds32_elf_add_symbol_hook (bfd *abfd,
3021                            struct bfd_link_info *info ATTRIBUTE_UNUSED,
3022                            Elf_Internal_Sym *sym,
3023                            const char **namep ATTRIBUTE_UNUSED,
3024                            flagword *flagsp ATTRIBUTE_UNUSED,
3025                            asection **secp, bfd_vma *valp)
3026 {
3027   switch (sym->st_shndx)
3028     {
3029     case SHN_COMMON:
3030       /* Common symbols less than the GP size are automatically
3031          treated as SHN_MIPS_SCOMMON symbols.  */
3032       if (sym->st_size > elf_gp_size (abfd)
3033           || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3034         break;
3035
3036       /* st_value is the alignemnt constraint.
3037          That might be its actual size if it is an array or structure.  */
3038       switch (sym->st_value)
3039         {
3040         case 1:
3041           *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3042           break;
3043         case 2:
3044           *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3045           break;
3046         case 4:
3047           *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3048           break;
3049         case 8:
3050           *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3051           break;
3052         default:
3053           return TRUE;
3054         }
3055
3056       (*secp)->flags |= SEC_IS_COMMON;
3057       *valp = sym->st_size;
3058       break;
3059     }
3060
3061   return TRUE;
3062 }
3063
3064
3065 /* This function can figure out the best location for a base register to access
3066    data relative to this base register
3067    INPUT:
3068    sda_d0: size of first DOUBLE WORD data section
3069    sda_w0: size of first WORD data section
3070    sda_h0: size of first HALF WORD data section
3071    sda_b : size of BYTE data section
3072    sda_hi: size of second HALF WORD data section
3073    sda_w1: size of second WORD data section
3074    sda_d1: size of second DOUBLE WORD data section
3075    OUTPUT:
3076    offset (always positive) from the beginning of sda_d0 if OK
3077    a negative error value if fail
3078    NOTE:
3079    these 7 sections have to be located back to back if exist
3080    a pass in 0 value for non-existing section   */
3081
3082 /* Due to the interpretation of simm15 field of load/store depending on
3083    data accessing size, the organization of base register relative data shall
3084    like the following figure
3085    -------------------------------------------
3086    |  DOUBLE WORD sized data (range +/- 128K)
3087    -------------------------------------------
3088    |  WORD sized data (range +/- 64K)
3089    -------------------------------------------
3090    |  HALF WORD sized data (range +/- 32K)
3091    -------------------------------------------
3092    |  BYTE sized data (range +/- 16K)
3093    -------------------------------------------
3094    |  HALF WORD sized data (range +/- 32K)
3095    -------------------------------------------
3096    |  WORD sized data (range +/- 64K)
3097    -------------------------------------------
3098    |  DOUBLE WORD sized data (range +/- 128K)
3099    -------------------------------------------
3100    Its base register shall be set to access these data freely.  */
3101
3102 /* We have to figure out the SDA_BASE value, so that we can adjust the
3103    symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3104    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3105    target data.  We don't need to adjust the symbol value for an
3106    external symbol if we are producing relocatable output.  */
3107
3108 static asection *sda_rela_sec = NULL;
3109
3110 #define SDA_SECTION_NUM 10
3111
3112 static bfd_reloc_status_type
3113 nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
3114                           bfd_vma *psb, bfd_boolean add_symbol)
3115 {
3116   int relax_fp_as_gp;
3117   struct elf_nds32_link_hash_table *table;
3118   struct bfd_link_hash_entry *h, *h2;
3119   long unsigned int total = 0;
3120
3121   h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3122   if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
3123     {
3124       asection *first = NULL, *final = NULL, *temp;
3125       bfd_vma sda_base;
3126       /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3127          4 byte-aligned.  Therefore, it has to set the first section ".data"
3128          4 byte-aligned.  */
3129       static const char sec_name[SDA_SECTION_NUM][10] =
3130         {
3131           ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3132           ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3133         };
3134       size_t i = 0;
3135
3136       if (output_bfd->sections == NULL)
3137         {
3138           *psb = elf_gp (output_bfd);
3139           return bfd_reloc_ok;
3140         }
3141
3142       /* Get the first and final section.  */
3143       while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3144         {
3145           temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3146           if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3147             first = temp;
3148           if (temp && (temp->size != 0 || temp->rawsize != 0))
3149             final = temp;
3150
3151           /* Summarize the sections in order to check if joining .bss.  */
3152           if (temp && temp->size != 0)
3153             total += temp->size;
3154           else if (temp && temp->rawsize != 0)
3155             total += temp->rawsize;
3156
3157           i++;
3158         }
3159
3160       /* Check .bss size.  */
3161       temp = bfd_get_section_by_name (output_bfd, ".bss");
3162       if (temp)
3163         {
3164           if (temp->size != 0)
3165             total += temp->size;
3166           else if (temp->rawsize != 0)
3167             total += temp->rawsize;
3168
3169           if (total < 0x80000)
3170             {
3171               if (!first && (temp->size != 0 || temp->rawsize != 0))
3172                 first = temp;
3173               if ((temp->size != 0 || temp->rawsize != 0))
3174                 final = temp;
3175             }
3176         }
3177
3178       if (first && final)
3179         {
3180           /* The middle of data region.  */
3181           sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3182
3183           /* Find the section sda_base located.  */
3184           i = 0;
3185           while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3186             {
3187               final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3188               if (final && (final->size != 0 || final->rawsize != 0)
3189                   && sda_base >= final->vma)
3190                 {
3191                   first = final;
3192                   i++;
3193                 }
3194               else
3195                 break;
3196             }
3197         }
3198       else
3199         {
3200           /* There is not any data section in output bfd, and set _SDA_BASE_ in
3201              first output section.  */
3202           first = output_bfd->sections;
3203           while (first && first->size == 0 && first->rawsize == 0)
3204             first = first->next;
3205           if (!first)
3206             {
3207               *psb = elf_gp (output_bfd);
3208               return bfd_reloc_ok;
3209             }
3210           sda_base = first->vma + first->rawsize;
3211         }
3212
3213       sda_base -= first->vma;
3214       sda_base = sda_base & (~7);
3215
3216       if (!_bfd_generic_link_add_one_symbol
3217              (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3218               (bfd_vma) sda_base, (const char *) NULL, FALSE,
3219               get_elf_backend_data (output_bfd)->collect, &h))
3220         return FALSE;
3221
3222       sda_rela_sec = first;
3223
3224       table = nds32_elf_hash_table (info);
3225       relax_fp_as_gp = table->relax_fp_as_gp;
3226       if (relax_fp_as_gp)
3227         {
3228           h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
3229                                      FALSE, FALSE, FALSE);
3230           /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3231              And set FP equal to SDA_BASE to do relaxation for
3232              la $fp, _FP_BASE_.  */
3233           if (!_bfd_generic_link_add_one_symbol
3234                  (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3235                   first, (bfd_vma) sda_base, (const char *) NULL,
3236                   FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3237             return FALSE;
3238         }
3239     }
3240
3241   if (add_symbol)
3242     {
3243       if (h)
3244         {
3245           /* Now set gp.  */
3246           elf_gp (output_bfd) = (h->u.def.value
3247                                  + h->u.def.section->output_section->vma
3248                                  + h->u.def.section->output_offset);
3249         }
3250       else
3251         {
3252           _bfd_error_handler (_("error: Can't find symbol: _SDA_BASE_."));
3253           return bfd_reloc_dangerous;
3254         }
3255     }
3256
3257   *psb = h->u.def.value + h->u.def.section->output_section->vma
3258          + h->u.def.section->output_offset;
3259   return bfd_reloc_ok;
3260 }
3261 \f
3262
3263 /* Return size of a PLT entry.  */
3264 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3265
3266
3267 /* Create an entry in an nds32 ELF linker hash table.  */
3268
3269 static struct bfd_hash_entry *
3270 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3271                              struct bfd_hash_table *table,
3272                              const char *string)
3273 {
3274   struct elf_nds32_link_hash_entry *ret;
3275
3276   ret = (struct elf_nds32_link_hash_entry *) entry;
3277
3278   /* Allocate the structure if it has not already been allocated by a
3279      subclass.  */
3280   if (ret == NULL)
3281     ret = (struct elf_nds32_link_hash_entry *)
3282        bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3283
3284   if (ret == NULL)
3285     return (struct bfd_hash_entry *) ret;
3286
3287   /* Call the allocation method of the superclass.  */
3288   ret = (struct elf_nds32_link_hash_entry *)
3289     _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3290
3291   if (ret != NULL)
3292     {
3293       struct elf_nds32_link_hash_entry *eh;
3294
3295       eh = (struct elf_nds32_link_hash_entry *) ret;
3296       eh->dyn_relocs = NULL;
3297       eh->tls_type = GOT_UNKNOWN;
3298     }
3299
3300   return (struct bfd_hash_entry *) ret;
3301 }
3302
3303 /* Create an nds32 ELF linker hash table.  */
3304
3305 static struct bfd_link_hash_table *
3306 nds32_elf_link_hash_table_create (bfd *abfd)
3307 {
3308   struct elf_nds32_link_hash_table *ret;
3309
3310   bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3311
3312   ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3313   if (ret == NULL)
3314     return NULL;
3315
3316   /* patch tag.  */
3317   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3318                                       nds32_elf_link_hash_newfunc,
3319                                       sizeof (struct elf_nds32_link_hash_entry),
3320                                       NDS32_ELF_DATA))
3321     {
3322       free (ret);
3323       return NULL;
3324     }
3325
3326   return &ret->root.root;
3327 }
3328
3329 /* Create dynamic sections when linking against a dynamic object.  */
3330
3331 static bfd_boolean
3332 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3333 {
3334   struct elf_nds32_link_hash_table *htab;
3335   flagword flags, pltflags;
3336   register asection *s;
3337   const struct elf_backend_data *bed;
3338   int ptralign = 2;             /* 32-bit  */
3339
3340   bed = get_elf_backend_data (abfd);
3341
3342   htab = nds32_elf_hash_table (info);
3343
3344   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3345      .rel[a].bss sections.  */
3346
3347   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3348            | SEC_LINKER_CREATED);
3349
3350   pltflags = flags;
3351   pltflags |= SEC_CODE;
3352   if (bed->plt_not_loaded)
3353     pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3354   if (bed->plt_readonly)
3355     pltflags |= SEC_READONLY;
3356
3357   s = bfd_make_section (abfd, ".plt");
3358   htab->root.splt = s;
3359   if (s == NULL
3360       || !bfd_set_section_flags (abfd, s, pltflags)
3361       || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3362     return FALSE;
3363
3364   if (bed->want_plt_sym)
3365     {
3366       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3367          .plt section.  */
3368       struct bfd_link_hash_entry *bh = NULL;
3369       struct elf_link_hash_entry *h;
3370
3371       if (!(_bfd_generic_link_add_one_symbol
3372             (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3373              (bfd_vma) 0, (const char *) NULL, FALSE,
3374              get_elf_backend_data (abfd)->collect, &bh)))
3375         return FALSE;
3376
3377       h = (struct elf_link_hash_entry *) bh;
3378       h->def_regular = 1;
3379       h->type = STT_OBJECT;
3380
3381       if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3382         return FALSE;
3383     }
3384
3385   s = bfd_make_section (abfd,
3386                         bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3387   htab->root.srelplt = s;
3388   if (s == NULL
3389       || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3390       || !bfd_set_section_alignment (abfd, s, ptralign))
3391     return FALSE;
3392
3393   if (htab->root.sgot == NULL && !_bfd_elf_create_got_section (abfd, info))
3394     return FALSE;
3395
3396   {
3397     const char *secname;
3398     char *relname;
3399     flagword secflags;
3400     asection *sec;
3401
3402     for (sec = abfd->sections; sec; sec = sec->next)
3403       {
3404         secflags = bfd_get_section_flags (abfd, sec);
3405         if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3406             || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3407           continue;
3408         secname = bfd_get_section_name (abfd, sec);
3409         relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3410         strcpy (relname, ".rela");
3411         strcat (relname, secname);
3412         if (bfd_get_section_by_name (abfd, secname))
3413           continue;
3414         s = bfd_make_section (abfd, relname);
3415         if (s == NULL
3416             || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3417             || !bfd_set_section_alignment (abfd, s, ptralign))
3418           return FALSE;
3419       }
3420   }
3421
3422   if (bed->want_dynbss)
3423     {
3424       /* The .dynbss section is a place to put symbols which are defined
3425          by dynamic objects, are referenced by regular objects, and are
3426          not functions.  We must allocate space for them in the process
3427          image and use a R_*_COPY reloc to tell the dynamic linker to
3428          initialize them at run time.  The linker script puts the .dynbss
3429          section into the .bss section of the final image.  */
3430       s = bfd_make_section (abfd, ".dynbss");
3431       htab->sdynbss = s;
3432       if (s == NULL
3433           || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3434         return FALSE;
3435       /* The .rel[a].bss section holds copy relocs.  This section is not
3436          normally needed.  We need to create it here, though, so that the
3437          linker will map it to an output section.  We can't just create it
3438          only if we need it, because we will not know whether we need it
3439          until we have seen all the input files, and the first time the
3440          main linker code calls BFD after examining all the input files
3441          (size_dynamic_sections) the input sections have already been
3442          mapped to the output sections.  If the section turns out not to
3443          be needed, we can discard it later.  We will never need this
3444          section when generating a shared object, since they do not use
3445          copy relocs.  */
3446       if (!bfd_link_pic (info))
3447         {
3448           s = bfd_make_section (abfd, (bed->default_use_rela_p
3449                                        ? ".rela.bss" : ".rel.bss"));
3450           htab->srelbss = s;
3451           if (s == NULL
3452               || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3453               || !bfd_set_section_alignment (abfd, s, ptralign))
3454             return FALSE;
3455         }
3456     }
3457
3458   return TRUE;
3459 }
3460
3461 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
3462 static void
3463 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3464                                 struct elf_link_hash_entry *dir,
3465                                 struct elf_link_hash_entry *ind)
3466 {
3467   struct elf_nds32_link_hash_entry *edir, *eind;
3468
3469   edir = (struct elf_nds32_link_hash_entry *) dir;
3470   eind = (struct elf_nds32_link_hash_entry *) ind;
3471
3472   if (eind->dyn_relocs != NULL)
3473     {
3474       if (edir->dyn_relocs != NULL)
3475         {
3476           struct elf_nds32_dyn_relocs **pp;
3477           struct elf_nds32_dyn_relocs *p;
3478
3479           if (ind->root.type == bfd_link_hash_indirect)
3480             abort ();
3481
3482           /* Add reloc counts against the weak sym to the strong sym
3483              list.  Merge any entries against the same section.  */
3484           for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3485             {
3486               struct elf_nds32_dyn_relocs *q;
3487
3488               for (q = edir->dyn_relocs; q != NULL; q = q->next)
3489                 if (q->sec == p->sec)
3490                   {
3491                     q->pc_count += p->pc_count;
3492                     q->count += p->count;
3493                     *pp = p->next;
3494                     break;
3495                   }
3496               if (q == NULL)
3497                 pp = &p->next;
3498             }
3499           *pp = edir->dyn_relocs;
3500         }
3501
3502       edir->dyn_relocs = eind->dyn_relocs;
3503       eind->dyn_relocs = NULL;
3504     }
3505
3506   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3507 }
3508 \f
3509 /* Find dynamic relocs for H that apply to read-only sections.  */
3510
3511 static asection *
3512 readonly_dynrelocs (struct elf_link_hash_entry *h)
3513 {
3514   struct elf_nds32_dyn_relocs *p;
3515
3516   for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3517     {
3518       asection *s = p->sec->output_section;
3519
3520       if (s != NULL && (s->flags & SEC_READONLY) != 0)
3521         return p->sec;
3522     }
3523   return NULL;
3524 }
3525
3526 /* Adjust a symbol defined by a dynamic object and referenced by a
3527    regular object.  The current definition is in some section of the
3528    dynamic object, but we're not including those sections.  We have to
3529    change the definition to something the rest of the link can
3530    understand.  */
3531
3532 static bfd_boolean
3533 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3534                                  struct elf_link_hash_entry *h)
3535 {
3536   struct elf_nds32_link_hash_table *htab;
3537   struct elf_nds32_link_hash_entry *eh;
3538   struct elf_nds32_dyn_relocs *p;
3539   bfd *dynobj;
3540   asection *s;
3541   unsigned int power_of_two;
3542
3543   dynobj = elf_hash_table (info)->dynobj;
3544
3545   /* Make sure we know what is going on here.  */
3546   BFD_ASSERT (dynobj != NULL
3547               && (h->needs_plt
3548                   || h->is_weakalias
3549                   || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3550
3551
3552   /* If this is a function, put it in the procedure linkage table.  We
3553      will fill in the contents of the procedure linkage table later,
3554      when we know the address of the .got section.  */
3555   if (h->type == STT_FUNC || h->needs_plt)
3556     {
3557       if (!bfd_link_pic (info)
3558           && !h->def_dynamic
3559           && !h->ref_dynamic
3560           && h->root.type != bfd_link_hash_undefweak
3561           && h->root.type != bfd_link_hash_undefined)
3562         {
3563           /* This case can occur if we saw a PLT reloc in an input
3564              file, but the symbol was never referred to by a dynamic
3565              object.  In such a case, we don't actually need to build
3566              a procedure linkage table, and we can just do a PCREL
3567              reloc instead.  */
3568           h->plt.offset = (bfd_vma) - 1;
3569           h->needs_plt = 0;
3570         }
3571
3572       return TRUE;
3573     }
3574   else
3575     h->plt.offset = (bfd_vma) - 1;
3576
3577   /* If this is a weak symbol, and there is a real definition, the
3578      processor independent code will have arranged for us to see the
3579      real definition first, and we can just use the same value.  */
3580   if (h->is_weakalias)
3581     {
3582       struct elf_link_hash_entry *def = weakdef (h);
3583       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3584       h->root.u.def.section = def->root.u.def.section;
3585       h->root.u.def.value = def->root.u.def.value;
3586       return TRUE;
3587     }
3588
3589   /* This is a reference to a symbol defined by a dynamic object which
3590      is not a function.  */
3591
3592   /* If we are creating a shared library, we must presume that the
3593      only references to the symbol are via the global offset table.
3594      For such cases we need not do anything here; the relocations will
3595      be handled correctly by relocate_section.  */
3596   if (bfd_link_pic (info))
3597     return TRUE;
3598
3599   /* If there are no references to this symbol that do not use the
3600      GOT, we don't need to generate a copy reloc.  */
3601   if (!h->non_got_ref)
3602     return TRUE;
3603
3604   /* If -z nocopyreloc was given, we won't generate them either.  */
3605   if (info->nocopyreloc)
3606     {
3607       h->non_got_ref = 0;
3608       return TRUE;
3609     }
3610
3611   eh = (struct elf_nds32_link_hash_entry *) h;
3612   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3613     {
3614       s = p->sec->output_section;
3615       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
3616         break;
3617     }
3618
3619   /* If we didn't find any dynamic relocs in sections which needs the
3620      copy reloc, then we'll be keeping the dynamic relocs and avoiding
3621      the copy reloc.  */
3622   if (p == NULL)
3623     {
3624       h->non_got_ref = 0;
3625       return TRUE;
3626     }
3627
3628   /* We must allocate the symbol in our .dynbss section, which will
3629      become part of the .bss section of the executable.  There will be
3630      an entry for this symbol in the .dynsym section.  The dynamic
3631      object will contain position independent code, so all references
3632      from the dynamic object to this symbol will go through the global
3633      offset table.  The dynamic linker will use the .dynsym entry to
3634      determine the address it must put in the global offset table, so
3635      both the dynamic object and the regular object will refer to the
3636      same memory location for the variable.  */
3637
3638   htab = nds32_elf_hash_table (info);
3639   s = htab->sdynbss;
3640   BFD_ASSERT (s != NULL);
3641
3642   /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3643      to copy the initial value out of the dynamic object and into the
3644      runtime process image.  We need to remember the offset into the
3645      .rela.bss section we are going to use.  */
3646   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3647     {
3648       asection *srel;
3649
3650       srel = htab->srelbss;
3651       BFD_ASSERT (srel != NULL);
3652       srel->size += sizeof (Elf32_External_Rela);
3653       h->needs_copy = 1;
3654     }
3655
3656   /* We need to figure out the alignment required for this symbol.  I
3657      have no idea how ELF linkers handle this.  */
3658   power_of_two = bfd_log2 (h->size);
3659   if (power_of_two > 3)
3660     power_of_two = 3;
3661
3662   /* Apply the required alignment.  */
3663   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
3664   if (power_of_two > bfd_get_section_alignment (dynobj, s))
3665     {
3666       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
3667         return FALSE;
3668     }
3669
3670   /* Define the symbol as being at this point in the section.  */
3671   h->root.u.def.section = s;
3672   h->root.u.def.value = s->size;
3673
3674   /* Increment the section size to make room for the symbol.  */
3675   s->size += h->size;
3676
3677   return TRUE;
3678 }
3679
3680 /* Allocate space in .plt, .got and associated reloc sections for
3681    dynamic relocs.  */
3682
3683 static bfd_boolean
3684 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3685 {
3686   struct bfd_link_info *info;
3687   struct elf_nds32_link_hash_table *htab;
3688   struct elf_nds32_link_hash_entry *eh;
3689   struct elf_nds32_dyn_relocs *p;
3690
3691   if (h->root.type == bfd_link_hash_indirect)
3692     return TRUE;
3693
3694   if (h->root.type == bfd_link_hash_warning)
3695     /* When warning symbols are created, they **replace** the "real"
3696        entry in the hash table, thus we never get to see the real
3697        symbol in a hash traversal.  So look at it now.  */
3698     h = (struct elf_link_hash_entry *) h->root.u.i.link;
3699
3700   info = (struct bfd_link_info *) inf;
3701   htab = nds32_elf_hash_table (info);
3702
3703   eh = (struct elf_nds32_link_hash_entry *) h;
3704
3705   if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
3706     {
3707       /* Make sure this symbol is output as a dynamic symbol.
3708          Undefined weak syms won't yet be marked as dynamic.  */
3709       if (h->dynindx == -1 && !h->forced_local)
3710         {
3711           if (!bfd_elf_link_record_dynamic_symbol (info, h))
3712             return FALSE;
3713         }
3714
3715       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
3716         {
3717           asection *s = htab->root.splt;
3718
3719           /* If this is the first .plt entry, make room for the special
3720              first entry.  */
3721           if (s->size == 0)
3722             s->size += PLT_ENTRY_SIZE;
3723
3724           h->plt.offset = s->size;
3725
3726           /* If this symbol is not defined in a regular file, and we are
3727              not generating a shared library, then set the symbol to this
3728              location in the .plt.  This is required to make function
3729              pointers compare as equal between the normal executable and
3730              the shared library.  */
3731           if (!bfd_link_pic (info) && !h->def_regular)
3732             {
3733               h->root.u.def.section = s;
3734               h->root.u.def.value = h->plt.offset;
3735             }
3736
3737           /* Make room for this entry.  */
3738           s->size += PLT_ENTRY_SIZE;
3739
3740           /* We also need to make an entry in the .got.plt section, which
3741              will be placed in the .got section by the linker script.  */
3742           htab->root.sgotplt->size += 4;
3743
3744           /* We also need to make an entry in the .rel.plt section.  */
3745           htab->root.srelplt->size += sizeof (Elf32_External_Rela);
3746         }
3747       else
3748         {
3749           h->plt.offset = (bfd_vma) - 1;
3750           h->needs_plt = 0;
3751         }
3752     }
3753   else
3754     {
3755       h->plt.offset = (bfd_vma) - 1;
3756       h->needs_plt = 0;
3757     }
3758
3759   if (h->got.refcount > 0)
3760     {
3761       asection *s;
3762       bfd_boolean dyn;
3763       int tls_type = elf32_nds32_hash_entry (h)->tls_type;
3764
3765       /* Make sure this symbol is output as a dynamic symbol.
3766          Undefined weak syms won't yet be marked as dynamic.  */
3767       if (h->dynindx == -1 && !h->forced_local)
3768         {
3769           if (!bfd_elf_link_record_dynamic_symbol (info, h))
3770             return FALSE;
3771         }
3772
3773       s = htab->root.sgot;
3774       h->got.offset = s->size;
3775
3776       if (tls_type == GOT_UNKNOWN)
3777         abort ();
3778       else if (tls_type == GOT_NORMAL
3779                || tls_type == GOT_TLS_IE)
3780         /* Need a GOT slot.  */
3781         s->size += 4;
3782
3783       dyn = htab->root.dynamic_sections_created;
3784       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
3785         htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3786     }
3787   else
3788     h->got.offset = (bfd_vma) - 1;
3789
3790   if (eh->dyn_relocs == NULL)
3791     return TRUE;
3792
3793   /* In the shared -Bsymbolic case, discard space allocated for
3794      dynamic pc-relative relocs against symbols which turn out to be
3795      defined in regular objects.  For the normal shared case, discard
3796      space for pc-relative relocs that have become local due to symbol
3797      visibility changes.  */
3798
3799   if (bfd_link_pic (info))
3800     {
3801       if (h->def_regular && (h->forced_local || info->symbolic))
3802         {
3803           struct elf_nds32_dyn_relocs **pp;
3804
3805           for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
3806             {
3807               p->count -= p->pc_count;
3808               p->pc_count = 0;
3809               if (p->count == 0)
3810                 *pp = p->next;
3811               else
3812                 pp = &p->next;
3813             }
3814         }
3815     }
3816   else
3817     {
3818       /* For the non-shared case, discard space for relocs against
3819          symbols which turn out to need copy relocs or are not dynamic.  */
3820
3821       if (!h->non_got_ref
3822           && ((h->def_dynamic
3823                && !h->def_regular)
3824               || (htab->root.dynamic_sections_created
3825                   && (h->root.type == bfd_link_hash_undefweak
3826                       || h->root.type == bfd_link_hash_undefined))))
3827         {
3828           /* Make sure this symbol is output as a dynamic symbol.
3829              Undefined weak syms won't yet be marked as dynamic.  */
3830           if (h->dynindx == -1 && !h->forced_local)
3831             {
3832               if (!bfd_elf_link_record_dynamic_symbol (info, h))
3833                 return FALSE;
3834             }
3835
3836           /* If that succeeded, we know we'll be keeping all the
3837              relocs.  */
3838           if (h->dynindx != -1)
3839             goto keep;
3840         }
3841
3842       eh->dyn_relocs = NULL;
3843
3844     keep:;
3845     }
3846
3847   /* Finally, allocate space.  */
3848   for (p = eh->dyn_relocs; p != NULL; p = p->next)
3849     {
3850       asection *sreloc = elf_section_data (p->sec)->sreloc;
3851       sreloc->size += p->count * sizeof (Elf32_External_Rela);
3852     }
3853
3854   return TRUE;
3855 }
3856
3857 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
3858    read-only sections.  */
3859
3860 static bfd_boolean
3861 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
3862 {
3863   asection *sec;
3864
3865   if (h->root.type == bfd_link_hash_indirect)
3866     return TRUE;
3867
3868   sec = readonly_dynrelocs (h);
3869   if (sec != NULL)
3870     {
3871       struct bfd_link_info *info = (struct bfd_link_info *) info_p;
3872
3873       info->flags |= DF_TEXTREL;
3874       info->callbacks->minfo
3875         (_("%B: dynamic relocation against `%T' in read-only section `%A'\n"),
3876          sec->owner, h->root.root.string, sec);
3877
3878       /* Not an error, just cut short the traversal.  */
3879       return FALSE;
3880     }
3881   return TRUE;
3882 }
3883
3884 /* Set the sizes of the dynamic sections.  */
3885
3886 static bfd_boolean
3887 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3888                                  struct bfd_link_info *info)
3889 {
3890   struct elf_nds32_link_hash_table *htab;
3891   bfd *dynobj;
3892   asection *s;
3893   bfd_boolean relocs;
3894   bfd *ibfd;
3895
3896   htab = nds32_elf_hash_table (info);
3897   dynobj = htab->root.dynobj;
3898   BFD_ASSERT (dynobj != NULL);
3899
3900   if (htab->root.dynamic_sections_created)
3901     {
3902       /* Set the contents of the .interp section to the interpreter.  */
3903       if (bfd_link_executable (info) && !info->nointerp)
3904         {
3905           s = bfd_get_section_by_name (dynobj, ".interp");
3906           BFD_ASSERT (s != NULL);
3907           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3908           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3909         }
3910     }
3911
3912   /* Set up .got offsets for local syms, and space for local dynamic
3913      relocs.  */
3914   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3915     {
3916       bfd_signed_vma *local_got;
3917       bfd_signed_vma *end_local_got;
3918       bfd_size_type locsymcount;
3919       Elf_Internal_Shdr *symtab_hdr;
3920       asection *srel;
3921
3922       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3923         continue;
3924
3925       for (s = ibfd->sections; s != NULL; s = s->next)
3926         {
3927           struct elf_nds32_dyn_relocs *p;
3928
3929           for (p = ((struct elf_nds32_dyn_relocs *)
3930                     elf_section_data (s)->local_dynrel);
3931                p != NULL; p = p->next)
3932             {
3933               if (!bfd_is_abs_section (p->sec)
3934                   && bfd_is_abs_section (p->sec->output_section))
3935                 {
3936                   /* Input section has been discarded, either because
3937                      it is a copy of a linkonce section or due to
3938                      linker script /DISCARD/, so we'll be discarding
3939                      the relocs too.  */
3940                 }
3941               else if (p->count != 0)
3942                 {
3943                   srel = elf_section_data (p->sec)->sreloc;
3944                   srel->size += p->count * sizeof (Elf32_External_Rela);
3945                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3946                     info->flags |= DF_TEXTREL;
3947                 }
3948             }
3949         }
3950
3951       local_got = elf_local_got_refcounts (ibfd);
3952       if (!local_got)
3953         continue;
3954
3955       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
3956       locsymcount = symtab_hdr->sh_info;
3957       end_local_got = local_got + locsymcount;
3958       s = htab->root.sgot;
3959       srel = htab->root.srelgot;
3960       for (; local_got < end_local_got; ++local_got)
3961         {
3962           if (*local_got > 0)
3963             {
3964               *local_got = s->size;
3965               s->size += 4;
3966               if (bfd_link_pic (info))
3967                 srel->size += sizeof (Elf32_External_Rela);
3968             }
3969           else
3970             *local_got = (bfd_vma) - 1;
3971         }
3972     }
3973
3974   /* Allocate global sym .plt and .got entries, and space for global
3975      sym dynamic relocs.  */
3976   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
3977
3978   /* We now have determined the sizes of the various dynamic sections.
3979      Allocate memory for them.  */
3980   relocs = FALSE;
3981   for (s = dynobj->sections; s != NULL; s = s->next)
3982     {
3983       if ((s->flags & SEC_LINKER_CREATED) == 0)
3984         continue;
3985
3986       if (s == htab->root.splt)
3987         {
3988           /* Strip this section if we don't need it; see the
3989              comment below.  */
3990         }
3991       else if (s == htab->root.sgot)
3992         {
3993           got_size += s->size;
3994         }
3995       else if (s == htab->root.sgotplt)
3996         {
3997           got_size += s->size;
3998         }
3999       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4000         {
4001           if (s->size != 0 && s != htab->root.srelplt)
4002             relocs = TRUE;
4003
4004           /* We use the reloc_count field as a counter if we need
4005              to copy relocs into the output file.  */
4006           s->reloc_count = 0;
4007         }
4008       else
4009         {
4010           /* It's not one of our sections, so don't allocate space.  */
4011           continue;
4012         }
4013
4014       if (s->size == 0)
4015         {
4016           /* If we don't need this section, strip it from the
4017              output file.  This is mostly to handle .rela.bss and
4018              .rela.plt.  We must create both sections in
4019              create_dynamic_sections, because they must be created
4020              before the linker maps input sections to output
4021              sections.  The linker does that before
4022              adjust_dynamic_symbol is called, and it is that
4023              function which decides whether anything needs to go
4024              into these sections.  */
4025           s->flags |= SEC_EXCLUDE;
4026           continue;
4027         }
4028
4029       /* Allocate memory for the section contents.  We use bfd_zalloc
4030          here in case unused entries are not reclaimed before the
4031          section's contents are written out.  This should not happen,
4032          but this way if it does, we get a R_NDS32_NONE reloc instead
4033          of garbage.  */
4034       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4035       if (s->contents == NULL)
4036         return FALSE;
4037     }
4038
4039
4040   if (htab->root.dynamic_sections_created)
4041     {
4042       /* Add some entries to the .dynamic section.  We fill in the
4043          values later, in nds32_elf_finish_dynamic_sections, but we
4044          must add the entries now so that we get the correct size for
4045          the .dynamic section.  The DT_DEBUG entry is filled in by the
4046          dynamic linker and used by the debugger.  */
4047 #define add_dynamic_entry(TAG, VAL) \
4048   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4049
4050       if (!bfd_link_pic (info))
4051         {
4052           if (!add_dynamic_entry (DT_DEBUG, 0))
4053             return FALSE;
4054         }
4055
4056       if (htab->root.splt->size != 0)
4057         {
4058           if (!add_dynamic_entry (DT_PLTGOT, 0)
4059               || !add_dynamic_entry (DT_PLTRELSZ, 0)
4060               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4061               || !add_dynamic_entry (DT_JMPREL, 0))
4062             return FALSE;
4063         }
4064
4065       if (relocs)
4066         {
4067           if (!add_dynamic_entry (DT_RELA, 0)
4068               || !add_dynamic_entry (DT_RELASZ, 0)
4069               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4070             return FALSE;
4071
4072           /* If any dynamic relocs apply to a read-only section,
4073              then we need a DT_TEXTREL entry.  */
4074           if ((info->flags & DF_TEXTREL) == 0)
4075             elf_link_hash_traverse (&htab->root, maybe_set_textrel,
4076                                     (void *) info);
4077
4078           if ((info->flags & DF_TEXTREL) != 0)
4079             {
4080               if (!add_dynamic_entry (DT_TEXTREL, 0))
4081                 return FALSE;
4082             }
4083         }
4084     }
4085 #undef add_dynamic_entry
4086
4087   return TRUE;
4088 }
4089
4090 static bfd_reloc_status_type
4091 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4092                          bfd_vma relocation, bfd_byte *location)
4093 {
4094   int size;
4095   bfd_vma x = 0;
4096   bfd_reloc_status_type flag;
4097   unsigned int rightshift = howto->rightshift;
4098   unsigned int bitpos = howto->bitpos;
4099
4100   /* If the size is negative, negate RELOCATION.  This isn't very
4101      general.  */
4102   if (howto->size < 0)
4103     relocation = -relocation;
4104
4105   /* Get the value we are going to relocate.  */
4106   size = bfd_get_reloc_size (howto);
4107   switch (size)
4108     {
4109     default:
4110       abort ();
4111       break;
4112     case 0:
4113       return bfd_reloc_ok;
4114     case 2:
4115       x = bfd_getb16 (location);
4116       break;
4117     case 4:
4118       x = bfd_getb32 (location);
4119       break;
4120     }
4121
4122   /* Check for overflow.  FIXME: We may drop bits during the addition
4123      which we don't check for.  We must either check at every single
4124      operation, which would be tedious, or we must do the computations
4125      in a type larger than bfd_vma, which would be inefficient.  */
4126   flag = bfd_reloc_ok;
4127   if (howto->complain_on_overflow != complain_overflow_dont)
4128     {
4129       bfd_vma addrmask, fieldmask, signmask, ss;
4130       bfd_vma a, b, sum;
4131
4132       /* Get the values to be added together.  For signed and unsigned
4133          relocations, we assume that all values should be truncated to
4134          the size of an address.  For bitfields, all the bits matter.
4135          See also bfd_check_overflow.  */
4136       fieldmask = N_ONES (howto->bitsize);
4137       signmask = ~fieldmask;
4138       addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4139       a = (relocation & addrmask) >> rightshift;
4140       b = (x & howto->src_mask & addrmask) >> bitpos;
4141
4142       switch (howto->complain_on_overflow)
4143         {
4144         case complain_overflow_signed:
4145           /* If any sign bits are set, all sign bits must be set.
4146              That is, A must be a valid negative address after
4147              shifting.  */
4148           signmask = ~(fieldmask >> 1);
4149           /* Fall through.  */
4150
4151         case complain_overflow_bitfield:
4152           /* Much like the signed check, but for a field one bit
4153              wider.  We allow a bitfield to represent numbers in the
4154              range -2**n to 2**n-1, where n is the number of bits in the
4155              field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4156              can't overflow, which is exactly what we want.  */
4157           ss = a & signmask;
4158           if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4159             flag = bfd_reloc_overflow;
4160
4161           /* We only need this next bit of code if the sign bit of B
4162              is below the sign bit of A.  This would only happen if
4163              SRC_MASK had fewer bits than BITSIZE.  Note that if
4164              SRC_MASK has more bits than BITSIZE, we can get into
4165              trouble; we would need to verify that B is in range, as
4166              we do for A above.  */
4167           ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4168           ss >>= bitpos;
4169
4170           /* Set all the bits above the sign bit.  */
4171           b = (b ^ ss) - ss;
4172
4173           /* Now we can do the addition.  */
4174           sum = a + b;
4175
4176           /* See if the result has the correct sign.  Bits above the
4177              sign bit are junk now; ignore them.  If the sum is
4178              positive, make sure we did not have all negative inputs;
4179              if the sum is negative, make sure we did not have all
4180              positive inputs.  The test below looks only at the sign
4181              bits, and it really just
4182              SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4183
4184              We mask with addrmask here to explicitly allow an address
4185              wrap-around.  The Linux kernel relies on it, and it is
4186              the only way to write assembler code which can run when
4187              loaded at a location 0x80000000 away from the location at
4188              which it is linked.  */
4189           if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4190             flag = bfd_reloc_overflow;
4191
4192           break;
4193
4194         case complain_overflow_unsigned:
4195           /* Checking for an unsigned overflow is relatively easy:
4196              trim the addresses and add, and trim the result as well.
4197              Overflow is normally indicated when the result does not
4198              fit in the field.  However, we also need to consider the
4199              case when, e.g., fieldmask is 0x7fffffff or smaller, an
4200              input is 0x80000000, and bfd_vma is only 32 bits; then we
4201              will get sum == 0, but there is an overflow, since the
4202              inputs did not fit in the field.  Instead of doing a
4203              separate test, we can check for this by or-ing in the
4204              operands when testing for the sum overflowing its final
4205              field.  */
4206           sum = (a + b) & addrmask;
4207           if ((a | b | sum) & signmask)
4208             flag = bfd_reloc_overflow;
4209           break;
4210
4211         default:
4212           abort ();
4213         }
4214     }
4215
4216   /* Put RELOCATION in the right bits.  */
4217   relocation >>= (bfd_vma) rightshift;
4218   relocation <<= (bfd_vma) bitpos;
4219
4220   /* Add RELOCATION to the right bits of X.  */
4221   /* FIXME : 090616
4222      Because the relaxation may generate duplicate relocation at one address,
4223      an addition to immediate in the instruction may cause the relocation added
4224      several times.
4225      This bug should be fixed in assembler, but a check is also needed here.  */
4226   if (howto->partial_inplace)
4227     x = ((x & ~howto->dst_mask)
4228          | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4229   else
4230     x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4231
4232
4233   /* Put the relocated value back in the object file.  */
4234   switch (size)
4235     {
4236     default:
4237     case 0:
4238     case 1:
4239     case 8:
4240       abort ();
4241       break;
4242     case 2:
4243       bfd_putb16 (x, location);
4244       break;
4245     case 4:
4246       bfd_putb32 (x, location);
4247       break;
4248     }
4249
4250   return flag;
4251 }
4252
4253 static bfd_reloc_status_type
4254 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4255                                asection *input_section, bfd_byte *contents,
4256                                bfd_vma address, bfd_vma value, bfd_vma addend)
4257 {
4258   bfd_vma relocation;
4259
4260   /* Sanity check the address.  */
4261   if (address > bfd_get_section_limit (input_bfd, input_section))
4262     return bfd_reloc_outofrange;
4263
4264   /* This function assumes that we are dealing with a basic relocation
4265      against a symbol.  We want to compute the value of the symbol to
4266      relocate to.  This is just VALUE, the value of the symbol, plus
4267      ADDEND, any addend associated with the reloc.  */
4268   relocation = value + addend;
4269
4270   /* If the relocation is PC relative, we want to set RELOCATION to
4271      the distance between the symbol (currently in RELOCATION) and the
4272      location we are relocating.  Some targets (e.g., i386-aout)
4273      arrange for the contents of the section to be the negative of the
4274      offset of the location within the section; for such targets
4275      pcrel_offset is FALSE.  Other targets (e.g., m88kbcs or ELF)
4276      simply leave the contents of the section as zero; for such
4277      targets pcrel_offset is TRUE.  If pcrel_offset is FALSE we do not
4278      need to subtract out the offset of the location within the
4279      section (which is just ADDRESS).  */
4280   if (howto->pc_relative)
4281     {
4282       relocation -= (input_section->output_section->vma
4283                      + input_section->output_offset);
4284       if (howto->pcrel_offset)
4285         relocation -= address;
4286     }
4287
4288   return nds32_relocate_contents (howto, input_bfd, relocation,
4289                                   contents + address);
4290 }
4291
4292 static bfd_boolean
4293 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4294                               const char *name,
4295                               Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4296                               asection *input_sec,
4297                               struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4298 {
4299   const char *source;
4300   FILE *sym_ld_script = NULL;
4301   struct elf_nds32_link_hash_table *table;
4302
4303   table = nds32_elf_hash_table (info);
4304   sym_ld_script = table->sym_ld_script;
4305   if (!sym_ld_script)
4306     return TRUE;
4307
4308   if (!h || !name || *name == '\0')
4309     return TRUE;
4310
4311   if (input_sec->flags & SEC_EXCLUDE)
4312     return TRUE;
4313
4314   if (!check_start_export_sym)
4315     {
4316       fprintf (sym_ld_script, "SECTIONS\n{\n");
4317       check_start_export_sym = 1;
4318     }
4319
4320   if (h->root.type == bfd_link_hash_defined
4321       || h->root.type == bfd_link_hash_defweak)
4322     {
4323       if (!h->root.u.def.section->output_section)
4324         return TRUE;
4325
4326       if (bfd_is_const_section (input_sec))
4327         source = input_sec->name;
4328       else
4329         source = input_sec->owner->filename;
4330
4331       fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4332                h->root.root.string,
4333                (long) (h->root.u.def.value
4334                 + h->root.u.def.section->output_section->vma
4335                 + h->root.u.def.section->output_offset), source);
4336     }
4337
4338   return TRUE;
4339 }
4340
4341 /* Relocate an NDS32/D ELF section.
4342    There is some attempt to make this function usable for many architectures,
4343    both for RELA and REL type relocs, if only to serve as a learning tool.
4344
4345    The RELOCATE_SECTION function is called by the new ELF backend linker
4346    to handle the relocations for a section.
4347
4348    The relocs are always passed as Rela structures; if the section
4349    actually uses Rel structures, the r_addend field will always be
4350    zero.
4351
4352    This function is responsible for adjust the section contents as
4353    necessary, and (if using Rela relocs and generating a
4354    relocatable output file) adjusting the reloc addend as
4355    necessary.
4356
4357    This function does not have to worry about setting the reloc
4358    address or the reloc symbol index.
4359
4360    LOCAL_SYMS is a pointer to the swapped in local symbols.
4361
4362    LOCAL_SECTIONS is an array giving the section in the input file
4363    corresponding to the st_shndx field of each local symbol.
4364
4365    The global hash table entry for the global symbols can be found
4366    via elf_sym_hashes (input_bfd).
4367
4368    When generating relocatable output, this function must handle
4369    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4370    going to be the section symbol corresponding to the output
4371    section, which means that the addend must be adjusted
4372    accordingly.  */
4373
4374 static bfd_vma
4375 dtpoff_base (struct bfd_link_info *info)
4376 {
4377   /* If tls_sec is NULL, we should have signalled an error already.  */
4378   if (elf_hash_table (info)->tls_sec == NULL)
4379     return 0;
4380   return elf_hash_table (info)->tls_sec->vma;
4381 }
4382
4383 static bfd_boolean
4384 nds32_elf_relocate_section (bfd *                  output_bfd ATTRIBUTE_UNUSED,
4385                             struct bfd_link_info * info,
4386                             bfd *                  input_bfd,
4387                             asection *             input_section,
4388                             bfd_byte *             contents,
4389                             Elf_Internal_Rela *    relocs,
4390                             Elf_Internal_Sym *     local_syms,
4391                             asection **            local_sections)
4392 {
4393   Elf_Internal_Shdr *symtab_hdr;
4394   struct elf_link_hash_entry **sym_hashes;
4395   Elf_Internal_Rela *rel, *relend;
4396   bfd_boolean ret = TRUE;               /* Assume success.  */
4397   int align = 0;
4398   bfd_reloc_status_type r;
4399   const char *errmsg = NULL;
4400   bfd_vma gp;
4401   struct elf_nds32_link_hash_table *htab;
4402   bfd *dynobj;
4403   bfd_vma *local_got_offsets;
4404   asection *sgot, *splt, *sreloc;
4405   bfd_vma high_address;
4406   struct elf_nds32_link_hash_table *table;
4407   int eliminate_gc_relocs;
4408   bfd_vma fpbase_addr;
4409
4410   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4411   sym_hashes = elf_sym_hashes (input_bfd);
4412   htab = nds32_elf_hash_table (info);
4413   high_address = bfd_get_section_limit (input_bfd, input_section);
4414
4415   dynobj = htab->root.dynobj;
4416   local_got_offsets = elf_local_got_offsets (input_bfd);
4417
4418   sgot = htab->root.sgot;
4419   splt = htab->root.splt;
4420   sreloc = NULL;
4421
4422   rel = relocs;
4423   relend = relocs + input_section->reloc_count;
4424
4425   table = nds32_elf_hash_table (info);
4426   eliminate_gc_relocs = table->eliminate_gc_relocs;
4427   /* By this time, we can adjust the value of _SDA_BASE_.  */
4428   if ((!bfd_link_relocatable (info)))
4429     {
4430       is_SDA_BASE_set = 1;
4431       r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
4432       if (r != bfd_reloc_ok)
4433         return FALSE;
4434     }
4435
4436   if (is_ITB_BASE_set == 0)
4437     {
4438       /* Set the _ITB_BASE_.  */
4439       if (!nds32_elf_ex9_itb_base (info))
4440         {
4441           _bfd_error_handler (_("%B: error: Cannot set _ITB_BASE_"),
4442                               output_bfd);
4443           bfd_set_error (bfd_error_bad_value);
4444         }
4445     }
4446
4447   if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
4448     if (!nds32_elf_ifc_reloc ())
4449       _bfd_error_handler (_("error: IFC relocation error."));
4450
4451  /* Relocation for .ex9.itable.  */
4452   if (table->target_optimize & NDS32_RELAX_EX9_ON
4453       || (table->ex9_import_file && table->update_ex9_table))
4454     nds32_elf_ex9_reloc_jmp (info);
4455
4456   /* Use gp as fp to prevent truncated fit.  Because in relaxation time
4457      the fp value is set as gp, and it has be reverted for instruction
4458      setting fp.  */
4459   fpbase_addr = elf_gp (output_bfd);
4460
4461   for (rel = relocs; rel < relend; rel++)
4462     {
4463       enum elf_nds32_reloc_type r_type;
4464       reloc_howto_type *howto = NULL;
4465       unsigned long r_symndx;
4466       struct elf_link_hash_entry *h = NULL;
4467       Elf_Internal_Sym *sym = NULL;
4468       asection *sec;
4469       bfd_vma relocation;
4470
4471       /* We can't modify r_addend here as elf_link_input_bfd has an assert to
4472          ensure it's zero (we use REL relocs, not RELA).  Therefore this
4473          should be assigning zero to `addend', but for clarity we use
4474          `r_addend'.  */
4475
4476       bfd_vma addend = rel->r_addend;
4477       bfd_vma offset = rel->r_offset;
4478
4479       r_type = ELF32_R_TYPE (rel->r_info);
4480       if (r_type >= R_NDS32_max)
4481         {
4482           /* xgettext:c-format */
4483           _bfd_error_handler (_("%B: error: unknown relocation type %d."),
4484                               input_bfd, r_type);
4485           bfd_set_error (bfd_error_bad_value);
4486           ret = FALSE;
4487           continue;
4488         }
4489
4490       if (r_type == R_NDS32_GNU_VTENTRY
4491           || r_type == R_NDS32_GNU_VTINHERIT
4492           || r_type == R_NDS32_NONE
4493           || r_type == R_NDS32_RELA_GNU_VTENTRY
4494           || r_type == R_NDS32_RELA_GNU_VTINHERIT
4495           || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
4496           || r_type == R_NDS32_DATA
4497           || r_type == R_NDS32_TRAN
4498           || (r_type >= R_NDS32_LONGCALL4 && r_type <= R_NDS32_LONGJUMP7))
4499         continue;
4500
4501       /* If we enter the fp-as-gp region.  Resolve the address
4502          of best fp-base.  */
4503       if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
4504           && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4505         {
4506           int dist;
4507
4508           /* Distance to relocation of best fp-base is encoded in R_SYM.  */
4509           dist =  rel->r_addend >> 16;
4510           fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
4511                                                   local_syms, symtab_hdr);
4512         }
4513       else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
4514                && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4515         {
4516           fpbase_addr = elf_gp (output_bfd);
4517         }
4518
4519       if (((r_type >= R_NDS32_DWARF2_OP1_RELA
4520             && r_type <= R_NDS32_DWARF2_LEB_RELA)
4521            || r_type >= R_NDS32_RELAX_ENTRY) && !bfd_link_relocatable (info))
4522         continue;
4523
4524       howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
4525       r_symndx = ELF32_R_SYM (rel->r_info);
4526
4527       /* This is a final link.  */
4528       sym = NULL;
4529       sec = NULL;
4530       h = NULL;
4531
4532       if (r_symndx < symtab_hdr->sh_info)
4533         {
4534           /* Local symbol.  */
4535           sym = local_syms + r_symndx;
4536           sec = local_sections[r_symndx];
4537
4538           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4539           addend = rel->r_addend;
4540         }
4541       else
4542         {
4543           /* External symbol.  */
4544           bfd_boolean warned, ignored, unresolved_reloc;
4545           int symndx = r_symndx - symtab_hdr->sh_info;
4546
4547           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4548                                    r_symndx, symtab_hdr, sym_hashes, h, sec,
4549                                    relocation, unresolved_reloc, warned,
4550                                    ignored);
4551
4552           /* la $fp, _FP_BASE_ is per-function (region).
4553              Handle it specially.  */
4554           switch ((int) r_type)
4555             {
4556             case R_NDS32_SDA19S0_RELA:
4557             case R_NDS32_SDA15S0_RELA:
4558             case R_NDS32_20_RELA:
4559               if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
4560                           FP_BASE_NAME) == 0)
4561                 {
4562                   relocation = fpbase_addr;
4563                   break;
4564                 }
4565             }
4566
4567         }
4568
4569       if (bfd_link_relocatable (info))
4570         {
4571           /* This is a relocatable link.  We don't have to change
4572              anything, unless the reloc is against a section symbol,
4573              in which case we have to adjust according to where the
4574              section symbol winds up in the output section.  */
4575           if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4576             rel->r_addend += sec->output_offset + sym->st_value;
4577
4578           continue;
4579         }
4580
4581       /* Sanity check the address.  */
4582       if (offset > high_address)
4583         {
4584           r = bfd_reloc_outofrange;
4585           goto check_reloc;
4586         }
4587
4588       if ((r_type >= R_NDS32_DWARF2_OP1_RELA
4589            && r_type <= R_NDS32_DWARF2_LEB_RELA)
4590           || r_type >= R_NDS32_RELAX_ENTRY)
4591         continue;
4592
4593       switch ((int) r_type)
4594         {
4595         case R_NDS32_GOTOFF:
4596           /* Relocation is relative to the start of the global offset
4597              table (for ld24 rx, #uimm24), e.g. access at label+addend
4598
4599              ld24 rx. #label@GOTOFF + addend
4600              sub  rx, r12.  */
4601         case R_NDS32_GOTOFF_HI20:
4602         case R_NDS32_GOTOFF_LO12:
4603         case R_NDS32_GOTOFF_LO15:
4604         case R_NDS32_GOTOFF_LO19:
4605           BFD_ASSERT (sgot != NULL);
4606
4607           relocation -= elf_gp (output_bfd);
4608           break;
4609
4610         case R_NDS32_9_PLTREL:
4611         case R_NDS32_25_PLTREL:
4612           /* Relocation is to the entry for this symbol in the
4613              procedure linkage table.  */
4614
4615           /* The native assembler will generate a 25_PLTREL reloc
4616              for a local symbol if you assemble a call from one
4617              section to another when using -K pic.  */
4618           if (h == NULL)
4619             break;
4620
4621           if (h->forced_local)
4622             break;
4623
4624           /* We didn't make a PLT entry for this symbol.  This
4625              happens when statically linking PIC code, or when
4626              using -Bsymbolic.  */
4627           if (h->plt.offset == (bfd_vma) - 1)
4628             break;
4629
4630           relocation = (splt->output_section->vma
4631                         + splt->output_offset + h->plt.offset);
4632           break;
4633
4634         case R_NDS32_PLT_GOTREL_HI20:
4635         case R_NDS32_PLT_GOTREL_LO12:
4636         case R_NDS32_PLT_GOTREL_LO15:
4637         case R_NDS32_PLT_GOTREL_LO19:
4638         case R_NDS32_PLT_GOTREL_LO20:
4639           if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
4640             {
4641               /* We didn't make a PLT entry for this symbol.  This
4642                  happens when statically linking PIC code, or when
4643                  using -Bsymbolic.  */
4644               relocation -= elf_gp (output_bfd);
4645               break;
4646             }
4647
4648           relocation = (splt->output_section->vma
4649                         + splt->output_offset + h->plt.offset);
4650
4651           relocation -= elf_gp (output_bfd);
4652           break;
4653
4654         case R_NDS32_PLTREL_HI20:
4655         case R_NDS32_PLTREL_LO12:
4656
4657           /* Relocation is to the entry for this symbol in the
4658              procedure linkage table.  */
4659
4660           /* The native assembler will generate a 25_PLTREL reloc
4661              for a local symbol if you assemble a call from one
4662              section to another when using -K pic.  */
4663           if (h == NULL)
4664             break;
4665
4666           if (h->forced_local)
4667             break;
4668
4669           if (h->plt.offset == (bfd_vma) - 1)
4670             /* We didn't make a PLT entry for this symbol.  This
4671                happens when statically linking PIC code, or when
4672                using -Bsymbolic.  */
4673             break;
4674
4675           if (splt == NULL)
4676             break;
4677
4678           relocation = (splt->output_section->vma
4679                         + splt->output_offset
4680                         + h->plt.offset + 4)
4681                        - (input_section->output_section->vma
4682                           + input_section->output_offset
4683                           + rel->r_offset);
4684
4685           break;
4686
4687         case R_NDS32_GOTPC20:
4688           /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4689              ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
4690           relocation = elf_gp (output_bfd);
4691           break;
4692
4693         case R_NDS32_GOTPC_HI20:
4694         case R_NDS32_GOTPC_LO12:
4695             {
4696               /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4697                  bl .+4
4698                  seth rx,#high(_GLOBAL_OFFSET_TABLE_)
4699                  or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4700                  or
4701                  bl .+4
4702                  seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
4703                  add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4704                */
4705               relocation = elf_gp (output_bfd);
4706               relocation -= (input_section->output_section->vma
4707                              + input_section->output_offset + rel->r_offset);
4708               break;
4709             }
4710
4711         case R_NDS32_GOT20:
4712           /* Fall through.  */
4713         case R_NDS32_GOT_HI20:
4714         case R_NDS32_GOT_LO12:
4715         case R_NDS32_GOT_LO15:
4716         case R_NDS32_GOT_LO19:
4717           /* Relocation is to the entry for this symbol in the global
4718              offset table.  */
4719           BFD_ASSERT (sgot != NULL);
4720
4721           if (h != NULL)
4722             {
4723               bfd_boolean dyn;
4724               bfd_vma off;
4725
4726               off = h->got.offset;
4727               BFD_ASSERT (off != (bfd_vma) - 1);
4728               dyn = htab->root.dynamic_sections_created;
4729               if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4730                                                     bfd_link_pic (info),
4731                                                     h)
4732                   || (bfd_link_pic (info)
4733                       && (info->symbolic
4734                           || h->dynindx == -1
4735                           || h->forced_local) && h->def_regular))
4736                 {
4737                   /* This is actually a static link, or it is a
4738                      -Bsymbolic link and the symbol is defined
4739                      locally, or the symbol was forced to be local
4740                      because of a version file.  We must initialize
4741                      this entry in the global offset table.  Since the
4742                      offset must always be a multiple of 4, we use the
4743                      least significant bit to record whether we have
4744                      initialized it already.
4745
4746                      When doing a dynamic link, we create a .rela.got
4747                      relocation entry to initialize the value.  This
4748                      is done in the finish_dynamic_symbol routine.  */
4749                   if ((off & 1) != 0)
4750                     off &= ~1;
4751                   else
4752                     {
4753                       bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4754                       h->got.offset |= 1;
4755                     }
4756                 }
4757               relocation = sgot->output_section->vma + sgot->output_offset + off
4758                            - elf_gp (output_bfd);
4759             }
4760           else
4761             {
4762               bfd_vma off;
4763               bfd_byte *loc;
4764
4765               BFD_ASSERT (local_got_offsets != NULL
4766                           && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4767
4768               off = local_got_offsets[r_symndx];
4769
4770               /* The offset must always be a multiple of 4.  We use
4771                  the least significant bit to record whether we have
4772                  already processed this entry.  */
4773               if ((off & 1) != 0)
4774                 off &= ~1;
4775               else
4776                 {
4777                   bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4778
4779                   if (bfd_link_pic (info))
4780                     {
4781                       asection *srelgot;
4782                       Elf_Internal_Rela outrel;
4783
4784                       /* We need to generate a R_NDS32_RELATIVE reloc
4785                          for the dynamic linker.  */
4786                       srelgot = htab->root.srelgot;
4787                       BFD_ASSERT (srelgot != NULL);
4788
4789                       outrel.r_offset = (elf_gp (output_bfd)
4790                                          + sgot->output_offset + off);
4791                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4792                       outrel.r_addend = relocation;
4793                       loc = srelgot->contents;
4794                       loc +=
4795                         srelgot->reloc_count * sizeof (Elf32_External_Rela);
4796                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4797                       ++srelgot->reloc_count;
4798                     }
4799                   local_got_offsets[r_symndx] |= 1;
4800                 }
4801               relocation = sgot->output_section->vma + sgot->output_offset + off
4802                            - elf_gp (output_bfd);
4803             }
4804
4805           break;
4806
4807         case R_NDS32_16_RELA:
4808         case R_NDS32_20_RELA:
4809         case R_NDS32_5_RELA:
4810         case R_NDS32_32_RELA:
4811         case R_NDS32_9_PCREL_RELA:
4812         case R_NDS32_WORD_9_PCREL_RELA:
4813         case R_NDS32_10_UPCREL_RELA:
4814         case R_NDS32_15_PCREL_RELA:
4815         case R_NDS32_17_PCREL_RELA:
4816         case R_NDS32_25_PCREL_RELA:
4817         case R_NDS32_HI20_RELA:
4818         case R_NDS32_LO12S3_RELA:
4819         case R_NDS32_LO12S2_RELA:
4820         case R_NDS32_LO12S2_DP_RELA:
4821         case R_NDS32_LO12S2_SP_RELA:
4822         case R_NDS32_LO12S1_RELA:
4823         case R_NDS32_LO12S0_RELA:
4824         case R_NDS32_LO12S0_ORI_RELA:
4825           if (bfd_link_pic (info) && r_symndx != 0
4826               && (input_section->flags & SEC_ALLOC) != 0
4827               && (eliminate_gc_relocs == 0
4828                   || (sec && (sec->flags & SEC_EXCLUDE) == 0))
4829               && ((r_type != R_NDS32_9_PCREL_RELA
4830                    && r_type != R_NDS32_WORD_9_PCREL_RELA
4831                    && r_type != R_NDS32_10_UPCREL_RELA
4832                    && r_type != R_NDS32_15_PCREL_RELA
4833                    && r_type != R_NDS32_17_PCREL_RELA
4834                    && r_type != R_NDS32_25_PCREL_RELA
4835                    && !(r_type == R_NDS32_32_RELA
4836                         && strcmp (input_section->name, ".eh_frame") == 0))
4837                   || (h != NULL && h->dynindx != -1
4838                       && (!info->symbolic || !h->def_regular))))
4839             {
4840               Elf_Internal_Rela outrel;
4841               bfd_boolean skip, relocate;
4842               bfd_byte *loc;
4843
4844               /* When generating a shared object, these relocations
4845                  are copied into the output file to be resolved at run
4846                  time.  */
4847
4848               if (sreloc == NULL)
4849                 {
4850                   const char *name;
4851
4852                   name = bfd_elf_string_from_elf_section
4853                     (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
4854                      elf_section_data (input_section)->rela.hdr->sh_name);
4855                   if (name == NULL)
4856                     return FALSE;
4857
4858                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4859                               && strcmp (bfd_get_section_name (input_bfd,
4860                                                                input_section),
4861                                          name + 5) == 0);
4862
4863                   sreloc = bfd_get_section_by_name (dynobj, name);
4864                   BFD_ASSERT (sreloc != NULL);
4865                 }
4866
4867               skip = FALSE;
4868               relocate = FALSE;
4869
4870               outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4871                                                          info,
4872                                                          input_section,
4873                                                          rel->r_offset);
4874               if (outrel.r_offset == (bfd_vma) - 1)
4875                 skip = TRUE;
4876               else if (outrel.r_offset == (bfd_vma) - 2)
4877                 skip = TRUE, relocate = TRUE;
4878               outrel.r_offset += (input_section->output_section->vma
4879                                   + input_section->output_offset);
4880
4881               if (skip)
4882                 memset (&outrel, 0, sizeof outrel);
4883               else if (r_type == R_NDS32_17_PCREL_RELA
4884                        || r_type == R_NDS32_15_PCREL_RELA
4885                        || r_type == R_NDS32_25_PCREL_RELA)
4886                 {
4887                   BFD_ASSERT (h != NULL && h->dynindx != -1);
4888                   outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4889                   outrel.r_addend = rel->r_addend;
4890                 }
4891               else
4892                 {
4893                   /* h->dynindx may be -1 if this symbol was marked to
4894                      become local.  */
4895                   if (h == NULL
4896                       || ((info->symbolic || h->dynindx == -1)
4897                           && h->def_regular))
4898                     {
4899                       relocate = TRUE;
4900                       outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4901                       outrel.r_addend = relocation + rel->r_addend;
4902                     }
4903                   else
4904                     {
4905                       BFD_ASSERT (h->dynindx != -1);
4906                       outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4907                       outrel.r_addend = rel->r_addend;
4908                     }
4909                 }
4910
4911               loc = sreloc->contents;
4912               loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
4913               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4914               ++sreloc->reloc_count;
4915
4916               /* If this reloc is against an external symbol, we do
4917                  not want to fiddle with the addend.  Otherwise, we
4918                  need to include the symbol value so that it becomes
4919                  an addend for the dynamic reloc.  */
4920               if (!relocate)
4921                 continue;
4922             }
4923           break;
4924
4925         case R_NDS32_25_ABS_RELA:
4926           if (bfd_link_pic (info))
4927             {
4928               _bfd_error_handler
4929                 (_("%B: warning: cannot deal R_NDS32_25_ABS_RELA in shared "
4930                    "mode."), input_bfd);
4931               return FALSE;
4932             }
4933           break;
4934
4935         case R_NDS32_9_PCREL:
4936           r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
4937                                           contents, offset,
4938                                           sec, relocation, addend);
4939           goto check_reloc;
4940
4941         case R_NDS32_HI20:
4942             {
4943               Elf_Internal_Rela *lorel;
4944
4945               /* We allow an arbitrary number of HI20 relocs before the
4946                  LO12 reloc.  This permits gcc to emit the HI and LO relocs
4947                  itself.  */
4948               for (lorel = rel + 1;
4949                    (lorel < relend
4950                     && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
4951                 continue;
4952               if (lorel < relend
4953                   && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
4954                       || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
4955                       || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
4956                       || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
4957                 {
4958                   nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
4959                                            contents, relocation + addend);
4960                   r = bfd_reloc_ok;
4961                 }
4962               else
4963                 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4964                                               contents, offset, relocation,
4965                                               addend);
4966             }
4967
4968           goto check_reloc;
4969
4970         case R_NDS32_GOT17S2_RELA:
4971         case R_NDS32_GOT15S2_RELA:
4972             {
4973               bfd_vma off;
4974
4975               BFD_ASSERT (sgot != NULL);
4976
4977               if (h != NULL)
4978                 {
4979                   bfd_boolean dyn;
4980
4981                   off = h->got.offset;
4982                   BFD_ASSERT (off != (bfd_vma) - 1);
4983
4984                   dyn = htab->root.dynamic_sections_created;
4985                   if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
4986                       (dyn, bfd_link_pic (info), h)
4987                       || (bfd_link_pic (info)
4988                           && (info->symbolic
4989                               || h->dynindx == -1
4990                               || h->forced_local)
4991                           && h->def_regular))
4992                     {
4993                       /* This is actually a static link, or it is a
4994                          -Bsymbolic link and the symbol is defined
4995                          locally, or the symbol was forced to be local
4996                          because of a version file.  We must initialize
4997                          this entry in the global offset table.  Since the
4998                          offset must always be a multiple of 4, we use the
4999                          least significant bit to record whether we have
5000                          initialized it already.
5001
5002                          When doing a dynamic link, we create a .rela.got
5003                          relocation entry to initialize the value.  This
5004                          is done in the finish_dynamic_symbol routine.  */
5005                       if ((off & 1) != 0)
5006                         off &= ~1;
5007                       else
5008                         {
5009                           bfd_put_32 (output_bfd, relocation,
5010                                       sgot->contents + off);
5011                           h->got.offset |= 1;
5012                         }
5013                     }
5014                 }
5015               else
5016                 {
5017                   bfd_byte *loc;
5018
5019                   BFD_ASSERT (local_got_offsets != NULL
5020                               && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5021
5022                   off = local_got_offsets[r_symndx];
5023
5024                   /* The offset must always be a multiple of 4.  We use
5025                      the least significant bit to record whether we have
5026                      already processed this entry.  */
5027                   if ((off & 1) != 0)
5028                     off &= ~1;
5029                   else
5030                     {
5031                       bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5032
5033                       if (bfd_link_pic (info))
5034                         {
5035                           asection *srelgot;
5036                           Elf_Internal_Rela outrel;
5037
5038                           /* We need to generate a R_NDS32_RELATIVE reloc
5039                              for the dynamic linker.  */
5040                           srelgot = htab->root.srelgot;
5041                           BFD_ASSERT (srelgot != NULL);
5042
5043                           outrel.r_offset = (elf_gp (output_bfd)
5044                                              + sgot->output_offset + off);
5045                           outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5046                           outrel.r_addend = relocation;
5047                           loc = srelgot->contents;
5048                           loc +=
5049                             srelgot->reloc_count * sizeof (Elf32_External_Rela);
5050                           bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5051                           ++srelgot->reloc_count;
5052                         }
5053                       local_got_offsets[r_symndx] |= 1;
5054                     }
5055                 }
5056               relocation = sgot->output_section->vma + sgot->output_offset + off
5057                            - elf_gp (output_bfd);
5058             }
5059           if (relocation & align)
5060             {
5061               /* Incorrect alignment.  */
5062               _bfd_error_handler
5063                 (_("%B: warning: unaligned access to GOT entry."), input_bfd);
5064               ret = FALSE;
5065               r = bfd_reloc_dangerous;
5066               goto check_reloc;
5067             }
5068           break;
5069
5070         case R_NDS32_SDA16S3_RELA:
5071         case R_NDS32_SDA15S3_RELA:
5072         case R_NDS32_SDA15S3:
5073           align = 0x7;
5074           goto handle_sda;
5075
5076         case R_NDS32_SDA17S2_RELA:
5077         case R_NDS32_SDA15S2_RELA:
5078         case R_NDS32_SDA12S2_SP_RELA:
5079         case R_NDS32_SDA12S2_DP_RELA:
5080         case R_NDS32_SDA15S2:
5081         case R_NDS32_SDA_FP7U2_RELA:
5082           align = 0x3;
5083           goto handle_sda;
5084
5085         case R_NDS32_SDA18S1_RELA:
5086         case R_NDS32_SDA15S1_RELA:
5087         case R_NDS32_SDA15S1:
5088           align = 0x1;
5089           goto handle_sda;
5090
5091         case R_NDS32_SDA19S0_RELA:
5092         case R_NDS32_SDA15S0_RELA:
5093         case R_NDS32_SDA15S0:
5094             {
5095               align = 0x0;
5096 handle_sda:
5097               BFD_ASSERT (sec != NULL);
5098
5099               /* If the symbol is in the abs section, the out_bfd will be null.
5100                  This happens when the relocation has a symbol@GOTOFF.  */
5101               r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5102               if (r != bfd_reloc_ok)
5103                 {
5104                   _bfd_error_handler
5105                     (_("%B: warning: relocate SDA_BASE failed."), input_bfd);
5106                   ret = FALSE;
5107                   goto check_reloc;
5108                 }
5109
5110               /* At this point `relocation' contains the object's
5111                  address.  */
5112               if (r_type == R_NDS32_SDA_FP7U2_RELA)
5113                 {
5114                   relocation -= fpbase_addr;
5115                 }
5116               else
5117                 relocation -= gp;
5118               /* Now it contains the offset from _SDA_BASE_.  */
5119
5120               /* Make sure alignment is correct.  */
5121
5122               if (relocation & align)
5123                 {
5124                   /* Incorrect alignment.  */
5125                   _bfd_error_handler
5126                     /* xgettext:c-format */
5127                     (_("%B(%A): warning: unaligned small data access of type %d."),
5128                      input_bfd, input_section, r_type);
5129                   ret = FALSE;
5130                   goto check_reloc;
5131                 }
5132             }
5133
5134           break;
5135         case R_NDS32_17IFC_PCREL_RELA:
5136         case R_NDS32_10IFCU_PCREL_RELA:
5137           /* do nothing */
5138           break;
5139
5140         case R_NDS32_TLS_LE_HI20:
5141         case R_NDS32_TLS_LE_LO12:
5142         case R_NDS32_TLS_LE_20:
5143         case R_NDS32_TLS_LE_15S0:
5144         case R_NDS32_TLS_LE_15S1:
5145         case R_NDS32_TLS_LE_15S2:
5146           if (elf_hash_table (info)->tls_sec != NULL)
5147             relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5148           break;
5149         case R_NDS32_TLS_IE_HI20:
5150         case R_NDS32_TLS_IE_LO12S2:
5151           {
5152             /* Relocation is to the entry for this symbol in the global
5153                offset table.  */
5154             unsigned int tls_type;
5155             asection *srelgot;
5156             Elf_Internal_Rela outrel;
5157             bfd_vma off;
5158             bfd_byte *loc;
5159             int indx = 0;
5160
5161             BFD_ASSERT (sgot != NULL);
5162             if (h != NULL)
5163               {
5164                 bfd_boolean dyn;
5165
5166                 off = h->got.offset;
5167                 BFD_ASSERT (off != (bfd_vma) - 1);
5168                 dyn = htab->root.dynamic_sections_created;
5169                 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5170                 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5171                     && (!bfd_link_pic (info)
5172                         || !SYMBOL_REFERENCES_LOCAL (info, h)))
5173                   indx = h->dynindx;
5174               }
5175             else
5176               {
5177                 /* Never happen currently.  */
5178                 BFD_ASSERT (local_got_offsets != NULL
5179                             && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5180
5181                 off = local_got_offsets[r_symndx];
5182
5183                 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5184               }
5185             relocation = sgot->output_section->vma + sgot->output_offset + off;
5186
5187             if (r_type == R_NDS32_TLS_IE_LO12S2)
5188               break;
5189
5190             /* The offset must always be a multiple of 4.  We use
5191                the least significant bit to record whether we have
5192                already processed this entry.  */
5193             if ((off & 1) != 0)
5194               off &= ~1;
5195             else
5196               {
5197                 bfd_boolean need_relocs = FALSE;
5198                 srelgot = htab->root.srelgot;
5199                 if ((bfd_link_pic (info) || indx != 0)
5200                     && (h == NULL
5201                         || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5202                         || h->root.type != bfd_link_hash_undefweak))
5203                   {
5204                     need_relocs = TRUE;
5205                     BFD_ASSERT (srelgot != NULL);
5206                   }
5207                 if (tls_type & GOT_TLS_IE)
5208                   {
5209                     if (need_relocs)
5210                       {
5211                         if (h->dynindx == 0)
5212                           outrel.r_addend = relocation - dtpoff_base (info);
5213                         else
5214                           outrel.r_addend = 0;
5215                         outrel.r_offset = (sgot->output_section->vma
5216                                            + sgot->output_offset
5217                                            + off);
5218                         outrel.r_info =
5219                           ELF32_R_INFO (h->dynindx, R_NDS32_TLS_TPOFF);
5220
5221                         loc = srelgot->contents;
5222                         loc +=
5223                           srelgot->reloc_count * sizeof (Elf32_External_Rela);
5224                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5225                         ++srelgot->reloc_count;
5226                       }
5227                     else
5228                       bfd_put_32 (output_bfd, h->root.u.def.value - TP_OFFSET,
5229                                   sgot->contents + off);
5230                   }
5231               }
5232           }
5233         break;
5234
5235           /* DON'T   fall through.  */
5236
5237         default:
5238           /* OLD_NDS32_RELOC.  */
5239
5240           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5241                                         contents, offset, relocation, addend);
5242           goto check_reloc;
5243         }
5244
5245       switch ((int) r_type)
5246         {
5247         case R_NDS32_20_RELA:
5248         case R_NDS32_5_RELA:
5249         case R_NDS32_9_PCREL_RELA:
5250         case R_NDS32_WORD_9_PCREL_RELA:
5251         case R_NDS32_10_UPCREL_RELA:
5252         case R_NDS32_15_PCREL_RELA:
5253         case R_NDS32_17_PCREL_RELA:
5254         case R_NDS32_25_PCREL_RELA:
5255         case R_NDS32_25_ABS_RELA:
5256         case R_NDS32_HI20_RELA:
5257         case R_NDS32_LO12S3_RELA:
5258         case R_NDS32_LO12S2_RELA:
5259         case R_NDS32_LO12S2_DP_RELA:
5260         case R_NDS32_LO12S2_SP_RELA:
5261         case R_NDS32_LO12S1_RELA:
5262         case R_NDS32_LO12S0_RELA:
5263         case R_NDS32_LO12S0_ORI_RELA:
5264         case R_NDS32_SDA16S3_RELA:
5265         case R_NDS32_SDA17S2_RELA:
5266         case R_NDS32_SDA18S1_RELA:
5267         case R_NDS32_SDA19S0_RELA:
5268         case R_NDS32_SDA15S3_RELA:
5269         case R_NDS32_SDA15S2_RELA:
5270         case R_NDS32_SDA12S2_DP_RELA:
5271         case R_NDS32_SDA12S2_SP_RELA:
5272         case R_NDS32_SDA15S1_RELA:
5273         case R_NDS32_SDA15S0_RELA:
5274         case R_NDS32_SDA_FP7U2_RELA:
5275         case R_NDS32_9_PLTREL:
5276         case R_NDS32_25_PLTREL:
5277         case R_NDS32_GOT20:
5278         case R_NDS32_GOT_HI20:
5279         case R_NDS32_GOT_LO12:
5280         case R_NDS32_GOT_LO15:
5281         case R_NDS32_GOT_LO19:
5282         case R_NDS32_GOT15S2_RELA:
5283         case R_NDS32_GOT17S2_RELA:
5284         case R_NDS32_GOTPC20:
5285         case R_NDS32_GOTPC_HI20:
5286         case R_NDS32_GOTPC_LO12:
5287         case R_NDS32_GOTOFF:
5288         case R_NDS32_GOTOFF_HI20:
5289         case R_NDS32_GOTOFF_LO12:
5290         case R_NDS32_GOTOFF_LO15:
5291         case R_NDS32_GOTOFF_LO19:
5292         case R_NDS32_PLTREL_HI20:
5293         case R_NDS32_PLTREL_LO12:
5294         case R_NDS32_PLT_GOTREL_HI20:
5295         case R_NDS32_PLT_GOTREL_LO12:
5296         case R_NDS32_PLT_GOTREL_LO15:
5297         case R_NDS32_PLT_GOTREL_LO19:
5298         case R_NDS32_PLT_GOTREL_LO20:
5299         case R_NDS32_17IFC_PCREL_RELA:
5300         case R_NDS32_10IFCU_PCREL_RELA:
5301         case R_NDS32_TLS_LE_HI20:
5302         case R_NDS32_TLS_LE_LO12:
5303         case R_NDS32_TLS_IE_HI20:
5304         case R_NDS32_TLS_IE_LO12S2:
5305         case R_NDS32_TLS_LE_20:
5306         case R_NDS32_TLS_LE_15S0:
5307         case R_NDS32_TLS_LE_15S1:
5308         case R_NDS32_TLS_LE_15S2:
5309           /* Instruction related relocs must handle endian properly.  */
5310           /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
5311           r = nds32_elf_final_link_relocate (howto, input_bfd,
5312                                              input_section, contents,
5313                                              rel->r_offset, relocation,
5314                                              rel->r_addend);
5315           break;
5316
5317         default:
5318           /* All other relocs can use default handler.  */
5319           r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5320                                         contents, rel->r_offset,
5321                                         relocation, rel->r_addend);
5322           break;
5323         }
5324
5325 check_reloc:
5326
5327       if (r != bfd_reloc_ok)
5328         {
5329           /* FIXME: This should be generic enough to go in a utility.  */
5330           const char *name;
5331
5332           if (h != NULL)
5333             name = h->root.root.string;
5334           else
5335             {
5336               name = bfd_elf_string_from_elf_section
5337                       (input_bfd, symtab_hdr->sh_link, sym->st_name);
5338               if (name == NULL || *name == '\0')
5339                 name = bfd_section_name (input_bfd, sec);
5340             }
5341
5342           if (errmsg != NULL)
5343             goto common_error;
5344
5345           switch (r)
5346             {
5347             case bfd_reloc_overflow:
5348               (*info->callbacks->reloc_overflow)
5349                 (info, (h ? &h->root : NULL), name, howto->name,
5350                  (bfd_vma) 0, input_bfd, input_section, offset);
5351               break;
5352
5353             case bfd_reloc_undefined:
5354               (*info->callbacks->undefined_symbol)
5355                 (info, name, input_bfd, input_section, offset, TRUE);
5356               break;
5357
5358             case bfd_reloc_outofrange:
5359               errmsg = _("internal error: out of range error");
5360               goto common_error;
5361
5362             case bfd_reloc_notsupported:
5363               errmsg = _("internal error: unsupported relocation error");
5364               goto common_error;
5365
5366             case bfd_reloc_dangerous:
5367               errmsg = _("internal error: dangerous error");
5368               goto common_error;
5369
5370             default:
5371               errmsg = _("internal error: unknown error");
5372               /* Fall through.  */
5373
5374             common_error:
5375               (*info->callbacks->warning) (info, errmsg, name, input_bfd,
5376                                            input_section, offset);
5377               break;
5378             }
5379         }
5380     }
5381
5382   return ret;
5383 }
5384
5385 /* Finish up dynamic symbol handling.  We set the contents of various
5386    dynamic sections here.  */
5387
5388 static bfd_boolean
5389 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5390                                  struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
5391 {
5392   struct elf_nds32_link_hash_table *htab;
5393   bfd_byte *loc;
5394
5395   htab = nds32_elf_hash_table (info);
5396
5397   if (h->plt.offset != (bfd_vma) - 1)
5398     {
5399       asection *splt;
5400       asection *sgot;
5401       asection *srela;
5402
5403       bfd_vma plt_index;
5404       bfd_vma got_offset;
5405       bfd_vma local_plt_offset;
5406       Elf_Internal_Rela rela;
5407
5408       /* This symbol has an entry in the procedure linkage table.  Set
5409          it up.  */
5410
5411       BFD_ASSERT (h->dynindx != -1);
5412
5413       splt = htab->root.splt;
5414       sgot = htab->root.sgotplt;
5415       srela = htab->root.srelplt;
5416       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
5417
5418       /* Get the index in the procedure linkage table which
5419          corresponds to this symbol.  This is the index of this symbol
5420          in all the symbols for which we are making plt entries.  The
5421          first entry in the procedure linkage table is reserved.  */
5422       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
5423
5424       /* Get the offset into the .got table of the entry that
5425          corresponds to this function.  Each .got entry is 4 bytes.
5426          The first three are reserved.  */
5427       got_offset = (plt_index + 3) * 4;
5428
5429       /* Fill in the entry in the procedure linkage table.  */
5430       if (!bfd_link_pic (info))
5431         {
5432           unsigned long insn;
5433
5434           insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
5435                                       + sgot->output_offset + got_offset) >> 12)
5436                                     & 0xfffff);
5437           bfd_putb32 (insn, splt->contents + h->plt.offset);
5438
5439           insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
5440                                       + sgot->output_offset + got_offset) & 0x0fff)
5441                                     >> 2);
5442           bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5443
5444           insn = PLT_ENTRY_WORD2;
5445           bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5446
5447           insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
5448           bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5449
5450           insn = PLT_ENTRY_WORD4
5451                  + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
5452           bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5453           local_plt_offset = 12;
5454         }
5455       else
5456         {
5457           /* sda_base must be set at this time.  */
5458           unsigned long insn;
5459           long offset;
5460
5461           /* FIXME, sda_base is 65536, it will damage opcode.  */
5462           /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5463           offset = sgot->output_section->vma + sgot->output_offset + got_offset
5464                    - elf_gp (output_bfd);
5465           insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
5466           bfd_putb32 (insn, splt->contents + h->plt.offset);
5467
5468           insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
5469           bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5470
5471           insn = PLT_PIC_ENTRY_WORD2;
5472           bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5473
5474           insn = PLT_PIC_ENTRY_WORD3;
5475           bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5476
5477           insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
5478           bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5479
5480           insn = PLT_PIC_ENTRY_WORD5
5481             + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
5482           bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
5483
5484           local_plt_offset = 16;
5485         }
5486
5487       /* Fill in the entry in the global offset table,
5488          so it will fall through to the next instruction for the first time.  */
5489       bfd_put_32 (output_bfd,
5490                   (splt->output_section->vma + splt->output_offset
5491                    + h->plt.offset + local_plt_offset),
5492                   sgot->contents + got_offset);
5493
5494       /* Fill in the entry in the .rela.plt section.  */
5495       rela.r_offset = (sgot->output_section->vma
5496                        + sgot->output_offset + got_offset);
5497       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
5498       rela.r_addend = 0;
5499       loc = srela->contents;
5500       loc += plt_index * sizeof (Elf32_External_Rela);
5501       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5502
5503       if (!h->def_regular)
5504         {
5505           /* Mark the symbol as undefined, rather than as defined in
5506              the .plt section.  Leave the value alone.  */
5507           sym->st_shndx = SHN_UNDEF;
5508           if (!h->ref_regular_nonweak)
5509             sym->st_value = 0;
5510         }
5511     }
5512
5513   if (h->got.offset != (bfd_vma) - 1)
5514     {
5515       asection *sgot;
5516       asection *srela;
5517       Elf_Internal_Rela rela;
5518
5519       /* This symbol has an entry in the global offset table.
5520          Set it up.  */
5521
5522       sgot = htab->root.sgot;
5523       srela = htab->root.srelgot;
5524       BFD_ASSERT (sgot != NULL && srela != NULL);
5525
5526       rela.r_offset = (sgot->output_section->vma
5527                        + sgot->output_offset + (h->got.offset & ~1));
5528
5529       /* If this is a -Bsymbolic link, and the symbol is defined
5530          locally, we just want to emit a RELATIVE reloc.  Likewise if
5531          the symbol was forced to be local because of a version file.
5532          The entry in the global offset table will already have been
5533          initialized in the relocate_section function.  */
5534       if (bfd_link_pic (info)
5535           && (info->symbolic
5536               || h->dynindx == -1 || h->forced_local) && h->def_regular)
5537         {
5538           rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5539           rela.r_addend = (h->root.u.def.value
5540                            + h->root.u.def.section->output_section->vma
5541                            + h->root.u.def.section->output_offset);
5542         }
5543       else
5544         {
5545           BFD_ASSERT ((h->got.offset & 1) == 0);
5546           bfd_put_32 (output_bfd, (bfd_vma) 0,
5547                       sgot->contents + h->got.offset);
5548           rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
5549           rela.r_addend = 0;
5550         }
5551
5552       loc = srela->contents;
5553       loc += srela->reloc_count * sizeof (Elf32_External_Rela);
5554       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5555       ++srela->reloc_count;
5556     }
5557
5558   if (h->needs_copy)
5559     {
5560       asection *s;
5561       Elf_Internal_Rela rela;
5562
5563       /* This symbols needs a copy reloc.  Set it up.  */
5564
5565       BFD_ASSERT (h->dynindx != -1
5566                   && (h->root.type == bfd_link_hash_defined
5567                       || h->root.type == bfd_link_hash_defweak));
5568
5569       s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
5570       BFD_ASSERT (s != NULL);
5571
5572       rela.r_offset = (h->root.u.def.value
5573                        + h->root.u.def.section->output_section->vma
5574                        + h->root.u.def.section->output_offset);
5575       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
5576       rela.r_addend = 0;
5577       loc = s->contents;
5578       loc += s->reloc_count * sizeof (Elf32_External_Rela);
5579       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5580       ++s->reloc_count;
5581     }
5582
5583   /* Mark some specially defined symbols as absolute.  */
5584   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5585       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5586     sym->st_shndx = SHN_ABS;
5587
5588   return TRUE;
5589 }
5590
5591
5592 /* Finish up the dynamic sections.  */
5593
5594 static bfd_boolean
5595 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
5596 {
5597   struct elf_nds32_link_hash_table *htab;
5598   bfd *dynobj;
5599   asection *sdyn;
5600   asection *sgot;
5601
5602   htab = nds32_elf_hash_table (info);
5603   dynobj = htab->root.dynobj;
5604
5605   sgot = htab->root.sgotplt;
5606   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5607
5608   if (htab->root.dynamic_sections_created)
5609     {
5610       asection *splt;
5611       Elf32_External_Dyn *dyncon, *dynconend;
5612
5613       BFD_ASSERT (sgot != NULL && sdyn != NULL);
5614
5615       dyncon = (Elf32_External_Dyn *) sdyn->contents;
5616       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5617
5618       for (; dyncon < dynconend; dyncon++)
5619         {
5620           Elf_Internal_Dyn dyn;
5621           asection *s;
5622
5623           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5624
5625           switch (dyn.d_tag)
5626             {
5627             default:
5628               break;
5629
5630             case DT_PLTGOT:
5631               s = htab->root.sgotplt;
5632               goto get_vma;
5633             case DT_JMPREL:
5634               s = htab->root.srelplt;
5635             get_vma:
5636               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5637               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5638               break;
5639
5640             case DT_PLTRELSZ:
5641               s = htab->root.srelplt;
5642               dyn.d_un.d_val = s->size;
5643               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5644               break;
5645             }
5646         }
5647
5648       /* Fill in the first entry in the procedure linkage table.  */
5649       splt = htab->root.splt;
5650       if (splt && splt->size > 0)
5651         {
5652           if (bfd_link_pic (info))
5653             {
5654               unsigned long insn;
5655               long offset;
5656
5657               /* FIXME, sda_base is 65536, it will damage opcode.  */
5658               /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5659               offset = sgot->output_section->vma + sgot->output_offset + 4
5660                        - elf_gp (output_bfd);
5661               insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
5662               bfd_putb32 (insn, splt->contents);
5663
5664               /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
5665               /* here has a typo?  */
5666               insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
5667               bfd_putb32 (insn, splt->contents + 4);
5668
5669               insn = PLT0_PIC_ENTRY_WORD2;
5670               bfd_putb32 (insn, splt->contents + 8);
5671
5672               insn = PLT0_PIC_ENTRY_WORD3;
5673               bfd_putb32 (insn, splt->contents + 12);
5674
5675               insn = PLT0_PIC_ENTRY_WORD4;
5676               bfd_putb32 (insn, splt->contents + 16);
5677
5678               insn = PLT0_PIC_ENTRY_WORD5;
5679               bfd_putb32 (insn, splt->contents + 20);
5680             }
5681           else
5682             {
5683               unsigned long insn;
5684               unsigned long addr;
5685
5686               /* addr = .got + 4 */
5687               addr = sgot->output_section->vma + sgot->output_offset + 4;
5688               insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
5689               bfd_putb32 (insn, splt->contents);
5690
5691               insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
5692               bfd_putb32 (insn, splt->contents + 4);
5693
5694               insn = PLT0_ENTRY_WORD2;
5695               bfd_putb32 (insn, splt->contents + 8);
5696
5697               insn = PLT0_ENTRY_WORD3;
5698               bfd_putb32 (insn, splt->contents + 12);
5699
5700               insn = PLT0_ENTRY_WORD4;
5701               bfd_putb32 (insn, splt->contents + 16);
5702             }
5703
5704           elf_section_data (splt->output_section)->this_hdr.sh_entsize =
5705             PLT_ENTRY_SIZE;
5706         }
5707     }
5708
5709   /* Fill in the first three entries in the global offset table.  */
5710   if (sgot && sgot->size > 0)
5711     {
5712       if (sdyn == NULL)
5713         bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5714       else
5715         bfd_put_32 (output_bfd,
5716                     sdyn->output_section->vma + sdyn->output_offset,
5717                     sgot->contents);
5718       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5719       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5720
5721       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5722     }
5723
5724   return TRUE;
5725 }
5726 \f
5727
5728 /* Set the right machine number.  */
5729
5730 static bfd_boolean
5731 nds32_elf_object_p (bfd *abfd)
5732 {
5733   static unsigned int cur_arch = 0;
5734
5735   if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
5736     {
5737       /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
5738       cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
5739     }
5740
5741   switch (cur_arch)
5742     {
5743     default:
5744     case E_N1_ARCH:
5745       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
5746       break;
5747     case E_N1H_ARCH:
5748       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
5749       break;
5750     case E_NDS_ARCH_STAR_V2_0:
5751       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
5752       break;
5753     case E_NDS_ARCH_STAR_V3_0:
5754       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
5755       break;
5756     case E_NDS_ARCH_STAR_V3_M:
5757       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
5758       break;
5759     }
5760
5761   return TRUE;
5762 }
5763
5764 /* Store the machine number in the flags field.  */
5765
5766 static void
5767 nds32_elf_final_write_processing (bfd *abfd,
5768                                   bfd_boolean linker ATTRIBUTE_UNUSED)
5769 {
5770   unsigned long val;
5771   static unsigned int cur_mach = 0;
5772
5773   if (bfd_mach_n1 != bfd_get_mach (abfd))
5774     {
5775       cur_mach = bfd_get_mach (abfd);
5776     }
5777
5778   switch (cur_mach)
5779     {
5780     case bfd_mach_n1:
5781       /* Only happen when object is empty, since the case is abandon.  */
5782       val = E_N1_ARCH;
5783       val |= E_NDS_ABI_AABI;
5784       val |= E_NDS32_ELF_VER_1_4;
5785       break;
5786     case bfd_mach_n1h:
5787       val = E_N1H_ARCH;
5788       break;
5789     case bfd_mach_n1h_v2:
5790       val = E_NDS_ARCH_STAR_V2_0;
5791       break;
5792     case bfd_mach_n1h_v3:
5793       val = E_NDS_ARCH_STAR_V3_0;
5794       break;
5795     case bfd_mach_n1h_v3m:
5796       val = E_NDS_ARCH_STAR_V3_M;
5797       break;
5798     default:
5799       val = 0;
5800       break;
5801     }
5802
5803   elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
5804   elf_elfheader (abfd)->e_flags |= val;
5805 }
5806
5807 /* Function to keep NDS32 specific file flags.  */
5808
5809 static bfd_boolean
5810 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
5811 {
5812   BFD_ASSERT (!elf_flags_init (abfd)
5813               || elf_elfheader (abfd)->e_flags == flags);
5814
5815   elf_elfheader (abfd)->e_flags = flags;
5816   elf_flags_init (abfd) = TRUE;
5817   return TRUE;
5818 }
5819
5820 static unsigned int
5821 convert_e_flags (unsigned int e_flags, unsigned int arch)
5822 {
5823   if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
5824     {
5825       /* From 0.9 to 1.0.  */
5826       e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
5827
5828       /* Invert E_NDS32_HAS_NO_MAC_INST.  */
5829       e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5830       if (arch == E_NDS_ARCH_STAR_V1_0)
5831         {
5832           /* Done.  */
5833           return e_flags;
5834         }
5835     }
5836
5837   /* From 1.0 to 2.0.  */
5838   e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
5839
5840   /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
5841   e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
5842
5843   /* Invert E_NDS32_HAS_NO_MAC_INST.  */
5844   e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5845   return e_flags;
5846 }
5847
5848 static bfd_boolean
5849 nds32_check_vec_size (bfd *ibfd)
5850 {
5851   static unsigned int nds32_vec_size = 0;
5852
5853   asection *sec_t = NULL;
5854   bfd_byte *contents = NULL;
5855
5856   sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
5857
5858   if (sec_t && sec_t->size >= 4)
5859     {
5860       /* Get vec_size in file.  */
5861       unsigned int flag_t;
5862
5863       nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
5864       flag_t = bfd_get_32 (ibfd, contents);
5865
5866       /* The value could only be 4 or 16.  */
5867
5868       if (!nds32_vec_size)
5869         /* Set if not set yet.  */
5870         nds32_vec_size = (flag_t & 0x3);
5871       else if (nds32_vec_size != (flag_t & 0x3))
5872         {
5873           _bfd_error_handler
5874             /* xgettext:c-format */
5875             (_("%B: ISR vector size mismatch"
5876                " with previous modules, previous %u-byte, current %u-byte"),
5877              ibfd,
5878              nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
5879              (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
5880           return FALSE;
5881         }
5882       else
5883         /* Only keep the first vec_size section.  */
5884         sec_t->flags |= SEC_EXCLUDE;
5885     }
5886
5887   return TRUE;
5888 }
5889
5890 /* Merge backend specific data from an object file to the output
5891    object file when linking.  */
5892
5893 static bfd_boolean
5894 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5895 {
5896   bfd *obfd = info->output_bfd;
5897   flagword out_flags;
5898   flagword in_flags;
5899   flagword out_16regs;
5900   flagword in_no_mac;
5901   flagword out_no_mac;
5902   flagword in_16regs;
5903   flagword out_version;
5904   flagword in_version;
5905   flagword out_fpu_config;
5906   flagword in_fpu_config;
5907
5908   /* TODO: Revise to use object-attributes instead.  */
5909   if (!nds32_check_vec_size (ibfd))
5910     return FALSE;
5911
5912   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5913       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5914     return TRUE;
5915
5916   if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
5917     {
5918       _bfd_error_handler
5919         (_("%B: warning: Endian mismatch with previous modules."), ibfd);
5920
5921       bfd_set_error (bfd_error_bad_value);
5922       return FALSE;
5923     }
5924
5925   in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
5926   if (in_version == E_NDS32_ELF_VER_1_2)
5927     {
5928       _bfd_error_handler
5929         (_("%B: warning: Older version of object file encountered, "
5930            "Please recompile with current tool chain."), ibfd);
5931     }
5932
5933   /* We may need to merge V1 and V2 arch object files to V2.  */
5934   if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5935       != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5936     {
5937       /* Need to convert version.  */
5938       if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5939           == E_NDS_ARCH_STAR_RESERVED)
5940         {
5941           elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5942         }
5943       else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
5944                || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
5945                   > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
5946         {
5947           elf_elfheader (obfd)->e_flags =
5948             convert_e_flags (elf_elfheader (obfd)->e_flags,
5949                              (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
5950         }
5951       else
5952         {
5953           elf_elfheader (ibfd)->e_flags =
5954             convert_e_flags (elf_elfheader (ibfd)->e_flags,
5955                              (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
5956         }
5957     }
5958
5959   /* Extract some flags.  */
5960   in_flags = elf_elfheader (ibfd)->e_flags
5961              & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
5962                   | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
5963
5964   /* The following flags need special treatment.  */
5965   in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
5966   in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
5967   in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
5968
5969   /* Extract some flags.  */
5970   out_flags = elf_elfheader (obfd)->e_flags
5971               & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
5972                    | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
5973
5974   /* The following flags need special treatment.  */
5975   out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
5976   out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
5977   out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
5978   out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
5979   if (!elf_flags_init (obfd))
5980     {
5981       /* If the input is the default architecture then do not
5982          bother setting the flags for the output architecture,
5983          instead allow future merges to do this.  If no future
5984          merges ever set these flags then they will retain their
5985          unitialised values, which surprise surprise, correspond
5986          to the default values.  */
5987       if (bfd_get_arch_info (ibfd)->the_default)
5988         return TRUE;
5989
5990       elf_flags_init (obfd) = TRUE;
5991       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5992
5993       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
5994           && bfd_get_arch_info (obfd)->the_default)
5995         {
5996           return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
5997                                     bfd_get_mach (ibfd));
5998         }
5999
6000       return TRUE;
6001     }
6002
6003   /* Check flag compatibility.  */
6004   if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6005     {
6006       _bfd_error_handler
6007         (_("%B: error: ABI mismatch with previous modules."), ibfd);
6008
6009       bfd_set_error (bfd_error_bad_value);
6010       return FALSE;
6011     }
6012
6013   if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6014     {
6015       if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6016         {
6017           _bfd_error_handler
6018             (_("%B: error: Instruction set mismatch with previous modules."), ibfd);
6019
6020           bfd_set_error (bfd_error_bad_value);
6021           return FALSE;
6022         }
6023     }
6024
6025   /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6026      and perf ext1 and DIV are mergerd to perf ext1.  */
6027   if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6028     {
6029       elf_elfheader (obfd)->e_flags =
6030         (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6031         | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6032         | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6033            ?  E_NDS32_HAS_EXT_INST : 0)
6034         | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6035            ?  E_NDS32_HAS_EXT_INST : 0)
6036         | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6037         | ((in_version > out_version) ? out_version : in_version);
6038     }
6039   else
6040     {
6041       if (in_version != out_version)
6042         _bfd_error_handler
6043           /* xgettext:c-format */
6044           (_("%B: warning: Incompatible elf-versions %s and  %s."),
6045            ibfd, nds32_elfver_strtab[out_version],
6046            nds32_elfver_strtab[in_version]);
6047
6048       elf_elfheader (obfd)->e_flags = in_flags | out_flags
6049         | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6050         | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6051         | (in_version > out_version ?  out_version : in_version);
6052     }
6053
6054   return TRUE;
6055 }
6056
6057 /* Display the flags field.  */
6058
6059 static bfd_boolean
6060 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6061 {
6062   FILE *file = (FILE *) ptr;
6063
6064   BFD_ASSERT (abfd != NULL && ptr != NULL);
6065
6066   _bfd_elf_print_private_bfd_data (abfd, ptr);
6067
6068   fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6069
6070   switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6071     {
6072     default:
6073     case E_N1_ARCH:
6074       fprintf (file, _(": n1 instructions"));
6075       break;
6076     case E_N1H_ARCH:
6077       fprintf (file, _(": n1h instructions"));
6078       break;
6079     }
6080
6081   fputc ('\n', file);
6082
6083   return TRUE;
6084 }
6085
6086 static unsigned int
6087 nds32_elf_action_discarded (asection *sec)
6088 {
6089
6090   if (strncmp
6091       (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6092     return 0;
6093
6094   return _bfd_elf_default_action_discarded (sec);
6095 }
6096
6097 static asection *
6098 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6099                         Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6100                         Elf_Internal_Sym *sym)
6101 {
6102   if (h != NULL)
6103     switch (ELF32_R_TYPE (rel->r_info))
6104       {
6105       case R_NDS32_GNU_VTINHERIT:
6106       case R_NDS32_GNU_VTENTRY:
6107       case R_NDS32_RELA_GNU_VTINHERIT:
6108       case R_NDS32_RELA_GNU_VTENTRY:
6109         return NULL;
6110       }
6111
6112   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6113 }
6114
6115 /* Look through the relocs for a section during the first phase.
6116    Since we don't do .gots or .plts, we just need to consider the
6117    virtual table relocs for gc.  */
6118
6119 static bfd_boolean
6120 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6121                         asection *sec, const Elf_Internal_Rela *relocs)
6122 {
6123   Elf_Internal_Shdr *symtab_hdr;
6124   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6125   const Elf_Internal_Rela *rel;
6126   const Elf_Internal_Rela *rel_end;
6127   struct elf_nds32_link_hash_table *htab;
6128   bfd *dynobj;
6129   asection *sreloc = NULL;
6130
6131   if (bfd_link_relocatable (info))
6132     return TRUE;
6133
6134   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6135   sym_hashes = elf_sym_hashes (abfd);
6136   sym_hashes_end =
6137     sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6138   if (!elf_bad_symtab (abfd))
6139     sym_hashes_end -= symtab_hdr->sh_info;
6140
6141   htab = nds32_elf_hash_table (info);
6142   dynobj = htab->root.dynobj;
6143
6144   rel_end = relocs + sec->reloc_count;
6145   for (rel = relocs; rel < rel_end; rel++)
6146     {
6147       enum elf_nds32_reloc_type r_type;
6148       struct elf_link_hash_entry *h;
6149       unsigned long r_symndx;
6150       int tls_type, old_tls_type;
6151
6152       r_symndx = ELF32_R_SYM (rel->r_info);
6153       r_type = ELF32_R_TYPE (rel->r_info);
6154       if (r_symndx < symtab_hdr->sh_info)
6155         h = NULL;
6156       else
6157         {
6158           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6159           while (h->root.type == bfd_link_hash_indirect
6160                  || h->root.type == bfd_link_hash_warning)
6161             h = (struct elf_link_hash_entry *) h->root.u.i.link;
6162         }
6163
6164       /* Some relocs require a global offset table.  We create
6165          got section here, since these relocation need got section
6166          and it is not created yet.  */
6167       if (htab->root.sgot == NULL)
6168         {
6169           switch (r_type)
6170             {
6171             case R_NDS32_GOT_HI20:
6172             case R_NDS32_GOT_LO12:
6173             case R_NDS32_GOT_LO15:
6174             case R_NDS32_GOT_LO19:
6175             case R_NDS32_GOT17S2_RELA:
6176             case R_NDS32_GOT15S2_RELA:
6177             case R_NDS32_GOTOFF:
6178             case R_NDS32_GOTOFF_HI20:
6179             case R_NDS32_GOTOFF_LO12:
6180             case R_NDS32_GOTOFF_LO15:
6181             case R_NDS32_GOTOFF_LO19:
6182             case R_NDS32_GOTPC20:
6183             case R_NDS32_GOTPC_HI20:
6184             case R_NDS32_GOTPC_LO12:
6185             case R_NDS32_GOT20:
6186             case R_NDS32_TLS_IE_HI20:
6187             case R_NDS32_TLS_IE_LO12S2:
6188               if (dynobj == NULL)
6189                 htab->root.dynobj = dynobj = abfd;
6190               if (!_bfd_elf_create_got_section (dynobj, info))
6191                 return FALSE;
6192               break;
6193
6194             default:
6195               break;
6196             }
6197         }
6198
6199       switch ((int) r_type)
6200         {
6201         case R_NDS32_GOT_HI20:
6202         case R_NDS32_GOT_LO12:
6203         case R_NDS32_GOT_LO15:
6204         case R_NDS32_GOT_LO19:
6205         case R_NDS32_GOT20:
6206         case R_NDS32_TLS_IE_HI20:
6207         case R_NDS32_TLS_IE_LO12S2:
6208           switch (r_type)
6209             {
6210             case R_NDS32_TLS_IE_HI20:
6211             case R_NDS32_TLS_IE_LO12S2:
6212               tls_type = GOT_TLS_IE;
6213               break;
6214             default:
6215               tls_type = GOT_NORMAL;
6216               break;
6217             }
6218           if (h != NULL)
6219             {
6220               old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
6221               h->got.refcount += 1;
6222             }
6223           else
6224             {
6225               bfd_signed_vma *local_got_refcounts;
6226
6227               /* This is a global offset table entry for a local
6228                  symbol.  */
6229               local_got_refcounts = elf_local_got_refcounts (abfd);
6230               if (local_got_refcounts == NULL)
6231                 {
6232                   bfd_size_type size;
6233
6234                   size = symtab_hdr->sh_info;
6235                   size *= sizeof (bfd_signed_vma);
6236                   local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
6237                   if (local_got_refcounts == NULL)
6238                     return FALSE;
6239                   elf_local_got_refcounts (abfd) = local_got_refcounts;
6240                 }
6241               local_got_refcounts[r_symndx] += 1;
6242               old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
6243             }
6244
6245           /* We will already have issued an error message if there
6246              is a TLS/non-TLS mismatch, based on the symbol
6247              type.  So just combine any TLS types needed.  */
6248           if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
6249               && tls_type != GOT_NORMAL)
6250             tls_type |= old_tls_type;
6251
6252           if (old_tls_type != tls_type)
6253             {
6254               if (h != NULL)
6255                 elf32_nds32_hash_entry (h)->tls_type = tls_type;
6256               else
6257                 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
6258             }
6259           break;
6260         case R_NDS32_9_PLTREL:
6261         case R_NDS32_25_PLTREL:
6262         case R_NDS32_PLTREL_HI20:
6263         case R_NDS32_PLTREL_LO12:
6264         case R_NDS32_PLT_GOTREL_HI20:
6265         case R_NDS32_PLT_GOTREL_LO12:
6266         case R_NDS32_PLT_GOTREL_LO15:
6267         case R_NDS32_PLT_GOTREL_LO19:
6268         case R_NDS32_PLT_GOTREL_LO20:
6269
6270           /* This symbol requires a procedure linkage table entry.  We
6271              actually build the entry in adjust_dynamic_symbol,
6272              because this might be a case of linking PIC code without
6273              linking in any dynamic objects, in which case we don't
6274              need to generate a procedure linkage table after all.  */
6275
6276           /* If this is a local symbol, we resolve it directly without
6277              creating a procedure linkage table entry.  */
6278           if (h == NULL)
6279             continue;
6280
6281           if (h->forced_local)
6282             break;
6283
6284           elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
6285           h->needs_plt = 1;
6286           h->plt.refcount += 1;
6287           break;
6288
6289         case R_NDS32_16_RELA:
6290         case R_NDS32_20_RELA:
6291         case R_NDS32_5_RELA:
6292         case R_NDS32_32_RELA:
6293         case R_NDS32_HI20_RELA:
6294         case R_NDS32_LO12S3_RELA:
6295         case R_NDS32_LO12S2_RELA:
6296         case R_NDS32_LO12S2_DP_RELA:
6297         case R_NDS32_LO12S2_SP_RELA:
6298         case R_NDS32_LO12S1_RELA:
6299         case R_NDS32_LO12S0_RELA:
6300         case R_NDS32_LO12S0_ORI_RELA:
6301         case R_NDS32_SDA16S3_RELA:
6302         case R_NDS32_SDA17S2_RELA:
6303         case R_NDS32_SDA18S1_RELA:
6304         case R_NDS32_SDA19S0_RELA:
6305         case R_NDS32_SDA15S3_RELA:
6306         case R_NDS32_SDA15S2_RELA:
6307         case R_NDS32_SDA12S2_DP_RELA:
6308         case R_NDS32_SDA12S2_SP_RELA:
6309         case R_NDS32_SDA15S1_RELA:
6310         case R_NDS32_SDA15S0_RELA:
6311         case R_NDS32_SDA_FP7U2_RELA:
6312         case R_NDS32_15_PCREL_RELA:
6313         case R_NDS32_17_PCREL_RELA:
6314         case R_NDS32_25_PCREL_RELA:
6315
6316           if (h != NULL && !bfd_link_pic (info))
6317             {
6318               h->non_got_ref = 1;
6319               h->plt.refcount += 1;
6320             }
6321
6322           /* If we are creating a shared library, and this is a reloc against
6323              a global symbol, or a non PC relative reloc against a local
6324              symbol, then we need to copy the reloc into the shared library.
6325              However, if we are linking with -Bsymbolic, we do not need to
6326              copy a reloc against a global symbol which is defined in an
6327              object we are including in the link (i.e., DEF_REGULAR is set).
6328              At this point we have not seen all the input files, so it is
6329              possible that DEF_REGULAR is not set now but will be set later
6330              (it is never cleared).  We account for that possibility below by
6331              storing information in the dyn_relocs field of the hash table
6332              entry.  A similar situation occurs when creating shared libraries
6333              and symbol visibility changes render the symbol local.
6334
6335              If on the other hand, we are creating an executable, we may need
6336              to keep relocations for symbols satisfied by a dynamic library
6337              if we manage to avoid copy relocs for the symbol.  */
6338           if ((bfd_link_pic (info)
6339                && (sec->flags & SEC_ALLOC) != 0
6340                && ((r_type != R_NDS32_25_PCREL_RELA
6341                     && r_type != R_NDS32_15_PCREL_RELA
6342                     && r_type != R_NDS32_17_PCREL_RELA
6343                     && !(r_type == R_NDS32_32_RELA
6344                          && strcmp (sec->name, ".eh_frame") == 0))
6345                    || (h != NULL
6346                        && (!info->symbolic
6347                            || h->root.type == bfd_link_hash_defweak
6348                            || !h->def_regular))))
6349               || (!bfd_link_pic (info)
6350                   && (sec->flags & SEC_ALLOC) != 0
6351                   && h != NULL
6352                   && (h->root.type == bfd_link_hash_defweak
6353                       || !h->def_regular)))
6354             {
6355               struct elf_nds32_dyn_relocs *p;
6356               struct elf_nds32_dyn_relocs **head;
6357
6358               if (dynobj == NULL)
6359                 htab->root.dynobj = dynobj = abfd;
6360
6361               /* When creating a shared object, we must copy these
6362                  relocs into the output file.  We create a reloc
6363                  section in dynobj and make room for the reloc.  */
6364               if (sreloc == NULL)
6365                 {
6366                   const char *name;
6367
6368                   name = bfd_elf_string_from_elf_section
6369                     (abfd, elf_elfheader (abfd)->e_shstrndx,
6370                      elf_section_data (sec)->rela.hdr->sh_name);
6371                   if (name == NULL)
6372                     return FALSE;
6373
6374                   BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6375                               && strcmp (bfd_get_section_name (abfd, sec),
6376                                          name + 5) == 0);
6377
6378                   sreloc = bfd_get_section_by_name (dynobj, name);
6379                   if (sreloc == NULL)
6380                     {
6381                       flagword flags;
6382
6383                       sreloc = bfd_make_section (dynobj, name);
6384                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
6385                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6386                       if ((sec->flags & SEC_ALLOC) != 0)
6387                         flags |= SEC_ALLOC | SEC_LOAD;
6388                       if (sreloc == NULL
6389                           || !bfd_set_section_flags (dynobj, sreloc, flags)
6390                           || !bfd_set_section_alignment (dynobj, sreloc, 2))
6391                         return FALSE;
6392
6393                       elf_section_type (sreloc) = SHT_RELA;
6394                     }
6395                   elf_section_data (sec)->sreloc = sreloc;
6396                 }
6397
6398               /* If this is a global symbol, we count the number of
6399                  relocations we need for this symbol.  */
6400               if (h != NULL)
6401                 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
6402               else
6403                 {
6404                   asection *s;
6405                   void *vpp;
6406
6407                   Elf_Internal_Sym *isym;
6408                   isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
6409                   if (isym == NULL)
6410                     return FALSE;
6411
6412                   /* Track dynamic relocs needed for local syms too.  */
6413                   s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6414                   if (s == NULL)
6415                     return FALSE;
6416
6417                   vpp = &elf_section_data (s)->local_dynrel;
6418                   head = (struct elf_nds32_dyn_relocs **) vpp;
6419                 }
6420
6421               p = *head;
6422               if (p == NULL || p->sec != sec)
6423                 {
6424                   bfd_size_type amt = sizeof (*p);
6425                   p = (struct elf_nds32_dyn_relocs *) bfd_alloc (dynobj, amt);
6426                   if (p == NULL)
6427                     return FALSE;
6428                   p->next = *head;
6429                   *head = p;
6430                   p->sec = sec;
6431                   p->count = 0;
6432                   p->pc_count = 0;
6433                 }
6434
6435               p->count += 1;
6436               if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
6437                   || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6438                   || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
6439                 p->pc_count += 1;
6440             }
6441           break;
6442
6443           /* This relocation describes the C++ object vtable hierarchy.
6444              Reconstruct it for later use during GC.  */
6445         case R_NDS32_RELA_GNU_VTINHERIT:
6446         case R_NDS32_GNU_VTINHERIT:
6447           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6448             return FALSE;
6449           break;
6450
6451           /* This relocation describes which C++ vtable entries are actually
6452              used.  Record for later use during GC.  */
6453         case R_NDS32_GNU_VTENTRY:
6454           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6455             return FALSE;
6456           break;
6457         case R_NDS32_RELA_GNU_VTENTRY:
6458           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6459             return FALSE;
6460           break;
6461         }
6462     }
6463
6464   return TRUE;
6465 }
6466
6467 /* Write VAL in uleb128 format to P, returning a pointer to the
6468    following byte.
6469    This code is copied from elf-attr.c.  */
6470
6471 static bfd_byte *
6472 write_uleb128 (bfd_byte *p, unsigned int val)
6473 {
6474   bfd_byte c;
6475   do
6476     {
6477       c = val & 0x7f;
6478       val >>= 7;
6479       if (val)
6480         c |= 0x80;
6481       *(p++) = c;
6482     }
6483   while (val);
6484   return p;
6485 }
6486
6487 static bfd_signed_vma
6488 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
6489                   Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
6490                   int *pic_ext_target)
6491 {
6492   bfd_signed_vma foff;
6493   bfd_vma symval, addend;
6494   asection *sym_sec;
6495
6496   /* Get the value of the symbol referred to by the reloc.  */
6497   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6498     {
6499       Elf_Internal_Sym *isym;
6500
6501       /* A local symbol.  */
6502       isym = isymbuf + ELF32_R_SYM (irel->r_info);
6503
6504       if (isym->st_shndx == SHN_UNDEF)
6505         sym_sec = bfd_und_section_ptr;
6506       else if (isym->st_shndx == SHN_ABS)
6507         sym_sec = bfd_abs_section_ptr;
6508       else if (isym->st_shndx == SHN_COMMON)
6509         sym_sec = bfd_com_section_ptr;
6510       else
6511         sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6512       symval = isym->st_value + sym_sec->output_section->vma
6513                + sym_sec->output_offset;
6514     }
6515   else
6516     {
6517       unsigned long indx;
6518       struct elf_link_hash_entry *h;
6519       bfd *owner;
6520
6521       /* An external symbol.  */
6522       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6523       h = elf_sym_hashes (abfd)[indx];
6524       BFD_ASSERT (h != NULL);
6525
6526       if (h->root.type != bfd_link_hash_defined
6527           && h->root.type != bfd_link_hash_defweak)
6528         /* This appears to be a reference to an undefined
6529            symbol.  Just ignore it--it will be caught by the
6530            regular reloc processing.  */
6531         return 0;
6532       owner = h->root.u.def.section->owner;
6533       if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
6534         *pic_ext_target = 1;
6535
6536       if (h->root.u.def.section->flags & SEC_MERGE)
6537         {
6538           sym_sec = h->root.u.def.section;
6539           symval = _bfd_merged_section_offset (abfd, &sym_sec,
6540                                                elf_section_data (sym_sec)->sec_info,
6541                                                h->root.u.def.value);
6542           symval = symval + sym_sec->output_section->vma
6543                    + sym_sec->output_offset;
6544         }
6545       else
6546         symval = (h->root.u.def.value
6547                   + h->root.u.def.section->output_section->vma
6548                   + h->root.u.def.section->output_offset);
6549     }
6550
6551   addend = irel->r_addend;
6552
6553   foff = (symval + addend
6554           - (irel->r_offset + sec->output_section->vma + sec->output_offset));
6555   return foff;
6556 }
6557
6558 static bfd_vma
6559 calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
6560                               Elf_Internal_Sym *isymbuf,
6561                               Elf_Internal_Rela *irel,
6562                               Elf_Internal_Shdr *symtab_hdr)
6563 {
6564   bfd_vma symval;
6565
6566   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6567     {
6568       Elf_Internal_Sym *isym;
6569       asection *sym_sec;
6570       /* A local symbol.  */
6571       isym = isymbuf + ELF32_R_SYM (irel->r_info);
6572
6573       if (isym->st_shndx == SHN_UNDEF)
6574         sym_sec = bfd_und_section_ptr;
6575       else if (isym->st_shndx == SHN_ABS)
6576         sym_sec = bfd_abs_section_ptr;
6577       else if (isym->st_shndx == SHN_COMMON)
6578         sym_sec = bfd_com_section_ptr;
6579       else
6580         sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6581       symval = isym->st_value + sym_sec->output_section->vma
6582                + sym_sec->output_offset;
6583     }
6584   else
6585     {
6586       unsigned long indx;
6587       struct elf_link_hash_entry *h;
6588       struct elf_nds32_link_hash_table *htab;
6589       asection *splt;
6590
6591       /* An external symbol.  */
6592       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6593       h = elf_sym_hashes (abfd)[indx];
6594       BFD_ASSERT (h != NULL);
6595       htab = nds32_elf_hash_table (link_info);
6596       splt = htab->root.splt;
6597
6598       while (h->root.type == bfd_link_hash_indirect
6599              || h->root.type == bfd_link_hash_warning)
6600         h = (struct elf_link_hash_entry *) h->root.u.i.link;
6601
6602       if (h->plt.offset == (bfd_vma) - 1)
6603         {
6604           if (h->root.type != bfd_link_hash_defined
6605               && h->root.type != bfd_link_hash_defweak)
6606             /* This appears to be a reference to an undefined
6607              * symbol.  Just ignore it--it will be caught by the
6608              * regular reloc processing.  */
6609             return 0;
6610           symval = (h->root.u.def.value
6611                     + h->root.u.def.section->output_section->vma
6612                     + h->root.u.def.section->output_offset);
6613         }
6614       else
6615         symval = splt->output_section->vma + h->plt.offset;
6616     }
6617
6618   return symval;
6619 }
6620
6621 static bfd_signed_vma
6622 calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
6623                       Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
6624                       Elf_Internal_Shdr *symtab_hdr)
6625 {
6626   bfd_vma foff;
6627   if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
6628                                             symtab_hdr)) == 0)
6629     return 0;
6630   else
6631     return foff - (irel->r_offset
6632                    + sec->output_section->vma + sec->output_offset);
6633 }
6634 \f
6635 /* Convert a 32-bit instruction to 16-bit one.
6636    INSN is the input 32-bit instruction, INSN16 is the output 16-bit
6637    instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
6638    type of INSN16.  Return 1 if successful.  */
6639
6640 static int
6641 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6642                              int *pinsn_type)
6643 {
6644   uint16_t insn16 = 0;
6645   int insn_type = 0;
6646   unsigned long mach = bfd_get_mach (abfd);
6647
6648   if (N32_SH5 (insn) != 0)
6649     return 0;
6650
6651   switch (N32_SUB5 (insn))
6652     {
6653     case N32_ALU1_ADD_SLLI:
6654     case N32_ALU1_ADD_SRLI:
6655       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6656         {
6657           insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
6658                                 N32_RB5 (insn));
6659           insn_type = NDS32_INSN_ADD333;
6660         }
6661       else if (N32_IS_RT4 (insn))
6662         {
6663           if (N32_RT5 (insn) == N32_RA5 (insn))
6664             insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
6665           else if (N32_RT5 (insn) == N32_RB5 (insn))
6666             insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
6667           insn_type = NDS32_INSN_ADD45;
6668         }
6669       break;
6670
6671     case N32_ALU1_SUB_SLLI:
6672     case N32_ALU1_SUB_SRLI:
6673       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6674         {
6675           insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
6676                                 N32_RB5 (insn));
6677           insn_type = NDS32_INSN_SUB333;
6678         }
6679       else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6680         {
6681           insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
6682           insn_type = NDS32_INSN_SUB45;
6683         }
6684       break;
6685
6686     case N32_ALU1_AND_SLLI:
6687     case N32_ALU1_AND_SRLI:
6688       /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
6689       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6690           && N32_IS_RB3 (insn))
6691         {
6692           if (N32_RT5 (insn) == N32_RA5 (insn))
6693             insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
6694           else if (N32_RT5 (insn) == N32_RB5 (insn))
6695             insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
6696           if (insn16)
6697             insn_type = NDS32_INSN_AND33;
6698         }
6699       break;
6700
6701     case N32_ALU1_XOR_SLLI:
6702     case N32_ALU1_XOR_SRLI:
6703       /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
6704       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6705           && N32_IS_RB3 (insn))
6706         {
6707           if (N32_RT5 (insn) == N32_RA5 (insn))
6708             insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
6709           else if (N32_RT5 (insn) == N32_RB5 (insn))
6710             insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
6711           if (insn16)
6712             insn_type = NDS32_INSN_XOR33;
6713         }
6714       break;
6715
6716     case N32_ALU1_OR_SLLI:
6717     case N32_ALU1_OR_SRLI:
6718       /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
6719       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6720           && N32_IS_RB3 (insn))
6721         {
6722           if (N32_RT5 (insn) == N32_RA5 (insn))
6723             insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
6724           else if (N32_RT5 (insn) == N32_RB5 (insn))
6725             insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
6726           if (insn16)
6727             insn_type = NDS32_INSN_OR33;
6728         }
6729       break;
6730     case N32_ALU1_NOR:
6731       /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
6732       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
6733           && N32_RA5 (insn) == N32_RB5 (insn))
6734         {
6735           insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
6736           insn_type = NDS32_INSN_NOT33;
6737         }
6738       break;
6739     case N32_ALU1_SRAI:
6740       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6741         {
6742           insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
6743           insn_type = NDS32_INSN_SRAI45;
6744         }
6745       break;
6746
6747     case N32_ALU1_SRLI:
6748       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6749         {
6750           insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
6751           insn_type = NDS32_INSN_SRLI45;
6752         }
6753       break;
6754
6755     case N32_ALU1_SLLI:
6756       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
6757         {
6758           insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
6759                                 N32_UB5 (insn));
6760           insn_type = NDS32_INSN_SLLI333;
6761         }
6762       break;
6763
6764     case N32_ALU1_ZEH:
6765       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6766         {
6767           insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
6768           insn_type = NDS32_INSN_ZEH33;
6769         }
6770       break;
6771
6772     case N32_ALU1_SEB:
6773       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6774         {
6775           insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
6776           insn_type = NDS32_INSN_SEB33;
6777         }
6778       break;
6779
6780     case N32_ALU1_SEH:
6781       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6782         {
6783           insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
6784           insn_type = NDS32_INSN_SEH33;
6785         }
6786       break;
6787
6788     case N32_ALU1_SLT:
6789       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6790         {
6791           /* Implicit r15.  */
6792           insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
6793           insn_type = NDS32_INSN_SLT45;
6794         }
6795       break;
6796
6797     case N32_ALU1_SLTS:
6798       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
6799         {
6800           /* Implicit r15.  */
6801           insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
6802           insn_type = NDS32_INSN_SLTS45;
6803         }
6804       break;
6805     }
6806
6807   if ((insn16 & 0x8000) == 0)
6808     return 0;
6809
6810   if (pinsn16)
6811     *pinsn16 = insn16;
6812   if (pinsn_type)
6813     *pinsn_type = insn_type;
6814   return 1;
6815 }
6816
6817 static int
6818 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6819                              int *pinsn_type)
6820 {
6821   uint16_t insn16 = 0;
6822   int insn_type;
6823   unsigned long mach = bfd_get_mach (abfd);
6824
6825   /* TODO: bset, bclr, btgl, btst.  */
6826   if (__GF (insn, 6, 4) != 0)
6827     return 0;
6828
6829   switch (N32_IMMU (insn, 6))
6830     {
6831     case N32_ALU2_MUL:
6832       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6833           && N32_IS_RB3 (insn))
6834         {
6835           if (N32_RT5 (insn) == N32_RA5 (insn))
6836             insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
6837           else if (N32_RT5 (insn) == N32_RB5 (insn))
6838             insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
6839           if (insn16)
6840             insn_type = NDS32_INSN_MUL33;
6841         }
6842     }
6843
6844   if ((insn16 & 0x8000) == 0)
6845     return 0;
6846
6847   if (pinsn16)
6848     *pinsn16 = insn16;
6849   if (pinsn_type)
6850     *pinsn_type = insn_type;
6851   return 1;
6852 }
6853
6854 int
6855 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6856                         int *pinsn_type)
6857 {
6858   int op6;
6859   uint16_t insn16 = 0;
6860   int insn_type;
6861   unsigned long mach = bfd_get_mach (abfd);
6862
6863   /* Decode 32-bit instruction.  */
6864   if (insn & 0x80000000)
6865     {
6866       /* Not 32-bit insn.  */
6867       return 0;
6868     }
6869
6870   op6 = N32_OP6 (insn);
6871
6872   /* Convert it to 16-bit instruction.  */
6873   switch (op6)
6874     {
6875     case N32_OP6_MOVI:
6876       if (IS_WITHIN_S (N32_IMM20S (insn), 5))
6877         {
6878           insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
6879           insn_type = NDS32_INSN_MOVI55;
6880         }
6881       else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
6882                && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
6883         {
6884           insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
6885                                N32_IMM20S (insn) - 16);
6886           insn_type = NDS32_INSN_MOVPI45;
6887         }
6888       break;
6889
6890     case N32_OP6_ADDI:
6891       if (N32_IMM15S (insn) == 0)
6892         {
6893           /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
6894              because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
6895           if (mach <= MACH_V2
6896               || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
6897             {
6898               insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
6899               insn_type = NDS32_INSN_MOV55;
6900             }
6901         }
6902       else if (N32_IMM15S (insn) > 0)
6903         {
6904           if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
6905             {
6906               insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
6907                                     N32_IMM15S (insn));
6908               insn_type = NDS32_INSN_ADDI333;
6909             }
6910           else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6911                    && N32_IMM15S (insn) < 32)
6912             {
6913               insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
6914               insn_type = NDS32_INSN_ADDI45;
6915             }
6916           else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6917                    && N32_RT5 (insn) == N32_RA5 (insn)
6918                    && N32_IMM15S (insn) < 512)
6919             {
6920               insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6921               insn_type = NDS32_INSN_ADDI10_SP;
6922             }
6923           else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
6924                    && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
6925                    && (N32_IMM15S (insn) % 4 == 0))
6926             {
6927               insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
6928                                    N32_IMM15S (insn) >> 2);
6929               insn_type = NDS32_INSN_ADDRI36_SP;
6930             }
6931         }
6932       else
6933         {
6934           /* Less than 0.  */
6935           if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
6936             {
6937               insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
6938                                     0 - N32_IMM15S (insn));
6939               insn_type = NDS32_INSN_SUBI333;
6940             }
6941           else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
6942                    && N32_IMM15S (insn) > -32)
6943             {
6944               insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
6945                                    0 - N32_IMM15S (insn));
6946               insn_type = NDS32_INSN_SUBI45;
6947             }
6948           else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
6949                    && N32_RT5 (insn) == N32_RA5 (insn)
6950                    && N32_IMM15S (insn) >= -512)
6951             {
6952               insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
6953               insn_type = NDS32_INSN_ADDI10_SP;
6954             }
6955         }
6956       break;
6957
6958     case N32_OP6_ORI:
6959       if (N32_IMM15S (insn) == 0)
6960         {
6961           /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
6962              because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
6963           if (mach <= MACH_V2
6964               || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
6965             {
6966               insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
6967               insn_type = NDS32_INSN_MOV55;
6968             }
6969         }
6970       break;
6971
6972     case N32_OP6_SUBRI:
6973       if (mach >= MACH_V3 && N32_IS_RT3 (insn)
6974           && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
6975         {
6976           insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
6977           insn_type = NDS32_INSN_NEG33;
6978         }
6979       break;
6980
6981     case N32_OP6_ANDI:
6982       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6983         {
6984           if (N32_IMM15U (insn) == 1)
6985             {
6986               insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
6987               insn_type = NDS32_INSN_XLSB33;
6988             }
6989           else if (N32_IMM15U (insn) == 0x7ff)
6990             {
6991               insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
6992               insn_type = NDS32_INSN_X11B33;
6993             }
6994           else if (N32_IMM15U (insn) == 0xff)
6995             {
6996               insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
6997               insn_type = NDS32_INSN_ZEB33;
6998             }
6999           else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7000                    && N32_IMM15U (insn) < 256)
7001             {
7002               int imm15u = N32_IMM15U (insn);
7003
7004               if (__builtin_popcount (imm15u) == 1)
7005                 {
7006                   /* BMSKI33 */
7007                   int imm3u = __builtin_ctz (imm15u);
7008
7009                   insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7010                   insn_type = NDS32_INSN_BMSKI33;
7011                 }
7012               else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7013                 {
7014                   /* FEXTI33 */
7015                   int imm3u = __builtin_ctz (imm15u + 1) - 1;
7016
7017                   insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7018                   insn_type = NDS32_INSN_FEXTI33;
7019                 }
7020             }
7021         }
7022       break;
7023
7024     case N32_OP6_SLTI:
7025       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7026           && IS_WITHIN_U (N32_IMM15S (insn), 5))
7027         {
7028           insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7029           insn_type = NDS32_INSN_SLTI45;
7030         }
7031       break;
7032
7033     case N32_OP6_SLTSI:
7034       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7035           && IS_WITHIN_U (N32_IMM15S (insn), 5))
7036         {
7037           insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7038           insn_type = NDS32_INSN_SLTSI45;
7039         }
7040       break;
7041
7042     case N32_OP6_LWI:
7043       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7044         {
7045           insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7046           insn_type = NDS32_INSN_LWI450;
7047         }
7048       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7049                && IS_WITHIN_U (N32_IMM15S (insn), 3))
7050         {
7051           insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7052                                 N32_IMM15S (insn));
7053           insn_type = NDS32_INSN_LWI333;
7054         }
7055       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7056                && IS_WITHIN_U (N32_IMM15S (insn), 7))
7057         {
7058           insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7059           insn_type = NDS32_INSN_LWI37;
7060         }
7061       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7062                && IS_WITHIN_U (N32_IMM15S (insn), 7))
7063         {
7064           insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7065           insn_type = NDS32_INSN_LWI37_SP;
7066         }
7067       else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7068                && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7069         {
7070           insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7071                                N32_IMM15S (insn) + 32);
7072           insn_type = NDS32_INSN_LWI45_FE;
7073         }
7074       break;
7075
7076     case N32_OP6_SWI:
7077       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7078         {
7079           insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7080           insn_type = NDS32_INSN_SWI450;
7081         }
7082       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7083                && IS_WITHIN_U (N32_IMM15S (insn), 3))
7084         {
7085           insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7086                                 N32_IMM15S (insn));
7087           insn_type = NDS32_INSN_SWI333;
7088         }
7089       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7090                && IS_WITHIN_U (N32_IMM15S (insn), 7))
7091         {
7092           insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7093           insn_type = NDS32_INSN_SWI37;
7094         }
7095       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7096                && IS_WITHIN_U (N32_IMM15S (insn), 7))
7097         {
7098           insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7099           insn_type = NDS32_INSN_SWI37_SP;
7100         }
7101       break;
7102
7103     case N32_OP6_LWI_BI:
7104       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7105           && IS_WITHIN_U (N32_IMM15S (insn), 3))
7106         {
7107           insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7108                                 N32_IMM15S (insn));
7109           insn_type = NDS32_INSN_LWI333_BI;
7110         }
7111       break;
7112
7113     case N32_OP6_SWI_BI:
7114       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7115           && IS_WITHIN_U (N32_IMM15S (insn), 3))
7116         {
7117           insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7118                                 N32_IMM15S (insn));
7119           insn_type = NDS32_INSN_SWI333_BI;
7120         }
7121       break;
7122
7123     case N32_OP6_LHI:
7124       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7125           && IS_WITHIN_U (N32_IMM15S (insn), 3))
7126         {
7127           insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7128                                 N32_IMM15S (insn));
7129           insn_type = NDS32_INSN_LHI333;
7130         }
7131       break;
7132
7133     case N32_OP6_SHI:
7134       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7135           && IS_WITHIN_U (N32_IMM15S (insn), 3))
7136         {
7137           insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
7138                                 N32_IMM15S (insn));
7139           insn_type = NDS32_INSN_SHI333;
7140         }
7141       break;
7142
7143     case N32_OP6_LBI:
7144       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7145           && IS_WITHIN_U (N32_IMM15S (insn), 3))
7146         {
7147           insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
7148                                 N32_IMM15S (insn));
7149           insn_type = NDS32_INSN_LBI333;
7150         }
7151       break;
7152
7153     case N32_OP6_SBI:
7154       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7155           && IS_WITHIN_U (N32_IMM15S (insn), 3))
7156         {
7157           insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
7158                                 N32_IMM15S (insn));
7159           insn_type = NDS32_INSN_SBI333;
7160         }
7161       break;
7162
7163     case N32_OP6_ALU1:
7164       return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
7165
7166     case N32_OP6_ALU2:
7167       return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
7168
7169     case N32_OP6_BR1:
7170       if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
7171         goto done;
7172
7173       if ((insn & N32_BIT (14)) == 0)
7174         {
7175           /* N32_BR1_BEQ */
7176           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7177               && N32_RT5 (insn) != REG_R5)
7178             insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
7179           else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7180                    && N32_RA5 (insn) != REG_R5)
7181             insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
7182           insn_type = NDS32_INSN_BEQS38;
7183           break;
7184         }
7185       else
7186         {
7187           /* N32_BR1_BNE */
7188           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7189               && N32_RT5 (insn) != REG_R5)
7190             insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
7191           else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7192                    && N32_RA5 (insn) != REG_R5)
7193             insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
7194           insn_type = NDS32_INSN_BNES38;
7195           break;
7196         }
7197       break;
7198
7199     case N32_OP6_BR2:
7200       switch (N32_BR2_SUB (insn))
7201         {
7202         case N32_BR2_BEQZ:
7203           if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7204             {
7205               insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
7206               insn_type = NDS32_INSN_BEQZ38;
7207             }
7208           else if (N32_RT5 (insn) == REG_R15
7209                    && IS_WITHIN_S (N32_IMM16S (insn), 8))
7210             {
7211               insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
7212               insn_type = NDS32_INSN_BEQZS8;
7213             }
7214           break;
7215
7216         case N32_BR2_BNEZ:
7217           if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7218             {
7219               insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
7220               insn_type = NDS32_INSN_BNEZ38;
7221             }
7222           else if (N32_RT5 (insn) == REG_R15
7223                    && IS_WITHIN_S (N32_IMM16S (insn), 8))
7224             {
7225               insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
7226               insn_type = NDS32_INSN_BNEZS8;
7227             }
7228           break;
7229
7230         case N32_BR2_IFCALL:
7231           if (IS_WITHIN_U (N32_IMM16S (insn), 9))
7232             {
7233               insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
7234               insn_type = NDS32_INSN_IFCALL9;
7235             }
7236           break;
7237         }
7238       break;
7239
7240     case N32_OP6_JI:
7241       if ((insn & N32_BIT (24)) == 0)
7242         {
7243           /* N32_JI_J */
7244           if (IS_WITHIN_S (N32_IMM24S (insn), 8))
7245             {
7246               insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
7247               insn_type = NDS32_INSN_J8;
7248             }
7249         }
7250       break;
7251
7252     case N32_OP6_JREG:
7253       if (__GF (insn, 8, 2) != 0)
7254         goto done;
7255
7256       switch (N32_IMMU (insn, 5))
7257         {
7258         case N32_JREG_JR:
7259           if (N32_JREG_HINT (insn) == 0)
7260             {
7261               /* jr */
7262               insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
7263               insn_type = NDS32_INSN_JR5;
7264             }
7265           else if (N32_JREG_HINT (insn) == 1)
7266             {
7267               /* ret */
7268               insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
7269               insn_type = NDS32_INSN_RET5;
7270             }
7271           else if (N32_JREG_HINT (insn) == 3)
7272             {
7273               /* ifret = mov55 $sp, $sp */
7274               insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
7275               insn_type = NDS32_INSN_IFRET;
7276             }
7277           break;
7278
7279         case N32_JREG_JRAL:
7280           /* It's convertible when return rt5 is $lp and address
7281              translation is kept.  */
7282           if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
7283             {
7284               insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
7285               insn_type = NDS32_INSN_JRAL5;
7286             }
7287           break;
7288         }
7289       break;
7290
7291     case N32_OP6_MISC:
7292       if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
7293         {
7294           /* For v3, swid above 31 are used for ex9.it.  */
7295           insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
7296           insn_type = NDS32_INSN_BREAK16;
7297         }
7298       break;
7299
7300     default:
7301       /* This instruction has no 16-bit variant.  */
7302       goto done;
7303     }
7304
7305 done:
7306   /* Bit-15 of insn16 should be set for a valid instruction.  */
7307   if ((insn16 & 0x8000) == 0)
7308     return 0;
7309
7310   if (pinsn16)
7311     *pinsn16 = insn16;
7312   if (pinsn_type)
7313     *pinsn_type = insn_type;
7314   return 1;
7315 }
7316
7317 static int
7318 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
7319                           Elf_Internal_Rela *reloc)
7320 {
7321   uint16_t insn16 = 0;
7322
7323   if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
7324       || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
7325     return 0;
7326
7327   if (!N32_IS_RT3 (insn))
7328     return 0;
7329
7330   switch (N32_OP6 (insn))
7331     {
7332     case N32_OP6_LWI:
7333       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7334         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7335       break;
7336     case N32_OP6_SWI:
7337       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7338         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7339       break;
7340     case N32_OP6_HWGP:
7341       if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
7342         break;
7343
7344       if (__GF (insn, 17, 3) == 6)
7345         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
7346       else if (__GF (insn, 17, 3) == 7)
7347         insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
7348       break;
7349     }
7350
7351   if ((insn16 & 0x8000) == 0)
7352     return 0;
7353
7354   *pinsn16 = insn16;
7355   return 1;
7356 }
7357
7358 /* Convert a 16-bit instruction to 32-bit one.
7359    INSN16 it the input and PINSN it the point to output.
7360    Return non-zero on successful.  Otherwise 0 is returned.  */
7361
7362 int
7363 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
7364 {
7365   uint32_t insn = 0xffffffff;
7366   unsigned long mach = bfd_get_mach (abfd);
7367
7368   /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
7369
7370   switch (__GF (insn16, 9, 6))
7371     {
7372     case 0x4:                   /* add45 */
7373       insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
7374                        N16_RA5 (insn16));
7375       goto done;
7376     case 0x5:                   /* sub45 */
7377       insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
7378                        N16_RA5 (insn16));
7379       goto done;
7380     case 0x6:                   /* addi45 */
7381       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7382                         N16_IMM5U (insn16));
7383       goto done;
7384     case 0x7:                   /* subi45 */
7385       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7386                         -N16_IMM5U (insn16));
7387       goto done;
7388     case 0x8:                   /* srai45 */
7389       insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
7390                        N16_IMM5U (insn16));
7391       goto done;
7392     case 0x9:                   /* srli45 */
7393       insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
7394                        N16_IMM5U (insn16));
7395       goto done;
7396     case 0xa:                   /* slli333 */
7397       insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
7398                        N16_IMM3U (insn16));
7399       goto done;
7400     case 0xc:                   /* add333 */
7401       insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
7402                        N16_RB3 (insn16));
7403       goto done;
7404     case 0xd:                   /* sub333 */
7405       insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
7406                        N16_RB3 (insn16));
7407       goto done;
7408     case 0xe:                   /* addi333 */
7409       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7410                         N16_IMM3U (insn16));
7411       goto done;
7412     case 0xf:                   /* subi333 */
7413       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7414                         -N16_IMM3U (insn16));
7415       goto done;
7416     case 0x10:                  /* lwi333 */
7417       insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
7418                         N16_IMM3U (insn16));
7419       goto done;
7420     case 0x12:                  /* lhi333 */
7421       insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
7422                         N16_IMM3U (insn16));
7423       goto done;
7424     case 0x13:                  /* lbi333 */
7425       insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
7426                         N16_IMM3U (insn16));
7427       goto done;
7428     case 0x11:                  /* lwi333.bi */
7429       insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7430                         N16_IMM3U (insn16));
7431       goto done;
7432     case 0x14:                  /* swi333 */
7433       insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
7434                         N16_IMM3U (insn16));
7435       goto done;
7436     case 0x16:                  /* shi333 */
7437       insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
7438                         N16_IMM3U (insn16));
7439       goto done;
7440     case 0x17:                  /* sbi333 */
7441       insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
7442                         N16_IMM3U (insn16));
7443       goto done;
7444     case 0x15:                  /* swi333.bi */
7445       insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7446                         N16_IMM3U (insn16));
7447       goto done;
7448     case 0x18:                  /* addri36.sp */
7449       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
7450                         N16_IMM6U (insn16) << 2);
7451       goto done;
7452     case 0x19:                  /* lwi45.fe */
7453       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
7454                         (N16_IMM5U (insn16) - 32));
7455       goto done;
7456     case 0x1a:                  /* lwi450 */
7457       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7458       goto done;
7459     case 0x1b:                  /* swi450 */
7460       insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7461       goto done;
7462
7463       /* These are r15 implied instructions.  */
7464     case 0x30:                  /* slts45 */
7465       insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7466       goto done;
7467     case 0x31:                  /* slt45 */
7468       insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7469       goto done;
7470     case 0x32:                  /* sltsi45 */
7471       insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7472       goto done;
7473     case 0x33:                  /* slti45 */
7474       insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7475       goto done;
7476     case 0x34:                  /* beqzs8, bnezs8 */
7477       if (insn16 & N32_BIT (8))
7478         insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
7479       else
7480         insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
7481       goto done;
7482
7483     case 0x35:                  /* break16, ex9.it */
7484       /* Only consider range of v3 break16.  */
7485       insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
7486       goto done;
7487
7488     case 0x3c:                  /* ifcall9 */
7489       insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
7490       goto done;
7491     case 0x3d:                  /* movpi45 */
7492       insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
7493       goto done;
7494
7495     case 0x3f:                  /* MISC33 */
7496       switch (insn16 & 0x7)
7497         {
7498         case 2:                 /* neg33 */
7499           insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7500           break;
7501         case 3:                 /* not33 */
7502           insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
7503                            N16_RA3 (insn16));
7504           break;
7505         case 4:                 /* mul33 */
7506           insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
7507                            N16_RA3 (insn16));
7508           break;
7509         case 5:                 /* xor33 */
7510           insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
7511                            N16_RA3 (insn16));
7512           break;
7513         case 6:                 /* and33 */
7514           insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
7515                            N16_RA3 (insn16));
7516           break;
7517         case 7:                 /* or33 */
7518           insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
7519                            N16_RA3 (insn16));
7520           break;
7521         }
7522       goto done;
7523
7524     case 0xb:
7525       switch (insn16 & 0x7)
7526         {
7527         case 0:                 /* zeb33 */
7528           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
7529           break;
7530         case 1:                 /* zeh33 */
7531           insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7532           break;
7533         case 2:                 /* seb33 */
7534           insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7535           break;
7536         case 3:                 /* seh33 */
7537           insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7538           break;
7539         case 4:                 /* xlsb33 */
7540           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
7541           break;
7542         case 5:                 /* x11b33 */
7543           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
7544           break;
7545         case 6:                 /* bmski33 */
7546           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7547                             1 << __GF (insn16, 3, 3));
7548           break;
7549         case 7:                 /* fexti33 */
7550           insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7551                             (1 << (__GF (insn16, 3, 3) + 1)) - 1);
7552           break;
7553         }
7554       goto done;
7555     }
7556
7557   switch (__GF (insn16, 10, 5))
7558     {
7559     case 0x0:                   /* mov55 or ifret16 */
7560       if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
7561           && N16_RT5 (insn16) == N16_RA5 (insn16))
7562         insn = N32_JREG (JR, 0, 0, 0, 3);
7563       else
7564         insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
7565       goto done;
7566     case 0x1:                   /* movi55 */
7567       insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
7568       goto done;
7569     case 0x1b:                  /* addi10s (V2) */
7570       insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
7571       goto done;
7572     }
7573
7574   switch (__GF (insn16, 11, 4))
7575     {
7576     case 0x7:                   /* lwi37.fp/swi37.fp */
7577       if (insn16 & N32_BIT (7)) /* swi37.fp */
7578         insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7579       else                      /* lwi37.fp */
7580         insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7581       goto done;
7582     case 0x8:                   /* beqz38 */
7583       insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7584       goto done;
7585     case 0x9:                   /* bnez38 */
7586       insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7587       goto done;
7588     case 0xa:                   /* beqs38/j8, implied r5 */
7589       if (N16_RT38 (insn16) == 5)
7590         insn = N32_JI (J, N16_IMM8S (insn16));
7591       else
7592         insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7593       goto done;
7594     case 0xb:                   /* bnes38 and others */
7595       if (N16_RT38 (insn16) == 5)
7596         {
7597           switch (__GF (insn16, 5, 3))
7598             {
7599             case 0:             /* jr5 */
7600               insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
7601               break;
7602             case 4:             /* ret5 */
7603               insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
7604               break;
7605             case 1:             /* jral5 */
7606               insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
7607               break;
7608             case 2:             /* ex9.it imm5 */
7609               /* ex9.it had no 32-bit variantl.  */
7610               break;
7611             case 5:             /* add5.pc */
7612               /* add5.pc had no 32-bit variantl.  */
7613               break;
7614             }
7615         }
7616       else                      /* bnes38 */
7617         insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7618       goto done;
7619     case 0xe:                   /* lwi37/swi37 */
7620       if (insn16 & (1 << 7))    /* swi37.sp */
7621         insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7622       else                      /* lwi37.sp */
7623         insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7624       goto done;
7625     }
7626
7627 done:
7628   if (insn & 0x80000000)
7629     return 0;
7630
7631   if (pinsn)
7632     *pinsn = insn;
7633   return 1;
7634 }
7635 \f
7636 static bfd_boolean
7637 is_sda_access_insn (unsigned long insn)
7638 {
7639   switch (N32_OP6 (insn))
7640     {
7641     case N32_OP6_LWI:
7642     case N32_OP6_LHI:
7643     case N32_OP6_LHSI:
7644     case N32_OP6_LBI:
7645     case N32_OP6_LBSI:
7646     case N32_OP6_SWI:
7647     case N32_OP6_SHI:
7648     case N32_OP6_SBI:
7649     case N32_OP6_LWC:
7650     case N32_OP6_LDC:
7651     case N32_OP6_SWC:
7652     case N32_OP6_SDC:
7653       return TRUE;
7654     default:
7655       ;
7656     }
7657   return FALSE;
7658 }
7659
7660 static unsigned long
7661 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
7662 {
7663   uint32_t oinsn = 0;
7664
7665   switch (type)
7666     {
7667     case R_NDS32_GOT_LO12:
7668     case R_NDS32_GOTOFF_LO12:
7669     case R_NDS32_PLTREL_LO12:
7670     case R_NDS32_PLT_GOTREL_LO12:
7671     case R_NDS32_LO12S0_RELA:
7672       switch (N32_OP6 (insn))
7673         {
7674         case N32_OP6_LBI:
7675           /* lbi.gp */
7676           oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
7677           break;
7678         case N32_OP6_LBSI:
7679           /* lbsi.gp */
7680           oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
7681           break;
7682         case N32_OP6_SBI:
7683           /* sbi.gp */
7684           oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
7685           break;
7686         case N32_OP6_ORI:
7687           /* addi.gp */
7688           oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
7689           break;
7690         }
7691       break;
7692
7693     case R_NDS32_LO12S1_RELA:
7694       switch (N32_OP6 (insn))
7695         {
7696         case N32_OP6_LHI:
7697           /* lhi.gp */
7698           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
7699           break;
7700         case N32_OP6_LHSI:
7701           /* lhsi.gp */
7702           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
7703           break;
7704         case N32_OP6_SHI:
7705           /* shi.gp */
7706           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
7707           break;
7708         }
7709       break;
7710
7711     case R_NDS32_LO12S2_RELA:
7712       switch (N32_OP6 (insn))
7713         {
7714         case N32_OP6_LWI:
7715           /* lwi.gp */
7716           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
7717           break;
7718         case N32_OP6_SWI:
7719           /* swi.gp */
7720           oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
7721           break;
7722         }
7723       break;
7724
7725     case R_NDS32_LO12S2_DP_RELA:
7726     case R_NDS32_LO12S2_SP_RELA:
7727       oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
7728       break;
7729     }
7730
7731   if (oinsn)
7732     *pinsn = oinsn;
7733
7734   return oinsn != 0;
7735 }
7736
7737 /* Linker hasn't found the correct merge section for non-section symbol
7738    in relax time, this work is left to the function elf_link_input_bfd().
7739    So for non-section symbol, _bfd_merged_section_offset is also needed
7740    to find the correct symbol address.  */
7741
7742 static bfd_vma
7743 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
7744                           asection **psec, Elf_Internal_Rela *rel)
7745 {
7746   asection *sec = *psec;
7747   bfd_vma relocation;
7748
7749   relocation = (sec->output_section->vma
7750                 + sec->output_offset + sym->st_value);
7751   if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
7752     {
7753       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7754         rel->r_addend =
7755           _bfd_merged_section_offset (abfd, psec,
7756                                       elf_section_data (sec)->sec_info,
7757                                       sym->st_value + rel->r_addend);
7758       else
7759         rel->r_addend =
7760           _bfd_merged_section_offset (abfd, psec,
7761                                       elf_section_data (sec)->sec_info,
7762                                       sym->st_value) + rel->r_addend;
7763
7764       if (sec != *psec)
7765         {
7766           /* If we have changed the section, and our original section is
7767              marked with SEC_EXCLUDE, it means that the original
7768              SEC_MERGE section has been completely subsumed in some
7769              other SEC_MERGE section.  In this case, we need to leave
7770              some info around for --emit-relocs.  */
7771           if ((sec->flags & SEC_EXCLUDE) != 0)
7772             sec->kept_section = *psec;
7773           sec = *psec;
7774         }
7775       rel->r_addend -= relocation;
7776       rel->r_addend += sec->output_section->vma + sec->output_offset;
7777     }
7778   return relocation;
7779 }
7780
7781 static bfd_vma
7782 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
7783                           Elf_Internal_Sym *isymbuf,
7784                           Elf_Internal_Shdr *symtab_hdr)
7785 {
7786   bfd_signed_vma foff;
7787   bfd_vma symval, addend;
7788   Elf_Internal_Rela irel_fn;
7789   Elf_Internal_Sym *isym;
7790   asection *sym_sec;
7791
7792   /* Get the value of the symbol referred to by the reloc.  */
7793   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7794     {
7795       /* A local symbol.  */
7796       isym = isymbuf + ELF32_R_SYM (irel->r_info);
7797
7798       if (isym->st_shndx == SHN_UNDEF)
7799         sym_sec = bfd_und_section_ptr;
7800       else if (isym->st_shndx == SHN_ABS)
7801         sym_sec = bfd_abs_section_ptr;
7802       else if (isym->st_shndx == SHN_COMMON)
7803         sym_sec = bfd_com_section_ptr;
7804       else
7805         sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7806       memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
7807       symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
7808       addend = irel_fn.r_addend;
7809     }
7810   else
7811     {
7812       unsigned long indx;
7813       struct elf_link_hash_entry *h;
7814
7815       /* An external symbol.  */
7816       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7817       h = elf_sym_hashes (abfd)[indx];
7818       BFD_ASSERT (h != NULL);
7819
7820       while (h->root.type == bfd_link_hash_indirect
7821              || h->root.type == bfd_link_hash_warning)
7822         h = (struct elf_link_hash_entry *) h->root.u.i.link;
7823
7824       if (h->root.type != bfd_link_hash_defined
7825           && h->root.type != bfd_link_hash_defweak)
7826         /* This appears to be a reference to an undefined
7827            symbol.  Just ignore it--it will be caught by the
7828            regular reloc processing.  */
7829         return 0;
7830
7831       if (h->root.u.def.section->flags & SEC_MERGE)
7832         {
7833           sym_sec = h->root.u.def.section;
7834           symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
7835                                                (sym_sec)->sec_info, h->root.u.def.value);
7836           symval = symval + sym_sec->output_section->vma
7837                    + sym_sec->output_offset;
7838         }
7839       else
7840         symval = (h->root.u.def.value
7841                   + h->root.u.def.section->output_section->vma
7842                   + h->root.u.def.section->output_offset);
7843       addend = irel->r_addend;
7844     }
7845
7846   foff = symval + addend;
7847
7848   return foff;
7849 }
7850
7851 static bfd_vma
7852 calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
7853                               Elf_Internal_Rela *irel,
7854                               Elf_Internal_Shdr *symtab_hdr)
7855 {
7856   int symndx;
7857   bfd_vma *local_got_offsets;
7858   /* Get the value of the symbol referred to by the reloc.  */
7859   struct elf_link_hash_entry *h;
7860   struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);
7861
7862   /* An external symbol.  */
7863   symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7864   h = elf_sym_hashes (abfd)[symndx];
7865   while (h->root.type == bfd_link_hash_indirect
7866          || h->root.type == bfd_link_hash_warning)
7867     h = (struct elf_link_hash_entry *) h->root.u.i.link;
7868
7869   if (symndx >= 0)
7870     {
7871       BFD_ASSERT (h != NULL);
7872       return (htab->root.sgot->output_section->vma
7873               + htab->root.sgot->output_offset
7874               + h->got.offset);
7875     }
7876   else
7877     {
7878       local_got_offsets = elf_local_got_offsets (abfd);
7879       BFD_ASSERT (local_got_offsets != NULL);
7880       return (htab->root.sgot->output_section->vma
7881               + htab->root.sgot->output_offset
7882               + local_got_offsets[ELF32_R_SYM (irel->r_info)]);
7883     }
7884
7885   /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?).  */
7886   /* The check of h->root.type is passed.  */
7887 }
7888
7889 static int
7890 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
7891               asection *sec, Elf_Internal_Rela *rel)
7892 {
7893   bfd_byte *contents;
7894   unsigned short insn16;
7895
7896   if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
7897     return FALSE;
7898   contents = elf_section_data (sec)->this_hdr.contents;
7899   insn16 = bfd_getb16 (contents + rel->r_offset);
7900   if (insn16 == NDS32_NOP16)
7901     return TRUE;
7902   return FALSE;
7903 }
7904
7905 /* It checks whether the instruction could be converted to
7906    16-bit form and returns the converted one.
7907
7908    `internal_relocs' is supposed to be sorted.  */
7909
7910 static int
7911 is_convert_32_to_16 (bfd *abfd, asection *sec,
7912                      Elf_Internal_Rela *reloc,
7913                      Elf_Internal_Rela *internal_relocs,
7914                      Elf_Internal_Rela *irelend,
7915                      uint16_t *insn16)
7916 {
7917 #define NORMAL_32_TO_16 (1 << 0)
7918 #define SPECIAL_32_TO_16 (1 << 1)
7919   bfd_byte *contents = NULL;
7920   bfd_signed_vma off;
7921   bfd_vma mem_addr;
7922   uint32_t insn = 0;
7923   Elf_Internal_Rela *pc_rel;
7924   int pic_ext_target = 0;
7925   Elf_Internal_Shdr *symtab_hdr;
7926   Elf_Internal_Sym *isymbuf = NULL;
7927   int convert_type;
7928   bfd_vma offset;
7929
7930   if (reloc->r_offset + 4 > sec->size)
7931     return FALSE;
7932
7933   offset = reloc->r_offset;
7934
7935   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
7936     return FALSE;
7937   insn = bfd_getb32 (contents + offset);
7938
7939   if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
7940     convert_type = NORMAL_32_TO_16;
7941   else if (special_convert_32_to_16 (insn, insn16, reloc))
7942     convert_type = SPECIAL_32_TO_16;
7943   else
7944     return FALSE;
7945
7946   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7947   if (!nds32_get_local_syms (abfd, sec, &isymbuf))
7948     return FALSE;
7949
7950   /* Find the first relocation of the same relocation-type,
7951      so we iteratie them forward.  */
7952   pc_rel = reloc;
7953   while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
7954     pc_rel--;
7955
7956   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
7957     {
7958       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
7959           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
7960           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
7961           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
7962         {
7963           off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
7964                                   &pic_ext_target);
7965           if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
7966               || off == 0)
7967             return FALSE;
7968           break;
7969         }
7970       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
7971         {
7972           /* movi => movi55  */
7973           mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
7974                                                symtab_hdr);
7975           /* mem_addr is unsigned, but the value should
7976              be between [-16, 15].  */
7977           if ((mem_addr + 0x10) >> 5)
7978             return FALSE;
7979           break;
7980         }
7981       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
7982                || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
7983         {
7984           /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
7985              because it can be relaxed to addi for TLS_LE_ADD.  */
7986           return FALSE;
7987         }
7988       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
7989                 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
7990                && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
7991                && convert_type == SPECIAL_32_TO_16)
7992         {
7993           /* fp-as-gp
7994              We've selected a best fp-base for this access, so we can
7995              always resolve it anyway.  Do nothing.  */
7996           break;
7997         }
7998       else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
7999                 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8000                || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8001                    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8002                || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8003                    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8004         {
8005           /* Prevent unresolved addi instruction translate
8006              to addi45 or addi333.  */
8007           return FALSE;
8008         }
8009       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8010         {
8011           off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
8012                                   &pic_ext_target);
8013           if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8014             return FALSE;
8015           break;
8016         }
8017     }
8018
8019   return TRUE;
8020 }
8021
8022 static void
8023 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8024                     Elf_Internal_Rela *reloc,
8025                     Elf_Internal_Rela *internal_relocs,
8026                     Elf_Internal_Rela *irelend,
8027                     unsigned short insn16)
8028 {
8029   Elf_Internal_Rela *pc_rel;
8030   bfd_vma offset;
8031
8032   offset = reloc->r_offset;
8033   bfd_putb16 (insn16, contents + offset);
8034   /* Find the first relocation of the same relocation-type,
8035      so we iteratie them forward.  */
8036   pc_rel = reloc;
8037   while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8038     pc_rel--;
8039
8040   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8041     {
8042       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8043           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8044           || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8045         {
8046           pc_rel->r_info =
8047             ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8048         }
8049       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8050         pc_rel->r_info =
8051           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8052       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8053         pc_rel->r_info =
8054           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8055       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8056                || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8057         pc_rel->r_info =
8058           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8059       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8060         pc_rel->r_info =
8061           ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8062     }
8063 }
8064
8065 /* Find a relocation of type specified by `reloc_type'
8066    of the same r_offset with reloc.
8067    If not found, return irelend.
8068
8069    Assuming relocations are sorted by r_offset,
8070    we find the relocation from `reloc' backward untill relocs,
8071    or find it from `reloc' forward untill irelend.  */
8072
8073 static Elf_Internal_Rela *
8074 find_relocs_at_address (Elf_Internal_Rela *reloc,
8075                         Elf_Internal_Rela *relocs,
8076                         Elf_Internal_Rela *irelend,
8077                         enum elf_nds32_reloc_type reloc_type)
8078 {
8079   Elf_Internal_Rela *rel_t;
8080
8081   /* Find backward.  */
8082   for (rel_t = reloc;
8083        rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8084        rel_t--)
8085     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8086       return rel_t;
8087
8088   /* We didn't find it backward.  Try find it forward.  */
8089   for (rel_t = reloc;
8090        rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8091        rel_t++)
8092     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8093       return rel_t;
8094
8095   return irelend;
8096 }
8097
8098 /* Find a relocation of specified type and offset.
8099    `reloc' is just a refence point to find a relocation at specified offset.
8100    If not found, return irelend.
8101
8102    Assuming relocations are sorted by r_offset,
8103    we find the relocation from `reloc' backward untill relocs,
8104    or find it from `reloc' forward untill irelend.  */
8105
8106 static Elf_Internal_Rela *
8107 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8108                              Elf_Internal_Rela *relocs,
8109                              Elf_Internal_Rela *irelend,
8110                              enum elf_nds32_reloc_type reloc_type,
8111                              bfd_vma offset_p)
8112 {
8113   Elf_Internal_Rela *rel_t = NULL;
8114
8115   /* First, we try to find a relocation of offset `offset_p',
8116      and then we use find_relocs_at_address to find specific type.  */
8117
8118   if (reloc->r_offset > offset_p)
8119     {
8120       /* Find backward.  */
8121       for (rel_t = reloc;
8122            rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8123         /* Do nothing.  */;
8124     }
8125   else if (reloc->r_offset < offset_p)
8126     {
8127       /* Find forward.  */
8128       for (rel_t = reloc;
8129            rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8130         /* Do nothing.  */;
8131     }
8132   else
8133     rel_t = reloc;
8134
8135   /* Not found?  */
8136   if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8137     return irelend;
8138
8139   return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8140 }
8141
8142 static bfd_boolean
8143 nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
8144                             Elf_Internal_Rela *internal_relocs,
8145                             Elf_Internal_Rela *irelend,
8146                             unsigned char reloc_type)
8147 {
8148   Elf_Internal_Rela *rel_t;
8149
8150   for (rel_t = reloc;
8151        rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
8152        rel_t--)
8153     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8154       {
8155         if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8156             && rel_t->r_addend == reloc->r_addend)
8157           continue;
8158         return TRUE;
8159       }
8160
8161   for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8162        rel_t++)
8163     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8164       {
8165         if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8166             && rel_t->r_addend == reloc->r_addend)
8167           continue;
8168         return TRUE;
8169       }
8170
8171   return FALSE;
8172 }
8173
8174 typedef struct nds32_elf_blank nds32_elf_blank_t;
8175 struct nds32_elf_blank
8176 {
8177   /* Where the blank begins.  */
8178   bfd_vma offset;
8179   /* The size of the blank.  */
8180   bfd_vma size;
8181   /* The accumulative size before this blank.  */
8182   bfd_vma total_size;
8183   nds32_elf_blank_t *next;
8184   nds32_elf_blank_t *prev;
8185 };
8186
8187 static nds32_elf_blank_t *blank_free_list = NULL;
8188
8189 static nds32_elf_blank_t *
8190 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8191 {
8192   nds32_elf_blank_t *blank_t;
8193
8194   if (blank_free_list)
8195     {
8196       blank_t = blank_free_list;
8197       blank_free_list = blank_free_list->next;
8198     }
8199   else
8200     blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
8201
8202   if (blank_t == NULL)
8203     return NULL;
8204
8205   blank_t->offset = offset_p;
8206   blank_t->size = size_p;
8207   blank_t->total_size = 0;
8208   blank_t->next = NULL;
8209   blank_t->prev = NULL;
8210
8211   return blank_t;
8212 }
8213
8214 static void
8215 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
8216 {
8217   if (blank_free_list)
8218     {
8219       blank_free_list->prev = blank_p;
8220       blank_p->next = blank_free_list;
8221     }
8222   else
8223     blank_p->next = NULL;
8224
8225   blank_p->prev = NULL;
8226   blank_free_list = blank_p;
8227 }
8228
8229 static void
8230 clean_nds32_elf_blank (void)
8231 {
8232   nds32_elf_blank_t *blank_t;
8233
8234   while (blank_free_list)
8235     {
8236       blank_t = blank_free_list;
8237       blank_free_list = blank_free_list->next;
8238       free (blank_t);
8239     }
8240 }
8241
8242 static nds32_elf_blank_t *
8243 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
8244 {
8245   nds32_elf_blank_t *blank_t;
8246
8247   if (!blank_p)
8248     return NULL;
8249   blank_t = blank_p;
8250
8251   while (blank_t && addr < blank_t->offset)
8252     blank_t = blank_t->prev;
8253   while (blank_t && blank_t->next && addr >= blank_t->next->offset)
8254     blank_t = blank_t->next;
8255
8256   return blank_t;
8257 }
8258
8259 static bfd_vma
8260 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8261                            int overwrite)
8262 {
8263   nds32_elf_blank_t *blank_t;
8264
8265   blank_t = search_nds32_elf_blank (*blank_p, addr);
8266   if (!blank_t)
8267     return 0;
8268
8269   if (overwrite)
8270     *blank_p = blank_t;
8271
8272   if (addr < blank_t->offset + blank_t->size)
8273     return blank_t->total_size + (addr - blank_t->offset);
8274   else
8275     return blank_t->total_size + blank_t->size;
8276 }
8277
8278 static bfd_boolean
8279 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
8280 {
8281   nds32_elf_blank_t *blank_t, *blank_t2;
8282
8283   if (!*blank_p)
8284     {
8285       *blank_p = create_nds32_elf_blank (addr, len);
8286       return *blank_p ? TRUE : FALSE;
8287     }
8288
8289   blank_t = search_nds32_elf_blank (*blank_p, addr);
8290
8291   if (blank_t == NULL)
8292     {
8293       blank_t = create_nds32_elf_blank (addr, len);
8294       if (!blank_t)
8295         return FALSE;
8296       while ((*blank_p)->prev != NULL)
8297         *blank_p = (*blank_p)->prev;
8298       blank_t->next = *blank_p;
8299       (*blank_p)->prev = blank_t;
8300       (*blank_p) = blank_t;
8301       return TRUE;
8302     }
8303
8304   if (addr < blank_t->offset + blank_t->size)
8305     {
8306       if (addr > blank_t->offset + blank_t->size)
8307         blank_t->size = addr - blank_t->offset;
8308     }
8309   else
8310     {
8311       blank_t2 = create_nds32_elf_blank (addr, len);
8312       if (!blank_t2)
8313         return FALSE;
8314       if (blank_t->next)
8315         {
8316           blank_t->next->prev = blank_t2;
8317           blank_t2->next = blank_t->next;
8318         }
8319       blank_t2->prev = blank_t;
8320       blank_t->next = blank_t2;
8321       *blank_p = blank_t2;
8322     }
8323
8324   return TRUE;
8325 }
8326
8327 static bfd_boolean
8328 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8329                                      bfd_vma len)
8330 {
8331   nds32_elf_blank_t *blank_t;
8332
8333   if (!insert_nds32_elf_blank (blank_p, addr, len))
8334     return FALSE;
8335
8336   blank_t = *blank_p;
8337
8338   if (!blank_t->prev)
8339     {
8340       blank_t->total_size = 0;
8341       blank_t = blank_t->next;
8342     }
8343
8344   while (blank_t)
8345     {
8346       blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
8347       blank_t = blank_t->next;
8348     }
8349
8350   return TRUE;
8351 }
8352
8353 static void
8354 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
8355 {
8356   nds32_elf_blank_t *blank_t;
8357   bfd_vma total_size = 0;
8358
8359   if (!blank_p)
8360     return;
8361
8362   blank_t = blank_p;
8363   while (blank_t->prev)
8364     blank_t = blank_t->prev;
8365   while (blank_t)
8366     {
8367       blank_t->total_size = total_size;
8368       total_size += blank_t->size;
8369       blank_t = blank_t->next;
8370     }
8371 }
8372
8373 static bfd_boolean
8374 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
8375                                nds32_elf_blank_t *blank_p)
8376 {
8377   Elf_Internal_Shdr *symtab_hdr;        /* Symbol table header of this bfd.  */
8378   Elf_Internal_Sym *isym = NULL;        /* Symbol table of this bfd.  */
8379   Elf_Internal_Sym *isymend;            /* Symbol entry iterator.  */
8380   unsigned int sec_shndx;               /* The section the be relaxed.  */
8381   bfd_byte *contents;                   /* Contents data of iterating section.  */
8382   Elf_Internal_Rela *internal_relocs;
8383   Elf_Internal_Rela *irel;
8384   Elf_Internal_Rela *irelend;
8385   struct elf_link_hash_entry **sym_hashes;
8386   struct elf_link_hash_entry **end_hashes;
8387   unsigned int symcount;
8388   asection *sect;
8389   nds32_elf_blank_t *blank_t;
8390   nds32_elf_blank_t *blank_t2;
8391   nds32_elf_blank_t *blank_head;
8392
8393   blank_head = blank_t = blank_p;
8394   while (blank_head->prev != NULL)
8395     blank_head = blank_head->prev;
8396   while (blank_t->next != NULL)
8397     blank_t = blank_t->next;
8398
8399   if (blank_t->offset + blank_t->size <= sec->size)
8400     {
8401       blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
8402       blank_t->next->prev = blank_t;
8403     }
8404   if (blank_head->offset > 0)
8405     {
8406       blank_head->prev = create_nds32_elf_blank (0, 0);
8407       blank_head->prev->next = blank_head;
8408       blank_head = blank_head->prev;
8409     }
8410
8411   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8412
8413   /* The deletion must stop at the next ALIGN reloc for an alignment
8414      power larger than the number of bytes we are deleting.  */
8415
8416   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8417   if (!nds32_get_local_syms (abfd, sec, &isym))
8418     return FALSE;
8419
8420   if (isym == NULL)
8421     {
8422       isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8423                                    symtab_hdr->sh_info, 0, NULL, NULL, NULL);
8424       symtab_hdr->contents = (bfd_byte *) isym;
8425     }
8426
8427   if (isym == NULL || symtab_hdr->sh_info == 0)
8428     return FALSE;
8429
8430   blank_t = blank_head;
8431   calc_nds32_blank_total (blank_head);
8432
8433   for (sect = abfd->sections; sect != NULL; sect = sect->next)
8434     {
8435       /* Adjust all the relocs.  */
8436
8437       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
8438       internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
8439                                                    TRUE /* keep_memory */);
8440       irelend = internal_relocs + sect->reloc_count;
8441
8442       blank_t = blank_head;
8443       blank_t2 = blank_head;
8444
8445       if (!(sect->flags & SEC_RELOC))
8446         continue;
8447
8448       nds32_get_section_contents (abfd, sect, &contents, TRUE);
8449
8450       for (irel = internal_relocs; irel < irelend; irel++)
8451         {
8452           bfd_vma raddr;
8453
8454           if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
8455               && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
8456               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8457             {
8458               unsigned long val = 0;
8459               unsigned long mask;
8460               long before, between;
8461               long offset = 0;
8462
8463               switch (ELF32_R_TYPE (irel->r_info))
8464                 {
8465                 case R_NDS32_DIFF8:
8466                   offset = bfd_get_8 (abfd, contents + irel->r_offset);
8467                   break;
8468                 case R_NDS32_DIFF16:
8469                   offset = bfd_get_16 (abfd, contents + irel->r_offset);
8470                   break;
8471                 case R_NDS32_DIFF32:
8472                   val = bfd_get_32 (abfd, contents + irel->r_offset);
8473                   /* Get the signed bit and mask for the high part.  The
8474                      gcc will alarm when right shift 32-bit since the
8475                      type size of long may be 32-bit.  */
8476                   mask = 0 - (val >> 31);
8477                   if (mask)
8478                     offset = (val | (mask - 0xffffffff));
8479                   else
8480                     offset = val;
8481                   break;
8482                 default:
8483                   BFD_ASSERT (0);
8484                 }
8485
8486               /*                  DIFF value
8487                 0            |encoded in location|
8488                 |------------|-------------------|---------
8489                             sym+off(addend)
8490                 -- before ---| *****************
8491                 --------------------- between ---|
8492
8493                 We only care how much data are relax between DIFF,
8494                 marked as ***.  */
8495
8496               before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8497               between = get_nds32_elf_blank_total (&blank_t,
8498                                                    irel->r_addend + offset, 0);
8499               if (between == before)
8500                 goto done_adjust_diff;
8501
8502               switch (ELF32_R_TYPE (irel->r_info))
8503                 {
8504                 case R_NDS32_DIFF8:
8505                   bfd_put_8 (abfd, offset - (between - before),
8506                              contents + irel->r_offset);
8507                   break;
8508                 case R_NDS32_DIFF16:
8509                   bfd_put_16 (abfd, offset - (between - before),
8510                               contents + irel->r_offset);
8511                   break;
8512                 case R_NDS32_DIFF32:
8513                   bfd_put_32 (abfd, offset - (between - before),
8514                               contents + irel->r_offset);
8515                   break;
8516                 }
8517             }
8518           else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
8519               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8520             {
8521               bfd_vma val = 0;
8522               unsigned int len = 0;
8523               unsigned long before, between;
8524               bfd_byte *endp, *p;
8525
8526               val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
8527                                                &len);
8528
8529               before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8530               between = get_nds32_elf_blank_total (&blank_t,
8531                                                    irel->r_addend + val, 0);
8532               if (between == before)
8533                 goto done_adjust_diff;
8534
8535               p = contents + irel->r_offset;
8536               endp = p + len -1;
8537               memset (p, 0x80, len);
8538               *(endp) = 0;
8539               p = write_uleb128 (p, val - (between - before)) - 1;
8540               if (p < endp)
8541                 *p |= 0x80;
8542             }
8543 done_adjust_diff:
8544
8545           if (sec == sect)
8546             {
8547               raddr = irel->r_offset;
8548               irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
8549                                                            irel->r_offset, 1);
8550
8551               if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
8552                 continue;
8553               if (blank_t2 && blank_t2->next
8554                   && (blank_t2->offset > raddr
8555                       || blank_t2->next->offset <= raddr))
8556                 _bfd_error_handler
8557                   (_("%B: Error: search_nds32_elf_blank reports wrong node\n"), abfd);
8558
8559               /* Mark reloc in deleted portion as NONE.
8560                  For some relocs like R_NDS32_LABEL that doesn't modify the
8561                  content in the section.  R_NDS32_LABEL doesn't belong to the
8562                  instruction in the section, so we should preserve it.  */
8563               if (raddr >= blank_t2->offset
8564                   && raddr < blank_t2->offset + blank_t2->size
8565                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
8566                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
8567                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
8568                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
8569                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
8570                   && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
8571                 {
8572                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
8573                                                R_NDS32_NONE);
8574                   continue;
8575                 }
8576             }
8577
8578           if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
8579               || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
8580               || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
8581             continue;
8582
8583           if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
8584               && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
8585               && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
8586             {
8587               if (irel->r_addend <= sec->size)
8588                 irel->r_addend -=
8589                   get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
8590             }
8591         }
8592     }
8593
8594   /* Adjust the local symbols defined in this section.  */
8595   blank_t = blank_head;
8596   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
8597     {
8598       if (isym->st_shndx == sec_shndx)
8599         {
8600           if (isym->st_value <= sec->size)
8601             {
8602               bfd_vma ahead;
8603               bfd_vma orig_addr = isym->st_value;
8604
8605               ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
8606               isym->st_value -= ahead;
8607
8608               /* Adjust function size.  */
8609               if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
8610                   && isym->st_size > 0)
8611                 isym->st_size -=
8612                   get_nds32_elf_blank_total
8613                   (&blank_t, orig_addr + isym->st_size, 0) - ahead;
8614             }
8615         }
8616     }
8617
8618   /* Now adjust the global symbols defined in this section.  */
8619   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
8620               - symtab_hdr->sh_info);
8621   sym_hashes = elf_sym_hashes (abfd);
8622   end_hashes = sym_hashes + symcount;
8623   blank_t = blank_head;
8624   for (; sym_hashes < end_hashes; sym_hashes++)
8625     {
8626       struct elf_link_hash_entry *sym_hash = *sym_hashes;
8627
8628       if ((sym_hash->root.type == bfd_link_hash_defined
8629            || sym_hash->root.type == bfd_link_hash_defweak)
8630           && sym_hash->root.u.def.section == sec)
8631         {
8632           if (sym_hash->root.u.def.value <= sec->size)
8633             {
8634               bfd_vma ahead;
8635               bfd_vma orig_addr = sym_hash->root.u.def.value;
8636
8637               ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
8638               sym_hash->root.u.def.value -= ahead;
8639
8640               /* Adjust function size.  */
8641               if (sym_hash->type == STT_FUNC)
8642                 sym_hash->size -=
8643                   get_nds32_elf_blank_total
8644                   (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
8645
8646             }
8647         }
8648     }
8649
8650   contents = elf_section_data (sec)->this_hdr.contents;
8651   blank_t = blank_head;
8652   while (blank_t->next)
8653     {
8654       /* Actually delete the bytes.  */
8655
8656       /* If current blank is the last blank overlap with current section,
8657          go to finish process.  */
8658       if (sec->size <= (blank_t->next->offset))
8659         break;
8660
8661       memmove (contents + blank_t->offset - blank_t->total_size,
8662                contents + blank_t->offset + blank_t->size,
8663                blank_t->next->offset - (blank_t->offset + blank_t->size));
8664
8665       blank_t = blank_t->next;
8666     }
8667
8668   if (sec->size > (blank_t->offset + blank_t->size))
8669     {
8670       /* There are remaining code between blank and section boundary.
8671          Move the remaining code to appropriate location.  */
8672       memmove (contents + blank_t->offset - blank_t->total_size,
8673                contents + blank_t->offset + blank_t->size,
8674                sec->size - (blank_t->offset + blank_t->size));
8675       sec->size -= blank_t->total_size + blank_t->size;
8676     }
8677   else
8678     /* This blank is not entirely included in the section,
8679        reduce the section size by only part of the blank size.  */
8680     sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
8681
8682   while (blank_head)
8683     {
8684       blank_t = blank_head;
8685       blank_head = blank_head->next;
8686       remove_nds32_elf_blank (blank_t);
8687     }
8688
8689   return TRUE;
8690 }
8691
8692 /* Get the contents of a section.  */
8693
8694 static int
8695 nds32_get_section_contents (bfd *abfd, asection *sec,
8696                             bfd_byte **contents_p, bfd_boolean cache)
8697 {
8698   /* Get the section contents.  */
8699   if (elf_section_data (sec)->this_hdr.contents != NULL)
8700     *contents_p = elf_section_data (sec)->this_hdr.contents;
8701   else
8702     {
8703       if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
8704         return FALSE;
8705       if (cache)
8706         elf_section_data (sec)->this_hdr.contents = *contents_p;
8707     }
8708
8709   return TRUE;
8710 }
8711
8712 /* Get the contents of the internal symbol of abfd.  */
8713
8714 static int
8715 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
8716                       Elf_Internal_Sym **isymbuf_p)
8717 {
8718   Elf_Internal_Shdr *symtab_hdr;
8719   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8720
8721   /* Read this BFD's local symbols if we haven't done so already.  */
8722   if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
8723     {
8724       *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
8725       if (*isymbuf_p == NULL)
8726         {
8727           *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8728                                              symtab_hdr->sh_info, 0,
8729                                              NULL, NULL, NULL);
8730           if (*isymbuf_p == NULL)
8731             return FALSE;
8732         }
8733     }
8734   symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
8735
8736   return TRUE;
8737 }
8738
8739 /* Range of small data.  */
8740 static bfd_vma sdata_range[2][2];
8741 static bfd_vma const sdata_init_range[2] =
8742 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
8743
8744 static int
8745 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
8746                      bfd_byte *contents, bfd_vma addr)
8747 {
8748   unsigned long insn = bfd_getb32 (contents + addr);
8749
8750   if (insn & 0x80000000)
8751     return 2;
8752
8753   return 4;
8754 }
8755
8756 /* Set the gp relax range.  We have to measure the safe range
8757    to do gp relaxation.  */
8758
8759 static void
8760 relax_range_measurement (bfd *abfd)
8761 {
8762   asection *sec_f, *sec_b;
8763   /* For upper bound.   */
8764   bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
8765   bfd_vma align;
8766   static int decide_relax_range = 0;
8767   int i;
8768   int range_number = sizeof (sdata_init_range) / sizeof (sdata_init_range[0]);
8769
8770   if (decide_relax_range)
8771     return;
8772   decide_relax_range = 1;
8773
8774   if (sda_rela_sec == NULL)
8775     {
8776       /* Since there is no data sections, we assume the range is page size.  */
8777       for (i = 0; i < range_number; i++)
8778         {
8779           sdata_range[i][0] = sdata_init_range[i] - 0x1000;
8780           sdata_range[i][1] = sdata_init_range[i] - 0x1000;
8781         }
8782       return;
8783     }
8784
8785   /* Get the biggest alignment power after the gp located section.  */
8786   sec_f = sda_rela_sec->output_section;
8787   sec_b = sec_f->next;
8788   align = 0;
8789   while (sec_b != NULL)
8790     {
8791       if ((unsigned)(1 << sec_b->alignment_power) > align)
8792         align = (1 << sec_b->alignment_power);
8793       sec_b = sec_b->next;
8794     }
8795
8796   /* I guess we can not determine the section before
8797      gp located section, so we assume the align is max page size.  */
8798   for (i = 0; i < range_number; i++)
8799     {
8800       sdata_range[i][1] = sdata_init_range[i] - align;
8801       BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
8802       sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
8803       BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
8804     }
8805 }
8806
8807 /* These are macros used to check flags encoded in r_addend.
8808    They are only used by nds32_elf_relax_section ().  */
8809 #define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
8810 #define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
8811 #define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
8812 #define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
8813
8814 static const char * unrecognized_reloc_msg =
8815   /* xgettext:c-format */
8816   N_("%B: warning: %s points to unrecognized reloc at %#Lx");
8817
8818 /* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
8819
8820 static bfd_boolean
8821 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
8822                            Elf_Internal_Rela *internal_relocs, int *insn_len,
8823                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
8824                            Elf_Internal_Shdr *symtab_hdr)
8825 {
8826   /* There are 3 variations for LONGCALL1
8827      case 4-4-2; 16-bit on, optimize off or optimize for space
8828      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
8829      ori   ta, ta, lo12(symbol) ; LO12S0
8830      jral5 ta                   ;
8831
8832      case 4-4-4; 16-bit off, optimize don't care
8833      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
8834      ori   ta, ta, lo12(symbol) ; LO12S0
8835      jral  ta                   ;
8836
8837      case 4-4-4; 16-bit on, optimize for speed
8838      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
8839      ori   ta, ta, lo12(symbol) ; LO12S0
8840      jral  ta                   ;
8841      Check code for -mlong-calls output.  */
8842
8843   /* Get the reloc for the address from which the register is
8844      being loaded.  This reloc will tell us which function is
8845      actually being called.  */
8846
8847   bfd_vma laddr;
8848   int seq_len;  /* Original length of instruction sequence.  */
8849   uint32_t insn;
8850   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
8851   int pic_ext_target = 0;
8852   bfd_signed_vma foff;
8853   uint16_t insn16;
8854
8855   irelend = internal_relocs + sec->reloc_count;
8856   seq_len = GET_SEQ_LEN (irel->r_addend);
8857   laddr = irel->r_offset;
8858   *insn_len = seq_len;
8859
8860   hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8861                                            R_NDS32_HI20_RELA, laddr);
8862   lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
8863                                            R_NDS32_LO12S0_ORI_RELA,
8864                                            laddr + 4);
8865
8866   if (hi_irelfn == irelend || lo_irelfn == irelend)
8867     {
8868       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
8869                           irel->r_offset);
8870       return FALSE;
8871     }
8872
8873   /* Get the value of the symbol referred to by the reloc.  */
8874   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
8875                            &pic_ext_target);
8876
8877   /* This condition only happened when symbol is undefined.  */
8878   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
8879       || foff >= CONSERVATIVE_24BIT_S1)
8880     return FALSE;
8881
8882   /* Relax to: jal symbol; 25_PCREL */
8883   /* For simplicity of coding, we are going to modify the section
8884      contents, the section relocs, and the BFD symbol table.  We
8885      must tell the rest of the code not to free up this
8886      information.  It would be possible to instead create a table
8887      of changes which have to be made, as is done in coff-mips.c;
8888      that would be more work, but would require less memory when
8889      the linker is run.  */
8890
8891   /* Replace the long call with a jal.  */
8892   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
8893                                R_NDS32_25_PCREL_RELA);
8894   irel->r_addend = hi_irelfn->r_addend;
8895
8896   /* We don't resolve this here but resolve it in relocate_section.  */
8897   insn = INSN_JAL;
8898   bfd_putb32 (insn, contents + irel->r_offset);
8899
8900   hi_irelfn->r_info =
8901     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
8902   lo_irelfn->r_info =
8903     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
8904   *insn_len = 4;
8905
8906   if (seq_len & 0x2)
8907     {
8908       insn16 = NDS32_NOP16;
8909       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
8910       lo_irelfn->r_info =
8911         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
8912       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
8913       *insn_len += 2;
8914     }
8915   return TRUE;
8916 }
8917
8918 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
8919 /* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
8920
8921 static bfd_boolean
8922 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
8923                            Elf_Internal_Rela *internal_relocs, int *insn_len,
8924                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
8925                            Elf_Internal_Shdr *symtab_hdr)
8926 {
8927   /* bltz  rt, .L1   ; LONGCALL2
8928      jal   symbol   ; 25_PCREL
8929      .L1: */
8930
8931   /* Get the reloc for the address from which the register is
8932      being loaded.  This reloc will tell us which function is
8933      actually being called.  */
8934
8935   bfd_vma laddr;
8936   uint32_t insn;
8937   Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
8938   int pic_ext_target = 0;
8939   bfd_signed_vma foff;
8940
8941   irelend = internal_relocs + sec->reloc_count;
8942   laddr = irel->r_offset;
8943   i1_irelfn =
8944     find_relocs_at_address_addr (irel, internal_relocs, irelend,
8945                                  R_NDS32_25_PCREL_RELA, laddr + 4);
8946
8947   if (i1_irelfn == irelend)
8948     {
8949       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
8950                           irel->r_offset);
8951       return FALSE;
8952     }
8953
8954   insn = bfd_getb32 (contents + laddr);
8955
8956   /* Get the value of the symbol referred to by the reloc.  */
8957   foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
8958                            &pic_ext_target);
8959
8960   if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
8961       || foff >= CONSERVATIVE_16BIT_S1)
8962     return FALSE;
8963
8964   /* Relax to   bgezal   rt, label ; 17_PCREL
8965      or         bltzal   rt, label ; 17_PCREL */
8966
8967   /* Convert to complimentary conditional call.  */
8968   insn = CONVERT_CONDITION_CALL (insn);
8969
8970   /* For simplicity of coding, we are going to modify the section
8971      contents, the section relocs, and the BFD symbol table.  We
8972      must tell the rest of the code not to free up this
8973      information.  It would be possible to instead create a table
8974      of changes which have to be made, as is done in coff-mips.c;
8975      that would be more work, but would require less memory when
8976      the linker is run.  */
8977
8978   /* Clean unnessary relocations.  */
8979   i1_irelfn->r_info =
8980     ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
8981   cond_irelfn =
8982     find_relocs_at_address_addr (irel, internal_relocs, irelend,
8983                                  R_NDS32_17_PCREL_RELA, laddr);
8984   if (cond_irelfn != irelend)
8985     cond_irelfn->r_info =
8986       ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
8987
8988   /* Replace the long call with a bgezal.  */
8989   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
8990                                R_NDS32_17_PCREL_RELA);
8991   irel->r_addend = i1_irelfn->r_addend;
8992
8993   bfd_putb32 (insn, contents + irel->r_offset);
8994
8995   *insn_len = 4;
8996   return TRUE;
8997 }
8998
8999 /* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9000
9001 static bfd_boolean
9002 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9003                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9004                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9005                            Elf_Internal_Shdr *symtab_hdr)
9006 {
9007   /* There are 3 variations for LONGCALL3
9008      case 4-4-4-2; 16-bit on, optimize off or optimize for space
9009      bltz  rt,   $1                ; LONGCALL3
9010      sethi ta,   hi20(symbol)      ; HI20
9011      ori   ta, ta,  lo12(symbol)   ; LO12S0
9012      jral5 ta                      ;
9013      $1
9014
9015      case 4-4-4-4; 16-bit off, optimize don't care
9016      bltz  rt,   $1                ; LONGCALL3
9017      sethi ta,   hi20(symbol)      ; HI20
9018      ori   ta, ta,  lo12(symbol)   ; LO12S0
9019      jral  ta                      ;
9020      $1
9021
9022      case 4-4-4-4; 16-bit on, optimize for speed
9023      bltz  rt,   $1                ; LONGCALL3
9024      sethi ta,   hi20(symbol)      ; HI20
9025      ori   ta, ta,  lo12(symbol)   ; LO12S0
9026      jral  ta                      ;
9027      $1 */
9028
9029   /* Get the reloc for the address from which the register is
9030      being loaded.  This reloc will tell us which function is
9031      actually being called.  */
9032
9033   bfd_vma laddr;
9034   int seq_len;  /* Original length of instruction sequence.  */
9035   uint32_t insn;
9036   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9037   int pic_ext_target = 0;
9038   bfd_signed_vma foff;
9039   uint16_t insn16;
9040
9041   irelend = internal_relocs + sec->reloc_count;
9042   seq_len = GET_SEQ_LEN (irel->r_addend);
9043   laddr = irel->r_offset;
9044   *insn_len = seq_len;
9045
9046   hi_irelfn =
9047     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9048                                  R_NDS32_HI20_RELA, laddr + 4);
9049   lo_irelfn =
9050     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9051                                  R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9052
9053   if (hi_irelfn == irelend || lo_irelfn == irelend)
9054     {
9055       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9056                           irel->r_offset);
9057       return FALSE;
9058     }
9059
9060   /* Get the value of the symbol referred to by the reloc.  */
9061   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9062                            &pic_ext_target);
9063
9064   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9065       || foff >= CONSERVATIVE_24BIT_S1)
9066     return FALSE;
9067
9068   insn = bfd_getb32 (contents + laddr);
9069   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9070     {
9071       /* Relax to  bgezal   rt, label ; 17_PCREL
9072          or        bltzal   rt, label ; 17_PCREL */
9073
9074       /* Convert to complimentary conditional call.  */
9075       insn = CONVERT_CONDITION_CALL (insn);
9076       bfd_putb32 (insn, contents + irel->r_offset);
9077
9078       *insn_len = 4;
9079       irel->r_info =
9080         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9081       hi_irelfn->r_info =
9082         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9083       lo_irelfn->r_info =
9084         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9085
9086       cond_irelfn =
9087         find_relocs_at_address_addr (irel, internal_relocs, irelend,
9088                                      R_NDS32_17_PCREL_RELA, laddr);
9089       if (cond_irelfn != irelend)
9090         {
9091           cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9092                                               R_NDS32_17_PCREL_RELA);
9093           cond_irelfn->r_addend = hi_irelfn->r_addend;
9094         }
9095
9096       if (seq_len & 0x2)
9097         {
9098           insn16 = NDS32_NOP16;
9099           bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9100           hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9101                                             R_NDS32_INSN16);
9102           hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9103           insn_len += 2;
9104         }
9105     }
9106   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9107     {
9108       /* Relax to the following instruction sequence
9109          bltz  rt,   $1 ; LONGCALL2
9110          jal   symbol   ; 25_PCREL
9111          $1     */
9112       *insn_len = 8;
9113       insn = INSN_JAL;
9114       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9115
9116       hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9117                                         R_NDS32_25_PCREL_RELA);
9118       irel->r_info =
9119         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9120
9121       lo_irelfn->r_info =
9122         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9123
9124       if (seq_len & 0x2)
9125         {
9126           insn16 = NDS32_NOP16;
9127           bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9128           lo_irelfn->r_info =
9129             ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9130           lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9131           insn_len += 2;
9132         }
9133     }
9134   return TRUE;
9135 }
9136
9137 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
9138
9139 static bfd_boolean
9140 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9141                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9142                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9143                            Elf_Internal_Shdr *symtab_hdr)
9144 {
9145   /* There are 3 variations for LONGJUMP1
9146      case 4-4-2; 16-bit bit on, optimize off or optimize for space
9147      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
9148      ori   ta, ta, lo12(symbol)  ; LO12S0
9149      jr5   ta                    ;
9150
9151      case 4-4-4; 16-bit off, optimize don't care
9152      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
9153      ori   ta, ta, lo12(symbol)  ; LO12S0
9154      jr    ta                    ;
9155
9156      case 4-4-4; 16-bit on, optimize for speed
9157      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
9158      ori   ta, ta, lo12(symbol)  ; LO12S0
9159      jr    ta                    ;      */
9160
9161   /* Get the reloc for the address from which the register is
9162      being loaded.  This reloc will tell us which function is
9163      actually being called.  */
9164
9165   bfd_vma laddr;
9166   int seq_len;  /* Original length of instruction sequence.  */
9167   int insn16_on;        /* 16-bit on/off.  */
9168   uint32_t insn;
9169   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9170   int pic_ext_target = 0;
9171   bfd_signed_vma foff;
9172   uint16_t insn16;
9173   unsigned long reloc;
9174
9175   irelend = internal_relocs + sec->reloc_count;
9176   seq_len = GET_SEQ_LEN (irel->r_addend);
9177   laddr = irel->r_offset;
9178   *insn_len = seq_len;
9179   insn16_on = IS_16BIT_ON (irel->r_addend);
9180
9181   hi_irelfn =
9182     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9183                                  R_NDS32_HI20_RELA, laddr);
9184   lo_irelfn =
9185     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9186                                  R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9187   if (hi_irelfn == irelend || lo_irelfn == irelend)
9188     {
9189       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
9190                           irel->r_offset);
9191       return FALSE;
9192     }
9193
9194   /* Get the value of the symbol referred to by the reloc.  */
9195   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9196                            &pic_ext_target);
9197
9198   if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
9199       || foff < -CONSERVATIVE_24BIT_S1)
9200     return FALSE;
9201
9202   if (insn16_on && foff >= -ACCURATE_8BIT_S1
9203       && foff < ACCURATE_8BIT_S1 && (seq_len & 0x2))
9204     {
9205       /* j8     label */
9206       /* 16-bit on, but not optimized for speed.  */
9207       reloc = R_NDS32_9_PCREL_RELA;
9208       insn16 = INSN_J8;
9209       bfd_putb16 (insn16, contents + irel->r_offset);
9210       *insn_len = 2;
9211       irel->r_info =
9212         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9213     }
9214   else
9215     {
9216       /* j     label */
9217       reloc = R_NDS32_25_PCREL_RELA;
9218       insn = INSN_J;
9219       bfd_putb32 (insn, contents + irel->r_offset);
9220       *insn_len = 4;
9221       irel->r_info =
9222         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9223       irel->r_addend = 0;
9224     }
9225
9226   hi_irelfn->r_info =
9227     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9228   lo_irelfn->r_info =
9229     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9230
9231   if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
9232     {
9233       insn16 = NDS32_NOP16;
9234       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9235       lo_irelfn->r_info =
9236         ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9237                       R_NDS32_INSN16);
9238       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9239       *insn_len += 2;
9240     }
9241   return TRUE;
9242 }
9243
9244 /* Revert condition branch.  This function does not check if the input
9245    instruction is condition branch or not.  */
9246
9247 static void
9248 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
9249                            uint16_t *re_insn16, uint32_t *re_insn)
9250 {
9251   uint32_t comp_insn = 0;
9252   uint16_t comp_insn16 = 0;
9253
9254   if (insn)
9255     {
9256       if (N32_OP6 (insn) == N32_OP6_BR1)
9257         {
9258           /* beqs label.  */
9259           comp_insn = (insn ^ 0x4000) & 0xffffc000;
9260           if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
9261             {
9262               /* Insn can be contracted to 16-bit implied r5.  */
9263               comp_insn16 =
9264                 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
9265               comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9266             }
9267         }
9268       else if (N32_OP6 (insn) == N32_OP6_BR3)
9269         {
9270           /* bnec $ta, imm11, label.  */
9271           comp_insn = (insn ^ 0x80000) & 0xffffff00;
9272         }
9273       else
9274         {
9275           comp_insn = (insn ^ 0x10000) & 0xffffc000;
9276           if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
9277               || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
9278             {
9279               if (N32_IS_RT3 (insn))
9280                 {
9281                   /* Insn can be contracted to 16-bit.  */
9282                   comp_insn16 =
9283                     (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
9284                   comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9285                 }
9286               else if (N32_RT5 (insn) == REG_R15)
9287                 {
9288                   /* Insn can be contracted to 16-bit.  */
9289                   comp_insn16 =
9290                     (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
9291                 }
9292             }
9293         }
9294     }
9295   else
9296     {
9297       switch ((insn16 & 0xf000) >> 12)
9298         {
9299         case 0xc:
9300           /* beqz38 or bnez38 */
9301           comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9302           comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
9303           comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
9304           break;
9305
9306         case 0xd:
9307           /* beqs38 or bnes38 */
9308           comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9309           comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
9310           comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
9311             | (REG_R5 << 15);
9312           break;
9313
9314         case 0xe:
9315           /* beqzS8 or bnezS8 */
9316           comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
9317           comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
9318           comp_insn |= REG_R15 << 20;
9319           break;
9320
9321         default:
9322           break;
9323         }
9324     }
9325   if (comp_insn && re_insn)
9326     *re_insn = comp_insn;
9327   if (comp_insn16 && re_insn16)
9328     *re_insn16 = comp_insn16;
9329 }
9330
9331 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
9332
9333 static bfd_boolean
9334 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9335                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9336                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9337                            Elf_Internal_Shdr *symtab_hdr)
9338 {
9339   /* There are 3 variations for LONGJUMP2
9340      case 2-4;  1st insn convertible, 16-bit on,
9341      optimize off or optimize for space
9342      bnes38  rt, ra, $1 ; LONGJUMP2
9343      j       label      ; 25_PCREL
9344      $1:
9345
9346      case 4-4; 1st insn not convertible
9347      bne  rt, ra, $1 ; LONGJUMP2
9348      j    label      ; 25_PCREL
9349      $1:
9350
9351      case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9352      bne  rt, ra, $1 ; LONGJUMP2
9353      j    label      ; 25_PCREL
9354      $1: */
9355
9356   /* Get the reloc for the address from which the register is
9357      being loaded.  This reloc will tell us which function is
9358      actually being called.  */
9359
9360   bfd_vma laddr;
9361   int seq_len;  /* Original length of instruction sequence.  */
9362   Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
9363   int pic_ext_target = 0, first_size;
9364   unsigned int i;
9365   bfd_signed_vma foff;
9366   uint32_t insn, re_insn = 0;
9367   uint16_t insn16, re_insn16 = 0;
9368   unsigned long reloc, cond_reloc;
9369
9370   enum elf_nds32_reloc_type checked_types[] =
9371     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9372
9373   irelend = internal_relocs + sec->reloc_count;
9374   seq_len = GET_SEQ_LEN (irel->r_addend);
9375   laddr = irel->r_offset;
9376   *insn_len = seq_len;
9377   first_size = (seq_len == 6) ? 2 : 4;
9378
9379   i2_irelfn =
9380     find_relocs_at_address_addr (irel, internal_relocs,
9381                                  irelend, R_NDS32_25_PCREL_RELA,
9382                                  laddr + first_size);
9383
9384   for (i = 0; i < sizeof (checked_types) / sizeof(checked_types[0]); i++)
9385     {
9386       cond_irelfn =
9387         find_relocs_at_address_addr (irel, internal_relocs, irelend,
9388                                      checked_types[i], laddr);
9389       if (cond_irelfn != irelend)
9390         break;
9391     }
9392
9393   if (i2_irelfn == irelend || cond_irelfn == irelend)
9394     {
9395       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
9396                           irel->r_offset);
9397       return FALSE;
9398     }
9399
9400   /* Get the value of the symbol referred to by the reloc.  */
9401   foff =
9402     calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
9403                       &pic_ext_target);
9404   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9405       || foff >= CONSERVATIVE_16BIT_S1)
9406     return FALSE;
9407
9408   /* Get the all corresponding instructions.  */
9409   if (first_size == 4)
9410     {
9411       insn = bfd_getb32 (contents + laddr);
9412       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9413     }
9414   else
9415     {
9416       insn16 = bfd_getb16 (contents + laddr);
9417       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9418     }
9419
9420   if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
9421       && foff < ACCURATE_8BIT_S1 - first_size)
9422     {
9423       if (first_size == 4)
9424         {
9425           /* Don't convert it to 16-bit now, keep this as relaxable for
9426              ``label reloc; INSN16''.  */
9427
9428           /* Save comp_insn32 to buffer.  */
9429           bfd_putb32 (re_insn, contents + irel->r_offset);
9430           *insn_len = 4;
9431           reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9432             R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9433           cond_reloc = R_NDS32_INSN16;
9434         }
9435       else
9436         {
9437           bfd_putb16 (re_insn16, contents + irel->r_offset);
9438           *insn_len = 2;
9439           reloc = R_NDS32_9_PCREL_RELA;
9440           cond_reloc = R_NDS32_NONE;
9441         }
9442     }
9443   else if (N32_OP6 (re_insn) == N32_OP6_BR1
9444            && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9445                && foff < ACCURATE_14BIT_S1 - first_size))
9446     {
9447       /* beqs     label    ; 15_PCREL */
9448       bfd_putb32 (re_insn, contents + irel->r_offset);
9449       *insn_len = 4;
9450       reloc = R_NDS32_15_PCREL_RELA;
9451       cond_reloc = R_NDS32_NONE;
9452     }
9453   else if (N32_OP6 (re_insn) == N32_OP6_BR2
9454            && foff >= -CONSERVATIVE_16BIT_S1
9455            && foff < CONSERVATIVE_16BIT_S1)
9456     {
9457       /* beqz     label ; 17_PCREL */
9458       bfd_putb32 (re_insn, contents + irel->r_offset);
9459       *insn_len = 4;
9460       reloc = R_NDS32_17_PCREL_RELA;
9461       cond_reloc = R_NDS32_NONE;
9462     }
9463   else
9464     return FALSE;
9465
9466   /* Set all relocations.  */
9467   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
9468   irel->r_addend = i2_irelfn->r_addend;
9469
9470   cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9471                                       cond_reloc);
9472   cond_irelfn->r_addend = 0;
9473
9474   if ((seq_len ^ *insn_len ) & 0x2)
9475     {
9476       insn16 = NDS32_NOP16;
9477       bfd_putb16 (insn16, contents + irel->r_offset + 4);
9478       i2_irelfn->r_offset = 4;
9479       i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9480                                         R_NDS32_INSN16);
9481       i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9482       *insn_len += 2;
9483     }
9484   else
9485     i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9486                                       R_NDS32_NONE);
9487   return TRUE;
9488 }
9489
9490 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
9491
9492 static bfd_boolean
9493 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9494                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9495                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9496                            Elf_Internal_Shdr *symtab_hdr)
9497 {
9498   /* There are 5 variations for LONGJUMP3
9499      case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
9500      optimize off or optimize for space
9501      bnes38   rt, ra, $1            ; LONGJUMP3
9502      sethi    ta, hi20(symbol)      ; HI20
9503      ori      ta, ta, lo12(symbol)  ; LO12S0
9504      jr5      ta                    ;
9505      $1:                            ;
9506
9507      case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
9508      bnes38   rt, ra, $1           ; LONGJUMP3
9509      sethi    ta, hi20(symbol)     ; HI20
9510      ori      ta, ta, lo12(symbol) ; LO12S0
9511      jr5      ta                   ;
9512      $1:                           ; LABEL
9513
9514      case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
9515      optimize off or optimize for space
9516      bne   rt, ra, $1           ; LONGJUMP3
9517      sethi ta, hi20(symbol)     ; HI20
9518      ori   ta, ta, lo12(symbol) ; LO12S0
9519      jr5   ta                   ;
9520      $1:                        ;
9521
9522      case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
9523      16-bit off if no INSN16
9524      bne   rt, ra, $1           ; LONGJUMP3
9525      sethi ta, hi20(symbol)     ; HI20
9526      ori   ta, ta, lo12(symbol) ; LO12S0
9527      jr    ta                   ;
9528      $1:                        ;
9529
9530      case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
9531      16-bit off if no INSN16
9532      bne   rt, ra, $1           ; LONGJUMP3
9533      sethi ta, hi20(symbol)     ; HI20
9534      ori   ta, ta, lo12(symbol) ; LO12S0
9535      jr    ta                   ;
9536      $1:                        ; LABEL */
9537
9538   /* Get the reloc for the address from which the register is
9539      being loaded.  This reloc will tell us which function is
9540      actually being called.  */
9541   enum elf_nds32_reloc_type checked_types[] =
9542     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9543
9544   int reloc_off = 0, cond_removed = 0, convertible;
9545   bfd_vma laddr;
9546   int seq_len;  /* Original length of instruction sequence.  */
9547   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9548   int pic_ext_target = 0, first_size;
9549   unsigned int i;
9550   bfd_signed_vma foff;
9551   uint32_t insn, re_insn = 0;
9552   uint16_t insn16, re_insn16 = 0;
9553   unsigned long reloc, cond_reloc;
9554
9555   irelend = internal_relocs + sec->reloc_count;
9556   seq_len = GET_SEQ_LEN (irel->r_addend);
9557   laddr = irel->r_offset;
9558   *insn_len = seq_len;
9559
9560   convertible = IS_1ST_CONVERT (irel->r_addend);
9561
9562   if (convertible)
9563     first_size = 2;
9564   else
9565     first_size = 4;
9566
9567   /* Get all needed relocations.  */
9568   hi_irelfn =
9569     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9570                                  R_NDS32_HI20_RELA, laddr + first_size);
9571   lo_irelfn =
9572     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9573                                  R_NDS32_LO12S0_ORI_RELA,
9574                                  laddr + first_size + 4);
9575
9576   for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
9577     {
9578       cond_irelfn =
9579         find_relocs_at_address_addr (irel, internal_relocs, irelend,
9580                                      checked_types[i], laddr);
9581       if (cond_irelfn != irelend)
9582         break;
9583     }
9584
9585   if (hi_irelfn == irelend || lo_irelfn == irelend || cond_irelfn == irelend)
9586     {
9587       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
9588                           irel->r_offset);
9589       return FALSE;
9590     }
9591
9592   /* Get the value of the symbol referred to by the reloc.  */
9593   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9594                            &pic_ext_target);
9595
9596   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9597       || foff >= CONSERVATIVE_24BIT_S1)
9598     return FALSE;
9599
9600   /* Get the all corresponding instructions.  */
9601   if (first_size == 4)
9602     {
9603       insn = bfd_getb32 (contents + laddr);
9604       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9605     }
9606   else
9607     {
9608       insn16 = bfd_getb16 (contents + laddr);
9609       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9610     }
9611
9612   /* For simplicity of coding, we are going to modify the section
9613      contents, the section relocs, and the BFD symbol table.  We
9614      must tell the rest of the code not to free up this
9615      information.  It would be possible to instead create a table
9616      of changes which have to be made, as is done in coff-mips.c;
9617      that would be more work, but would require less memory when
9618      the linker is run.  */
9619
9620   if (re_insn16 && foff >= -ACCURATE_8BIT_S1 - first_size
9621       && foff < ACCURATE_8BIT_S1 - first_size)
9622     {
9623       if (!(seq_len & 0x2))
9624         {
9625           /* Don't convert it to 16-bit now, keep this as relaxable
9626              for ``label reloc; INSN1a''6.  */
9627           /* Save comp_insn32 to buffer.  */
9628           bfd_putb32 (re_insn, contents + irel->r_offset);
9629           *insn_len = 4;
9630           reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9631             R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9632           cond_reloc = R_NDS32_INSN16;
9633         }
9634       else
9635         {
9636           /* Not optimize for speed; convert sequence to 16-bit.  */
9637           /* Save comp_insn16 to buffer.  */
9638           bfd_putb16 (re_insn16, contents + irel->r_offset);
9639           *insn_len = 2;
9640           reloc = R_NDS32_9_PCREL_RELA;
9641           cond_reloc = R_NDS32_NONE;
9642         }
9643       cond_removed = 1;
9644     }
9645   else if (N32_OP6 (re_insn) == N32_OP6_BR1
9646            && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9647                && foff < ACCURATE_14BIT_S1 - first_size))
9648     {
9649       /* beqs     label    ; 15_PCREL */
9650       bfd_putb32 (re_insn, contents + irel->r_offset);
9651       *insn_len = 4;
9652       reloc = R_NDS32_15_PCREL_RELA;
9653       cond_reloc = R_NDS32_NONE;
9654       cond_removed = 1;
9655     }
9656   else if (N32_OP6 (re_insn) == N32_OP6_BR2
9657            && foff >= -CONSERVATIVE_16BIT_S1
9658            && foff < CONSERVATIVE_16BIT_S1)
9659     {
9660       /* beqz     label ; 17_PCREL */
9661       bfd_putb32 (re_insn, contents + irel->r_offset);
9662       *insn_len = 4;
9663       reloc = R_NDS32_17_PCREL_RELA;
9664       cond_reloc = R_NDS32_NONE;
9665       cond_removed = 1;
9666     }
9667   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
9668            && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
9669     {
9670       /* Relax to one of the following 3 variations
9671
9672          case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
9673          for space
9674          bnes38  rt, $1 ; LONGJUMP2
9675          j       label  ; 25_PCREL
9676          $1
9677
9678          case 4-4; 1st insn not convertible, others don't care
9679          bne   rt, ra, $1 ; LONGJUMP2
9680          j     label      ; 25_PCREL
9681          $1
9682
9683          case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9684          bne   rt, ra, $1 ; LONGJUMP2
9685          j     label      ; 25_PCREL
9686          $1 */
9687
9688       /* Offset for first instruction.  */
9689
9690       /* Use j label as second instruction.  */
9691       *insn_len = 4 + first_size;
9692       insn = INSN_J;
9693       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9694       reloc = R_NDS32_LONGJUMP2;
9695       cond_reloc = R_NDS32_25_PLTREL;
9696     }
9697     else
9698       return FALSE;
9699
9700     if (cond_removed == 1)
9701       {
9702         /* Set all relocations.  */
9703         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9704         irel->r_addend = hi_irelfn->r_addend;
9705
9706         cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9707                                             cond_reloc);
9708         cond_irelfn->r_addend = 0;
9709         hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9710                                           R_NDS32_NONE);
9711       }
9712     else
9713       {
9714         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
9715         irel->r_addend = irel->r_addend;
9716         hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9717                                           cond_reloc);
9718       }
9719
9720   if ((seq_len ^ *insn_len ) & 0x2)
9721     {
9722       insn16 = NDS32_NOP16;
9723       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9724       lo_irelfn->r_offset = *insn_len;
9725       lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9726                                         R_NDS32_INSN16);
9727       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9728       *insn_len += 2;
9729     }
9730   else
9731     lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9732                                       R_NDS32_NONE);
9733   return TRUE;
9734 }
9735
9736 /* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
9737
9738 static bfd_boolean
9739 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9740                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9741                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9742                            Elf_Internal_Shdr *symtab_hdr)
9743 {
9744   /* The pattern for LONGCALL4.  Support for function cse.
9745      sethi ta, hi20(symbol)     ; LONGCALL4/HI20
9746      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
9747      jral  ta                   ; PTR_RES/EMPTY/INSN16  */
9748
9749   bfd_vma laddr;
9750   uint32_t insn;
9751   Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
9752   Elf_Internal_Rela *irelend;
9753   int pic_ext_target = 0;
9754   bfd_signed_vma foff;
9755
9756   irelend = internal_relocs + sec->reloc_count;
9757   laddr = irel->r_offset;
9758
9759   /* Get the reloc for the address from which the register is
9760      being loaded.  This reloc will tell us which function is
9761      actually being called.  */
9762   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9763                                          R_NDS32_HI20_RELA, laddr);
9764
9765   if (hi_irel == irelend)
9766     {
9767       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9768                           irel->r_offset);
9769       return FALSE;
9770     }
9771
9772   /* Get the value of the symbol referred to by the reloc.  */
9773   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
9774                            &pic_ext_target);
9775
9776   /* This condition only happened when symbol is undefined.  */
9777   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9778       || foff >= CONSERVATIVE_24BIT_S1)
9779     return FALSE;
9780
9781   /* Relax to: jal symbol; 25_PCREL */
9782   /* For simplicity of coding, we are going to modify the section
9783      contents, the section relocs, and the BFD symbol table.  We
9784      must tell the rest of the code not to free up this
9785      information.  It would be possible to instead create a table
9786      of changes which have to be made, as is done in coff-mips.c;
9787      that would be more work, but would require less memory when
9788      the linker is run.  */
9789
9790   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9791                                           R_NDS32_PTR_RESOLVED, irel->r_addend);
9792   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9793                                           R_NDS32_EMPTY, irel->r_addend);
9794
9795   if (ptr_irel == irelend || em_irel == irelend)
9796     {
9797       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
9798                           irel->r_offset);
9799       return FALSE;
9800     }
9801   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
9802   insn = bfd_getb32 (contents + irel->r_addend);
9803   if (insn & 0x80000000)
9804     return FALSE;
9805
9806   /* Replace the long call with a jal.  */
9807   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
9808                                   R_NDS32_25_PCREL_RELA);
9809   ptr_irel->r_addend = 1;
9810
9811   /* We don't resolve this here but resolve it in relocate_section.  */
9812   insn = INSN_JAL;
9813   bfd_putb32 (insn, contents + em_irel->r_offset);
9814
9815   irel->r_info =
9816     ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9817
9818   /* If there is function cse, HI20 can not remove now.  */
9819   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9820                                            R_NDS32_LONGCALL4, laddr);
9821   if (call_irel == irelend)
9822     {
9823       *insn_len = 0;
9824       hi_irel->r_info =
9825         ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
9826     }
9827
9828   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9829                                           R_NDS32_INSN16, irel->r_addend);
9830   if (insn_irel != irelend)
9831     insn_irel->r_info =
9832       ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9833
9834   return TRUE;
9835 }
9836
9837 /* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
9838
9839 static bfd_boolean
9840 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9841                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9842                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9843                            Elf_Internal_Shdr *symtab_hdr)
9844 {
9845   /* The pattern for LONGCALL5.
9846      bltz  rt, .L1      ; LONGCALL5/17_PCREL
9847      jal   symbol       ; 25_PCREL
9848      .L1:  */
9849
9850   bfd_vma laddr;
9851   uint32_t insn;
9852   Elf_Internal_Rela *cond_irel, *irelend;
9853   int pic_ext_target = 0;
9854   bfd_signed_vma foff;
9855
9856   irelend = internal_relocs + sec->reloc_count;
9857   laddr = irel->r_offset;
9858   insn = bfd_getb32 (contents + laddr);
9859
9860   /* Get the reloc for the address from which the register is
9861      being loaded.  This reloc will tell us which function is
9862      actually being called.  */
9863   cond_irel =
9864     find_relocs_at_address_addr (irel, internal_relocs, irelend,
9865                                  R_NDS32_25_PCREL_RELA, irel->r_addend);
9866   if (cond_irel == irelend)
9867     {
9868       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
9869                           irel->r_offset);
9870       return FALSE;
9871     }
9872
9873   /* Get the value of the symbol referred to by the reloc.  */
9874   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
9875                            &pic_ext_target);
9876
9877   if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9878       || foff >= CONSERVATIVE_16BIT_S1)
9879     return FALSE;
9880
9881   /* Relax to   bgezal   rt, label ; 17_PCREL
9882      or         bltzal   rt, label ; 17_PCREL */
9883
9884   /* Convert to complimentary conditional call.  */
9885   insn = CONVERT_CONDITION_CALL (insn);
9886
9887   /* For simplicity of coding, we are going to modify the section
9888      contents, the section relocs, and the BFD symbol table.  We
9889      must tell the rest of the code not to free up this
9890      information.  It would be possible to instead create a table
9891      of changes which have to be made, as is done in coff-mips.c;
9892      that would be more work, but would require less memory when
9893      the linker is run.  */
9894
9895   /* Modify relocation and contents.  */
9896   cond_irel->r_info =
9897     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
9898
9899   /* Replace the long call with a bgezal.  */
9900   bfd_putb32 (insn, contents + cond_irel->r_offset);
9901   *insn_len = 0;
9902
9903   /* Clean unnessary relocations.  */
9904   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9905
9906   cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9907                                            R_NDS32_17_PCREL_RELA, laddr);
9908   cond_irel->r_info =
9909     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
9910
9911   return TRUE;
9912 }
9913
9914 /* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
9915
9916 static bfd_boolean
9917 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9918                            Elf_Internal_Rela *internal_relocs, int *insn_len,
9919                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9920                            Elf_Internal_Shdr *symtab_hdr)
9921 {
9922   /* The pattern for LONGCALL6.
9923      bltz  rt,   .L1                    ; LONGCALL6/17_PCREL
9924      sethi ta,   hi20(symbol)           ; HI20/PTR
9925      ori   ta, ta,  lo12(symbol)        ; LO12S0_ORI/PTR
9926      jral  ta                           ; PTR_RES/EMPTY/INSN16
9927      .L1  */
9928
9929   bfd_vma laddr;
9930   uint32_t insn;
9931   Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
9932   int pic_ext_target = 0;
9933   bfd_signed_vma foff;
9934
9935   irelend = internal_relocs + sec->reloc_count;
9936   laddr = irel->r_offset;
9937
9938   /* Get the reloc for the address from which the register is
9939      being loaded.  This reloc will tell us which function is
9940      actually being called.  */
9941   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9942                                          R_NDS32_EMPTY, irel->r_addend);
9943
9944   if (em_irel == irelend)
9945     {
9946       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
9947                           irel->r_offset);
9948       return FALSE;
9949     }
9950
9951   /* Get the value of the symbol referred to by the reloc.  */
9952   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
9953                            &pic_ext_target);
9954
9955   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9956       || foff >= CONSERVATIVE_24BIT_S1)
9957     return FALSE;
9958
9959   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
9960   insn = bfd_getb32 (contents + irel->r_addend);
9961   if (insn & 0x80000000)
9962     return FALSE;
9963
9964   insn = bfd_getb32 (contents + laddr);
9965   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9966     {
9967       /* Relax to  bgezal   rt, label ; 17_PCREL
9968          or        bltzal   rt, label ; 17_PCREL */
9969
9970       /* Convert to complimentary conditional call.  */
9971       *insn_len = 0;
9972       insn = CONVERT_CONDITION_CALL (insn);
9973       bfd_putb32 (insn, contents + em_irel->r_offset);
9974
9975       em_irel->r_info =
9976         ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
9977
9978       /* Set resolved relocation.  */
9979       cond_irel =
9980         find_relocs_at_address_addr (irel, internal_relocs, irelend,
9981                                      R_NDS32_PTR_RESOLVED, irel->r_addend);
9982       if (cond_irel == irelend)
9983         {
9984           _bfd_error_handler (unrecognized_reloc_msg, abfd,
9985                               "R_NDS32_LONGCALL6", irel->r_offset);
9986           return FALSE;
9987         }
9988       cond_irel->r_addend = 1;
9989
9990       /* Clear relocations.  */
9991
9992       irel->r_info =
9993         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9994
9995       cond_irel =
9996         find_relocs_at_address_addr (irel, internal_relocs, irelend,
9997                                      R_NDS32_17_PCREL_RELA, laddr);
9998       if (cond_irel != irelend)
9999         cond_irel->r_info =
10000           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10001
10002       cond_irel =
10003         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10004                                      R_NDS32_INSN16, irel->r_addend);
10005       if (cond_irel != irelend)
10006         cond_irel->r_info =
10007           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10008
10009     }
10010   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10011     {
10012       /* Relax to the following instruction sequence
10013          bltz  rt, .L1  ; LONGCALL2/17_PCREL
10014          jal   symbol   ; 25_PCREL/PTR_RES
10015          .L1  */
10016       *insn_len = 4;
10017       /* Convert instruction.  */
10018       insn = INSN_JAL;
10019       bfd_putb32 (insn, contents + em_irel->r_offset);
10020
10021       /* Convert relocations.  */
10022       em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10023                                       R_NDS32_25_PCREL_RELA);
10024       irel->r_info =
10025         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10026
10027       /* Set resolved relocation.  */
10028       cond_irel =
10029         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10030                                      R_NDS32_PTR_RESOLVED, irel->r_addend);
10031       if (cond_irel == irelend)
10032         {
10033           _bfd_error_handler (unrecognized_reloc_msg, abfd,
10034                               "R_NDS32_LONGCALL6", irel->r_offset);
10035           return FALSE;
10036         }
10037       cond_irel->r_addend = 1;
10038
10039       cond_irel =
10040         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10041                                      R_NDS32_INSN16, irel->r_addend);
10042       if (cond_irel != irelend)
10043         cond_irel->r_info =
10044           ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10045     }
10046   return TRUE;
10047 }
10048
10049 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10050
10051 static bfd_boolean
10052 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10053                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10054                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10055                            Elf_Internal_Shdr *symtab_hdr)
10056 {
10057   /* The pattern for LONGJUMP4.
10058      sethi ta, hi20(symbol)     ; LONGJUMP4/HI20
10059      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10060      jr    ta                   ; PTR_RES/INSN16/EMPTY  */
10061
10062   bfd_vma laddr;
10063   int seq_len;  /* Original length of instruction sequence.  */
10064   uint32_t insn;
10065   Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10066   int pic_ext_target = 0;
10067   bfd_signed_vma foff;
10068
10069   irelend = internal_relocs + sec->reloc_count;
10070   seq_len = GET_SEQ_LEN (irel->r_addend);
10071   laddr = irel->r_offset;
10072   *insn_len = seq_len;
10073
10074   /* Get the reloc for the address from which the register is
10075      being loaded.  This reloc will tell us which function is
10076      actually being called.  */
10077
10078   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10079                                          R_NDS32_HI20_RELA, laddr);
10080
10081   if (hi_irel == irelend)
10082     {
10083       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10084                           irel->r_offset);
10085       return FALSE;
10086     }
10087
10088   /* Get the value of the symbol referred to by the reloc.  */
10089   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
10090                            &pic_ext_target);
10091
10092   if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
10093       || foff < -CONSERVATIVE_24BIT_S1)
10094     return FALSE;
10095
10096   /* Convert it to "j label", it may be converted to j8 in the final
10097      pass of relaxation.  Therefore, we do not consider this currently.  */
10098   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10099                                           R_NDS32_PTR_RESOLVED, irel->r_addend);
10100   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10101                                          R_NDS32_EMPTY, irel->r_addend);
10102
10103   if (ptr_irel == irelend || em_irel == irelend)
10104     {
10105       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10106                           irel->r_offset);
10107       return FALSE;
10108     }
10109
10110   em_irel->r_info =
10111     ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10112   ptr_irel->r_addend = 1;
10113
10114   /* Write instruction.  */
10115   insn = INSN_J;
10116   bfd_putb32 (insn, contents + em_irel->r_offset);
10117
10118   /* Clear relocations.  */
10119   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10120
10121   /* If there is function cse, HI20 can not remove now.  */
10122   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10123                                            R_NDS32_LONGJUMP4, laddr);
10124   if (call_irel == irelend)
10125     {
10126       *insn_len = 0;
10127       hi_irel->r_info =
10128         ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10129     }
10130
10131   return TRUE;
10132 }
10133
10134 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
10135
10136 static bfd_boolean
10137 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10138                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10139                            int *seq_len, bfd_byte *contents,
10140                            Elf_Internal_Sym *isymbuf,
10141                            Elf_Internal_Shdr *symtab_hdr)
10142 {
10143   /* There are 2 variations for LONGJUMP5
10144      case 2-4;  1st insn convertible, 16-bit on.
10145      bnes38  rt, ra, .L1        ; LONGJUMP5/9_PCREL/INSN16
10146      j       label              ; 25_PCREL/INSN16
10147      $1:
10148
10149      case 4-4; 1st insn not convertible
10150      bne  rt, ra, .L1   ; LONGJUMP5/15_PCREL/INSN16
10151      j    label         ; 25_PCREL/INSN16
10152      .L1:  */
10153
10154   bfd_vma laddr;
10155   Elf_Internal_Rela *cond_irel,  *irelend;
10156   int pic_ext_target = 0;
10157   unsigned int i;
10158   bfd_signed_vma foff;
10159   uint32_t insn, re_insn = 0;
10160   uint16_t insn16, re_insn16 = 0;
10161   unsigned long reloc;
10162
10163   enum elf_nds32_reloc_type checked_types[] =
10164     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10165       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10166
10167   irelend = internal_relocs + sec->reloc_count;
10168   laddr = irel->r_offset;
10169
10170   /* Get the reloc for the address from which the register is
10171      being loaded.  This reloc will tell us which function is
10172      actually being called.  */
10173
10174   cond_irel =
10175     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10176                                  R_NDS32_25_PCREL_RELA, irel->r_addend);
10177   if (cond_irel == irelend)
10178     {
10179       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10180                           irel->r_offset);
10181       return FALSE;
10182     }
10183
10184   /* Get the value of the symbol referred to by the reloc.  */
10185   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10186                            &pic_ext_target);
10187
10188   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
10189       || foff >= CONSERVATIVE_16BIT_S1)
10190     return FALSE;
10191
10192   /* Get the all corresponding instructions.  */
10193   insn = bfd_getb32 (contents + laddr);
10194   /* Check instruction size.  */
10195   if (insn & 0x80000000)
10196     {
10197       *seq_len = 0;
10198       insn16 = insn >> 16;
10199       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10200     }
10201   else
10202     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10203
10204   if (N32_OP6 (re_insn) == N32_OP6_BR1
10205       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10206     {
10207       /* beqs label ; 15_PCREL.  */
10208       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10209       reloc = R_NDS32_15_PCREL_RELA;
10210     }
10211   else if (N32_OP6 (re_insn) == N32_OP6_BR2
10212            && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10213     {
10214       /* beqz label ; 17_PCREL.  */
10215       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10216       reloc = R_NDS32_17_PCREL_RELA;
10217     }
10218   else if ( N32_OP6 (re_insn) == N32_OP6_BR3
10219            && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
10220     {
10221       /* beqc label ; 9_PCREL.  */
10222       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10223       reloc = R_NDS32_WORD_9_PCREL_RELA;
10224     }
10225   else
10226     return FALSE;
10227
10228   /* Set all relocations.  */
10229   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
10230
10231   /* Clean relocations.  */
10232   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10233   for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10234     {
10235       cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10236                                                checked_types[i], laddr);
10237       if (cond_irel != irelend)
10238         {
10239           if (*seq_len == 0
10240               && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10241             {
10242               /* If the branch instruction is 2 byte, it cannot remove
10243                  directly.  Only convert it to nop16 and remove it after
10244                  checking alignment issue.  */
10245               insn16 = NDS32_NOP16;
10246               bfd_putb16 (insn16, contents + laddr);
10247               cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10248             }
10249           else
10250             cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10251                                               R_NDS32_NONE);
10252         }
10253     }
10254   *insn_len = 0;
10255
10256   return TRUE;
10257 }
10258
10259 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
10260
10261 static bfd_boolean
10262 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10263                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10264                            int *seq_len, bfd_byte *contents,
10265                            Elf_Internal_Sym *isymbuf,
10266                            Elf_Internal_Shdr *symtab_hdr)
10267 {
10268   /* There are 5 variations for LONGJUMP6
10269      case : 2-4-4-4; 1st insn convertible, 16-bit on.
10270      bnes38   rt, ra, .L1               ; LONGJUMP6/15_PCREL/INSN16
10271      sethi    ta, hi20(symbol)          ; HI20/PTR
10272      ori      ta, ta, lo12(symbol)      ; LO12S0_ORI/PTR
10273      jr       ta                        ; PTR_RES/INSN16/EMPTY
10274      .L1:
10275
10276      case : 4-4-4-4; 1st insn not convertible, 16-bit on.
10277      bne   rt, ra, .L1          ; LONGJUMP6/15_PCREL/INSN16
10278      sethi ta, hi20(symbol)     ; HI20/PTR
10279      ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10280      jr    ta                   ; PTR_RES/INSN16/EMPTY
10281      .L1:  */
10282
10283   enum elf_nds32_reloc_type checked_types[] =
10284     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10285       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10286
10287   int reloc_off = 0, cond_removed = 0;
10288   bfd_vma laddr;
10289   Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
10290   int pic_ext_target = 0;
10291   unsigned int i;
10292   bfd_signed_vma foff;
10293   uint32_t insn, re_insn = 0;
10294   uint16_t insn16, re_insn16 = 0;
10295   unsigned long reloc;
10296
10297   irelend = internal_relocs + sec->reloc_count;
10298   laddr = irel->r_offset;
10299
10300   /* Get the reloc for the address from which the register is
10301      being loaded.  This reloc will tell us which function is
10302      actually being called.  */
10303   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10304                                          R_NDS32_EMPTY, irel->r_addend);
10305
10306   if (em_irel == irelend)
10307     {
10308       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
10309                           irel->r_offset);
10310       return FALSE;
10311     }
10312
10313   /* Get the value of the symbol referred to by the reloc.  */
10314   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
10315                            &pic_ext_target);
10316
10317   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
10318       || foff >= CONSERVATIVE_24BIT_S1)
10319     return FALSE;
10320
10321   insn = bfd_getb32 (contents + laddr);
10322   /* Check instruction size.  */
10323   if (insn & 0x80000000)
10324     {
10325       *seq_len = 0;
10326       insn16 = insn >> 16;
10327       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10328     }
10329   else
10330     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10331
10332   /* For simplicity of coding, we are going to modify the section
10333      contents, the section relocs, and the BFD symbol table.  We
10334      must tell the rest of the code not to free up this
10335      information.  It would be possible to instead create a table
10336      of changes which have to be made, as is done in coff-mips.c;
10337      that would be more work, but would require less memory when
10338      the linker is run.  */
10339
10340   if (N32_OP6 (re_insn) == N32_OP6_BR1
10341       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10342     {
10343       /* beqs     label    ; 15_PCREL */
10344       bfd_putb32 (re_insn, contents + em_irel->r_offset);
10345       reloc = R_NDS32_15_PCREL_RELA;
10346       cond_removed = 1;
10347     }
10348   else if (N32_OP6 (re_insn) == N32_OP6_BR2
10349            && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10350     {
10351       /* beqz     label ; 17_PCREL */
10352       bfd_putb32 (re_insn, contents + em_irel->r_offset);
10353       reloc = R_NDS32_17_PCREL_RELA;
10354       cond_removed = 1;
10355     }
10356   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10357            && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10358     {
10359       /* Relax to one of the following 2 variations
10360
10361          case 2-4;  1st insn convertible, 16-bit on.
10362          bnes38  rt, ra, .L1    ; LONGJUMP5/9_PCREL/INSN16
10363          j       label          ; 25_PCREL/INSN16
10364          $1:
10365
10366          case 4-4; 1st insn not convertible
10367          bne  rt, ra, .L1       ; LONGJUMP5/15_PCREL/INSN16
10368          j    label             ; 25_PCREL/INSN16
10369          .L1:  */
10370
10371       /* Use j label as second instruction.  */
10372       insn = INSN_J;
10373       reloc = R_NDS32_25_PCREL_RELA;
10374       bfd_putb32 (insn, contents + em_irel->r_offset);
10375     }
10376   else
10377     return FALSE;
10378
10379   /* Set all relocations.  */
10380   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
10381
10382   cond_irel =
10383     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10384                                  R_NDS32_PTR_RESOLVED, em_irel->r_offset);
10385   cond_irel->r_addend = 1;
10386
10387   /* Use INSN16 of first branch instruction to distinguish if keeping
10388      INSN16 of final instruction or not.  */
10389   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10390                                            R_NDS32_INSN16, irel->r_offset);
10391   if (insn_irel == irelend)
10392     {
10393       /* Clean the final INSN16.  */
10394       insn_irel =
10395         find_relocs_at_address_addr (irel, internal_relocs, irelend,
10396                                      R_NDS32_INSN16, em_irel->r_offset);
10397       insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10398                                         R_NDS32_NONE);
10399     }
10400
10401   if (cond_removed == 1)
10402     {
10403       *insn_len = 0;
10404
10405       /* Clear relocations.  */
10406       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10407
10408       for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10409         {
10410           cond_irel =
10411             find_relocs_at_address_addr (irel, internal_relocs, irelend,
10412                                          checked_types[i], laddr);
10413           if (cond_irel != irelend)
10414             {
10415               if (*seq_len == 0
10416                   && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10417                 {
10418                   /* If the branch instruction is 2 byte, it cannot remove
10419                      directly.  Only convert it to nop16 and remove it after
10420                      checking alignment issue.  */
10421                   insn16 = NDS32_NOP16;
10422                   bfd_putb16 (insn16, contents + laddr);
10423                   cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10424                 }
10425               else
10426                 cond_irel->r_info =
10427                   ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10428             }
10429         }
10430     }
10431   else
10432     {
10433       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10434                                    R_NDS32_LONGJUMP5);
10435     }
10436
10437   return TRUE;
10438 }
10439
10440 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
10441
10442 static bfd_boolean
10443 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10444                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10445                            int *seq_len, bfd_byte *contents,
10446                            Elf_Internal_Sym *isymbuf,
10447                            Elf_Internal_Shdr *symtab_hdr)
10448 {
10449   /* There are 2 variations for LONGJUMP5
10450      case 2-4;  1st insn convertible, 16-bit on.
10451      movi55  ta, imm11          ; LONGJUMP7/INSN16
10452      beq     rt, ta, label      ; 15_PCREL
10453
10454      case 4-4; 1st insn not convertible
10455      movi55  ta, imm11          ; LONGJUMP7/INSN16
10456      beq     rt, ta, label      ; 15_PCREL  */
10457
10458   bfd_vma laddr;
10459   Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
10460   int pic_ext_target = 0;
10461   bfd_signed_vma foff;
10462   uint32_t insn, re_insn = 0;
10463   uint16_t insn16;
10464   uint32_t imm11;
10465
10466   irelend = internal_relocs + sec->reloc_count;
10467   laddr = irel->r_offset;
10468
10469   /* Get the reloc for the address from which the register is
10470      being loaded.  This reloc will tell us which function is
10471      actually being called.  */
10472
10473   cond_irel =
10474     find_relocs_at_address_addr (irel, internal_relocs, irelend,
10475                                  R_NDS32_15_PCREL_RELA, irel->r_addend);
10476   if (cond_irel == irelend)
10477     {
10478       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
10479                           irel->r_offset);
10480       return FALSE;
10481     }
10482
10483   /* Get the value of the symbol referred to by the reloc.  */
10484   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10485                            &pic_ext_target);
10486
10487   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_8BIT_S1
10488       || foff >= CONSERVATIVE_8BIT_S1)
10489     return FALSE;
10490
10491   /* Get the first instruction for its size.  */
10492   insn = bfd_getb32 (contents + laddr);
10493   if (insn & 0x80000000)
10494     {
10495       *seq_len = 0;
10496       /* Get the immediate from movi55.  */
10497       imm11 = N16_IMM5S (insn >> 16);
10498     }
10499   else
10500     {
10501       /* Get the immediate from movi.  */
10502       imm11 = N32_IMM20S (insn);
10503     }
10504
10505   /* Get the branch instruction.  */
10506   insn = bfd_getb32 (contents + irel->r_addend);
10507   /* Convert instruction to BR3.  */
10508   if ((insn >> 14) & 0x1)
10509     re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
10510   else
10511     re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
10512
10513   bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10514
10515   /* Set all relocations.  */
10516   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10517                                     R_NDS32_WORD_9_PCREL_RELA);
10518
10519   /* Clean relocations.  */
10520   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10521   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10522                                            R_NDS32_INSN16, irel->r_offset);
10523   if (insn_irel != irelend)
10524     {
10525       if (*seq_len == 0)
10526         {
10527           /* If the first insntruction is 16bit, convert it to nop16.  */
10528           insn16 = NDS32_NOP16;
10529           bfd_putb16 (insn16, contents + laddr);
10530           insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10531         }
10532       else
10533         cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10534                                           R_NDS32_NONE);
10535     }
10536   *insn_len = 0;
10537
10538   return TRUE;
10539 }
10540
10541 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
10542
10543 /* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
10544
10545 static bfd_boolean
10546 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
10547                            asection *sec, Elf_Internal_Rela *irel,
10548                            Elf_Internal_Rela *internal_relocs, int *insn_len,
10549                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10550                            Elf_Internal_Shdr *symtab_hdr, int load_store_relax)
10551 {
10552   int eliminate_sethi = 0, range_type;
10553   unsigned int i;
10554   bfd_vma local_sda, laddr;
10555   int seq_len;  /* Original length of instruction sequence.  */
10556   uint32_t insn;
10557   Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
10558   bfd_vma access_addr = 0;
10559   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
10560   enum elf_nds32_reloc_type checked_types[] =
10561     { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
10562       R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
10563       R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
10564       R_NDS32_TLS_LE_HI20
10565     };
10566
10567   irelend = internal_relocs + sec->reloc_count;
10568   seq_len = GET_SEQ_LEN (irel->r_addend);
10569   laddr = irel->r_offset;
10570   *insn_len = seq_len;
10571
10572   /* Get the high part relocation.  */
10573   for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10574     {
10575       hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10576                                                checked_types[i], laddr);
10577       if (hi_irelfn != irelend)
10578         break;
10579     }
10580
10581   if (hi_irelfn == irelend)
10582     {
10583       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
10584                           irel->r_offset);
10585         return FALSE;
10586     }
10587
10588   range_type = GET_LOADSTORE_RANGE (irel->r_addend);
10589   nds32_elf_final_sda_base (sec->output_section->owner,
10590                             link_info, &local_sda, FALSE);
10591
10592   switch (ELF32_R_TYPE (hi_irelfn->r_info))
10593     {
10594     case R_NDS32_HI20_RELA:
10595       insn = bfd_getb32 (contents + laddr);
10596       access_addr =
10597         calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10598
10599       if (range_type == NDS32_LOADSTORE_IMM)
10600         {
10601           struct elf_link_hash_entry *h = NULL;
10602           int indx;
10603
10604           if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
10605             {
10606               indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
10607               h = elf_sym_hashes (abfd)[indx];
10608             }
10609
10610           if ((access_addr < CONSERVATIVE_20BIT)
10611               && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10612             {
10613               eliminate_sethi = 1;
10614               break;
10615             }
10616
10617           /* This is avoid to relax symbol address which is fixed
10618              relocations.  Ex: _stack.  */
10619           if (h && bfd_is_abs_section (h->root.u.def.section))
10620             return FALSE;
10621         }
10622
10623       if (!load_store_relax)
10624         return FALSE;
10625
10626       /* Case for set gp register.  */
10627       if (N32_RT5 (insn) == REG_GP)
10628         break;
10629
10630       if (range_type == NDS32_LOADSTORE_FLOAT_S
10631           || range_type == NDS32_LOADSTORE_FLOAT_S)
10632         {
10633           range_l = sdata_range[0][0];
10634           range_h = sdata_range[0][1];
10635         }
10636       else
10637         {
10638           range_l = sdata_range[1][0];
10639           range_h = sdata_range[1][1];
10640         }
10641       break;
10642
10643     case R_NDS32_GOT_HI20:
10644       access_addr =
10645         calculate_got_memory_address (abfd, link_info, hi_irelfn, symtab_hdr);
10646
10647       /* If this symbol is not in .got, the return value will be -1.
10648          Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
10649          a negative offset is allowed.  */
10650       if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10651           && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10652         eliminate_sethi = 1;
10653       break;
10654
10655     case R_NDS32_PLT_GOTREL_HI20:
10656       access_addr = calculate_plt_memory_address (abfd, link_info, isymbuf,
10657                                                   hi_irelfn, symtab_hdr);
10658
10659       if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10660           && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10661         eliminate_sethi = 1;
10662       break;
10663
10664     case R_NDS32_GOTOFF_HI20:
10665       access_addr =
10666         calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10667
10668       if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10669           && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10670         eliminate_sethi = 1;
10671       break;
10672
10673     case R_NDS32_GOTPC_HI20:
10674       /* The access_addr must consider r_addend of hi_irel.  */
10675       access_addr = sec->output_section->vma + sec->output_offset
10676         + irel->r_offset + hi_irelfn->r_addend;
10677
10678       if ((bfd_signed_vma) (local_sda - access_addr) < CONSERVATIVE_20BIT
10679           && (bfd_signed_vma) (local_sda - access_addr) >= -CONSERVATIVE_20BIT)
10680         eliminate_sethi = 1;
10681       break;
10682
10683     case R_NDS32_TLS_LE_HI20:
10684       access_addr =
10685         calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10686       BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10687       access_addr -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10688       if ((range_type == NDS32_LOADSTORE_IMM)
10689           && (bfd_signed_vma) (access_addr) < CONSERVATIVE_20BIT
10690           && (bfd_signed_vma) (access_addr) >= -CONSERVATIVE_20BIT)
10691         eliminate_sethi = 1;
10692       break;
10693
10694     default:
10695       return FALSE;
10696     }
10697
10698   /* Delete sethi instruction.  */
10699   if (eliminate_sethi == 1
10700       || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
10701       || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10702     {
10703       hi_irelfn->r_info =
10704         ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
10705       irel->r_info =
10706         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10707       *insn_len = 0;
10708     }
10709   return TRUE;
10710 }
10711
10712 /* Relax LO12 relocation for nds32_elf_relax_section.  */
10713
10714 static void
10715 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
10716                       asection *sec, Elf_Internal_Rela *irel,
10717                       Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
10718                       Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
10719 {
10720   uint32_t insn;
10721   bfd_vma local_sda, laddr;
10722   unsigned long reloc;
10723   bfd_vma access_addr;
10724   bfd_vma range_l = 0, range_h = 0;     /* Upper/lower bound.  */
10725   Elf_Internal_Rela *irelfn = NULL, *irelend;
10726   struct elf_link_hash_entry *h = NULL;
10727   int indx;
10728
10729   /* For SDA base relative relaxation.  */
10730   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10731                             &local_sda, FALSE);
10732
10733   irelend = internal_relocs + sec->reloc_count;
10734   laddr = irel->r_offset;
10735   insn = bfd_getb32 (contents + laddr);
10736
10737   if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
10738     return;
10739
10740   access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10741
10742   if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
10743     {
10744       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
10745       h = elf_sym_hashes (abfd)[indx];
10746     }
10747
10748   if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
10749       && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10750     {
10751       reloc = R_NDS32_20_RELA;
10752       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10753       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10754       bfd_putb32 (insn, contents + laddr);
10755     }
10756   /* This is avoid to relax symbol address which is fixed
10757      relocations.  Ex: _stack.  */
10758   else if (N32_OP6 (insn) == N32_OP6_ORI
10759            && h && bfd_is_abs_section (h->root.u.def.section))
10760     return;
10761   else
10762     {
10763       range_l = sdata_range[1][0];
10764       range_h = sdata_range[1][1];
10765       switch (ELF32_R_TYPE (irel->r_info))
10766         {
10767         case R_NDS32_LO12S0_RELA:
10768           reloc = R_NDS32_SDA19S0_RELA;
10769           break;
10770         case R_NDS32_LO12S1_RELA:
10771           reloc = R_NDS32_SDA18S1_RELA;
10772           break;
10773         case R_NDS32_LO12S2_RELA:
10774           reloc = R_NDS32_SDA17S2_RELA;
10775           break;
10776         case R_NDS32_LO12S2_DP_RELA:
10777           range_l = sdata_range[0][0];
10778           range_h = sdata_range[0][1];
10779           reloc = R_NDS32_SDA12S2_DP_RELA;
10780           break;
10781         case R_NDS32_LO12S2_SP_RELA:
10782           range_l = sdata_range[0][0];
10783           range_h = sdata_range[0][1];
10784           reloc = R_NDS32_SDA12S2_SP_RELA;
10785           break;
10786         default:
10787           return;
10788         }
10789
10790       /* There are range_h and range_l because linker has to promise
10791          all sections move cross one page together.  */
10792       if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
10793           || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10794         {
10795           if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
10796             {
10797               /* Maybe we should add R_NDS32_INSN16 reloc type here
10798                  or manually do some optimization.  sethi can't be
10799                  eliminated when updating $gp so the relative ori
10800                  needs to be preserved.  */
10801               return;
10802             }
10803           if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
10804                                         &insn))
10805             return;
10806           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10807           bfd_putb32 (insn, contents + laddr);
10808
10809           irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10810                                            R_NDS32_INSN16);
10811           /* SDA17 must keep INSN16 for converting fp_as_gp.  */
10812           if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
10813             irelfn->r_info =
10814               ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
10815
10816         }
10817     }
10818   return;
10819 }
10820
10821 /* Relax low part of PIC instruction pattern.  */
10822
10823 static void
10824 nds32_elf_relax_piclo12 (struct bfd_link_info *link_info, bfd *abfd,
10825                          asection *sec, Elf_Internal_Rela *irel,
10826                          bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10827                          Elf_Internal_Shdr *symtab_hdr)
10828 {
10829   uint32_t insn;
10830   bfd_vma local_sda, laddr;
10831   bfd_signed_vma foff;
10832   unsigned long reloc;
10833
10834   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10835                             &local_sda, FALSE);
10836   laddr = irel->r_offset;
10837   insn = bfd_getb32 (contents + laddr);
10838
10839   if (N32_OP6 (insn) != N32_OP6_ORI)
10840     return;
10841
10842   if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
10843     {
10844       foff = calculate_got_memory_address (abfd, link_info, irel,
10845                                            symtab_hdr) - local_sda;
10846       reloc = R_NDS32_GOT20;
10847     }
10848   else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
10849     {
10850       foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
10851                                            symtab_hdr) - local_sda;
10852       reloc = R_NDS32_PLT_GOTREL_LO20;
10853     }
10854   else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
10855     {
10856       foff = calculate_memory_address (abfd, irel, isymbuf,
10857                                        symtab_hdr) - local_sda;
10858       reloc = R_NDS32_GOTOFF;
10859     }
10860   else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12)
10861     {
10862       foff = local_sda - sec->output_section->vma + sec->output_offset
10863         + irel->r_offset + irel->r_addend;
10864       reloc = R_NDS32_GOTPC20;
10865     }
10866   else
10867     return;
10868
10869   if ((foff < CONSERVATIVE_20BIT) && (foff >= -CONSERVATIVE_20BIT))
10870     {
10871       /* Turn into MOVI.  */
10872       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10873       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10874       bfd_putb32 (insn, contents + laddr);
10875     }
10876 }
10877
10878 /* Relax low part of LE TLS instruction pattern.  */
10879
10880 static void
10881 nds32_elf_relax_letlslo12 (struct bfd_link_info *link_info, bfd *abfd,
10882                            Elf_Internal_Rela *irel,
10883                            bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10884                            Elf_Internal_Shdr *symtab_hdr)
10885 {
10886   uint32_t insn;
10887   bfd_vma laddr;
10888   bfd_signed_vma foff;
10889   unsigned long reloc;
10890
10891   laddr = irel->r_offset;
10892   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10893   BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10894   foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10895   insn = bfd_getb32 (contents + laddr);
10896
10897   if ( (bfd_signed_vma) (foff) < CONSERVATIVE_20BIT
10898       && (bfd_signed_vma) (foff) >= -CONSERVATIVE_20BIT)
10899     {
10900       /* Pattern sethi-ori transform to movi.  */
10901       reloc = R_NDS32_TLS_LE_20;
10902       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10903       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10904       bfd_putb32 (insn, contents + laddr);
10905     }
10906 }
10907
10908 /* Relax LE TLS calculate address instruction pattern.  */
10909
10910 static void
10911 nds32_elf_relax_letlsadd (struct bfd_link_info *link_info, bfd *abfd,
10912                           asection *sec, Elf_Internal_Rela *irel,
10913                           Elf_Internal_Rela *internal_relocs,
10914                           bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10915                           Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
10916 {
10917   /* Local TLS non-pic
10918      sethi    ta, hi20(symbol@tpoff)      ; TLS_LE_HI20
10919      ori      ta, ta, lo12(symbol@tpoff)  ; TLS_LE_LO12
10920      add      ra, ta, tp                  ; TLS_LE_ADD */
10921
10922   uint32_t insn;
10923   bfd_vma laddr;
10924   bfd_signed_vma foff;
10925   Elf_Internal_Rela *i1_irelfn, *irelend;
10926
10927   irelend = internal_relocs + sec->reloc_count;
10928   laddr = irel->r_offset;
10929   insn = bfd_getb32 (contents + laddr);
10930   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10931                                       R_NDS32_PTR_RESOLVED);
10932   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10933   BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10934   foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10935
10936   /* The range is +/-16k.  */
10937   if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
10938       && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
10939     {
10940       /* Transform add to addi.  */
10941       insn = N32_TYPE2 (ADDI, N32_RT5 (insn), N32_RB5 (insn), 0);
10942       irel->r_info =
10943         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
10944
10945       bfd_putb32 (insn, contents + laddr);
10946       if (i1_irelfn != irelend)
10947         {
10948           i1_irelfn->r_addend |= 1;
10949           *again = TRUE;
10950         }
10951     }
10952 }
10953
10954 /* Relax LE TLS load store instruction pattern.  */
10955
10956 static void
10957 nds32_elf_relax_letlsls (struct bfd_link_info *link_info, bfd *abfd,
10958                          asection *sec, Elf_Internal_Rela *irel,
10959                          Elf_Internal_Rela *internal_relocs,
10960                          bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10961                          Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
10962 {
10963
10964   uint32_t insn;
10965   bfd_vma laddr;
10966   bfd_signed_vma foff;
10967   Elf_Internal_Rela *i1_irelfn, *irelend;
10968   int success = 0;
10969
10970   irelend = internal_relocs + sec->reloc_count;
10971   laddr = irel->r_offset;
10972   insn = bfd_getb32 (contents + laddr);
10973   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
10974                                       R_NDS32_PTR_RESOLVED);
10975   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10976   BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10977   foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10978
10979   switch ((N32_OP6 (insn) << 8) | (insn & 0xff))
10980     {
10981     case (N32_OP6_MEM << 8) | N32_MEM_LB:
10982     case (N32_OP6_MEM << 8) | N32_MEM_SB:
10983     case (N32_OP6_MEM << 8) | N32_MEM_LBS:
10984       /* The range is +/-16k.  */
10985       if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
10986           && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
10987         {
10988           insn =
10989             ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
10990           irel->r_info =
10991             ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
10992           success = 1;
10993           break;
10994         }
10995       /* Fall through.  */
10996     case (N32_OP6_MEM << 8) | N32_MEM_LH:
10997     case (N32_OP6_MEM << 8) | N32_MEM_SH:
10998     case (N32_OP6_MEM << 8) | N32_MEM_LHS:
10999       /* The range is +/-32k.  */
11000       if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S1
11001           && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S1)
11002         {
11003           insn =
11004             ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11005           irel->r_info =
11006             ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S1);
11007           success = 1;
11008           break;
11009         }
11010       /* Fall through.  */
11011     case (N32_OP6_MEM << 8) | N32_MEM_LW:
11012     case (N32_OP6_MEM << 8) | N32_MEM_SW:
11013       /* The range is +/-64k.  */
11014       if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S2
11015           && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S2)
11016         {
11017           insn =
11018             ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11019           irel->r_info =
11020             ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S2);
11021           success = 1;
11022           break;
11023         }
11024       /* Fall through.  */
11025     default:
11026       break;
11027     }
11028
11029   if (success)
11030     {
11031       bfd_putb32 (insn, contents + laddr);
11032       if (i1_irelfn != irelend)
11033         {
11034           i1_irelfn->r_addend |= 1;
11035           *again = TRUE;
11036         }
11037     }
11038 }
11039
11040 /* Relax PTR relocation for nds32_elf_relax_section.  */
11041
11042 static bfd_boolean
11043 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11044                      Elf_Internal_Rela *internal_relocs, int *insn_len,
11045                      int *seq_len, bfd_byte *contents)
11046 {
11047   Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11048
11049   irelend = internal_relocs + sec->reloc_count;
11050
11051   re_irel =
11052     find_relocs_at_address_addr (irel, internal_relocs, irelend,
11053                                  R_NDS32_PTR_RESOLVED, irel->r_addend);
11054
11055   if (re_irel == irelend)
11056     {
11057       _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11058                           irel->r_offset);
11059       return FALSE;
11060     }
11061
11062   if (re_irel->r_addend != 1)
11063     return FALSE;
11064
11065   /* Pointed target is relaxed and no longer needs this void *,
11066      change the type to NONE.  */
11067   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11068
11069   /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11070      not exist, it means only count 1 and remove it directly.  */
11071   /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11072   count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11073                                        R_NDS32_PTR_COUNT);
11074   ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11075                                      R_NDS32_PTR);
11076   if (count_irel != irelend)
11077     {
11078       if (--count_irel->r_addend > 0)
11079         return FALSE;
11080     }
11081
11082   if (ptr_irel != irelend)
11083     return FALSE;
11084
11085   /* If the PTR_COUNT is already 0, remove current instruction.  */
11086   *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11087   *insn_len = 0;
11088   return TRUE;
11089 }
11090
11091 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section.  */
11092
11093 static void
11094 nds32_elf_relax_pltgot_suff (struct bfd_link_info *link_info, bfd *abfd,
11095                              asection *sec, Elf_Internal_Rela *irel,
11096                              Elf_Internal_Rela *internal_relocs,
11097                              bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11098                              Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11099 {
11100   uint32_t insn;
11101   bfd_signed_vma foff;
11102   Elf_Internal_Rela *i1_irelfn, *irelend;
11103   bfd_vma local_sda, laddr;
11104
11105   irelend = internal_relocs + sec->reloc_count;
11106   laddr = irel->r_offset;
11107   insn = bfd_getb32 (contents + laddr);
11108
11109   /* FIXME: It's a little trouble to turn JRAL5 to JAL since
11110      we need additional space.  It might be help if we could
11111      borrow some space from instructions to be eliminated
11112      such as sethi, ori, add.  */
11113   if (insn & 0x80000000)
11114     return;
11115
11116   if (nds32_elf_check_dup_relocs
11117       (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
11118     return;
11119
11120   i1_irelfn =
11121     find_relocs_at_address (irel, internal_relocs, irelend,
11122                             R_NDS32_PTR_RESOLVED);
11123
11124   /* FIXIT 090606
11125      The boundary should be reduced since the .plt section hasn't
11126      been created and the address of specific entry is still unknown
11127      Maybe the range between the function call and the begin of the
11128      .text section can be used to decide if the .plt is in the range
11129      of function call.  */
11130
11131   if (N32_OP6 (insn) == N32_OP6_ALU1
11132       && N32_SUB5 (insn) == N32_ALU1_ADD)
11133     {
11134       /* Get the value of the symbol referred to by the reloc.  */
11135       nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11136                                 &local_sda, FALSE);
11137       foff = (bfd_signed_vma) (calculate_plt_memory_address
11138                                (abfd, link_info, isymbuf, irel,
11139                                 symtab_hdr) - local_sda);
11140       /* This condition only happened when symbol is undefined.  */
11141       if (foff == 0)
11142         return;
11143
11144       if (foff < -CONSERVATIVE_19BIT || foff >= CONSERVATIVE_19BIT)
11145         return;
11146       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11147                                    R_NDS32_PLT_GOTREL_LO19);
11148       /* addi.gp */
11149       insn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
11150     }
11151   else if (N32_OP6 (insn) == N32_OP6_JREG
11152            && N32_SUB5 (insn) == N32_JREG_JRAL)
11153     {
11154       /* Get the value of the symbol referred to by the reloc.  */
11155       foff =
11156         calculate_plt_offset (abfd, sec, link_info, isymbuf, irel, symtab_hdr);
11157       /* This condition only happened when symbol is undefined.  */
11158       if (foff == 0)
11159         return;
11160       if (foff < -CONSERVATIVE_24BIT_S1 || foff >= CONSERVATIVE_24BIT_S1)
11161         return;
11162       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
11163       insn = INSN_JAL;
11164     }
11165   else
11166     return;
11167
11168   bfd_putb32 (insn, contents + laddr);
11169   if (i1_irelfn != irelend)
11170     {
11171       i1_irelfn->r_addend |= 1;
11172       *again = TRUE;
11173     }
11174 }
11175
11176 /* Relax GOT_SUFF relocation for nds32_elf_relax_section.  */
11177
11178 static void
11179 nds32_elf_relax_got_suff (struct bfd_link_info *link_info, bfd *abfd,
11180                           asection *sec, Elf_Internal_Rela *irel,
11181                           Elf_Internal_Rela *internal_relocs,
11182                           bfd_byte *contents, Elf_Internal_Shdr *symtab_hdr,
11183                           bfd_boolean *again)
11184 {
11185   uint32_t insn;
11186   bfd_signed_vma foff;
11187   Elf_Internal_Rela *i1_irelfn, *irelend;
11188   bfd_vma local_sda, laddr;
11189
11190   irelend = internal_relocs + sec->reloc_count;
11191   laddr = irel->r_offset;
11192   insn = bfd_getb32 (contents + laddr);
11193   if (insn & 0x80000000)
11194     return;
11195
11196   if (nds32_elf_check_dup_relocs
11197       (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
11198     return;
11199
11200   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11201                                       R_NDS32_PTR_RESOLVED);
11202
11203   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11204                             &local_sda, FALSE);
11205   foff = calculate_got_memory_address (abfd, link_info, irel,
11206                                        symtab_hdr) - local_sda;
11207
11208   if (foff < CONSERVATIVE_19BIT && foff >= -CONSERVATIVE_19BIT)
11209     {
11210       /* Turn LW to LWI.GP.  Change relocation type to R_NDS32_GOT_REL.  */
11211       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11212       irel->r_info =
11213         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
11214       bfd_putb32 (insn, contents + laddr);
11215       if (i1_irelfn != irelend)
11216         {
11217           i1_irelfn->r_addend |= 1;
11218           *again = TRUE;
11219         }
11220     }
11221 }
11222
11223 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section.  */
11224
11225 static void
11226 nds32_elf_relax_gotoff_suff (struct bfd_link_info *link_info, bfd *abfd,
11227                              asection *sec, Elf_Internal_Rela *irel,
11228                              Elf_Internal_Rela *internal_relocs,
11229                              bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11230                              Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11231 {
11232   int opc_insn_gotoff;
11233   uint32_t insn;
11234   bfd_signed_vma foff;
11235   Elf_Internal_Rela *i1_irelfn, *i2_irelfn, *irelend;
11236   bfd_vma local_sda, laddr;
11237
11238   irelend = internal_relocs + sec->reloc_count;
11239   laddr = irel->r_offset;
11240   insn = bfd_getb32 (contents + laddr);
11241
11242   if (insn & 0x80000000)
11243     return;
11244
11245   if (nds32_elf_check_dup_relocs
11246       (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
11247     return;
11248
11249   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11250                                       R_NDS32_PTR_RESOLVED);
11251   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11252                             &local_sda, FALSE);
11253   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11254   foff = foff - local_sda;
11255
11256   if (foff >= CONSERVATIVE_19BIT || foff < -CONSERVATIVE_19BIT)
11257     return;
11258
11259   /* Concatenate opcode and sub-opcode for switch case.
11260      It may be MEM or ALU1.  */
11261   opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
11262   switch (opc_insn_gotoff)
11263     {
11264     case (N32_OP6_MEM << 8) | N32_MEM_LW:
11265       /* 4-byte aligned.  */
11266       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11267       irel->r_info =
11268         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11269       break;
11270     case (N32_OP6_MEM << 8) | N32_MEM_SW:
11271       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
11272       irel->r_info =
11273         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11274       break;
11275     case (N32_OP6_MEM << 8) | N32_MEM_LH:
11276       /* 2-byte aligned.  */
11277       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
11278       irel->r_info =
11279         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11280       break;
11281     case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11282       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
11283       irel->r_info =
11284         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11285       break;
11286     case (N32_OP6_MEM << 8) | N32_MEM_SH:
11287       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
11288       irel->r_info =
11289         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11290       break;
11291     case (N32_OP6_MEM << 8) | N32_MEM_LB:
11292       /* 1-byte aligned.  */
11293       insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
11294       irel->r_info =
11295         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11296       break;
11297     case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11298       insn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
11299       irel->r_info =
11300         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11301       break;
11302     case (N32_OP6_MEM << 8) | N32_MEM_SB:
11303       insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
11304       irel->r_info =
11305         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11306       break;
11307     case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD:
11308       insn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
11309       irel->r_info =
11310         ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11311       break;
11312     default:
11313       return;
11314     }
11315
11316   bfd_putb32 (insn, contents + laddr);
11317   if (i1_irelfn != irelend)
11318     {
11319       i1_irelfn->r_addend |= 1;
11320       *again = TRUE;
11321     }
11322   if ((i2_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11323                                            R_NDS32_INSN16)) != irelend)
11324     i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11325
11326 }
11327
11328 static bfd_boolean
11329 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11330                           Elf_Internal_Rela *internal_relocs,
11331                           bfd_byte *contents,
11332                           nds32_elf_blank_t **relax_blank_list,
11333                           int optimize, int opt_size)
11334 {
11335   /* This code block is used to adjust 4-byte alignment by relax a pair
11336      of instruction a time.
11337
11338      It recognizes three types of relocations.
11339      1. R_NDS32_LABEL - a alignment.
11340      2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11341      3. is_16bit_NOP () - remove a 16-bit instruction.  */
11342
11343   /* TODO: It seems currently implementation only support 4-byte alignment.
11344      We should handle any-alignment.  */
11345
11346   Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11347   Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11348   Elf_Internal_Rela rel_temp;
11349   Elf_Internal_Rela *irelend;
11350   bfd_vma address;
11351   uint16_t insn16;
11352
11353   /* Checking for branch relaxation relies on the relocations to
11354      be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11355   nds32_insertion_sort (internal_relocs, sec->reloc_count,
11356                         sizeof (Elf_Internal_Rela), compar_reloc);
11357
11358   irelend = internal_relocs + sec->reloc_count;
11359
11360   /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11361   /* FIXME: Can we generate the right order in assembler?
11362      So we don't have to swapping them here.  */
11363
11364   for (label_rel = internal_relocs, insn_rel = internal_relocs;
11365        label_rel < irelend; label_rel++)
11366     {
11367       if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11368         continue;
11369
11370       /* Find the first reloc has the same offset with label_rel.  */
11371       while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11372         insn_rel++;
11373
11374       for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11375            insn_rel++)
11376         /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11377            address.  */
11378         if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11379           break;
11380
11381       if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11382           && insn_rel < label_rel)
11383         {
11384           /* Swap the two reloc if the R_NDS32_INSN16 is
11385              before R_NDS32_LABEL.  */
11386           memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11387           memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11388           memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11389         }
11390     }
11391
11392   label_rel = NULL;
11393   insn_rel = NULL;
11394   /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11395      or higher, remove other R_NDS32_LABEL with lower alignment.
11396      If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11397      then the R_NDS32_LABEL sequence is broke.  */
11398   for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11399     {
11400       if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11401         {
11402           if (label_rel == NULL)
11403             {
11404               if (tmp_rel->r_addend < 2)
11405                 label_rel = tmp_rel;
11406               continue;
11407             }
11408           else if (tmp_rel->r_addend > 1)
11409             {
11410               /* Remove all LABEL relocation from label_rel to tmp_rel
11411                  including relocations with same offset as tmp_rel.  */
11412               for (tmp2_rel = label_rel; tmp2_rel < tmp_rel
11413                    || tmp2_rel->r_offset == tmp_rel->r_offset; tmp2_rel++)
11414                 {
11415                   if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11416                       && tmp2_rel->r_addend < 2)
11417                     tmp2_rel->r_info =
11418                       ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11419                                     R_NDS32_NONE);
11420                 }
11421               label_rel = NULL;
11422             }
11423         }
11424       else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11425         {
11426           /* A new INSN16 which can be converted, so clear label_rel.  */
11427           if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11428                                    irelend, &insn16)
11429               || is_16bit_NOP (abfd, sec, tmp_rel))
11430             label_rel = NULL;
11431         }
11432     }
11433
11434   label_rel = NULL;
11435   insn_rel = NULL;
11436   /* Optimized for speed and nothing has not been relaxed.
11437      It's time to align labels.
11438      We may convert a 16-bit instruction right before a label to
11439      32-bit, in order to align the label if necessary
11440      all reloc entries has been sorted by r_offset.  */
11441   for (irel = internal_relocs; irel < irelend; irel++)
11442     {
11443       if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11444           && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11445         continue;
11446
11447       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11448         {
11449           /* A new INSN16 found, resize the old one.  */
11450           if (is_convert_32_to_16
11451               (abfd, sec, irel, internal_relocs, irelend, &insn16)
11452               || is_16bit_NOP (abfd, sec, irel))
11453             {
11454               if (insn_rel)
11455                 {
11456                   /* Previous INSN16 reloc exists, reduce its
11457                      size to 16-bit.  */
11458                   if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11459                                            irelend, &insn16))
11460                     {
11461                       nds32_elf_write_16 (abfd, contents, insn_rel,
11462                                           internal_relocs, irelend, insn16);
11463
11464                       if (!insert_nds32_elf_blank_recalc_total
11465                           (relax_blank_list, insn_rel->r_offset + 2, 2))
11466                         return FALSE;
11467                     }
11468                   else if (is_16bit_NOP (abfd, sec, insn_rel))
11469                     {
11470                       if (!insert_nds32_elf_blank_recalc_total
11471                           (relax_blank_list, insn_rel->r_offset, 2))
11472                         return FALSE;
11473                     }
11474                   insn_rel->r_info =
11475                     ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11476                 }
11477               /* Save the new one for later use.  */
11478               insn_rel = irel;
11479             }
11480           else
11481             irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11482                                          R_NDS32_NONE);
11483         }
11484       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11485         {
11486           /* Search for label.  */
11487           int force_relax = 0;
11488
11489           /* Label on 16-bit instruction or optimization
11490              needless, just reset this reloc.  */
11491           insn16 = bfd_getb16 (contents + irel->r_offset);
11492           if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
11493             {
11494               irel->r_info =
11495                 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11496               continue;
11497             }
11498
11499           address =
11500             irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
11501                                                         irel->r_offset, 1);
11502
11503           if (!insn_rel)
11504             {
11505               /* Check if there is case which can not be aligned.  */
11506               if (irel->r_addend == 2 && address & 0x2)
11507                 return FALSE;
11508               continue;
11509             }
11510
11511           /* Try to align this label.  */
11512
11513           if ((irel->r_addend & 0x1f) < 2)
11514             {
11515               /* Check if there is a INSN16 at the same address.
11516                  Label_rel always seats before insn_rel after
11517                  our sort.  */
11518
11519               /* Search for INSN16 at LABEL location.  If INSN16 is at
11520                  same location and this LABEL alignment is lower than 2,
11521                  the INSN16 can be converted to 2-byte.  */
11522               for (tmp_rel = irel;
11523                    tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
11524                    tmp_rel++)
11525                 {
11526                   if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
11527                       && (is_convert_32_to_16
11528                           (abfd, sec, tmp_rel, internal_relocs,
11529                            irelend, &insn16)
11530                           || is_16bit_NOP (abfd, sec, tmp_rel)))
11531                     {
11532                       force_relax = 1;
11533                       break;
11534                     }
11535                 }
11536             }
11537
11538           if (force_relax || irel->r_addend == 1 || address & 0x2)
11539             {
11540               /* Label not aligned.  */
11541               /* Previous reloc exists, reduce its size to 16-bit.  */
11542               if (is_convert_32_to_16 (abfd, sec, insn_rel,
11543                                        internal_relocs, irelend, &insn16))
11544                 {
11545                   nds32_elf_write_16 (abfd, contents, insn_rel,
11546                                       internal_relocs, irelend, insn16);
11547
11548                   if (!insert_nds32_elf_blank_recalc_total
11549                       (relax_blank_list, insn_rel->r_offset + 2, 2))
11550                     return FALSE;
11551                 }
11552               else if (is_16bit_NOP (abfd, sec, insn_rel))
11553                 {
11554                   if (!insert_nds32_elf_blank_recalc_total
11555                       (relax_blank_list, insn_rel->r_offset, 2))
11556                     return FALSE;
11557                 }
11558
11559             }
11560           /* INSN16 reloc is used.  */
11561           insn_rel = NULL;
11562         }
11563     }
11564
11565   address =
11566     sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
11567   if (insn_rel && (address & 0x2 || opt_size))
11568     {
11569       if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11570                                irelend, &insn16))
11571         {
11572           nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
11573                               irelend, insn16);
11574           if (!insert_nds32_elf_blank_recalc_total
11575               (relax_blank_list, insn_rel->r_offset + 2, 2))
11576             return FALSE;
11577           insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11578                                            R_NDS32_NONE);
11579         }
11580       else if (is_16bit_NOP (abfd, sec, insn_rel))
11581         {
11582           if (!insert_nds32_elf_blank_recalc_total
11583               (relax_blank_list, insn_rel->r_offset, 2))
11584             return FALSE;
11585           insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11586                                            R_NDS32_NONE);
11587         }
11588     }
11589   insn_rel = NULL;
11590   return TRUE;
11591 }
11592
11593 /* Pick relaxation round.  */
11594
11595 static int
11596 nds32_elf_pick_relax (bfd_boolean init, asection *sec, bfd_boolean *again,
11597                       struct elf_nds32_link_hash_table *table,
11598                       struct bfd_link_info *link_info)
11599 {
11600   static asection *final_sec, *first_sec = NULL;
11601   static bfd_boolean normal_again = FALSE;
11602   static bfd_boolean set = FALSE;
11603   static bfd_boolean first = TRUE;
11604   int round_table[] = {
11605       NDS32_RELAX_NORMAL_ROUND,
11606       NDS32_RELAX_JUMP_IFC_ROUND,
11607       NDS32_RELAX_EX9_BUILD_ROUND,
11608       NDS32_RELAX_EX9_REPLACE_ROUND,
11609   };
11610   static int pass = 0;
11611   static int relax_round;
11612
11613   /* The new round.  */
11614   if (init && first_sec == sec)
11615     {
11616       set = TRUE;
11617       normal_again = FALSE;
11618     }
11619
11620   if (first)
11621     {
11622       /* Run an empty run to get the final section.  */
11623       relax_round = NDS32_RELAX_EMPTY_ROUND;
11624
11625       /* It has to enter relax again because we can
11626          not make sure what the final turn is.  */
11627       *again = TRUE;
11628
11629       first = FALSE;
11630       first_sec = sec;
11631     }
11632
11633   if (!set)
11634     {
11635       /* Not reenter yet.  */
11636       final_sec = sec;
11637       return relax_round;
11638     }
11639
11640   relax_round = round_table[pass];
11641
11642   if (!init && relax_round == NDS32_RELAX_NORMAL_ROUND && *again)
11643     normal_again = TRUE;
11644
11645   if (!init && final_sec == sec)
11646     {
11647       switch (relax_round)
11648         {
11649         case NDS32_RELAX_NORMAL_ROUND:
11650           if (!normal_again)
11651             {
11652               /* Normal relaxation done.  */
11653               if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11654                 {
11655                   pass++;
11656                   *again = TRUE;
11657                 }
11658               else if (table->target_optimize & NDS32_RELAX_EX9_ON)
11659                 {
11660                   pass += 2;    /* NDS32_RELAX_EX9_BUILD_ROUND */
11661                   *again = TRUE;
11662                 }
11663               else if (table->ex9_import_file)
11664                 {
11665                   /* Import ex9 table.  */
11666                   if (table->update_ex9_table)
11667                     pass += 2;  /* NDS32_RELAX_EX9_BUILD_ROUND */
11668                   else
11669                     pass += 3;  /* NDS32_RELAX_EX9_REPLACE_ROUND */
11670                   nds32_elf_ex9_import_table (link_info);
11671                   *again = TRUE;
11672                 }
11673             }
11674           break;
11675         case NDS32_RELAX_JUMP_IFC_ROUND:
11676           if (!nds32_elf_ifc_finish (link_info))
11677             _bfd_error_handler (_("error: Jump IFC Fail."));
11678           if (table->target_optimize & NDS32_RELAX_EX9_ON)
11679             {
11680               pass++;
11681               *again = TRUE;
11682             }
11683           break;
11684         case NDS32_RELAX_EX9_BUILD_ROUND:
11685           nds32_elf_ex9_finish (link_info);
11686           pass++;
11687           *again = TRUE;
11688           break;
11689         case NDS32_RELAX_EX9_REPLACE_ROUND:
11690           if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11691             {
11692               /* Do jump IFC optimization again.  */
11693               if (!nds32_elf_ifc_finish (link_info))
11694                 _bfd_error_handler (_("error: Jump IFC Fail."));
11695             }
11696           break;
11697         default:
11698           break;
11699         }
11700     }
11701
11702   return relax_round;
11703 }
11704
11705 static bfd_boolean
11706 nds32_elf_relax_section (bfd *abfd, asection *sec,
11707                          struct bfd_link_info *link_info, bfd_boolean *again)
11708 {
11709   nds32_elf_blank_t *relax_blank_list = NULL;
11710   Elf_Internal_Shdr *symtab_hdr;
11711   Elf_Internal_Rela *internal_relocs;
11712   Elf_Internal_Rela *irel;
11713   Elf_Internal_Rela *irelend;
11714   Elf_Internal_Sym *isymbuf = NULL;
11715   bfd_byte *contents = NULL;
11716   bfd_boolean result = TRUE;
11717   int optimize = 0;
11718   int opt_size = 0;
11719   uint32_t insn;
11720   uint16_t insn16;
11721
11722   /* Target dependnet option.  */
11723   struct elf_nds32_link_hash_table *table;
11724   int load_store_relax;
11725   int relax_round;
11726
11727   relax_blank_list = NULL;
11728
11729   *again = FALSE;
11730
11731   /* Nothing to do for
11732    * relocatable link or
11733    * non-relocatable section or
11734    * non-code section or
11735    * empty content or
11736    * no reloc entry.  */
11737   if (bfd_link_relocatable (link_info)
11738       || (sec->flags & SEC_RELOC) == 0
11739       || (sec->flags & SEC_EXCLUDE) != 0
11740       || (sec->flags & SEC_CODE) == 0
11741       || sec->size == 0)
11742     return TRUE;
11743
11744   /* 09.12.11 Workaround.  */
11745   /*  We have to adjust align for R_NDS32_LABEL if needed.
11746       The adjust approach only can fix 2-byte align once.  */
11747   if (sec->alignment_power > 2)
11748     return TRUE;
11749
11750   /* The optimization type to do.  */
11751
11752   table = nds32_elf_hash_table (link_info);
11753   relax_round = nds32_elf_pick_relax (TRUE, sec, again, table, link_info);
11754   switch (relax_round)
11755     {
11756     case NDS32_RELAX_JUMP_IFC_ROUND:
11757       /* Here is the entrance of ifc jump relaxation.  */
11758       if (!nds32_elf_ifc_calc (link_info, abfd, sec))
11759         return FALSE;
11760       nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11761       return TRUE;
11762
11763     case NDS32_RELAX_EX9_BUILD_ROUND:
11764       /* Here is the entrance of ex9 relaxation.  There are two pass of
11765          ex9 relaxation.  The one is to traverse all instructions and build
11766          the hash table.  The other one is to compare instructions and replace
11767          it by ex9.it.  */
11768       if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
11769         return FALSE;
11770       nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11771       return TRUE;
11772
11773     case NDS32_RELAX_EX9_REPLACE_ROUND:
11774       if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
11775         return FALSE;
11776       return TRUE;
11777
11778     case NDS32_RELAX_EMPTY_ROUND:
11779       nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11780       return TRUE;
11781
11782     case NDS32_RELAX_NORMAL_ROUND:
11783     default:
11784       if (sec->reloc_count == 0)
11785         return TRUE;
11786       break;
11787     }
11788
11789   /* The begining of general relaxation.  */
11790
11791   if (is_SDA_BASE_set == 0)
11792     {
11793       bfd_vma gp;
11794       is_SDA_BASE_set = 1;
11795       nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11796                                 &gp, FALSE);
11797       relax_range_measurement (abfd);
11798     }
11799
11800   if (is_ITB_BASE_set == 0)
11801     {
11802       /* Set the _ITB_BASE_.  */
11803       if (!nds32_elf_ex9_itb_base (link_info))
11804         {
11805           _bfd_error_handler (_("%B: error: Cannot set _ITB_BASE_"), abfd);
11806           bfd_set_error (bfd_error_bad_value);
11807         }
11808     }
11809
11810   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
11811   /* Relocations MUST be kept in memory, because relaxation adjust them.  */
11812   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
11813                                                TRUE /* keep_memory */);
11814   if (internal_relocs == NULL)
11815     goto error_return;
11816
11817   irelend = internal_relocs + sec->reloc_count;
11818   irel = find_relocs_at_address (internal_relocs, internal_relocs,
11819                                  irelend, R_NDS32_RELAX_ENTRY);
11820
11821   if (irel == irelend)
11822     return TRUE;
11823
11824   if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
11825     {
11826       if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
11827         {
11828           nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11829           return TRUE;
11830         }
11831
11832       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
11833         optimize = 1;
11834
11835       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
11836         opt_size = 1;
11837     }
11838
11839   load_store_relax = table->load_store_relax;
11840
11841   /* Get symbol table and section content.  */
11842   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
11843       || !nds32_get_local_syms (abfd, sec, &isymbuf))
11844     goto error_return;
11845
11846   /* Do relax loop only when finalize is not done.
11847      Take care of relaxable relocs except INSN16.  */
11848   for (irel = internal_relocs; irel < irelend; irel++)
11849     {
11850       int seq_len;              /* Original length of instruction sequence.  */
11851       int insn_len = 0;         /* Final length of instruction sequence.  */
11852       bfd_boolean removed;
11853
11854       insn = 0;
11855       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
11856           && (irel->r_addend & 0x1f) >= 2)
11857         optimize = 1;
11858
11859       /* Relocation Types
11860          R_NDS32_LONGCALL1      53
11861          R_NDS32_LONGCALL2      54
11862          R_NDS32_LONGCALL3      55
11863          R_NDS32_LONGJUMP1      56
11864          R_NDS32_LONGJUMP2      57
11865          R_NDS32_LONGJUMP3      58
11866          R_NDS32_LOADSTORE      59  */
11867       if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
11868           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
11869         seq_len = GET_SEQ_LEN (irel->r_addend);
11870
11871       /* Relocation Types
11872          R_NDS32_LONGCALL4      107
11873          R_NDS32_LONGCALL5      108
11874          R_NDS32_LONGCALL6      109
11875          R_NDS32_LONGJUMP4      110
11876          R_NDS32_LONGJUMP5      111
11877          R_NDS32_LONGJUMP6      112
11878          R_NDS32_LONGJUMP7      113  */
11879       else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
11880                && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
11881         seq_len = 4;
11882
11883         /* Relocation Types
11884          R_NDS32_LO12S0_RELA            30
11885          R_NDS32_LO12S1_RELA            29
11886          R_NDS32_LO12S2_RELA            28
11887          R_NDS32_LO12S2_SP_RELA         71
11888          R_NDS32_LO12S2_DP_RELA         70
11889          R_NDS32_GOT_LO12               46
11890          R_NDS32_GOTOFF_LO12            50
11891          R_NDS32_PLTREL_LO12            65
11892          R_NDS32_PLT_GOTREL_LO12        67
11893          R_NDS32_17IFC_PCREL_RELA       96
11894          R_NDS32_GOT_SUFF               193
11895          R_NDS32_GOTOFF_SUFF            194
11896          R_NDS32_PLT_GOT_SUFF           195
11897          R_NDS32_MULCALL_SUFF           196
11898          R_NDS32_PTR                    197  */
11899       else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
11900                 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
11901                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
11902                || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
11903                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
11904                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
11905                || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
11906                || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
11907                || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
11908                || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
11909                    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
11910                || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
11911                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
11912                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
11913                || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS)
11914         seq_len = 0;
11915       else
11916         continue;
11917
11918       insn_len = seq_len;
11919       removed = FALSE;
11920
11921       switch (ELF32_R_TYPE (irel->r_info))
11922         {
11923         case R_NDS32_LONGCALL1:
11924           removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
11925                                                &insn_len, contents, isymbuf,
11926                                                symtab_hdr);
11927           break;
11928         case R_NDS32_LONGCALL2:
11929           removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
11930                                                &insn_len, contents, isymbuf,
11931                                                symtab_hdr);
11932           break;
11933         case R_NDS32_LONGCALL3:
11934           removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
11935                                                &insn_len, contents, isymbuf,
11936                                                symtab_hdr);
11937           break;
11938         case R_NDS32_LONGJUMP1:
11939           removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
11940                                                &insn_len, contents, isymbuf,
11941                                                symtab_hdr);
11942           break;
11943         case R_NDS32_LONGJUMP2:
11944           removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
11945                                                &insn_len, contents, isymbuf,
11946                                                symtab_hdr);
11947           break;
11948         case R_NDS32_LONGJUMP3:
11949           removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
11950                                                &insn_len, contents, isymbuf,
11951                                                symtab_hdr);
11952           break;
11953         case R_NDS32_LONGCALL4:
11954           removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
11955                                                &insn_len, contents, isymbuf,
11956                                                symtab_hdr);
11957           break;
11958         case R_NDS32_LONGCALL5:
11959           removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
11960                                                &insn_len, contents, isymbuf,
11961                                                symtab_hdr);
11962           break;
11963         case R_NDS32_LONGCALL6:
11964           removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
11965                                                &insn_len, contents, isymbuf,
11966                                                symtab_hdr);
11967           break;
11968         case R_NDS32_LONGJUMP4:
11969           removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
11970                                                &insn_len, contents, isymbuf,
11971                                                symtab_hdr);
11972           break;
11973         case R_NDS32_LONGJUMP5:
11974           removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
11975                                                &insn_len, &seq_len, contents,
11976                                                isymbuf, symtab_hdr);
11977           break;
11978         case R_NDS32_LONGJUMP6:
11979           removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
11980                                                &insn_len, &seq_len, contents,
11981                                                isymbuf, symtab_hdr);
11982           break;
11983         case R_NDS32_LONGJUMP7:
11984           removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
11985                                                &insn_len, &seq_len, contents,
11986                                                isymbuf, symtab_hdr);
11987           break;
11988         case R_NDS32_LOADSTORE:
11989           removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
11990                                                internal_relocs, &insn_len,
11991                                                contents, isymbuf, symtab_hdr,
11992                                                load_store_relax);
11993           break;
11994         case R_NDS32_LO12S0_RELA:
11995         case R_NDS32_LO12S1_RELA:
11996         case R_NDS32_LO12S2_DP_RELA:
11997         case R_NDS32_LO12S2_SP_RELA:
11998         case R_NDS32_LO12S2_RELA:
11999           /* Relax for low part.  */
12000           nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12001                                 contents, isymbuf, symtab_hdr);
12002
12003           /* It is impossible to delete blank, so just continue.  */
12004           continue;
12005         case R_NDS32_GOT_LO12:
12006         case R_NDS32_GOTOFF_LO12:
12007         case R_NDS32_PLTREL_LO12:
12008         case R_NDS32_PLT_GOTREL_LO12:
12009         case R_NDS32_GOTPC_LO12:
12010           /* Relax for PIC gp-relative low part.  */
12011           nds32_elf_relax_piclo12 (link_info, abfd, sec, irel, contents,
12012                                    isymbuf, symtab_hdr);
12013
12014           /* It is impossible to delete blank, so just continue.  */
12015           continue;
12016         case R_NDS32_TLS_LE_LO12:
12017           /* Relax for LE TLS low part.  */
12018           nds32_elf_relax_letlslo12 (link_info, abfd, irel, contents,
12019                                      isymbuf, symtab_hdr);
12020
12021           /* It is impossible to delete blank, so just continue.  */
12022           continue;
12023         case R_NDS32_TLS_LE_ADD:
12024           nds32_elf_relax_letlsadd (link_info, abfd, sec, irel, internal_relocs,
12025                                     contents, isymbuf, symtab_hdr, again);
12026           /* It is impossible to delete blank, so just continue.  */
12027           continue;
12028         case R_NDS32_TLS_LE_LS:
12029           nds32_elf_relax_letlsls (link_info, abfd, sec, irel, internal_relocs,
12030                                    contents, isymbuf, symtab_hdr, again);
12031           continue;
12032         case R_NDS32_PTR:
12033           removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12034                                          &insn_len, &seq_len, contents);
12035           break;
12036         case R_NDS32_PLT_GOT_SUFF:
12037           nds32_elf_relax_pltgot_suff (link_info, abfd, sec, irel,
12038                                        internal_relocs, contents,
12039                                        isymbuf, symtab_hdr, again);
12040           /* It is impossible to delete blank, so just continue.  */
12041           continue;
12042         case R_NDS32_GOT_SUFF:
12043           nds32_elf_relax_got_suff (link_info, abfd, sec, irel,
12044                                     internal_relocs, contents,
12045                                     symtab_hdr, again);
12046           /* It is impossible to delete blank, so just continue.  */
12047           continue;
12048         case R_NDS32_GOTOFF_SUFF:
12049           nds32_elf_relax_gotoff_suff (link_info, abfd, sec, irel,
12050                                        internal_relocs, contents,
12051                                        isymbuf, symtab_hdr, again);
12052           /* It is impossible to delete blank, so just continue.  */
12053           continue;
12054         default:
12055           continue;
12056
12057         }
12058       if (removed && seq_len - insn_len > 0)
12059         {
12060           if (!insert_nds32_elf_blank
12061               (&relax_blank_list, irel->r_offset + insn_len,
12062                seq_len - insn_len))
12063             goto error_return;
12064           *again = TRUE;
12065         }
12066     }
12067
12068   calc_nds32_blank_total (relax_blank_list);
12069
12070   if (table->relax_fp_as_gp)
12071     {
12072       if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12073                                  irelend, isymbuf))
12074         goto error_return;
12075
12076       if (!*again)
12077         {
12078           if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12079                                                irelend))
12080             goto error_return;
12081         }
12082     }
12083
12084   nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
12085
12086   if (!*again)
12087     {
12088       if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12089                                      &relax_blank_list, optimize, opt_size))
12090         goto error_return;
12091     }
12092
12093   /* It doesn't matter optimize_for_space_no_align anymore.
12094        If object file is assembled with flag '-Os',
12095        the we don't adjust jump-destination on 4-byte boundary.  */
12096
12097   if (relax_blank_list)
12098     {
12099       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12100       relax_blank_list = NULL;
12101     }
12102
12103   if (!*again)
12104     {
12105       /* Closing the section, so we don't relax it anymore.  */
12106       bfd_vma sec_size_align;
12107       Elf_Internal_Rela *tmp_rel;
12108
12109       /* Pad to alignment boundary.  Only handle current section alignment.  */
12110       sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12111                        & ((-1U) << sec->alignment_power);
12112       if ((sec_size_align - sec->size) & 0x2)
12113         {
12114           insn16 = NDS32_NOP16;
12115           bfd_putb16 (insn16, contents + sec->size);
12116           sec->size += 2;
12117         }
12118
12119       while (sec_size_align != sec->size)
12120         {
12121           insn = NDS32_NOP32;
12122           bfd_putb32 (insn, contents + sec->size);
12123           sec->size += 4;
12124         }
12125
12126       tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12127                                         irelend, R_NDS32_RELAX_ENTRY);
12128       if (tmp_rel != irelend)
12129         tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12130
12131       clean_nds32_elf_blank ();
12132     }
12133
12134 finish:
12135   if (internal_relocs != NULL
12136       && elf_section_data (sec)->relocs != internal_relocs)
12137     free (internal_relocs);
12138
12139   if (contents != NULL
12140       && elf_section_data (sec)->this_hdr.contents != contents)
12141     free (contents);
12142
12143   if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12144     free (isymbuf);
12145
12146   return result;
12147
12148 error_return:
12149   result = FALSE;
12150   goto finish;
12151 }
12152
12153 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12154 {
12155   {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12156   {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12157   {NULL, 0, 0, 0, 0}
12158 };
12159
12160 static bfd_boolean
12161 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12162                             struct bfd_link_info *info,
12163                             void *finfo ATTRIBUTE_UNUSED,
12164                             bfd_boolean (*func) (void *, const char *,
12165                                                  Elf_Internal_Sym *,
12166                                                  asection *,
12167                                                  struct elf_link_hash_entry *)
12168                             ATTRIBUTE_UNUSED)
12169 {
12170   FILE *sym_ld_script = NULL;
12171   struct elf_nds32_link_hash_table *table;
12172
12173   table = nds32_elf_hash_table (info);
12174   sym_ld_script = table->sym_ld_script;
12175
12176   if (check_start_export_sym)
12177     fprintf (sym_ld_script, "}\n");
12178
12179   return TRUE;
12180 }
12181
12182 static enum elf_reloc_type_class
12183 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12184                             const asection *rel_sec ATTRIBUTE_UNUSED,
12185                             const Elf_Internal_Rela *rela)
12186 {
12187   switch ((int) ELF32_R_TYPE (rela->r_info))
12188     {
12189     case R_NDS32_RELATIVE:
12190       return reloc_class_relative;
12191     case R_NDS32_JMP_SLOT:
12192       return reloc_class_plt;
12193     case R_NDS32_COPY:
12194       return reloc_class_copy;
12195     default:
12196       return reloc_class_normal;
12197     }
12198 }
12199
12200 /* Put target dependent option into info hash table.  */
12201 void
12202 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12203                                    int relax_fp_as_gp,
12204                                    int eliminate_gc_relocs,
12205                                    FILE * sym_ld_script, int load_store_relax,
12206                                    int target_optimize, int relax_status,
12207                                    int relax_round, FILE * ex9_export_file,
12208                                    FILE * ex9_import_file,
12209                                    int update_ex9_table, int ex9_limit,
12210                                    bfd_boolean ex9_loop_aware,
12211                                    bfd_boolean ifc_loop_aware)
12212 {
12213   struct elf_nds32_link_hash_table *table;
12214
12215   table = nds32_elf_hash_table (link_info);
12216   if (table == NULL)
12217     return;
12218
12219   table->relax_fp_as_gp = relax_fp_as_gp;
12220   table->eliminate_gc_relocs = eliminate_gc_relocs;
12221   table->sym_ld_script = sym_ld_script;
12222   table ->load_store_relax = load_store_relax;
12223   table->target_optimize = target_optimize;
12224   table->relax_status = relax_status;
12225   table->relax_round = relax_round;
12226   table->ex9_export_file = ex9_export_file;
12227   table->ex9_import_file = ex9_import_file;
12228   table->update_ex9_table = update_ex9_table;
12229   table->ex9_limit = ex9_limit;
12230   table->ex9_loop_aware = ex9_loop_aware;
12231   table->ifc_loop_aware = ifc_loop_aware;
12232 }
12233 \f
12234 /* These functions and data-structures are used for fp-as-gp
12235    optimization.  */
12236
12237 #define FAG_THRESHOLD   3       /* At least 3 gp-access.  */
12238 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12239    the read-only section and read-write section.  */
12240 #define FAG_WINDOW      (508 - 32)
12241
12242 /* An nds32_fag represent a gp-relative access.
12243    We find best fp-base by using a sliding window
12244    to find a base address which can cover most gp-access.  */
12245 struct nds32_fag
12246 {
12247   struct nds32_fag *next;       /* NULL-teminated linked list.  */
12248   bfd_vma addr;                 /* The address of this fag.  */
12249   Elf_Internal_Rela **relas;    /* The relocations associated with this fag.
12250                                    It is used for applying FP7U2_FLAG.  */
12251   int count;                    /* How many times this address is referred.
12252                                    There should be exactly `count' relocations
12253                                    in relas.  */
12254   int relas_capcity;            /* The buffer size of relas.
12255                                    We use an array instead of linked-list,
12256                                    and realloc is used to adjust buffer size.  */
12257 };
12258
12259 static void
12260 nds32_fag_init (struct nds32_fag *head)
12261 {
12262   memset (head, 0, sizeof (struct nds32_fag));
12263 }
12264
12265 static void
12266 nds32_fag_verify (struct nds32_fag *head)
12267 {
12268   struct nds32_fag *iter;
12269   struct nds32_fag *prev;
12270
12271   prev = NULL;
12272   iter = head->next;
12273   while (iter)
12274     {
12275       if (prev && prev->addr >= iter->addr)
12276         puts ("Bug in fp-as-gp insertion.");
12277       prev = iter;
12278       iter = iter->next;
12279     }
12280 }
12281
12282 /* Insert a fag in ascending order.
12283    If a fag of the same address already exists,
12284    they are chained by relas array.  */
12285
12286 static void
12287 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12288                   Elf_Internal_Rela * rel)
12289 {
12290   struct nds32_fag *iter;
12291   struct nds32_fag *new_fag;
12292   const int INIT_RELAS_CAP = 4;
12293
12294   for (iter = head;
12295        iter->next && iter->next->addr <= addr;
12296        iter = iter->next)
12297     /* Find somewhere to insert.  */ ;
12298
12299   /* `iter' will be equal to `head' if the list is empty.  */
12300   if (iter != head && iter->addr == addr)
12301     {
12302       /* The address exists in the list.
12303          Insert `rel' into relocation list, relas.  */
12304
12305       /* Check whether relas is big enough.  */
12306       if (iter->count >= iter->relas_capcity)
12307         {
12308           iter->relas_capcity *= 2;
12309           iter->relas = bfd_realloc
12310             (iter->relas, iter->relas_capcity * sizeof (void *));
12311         }
12312       iter->relas[iter->count++] = rel;
12313       return;
12314     }
12315
12316   /* This is a new address.  Create a fag node for it.  */
12317   new_fag = bfd_malloc (sizeof (struct nds32_fag));
12318   memset (new_fag, 0, sizeof (*new_fag));
12319   new_fag->addr = addr;
12320   new_fag->count = 1;
12321   new_fag->next = iter->next;
12322   new_fag->relas_capcity = INIT_RELAS_CAP;
12323   new_fag->relas = (Elf_Internal_Rela **)
12324     bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12325   new_fag->relas[0] = rel;
12326   iter->next = new_fag;
12327
12328   nds32_fag_verify (head);
12329 }
12330
12331 static void
12332 nds32_fag_free_list (struct nds32_fag *head)
12333 {
12334   struct nds32_fag *iter;
12335
12336   iter = head->next;
12337   while (iter)
12338     {
12339       struct nds32_fag *tmp = iter;
12340       iter = iter->next;
12341       free (tmp->relas);
12342       tmp->relas = NULL;
12343       free (tmp);
12344     }
12345 }
12346
12347 /* Find the best fp-base address.
12348    The relocation associated with that address is returned,
12349    so we can track the symbol instead of a fixed address.
12350
12351    When relaxation, the address of an datum may change,
12352    because a text section is shrinked, so the data section
12353    moves forward.  If the aligments of text and data section
12354    are different, their distance may change too.
12355    Therefore, tracking a fixed address is not appriate.  */
12356
12357 static int
12358 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12359 {
12360   struct nds32_fag *base;       /* First fag in the window.  */
12361   struct nds32_fag *last;       /* First fag outside the window.  */
12362   int accu = 0;                 /* Usage accumulation.  */
12363   struct nds32_fag *best;       /* Best fag.  */
12364   int baccu = 0;                /* Best accumulation.  */
12365
12366   /* Use first fag for initial, and find the last fag in the window.
12367
12368      In each iteration, we could simply subtract previous fag
12369      and accumulate following fags which are inside the window,
12370      untill we each the end.  */
12371
12372   if (head->next == NULL)
12373     {
12374       *bestpp = NULL;
12375       return 0;
12376     }
12377
12378   /* Initialize base.  */
12379   base = head->next;
12380   best = base;
12381   for (last = base;
12382        last && last->addr < base->addr + FAG_WINDOW;
12383        last = last->next)
12384     accu += last->count;
12385
12386   baccu = accu;
12387
12388   /* Record the best base in each iteration.  */
12389   while (base->next)
12390     {
12391       accu -= base->count;
12392       base = base->next;
12393       /* Account fags in window.  */
12394       for (/* Nothing.  */;
12395            last && last->addr < base->addr + FAG_WINDOW;
12396            last = last->next)
12397         accu += last->count;
12398
12399       /* A better fp-base?  */
12400       if (accu > baccu)
12401         {
12402           best = base;
12403           baccu = accu;
12404         }
12405     }
12406
12407   if (bestpp)
12408     *bestpp = best;
12409   return baccu;
12410 }
12411
12412 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12413    so we can convert it fo fp-relative access later.
12414    `best_fag' is the best fp-base.  Only those inside the window
12415    of best_fag is applied the flag.  */
12416
12417 static bfd_boolean
12418 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12419                       bfd *abfd, struct nds32_fag *best_fag,
12420                       Elf_Internal_Rela *internal_relocs,
12421                       Elf_Internal_Rela *irelend)
12422 {
12423   struct nds32_fag *ifag;
12424   bfd_vma best_fpbase, gp;
12425   bfd *output_bfd;
12426
12427   output_bfd = abfd->sections->output_section->owner;
12428   nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12429   best_fpbase = best_fag->addr;
12430
12431   if (best_fpbase > gp + sdata_range[1][1]
12432       || best_fpbase < gp - sdata_range[1][0])
12433     return FALSE;
12434
12435   /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12436      so we know they can be converted to lwi37.fp.   */
12437   for (ifag = best_fag;
12438        ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12439     {
12440       int i;
12441
12442       for (i = 0; i < ifag->count; i++)
12443         {
12444           Elf_Internal_Rela *insn16_rel;
12445           Elf_Internal_Rela *fag_rel;
12446
12447           fag_rel = ifag->relas[i];
12448
12449           /* Only if this is within the WINDOWS, FP7U2_FLAG
12450              is applied.  */
12451
12452           insn16_rel = find_relocs_at_address
12453             (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12454
12455           if (insn16_rel != irelend)
12456             insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12457         }
12458     }
12459   return TRUE;
12460 }
12461
12462 /* Reset INSN16 to clean fp as gp.  */
12463
12464 static void
12465 nds32_fag_unmark_relax (struct nds32_fag *fag,
12466                         Elf_Internal_Rela *internal_relocs,
12467                         Elf_Internal_Rela *irelend)
12468 {
12469   struct nds32_fag *ifag;
12470   int i;
12471   Elf_Internal_Rela *insn16_rel;
12472   Elf_Internal_Rela *fag_rel;
12473
12474   for (ifag = fag; ifag; ifag = ifag->next)
12475     {
12476       for (i = 0; i < ifag->count; i++)
12477         {
12478           fag_rel = ifag->relas[i];
12479
12480           /* Restore the INSN16 relocation.  */
12481           insn16_rel = find_relocs_at_address
12482             (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12483
12484           if (insn16_rel != irelend)
12485             insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12486         }
12487     }
12488 }
12489
12490 /* This is the main function of fp-as-gp optimization.
12491    It should be called by relax_section.  */
12492
12493 static bfd_boolean
12494 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12495                       bfd *abfd, asection *sec,
12496                       Elf_Internal_Rela *internal_relocs,
12497                       Elf_Internal_Rela *irelend,
12498                       Elf_Internal_Sym *isymbuf)
12499 {
12500   Elf_Internal_Rela *begin_rel = NULL;
12501   Elf_Internal_Rela *irel;
12502   struct nds32_fag fag_head;
12503   Elf_Internal_Shdr *symtab_hdr;
12504   bfd_byte *contents;
12505   bfd_boolean ifc_inside = FALSE;
12506
12507   /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12508
12509   /* Per-function fp-base selection.
12510      1. Create a list for all the gp-relative access.
12511      2. Base on those gp-relative address,
12512         find a fp-base which can cover most access.
12513      3. Use the fp-base for fp-as-gp relaxation.
12514
12515      NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12516      we should
12517      1. delete the `la $fp, _FP_BASE_' instruction and
12518      2. not convert lwi.gp to lwi37.fp.
12519
12520      To delete the _FP_BASE_ instruction, we simply apply
12521      R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12522
12523      To suppress the conversion, we simply NOT to apply
12524      R_NDS32_INSN16_FP7U2_FLAG flag.  */
12525
12526   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12527
12528   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12529       || !nds32_get_local_syms (abfd, sec, &isymbuf))
12530     return FALSE;
12531
12532   /* Check whether it is worth for fp-as-gp optimization,
12533      i.e., at least 3 gp-load.
12534
12535      Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12536      apply this optimization.  */
12537
12538   for (irel = internal_relocs; irel < irelend; irel++)
12539     {
12540       /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12541          One we enter the begin of the region, we track all the LW/ST
12542          instructions, so when we leave the region, we try to find
12543          the best fp-base address for those LW/ST instructions.  */
12544
12545       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12546           && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12547         {
12548           /* Begin of the region.  */
12549           if (begin_rel)
12550             /* xgettext:c-format */
12551             _bfd_error_handler (_("%B: Nested OMIT_FP in %A."), abfd, sec);
12552
12553           begin_rel = irel;
12554           nds32_fag_init (&fag_head);
12555           ifc_inside = FALSE;
12556         }
12557       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12558                && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12559         {
12560           int accu;
12561           struct nds32_fag *best_fag, *tmp_fag;
12562           int dist;
12563
12564           /* End of the region.
12565              Check whether it is worth to do fp-as-gp.  */
12566
12567           if (begin_rel == NULL)
12568             {
12569               /* xgettext:c-format */
12570               _bfd_error_handler (_("%B: Unmatched OMIT_FP in %A."), abfd, sec);
12571               continue;
12572             }
12573
12574           accu = nds32_fag_find_base (&fag_head, &best_fag);
12575
12576           /* Clean FP7U2_FLAG because they may set ever.  */
12577           tmp_fag = fag_head.next;
12578           nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12579
12580           /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
12581           if (accu < FAG_THRESHOLD
12582               || !nds32_fag_mark_relax (link_info, abfd, best_fag,
12583                                         internal_relocs, irelend))
12584             {
12585               /* Not worth to do fp-as-gp.  */
12586               begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12587               begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12588               irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12589               irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12590               nds32_fag_free_list (&fag_head);
12591               begin_rel = NULL;
12592               continue;
12593             }
12594
12595           /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12596              so we use it to record the distance to the reloction of best
12597              fp-base.  */
12598           dist = best_fag->relas[0] - begin_rel;
12599           BFD_ASSERT (dist > 0 && dist < 0xffffff);
12600           /* Use high 16 bits of addend to record the _FP_BASE_ matched
12601              relocation.  And get the base value when relocating.  */
12602           begin_rel->r_addend &= (0x1 << 16) - 1;
12603           begin_rel->r_addend |= dist << 16;
12604
12605           nds32_fag_free_list (&fag_head);
12606           begin_rel = NULL;
12607         }
12608
12609       if (begin_rel == NULL || ifc_inside)
12610         /* Skip if we are not in the region of fp-as-gp.  */
12611         continue;
12612
12613       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12614           || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12615         {
12616           bfd_vma addr;
12617           uint32_t insn;
12618
12619           /* A gp-relative access is found.  Insert it to the fag-list.  */
12620
12621           /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
12622           insn = bfd_getb32 (contents + irel->r_offset);
12623           if (!N32_IS_RT3 (insn))
12624             continue;
12625
12626           addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12627           nds32_fag_insert (&fag_head, addr, irel);
12628         }
12629       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12630         {
12631           begin_rel = NULL;
12632         }
12633       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12634                || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12635         {
12636           /* Suppress fp as gp when encounter ifc.  */
12637           ifc_inside = TRUE;
12638         }
12639     }
12640
12641   return TRUE;
12642 }
12643
12644 /* Remove unused `la $fp, _FD_BASE_' instruction.  */
12645
12646 static bfd_boolean
12647 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12648                                 Elf_Internal_Rela *internal_relocs,
12649                                 Elf_Internal_Rela *irelend)
12650 {
12651   Elf_Internal_Rela *irel;
12652   Elf_Internal_Shdr *symtab_hdr;
12653   bfd_byte *contents = NULL;
12654   nds32_elf_blank_t *relax_blank_list = NULL;
12655   bfd_boolean result = TRUE;
12656   bfd_boolean unused_region = FALSE;
12657
12658   /*
12659      NOTE: Disable fp-as-gp if we encounter ifcall relocations.
12660      * R_NDS32_17IFC_PCREL_RELA
12661      * R_NDS32_10IFCU_PCREL_RELA
12662
12663      CASE??????????????
12664   */
12665
12666   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12667   nds32_get_section_contents (abfd, sec, &contents, TRUE);
12668
12669   for (irel = internal_relocs; irel < irelend; irel++)
12670     {
12671       /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
12672          we marked to in previous pass.
12673          DO NOT scan relocations again, since we've alreadly decided it
12674          and set the flag.  */
12675       const char *syname;
12676       int syndx;
12677       uint32_t insn;
12678
12679       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12680           && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12681         unused_region = TRUE;
12682       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12683                && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12684         unused_region = FALSE;
12685
12686       /* We're not in the region.  */
12687       if (!unused_region)
12688         continue;
12689
12690       /* _FP_BASE_ must be a GLOBAL symbol.  */
12691       syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
12692       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
12693         continue;
12694
12695       /* The symbol name must be _FP_BASE_.  */
12696       syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
12697       if (strcmp (syname, FP_BASE_NAME) != 0)
12698         continue;
12699
12700       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
12701         {
12702           /* addi.gp  $fp, -256  */
12703           insn = bfd_getb32 (contents + irel->r_offset);
12704           if (insn != INSN_ADDIGP_TO_FP)
12705             continue;
12706         }
12707       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
12708         {
12709           /* addi  $fp, $gp, -256  */
12710           insn = bfd_getb32 (contents + irel->r_offset);
12711           if (insn != INSN_ADDI_GP_TO_FP)
12712             continue;
12713         }
12714       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
12715         {
12716           /* movi  $fp, FP_BASE  */
12717           insn = bfd_getb32 (contents + irel->r_offset);
12718           if (insn != INSN_MOVI_TO_FP)
12719             continue;
12720         }
12721       else
12722         continue;
12723
12724       /* We got here because a FP_BASE instruction is found.  */
12725       if (!insert_nds32_elf_blank_recalc_total
12726           (&relax_blank_list, irel->r_offset, 4))
12727         goto error_return;
12728     }
12729
12730 finish:
12731   if (relax_blank_list)
12732     {
12733       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12734       relax_blank_list = NULL;
12735     }
12736   return result;
12737
12738 error_return:
12739   result = FALSE;
12740   goto finish;
12741 }
12742
12743 /* This is a version of bfd_generic_get_relocated_section_contents.
12744    We need this variety because relaxation will modify the dwarf
12745    infomation.  When there is undefined symbol reference error mesage,
12746    linker need to dump line number where the symbol be used.  However
12747    the address is be relaxed, it can not get the original dwarf contents.
12748    The variety only modify function call for reading in the section.  */
12749
12750 static bfd_byte *
12751 nds32_elf_get_relocated_section_contents (bfd *abfd,
12752                                           struct bfd_link_info *link_info,
12753                                           struct bfd_link_order *link_order,
12754                                           bfd_byte *data,
12755                                           bfd_boolean relocatable,
12756                                           asymbol **symbols)
12757 {
12758   bfd *input_bfd = link_order->u.indirect.section->owner;
12759   asection *input_section = link_order->u.indirect.section;
12760   long reloc_size;
12761   arelent **reloc_vector;
12762   long reloc_count;
12763
12764   reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12765   if (reloc_size < 0)
12766     return NULL;
12767
12768   /* Read in the section.  */
12769   if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
12770     return NULL;
12771
12772   if (reloc_size == 0)
12773     return data;
12774
12775   reloc_vector = (arelent **) bfd_malloc (reloc_size);
12776   if (reloc_vector == NULL)
12777     return NULL;
12778
12779   reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
12780                                         reloc_vector, symbols);
12781   if (reloc_count < 0)
12782     goto error_return;
12783
12784   if (reloc_count > 0)
12785     {
12786       arelent **parent;
12787       for (parent = reloc_vector; *parent != NULL; parent++)
12788         {
12789           char *error_message = NULL;
12790           asymbol *symbol;
12791           bfd_reloc_status_type r;
12792
12793           symbol = *(*parent)->sym_ptr_ptr;
12794           if (symbol->section && discarded_section (symbol->section))
12795             {
12796               bfd_byte *p;
12797               static reloc_howto_type none_howto
12798                 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
12799                          "unused", FALSE, 0, 0, FALSE);
12800
12801               p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
12802               _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
12803                                    p);
12804               (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
12805               (*parent)->addend = 0;
12806               (*parent)->howto = &none_howto;
12807               r = bfd_reloc_ok;
12808             }
12809           else
12810             r = bfd_perform_relocation (input_bfd, *parent, data,
12811                                         input_section,
12812                                         relocatable ? abfd : NULL,
12813                                         &error_message);
12814
12815           if (relocatable)
12816             {
12817               asection *os = input_section->output_section;
12818
12819               /* A partial link, so keep the relocs.  */
12820               os->orelocation[os->reloc_count] = *parent;
12821               os->reloc_count++;
12822             }
12823
12824           if (r != bfd_reloc_ok)
12825             {
12826               switch (r)
12827                 {
12828                 case bfd_reloc_undefined:
12829                   (*link_info->callbacks->undefined_symbol)
12830                     (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12831                      input_bfd, input_section, (*parent)->address, TRUE);
12832                   break;
12833                 case bfd_reloc_dangerous:
12834                   BFD_ASSERT (error_message != NULL);
12835                   (*link_info->callbacks->reloc_dangerous)
12836                     (link_info, error_message,
12837                      input_bfd, input_section, (*parent)->address);
12838                   break;
12839                 case bfd_reloc_overflow:
12840                   (*link_info->callbacks->reloc_overflow)
12841                     (link_info, NULL,
12842                      bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
12843                      (*parent)->howto->name, (*parent)->addend,
12844                      input_bfd, input_section, (*parent)->address);
12845                   break;
12846                 case bfd_reloc_outofrange:
12847                   /* PR ld/13730:
12848                      This error can result when processing some partially
12849                      complete binaries.  Do not abort, but issue an error
12850                      message instead.  */
12851                   link_info->callbacks->einfo
12852                     /* xgettext:c-format */
12853                     (_("%X%P: %B(%A): relocation \"%R\" goes out of range\n"),
12854                      abfd, input_section, * parent);
12855                   goto error_return;
12856
12857                 default:
12858                   abort ();
12859                   break;
12860                 }
12861             }
12862         }
12863     }
12864
12865   free (reloc_vector);
12866   return data;
12867
12868 error_return:
12869   free (reloc_vector);
12870   return NULL;
12871 }
12872 \f
12873 /* Link-time IFC relaxation.
12874    In this optimization, we chains jump instructions
12875    of the same destination with ifcall.  */
12876
12877
12878 /* List to save jal and j relocation.  */
12879 struct elf_nds32_ifc_symbol_entry
12880 {
12881   asection *sec;
12882   struct elf_link_hash_entry *h;
12883   struct elf_nds32_ifc_irel_list *irel_head;
12884   unsigned long insn;
12885   int times;
12886   int enable;           /* Apply ifc.  */
12887   int ex9_enable;       /* Apply ifc after ex9.  */
12888   struct elf_nds32_ifc_symbol_entry *next;
12889 };
12890
12891 struct elf_nds32_ifc_irel_list
12892 {
12893   Elf_Internal_Rela *irel;
12894   asection *sec;
12895   bfd_vma addr;
12896   /* If this is set, then it is the last instruction for
12897      ifc-chain, so it must be keep for the actual branching.  */
12898   int keep;
12899   struct elf_nds32_ifc_irel_list *next;
12900 };
12901
12902 static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;
12903
12904 /* Insert symbol of jal and j for ifc.  */
12905
12906 static void
12907 nds32_elf_ifc_insert_symbol (asection *sec,
12908                              struct elf_link_hash_entry *h,
12909                              Elf_Internal_Rela *irel,
12910                              unsigned long insn)
12911 {
12912   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
12913
12914   /* Check there is target of existing entry the same as the new one.  */
12915   while (ptr != NULL)
12916     {
12917       if (((h == NULL && ptr->sec == sec
12918             && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
12919             && ptr->irel_head->irel->r_addend == irel->r_addend)
12920            || h != NULL)
12921           && ptr->h == h
12922           && ptr->insn == insn)
12923         {
12924           /* The same target exist, so insert into list.  */
12925           struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;
12926
12927           while (irel_list->next != NULL)
12928             irel_list = irel_list->next;
12929           irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
12930           irel_list = irel_list->next;
12931           irel_list->irel = irel;
12932           irel_list->keep = 1;
12933
12934           if (h == NULL)
12935             irel_list->sec = NULL;
12936           else
12937             irel_list->sec = sec;
12938           irel_list->next = NULL;
12939           return;
12940         }
12941       if (ptr->next == NULL)
12942         break;
12943       ptr = ptr->next;
12944     }
12945
12946   /* There is no same target entry, so build a new one.  */
12947   if (ifc_symbol_head == NULL)
12948     {
12949       ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
12950       ptr = ifc_symbol_head;
12951     }
12952   else
12953     {
12954       ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
12955       ptr = ptr->next;
12956     }
12957
12958   ptr->h = h;
12959   ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
12960   ptr->irel_head->irel = irel;
12961   ptr->insn = insn;
12962   ptr->irel_head->keep = 1;
12963
12964   if (h == NULL)
12965     {
12966       /* Local symbols.  */
12967       ptr->sec = sec;
12968       ptr->irel_head->sec = NULL;
12969     }
12970   else
12971     {
12972       /* Global symbol.  */
12973       ptr->sec = NULL;
12974       ptr->irel_head->sec = sec;
12975     }
12976
12977   ptr->irel_head->next = NULL;
12978   ptr->times = 0;
12979   ptr->enable = 0;
12980   ptr->ex9_enable = 0;
12981   ptr->next = NULL;
12982 }
12983
12984 /* Gather all jal and j instructions.  */
12985
12986 static bfd_boolean
12987 nds32_elf_ifc_calc (struct bfd_link_info *info,
12988                     bfd *abfd, asection *sec)
12989 {
12990   Elf_Internal_Rela *internal_relocs;
12991   Elf_Internal_Rela *irelend;
12992   Elf_Internal_Rela *irel;
12993   Elf_Internal_Shdr *symtab_hdr;
12994   bfd_byte *contents = NULL;
12995   uint32_t insn, insn_with_reg;
12996   unsigned long r_symndx;
12997   struct elf_link_hash_entry *h;
12998   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
12999   struct elf_nds32_link_hash_table *table;
13000   bfd_boolean ifc_loop_aware;
13001
13002   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13003                                                TRUE /* keep_memory */);
13004   irelend = internal_relocs + sec->reloc_count;
13005   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13006
13007   /* Check if the object enable ifc.  */
13008   irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
13009                                  R_NDS32_RELAX_ENTRY);
13010
13011   if (irel == NULL
13012       || irel >= irelend
13013       || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
13014       || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
13015           && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
13016     return TRUE;
13017
13018   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
13019     return FALSE;
13020
13021   table = nds32_elf_hash_table (info);
13022   ifc_loop_aware = table->ifc_loop_aware;
13023   while (irel != NULL && irel < irelend)
13024     {
13025       /* Traverse all relocation and gather all of them to build the list.  */
13026
13027       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
13028         {
13029           if (ifc_loop_aware == 1
13030               && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
13031             {
13032               /* Check the region if loop or not.  If it is true and
13033                  ifc-loop-aware is true, ignore the region till region end.  */
13034               while (irel != NULL
13035                      && irel < irelend
13036                      && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
13037                          || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
13038                 irel++;
13039             }
13040         }
13041
13042       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
13043         {
13044           insn = bfd_getb32 (contents + irel->r_offset);
13045           nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
13046           r_symndx = ELF32_R_SYM (irel->r_info);
13047           if (r_symndx < symtab_hdr->sh_info)
13048             {
13049               /* Local symbol.  */
13050               nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
13051             }
13052           else
13053             {
13054               /* External symbol.  */
13055               h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13056               nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
13057             }
13058         }
13059       irel++;
13060     }
13061   return TRUE;
13062 }
13063
13064 /* Determine whether j and jal should be substituted.  */
13065
13066 static void
13067 nds32_elf_ifc_filter (struct bfd_link_info *info)
13068 {
13069   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13070   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13071   struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13072   struct elf_nds32_link_hash_table *table;
13073   int target_optimize;
13074   bfd_vma address;
13075
13076   table = nds32_elf_hash_table (info);
13077   target_optimize = table->target_optimize;
13078   while (ptr)
13079     {
13080       irel_ptr = ptr->irel_head;
13081       if (ptr->h == NULL)
13082         {
13083           /* Local symbol.  */
13084           irel_keeper = irel_ptr;
13085           while (irel_ptr && irel_ptr->next)
13086             {
13087               /* Check there is jump target can be used.  */
13088               if ((irel_ptr->next->irel->r_offset
13089                    - irel_keeper->irel->r_offset) > 1022)
13090                 irel_keeper = irel_ptr->next;
13091               else
13092                 {
13093                   ptr->enable = 1;
13094                   irel_ptr->keep = 0;
13095                 }
13096               irel_ptr = irel_ptr->next;
13097             }
13098         }
13099       else
13100         {
13101           /* Global symbol.  */
13102           /* We have to get the absolute address and decide
13103              whether to keep it or not.  */
13104           while (irel_ptr)
13105             {
13106               address = (irel_ptr->irel->r_offset
13107                          + irel_ptr->sec->output_section->vma
13108                          + irel_ptr->sec->output_offset);
13109               irel_ptr->addr = address;
13110               irel_ptr = irel_ptr->next;
13111             }
13112
13113           irel_ptr = ptr->irel_head;
13114           while (irel_ptr)
13115             {
13116               /* Sort by address.  */
13117               struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
13118               struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
13119               struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
13120               struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;
13121
13122               /* Get the smallest one.  */
13123               while (irel_temp->next)
13124                 {
13125                   if (irel_temp->next->addr < irel_dest->addr)
13126                     {
13127                       irel_dest_prev = irel_temp;
13128                       irel_dest = irel_temp->next;
13129                     }
13130                   irel_temp = irel_temp->next;
13131                 }
13132
13133               if (irel_dest != irel_ptr)
13134                 {
13135                   if (irel_ptr_prev)
13136                     irel_ptr_prev->next = irel_dest;
13137                   if (irel_dest_prev)
13138                     irel_dest_prev->next = irel_ptr;
13139                   irel_temp = irel_ptr->next;
13140                   irel_ptr->next = irel_dest->next;
13141                   irel_dest->next = irel_temp;
13142                 }
13143               irel_ptr_prev = irel_ptr;
13144               irel_ptr = irel_ptr->next;
13145             }
13146
13147           irel_ptr = ptr->irel_head;
13148           irel_keeper = irel_ptr;
13149           while (irel_ptr && irel_ptr->next)
13150             {
13151               if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
13152                 irel_keeper = irel_ptr->next;
13153               else
13154                 {
13155                   ptr->enable = 1;
13156                   irel_ptr->keep = 0;
13157                 }
13158               irel_ptr = irel_ptr->next;
13159             }
13160         }
13161
13162         /* Ex9 enable.  Reserve it for ex9.  */
13163       if ((target_optimize & NDS32_RELAX_EX9_ON)
13164           && ptr->irel_head != irel_keeper)
13165         ptr->enable = 0;
13166       ptr = ptr->next;
13167     }
13168 }
13169
13170 /* Determine whether j and jal should be substituted after ex9 done.  */
13171
13172 static void
13173 nds32_elf_ifc_filter_after_ex9 (void)
13174 {
13175   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13176   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13177
13178   while (ptr)
13179     {
13180       if (ptr->enable == 0)
13181         {
13182           /* Check whether ifc is applied or not.  */
13183           irel_ptr = ptr->irel_head;
13184           ptr->ex9_enable = 1;
13185           while (irel_ptr)
13186             {
13187               if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
13188                 {
13189                   /* Ex9 already.  */
13190                   ptr->ex9_enable = 0;
13191                   break;
13192                 }
13193               irel_ptr = irel_ptr->next;
13194             }
13195         }
13196       ptr = ptr->next;
13197     }
13198 }
13199
13200 /* Wrapper to do ifc relaxation.  */
13201
13202 bfd_boolean
13203 nds32_elf_ifc_finish (struct bfd_link_info *info)
13204 {
13205   int relax_status;
13206   struct elf_nds32_link_hash_table *table;
13207
13208   table = nds32_elf_hash_table (info);
13209   relax_status = table->relax_status;
13210
13211   if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
13212     nds32_elf_ifc_filter (info);
13213   else
13214     nds32_elf_ifc_filter_after_ex9 ();
13215
13216   if (!nds32_elf_ifc_replace (info))
13217     return FALSE;
13218
13219   if (table)
13220     table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
13221   return TRUE;
13222 }
13223
13224 /* Traverse the result of ifc filter and replace it with ifcall9.  */
13225
13226 static bfd_boolean
13227 nds32_elf_ifc_replace (struct bfd_link_info *info)
13228 {
13229   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13230   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13231   nds32_elf_blank_t *relax_blank_list = NULL;
13232   bfd_byte *contents = NULL;
13233   Elf_Internal_Rela *internal_relocs;
13234   Elf_Internal_Rela *irel;
13235   Elf_Internal_Rela *irelend;
13236   unsigned short insn16 = INSN_IFCALL9;
13237   struct elf_nds32_link_hash_table *table;
13238   int relax_status;
13239
13240   table = nds32_elf_hash_table (info);
13241   relax_status = table->relax_status;
13242
13243   while (ptr)
13244     {
13245       /* Traverse the ifc gather list, and replace the
13246          filter entries by ifcall9.  */
13247       if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
13248           || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE)
13249               && ptr->ex9_enable == 1))
13250         {
13251           irel_ptr = ptr->irel_head;
13252           if (ptr->h == NULL)
13253             {
13254               /* Local symbol.  */
13255               internal_relocs = _bfd_elf_link_read_relocs
13256                 (ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
13257               irelend = internal_relocs + ptr->sec->reloc_count;
13258
13259               if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13260                                                &contents, TRUE))
13261                 return FALSE;
13262
13263               while (irel_ptr)
13264                 {
13265                   if (irel_ptr->keep == 0 && irel_ptr->next)
13266                     {
13267                       /* The one can be replaced.  We have to check whether
13268                          there is any alignment point in the region.  */
13269                       irel = irel_ptr->irel;
13270                       while (((irel_ptr->next->keep == 0
13271                                && irel < irel_ptr->next->irel)
13272                               || (irel_ptr->next->keep == 1 && irel < irelend))
13273                              && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13274                                   && (irel->r_addend & 0x1f) == 2))
13275                         irel++;
13276                       if (irel >= irelend
13277                           || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13278                                && (irel->r_addend & 0x1f) == 2
13279                                && ((irel->r_offset - get_nds32_elf_blank_total
13280                                     (&relax_blank_list, irel->r_offset, 1))
13281                                    & 0x02) == 0))
13282                         {
13283                           /* Replace by ifcall9.  */
13284                           bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13285                           if (!insert_nds32_elf_blank_recalc_total
13286                               (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13287                             return FALSE;
13288                           irel_ptr->irel->r_info =
13289                             ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13290                                           R_NDS32_10IFCU_PCREL_RELA);
13291                         }
13292                     }
13293                   irel_ptr = irel_ptr->next;
13294                 }
13295
13296               /* Delete the redundant code.  */
13297               if (relax_blank_list)
13298                 {
13299                   nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
13300                                                  relax_blank_list);
13301                   relax_blank_list = NULL;
13302                 }
13303             }
13304           else
13305             {
13306               /* Global symbol.  */
13307               while (irel_ptr)
13308                 {
13309                   if (irel_ptr->keep == 0 && irel_ptr->next)
13310                     {
13311                       /* The one can be replaced, and we have to check
13312                          whether there is any alignment point in the region.  */
13313                       internal_relocs = _bfd_elf_link_read_relocs
13314                         (irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
13315                          TRUE /* keep_memory */);
13316                       irelend = internal_relocs + irel_ptr->sec->reloc_count;
13317                       if (!nds32_get_section_contents (irel_ptr->sec->owner,
13318                                                        irel_ptr->sec, &contents,
13319                                                        TRUE))
13320                         return FALSE;
13321
13322                       irel = irel_ptr->irel;
13323                       while (((irel_ptr->sec == irel_ptr->next->sec
13324                                && irel_ptr->next->keep == 0
13325                                && irel < irel_ptr->next->irel)
13326                               || ((irel_ptr->sec != irel_ptr->next->sec
13327                                    || irel_ptr->next->keep == 1)
13328                                   && irel < irelend))
13329                              && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13330                                   && (irel->r_addend & 0x1f) == 2))
13331                         irel++;
13332                       if (irel >= irelend
13333                           || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13334                                && (irel->r_addend & 0x1f) == 2
13335                                && ((irel->r_offset
13336                                     - get_nds32_elf_blank_total (&relax_blank_list,
13337                                                             irel->r_offset, 1)) & 0x02) == 0))
13338                         {
13339                           /* Replace by ifcall9.  */
13340                           bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13341                           if (!insert_nds32_elf_blank_recalc_total
13342                               (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13343                             return FALSE;
13344
13345                           /* Delete the redundant code, and clear the relocation.  */
13346                           nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
13347                                                          irel_ptr->sec,
13348                                                          relax_blank_list);
13349                           irel_ptr->irel->r_info =
13350                             ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13351                                           R_NDS32_10IFCU_PCREL_RELA);
13352                           relax_blank_list = NULL;
13353                         }
13354                     }
13355
13356                   irel_ptr = irel_ptr->next;
13357                 }
13358             }
13359         }
13360       ptr = ptr->next;
13361     }
13362
13363   return TRUE;
13364 }
13365
13366 /* Relocate ifcall.  */
13367
13368 static bfd_boolean
13369 nds32_elf_ifc_reloc (void)
13370 {
13371   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13372   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13373   struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13374   bfd_vma relocation, address;
13375   unsigned short insn16;
13376   bfd_byte *contents = NULL;
13377   static bfd_boolean done = FALSE;
13378
13379   if (done)
13380     return TRUE;
13381
13382   done = TRUE;
13383
13384   while (ptr)
13385     {
13386       /* Check the entry is enable ifcall.  */
13387       if (ptr->enable == 1 || ptr->ex9_enable == 1)
13388         {
13389           /* Get the reserve jump.  */
13390           irel_ptr = ptr->irel_head;
13391           while (irel_ptr)
13392             {
13393               if (irel_ptr->keep == 1)
13394                 {
13395                   irel_keeper = irel_ptr;
13396                   break;
13397                 }
13398               irel_ptr = irel_ptr->next;
13399             }
13400
13401           irel_ptr = ptr->irel_head;
13402           if (ptr->h == NULL)
13403             {
13404               /* Local symbol.  */
13405               if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13406                                                &contents, TRUE))
13407                 return FALSE;
13408
13409               while (irel_ptr)
13410                 {
13411                   if (irel_ptr->keep == 0
13412                       && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13413                     {
13414                       relocation = irel_keeper->irel->r_offset;
13415                       relocation = relocation - irel_ptr->irel->r_offset;
13416                       while (irel_keeper && relocation > 1022)
13417                         {
13418                           irel_keeper = irel_keeper->next;
13419                           if (irel_keeper && irel_keeper->keep == 1)
13420                             {
13421                               relocation = irel_keeper->irel->r_offset;
13422                               relocation = relocation - irel_ptr->irel->r_offset;
13423                             }
13424                         }
13425                       if (relocation > 1022)
13426                         {
13427                           /* Double check.  */
13428                           irel_keeper = ptr->irel_head;
13429                           while (irel_keeper)
13430                             {
13431                               if (irel_keeper->keep == 1)
13432                                 {
13433                                   relocation = irel_keeper->irel->r_offset;
13434                                   relocation = relocation - irel_ptr->irel->r_offset;
13435                                 }
13436                               if (relocation <= 1022)
13437                                 break;
13438                               irel_keeper = irel_keeper->next;
13439                             }
13440                           if (!irel_keeper)
13441                             return FALSE;
13442                         }
13443                       irel_ptr->irel->r_info =
13444                         ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13445                                       R_NDS32_NONE);
13446                       insn16 = INSN_IFCALL9 | (relocation >> 1);
13447                       bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13448                     }
13449                   irel_ptr = irel_ptr->next;
13450                 }
13451             }
13452           else
13453             {
13454               /* Global symbol.  */
13455               while (irel_ptr)
13456                 {
13457                   if (irel_ptr->keep == 0
13458                       && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13459                     {
13460                       /* Get the distance between ifcall and jump.  */
13461                       relocation = (irel_keeper->irel->r_offset
13462                                     + irel_keeper->sec->output_section->vma
13463                                     + irel_keeper->sec->output_offset);
13464                       address = (irel_ptr->irel->r_offset
13465                                  + irel_ptr->sec->output_section->vma
13466                                  + irel_ptr->sec->output_offset);
13467                       relocation = relocation - address;
13468
13469                       /* The distance is over ragne, find callee again.  */
13470                       while (irel_keeper && relocation > 1022)
13471                         {
13472                           irel_keeper = irel_keeper->next;
13473                           if (irel_keeper && irel_keeper->keep ==1)
13474                             {
13475                               relocation = (irel_keeper->irel->r_offset
13476                                             + irel_keeper->sec->output_section->vma
13477                                             + irel_keeper->sec->output_offset);
13478                               relocation = relocation - address;
13479                             }
13480                         }
13481
13482                       if (relocation > 1022)
13483                         {
13484                           /* Double check.  */
13485                           irel_keeper = ptr->irel_head;
13486                           while (irel_keeper)
13487                             {
13488                               if (irel_keeper->keep == 1)
13489                                 {
13490
13491                                   relocation = (irel_keeper->irel->r_offset
13492                                                 + irel_keeper->sec->output_section->vma
13493                                                 + irel_keeper->sec->output_offset);
13494                                   relocation = relocation - address;
13495                                 }
13496                               if (relocation <= 1022)
13497                                 break;
13498                               irel_keeper = irel_keeper->next;
13499                             }
13500                           if (!irel_keeper)
13501                             return FALSE;
13502                         }
13503                       if (!nds32_get_section_contents
13504                           (irel_ptr->sec->owner, irel_ptr->sec, &contents, TRUE))
13505                         return FALSE;
13506                       insn16 = INSN_IFCALL9 | (relocation >> 1);
13507                       bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13508                       irel_ptr->irel->r_info =
13509                         ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13510                                       R_NDS32_NONE);
13511                     }
13512                   irel_ptr =irel_ptr->next;
13513                 }
13514             }
13515         }
13516       ptr = ptr->next;
13517     }
13518
13519   return TRUE;
13520 }
13521
13522 /* End of IFC relaxation.  */
13523 \f
13524 /* EX9 Instruction Table Relaxation.  */
13525
13526 /* Global hash list.  */
13527 struct elf_link_hash_entry_list
13528 {
13529   struct elf_link_hash_entry *h;
13530   struct elf_link_hash_entry_list *next;
13531 };
13532
13533 /* Save different destination but same insn.  */
13534 struct elf_link_hash_entry_mul_list
13535 {
13536   /* Global symbol times.  */
13537   int times;
13538   /* Save relocation for each global symbol but useful??  */
13539   Elf_Internal_Rela *irel;
13540   /* For sethi, two sethi may have the same high-part but different low-parts.  */
13541   Elf_Internal_Rela rel_backup;
13542   struct elf_link_hash_entry_list *h_list;
13543   struct elf_link_hash_entry_mul_list *next;
13544 };
13545
13546 /* Instruction hash table.  */
13547 struct elf_nds32_code_hash_entry
13548 {
13549   struct bfd_hash_entry root;
13550   int times;
13551   /* For insn that can use relocation or constant ex: sethi.  */
13552   int const_insn;
13553   asection *sec;
13554   struct elf_link_hash_entry_mul_list *m_list;
13555   /* Using r_addend.  */
13556   Elf_Internal_Rela *irel;
13557   /* Using r_info.  */
13558   Elf_Internal_Rela rel_backup;
13559 };
13560
13561 /* Instruction count list.  */
13562 struct elf_nds32_insn_times_entry
13563 {
13564   const char *string;
13565   int times;
13566   int order;
13567   asection *sec;
13568   struct elf_link_hash_entry_mul_list *m_list;
13569   Elf_Internal_Rela *irel;
13570   Elf_Internal_Rela rel_backup;
13571   struct elf_nds32_insn_times_entry *next;
13572 };
13573
13574 /* J and JAL symbol list.  */
13575 struct elf_nds32_symbol_entry
13576 {
13577   char *string;
13578   unsigned long insn;
13579   struct elf_nds32_symbol_entry *next;
13580 };
13581
13582 /* Relocation list.  */
13583 struct elf_nds32_irel_entry
13584 {
13585   Elf_Internal_Rela *irel;
13586   struct elf_nds32_irel_entry *next;
13587 };
13588
13589 /* ex9.it insn need to be fixed.  */
13590 struct elf_nds32_ex9_refix
13591 {
13592   Elf_Internal_Rela *irel;
13593   asection *sec;
13594   struct elf_link_hash_entry *h;
13595   int order;
13596   struct elf_nds32_ex9_refix *next;
13597 };
13598
13599 static struct bfd_hash_table ex9_code_table;
13600 static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
13601 static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;
13602
13603 /* EX9 hash function.  */
13604
13605 static struct bfd_hash_entry *
13606 nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
13607                              struct bfd_hash_table *table,
13608                              const char *string)
13609 {
13610   struct elf_nds32_code_hash_entry *ret;
13611
13612   /* Allocate the structure if it has not already been allocated by a
13613      subclass.  */
13614   if (entry == NULL)
13615     {
13616       entry = (struct bfd_hash_entry *)
13617         bfd_hash_allocate (table, sizeof (*ret));
13618       if (entry == NULL)
13619         return entry;
13620     }
13621
13622   /* Call the allocation method of the superclass.  */
13623   entry = bfd_hash_newfunc (entry, table, string);
13624   if (entry == NULL)
13625     return entry;
13626
13627   ret = (struct elf_nds32_code_hash_entry*) entry;
13628   ret->times = 0;
13629   ret->const_insn = 0;
13630   ret->m_list = NULL;
13631   ret->sec = NULL;
13632   ret->irel = NULL;
13633   return &ret->root;
13634 }
13635
13636 /* Insert ex9 entry
13637    this insert must be stable sorted by times.  */
13638
13639 static void
13640 nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
13641 {
13642   struct elf_nds32_insn_times_entry *temp;
13643   struct elf_nds32_insn_times_entry *temp2;
13644
13645   if (ex9_insn_head == NULL)
13646     {
13647       ex9_insn_head = ptr;
13648       ptr->next = NULL;
13649     }
13650   else
13651     {
13652       temp = ex9_insn_head;
13653       temp2 = ex9_insn_head;
13654       while (temp->next &&
13655              (temp->next->times >= ptr->times
13656               || temp->times == -1))
13657         {
13658           if (temp->times == -1)
13659             temp2 = temp;
13660           temp = temp->next;
13661         }
13662       if (ptr->times > temp->times && temp->times != -1)
13663         {
13664           ptr->next = temp;
13665           if (temp2->times == -1)
13666             temp2->next = ptr;
13667           else
13668             ex9_insn_head = ptr;
13669         }
13670       else if (temp->next == NULL)
13671         {
13672           temp->next = ptr;
13673           ptr->next = NULL;
13674         }
13675       else
13676         {
13677           ptr->next = temp->next;
13678           temp->next = ptr;
13679         }
13680     }
13681 }
13682
13683 /* Examine each insn times in hash table.
13684    Handle multi-link hash entry.
13685
13686    TODO: This function doesn't assign so much info since it is fake.  */
13687
13688 static int
13689 nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
13690 {
13691   struct elf_nds32_insn_times_entry *ptr;
13692   int times;
13693
13694   if (h->m_list == NULL)
13695     {
13696       /* Local symbol insn or insn without relocation.  */
13697       if (h->times < 3)
13698         return TRUE;
13699
13700       ptr = (struct elf_nds32_insn_times_entry *)
13701         bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13702       ptr->times = h->times;
13703       ptr->string = h->root.string;
13704       ptr->m_list = NULL;
13705       ptr->sec = h->sec;
13706       ptr->irel = h->irel;
13707       ptr->rel_backup = h->rel_backup;
13708       nds32_elf_ex9_insert_entry (ptr);
13709     }
13710   else
13711     {
13712       /* Global symbol insn.  */
13713       /* Only sethi insn has multiple m_list.  */
13714       struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13715
13716       times = 0;
13717       while (m_list)
13718         {
13719           times += m_list->times;
13720           m_list = m_list->next;
13721         }
13722       if (times >= 3)
13723         {
13724           m_list = h->m_list;
13725           ptr = (struct elf_nds32_insn_times_entry *)
13726             bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13727           ptr->times = times; /* Use the total times.  */
13728           ptr->string = h->root.string;
13729           ptr->m_list = m_list;
13730           ptr->sec = h->sec;
13731           ptr->irel = m_list->irel;
13732           ptr->rel_backup = m_list->rel_backup;
13733           nds32_elf_ex9_insert_entry (ptr);
13734         }
13735       if (h->const_insn == 1)
13736         {
13737           /* sethi with constant value.  */
13738           if (h->times < 3)
13739             return TRUE;
13740
13741           ptr = (struct elf_nds32_insn_times_entry *)
13742             bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13743           ptr->times = h->times;
13744           ptr->string = h->root.string;
13745           ptr->m_list = NULL;
13746           ptr->sec = NULL;
13747           ptr->irel = NULL;
13748           ptr->rel_backup = h->rel_backup;
13749           nds32_elf_ex9_insert_entry (ptr);
13750         }
13751     }
13752   return TRUE;
13753 }
13754
13755 /* Count each insn times in hash table.
13756    Handle multi-link hash entry.  */
13757
13758 static int
13759 nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
13760 {
13761   int reservation, times;
13762   unsigned long relocation, min_relocation;
13763   struct elf_nds32_insn_times_entry *ptr;
13764
13765   if (h->m_list == NULL)
13766     {
13767       /* Local symbol insn or insn without relocation.  */
13768       if (h->times < 3)
13769         return TRUE;
13770       ptr = (struct elf_nds32_insn_times_entry *)
13771         bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13772       ptr->times = h->times;
13773       ptr->string = h->root.string;
13774       ptr->m_list = NULL;
13775       ptr->sec = h->sec;
13776       ptr->irel = h->irel;
13777       ptr->rel_backup = h->rel_backup;
13778       nds32_elf_ex9_insert_entry (ptr);
13779     }
13780   else
13781     {
13782       /* Global symbol insn.  */
13783       /* Only sethi insn has multiple m_list.  */
13784       struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13785
13786       if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
13787           && m_list->next != NULL)
13788         {
13789           /* Sethi insn has different symbol or addend but has same hi20.  */
13790           times = 0;
13791           reservation = 1;
13792           relocation = 0;
13793           min_relocation = 0xffffffff;
13794           while (m_list)
13795             {
13796               /* Get the minimum sethi address
13797                  and calculate how many entry the sethi-list have to use.  */
13798               if ((m_list->h_list->h->root.type == bfd_link_hash_defined
13799                    || m_list->h_list->h->root.type == bfd_link_hash_defweak)
13800                   && (m_list->h_list->h->root.u.def.section != NULL
13801                       && m_list->h_list->h->root.u.def.section->output_section != NULL))
13802                 {
13803                   relocation = (m_list->h_list->h->root.u.def.value +
13804                                 m_list->h_list->h->root.u.def.section->output_section->vma +
13805                                 m_list->h_list->h->root.u.def.section->output_offset);
13806                   relocation += m_list->irel->r_addend;
13807                 }
13808               else
13809                 relocation = 0;
13810               if (relocation < min_relocation)
13811                 min_relocation = relocation;
13812               times += m_list->times;
13813               m_list = m_list->next;
13814             }
13815           if (min_relocation < ex9_relax_size)
13816             reservation = (min_relocation >> 12) + 1;
13817           else
13818             reservation = (min_relocation >> 12)
13819                           - ((min_relocation - ex9_relax_size) >> 12) + 1;
13820           if (reservation < (times / 3))
13821             {
13822               /* Efficient enough to use ex9.  */
13823               int i;
13824
13825               for (i = reservation ; i > 0; i--)
13826                 {
13827                   /* Allocate number of reservation ex9 entry.  */
13828                   ptr = (struct elf_nds32_insn_times_entry *)
13829                     bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13830                   ptr->times = h->m_list->times / reservation;
13831                   ptr->string = h->root.string;
13832                   ptr->m_list = h->m_list;
13833                   ptr->sec = h->sec;
13834                   ptr->irel = h->m_list->irel;
13835                   ptr->rel_backup = h->m_list->rel_backup;
13836                   nds32_elf_ex9_insert_entry (ptr);
13837                 }
13838             }
13839         }
13840       else
13841         {
13842           /* Normal global symbol that means no different address symbol
13843              using same ex9 entry.  */
13844           if (m_list->times >= 3)
13845             {
13846               ptr = (struct elf_nds32_insn_times_entry *)
13847                 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13848               ptr->times = m_list->times;
13849               ptr->string = h->root.string;
13850               ptr->m_list = h->m_list;
13851               ptr->sec = h->sec;
13852               ptr->irel = h->m_list->irel;
13853               ptr->rel_backup = h->m_list->rel_backup;
13854               nds32_elf_ex9_insert_entry (ptr);
13855             }
13856         }
13857
13858       if (h->const_insn == 1)
13859         {
13860           /* sethi with constant value.  */
13861           if (h->times < 3)
13862             return TRUE;
13863
13864           ptr = (struct elf_nds32_insn_times_entry *)
13865             bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13866           ptr->times = h->times;
13867           ptr->string = h->root.string;
13868           ptr->m_list = NULL;
13869           ptr->sec = NULL;
13870           ptr->irel = NULL;
13871           ptr->rel_backup = h->rel_backup;
13872           nds32_elf_ex9_insert_entry (ptr);
13873         }
13874     }
13875
13876   return TRUE;
13877 }
13878
13879 /* Hash table traverse function.  */
13880
13881 static void
13882 nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
13883 {
13884   unsigned int i;
13885
13886   ex9_code_table.frozen = 1;
13887   for (i = 0; i < ex9_code_table.size; i++)
13888     {
13889       struct bfd_hash_entry *p;
13890
13891       for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
13892         if (!func ((struct elf_nds32_code_hash_entry *) p))
13893           goto out;
13894     }
13895 out:
13896   ex9_code_table.frozen = 0;
13897 }
13898
13899
13900 /* Give order number to insn list.  */
13901
13902 static void
13903 nds32_elf_order_insn_times (struct bfd_link_info *info)
13904 {
13905   struct elf_nds32_insn_times_entry *ex9_insn;
13906   struct elf_nds32_insn_times_entry *temp = NULL;
13907   struct elf_nds32_link_hash_table *table;
13908   int ex9_limit;
13909   int number = 0;
13910
13911   if (ex9_insn_head == NULL)
13912     return;
13913
13914 /* The max number of entries is 512.  */
13915   ex9_insn = ex9_insn_head;
13916   table = nds32_elf_hash_table (info);
13917   ex9_limit = table->ex9_limit;
13918
13919   ex9_insn = ex9_insn_head;
13920
13921   while (ex9_insn != NULL && number < ex9_limit)
13922     {
13923       ex9_insn->order = number;
13924       number++;
13925       temp = ex9_insn;
13926       ex9_insn = ex9_insn->next;
13927     }
13928
13929   if (ex9_insn && temp)
13930     temp->next = NULL;
13931
13932   while (ex9_insn != NULL)
13933     {
13934       /* Free useless entry.  */
13935       temp = ex9_insn;
13936       ex9_insn = ex9_insn->next;
13937       free (temp);
13938     }
13939 }
13940
13941 /* Build .ex9.itable section.  */
13942
13943 static void
13944 nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
13945 {
13946   asection *table_sec;
13947   struct elf_nds32_insn_times_entry *ptr;
13948   bfd *it_abfd;
13949   int number = 0;
13950   bfd_byte *contents = NULL;
13951
13952   for (it_abfd = link_info->input_bfds; it_abfd != NULL;
13953        it_abfd = it_abfd->link.next)
13954     {
13955       /* Find the section .ex9.itable, and put all entries into it.  */
13956       table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
13957       if (table_sec != NULL)
13958         {
13959           if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
13960             return;
13961
13962           for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
13963             number++;
13964
13965           table_sec->size = number * 4;
13966
13967           if (number == 0)
13968             return;
13969
13970           elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
13971           number = 0;
13972           for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
13973             {
13974               long val;
13975
13976               val = strtol (ptr->string, NULL, 16);
13977               bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
13978               number++;
13979             }
13980           break;
13981         }
13982     }
13983 }
13984
13985 /* Get insn with regs according to relocation type.  */
13986
13987 static void
13988 nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
13989                              uint32_t insn, uint32_t *insn_with_reg)
13990 {
13991   reloc_howto_type *howto = NULL;
13992
13993   if (irel == NULL
13994       || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
13995           && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
13996              >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
13997     {
13998       *insn_with_reg = insn;
13999       return;
14000     }
14001
14002   howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14003   *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
14004 }
14005
14006 /* Mask number of address bits according to relocation.  */
14007
14008 static unsigned long
14009 nds32_elf_irel_mask (Elf_Internal_Rela *irel)
14010 {
14011   reloc_howto_type *howto = NULL;
14012
14013   if (irel == NULL
14014       || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
14015           && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
14016              >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
14017     return 0;
14018
14019   howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14020   return howto->dst_mask;
14021 }
14022
14023 static void
14024 nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
14025                              struct elf_nds32_irel_entry *irel_ptr)
14026 {
14027   if (*irel_list == NULL)
14028     {
14029       *irel_list = irel_ptr;
14030       irel_ptr->next = NULL;
14031     }
14032   else
14033     {
14034       irel_ptr->next = *irel_list;
14035       *irel_list = irel_ptr;
14036     }
14037 }
14038
14039 static void
14040 nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
14041                           struct elf_link_hash_entry *h, int order)
14042 {
14043   struct elf_nds32_ex9_refix *ptr;
14044
14045   ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
14046   ptr->sec = sec;
14047   ptr->irel = irel;
14048   ptr->h = h;
14049   ptr->order = order;
14050   ptr->next = NULL;
14051
14052   if (ex9_refix_head == NULL)
14053     ex9_refix_head = ptr;
14054   else
14055     {
14056       struct elf_nds32_ex9_refix *temp = ex9_refix_head;
14057
14058       while (temp->next != NULL)
14059         temp = temp->next;
14060       temp->next = ptr;
14061     }
14062 }
14063
14064 enum
14065 {
14066   DATA_EXIST = 1,
14067   CLEAN_PRE = 1 << 1,
14068   PUSH_PRE = 1 << 2
14069 };
14070
14071 /* Check relocation type if supporting for ex9.  */
14072
14073 static int
14074 nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
14075                                 Elf_Internal_Rela **irel,
14076                                 Elf_Internal_Rela *irelend,
14077                                 nds32_elf_blank_t *relax_blank_list,
14078                                 asection *sec,bfd_vma *off,
14079                                 bfd_byte *contents)
14080 {
14081   /* Suppress ex9 if `.no_relax ex9' or inner loop.  */
14082   bfd_boolean nested_ex9, nested_loop;
14083   bfd_boolean ex9_loop_aware;
14084   /* We use the highest 1 byte of result to record
14085      how many bytes location counter has to move.  */
14086   int result = 0;
14087   Elf_Internal_Rela *irel_save = NULL;
14088   struct elf_nds32_link_hash_table *table;
14089
14090   table = nds32_elf_hash_table (info);
14091   ex9_loop_aware = table->ex9_loop_aware;
14092
14093   while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
14094     {
14095       switch (ELF32_R_TYPE ((*irel)->r_info))
14096         {
14097         case R_NDS32_RELAX_REGION_BEGIN:
14098           /* Ignore code block.  */
14099           nested_ex9 = FALSE;
14100           nested_loop = FALSE;
14101           if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
14102               || (ex9_loop_aware
14103                   && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
14104             {
14105               /* Check the region if loop or not.  If it is true and
14106                  ex9-loop-aware is true, ignore the region till region end.  */
14107               /* To save the status for in .no_relax ex9 region and
14108                  loop region to conform the block can do ex9 relaxation.  */
14109               nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
14110               nested_loop = (ex9_loop_aware
14111                              && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
14112               while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
14113                 {
14114                   (*irel)++;
14115                   if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
14116                     {
14117                       /* There may be nested region.  */
14118                       if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14119                         nested_ex9 = TRUE;
14120                       else if (ex9_loop_aware
14121                                && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14122                         nested_loop = TRUE;
14123                     }
14124                   else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
14125                     {
14126                       /* The end of region.  */
14127                       if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14128                         nested_ex9 = FALSE;
14129                       else if (ex9_loop_aware
14130                                && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14131                         nested_loop = FALSE;
14132                     }
14133                   else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
14134                            && ((*irel)->r_addend & 0x1f) == 2)
14135                     {
14136                       /* Alignment exist in the region.  */
14137                       result |= CLEAN_PRE;
14138                       if (((*irel)->r_offset -
14139                            get_nds32_elf_blank_total (&relax_blank_list,
14140                                                       (*irel)->r_offset, 0)) & 0x02)
14141                         result |= PUSH_PRE;
14142                     }
14143                 }
14144               if ((*irel) >= irelend)
14145                 *off = sec->size;
14146               else
14147                 *off = (*irel)->r_offset;
14148
14149               /* The final instruction in the region, regard this one as data to ignore it.  */
14150               result |= DATA_EXIST;
14151               return result;
14152             }
14153           break;
14154
14155         case R_NDS32_LABEL:
14156           if (((*irel)->r_addend & 0x1f) == 2)
14157             {
14158               /* Check this point is align and decide to do ex9 or not.  */
14159               result |= CLEAN_PRE;
14160               if (((*irel)->r_offset -
14161                    get_nds32_elf_blank_total (&relax_blank_list,
14162                                               (*irel)->r_offset, 0)) & 0x02)
14163                 result |= PUSH_PRE;
14164             }
14165           break;
14166         case R_NDS32_32_RELA:
14167           /* Data.  */
14168           result |= (4 << 24);
14169           result |= DATA_EXIST;
14170           break;
14171         case R_NDS32_16_RELA:
14172           /* Data.  */
14173           result |= (2 << 24);
14174           result |= DATA_EXIST;
14175           break;
14176         case R_NDS32_DATA:
14177           /* Data.  */
14178           /* The least code alignment is 2.  If the data is only one byte,
14179              we have to shift one more byte.  */
14180           if ((*irel)->r_addend == 1)
14181             result |= ((*irel)->r_addend << 25) ;
14182           else
14183             result |= ((*irel)->r_addend << 24) ;
14184
14185           result |= DATA_EXIST;
14186           break;
14187
14188         case R_NDS32_25_PCREL_RELA:
14189         case R_NDS32_SDA16S3_RELA:
14190         case R_NDS32_SDA15S3_RELA:
14191         case R_NDS32_SDA15S3:
14192         case R_NDS32_SDA17S2_RELA:
14193         case R_NDS32_SDA15S2_RELA:
14194         case R_NDS32_SDA12S2_SP_RELA:
14195         case R_NDS32_SDA12S2_DP_RELA:
14196         case R_NDS32_SDA15S2:
14197         case R_NDS32_SDA18S1_RELA:
14198         case R_NDS32_SDA15S1_RELA:
14199         case R_NDS32_SDA15S1:
14200         case R_NDS32_SDA19S0_RELA:
14201         case R_NDS32_SDA15S0_RELA:
14202         case R_NDS32_SDA15S0:
14203         case R_NDS32_HI20_RELA:
14204         case R_NDS32_LO12S0_ORI_RELA:
14205         case R_NDS32_LO12S0_RELA:
14206         case R_NDS32_LO12S1_RELA:
14207         case R_NDS32_LO12S2_RELA:
14208           /* These relocation is supported ex9 relaxation currently.  */
14209           /* We have to save the relocation for using later, since we have
14210              to check there is any alignment in the same address.  */
14211           irel_save = *irel;
14212           break;
14213         default:
14214           /* Not support relocations.  */
14215           if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
14216               && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE
14217               && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_INSN16)
14218             {
14219               /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
14220                  But we have to consider if there is any side-effect.  */
14221               if (!(result & DATA_EXIST))
14222                 {
14223                   /* We have to confirm there is no data relocation in the
14224                      same address.  In general case, this won't happen.  */
14225                   /* We have to do ex9 conservative, for those relocation not
14226                      considerd we ignore instruction.  */
14227                   result |= DATA_EXIST;
14228                   if (*(contents + *off) & 0x80)
14229                     result |= (2 << 24);
14230                   else
14231                     result |= (4 << 24);
14232                   break;
14233                 }
14234             }
14235         }
14236       if ((*irel) < irelend
14237           && ((*irel) + 1) < irelend
14238           && (*irel)->r_offset == ((*irel) + 1)->r_offset)
14239         /* There are relocations pointing to the same address, we have to
14240            check all of them.  */
14241         (*irel)++;
14242       else
14243         {
14244           if (irel_save)
14245             *irel = irel_save;
14246           return result;
14247         }
14248     }
14249   return result;
14250 }
14251
14252 /* Replace with ex9 instruction.  */
14253
14254 static bfd_boolean
14255 nds32_elf_ex9_push_insn (uint16_t insn16, bfd_byte *contents, bfd_vma pre_off,
14256                          nds32_elf_blank_t **relax_blank_list,
14257                          struct elf_nds32_irel_entry *pre_irel_ptr,
14258                          struct elf_nds32_irel_entry **irel_list)
14259 {
14260   if (insn16 != 0)
14261     {
14262       /* Implement the ex9 relaxation.  */
14263       bfd_putb16 (insn16, contents + pre_off);
14264       if (!insert_nds32_elf_blank_recalc_total (relax_blank_list,
14265                                                 pre_off + 2, 2))
14266         return FALSE;
14267       if (pre_irel_ptr != NULL)
14268         nds32_elf_insert_irel_entry (irel_list, pre_irel_ptr);
14269     }
14270   return TRUE;
14271 }
14272
14273 /* Replace input file instruction which is in ex9 itable.  */
14274
14275 static bfd_boolean
14276 nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
14277 {
14278   struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14279   bfd_byte *contents = NULL;
14280   bfd_vma off;
14281   uint16_t insn16, insn_ex9;
14282   /* `pre_*' are used to track previous instruction that can use ex9.it.  */
14283   bfd_vma pre_off = -1;
14284   uint16_t pre_insn16 = 0;
14285   struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
14286   Elf_Internal_Rela *internal_relocs;
14287   Elf_Internal_Rela *irel;
14288   Elf_Internal_Rela *irelend;
14289   Elf_Internal_Shdr *symtab_hdr;
14290   Elf_Internal_Sym *isym = NULL;
14291   nds32_elf_blank_t *relax_blank_list = NULL;
14292   uint32_t insn = 0;
14293   uint32_t insn_with_reg = 0;
14294   uint32_t it_insn;
14295   uint32_t it_insn_with_reg;
14296   unsigned long r_symndx;
14297   asection *isec;
14298   struct elf_nds32_irel_entry *irel_list = NULL;
14299   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
14300   int data_flag, do_replace, save_irel;
14301   struct elf_link_hash_entry_list *h_list;
14302
14303
14304   /* Load section instructions, relocations, and symbol table.  */
14305   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
14306       || !nds32_get_local_syms (abfd, sec, &isym))
14307     return FALSE;
14308   internal_relocs =
14309     _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, TRUE /* keep_memory */);
14310   irelend = internal_relocs + sec->reloc_count;
14311   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14312
14313   off = 0;
14314
14315   /* Check if the object enable ex9.  */
14316   irel = find_relocs_at_address (internal_relocs, internal_relocs,
14317                                  irelend, R_NDS32_RELAX_ENTRY);
14318
14319   /* Check this section trigger ex9 relaxation.  */
14320   if (irel == NULL
14321       || irel >= irelend
14322       || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
14323       || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
14324           && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
14325     return TRUE;
14326
14327   irel = internal_relocs;
14328
14329   /* Check alignment and fetch proper relocation.  */
14330   while (off < sec->size)
14331     {
14332       struct elf_link_hash_entry *h = NULL;
14333       struct elf_nds32_irel_entry *irel_ptr = NULL;
14334
14335       /* Syn the instruction and the relocation.  */
14336       while (irel != NULL && irel < irelend && irel->r_offset < off)
14337         irel++;
14338
14339       data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
14340                                                   relax_blank_list, sec,
14341                                                   &off, contents);
14342       if (data_flag & PUSH_PRE)
14343         if (!nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14344                                       &relax_blank_list, pre_irel_ptr,
14345                                       &irel_list))
14346           return FALSE;
14347
14348       if (data_flag & CLEAN_PRE)
14349         {
14350           pre_off = 0;
14351           pre_insn16 = 0;
14352           pre_irel_ptr = NULL;
14353         }
14354       if (data_flag & DATA_EXIST)
14355         {
14356           /* We save the move offset in the highest byte.  */
14357           off += (data_flag >> 24);
14358           continue;
14359         }
14360
14361       if (*(contents + off) & 0x80)
14362         {
14363           /* 2-byte instruction.  */
14364           off += 2;
14365           continue;
14366         }
14367
14368       /* Load the instruction and its opcode with register for comparing.  */
14369       ex9_insn = ex9_insn_head;
14370       insn = bfd_getb32 (contents + off);
14371       insn_with_reg = 0;
14372       while (ex9_insn)
14373         {
14374           it_insn = strtol (ex9_insn->string, NULL, 16);
14375           it_insn_with_reg = 0;
14376           do_replace = 0;
14377           save_irel = 0;
14378
14379           if (irel != NULL && irel < irelend && irel->r_offset == off)
14380             {
14381               /* Insn with relocation.  */
14382               nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
14383
14384               if (ex9_insn->irel != NULL)
14385                 nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn,
14386                                              &it_insn_with_reg);
14387
14388               if (ex9_insn->irel != NULL
14389                   && (ELF32_R_TYPE (irel->r_info) ==
14390                       ELF32_R_TYPE (ex9_insn->irel->r_info))
14391                   && (insn_with_reg == it_insn_with_reg))
14392                 {
14393                   /* Insn relocation and format is the same as table entry.  */
14394
14395                   if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
14396                       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
14397                       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
14398                       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
14399                       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
14400                       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
14401                           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
14402                       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
14403                           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
14404                       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
14405                           && ELF32_R_TYPE (irel->r_info) <=
14406                           R_NDS32_SDA12S2_SP_RELA)
14407                       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
14408                           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
14409                     {
14410                       r_symndx = ELF32_R_SYM (irel->r_info);
14411                       if (r_symndx < symtab_hdr->sh_info)
14412                         {
14413                           /* Local symbol.  */
14414                           int shndx = isym[r_symndx].st_shndx;
14415
14416                           isec = elf_elfsections (abfd)[shndx]->bfd_section;
14417                           if (ex9_insn->sec == isec
14418                               && ex9_insn->irel->r_addend == irel->r_addend
14419                               && ex9_insn->irel->r_info == irel->r_info)
14420                             {
14421                               do_replace = 1;
14422                               save_irel = 1;
14423                             }
14424                         }
14425                       else
14426                         {
14427                           /* External symbol.  */
14428                           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14429                           if (ex9_insn->m_list)
14430                             {
14431                               h_list = ex9_insn->m_list->h_list;
14432                               while (h_list)
14433                                 {
14434                                   if (h == h_list->h
14435                                       && (ex9_insn->m_list->irel->r_addend ==
14436                                           irel->r_addend))
14437                                     {
14438                                       do_replace = 1;
14439                                       save_irel = 1;
14440                                       break;
14441                                     }
14442                                   h_list = h_list->next;
14443                                 }
14444                             }
14445                         }
14446                     }
14447                   else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
14448                     {
14449                       r_symndx = ELF32_R_SYM (irel->r_info);
14450                       if (r_symndx < symtab_hdr->sh_info)
14451                         {
14452                           /* Local symbols.  Compare its base symbol and offset.  */
14453                           int shndx = isym[r_symndx].st_shndx;
14454
14455                           isec = elf_elfsections (abfd)[shndx]->bfd_section;
14456                           if (ex9_insn->sec == isec
14457                               && ex9_insn->irel->r_addend == irel->r_addend
14458                               && ex9_insn->irel->r_info == irel->r_info)
14459                             {
14460                               do_replace = 1;
14461                               save_irel = 1;
14462                             }
14463                         }
14464                       else
14465                         {
14466                           /* External symbol.  */
14467                           struct elf_link_hash_entry_mul_list *m_list;
14468
14469                           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14470                           m_list = ex9_insn->m_list;
14471
14472                           while (m_list)
14473                             {
14474                               h_list = m_list->h_list;
14475
14476                               while (h_list)
14477                                 {
14478                                   if (h == h_list->h
14479                                       && (m_list->irel->r_addend
14480                                           == irel->r_addend))
14481                                     {
14482                                       do_replace = 1;
14483                                       save_irel = 1;
14484                                       if (ex9_insn->next
14485                                           && ex9_insn->m_list
14486                                           && ex9_insn->m_list == ex9_insn->next->m_list)
14487                                         {
14488                                           /* sethi multiple entry must be fixed */
14489                                           nds32_elf_ex9_insert_fix (sec, irel,
14490                                                                     h, ex9_insn->order);
14491                                         }
14492                                       break;
14493                                     }
14494                                   h_list = h_list->next;
14495                                 }
14496                               m_list = m_list->next;
14497                             }
14498                         }
14499                     }
14500                 }
14501
14502               /* Import table: Check the symbol hash table and the
14503                  jump target.  Only R_NDS32_25_PCREL_RELA now.  */
14504               else if (ex9_insn->times == -1
14505                        && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
14506                 {
14507                   nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
14508                   if (insn_with_reg == it_insn_with_reg)
14509                     {
14510                       char code[10];
14511                       bfd_vma relocation;
14512
14513                       r_symndx = ELF32_R_SYM (irel->r_info);
14514                       if (r_symndx >= symtab_hdr->sh_info)
14515                         {
14516                           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14517                           if ((h->root.type == bfd_link_hash_defined
14518                                || h->root.type == bfd_link_hash_defweak)
14519                               && h->root.u.def.section != NULL
14520                               && h->root.u.def.section->output_section != NULL
14521                               && h->root.u.def.section->gc_mark == 1
14522                               && bfd_is_abs_section (h->root.u.def.section)
14523                               && h->root.u.def.value > sec->size)
14524                             {
14525                               relocation = h->root.u.def.value +
14526                                 h->root.u.def.section->output_section->vma +
14527                                 h->root.u.def.section->output_offset;
14528                               relocation += irel->r_addend;
14529                               insn = insn_with_reg
14530                                 | ((relocation >> 1) & 0xffffff);
14531                               snprintf (code, sizeof (code), "%08x", insn);
14532                               if (strcmp (code, ex9_insn->string) == 0)
14533                                 {
14534                                   do_replace = 1;
14535                                   save_irel = 1;
14536                                 }
14537                             }
14538                         }
14539                     }
14540                 }
14541               else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
14542                        || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
14543                        || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
14544                 {
14545                   /* These relocations do not have to relocate contens, so it can
14546                      be regard as instruction without relocation.  */
14547                   if (insn == it_insn && ex9_insn->irel == NULL)
14548                     do_replace = 1;
14549                 }
14550             }
14551           else
14552             {
14553               /* Instruction without relocation, we only
14554                  have to compare their byte code.  */
14555               if (insn == it_insn && ex9_insn->irel == NULL)
14556                 do_replace = 1;
14557             }
14558
14559           /* Insntruction match so replacing the code here.  */
14560           if (do_replace == 1)
14561             {
14562               /* There are two formats of ex9 instruction.  */
14563               if (ex9_insn->order < 32)
14564                 insn_ex9 = INSN_EX9_IT_2;
14565               else
14566                 insn_ex9 = INSN_EX9_IT_1;
14567               insn16 = insn_ex9 | ex9_insn->order;
14568
14569               /* Insert ex9 instruction.  */
14570               nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14571                                        &relax_blank_list, pre_irel_ptr,
14572                                        &irel_list);
14573               pre_off = off;
14574               pre_insn16 = insn16;
14575
14576               if (save_irel)
14577                 {
14578                   /* For instuction with relocation do relax.  */
14579                   irel_ptr = (struct elf_nds32_irel_entry *)
14580                     bfd_malloc (sizeof (struct elf_nds32_irel_entry));
14581                   irel_ptr->irel = irel;
14582                   irel_ptr->next = NULL;
14583                   pre_irel_ptr = irel_ptr;
14584                 }
14585               else
14586                 pre_irel_ptr = NULL;
14587               break;
14588             }
14589           ex9_insn = ex9_insn->next;
14590         }
14591       off += 4;
14592     }
14593
14594   /* Insert ex9 instruction.  */
14595   nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14596                            &relax_blank_list, pre_irel_ptr,
14597                            &irel_list);
14598
14599   /* Delete the redundant code.  */
14600   if (relax_blank_list)
14601     {
14602       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
14603       relax_blank_list = NULL;
14604     }
14605
14606   /* Clear the relocation that is replaced by ex9.  */
14607   while (irel_list)
14608     {
14609       struct elf_nds32_irel_entry *irel_ptr;
14610
14611       irel_ptr = irel_list;
14612       irel_list = irel_ptr->next;
14613       irel_ptr->irel->r_info =
14614         ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
14615       free (irel_ptr);
14616     }
14617   return TRUE;
14618 }
14619
14620 /* Initialize ex9 hash table.  */
14621
14622 int
14623 nds32_elf_ex9_init (void)
14624 {
14625   if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
14626                               sizeof (struct elf_nds32_code_hash_entry),
14627                               1023))
14628     {
14629       _bfd_error_handler (_("Linker: cannot init ex9 hash table error \n"));
14630       return FALSE;
14631     }
14632   return TRUE;
14633 }
14634
14635 /* Predict how many bytes will be relaxed with ex9 and ifc.  */
14636
14637 static void
14638 nds32_elf_ex9_total_relax (struct bfd_link_info *info)
14639 {
14640   struct elf_nds32_insn_times_entry *ex9_insn;
14641   struct elf_nds32_insn_times_entry *temp;
14642   int target_optimize;
14643   struct elf_nds32_link_hash_table *table;
14644
14645   if (ex9_insn_head == NULL)
14646     return;
14647
14648   table = nds32_elf_hash_table (info);
14649   target_optimize  = table->target_optimize;
14650   ex9_insn = ex9_insn_head;
14651   while (ex9_insn)
14652     {
14653       ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
14654       temp = ex9_insn;
14655       ex9_insn = ex9_insn->next;
14656       free (temp);
14657     }
14658   ex9_insn_head = NULL;
14659
14660   if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
14661     {
14662       /* Examine ifc reduce size.  */
14663       struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
14664       struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
14665       int size = 0;
14666
14667       while (ifc_ent)
14668         {
14669           if (ifc_ent->enable == 0)
14670             {
14671               /* Not ifc yet.  */
14672               irel_ptr = ifc_ent->irel_head;
14673               while (irel_ptr)
14674                 {
14675                   size += 2;
14676                   irel_ptr = irel_ptr->next;
14677                 }
14678             }
14679           size -= 2;
14680           ifc_ent = ifc_ent->next;
14681         }
14682       ex9_relax_size += size;
14683     }
14684 }
14685
14686 /* Finish ex9 table.  */
14687
14688 void
14689 nds32_elf_ex9_finish (struct bfd_link_info *link_info)
14690 {
14691   nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14692   nds32_elf_order_insn_times (link_info);
14693   nds32_elf_ex9_total_relax (link_info);
14694   /* Traverse the hash table and count its times.  */
14695   nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
14696   nds32_elf_order_insn_times (link_info);
14697   nds32_elf_ex9_build_itable (link_info);
14698 }
14699
14700 /* Relocate the entries in ex9 table.  */
14701
14702 static bfd_vma
14703 nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
14704                           struct bfd_link_info *link_info)
14705 {
14706   Elf_Internal_Sym *isym = NULL;
14707   bfd_vma relocation = -1;
14708   struct elf_link_hash_entry *h;
14709
14710   if (ptr->m_list != NULL)
14711     {
14712       /* Global symbol.  */
14713       h = ptr->m_list->h_list->h;
14714       if ((h->root.type == bfd_link_hash_defined
14715            || h->root.type == bfd_link_hash_defweak)
14716           && h->root.u.def.section != NULL
14717           && h->root.u.def.section->output_section != NULL)
14718         {
14719
14720           relocation = h->root.u.def.value +
14721             h->root.u.def.section->output_section->vma +
14722             h->root.u.def.section->output_offset;
14723           relocation += ptr->m_list->irel->r_addend;
14724         }
14725       else
14726         relocation = 0;
14727     }
14728   else if (ptr->sec !=NULL)
14729     {
14730       /* Local symbol.  */
14731       Elf_Internal_Sym sym;
14732       asection *sec = NULL;
14733       asection isec;
14734       asection *isec_ptr = &isec;
14735       Elf_Internal_Rela irel_backup = *(ptr->irel);
14736       asection *sec_backup = ptr->sec;
14737       bfd *abfd = ptr->sec->owner;
14738
14739       if (!nds32_get_local_syms (abfd, sec, &isym))
14740         return FALSE;
14741       isym = isym + ELF32_R_SYM (ptr->irel->r_info);
14742
14743       sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14744       if (sec != NULL)
14745         *isec_ptr = *sec;
14746       sym = *isym;
14747
14748       /* The purpose is same as elf_link_input_bfd.  */
14749       if (isec_ptr != NULL
14750           && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
14751           && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
14752         {
14753           sym.st_value =
14754             _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
14755                                         elf_section_data (isec_ptr)->sec_info,
14756                                         isym->st_value);
14757         }
14758       relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
14759                                             &ptr->sec, ptr->irel);
14760       if (ptr->irel != NULL)
14761         relocation += ptr->irel->r_addend;
14762
14763       /* Restore origin value since there may be some insntructions that
14764          could not be replaced with ex9.it.  */
14765       *(ptr->irel) = irel_backup;
14766       ptr->sec = sec_backup;
14767     }
14768
14769   return relocation;
14770 }
14771
14772 /* Import ex9 table and build list.  */
14773
14774 void
14775 nds32_elf_ex9_import_table (struct bfd_link_info *info)
14776 {
14777   int num = 0;
14778   bfd_byte *contents;
14779   FILE *ex9_import_file;
14780   int update_ex9_table;
14781   struct elf_nds32_link_hash_table *table;
14782
14783   table = nds32_elf_hash_table (info);
14784   ex9_import_file = table->ex9_import_file;
14785   rewind (table->ex9_import_file);
14786
14787   contents = bfd_malloc (sizeof (bfd_byte) * 4);
14788
14789   /* Read instructions from the input file and build the list.  */
14790   while (!feof (ex9_import_file))
14791     {
14792       unsigned long insn;
14793       char *code;
14794       struct elf_nds32_insn_times_entry *ptr;
14795       size_t nread;
14796
14797       nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
14798       /* Ignore the final byte 0x0a.  */
14799       if (nread < 1)
14800         break;
14801       insn = bfd_getb32 (contents);
14802       code = bfd_malloc (sizeof (char) * 9);
14803       snprintf (code, 9, "%08lx", (insn & 0xffffffff));
14804       ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14805       ptr->string = code;
14806       ptr->order = num;
14807       ptr->times = -1;
14808       ptr->sec = NULL;
14809       ptr->m_list = NULL;
14810       ptr->rel_backup.r_offset = 0;
14811       ptr->rel_backup.r_info = 0;
14812       ptr->rel_backup.r_addend = 0;
14813       ptr->irel = NULL;
14814       ptr->next = NULL;
14815       nds32_elf_ex9_insert_entry (ptr);
14816       num++;
14817     }
14818
14819   update_ex9_table = table->update_ex9_table;
14820   if (update_ex9_table == 1)
14821     {
14822       /* It has to consider of sethi need to use multiple page
14823          but it not be done yet.  */
14824       nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14825       nds32_elf_order_insn_times (info);
14826     }
14827 }
14828
14829 /* Export ex9 table.  */
14830
14831 static void
14832 nds32_elf_ex9_export (struct bfd_link_info *info,
14833                       bfd_byte *contents, int size)
14834 {
14835   FILE *ex9_export_file;
14836   struct elf_nds32_link_hash_table *table;
14837
14838   table = nds32_elf_hash_table (info);
14839   ex9_export_file = table->ex9_export_file;
14840   fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
14841   fclose (ex9_export_file);
14842 }
14843
14844 /* Adjust relocations of J and JAL in ex9.itable.
14845    Export ex9 table.  */
14846
14847 static void
14848 nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
14849 {
14850   asection *table_sec = NULL;
14851   struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14852   struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
14853   bfd *it_abfd;
14854   uint32_t insn, insn_with_reg, source_insn;
14855   bfd_byte *contents = NULL, *source_contents = NULL;
14856   int size = 0;
14857   bfd_vma gp;
14858   int shift, update_ex9_table, offset = 0;
14859   reloc_howto_type *howto = NULL;
14860   Elf_Internal_Rela rel_backup;
14861   unsigned short insn_ex9;
14862   struct elf_nds32_link_hash_table *table;
14863   FILE *ex9_export_file;
14864   static bfd_boolean done = FALSE;
14865
14866   if (done)
14867     return;
14868
14869   done = TRUE;
14870
14871   table = nds32_elf_hash_table (link_info);
14872   if (table)
14873     table->relax_status |= NDS32_RELAX_EX9_DONE;
14874
14875
14876   update_ex9_table = table->update_ex9_table;
14877   /* Generated ex9.itable exactly.  */
14878   if (update_ex9_table == 0)
14879     {
14880       for (it_abfd = link_info->input_bfds; it_abfd != NULL;
14881            it_abfd = it_abfd->link.next)
14882         {
14883           table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
14884           if (table_sec != NULL)
14885             break;
14886         }
14887
14888       if (table_sec != NULL)
14889         {
14890           bfd *output_bfd;
14891
14892           output_bfd = table_sec->output_section->owner;
14893           nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
14894           if (table_sec->size == 0)
14895             return;
14896
14897           if (!nds32_get_section_contents (it_abfd, table_sec, &contents, TRUE))
14898             return;
14899         }
14900     }
14901   else
14902     {
14903       /* Set gp.  */
14904       bfd *output_bfd;
14905
14906       output_bfd = link_info->input_bfds->sections->output_section->owner;
14907       nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
14908       contents = bfd_malloc (sizeof (bfd_byte) * 2048);
14909     }
14910
14911   /* Relocate instruction.  */
14912   while (ex9_insn)
14913     {
14914       bfd_vma relocation, min_relocation = 0xffffffff;
14915
14916       insn = strtol (ex9_insn->string, NULL, 16);
14917       insn_with_reg = 0;
14918       if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
14919         {
14920           if (ex9_insn->m_list)
14921             rel_backup = ex9_insn->m_list->rel_backup;
14922           else
14923             rel_backup = ex9_insn->rel_backup;
14924
14925           nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
14926           howto =
14927             bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
14928                                                    (rel_backup.r_info));
14929           shift = howto->rightshift;
14930           if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
14931               || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
14932               || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
14933               || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
14934               || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
14935             {
14936               relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
14937               insn =
14938                 insn_with_reg | ((relocation >> shift) &
14939                                  nds32_elf_irel_mask (&rel_backup));
14940               bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14941             }
14942           else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
14943                     && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
14944                    || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
14945                        && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
14946                    || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
14947                        && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
14948                    || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
14949                        && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
14950             {
14951               relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
14952               insn =
14953                 insn_with_reg | (((relocation - gp) >> shift) &
14954                                  nds32_elf_irel_mask (&rel_backup));
14955               bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14956             }
14957           else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
14958             {
14959               /* Sethi may be multiple entry for one insn.  */
14960               if (ex9_insn->next && ex9_insn->m_list
14961                   && ex9_insn->m_list == ex9_insn->next->m_list)
14962                 {
14963                   struct elf_link_hash_entry_mul_list *m_list;
14964                   struct elf_nds32_ex9_refix *fix_ptr;
14965                   struct elf_link_hash_entry *h;
14966
14967                   temp_ptr = ex9_insn;
14968                   temp_ptr2 = ex9_insn;
14969                   m_list = ex9_insn->m_list;
14970                   while (m_list)
14971                     {
14972                       h = m_list->h_list->h;
14973                       relocation = h->root.u.def.value +
14974                         h->root.u.def.section->output_section->vma +
14975                         h->root.u.def.section->output_offset;
14976                       relocation += m_list->irel->r_addend;
14977
14978                       if (relocation < min_relocation)
14979                         min_relocation = relocation;
14980                       m_list = m_list->next;
14981                     }
14982                   relocation = min_relocation;
14983
14984                   /* Put insntruction into ex9 table.  */
14985                   insn = insn_with_reg
14986                     | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
14987                   bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
14988                   relocation = relocation + 0x1000;     /* hi20 */
14989
14990                   while (ex9_insn->next && ex9_insn->m_list
14991                          && ex9_insn->m_list == ex9_insn->next->m_list)
14992                     {
14993                       /* Multiple sethi.  */
14994                       ex9_insn = ex9_insn->next;
14995                       size += 4;
14996                       insn =
14997                         insn_with_reg | ((relocation >> shift) &
14998                                          nds32_elf_irel_mask (&rel_backup));
14999                       bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15000                       relocation = relocation + 0x1000; /* hi20 */
15001                     }
15002
15003                   fix_ptr = ex9_refix_head;
15004                   while (fix_ptr)
15005                     {
15006                       /* Fix ex9 insn.  */
15007                       /* temp_ptr2 points to the head of multiple sethi.  */
15008                       temp_ptr = temp_ptr2;
15009                       while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
15010                         {
15011                           fix_ptr = fix_ptr->next;
15012                         }
15013                       if (fix_ptr->order != temp_ptr->order)
15014                         break;
15015
15016                       /* Set source insn.  */
15017                       relocation =
15018                         fix_ptr->h->root.u.def.value +
15019                         fix_ptr->h->root.u.def.section->output_section->vma +
15020                         fix_ptr->h->root.u.def.section->output_offset;
15021                       relocation += fix_ptr->irel->r_addend;
15022                       /* sethi imm is imm20s.  */
15023                       source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);
15024
15025                       while (temp_ptr)
15026                         {
15027                           /* Match entry and source code.  */
15028                           insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
15029                           if (insn == source_insn)
15030                             {
15031                               /* Fix the ex9 insn.  */
15032                               if (temp_ptr->order != fix_ptr->order)
15033                                 {
15034                                   if (!nds32_get_section_contents
15035                                          (fix_ptr->sec->owner, fix_ptr->sec,
15036                                           &source_contents, TRUE))
15037                                     _bfd_error_handler
15038                                       (_("Linker: error cannot fixed ex9 relocation \n"));
15039                                   if (temp_ptr->order < 32)
15040                                     insn_ex9 = INSN_EX9_IT_2;
15041                                   else
15042                                     insn_ex9 = INSN_EX9_IT_1;
15043                                   insn_ex9 = insn_ex9 | temp_ptr->order;
15044                                   bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
15045                                 }
15046                                 break;
15047                             }
15048                           else
15049                             {
15050                               if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
15051                                 _bfd_error_handler
15052                                   (_("Linker: error cannot fixed ex9 relocation \n"));
15053                               else
15054                                 temp_ptr = temp_ptr->next;
15055                             }
15056                         }
15057                       fix_ptr = fix_ptr->next;
15058                     }
15059                 }
15060               else
15061                 {
15062                   relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15063                   insn = insn_with_reg
15064                          | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15065                   bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15066                 }
15067             }
15068         }
15069       else
15070         {
15071           /* Insn without relocation does not have to be fixed
15072              if need to update export table.  */
15073           if (update_ex9_table == 1)
15074             bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15075         }
15076       ex9_insn = ex9_insn->next;
15077       size += 4;
15078     }
15079
15080   ex9_export_file = table->ex9_export_file;
15081   if (ex9_export_file != NULL)
15082     nds32_elf_ex9_export (link_info, contents, table_sec->size);
15083   else if (update_ex9_table == 1)
15084     {
15085       table->ex9_export_file = table->ex9_import_file;
15086       rewind (table->ex9_export_file);
15087       nds32_elf_ex9_export (link_info, contents, size);
15088     }
15089 }
15090
15091 /* Generate ex9 hash table.  */
15092
15093 static bfd_boolean
15094 nds32_elf_ex9_build_hash_table (bfd *abfd, asection *sec,
15095                                 struct bfd_link_info *link_info)
15096 {
15097   Elf_Internal_Rela *internal_relocs;
15098   Elf_Internal_Rela *irelend;
15099   Elf_Internal_Rela *irel;
15100   Elf_Internal_Rela *jrel;
15101   Elf_Internal_Rela rel_backup;
15102   Elf_Internal_Shdr *symtab_hdr;
15103   Elf_Internal_Sym *isym = NULL;
15104   asection *isec;
15105   struct elf_link_hash_entry **sym_hashes;
15106   bfd_byte *contents = NULL;
15107   bfd_vma off = 0;
15108   unsigned long r_symndx;
15109   uint32_t insn, insn_with_reg;
15110   struct elf_link_hash_entry *h;
15111   int data_flag, shift, align;
15112   bfd_vma relocation;
15113   /* Suppress ex9 if `.no_relax ex9' or inner loop.  */
15114   reloc_howto_type *howto = NULL;
15115
15116   sym_hashes = elf_sym_hashes (abfd);
15117   /* Load section instructions, relocations, and symbol table.  */
15118   if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
15119     return FALSE;
15120
15121   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
15122                                                TRUE /* keep_memory */);
15123   irelend = internal_relocs + sec->reloc_count;
15124   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
15125   if (!nds32_get_local_syms (abfd, sec, &isym))
15126     return FALSE;
15127
15128   /* Check the object if enable ex9.  */
15129   irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
15130                                  R_NDS32_RELAX_ENTRY);
15131
15132   /* Check this section trigger ex9 relaxation.  */
15133   if (irel == NULL
15134       || irel >= irelend
15135       || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
15136       || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
15137           && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
15138     return TRUE;
15139
15140   irel = internal_relocs;
15141
15142   /* Push each insn into hash table.  */
15143   while (off < sec->size)
15144     {
15145       char code[10];
15146       struct elf_nds32_code_hash_entry *entry;
15147
15148       while (irel != NULL && irel < irelend && irel->r_offset < off)
15149         irel++;
15150
15151       data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend,
15152                                                   NULL, sec, &off, contents);
15153       if (data_flag & DATA_EXIST)
15154         {
15155           /* We save the move offset in the highest byte.  */
15156           off += (data_flag >> 24);
15157           continue;
15158         }
15159
15160       if (*(contents + off) & 0x80)
15161         {
15162           off += 2;
15163         }
15164       else
15165         {
15166           h = NULL;
15167           isec = NULL;
15168           jrel = NULL;
15169           rel_backup.r_info = 0;
15170           rel_backup.r_offset = 0;
15171           rel_backup.r_addend = 0;
15172           /* Load the instruction and its opcode with register for comparing.  */
15173           insn = bfd_getb32 (contents + off);
15174           insn_with_reg = 0;
15175           if (irel != NULL && irel < irelend && irel->r_offset == off)
15176             {
15177               nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
15178               howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
15179               shift = howto->rightshift;
15180               align = (1 << shift) - 1;
15181               if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
15182                   || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
15183                   || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
15184                   || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
15185                   || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
15186                   || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
15187                   ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15188                      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15189                   || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15190                       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15191                   || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15192                       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15193                   || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15194                       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15195                 {
15196                   r_symndx = ELF32_R_SYM (irel->r_info);
15197                   jrel = irel;
15198                   rel_backup = *irel;
15199                   if (r_symndx < symtab_hdr->sh_info)
15200                     {
15201                       /* Local symbol.  */
15202                       int shndx = isym[r_symndx].st_shndx;
15203
15204                       bfd_vma st_value = (isym + r_symndx)->st_value;
15205                       isec = elf_elfsections (abfd)[shndx]->bfd_section;
15206                       relocation = (isec->output_section->vma + isec->output_offset
15207                                     + st_value + irel->r_addend);
15208                     }
15209                   else
15210                     {
15211                       /* External symbol.  */
15212                       bfd_boolean warned ATTRIBUTE_UNUSED;
15213                       bfd_boolean ignored ATTRIBUTE_UNUSED;
15214                       bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
15215                       asection *sym_sec;
15216
15217                       /* Maybe there is a better way to get h and relocation */
15218                       RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
15219                                                r_symndx, symtab_hdr, sym_hashes,
15220                                                h, sym_sec, relocation,
15221                                                unresolved_reloc, warned, ignored);
15222                       relocation += irel->r_addend;
15223                       if ((h->root.type != bfd_link_hash_defined
15224                            && h->root.type != bfd_link_hash_defweak)
15225                           || strcmp (h->root.root.string, "_FP_BASE_") == 0)
15226                         {
15227                           off += 4;
15228                           continue;
15229                         }
15230                     }
15231
15232                   /* Check for gp relative instruction alignment.  */
15233                   if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15234                        && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15235                       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15236                           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15237                       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15238                           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15239                       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15240                           && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15241                     {
15242                       bfd_vma gp;
15243                       bfd *output_bfd = sec->output_section->owner;
15244                       bfd_reloc_status_type r;
15245
15246                       /* If the symbol is in the abs section, the out_bfd will be null.
15247                          This happens when the relocation has a symbol@GOTOFF.  */
15248                       r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15249                       if (r != bfd_reloc_ok)
15250                         {
15251                           off += 4;
15252                           continue;
15253                         }
15254
15255                       relocation -= gp;
15256
15257                       /* Make sure alignment is correct.  */
15258                       if (relocation & align)
15259                         {
15260                           /* Incorrect alignment.  */
15261                           _bfd_error_handler
15262                             /* xgettext:c-format */
15263                             (_("%B: warning: unaligned small data access "
15264                                "for entry: {%Ld, %Ld, %Ld}, addr = %#Lx, align = %#x"),
15265                              abfd, irel->r_offset,
15266                              irel->r_info, irel->r_addend, relocation, align);
15267                           off += 4;
15268                           continue;
15269                         }
15270                     }
15271
15272                   insn = insn_with_reg
15273                     | ((relocation >> shift) & nds32_elf_irel_mask (irel));
15274                 }
15275               else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
15276                        || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
15277                        || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
15278                 {
15279                   /* These relocations do not have to relocate contens, so it can
15280                      be regard as instruction without relocation.  */
15281                 }
15282               else
15283                 {
15284                   off += 4;
15285                   continue;
15286                 }
15287             }
15288
15289           snprintf (code, sizeof (code), "%08x", insn);
15290           /* Copy "code".  */
15291           entry = (struct elf_nds32_code_hash_entry*)
15292             bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
15293           if (entry == NULL)
15294             {
15295               _bfd_error_handler
15296                 (_("failed creating ex9.it %s hash table entry"), code);
15297               return FALSE;
15298             }
15299           if (h)
15300             {
15301               if (h->root.type == bfd_link_hash_undefined)
15302                 return TRUE;
15303               /* Global symbol.  */
15304               /* In order to do sethi with different symbol but same value.  */
15305               if (entry->m_list == NULL)
15306                 {
15307                   struct elf_link_hash_entry_mul_list *m_list_new;
15308                   struct elf_link_hash_entry_list *h_list_new;
15309
15310                   m_list_new = (struct elf_link_hash_entry_mul_list *)
15311                     bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15312                   h_list_new = (struct elf_link_hash_entry_list *)
15313                     bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15314                   entry->m_list = m_list_new;
15315                   m_list_new->h_list = h_list_new;
15316                   m_list_new->rel_backup = rel_backup;
15317                   m_list_new->times = 1;
15318                   m_list_new->irel = jrel;
15319                   m_list_new->next = NULL;
15320                   h_list_new->h = h;
15321                   h_list_new->next = NULL;
15322                 }
15323               else
15324                 {
15325                   struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
15326                   struct elf_link_hash_entry_list *h_list;
15327
15328                   while (m_list)
15329                     {
15330                       /* Build the different symbols that point to the same address.  */
15331                       h_list = m_list->h_list;
15332                       if (h_list->h->root.u.def.value == h->root.u.def.value
15333                           && h_list->h->root.u.def.section->output_section->vma
15334                              == h->root.u.def.section->output_section->vma
15335                           && h_list->h->root.u.def.section->output_offset
15336                              == h->root.u.def.section->output_offset
15337                           && m_list->rel_backup.r_addend == rel_backup.r_addend)
15338                         {
15339                           m_list->times++;
15340                           m_list->irel = jrel;
15341                           while (h_list->h != h && h_list->next)
15342                             h_list = h_list->next;
15343                           if (h_list->h != h)
15344                             {
15345                               struct elf_link_hash_entry_list *h_list_new;
15346
15347                               h_list_new = (struct elf_link_hash_entry_list *)
15348                                 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15349                               h_list->next = h_list_new;
15350                               h_list_new->h = h;
15351                               h_list_new->next = NULL;
15352                             }
15353                           break;
15354                         }
15355                       /* The sethi case may have different address but the
15356                          hi20 is the same.  */
15357                       else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
15358                                && m_list->next == NULL)
15359                         {
15360                           struct elf_link_hash_entry_mul_list *m_list_new;
15361                           struct elf_link_hash_entry_list *h_list_new;
15362
15363                           m_list_new = (struct elf_link_hash_entry_mul_list *)
15364                             bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15365                           h_list_new = (struct elf_link_hash_entry_list *)
15366                             bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15367                           m_list->next = m_list_new;
15368                           m_list_new->h_list = h_list_new;
15369                           m_list_new->rel_backup = rel_backup;
15370                           m_list_new->times = 1;
15371                           m_list_new->irel = jrel;
15372                           m_list_new->next = NULL;
15373                           h_list_new->h = h;
15374                           h_list_new->next = NULL;
15375                           break;
15376                         }
15377                       m_list = m_list->next;
15378                     }
15379                   if (!m_list)
15380                     {
15381                       off += 4;
15382                       continue;
15383                     }
15384                 }
15385             }
15386           else
15387             {
15388               /* Local symbol and insn without relocation*/
15389               entry->times++;
15390               entry->rel_backup = rel_backup;
15391             }
15392
15393           /* Use in sethi insn with constant and global symbol in same format.  */
15394           if (!jrel)
15395             entry->const_insn = 1;
15396           else
15397             entry->irel = jrel;
15398           entry->sec = isec;
15399           off += 4;
15400         }
15401     }
15402   return TRUE;
15403 }
15404
15405 /* Set the _ITB_BASE, and point it to ex9 table.  */
15406
15407 bfd_boolean
15408 nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
15409 {
15410   bfd *abfd;
15411   asection *sec;
15412   bfd *output_bfd = NULL;
15413   struct bfd_link_hash_entry *bh = NULL;
15414
15415   if (is_ITB_BASE_set == 1)
15416     return TRUE;
15417
15418   is_ITB_BASE_set = 1;
15419
15420   bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_", FALSE, FALSE, TRUE);
15421
15422   if (bh && (bh->type == bfd_link_hash_defined
15423              || bh->type == bfd_link_hash_defweak))
15424     return TRUE;
15425
15426   for (abfd = link_info->input_bfds; abfd != NULL;
15427        abfd = abfd->link.next)
15428     {
15429       sec = bfd_get_section_by_name (abfd, ".ex9.itable");
15430       if (sec != NULL)
15431         {
15432           output_bfd = sec->output_section->owner;
15433           break;
15434         }
15435     }
15436   if (output_bfd == NULL)
15437     {
15438       output_bfd = link_info->output_bfd;
15439       if (output_bfd->sections == NULL)
15440         return TRUE;
15441       else
15442         sec = bfd_abs_section_ptr;
15443     }
15444   bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
15445                              FALSE, FALSE, TRUE);
15446   return (_bfd_generic_link_add_one_symbol
15447           (link_info, output_bfd, "_ITB_BASE_",
15448            BSF_GLOBAL | BSF_WEAK, sec, 0,
15449            (const char *) NULL, FALSE, get_elf_backend_data
15450            (output_bfd)->collect, &bh));
15451 } /* End EX9.IT  */
15452 \f
15453
15454 #define ELF_ARCH                                bfd_arch_nds32
15455 #define ELF_MACHINE_CODE                        EM_NDS32
15456 #define ELF_MAXPAGESIZE                         0x1000
15457 #define ELF_TARGET_ID                           NDS32_ELF_DATA
15458
15459 #define TARGET_BIG_SYM                          nds32_elf32_be_vec
15460 #define TARGET_BIG_NAME                         "elf32-nds32be"
15461 #define TARGET_LITTLE_SYM                       nds32_elf32_le_vec
15462 #define TARGET_LITTLE_NAME                      "elf32-nds32le"
15463
15464 #define elf_info_to_howto                       nds32_info_to_howto
15465 #define elf_info_to_howto_rel                   nds32_info_to_howto_rel
15466
15467 #define bfd_elf32_bfd_link_hash_table_create    nds32_elf_link_hash_table_create
15468 #define bfd_elf32_bfd_merge_private_bfd_data    nds32_elf_merge_private_bfd_data
15469 #define bfd_elf32_bfd_print_private_bfd_data    nds32_elf_print_private_bfd_data
15470 #define bfd_elf32_bfd_relax_section             nds32_elf_relax_section
15471 #define bfd_elf32_bfd_set_private_flags         nds32_elf_set_private_flags
15472
15473 #define bfd_elf32_mkobject                      nds32_elf_mkobject
15474 #define elf_backend_action_discarded            nds32_elf_action_discarded
15475 #define elf_backend_add_symbol_hook             nds32_elf_add_symbol_hook
15476 #define elf_backend_check_relocs                nds32_elf_check_relocs
15477 #define elf_backend_adjust_dynamic_symbol       nds32_elf_adjust_dynamic_symbol
15478 #define elf_backend_create_dynamic_sections     nds32_elf_create_dynamic_sections
15479 #define elf_backend_finish_dynamic_sections     nds32_elf_finish_dynamic_sections
15480 #define elf_backend_finish_dynamic_symbol       nds32_elf_finish_dynamic_symbol
15481 #define elf_backend_size_dynamic_sections       nds32_elf_size_dynamic_sections
15482 #define elf_backend_relocate_section            nds32_elf_relocate_section
15483 #define elf_backend_gc_mark_hook                nds32_elf_gc_mark_hook
15484 #define elf_backend_grok_prstatus               nds32_elf_grok_prstatus
15485 #define elf_backend_grok_psinfo                 nds32_elf_grok_psinfo
15486 #define elf_backend_reloc_type_class            nds32_elf_reloc_type_class
15487 #define elf_backend_copy_indirect_symbol        nds32_elf_copy_indirect_symbol
15488 #define elf_backend_link_output_symbol_hook     nds32_elf_output_symbol_hook
15489 #define elf_backend_output_arch_syms            nds32_elf_output_arch_syms
15490 #define elf_backend_object_p                    nds32_elf_object_p
15491 #define elf_backend_final_write_processing      nds32_elf_final_write_processing
15492 #define elf_backend_special_sections            nds32_elf_special_sections
15493 #define bfd_elf32_bfd_get_relocated_section_contents \
15494                                 nds32_elf_get_relocated_section_contents
15495
15496 #define elf_backend_can_gc_sections             1
15497 #define elf_backend_can_refcount                1
15498 #define elf_backend_want_got_plt                1
15499 #define elf_backend_plt_readonly                1
15500 #define elf_backend_want_plt_sym                0
15501 #define elf_backend_got_header_size             12
15502 #define elf_backend_may_use_rel_p               1
15503 #define elf_backend_default_use_rela_p          1
15504 #define elf_backend_may_use_rela_p              1
15505 #define elf_backend_dtrel_excludes_plt          1
15506
15507 #include "elf32-target.h"
15508
15509 #undef ELF_MAXPAGESIZE
15510 #define ELF_MAXPAGESIZE                         0x2000
15511
15512 #undef TARGET_BIG_SYM
15513 #define TARGET_BIG_SYM                          nds32_elf32_linux_be_vec
15514 #undef TARGET_BIG_NAME
15515 #define TARGET_BIG_NAME                         "elf32-nds32be-linux"
15516 #undef TARGET_LITTLE_SYM
15517 #define TARGET_LITTLE_SYM                       nds32_elf32_linux_le_vec
15518 #undef TARGET_LITTLE_NAME
15519 #define TARGET_LITTLE_NAME                      "elf32-nds32le-linux"
15520 #undef elf32_bed
15521 #define elf32_bed                               elf32_nds32_lin_bed
15522
15523 #include "elf32-target.h"