Home | History | Annotate | Download | only in bfd
      1 /* NDS32-specific support for 32-bit ELF.
      2    Copyright (C) 2012-2014 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 static void nds32_elf_relocate_hi20
     60   (bfd *, int, Elf_Internal_Rela *, Elf_Internal_Rela *, bfd_byte *, bfd_vma);
     61 static reloc_howto_type *bfd_elf32_bfd_reloc_type_table_lookup
     62   (enum elf_nds32_reloc_type);
     63 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
     64   (bfd *, bfd_reloc_code_real_type);
     65 
     66 /* Target hooks.  */
     67 static void nds32_info_to_howto_rel
     68   (bfd *, arelent *, Elf_Internal_Rela *dst);
     69 static void nds32_info_to_howto
     70   (bfd *, arelent *, Elf_Internal_Rela *dst);
     71 static bfd_boolean nds32_elf_add_symbol_hook
     72   (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, const char **,
     73    flagword *, asection **, bfd_vma *);
     74 static bfd_boolean nds32_elf_relocate_section
     75   (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
     76    Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
     77 static bfd_boolean nds32_elf_object_p (bfd *);
     78 static void nds32_elf_final_write_processing (bfd *, bfd_boolean);
     79 static bfd_boolean nds32_elf_set_private_flags (bfd *, flagword);
     80 static bfd_boolean nds32_elf_merge_private_bfd_data (bfd *, bfd *);
     81 static bfd_boolean nds32_elf_print_private_bfd_data (bfd *, void *);
     82 static bfd_boolean nds32_elf_gc_sweep_hook
     83   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
     84 static bfd_boolean nds32_elf_check_relocs
     85   (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
     86 static asection *nds32_elf_gc_mark_hook
     87   (asection *, struct bfd_link_info *, Elf_Internal_Rela *,
     88    struct elf_link_hash_entry *, Elf_Internal_Sym *);
     89 static bfd_boolean nds32_elf_adjust_dynamic_symbol
     90   (struct bfd_link_info *, struct elf_link_hash_entry *);
     91 static bfd_boolean nds32_elf_size_dynamic_sections
     92   (bfd *, struct bfd_link_info *);
     93 static bfd_boolean nds32_elf_create_dynamic_sections
     94   (bfd *, struct bfd_link_info *);
     95 static bfd_boolean nds32_elf_finish_dynamic_sections
     96   (bfd *, struct bfd_link_info *info);
     97 static bfd_boolean nds32_elf_finish_dynamic_symbol
     98   (bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
     99    Elf_Internal_Sym *);
    100 static bfd_boolean nds32_elf_mkobject (bfd *);
    101 
    102 /* Nds32 helper functions.  */
    103 static bfd_reloc_status_type nds32_elf_final_sda_base
    104   (bfd *, struct bfd_link_info *, bfd_vma *, bfd_boolean);
    105 static bfd_boolean allocate_dynrelocs (struct elf_link_hash_entry *, void *);
    106 static bfd_boolean readonly_dynrelocs (struct elf_link_hash_entry *, void *);
    107 static Elf_Internal_Rela *find_relocs_at_address
    108   (Elf_Internal_Rela *, Elf_Internal_Rela *,
    109    Elf_Internal_Rela *, enum elf_nds32_reloc_type);
    110 static bfd_vma calculate_memory_address
    111   (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
    112 static int nds32_get_section_contents (bfd *, asection *, bfd_byte **);
    113 static bfd_boolean nds32_elf_ex9_build_hash_table
    114   (bfd *, asection *, struct bfd_link_info *);
    115 static bfd_boolean nds32_elf_ex9_itb_base (struct bfd_link_info *);
    116 static void nds32_elf_ex9_import_table (struct bfd_link_info *);
    117 static void nds32_elf_ex9_finish (struct bfd_link_info *);
    118 static void nds32_elf_ex9_reloc_jmp (struct bfd_link_info *);
    119 static void nds32_elf_get_insn_with_reg
    120   (Elf_Internal_Rela *, uint32_t, uint32_t *);
    121 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
    122 				 Elf_Internal_Sym **);
    123 static bfd_boolean nds32_elf_ex9_replace_instruction
    124   (struct bfd_link_info *, bfd *, asection *);
    125 static bfd_boolean nds32_elf_ifc_calc (struct bfd_link_info *, bfd *,
    126 				       asection *);
    127 static bfd_boolean nds32_elf_ifc_finish (struct bfd_link_info *);
    128 static bfd_boolean nds32_elf_ifc_replace (struct bfd_link_info *);
    129 static bfd_boolean nds32_elf_ifc_reloc (void);
    130 static bfd_boolean  nds32_relax_fp_as_gp
    131   (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
    132    Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
    133    Elf_Internal_Sym *isymbuf);
    134 static bfd_boolean nds32_fag_remove_unused_fpbase
    135   (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
    136    Elf_Internal_Rela *irelend);
    137 static bfd_byte *
    138 nds32_elf_get_relocated_section_contents (bfd *abfd,
    139 					  struct bfd_link_info *link_info,
    140 					  struct bfd_link_order *link_order,
    141 					  bfd_byte *data,
    142 					  bfd_boolean relocatable,
    143 					  asymbol **symbols);
    144 
    145 enum
    146 {
    147   MACH_V1 = bfd_mach_n1h,
    148   MACH_V2 = bfd_mach_n1h_v2,
    149   MACH_V3 = bfd_mach_n1h_v3,
    150   MACH_V3M = bfd_mach_n1h_v3m
    151 };
    152 
    153 #define MIN(a, b) ((a) > (b) ? (b) : (a))
    154 #define MAX(a, b) ((a) > (b) ? (a) : (b))
    155 
    156 /* The name of the dynamic interpreter.  This is put in the .interp
    157    section.  */
    158 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
    159 
    160 /* The nop opcode we use.  */
    161 #define NDS32_NOP32 0x40000009
    162 #define NDS32_NOP16 0x9200
    163 
    164 /* The size in bytes of an entry in the procedure linkage table.  */
    165 #define PLT_ENTRY_SIZE 24
    166 #define PLT_HEADER_SIZE 24
    167 
    168 /* The first entry in a procedure linkage table are reserved,
    169    and the initial contents are unimportant (we zero them out).
    170    Subsequent entries look like this.  */
    171 #define PLT0_ENTRY_WORD0  0x46f00000		/* sethi   r15, HI20(.got+4)      */
    172 #define PLT0_ENTRY_WORD1  0x58f78000		/* ori     r15, r25, LO12(.got+4) */
    173 #define PLT0_ENTRY_WORD2  0x05178000		/* lwi     r17, [r15+0]           */
    174 #define PLT0_ENTRY_WORD3  0x04f78001		/* lwi     r15, [r15+4]           */
    175 #define PLT0_ENTRY_WORD4  0x4a003c00		/* jr      r15                    */
    176 
    177 /* $ta is change to $r15 (from $r25).  */
    178 #define PLT0_PIC_ENTRY_WORD0  0x46f00000	/* sethi   r15, HI20(got[1]@GOT)  */
    179 #define PLT0_PIC_ENTRY_WORD1  0x58f78000	/* ori     r15, r15, LO12(got[1]@GOT) */
    180 #define PLT0_PIC_ENTRY_WORD2  0x40f7f400	/* add     r15, gp, r15           */
    181 #define PLT0_PIC_ENTRY_WORD3  0x05178000	/* lwi     r17, [r15+0]           */
    182 #define PLT0_PIC_ENTRY_WORD4  0x04f78001	/* lwi     r15, [r15+4]           */
    183 #define PLT0_PIC_ENTRY_WORD5  0x4a003c00	/* jr      r15                    */
    184 
    185 #define PLT_ENTRY_WORD0  0x46f00000		/* sethi   r15, HI20(&got[n+3])      */
    186 #define PLT_ENTRY_WORD1  0x04f78000		/* lwi     r15, r15, LO12(&got[n+3]) */
    187 #define PLT_ENTRY_WORD2  0x4a003c00		/* jr      r15                       */
    188 #define PLT_ENTRY_WORD3  0x45000000		/* movi    r16, sizeof(RELA) * n     */
    189 #define PLT_ENTRY_WORD4  0x48000000		/* j      .plt0.                     */
    190 
    191 #define PLT_PIC_ENTRY_WORD0  0x46f00000		/* sethi  r15, HI20(got[n+3]@GOT)    */
    192 #define PLT_PIC_ENTRY_WORD1  0x58f78000		/* ori    r15, r15,    LO12(got[n+3]@GOT) */
    193 #define PLT_PIC_ENTRY_WORD2  0x38febc02		/* lw     r15, [gp+r15]              */
    194 #define PLT_PIC_ENTRY_WORD3  0x4a003c00		/* jr     r15                        */
    195 #define PLT_PIC_ENTRY_WORD4  0x45000000		/* movi   r16, sizeof(RELA) * n      */
    196 #define PLT_PIC_ENTRY_WORD5  0x48000000		/* j      .plt0                      */
    197 
    198 /* These are macros used to get the relocation accurate value.  */
    199 #define ACCURATE_8BIT_S1	(0x100)
    200 #define ACCURATE_U9BIT_S1	(0x400)
    201 #define ACCURATE_12BIT_S1	(0x2000)
    202 #define ACCURATE_14BIT_S1	(0x4000)
    203 #define ACCURATE_19BIT		(0x40000)
    204 
    205 /* These are macros used to get the relocation conservative value.  */
    206 #define CONSERVATIVE_8BIT_S1	(0x100 - 4)
    207 #define CONSERVATIVE_14BIT_S1	(0x4000 - 4)
    208 #define CONSERVATIVE_16BIT_S1	(0x10000 - 4)
    209 #define CONSERVATIVE_24BIT_S1	(0x1000000 - 4)
    210 /* These must be more conservative because the address may be in
    211    different segment.  */
    212 #define CONSERVATIVE_15BIT	(0x4000 - 0x1000)
    213 #define CONSERVATIVE_15BIT_S1	(0x8000 - 0x1000)
    214 #define CONSERVATIVE_15BIT_S2	(0x10000 - 0x1000)
    215 #define CONSERVATIVE_19BIT	(0x40000 - 0x1000)
    216 #define CONSERVATIVE_20BIT	(0x80000 - 0x1000)
    217 
    218 /* Size of small data/bss sections, used to calculate SDA_BASE.  */
    219 static long got_size = 0;
    220 static int is_SDA_BASE_set = 0;
    221 static int is_ITB_BASE_set = 0;
    222 
    223 /* Convert ELF-VER in eflags to string for debugging purpose.  */
    224 static const char *const nds32_elfver_strtab[] =
    225 {
    226   "ELF-1.2",
    227   "ELF-1.3",
    228   "ELF-1.4",
    229 };
    230 
    231 /* The nds32 linker needs to keep track of the number of relocs that it
    232    decides to copy in check_relocs for each symbol.  This is so that
    233    it can discard PC relative relocs if it doesn't need them when
    234    linking with -Bsymbolic.  We store the information in a field
    235    extending the regular ELF linker hash table.  */
    236 
    237 /* This structure keeps track of the number of PC relative relocs we
    238    have copied for a given symbol.  */
    239 
    240 struct elf_nds32_pcrel_relocs_copied
    241 {
    242   /* Next section.  */
    243   struct elf_nds32_pcrel_relocs_copied *next;
    244   /* A section in dynobj.  */
    245   asection *section;
    246   /* Number of relocs copied in this section.  */
    247   bfd_size_type count;
    248 };
    249 
    250 /* The sh linker needs to keep track of the number of relocs that it
    251    decides to copy as dynamic relocs in check_relocs for each symbol.
    252    This is so that it can later discard them if they are found to be
    253    unnecessary.  We store the information in a field extending the
    254    regular ELF linker hash table.  */
    255 
    256 struct elf_nds32_dyn_relocs
    257 {
    258   struct elf_nds32_dyn_relocs *next;
    259 
    260   /* The input section of the reloc.  */
    261   asection *sec;
    262 
    263   /* Total number of relocs copied for the input section.  */
    264   bfd_size_type count;
    265 
    266   /* Number of pc-relative relocs copied for the input section.  */
    267   bfd_size_type pc_count;
    268 };
    269 
    270 /* Nds32 ELF linker hash entry.  */
    271 
    272 struct elf_nds32_link_hash_entry
    273 {
    274   struct elf_link_hash_entry root;
    275 
    276   /* Track dynamic relocs copied for this symbol.  */
    277   struct elf_nds32_dyn_relocs *dyn_relocs;
    278 
    279   /* For checking relocation type.  */
    280 #define GOT_UNKNOWN     0
    281 #define GOT_NORMAL      1
    282 #define GOT_TLS_IE      2
    283   unsigned int tls_type;
    284 };
    285 
    286 /* Get the nds32 ELF linker hash table from a link_info structure.  */
    287 
    288 #define FP_BASE_NAME "_FP_BASE_"
    289 static int check_start_export_sym = 0;
    290 static size_t ex9_relax_size = 0;		/* Save ex9 predicted reducing size.  */
    291 
    292 /* The offset for executable tls relaxation.  */
    293 #define TP_OFFSET 0x0
    294 
    295 struct elf_nds32_obj_tdata
    296 {
    297   struct elf_obj_tdata root;
    298 
    299   /* tls_type for each local got entry.  */
    300   char *local_got_tls_type;
    301 };
    302 
    303 #define elf_nds32_tdata(bfd) \
    304   ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
    305 
    306 #define elf32_nds32_local_got_tls_type(bfd) \
    307   (elf_nds32_tdata (bfd)->local_got_tls_type)
    308 
    309 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
    310 
    311 static bfd_boolean
    312 nds32_elf_mkobject (bfd *abfd)
    313 {
    314   return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
    315 				  NDS32_ELF_DATA);
    316 }
    317 
    318 /* Relocations used for relocation.  */
    319 static reloc_howto_type nds32_elf_howto_table[] =
    320 {
    321   /* This reloc does nothing.  */
    322   HOWTO (R_NDS32_NONE,		/* type */
    323 	 0,			/* rightshift */
    324 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    325 	 32,			/* bitsize */
    326 	 FALSE,			/* pc_relative */
    327 	 0,			/* bitpos */
    328 	 complain_overflow_bitfield,	/* complain_on_overflow */
    329 	 bfd_elf_generic_reloc,	/* special_function */
    330 	 "R_NDS32_NONE",	/* name */
    331 	 FALSE,			/* partial_inplace */
    332 	 0,			/* src_mask */
    333 	 0,			/* dst_mask */
    334 	 FALSE),		/* pcrel_offset */
    335 
    336   /* A 16 bit absolute relocation.  */
    337   HOWTO (R_NDS32_16,		/* type */
    338 	 0,			/* rightshift */
    339 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    340 	 16,			/* bitsize */
    341 	 FALSE,			/* pc_relative */
    342 	 0,			/* bitpos */
    343 	 complain_overflow_bitfield,	/* complain_on_overflow */
    344 	 nds32_elf_generic_reloc,	/* special_function */
    345 	 "R_NDS32_16",		/* name */
    346 	 FALSE,			/* partial_inplace */
    347 	 0xffff,		/* src_mask */
    348 	 0xffff,		/* dst_mask */
    349 	 FALSE),		/* pcrel_offset */
    350 
    351   /* A 32 bit absolute relocation.  */
    352   HOWTO (R_NDS32_32,		/* type */
    353 	 0,			/* rightshift */
    354 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    355 	 32,			/* bitsize */
    356 	 FALSE,			/* pc_relative */
    357 	 0,			/* bitpos */
    358 	 complain_overflow_bitfield,	/* complain_on_overflow */
    359 	 nds32_elf_generic_reloc,	/* special_function */
    360 	 "R_NDS32_32",		/* name */
    361 	 FALSE,			/* partial_inplace */
    362 	 0xffffffff,		/* src_mask */
    363 	 0xffffffff,		/* dst_mask */
    364 	 FALSE),		/* pcrel_offset */
    365 
    366   /* A 20 bit address.  */
    367   HOWTO (R_NDS32_20,		/* type */
    368 	 0,			/* rightshift */
    369 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    370 	 20,			/* bitsize */
    371 	 FALSE,			/* pc_relative */
    372 	 0,			/* bitpos */
    373 	 complain_overflow_unsigned,	/* complain_on_overflow */
    374 	 nds32_elf_generic_reloc,	/* special_function */
    375 	 "R_NDS32_20",		/* name */
    376 	 FALSE,			/* partial_inplace */
    377 	 0xfffff,		/* src_mask */
    378 	 0xfffff,		/* dst_mask */
    379 	 FALSE),		/* pcrel_offset */
    380 
    381   /* An PC Relative 9-bit relocation, shifted by 2.
    382      This reloc is complicated because relocations are relative to pc & -4.
    383      i.e. branches in the right insn slot use the address of the left insn
    384      slot for pc.  */
    385   /* ??? It's not clear whether this should have partial_inplace set or not.
    386      Branch relaxing in the assembler can store the addend in the insn,
    387      and if bfd_install_relocation gets called the addend may get added
    388      again.  */
    389   HOWTO (R_NDS32_9_PCREL,	/* type */
    390 	 1,			/* rightshift */
    391 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    392 	 8,			/* bitsize */
    393 	 TRUE,			/* pc_relative */
    394 	 0,			/* bitpos */
    395 	 complain_overflow_signed,	/* complain_on_overflow */
    396 	 nds32_elf_9_pcrel_reloc,	/* special_function */
    397 	 "R_NDS32_9_PCREL",	/* name */
    398 	 FALSE,			/* partial_inplace */
    399 	 0xff,			/* src_mask */
    400 	 0xff,			/* dst_mask */
    401 	 TRUE),			/* pcrel_offset */
    402 
    403   /* A relative 15 bit relocation, right shifted by 1.  */
    404   HOWTO (R_NDS32_15_PCREL,	/* type */
    405 	 1,			/* rightshift */
    406 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    407 	 14,			/* bitsize */
    408 	 TRUE,			/* pc_relative */
    409 	 0,			/* bitpos */
    410 	 complain_overflow_signed,	/* complain_on_overflow */
    411 	 bfd_elf_generic_reloc,	/* special_function */
    412 	 "R_NDS32_15_PCREL",	/* name */
    413 	 FALSE,			/* partial_inplace */
    414 	 0x3fff,		/* src_mask */
    415 	 0x3fff,		/* dst_mask */
    416 	 TRUE),			/* pcrel_offset */
    417 
    418   /* A relative 17 bit relocation, right shifted by 1.  */
    419   HOWTO (R_NDS32_17_PCREL,	/* type */
    420 	 1,			/* rightshift */
    421 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    422 	 16,			/* bitsize */
    423 	 TRUE,			/* pc_relative */
    424 	 0,			/* bitpos */
    425 	 complain_overflow_signed,	/* complain_on_overflow */
    426 	 bfd_elf_generic_reloc,	/* special_function */
    427 	 "R_NDS32_17_PCREL",	/* name */
    428 	 FALSE,			/* partial_inplace */
    429 	 0xffff,		/* src_mask */
    430 	 0xffff,		/* dst_mask */
    431 	 TRUE),			/* pcrel_offset */
    432 
    433   /* A relative 25 bit relocation, right shifted by 1.  */
    434   /* ??? It's not clear whether this should have partial_inplace set or not.
    435      Branch relaxing in the assembler can store the addend in the insn,
    436      and if bfd_install_relocation gets called the addend may get added
    437      again.  */
    438   HOWTO (R_NDS32_25_PCREL,	/* type */
    439 	 1,			/* rightshift */
    440 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    441 	 24,			/* bitsize */
    442 	 TRUE,			/* pc_relative */
    443 	 0,			/* bitpos */
    444 	 complain_overflow_signed,	/* complain_on_overflow */
    445 	 bfd_elf_generic_reloc,	/* special_function */
    446 	 "R_NDS32_25_PCREL",	/* name */
    447 	 FALSE,			/* partial_inplace */
    448 	 0xffffff,		/* src_mask */
    449 	 0xffffff,		/* dst_mask */
    450 	 TRUE),			/* pcrel_offset */
    451 
    452   /* High 20 bits of address when lower 12 is or'd in.  */
    453   HOWTO (R_NDS32_HI20,		/* type */
    454 	 12,			/* rightshift */
    455 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    456 	 20,			/* bitsize */
    457 	 FALSE,			/* pc_relative */
    458 	 0,			/* bitpos */
    459 	 complain_overflow_dont,/* complain_on_overflow */
    460 	 nds32_elf_hi20_reloc,	/* special_function */
    461 	 "R_NDS32_HI20",	/* name */
    462 	 FALSE,			/* partial_inplace */
    463 	 0x000fffff,		/* src_mask */
    464 	 0x000fffff,		/* dst_mask */
    465 	 FALSE),		/* pcrel_offset */
    466 
    467   /* Lower 12 bits of address.  */
    468   HOWTO (R_NDS32_LO12S3,	/* type */
    469 	 3,			/* rightshift */
    470 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    471 	 9,			/* bitsize */
    472 	 FALSE,			/* pc_relative */
    473 	 0,			/* bitpos */
    474 	 complain_overflow_dont,/* complain_on_overflow */
    475 	 nds32_elf_lo12_reloc,	/* special_function */
    476 	 "R_NDS32_LO12S3",	/* name */
    477 	 FALSE,			/* partial_inplace */
    478 	 0x000001ff,		/* src_mask */
    479 	 0x000001ff,		/* dst_mask */
    480 	 FALSE),		/* pcrel_offset */
    481 
    482   /* Lower 12 bits of address.  */
    483   HOWTO (R_NDS32_LO12S2,	/* type */
    484 	 2,			/* rightshift */
    485 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    486 	 10,			/* bitsize */
    487 	 FALSE,			/* pc_relative */
    488 	 0,			/* bitpos */
    489 	 complain_overflow_dont,/* complain_on_overflow */
    490 	 nds32_elf_lo12_reloc,	/* special_function */
    491 	 "R_NDS32_LO12S2",	/* name */
    492 	 FALSE,			/* partial_inplace */
    493 	 0x000003ff,		/* src_mask */
    494 	 0x000003ff,		/* dst_mask */
    495 	 FALSE),		/* pcrel_offset */
    496 
    497   /* Lower 12 bits of address.  */
    498   HOWTO (R_NDS32_LO12S1,	/* type */
    499 	 1,			/* rightshift */
    500 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    501 	 11,			/* bitsize */
    502 	 FALSE,			/* pc_relative */
    503 	 0,			/* bitpos */
    504 	 complain_overflow_dont,/* complain_on_overflow */
    505 	 nds32_elf_lo12_reloc,	/* special_function */
    506 	 "R_NDS32_LO12S1",	/* name */
    507 	 FALSE,			/* partial_inplace */
    508 	 0x000007ff,		/* src_mask */
    509 	 0x000007ff,		/* dst_mask */
    510 	 FALSE),		/* pcrel_offset */
    511 
    512   /* Lower 12 bits of address.  */
    513   HOWTO (R_NDS32_LO12S0,	/* type */
    514 	 0,			/* rightshift */
    515 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    516 	 12,			/* bitsize */
    517 	 FALSE,			/* pc_relative */
    518 	 0,			/* bitpos */
    519 	 complain_overflow_dont,/* complain_on_overflow */
    520 	 nds32_elf_lo12_reloc,	/* special_function */
    521 	 "R_NDS32_LO12S0",	/* name */
    522 	 FALSE,			/* partial_inplace */
    523 	 0x00000fff,		/* src_mask */
    524 	 0x00000fff,		/* dst_mask */
    525 	 FALSE),		/* pcrel_offset */
    526 
    527   /* Small data area 15 bits offset.  */
    528   HOWTO (R_NDS32_SDA15S3,	/* type */
    529 	 3,			/* rightshift */
    530 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    531 	 15,			/* bitsize */
    532 	 FALSE,			/* pc_relative */
    533 	 0,			/* bitpos */
    534 	 complain_overflow_signed,	/* complain_on_overflow */
    535 	 nds32_elf_sda15_reloc,	/* special_function */
    536 	 "R_NDS32_SDA15S3",	/* name */
    537 	 FALSE,			/* partial_inplace */
    538 	 0x00007fff,		/* src_mask */
    539 	 0x00007fff,		/* dst_mask */
    540 	 FALSE),		/* pcrel_offset */
    541 
    542   /* Small data area 15 bits offset.  */
    543   HOWTO (R_NDS32_SDA15S2,	/* type */
    544 	 2,			/* rightshift */
    545 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    546 	 15,			/* bitsize */
    547 	 FALSE,			/* pc_relative */
    548 	 0,			/* bitpos */
    549 	 complain_overflow_signed,	/* complain_on_overflow */
    550 	 nds32_elf_sda15_reloc,	/* special_function */
    551 	 "R_NDS32_SDA15S2",	/* name */
    552 	 FALSE,			/* partial_inplace */
    553 	 0x00007fff,		/* src_mask */
    554 	 0x00007fff,		/* dst_mask */
    555 	 FALSE),		/* pcrel_offset */
    556 
    557   /* Small data area 15 bits offset.  */
    558   HOWTO (R_NDS32_SDA15S1,	/* type */
    559 	 1,			/* rightshift */
    560 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    561 	 15,			/* bitsize */
    562 	 FALSE,			/* pc_relative */
    563 	 0,			/* bitpos */
    564 	 complain_overflow_signed,	/* complain_on_overflow */
    565 	 nds32_elf_sda15_reloc,	/* special_function */
    566 	 "R_NDS32_SDA15S1",	/* name */
    567 	 FALSE,			/* partial_inplace */
    568 	 0x00007fff,		/* src_mask */
    569 	 0x00007fff,		/* dst_mask */
    570 	 FALSE),		/* pcrel_offset */
    571 
    572   /* Small data area 15 bits offset.  */
    573   HOWTO (R_NDS32_SDA15S0,	/* type */
    574 	 0,			/* rightshift */
    575 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    576 	 15,			/* bitsize */
    577 	 FALSE,			/* pc_relative */
    578 	 0,			/* bitpos */
    579 	 complain_overflow_signed,	/* complain_on_overflow */
    580 	 nds32_elf_sda15_reloc,	/* special_function */
    581 	 "R_NDS32_SDA15S0",	/* name */
    582 	 FALSE,			/* partial_inplace */
    583 	 0x00007fff,		/* src_mask */
    584 	 0x00007fff,		/* dst_mask */
    585 	 FALSE),		/* pcrel_offset */
    586 
    587   /* GNU extension to record C++ vtable hierarchy */
    588   HOWTO (R_NDS32_GNU_VTINHERIT,	/* type */
    589 	 0,			/* rightshift */
    590 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    591 	 0,			/* bitsize */
    592 	 FALSE,			/* pc_relative */
    593 	 0,			/* bitpos */
    594 	 complain_overflow_dont,/* complain_on_overflow */
    595 	 NULL,			/* special_function */
    596 	 "R_NDS32_GNU_VTINHERIT",	/* name */
    597 	 FALSE,			/* partial_inplace */
    598 	 0,			/* src_mask */
    599 	 0,			/* dst_mask */
    600 	 FALSE),		/* pcrel_offset */
    601 
    602   /* GNU extension to record C++ vtable member usage */
    603   HOWTO (R_NDS32_GNU_VTENTRY,	/* type */
    604 	 0,			/* rightshift */
    605 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    606 	 0,			/* bitsize */
    607 	 FALSE,			/* pc_relative */
    608 	 0,			/* bitpos */
    609 	 complain_overflow_dont,/* complain_on_overflow */
    610 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
    611 	 "R_NDS32_GNU_VTENTRY",	/* name */
    612 	 FALSE,			/* partial_inplace */
    613 	 0,			/* src_mask */
    614 	 0,			/* dst_mask */
    615 	 FALSE),		/* pcrel_offset */
    616 
    617   /* A 16 bit absolute relocation.  */
    618   HOWTO (R_NDS32_16_RELA,	/* type */
    619 	 0,			/* rightshift */
    620 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    621 	 16,			/* bitsize */
    622 	 FALSE,			/* pc_relative */
    623 	 0,			/* bitpos */
    624 	 complain_overflow_bitfield,	/* complain_on_overflow */
    625 	 bfd_elf_generic_reloc,	/* special_function */
    626 	 "R_NDS32_16_RELA",	/* name */
    627 	 FALSE,			/* partial_inplace */
    628 	 0xffff,		/* src_mask */
    629 	 0xffff,		/* dst_mask */
    630 	 FALSE),		/* pcrel_offset */
    631 
    632   /* A 32 bit absolute relocation.  */
    633   HOWTO (R_NDS32_32_RELA,	/* type */
    634 	 0,			/* rightshift */
    635 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    636 	 32,			/* bitsize */
    637 	 FALSE,			/* pc_relative */
    638 	 0,			/* bitpos */
    639 	 complain_overflow_bitfield,	/* complain_on_overflow */
    640 	 bfd_elf_generic_reloc,	/* special_function */
    641 	 "R_NDS32_32_RELA",	/* name */
    642 	 FALSE,			/* partial_inplace */
    643 	 0xffffffff,		/* src_mask */
    644 	 0xffffffff,		/* dst_mask */
    645 	 FALSE),		/* pcrel_offset */
    646 
    647   /* A 20 bit address.  */
    648   HOWTO (R_NDS32_20_RELA,	/* type */
    649 	 0,			/* rightshift */
    650 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    651 	 20,			/* bitsize */
    652 	 FALSE,			/* pc_relative */
    653 	 0,			/* bitpos */
    654 	 complain_overflow_signed,	/* complain_on_overflow */
    655 	 bfd_elf_generic_reloc,	/* special_function */
    656 	 "R_NDS32_20_RELA",	/* name */
    657 	 FALSE,			/* partial_inplace */
    658 	 0xfffff,		/* src_mask */
    659 	 0xfffff,		/* dst_mask */
    660 	 FALSE),		/* pcrel_offset */
    661 
    662   HOWTO (R_NDS32_9_PCREL_RELA,	/* type */
    663 	 1,			/* rightshift */
    664 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
    665 	 8,			/* bitsize */
    666 	 TRUE,			/* pc_relative */
    667 	 0,			/* bitpos */
    668 	 complain_overflow_signed,	/* complain_on_overflow */
    669 	 bfd_elf_generic_reloc,	/* special_function */
    670 	 "R_NDS32_9_PCREL_RELA",/* name */
    671 	 FALSE,			/* partial_inplace */
    672 	 0xff,			/* src_mask */
    673 	 0xff,			/* dst_mask */
    674 	 TRUE),			/* pcrel_offset */
    675 
    676   /* A relative 15 bit relocation, right shifted by 1.  */
    677   HOWTO (R_NDS32_15_PCREL_RELA,	/* type */
    678 	 1,			/* rightshift */
    679 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    680 	 14,			/* bitsize */
    681 	 TRUE,			/* pc_relative */
    682 	 0,			/* bitpos */
    683 	 complain_overflow_signed,	/* complain_on_overflow */
    684 	 bfd_elf_generic_reloc,	/* special_function */
    685 	 "R_NDS32_15_PCREL_RELA",	/* name */
    686 	 FALSE,			/* partial_inplace */
    687 	 0x3fff,		/* src_mask */
    688 	 0x3fff,		/* dst_mask */
    689 	 TRUE),			/* pcrel_offset */
    690 
    691   /* A relative 17 bit relocation, right shifted by 1.  */
    692   HOWTO (R_NDS32_17_PCREL_RELA,	/* type */
    693 	 1,			/* rightshift */
    694 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    695 	 16,			/* bitsize */
    696 	 TRUE,			/* pc_relative */
    697 	 0,			/* bitpos */
    698 	 complain_overflow_signed,	/* complain_on_overflow */
    699 	 bfd_elf_generic_reloc,	/* special_function */
    700 	 "R_NDS32_17_PCREL_RELA",	/* name */
    701 	 FALSE,			/* partial_inplace */
    702 	 0xffff,		/* src_mask */
    703 	 0xffff,		/* dst_mask */
    704 	 TRUE),			/* pcrel_offset */
    705 
    706   /* A relative 25 bit relocation, right shifted by 2.  */
    707   HOWTO (R_NDS32_25_PCREL_RELA,	/* type */
    708 	 1,			/* rightshift */
    709 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    710 	 24,			/* bitsize */
    711 	 TRUE,			/* pc_relative */
    712 	 0,			/* bitpos */
    713 	 complain_overflow_signed,	/* complain_on_overflow */
    714 	 bfd_elf_generic_reloc,	/* special_function */
    715 	 "R_NDS32_25_PCREL_RELA",	/* name */
    716 	 FALSE,			/* partial_inplace */
    717 	 0xffffff,		/* src_mask */
    718 	 0xffffff,		/* dst_mask */
    719 	 TRUE),			/* pcrel_offset */
    720 
    721   /* High 20 bits of address when lower 16 is or'd in.  */
    722   HOWTO (R_NDS32_HI20_RELA,	/* type */
    723 	 12,			/* rightshift */
    724 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    725 	 20,			/* bitsize */
    726 	 FALSE,			/* pc_relative */
    727 	 0,			/* bitpos */
    728 	 complain_overflow_dont,/* complain_on_overflow */
    729 	 bfd_elf_generic_reloc,	/* special_function */
    730 	 "R_NDS32_HI20_RELA",	/* name */
    731 	 FALSE,			/* partial_inplace */
    732 	 0x000fffff,		/* src_mask */
    733 	 0x000fffff,		/* dst_mask */
    734 	 FALSE),		/* pcrel_offset */
    735 
    736   /* Lower 12 bits of address.  */
    737   HOWTO (R_NDS32_LO12S3_RELA,	/* type */
    738 	 3,			/* rightshift */
    739 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    740 	 9,			/* bitsize */
    741 	 FALSE,			/* pc_relative */
    742 	 0,			/* bitpos */
    743 	 complain_overflow_dont,/* complain_on_overflow */
    744 	 bfd_elf_generic_reloc,	/* special_function */
    745 	 "R_NDS32_LO12S3_RELA",	/* name */
    746 	 FALSE,			/* partial_inplace */
    747 	 0x000001ff,		/* src_mask */
    748 	 0x000001ff,		/* dst_mask */
    749 	 FALSE),		/* pcrel_offset */
    750 
    751   /* Lower 12 bits of address.  */
    752   HOWTO (R_NDS32_LO12S2_RELA,	/* type */
    753 	 2,			/* rightshift */
    754 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    755 	 10,			/* bitsize */
    756 	 FALSE,			/* pc_relative */
    757 	 0,			/* bitpos */
    758 	 complain_overflow_dont,/* complain_on_overflow */
    759 	 bfd_elf_generic_reloc,	/* special_function */
    760 	 "R_NDS32_LO12S2_RELA",	/* name */
    761 	 FALSE,			/* partial_inplace */
    762 	 0x000003ff,		/* src_mask */
    763 	 0x000003ff,		/* dst_mask */
    764 	 FALSE),		/* pcrel_offset */
    765 
    766   /* Lower 12 bits of address.  */
    767   HOWTO (R_NDS32_LO12S1_RELA,	/* type */
    768 	 1,			/* rightshift */
    769 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    770 	 11,			/* bitsize */
    771 	 FALSE,			/* pc_relative */
    772 	 0,			/* bitpos */
    773 	 complain_overflow_dont,/* complain_on_overflow */
    774 	 bfd_elf_generic_reloc,	/* special_function */
    775 	 "R_NDS32_LO12S1_RELA",	/* name */
    776 	 FALSE,			/* partial_inplace */
    777 	 0x000007ff,		/* src_mask */
    778 	 0x000007ff,		/* dst_mask */
    779 	 FALSE),		/* pcrel_offset */
    780 
    781   /* Lower 12 bits of address.  */
    782   HOWTO (R_NDS32_LO12S0_RELA,	/* type */
    783 	 0,			/* rightshift */
    784 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    785 	 12,			/* bitsize */
    786 	 FALSE,			/* pc_relative */
    787 	 0,			/* bitpos */
    788 	 complain_overflow_dont,/* complain_on_overflow */
    789 	 bfd_elf_generic_reloc,	/* special_function */
    790 	 "R_NDS32_LO12S0_RELA",	/* name */
    791 	 FALSE,			/* partial_inplace */
    792 	 0x00000fff,		/* src_mask */
    793 	 0x00000fff,		/* dst_mask */
    794 	 FALSE),		/* pcrel_offset */
    795 
    796   /* Small data area 15 bits offset.  */
    797   HOWTO (R_NDS32_SDA15S3_RELA,	/* type */
    798 	 3,			/* rightshift */
    799 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    800 	 15,			/* bitsize */
    801 	 FALSE,			/* pc_relative */
    802 	 0,			/* bitpos */
    803 	 complain_overflow_signed,	/* complain_on_overflow */
    804 	 bfd_elf_generic_reloc,	/* special_function */
    805 	 "R_NDS32_SDA15S3_RELA",/* name */
    806 	 FALSE,			/* partial_inplace */
    807 	 0x00007fff,		/* src_mask */
    808 	 0x00007fff,		/* dst_mask */
    809 	 FALSE),		/* pcrel_offset */
    810 
    811   /* Small data area 15 bits offset.  */
    812   HOWTO (R_NDS32_SDA15S2_RELA,	/* type */
    813 	 2,			/* rightshift */
    814 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    815 	 15,			/* bitsize */
    816 	 FALSE,			/* pc_relative */
    817 	 0,			/* bitpos */
    818 	 complain_overflow_signed,	/* complain_on_overflow */
    819 	 bfd_elf_generic_reloc,	/* special_function */
    820 	 "R_NDS32_SDA15S2_RELA",/* name */
    821 	 FALSE,			/* partial_inplace */
    822 	 0x00007fff,		/* src_mask */
    823 	 0x00007fff,		/* dst_mask */
    824 	 FALSE),		/* pcrel_offset */
    825 
    826   HOWTO (R_NDS32_SDA15S1_RELA,	/* type */
    827 	 1,			/* rightshift */
    828 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    829 	 15,			/* bitsize */
    830 	 FALSE,			/* pc_relative */
    831 	 0,			/* bitpos */
    832 	 complain_overflow_signed,	/* complain_on_overflow */
    833 	 bfd_elf_generic_reloc,	/* special_function */
    834 	 "R_NDS32_SDA15S1_RELA",/* name */
    835 	 FALSE,			/* partial_inplace */
    836 	 0x00007fff,		/* src_mask */
    837 	 0x00007fff,		/* dst_mask */
    838 	 FALSE),		/* pcrel_offset */
    839 
    840   HOWTO (R_NDS32_SDA15S0_RELA,	/* type */
    841 	 0,			/* rightshift */
    842 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    843 	 15,			/* bitsize */
    844 	 FALSE,			/* pc_relative */
    845 	 0,			/* bitpos */
    846 	 complain_overflow_signed,	/* complain_on_overflow */
    847 	 bfd_elf_generic_reloc,	/* special_function */
    848 	 "R_NDS32_SDA15S0_RELA",/* name */
    849 	 FALSE,			/* partial_inplace */
    850 	 0x00007fff,		/* src_mask */
    851 	 0x00007fff,		/* dst_mask */
    852 	 FALSE),		/* pcrel_offset */
    853 
    854   /* GNU extension to record C++ vtable hierarchy */
    855   HOWTO (R_NDS32_RELA_GNU_VTINHERIT,	/* type */
    856 	 0,			/* rightshift */
    857 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    858 	 0,			/* bitsize */
    859 	 FALSE,			/* pc_relative */
    860 	 0,			/* bitpos */
    861 	 complain_overflow_dont,/* complain_on_overflow */
    862 	 NULL,			/* special_function */
    863 	 "R_NDS32_RELA_GNU_VTINHERIT",	/* name */
    864 	 FALSE,			/* partial_inplace */
    865 	 0,			/* src_mask */
    866 	 0,			/* dst_mask */
    867 	 FALSE),		/* pcrel_offset */
    868 
    869   /* GNU extension to record C++ vtable member usage */
    870   HOWTO (R_NDS32_RELA_GNU_VTENTRY,	/* type */
    871 	 0,			/* rightshift */
    872 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    873 	 0,			/* bitsize */
    874 	 FALSE,			/* pc_relative */
    875 	 0,			/* bitpos */
    876 	 complain_overflow_dont,/* complain_on_overflow */
    877 	 _bfd_elf_rel_vtable_reloc_fn,	/* special_function */
    878 	 "R_NDS32_RELA_GNU_VTENTRY",	/* name */
    879 	 FALSE,			/* partial_inplace */
    880 	 0,			/* src_mask */
    881 	 0,			/* dst_mask */
    882 	 FALSE),		/* pcrel_offset */
    883 
    884   /* Like R_NDS32_20, but referring to the GOT table entry for
    885      the symbol.  */
    886   HOWTO (R_NDS32_GOT20,		/* type */
    887 	 0,			/* rightshift */
    888 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    889 	 20,			/* bitsize */
    890 	 FALSE,			/* pc_relative */
    891 	 0,			/* bitpos */
    892 	 complain_overflow_signed,	/* complain_on_overflow */
    893 	 bfd_elf_generic_reloc,	/* special_function */
    894 	 "R_NDS32_GOT20",	/* name */
    895 	 FALSE,			/* partial_inplace */
    896 	 0xfffff,		/* src_mask */
    897 	 0xfffff,		/* dst_mask */
    898 	 FALSE),		/* pcrel_offset */
    899 
    900   /* Like R_NDS32_PCREL, but referring to the procedure linkage table
    901      entry for the symbol.  */
    902   HOWTO (R_NDS32_25_PLTREL,	/* type */
    903 	 1,			/* rightshift */
    904 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    905 	 24,			/* bitsize */
    906 	 TRUE,			/* pc_relative */
    907 	 0,			/* bitpos */
    908 	 complain_overflow_signed,	/* complain_on_overflow */
    909 	 bfd_elf_generic_reloc,	/* special_function */
    910 	 "R_NDS32_25_PLTREL",	/* name */
    911 	 FALSE,			/* partial_inplace */
    912 	 0xffffff,		/* src_mask */
    913 	 0xffffff,		/* dst_mask */
    914 	 TRUE),			/* pcrel_offset */
    915 
    916   /* This is used only by the dynamic linker.  The symbol should exist
    917      both in the object being run and in some shared library.  The
    918      dynamic linker copies the data addressed by the symbol from the
    919      shared library into the object, because the object being
    920      run has to have the data at some particular address.  */
    921   HOWTO (R_NDS32_COPY,		/* type */
    922 	 0,			/* rightshift */
    923 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    924 	 32,			/* bitsize */
    925 	 FALSE,			/* pc_relative */
    926 	 0,			/* bitpos */
    927 	 complain_overflow_bitfield,	/* complain_on_overflow */
    928 	 bfd_elf_generic_reloc,	/* special_function */
    929 	 "R_NDS32_COPY",	/* name */
    930 	 FALSE,			/* partial_inplace */
    931 	 0xffffffff,		/* src_mask */
    932 	 0xffffffff,		/* dst_mask */
    933 	 FALSE),		/* pcrel_offset */
    934 
    935   /* Like R_NDS32_20, but used when setting global offset table
    936      entries.  */
    937   HOWTO (R_NDS32_GLOB_DAT,	/* type */
    938 	 0,			/* rightshift */
    939 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    940 	 32,			/* bitsize */
    941 	 FALSE,			/* pc_relative */
    942 	 0,			/* bitpos */
    943 	 complain_overflow_bitfield,	/* complain_on_overflow */
    944 	 bfd_elf_generic_reloc,	/* special_function */
    945 	 "R_NDS32_GLOB_DAT",	/* name */
    946 	 FALSE,			/* partial_inplace */
    947 	 0xffffffff,		/* src_mask */
    948 	 0xffffffff,		/* dst_mask */
    949 	 FALSE),		/* pcrel_offset */
    950 
    951   /* Marks a procedure linkage table entry for a symbol.  */
    952   HOWTO (R_NDS32_JMP_SLOT,	/* type */
    953 	 0,			/* rightshift */
    954 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    955 	 32,			/* bitsize */
    956 	 FALSE,			/* pc_relative */
    957 	 0,			/* bitpos */
    958 	 complain_overflow_bitfield,	/* complain_on_overflow */
    959 	 bfd_elf_generic_reloc,	/* special_function */
    960 	 "R_NDS32_JMP_SLOT",	/* name */
    961 	 FALSE,			/* partial_inplace */
    962 	 0xffffffff,		/* src_mask */
    963 	 0xffffffff,		/* dst_mask */
    964 	 FALSE),		/* pcrel_offset */
    965 
    966   /* Used only by the dynamic linker.  When the object is run, this
    967      longword is set to the load address of the object, plus the
    968      addend.  */
    969   HOWTO (R_NDS32_RELATIVE,	/* type */
    970 	 0,			/* rightshift */
    971 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    972 	 32,			/* bitsize */
    973 	 FALSE,			/* pc_relative */
    974 	 0,			/* bitpos */
    975 	 complain_overflow_bitfield,	/* complain_on_overflow */
    976 	 bfd_elf_generic_reloc,	/* special_function */
    977 	 "R_NDS32_RELATIVE",	/* name */
    978 	 FALSE,			/* partial_inplace */
    979 	 0xffffffff,		/* src_mask */
    980 	 0xffffffff,		/* dst_mask */
    981 	 FALSE),		/* pcrel_offset */
    982 
    983   HOWTO (R_NDS32_GOTOFF,	/* type */
    984 	 0,			/* rightshift */
    985 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
    986 	 20,			/* bitsize */
    987 	 FALSE,			/* pc_relative */
    988 	 0,			/* bitpos */
    989 	 complain_overflow_signed,	/* complain_on_overflow */
    990 	 bfd_elf_generic_reloc,	/* special_function */
    991 	 "R_NDS32_GOTOFF",	/* name */
    992 	 FALSE,			/* partial_inplace */
    993 	 0xfffff,		/* src_mask */
    994 	 0xfffff,		/* dst_mask */
    995 	 FALSE),		/* pcrel_offset */
    996 
    997   /* An PC Relative 20-bit relocation used when setting PIC offset
    998      table register.  */
    999   HOWTO (R_NDS32_GOTPC20,	/* type */
   1000 	 0,			/* rightshift */
   1001 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1002 	 20,			/* bitsize */
   1003 	 TRUE,			/* pc_relative */
   1004 	 0,			/* bitpos */
   1005 	 complain_overflow_signed,	/* complain_on_overflow */
   1006 	 bfd_elf_generic_reloc,	/* special_function */
   1007 	 "R_NDS32_GOTPC20",	/* name */
   1008 	 FALSE,			/* partial_inplace */
   1009 	 0xfffff,		/* src_mask */
   1010 	 0xfffff,		/* dst_mask */
   1011 	 TRUE),			/* pcrel_offset */
   1012 
   1013   /* Like R_NDS32_HI20, but referring to the GOT table entry for
   1014      the symbol.  */
   1015   HOWTO (R_NDS32_GOT_HI20,	/* type */
   1016 	 12,			/* rightshift */
   1017 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1018 	 20,			/* bitsize */
   1019 	 FALSE,			/* pc_relative */
   1020 	 0,			/* bitpos */
   1021 	 complain_overflow_dont,/* complain_on_overflow */
   1022 	 bfd_elf_generic_reloc,	/* special_function */
   1023 	 "R_NDS32_GOT_HI20",	/* name */
   1024 	 FALSE,			/* partial_inplace */
   1025 	 0x000fffff,		/* src_mask */
   1026 	 0x000fffff,		/* dst_mask */
   1027 	 FALSE),		/* pcrel_offset */
   1028   HOWTO (R_NDS32_GOT_LO12,	/* type */
   1029 	 0,			/* rightshift */
   1030 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1031 	 12,			/* bitsize */
   1032 	 FALSE,			/* pc_relative */
   1033 	 0,			/* bitpos */
   1034 	 complain_overflow_dont,/* complain_on_overflow */
   1035 	 bfd_elf_generic_reloc,	/* special_function */
   1036 	 "R_NDS32_GOT_LO12",	/* name */
   1037 	 FALSE,			/* partial_inplace */
   1038 	 0x00000fff,		/* src_mask */
   1039 	 0x00000fff,		/* dst_mask */
   1040 	 FALSE),		/* pcrel_offset */
   1041 
   1042   /* An PC Relative relocation used when setting PIC offset table register.
   1043      Like R_NDS32_HI20, but referring to the GOT table entry for
   1044      the symbol.  */
   1045   HOWTO (R_NDS32_GOTPC_HI20,	/* type */
   1046 	 12,			/* rightshift */
   1047 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1048 	 20,			/* bitsize */
   1049 	 FALSE,			/* pc_relative */
   1050 	 0,			/* bitpos */
   1051 	 complain_overflow_dont,/* complain_on_overflow */
   1052 	 bfd_elf_generic_reloc,	/* special_function */
   1053 	 "R_NDS32_GOTPC_HI20",	/* name */
   1054 	 FALSE,			/* partial_inplace */
   1055 	 0x000fffff,		/* src_mask */
   1056 	 0x000fffff,		/* dst_mask */
   1057 	 TRUE),			/* pcrel_offset */
   1058   HOWTO (R_NDS32_GOTPC_LO12,	/* type */
   1059 	 0,			/* rightshift */
   1060 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1061 	 12,			/* bitsize */
   1062 	 FALSE,			/* pc_relative */
   1063 	 0,			/* bitpos */
   1064 	 complain_overflow_dont,	/* complain_on_overflow */
   1065 	 bfd_elf_generic_reloc,	/* special_function */
   1066 	 "R_NDS32_GOTPC_LO12",	/* name */
   1067 	 FALSE,			/* partial_inplace */
   1068 	 0x00000fff,		/* src_mask */
   1069 	 0x00000fff,		/* dst_mask */
   1070 	 TRUE),			/* pcrel_offset */
   1071 
   1072   HOWTO (R_NDS32_GOTOFF_HI20,	/* type */
   1073 	 12,			/* rightshift */
   1074 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1075 	 20,			/* bitsize */
   1076 	 FALSE,			/* pc_relative */
   1077 	 0,			/* bitpos */
   1078 	 complain_overflow_dont,/* complain_on_overflow */
   1079 	 bfd_elf_generic_reloc,	/* special_function */
   1080 	 "R_NDS32_GOTOFF_HI20",	/* name */
   1081 	 FALSE,			/* partial_inplace */
   1082 	 0x000fffff,		/* src_mask */
   1083 	 0x000fffff,		/* dst_mask */
   1084 	 FALSE),		/* pcrel_offset */
   1085   HOWTO (R_NDS32_GOTOFF_LO12,	/* type */
   1086 	 0,			/* rightshift */
   1087 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1088 	 12,			/* bitsize */
   1089 	 FALSE,			/* pc_relative */
   1090 	 0,			/* bitpos */
   1091 	 complain_overflow_dont,/* complain_on_overflow */
   1092 	 bfd_elf_generic_reloc,	/* special_function */
   1093 	 "R_NDS32_GOTOFF_LO12",	/* name */
   1094 	 FALSE,			/* partial_inplace */
   1095 	 0x00000fff,		/* src_mask */
   1096 	 0x00000fff,		/* dst_mask */
   1097 	 FALSE),		/* pcrel_offset */
   1098 
   1099   /* Alignment hint for relaxable instruction.  This is used with
   1100      R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
   1101      in order to make next label aligned on word boundary.  */
   1102   HOWTO (R_NDS32_INSN16,	/* type */
   1103 	 0,			/* rightshift */
   1104 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1105 	 32,			/* bitsize */
   1106 	 FALSE,			/* pc_relative */
   1107 	 0,			/* bitpos */
   1108 	 complain_overflow_dont,/* complain_on_overflow */
   1109 	 nds32_elf_ignore_reloc,/* special_function */
   1110 	 "R_NDS32_INSN16",	/* name */
   1111 	 FALSE,			/* partial_inplace */
   1112 	 0x00000fff,		/* src_mask */
   1113 	 0x00000fff,		/* dst_mask */
   1114 	 FALSE),		/* pcrel_offset */
   1115 
   1116   /* Alignment hint for label.  */
   1117   HOWTO (R_NDS32_LABEL,		/* type */
   1118 	 0,			/* rightshift */
   1119 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1120 	 32,			/* bitsize */
   1121 	 FALSE,			/* pc_relative */
   1122 	 0,			/* bitpos */
   1123 	 complain_overflow_dont,/* complain_on_overflow */
   1124 	 nds32_elf_ignore_reloc,/* special_function */
   1125 	 "R_NDS32_LABEL",	/* name */
   1126 	 FALSE,			/* partial_inplace */
   1127 	 0xffffffff,		/* src_mask */
   1128 	 0xffffffff,		/* dst_mask */
   1129 	 FALSE),		/* pcrel_offset */
   1130 
   1131   /* Relax hint for unconditional call sequence  */
   1132   HOWTO (R_NDS32_LONGCALL1,	/* type */
   1133 	 0,			/* rightshift */
   1134 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1135 	 32,			/* bitsize */
   1136 	 FALSE,			/* pc_relative */
   1137 	 0,			/* bitpos */
   1138 	 complain_overflow_dont,/* complain_on_overflow */
   1139 	 nds32_elf_ignore_reloc,/* special_function */
   1140 	 "R_NDS32_LONGCALL1",	/* name */
   1141 	 FALSE,			/* partial_inplace */
   1142 	 0xffffffff,		/* src_mask */
   1143 	 0xffffffff,		/* dst_mask */
   1144 	 FALSE),		/* pcrel_offset */
   1145 
   1146   /* Relax hint for conditional call sequence.  */
   1147   HOWTO (R_NDS32_LONGCALL2,	/* type */
   1148 	 0,			/* rightshift */
   1149 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1150 	 32,			/* bitsize */
   1151 	 FALSE,			/* pc_relative */
   1152 	 0,			/* bitpos */
   1153 	 complain_overflow_dont,/* complain_on_overflow */
   1154 	 nds32_elf_ignore_reloc,/* special_function */
   1155 	 "R_NDS32_LONGCALL2",	/* name */
   1156 	 FALSE,			/* partial_inplace */
   1157 	 0xffffffff,		/* src_mask */
   1158 	 0xffffffff,		/* dst_mask */
   1159 	 FALSE),		/* pcrel_offset */
   1160 
   1161   /* Relax hint for conditional call sequence.  */
   1162   HOWTO (R_NDS32_LONGCALL3,	/* type */
   1163 	 0,			/* rightshift */
   1164 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1165 	 32,			/* bitsize */
   1166 	 FALSE,			/* pc_relative */
   1167 	 0,			/* bitpos */
   1168 	 complain_overflow_dont,/* complain_on_overflow */
   1169 	 nds32_elf_ignore_reloc,/* special_function */
   1170 	 "R_NDS32_LONGCALL3",	/* name */
   1171 	 FALSE,			/* partial_inplace */
   1172 	 0xffffffff,		/* src_mask */
   1173 	 0xffffffff,		/* dst_mask */
   1174 	 FALSE),		/* pcrel_offset */
   1175 
   1176   /* Relax hint for unconditional branch sequence.  */
   1177   HOWTO (R_NDS32_LONGJUMP1,	/* type */
   1178 	 0,			/* rightshift */
   1179 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1180 	 32,			/* bitsize */
   1181 	 FALSE,			/* pc_relative */
   1182 	 0,			/* bitpos */
   1183 	 complain_overflow_dont,/* complain_on_overflow */
   1184 	 nds32_elf_ignore_reloc,/* special_function */
   1185 	 "R_NDS32_LONGJUMP1",	/* name */
   1186 	 FALSE,			/* partial_inplace */
   1187 	 0xffffffff,		/* src_mask */
   1188 	 0xffffffff,		/* dst_mask */
   1189 	 FALSE),		/* pcrel_offset */
   1190 
   1191   /* Relax hint for conditional branch sequence.  */
   1192   HOWTO (R_NDS32_LONGJUMP2,	/* type */
   1193 	 0,			/* rightshift */
   1194 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1195 	 32,			/* bitsize */
   1196 	 FALSE,			/* pc_relative */
   1197 	 0,			/* bitpos */
   1198 	 complain_overflow_dont,/* complain_on_overflow */
   1199 	 nds32_elf_ignore_reloc,/* special_function */
   1200 	 "R_NDS32_LONGJUMP2",	/* name */
   1201 	 FALSE,			/* partial_inplace */
   1202 	 0xffffffff,		/* src_mask */
   1203 	 0xffffffff,		/* dst_mask */
   1204 	 FALSE),		/* pcrel_offset */
   1205 
   1206   /* Relax hint for conditional branch sequence.  */
   1207   HOWTO (R_NDS32_LONGJUMP3,	/* type */
   1208 	 0,			/* rightshift */
   1209 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1210 	 32,			/* bitsize */
   1211 	 FALSE,			/* pc_relative */
   1212 	 0,			/* bitpos */
   1213 	 complain_overflow_dont,/* complain_on_overflow */
   1214 	 nds32_elf_ignore_reloc,/* special_function */
   1215 	 "R_NDS32_LONGJUMP3",	/* name */
   1216 	 FALSE,			/* partial_inplace */
   1217 	 0xffffffff,		/* src_mask */
   1218 	 0xffffffff,		/* dst_mask */
   1219 	 FALSE),		/* pcrel_offset */
   1220 
   1221   /* Relax hint for load/store sequence.   */
   1222   HOWTO (R_NDS32_LOADSTORE,	/* type */
   1223 	 0,			/* rightshift */
   1224 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1225 	 32,			/* bitsize */
   1226 	 FALSE,			/* pc_relative */
   1227 	 0,			/* bitpos */
   1228 	 complain_overflow_dont,/* complain_on_overflow */
   1229 	 nds32_elf_ignore_reloc,/* special_function */
   1230 	 "R_NDS32_LOADSTORE",	/* name */
   1231 	 FALSE,			/* partial_inplace */
   1232 	 0xffffffff,		/* src_mask */
   1233 	 0xffffffff,		/* dst_mask */
   1234 	 FALSE),		/* pcrel_offset */
   1235 
   1236   /* Relax hint for load/store sequence.  */
   1237   HOWTO (R_NDS32_9_FIXED_RELA,	/* type */
   1238 	 0,			/* rightshift */
   1239 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1240 	 16,			/* bitsize */
   1241 	 FALSE,			/* pc_relative */
   1242 	 0,			/* bitpos */
   1243 	 complain_overflow_dont,/* complain_on_overflow */
   1244 	 nds32_elf_ignore_reloc,/* special_function */
   1245 	 "R_NDS32_9_FIXED_RELA",/* name */
   1246 	 FALSE,			/* partial_inplace */
   1247 	 0x000000ff,		/* src_mask */
   1248 	 0x000000ff,		/* dst_mask */
   1249 	 FALSE),		/* pcrel_offset */
   1250 
   1251   /* Relax hint for load/store sequence.  */
   1252   HOWTO (R_NDS32_15_FIXED_RELA,	/* type */
   1253 	 0,			/* rightshift */
   1254 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1255 	 32,			/* bitsize */
   1256 	 FALSE,			/* pc_relative */
   1257 	 0,			/* bitpos */
   1258 	 complain_overflow_dont,/* complain_on_overflow */
   1259 	 nds32_elf_ignore_reloc,/* special_function */
   1260 	 "R_NDS32_15_FIXED_RELA",	/* name */
   1261 	 FALSE,			/* partial_inplace */
   1262 	 0x00003fff,		/* src_mask */
   1263 	 0x00003fff,		/* dst_mask */
   1264 	 FALSE),		/* pcrel_offset */
   1265 
   1266   /* Relax hint for load/store sequence.  */
   1267   HOWTO (R_NDS32_17_FIXED_RELA,	/* type */
   1268 	 0,			/* rightshift */
   1269 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1270 	 32,			/* bitsize */
   1271 	 FALSE,			/* pc_relative */
   1272 	 0,			/* bitpos */
   1273 	 complain_overflow_dont,/* complain_on_overflow */
   1274 	 nds32_elf_ignore_reloc,/* special_function */
   1275 	 "R_NDS32_17_FIXED_RELA",	/* name */
   1276 	 FALSE,			/* partial_inplace */
   1277 	 0x0000ffff,		/* src_mask */
   1278 	 0x0000ffff,		/* dst_mask */
   1279 	 FALSE),		/* pcrel_offset */
   1280 
   1281   /* Relax hint for load/store sequence.  */
   1282   HOWTO (R_NDS32_25_FIXED_RELA,	/* type */
   1283 	 0,			/* rightshift */
   1284 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1285 	 32,			/* bitsize */
   1286 	 FALSE,			/* pc_relative */
   1287 	 0,			/* bitpos */
   1288 	 complain_overflow_dont,/* complain_on_overflow */
   1289 	 nds32_elf_ignore_reloc,/* special_function */
   1290 	 "R_NDS32_25_FIXED_RELA",	/* name */
   1291 	 FALSE,			/* partial_inplace */
   1292 	 0x00ffffff,		/* src_mask */
   1293 	 0x00ffffff,		/* dst_mask */
   1294 	 FALSE),		/* pcrel_offset */
   1295 
   1296   /* High 20 bits of PLT symbol offset relative to PC.  */
   1297   HOWTO (R_NDS32_PLTREL_HI20,	/* type */
   1298 	 12,			/* rightshift */
   1299 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1300 	 20,			/* bitsize */
   1301 	 FALSE,			/* pc_relative */
   1302 	 0,			/* bitpos */
   1303 	 complain_overflow_dont,/* complain_on_overflow */
   1304 	 bfd_elf_generic_reloc,	/* special_function */
   1305 	 "R_NDS32_PLTREL_HI20",	/* name */
   1306 	 FALSE,			/* partial_inplace */
   1307 	 0x000fffff,		/* src_mask */
   1308 	 0x000fffff,		/* dst_mask */
   1309 	 FALSE),		/* pcrel_offset */
   1310 
   1311   /* Low 12 bits of PLT symbol offset relative to PC.  */
   1312   HOWTO (R_NDS32_PLTREL_LO12,	/* type */
   1313 	 0,			/* rightshift */
   1314 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1315 	 12,			/* bitsize */
   1316 	 FALSE,			/* pc_relative */
   1317 	 0,			/* bitpos */
   1318 	 complain_overflow_dont,/* complain_on_overflow */
   1319 	 bfd_elf_generic_reloc,	/* special_function */
   1320 	 "R_NDS32_PLTREL_LO12",	/* name */
   1321 	 FALSE,			/* partial_inplace */
   1322 	 0x00000fff,		/* src_mask */
   1323 	 0x00000fff,		/* dst_mask */
   1324 	 FALSE),		/* pcrel_offset */
   1325 
   1326   /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
   1327   HOWTO (R_NDS32_PLT_GOTREL_HI20,	/* type */
   1328 	 12,			/* rightshift */
   1329 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1330 	 20,			/* bitsize */
   1331 	 FALSE,			/* pc_relative */
   1332 	 0,			/* bitpos */
   1333 	 complain_overflow_dont,/* complain_on_overflow */
   1334 	 bfd_elf_generic_reloc,	/* special_function */
   1335 	 "R_NDS32_PLT_GOTREL_HI20",	/* name */
   1336 	 FALSE,			/* partial_inplace */
   1337 	 0x000fffff,		/* src_mask */
   1338 	 0x000fffff,		/* dst_mask */
   1339 	 FALSE),		/* pcrel_offset */
   1340 
   1341   /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
   1342   HOWTO (R_NDS32_PLT_GOTREL_LO12,	/* type */
   1343 	 0,			/* rightshift */
   1344 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1345 	 12,			/* bitsize */
   1346 	 FALSE,			/* pc_relative */
   1347 	 0,			/* bitpos */
   1348 	 complain_overflow_dont,/* complain_on_overflow */
   1349 	 bfd_elf_generic_reloc,	/* special_function */
   1350 	 "R_NDS32_PLT_GOTREL_LO12",	/* name */
   1351 	 FALSE,			/* partial_inplace */
   1352 	 0x00000fff,		/* src_mask */
   1353 	 0x00000fff,		/* dst_mask */
   1354 	 FALSE),		/* pcrel_offset */
   1355 
   1356   /* Small data area 12 bits offset.  */
   1357   HOWTO (R_NDS32_SDA12S2_DP_RELA,	/* type */
   1358 	 2,			/* rightshift */
   1359 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1360 	 12,			/* bitsize */
   1361 	 FALSE,			/* pc_relative */
   1362 	 0,			/* bitpos */
   1363 	 complain_overflow_signed,	/* complain_on_overflow */
   1364 	 bfd_elf_generic_reloc,	/* special_function */
   1365 	 "R_NDS32_SDA12S2_DP_RELA",	/* name */
   1366 	 FALSE,			/* partial_inplace */
   1367 	 0x00000fff,		/* src_mask */
   1368 	 0x00000fff,		/* dst_mask */
   1369 	 FALSE),		/* pcrel_offset */
   1370 
   1371   /* Small data area 12 bits offset.  */
   1372   HOWTO (R_NDS32_SDA12S2_SP_RELA,	/* type */
   1373 	 2,			/* rightshift */
   1374 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1375 	 12,			/* bitsize */
   1376 	 FALSE,			/* pc_relative */
   1377 	 0,			/* bitpos */
   1378 	 complain_overflow_signed,	/* complain_on_overflow */
   1379 	 bfd_elf_generic_reloc,	/* special_function */
   1380 	 "R_NDS32_SDA12S2_SP_RELA",	/* name */
   1381 	 FALSE,			/* partial_inplace */
   1382 	 0x00000fff,		/* src_mask */
   1383 	 0x00000fff,		/* dst_mask */
   1384 	 FALSE),		/* pcrel_offset */
   1385   /* Lower 12 bits of address.  */
   1386 
   1387   HOWTO (R_NDS32_LO12S2_DP_RELA,	/* type */
   1388 	 2,			/* rightshift */
   1389 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1390 	 10,			/* bitsize */
   1391 	 FALSE,			/* pc_relative */
   1392 	 0,			/* bitpos */
   1393 	 complain_overflow_dont,/* complain_on_overflow */
   1394 	 bfd_elf_generic_reloc,	/* special_function */
   1395 	 "R_NDS32_LO12S2_DP_RELA",	/* name */
   1396 	 FALSE,			/* partial_inplace */
   1397 	 0x000003ff,		/* src_mask */
   1398 	 0x000003ff,		/* dst_mask */
   1399 	 FALSE),		/* pcrel_offset */
   1400 
   1401   /* Lower 12 bits of address.  */
   1402   HOWTO (R_NDS32_LO12S2_SP_RELA,/* type */
   1403 	 2,			/* rightshift */
   1404 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1405 	 10,			/* bitsize */
   1406 	 FALSE,			/* pc_relative */
   1407 	 0,			/* bitpos */
   1408 	 complain_overflow_dont,/* complain_on_overflow */
   1409 	 bfd_elf_generic_reloc,	/* special_function */
   1410 	 "R_NDS32_LO12S2_SP_RELA",	/* name */
   1411 	 FALSE,			/* partial_inplace */
   1412 	 0x000003ff,		/* src_mask */
   1413 	 0x000003ff,		/* dst_mask */
   1414 	 FALSE),		/* pcrel_offset */
   1415   /* Lower 12 bits of address.  Special identity for or case.  */
   1416   HOWTO (R_NDS32_LO12S0_ORI_RELA,	/* type */
   1417 	 0,			/* rightshift */
   1418 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1419 	 12,			/* bitsize */
   1420 	 FALSE,			/* pc_relative */
   1421 	 0,			/* bitpos */
   1422 	 complain_overflow_dont,/* complain_on_overflow */
   1423 	 bfd_elf_generic_reloc,	/* special_function */
   1424 	 "R_NDS32_LO12S0_ORI_RELA",	/* name */
   1425 	 FALSE,			/* partial_inplace */
   1426 	 0x00000fff,		/* src_mask */
   1427 	 0x00000fff,		/* dst_mask */
   1428 	 FALSE),		/* pcrel_offset */
   1429   /* Small data area 19 bits offset.  */
   1430   HOWTO (R_NDS32_SDA16S3_RELA,	/* type */
   1431 	 3,			/* rightshift */
   1432 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1433 	 16,			/* bitsize */
   1434 	 FALSE,			/* pc_relative */
   1435 	 0,			/* bitpos */
   1436 	 complain_overflow_signed,	/* complain_on_overflow */
   1437 	 bfd_elf_generic_reloc,	/* special_function */
   1438 	 "R_NDS32_SDA16S3_RELA",/* name */
   1439 	 FALSE,			/* partial_inplace */
   1440 	 0x0000ffff,		/* src_mask */
   1441 	 0x0000ffff,		/* dst_mask */
   1442 	 FALSE),		/* pcrel_offset */
   1443 
   1444   /* Small data area 15 bits offset.  */
   1445   HOWTO (R_NDS32_SDA17S2_RELA,	/* type */
   1446 	 2,			/* rightshift */
   1447 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1448 	 17,			/* bitsize */
   1449 	 FALSE,			/* pc_relative */
   1450 	 0,			/* bitpos */
   1451 	 complain_overflow_signed,	/* complain_on_overflow */
   1452 	 bfd_elf_generic_reloc,	/* special_function */
   1453 	 "R_NDS32_SDA17S2_RELA",/* name */
   1454 	 FALSE,			/* partial_inplace */
   1455 	 0x0001ffff,		/* src_mask */
   1456 	 0x0001ffff,		/* dst_mask */
   1457 	 FALSE),		/* pcrel_offset */
   1458 
   1459   HOWTO (R_NDS32_SDA18S1_RELA,	/* type */
   1460 	 1,			/* rightshift */
   1461 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1462 	 18,			/* bitsize */
   1463 	 FALSE,			/* pc_relative */
   1464 	 0,			/* bitpos */
   1465 	 complain_overflow_signed,	/* complain_on_overflow */
   1466 	 bfd_elf_generic_reloc,	/* special_function */
   1467 	 "R_NDS32_SDA18S1_RELA",/* name */
   1468 	 FALSE,			/* partial_inplace */
   1469 	 0x0003ffff,		/* src_mask */
   1470 	 0x0003ffff,		/* dst_mask */
   1471 	 FALSE),		/* pcrel_offset */
   1472 
   1473   HOWTO (R_NDS32_SDA19S0_RELA,	/* type */
   1474 	 0,			/* rightshift */
   1475 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1476 	 19,			/* bitsize */
   1477 	 FALSE,			/* pc_relative */
   1478 	 0,			/* bitpos */
   1479 	 complain_overflow_signed,	/* complain_on_overflow */
   1480 	 bfd_elf_generic_reloc,	/* special_function */
   1481 	 "R_NDS32_SDA19S0_RELA",/* name */
   1482 	 FALSE,			/* partial_inplace */
   1483 	 0x0007ffff,		/* src_mask */
   1484 	 0x0007ffff,		/* dst_mask */
   1485 	 FALSE),		/* pcrel_offset */
   1486   HOWTO (R_NDS32_DWARF2_OP1_RELA,	/* type */
   1487 	 0,			/* rightshift */
   1488 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   1489 	 8,			/* bitsize */
   1490 	 FALSE,			/* pc_relative */
   1491 	 0,			/* bitpos */
   1492 	 complain_overflow_dont,/* complain_on_overflow */
   1493 	 nds32_elf_ignore_reloc,/* special_function */
   1494 	 "R_NDS32_DWARF2_OP1_RELA",	/* name */
   1495 	 FALSE,			/* partial_inplace */
   1496 	 0xff,			/* src_mask */
   1497 	 0xff,			/* dst_mask */
   1498 	 FALSE),		/* pcrel_offset */
   1499   HOWTO (R_NDS32_DWARF2_OP2_RELA,	/* type */
   1500 	 0,			/* rightshift */
   1501 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1502 	 16,			/* bitsize */
   1503 	 FALSE,			/* pc_relative */
   1504 	 0,			/* bitpos */
   1505 	 complain_overflow_dont,/* complain_on_overflow */
   1506 	 nds32_elf_ignore_reloc,/* special_function */
   1507 	 "R_NDS32_DWARF2_OP2_RELA",	/* name */
   1508 	 FALSE,			/* partial_inplace */
   1509 	 0xffff,		/* src_mask */
   1510 	 0xffff,		/* dst_mask */
   1511 	 FALSE),		/* pcrel_offset */
   1512   HOWTO (R_NDS32_DWARF2_LEB_RELA,	/* type */
   1513 	 0,			/* rightshift */
   1514 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1515 	 32,			/* bitsize */
   1516 	 FALSE,			/* pc_relative */
   1517 	 0,			/* bitpos */
   1518 	 complain_overflow_dont,/* complain_on_overflow */
   1519 	 nds32_elf_ignore_reloc,/* special_function */
   1520 	 "R_NDS32_DWARF2_LEB_RELA",	/* name */
   1521 	 FALSE,			/* partial_inplace */
   1522 	 0xffffffff,		/* src_mask */
   1523 	 0xffffffff,		/* dst_mask */
   1524 	 FALSE),		/* pcrel_offset */
   1525   HOWTO (R_NDS32_UPDATE_TA_RELA,/* type */
   1526 	 0,			/* rightshift */
   1527 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1528 	 16,			/* bitsize */
   1529 	 FALSE,			/* pc_relative */
   1530 	 0,			/* bitpos */
   1531 	 complain_overflow_dont,/* complain_on_overflow */
   1532 	 nds32_elf_ignore_reloc,/* special_function */
   1533 	 "R_NDS32_UPDATE_TA_RELA",	/* name */
   1534 	 FALSE,			/* partial_inplace */
   1535 	 0xffff,		/* src_mask */
   1536 	 0xffff,		/* dst_mask */
   1537 	 FALSE),		/* pcrel_offset */
   1538   /* Like R_NDS32_PCREL, but referring to the procedure linkage table
   1539      entry for the symbol.  */
   1540   HOWTO (R_NDS32_9_PLTREL,	/* type */
   1541 	 1,			/* rightshift */
   1542 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1543 	 8,			/* bitsize */
   1544 	 TRUE,			/* pc_relative */
   1545 	 0,			/* bitpos */
   1546 	 complain_overflow_signed,	/* complain_on_overflow */
   1547 	 bfd_elf_generic_reloc,	/* special_function */
   1548 	 "R_NDS32_9_PLTREL",	/* name */
   1549 	 FALSE,			/* partial_inplace */
   1550 	 0xff,			/* src_mask */
   1551 	 0xff,			/* dst_mask */
   1552 	 TRUE),			/* pcrel_offset */
   1553   /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
   1554   HOWTO (R_NDS32_PLT_GOTREL_LO20,	/* type */
   1555 	 0,			/* rightshift */
   1556 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1557 	 20,			/* bitsize */
   1558 	 FALSE,			/* pc_relative */
   1559 	 0,			/* bitpos */
   1560 	 complain_overflow_dont,/* complain_on_overflow */
   1561 	 bfd_elf_generic_reloc,	/* special_function */
   1562 	 "R_NDS32_PLT_GOTREL_LO20",	/* name */
   1563 	 FALSE,			/* partial_inplace */
   1564 	 0x000fffff,		/* src_mask */
   1565 	 0x000fffff,		/* dst_mask */
   1566 	 FALSE),		/* pcrel_offset */
   1567   /* low 15 bits of PLT symbol offset relative to GOT (GP) */
   1568   HOWTO (R_NDS32_PLT_GOTREL_LO15,	/* type */
   1569 	 0,			/* rightshift */
   1570 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1571 	 15,			/* bitsize */
   1572 	 FALSE,			/* pc_relative */
   1573 	 0,			/* bitpos */
   1574 	 complain_overflow_dont,/* complain_on_overflow */
   1575 	 bfd_elf_generic_reloc,	/* special_function */
   1576 	 "R_NDS32_PLT_GOTREL_LO15",	/* name */
   1577 	 FALSE,			/* partial_inplace */
   1578 	 0x00007fff,		/* src_mask */
   1579 	 0x00007fff,		/* dst_mask */
   1580 	 FALSE),		/* pcrel_offset */
   1581   /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
   1582   HOWTO (R_NDS32_PLT_GOTREL_LO19,	/* type */
   1583 	 0,			/* rightshift */
   1584 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1585 	 19,			/* bitsize */
   1586 	 FALSE,			/* pc_relative */
   1587 	 0,			/* bitpos */
   1588 	 complain_overflow_dont,/* complain_on_overflow */
   1589 	 bfd_elf_generic_reloc,	/* special_function */
   1590 	 "R_NDS32_PLT_GOTREL_LO19",	/* name */
   1591 	 FALSE,			/* partial_inplace */
   1592 	 0x0007ffff,		/* src_mask */
   1593 	 0x0007ffff,		/* dst_mask */
   1594 	 FALSE),		/* pcrel_offset */
   1595   HOWTO (R_NDS32_GOT_LO15,	/* type */
   1596 	 0,			/* rightshift */
   1597 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1598 	 15,			/* bitsize */
   1599 	 FALSE,			/* pc_relative */
   1600 	 0,			/* bitpos */
   1601 	 complain_overflow_dont,/* complain_on_overflow */
   1602 	 bfd_elf_generic_reloc,	/* special_function */
   1603 	 "R_NDS32_GOT_LO15",	/* name */
   1604 	 FALSE,			/* partial_inplace */
   1605 	 0x00007fff,		/* src_mask */
   1606 	 0x00007fff,		/* dst_mask */
   1607 	 FALSE),		/* pcrel_offset */
   1608   HOWTO (R_NDS32_GOT_LO19,	/* type */
   1609 	 0,			/* rightshift */
   1610 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1611 	 19,			/* bitsize */
   1612 	 FALSE,			/* pc_relative */
   1613 	 0,			/* bitpos */
   1614 	 complain_overflow_dont,/* complain_on_overflow */
   1615 	 bfd_elf_generic_reloc,	/* special_function */
   1616 	 "R_NDS32_GOT_LO19",	/* name */
   1617 	 FALSE,			/* partial_inplace */
   1618 	 0x0007ffff,		/* src_mask */
   1619 	 0x0007ffff,		/* dst_mask */
   1620 	 FALSE),		/* pcrel_offset */
   1621   HOWTO (R_NDS32_GOTOFF_LO15,	/* type */
   1622 	 0,			/* rightshift */
   1623 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1624 	 15,			/* bitsize */
   1625 	 FALSE,			/* pc_relative */
   1626 	 0,			/* bitpos */
   1627 	 complain_overflow_dont,/* complain_on_overflow */
   1628 	 bfd_elf_generic_reloc,	/* special_function */
   1629 	 "R_NDS32_GOTOFF_LO15",	/* name */
   1630 	 FALSE,			/* partial_inplace */
   1631 	 0x00007fff,		/* src_mask */
   1632 	 0x00007fff,		/* dst_mask */
   1633 	 FALSE),		/* pcrel_offset */
   1634   HOWTO (R_NDS32_GOTOFF_LO19,	/* type */
   1635 	 0,			/* rightshift */
   1636 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1637 	 19,			/* bitsize */
   1638 	 FALSE,			/* pc_relative */
   1639 	 0,			/* bitpos */
   1640 	 complain_overflow_dont,/* complain_on_overflow */
   1641 	 bfd_elf_generic_reloc,	/* special_function */
   1642 	 "R_NDS32_GOTOFF_LO19",	/* name */
   1643 	 FALSE,			/* partial_inplace */
   1644 	 0x0007ffff,		/* src_mask */
   1645 	 0x0007ffff,		/* dst_mask */
   1646 	 FALSE),		/* pcrel_offset */
   1647   /* GOT 15 bits offset.  */
   1648   HOWTO (R_NDS32_GOT15S2_RELA,	/* type */
   1649 	 2,			/* rightshift */
   1650 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1651 	 15,			/* bitsize */
   1652 	 FALSE,			/* pc_relative */
   1653 	 0,			/* bitpos */
   1654 	 complain_overflow_signed,	/* complain_on_overflow */
   1655 	 bfd_elf_generic_reloc,	/* special_function */
   1656 	 "R_NDS32_GOT15S2_RELA",/* name */
   1657 	 FALSE,			/* partial_inplace */
   1658 	 0x00007fff,		/* src_mask */
   1659 	 0x00007fff,		/* dst_mask */
   1660 	 FALSE),		/* pcrel_offset */
   1661   /* GOT 17 bits offset.  */
   1662   HOWTO (R_NDS32_GOT17S2_RELA,	/* type */
   1663 	 2,			/* rightshift */
   1664 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1665 	 17,			/* bitsize */
   1666 	 FALSE,			/* pc_relative */
   1667 	 0,			/* bitpos */
   1668 	 complain_overflow_signed,	/* complain_on_overflow */
   1669 	 bfd_elf_generic_reloc,	/* special_function */
   1670 	 "R_NDS32_GOT17S2_RELA",/* name */
   1671 	 FALSE,			/* partial_inplace */
   1672 	 0x0001ffff,		/* src_mask */
   1673 	 0x0001ffff,		/* dst_mask */
   1674 	 FALSE),		/* pcrel_offset */
   1675   /* A 5 bit address.  */
   1676   HOWTO (R_NDS32_5_RELA,	/* type */
   1677 	 0,			/* rightshift */
   1678 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1679 	 5,			/* bitsize */
   1680 	 FALSE,			/* pc_relative */
   1681 	 0,			/* bitpos */
   1682 	 complain_overflow_signed,	/* complain_on_overflow */
   1683 	 bfd_elf_generic_reloc,	/* special_function */
   1684 	 "R_NDS32_5_RELA",	/* name */
   1685 	 FALSE,			/* partial_inplace */
   1686 	 0x1f,			/* src_mask */
   1687 	 0x1f,			/* dst_mask */
   1688 	 FALSE),		/* pcrel_offset */
   1689   HOWTO (R_NDS32_10_UPCREL_RELA,/* type */
   1690 	 1,			/* rightshift */
   1691 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1692 	 9,			/* bitsize */
   1693 	 TRUE,			/* pc_relative */
   1694 	 0,			/* bitpos */
   1695 	 complain_overflow_unsigned,	/* complain_on_overflow */
   1696 	 bfd_elf_generic_reloc,	/* special_function */
   1697 	 "R_NDS32_10_UPCREL_RELA",	/* name */
   1698 	 FALSE,			/* partial_inplace */
   1699 	 0x1ff,			/* src_mask */
   1700 	 0x1ff,			/* dst_mask */
   1701 	 TRUE),			/* pcrel_offset */
   1702   HOWTO (R_NDS32_SDA_FP7U2_RELA,/* type */
   1703 	 2,			/* rightshift */
   1704 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1705 	 7,			/* bitsize */
   1706 	 FALSE,			/* pc_relative */
   1707 	 0,			/* bitpos */
   1708 	 complain_overflow_unsigned,	/* complain_on_overflow */
   1709 	 bfd_elf_generic_reloc,	/* special_function */
   1710 	 "R_NDS32_SDA_FP7U2_RELA",	/* name */
   1711 	 FALSE,			/* partial_inplace */
   1712 	 0x0000007f,		/* src_mask */
   1713 	 0x0000007f,		/* dst_mask */
   1714 	 FALSE),		/* pcrel_offset */
   1715   HOWTO (R_NDS32_WORD_9_PCREL_RELA,	/* type */
   1716 	 1,			/* rightshift */
   1717 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1718 	 8,			/* bitsize */
   1719 	 TRUE,			/* pc_relative */
   1720 	 0,			/* bitpos */
   1721 	 complain_overflow_signed,	/* complain_on_overflow */
   1722 	 bfd_elf_generic_reloc,	/* special_function */
   1723 	 "R_NDS32_WORD_9_PCREL_RELA",	/* name */
   1724 	 FALSE,			/* partial_inplace */
   1725 	 0xff,			/* src_mask */
   1726 	 0xff,			/* dst_mask */
   1727 	 TRUE),			/* pcrel_offset */
   1728   HOWTO (R_NDS32_25_ABS_RELA,	/* type */
   1729 	 1,			/* rightshift */
   1730 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1731 	 24,			/* bitsize */
   1732 	 FALSE,			/* pc_relative */
   1733 	 0,			/* bitpos */
   1734 	 complain_overflow_dont,/* complain_on_overflow */
   1735 	 bfd_elf_generic_reloc,	/* special_function */
   1736 	 "R_NDS32_25_ABS_RELA",	/* name */
   1737 	 FALSE,			/* partial_inplace */
   1738 	 0xffffff,		/* src_mask */
   1739 	 0xffffff,		/* dst_mask */
   1740 	 FALSE),		/* pcrel_offset */
   1741 
   1742   /* A relative 17 bit relocation for ifc, right shifted by 1.  */
   1743   HOWTO (R_NDS32_17IFC_PCREL_RELA,	/* type */
   1744 	 1,			/* rightshift */
   1745 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1746 	 16,			/* bitsize */
   1747 	 TRUE,			/* pc_relative */
   1748 	 0,			/* bitpos */
   1749 	 complain_overflow_signed,	/* complain_on_overflow */
   1750 	 bfd_elf_generic_reloc,	/* special_function */
   1751 	 "R_NDS32_17IFC_PCREL_RELA",	/* name */
   1752 	 FALSE,			/* partial_inplace */
   1753 	 0xffff,		/* src_mask */
   1754 	 0xffff,		/* dst_mask */
   1755 	 TRUE),			/* pcrel_offset */
   1756 
   1757   /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
   1758   HOWTO (R_NDS32_10IFCU_PCREL_RELA,	/* type */
   1759 	 1,			/* rightshift */
   1760 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   1761 	 9,			/* bitsize */
   1762 	 TRUE,			/* pc_relative */
   1763 	 0,			/* bitpos */
   1764 	 complain_overflow_unsigned,	/* complain_on_overflow */
   1765 	 bfd_elf_generic_reloc,	/* special_function */
   1766 	 "R_NDS32_10IFCU_PCREL_RELA",	/* name */
   1767 	 FALSE,			/* partial_inplace */
   1768 	 0x1ff,			/* src_mask */
   1769 	 0x1ff,			/* dst_mask */
   1770 	 TRUE),			/* pcrel_offset */
   1771 
   1772   /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol.  */
   1773   HOWTO (R_NDS32_TLS_LE_HI20,	/* type */
   1774 	 12,			/* rightshift */
   1775 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1776 	 20,			/* bitsize */
   1777 	 FALSE,			/* pc_relative */
   1778 	 0,			/* bitpos */
   1779 	 complain_overflow_dont,	/* complain_on_overflow */
   1780 	 bfd_elf_generic_reloc,	/* special_function */
   1781 	 "R_NDS32_TLS_LE_HI20",	/* name */
   1782 	 FALSE,			/* partial_inplace */
   1783 	 0x000fffff,		/* src_mask */
   1784 	 0x000fffff,		/* dst_mask */
   1785 	 FALSE),		/* pcrel_offset */
   1786   HOWTO (R_NDS32_TLS_LE_LO12,	/* type */
   1787 	 0,			/* rightshift */
   1788 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1789 	 12,			/* bitsize */
   1790 	 FALSE,			/* pc_relative */
   1791 	 0,			/* bitpos */
   1792 	 complain_overflow_dont,	/* complain_on_overflow */
   1793 	 bfd_elf_generic_reloc,	/* special_function */
   1794 	 "R_NDS32_TLS_LE_LO12",	/* name */
   1795 	 FALSE,			/* partial_inplace */
   1796 	 0x00000fff,		/* src_mask */
   1797 	 0x00000fff,		/* dst_mask */
   1798 	 FALSE),		/* pcrel_offset */
   1799 
   1800   /* Like R_NDS32_HI20, but referring to the TLS entry for the symbol.  */
   1801   HOWTO (R_NDS32_TLS_IE_HI20,	/* type */
   1802 	 12,			/* rightshift */
   1803 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1804 	 20,			/* bitsize */
   1805 	 FALSE,			/* pc_relative */
   1806 	 0,			/* bitpos */
   1807 	 complain_overflow_dont,	/* complain_on_overflow */
   1808 	 bfd_elf_generic_reloc,	/* special_function */
   1809 	 "R_NDS32_TLS_IE_HI20",	/* name */
   1810 	 FALSE,			/* partial_inplace */
   1811 	 0x000fffff,		/* src_mask */
   1812 	 0x000fffff,		/* dst_mask */
   1813 	 FALSE),		/* pcrel_offset */
   1814   HOWTO (R_NDS32_TLS_IE_LO12S2,	/* type */
   1815 	 2,			/* rightshift */
   1816 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1817 	 10,			/* bitsize */
   1818 	 FALSE,			/* pc_relative */
   1819 	 0,			/* bitpos */
   1820 	 complain_overflow_dont,	/* complain_on_overflow */
   1821 	 bfd_elf_generic_reloc,	/* special_function */
   1822 	 "R_NDS32_TLS_IE_LO12S2",	/* name */
   1823 	 FALSE,			/* partial_inplace */
   1824 	 0x000003ff,		/* src_mask */
   1825 	 0x000003ff,		/* dst_mask */
   1826 	 FALSE),		/* pcrel_offset */
   1827   /* Mark a TLS IE entry in GOT.  */
   1828   HOWTO (R_NDS32_TLS_TPOFF,	/* type */
   1829 	 0,			/* rightshift */
   1830 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1831 	 32,			/* bitsize */
   1832 	 FALSE,			/* pc_relative */
   1833 	 0,			/* bitpos */
   1834 	 complain_overflow_bitfield,	/* complain_on_overflow */
   1835 	 bfd_elf_generic_reloc,	/* special_function */
   1836 	 "R_NDS32_TLS_TPOFF",	/* name */
   1837 	 FALSE,			/* partial_inplace */
   1838 	 0xffffffff,		/* src_mask */
   1839 	 0xffffffff,		/* dst_mask */
   1840 	 FALSE),		/* pcrel_offset */
   1841   /* A 20 bit address.  */
   1842   HOWTO (R_NDS32_TLS_LE_20,	/* type */
   1843 	 0,			/* rightshift */
   1844 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1845 	 20,			/* bitsize */
   1846 	 FALSE,		/* pc_relative */
   1847 	 0,			/* bitpos */
   1848 	 complain_overflow_signed,	/* complain_on_overflow */
   1849 	 bfd_elf_generic_reloc,	/* special_function */
   1850 	 "R_NDS32_TLS_LE_20",	/* name */
   1851 	 FALSE,		/* partial_inplace */
   1852 	 0xfffff,		/* src_mask */
   1853 	 0xfffff,		/* dst_mask */
   1854 	 FALSE),		/* pcrel_offset */
   1855   HOWTO (R_NDS32_TLS_LE_15S0,	/* type */
   1856 	 0,			/* rightshift */
   1857 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1858 	 15,			/* bitsize */
   1859 	 FALSE,		/* pc_relative */
   1860 	 0,			/* bitpos */
   1861 	 complain_overflow_signed,	/* complain_on_overflow */
   1862 	 bfd_elf_generic_reloc,	/* special_function */
   1863 	 "R_NDS32_TLS_LE_15S0",	/* name */
   1864 	 FALSE,		/* partial_inplace */
   1865 	 0x7fff,		/* src_mask */
   1866 	 0x7fff,		/* dst_mask */
   1867 	 FALSE),		/* pcrel_offset */
   1868   HOWTO (R_NDS32_TLS_LE_15S1,	/* type */
   1869 	 1,			/* rightshift */
   1870 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1871 	 15,			/* bitsize */
   1872 	 FALSE,		/* pc_relative */
   1873 	 0,			/* bitpos */
   1874 	 complain_overflow_signed,	/* complain_on_overflow */
   1875 	 bfd_elf_generic_reloc,	/* special_function */
   1876 	 "R_NDS32_TLS_LE_15S1",	/* name */
   1877 	 FALSE,		/* partial_inplace */
   1878 	 0x7fff,		/* src_mask */
   1879 	 0x7fff,		/* dst_mask */
   1880 	 FALSE),		/* pcrel_offset */
   1881   HOWTO (R_NDS32_TLS_LE_15S2,	/* type */
   1882 	 2,			/* rightshift */
   1883 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1884 	 15,			/* bitsize */
   1885 	 FALSE,		/* pc_relative */
   1886 	 0,			/* bitpos */
   1887 	 complain_overflow_signed,	/* complain_on_overflow */
   1888 	 bfd_elf_generic_reloc,	/* special_function */
   1889 	 "R_NDS32_TLS_LE_15S2",	/* name */
   1890 	 FALSE,		/* partial_inplace */
   1891 	 0x7fff,		/* src_mask */
   1892 	 0x7fff,		/* dst_mask */
   1893 	 FALSE),		/* pcrel_offset */
   1894 
   1895   /* Relax hint for unconditional call sequence  */
   1896   HOWTO (R_NDS32_LONGCALL4,	/* type */
   1897 	 0,			/* rightshift */
   1898 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1899 	 32,			/* bitsize */
   1900 	 FALSE,			/* pc_relative */
   1901 	 0,			/* bitpos */
   1902 	 complain_overflow_dont,	/* complain_on_overflow */
   1903 	 nds32_elf_ignore_reloc,	/* special_function */
   1904 	 "R_NDS32_LONGCALL4",	/* name */
   1905 	 FALSE,			/* partial_inplace */
   1906 	 0xffffffff,		/* src_mask */
   1907 	 0xffffffff,		/* dst_mask */
   1908 	 FALSE),		/* pcrel_offset */
   1909 
   1910   /* Relax hint for conditional call sequence.  */
   1911   HOWTO (R_NDS32_LONGCALL5,	/* type */
   1912 	 0,			/* rightshift */
   1913 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1914 	 32,			/* bitsize */
   1915 	 FALSE,			/* pc_relative */
   1916 	 0,			/* bitpos */
   1917 	 complain_overflow_dont,	/* complain_on_overflow */
   1918 	 nds32_elf_ignore_reloc,	/* special_function */
   1919 	 "R_NDS32_LONGCALL5",	/* name */
   1920 	 FALSE,			/* partial_inplace */
   1921 	 0xffffffff,		/* src_mask */
   1922 	 0xffffffff,		/* dst_mask */
   1923 	 FALSE),		/* pcrel_offset */
   1924 
   1925   /* Relax hint for conditional call sequence.  */
   1926   HOWTO (R_NDS32_LONGCALL6,	/* type */
   1927 	 0,			/* rightshift */
   1928 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1929 	 32,			/* bitsize */
   1930 	 FALSE,			/* pc_relative */
   1931 	 0,			/* bitpos */
   1932 	 complain_overflow_dont,	/* complain_on_overflow */
   1933 	 nds32_elf_ignore_reloc,	/* special_function */
   1934 	 "R_NDS32_LONGCALL6",	/* name */
   1935 	 FALSE,			/* partial_inplace */
   1936 	 0xffffffff,		/* src_mask */
   1937 	 0xffffffff,		/* dst_mask */
   1938 	 FALSE),		/* pcrel_offset */
   1939 
   1940   /* Relax hint for unconditional branch sequence.  */
   1941   HOWTO (R_NDS32_LONGJUMP4,	/* type */
   1942 	 0,			/* rightshift */
   1943 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1944 	 32,			/* bitsize */
   1945 	 FALSE,			/* pc_relative */
   1946 	 0,			/* bitpos */
   1947 	 complain_overflow_dont,	/* complain_on_overflow */
   1948 	 nds32_elf_ignore_reloc,	/* special_function */
   1949 	 "R_NDS32_LONGJUMP4",	/* name */
   1950 	 FALSE,			/* partial_inplace */
   1951 	 0xffffffff,		/* src_mask */
   1952 	 0xffffffff,		/* dst_mask */
   1953 	 FALSE),		/* pcrel_offset */
   1954 
   1955   /* Relax hint for conditional branch sequence.  */
   1956   HOWTO (R_NDS32_LONGJUMP5,	/* type */
   1957 	 0,			/* rightshift */
   1958 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1959 	 32,			/* bitsize */
   1960 	 FALSE,			/* pc_relative */
   1961 	 0,			/* bitpos */
   1962 	 complain_overflow_dont,	/* complain_on_overflow */
   1963 	 nds32_elf_ignore_reloc,	/* special_function */
   1964 	 "R_NDS32_LONGJUMP5",	/* name */
   1965 	 FALSE,			/* partial_inplace */
   1966 	 0xffffffff,		/* src_mask */
   1967 	 0xffffffff,		/* dst_mask */
   1968 	 FALSE),		/* pcrel_offset */
   1969 
   1970   /* Relax hint for conditional branch sequence.  */
   1971   HOWTO (R_NDS32_LONGJUMP6,	/* type */
   1972 	 0,			/* rightshift */
   1973 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1974 	 32,			/* bitsize */
   1975 	 FALSE,			/* pc_relative */
   1976 	 0,			/* bitpos */
   1977 	 complain_overflow_dont,	/* complain_on_overflow */
   1978 	 nds32_elf_ignore_reloc,	/* special_function */
   1979 	 "R_NDS32_LONGJUMP6",	/* name */
   1980 	 FALSE,			/* partial_inplace */
   1981 	 0xffffffff,		/* src_mask */
   1982 	 0xffffffff,		/* dst_mask */
   1983 	 FALSE),		/* pcrel_offset */
   1984 
   1985   /* Relax hint for conditional branch sequence.  */
   1986   HOWTO (R_NDS32_LONGJUMP7,	/* type */
   1987 	 0,			/* rightshift */
   1988 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   1989 	 32,			/* bitsize */
   1990 	 FALSE,			/* pc_relative */
   1991 	 0,			/* bitpos */
   1992 	 complain_overflow_dont,	/* complain_on_overflow */
   1993 	 nds32_elf_ignore_reloc,	/* special_function */
   1994 	 "R_NDS32_LONGJUMP7",	/* name */
   1995 	 FALSE,			/* partial_inplace */
   1996 	 0xffffffff,		/* src_mask */
   1997 	 0xffffffff,		/* dst_mask */
   1998 	 FALSE),		/* pcrel_offset */
   1999 };
   2000 
   2001 /* Relocations used for relaxation.  */
   2002 static reloc_howto_type nds32_elf_relax_howto_table[] =
   2003 {
   2004   HOWTO (R_NDS32_RELAX_ENTRY,	/* type */
   2005 	 0,			/* rightshift */
   2006 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2007 	 32,			/* bitsize */
   2008 	 FALSE,			/* pc_relative */
   2009 	 0,			/* bitpos */
   2010 	 complain_overflow_dont,/* complain_on_overflow */
   2011 	 nds32_elf_ignore_reloc,/* special_function */
   2012 	 "R_NDS32_RELAX_ENTRY",	/* name */
   2013 	 FALSE,			/* partial_inplace */
   2014 	 0xffffffff,		/* src_mask */
   2015 	 0xffffffff,		/* dst_mask */
   2016 	 FALSE),		/* pcrel_offset */
   2017   HOWTO (R_NDS32_GOT_SUFF,	/* type */
   2018 	 0,			/* rightshift */
   2019 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2020 	 32,			/* bitsize */
   2021 	 FALSE,			/* pc_relative */
   2022 	 0,			/* bitpos */
   2023 	 complain_overflow_dont,/* complain_on_overflow */
   2024 	 nds32_elf_ignore_reloc,/* special_function */
   2025 	 "R_NDS32_GOT_SUFF",	/* name */
   2026 	 FALSE,			/* partial_inplace */
   2027 	 0xffffffff,		/* src_mask */
   2028 	 0xffffffff,		/* dst_mask */
   2029 	 FALSE),		/* pcrel_offset */
   2030   HOWTO (R_NDS32_GOTOFF_SUFF,	/* type */
   2031 	 0,			/* rightshift */
   2032 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2033 	 32,			/* bitsize */
   2034 	 FALSE,			/* pc_relative */
   2035 	 0,			/* bitpos */
   2036 	 complain_overflow_bitfield,	/* complain_on_overflow */
   2037 	 nds32_elf_ignore_reloc,/* special_function */
   2038 	 "R_NDS32_GOTOFF_SUFF",	/* name */
   2039 	 FALSE,			/* partial_inplace */
   2040 	 0xffffffff,		/* src_mask */
   2041 	 0xffffffff,		/* dst_mask */
   2042 	 FALSE),		/* pcrel_offset */
   2043   HOWTO (R_NDS32_PLT_GOT_SUFF,	/* type */
   2044 	 0,			/* rightshift */
   2045 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2046 	 32,			/* bitsize */
   2047 	 FALSE,			/* pc_relative */
   2048 	 0,			/* bitpos */
   2049 	 complain_overflow_dont,/* complain_on_overflow */
   2050 	 nds32_elf_ignore_reloc,/* special_function */
   2051 	 "R_NDS32_PLT_GOT_SUFF",/* name */
   2052 	 FALSE,			/* partial_inplace */
   2053 	 0xffffffff,		/* src_mask */
   2054 	 0xffffffff,		/* dst_mask */
   2055 	 FALSE),		/* pcrel_offset */
   2056   HOWTO (R_NDS32_MULCALL_SUFF,	/* type */
   2057 	 0,			/* rightshift */
   2058 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2059 	 32,			/* bitsize */
   2060 	 FALSE,			/* pc_relative */
   2061 	 0,			/* bitpos */
   2062 	 complain_overflow_dont,/* complain_on_overflow */
   2063 	 nds32_elf_ignore_reloc,/* special_function */
   2064 	 "R_NDS32_MULCALL_SUFF",/* name */
   2065 	 FALSE,			/* partial_inplace */
   2066 	 0xffffffff,		/* src_mask */
   2067 	 0xffffffff,		/* dst_mask */
   2068 	 FALSE),		/* pcrel_offset */
   2069   HOWTO (R_NDS32_PTR,		/* type */
   2070 	 0,			/* rightshift */
   2071 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2072 	 32,			/* bitsize */
   2073 	 FALSE,			/* pc_relative */
   2074 	 0,			/* bitpos */
   2075 	 complain_overflow_dont,/* complain_on_overflow */
   2076 	 nds32_elf_ignore_reloc,/* special_function */
   2077 	 "R_NDS32_PTR",		/* name */
   2078 	 FALSE,			/* partial_inplace */
   2079 	 0xffffffff,		/* src_mask */
   2080 	 0xffffffff,		/* dst_mask */
   2081 	 FALSE),		/* pcrel_offset */
   2082   HOWTO (R_NDS32_PTR_COUNT,	/* type */
   2083 	 0,			/* rightshift */
   2084 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2085 	 32,			/* bitsize */
   2086 	 FALSE,			/* pc_relative */
   2087 	 0,			/* bitpos */
   2088 	 complain_overflow_dont,/* complain_on_overflow */
   2089 	 nds32_elf_ignore_reloc,/* special_function */
   2090 	 "R_NDS32_PTR_COUNT",	/* name */
   2091 	 FALSE,			/* partial_inplace */
   2092 	 0xffffffff,		/* src_mask */
   2093 	 0xffffffff,		/* dst_mask */
   2094 	 FALSE),		/* pcrel_offset */
   2095   HOWTO (R_NDS32_PTR_RESOLVED,	/* type */
   2096 	 0,			/* rightshift */
   2097 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2098 	 32,			/* bitsize */
   2099 	 FALSE,			/* pc_relative */
   2100 	 0,			/* bitpos */
   2101 	 complain_overflow_dont,/* complain_on_overflow */
   2102 	 nds32_elf_ignore_reloc,/* special_function */
   2103 	 "R_NDS32_PTR_RESOLVED",/* name */
   2104 	 FALSE,			/* partial_inplace */
   2105 	 0xffffffff,		/* src_mask */
   2106 	 0xffffffff,		/* dst_mask */
   2107 	 FALSE),		/* pcrel_offset */
   2108   HOWTO (R_NDS32_PLTBLOCK,	/* type */
   2109 	 0,			/* rightshift */
   2110 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2111 	 32,			/* bitsize */
   2112 	 FALSE,			/* pc_relative */
   2113 	 0,			/* bitpos */
   2114 	 complain_overflow_dont,/* complain_on_overflow */
   2115 	 nds32_elf_ignore_reloc,/* special_function */
   2116 	 "R_NDS32_PLTBLOCK",	/* name */
   2117 	 FALSE,			/* partial_inplace */
   2118 	 0xffffffff,		/* src_mask */
   2119 	 0xffffffff,		/* dst_mask */
   2120 	 FALSE),		/* pcrel_offset */
   2121   HOWTO (R_NDS32_RELAX_REGION_BEGIN,	/* type */
   2122 	 0,			/* rightshift */
   2123 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2124 	 32,			/* bitsize */
   2125 	 FALSE,			/* pc_relative */
   2126 	 0,			/* bitpos */
   2127 	 complain_overflow_dont,/* complain_on_overflow */
   2128 	 nds32_elf_ignore_reloc,/* special_function */
   2129 	 "R_NDS32_RELAX_REGION_BEGIN",	/* name */
   2130 	 FALSE,			/* partial_inplace */
   2131 	 0xffffffff,		/* src_mask */
   2132 	 0xffffffff,		/* dst_mask */
   2133 	 FALSE),		/* pcrel_offset */
   2134   HOWTO (R_NDS32_RELAX_REGION_END,	/* type */
   2135 	 0,			/* rightshift */
   2136 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2137 	 32,			/* bitsize */
   2138 	 FALSE,			/* pc_relative */
   2139 	 0,			/* bitpos */
   2140 	 complain_overflow_dont,/* complain_on_overflow */
   2141 	 nds32_elf_ignore_reloc,/* special_function */
   2142 	 "R_NDS32_RELAX_REGION_END",	/* name */
   2143 	 FALSE,			/* partial_inplace */
   2144 	 0xffffffff,		/* src_mask */
   2145 	 0xffffffff,		/* dst_mask */
   2146 	 FALSE),		/* pcrel_offset */
   2147   HOWTO (R_NDS32_MINUEND,	/* type */
   2148 	 0,			/* rightshift */
   2149 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2150 	 32,			/* bitsize */
   2151 	 FALSE,			/* pc_relative */
   2152 	 0,			/* bitpos */
   2153 	 complain_overflow_dont,/* complain_on_overflow */
   2154 	 nds32_elf_ignore_reloc,/* special_function */
   2155 	 "R_NDS32_MINUEND",	/* name */
   2156 	 FALSE,			/* partial_inplace */
   2157 	 0xffffffff,		/* src_mask */
   2158 	 0xffffffff,		/* dst_mask */
   2159 	 FALSE),		/* pcrel_offset */
   2160   HOWTO (R_NDS32_SUBTRAHEND,	/* type */
   2161 	 0,			/* rightshift */
   2162 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2163 	 32,			/* bitsize */
   2164 	 FALSE,			/* pc_relative */
   2165 	 0,			/* bitpos */
   2166 	 complain_overflow_dont,/* complain_on_overflow */
   2167 	 nds32_elf_ignore_reloc,/* special_function */
   2168 	 "R_NDS32_SUBTRAHEND",	/* name */
   2169 	 FALSE,			/* partial_inplace */
   2170 	 0xffffffff,		/* src_mask */
   2171 	 0xffffffff,		/* dst_mask */
   2172 	 FALSE),		/* pcrel_offset */
   2173   HOWTO (R_NDS32_DIFF8,		/* type */
   2174 	 0,			/* rightshift */
   2175 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2176 	 8,			/* bitsize */
   2177 	 FALSE,			/* pc_relative */
   2178 	 0,			/* bitpos */
   2179 	 complain_overflow_dont,/* complain_on_overflow */
   2180 	 nds32_elf_ignore_reloc,/* special_function */
   2181 	 "R_NDS32_DIFF8",	/* name */
   2182 	 FALSE,			/* partial_inplace */
   2183 	 0x000000ff,		/* src_mask */
   2184 	 0x000000ff,		/* dst_mask */
   2185 	 FALSE),		/* pcrel_offset */
   2186   HOWTO (R_NDS32_DIFF16,	/* type */
   2187 	 0,			/* rightshift */
   2188 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
   2189 	 16,			/* bitsize */
   2190 	 FALSE,			/* pc_relative */
   2191 	 0,			/* bitpos */
   2192 	 complain_overflow_dont,/* complain_on_overflow */
   2193 	 nds32_elf_ignore_reloc,/* special_function */
   2194 	 "R_NDS32_DIFF16",	/* name */
   2195 	 FALSE,			/* partial_inplace */
   2196 	 0x0000ffff,		/* src_mask */
   2197 	 0x0000ffff,		/* dst_mask */
   2198 	 FALSE),		/* pcrel_offset */
   2199   HOWTO (R_NDS32_DIFF32,	/* type */
   2200 	 0,			/* rightshift */
   2201 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2202 	 32,			/* bitsize */
   2203 	 FALSE,			/* pc_relative */
   2204 	 0,			/* bitpos */
   2205 	 complain_overflow_dont,/* complain_on_overflow */
   2206 	 nds32_elf_ignore_reloc,/* special_function */
   2207 	 "R_NDS32_DIFF32",	/* name */
   2208 	 FALSE,			/* partial_inplace */
   2209 	 0xffffffff,		/* src_mask */
   2210 	 0xffffffff,		/* dst_mask */
   2211 	 FALSE),		/* pcrel_offset */
   2212   HOWTO (R_NDS32_DIFF_ULEB128,	/* type */
   2213 	 0,			/* rightshift */
   2214 	 0,			/* size (0 = byte, 1 = short, 2 = long) */
   2215 	 0,			/* bitsize */
   2216 	 FALSE,			/* pc_relative */
   2217 	 0,			/* bitpos */
   2218 	 complain_overflow_dont,/* complain_on_overflow */
   2219 	 nds32_elf_ignore_reloc,/* special_function */
   2220 	 "R_NDS32_DIFF_ULEB128",/* name */
   2221 	 FALSE,			/* partial_inplace */
   2222 	 0xffffffff,		/* src_mask */
   2223 	 0xffffffff,		/* dst_mask */
   2224 	 FALSE),		/* pcrel_offset */
   2225   HOWTO (R_NDS32_DATA,		/* type */
   2226 	 0,			/* rightshift */
   2227 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2228 	 32,			/* bitsize */
   2229 	 FALSE,			/* pc_relative */
   2230 	 0,			/* bitpos */
   2231 	 complain_overflow_dont,/* complain_on_overflow */
   2232 	 nds32_elf_ignore_reloc,/* special_function */
   2233 	 "R_NDS32_DATA",	/* name */
   2234 	 FALSE,			/* partial_inplace */
   2235 	 0xffffffff,		/* src_mask */
   2236 	 0xffffffff,		/* dst_mask */
   2237 	 FALSE),		/* pcrel_offset */
   2238   HOWTO (R_NDS32_TRAN,		/* type */
   2239 	 0,			/* rightshift */
   2240 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2241 	 32,			/* bitsize */
   2242 	 FALSE,			/* pc_relative */
   2243 	 0,			/* bitpos */
   2244 	 complain_overflow_dont,/* complain_on_overflow */
   2245 	 nds32_elf_ignore_reloc,/* special_function */
   2246 	 "R_NDS32_TRAN",	/* name */
   2247 	 FALSE,			/* partial_inplace */
   2248 	 0xffffffff,		/* src_mask */
   2249 	 0xffffffff,		/* dst_mask */
   2250 	 FALSE),		/* pcrel_offset */
   2251   HOWTO (R_NDS32_TLS_LE_ADD,	/* type */
   2252 	 0,			/* rightshift */
   2253 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2254 	 32,			/* bitsize */
   2255 	 FALSE,			/* pc_relative */
   2256 	 0,			/* bitpos */
   2257 	 complain_overflow_dont,	/* complain_on_overflow */
   2258 	 nds32_elf_ignore_reloc,	/* special_function */
   2259 	 "R_NDS32_TLS_LE_ADD",	/* name */
   2260 	 FALSE,			/* partial_inplace */
   2261 	 0xffffffff,		/* src_mask */
   2262 	 0xffffffff,		/* dst_mask */
   2263 	 FALSE),		/* pcrel_offset */
   2264   HOWTO (R_NDS32_TLS_LE_LS,	/* type */
   2265 	 0,			/* rightshift */
   2266 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2267 	 32,			/* bitsize */
   2268 	 FALSE,			/* pc_relative */
   2269 	 0,			/* bitpos */
   2270 	 complain_overflow_dont,	/* complain_on_overflow */
   2271 	 nds32_elf_ignore_reloc,	/* special_function */
   2272 	 "R_NDS32_TLS_LE_LS",	/* name */
   2273 	 FALSE,			/* partial_inplace */
   2274 	 0xffffffff,		/* src_mask */
   2275 	 0xffffffff,		/* dst_mask */
   2276 	 FALSE),		/* pcrel_offset */
   2277   HOWTO (R_NDS32_EMPTY,		/* type */
   2278 	 0,			/* rightshift */
   2279 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
   2280 	 32,			/* bitsize */
   2281 	 FALSE,			/* pc_relative */
   2282 	 0,			/* bitpos */
   2283 	 complain_overflow_dont,	/* complain_on_overflow */
   2284 	 nds32_elf_ignore_reloc,	/* special_function */
   2285 	 "R_NDS32_EMPTY",		/* name */
   2286 	 FALSE,			/* partial_inplace */
   2287 	 0xffffffff,		/* src_mask */
   2288 	 0xffffffff,		/* dst_mask */
   2289 	 FALSE),		/* pcrel_offset */
   2290 };
   2291 
   2292 
   2293 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
   2295    This prototype is the same as qsort ().  */
   2296 
   2297 void
   2298 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
   2299 		      int (*compar) (const void *lhs, const void *rhs))
   2300 {
   2301   char *ptr = (char *) base;
   2302   int i, j;
   2303   char *tmp = alloca (size);
   2304 
   2305   /* If i is less than j, i is inserted before j.
   2306 
   2307      |---- j ----- i --------------|
   2308       \		 / \		  /
   2309 	 sorted		unsorted
   2310    */
   2311 
   2312   for (i = 1; i < (int) nmemb; i++)
   2313     {
   2314       for (j = (i - 1); j >= 0; j--)
   2315 	if (compar (ptr + i * size, ptr + j * size) >= 0)
   2316 	  break;
   2317 
   2318       j++;
   2319 
   2320       if (i == j)
   2321 	continue; /* i is in order.  */
   2322 
   2323       memcpy (tmp, ptr + i * size, size);
   2324       memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
   2325       memcpy (ptr + j * size, tmp, size);
   2326     }
   2327 }
   2328 
   2329 /* Sort relocation by r_offset.
   2330 
   2331    We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
   2332    algorithm.  Relocations at the same r_offset must keep their order.
   2333    For example, RELAX_ENTRY must be the very first relocation entry.
   2334 
   2335    Currently, this function implements insertion-sort.
   2336 
   2337    FIXME: If we already sort them in assembler, why bother sort them
   2338 	  here again?  */
   2339 
   2340 static int
   2341 compar_reloc (const void *lhs, const void *rhs)
   2342 {
   2343   const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
   2344   const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
   2345 
   2346   if (l->r_offset > r->r_offset)
   2347     return 1;
   2348   else if (l->r_offset == r->r_offset)
   2349     return 0;
   2350   else
   2351     return -1;
   2352 }
   2353 
   2354 /* Functions listed below are only used for old relocs.
   2355    * nds32_elf_9_pcrel_reloc
   2356    * nds32_elf_do_9_pcrel_reloc
   2357    * nds32_elf_hi20_reloc
   2358    * nds32_elf_relocate_hi20
   2359    * nds32_elf_lo12_reloc
   2360    * nds32_elf_sda15_reloc
   2361    * nds32_elf_generic_reloc
   2362    */
   2363 
   2364 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
   2365 
   2366 static bfd_reloc_status_type
   2367 nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   2368 			 void *data, asection *input_section, bfd *output_bfd,
   2369 			 char **error_message ATTRIBUTE_UNUSED)
   2370 {
   2371   /* This part is from bfd_elf_generic_reloc.  */
   2372   if (output_bfd != (bfd *) NULL
   2373       && (symbol->flags & BSF_SECTION_SYM) == 0
   2374       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
   2375     {
   2376       reloc_entry->address += input_section->output_offset;
   2377       return bfd_reloc_ok;
   2378     }
   2379 
   2380   if (output_bfd != NULL)
   2381     {
   2382       /* FIXME: See bfd_perform_relocation.  Is this right?  */
   2383       return bfd_reloc_continue;
   2384     }
   2385 
   2386   return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
   2387 				     input_section,
   2388 				     data, reloc_entry->address,
   2389 				     symbol->section,
   2390 				     (symbol->value
   2391 				      + symbol->section->output_section->vma
   2392 				      + symbol->section->output_offset),
   2393 				     reloc_entry->addend);
   2394 }
   2395 
   2396 /* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
   2397 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
   2398 
   2399 static bfd_reloc_status_type
   2400 nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
   2401 			    asection *input_section, bfd_byte *data,
   2402 			    bfd_vma offset,
   2403 			    asection *symbol_section ATTRIBUTE_UNUSED,
   2404 			    bfd_vma symbol_value, bfd_vma addend)
   2405 {
   2406   bfd_signed_vma relocation;
   2407   unsigned short x;
   2408   bfd_reloc_status_type status;
   2409 
   2410   /* Sanity check the address (offset in section).  */
   2411   if (offset > bfd_get_section_limit (abfd, input_section))
   2412     return bfd_reloc_outofrange;
   2413 
   2414   relocation = symbol_value + addend;
   2415   /* Make it pc relative.  */
   2416   relocation -= (input_section->output_section->vma
   2417 		 + input_section->output_offset);
   2418   /* These jumps mask off the lower two bits of the current address
   2419      before doing pcrel calculations.  */
   2420   relocation -= (offset & -(bfd_vma) 2);
   2421 
   2422   if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
   2423     status = bfd_reloc_overflow;
   2424   else
   2425     status = bfd_reloc_ok;
   2426 
   2427   x = bfd_getb16 (data + offset);
   2428 
   2429   relocation >>= howto->rightshift;
   2430   relocation <<= howto->bitpos;
   2431   x = (x & ~howto->dst_mask)
   2432       | (((x & howto->src_mask) + relocation) & howto->dst_mask);
   2433 
   2434   bfd_putb16 ((bfd_vma) x, data + offset);
   2435 
   2436   return status;
   2437 }
   2438 
   2439 /* Handle the R_NDS32_HI20_[SU]LO relocs.
   2440    HI20_SLO is for the add3 and load/store with displacement instructions.
   2441    HI20 is for the or3 instruction.
   2442    For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
   2443    the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
   2444    we must add one to the high 16 bytes (which will get subtracted off when
   2445    the low 16 bits are added).
   2446    These relocs have to be done in combination with an R_NDS32_LO12 reloc
   2447    because there is a carry from the LO12 to the HI20.  Here we just save
   2448    the information we need; we do the actual relocation when we see the LO12.
   2449    This code is copied from the elf32-mips.c.  We also support an arbitrary
   2450    number of HI20 relocs to be associated with a single LO12 reloc.  The
   2451    assembler sorts the relocs to ensure each HI20 immediately precedes its
   2452    LO12.  However if there are multiple copies, the assembler may not find
   2453    the real LO12 so it picks the first one it finds.  */
   2454 
   2455 struct nds32_hi20
   2456 {
   2457   struct nds32_hi20 *next;
   2458   bfd_byte *addr;
   2459   bfd_vma addend;
   2460 };
   2461 
   2462 static struct nds32_hi20 *nds32_hi20_list;
   2463 
   2464 static bfd_reloc_status_type
   2465 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2466 		      asymbol *symbol, void *data, asection *input_section,
   2467 		      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
   2468 {
   2469   bfd_reloc_status_type ret;
   2470   bfd_vma relocation;
   2471   struct nds32_hi20 *n;
   2472 
   2473   /* This part is from bfd_elf_generic_reloc.
   2474      If we're relocating, and this an external symbol, we don't want
   2475      to change anything.  */
   2476   if (output_bfd != (bfd *) NULL
   2477       && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
   2478     {
   2479       reloc_entry->address += input_section->output_offset;
   2480       return bfd_reloc_ok;
   2481     }
   2482 
   2483   /* Sanity check the address (offset in section).  */
   2484   if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
   2485     return bfd_reloc_outofrange;
   2486 
   2487   ret = bfd_reloc_ok;
   2488   if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
   2489     ret = bfd_reloc_undefined;
   2490 
   2491   if (bfd_is_com_section (symbol->section))
   2492     relocation = 0;
   2493   else
   2494     relocation = symbol->value;
   2495 
   2496   relocation += symbol->section->output_section->vma;
   2497   relocation += symbol->section->output_offset;
   2498   relocation += reloc_entry->addend;
   2499 
   2500   /* Save the information, and let LO12 do the actual relocation.  */
   2501   n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
   2502   if (n == NULL)
   2503     return bfd_reloc_outofrange;
   2504 
   2505   n->addr = (bfd_byte *) data + reloc_entry->address;
   2506   n->addend = relocation;
   2507   n->next = nds32_hi20_list;
   2508   nds32_hi20_list = n;
   2509 
   2510   if (output_bfd != (bfd *) NULL)
   2511     reloc_entry->address += input_section->output_offset;
   2512 
   2513   return ret;
   2514 }
   2515 
   2516 /* Handle an NDS32 ELF HI20 reloc.  */
   2517 
   2518 static void
   2519 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
   2520 			 int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
   2521 			 Elf_Internal_Rela *rello, bfd_byte *contents,
   2522 			 bfd_vma addend)
   2523 {
   2524   unsigned long insn;
   2525   bfd_vma addlo;
   2526 
   2527   insn = bfd_getb32 (contents + relhi->r_offset);
   2528 
   2529   addlo = bfd_getb32 (contents + rello->r_offset);
   2530   addlo &= 0xfff;
   2531 
   2532   addend += ((insn & 0xfffff) << 20) + addlo;
   2533 
   2534   insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
   2535   bfd_putb32 (insn, contents + relhi->r_offset);
   2536 }
   2537 
   2538 /* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
   2539    inplace relocation; this function exists in order to do the
   2540    R_NDS32_HI20_[SU]LO relocation described above.  */
   2541 
   2542 static bfd_reloc_status_type
   2543 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
   2544 		      void *data, asection *input_section, bfd *output_bfd,
   2545 		      char **error_message)
   2546 {
   2547   /* This part is from bfd_elf_generic_reloc.
   2548      If we're relocating, and this an external symbol, we don't want
   2549      to change anything.  */
   2550   if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
   2551       && reloc_entry->addend == 0)
   2552     {
   2553       reloc_entry->address += input_section->output_offset;
   2554       return bfd_reloc_ok;
   2555     }
   2556 
   2557   if (nds32_hi20_list != NULL)
   2558     {
   2559       struct nds32_hi20 *l;
   2560 
   2561       l = nds32_hi20_list;
   2562       while (l != NULL)
   2563 	{
   2564 	  unsigned long insn;
   2565 	  unsigned long val;
   2566 	  unsigned long vallo;
   2567 	  struct nds32_hi20 *next;
   2568 
   2569 	  /* Do the HI20 relocation.  Note that we actually don't need
   2570 	     to know anything about the LO12 itself, except where to
   2571 	     find the low 12 bits of the addend needed by the LO12.  */
   2572 	  insn = bfd_getb32 (l->addr);
   2573 	  vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
   2574 	  vallo &= 0xfff;
   2575 	  switch (reloc_entry->howto->type)
   2576 	    {
   2577 	    case R_NDS32_LO12S3:
   2578 	      vallo <<= 3;
   2579 	      break;
   2580 
   2581 	    case R_NDS32_LO12S2:
   2582 	      vallo <<= 2;
   2583 	      break;
   2584 
   2585 	    case R_NDS32_LO12S1:
   2586 	      vallo <<= 1;
   2587 	      break;
   2588 
   2589 	    case R_NDS32_LO12S0:
   2590 	      vallo <<= 0;
   2591 	      break;
   2592 	    }
   2593 
   2594 	  val = ((insn & 0xfffff) << 12) + vallo;
   2595 	  val += l->addend;
   2596 
   2597 	  insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
   2598 	  bfd_putb32 ((bfd_vma) insn, l->addr);
   2599 
   2600 	  next = l->next;
   2601 	  free (l);
   2602 	  l = next;
   2603 	}
   2604 
   2605       nds32_hi20_list = NULL;
   2606     }
   2607 
   2608   /* Now do the LO12 reloc in the usual way.
   2609      ??? It would be nice to call bfd_elf_generic_reloc here,
   2610      but we have partial_inplace set.  bfd_elf_generic_reloc will
   2611      pass the handling back to bfd_install_relocation which will install
   2612      a section relative addend which is wrong.  */
   2613   return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
   2614 				  input_section, output_bfd, error_message);
   2615 }
   2616 
   2617 /* Do generic partial_inplace relocation.
   2618    This is a local replacement for bfd_elf_generic_reloc.  */
   2619 
   2620 static bfd_reloc_status_type
   2621 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
   2622 			 asymbol *symbol, void *data, asection *input_section,
   2623 			 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
   2624 {
   2625   bfd_reloc_status_type ret;
   2626   bfd_vma relocation;
   2627   bfd_byte *inplace_address;
   2628 
   2629   /* This part is from bfd_elf_generic_reloc.
   2630      If we're relocating, and this an external symbol, we don't want
   2631      to change anything.  */
   2632   if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
   2633       && reloc_entry->addend == 0)
   2634     {
   2635       reloc_entry->address += input_section->output_offset;
   2636       return bfd_reloc_ok;
   2637     }
   2638 
   2639   /* Now do the reloc in the usual way.
   2640      ??? It would be nice to call bfd_elf_generic_reloc here,
   2641      but we have partial_inplace set.  bfd_elf_generic_reloc will
   2642      pass the handling back to bfd_install_relocation which will install
   2643      a section relative addend which is wrong.  */
   2644 
   2645   /* Sanity check the address (offset in section).  */
   2646   if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
   2647     return bfd_reloc_outofrange;
   2648 
   2649   ret = bfd_reloc_ok;
   2650   if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
   2651     ret = bfd_reloc_undefined;
   2652 
   2653   if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
   2654     relocation = 0;
   2655   else
   2656     relocation = symbol->value;
   2657 
   2658   /* Only do this for a final link.  */
   2659   if (output_bfd == (bfd *) NULL)
   2660     {
   2661       relocation += symbol->section->output_section->vma;
   2662       relocation += symbol->section->output_offset;
   2663     }
   2664 
   2665   relocation += reloc_entry->addend;
   2666   switch (reloc_entry->howto->type)
   2667     {
   2668     case R_NDS32_LO12S3:
   2669       relocation >>= 3;
   2670       break;
   2671 
   2672     case R_NDS32_LO12S2:
   2673       relocation >>= 2;
   2674       break;
   2675 
   2676     case R_NDS32_LO12S1:
   2677       relocation >>= 1;
   2678       break;
   2679 
   2680     case R_NDS32_LO12S0:
   2681     default:
   2682       relocation >>= 0;
   2683       break;
   2684     }
   2685 
   2686   inplace_address = (bfd_byte *) data + reloc_entry->address;
   2687 
   2688 #define DOIT(x)						\
   2689   x = ((x & ~reloc_entry->howto->dst_mask) |		\
   2690   (((x & reloc_entry->howto->src_mask) +  relocation) &	\
   2691   reloc_entry->howto->dst_mask))
   2692 
   2693   switch (reloc_entry->howto->size)
   2694     {
   2695     case 1:
   2696       {
   2697 	short x = bfd_getb16 (inplace_address);
   2698 
   2699 	DOIT (x);
   2700 	bfd_putb16 ((bfd_vma) x, inplace_address);
   2701       }
   2702       break;
   2703     case 2:
   2704       {
   2705 	unsigned long x = bfd_getb32 (inplace_address);
   2706 
   2707 	DOIT (x);
   2708 	bfd_putb32 ((bfd_vma) x, inplace_address);
   2709       }
   2710       break;
   2711     default:
   2712       BFD_ASSERT (0);
   2713     }
   2714 
   2715   if (output_bfd != (bfd *) NULL)
   2716     reloc_entry->address += input_section->output_offset;
   2717 
   2718   return ret;
   2719 }
   2720 
   2721 /* Handle the R_NDS32_SDA15 reloc.
   2722    This reloc is used to compute the address of objects in the small data area
   2723    and to perform loads and stores from that area.
   2724    The lower 15 bits are sign extended and added to the register specified
   2725    in the instruction, which is assumed to point to _SDA_BASE_.
   2726 
   2727    Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
   2728    the access size, this must be taken care of.  */
   2729 
   2730 static bfd_reloc_status_type
   2731 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2732 		       asymbol *symbol, void *data ATTRIBUTE_UNUSED,
   2733 		       asection *input_section, bfd *output_bfd,
   2734 		       char **error_message ATTRIBUTE_UNUSED)
   2735 {
   2736   /* This part is from bfd_elf_generic_reloc.  */
   2737   if (output_bfd != (bfd *) NULL
   2738       && (symbol->flags & BSF_SECTION_SYM) == 0
   2739       && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
   2740     {
   2741       reloc_entry->address += input_section->output_offset;
   2742       return bfd_reloc_ok;
   2743     }
   2744 
   2745   if (output_bfd != NULL)
   2746     {
   2747       /* FIXME: See bfd_perform_relocation.  Is this right?  */
   2748       return bfd_reloc_continue;
   2749     }
   2750 
   2751   /* FIXME: not sure what to do here yet.  But then again, the linker
   2752      may never call us.  */
   2753   abort ();
   2754 }
   2755 
   2756 /* nds32_elf_ignore_reloc is the special function for
   2757    relocation types which don't need to be relocated
   2758    like relaxation relocation types.
   2759    This function simply return bfd_reloc_ok when it is
   2760    invoked.  */
   2761 
   2762 static bfd_reloc_status_type
   2763 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
   2764 			asymbol *symbol ATTRIBUTE_UNUSED,
   2765 			void *data ATTRIBUTE_UNUSED, asection *input_section,
   2766 			bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
   2767 {
   2768   if (output_bfd != NULL)
   2769     reloc_entry->address += input_section->output_offset;
   2770 
   2771   return bfd_reloc_ok;
   2772 }
   2773 
   2774 
   2776 /* Map BFD reloc types to NDS32 ELF reloc types.  */
   2777 
   2778 struct nds32_reloc_map_entry
   2779 {
   2780   bfd_reloc_code_real_type bfd_reloc_val;
   2781   unsigned char elf_reloc_val;
   2782 };
   2783 
   2784 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
   2785 {
   2786   {BFD_RELOC_NONE, R_NDS32_NONE},
   2787   {BFD_RELOC_16, R_NDS32_16_RELA},
   2788   {BFD_RELOC_32, R_NDS32_32_RELA},
   2789   {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
   2790   {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
   2791   {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
   2792   {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
   2793   {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
   2794   {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
   2795   {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
   2796   {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
   2797   {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
   2798   {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
   2799   {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
   2800   {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
   2801   {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
   2802   {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
   2803   {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
   2804   {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
   2805   {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
   2806   {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
   2807   {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
   2808   {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
   2809 
   2810   {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
   2811   {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
   2812   {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
   2813   {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
   2814   {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
   2815   {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
   2816   {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
   2817   {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
   2818   {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
   2819   {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
   2820   {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
   2821   {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
   2822   {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
   2823   {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
   2824   {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
   2825   {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
   2826   {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
   2827   {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
   2828   {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
   2829   {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
   2830   {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
   2831   {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
   2832   {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
   2833   {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
   2834   {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
   2835   {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
   2836   {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
   2837   {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
   2838   {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
   2839   {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
   2840   {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
   2841   {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
   2842   {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
   2843   {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
   2844   {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
   2845   {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
   2846   {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
   2847   {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
   2848   {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
   2849   {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
   2850   {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
   2851   {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
   2852   {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
   2853   {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
   2854   {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
   2855   {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
   2856   {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
   2857   {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
   2858   {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
   2859   {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
   2860   {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
   2861   {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
   2862   {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
   2863   {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
   2864   {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
   2865   {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
   2866   {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
   2867   {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
   2868   {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
   2869   {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
   2870   {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
   2871   {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
   2872   {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
   2873   {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
   2874   {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
   2875   {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
   2876   {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
   2877   {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
   2878   {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
   2879   {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
   2880   {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
   2881   {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
   2882   {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
   2883   {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
   2884   {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
   2885 
   2886   {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
   2887   {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
   2888   {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
   2889   {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
   2890   {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
   2891   {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
   2892   {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
   2893   {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
   2894   {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
   2895   {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
   2896   {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
   2897   {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
   2898   {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
   2899   {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
   2900   {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
   2901   {BFD_RELOC_NDS32_TLS_TPOFF, R_NDS32_TLS_TPOFF},
   2902   {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
   2903   {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
   2904   {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
   2905   {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
   2906 };
   2907 
   2908 /* Patch tag.  */
   2909 
   2910 static reloc_howto_type *
   2911 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2912 				 const char *r_name)
   2913 {
   2914   unsigned int i;
   2915 
   2916   for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
   2917     if (nds32_elf_howto_table[i].name != NULL
   2918 	&& strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
   2919       return &nds32_elf_howto_table[i];
   2920 
   2921   for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
   2922     if (nds32_elf_relax_howto_table[i].name != NULL
   2923 	&& strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
   2924       return &nds32_elf_relax_howto_table[i];
   2925 
   2926   return NULL;
   2927 }
   2928 
   2929 static reloc_howto_type *
   2930 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
   2931 {
   2932   if (code < R_NDS32_RELAX_ENTRY)
   2933     {
   2934       BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
   2935       return &nds32_elf_howto_table[code];
   2936     }
   2937   else
   2938     {
   2939       BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
   2940 		  < ARRAY_SIZE (nds32_elf_relax_howto_table));
   2941       return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
   2942     }
   2943 }
   2944 
   2945 static reloc_howto_type *
   2946 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
   2947 				 bfd_reloc_code_real_type code)
   2948 {
   2949   unsigned int i;
   2950 
   2951   for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
   2952     {
   2953       if (nds32_reloc_map[i].bfd_reloc_val == code)
   2954 	return bfd_elf32_bfd_reloc_type_table_lookup
   2955 		 (nds32_reloc_map[i].elf_reloc_val);
   2956     }
   2957 
   2958   return NULL;
   2959 }
   2960 
   2961 /* Set the howto pointer for an NDS32 ELF reloc.  */
   2962 
   2963 static void
   2964 nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2965 			 Elf_Internal_Rela *dst)
   2966 {
   2967   enum elf_nds32_reloc_type r_type;
   2968 
   2969   r_type = ELF32_R_TYPE (dst->r_info);
   2970   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
   2971   cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
   2972 }
   2973 
   2974 static void
   2975 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
   2976 		     Elf_Internal_Rela *dst)
   2977 {
   2978   BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
   2979 	      || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
   2980 		  && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
   2981   cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
   2982 }
   2983 
   2984 /* Support for core dump NOTE sections.
   2985    Reference to include/linux/elfcore.h in Linux.  */
   2986 
   2987 static bfd_boolean
   2988 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
   2989 {
   2990   int offset;
   2991   size_t size;
   2992 
   2993   switch (note->descsz)
   2994     {
   2995     case 0x114:
   2996       /* Linux/NDS32 32-bit, ABI1 */
   2997 
   2998       /* pr_cursig */
   2999       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   3000 
   3001       /* pr_pid */
   3002       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
   3003 
   3004       /* pr_reg */
   3005       offset = 72;
   3006       size = 200;
   3007       break;
   3008 
   3009     case 0xfc:
   3010       /* Linux/NDS32 32-bit */
   3011 
   3012       /* pr_cursig */
   3013       elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
   3014 
   3015       /* pr_pid */
   3016       elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
   3017 
   3018       /* pr_reg */
   3019       offset = 72;
   3020       size = 176;
   3021       break;
   3022 
   3023     default:
   3024       return FALSE;
   3025     }
   3026 
   3027   /* Make a ".reg" section.  */
   3028   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
   3029 					  size, note->descpos + offset);
   3030 }
   3031 
   3032 static bfd_boolean
   3033 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   3034 {
   3035   switch (note->descsz)
   3036     {
   3037     case 124:
   3038       /* Linux/NDS32 */
   3039 
   3040       /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
   3041       elf_tdata (abfd)->core->program =
   3042 	_bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
   3043       elf_tdata (abfd)->core->command =
   3044 	_bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
   3045 
   3046     default:
   3047       return FALSE;
   3048     }
   3049 
   3050   /* Note that for some reason, a spurious space is tacked
   3051      onto the end of the args in some (at least one anyway)
   3052      implementations, so strip it off if it exists.  */
   3053   {
   3054     char *command = elf_tdata (abfd)->core->command;
   3055     int n = strlen (command);
   3056 
   3057     if (0 < n && command[n - 1] == ' ')
   3058       command[n - 1] = '\0';
   3059   }
   3060 
   3061   return TRUE;
   3062 }
   3063 
   3064 /* Hook called by the linker routine which adds symbols from an object
   3065    file.  We must handle the special NDS32 section numbers here.
   3066    We also keep watching for whether we need to create the sdata special
   3067    linker sections.  */
   3068 
   3069 static bfd_boolean
   3070 nds32_elf_add_symbol_hook (bfd *abfd,
   3071 			   struct bfd_link_info *info ATTRIBUTE_UNUSED,
   3072 			   Elf_Internal_Sym *sym,
   3073 			   const char **namep ATTRIBUTE_UNUSED,
   3074 			   flagword *flagsp ATTRIBUTE_UNUSED,
   3075 			   asection **secp, bfd_vma *valp)
   3076 {
   3077   switch (sym->st_shndx)
   3078     {
   3079     case SHN_COMMON:
   3080       /* Common symbols less than the GP size are automatically
   3081 	 treated as SHN_MIPS_SCOMMON symbols.  */
   3082       if (sym->st_size > elf_gp_size (abfd)
   3083 	  || ELF_ST_TYPE (sym->st_info) == STT_TLS)
   3084 	break;
   3085 
   3086       /* st_value is the alignemnt constraint.
   3087 	 That might be its actual size if it is an array or structure.  */
   3088       switch (sym->st_value)
   3089 	{
   3090 	case 1:
   3091 	  *secp = bfd_make_section_old_way (abfd, ".scommon_b");
   3092 	  break;
   3093 	case 2:
   3094 	  *secp = bfd_make_section_old_way (abfd, ".scommon_h");
   3095 	  break;
   3096 	case 4:
   3097 	  *secp = bfd_make_section_old_way (abfd, ".scommon_w");
   3098 	  break;
   3099 	case 8:
   3100 	  *secp = bfd_make_section_old_way (abfd, ".scommon_d");
   3101 	  break;
   3102 	default:
   3103 	  return TRUE;
   3104 	}
   3105 
   3106       (*secp)->flags |= SEC_IS_COMMON;
   3107       *valp = sym->st_size;
   3108       break;
   3109     }
   3110 
   3111   return TRUE;
   3112 }
   3113 
   3114 
   3115 /* This function can figure out the best location for a base register to access
   3116    data relative to this base register
   3117    INPUT:
   3118    sda_d0: size of first DOUBLE WORD data section
   3119    sda_w0: size of first WORD data section
   3120    sda_h0: size of first HALF WORD data section
   3121    sda_b : size of BYTE data section
   3122    sda_hi: size of second HALF WORD data section
   3123    sda_w1: size of second WORD data section
   3124    sda_d1: size of second DOUBLE WORD data section
   3125    OUTPUT:
   3126    offset (always positive) from the beginning of sda_d0 if OK
   3127    a negative error value if fail
   3128    NOTE:
   3129    these 7 sections have to be located back to back if exist
   3130    a pass in 0 value for non-existing section   */
   3131 
   3132 /* Due to the interpretation of simm15 field of load/store depending on
   3133    data accessing size, the organization of base register relative data shall
   3134    like the following figure
   3135    -------------------------------------------
   3136    |  DOUBLE WORD sized data (range +/- 128K)
   3137    -------------------------------------------
   3138    |  WORD sized data (range +/- 64K)
   3139    -------------------------------------------
   3140    |  HALF WORD sized data (range +/- 32K)
   3141    -------------------------------------------
   3142    |  BYTE sized data (range +/- 16K)
   3143    -------------------------------------------
   3144    |  HALF WORD sized data (range +/- 32K)
   3145    -------------------------------------------
   3146    |  WORD sized data (range +/- 64K)
   3147    -------------------------------------------
   3148    |  DOUBLE WORD sized data (range +/- 128K)
   3149    -------------------------------------------
   3150    Its base register shall be set to access these data freely.  */
   3151 
   3152 /* We have to figure out the SDA_BASE value, so that we can adjust the
   3153    symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
   3154    BFD.  If we can't find it, we're stuck.  We cache it in the ELF
   3155    target data.  We don't need to adjust the symbol value for an
   3156    external symbol if we are producing relocatable output.  */
   3157 
   3158 static asection *sda_rela_sec = NULL;
   3159 
   3160 #define SDA_SECTION_NUM 10
   3161 
   3162 static bfd_reloc_status_type
   3163 nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
   3164 			  bfd_vma *psb, bfd_boolean add_symbol)
   3165 {
   3166   int relax_fp_as_gp;
   3167   struct elf_nds32_link_hash_table *table;
   3168   struct bfd_link_hash_entry *h, *h2;
   3169   long unsigned int total = 0;
   3170 
   3171   h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
   3172   if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
   3173     {
   3174       asection *first = NULL, *final = NULL, *temp;
   3175       bfd_vma sda_base;
   3176       /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
   3177 	 4 byte-aligned.  Therefore, it has to set the first section ".data"
   3178 	 4 byte-aligned.  */
   3179       static const char sec_name[SDA_SECTION_NUM][10] =
   3180 	{
   3181 	  ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
   3182 	  ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
   3183 	};
   3184       size_t i = 0;
   3185 
   3186       if (output_bfd->sections == NULL)
   3187 	{
   3188 	  *psb = elf_gp (output_bfd);
   3189 	  return bfd_reloc_ok;
   3190 	}
   3191 
   3192       /* Get the first and final section.  */
   3193       while (i < sizeof (sec_name) / sizeof (sec_name [0]))
   3194 	{
   3195 	  temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
   3196 	  if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
   3197 	    first = temp;
   3198 	  if (temp && (temp->size != 0 || temp->rawsize != 0))
   3199 	    final = temp;
   3200 
   3201 	  /* Summarize the sections in order to check if joining .bss.  */
   3202 	  if (temp && temp->size != 0)
   3203 	    total += temp->size;
   3204 	  else if (temp && temp->rawsize != 0)
   3205 	    total += temp->rawsize;
   3206 
   3207 	  i++;
   3208 	}
   3209 
   3210       /* Check .bss size.  */
   3211       temp = bfd_get_section_by_name (output_bfd, ".bss");
   3212       if (temp)
   3213 	{
   3214 	  if (temp->size != 0)
   3215 	    total += temp->size;
   3216 	  else if (temp->rawsize != 0)
   3217 	    total += temp->rawsize;
   3218 
   3219 	  if (total < 0x80000)
   3220 	    {
   3221 	      if (!first && (temp->size != 0 || temp->rawsize != 0))
   3222 		first = temp;
   3223 	      if ((temp->size != 0 || temp->rawsize != 0))
   3224 		final = temp;
   3225 	    }
   3226 	}
   3227 
   3228       if (first && final)
   3229 	{
   3230 	  /* The middle of data region.  */
   3231 	  sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
   3232 
   3233 	  /* Find the section sda_base located.  */
   3234 	  i = 0;
   3235 	  while (i < sizeof (sec_name) / sizeof (sec_name [0]))
   3236 	    {
   3237 	      final = bfd_get_section_by_name (output_bfd, sec_name[i]);
   3238 	      if (final && (final->size != 0 || final->rawsize != 0)
   3239 		  && sda_base >= final->vma)
   3240 		{
   3241 		  first = final;
   3242 		  i++;
   3243 		}
   3244 	      else
   3245 		break;
   3246 	    }
   3247 	}
   3248       else
   3249 	{
   3250 	  /* There is not any data section in output bfd, and set _SDA_BASE_ in
   3251 	     first output section.  */
   3252 	  first = output_bfd->sections;
   3253 	  while (first && first->size == 0 && first->rawsize == 0)
   3254 	    first = first->next;
   3255 	  if (!first)
   3256 	    {
   3257 	      *psb = elf_gp (output_bfd);
   3258 	      return bfd_reloc_ok;
   3259 	    }
   3260 	  sda_base = first->vma + first->rawsize;
   3261 	}
   3262 
   3263       sda_base -= first->vma;
   3264       sda_base = sda_base & (~7);
   3265 
   3266       if (!_bfd_generic_link_add_one_symbol
   3267 	     (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
   3268 	      (bfd_vma) sda_base, (const char *) NULL, FALSE,
   3269 	      get_elf_backend_data (output_bfd)->collect, &h))
   3270 	return FALSE;
   3271 
   3272       sda_rela_sec = first;
   3273 
   3274       table = nds32_elf_hash_table (info);
   3275       relax_fp_as_gp = table->relax_fp_as_gp;
   3276       if (relax_fp_as_gp)
   3277 	{
   3278 	  h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
   3279 				     FALSE, FALSE, FALSE);
   3280 	  /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
   3281 	     And set FP equal to SDA_BASE to do relaxation for
   3282 	     la $fp, _FP_BASE_.  */
   3283 	  if (!_bfd_generic_link_add_one_symbol
   3284 		 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
   3285 		  first, (bfd_vma) sda_base, (const char *) NULL,
   3286 		  FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
   3287 	    return FALSE;
   3288 	}
   3289     }
   3290 
   3291   if (add_symbol == TRUE)
   3292     {
   3293       if (h)
   3294 	{
   3295 	  /* Now set gp.  */
   3296 	  elf_gp (output_bfd) = (h->u.def.value
   3297 				 + h->u.def.section->output_section->vma
   3298 				 + h->u.def.section->output_offset);
   3299 	}
   3300       else
   3301 	{
   3302 	  (*_bfd_error_handler) (_("error: Can't find symbol: _SDA_BASE_."));
   3303 	  return bfd_reloc_dangerous;
   3304 	}
   3305     }
   3306 
   3307   *psb = h->u.def.value + h->u.def.section->output_section->vma
   3308 	 + h->u.def.section->output_offset;
   3309   return bfd_reloc_ok;
   3310 }
   3311 
   3312 
   3314 /* Return size of a PLT entry.  */
   3315 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
   3316 
   3317 
   3318 /* Create an entry in an nds32 ELF linker hash table.  */
   3319 
   3320 static struct bfd_hash_entry *
   3321 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
   3322 			     struct bfd_hash_table *table,
   3323 			     const char *string)
   3324 {
   3325   struct elf_nds32_link_hash_entry *ret;
   3326 
   3327   ret = (struct elf_nds32_link_hash_entry *) entry;
   3328 
   3329   /* Allocate the structure if it has not already been allocated by a
   3330      subclass.  */
   3331   if (ret == NULL)
   3332     ret = (struct elf_nds32_link_hash_entry *)
   3333        bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
   3334 
   3335   if (ret == NULL)
   3336     return (struct bfd_hash_entry *) ret;
   3337 
   3338   /* Call the allocation method of the superclass.  */
   3339   ret = (struct elf_nds32_link_hash_entry *)
   3340     _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
   3341 
   3342   if (ret != NULL)
   3343     {
   3344       struct elf_nds32_link_hash_entry *eh;
   3345 
   3346       eh = (struct elf_nds32_link_hash_entry *) ret;
   3347       eh->dyn_relocs = NULL;
   3348       eh->tls_type = GOT_UNKNOWN;
   3349     }
   3350 
   3351   return (struct bfd_hash_entry *) ret;
   3352 }
   3353 
   3354 /* Create an nds32 ELF linker hash table.  */
   3355 
   3356 static struct bfd_link_hash_table *
   3357 nds32_elf_link_hash_table_create (bfd *abfd)
   3358 {
   3359   struct elf_nds32_link_hash_table *ret;
   3360 
   3361   bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
   3362 
   3363   ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
   3364   if (ret == NULL)
   3365     return NULL;
   3366 
   3367   /* patch tag.  */
   3368   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
   3369 				      nds32_elf_link_hash_newfunc,
   3370 				      sizeof (struct elf_nds32_link_hash_entry),
   3371 				      NDS32_ELF_DATA))
   3372     {
   3373       free (ret);
   3374       return NULL;
   3375     }
   3376 
   3377   ret->sgot = NULL;
   3378   ret->sgotplt = NULL;
   3379   ret->srelgot = NULL;
   3380   ret->splt = NULL;
   3381   ret->srelplt = NULL;
   3382   ret->sdynbss = NULL;
   3383   ret->srelbss = NULL;
   3384   ret->sym_ld_script = NULL;
   3385   ret->ex9_export_file = NULL;
   3386   ret->ex9_import_file = NULL;
   3387 
   3388   return &ret->root.root;
   3389 }
   3390 
   3391 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
   3392    shortcuts to them in our hash table.  */
   3393 
   3394 static bfd_boolean
   3395 create_got_section (bfd *dynobj, struct bfd_link_info *info)
   3396 {
   3397   struct elf_nds32_link_hash_table *htab;
   3398 
   3399   if (!_bfd_elf_create_got_section (dynobj, info))
   3400     return FALSE;
   3401 
   3402   htab = nds32_elf_hash_table (info);
   3403   htab->sgot = bfd_get_section_by_name (dynobj, ".got");
   3404   htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
   3405   if (!htab->sgot || !htab->sgotplt)
   3406     abort ();
   3407 
   3408   /* _bfd_elf_create_got_section will create it for us.  */
   3409   htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
   3410   if (htab->srelgot == NULL
   3411       || !bfd_set_section_flags (dynobj, htab->srelgot,
   3412 				 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
   3413 				  | SEC_IN_MEMORY | SEC_LINKER_CREATED
   3414 				  | SEC_READONLY))
   3415       || !bfd_set_section_alignment (dynobj, htab->srelgot, 2))
   3416     return FALSE;
   3417 
   3418   return TRUE;
   3419 }
   3420 
   3421 /* Create dynamic sections when linking against a dynamic object.  */
   3422 
   3423 static bfd_boolean
   3424 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   3425 {
   3426   struct elf_nds32_link_hash_table *htab;
   3427   flagword flags, pltflags;
   3428   register asection *s;
   3429   const struct elf_backend_data *bed;
   3430   int ptralign = 2;		/* 32-bit  */
   3431 
   3432   bed = get_elf_backend_data (abfd);
   3433 
   3434   htab = nds32_elf_hash_table (info);
   3435 
   3436   /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
   3437      .rel[a].bss sections.  */
   3438 
   3439   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
   3440 	   | SEC_LINKER_CREATED);
   3441 
   3442   pltflags = flags;
   3443   pltflags |= SEC_CODE;
   3444   if (bed->plt_not_loaded)
   3445     pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
   3446   if (bed->plt_readonly)
   3447     pltflags |= SEC_READONLY;
   3448 
   3449   s = bfd_make_section (abfd, ".plt");
   3450   htab->splt = s;
   3451   if (s == NULL
   3452       || !bfd_set_section_flags (abfd, s, pltflags)
   3453       || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
   3454     return FALSE;
   3455 
   3456   if (bed->want_plt_sym)
   3457     {
   3458       /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
   3459 	 .plt section.  */
   3460       struct bfd_link_hash_entry *bh = NULL;
   3461       struct elf_link_hash_entry *h;
   3462 
   3463       if (!(_bfd_generic_link_add_one_symbol
   3464 	    (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
   3465 	     (bfd_vma) 0, (const char *) NULL, FALSE,
   3466 	     get_elf_backend_data (abfd)->collect, &bh)))
   3467 	return FALSE;
   3468 
   3469       h = (struct elf_link_hash_entry *) bh;
   3470       h->def_regular = 1;
   3471       h->type = STT_OBJECT;
   3472 
   3473       if (info->shared && !bfd_elf_link_record_dynamic_symbol (info, h))
   3474 	return FALSE;
   3475     }
   3476 
   3477   s = bfd_make_section (abfd,
   3478 			bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
   3479   htab->srelplt = s;
   3480   if (s == NULL
   3481       || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
   3482       || !bfd_set_section_alignment (abfd, s, ptralign))
   3483     return FALSE;
   3484 
   3485   if (htab->sgot == NULL && !create_got_section (abfd, info))
   3486     return FALSE;
   3487 
   3488   {
   3489     const char *secname;
   3490     char *relname;
   3491     flagword secflags;
   3492     asection *sec;
   3493 
   3494     for (sec = abfd->sections; sec; sec = sec->next)
   3495       {
   3496 	secflags = bfd_get_section_flags (abfd, sec);
   3497 	if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
   3498 	    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
   3499 	  continue;
   3500 	secname = bfd_get_section_name (abfd, sec);
   3501 	relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
   3502 	strcpy (relname, ".rela");
   3503 	strcat (relname, secname);
   3504 	if (bfd_get_section_by_name (abfd, secname))
   3505 	  continue;
   3506 	s = bfd_make_section (abfd, relname);
   3507 	if (s == NULL
   3508 	    || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
   3509 	    || !bfd_set_section_alignment (abfd, s, ptralign))
   3510 	  return FALSE;
   3511       }
   3512   }
   3513 
   3514   if (bed->want_dynbss)
   3515     {
   3516       /* The .dynbss section is a place to put symbols which are defined
   3517 	 by dynamic objects, are referenced by regular objects, and are
   3518 	 not functions.  We must allocate space for them in the process
   3519 	 image and use a R_*_COPY reloc to tell the dynamic linker to
   3520 	 initialize them at run time.  The linker script puts the .dynbss
   3521 	 section into the .bss section of the final image.  */
   3522       s = bfd_make_section (abfd, ".dynbss");
   3523       htab->sdynbss = s;
   3524       if (s == NULL
   3525 	  || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
   3526 	return FALSE;
   3527       /* The .rel[a].bss section holds copy relocs.  This section is not
   3528 	 normally needed.  We need to create it here, though, so that the
   3529 	 linker will map it to an output section.  We can't just create it
   3530 	 only if we need it, because we will not know whether we need it
   3531 	 until we have seen all the input files, and the first time the
   3532 	 main linker code calls BFD after examining all the input files
   3533 	 (size_dynamic_sections) the input sections have already been
   3534 	 mapped to the output sections.  If the section turns out not to
   3535 	 be needed, we can discard it later.  We will never need this
   3536 	 section when generating a shared object, since they do not use
   3537 	 copy relocs.  */
   3538       if (!info->shared)
   3539 	{
   3540 	  s = bfd_make_section (abfd, (bed->default_use_rela_p
   3541 				       ? ".rela.bss" : ".rel.bss"));
   3542 	  htab->srelbss = s;
   3543 	  if (s == NULL
   3544 	      || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
   3545 	      || !bfd_set_section_alignment (abfd, s, ptralign))
   3546 	    return FALSE;
   3547 	}
   3548     }
   3549 
   3550   return TRUE;
   3551 }
   3552 
   3553 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
   3554 static void
   3555 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
   3556 				struct elf_link_hash_entry *dir,
   3557 				struct elf_link_hash_entry *ind)
   3558 {
   3559   struct elf_nds32_link_hash_entry *edir, *eind;
   3560 
   3561   edir = (struct elf_nds32_link_hash_entry *) dir;
   3562   eind = (struct elf_nds32_link_hash_entry *) ind;
   3563 
   3564   if (eind->dyn_relocs != NULL)
   3565     {
   3566       if (edir->dyn_relocs != NULL)
   3567 	{
   3568 	  struct elf_nds32_dyn_relocs **pp;
   3569 	  struct elf_nds32_dyn_relocs *p;
   3570 
   3571 	  if (ind->root.type == bfd_link_hash_indirect)
   3572 	    abort ();
   3573 
   3574 	  /* Add reloc counts against the weak sym to the strong sym
   3575 	     list.  Merge any entries against the same section.  */
   3576 	  for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
   3577 	    {
   3578 	      struct elf_nds32_dyn_relocs *q;
   3579 
   3580 	      for (q = edir->dyn_relocs; q != NULL; q = q->next)
   3581 		if (q->sec == p->sec)
   3582 		  {
   3583 		    q->pc_count += p->pc_count;
   3584 		    q->count += p->count;
   3585 		    *pp = p->next;
   3586 		    break;
   3587 		  }
   3588 	      if (q == NULL)
   3589 		pp = &p->next;
   3590 	    }
   3591 	  *pp = edir->dyn_relocs;
   3592 	}
   3593 
   3594       edir->dyn_relocs = eind->dyn_relocs;
   3595       eind->dyn_relocs = NULL;
   3596     }
   3597 
   3598   _bfd_elf_link_hash_copy_indirect (info, dir, ind);
   3599 }
   3600 
   3601 
   3603 /* Adjust a symbol defined by a dynamic object and referenced by a
   3604    regular object.  The current definition is in some section of the
   3605    dynamic object, but we're not including those sections.  We have to
   3606    change the definition to something the rest of the link can
   3607    understand.  */
   3608 
   3609 static bfd_boolean
   3610 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   3611 				 struct elf_link_hash_entry *h)
   3612 {
   3613   struct elf_nds32_link_hash_table *htab;
   3614   struct elf_nds32_link_hash_entry *eh;
   3615   struct elf_nds32_dyn_relocs *p;
   3616   bfd *dynobj;
   3617   asection *s;
   3618   unsigned int power_of_two;
   3619 
   3620   dynobj = elf_hash_table (info)->dynobj;
   3621 
   3622   /* Make sure we know what is going on here.  */
   3623   BFD_ASSERT (dynobj != NULL
   3624 	      && (h->needs_plt
   3625 		  || h->u.weakdef != NULL
   3626 		  || (h->def_dynamic && h->ref_regular && !h->def_regular)));
   3627 
   3628 
   3629   /* If this is a function, put it in the procedure linkage table.  We
   3630      will fill in the contents of the procedure linkage table later,
   3631      when we know the address of the .got section.  */
   3632   if (h->type == STT_FUNC || h->needs_plt)
   3633     {
   3634       if (!info->shared
   3635 	  && !h->def_dynamic
   3636 	  && !h->ref_dynamic
   3637 	  && h->root.type != bfd_link_hash_undefweak
   3638 	  && h->root.type != bfd_link_hash_undefined)
   3639 	{
   3640 	  /* This case can occur if we saw a PLT reloc in an input
   3641 	     file, but the symbol was never referred to by a dynamic
   3642 	     object.  In such a case, we don't actually need to build
   3643 	     a procedure linkage table, and we can just do a PCREL
   3644 	     reloc instead.  */
   3645 	  h->plt.offset = (bfd_vma) - 1;
   3646 	  h->needs_plt = 0;
   3647 	}
   3648 
   3649       return TRUE;
   3650     }
   3651   else
   3652     h->plt.offset = (bfd_vma) - 1;
   3653 
   3654   /* If this is a weak symbol, and there is a real definition, the
   3655      processor independent code will have arranged for us to see the
   3656      real definition first, and we can just use the same value.  */
   3657   if (h->u.weakdef != NULL)
   3658     {
   3659       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
   3660 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
   3661       h->root.u.def.section = h->u.weakdef->root.u.def.section;
   3662       h->root.u.def.value = h->u.weakdef->root.u.def.value;
   3663       return TRUE;
   3664     }
   3665 
   3666   /* This is a reference to a symbol defined by a dynamic object which
   3667      is not a function.  */
   3668 
   3669   /* If we are creating a shared library, we must presume that the
   3670      only references to the symbol are via the global offset table.
   3671      For such cases we need not do anything here; the relocations will
   3672      be handled correctly by relocate_section.  */
   3673   if (info->shared)
   3674     return TRUE;
   3675 
   3676   /* If there are no references to this symbol that do not use the
   3677      GOT, we don't need to generate a copy reloc.  */
   3678   if (!h->non_got_ref)
   3679     return TRUE;
   3680 
   3681   /* If -z nocopyreloc was given, we won't generate them either.  */
   3682   if (info->nocopyreloc)
   3683     {
   3684       h->non_got_ref = 0;
   3685       return TRUE;
   3686     }
   3687 
   3688   eh = (struct elf_nds32_link_hash_entry *) h;
   3689   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   3690     {
   3691       s = p->sec->output_section;
   3692       if (s != NULL && (s->flags & (SEC_READONLY | SEC_HAS_CONTENTS)) != 0)
   3693 	break;
   3694     }
   3695 
   3696   /* If we didn't find any dynamic relocs in sections which needs the
   3697      copy reloc, then we'll be keeping the dynamic relocs and avoiding
   3698      the copy reloc.  */
   3699   if (p == NULL)
   3700     {
   3701       h->non_got_ref = 0;
   3702       return TRUE;
   3703     }
   3704 
   3705   /* We must allocate the symbol in our .dynbss section, which will
   3706      become part of the .bss section of the executable.  There will be
   3707      an entry for this symbol in the .dynsym section.  The dynamic
   3708      object will contain position independent code, so all references
   3709      from the dynamic object to this symbol will go through the global
   3710      offset table.  The dynamic linker will use the .dynsym entry to
   3711      determine the address it must put in the global offset table, so
   3712      both the dynamic object and the regular object will refer to the
   3713      same memory location for the variable.  */
   3714 
   3715   htab = nds32_elf_hash_table (info);
   3716   s = htab->sdynbss;
   3717   BFD_ASSERT (s != NULL);
   3718 
   3719   /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
   3720      to copy the initial value out of the dynamic object and into the
   3721      runtime process image.  We need to remember the offset into the
   3722      .rela.bss section we are going to use.  */
   3723   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
   3724     {
   3725       asection *srel;
   3726 
   3727       srel = htab->srelbss;
   3728       BFD_ASSERT (srel != NULL);
   3729       srel->size += sizeof (Elf32_External_Rela);
   3730       h->needs_copy = 1;
   3731     }
   3732 
   3733   /* We need to figure out the alignment required for this symbol.  I
   3734      have no idea how ELF linkers handle this.  */
   3735   power_of_two = bfd_log2 (h->size);
   3736   if (power_of_two > 3)
   3737     power_of_two = 3;
   3738 
   3739   /* Apply the required alignment.  */
   3740   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
   3741   if (power_of_two > bfd_get_section_alignment (dynobj, s))
   3742     {
   3743       if (!bfd_set_section_alignment (dynobj, s, power_of_two))
   3744 	return FALSE;
   3745     }
   3746 
   3747   /* Define the symbol as being at this point in the section.  */
   3748   h->root.u.def.section = s;
   3749   h->root.u.def.value = s->size;
   3750 
   3751   /* Increment the section size to make room for the symbol.  */
   3752   s->size += h->size;
   3753 
   3754   return TRUE;
   3755 }
   3756 
   3757 /* Allocate space in .plt, .got and associated reloc sections for
   3758    dynamic relocs.  */
   3759 
   3760 static bfd_boolean
   3761 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   3762 {
   3763   struct bfd_link_info *info;
   3764   struct elf_nds32_link_hash_table *htab;
   3765   struct elf_nds32_link_hash_entry *eh;
   3766   struct elf_nds32_dyn_relocs *p;
   3767 
   3768   if (h->root.type == bfd_link_hash_indirect)
   3769     return TRUE;
   3770 
   3771   if (h->root.type == bfd_link_hash_warning)
   3772     /* When warning symbols are created, they **replace** the "real"
   3773        entry in the hash table, thus we never get to see the real
   3774        symbol in a hash traversal.  So look at it now.  */
   3775     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3776 
   3777   info = (struct bfd_link_info *) inf;
   3778   htab = nds32_elf_hash_table (info);
   3779 
   3780   eh = (struct elf_nds32_link_hash_entry *) h;
   3781 
   3782   if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
   3783     {
   3784       /* Make sure this symbol is output as a dynamic symbol.
   3785 	 Undefined weak syms won't yet be marked as dynamic.  */
   3786       if (h->dynindx == -1 && !h->forced_local)
   3787 	{
   3788 	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3789 	    return FALSE;
   3790 	}
   3791 
   3792       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
   3793 	{
   3794 	  asection *s = htab->splt;
   3795 
   3796 	  /* If this is the first .plt entry, make room for the special
   3797 	     first entry.  */
   3798 	  if (s->size == 0)
   3799 	    s->size += PLT_ENTRY_SIZE;
   3800 
   3801 	  h->plt.offset = s->size;
   3802 
   3803 	  /* If this symbol is not defined in a regular file, and we are
   3804 	     not generating a shared library, then set the symbol to this
   3805 	     location in the .plt.  This is required to make function
   3806 	     pointers compare as equal between the normal executable and
   3807 	     the shared library.  */
   3808 	  if (!info->shared && !h->def_regular)
   3809 	    {
   3810 	      h->root.u.def.section = s;
   3811 	      h->root.u.def.value = h->plt.offset;
   3812 	    }
   3813 
   3814 	  /* Make room for this entry.  */
   3815 	  s->size += PLT_ENTRY_SIZE;
   3816 
   3817 	  /* We also need to make an entry in the .got.plt section, which
   3818 	     will be placed in the .got section by the linker script.  */
   3819 	  htab->sgotplt->size += 4;
   3820 
   3821 	  /* We also need to make an entry in the .rel.plt section.  */
   3822 	  htab->srelplt->size += sizeof (Elf32_External_Rela);
   3823 	}
   3824       else
   3825 	{
   3826 	  h->plt.offset = (bfd_vma) - 1;
   3827 	  h->needs_plt = 0;
   3828 	}
   3829     }
   3830   else
   3831     {
   3832       h->plt.offset = (bfd_vma) - 1;
   3833       h->needs_plt = 0;
   3834     }
   3835 
   3836   if (h->got.refcount > 0)
   3837     {
   3838       asection *s;
   3839       bfd_boolean dyn;
   3840       int tls_type = elf32_nds32_hash_entry (h)->tls_type;
   3841 
   3842       /* Make sure this symbol is output as a dynamic symbol.
   3843 	 Undefined weak syms won't yet be marked as dynamic.  */
   3844       if (h->dynindx == -1 && !h->forced_local)
   3845 	{
   3846 	  if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3847 	    return FALSE;
   3848 	}
   3849 
   3850       s = htab->sgot;
   3851       h->got.offset = s->size;
   3852 
   3853       if (tls_type == GOT_UNKNOWN)
   3854 	abort ();
   3855       else if (tls_type == GOT_NORMAL
   3856 	       || tls_type == GOT_TLS_IE)
   3857 	/* Need a GOT slot.  */
   3858 	s->size += 4;
   3859 
   3860       dyn = htab->root.dynamic_sections_created;
   3861       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h))
   3862 	htab->srelgot->size += sizeof (Elf32_External_Rela);
   3863     }
   3864   else
   3865     h->got.offset = (bfd_vma) - 1;
   3866 
   3867   if (eh->dyn_relocs == NULL)
   3868     return TRUE;
   3869 
   3870   /* In the shared -Bsymbolic case, discard space allocated for
   3871      dynamic pc-relative relocs against symbols which turn out to be
   3872      defined in regular objects.  For the normal shared case, discard
   3873      space for pc-relative relocs that have become local due to symbol
   3874      visibility changes.  */
   3875 
   3876   if (info->shared)
   3877     {
   3878       if (h->def_regular && (h->forced_local || info->symbolic))
   3879 	{
   3880 	  struct elf_nds32_dyn_relocs **pp;
   3881 
   3882 	  for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
   3883 	    {
   3884 	      p->count -= p->pc_count;
   3885 	      p->pc_count = 0;
   3886 	      if (p->count == 0)
   3887 		*pp = p->next;
   3888 	      else
   3889 		pp = &p->next;
   3890 	    }
   3891 	}
   3892     }
   3893   else
   3894     {
   3895       /* For the non-shared case, discard space for relocs against
   3896 	 symbols which turn out to need copy relocs or are not dynamic.  */
   3897 
   3898       if (!h->non_got_ref
   3899 	  && ((h->def_dynamic
   3900 	       && !h->def_regular)
   3901 	      || (htab->root.dynamic_sections_created
   3902 		  && (h->root.type == bfd_link_hash_undefweak
   3903 		      || h->root.type == bfd_link_hash_undefined))))
   3904 	{
   3905 	  /* Make sure this symbol is output as a dynamic symbol.
   3906 	     Undefined weak syms won't yet be marked as dynamic.  */
   3907 	  if (h->dynindx == -1 && !h->forced_local)
   3908 	    {
   3909 	      if (!bfd_elf_link_record_dynamic_symbol (info, h))
   3910 		return FALSE;
   3911 	    }
   3912 
   3913 	  /* If that succeeded, we know we'll be keeping all the
   3914 	     relocs.  */
   3915 	  if (h->dynindx != -1)
   3916 	    goto keep;
   3917 	}
   3918 
   3919       eh->dyn_relocs = NULL;
   3920 
   3921     keep:;
   3922     }
   3923 
   3924   /* Finally, allocate space.  */
   3925   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   3926     {
   3927       asection *sreloc = elf_section_data (p->sec)->sreloc;
   3928       sreloc->size += p->count * sizeof (Elf32_External_Rela);
   3929     }
   3930 
   3931   return TRUE;
   3932 }
   3933 
   3934 /* Find any dynamic relocs that apply to read-only sections.  */
   3935 
   3936 static bfd_boolean
   3937 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   3938 {
   3939   struct elf_nds32_link_hash_entry *eh;
   3940   struct elf_nds32_dyn_relocs *p;
   3941 
   3942   if (h->root.type == bfd_link_hash_warning)
   3943     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   3944 
   3945   eh = (struct elf_nds32_link_hash_entry *) h;
   3946   for (p = eh->dyn_relocs; p != NULL; p = p->next)
   3947     {
   3948       asection *s = p->sec->output_section;
   3949 
   3950       if (s != NULL && (s->flags & SEC_READONLY) != 0)
   3951 	{
   3952 	  struct bfd_link_info *info = (struct bfd_link_info *) inf;
   3953 
   3954 	  info->flags |= DF_TEXTREL;
   3955 
   3956 	  /* Not an error, just cut short the traversal.  */
   3957 	  return FALSE;
   3958 	}
   3959     }
   3960   return TRUE;
   3961 }
   3962 
   3963 /* Set the sizes of the dynamic sections.  */
   3964 
   3965 static bfd_boolean
   3966 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
   3967 				 struct bfd_link_info *info)
   3968 {
   3969   struct elf_nds32_link_hash_table *htab;
   3970   bfd *dynobj;
   3971   asection *s;
   3972   bfd_boolean relocs;
   3973   bfd *ibfd;
   3974 
   3975   htab = nds32_elf_hash_table (info);
   3976   dynobj = htab->root.dynobj;
   3977   BFD_ASSERT (dynobj != NULL);
   3978 
   3979   if (htab->root.dynamic_sections_created)
   3980     {
   3981       /* Set the contents of the .interp section to the interpreter.  */
   3982       if (!info->shared)
   3983 	{
   3984 	  s = bfd_get_section_by_name (dynobj, ".interp");
   3985 	  BFD_ASSERT (s != NULL);
   3986 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
   3987 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
   3988 	}
   3989     }
   3990 
   3991   /* Set up .got offsets for local syms, and space for local dynamic
   3992      relocs.  */
   3993   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
   3994     {
   3995       bfd_signed_vma *local_got;
   3996       bfd_signed_vma *end_local_got;
   3997       bfd_size_type locsymcount;
   3998       Elf_Internal_Shdr *symtab_hdr;
   3999       asection *srel;
   4000 
   4001       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
   4002 	continue;
   4003 
   4004       for (s = ibfd->sections; s != NULL; s = s->next)
   4005 	{
   4006 	  struct elf_nds32_dyn_relocs *p;
   4007 
   4008 	  for (p = ((struct elf_nds32_dyn_relocs *)
   4009 		    elf_section_data (s)->local_dynrel);
   4010 	       p != NULL; p = p->next)
   4011 	    {
   4012 	      if (!bfd_is_abs_section (p->sec)
   4013 		  && bfd_is_abs_section (p->sec->output_section))
   4014 		{
   4015 		  /* Input section has been discarded, either because
   4016 		     it is a copy of a linkonce section or due to
   4017 		     linker script /DISCARD/, so we'll be discarding
   4018 		     the relocs too.  */
   4019 		}
   4020 	      else if (p->count != 0)
   4021 		{
   4022 		  srel = elf_section_data (p->sec)->sreloc;
   4023 		  srel->size += p->count * sizeof (Elf32_External_Rela);
   4024 		  if ((p->sec->output_section->flags & SEC_READONLY) != 0)
   4025 		    info->flags |= DF_TEXTREL;
   4026 		}
   4027 	    }
   4028 	}
   4029 
   4030       local_got = elf_local_got_refcounts (ibfd);
   4031       if (!local_got)
   4032 	continue;
   4033 
   4034       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
   4035       locsymcount = symtab_hdr->sh_info;
   4036       end_local_got = local_got + locsymcount;
   4037       s = htab->sgot;
   4038       srel = htab->srelgot;
   4039       for (; local_got < end_local_got; ++local_got)
   4040 	{
   4041 	  if (*local_got > 0)
   4042 	    {
   4043 	      *local_got = s->size;
   4044 	      s->size += 4;
   4045 	      if (info->shared)
   4046 		srel->size += sizeof (Elf32_External_Rela);
   4047 	    }
   4048 	  else
   4049 	    *local_got = (bfd_vma) - 1;
   4050 	}
   4051     }
   4052 
   4053   /* Allocate global sym .plt and .got entries, and space for global
   4054      sym dynamic relocs.  */
   4055   elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
   4056 
   4057   /* We now have determined the sizes of the various dynamic sections.
   4058      Allocate memory for them.  */
   4059   relocs = FALSE;
   4060   for (s = dynobj->sections; s != NULL; s = s->next)
   4061     {
   4062       if ((s->flags & SEC_LINKER_CREATED) == 0)
   4063 	continue;
   4064 
   4065       if (s == htab->splt)
   4066 	{
   4067 	  /* Strip this section if we don't need it; see the
   4068 	     comment below.  */
   4069 	}
   4070       else if (s == htab->sgot)
   4071 	{
   4072 	  got_size += s->size;
   4073 	}
   4074       else if (s == htab->sgotplt)
   4075 	{
   4076 	  got_size += s->size;
   4077 	}
   4078       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
   4079 	{
   4080 	  if (s->size != 0 && s != htab->srelplt)
   4081 	    relocs = TRUE;
   4082 
   4083 	  /* We use the reloc_count field as a counter if we need
   4084 	     to copy relocs into the output file.  */
   4085 	  s->reloc_count = 0;
   4086 	}
   4087       else
   4088 	{
   4089 	  /* It's not one of our sections, so don't allocate space.  */
   4090 	  continue;
   4091 	}
   4092 
   4093       if (s->size == 0)
   4094 	{
   4095 	  /* If we don't need this section, strip it from the
   4096 	     output file.  This is mostly to handle .rela.bss and
   4097 	     .rela.plt.  We must create both sections in
   4098 	     create_dynamic_sections, because they must be created
   4099 	     before the linker maps input sections to output
   4100 	     sections.  The linker does that before
   4101 	     adjust_dynamic_symbol is called, and it is that
   4102 	     function which decides whether anything needs to go
   4103 	     into these sections.  */
   4104 	  s->flags |= SEC_EXCLUDE;
   4105 	  continue;
   4106 	}
   4107 
   4108       /* Allocate memory for the section contents.  We use bfd_zalloc
   4109 	 here in case unused entries are not reclaimed before the
   4110 	 section's contents are written out.  This should not happen,
   4111 	 but this way if it does, we get a R_NDS32_NONE reloc instead
   4112 	 of garbage.  */
   4113       s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
   4114       if (s->contents == NULL)
   4115 	return FALSE;
   4116     }
   4117 
   4118 
   4119   if (htab->root.dynamic_sections_created)
   4120     {
   4121       /* Add some entries to the .dynamic section.  We fill in the
   4122 	 values later, in nds32_elf_finish_dynamic_sections, but we
   4123 	 must add the entries now so that we get the correct size for
   4124 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
   4125 	 dynamic linker and used by the debugger.  */
   4126 #define add_dynamic_entry(TAG, VAL) \
   4127   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
   4128 
   4129       if (!info->shared)
   4130 	{
   4131 	  if (!add_dynamic_entry (DT_DEBUG, 0))
   4132 	    return FALSE;
   4133 	}
   4134 
   4135       if (htab->splt->size != 0)
   4136 	{
   4137 	  if (!add_dynamic_entry (DT_PLTGOT, 0)
   4138 	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
   4139 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
   4140 	      || !add_dynamic_entry (DT_JMPREL, 0))
   4141 	    return FALSE;
   4142 	}
   4143 
   4144       if (relocs)
   4145 	{
   4146 	  if (!add_dynamic_entry (DT_RELA, 0)
   4147 	      || !add_dynamic_entry (DT_RELASZ, 0)
   4148 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
   4149 	    return FALSE;
   4150 
   4151 	  /* If any dynamic relocs apply to a read-only section,
   4152 	     then we need a DT_TEXTREL entry.  */
   4153 	  if ((info->flags & DF_TEXTREL) == 0)
   4154 	    elf_link_hash_traverse (&htab->root, readonly_dynrelocs,
   4155 				    (void *) info);
   4156 
   4157 	  if ((info->flags & DF_TEXTREL) != 0)
   4158 	    {
   4159 	      if (!add_dynamic_entry (DT_TEXTREL, 0))
   4160 		return FALSE;
   4161 	    }
   4162 	}
   4163     }
   4164 #undef add_dynamic_entry
   4165 
   4166   return TRUE;
   4167 }
   4168 
   4169 static bfd_reloc_status_type
   4170 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
   4171 			 bfd_vma relocation, bfd_byte *location)
   4172 {
   4173   int size;
   4174   bfd_vma x = 0;
   4175   bfd_reloc_status_type flag;
   4176   unsigned int rightshift = howto->rightshift;
   4177   unsigned int bitpos = howto->bitpos;
   4178 
   4179   /* If the size is negative, negate RELOCATION.  This isn't very
   4180      general.  */
   4181   if (howto->size < 0)
   4182     relocation = -relocation;
   4183 
   4184   /* Get the value we are going to relocate.  */
   4185   size = bfd_get_reloc_size (howto);
   4186   switch (size)
   4187     {
   4188     default:
   4189     case 0:
   4190     case 1:
   4191     case 8:
   4192       abort ();
   4193       break;
   4194     case 2:
   4195       x = bfd_getb16 (location);
   4196       break;
   4197     case 4:
   4198       x = bfd_getb32 (location);
   4199       break;
   4200     }
   4201 
   4202   /* Check for overflow.  FIXME: We may drop bits during the addition
   4203      which we don't check for.  We must either check at every single
   4204      operation, which would be tedious, or we must do the computations
   4205      in a type larger than bfd_vma, which would be inefficient.  */
   4206   flag = bfd_reloc_ok;
   4207   if (howto->complain_on_overflow != complain_overflow_dont)
   4208     {
   4209       bfd_vma addrmask, fieldmask, signmask, ss;
   4210       bfd_vma a, b, sum;
   4211 
   4212       /* Get the values to be added together.  For signed and unsigned
   4213 	 relocations, we assume that all values should be truncated to
   4214 	 the size of an address.  For bitfields, all the bits matter.
   4215 	 See also bfd_check_overflow.  */
   4216       fieldmask = N_ONES (howto->bitsize);
   4217       signmask = ~fieldmask;
   4218       addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
   4219       a = (relocation & addrmask) >> rightshift;
   4220       b = (x & howto->src_mask & addrmask) >> bitpos;
   4221 
   4222       switch (howto->complain_on_overflow)
   4223 	{
   4224 	case complain_overflow_signed:
   4225 	  /* If any sign bits are set, all sign bits must be set.
   4226 	     That is, A must be a valid negative address after
   4227 	     shifting.  */
   4228 	  signmask = ~(fieldmask >> 1);
   4229 	  /* Fall through.  */
   4230 
   4231 	case complain_overflow_bitfield:
   4232 	  /* Much like the signed check, but for a field one bit
   4233 	     wider.  We allow a bitfield to represent numbers in the
   4234 	     range -2**n to 2**n-1, where n is the number of bits in the
   4235 	     field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
   4236 	     can't overflow, which is exactly what we want.  */
   4237 	  ss = a & signmask;
   4238 	  if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
   4239 	    flag = bfd_reloc_overflow;
   4240 
   4241 	  /* We only need this next bit of code if the sign bit of B
   4242 	     is below the sign bit of A.  This would only happen if
   4243 	     SRC_MASK had fewer bits than BITSIZE.  Note that if
   4244 	     SRC_MASK has more bits than BITSIZE, we can get into
   4245 	     trouble; we would need to verify that B is in range, as
   4246 	     we do for A above.  */
   4247 	  ss = ((~howto->src_mask) >> 1) & howto->src_mask;
   4248 	  ss >>= bitpos;
   4249 
   4250 	  /* Set all the bits above the sign bit.  */
   4251 	  b = (b ^ ss) - ss;
   4252 
   4253 	  /* Now we can do the addition.  */
   4254 	  sum = a + b;
   4255 
   4256 	  /* See if the result has the correct sign.  Bits above the
   4257 	     sign bit are junk now; ignore them.  If the sum is
   4258 	     positive, make sure we did not have all negative inputs;
   4259 	     if the sum is negative, make sure we did not have all
   4260 	     positive inputs.  The test below looks only at the sign
   4261 	     bits, and it really just
   4262 	     SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
   4263 
   4264 	     We mask with addrmask here to explicitly allow an address
   4265 	     wrap-around.  The Linux kernel relies on it, and it is
   4266 	     the only way to write assembler code which can run when
   4267 	     loaded at a location 0x80000000 away from the location at
   4268 	     which it is linked.  */
   4269 	  if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
   4270 	    flag = bfd_reloc_overflow;
   4271 
   4272 	  break;
   4273 
   4274 	case complain_overflow_unsigned:
   4275 	  /* Checking for an unsigned overflow is relatively easy:
   4276 	     trim the addresses and add, and trim the result as well.
   4277 	     Overflow is normally indicated when the result does not
   4278 	     fit in the field.  However, we also need to consider the
   4279 	     case when, e.g., fieldmask is 0x7fffffff or smaller, an
   4280 	     input is 0x80000000, and bfd_vma is only 32 bits; then we
   4281 	     will get sum == 0, but there is an overflow, since the
   4282 	     inputs did not fit in the field.  Instead of doing a
   4283 	     separate test, we can check for this by or-ing in the
   4284 	     operands when testing for the sum overflowing its final
   4285 	     field.  */
   4286 	  sum = (a + b) & addrmask;
   4287 	  if ((a | b | sum) & signmask)
   4288 	    flag = bfd_reloc_overflow;
   4289 	  break;
   4290 
   4291 	default:
   4292 	  abort ();
   4293 	}
   4294     }
   4295 
   4296   /* Put RELOCATION in the right bits.  */
   4297   relocation >>= (bfd_vma) rightshift;
   4298   relocation <<= (bfd_vma) bitpos;
   4299 
   4300   /* Add RELOCATION to the right bits of X.  */
   4301   /* FIXME : 090616
   4302      Because the relaxation may generate duplicate relocation at one address,
   4303      an addition to immediate in the instruction may cause the relocation added
   4304      several times.
   4305      This bug should be fixed in assembler, but a check is also needed here.  */
   4306   if (howto->partial_inplace)
   4307     x = ((x & ~howto->dst_mask)
   4308 	 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
   4309   else
   4310     x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
   4311 
   4312 
   4313   /* Put the relocated value back in the object file.  */
   4314   switch (size)
   4315     {
   4316     default:
   4317     case 0:
   4318     case 1:
   4319     case 8:
   4320       abort ();
   4321       break;
   4322     case 2:
   4323       bfd_putb16 (x, location);
   4324       break;
   4325     case 4:
   4326       bfd_putb32 (x, location);
   4327       break;
   4328     }
   4329 
   4330   return flag;
   4331 }
   4332 
   4333 static bfd_reloc_status_type
   4334 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
   4335 			       asection *input_section, bfd_byte *contents,
   4336 			       bfd_vma address, bfd_vma value, bfd_vma addend)
   4337 {
   4338   bfd_vma relocation;
   4339 
   4340   /* Sanity check the address.  */
   4341   if (address > bfd_get_section_limit (input_bfd, input_section))
   4342     return bfd_reloc_outofrange;
   4343 
   4344   /* This function assumes that we are dealing with a basic relocation
   4345      against a symbol.  We want to compute the value of the symbol to
   4346      relocate to.  This is just VALUE, the value of the symbol, plus
   4347      ADDEND, any addend associated with the reloc.  */
   4348   relocation = value + addend;
   4349 
   4350   /* If the relocation is PC relative, we want to set RELOCATION to
   4351      the distance between the symbol (currently in RELOCATION) and the
   4352      location we are relocating.  Some targets (e.g., i386-aout)
   4353      arrange for the contents of the section to be the negative of the
   4354      offset of the location within the section; for such targets
   4355      pcrel_offset is FALSE.  Other targets (e.g., m88kbcs or ELF)
   4356      simply leave the contents of the section as zero; for such
   4357      targets pcrel_offset is TRUE.  If pcrel_offset is FALSE we do not
   4358      need to subtract out the offset of the location within the
   4359      section (which is just ADDRESS).  */
   4360   if (howto->pc_relative)
   4361     {
   4362       relocation -= (input_section->output_section->vma
   4363 		     + input_section->output_offset);
   4364       if (howto->pcrel_offset)
   4365 	relocation -= address;
   4366     }
   4367 
   4368   return nds32_relocate_contents (howto, input_bfd, relocation,
   4369 				  contents + address);
   4370 }
   4371 
   4372 static bfd_boolean
   4373 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
   4374 			      const char *name,
   4375 			      Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
   4376 			      asection *input_sec,
   4377 			      struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
   4378 {
   4379   const char *source;
   4380   FILE *sym_ld_script = NULL;
   4381   struct elf_nds32_link_hash_table *table;
   4382 
   4383   table = nds32_elf_hash_table (info);
   4384   sym_ld_script = table->sym_ld_script;
   4385   if (!sym_ld_script)
   4386     return TRUE;
   4387 
   4388   if (!h || !name || *name == '\0')
   4389     return TRUE;
   4390 
   4391   if (input_sec->flags & SEC_EXCLUDE)
   4392     return TRUE;
   4393 
   4394   if (!check_start_export_sym)
   4395     {
   4396       fprintf (sym_ld_script, "SECTIONS\n{\n");
   4397       check_start_export_sym = 1;
   4398     }
   4399 
   4400   if (h->root.type == bfd_link_hash_defined
   4401       || h->root.type == bfd_link_hash_defweak)
   4402     {
   4403       if (!h->root.u.def.section->output_section)
   4404 	return TRUE;
   4405 
   4406       if (bfd_is_const_section (input_sec))
   4407 	source = input_sec->name;
   4408       else
   4409 	source = input_sec->owner->filename;
   4410 
   4411       fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
   4412 	       h->root.root.string,
   4413 	       (long) (h->root.u.def.value
   4414 		+ h->root.u.def.section->output_section->vma
   4415 		+ h->root.u.def.section->output_offset), source);
   4416     }
   4417 
   4418   return TRUE;
   4419 }
   4420 
   4421 /* Relocate an NDS32/D ELF section.
   4422    There is some attempt to make this function usable for many architectures,
   4423    both for RELA and REL type relocs, if only to serve as a learning tool.
   4424 
   4425    The RELOCATE_SECTION function is called by the new ELF backend linker
   4426    to handle the relocations for a section.
   4427 
   4428    The relocs are always passed as Rela structures; if the section
   4429    actually uses Rel structures, the r_addend field will always be
   4430    zero.
   4431 
   4432    This function is responsible for adjust the section contents as
   4433    necessary, and (if using Rela relocs and generating a
   4434    relocatable output file) adjusting the reloc addend as
   4435    necessary.
   4436 
   4437    This function does not have to worry about setting the reloc
   4438    address or the reloc symbol index.
   4439 
   4440    LOCAL_SYMS is a pointer to the swapped in local symbols.
   4441 
   4442    LOCAL_SECTIONS is an array giving the section in the input file
   4443    corresponding to the st_shndx field of each local symbol.
   4444 
   4445    The global hash table entry for the global symbols can be found
   4446    via elf_sym_hashes (input_bfd).
   4447 
   4448    When generating relocatable output, this function must handle
   4449    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
   4450    going to be the section symbol corresponding to the output
   4451    section, which means that the addend must be adjusted
   4452    accordingly.  */
   4453 
   4454 static bfd_vma
   4455 dtpoff_base (struct bfd_link_info *info)
   4456 {
   4457   /* If tls_sec is NULL, we should have signalled an error already.  */
   4458   if (elf_hash_table (info)->tls_sec == NULL)
   4459     return 0;
   4460   return elf_hash_table (info)->tls_sec->vma;
   4461 }
   4462 
   4463 static bfd_boolean
   4464 nds32_elf_relocate_section (bfd *                  output_bfd ATTRIBUTE_UNUSED,
   4465 			    struct bfd_link_info * info,
   4466 			    bfd *                  input_bfd,
   4467 			    asection *             input_section,
   4468 			    bfd_byte *             contents,
   4469 			    Elf_Internal_Rela *    relocs,
   4470 			    Elf_Internal_Sym *     local_syms,
   4471 			    asection **            local_sections)
   4472 {
   4473   Elf_Internal_Shdr *symtab_hdr;
   4474   struct elf_link_hash_entry **sym_hashes;
   4475   Elf_Internal_Rela *rel, *relend;
   4476   bfd_boolean ret = TRUE;		/* Assume success.  */
   4477   int align = 0;
   4478   bfd_reloc_status_type r;
   4479   const char *errmsg = NULL;
   4480   bfd_vma gp;
   4481   struct elf_nds32_link_hash_table *htab;
   4482   bfd *dynobj;
   4483   bfd_vma *local_got_offsets;
   4484   asection *sgot, *splt, *sreloc;
   4485   bfd_vma high_address;
   4486   struct elf_nds32_link_hash_table *table;
   4487   int eliminate_gc_relocs;
   4488   bfd_vma fpbase_addr;
   4489 
   4490   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
   4491   sym_hashes = elf_sym_hashes (input_bfd);
   4492   htab = nds32_elf_hash_table (info);
   4493   high_address = bfd_get_section_limit (input_bfd, input_section);
   4494 
   4495   dynobj = htab->root.dynobj;
   4496   local_got_offsets = elf_local_got_offsets (input_bfd);
   4497 
   4498   sgot = htab->sgot;
   4499   splt = htab->splt;
   4500   sreloc = NULL;
   4501 
   4502   rel = relocs;
   4503   relend = relocs + input_section->reloc_count;
   4504 
   4505   table = nds32_elf_hash_table (info);
   4506   eliminate_gc_relocs = table->eliminate_gc_relocs;
   4507   /* By this time, we can adjust the value of _SDA_BASE_.  */
   4508   if ((!info->relocatable))
   4509     {
   4510       is_SDA_BASE_set = 1;
   4511       r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
   4512       if (r != bfd_reloc_ok)
   4513 	return FALSE;
   4514     }
   4515 
   4516   if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
   4517     if (!nds32_elf_ifc_reloc ())
   4518       (*_bfd_error_handler) (_("error: IFC relocation error."));
   4519 
   4520  /* Relocation for .ex9.itable.  */
   4521   if (table->target_optimize & NDS32_RELAX_EX9_ON
   4522       || (table->ex9_import_file && table->update_ex9_table))
   4523     nds32_elf_ex9_reloc_jmp (info);
   4524 
   4525   /* Use gp as fp to prevent truncated fit.  Because in relaxation time
   4526      the fp value is set as gp, and it has be reverted for instruction
   4527      setting fp.  */
   4528   fpbase_addr = elf_gp (output_bfd);
   4529 
   4530   for (rel = relocs; rel < relend; rel++)
   4531     {
   4532       enum elf_nds32_reloc_type r_type;
   4533       reloc_howto_type *howto = NULL;
   4534       unsigned long r_symndx;
   4535       struct elf_link_hash_entry *h = NULL;
   4536       Elf_Internal_Sym *sym = NULL;
   4537       asection *sec;
   4538       bfd_vma relocation;
   4539 
   4540       /* We can't modify r_addend here as elf_link_input_bfd has an assert to
   4541 	 ensure it's zero (we use REL relocs, not RELA).  Therefore this
   4542 	 should be assigning zero to `addend', but for clarity we use
   4543 	 `r_addend'.  */
   4544 
   4545       bfd_vma addend = rel->r_addend;
   4546       bfd_vma offset = rel->r_offset;
   4547 
   4548       r_type = ELF32_R_TYPE (rel->r_info);
   4549       if (r_type >= R_NDS32_max)
   4550 	{
   4551 	  (*_bfd_error_handler) (_("%B: error: unknown relocation type %d."),
   4552 				 input_bfd, r_type);
   4553 	  bfd_set_error (bfd_error_bad_value);
   4554 	  ret = FALSE;
   4555 	  continue;
   4556 	}
   4557 
   4558       if (r_type == R_NDS32_GNU_VTENTRY
   4559 	  || r_type == R_NDS32_GNU_VTINHERIT
   4560 	  || r_type == R_NDS32_NONE
   4561 	  || r_type == R_NDS32_RELA_GNU_VTENTRY
   4562 	  || r_type == R_NDS32_RELA_GNU_VTINHERIT
   4563 	  || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
   4564 	  || r_type == R_NDS32_DATA
   4565 	  || r_type == R_NDS32_TRAN
   4566 	  || (r_type >= R_NDS32_LONGCALL4 && r_type <= R_NDS32_LONGJUMP6))
   4567 	continue;
   4568 
   4569       /* If we enter the fp-as-gp region.  Resolve the address of best fp-base.  */
   4570       if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
   4571 	  && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
   4572 	{
   4573 	  int dist;
   4574 
   4575 	  /* Distance to relocation of best fp-base is encoded in R_SYM.  */
   4576 	  dist =  rel->r_addend >> 16;
   4577 	  fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
   4578 						  local_syms, symtab_hdr);
   4579 	}
   4580       else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
   4581 	       && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
   4582 	{
   4583 	  fpbase_addr = elf_gp (output_bfd);
   4584 	}
   4585 
   4586       if (((r_type >= R_NDS32_DWARF2_OP1_RELA
   4587 	    && r_type <= R_NDS32_DWARF2_LEB_RELA)
   4588 	   || r_type >= R_NDS32_RELAX_ENTRY) && !info->relocatable)
   4589 	continue;
   4590 
   4591       howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
   4592       r_symndx = ELF32_R_SYM (rel->r_info);
   4593 
   4594       /* This is a final link.  */
   4595       sym = NULL;
   4596       sec = NULL;
   4597       h = NULL;
   4598 
   4599       if (r_symndx < symtab_hdr->sh_info)
   4600 	{
   4601 	  /* Local symbol.  */
   4602 	  sym = local_syms + r_symndx;
   4603 	  sec = local_sections[r_symndx];
   4604 
   4605 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
   4606 	  addend = rel->r_addend;
   4607 	}
   4608       else
   4609 	{
   4610 	  /* External symbol.  */
   4611 	  bfd_boolean warned, ignored, unresolved_reloc;
   4612 	  int symndx = r_symndx - symtab_hdr->sh_info;
   4613 
   4614 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
   4615 				   r_symndx, symtab_hdr, sym_hashes, h, sec,
   4616 				   relocation, unresolved_reloc, warned,
   4617 				   ignored);
   4618 
   4619 	  /* la $fp, _FP_BASE_ is per-function (region).
   4620 	     Handle it specially.  */
   4621 	  switch ((int) r_type)
   4622 	    {
   4623 	    case R_NDS32_SDA19S0_RELA:
   4624 	    case R_NDS32_SDA15S0_RELA:
   4625 	    case R_NDS32_20_RELA:
   4626 	      if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
   4627 			  FP_BASE_NAME) == 0)
   4628 		{
   4629 		  relocation = fpbase_addr;
   4630 		  break;
   4631 		}
   4632 	    }
   4633 
   4634 	}
   4635 
   4636       if (info->relocatable)
   4637 	{
   4638 	  /* This is a relocatable link.  We don't have to change
   4639 	     anything, unless the reloc is against a section symbol,
   4640 	     in which case we have to adjust according to where the
   4641 	     section symbol winds up in the output section.  */
   4642 	  if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   4643 	    rel->r_addend += sec->output_offset + sym->st_value;
   4644 
   4645 	  continue;
   4646 	}
   4647 
   4648       /* Sanity check the address.  */
   4649       if (offset > high_address)
   4650 	{
   4651 	  r = bfd_reloc_outofrange;
   4652 	  goto check_reloc;
   4653 	}
   4654 
   4655       if ((r_type >= R_NDS32_DWARF2_OP1_RELA
   4656 	   && r_type <= R_NDS32_DWARF2_LEB_RELA)
   4657 	  || r_type >= R_NDS32_RELAX_ENTRY)
   4658 	continue;
   4659 
   4660       switch ((int) r_type)
   4661 	{
   4662 	case R_NDS32_GOTOFF:
   4663 	  /* Relocation is relative to the start of the global offset
   4664 	     table (for ld24 rx, #uimm24), e.g. access at label+addend
   4665 
   4666 	     ld24 rx. #label@GOTOFF + addend
   4667 	     sub  rx, r12.  */
   4668 	case R_NDS32_GOTOFF_HI20:
   4669 	case R_NDS32_GOTOFF_LO12:
   4670 	case R_NDS32_GOTOFF_LO15:
   4671 	case R_NDS32_GOTOFF_LO19:
   4672 	  BFD_ASSERT (sgot != NULL);
   4673 
   4674 	  relocation -= elf_gp (output_bfd);
   4675 	  break;
   4676 
   4677 	case R_NDS32_9_PLTREL:
   4678 	case R_NDS32_25_PLTREL:
   4679 	  /* Relocation is to the entry for this symbol in the
   4680 	     procedure linkage table.  */
   4681 
   4682 	  /* The native assembler will generate a 25_PLTREL reloc
   4683 	     for a local symbol if you assemble a call from one
   4684 	     section to another when using -K pic.  */
   4685 	  if (h == NULL)
   4686 	    break;
   4687 
   4688 	  if (h->forced_local)
   4689 	    break;
   4690 
   4691 	  /* We didn't make a PLT entry for this symbol.  This
   4692 	     happens when statically linking PIC code, or when
   4693 	     using -Bsymbolic.  */
   4694 	  if (h->plt.offset == (bfd_vma) - 1)
   4695 	    break;
   4696 
   4697 	  relocation = (splt->output_section->vma
   4698 			+ splt->output_offset + h->plt.offset);
   4699 	  break;
   4700 
   4701 	case R_NDS32_PLT_GOTREL_HI20:
   4702 	case R_NDS32_PLT_GOTREL_LO12:
   4703 	case R_NDS32_PLT_GOTREL_LO15:
   4704 	case R_NDS32_PLT_GOTREL_LO19:
   4705 	case R_NDS32_PLT_GOTREL_LO20:
   4706 	  if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
   4707 	    {
   4708 	      /* We didn't make a PLT entry for this symbol.  This
   4709 		 happens when statically linking PIC code, or when
   4710 		 using -Bsymbolic.  */
   4711 	      relocation -= elf_gp (output_bfd);
   4712 	      break;
   4713 	    }
   4714 
   4715 	  relocation = (splt->output_section->vma
   4716 			+ splt->output_offset + h->plt.offset);
   4717 
   4718 	  relocation -= elf_gp (output_bfd);
   4719 	  break;
   4720 
   4721 	case R_NDS32_PLTREL_HI20:
   4722 	case R_NDS32_PLTREL_LO12:
   4723 
   4724 	  /* Relocation is to the entry for this symbol in the
   4725 	     procedure linkage table.  */
   4726 
   4727 	  /* The native assembler will generate a 25_PLTREL reloc
   4728 	     for a local symbol if you assemble a call from one
   4729 	     section to another when using -K pic.  */
   4730 	  if (h == NULL)
   4731 	    break;
   4732 
   4733 	  if (h->forced_local)
   4734 	    break;
   4735 
   4736 	  if (h->plt.offset == (bfd_vma) - 1)
   4737 	    /* We didn't make a PLT entry for this symbol.  This
   4738 	       happens when statically linking PIC code, or when
   4739 	       using -Bsymbolic.  */
   4740 	    break;
   4741 
   4742 	  if (splt == NULL)
   4743 	    break;
   4744 
   4745 	  relocation = (splt->output_section->vma
   4746 			+ splt->output_offset
   4747 			+ h->plt.offset + 4)
   4748 		       - (input_section->output_section->vma
   4749 			  + input_section->output_offset
   4750 			  + rel->r_offset);
   4751 
   4752 	  break;
   4753 
   4754 	case R_NDS32_GOTPC20:
   4755 	  /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
   4756 	     ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
   4757 	  relocation = elf_gp (output_bfd);
   4758 	  break;
   4759 
   4760 	case R_NDS32_GOTPC_HI20:
   4761 	case R_NDS32_GOTPC_LO12:
   4762 	    {
   4763 	      /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
   4764 		 bl .+4
   4765 		 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
   4766 		 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
   4767 		 or
   4768 		 bl .+4
   4769 		 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
   4770 		 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
   4771 	       */
   4772 	      relocation = elf_gp (output_bfd);
   4773 	      relocation -= (input_section->output_section->vma
   4774 			     + input_section->output_offset + rel->r_offset);
   4775 	      break;
   4776 	    }
   4777 
   4778 	case R_NDS32_GOT20:
   4779 	  /* Fall through.  */
   4780 	case R_NDS32_GOT_HI20:
   4781 	case R_NDS32_GOT_LO12:
   4782 	case R_NDS32_GOT_LO15:
   4783 	case R_NDS32_GOT_LO19:
   4784 	  /* Relocation is to the entry for this symbol in the global
   4785 	     offset table.  */
   4786 	  BFD_ASSERT (sgot != NULL);
   4787 
   4788 	  if (h != NULL)
   4789 	    {
   4790 	      bfd_boolean dyn;
   4791 	      bfd_vma off;
   4792 
   4793 	      off = h->got.offset;
   4794 	      BFD_ASSERT (off != (bfd_vma) - 1);
   4795 	      dyn = htab->root.dynamic_sections_created;
   4796 	      if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
   4797 		  || (info->shared
   4798 		      && (info->symbolic
   4799 			  || h->dynindx == -1
   4800 			  || h->forced_local) && h->def_regular))
   4801 		{
   4802 		  /* This is actually a static link, or it is a
   4803 		     -Bsymbolic link and the symbol is defined
   4804 		     locally, or the symbol was forced to be local
   4805 		     because of a version file.  We must initialize
   4806 		     this entry in the global offset table.  Since the
   4807 		     offset must always be a multiple of 4, we use the
   4808 		     least significant bit to record whether we have
   4809 		     initialized it already.
   4810 
   4811 		     When doing a dynamic link, we create a .rela.got
   4812 		     relocation entry to initialize the value.  This
   4813 		     is done in the finish_dynamic_symbol routine.  */
   4814 		  if ((off & 1) != 0)
   4815 		    off &= ~1;
   4816 		  else
   4817 		    {
   4818 		      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
   4819 		      h->got.offset |= 1;
   4820 		    }
   4821 		}
   4822 	      relocation = sgot->output_section->vma + sgot->output_offset + off
   4823 			   - elf_gp (output_bfd);
   4824 	    }
   4825 	  else
   4826 	    {
   4827 	      bfd_vma off;
   4828 	      bfd_byte *loc;
   4829 
   4830 	      BFD_ASSERT (local_got_offsets != NULL
   4831 			  && local_got_offsets[r_symndx] != (bfd_vma) - 1);
   4832 
   4833 	      off = local_got_offsets[r_symndx];
   4834 
   4835 	      /* The offset must always be a multiple of 4.  We use
   4836 		 the least significant bit to record whether we have
   4837 		 already processed this entry.  */
   4838 	      if ((off & 1) != 0)
   4839 		off &= ~1;
   4840 	      else
   4841 		{
   4842 		  bfd_put_32 (output_bfd, relocation, sgot->contents + off);
   4843 
   4844 		  if (info->shared)
   4845 		    {
   4846 		      asection *srelgot;
   4847 		      Elf_Internal_Rela outrel;
   4848 
   4849 		      /* We need to generate a R_NDS32_RELATIVE reloc
   4850 			 for the dynamic linker.  */
   4851 		      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
   4852 		      BFD_ASSERT (srelgot != NULL);
   4853 
   4854 		      outrel.r_offset = (elf_gp (output_bfd)
   4855 					 + sgot->output_offset + off);
   4856 		      outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
   4857 		      outrel.r_addend = relocation;
   4858 		      loc = srelgot->contents;
   4859 		      loc +=
   4860 			srelgot->reloc_count * sizeof (Elf32_External_Rela);
   4861 		      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   4862 		      ++srelgot->reloc_count;
   4863 		    }
   4864 		  local_got_offsets[r_symndx] |= 1;
   4865 		}
   4866 	      relocation = sgot->output_section->vma + sgot->output_offset + off
   4867 			   - elf_gp (output_bfd);
   4868 	    }
   4869 
   4870 	  break;
   4871 
   4872 	case R_NDS32_16_RELA:
   4873 	case R_NDS32_20_RELA:
   4874 	case R_NDS32_5_RELA:
   4875 	case R_NDS32_32_RELA:
   4876 	case R_NDS32_9_PCREL_RELA:
   4877 	case R_NDS32_WORD_9_PCREL_RELA:
   4878 	case R_NDS32_10_UPCREL_RELA:
   4879 	case R_NDS32_15_PCREL_RELA:
   4880 	case R_NDS32_17_PCREL_RELA:
   4881 	case R_NDS32_25_PCREL_RELA:
   4882 	case R_NDS32_HI20_RELA:
   4883 	case R_NDS32_LO12S3_RELA:
   4884 	case R_NDS32_LO12S2_RELA:
   4885 	case R_NDS32_LO12S2_DP_RELA:
   4886 	case R_NDS32_LO12S2_SP_RELA:
   4887 	case R_NDS32_LO12S1_RELA:
   4888 	case R_NDS32_LO12S0_RELA:
   4889 	case R_NDS32_LO12S0_ORI_RELA:
   4890 	  if (info->shared && r_symndx != 0
   4891 	      && (input_section->flags & SEC_ALLOC) != 0
   4892 	      && (eliminate_gc_relocs == 0
   4893 		  || (sec && (sec->flags & SEC_EXCLUDE) == 0))
   4894 	      && ((r_type != R_NDS32_9_PCREL_RELA
   4895 		   && r_type != R_NDS32_WORD_9_PCREL_RELA
   4896 		   && r_type != R_NDS32_10_UPCREL_RELA
   4897 		   && r_type != R_NDS32_15_PCREL_RELA
   4898 		   && r_type != R_NDS32_17_PCREL_RELA
   4899 		   && r_type != R_NDS32_25_PCREL_RELA
   4900 		   && !(r_type == R_NDS32_32_RELA
   4901 			&& strcmp (input_section->name, ".eh_frame") == 0))
   4902 		  || (h != NULL && h->dynindx != -1
   4903 		      && (!info->symbolic || !h->def_regular))))
   4904 	    {
   4905 	      Elf_Internal_Rela outrel;
   4906 	      bfd_boolean skip, relocate;
   4907 	      bfd_byte *loc;
   4908 
   4909 	      /* When generating a shared object, these relocations
   4910 		 are copied into the output file to be resolved at run
   4911 		 time.  */
   4912 
   4913 	      if (sreloc == NULL)
   4914 		{
   4915 		  const char *name;
   4916 
   4917 		  name = bfd_elf_string_from_elf_section
   4918 		    (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
   4919 		     elf_section_data (input_section)->rela.hdr->sh_name);
   4920 		  if (name == NULL)
   4921 		    return FALSE;
   4922 
   4923 		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
   4924 			      && strcmp (bfd_get_section_name (input_bfd,
   4925 							       input_section),
   4926 					 name + 5) == 0);
   4927 
   4928 		  sreloc = bfd_get_section_by_name (dynobj, name);
   4929 		  BFD_ASSERT (sreloc != NULL);
   4930 		}
   4931 
   4932 	      skip = FALSE;
   4933 	      relocate = FALSE;
   4934 
   4935 	      outrel.r_offset = _bfd_elf_section_offset (output_bfd,
   4936 							 info,
   4937 							 input_section,
   4938 							 rel->r_offset);
   4939 	      if (outrel.r_offset == (bfd_vma) - 1)
   4940 		skip = TRUE;
   4941 	      else if (outrel.r_offset == (bfd_vma) - 2)
   4942 		skip = TRUE, relocate = TRUE;
   4943 	      outrel.r_offset += (input_section->output_section->vma
   4944 				  + input_section->output_offset);
   4945 
   4946 	      if (skip)
   4947 		memset (&outrel, 0, sizeof outrel);
   4948 	      else if (r_type == R_NDS32_17_PCREL_RELA
   4949 		       || r_type == R_NDS32_15_PCREL_RELA
   4950 		       || r_type == R_NDS32_25_PCREL_RELA)
   4951 		{
   4952 		  BFD_ASSERT (h != NULL && h->dynindx != -1);
   4953 		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
   4954 		  outrel.r_addend = rel->r_addend;
   4955 		}
   4956 	      else
   4957 		{
   4958 		  /* h->dynindx may be -1 if this symbol was marked to
   4959 		     become local.  */
   4960 		  if (h == NULL
   4961 		      || ((info->symbolic || h->dynindx == -1)
   4962 			  && h->def_regular))
   4963 		    {
   4964 		      relocate = TRUE;
   4965 		      outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
   4966 		      outrel.r_addend = relocation + rel->r_addend;
   4967 		    }
   4968 		  else
   4969 		    {
   4970 		      BFD_ASSERT (h->dynindx != -1);
   4971 		      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
   4972 		      outrel.r_addend = rel->r_addend;
   4973 		    }
   4974 		}
   4975 
   4976 	      loc = sreloc->contents;
   4977 	      loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
   4978 	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   4979 	      ++sreloc->reloc_count;
   4980 
   4981 	      /* If this reloc is against an external symbol, we do
   4982 		 not want to fiddle with the addend.  Otherwise, we
   4983 		 need to include the symbol value so that it becomes
   4984 		 an addend for the dynamic reloc.  */
   4985 	      if (!relocate)
   4986 		continue;
   4987 	    }
   4988 	  break;
   4989 
   4990 	case R_NDS32_25_ABS_RELA:
   4991 	  if (info->shared)
   4992 	    {
   4993 	      (*_bfd_error_handler)
   4994 		(_("%s: warning: cannot deal R_NDS32_25_ABS_RELA in shared "
   4995 		   "mode."), bfd_get_filename (input_bfd));
   4996 	      return FALSE;
   4997 	    }
   4998 	  break;
   4999 
   5000 	case R_NDS32_9_PCREL:
   5001 	  r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
   5002 					  contents, offset,
   5003 					  sec, relocation, addend);
   5004 	  goto check_reloc;
   5005 
   5006 	case R_NDS32_HI20:
   5007 	    {
   5008 	      Elf_Internal_Rela *lorel;
   5009 
   5010 	      /* We allow an arbitrary number of HI20 relocs before the
   5011 		 LO12 reloc.  This permits gcc to emit the HI and LO relocs
   5012 		 itself.  */
   5013 	      for (lorel = rel + 1;
   5014 		   (lorel < relend
   5015 		    && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
   5016 		continue;
   5017 	      if (lorel < relend
   5018 		  && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
   5019 		      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
   5020 		      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
   5021 		      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
   5022 		{
   5023 		  nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
   5024 					   contents, relocation + addend);
   5025 		  r = bfd_reloc_ok;
   5026 		}
   5027 	      else
   5028 		r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   5029 					      contents, offset, relocation,
   5030 					      addend);
   5031 	    }
   5032 
   5033 	  goto check_reloc;
   5034 
   5035 	case R_NDS32_GOT17S2_RELA:
   5036 	case R_NDS32_GOT15S2_RELA:
   5037 	    {
   5038 	      bfd_vma off;
   5039 
   5040 	      BFD_ASSERT (sgot != NULL);
   5041 
   5042 	      if (h != NULL)
   5043 		{
   5044 		  bfd_boolean dyn;
   5045 
   5046 		  off = h->got.offset;
   5047 		  BFD_ASSERT (off != (bfd_vma) - 1);
   5048 
   5049 		  dyn = htab->root.dynamic_sections_created;
   5050 		  if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
   5051 		      (dyn, info->shared, h) || (info->shared
   5052 						 && (info->symbolic
   5053 						     || h->dynindx == -1
   5054 						     || h->forced_local)
   5055 						 && h->def_regular))
   5056 		    {
   5057 		      /* This is actually a static link, or it is a
   5058 			 -Bsymbolic link and the symbol is defined
   5059 			 locally, or the symbol was forced to be local
   5060 			 because of a version file.  We must initialize
   5061 			 this entry in the global offset table.  Since the
   5062 			 offset must always be a multiple of 4, we use the
   5063 			 least significant bit to record whether we have
   5064 			 initialized it already.
   5065 
   5066 			 When doing a dynamic link, we create a .rela.got
   5067 			 relocation entry to initialize the value.  This
   5068 			 is done in the finish_dynamic_symbol routine.  */
   5069 		      if ((off & 1) != 0)
   5070 			off &= ~1;
   5071 		      else
   5072 			{
   5073 			  bfd_put_32 (output_bfd, relocation,
   5074 				      sgot->contents + off);
   5075 			  h->got.offset |= 1;
   5076 			}
   5077 		    }
   5078 		}
   5079 	      else
   5080 		{
   5081 		  bfd_byte *loc;
   5082 
   5083 		  BFD_ASSERT (local_got_offsets != NULL
   5084 			      && local_got_offsets[r_symndx] != (bfd_vma) - 1);
   5085 
   5086 		  off = local_got_offsets[r_symndx];
   5087 
   5088 		  /* The offset must always be a multiple of 4.  We use
   5089 		     the least significant bit to record whether we have
   5090 		     already processed this entry.  */
   5091 		  if ((off & 1) != 0)
   5092 		    off &= ~1;
   5093 		  else
   5094 		    {
   5095 		      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
   5096 
   5097 		      if (info->shared)
   5098 			{
   5099 			  asection *srelgot;
   5100 			  Elf_Internal_Rela outrel;
   5101 
   5102 			  /* We need to generate a R_NDS32_RELATIVE reloc
   5103 			     for the dynamic linker.  */
   5104 			  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
   5105 			  BFD_ASSERT (srelgot != NULL);
   5106 
   5107 			  outrel.r_offset = (elf_gp (output_bfd)
   5108 					     + sgot->output_offset + off);
   5109 			  outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
   5110 			  outrel.r_addend = relocation;
   5111 			  loc = srelgot->contents;
   5112 			  loc +=
   5113 			    srelgot->reloc_count * sizeof (Elf32_External_Rela);
   5114 			  bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   5115 			  ++srelgot->reloc_count;
   5116 			}
   5117 		      local_got_offsets[r_symndx] |= 1;
   5118 		    }
   5119 		}
   5120 	      relocation = sgot->output_section->vma + sgot->output_offset + off
   5121 			   - elf_gp (output_bfd);
   5122 	    }
   5123 	  if (relocation & align)
   5124 	    {
   5125 	      /* Incorrect alignment.  */
   5126 	      (*_bfd_error_handler)
   5127 		(_("%B: warning: unaligned access to GOT entry."), input_bfd);
   5128 	      ret = FALSE;
   5129 	      r = bfd_reloc_dangerous;
   5130 	      goto check_reloc;
   5131 	    }
   5132 	  break;
   5133 
   5134 	case R_NDS32_SDA16S3_RELA:
   5135 	case R_NDS32_SDA15S3_RELA:
   5136 	case R_NDS32_SDA15S3:
   5137 	  align = 0x7;
   5138 	  goto handle_sda;
   5139 
   5140 	case R_NDS32_SDA17S2_RELA:
   5141 	case R_NDS32_SDA15S2_RELA:
   5142 	case R_NDS32_SDA12S2_SP_RELA:
   5143 	case R_NDS32_SDA12S2_DP_RELA:
   5144 	case R_NDS32_SDA15S2:
   5145 	case R_NDS32_SDA_FP7U2_RELA:
   5146 	  align = 0x3;
   5147 	  goto handle_sda;
   5148 
   5149 	case R_NDS32_SDA18S1_RELA:
   5150 	case R_NDS32_SDA15S1_RELA:
   5151 	case R_NDS32_SDA15S1:
   5152 	  align = 0x1;
   5153 	  goto handle_sda;
   5154 
   5155 	case R_NDS32_SDA19S0_RELA:
   5156 	case R_NDS32_SDA15S0_RELA:
   5157 	case R_NDS32_SDA15S0:
   5158 	    {
   5159 	      align = 0x0;
   5160 handle_sda:
   5161 	      BFD_ASSERT (sec != NULL);
   5162 
   5163 	      /* If the symbol is in the abs section, the out_bfd will be null.
   5164 		 This happens when the relocation has a symbol@GOTOFF.  */
   5165 	      r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
   5166 	      if (r != bfd_reloc_ok)
   5167 		{
   5168 		  (*_bfd_error_handler)
   5169 		    (_("%B: warning: relocate SDA_BASE failed."), input_bfd);
   5170 		  ret = FALSE;
   5171 		  goto check_reloc;
   5172 		}
   5173 
   5174 	      /* At this point `relocation' contains the object's
   5175 		 address.  */
   5176 	      if (r_type == R_NDS32_SDA_FP7U2_RELA)
   5177 		{
   5178 		  relocation -= fpbase_addr;
   5179 		}
   5180 	      else
   5181 		relocation -= gp;
   5182 	      /* Now it contains the offset from _SDA_BASE_.  */
   5183 
   5184 	      /* Make sure alignment is correct.  */
   5185 
   5186 	      if (relocation & align)
   5187 		{
   5188 		  /* Incorrect alignment.  */
   5189 		  (*_bfd_error_handler)
   5190 		    (_("%B(%A): warning: unaligned small data access of type %d."),
   5191 		     input_bfd, input_section, r_type);
   5192 		  ret = FALSE;
   5193 		  goto check_reloc;
   5194 		}
   5195 	    }
   5196 
   5197 	  break;
   5198 	case R_NDS32_17IFC_PCREL_RELA:
   5199 	case R_NDS32_10IFCU_PCREL_RELA:
   5200 	  /* do nothing */
   5201 	  break;
   5202 
   5203 	case R_NDS32_TLS_LE_HI20:
   5204 	case R_NDS32_TLS_LE_LO12:
   5205 	case R_NDS32_TLS_LE_20:
   5206 	case R_NDS32_TLS_LE_15S0:
   5207 	case R_NDS32_TLS_LE_15S1:
   5208 	case R_NDS32_TLS_LE_15S2:
   5209 	  if (elf_hash_table (info)->tls_sec != NULL)
   5210 	    relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
   5211 	  break;
   5212 	case R_NDS32_TLS_IE_HI20:
   5213 	case R_NDS32_TLS_IE_LO12S2:
   5214 	  {
   5215 	    /* Relocation is to the entry for this symbol in the global
   5216 	       offset table.  */
   5217 	    unsigned int tls_type;
   5218 	    asection *srelgot;
   5219 	    Elf_Internal_Rela outrel;
   5220 	    bfd_vma off;
   5221 	    bfd_byte *loc;
   5222 	    int indx = 0;
   5223 
   5224 	    BFD_ASSERT (sgot != NULL);
   5225 	    if (h != NULL)
   5226 	      {
   5227 		bfd_boolean dyn;
   5228 
   5229 		off = h->got.offset;
   5230 		BFD_ASSERT (off != (bfd_vma) - 1);
   5231 		dyn = htab->root.dynamic_sections_created;
   5232 		tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
   5233 		if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
   5234 		    && (!info->shared
   5235 			|| !SYMBOL_REFERENCES_LOCAL (info, h)))
   5236 		  indx = h->dynindx;
   5237 	      }
   5238 	    else
   5239 	      {
   5240 		/* Never happen currently.  */
   5241 		BFD_ASSERT (local_got_offsets != NULL
   5242 			    && local_got_offsets[r_symndx] != (bfd_vma) - 1);
   5243 
   5244 		off = local_got_offsets[r_symndx];
   5245 
   5246 		tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
   5247 	      }
   5248 	    relocation = sgot->output_section->vma + sgot->output_offset + off;
   5249 
   5250 	    if (r_type == R_NDS32_TLS_IE_LO12S2)
   5251 	      break;
   5252 
   5253 	    /* The offset must always be a multiple of 4.  We use
   5254 	       the least significant bit to record whether we have
   5255 	       already processed this entry.  */
   5256 	    if ((off & 1) != 0)
   5257 	      off &= ~1;
   5258 	    else
   5259 	      {
   5260 		bfd_boolean need_relocs = FALSE;
   5261 		srelgot = htab->srelgot;
   5262 		if ((info->shared || indx != 0)
   5263 		    && (h == NULL
   5264 			|| ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
   5265 			|| h->root.type != bfd_link_hash_undefweak))
   5266 		  {
   5267 		    need_relocs = TRUE;
   5268 		    BFD_ASSERT (srelgot != NULL);
   5269 		  }
   5270 		if (tls_type & GOT_TLS_IE)
   5271 		  {
   5272 		    if (need_relocs)
   5273 		      {
   5274 			if (h->dynindx == 0)
   5275 			  outrel.r_addend = relocation - dtpoff_base (info);
   5276 			else
   5277 			  outrel.r_addend = 0;
   5278 			outrel.r_offset = (sgot->output_section->vma
   5279 					   + sgot->output_offset
   5280 					   + off);
   5281 			outrel.r_info =
   5282 			  ELF32_R_INFO (h->dynindx, R_NDS32_TLS_TPOFF);
   5283 
   5284 			loc = srelgot->contents;
   5285 			loc +=
   5286 			  srelgot->reloc_count * sizeof (Elf32_External_Rela);
   5287 			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
   5288 			++srelgot->reloc_count;
   5289 		      }
   5290 		    else
   5291 		      bfd_put_32 (output_bfd, h->root.u.def.value - TP_OFFSET,
   5292 				  sgot->contents + off);
   5293 		  }
   5294 	      }
   5295 	  }
   5296 	break;
   5297 
   5298 	  /* DON'T   fall through.  */
   5299 
   5300 	default:
   5301 	  /* OLD_NDS32_RELOC.  */
   5302 
   5303 	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   5304 					contents, offset, relocation, addend);
   5305 	  goto check_reloc;
   5306 	}
   5307 
   5308       switch ((int) r_type)
   5309 	{
   5310 	case R_NDS32_20_RELA:
   5311 	case R_NDS32_5_RELA:
   5312 	case R_NDS32_9_PCREL_RELA:
   5313 	case R_NDS32_WORD_9_PCREL_RELA:
   5314 	case R_NDS32_10_UPCREL_RELA:
   5315 	case R_NDS32_15_PCREL_RELA:
   5316 	case R_NDS32_17_PCREL_RELA:
   5317 	case R_NDS32_25_PCREL_RELA:
   5318 	case R_NDS32_25_ABS_RELA:
   5319 	case R_NDS32_HI20_RELA:
   5320 	case R_NDS32_LO12S3_RELA:
   5321 	case R_NDS32_LO12S2_RELA:
   5322 	case R_NDS32_LO12S2_DP_RELA:
   5323 	case R_NDS32_LO12S2_SP_RELA:
   5324 	case R_NDS32_LO12S1_RELA:
   5325 	case R_NDS32_LO12S0_RELA:
   5326 	case R_NDS32_LO12S0_ORI_RELA:
   5327 	case R_NDS32_SDA16S3_RELA:
   5328 	case R_NDS32_SDA17S2_RELA:
   5329 	case R_NDS32_SDA18S1_RELA:
   5330 	case R_NDS32_SDA19S0_RELA:
   5331 	case R_NDS32_SDA15S3_RELA:
   5332 	case R_NDS32_SDA15S2_RELA:
   5333 	case R_NDS32_SDA12S2_DP_RELA:
   5334 	case R_NDS32_SDA12S2_SP_RELA:
   5335 	case R_NDS32_SDA15S1_RELA:
   5336 	case R_NDS32_SDA15S0_RELA:
   5337 	case R_NDS32_SDA_FP7U2_RELA:
   5338 	case R_NDS32_9_PLTREL:
   5339 	case R_NDS32_25_PLTREL:
   5340 	case R_NDS32_GOT20:
   5341 	case R_NDS32_GOT_HI20:
   5342 	case R_NDS32_GOT_LO12:
   5343 	case R_NDS32_GOT_LO15:
   5344 	case R_NDS32_GOT_LO19:
   5345 	case R_NDS32_GOT15S2_RELA:
   5346 	case R_NDS32_GOT17S2_RELA:
   5347 	case R_NDS32_GOTPC20:
   5348 	case R_NDS32_GOTPC_HI20:
   5349 	case R_NDS32_GOTPC_LO12:
   5350 	case R_NDS32_GOTOFF:
   5351 	case R_NDS32_GOTOFF_HI20:
   5352 	case R_NDS32_GOTOFF_LO12:
   5353 	case R_NDS32_GOTOFF_LO15:
   5354 	case R_NDS32_GOTOFF_LO19:
   5355 	case R_NDS32_PLTREL_HI20:
   5356 	case R_NDS32_PLTREL_LO12:
   5357 	case R_NDS32_PLT_GOTREL_HI20:
   5358 	case R_NDS32_PLT_GOTREL_LO12:
   5359 	case R_NDS32_PLT_GOTREL_LO15:
   5360 	case R_NDS32_PLT_GOTREL_LO19:
   5361 	case R_NDS32_PLT_GOTREL_LO20:
   5362 	case R_NDS32_17IFC_PCREL_RELA:
   5363 	case R_NDS32_10IFCU_PCREL_RELA:
   5364 	case R_NDS32_TLS_LE_HI20:
   5365 	case R_NDS32_TLS_LE_LO12:
   5366 	case R_NDS32_TLS_IE_HI20:
   5367 	case R_NDS32_TLS_IE_LO12S2:
   5368 	case R_NDS32_TLS_LE_20:
   5369 	case R_NDS32_TLS_LE_15S0:
   5370 	case R_NDS32_TLS_LE_15S1:
   5371 	case R_NDS32_TLS_LE_15S2:
   5372 	  /* Instruction related relocs must handle endian properly.  */
   5373 	  /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
   5374 	  r = nds32_elf_final_link_relocate (howto, input_bfd,
   5375 					     input_section, contents,
   5376 					     rel->r_offset, relocation,
   5377 					     rel->r_addend);
   5378 	  break;
   5379 
   5380 	default:
   5381 	  /* All other relocs can use default handler.  */
   5382 	  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
   5383 					contents, rel->r_offset,
   5384 					relocation, rel->r_addend);
   5385 	  break;
   5386 	}
   5387 
   5388 check_reloc:
   5389 
   5390       if (r != bfd_reloc_ok)
   5391 	{
   5392 	  /* FIXME: This should be generic enough to go in a utility.  */
   5393 	  const char *name;
   5394 
   5395 	  if (h != NULL)
   5396 	    name = h->root.root.string;
   5397 	  else
   5398 	    {
   5399 	      name = bfd_elf_string_from_elf_section
   5400 		      (input_bfd, symtab_hdr->sh_link, sym->st_name);
   5401 	      if (name == NULL || *name == '\0')
   5402 		name = bfd_section_name (input_bfd, sec);
   5403 	    }
   5404 
   5405 	  if (errmsg != NULL)
   5406 	    goto common_error;
   5407 
   5408 	  switch (r)
   5409 	    {
   5410 	    case bfd_reloc_overflow:
   5411 	      if (!((*info->callbacks->reloc_overflow)
   5412 		    (info, (h ? &h->root : NULL), name, howto->name,
   5413 		     (bfd_vma) 0, input_bfd, input_section, offset)))
   5414 		return FALSE;
   5415 	      break;
   5416 
   5417 	    case bfd_reloc_undefined:
   5418 	      if (!((*info->callbacks->undefined_symbol)
   5419 		    (info, name, input_bfd, input_section, offset, TRUE)))
   5420 		return FALSE;
   5421 	      break;
   5422 
   5423 	    case bfd_reloc_outofrange:
   5424 	      errmsg = _("internal error: out of range error");
   5425 	      goto common_error;
   5426 
   5427 	    case bfd_reloc_notsupported:
   5428 	      errmsg = _("internal error: unsupported relocation error");
   5429 	      goto common_error;
   5430 
   5431 	    case bfd_reloc_dangerous:
   5432 	      errmsg = _("internal error: dangerous error");
   5433 	      goto common_error;
   5434 
   5435 	    default:
   5436 	      errmsg = _("internal error: unknown error");
   5437 	      /* Fall through.  */
   5438 
   5439 common_error:
   5440 	      if (!((*info->callbacks->warning)
   5441 		    (info, errmsg, name, input_bfd, input_section, offset)))
   5442 		return FALSE;
   5443 	      break;
   5444 	    }
   5445 	}
   5446     }
   5447 
   5448   return ret;
   5449 }
   5450 
   5451 /* Finish up dynamic symbol handling.  We set the contents of various
   5452    dynamic sections here.  */
   5453 
   5454 static bfd_boolean
   5455 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
   5456 				 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
   5457 {
   5458   struct elf_nds32_link_hash_table *htab;
   5459   bfd_byte *loc;
   5460 
   5461   htab = nds32_elf_hash_table (info);
   5462 
   5463   if (h->plt.offset != (bfd_vma) - 1)
   5464     {
   5465       asection *splt;
   5466       asection *sgot;
   5467       asection *srela;
   5468 
   5469       bfd_vma plt_index;
   5470       bfd_vma got_offset;
   5471       bfd_vma local_plt_offset;
   5472       Elf_Internal_Rela rela;
   5473 
   5474       /* This symbol has an entry in the procedure linkage table.  Set
   5475 	 it up.  */
   5476 
   5477       BFD_ASSERT (h->dynindx != -1);
   5478 
   5479       splt = htab->splt;
   5480       sgot = htab->sgotplt;
   5481       srela = htab->srelplt;
   5482       BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
   5483 
   5484       /* Get the index in the procedure linkage table which
   5485 	 corresponds to this symbol.  This is the index of this symbol
   5486 	 in all the symbols for which we are making plt entries.  The
   5487 	 first entry in the procedure linkage table is reserved.  */
   5488       plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
   5489 
   5490       /* Get the offset into the .got table of the entry that
   5491 	 corresponds to this function.  Each .got entry is 4 bytes.
   5492 	 The first three are reserved.  */
   5493       got_offset = (plt_index + 3) * 4;
   5494 
   5495       /* Fill in the entry in the procedure linkage table.  */
   5496       if (!info->shared)
   5497 	{
   5498 	  unsigned long insn;
   5499 
   5500 	  insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
   5501 				      + sgot->output_offset + got_offset) >> 12)
   5502 				    & 0xfffff);
   5503 	  bfd_putb32 (insn, splt->contents + h->plt.offset);
   5504 
   5505 	  insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
   5506 				      + sgot->output_offset + got_offset) & 0x0fff)
   5507 				    >> 2);
   5508 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
   5509 
   5510 	  insn = PLT_ENTRY_WORD2;
   5511 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
   5512 
   5513 	  insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
   5514 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
   5515 
   5516 	  insn = PLT_ENTRY_WORD4
   5517 		 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
   5518 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
   5519 	  local_plt_offset = 12;
   5520 	}
   5521       else
   5522 	{
   5523 	  /* sda_base must be set at this time.  */
   5524 	  unsigned long insn;
   5525 	  long offset;
   5526 
   5527 	  /* FIXME, sda_base is 65536, it will damage opcode.  */
   5528 	  /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
   5529 	  offset = sgot->output_section->vma + sgot->output_offset + got_offset
   5530 		   - elf_gp (output_bfd);
   5531 	  insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
   5532 	  bfd_putb32 (insn, splt->contents + h->plt.offset);
   5533 
   5534 	  insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
   5535 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
   5536 
   5537 	  insn = PLT_PIC_ENTRY_WORD2;
   5538 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
   5539 
   5540 	  insn = PLT_PIC_ENTRY_WORD3;
   5541 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
   5542 
   5543 	  insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
   5544 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
   5545 
   5546 	  insn = PLT_PIC_ENTRY_WORD5
   5547 	    + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
   5548 	  bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
   5549 
   5550 	  local_plt_offset = 16;
   5551 	}
   5552 
   5553       /* Fill in the entry in the global offset table,
   5554 	 so it will fall through to the next instruction for the first time.  */
   5555       bfd_put_32 (output_bfd,
   5556 		  (splt->output_section->vma + splt->output_offset
   5557 		   + h->plt.offset + local_plt_offset),
   5558 		  sgot->contents + got_offset);
   5559 
   5560       /* Fill in the entry in the .rela.plt section.  */
   5561       rela.r_offset = (sgot->output_section->vma
   5562 		       + sgot->output_offset + got_offset);
   5563       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
   5564       rela.r_addend = 0;
   5565       loc = srela->contents;
   5566       loc += plt_index * sizeof (Elf32_External_Rela);
   5567       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   5568 
   5569       if (!h->def_regular)
   5570 	{
   5571 	  /* Mark the symbol as undefined, rather than as defined in
   5572 	     the .plt section.  Leave the value alone.  */
   5573 	  sym->st_shndx = SHN_UNDEF;
   5574 	  if (!h->ref_regular_nonweak)
   5575 	    sym->st_value = 0;
   5576 	}
   5577     }
   5578 
   5579   if (h->got.offset != (bfd_vma) - 1)
   5580     {
   5581       asection *sgot;
   5582       asection *srela;
   5583       Elf_Internal_Rela rela;
   5584 
   5585       /* This symbol has an entry in the global offset table.
   5586 	 Set it up.  */
   5587 
   5588       sgot = htab->sgot;
   5589       srela = htab->srelgot;
   5590       BFD_ASSERT (sgot != NULL && srela != NULL);
   5591 
   5592       rela.r_offset = (sgot->output_section->vma
   5593 		       + sgot->output_offset + (h->got.offset & ~1));
   5594 
   5595       /* If this is a -Bsymbolic link, and the symbol is defined
   5596 	 locally, we just want to emit a RELATIVE reloc.  Likewise if
   5597 	 the symbol was forced to be local because of a version file.
   5598 	 The entry in the global offset table will already have been
   5599 	 initialized in the relocate_section function.  */
   5600       if (info->shared
   5601 	  && (info->symbolic
   5602 	      || h->dynindx == -1 || h->forced_local) && h->def_regular)
   5603 	{
   5604 	  rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
   5605 	  rela.r_addend = (h->root.u.def.value
   5606 			   + h->root.u.def.section->output_section->vma
   5607 			   + h->root.u.def.section->output_offset);
   5608 	}
   5609       else
   5610 	{
   5611 	  BFD_ASSERT ((h->got.offset & 1) == 0);
   5612 	  bfd_put_32 (output_bfd, (bfd_vma) 0,
   5613 		      sgot->contents + h->got.offset);
   5614 	  rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
   5615 	  rela.r_addend = 0;
   5616 	}
   5617 
   5618       loc = srela->contents;
   5619       loc += srela->reloc_count * sizeof (Elf32_External_Rela);
   5620       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   5621       ++srela->reloc_count;
   5622     }
   5623 
   5624   if (h->needs_copy)
   5625     {
   5626       asection *s;
   5627       Elf_Internal_Rela rela;
   5628 
   5629       /* This symbols needs a copy reloc.  Set it up.  */
   5630 
   5631       BFD_ASSERT (h->dynindx != -1
   5632 		  && (h->root.type == bfd_link_hash_defined
   5633 		      || h->root.type == bfd_link_hash_defweak));
   5634 
   5635       s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
   5636       BFD_ASSERT (s != NULL);
   5637 
   5638       rela.r_offset = (h->root.u.def.value
   5639 		       + h->root.u.def.section->output_section->vma
   5640 		       + h->root.u.def.section->output_offset);
   5641       rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
   5642       rela.r_addend = 0;
   5643       loc = s->contents;
   5644       loc += s->reloc_count * sizeof (Elf32_External_Rela);
   5645       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
   5646       ++s->reloc_count;
   5647     }
   5648 
   5649   /* Mark some specially defined symbols as absolute.  */
   5650   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
   5651       || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
   5652     sym->st_shndx = SHN_ABS;
   5653 
   5654   return TRUE;
   5655 }
   5656 
   5657 
   5658 /* Finish up the dynamic sections.  */
   5659 
   5660 static bfd_boolean
   5661 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
   5662 {
   5663   struct elf_nds32_link_hash_table *htab;
   5664   bfd *dynobj;
   5665   asection *sdyn;
   5666   asection *sgot;
   5667 
   5668   htab = nds32_elf_hash_table (info);
   5669   dynobj = htab->root.dynobj;
   5670 
   5671   sgot = htab->sgotplt;
   5672   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
   5673 
   5674   if (htab->root.dynamic_sections_created)
   5675     {
   5676       asection *splt;
   5677       Elf32_External_Dyn *dyncon, *dynconend;
   5678 
   5679       BFD_ASSERT (sgot != NULL && sdyn != NULL);
   5680 
   5681       dyncon = (Elf32_External_Dyn *) sdyn->contents;
   5682       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
   5683 
   5684       for (; dyncon < dynconend; dyncon++)
   5685 	{
   5686 	  Elf_Internal_Dyn dyn;
   5687 	  asection *s;
   5688 
   5689 	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
   5690 
   5691 	  switch (dyn.d_tag)
   5692 	    {
   5693 	    default:
   5694 	      break;
   5695 
   5696 	    case DT_PLTGOT:
   5697 	      /* name = ".got"; */
   5698 	      s = htab->sgot->output_section;
   5699 	      goto get_vma;
   5700 	    case DT_JMPREL:
   5701 	      s = htab->srelplt->output_section;
   5702 	    get_vma:
   5703 	      BFD_ASSERT (s != NULL);
   5704 	      dyn.d_un.d_ptr = s->vma;
   5705 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   5706 	      break;
   5707 
   5708 	    case DT_PLTRELSZ:
   5709 	      s = htab->srelplt->output_section;
   5710 	      BFD_ASSERT (s != NULL);
   5711 	      dyn.d_un.d_val = s->size;
   5712 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   5713 	      break;
   5714 
   5715 	    case DT_RELASZ:
   5716 	      /* My reading of the SVR4 ABI indicates that the
   5717 		 procedure linkage table relocs (DT_JMPREL) should be
   5718 		 included in the overall relocs (DT_RELA).  This is
   5719 		 what Solaris does.  However, UnixWare can not handle
   5720 		 that case.  Therefore, we override the DT_RELASZ entry
   5721 		 here to make it not include the JMPREL relocs.  Since
   5722 		 the linker script arranges for .rela.plt to follow all
   5723 		 other relocation sections, we don't have to worry
   5724 		 about changing the DT_RELA entry.  */
   5725 	      if (htab->srelplt != NULL)
   5726 		{
   5727 		  s = htab->srelplt->output_section;
   5728 		  dyn.d_un.d_val -= s->size;
   5729 		}
   5730 	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
   5731 	      break;
   5732 	    }
   5733 	}
   5734 
   5735       /* Fill in the first entry in the procedure linkage table.  */
   5736       splt = htab->splt;
   5737       if (splt && splt->size > 0)
   5738 	{
   5739 	  if (info->shared)
   5740 	    {
   5741 	      unsigned long insn;
   5742 	      long offset;
   5743 
   5744 	      /* FIXME, sda_base is 65536, it will damage opcode.  */
   5745 	      /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
   5746 	      offset = sgot->output_section->vma + sgot->output_offset + 4
   5747 		       - elf_gp (output_bfd);
   5748 	      insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
   5749 	      bfd_putb32 (insn, splt->contents);
   5750 
   5751 	      /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
   5752 	      /* here has a typo?  */
   5753 	      insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
   5754 	      bfd_putb32 (insn, splt->contents + 4);
   5755 
   5756 	      insn = PLT0_PIC_ENTRY_WORD2;
   5757 	      bfd_putb32 (insn, splt->contents + 8);
   5758 
   5759 	      insn = PLT0_PIC_ENTRY_WORD3;
   5760 	      bfd_putb32 (insn, splt->contents + 12);
   5761 
   5762 	      insn = PLT0_PIC_ENTRY_WORD4;
   5763 	      bfd_putb32 (insn, splt->contents + 16);
   5764 
   5765 	      insn = PLT0_PIC_ENTRY_WORD5;
   5766 	      bfd_putb32 (insn, splt->contents + 20);
   5767 	    }
   5768 	  else
   5769 	    {
   5770 	      unsigned long insn;
   5771 	      unsigned long addr;
   5772 
   5773 	      /* addr = .got + 4 */
   5774 	      addr = sgot->output_section->vma + sgot->output_offset + 4;
   5775 	      insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
   5776 	      bfd_putb32 (insn, splt->contents);
   5777 
   5778 	      insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
   5779 	      bfd_putb32 (insn, splt->contents + 4);
   5780 
   5781 	      insn = PLT0_ENTRY_WORD2;
   5782 	      bfd_putb32 (insn, splt->contents + 8);
   5783 
   5784 	      insn = PLT0_ENTRY_WORD3;
   5785 	      bfd_putb32 (insn, splt->contents + 12);
   5786 
   5787 	      insn = PLT0_ENTRY_WORD4;
   5788 	      bfd_putb32 (insn, splt->contents + 16);
   5789 	    }
   5790 
   5791 	  elf_section_data (splt->output_section)->this_hdr.sh_entsize =
   5792 	    PLT_ENTRY_SIZE;
   5793 	}
   5794     }
   5795 
   5796   /* Fill in the first three entries in the global offset table.  */
   5797   if (sgot && sgot->size > 0)
   5798     {
   5799       if (sdyn == NULL)
   5800 	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
   5801       else
   5802 	bfd_put_32 (output_bfd,
   5803 		    sdyn->output_section->vma + sdyn->output_offset,
   5804 		    sgot->contents);
   5805       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
   5806       bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
   5807 
   5808       elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
   5809     }
   5810 
   5811   return TRUE;
   5812 }
   5813 
   5814 
   5816 /* Set the right machine number.  */
   5817 
   5818 static bfd_boolean
   5819 nds32_elf_object_p (bfd *abfd)
   5820 {
   5821   static unsigned int cur_arch = 0;
   5822 
   5823   if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
   5824     {
   5825       /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
   5826       cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
   5827     }
   5828 
   5829   switch (cur_arch)
   5830     {
   5831     default:
   5832     case E_N1_ARCH:
   5833       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
   5834       break;
   5835     case E_N1H_ARCH:
   5836       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
   5837       break;
   5838     case E_NDS_ARCH_STAR_V2_0:
   5839       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
   5840       break;
   5841     case E_NDS_ARCH_STAR_V3_0:
   5842       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
   5843       break;
   5844     case E_NDS_ARCH_STAR_V3_M:
   5845       bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
   5846       break;
   5847     }
   5848 
   5849   return TRUE;
   5850 }
   5851 
   5852 /* Store the machine number in the flags field.  */
   5853 
   5854 static void
   5855 nds32_elf_final_write_processing (bfd *abfd,
   5856 				  bfd_boolean linker ATTRIBUTE_UNUSED)
   5857 {
   5858   unsigned long val;
   5859   static unsigned int cur_mach = 0;
   5860 
   5861   if (bfd_mach_n1 != bfd_get_mach (abfd))
   5862     {
   5863       cur_mach = bfd_get_mach (abfd);
   5864     }
   5865 
   5866   switch (cur_mach)
   5867     {
   5868     case bfd_mach_n1:
   5869       /* Only happen when object is empty, since the case is abandon.  */
   5870       val = E_N1_ARCH;
   5871       val |= E_NDS_ABI_AABI;
   5872       val |= E_NDS32_ELF_VER_1_4;
   5873       break;
   5874     case bfd_mach_n1h:
   5875       val = E_N1H_ARCH;
   5876       break;
   5877     case bfd_mach_n1h_v2:
   5878       val = E_NDS_ARCH_STAR_V2_0;
   5879       break;
   5880     case bfd_mach_n1h_v3:
   5881       val = E_NDS_ARCH_STAR_V3_0;
   5882       break;
   5883     case bfd_mach_n1h_v3m:
   5884       val = E_NDS_ARCH_STAR_V3_M;
   5885       break;
   5886     default:
   5887       val = 0;
   5888       break;
   5889     }
   5890 
   5891   elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
   5892   elf_elfheader (abfd)->e_flags |= val;
   5893 }
   5894 
   5895 /* Function to keep NDS32 specific file flags.  */
   5896 
   5897 static bfd_boolean
   5898 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
   5899 {
   5900   BFD_ASSERT (!elf_flags_init (abfd)
   5901 	      || elf_elfheader (abfd)->e_flags == flags);
   5902 
   5903   elf_elfheader (abfd)->e_flags = flags;
   5904   elf_flags_init (abfd) = TRUE;
   5905   return TRUE;
   5906 }
   5907 
   5908 static unsigned int
   5909 convert_e_flags (unsigned int e_flags, unsigned int arch)
   5910 {
   5911   if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
   5912     {
   5913       /* From 0.9 to 1.0.  */
   5914       e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
   5915 
   5916       /* Invert E_NDS32_HAS_NO_MAC_INST.  */
   5917       e_flags ^= E_NDS32_HAS_NO_MAC_INST;
   5918       if (arch == E_NDS_ARCH_STAR_V1_0)
   5919 	{
   5920 	  /* Done.  */
   5921 	  return e_flags;
   5922 	}
   5923     }
   5924 
   5925   /* From 1.0 to 2.0.  */
   5926   e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
   5927 
   5928   /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
   5929   e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
   5930 
   5931   /* Invert E_NDS32_HAS_NO_MAC_INST.  */
   5932   e_flags ^= E_NDS32_HAS_NO_MAC_INST;
   5933   return e_flags;
   5934 }
   5935 
   5936 static bfd_boolean
   5937 nds32_check_vec_size (bfd *ibfd)
   5938 {
   5939   static unsigned int nds32_vec_size = 0;
   5940 
   5941   asection *sec_t = NULL;
   5942   bfd_byte *contents = NULL;
   5943 
   5944   sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
   5945 
   5946   if (sec_t && sec_t->size >= 4)
   5947     {
   5948       /* Get vec_size in file.  */
   5949       unsigned int flag_t;
   5950 
   5951       nds32_get_section_contents (ibfd, sec_t, &contents);
   5952       flag_t = bfd_get_32 (ibfd, contents);
   5953 
   5954       /* The value could only be 4 or 16.  */
   5955 
   5956       if (!nds32_vec_size)
   5957 	/* Set if not set yet.  */
   5958 	nds32_vec_size = (flag_t & 0x3);
   5959       else if (nds32_vec_size != (flag_t & 0x3))
   5960 	{
   5961 	  (*_bfd_error_handler) (_("%B: ISR vector size mismatch"
   5962 				   " with previous modules, previous %u-byte, current %u-byte"),
   5963 				 ibfd,
   5964 				 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
   5965 				 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
   5966 	  return FALSE;
   5967 	}
   5968       else
   5969 	/* Only keep the first vec_size section.  */
   5970 	sec_t->flags |= SEC_EXCLUDE;
   5971     }
   5972 
   5973   return TRUE;
   5974 }
   5975 
   5976 /* Merge backend specific data from an object file to the output
   5977    object file when linking.  */
   5978 
   5979 static bfd_boolean
   5980 nds32_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   5981 {
   5982   flagword out_flags;
   5983   flagword in_flags;
   5984   flagword out_16regs;
   5985   flagword in_no_mac;
   5986   flagword out_no_mac;
   5987   flagword in_16regs;
   5988   flagword out_version;
   5989   flagword in_version;
   5990   flagword out_fpu_config;
   5991   flagword in_fpu_config;
   5992 
   5993   /* TODO: Revise to use object-attributes instead.  */
   5994   if (!nds32_check_vec_size (ibfd))
   5995     return FALSE;
   5996 
   5997   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
   5998       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
   5999     return TRUE;
   6000 
   6001   if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
   6002     {
   6003       (*_bfd_error_handler)
   6004 	(_("%B: warning: Endian mismatch with previous modules."), ibfd);
   6005 
   6006       bfd_set_error (bfd_error_bad_value);
   6007       return FALSE;
   6008     }
   6009 
   6010   in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
   6011   if (in_version == E_NDS32_ELF_VER_1_2)
   6012     {
   6013       (*_bfd_error_handler)
   6014 	(_("%B: warning: Older version of object file encountered, "
   6015 	   "Please recompile with current tool chain."), ibfd);
   6016     }
   6017 
   6018   /* We may need to merge V1 and V2 arch object files to V2.  */
   6019   if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
   6020       != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
   6021     {
   6022       /* Need to convert version.  */
   6023       if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
   6024 	  == E_NDS_ARCH_STAR_RESERVED)
   6025 	{
   6026 	  elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   6027 	}
   6028       else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
   6029 	       || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
   6030 		  > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
   6031 	{
   6032 	  elf_elfheader (obfd)->e_flags =
   6033 	    convert_e_flags (elf_elfheader (obfd)->e_flags,
   6034 			     (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
   6035 	}
   6036       else
   6037 	{
   6038 	  elf_elfheader (ibfd)->e_flags =
   6039 	    convert_e_flags (elf_elfheader (ibfd)->e_flags,
   6040 			     (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
   6041 	}
   6042     }
   6043 
   6044   /* Extract some flags.  */
   6045   in_flags = elf_elfheader (ibfd)->e_flags
   6046 	     & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
   6047 		  | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
   6048 
   6049   /* The following flags need special treatment.  */
   6050   in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
   6051   in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
   6052   in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
   6053 
   6054   /* Extract some flags.  */
   6055   out_flags = elf_elfheader (obfd)->e_flags
   6056 	      & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
   6057 		   | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
   6058 
   6059   /* The following flags need special treatment.  */
   6060   out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
   6061   out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
   6062   out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
   6063   out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
   6064   if (!elf_flags_init (obfd))
   6065     {
   6066       /* If the input is the default architecture then do not
   6067 	 bother setting the flags for the output architecture,
   6068 	 instead allow future merges to do this.  If no future
   6069 	 merges ever set these flags then they will retain their
   6070 	 unitialised values, which surprise surprise, correspond
   6071 	 to the default values.  */
   6072       if (bfd_get_arch_info (ibfd)->the_default)
   6073 	return TRUE;
   6074 
   6075       elf_flags_init (obfd) = TRUE;
   6076       elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   6077 
   6078       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
   6079 	  && bfd_get_arch_info (obfd)->the_default)
   6080 	{
   6081 	  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
   6082 				    bfd_get_mach (ibfd));
   6083 	}
   6084 
   6085       return TRUE;
   6086     }
   6087 
   6088   /* Check flag compatibility.  */
   6089   if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
   6090     {
   6091       (*_bfd_error_handler)
   6092 	(_("%B: error: ABI mismatch with previous modules."), ibfd);
   6093 
   6094       bfd_set_error (bfd_error_bad_value);
   6095       return FALSE;
   6096     }
   6097 
   6098   if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
   6099     {
   6100       if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
   6101 	{
   6102 	  (*_bfd_error_handler)
   6103 	    (_("%B: error: Instruction set mismatch with previous modules."), ibfd);
   6104 
   6105 	  bfd_set_error (bfd_error_bad_value);
   6106 	  return FALSE;
   6107 	}
   6108     }
   6109 
   6110   /* When linking with V1.2 and V1.3 objects together the output is V1.2.
   6111      and perf ext1 and DIV are mergerd to perf ext1.  */
   6112   if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
   6113     {
   6114       elf_elfheader (obfd)->e_flags =
   6115 	(in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
   6116 	| (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
   6117 	| (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
   6118 	   ?  E_NDS32_HAS_EXT_INST : 0)
   6119 	| (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
   6120 	   ?  E_NDS32_HAS_EXT_INST : 0)
   6121 	| (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
   6122 	| ((in_version > out_version) ? out_version : in_version);
   6123     }
   6124   else
   6125     {
   6126       if (in_version != out_version)
   6127 	(*_bfd_error_handler) (_("%B: warning: Incompatible elf-versions %s and  %s."),
   6128 				 ibfd, nds32_elfver_strtab[out_version],
   6129 				 nds32_elfver_strtab[in_version]);
   6130 
   6131       elf_elfheader (obfd)->e_flags = in_flags | out_flags
   6132 	| (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
   6133 	| (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
   6134 	| (in_version > out_version ?  out_version : in_version);
   6135     }
   6136 
   6137   return TRUE;
   6138 }
   6139 
   6140 /* Display the flags field.  */
   6141 
   6142 static bfd_boolean
   6143 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
   6144 {
   6145   FILE *file = (FILE *) ptr;
   6146 
   6147   BFD_ASSERT (abfd != NULL && ptr != NULL);
   6148 
   6149   _bfd_elf_print_private_bfd_data (abfd, ptr);
   6150 
   6151   fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
   6152 
   6153   switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
   6154     {
   6155     default:
   6156     case E_N1_ARCH:
   6157       fprintf (file, _(": n1 instructions"));
   6158       break;
   6159     case E_N1H_ARCH:
   6160       fprintf (file, _(": n1h instructions"));
   6161       break;
   6162     }
   6163 
   6164   fputc ('\n', file);
   6165 
   6166   return TRUE;
   6167 }
   6168 
   6169 static unsigned int
   6170 nds32_elf_action_discarded (asection *sec)
   6171 {
   6172 
   6173   if (strncmp
   6174       (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
   6175     return 0;
   6176 
   6177   return _bfd_elf_default_action_discarded (sec);
   6178 }
   6179 
   6180 static asection *
   6181 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
   6182 			Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
   6183 			Elf_Internal_Sym *sym)
   6184 {
   6185   if (h != NULL)
   6186     switch (ELF32_R_TYPE (rel->r_info))
   6187       {
   6188       case R_NDS32_GNU_VTINHERIT:
   6189       case R_NDS32_GNU_VTENTRY:
   6190       case R_NDS32_RELA_GNU_VTINHERIT:
   6191       case R_NDS32_RELA_GNU_VTENTRY:
   6192 	return NULL;
   6193       }
   6194 
   6195   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
   6196 }
   6197 
   6198 static bfd_boolean
   6199 nds32_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, asection *sec,
   6200 			 const Elf_Internal_Rela *relocs)
   6201 {
   6202   /* Update the got entry reference counts for the section being removed.  */
   6203   Elf_Internal_Shdr *symtab_hdr;
   6204   struct elf_link_hash_entry **sym_hashes;
   6205   bfd_signed_vma *local_got_refcounts;
   6206   const Elf_Internal_Rela *rel, *relend;
   6207 
   6208   elf_section_data (sec)->local_dynrel = NULL;
   6209 
   6210   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   6211   sym_hashes = elf_sym_hashes (abfd);
   6212   local_got_refcounts = elf_local_got_refcounts (abfd);
   6213 
   6214   relend = relocs + sec->reloc_count;
   6215   for (rel = relocs; rel < relend; rel++)
   6216     {
   6217       unsigned long r_symndx;
   6218       struct elf_link_hash_entry *h = NULL;
   6219 
   6220       r_symndx = ELF32_R_SYM (rel->r_info);
   6221       if (r_symndx >= symtab_hdr->sh_info)
   6222 	{
   6223 	  /* External symbol.  */
   6224 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6225 	  while (h->root.type == bfd_link_hash_indirect
   6226 		 || h->root.type == bfd_link_hash_warning)
   6227 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6228 	}
   6229 
   6230       switch (ELF32_R_TYPE (rel->r_info))
   6231 	{
   6232 	case R_NDS32_GOT_HI20:
   6233 	case R_NDS32_GOT_LO12:
   6234 	case R_NDS32_GOT_LO15:
   6235 	case R_NDS32_GOT_LO19:
   6236 	case R_NDS32_GOT17S2_RELA:
   6237 	case R_NDS32_GOT15S2_RELA:
   6238 	case R_NDS32_GOTOFF:
   6239 	case R_NDS32_GOTOFF_HI20:
   6240 	case R_NDS32_GOTOFF_LO12:
   6241 	case R_NDS32_GOTOFF_LO15:
   6242 	case R_NDS32_GOTOFF_LO19:
   6243 	case R_NDS32_GOT20:
   6244 	case R_NDS32_GOTPC_HI20:
   6245 	case R_NDS32_GOTPC_LO12:
   6246 	case R_NDS32_GOTPC20:
   6247 	  if (h != NULL)
   6248 	    {
   6249 	      if (h->got.refcount > 0)
   6250 		h->got.refcount--;
   6251 	    }
   6252 	  else
   6253 	    {
   6254 	      if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
   6255 		local_got_refcounts[r_symndx]--;
   6256 	    }
   6257 	  break;
   6258 
   6259 	case R_NDS32_16_RELA:
   6260 	case R_NDS32_20_RELA:
   6261 	case R_NDS32_5_RELA:
   6262 	case R_NDS32_32_RELA:
   6263 	case R_NDS32_HI20_RELA:
   6264 	case R_NDS32_LO12S3_RELA:
   6265 	case R_NDS32_LO12S2_RELA:
   6266 	case R_NDS32_LO12S2_DP_RELA:
   6267 	case R_NDS32_LO12S2_SP_RELA:
   6268 	case R_NDS32_LO12S1_RELA:
   6269 	case R_NDS32_LO12S0_RELA:
   6270 	case R_NDS32_LO12S0_ORI_RELA:
   6271 	case R_NDS32_SDA16S3_RELA:
   6272 	case R_NDS32_SDA17S2_RELA:
   6273 	case R_NDS32_SDA18S1_RELA:
   6274 	case R_NDS32_SDA19S0_RELA:
   6275 	case R_NDS32_SDA15S3_RELA:
   6276 	case R_NDS32_SDA15S2_RELA:
   6277 	case R_NDS32_SDA12S2_DP_RELA:
   6278 	case R_NDS32_SDA12S2_SP_RELA:
   6279 	case R_NDS32_SDA15S1_RELA:
   6280 	case R_NDS32_SDA15S0_RELA:
   6281 	case R_NDS32_SDA_FP7U2_RELA:
   6282 	case R_NDS32_15_PCREL_RELA:
   6283 	case R_NDS32_17_PCREL_RELA:
   6284 	case R_NDS32_25_PCREL_RELA:
   6285 	  if (h != NULL)
   6286 	    {
   6287 	      struct elf_nds32_link_hash_entry *eh;
   6288 	      struct elf_nds32_dyn_relocs **pp;
   6289 	      struct elf_nds32_dyn_relocs *p;
   6290 
   6291 	      if (!info->shared && h->plt.refcount > 0)
   6292 		h->plt.refcount -= 1;
   6293 
   6294 	      eh = (struct elf_nds32_link_hash_entry *) h;
   6295 
   6296 	      for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
   6297 		if (p->sec == sec)
   6298 		  {
   6299 		    if (ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
   6300 			|| ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
   6301 			|| ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA)
   6302 		      p->pc_count -= 1;
   6303 		    p->count -= 1;
   6304 		    if (p->count == 0)
   6305 		      *pp = p->next;
   6306 		    break;
   6307 		  }
   6308 	    }
   6309 	  break;
   6310 
   6311 	case R_NDS32_9_PLTREL:
   6312 	case R_NDS32_25_PLTREL:
   6313 	  if (h != NULL)
   6314 	    {
   6315 	      if (h->plt.refcount > 0)
   6316 		h->plt.refcount--;
   6317 	    }
   6318 	  break;
   6319 
   6320 	default:
   6321 	  break;
   6322 	}
   6323     }
   6324 
   6325   return TRUE;
   6326 }
   6327 
   6328 /* Look through the relocs for a section during the first phase.
   6329    Since we don't do .gots or .plts, we just need to consider the
   6330    virtual table relocs for gc.  */
   6331 
   6332 static bfd_boolean
   6333 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   6334 			asection *sec, const Elf_Internal_Rela *relocs)
   6335 {
   6336   Elf_Internal_Shdr *symtab_hdr;
   6337   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
   6338   const Elf_Internal_Rela *rel;
   6339   const Elf_Internal_Rela *rel_end;
   6340   struct elf_nds32_link_hash_table *htab;
   6341   bfd *dynobj;
   6342   asection *sreloc = NULL;
   6343 
   6344   if (info->relocatable)
   6345     return TRUE;
   6346 
   6347   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   6348   sym_hashes = elf_sym_hashes (abfd);
   6349   sym_hashes_end =
   6350     sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
   6351   if (!elf_bad_symtab (abfd))
   6352     sym_hashes_end -= symtab_hdr->sh_info;
   6353 
   6354   htab = nds32_elf_hash_table (info);
   6355   dynobj = htab->root.dynobj;
   6356 
   6357   rel_end = relocs + sec->reloc_count;
   6358   for (rel = relocs; rel < rel_end; rel++)
   6359     {
   6360       enum elf_nds32_reloc_type r_type;
   6361       struct elf_link_hash_entry *h;
   6362       unsigned long r_symndx;
   6363       int tls_type, old_tls_type;
   6364 
   6365       r_symndx = ELF32_R_SYM (rel->r_info);
   6366       r_type = ELF32_R_TYPE (rel->r_info);
   6367       if (r_symndx < symtab_hdr->sh_info)
   6368 	h = NULL;
   6369       else
   6370 	{
   6371 	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   6372 	  while (h->root.type == bfd_link_hash_indirect
   6373 		 || h->root.type == bfd_link_hash_warning)
   6374 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6375 	}
   6376 
   6377       /* Some relocs require a global offset table.  We create
   6378 	 got section here, since these relocation need got section
   6379 	 and it is not created yet.  */
   6380       if (htab->sgot == NULL)
   6381 	{
   6382 	  switch (r_type)
   6383 	    {
   6384 	    case R_NDS32_GOT_HI20:
   6385 	    case R_NDS32_GOT_LO12:
   6386 	    case R_NDS32_GOT_LO15:
   6387 	    case R_NDS32_GOT_LO19:
   6388 	    case R_NDS32_GOT17S2_RELA:
   6389 	    case R_NDS32_GOT15S2_RELA:
   6390 	    case R_NDS32_GOTOFF:
   6391 	    case R_NDS32_GOTOFF_HI20:
   6392 	    case R_NDS32_GOTOFF_LO12:
   6393 	    case R_NDS32_GOTOFF_LO15:
   6394 	    case R_NDS32_GOTOFF_LO19:
   6395 	    case R_NDS32_GOTPC20:
   6396 	    case R_NDS32_GOTPC_HI20:
   6397 	    case R_NDS32_GOTPC_LO12:
   6398 	    case R_NDS32_GOT20:
   6399 	    case R_NDS32_TLS_IE_HI20:
   6400 	    case R_NDS32_TLS_IE_LO12S2:
   6401 	      if (dynobj == NULL)
   6402 		htab->root.dynobj = dynobj = abfd;
   6403 	      if (!create_got_section (dynobj, info))
   6404 		return FALSE;
   6405 	      break;
   6406 
   6407 	    default:
   6408 	      break;
   6409 	    }
   6410 	}
   6411 
   6412       switch ((int) r_type)
   6413 	{
   6414 	case R_NDS32_GOT_HI20:
   6415 	case R_NDS32_GOT_LO12:
   6416 	case R_NDS32_GOT_LO15:
   6417 	case R_NDS32_GOT_LO19:
   6418 	case R_NDS32_GOT20:
   6419 	case R_NDS32_TLS_IE_HI20:
   6420 	case R_NDS32_TLS_IE_LO12S2:
   6421 	  switch (r_type)
   6422 	    {
   6423 	    case R_NDS32_TLS_IE_HI20:
   6424 	    case R_NDS32_TLS_IE_LO12S2:
   6425 	      tls_type = GOT_TLS_IE;
   6426 	      break;
   6427 	    default:
   6428 	      tls_type = GOT_NORMAL;
   6429 	      break;
   6430 	    }
   6431 	  if (h != NULL)
   6432 	    {
   6433 	      old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
   6434 	      h->got.refcount += 1;
   6435 	    }
   6436 	  else
   6437 	    {
   6438 	      bfd_signed_vma *local_got_refcounts;
   6439 
   6440 	      /* This is a global offset table entry for a local
   6441 		 symbol.  */
   6442 	      local_got_refcounts = elf_local_got_refcounts (abfd);
   6443 	      if (local_got_refcounts == NULL)
   6444 		{
   6445 		  bfd_size_type size;
   6446 
   6447 		  size = symtab_hdr->sh_info;
   6448 		  size *= sizeof (bfd_signed_vma);
   6449 		  local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
   6450 		  if (local_got_refcounts == NULL)
   6451 		    return FALSE;
   6452 		  elf_local_got_refcounts (abfd) = local_got_refcounts;
   6453 		}
   6454 	      local_got_refcounts[r_symndx] += 1;
   6455 	      old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
   6456 	    }
   6457 
   6458 	  /* We will already have issued an error message if there
   6459 	     is a TLS/non-TLS mismatch, based on the symbol
   6460 	     type.  So just combine any TLS types needed.  */
   6461 	  if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
   6462 	      && tls_type != GOT_NORMAL)
   6463 	    tls_type |= old_tls_type;
   6464 
   6465 	  if (old_tls_type != tls_type)
   6466 	    {
   6467 	      if (h != NULL)
   6468 		elf32_nds32_hash_entry (h)->tls_type = tls_type;
   6469 	      else
   6470 		elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
   6471 	    }
   6472 	  break;
   6473 	case R_NDS32_9_PLTREL:
   6474 	case R_NDS32_25_PLTREL:
   6475 	case R_NDS32_PLTREL_HI20:
   6476 	case R_NDS32_PLTREL_LO12:
   6477 	case R_NDS32_PLT_GOTREL_HI20:
   6478 	case R_NDS32_PLT_GOTREL_LO12:
   6479 	case R_NDS32_PLT_GOTREL_LO15:
   6480 	case R_NDS32_PLT_GOTREL_LO19:
   6481 	case R_NDS32_PLT_GOTREL_LO20:
   6482 
   6483 	  /* This symbol requires a procedure linkage table entry.  We
   6484 	     actually build the entry in adjust_dynamic_symbol,
   6485 	     because this might be a case of linking PIC code without
   6486 	     linking in any dynamic objects, in which case we don't
   6487 	     need to generate a procedure linkage table after all.  */
   6488 
   6489 	  /* If this is a local symbol, we resolve it directly without
   6490 	     creating a procedure linkage table entry.  */
   6491 	  if (h == NULL)
   6492 	    continue;
   6493 
   6494 	  if (h->forced_local)
   6495 	    break;
   6496 
   6497 	  elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
   6498 	  h->needs_plt = 1;
   6499 	  h->plt.refcount += 1;
   6500 	  break;
   6501 
   6502 	case R_NDS32_16_RELA:
   6503 	case R_NDS32_20_RELA:
   6504 	case R_NDS32_5_RELA:
   6505 	case R_NDS32_32_RELA:
   6506 	case R_NDS32_HI20_RELA:
   6507 	case R_NDS32_LO12S3_RELA:
   6508 	case R_NDS32_LO12S2_RELA:
   6509 	case R_NDS32_LO12S2_DP_RELA:
   6510 	case R_NDS32_LO12S2_SP_RELA:
   6511 	case R_NDS32_LO12S1_RELA:
   6512 	case R_NDS32_LO12S0_RELA:
   6513 	case R_NDS32_LO12S0_ORI_RELA:
   6514 	case R_NDS32_SDA16S3_RELA:
   6515 	case R_NDS32_SDA17S2_RELA:
   6516 	case R_NDS32_SDA18S1_RELA:
   6517 	case R_NDS32_SDA19S0_RELA:
   6518 	case R_NDS32_SDA15S3_RELA:
   6519 	case R_NDS32_SDA15S2_RELA:
   6520 	case R_NDS32_SDA12S2_DP_RELA:
   6521 	case R_NDS32_SDA12S2_SP_RELA:
   6522 	case R_NDS32_SDA15S1_RELA:
   6523 	case R_NDS32_SDA15S0_RELA:
   6524 	case R_NDS32_SDA_FP7U2_RELA:
   6525 	case R_NDS32_15_PCREL_RELA:
   6526 	case R_NDS32_17_PCREL_RELA:
   6527 	case R_NDS32_25_PCREL_RELA:
   6528 
   6529 	  if (h != NULL && !info->shared)
   6530 	    {
   6531 	      h->non_got_ref = 1;
   6532 	      h->plt.refcount += 1;
   6533 	    }
   6534 
   6535 	  /* If we are creating a shared library, and this is a reloc against
   6536 	     a global symbol, or a non PC relative reloc against a local
   6537 	     symbol, then we need to copy the reloc into the shared library.
   6538 	     However, if we are linking with -Bsymbolic, we do not need to
   6539 	     copy a reloc against a global symbol which is defined in an
   6540 	     object we are including in the link (i.e., DEF_REGULAR is set).
   6541 	     At this point we have not seen all the input files, so it is
   6542 	     possible that DEF_REGULAR is not set now but will be set later
   6543 	     (it is never cleared).  We account for that possibility below by
   6544 	     storing information in the dyn_relocs field of the hash table
   6545 	     entry.  A similar situation occurs when creating shared libraries
   6546 	     and symbol visibility changes render the symbol local.
   6547 
   6548 	     If on the other hand, we are creating an executable, we may need
   6549 	     to keep relocations for symbols satisfied by a dynamic library
   6550 	     if we manage to avoid copy relocs for the symbol.  */
   6551 	  if ((info->shared
   6552 	       && (sec->flags & SEC_ALLOC) != 0
   6553 	       && ((r_type != R_NDS32_25_PCREL_RELA
   6554 		    && r_type != R_NDS32_15_PCREL_RELA
   6555 		    && r_type != R_NDS32_17_PCREL_RELA
   6556 		    && !(r_type == R_NDS32_32_RELA
   6557 			 && strcmp (sec->name, ".eh_frame") == 0))
   6558 		   || (h != NULL
   6559 		       && (!info->symbolic
   6560 			   || h->root.type == bfd_link_hash_defweak
   6561 			   || !h->def_regular))))
   6562 	      || (!info->shared
   6563 		  && (sec->flags & SEC_ALLOC) != 0
   6564 		  && h != NULL
   6565 		  && (h->root.type == bfd_link_hash_defweak
   6566 		      || !h->def_regular)))
   6567 	    {
   6568 	      struct elf_nds32_dyn_relocs *p;
   6569 	      struct elf_nds32_dyn_relocs **head;
   6570 
   6571 	      if (dynobj == NULL)
   6572 		htab->root.dynobj = dynobj = abfd;
   6573 
   6574 	      /* When creating a shared object, we must copy these
   6575 		 relocs into the output file.  We create a reloc
   6576 		 section in dynobj and make room for the reloc.  */
   6577 	      if (sreloc == NULL)
   6578 		{
   6579 		  const char *name;
   6580 
   6581 		  name = bfd_elf_string_from_elf_section
   6582 		    (abfd, elf_elfheader (abfd)->e_shstrndx,
   6583 		     elf_section_data (sec)->rela.hdr->sh_name);
   6584 		  if (name == NULL)
   6585 		    return FALSE;
   6586 
   6587 		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
   6588 			      && strcmp (bfd_get_section_name (abfd, sec),
   6589 					 name + 5) == 0);
   6590 
   6591 		  sreloc = bfd_get_section_by_name (dynobj, name);
   6592 		  if (sreloc == NULL)
   6593 		    {
   6594 		      flagword flags;
   6595 
   6596 		      sreloc = bfd_make_section (dynobj, name);
   6597 		      flags = (SEC_HAS_CONTENTS | SEC_READONLY
   6598 			       | SEC_IN_MEMORY | SEC_LINKER_CREATED);
   6599 		      if ((sec->flags & SEC_ALLOC) != 0)
   6600 			flags |= SEC_ALLOC | SEC_LOAD;
   6601 		      if (sreloc == NULL
   6602 			  || !bfd_set_section_flags (dynobj, sreloc, flags)
   6603 			  || !bfd_set_section_alignment (dynobj, sreloc, 2))
   6604 			return FALSE;
   6605 
   6606 		      elf_section_type (sreloc) = SHT_RELA;
   6607 		    }
   6608 		  elf_section_data (sec)->sreloc = sreloc;
   6609 		}
   6610 
   6611 	      /* If this is a global symbol, we count the number of
   6612 		 relocations we need for this symbol.  */
   6613 	      if (h != NULL)
   6614 		head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
   6615 	      else
   6616 		{
   6617 		  asection *s;
   6618 
   6619 		  Elf_Internal_Sym *isym;
   6620 		  isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
   6621 		  if (isym == NULL)
   6622 		    return FALSE;
   6623 
   6624 		  /* Track dynamic relocs needed for local syms too.  */
   6625 		  s = bfd_section_from_elf_index (abfd, isym->st_shndx);
   6626 		  if (s == NULL)
   6627 		    return FALSE;
   6628 
   6629 		  head = ((struct elf_nds32_dyn_relocs **)
   6630 			&elf_section_data (s)->local_dynrel);
   6631 		}
   6632 
   6633 	      p = *head;
   6634 	      if (p == NULL || p->sec != sec)
   6635 		{
   6636 		  bfd_size_type amt = sizeof (*p);
   6637 		  p = (struct elf_nds32_dyn_relocs *) bfd_alloc (dynobj, amt);
   6638 		  if (p == NULL)
   6639 		    return FALSE;
   6640 		  p->next = *head;
   6641 		  *head = p;
   6642 		  p->sec = sec;
   6643 		  p->count = 0;
   6644 		  p->pc_count = 0;
   6645 		}
   6646 
   6647 	      p->count += 1;
   6648 	      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
   6649 		  || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
   6650 		  || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
   6651 		p->pc_count += 1;
   6652 	    }
   6653 	  break;
   6654 
   6655 	  /* This relocation describes the C++ object vtable hierarchy.
   6656 	     Reconstruct it for later use during GC.  */
   6657 	case R_NDS32_RELA_GNU_VTINHERIT:
   6658 	case R_NDS32_GNU_VTINHERIT:
   6659 	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
   6660 	    return FALSE;
   6661 	  break;
   6662 
   6663 	  /* This relocation describes which C++ vtable entries are actually
   6664 	     used.  Record for later use during GC.  */
   6665 	case R_NDS32_GNU_VTENTRY:
   6666 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
   6667 	    return FALSE;
   6668 	  break;
   6669 	case R_NDS32_RELA_GNU_VTENTRY:
   6670 	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
   6671 	    return FALSE;
   6672 	  break;
   6673 	}
   6674     }
   6675 
   6676   return TRUE;
   6677 }
   6678 
   6679 /* Write VAL in uleb128 format to P, returning a pointer to the
   6680    following byte.
   6681    This code is copied from elf-attr.c.  */
   6682 
   6683 static bfd_byte *
   6684 write_uleb128 (bfd_byte *p, unsigned int val)
   6685 {
   6686   bfd_byte c;
   6687   do
   6688     {
   6689       c = val & 0x7f;
   6690       val >>= 7;
   6691       if (val)
   6692 	c |= 0x80;
   6693       *(p++) = c;
   6694     }
   6695   while (val);
   6696   return p;
   6697 }
   6698 
   6699 static bfd_signed_vma
   6700 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   6701 		  Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
   6702 		  int *pic_ext_target)
   6703 {
   6704   bfd_signed_vma foff;
   6705   bfd_vma symval, addend;
   6706   asection *sym_sec;
   6707 
   6708   /* Get the value of the symbol referred to by the reloc.  */
   6709   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   6710     {
   6711       Elf_Internal_Sym *isym;
   6712 
   6713       /* A local symbol.  */
   6714       isym = isymbuf + ELF32_R_SYM (irel->r_info);
   6715 
   6716       if (isym->st_shndx == SHN_UNDEF)
   6717 	sym_sec = bfd_und_section_ptr;
   6718       else if (isym->st_shndx == SHN_ABS)
   6719 	sym_sec = bfd_abs_section_ptr;
   6720       else if (isym->st_shndx == SHN_COMMON)
   6721 	sym_sec = bfd_com_section_ptr;
   6722       else
   6723 	sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   6724       symval = isym->st_value + sym_sec->output_section->vma
   6725 	       + sym_sec->output_offset;
   6726     }
   6727   else
   6728     {
   6729       unsigned long indx;
   6730       struct elf_link_hash_entry *h;
   6731       bfd *owner;
   6732 
   6733       /* An external symbol.  */
   6734       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   6735       h = elf_sym_hashes (abfd)[indx];
   6736       BFD_ASSERT (h != NULL);
   6737 
   6738       if (h->root.type != bfd_link_hash_defined
   6739 	  && h->root.type != bfd_link_hash_defweak)
   6740 	/* This appears to be a reference to an undefined
   6741 	   symbol.  Just ignore it--it will be caught by the
   6742 	   regular reloc processing.  */
   6743 	return 0;
   6744       owner = h->root.u.def.section->owner;
   6745       if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
   6746 	*pic_ext_target = 1;
   6747 
   6748       if (h->root.u.def.section->flags & SEC_MERGE)
   6749 	{
   6750 	  sym_sec = h->root.u.def.section;
   6751 	  symval = _bfd_merged_section_offset (abfd, &sym_sec,
   6752 					       elf_section_data (sym_sec)->sec_info,
   6753 					       h->root.u.def.value);
   6754 	  symval = symval + sym_sec->output_section->vma
   6755 		   + sym_sec->output_offset;
   6756 	}
   6757       else
   6758 	symval = (h->root.u.def.value
   6759 		  + h->root.u.def.section->output_section->vma
   6760 		  + h->root.u.def.section->output_offset);
   6761     }
   6762 
   6763   addend = irel->r_addend;
   6764 
   6765   foff = (symval + addend
   6766 	  - (irel->r_offset + sec->output_section->vma + sec->output_offset));
   6767   return foff;
   6768 }
   6769 
   6770 static bfd_vma
   6771 calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
   6772 			      Elf_Internal_Sym *isymbuf,
   6773 			      Elf_Internal_Rela *irel,
   6774 			      Elf_Internal_Shdr *symtab_hdr)
   6775 {
   6776   bfd_vma symval;
   6777 
   6778   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   6779     {
   6780       Elf_Internal_Sym *isym;
   6781       asection *sym_sec;
   6782       /* A local symbol.  */
   6783       isym = isymbuf + ELF32_R_SYM (irel->r_info);
   6784 
   6785       if (isym->st_shndx == SHN_UNDEF)
   6786 	sym_sec = bfd_und_section_ptr;
   6787       else if (isym->st_shndx == SHN_ABS)
   6788 	sym_sec = bfd_abs_section_ptr;
   6789       else if (isym->st_shndx == SHN_COMMON)
   6790 	sym_sec = bfd_com_section_ptr;
   6791       else
   6792 	sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   6793       symval = isym->st_value + sym_sec->output_section->vma
   6794 	       + sym_sec->output_offset;
   6795     }
   6796   else
   6797     {
   6798       unsigned long indx;
   6799       struct elf_link_hash_entry *h;
   6800       struct elf_nds32_link_hash_table *htab;
   6801       asection *splt;
   6802 
   6803       /* An external symbol.  */
   6804       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   6805       h = elf_sym_hashes (abfd)[indx];
   6806       BFD_ASSERT (h != NULL);
   6807       htab = nds32_elf_hash_table (link_info);
   6808       splt = htab->splt;
   6809 
   6810       while (h->root.type == bfd_link_hash_indirect
   6811 	     || h->root.type == bfd_link_hash_warning)
   6812 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   6813 
   6814       if (h->plt.offset == (bfd_vma) - 1)
   6815 	{
   6816 	  if (h->root.type != bfd_link_hash_defined
   6817 	      && h->root.type != bfd_link_hash_defweak)
   6818 	    /* This appears to be a reference to an undefined
   6819 	     * symbol.  Just ignore it--it will be caught by the
   6820 	     * regular reloc processing.  */
   6821 	    return 0;
   6822 	  symval = (h->root.u.def.value
   6823 		    + h->root.u.def.section->output_section->vma
   6824 		    + h->root.u.def.section->output_offset);
   6825 	}
   6826       else
   6827 	symval = splt->output_section->vma + h->plt.offset;
   6828     }
   6829 
   6830   return symval;
   6831 }
   6832 
   6833 static bfd_signed_vma
   6834 calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
   6835 		      Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
   6836 		      Elf_Internal_Shdr *symtab_hdr)
   6837 {
   6838   bfd_vma foff;
   6839   if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
   6840 					    symtab_hdr)) == 0)
   6841     return 0;
   6842   else
   6843     return foff - (irel->r_offset
   6844 		   + sec->output_section->vma + sec->output_offset);
   6845 }
   6846 
   6847 /* Convert a 32-bit instruction to 16-bit one.
   6849    INSN is the input 32-bit instruction, INSN16 is the output 16-bit
   6850    instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
   6851    type of INSN16.  Return 1 if successful.  */
   6852 
   6853 static int
   6854 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
   6855 			     int *pinsn_type)
   6856 {
   6857   uint16_t insn16 = 0;
   6858   int insn_type;
   6859   unsigned long mach = bfd_get_mach (abfd);
   6860 
   6861   if (N32_SH5 (insn) != 0)
   6862     return 0;
   6863 
   6864   switch (N32_SUB5 (insn))
   6865     {
   6866     case N32_ALU1_ADD_SLLI:
   6867     case N32_ALU1_ADD_SRLI:
   6868       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
   6869 	{
   6870 	  insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
   6871 				N32_RB5 (insn));
   6872 	  insn_type = NDS32_INSN_ADD333;
   6873 	}
   6874       else if (N32_IS_RT4 (insn))
   6875 	{
   6876 	  if (N32_RT5 (insn) == N32_RA5 (insn))
   6877 	    insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
   6878 	  else if (N32_RT5 (insn) == N32_RB5 (insn))
   6879 	    insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
   6880 	  insn_type = NDS32_INSN_ADD45;
   6881 	}
   6882       break;
   6883 
   6884     case N32_ALU1_SUB_SLLI:
   6885     case N32_ALU1_SUB_SRLI:
   6886       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
   6887 	{
   6888 	  insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
   6889 				N32_RB5 (insn));
   6890 	  insn_type = NDS32_INSN_SUB333;
   6891 	}
   6892       else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
   6893 	{
   6894 	  insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
   6895 	  insn_type = NDS32_INSN_SUB45;
   6896 	}
   6897       break;
   6898 
   6899     case N32_ALU1_AND_SLLI:
   6900     case N32_ALU1_AND_SRLI:
   6901       /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
   6902       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   6903 	  && N32_IS_RB3 (insn))
   6904 	{
   6905 	  if (N32_RT5 (insn) == N32_RA5 (insn))
   6906 	    insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
   6907 	  else if (N32_RT5 (insn) == N32_RB5 (insn))
   6908 	    insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
   6909 	  if (insn16)
   6910 	    insn_type = NDS32_INSN_AND33;
   6911 	}
   6912       break;
   6913 
   6914     case N32_ALU1_XOR_SLLI:
   6915     case N32_ALU1_XOR_SRLI:
   6916       /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
   6917       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   6918 	  && N32_IS_RB3 (insn))
   6919 	{
   6920 	  if (N32_RT5 (insn) == N32_RA5 (insn))
   6921 	    insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
   6922 	  else if (N32_RT5 (insn) == N32_RB5 (insn))
   6923 	    insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
   6924 	  if (insn16)
   6925 	    insn_type = NDS32_INSN_XOR33;
   6926 	}
   6927       break;
   6928 
   6929     case N32_ALU1_OR_SLLI:
   6930     case N32_ALU1_OR_SRLI:
   6931       /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
   6932       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   6933 	  && N32_IS_RB3 (insn))
   6934 	{
   6935 	  if (N32_RT5 (insn) == N32_RA5 (insn))
   6936 	    insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
   6937 	  else if (N32_RT5 (insn) == N32_RB5 (insn))
   6938 	    insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
   6939 	  if (insn16)
   6940 	    insn_type = NDS32_INSN_OR33;
   6941 	}
   6942       break;
   6943     case N32_ALU1_NOR:
   6944       /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
   6945       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
   6946 	  && N32_RA5 (insn) == N32_RB5 (insn))
   6947 	{
   6948 	  insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
   6949 	  insn_type = NDS32_INSN_NOT33;
   6950 	}
   6951       break;
   6952     case N32_ALU1_SRAI:
   6953       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
   6954 	{
   6955 	  insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
   6956 	  insn_type = NDS32_INSN_SRAI45;
   6957 	}
   6958       break;
   6959 
   6960     case N32_ALU1_SRLI:
   6961       if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
   6962 	{
   6963 	  insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
   6964 	  insn_type = NDS32_INSN_SRLI45;
   6965 	}
   6966       break;
   6967 
   6968     case N32_ALU1_SLLI:
   6969       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
   6970 	{
   6971 	  insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
   6972 				N32_UB5 (insn));
   6973 	  insn_type = NDS32_INSN_SLLI333;
   6974 	}
   6975       break;
   6976 
   6977     case N32_ALU1_ZEH:
   6978       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
   6979 	{
   6980 	  insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
   6981 	  insn_type = NDS32_INSN_ZEH33;
   6982 	}
   6983       break;
   6984 
   6985     case N32_ALU1_SEB:
   6986       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
   6987 	{
   6988 	  insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
   6989 	  insn_type = NDS32_INSN_SEB33;
   6990 	}
   6991       break;
   6992 
   6993     case N32_ALU1_SEH:
   6994       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
   6995 	{
   6996 	  insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
   6997 	  insn_type = NDS32_INSN_SEH33;
   6998 	}
   6999       break;
   7000 
   7001     case N32_ALU1_SLT:
   7002       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
   7003 	{
   7004 	  /* Implicit r15.  */
   7005 	  insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
   7006 	  insn_type = NDS32_INSN_SLT45;
   7007 	}
   7008       break;
   7009 
   7010     case N32_ALU1_SLTS:
   7011       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
   7012 	{
   7013 	  /* Implicit r15.  */
   7014 	  insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
   7015 	  insn_type = NDS32_INSN_SLTS45;
   7016 	}
   7017       break;
   7018     }
   7019 
   7020   if ((insn16 & 0x8000) == 0)
   7021     return 0;
   7022 
   7023   if (pinsn16)
   7024     *pinsn16 = insn16;
   7025   if (pinsn_type)
   7026     *pinsn_type = insn_type;
   7027   return 1;
   7028 }
   7029 
   7030 static int
   7031 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
   7032 			     int *pinsn_type)
   7033 {
   7034   uint16_t insn16 = 0;
   7035   int insn_type;
   7036   unsigned long mach = bfd_get_mach (abfd);
   7037 
   7038   /* TODO: bset, bclr, btgl, btst.  */
   7039   if (__GF (insn, 6, 4) != 0)
   7040     return 0;
   7041 
   7042   switch (N32_IMMU (insn, 6))
   7043     {
   7044     case N32_ALU2_MUL:
   7045       if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7046 	  && N32_IS_RB3 (insn))
   7047 	{
   7048 	  if (N32_RT5 (insn) == N32_RA5 (insn))
   7049 	    insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
   7050 	  else if (N32_RT5 (insn) == N32_RB5 (insn))
   7051 	    insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
   7052 	  if (insn16)
   7053 	    insn_type = NDS32_INSN_MUL33;
   7054 	}
   7055     }
   7056 
   7057   if ((insn16 & 0x8000) == 0)
   7058     return 0;
   7059 
   7060   if (pinsn16)
   7061     *pinsn16 = insn16;
   7062   if (pinsn_type)
   7063     *pinsn_type = insn_type;
   7064   return 1;
   7065 }
   7066 
   7067 int
   7068 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
   7069 			int *pinsn_type)
   7070 {
   7071   int op6;
   7072   uint16_t insn16 = 0;
   7073   int insn_type;
   7074   unsigned long mach = bfd_get_mach (abfd);
   7075 
   7076   /* Decode 32-bit instruction.  */
   7077   if (insn & 0x80000000)
   7078     {
   7079       /* Not 32-bit insn.  */
   7080       return 0;
   7081     }
   7082 
   7083   op6 = N32_OP6 (insn);
   7084 
   7085   /* Convert it to 16-bit instruction.  */
   7086   switch (op6)
   7087     {
   7088     case N32_OP6_MOVI:
   7089       if (IS_WITHIN_S (N32_IMM20S (insn), 5))
   7090 	{
   7091 	  insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
   7092 	  insn_type = NDS32_INSN_MOVI55;
   7093 	}
   7094       else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
   7095 	       && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
   7096 	{
   7097 	  insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
   7098 			       N32_IMM20S (insn) - 16);
   7099 	  insn_type = NDS32_INSN_MOVPI45;
   7100 	}
   7101       break;
   7102 
   7103     case N32_OP6_ADDI:
   7104       if (N32_IMM15S (insn) == 0)
   7105 	{
   7106 	  /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
   7107 	     because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
   7108 	  if (mach <= MACH_V2
   7109 	      || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
   7110 	    {
   7111 	      insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
   7112 	      insn_type = NDS32_INSN_MOV55;
   7113 	    }
   7114 	}
   7115       else if (N32_IMM15S (insn) > 0)
   7116 	{
   7117 	  if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
   7118 	    {
   7119 	      insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
   7120 				    N32_IMM15S (insn));
   7121 	      insn_type = NDS32_INSN_ADDI333;
   7122 	    }
   7123 	  else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
   7124 		   && N32_IMM15S (insn) < 32)
   7125 	    {
   7126 	      insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
   7127 	      insn_type = NDS32_INSN_ADDI45;
   7128 	    }
   7129 	  else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
   7130 		   && N32_RT5 (insn) == N32_RA5 (insn)
   7131 		   && N32_IMM15S (insn) < 512)
   7132 	    {
   7133 	      insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
   7134 	      insn_type = NDS32_INSN_ADDI10_SP;
   7135 	    }
   7136 	  else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
   7137 		   && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
   7138 		   && (N32_IMM15S (insn) % 4 == 0))
   7139 	    {
   7140 	      insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
   7141 				   N32_IMM15S (insn) >> 2);
   7142 	      insn_type = NDS32_INSN_ADDRI36_SP;
   7143 	    }
   7144 	}
   7145       else
   7146 	{
   7147 	  /* Less than 0.  */
   7148 	  if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
   7149 	    {
   7150 	      insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
   7151 				    0 - N32_IMM15S (insn));
   7152 	      insn_type = NDS32_INSN_SUBI333;
   7153 	    }
   7154 	  else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
   7155 		   && N32_IMM15S (insn) > -32)
   7156 	    {
   7157 	      insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
   7158 				   0 - N32_IMM15S (insn));
   7159 	      insn_type = NDS32_INSN_SUBI45;
   7160 	    }
   7161 	  else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
   7162 		   && N32_RT5 (insn) == N32_RA5 (insn)
   7163 		   && N32_IMM15S (insn) >= -512)
   7164 	    {
   7165 	      insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
   7166 	      insn_type = NDS32_INSN_ADDI10_SP;
   7167 	    }
   7168 	}
   7169       break;
   7170 
   7171     case N32_OP6_ORI:
   7172       if (N32_IMM15S (insn) == 0)
   7173 	{
   7174 	  /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
   7175 	     because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
   7176 	  if (mach <= MACH_V2
   7177 	      || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
   7178 	    {
   7179 	      insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
   7180 	      insn_type = NDS32_INSN_MOV55;
   7181 	    }
   7182 	}
   7183       break;
   7184 
   7185     case N32_OP6_SUBRI:
   7186       if (mach >= MACH_V3 && N32_IS_RT3 (insn)
   7187 	  && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
   7188 	{
   7189 	  insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
   7190 	  insn_type = NDS32_INSN_NEG33;
   7191 	}
   7192       break;
   7193 
   7194     case N32_OP6_ANDI:
   7195       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
   7196 	{
   7197 	  if (N32_IMM15U (insn) == 1)
   7198 	    {
   7199 	      insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
   7200 	      insn_type = NDS32_INSN_XLSB33;
   7201 	    }
   7202 	  else if (N32_IMM15U (insn) == 0x7ff)
   7203 	    {
   7204 	      insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
   7205 	      insn_type = NDS32_INSN_X11B33;
   7206 	    }
   7207 	  else if (N32_IMM15U (insn) == 0xff)
   7208 	    {
   7209 	      insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
   7210 	      insn_type = NDS32_INSN_ZEB33;
   7211 	    }
   7212 	  else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
   7213 		   && N32_IMM15U (insn) < 256)
   7214 	    {
   7215 	      int imm15u = N32_IMM15U (insn);
   7216 
   7217 	      if (__builtin_popcount (imm15u) == 1)
   7218 		{
   7219 		  /* BMSKI33 */
   7220 		  int imm3u = __builtin_ctz (imm15u);
   7221 
   7222 		  insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
   7223 		  insn_type = NDS32_INSN_BMSKI33;
   7224 		}
   7225 	      else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
   7226 		{
   7227 		  /* FEXTI33 */
   7228 		  int imm3u = __builtin_ctz (imm15u + 1) - 1;
   7229 
   7230 		  insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
   7231 		  insn_type = NDS32_INSN_FEXTI33;
   7232 		}
   7233 	    }
   7234 	}
   7235       break;
   7236 
   7237     case N32_OP6_SLTI:
   7238       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
   7239 	  && IS_WITHIN_U (N32_IMM15S (insn), 5))
   7240 	{
   7241 	  insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
   7242 	  insn_type = NDS32_INSN_SLTI45;
   7243 	}
   7244       break;
   7245 
   7246     case N32_OP6_SLTSI:
   7247       if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
   7248 	  && IS_WITHIN_U (N32_IMM15S (insn), 5))
   7249 	{
   7250 	  insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
   7251 	  insn_type = NDS32_INSN_SLTSI45;
   7252 	}
   7253       break;
   7254 
   7255     case N32_OP6_LWI:
   7256       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
   7257 	{
   7258 	  insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
   7259 	  insn_type = NDS32_INSN_LWI450;
   7260 	}
   7261       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7262 	       && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7263 	{
   7264 	  insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
   7265 				N32_IMM15S (insn));
   7266 	  insn_type = NDS32_INSN_LWI333;
   7267 	}
   7268       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
   7269 	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
   7270 	{
   7271 	  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
   7272 	  insn_type = NDS32_INSN_LWI37;
   7273 	}
   7274       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
   7275 	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
   7276 	{
   7277 	  insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
   7278 	  insn_type = NDS32_INSN_LWI37_SP;
   7279 	}
   7280       else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
   7281 	       && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
   7282 	{
   7283 	  insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
   7284 			       N32_IMM15S (insn) + 32);
   7285 	  insn_type = NDS32_INSN_LWI45_FE;
   7286 	}
   7287       break;
   7288 
   7289     case N32_OP6_SWI:
   7290       if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
   7291 	{
   7292 	  insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
   7293 	  insn_type = NDS32_INSN_SWI450;
   7294 	}
   7295       else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7296 	       && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7297 	{
   7298 	  insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
   7299 				N32_IMM15S (insn));
   7300 	  insn_type = NDS32_INSN_SWI333;
   7301 	}
   7302       else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
   7303 	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
   7304 	{
   7305 	  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
   7306 	  insn_type = NDS32_INSN_SWI37;
   7307 	}
   7308       else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
   7309 	       && IS_WITHIN_U (N32_IMM15S (insn), 7))
   7310 	{
   7311 	  insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
   7312 	  insn_type = NDS32_INSN_SWI37_SP;
   7313 	}
   7314       break;
   7315 
   7316     case N32_OP6_LWI_BI:
   7317       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7318 	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7319 	{
   7320 	  insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
   7321 				N32_IMM15S (insn));
   7322 	  insn_type = NDS32_INSN_LWI333_BI;
   7323 	}
   7324       break;
   7325 
   7326     case N32_OP6_SWI_BI:
   7327       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7328 	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7329 	{
   7330 	  insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
   7331 				N32_IMM15S (insn));
   7332 	  insn_type = NDS32_INSN_SWI333_BI;
   7333 	}
   7334       break;
   7335 
   7336     case N32_OP6_LHI:
   7337       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7338 	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7339 	{
   7340 	  insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
   7341 				N32_IMM15S (insn));
   7342 	  insn_type = NDS32_INSN_LHI333;
   7343 	}
   7344       break;
   7345 
   7346     case N32_OP6_SHI:
   7347       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7348 	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7349 	{
   7350 	  insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
   7351 				N32_IMM15S (insn));
   7352 	  insn_type = NDS32_INSN_SHI333;
   7353 	}
   7354       break;
   7355 
   7356     case N32_OP6_LBI:
   7357       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7358 	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7359 	{
   7360 	  insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
   7361 				N32_IMM15S (insn));
   7362 	  insn_type = NDS32_INSN_LBI333;
   7363 	}
   7364       break;
   7365 
   7366     case N32_OP6_SBI:
   7367       if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
   7368 	  && IS_WITHIN_U (N32_IMM15S (insn), 3))
   7369 	{
   7370 	  insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
   7371 				N32_IMM15S (insn));
   7372 	  insn_type = NDS32_INSN_SBI333;
   7373 	}
   7374       break;
   7375 
   7376     case N32_OP6_ALU1:
   7377       return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
   7378 
   7379     case N32_OP6_ALU2:
   7380       return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
   7381 
   7382     case N32_OP6_BR1:
   7383       if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
   7384 	goto done;
   7385 
   7386       if ((insn & __BIT (14)) == 0)
   7387 	{
   7388 	  /* N32_BR1_BEQ */
   7389 	  if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
   7390 	      && N32_RT5 (insn) != REG_R5)
   7391 	    insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
   7392 	  else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
   7393 		   && N32_RA5 (insn) != REG_R5)
   7394 	    insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
   7395 	  insn_type = NDS32_INSN_BEQS38;
   7396 	  break;
   7397 	}
   7398       else
   7399 	{
   7400 	  /* N32_BR1_BNE */
   7401 	  if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
   7402 	      && N32_RT5 (insn) != REG_R5)
   7403 	    insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
   7404 	  else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
   7405 		   && N32_RA5 (insn) != REG_R5)
   7406 	    insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
   7407 	  insn_type = NDS32_INSN_BNES38;
   7408 	  break;
   7409 	}
   7410       break;
   7411 
   7412     case N32_OP6_BR2:
   7413       switch (N32_BR2_SUB (insn))
   7414 	{
   7415 	case N32_BR2_BEQZ:
   7416 	  if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
   7417 	    {
   7418 	      insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
   7419 	      insn_type = NDS32_INSN_BEQZ38;
   7420 	    }
   7421 	  else if (N32_RT5 (insn) == REG_R15
   7422 		   && IS_WITHIN_S (N32_IMM16S (insn), 8))
   7423 	    {
   7424 	      insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
   7425 	      insn_type = NDS32_INSN_BEQZS8;
   7426 	    }
   7427 	  break;
   7428 
   7429 	case N32_BR2_BNEZ:
   7430 	  if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
   7431 	    {
   7432 	      insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
   7433 	      insn_type = NDS32_INSN_BNEZ38;
   7434 	    }
   7435 	  else if (N32_RT5 (insn) == REG_R15
   7436 		   && IS_WITHIN_S (N32_IMM16S (insn), 8))
   7437 	    {
   7438 	      insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
   7439 	      insn_type = NDS32_INSN_BNEZS8;
   7440 	    }
   7441 	  break;
   7442 
   7443 	case N32_BR2_IFCALL:
   7444 	  if (IS_WITHIN_U (N32_IMM16S (insn), 9))
   7445 	    {
   7446 	      insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
   7447 	      insn_type = NDS32_INSN_IFCALL9;
   7448 	    }
   7449 	  break;
   7450 	}
   7451       break;
   7452 
   7453     case N32_OP6_JI:
   7454       if ((insn & __BIT (24)) == 0)
   7455 	{
   7456 	  /* N32_JI_J */
   7457 	  if (IS_WITHIN_S (N32_IMM24S (insn), 8))
   7458 	    {
   7459 	      insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
   7460 	      insn_type = NDS32_INSN_J8;
   7461 	    }
   7462 	}
   7463       break;
   7464 
   7465     case N32_OP6_JREG:
   7466       if (__GF (insn, 8, 2) != 0)
   7467 	goto done;
   7468 
   7469       switch (N32_IMMU (insn, 5))
   7470 	{
   7471 	case N32_JREG_JR:
   7472 	  if (N32_JREG_HINT (insn) == 0)
   7473 	    {
   7474 	      /* jr */
   7475 	      insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
   7476 	      insn_type = NDS32_INSN_JR5;
   7477 	    }
   7478 	  else if (N32_JREG_HINT (insn) == 1)
   7479 	    {
   7480 	      /* ret */
   7481 	      insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
   7482 	      insn_type = NDS32_INSN_RET5;
   7483 	    }
   7484 	  else if (N32_JREG_HINT (insn) == 3)
   7485 	    {
   7486 	      /* ifret = mov55 $sp, $sp */
   7487 	      insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
   7488 	      insn_type = NDS32_INSN_IFRET;
   7489 	    }
   7490 	  break;
   7491 
   7492 	case N32_JREG_JRAL:
   7493 	  /* It's convertible when return rt5 is $lp and address
   7494 	     translation is kept.  */
   7495 	  if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
   7496 	    {
   7497 	      insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
   7498 	      insn_type = NDS32_INSN_JRAL5;
   7499 	    }
   7500 	  break;
   7501 	}
   7502       break;
   7503 
   7504     case N32_OP6_MISC:
   7505       if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
   7506 	{
   7507 	  /* For v3, swid above 31 are used for ex9.it.  */
   7508 	  insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
   7509 	  insn_type = NDS32_INSN_BREAK16;
   7510 	}
   7511       break;
   7512 
   7513     default:
   7514       /* This instruction has no 16-bit variant.  */
   7515       goto done;
   7516     }
   7517 
   7518 done:
   7519   /* Bit-15 of insn16 should be set for a valid instruction.  */
   7520   if ((insn16 & 0x8000) == 0)
   7521     return 0;
   7522 
   7523   if (pinsn16)
   7524     *pinsn16 = insn16;
   7525   if (pinsn_type)
   7526     *pinsn_type = insn_type;
   7527   return 1;
   7528 }
   7529 
   7530 static int
   7531 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
   7532 			  Elf_Internal_Rela *reloc)
   7533 {
   7534   uint16_t insn16 = 0;
   7535 
   7536   if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
   7537       || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
   7538     return 0;
   7539 
   7540   if (!N32_IS_RT3 (insn))
   7541     return 0;
   7542 
   7543   switch (N32_OP6 (insn))
   7544     {
   7545     case N32_OP6_LWI:
   7546       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
   7547 	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
   7548       break;
   7549     case N32_OP6_SWI:
   7550       if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
   7551 	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
   7552       break;
   7553     case N32_OP6_HWGP:
   7554       if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
   7555 	break;
   7556 
   7557       if (__GF (insn, 17, 3) == 6)
   7558 	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
   7559       else if (__GF (insn, 17, 3) == 7)
   7560 	insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
   7561       break;
   7562     }
   7563 
   7564   if ((insn16 & 0x8000) == 0)
   7565     return 0;
   7566 
   7567   *pinsn16 = insn16;
   7568   return 1;
   7569 }
   7570 
   7571 /* Convert a 16-bit instruction to 32-bit one.
   7572    INSN16 it the input and PINSN it the point to output.
   7573    Return non-zero on successful.  Otherwise 0 is returned.  */
   7574 
   7575 int
   7576 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
   7577 {
   7578   uint32_t insn = 0xffffffff;
   7579   unsigned long mach = bfd_get_mach (abfd);
   7580 
   7581   /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
   7582 
   7583   switch (__GF (insn16, 9, 6))
   7584     {
   7585     case 0x4:			/* add45 */
   7586       insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
   7587 		       N16_RA5 (insn16));
   7588       goto done;
   7589     case 0x5:			/* sub45 */
   7590       insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
   7591 		       N16_RA5 (insn16));
   7592       goto done;
   7593     case 0x6:			/* addi45 */
   7594       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
   7595 			N16_IMM5U (insn16));
   7596       goto done;
   7597     case 0x7:			/* subi45 */
   7598       insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
   7599 			-N16_IMM5U (insn16));
   7600       goto done;
   7601     case 0x8:			/* srai45 */
   7602       insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
   7603 		       N16_IMM5U (insn16));
   7604       goto done;
   7605     case 0x9:			/* srli45 */
   7606       insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
   7607 		       N16_IMM5U (insn16));
   7608       goto done;
   7609     case 0xa:			/* slli333 */
   7610       insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
   7611 		       N16_IMM3U (insn16));
   7612       goto done;
   7613     case 0xc:			/* add333 */
   7614       insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
   7615 		       N16_RB3 (insn16));
   7616       goto done;
   7617     case 0xd:			/* sub333 */
   7618       insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
   7619 		       N16_RB3 (insn16));
   7620       goto done;
   7621     case 0xe:			/* addi333 */
   7622       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
   7623 			N16_IMM3U (insn16));
   7624       goto done;
   7625     case 0xf:			/* subi333 */
   7626       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
   7627 			-N16_IMM3U (insn16));
   7628       goto done;
   7629     case 0x10:			/* lwi333 */
   7630       insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
   7631 			N16_IMM3U (insn16));
   7632       goto done;
   7633     case 0x12:			/* lhi333 */
   7634       insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
   7635 			N16_IMM3U (insn16));
   7636       goto done;
   7637     case 0x13:			/* lbi333 */
   7638       insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
   7639 			N16_IMM3U (insn16));
   7640       goto done;
   7641     case 0x11:			/* lwi333.bi */
   7642       insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
   7643 			N16_IMM3U (insn16));
   7644       goto done;
   7645     case 0x14:			/* swi333 */
   7646       insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
   7647 			N16_IMM3U (insn16));
   7648       goto done;
   7649     case 0x16:			/* shi333 */
   7650       insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
   7651 			N16_IMM3U (insn16));
   7652       goto done;
   7653     case 0x17:			/* sbi333 */
   7654       insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
   7655 			N16_IMM3U (insn16));
   7656       goto done;
   7657     case 0x15:			/* swi333.bi */
   7658       insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
   7659 			N16_IMM3U (insn16));
   7660       goto done;
   7661     case 0x18:			/* addri36.sp */
   7662       insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
   7663 			N16_IMM6U (insn16) << 2);
   7664       goto done;
   7665     case 0x19:			/* lwi45.fe */
   7666       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
   7667 			(N16_IMM5U (insn16) - 32));
   7668       goto done;
   7669     case 0x1a:			/* lwi450 */
   7670       insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
   7671       goto done;
   7672     case 0x1b:			/* swi450 */
   7673       insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
   7674       goto done;
   7675 
   7676       /* These are r15 implied instructions.  */
   7677     case 0x30:			/* slts45 */
   7678       insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
   7679       goto done;
   7680     case 0x31:			/* slt45 */
   7681       insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
   7682       goto done;
   7683     case 0x32:			/* sltsi45 */
   7684       insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
   7685       goto done;
   7686     case 0x33:			/* slti45 */
   7687       insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
   7688       goto done;
   7689     case 0x34:			/* beqzs8, bnezs8 */
   7690       if (insn16 & __BIT (8))
   7691 	insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
   7692       else
   7693 	insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
   7694       goto done;
   7695 
   7696     case 0x35:			/* break16, ex9.it */
   7697       /* Only consider range of v3 break16.  */
   7698       insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
   7699       goto done;
   7700 
   7701     case 0x3c:			/* ifcall9 */
   7702       insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
   7703       goto done;
   7704     case 0x3d:			/* movpi45 */
   7705       insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
   7706       goto done;
   7707 
   7708     case 0x3f:			/* MISC33 */
   7709       switch (insn16 & 0x7)
   7710 	{
   7711 	case 2:			/* neg33 */
   7712 	  insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
   7713 	  break;
   7714 	case 3:			/* not33 */
   7715 	  insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
   7716 			   N16_RA3 (insn16));
   7717 	  break;
   7718 	case 4:			/* mul33 */
   7719 	  insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
   7720 			   N16_RA3 (insn16));
   7721 	  break;
   7722 	case 5:			/* xor33 */
   7723 	  insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
   7724 			   N16_RA3 (insn16));
   7725 	  break;
   7726 	case 6:			/* and33 */
   7727 	  insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
   7728 			   N16_RA3 (insn16));
   7729 	  break;
   7730 	case 7:			/* or33 */
   7731 	  insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
   7732 			   N16_RA3 (insn16));
   7733 	  break;
   7734 	}
   7735       goto done;
   7736 
   7737     case 0xb:
   7738       switch (insn16 & 0x7)
   7739 	{
   7740 	case 0:			/* zeb33 */
   7741 	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
   7742 	  break;
   7743 	case 1:			/* zeh33 */
   7744 	  insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
   7745 	  break;
   7746 	case 2:			/* seb33 */
   7747 	  insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
   7748 	  break;
   7749 	case 3:			/* seh33 */
   7750 	  insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
   7751 	  break;
   7752 	case 4:			/* xlsb33 */
   7753 	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
   7754 	  break;
   7755 	case 5:			/* x11b33 */
   7756 	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
   7757 	  break;
   7758 	case 6:			/* bmski33 */
   7759 	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
   7760 			    1 << __GF (insn16, 3, 3));
   7761 	  break;
   7762 	case 7:			/* fexti33 */
   7763 	  insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
   7764 			    (1 << (__GF (insn16, 3, 3) + 1)) - 1);
   7765 	  break;
   7766 	}
   7767       goto done;
   7768     }
   7769 
   7770   switch (__GF (insn16, 10, 5))
   7771     {
   7772     case 0x0:			/* mov55 or ifret16 */
   7773       if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
   7774 	  && N16_RT5 (insn16) == N16_RA5 (insn16))
   7775 	insn = N32_JREG (JR, 0, 0, 0, 3);
   7776       else
   7777 	insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
   7778       goto done;
   7779     case 0x1:			/* movi55 */
   7780       insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
   7781       goto done;
   7782     case 0x1b:			/* addi10s (V2) */
   7783       insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
   7784       goto done;
   7785     }
   7786 
   7787   switch (__GF (insn16, 11, 4))
   7788     {
   7789     case 0x7:			/* lwi37.fp/swi37.fp */
   7790       if (insn16 & __BIT (7))	/* swi37.fp */
   7791 	insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
   7792       else			/* lwi37.fp */
   7793 	insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
   7794       goto done;
   7795     case 0x8:			/* beqz38 */
   7796       insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
   7797       goto done;
   7798     case 0x9:			/* bnez38 */
   7799       insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
   7800       goto done;
   7801     case 0xa:			/* beqs38/j8, implied r5 */
   7802       if (N16_RT38 (insn16) == 5)
   7803 	insn = N32_JI (J, N16_IMM8S (insn16));
   7804       else
   7805 	insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
   7806       goto done;
   7807     case 0xb:			/* bnes38 and others */
   7808       if (N16_RT38 (insn16) == 5)
   7809 	{
   7810 	  switch (__GF (insn16, 5, 3))
   7811 	    {
   7812 	    case 0:		/* jr5 */
   7813 	      insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
   7814 	      break;
   7815 	    case 4:		/* ret5 */
   7816 	      insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
   7817 	      break;
   7818 	    case 1:		/* jral5 */
   7819 	      insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
   7820 	      break;
   7821 	    case 2:		/* ex9.it imm5 */
   7822 	      /* ex9.it had no 32-bit variantl.  */
   7823 	      break;
   7824 	    case 5:		/* add5.pc */
   7825 	      /* add5.pc had no 32-bit variantl.  */
   7826 	      break;
   7827 	    }
   7828 	}
   7829       else			/* bnes38 */
   7830 	insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
   7831       goto done;
   7832     case 0xe:			/* lwi37/swi37 */
   7833       if (insn16 & (1 << 7))	/* swi37.sp */
   7834 	insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
   7835       else			/* lwi37.sp */
   7836 	insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
   7837       goto done;
   7838     }
   7839 
   7840 done:
   7841   if (insn & 0x80000000)
   7842     return 0;
   7843 
   7844   if (pinsn)
   7845     *pinsn = insn;
   7846   return 1;
   7847 }
   7848 
   7849 static bfd_boolean
   7851 is_sda_access_insn (unsigned long insn)
   7852 {
   7853   switch (N32_OP6 (insn))
   7854     {
   7855     case N32_OP6_LWI:
   7856     case N32_OP6_LHI:
   7857     case N32_OP6_LHSI:
   7858     case N32_OP6_LBI:
   7859     case N32_OP6_LBSI:
   7860     case N32_OP6_SWI:
   7861     case N32_OP6_SHI:
   7862     case N32_OP6_SBI:
   7863     case N32_OP6_LWC:
   7864     case N32_OP6_LDC:
   7865     case N32_OP6_SWC:
   7866     case N32_OP6_SDC:
   7867       return TRUE;
   7868     default:
   7869       ;
   7870     }
   7871   return FALSE;
   7872 }
   7873 
   7874 static unsigned long
   7875 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
   7876 {
   7877   uint32_t oinsn = 0;
   7878 
   7879   switch (type)
   7880     {
   7881     case R_NDS32_GOT_LO12:
   7882     case R_NDS32_GOTOFF_LO12:
   7883     case R_NDS32_PLTREL_LO12:
   7884     case R_NDS32_PLT_GOTREL_LO12:
   7885     case R_NDS32_LO12S0_RELA:
   7886       switch (N32_OP6 (insn))
   7887 	{
   7888 	case N32_OP6_LBI:
   7889 	  /* lbi.gp */
   7890 	  oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
   7891 	  break;
   7892 	case N32_OP6_LBSI:
   7893 	  /* lbsi.gp */
   7894 	  oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
   7895 	  break;
   7896 	case N32_OP6_SBI:
   7897 	  /* sbi.gp */
   7898 	  oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
   7899 	  break;
   7900 	case N32_OP6_ORI:
   7901 	  /* addi.gp */
   7902 	  oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
   7903 	  break;
   7904 	}
   7905       break;
   7906 
   7907     case R_NDS32_LO12S1_RELA:
   7908       switch (N32_OP6 (insn))
   7909 	{
   7910 	case N32_OP6_LHI:
   7911 	  /* lhi.gp */
   7912 	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
   7913 	  break;
   7914 	case N32_OP6_LHSI:
   7915 	  /* lhsi.gp */
   7916 	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
   7917 	  break;
   7918 	case N32_OP6_SHI:
   7919 	  /* shi.gp */
   7920 	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
   7921 	  break;
   7922 	}
   7923       break;
   7924 
   7925     case R_NDS32_LO12S2_RELA:
   7926       switch (N32_OP6 (insn))
   7927 	{
   7928 	case N32_OP6_LWI:
   7929 	  /* lwi.gp */
   7930 	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
   7931 	  break;
   7932 	case N32_OP6_SWI:
   7933 	  /* swi.gp */
   7934 	  oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
   7935 	  break;
   7936 	}
   7937       break;
   7938 
   7939     case R_NDS32_LO12S2_DP_RELA:
   7940     case R_NDS32_LO12S2_SP_RELA:
   7941       oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
   7942       break;
   7943     }
   7944 
   7945   if (oinsn)
   7946     *pinsn = oinsn;
   7947 
   7948   return oinsn != 0;
   7949 }
   7950 
   7951 /* Linker hasn't found the correct merge section for non-section symbol
   7952    in relax time, this work is left to the function elf_link_input_bfd().
   7953    So for non-section symbol, _bfd_merged_section_offset is also needed
   7954    to find the correct symbol address.  */
   7955 
   7956 static bfd_vma
   7957 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
   7958 			  asection **psec, Elf_Internal_Rela *rel)
   7959 {
   7960   asection *sec = *psec;
   7961   bfd_vma relocation;
   7962 
   7963   relocation = (sec->output_section->vma
   7964 		+ sec->output_offset + sym->st_value);
   7965   if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
   7966     {
   7967       if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
   7968 	rel->r_addend =
   7969 	  _bfd_merged_section_offset (abfd, psec,
   7970 				      elf_section_data (sec)->sec_info,
   7971 				      sym->st_value + rel->r_addend);
   7972       else
   7973 	rel->r_addend =
   7974 	  _bfd_merged_section_offset (abfd, psec,
   7975 				      elf_section_data (sec)->sec_info,
   7976 				      sym->st_value) + rel->r_addend;
   7977 
   7978       if (sec != *psec)
   7979 	{
   7980 	  /* If we have changed the section, and our original section is
   7981 	     marked with SEC_EXCLUDE, it means that the original
   7982 	     SEC_MERGE section has been completely subsumed in some
   7983 	     other SEC_MERGE section.  In this case, we need to leave
   7984 	     some info around for --emit-relocs.  */
   7985 	  if ((sec->flags & SEC_EXCLUDE) != 0)
   7986 	    sec->kept_section = *psec;
   7987 	  sec = *psec;
   7988 	}
   7989       rel->r_addend -= relocation;
   7990       rel->r_addend += sec->output_section->vma + sec->output_offset;
   7991     }
   7992   return relocation;
   7993 }
   7994 
   7995 static bfd_vma
   7996 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
   7997 			  Elf_Internal_Sym *isymbuf,
   7998 			  Elf_Internal_Shdr *symtab_hdr)
   7999 {
   8000   bfd_signed_vma foff;
   8001   bfd_vma symval, addend;
   8002   Elf_Internal_Rela irel_fn;
   8003   Elf_Internal_Sym *isym;
   8004   asection *sym_sec;
   8005 
   8006   /* Get the value of the symbol referred to by the reloc.  */
   8007   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   8008     {
   8009       /* A local symbol.  */
   8010       isym = isymbuf + ELF32_R_SYM (irel->r_info);
   8011 
   8012       if (isym->st_shndx == SHN_UNDEF)
   8013 	sym_sec = bfd_und_section_ptr;
   8014       else if (isym->st_shndx == SHN_ABS)
   8015 	sym_sec = bfd_abs_section_ptr;
   8016       else if (isym->st_shndx == SHN_COMMON)
   8017 	sym_sec = bfd_com_section_ptr;
   8018       else
   8019 	sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   8020       memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
   8021       symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
   8022       addend = irel_fn.r_addend;
   8023     }
   8024   else
   8025     {
   8026       unsigned long indx;
   8027       struct elf_link_hash_entry *h;
   8028 
   8029       /* An external symbol.  */
   8030       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   8031       h = elf_sym_hashes (abfd)[indx];
   8032       BFD_ASSERT (h != NULL);
   8033 
   8034       while (h->root.type == bfd_link_hash_indirect
   8035 	     || h->root.type == bfd_link_hash_warning)
   8036 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8037 
   8038       if (h->root.type != bfd_link_hash_defined
   8039 	  && h->root.type != bfd_link_hash_defweak)
   8040 	/* This appears to be a reference to an undefined
   8041 	   symbol.  Just ignore it--it will be caught by the
   8042 	   regular reloc processing.  */
   8043 	return 0;
   8044 
   8045       if (h->root.u.def.section->flags & SEC_MERGE)
   8046 	{
   8047 	  sym_sec = h->root.u.def.section;
   8048 	  symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
   8049 					       (sym_sec)->sec_info, h->root.u.def.value);
   8050 	  symval = symval + sym_sec->output_section->vma
   8051 		   + sym_sec->output_offset;
   8052 	}
   8053       else
   8054 	symval = (h->root.u.def.value
   8055 		  + h->root.u.def.section->output_section->vma
   8056 		  + h->root.u.def.section->output_offset);
   8057       addend = irel->r_addend;
   8058     }
   8059 
   8060   foff = symval + addend;
   8061 
   8062   return foff;
   8063 }
   8064 
   8065 static bfd_vma
   8066 calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
   8067 			      Elf_Internal_Rela *irel,
   8068 			      Elf_Internal_Shdr *symtab_hdr)
   8069 {
   8070   int symndx;
   8071   bfd_vma *local_got_offsets;
   8072   /* Get the value of the symbol referred to by the reloc.  */
   8073   struct elf_link_hash_entry *h;
   8074   struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);
   8075 
   8076   /* An external symbol.  */
   8077   symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   8078   h = elf_sym_hashes (abfd)[symndx];
   8079   while (h->root.type == bfd_link_hash_indirect
   8080 	 || h->root.type == bfd_link_hash_warning)
   8081     h = (struct elf_link_hash_entry *) h->root.u.i.link;
   8082 
   8083   if (symndx >= 0)
   8084     {
   8085       BFD_ASSERT (h != NULL);
   8086       return htab->sgot->output_section->vma + htab->sgot->output_offset
   8087 	     + h->got.offset;
   8088     }
   8089   else
   8090     {
   8091       local_got_offsets = elf_local_got_offsets (abfd);
   8092       BFD_ASSERT (local_got_offsets != NULL);
   8093       return htab->sgot->output_section->vma + htab->sgot->output_offset
   8094 	     + local_got_offsets[ELF32_R_SYM (irel->r_info)];
   8095     }
   8096 
   8097   /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?).  */
   8098   /* The check of h->root.type is passed.  */
   8099 }
   8100 
   8101 static int
   8102 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
   8103 	      asection *sec, Elf_Internal_Rela *rel)
   8104 {
   8105   bfd_byte *contents;
   8106   unsigned short insn16;
   8107 
   8108   if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
   8109     return FALSE;
   8110   contents = elf_section_data (sec)->this_hdr.contents;
   8111   insn16 = bfd_getb16 (contents + rel->r_offset);
   8112   if (insn16 == NDS32_NOP16)
   8113     return TRUE;
   8114   return FALSE;
   8115 }
   8116 
   8117 /* It checks whether the instruction could be converted to
   8118    16-bit form and returns the converted one.
   8119 
   8120    `internal_relocs' is supposed to be sorted.  */
   8121 
   8122 static int
   8123 is_convert_32_to_16 (bfd *abfd, asection *sec,
   8124 		     Elf_Internal_Rela *reloc,
   8125 		     Elf_Internal_Rela *internal_relocs,
   8126 		     Elf_Internal_Rela *irelend,
   8127 		     uint16_t *insn16)
   8128 {
   8129 #define NORMAL_32_TO_16 (1 << 0)
   8130 #define SPECIAL_32_TO_16 (1 << 1)
   8131   bfd_byte *contents = NULL;
   8132   bfd_signed_vma off;
   8133   bfd_vma mem_addr;
   8134   uint32_t insn = 0;
   8135   Elf_Internal_Rela *pc_rel;
   8136   int pic_ext_target = 0;
   8137   Elf_Internal_Shdr *symtab_hdr;
   8138   Elf_Internal_Sym *isymbuf = NULL;
   8139   int convert_type;
   8140   bfd_vma offset;
   8141 
   8142   if (reloc->r_offset + 4 > sec->size)
   8143     return FALSE;
   8144 
   8145   offset = reloc->r_offset;
   8146 
   8147   if (!nds32_get_section_contents (abfd, sec, &contents))
   8148     return FALSE;
   8149   insn = bfd_getb32 (contents + offset);
   8150 
   8151   if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
   8152     convert_type = NORMAL_32_TO_16;
   8153   else if (special_convert_32_to_16 (insn, insn16, reloc))
   8154     convert_type = SPECIAL_32_TO_16;
   8155   else
   8156     return FALSE;
   8157 
   8158   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8159   if (!nds32_get_local_syms (abfd, sec, &isymbuf))
   8160     return FALSE;
   8161 
   8162   /* Find the first relocation of the same relocation-type,
   8163      so we iteratie them forward.  */
   8164   pc_rel = reloc;
   8165   while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
   8166     pc_rel--;
   8167 
   8168   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
   8169     {
   8170       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
   8171 	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
   8172 	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
   8173 	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
   8174 	{
   8175 	  off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
   8176 				  &pic_ext_target);
   8177 	  if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
   8178 	      || off == 0)
   8179 	    return FALSE;
   8180 	  break;
   8181 	}
   8182       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
   8183 	{
   8184 	  /* movi => movi55  */
   8185 	  mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
   8186 					       symtab_hdr);
   8187 	  /* mem_addr is unsigned, but the value should
   8188 	     be between [-16, 15].  */
   8189 	  if ((mem_addr + 0x10) >> 5)
   8190 	    return FALSE;
   8191 	  break;
   8192 	}
   8193       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
   8194 	       || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
   8195 	{
   8196 	  /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
   8197 	     because it can be relaxed to addi for TLS_LE_ADD.  */
   8198 	  return FALSE;
   8199 	}
   8200       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
   8201 		|| ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
   8202 	       && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
   8203 	       && convert_type == SPECIAL_32_TO_16)
   8204 	{
   8205 	  /* fp-as-gp
   8206 	     We've selected a best fp-base for this access, so we can
   8207 	     always resolve it anyway.  Do nothing.  */
   8208 	  break;
   8209 	}
   8210       else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
   8211 		&& (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
   8212 	       || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
   8213 		   && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
   8214 	       || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
   8215 		   && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
   8216 	{
   8217 	  /* Prevent unresolved addi instruction translate
   8218 	     to addi45 or addi333.  */
   8219 	  return FALSE;
   8220 	}
   8221       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
   8222 	{
   8223 	  off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
   8224 				  &pic_ext_target);
   8225 	  if (off >= ACCURATE_U9BIT_S1 || off <= 0)
   8226 	    return FALSE;
   8227 	  break;
   8228 	}
   8229     }
   8230 
   8231   return TRUE;
   8232 }
   8233 
   8234 static void
   8235 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
   8236 		    Elf_Internal_Rela *reloc,
   8237 		    Elf_Internal_Rela *internal_relocs,
   8238 		    Elf_Internal_Rela *irelend,
   8239 		    unsigned short insn16)
   8240 {
   8241   Elf_Internal_Rela *pc_rel;
   8242   bfd_vma offset;
   8243 
   8244   offset = reloc->r_offset;
   8245   bfd_putb16 (insn16, contents + offset);
   8246   /* Find the first relocation of the same relocation-type,
   8247      so we iteratie them forward.  */
   8248   pc_rel = reloc;
   8249   while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
   8250     pc_rel--;
   8251 
   8252   for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
   8253     {
   8254       if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
   8255 	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
   8256 	  || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
   8257 	{
   8258 	  pc_rel->r_info =
   8259 	    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
   8260 	}
   8261       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
   8262 	pc_rel->r_info =
   8263 	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
   8264       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
   8265 	pc_rel->r_info =
   8266 	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
   8267       else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
   8268 	       || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
   8269 	pc_rel->r_info =
   8270 	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
   8271       else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
   8272 	pc_rel->r_info =
   8273 	  ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
   8274     }
   8275 }
   8276 
   8277 /* Find a relocation of type specified by `reloc_type'
   8278    of the same r_offset with reloc.
   8279    If not found, return irelend.
   8280 
   8281    Assuming relocations are sorted by r_offset,
   8282    we find the relocation from `reloc' backward untill relocs,
   8283    or find it from `reloc' forward untill irelend.  */
   8284 
   8285 static Elf_Internal_Rela *
   8286 find_relocs_at_address (Elf_Internal_Rela *reloc,
   8287 			Elf_Internal_Rela *relocs,
   8288 			Elf_Internal_Rela *irelend,
   8289 			enum elf_nds32_reloc_type reloc_type)
   8290 {
   8291   Elf_Internal_Rela *rel_t;
   8292 
   8293   /* Find backward.  */
   8294   for (rel_t = reloc;
   8295        rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
   8296        rel_t--)
   8297     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
   8298       return rel_t;
   8299 
   8300   /* We didn't find it backward.  Try find it forward.  */
   8301   for (rel_t = reloc;
   8302        rel_t < irelend && rel_t->r_offset == reloc->r_offset;
   8303        rel_t++)
   8304     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
   8305       return rel_t;
   8306 
   8307   return irelend;
   8308 }
   8309 
   8310 /* Find a relocation of specified type and offset.
   8311    `reloc' is just a refence point to find a relocation at specified offset.
   8312    If not found, return irelend.
   8313 
   8314    Assuming relocations are sorted by r_offset,
   8315    we find the relocation from `reloc' backward untill relocs,
   8316    or find it from `reloc' forward untill irelend.  */
   8317 
   8318 static Elf_Internal_Rela *
   8319 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
   8320 			     Elf_Internal_Rela *relocs,
   8321 			     Elf_Internal_Rela *irelend,
   8322 			     unsigned char reloc_type,
   8323 			     bfd_vma offset_p)
   8324 {
   8325   Elf_Internal_Rela *rel_t = NULL;
   8326 
   8327   /* First, we try to find a relocation of offset `offset_p',
   8328      and then we use find_relocs_at_address to find specific type.  */
   8329 
   8330   if (reloc->r_offset > offset_p)
   8331     {
   8332       /* Find backward.  */
   8333       for (rel_t = reloc;
   8334 	   rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
   8335 	/* Do nothing.  */;
   8336     }
   8337   else if (reloc->r_offset < offset_p)
   8338     {
   8339       /* Find forward.  */
   8340       for (rel_t = reloc;
   8341 	   rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
   8342 	/* Do nothing.  */;
   8343     }
   8344   else
   8345     rel_t = reloc;
   8346 
   8347   /* Not found?  */
   8348   if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
   8349     return irelend;
   8350 
   8351   return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
   8352 }
   8353 
   8354 static bfd_boolean
   8355 nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
   8356 			    Elf_Internal_Rela *internal_relocs,
   8357 			    Elf_Internal_Rela *irelend,
   8358 			    unsigned char reloc_type)
   8359 {
   8360   Elf_Internal_Rela *rel_t;
   8361 
   8362   for (rel_t = reloc;
   8363        rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
   8364        rel_t--)
   8365     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
   8366       {
   8367 	if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
   8368 	    && rel_t->r_addend == reloc->r_addend)
   8369 	  continue;
   8370 	return TRUE;
   8371       }
   8372 
   8373   for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
   8374        rel_t++)
   8375     if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
   8376       {
   8377 	if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
   8378 	    && rel_t->r_addend == reloc->r_addend)
   8379 	  continue;
   8380 	return TRUE;
   8381       }
   8382 
   8383   return FALSE;
   8384 }
   8385 
   8386 typedef struct nds32_elf_blank nds32_elf_blank_t;
   8387 struct nds32_elf_blank
   8388 {
   8389   /* Where the blank begins.  */
   8390   bfd_vma offset;
   8391   /* The size of the blank.  */
   8392   bfd_vma size;
   8393   /* The accumulative size before this blank.  */
   8394   bfd_vma total_size;
   8395   nds32_elf_blank_t *next;
   8396   nds32_elf_blank_t *prev;
   8397 };
   8398 
   8399 static nds32_elf_blank_t *blank_free_list = NULL;
   8400 
   8401 static nds32_elf_blank_t *
   8402 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
   8403 {
   8404   nds32_elf_blank_t *blank_t;
   8405 
   8406   if (blank_free_list)
   8407     {
   8408       blank_t = blank_free_list;
   8409       blank_free_list = blank_free_list->next;
   8410     }
   8411   else
   8412     blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
   8413 
   8414   if (blank_t == NULL)
   8415     return NULL;
   8416 
   8417   blank_t->offset = offset_p;
   8418   blank_t->size = size_p;
   8419   blank_t->total_size = 0;
   8420   blank_t->next = NULL;
   8421   blank_t->prev = NULL;
   8422 
   8423   return blank_t;
   8424 }
   8425 
   8426 static void
   8427 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
   8428 {
   8429   if (blank_free_list)
   8430     {
   8431       blank_free_list->prev = blank_p;
   8432       blank_p->next = blank_free_list;
   8433     }
   8434   else
   8435     blank_p->next = NULL;
   8436 
   8437   blank_p->prev = NULL;
   8438   blank_free_list = blank_p;
   8439 }
   8440 
   8441 static void
   8442 clean_nds32_elf_blank (void)
   8443 {
   8444   nds32_elf_blank_t *blank_t;
   8445 
   8446   while (blank_free_list)
   8447     {
   8448       blank_t = blank_free_list;
   8449       blank_free_list = blank_free_list->next;
   8450       free (blank_t);
   8451     }
   8452 }
   8453 
   8454 static nds32_elf_blank_t *
   8455 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
   8456 {
   8457   nds32_elf_blank_t *blank_t;
   8458 
   8459   if (!blank_p)
   8460     return NULL;
   8461   blank_t = blank_p;
   8462 
   8463   while (blank_t && addr < blank_t->offset)
   8464     blank_t = blank_t->prev;
   8465   while (blank_t && blank_t->next && addr >= blank_t->next->offset)
   8466     blank_t = blank_t->next;
   8467 
   8468   return blank_t;
   8469 }
   8470 
   8471 static bfd_vma
   8472 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
   8473 			   int overwrite)
   8474 {
   8475   nds32_elf_blank_t *blank_t;
   8476 
   8477   blank_t = search_nds32_elf_blank (*blank_p, addr);
   8478   if (!blank_t)
   8479     return 0;
   8480 
   8481   if (overwrite)
   8482     *blank_p = blank_t;
   8483 
   8484   if (addr < blank_t->offset + blank_t->size)
   8485     return blank_t->total_size + (addr - blank_t->offset);
   8486   else
   8487     return blank_t->total_size + blank_t->size;
   8488 }
   8489 
   8490 static bfd_boolean
   8491 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
   8492 {
   8493   nds32_elf_blank_t *blank_t, *blank_t2;
   8494 
   8495   if (!*blank_p)
   8496     {
   8497       *blank_p = create_nds32_elf_blank (addr, len);
   8498       return *blank_p ? TRUE : FALSE;
   8499     }
   8500 
   8501   blank_t = search_nds32_elf_blank (*blank_p, addr);
   8502 
   8503   if (blank_t == NULL)
   8504     {
   8505       blank_t = create_nds32_elf_blank (addr, len);
   8506       if (!blank_t)
   8507 	return FALSE;
   8508       while ((*blank_p)->prev != NULL)
   8509 	*blank_p = (*blank_p)->prev;
   8510       blank_t->next = *blank_p;
   8511       (*blank_p)->prev = blank_t;
   8512       (*blank_p) = blank_t;
   8513       return TRUE;
   8514     }
   8515 
   8516   if (addr < blank_t->offset + blank_t->size)
   8517     {
   8518       if (addr > blank_t->offset + blank_t->size)
   8519 	blank_t->size = addr - blank_t->offset;
   8520     }
   8521   else
   8522     {
   8523       blank_t2 = create_nds32_elf_blank (addr, len);
   8524       if (!blank_t2)
   8525 	return FALSE;
   8526       if (blank_t->next)
   8527 	{
   8528 	  blank_t->next->prev = blank_t2;
   8529 	  blank_t2->next = blank_t->next;
   8530 	}
   8531       blank_t2->prev = blank_t;
   8532       blank_t->next = blank_t2;
   8533       *blank_p = blank_t2;
   8534     }
   8535 
   8536   return TRUE;
   8537 }
   8538 
   8539 static bfd_boolean
   8540 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
   8541 				     bfd_vma len)
   8542 {
   8543   nds32_elf_blank_t *blank_t;
   8544 
   8545   if (!insert_nds32_elf_blank (blank_p, addr, len))
   8546     return FALSE;
   8547 
   8548   blank_t = *blank_p;
   8549 
   8550   if (!blank_t->prev)
   8551     {
   8552       blank_t->total_size = 0;
   8553       blank_t = blank_t->next;
   8554     }
   8555 
   8556   while (blank_t)
   8557     {
   8558       blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
   8559       blank_t = blank_t->next;
   8560     }
   8561 
   8562   return TRUE;
   8563 }
   8564 
   8565 static void
   8566 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
   8567 {
   8568   nds32_elf_blank_t *blank_t;
   8569   bfd_vma total_size = 0;
   8570 
   8571   if (!blank_p)
   8572     return;
   8573 
   8574   blank_t = blank_p;
   8575   while (blank_t->prev)
   8576     blank_t = blank_t->prev;
   8577   while (blank_t)
   8578     {
   8579       blank_t->total_size = total_size;
   8580       total_size += blank_t->size;
   8581       blank_t = blank_t->next;
   8582     }
   8583 }
   8584 
   8585 static bfd_boolean
   8586 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
   8587 			       nds32_elf_blank_t *blank_p)
   8588 {
   8589   Elf_Internal_Shdr *symtab_hdr;	/* Symbol table header of this bfd.  */
   8590   Elf_Internal_Sym *isym = NULL;	/* Symbol table of this bfd.  */
   8591   Elf_Internal_Sym *isymend;		/* Symbol entry iterator.  */
   8592   unsigned int sec_shndx;		/* The section the be relaxed.  */
   8593   bfd_byte *contents;			/* Contents data of iterating section.  */
   8594   Elf_Internal_Rela *internal_relocs;
   8595   Elf_Internal_Rela *irel;
   8596   Elf_Internal_Rela *irelend;
   8597   struct elf_link_hash_entry **sym_hashes;
   8598   struct elf_link_hash_entry **end_hashes;
   8599   unsigned int symcount;
   8600   asection *sect;
   8601   nds32_elf_blank_t *blank_t;
   8602   nds32_elf_blank_t *blank_t2;
   8603   nds32_elf_blank_t *blank_head;
   8604 
   8605   blank_head = blank_t = blank_p;
   8606   while (blank_head->prev != NULL)
   8607     blank_head = blank_head->prev;
   8608   while (blank_t->next != NULL)
   8609     blank_t = blank_t->next;
   8610 
   8611   if (blank_t->offset + blank_t->size <= sec->size)
   8612     {
   8613       blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
   8614       blank_t->next->prev = blank_t;
   8615     }
   8616   if (blank_head->offset > 0)
   8617     {
   8618       blank_head->prev = create_nds32_elf_blank (0, 0);
   8619       blank_head->prev->next = blank_head;
   8620       blank_head = blank_head->prev;
   8621     }
   8622 
   8623   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
   8624 
   8625   /* The deletion must stop at the next ALIGN reloc for an alignment
   8626      power larger than the number of bytes we are deleting.  */
   8627 
   8628   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8629   if (!nds32_get_local_syms (abfd, sec, &isym))
   8630     return FALSE;
   8631 
   8632   if (isym == NULL)
   8633     {
   8634       isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   8635 				   symtab_hdr->sh_info, 0, NULL, NULL, NULL);
   8636       symtab_hdr->contents = (bfd_byte *) isym;
   8637     }
   8638 
   8639   if (isym == NULL || symtab_hdr->sh_info == 0)
   8640     return FALSE;
   8641 
   8642   blank_t = blank_head;
   8643   calc_nds32_blank_total (blank_head);
   8644 
   8645   for (sect = abfd->sections; sect != NULL; sect = sect->next)
   8646     {
   8647       /* Adjust all the relocs.  */
   8648 
   8649       /* Relocations MUST be kept in memory, because relaxation adjust them.  */
   8650       internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
   8651 						   TRUE /* keep_memory */);
   8652       irelend = internal_relocs + sect->reloc_count;
   8653 
   8654       blank_t = blank_head;
   8655       blank_t2 = blank_head;
   8656 
   8657       if (!(sect->flags & SEC_RELOC))
   8658 	continue;
   8659 
   8660       nds32_get_section_contents (abfd, sect, &contents);
   8661 
   8662       for (irel = internal_relocs; irel < irelend; irel++)
   8663 	{
   8664 	  bfd_vma raddr;
   8665 
   8666 	  if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
   8667 	      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
   8668 	      && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
   8669 	    {
   8670 	      unsigned long val = 0;
   8671 	      unsigned long mask;
   8672 	      long before, between;
   8673 	      long offset;
   8674 
   8675 	      switch (ELF32_R_TYPE (irel->r_info))
   8676 		{
   8677 		case R_NDS32_DIFF8:
   8678 		  offset = bfd_get_8 (abfd, contents + irel->r_offset);
   8679 		  break;
   8680 		case R_NDS32_DIFF16:
   8681 		  offset = bfd_get_16 (abfd, contents + irel->r_offset);
   8682 		  break;
   8683 		case R_NDS32_DIFF32:
   8684 		  val = bfd_get_32 (abfd, contents + irel->r_offset);
   8685 		  /* Get the signed bit and mask for the high part.  The
   8686 		     gcc will alarm when right shift 32-bit since the
   8687 		     type size of long may be 32-bit.  */
   8688 		  mask = 0 - (val >> 31);
   8689 		  if (mask)
   8690 		    offset = (val | (mask - 0xffffffff));
   8691 		  else
   8692 		    offset = val;
   8693 		  break;
   8694 		default:
   8695 		  BFD_ASSERT (0);
   8696 		}
   8697 
   8698 	      /*		  DIFF value
   8699 		0	     |encoded in location|
   8700 		|------------|-------------------|---------
   8701 			    sym+off(addend)
   8702 		-- before ---| *****************
   8703 		--------------------- between ---|
   8704 
   8705 		We only care how much data are relax between DIFF,
   8706 		marked as ***.  */
   8707 
   8708 	      before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
   8709 	      between = get_nds32_elf_blank_total (&blank_t,
   8710 						   irel->r_addend + offset, 0);
   8711 	      if (between == before)
   8712 		goto done_adjust_diff;
   8713 
   8714 	      switch (ELF32_R_TYPE (irel->r_info))
   8715 		{
   8716 		case R_NDS32_DIFF8:
   8717 		  bfd_put_8 (abfd, offset - (between - before),
   8718 			     contents + irel->r_offset);
   8719 		  break;
   8720 		case R_NDS32_DIFF16:
   8721 		  bfd_put_16 (abfd, offset - (between - before),
   8722 			      contents + irel->r_offset);
   8723 		  break;
   8724 		case R_NDS32_DIFF32:
   8725 		  bfd_put_32 (abfd, offset - (between - before),
   8726 			      contents + irel->r_offset);
   8727 		  break;
   8728 		}
   8729 	    }
   8730 	  else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
   8731 	      && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
   8732 	    {
   8733 	      bfd_vma val = 0;
   8734 	      unsigned int len = 0;
   8735 	      unsigned long before, between;
   8736 	      bfd_byte *endp, *p;
   8737 
   8738 	      val = read_unsigned_leb128 (abfd, contents + irel->r_offset,
   8739 					  &len);
   8740 
   8741 	      before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
   8742 	      between = get_nds32_elf_blank_total (&blank_t,
   8743 						   irel->r_addend + val, 0);
   8744 	      if (between == before)
   8745 		goto done_adjust_diff;
   8746 
   8747 	      p = contents + irel->r_offset;
   8748 	      endp = p + len -1;
   8749 	      memset (p, 0x80, len);
   8750 	      *(endp) = 0;
   8751 	      p = write_uleb128 (p, val - (between - before)) - 1;
   8752 	      if (p < endp)
   8753 		*p |= 0x80;
   8754 	    }
   8755 done_adjust_diff:
   8756 
   8757 	  if (sec == sect)
   8758 	    {
   8759 	      raddr = irel->r_offset;
   8760 	      irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
   8761 							   irel->r_offset, 1);
   8762 
   8763 	      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
   8764 		continue;
   8765 	      if (blank_t2 && blank_t2->next
   8766 		  && (blank_t2->offset > raddr
   8767 		      || blank_t2->next->offset <= raddr))
   8768 		(*_bfd_error_handler)
   8769 		  (_("%B: %s\n"), abfd,
   8770 		   "Error: search_nds32_elf_blank reports wrong node");
   8771 
   8772 	      /* Mark reloc in deleted portion as NONE.
   8773 		 For some relocs like R_NDS32_LABEL that doesn't modify the
   8774 		 content in the section.  R_NDS32_LABEL doesn't belong to the
   8775 		 instruction in the section, so we should preserve it.  */
   8776 	      if (raddr >= blank_t2->offset
   8777 		  && raddr < blank_t2->offset + blank_t2->size
   8778 		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
   8779 		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
   8780 		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
   8781 		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
   8782 		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
   8783 		  && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
   8784 		{
   8785 		  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   8786 					       R_NDS32_NONE);
   8787 		  continue;
   8788 		}
   8789 	    }
   8790 
   8791 	  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
   8792 	      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
   8793 	      || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
   8794 	    continue;
   8795 
   8796 	  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
   8797 	      && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
   8798 	      && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
   8799 	    {
   8800 	      if (irel->r_addend <= sec->size)
   8801 		irel->r_addend -=
   8802 		  get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
   8803 	    }
   8804 	}
   8805     }
   8806 
   8807   /* Adjust the local symbols defined in this section.  */
   8808   blank_t = blank_head;
   8809   for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
   8810     {
   8811       if (isym->st_shndx == sec_shndx)
   8812 	{
   8813 	  if (isym->st_value <= sec->size)
   8814 	    {
   8815 	      bfd_vma ahead;
   8816 	      bfd_vma orig_addr = isym->st_value;
   8817 
   8818 	      ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
   8819 	      isym->st_value -= ahead;
   8820 
   8821 	      /* Adjust function size.  */
   8822 	      if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
   8823 		  && isym->st_size > 0)
   8824 		isym->st_size -=
   8825 		  get_nds32_elf_blank_total
   8826 		  (&blank_t, orig_addr + isym->st_size, 0) - ahead;
   8827 	    }
   8828 	}
   8829     }
   8830 
   8831   /* Now adjust the global symbols defined in this section.  */
   8832   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
   8833 	      - symtab_hdr->sh_info);
   8834   sym_hashes = elf_sym_hashes (abfd);
   8835   end_hashes = sym_hashes + symcount;
   8836   blank_t = blank_head;
   8837   for (; sym_hashes < end_hashes; sym_hashes++)
   8838     {
   8839       struct elf_link_hash_entry *sym_hash = *sym_hashes;
   8840 
   8841       if ((sym_hash->root.type == bfd_link_hash_defined
   8842 	   || sym_hash->root.type == bfd_link_hash_defweak)
   8843 	  && sym_hash->root.u.def.section == sec)
   8844 	{
   8845 	  if (sym_hash->root.u.def.value <= sec->size)
   8846 	    {
   8847 	      bfd_vma ahead;
   8848 	      bfd_vma orig_addr = sym_hash->root.u.def.value;
   8849 
   8850 	      ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
   8851 	      sym_hash->root.u.def.value -= ahead;
   8852 
   8853 	      /* Adjust function size.  */
   8854 	      if (sym_hash->type == STT_FUNC)
   8855 		sym_hash->size -=
   8856 		  get_nds32_elf_blank_total
   8857 		  (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
   8858 
   8859 	    }
   8860 	}
   8861     }
   8862 
   8863   contents = elf_section_data (sec)->this_hdr.contents;
   8864   blank_t = blank_head;
   8865   while (blank_t->next)
   8866     {
   8867       /* Actually delete the bytes.  */
   8868 
   8869       /* If current blank is the last blank overlap with current section,
   8870 	 go to finish process.  */
   8871       if (sec->size <= (blank_t->next->offset))
   8872 	break;
   8873 
   8874       memmove (contents + blank_t->offset - blank_t->total_size,
   8875 	       contents + blank_t->offset + blank_t->size,
   8876 	       blank_t->next->offset - (blank_t->offset + blank_t->size));
   8877 
   8878       blank_t = blank_t->next;
   8879     }
   8880 
   8881   if (sec->size > (blank_t->offset + blank_t->size))
   8882     {
   8883       /* There are remaining code between blank and section boundary.
   8884 	 Move the remaining code to appropriate location.  */
   8885       memmove (contents + blank_t->offset - blank_t->total_size,
   8886 	       contents + blank_t->offset + blank_t->size,
   8887 	       sec->size - (blank_t->offset + blank_t->size));
   8888       sec->size -= blank_t->total_size + blank_t->size;
   8889     }
   8890   else
   8891     /* This blank is not entirely included in the section,
   8892        reduce the section size by only part of the blank size.  */
   8893     sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
   8894 
   8895   while (blank_head)
   8896     {
   8897       blank_t = blank_head;
   8898       blank_head = blank_head->next;
   8899       remove_nds32_elf_blank (blank_t);
   8900     }
   8901 
   8902   return TRUE;
   8903 }
   8904 
   8905 /* Get the contents of a section.  */
   8906 
   8907 static int
   8908 nds32_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents_p)
   8909 {
   8910   /* Get the section contents.  */
   8911   if (elf_section_data (sec)->this_hdr.contents != NULL)
   8912     *contents_p = elf_section_data (sec)->this_hdr.contents;
   8913   else
   8914     {
   8915       if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
   8916 	return FALSE;
   8917       elf_section_data (sec)->this_hdr.contents = *contents_p;
   8918     }
   8919 
   8920   return TRUE;
   8921 }
   8922 
   8923 /* Get the contents of the internal symbol of abfd.  */
   8924 
   8925 static int
   8926 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
   8927 		      Elf_Internal_Sym **isymbuf_p)
   8928 {
   8929   Elf_Internal_Shdr *symtab_hdr;
   8930   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   8931 
   8932   /* Read this BFD's local symbols if we haven't done so already.  */
   8933   if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
   8934     {
   8935       *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
   8936       if (*isymbuf_p == NULL)
   8937 	{
   8938 	  *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
   8939 					     symtab_hdr->sh_info, 0,
   8940 					     NULL, NULL, NULL);
   8941 	  if (*isymbuf_p == NULL)
   8942 	    return FALSE;
   8943 	}
   8944     }
   8945   symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
   8946 
   8947   return TRUE;
   8948 }
   8949 
   8950 /* Range of small data.  */
   8951 static bfd_vma sdata_range[2][2];
   8952 static bfd_vma const sdata_init_range[2] =
   8953 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
   8954 
   8955 static int
   8956 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
   8957 		     bfd_byte *contents, bfd_vma addr)
   8958 {
   8959   unsigned long insn = bfd_getb32 (contents + addr);
   8960 
   8961   if (insn & 0x80000000)
   8962     return 2;
   8963 
   8964   return 4;
   8965 }
   8966 
   8967 /* Set the gp relax range.  We have to measure the safe range
   8968    to do gp relaxation.  */
   8969 
   8970 static void
   8971 relax_range_measurement (bfd *abfd)
   8972 {
   8973   asection *sec_f, *sec_b;
   8974   /* For upper bound.   */
   8975   bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
   8976   bfd_vma align;
   8977   static int decide_relax_range = 0;
   8978   int i;
   8979   int range_number = sizeof (sdata_init_range) / sizeof (sdata_init_range[0]);
   8980 
   8981   if (decide_relax_range)
   8982     return;
   8983   decide_relax_range = 1;
   8984 
   8985   if (sda_rela_sec == NULL)
   8986     {
   8987       /* Since there is no data sections, we assume the range is page size.  */
   8988       for (i = 0; i < range_number; i++)
   8989 	{
   8990 	  sdata_range[i][0] = sdata_init_range[i] - 0x1000;
   8991 	  sdata_range[i][1] = sdata_init_range[i] - 0x1000;
   8992 	}
   8993       return;
   8994     }
   8995 
   8996   /* Get the biggest alignment power after the gp located section.  */
   8997   sec_f = sda_rela_sec->output_section;
   8998   sec_b = sec_f->next;
   8999   align = 0;
   9000   while (sec_b != NULL)
   9001     {
   9002       if ((unsigned)(1 << sec_b->alignment_power) > align)
   9003 	align = (1 << sec_b->alignment_power);
   9004       sec_b = sec_b->next;
   9005     }
   9006 
   9007   /* I guess we can not determine the section before
   9008      gp located section, so we assume the align is max page size.  */
   9009   for (i = 0; i < range_number; i++)
   9010     {
   9011       sdata_range[i][1] = sdata_init_range[i] - align;
   9012       BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
   9013       sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
   9014       BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
   9015     }
   9016 }
   9017 
   9018 /* These are macros used to check flags encoded in r_addend.
   9019    They are only used by nds32_elf_relax_section ().  */
   9020 #define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
   9021 #define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
   9022 #define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
   9023 #define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
   9024 
   9025 /* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
   9026 
   9027 static bfd_boolean
   9028 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   9029 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   9030 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   9031 			   Elf_Internal_Shdr *symtab_hdr)
   9032 {
   9033   /* There are 3 variations for LONGCALL1
   9034      case 4-4-2; 16-bit on, optimize off or optimize for space
   9035      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
   9036      ori   ta, ta, lo12(symbol) ; LO12S0
   9037      jral5 ta                   ;
   9038 
   9039      case 4-4-4; 16-bit off, optimize don't care
   9040      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
   9041      ori   ta, ta, lo12(symbol) ; LO12S0
   9042      jral  ta                   ;
   9043 
   9044      case 4-4-4; 16-bit on, optimize for speed
   9045      sethi ta, hi20(symbol)     ; LONGCALL1/HI20
   9046      ori   ta, ta, lo12(symbol) ; LO12S0
   9047      jral  ta                   ;
   9048      Check code for -mlong-calls output.  */
   9049 
   9050   /* Get the reloc for the address from which the register is
   9051      being loaded.  This reloc will tell us which function is
   9052      actually being called.  */
   9053 
   9054   bfd_vma laddr;
   9055   int seq_len;	/* Original length of instruction sequence.  */
   9056   uint32_t insn;
   9057   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
   9058   int pic_ext_target = 0;
   9059   bfd_signed_vma foff;
   9060   uint16_t insn16;
   9061 
   9062   irelend = internal_relocs + sec->reloc_count;
   9063   seq_len = GET_SEQ_LEN (irel->r_addend);
   9064   laddr = irel->r_offset;
   9065   *insn_len = seq_len;
   9066 
   9067   hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9068 					   R_NDS32_HI20_RELA, laddr);
   9069   lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9070 					   R_NDS32_LO12S0_ORI_RELA,
   9071 					   laddr + 4);
   9072 
   9073   if (hi_irelfn == irelend || lo_irelfn == irelend)
   9074     {
   9075       (*_bfd_error_handler)
   9076 	("%B: warning: R_NDS32_LONGCALL1 points to unrecognized"
   9077 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   9078       return FALSE;
   9079     }
   9080 
   9081   /* Get the value of the symbol referred to by the reloc.  */
   9082   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
   9083 			   &pic_ext_target);
   9084 
   9085   /* This condition only happened when symbol is undefined.  */
   9086   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
   9087       || foff >= CONSERVATIVE_24BIT_S1)
   9088     return FALSE;
   9089 
   9090   /* Relax to: jal symbol; 25_PCREL */
   9091   /* For simplicity of coding, we are going to modify the section
   9092      contents, the section relocs, and the BFD symbol table.  We
   9093      must tell the rest of the code not to free up this
   9094      information.  It would be possible to instead create a table
   9095      of changes which have to be made, as is done in coff-mips.c;
   9096      that would be more work, but would require less memory when
   9097      the linker is run.  */
   9098 
   9099   /* Replace the long call with a jal.  */
   9100   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
   9101 			       R_NDS32_25_PCREL_RELA);
   9102   irel->r_addend = hi_irelfn->r_addend;
   9103 
   9104   /* We don't resolve this here but resolve it in relocate_section.  */
   9105   insn = INSN_JAL;
   9106   bfd_putb32 (insn, contents + irel->r_offset);
   9107 
   9108   hi_irelfn->r_info =
   9109     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
   9110   lo_irelfn->r_info =
   9111     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
   9112   *insn_len = 4;
   9113 
   9114   if (seq_len & 0x2)
   9115     {
   9116       insn16 = NDS32_NOP16;
   9117       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
   9118       lo_irelfn->r_info =
   9119 	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
   9120       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   9121       *insn_len += 2;
   9122     }
   9123   return TRUE;
   9124 }
   9125 
   9126 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
   9127 /* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
   9128 
   9129 static bfd_boolean
   9130 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   9131 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   9132 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   9133 			   Elf_Internal_Shdr *symtab_hdr)
   9134 {
   9135   /* bltz  rt, .L1   ; LONGCALL2
   9136      jal   symbol   ; 25_PCREL
   9137      .L1: */
   9138 
   9139   /* Get the reloc for the address from which the register is
   9140      being loaded.  This reloc will tell us which function is
   9141      actually being called.  */
   9142 
   9143   bfd_vma laddr;
   9144   uint32_t insn;
   9145   Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
   9146   int pic_ext_target = 0;
   9147   bfd_signed_vma foff;
   9148 
   9149   irelend = internal_relocs + sec->reloc_count;
   9150   laddr = irel->r_offset;
   9151   i1_irelfn =
   9152     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9153 				 R_NDS32_25_PCREL_RELA, laddr + 4);
   9154 
   9155   if (i1_irelfn == irelend)
   9156     {
   9157       (*_bfd_error_handler)
   9158 	("%B: warning: R_NDS32_LONGCALL2 points to unrecognized"
   9159 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   9160       return FALSE;
   9161     }
   9162 
   9163   insn = bfd_getb32 (contents + laddr);
   9164 
   9165   /* Get the value of the symbol referred to by the reloc.  */
   9166   foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
   9167 			   &pic_ext_target);
   9168 
   9169   if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
   9170       || foff >= CONSERVATIVE_16BIT_S1)
   9171     return FALSE;
   9172 
   9173   /* Relax to	bgezal   rt, label ; 17_PCREL
   9174      or		bltzal   rt, label ; 17_PCREL */
   9175 
   9176   /* Convert to complimentary conditional call.  */
   9177   insn = CONVERT_CONDITION_CALL (insn);
   9178 
   9179   /* For simplicity of coding, we are going to modify the section
   9180      contents, the section relocs, and the BFD symbol table.  We
   9181      must tell the rest of the code not to free up this
   9182      information.  It would be possible to instead create a table
   9183      of changes which have to be made, as is done in coff-mips.c;
   9184      that would be more work, but would require less memory when
   9185      the linker is run.  */
   9186 
   9187   /* Clean unnessary relocations.  */
   9188   i1_irelfn->r_info =
   9189     ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
   9190   cond_irelfn =
   9191     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9192 				 R_NDS32_17_PCREL_RELA, laddr);
   9193   if (cond_irelfn != irelend)
   9194     cond_irelfn->r_info =
   9195       ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
   9196 
   9197   /* Replace the long call with a bgezal.  */
   9198   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
   9199 			       R_NDS32_17_PCREL_RELA);
   9200   irel->r_addend = i1_irelfn->r_addend;
   9201 
   9202   bfd_putb32 (insn, contents + irel->r_offset);
   9203 
   9204   *insn_len = 4;
   9205   return TRUE;
   9206 }
   9207 
   9208 /* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
   9209 
   9210 static bfd_boolean
   9211 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   9212 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   9213 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   9214 			   Elf_Internal_Shdr *symtab_hdr)
   9215 {
   9216   /* There are 3 variations for LONGCALL3
   9217      case 4-4-4-2; 16-bit on, optimize off or optimize for space
   9218      bltz  rt,   $1                ; LONGCALL3
   9219      sethi ta,   hi20(symbol)      ; HI20
   9220      ori   ta, ta,  lo12(symbol)   ; LO12S0
   9221      jral5 ta                      ;
   9222      $1
   9223 
   9224      case 4-4-4-4; 16-bit off, optimize don't care
   9225      bltz  rt,   $1                ; LONGCALL3
   9226      sethi ta,   hi20(symbol)      ; HI20
   9227      ori   ta, ta,  lo12(symbol)   ; LO12S0
   9228      jral  ta                      ;
   9229      $1
   9230 
   9231      case 4-4-4-4; 16-bit on, optimize for speed
   9232      bltz  rt,   $1                ; LONGCALL3
   9233      sethi ta,   hi20(symbol)      ; HI20
   9234      ori   ta, ta,  lo12(symbol)   ; LO12S0
   9235      jral  ta                      ;
   9236      $1 */
   9237 
   9238   /* Get the reloc for the address from which the register is
   9239      being loaded.  This reloc will tell us which function is
   9240      actually being called.  */
   9241 
   9242   bfd_vma laddr;
   9243   int seq_len;	/* Original length of instruction sequence.  */
   9244   uint32_t insn;
   9245   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
   9246   int pic_ext_target = 0;
   9247   bfd_signed_vma foff;
   9248   uint16_t insn16;
   9249 
   9250   irelend = internal_relocs + sec->reloc_count;
   9251   seq_len = GET_SEQ_LEN (irel->r_addend);
   9252   laddr = irel->r_offset;
   9253   *insn_len = seq_len;
   9254 
   9255   hi_irelfn =
   9256     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9257 				 R_NDS32_HI20_RELA, laddr + 4);
   9258   lo_irelfn =
   9259     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9260 				 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
   9261 
   9262   if (hi_irelfn == irelend || lo_irelfn == irelend)
   9263     {
   9264       (*_bfd_error_handler)
   9265 	("%B: warning: R_NDS32_LONGCALL3 points to unrecognized"
   9266 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   9267       return FALSE;
   9268     }
   9269 
   9270   /* Get the value of the symbol referred to by the reloc.  */
   9271   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
   9272 			   &pic_ext_target);
   9273 
   9274   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
   9275       || foff >= CONSERVATIVE_24BIT_S1)
   9276     return FALSE;
   9277 
   9278   insn = bfd_getb32 (contents + laddr);
   9279   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
   9280     {
   9281       /* Relax to  bgezal   rt, label ; 17_PCREL
   9282 	 or	   bltzal   rt, label ; 17_PCREL */
   9283 
   9284       /* Convert to complimentary conditional call.  */
   9285       insn = CONVERT_CONDITION_CALL (insn);
   9286       bfd_putb32 (insn, contents + irel->r_offset);
   9287 
   9288       *insn_len = 4;
   9289       irel->r_info =
   9290 	ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
   9291       hi_irelfn->r_info =
   9292 	ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
   9293       lo_irelfn->r_info =
   9294 	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
   9295 
   9296       cond_irelfn =
   9297 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9298 				     R_NDS32_17_PCREL_RELA, laddr);
   9299       if (cond_irelfn != irelend)
   9300 	{
   9301 	  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
   9302 					      R_NDS32_17_PCREL_RELA);
   9303 	  cond_irelfn->r_addend = hi_irelfn->r_addend;
   9304 	}
   9305 
   9306       if (seq_len & 0x2)
   9307 	{
   9308 	  insn16 = NDS32_NOP16;
   9309 	  bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
   9310 	  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
   9311 					    R_NDS32_INSN16);
   9312 	  hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   9313 	  insn_len += 2;
   9314 	}
   9315     }
   9316   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
   9317     {
   9318       /* Relax to the following instruction sequence
   9319 	 bltz  rt,   $1 ; LONGCALL2
   9320 	 jal   symbol   ; 25_PCREL
   9321 	 $1	*/
   9322       *insn_len = 8;
   9323       insn = INSN_JAL;
   9324       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
   9325 
   9326       hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
   9327 					R_NDS32_25_PCREL_RELA);
   9328       irel->r_info =
   9329 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
   9330 
   9331       lo_irelfn->r_info =
   9332 	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
   9333 
   9334       if (seq_len & 0x2)
   9335 	{
   9336 	  insn16 = NDS32_NOP16;
   9337 	  bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
   9338 	  lo_irelfn->r_info =
   9339 	    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
   9340 	  lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   9341 	  insn_len += 2;
   9342 	}
   9343     }
   9344   return TRUE;
   9345 }
   9346 
   9347 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
   9348 
   9349 static bfd_boolean
   9350 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   9351 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   9352 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   9353 			   Elf_Internal_Shdr *symtab_hdr)
   9354 {
   9355   /* There are 3 variations for LONGJUMP1
   9356      case 4-4-2; 16-bit bit on, optimize off or optimize for space
   9357      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
   9358      ori   ta, ta, lo12(symbol)  ; LO12S0
   9359      jr5   ta                    ;
   9360 
   9361      case 4-4-4; 16-bit off, optimize don't care
   9362      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
   9363      ori   ta, ta, lo12(symbol)  ; LO12S0
   9364      jr    ta                    ;
   9365 
   9366      case 4-4-4; 16-bit on, optimize for speed
   9367      sethi ta, hi20(symbol)      ; LONGJUMP1/HI20
   9368      ori   ta, ta, lo12(symbol)  ; LO12S0
   9369      jr    ta                    ;	*/
   9370 
   9371   /* Get the reloc for the address from which the register is
   9372      being loaded.  This reloc will tell us which function is
   9373      actually being called.  */
   9374 
   9375   bfd_vma laddr;
   9376   int seq_len;	/* Original length of instruction sequence.  */
   9377   int insn16_on;	/* 16-bit on/off.  */
   9378   uint32_t insn;
   9379   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
   9380   int pic_ext_target = 0;
   9381   bfd_signed_vma foff;
   9382   uint16_t insn16;
   9383   unsigned long reloc;
   9384 
   9385   irelend = internal_relocs + sec->reloc_count;
   9386   seq_len = GET_SEQ_LEN (irel->r_addend);
   9387   laddr = irel->r_offset;
   9388   *insn_len = seq_len;
   9389   insn16_on = IS_16BIT_ON (irel->r_addend);
   9390 
   9391   hi_irelfn =
   9392     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9393 				 R_NDS32_HI20_RELA, laddr);
   9394   lo_irelfn =
   9395     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9396 				 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
   9397   if (hi_irelfn == irelend || lo_irelfn == irelend)
   9398     {
   9399       (*_bfd_error_handler)
   9400 	("%B: warning: R_NDS32_LONGJUMP1 points to unrecognized"
   9401 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   9402       return FALSE;
   9403     }
   9404 
   9405   /* Get the value of the symbol referred to by the reloc.  */
   9406   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
   9407 			   &pic_ext_target);
   9408 
   9409   if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
   9410       || foff < -CONSERVATIVE_24BIT_S1)
   9411     return FALSE;
   9412 
   9413   if (insn16_on && foff >= -ACCURATE_8BIT_S1
   9414       && foff < ACCURATE_8BIT_S1 && (seq_len & 0x2))
   9415     {
   9416       /* j8	label */
   9417       /* 16-bit on, but not optimized for speed.  */
   9418       reloc = R_NDS32_9_PCREL_RELA;
   9419       insn16 = INSN_J8;
   9420       bfd_putb16 (insn16, contents + irel->r_offset);
   9421       *insn_len = 2;
   9422       irel->r_info =
   9423 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   9424     }
   9425   else
   9426     {
   9427       /* j     label */
   9428       reloc = R_NDS32_25_PCREL_RELA;
   9429       insn = INSN_J;
   9430       bfd_putb32 (insn, contents + irel->r_offset);
   9431       *insn_len = 4;
   9432       irel->r_info =
   9433 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
   9434       irel->r_addend = 0;
   9435     }
   9436 
   9437   hi_irelfn->r_info =
   9438     ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
   9439   lo_irelfn->r_info =
   9440     ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
   9441 
   9442   if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
   9443     {
   9444       insn16 = NDS32_NOP16;
   9445       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
   9446       lo_irelfn->r_info =
   9447 	ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
   9448 		      R_NDS32_INSN16);
   9449       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   9450       *insn_len += 2;
   9451     }
   9452   return TRUE;
   9453 }
   9454 
   9455 /* Revert condition branch.  This function does not check if the input
   9456    instruction is condition branch or not.  */
   9457 
   9458 static void
   9459 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
   9460 			   uint16_t *re_insn16, uint32_t *re_insn)
   9461 {
   9462   uint32_t comp_insn = 0;
   9463   uint16_t comp_insn16 = 0;
   9464 
   9465   if (insn)
   9466     {
   9467       if (N32_OP6 (insn) == N32_OP6_BR1)
   9468 	{
   9469 	  /* beqs label.  */
   9470 	  comp_insn = (insn ^ 0x4000) & 0xffffc000;
   9471 	  if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
   9472 	    {
   9473 	      /* Insn can be contracted to 16-bit implied r5.  */
   9474 	      comp_insn16 =
   9475 		(comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
   9476 	      comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
   9477 	    }
   9478 	}
   9479       else if (N32_OP6 (insn) == N32_OP6_BR3)
   9480 	{
   9481 	  /* bnec $ta, imm11, label.  */
   9482 	  comp_insn = (insn ^ 0x80000) & 0xffffff00;
   9483 	}
   9484       else
   9485 	{
   9486 	  comp_insn = (insn ^ 0x10000) & 0xffffc000;
   9487 	  if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
   9488 	      || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
   9489 	    {
   9490 	      if (N32_IS_RT3 (insn))
   9491 		{
   9492 		  /* Insn can be contracted to 16-bit.  */
   9493 		  comp_insn16 =
   9494 		    (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
   9495 		  comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
   9496 		}
   9497 	      else if (N32_RT5 (insn) == REG_R15)
   9498 		{
   9499 		  /* Insn can be contracted to 16-bit.  */
   9500 		  comp_insn16 =
   9501 		    (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
   9502 		}
   9503 	    }
   9504 	}
   9505     }
   9506   else
   9507     {
   9508       switch ((insn16 & 0xf000) >> 12)
   9509 	{
   9510 	case 0xc:
   9511 	  /* beqz38 or bnez38 */
   9512 	  comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
   9513 	  comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
   9514 	  comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
   9515 	  break;
   9516 
   9517 	case 0xd:
   9518 	  /* beqs38 or bnes38 */
   9519 	  comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
   9520 	  comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
   9521 	  comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
   9522 	    | (REG_R5 << 15);
   9523 	  break;
   9524 
   9525 	case 0xe:
   9526 	  /* beqzS8 or bnezS8 */
   9527 	  comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
   9528 	  comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
   9529 	  comp_insn |= REG_R15 << 20;
   9530 	  break;
   9531 
   9532 	default:
   9533 	  break;
   9534 	}
   9535     }
   9536   if (comp_insn && re_insn)
   9537     *re_insn = comp_insn;
   9538   if (comp_insn16 && re_insn16)
   9539     *re_insn16 = comp_insn16;
   9540 }
   9541 
   9542 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
   9543 
   9544 static bfd_boolean
   9545 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   9546 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   9547 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   9548 			   Elf_Internal_Shdr *symtab_hdr)
   9549 {
   9550   /* There are 3 variations for LONGJUMP2
   9551      case 2-4;  1st insn convertible, 16-bit on,
   9552      optimize off or optimize for space
   9553      bnes38  rt, ra, $1 ; LONGJUMP2
   9554      j       label      ; 25_PCREL
   9555      $1:
   9556 
   9557      case 4-4; 1st insn not convertible
   9558      bne  rt, ra, $1 ; LONGJUMP2
   9559      j    label      ; 25_PCREL
   9560      $1:
   9561 
   9562      case 4-4; 1st insn convertible, 16-bit on, optimize for speed
   9563      bne  rt, ra, $1 ; LONGJUMP2
   9564      j    label      ; 25_PCREL
   9565      $1: */
   9566 
   9567   /* Get the reloc for the address from which the register is
   9568      being loaded.  This reloc will tell us which function is
   9569      actually being called.  */
   9570 
   9571   bfd_vma laddr;
   9572   int seq_len;	/* Original length of instruction sequence.  */
   9573   Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
   9574   int pic_ext_target = 0, first_size;
   9575   unsigned int i;
   9576   bfd_signed_vma foff;
   9577   uint32_t insn, re_insn = 0;
   9578   uint16_t insn16, re_insn16 = 0;
   9579   unsigned long reloc, cond_reloc;
   9580 
   9581   enum elf_nds32_reloc_type checked_types[] =
   9582     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
   9583 
   9584   irelend = internal_relocs + sec->reloc_count;
   9585   seq_len = GET_SEQ_LEN (irel->r_addend);
   9586   laddr = irel->r_offset;
   9587   *insn_len = seq_len;
   9588   first_size = (seq_len == 6) ? 2 : 4;
   9589 
   9590   i2_irelfn =
   9591     find_relocs_at_address_addr (irel, internal_relocs,
   9592 				 irelend, R_NDS32_25_PCREL_RELA,
   9593 				 laddr + first_size);
   9594 
   9595   for (i = 0; i < sizeof (checked_types) / sizeof(checked_types[0]); i++)
   9596     {
   9597       cond_irelfn =
   9598 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9599 				     checked_types[i], laddr);
   9600       if (cond_irelfn != irelend)
   9601 	break;
   9602     }
   9603 
   9604   if (i2_irelfn == irelend || cond_irelfn == irelend)
   9605     {
   9606       (*_bfd_error_handler)
   9607 	("%B: warning: R_NDS32_LONGJUMP2 points to unrecognized"
   9608 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   9609       return FALSE;
   9610     }
   9611 
   9612   /* Get the value of the symbol referred to by the reloc.  */
   9613   foff =
   9614     calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
   9615 		      &pic_ext_target);
   9616   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
   9617       || foff >= CONSERVATIVE_16BIT_S1)
   9618     return FALSE;
   9619 
   9620   /* Get the all corresponding instructions.  */
   9621   if (first_size == 4)
   9622     {
   9623       insn = bfd_getb32 (contents + laddr);
   9624       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
   9625     }
   9626   else
   9627     {
   9628       insn16 = bfd_getb16 (contents + laddr);
   9629       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
   9630     }
   9631 
   9632   if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
   9633       && foff < ACCURATE_8BIT_S1 - first_size)
   9634     {
   9635       if (first_size == 4)
   9636 	{
   9637 	  /* Don't convert it to 16-bit now, keep this as relaxable for
   9638 	     ``label reloc; INSN16''.  */
   9639 
   9640 	  /* Save comp_insn32 to buffer.  */
   9641 	  bfd_putb32 (re_insn, contents + irel->r_offset);
   9642 	  *insn_len = 4;
   9643 	  reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
   9644 	    R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
   9645 	  cond_reloc = R_NDS32_INSN16;
   9646 	}
   9647       else
   9648 	{
   9649 	  bfd_putb16 (re_insn16, contents + irel->r_offset);
   9650 	  *insn_len = 2;
   9651 	  reloc = R_NDS32_9_PCREL_RELA;
   9652 	  cond_reloc = R_NDS32_NONE;
   9653 	}
   9654     }
   9655   else if (N32_OP6 (re_insn) == N32_OP6_BR1
   9656 	   && (foff >= -(ACCURATE_14BIT_S1 - first_size)
   9657 	       && foff < ACCURATE_14BIT_S1 - first_size))
   9658     {
   9659       /* beqs     label    ; 15_PCREL */
   9660       bfd_putb32 (re_insn, contents + irel->r_offset);
   9661       *insn_len = 4;
   9662       reloc = R_NDS32_15_PCREL_RELA;
   9663       cond_reloc = R_NDS32_NONE;
   9664     }
   9665   else if (N32_OP6 (re_insn) == N32_OP6_BR2
   9666 	   && foff >= -CONSERVATIVE_16BIT_S1
   9667 	   && foff < CONSERVATIVE_16BIT_S1)
   9668     {
   9669       /* beqz     label ; 17_PCREL */
   9670       bfd_putb32 (re_insn, contents + irel->r_offset);
   9671       *insn_len = 4;
   9672       reloc = R_NDS32_17_PCREL_RELA;
   9673       cond_reloc = R_NDS32_NONE;
   9674     }
   9675   else
   9676     return FALSE;
   9677 
   9678   /* Set all relocations.  */
   9679   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
   9680   irel->r_addend = i2_irelfn->r_addend;
   9681 
   9682   cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
   9683 				      cond_reloc);
   9684   cond_irelfn->r_addend = 0;
   9685 
   9686   if ((seq_len ^ *insn_len ) & 0x2)
   9687     {
   9688       insn16 = NDS32_NOP16;
   9689       bfd_putb16 (insn16, contents + irel->r_offset + 4);
   9690       i2_irelfn->r_offset = 4;
   9691       i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
   9692 					R_NDS32_INSN16);
   9693       i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   9694       *insn_len += 2;
   9695     }
   9696   else
   9697     i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
   9698 				      R_NDS32_NONE);
   9699   return TRUE;
   9700 }
   9701 
   9702 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
   9703 
   9704 static bfd_boolean
   9705 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   9706 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   9707 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   9708 			   Elf_Internal_Shdr *symtab_hdr)
   9709 {
   9710   /* There are 5 variations for LONGJUMP3
   9711      case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
   9712      optimize off or optimize for space
   9713      bnes38   rt, ra, $1            ; LONGJUMP3
   9714      sethi    ta, hi20(symbol)      ; HI20
   9715      ori      ta, ta, lo12(symbol)  ; LO12S0
   9716      jr5      ta                    ;
   9717      $1:                            ;
   9718 
   9719      case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
   9720      bnes38   rt, ra, $1           ; LONGJUMP3
   9721      sethi    ta, hi20(symbol)     ; HI20
   9722      ori      ta, ta, lo12(symbol) ; LO12S0
   9723      jr5      ta                   ;
   9724      $1:                           ; LABEL
   9725 
   9726      case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
   9727      optimize off or optimize for space
   9728      bne   rt, ra, $1           ; LONGJUMP3
   9729      sethi ta, hi20(symbol)     ; HI20
   9730      ori   ta, ta, lo12(symbol) ; LO12S0
   9731      jr5   ta                   ;
   9732      $1:                        ;
   9733 
   9734      case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
   9735      16-bit off if no INSN16
   9736      bne   rt, ra, $1           ; LONGJUMP3
   9737      sethi ta, hi20(symbol)     ; HI20
   9738      ori   ta, ta, lo12(symbol) ; LO12S0
   9739      jr    ta                   ;
   9740      $1:                        ;
   9741 
   9742      case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
   9743      16-bit off if no INSN16
   9744      bne   rt, ra, $1           ; LONGJUMP3
   9745      sethi ta, hi20(symbol)     ; HI20
   9746      ori   ta, ta, lo12(symbol) ; LO12S0
   9747      jr    ta                   ;
   9748      $1:                        ; LABEL */
   9749 
   9750   /* Get the reloc for the address from which the register is
   9751      being loaded.  This reloc will tell us which function is
   9752      actually being called.  */
   9753   enum elf_nds32_reloc_type checked_types[] =
   9754     { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
   9755 
   9756   int reloc_off = 0, cond_removed = 0, convertible;
   9757   bfd_vma laddr;
   9758   int seq_len;	/* Original length of instruction sequence.  */
   9759   Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
   9760   int pic_ext_target = 0, first_size;
   9761   unsigned int i;
   9762   bfd_signed_vma foff;
   9763   uint32_t insn, re_insn = 0;
   9764   uint16_t insn16, re_insn16 = 0;
   9765   unsigned long reloc, cond_reloc;
   9766 
   9767   irelend = internal_relocs + sec->reloc_count;
   9768   seq_len = GET_SEQ_LEN (irel->r_addend);
   9769   laddr = irel->r_offset;
   9770   *insn_len = seq_len;
   9771 
   9772   convertible = IS_1ST_CONVERT (irel->r_addend);
   9773 
   9774   if (convertible)
   9775     first_size = 2;
   9776   else
   9777     first_size = 4;
   9778 
   9779   /* Get all needed relocations.  */
   9780   hi_irelfn =
   9781     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9782 				 R_NDS32_HI20_RELA, laddr + first_size);
   9783   lo_irelfn =
   9784     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9785 				 R_NDS32_LO12S0_ORI_RELA,
   9786 				 laddr + first_size + 4);
   9787 
   9788   for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
   9789     {
   9790       cond_irelfn =
   9791 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9792 				     checked_types[i], laddr);
   9793       if (cond_irelfn != irelend)
   9794 	break;
   9795     }
   9796 
   9797   if (hi_irelfn == irelend || lo_irelfn == irelend || cond_irelfn == irelend)
   9798     {
   9799       (*_bfd_error_handler)
   9800 	("%B: warning: R_NDS32_LONGJUMP3 points to unrecognized"
   9801 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   9802       return FALSE;
   9803     }
   9804 
   9805   /* Get the value of the symbol referred to by the reloc.  */
   9806   foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
   9807 			   &pic_ext_target);
   9808 
   9809   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
   9810       || foff >= CONSERVATIVE_24BIT_S1)
   9811     return FALSE;
   9812 
   9813   /* Get the all corresponding instructions.  */
   9814   if (first_size == 4)
   9815     {
   9816       insn = bfd_getb32 (contents + laddr);
   9817       nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
   9818     }
   9819   else
   9820     {
   9821       insn16 = bfd_getb16 (contents + laddr);
   9822       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
   9823     }
   9824 
   9825   /* For simplicity of coding, we are going to modify the section
   9826      contents, the section relocs, and the BFD symbol table.  We
   9827      must tell the rest of the code not to free up this
   9828      information.  It would be possible to instead create a table
   9829      of changes which have to be made, as is done in coff-mips.c;
   9830      that would be more work, but would require less memory when
   9831      the linker is run.  */
   9832 
   9833   if (re_insn16 && foff >= -ACCURATE_8BIT_S1 - first_size
   9834       && foff < ACCURATE_8BIT_S1 - first_size)
   9835     {
   9836       if (!(seq_len & 0x2))
   9837 	{
   9838 	  /* Don't convert it to 16-bit now, keep this as relaxable
   9839 	     for ``label reloc; INSN1a''6.  */
   9840 	  /* Save comp_insn32 to buffer.  */
   9841 	  bfd_putb32 (re_insn, contents + irel->r_offset);
   9842 	  *insn_len = 4;
   9843 	  reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
   9844 	    R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
   9845 	  cond_reloc = R_NDS32_INSN16;
   9846 	}
   9847       else
   9848 	{
   9849 	  /* Not optimize for speed; convert sequence to 16-bit.  */
   9850 	  /* Save comp_insn16 to buffer.  */
   9851 	  bfd_putb16 (re_insn16, contents + irel->r_offset);
   9852 	  *insn_len = 2;
   9853 	  reloc = R_NDS32_9_PCREL_RELA;
   9854 	  cond_reloc = R_NDS32_NONE;
   9855 	}
   9856       cond_removed = 1;
   9857     }
   9858   else if (N32_OP6 (re_insn) == N32_OP6_BR1
   9859 	   && (foff >= -(ACCURATE_14BIT_S1 - first_size)
   9860 	       && foff < ACCURATE_14BIT_S1 - first_size))
   9861     {
   9862       /* beqs     label    ; 15_PCREL */
   9863       bfd_putb32 (re_insn, contents + irel->r_offset);
   9864       *insn_len = 4;
   9865       reloc = R_NDS32_15_PCREL_RELA;
   9866       cond_reloc = R_NDS32_NONE;
   9867       cond_removed = 1;
   9868     }
   9869   else if (N32_OP6 (re_insn) == N32_OP6_BR2
   9870 	   && foff >= -CONSERVATIVE_16BIT_S1
   9871 	   && foff < CONSERVATIVE_16BIT_S1)
   9872     {
   9873       /* beqz     label ; 17_PCREL */
   9874       bfd_putb32 (re_insn, contents + irel->r_offset);
   9875       *insn_len = 4;
   9876       reloc = R_NDS32_17_PCREL_RELA;
   9877       cond_reloc = R_NDS32_NONE;
   9878       cond_removed = 1;
   9879     }
   9880   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
   9881 	   && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
   9882     {
   9883       /* Relax to one of the following 3 variations
   9884 
   9885 	 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
   9886 	 for space
   9887 	 bnes38  rt, $1 ; LONGJUMP2
   9888 	 j       label  ; 25_PCREL
   9889 	 $1
   9890 
   9891 	 case 4-4; 1st insn not convertible, others don't care
   9892 	 bne   rt, ra, $1 ; LONGJUMP2
   9893 	 j     label      ; 25_PCREL
   9894 	 $1
   9895 
   9896 	 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
   9897 	 bne   rt, ra, $1 ; LONGJUMP2
   9898 	 j     label      ; 25_PCREL
   9899 	 $1 */
   9900 
   9901       /* Offset for first instruction.  */
   9902 
   9903       /* Use j label as second instruction.  */
   9904       *insn_len = 4 + first_size;
   9905       insn = INSN_J;
   9906       bfd_putb32 (insn, contents + hi_irelfn->r_offset);
   9907       reloc = R_NDS32_LONGJUMP2;
   9908       cond_reloc = R_NDS32_25_PLTREL;
   9909     }
   9910     else
   9911       return FALSE;
   9912 
   9913     if (cond_removed == 1)
   9914       {
   9915 	/* Set all relocations.  */
   9916 	irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
   9917 	irel->r_addend = hi_irelfn->r_addend;
   9918 
   9919 	cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
   9920 					    cond_reloc);
   9921 	cond_irelfn->r_addend = 0;
   9922 	hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
   9923 					  R_NDS32_NONE);
   9924       }
   9925     else
   9926       {
   9927 	irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
   9928 	irel->r_addend = irel->r_addend;
   9929 	hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
   9930 					  cond_reloc);
   9931       }
   9932 
   9933   if ((seq_len ^ *insn_len ) & 0x2)
   9934     {
   9935       insn16 = NDS32_NOP16;
   9936       bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
   9937       lo_irelfn->r_offset = *insn_len;
   9938       lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
   9939 					R_NDS32_INSN16);
   9940       lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   9941       *insn_len += 2;
   9942     }
   9943   else
   9944     lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
   9945 				      R_NDS32_NONE);
   9946   return TRUE;
   9947 }
   9948 
   9949 /* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
   9950 
   9951 static bfd_boolean
   9952 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   9953 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   9954 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   9955 			   Elf_Internal_Shdr *symtab_hdr)
   9956 {
   9957   /* The pattern for LONGCALL4.  Support for function cse.
   9958      sethi ta, hi20(symbol)	; LONGCALL4/HI20
   9959      ori   ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
   9960      jral  ta			; PTR_RES/EMPTY/INSN16  */
   9961 
   9962   bfd_vma laddr;
   9963   uint32_t insn;
   9964   Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
   9965   Elf_Internal_Rela *irelend;
   9966   int pic_ext_target = 0;
   9967   bfd_signed_vma foff;
   9968 
   9969   irelend = internal_relocs + sec->reloc_count;
   9970   laddr = irel->r_offset;
   9971 
   9972   /* Get the reloc for the address from which the register is
   9973      being loaded.  This reloc will tell us which function is
   9974      actually being called.  */
   9975   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   9976 					 R_NDS32_HI20_RELA, laddr);
   9977 
   9978   if (hi_irel == irelend)
   9979     {
   9980       (*_bfd_error_handler)
   9981 	("%B: warning: R_NDS32_LONGCALL4 points to unrecognized"
   9982 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   9983       return FALSE;
   9984     }
   9985 
   9986   /* Get the value of the symbol referred to by the reloc.  */
   9987   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
   9988 			   &pic_ext_target);
   9989 
   9990   /* This condition only happened when symbol is undefined.  */
   9991   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
   9992       || foff >= CONSERVATIVE_24BIT_S1)
   9993     return FALSE;
   9994 
   9995   /* Relax to: jal symbol; 25_PCREL */
   9996   /* For simplicity of coding, we are going to modify the section
   9997      contents, the section relocs, and the BFD symbol table.  We
   9998      must tell the rest of the code not to free up this
   9999      information.  It would be possible to instead create a table
   10000      of changes which have to be made, as is done in coff-mips.c;
   10001      that would be more work, but would require less memory when
   10002      the linker is run.  */
   10003 
   10004   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10005 					  R_NDS32_PTR_RESOLVED, irel->r_addend);
   10006   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10007 					  R_NDS32_EMPTY, irel->r_addend);
   10008 
   10009   if (ptr_irel == irelend || em_irel == irelend)
   10010     {
   10011       (*_bfd_error_handler)
   10012 	("%B: warning: R_NDS32_LONGCALL4 points to unrecognized"
   10013 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10014       return FALSE;
   10015     }
   10016   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
   10017   insn = bfd_getb32 (contents + irel->r_addend);
   10018   if (insn & 0x80000000)
   10019     return FALSE;
   10020 
   10021   /* Replace the long call with a jal.  */
   10022   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
   10023 				  R_NDS32_25_PCREL_RELA);
   10024   ptr_irel->r_addend = 1;
   10025 
   10026   /* We don't resolve this here but resolve it in relocate_section.  */
   10027   insn = INSN_JAL;
   10028   bfd_putb32 (insn, contents + em_irel->r_offset);
   10029 
   10030   irel->r_info =
   10031     ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10032 
   10033   /* If there is function cse, HI20 can not remove now.  */
   10034   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10035 					   R_NDS32_LONGCALL4, laddr);
   10036   if (call_irel == irelend)
   10037     {
   10038       *insn_len = 0;
   10039       hi_irel->r_info =
   10040 	ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
   10041     }
   10042 
   10043   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10044 					  R_NDS32_INSN16, irel->r_addend);
   10045   if (insn_irel != irelend)
   10046     insn_irel->r_info =
   10047       ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10048 
   10049   return TRUE;
   10050 }
   10051 
   10052 /* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
   10053 
   10054 static bfd_boolean
   10055 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   10056 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   10057 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   10058 			   Elf_Internal_Shdr *symtab_hdr)
   10059 {
   10060   /* The pattern for LONGCALL5.
   10061      bltz  rt, .L1	; LONGCALL5/17_PCREL
   10062      jal   symbol	; 25_PCREL
   10063      .L1:  */
   10064 
   10065   bfd_vma laddr;
   10066   uint32_t insn;
   10067   Elf_Internal_Rela *cond_irel, *irelend;
   10068   int pic_ext_target = 0;
   10069   bfd_signed_vma foff;
   10070 
   10071   irelend = internal_relocs + sec->reloc_count;
   10072   laddr = irel->r_offset;
   10073   insn = bfd_getb32 (contents + laddr);
   10074 
   10075   /* Get the reloc for the address from which the register is
   10076      being loaded.  This reloc will tell us which function is
   10077      actually being called.  */
   10078   cond_irel =
   10079     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10080 				 R_NDS32_25_PCREL_RELA, irel->r_addend);
   10081   if (cond_irel == irelend)
   10082     {
   10083       (*_bfd_error_handler)
   10084 	("%B: warning: R_NDS32_LONGCALL5 points to unrecognized"
   10085 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10086       return FALSE;
   10087     }
   10088 
   10089   /* Get the value of the symbol referred to by the reloc.  */
   10090   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
   10091 			   &pic_ext_target);
   10092 
   10093   if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
   10094       || foff >= CONSERVATIVE_16BIT_S1)
   10095     return FALSE;
   10096 
   10097   /* Relax to	bgezal   rt, label ; 17_PCREL
   10098      or		bltzal   rt, label ; 17_PCREL */
   10099 
   10100   /* Convert to complimentary conditional call.  */
   10101   insn = CONVERT_CONDITION_CALL (insn);
   10102 
   10103   /* For simplicity of coding, we are going to modify the section
   10104      contents, the section relocs, and the BFD symbol table.  We
   10105      must tell the rest of the code not to free up this
   10106      information.  It would be possible to instead create a table
   10107      of changes which have to be made, as is done in coff-mips.c;
   10108      that would be more work, but would require less memory when
   10109      the linker is run.  */
   10110 
   10111   /* Modify relocation and contents.  */
   10112   cond_irel->r_info =
   10113     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
   10114 
   10115   /* Replace the long call with a bgezal.  */
   10116   bfd_putb32 (insn, contents + cond_irel->r_offset);
   10117   *insn_len = 0;
   10118 
   10119   /* Clean unnessary relocations.  */
   10120   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10121 
   10122   cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10123 					   R_NDS32_17_PCREL_RELA, laddr);
   10124   cond_irel->r_info =
   10125     ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
   10126 
   10127   return TRUE;
   10128 }
   10129 
   10130 /* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
   10131 
   10132 static bfd_boolean
   10133 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   10134 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   10135 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   10136 			   Elf_Internal_Shdr *symtab_hdr)
   10137 {
   10138   /* The pattern for LONGCALL6.
   10139      bltz  rt,   .L1			; LONGCALL6/17_PCREL
   10140      sethi ta,   hi20(symbol)		; HI20/PTR
   10141      ori   ta, ta,  lo12(symbol)	; LO12S0_ORI/PTR
   10142      jral  ta				; PTR_RES/EMPTY/INSN16
   10143      .L1  */
   10144 
   10145   bfd_vma laddr;
   10146   uint32_t insn;
   10147   Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
   10148   int pic_ext_target = 0;
   10149   bfd_signed_vma foff;
   10150 
   10151   irelend = internal_relocs + sec->reloc_count;
   10152   laddr = irel->r_offset;
   10153 
   10154   /* Get the reloc for the address from which the register is
   10155      being loaded.  This reloc will tell us which function is
   10156      actually being called.  */
   10157   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10158 					 R_NDS32_EMPTY, irel->r_addend);
   10159 
   10160   if (em_irel == irelend)
   10161     {
   10162       (*_bfd_error_handler)
   10163 	("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
   10164 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10165       return FALSE;
   10166     }
   10167 
   10168   /* Get the value of the symbol referred to by the reloc.  */
   10169   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
   10170 			   &pic_ext_target);
   10171 
   10172   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
   10173       || foff >= CONSERVATIVE_24BIT_S1)
   10174     return FALSE;
   10175 
   10176   /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
   10177   insn = bfd_getb32 (contents + irel->r_addend);
   10178   if (insn & 0x80000000)
   10179     return FALSE;
   10180 
   10181   insn = bfd_getb32 (contents + laddr);
   10182   if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
   10183     {
   10184       /* Relax to  bgezal   rt, label ; 17_PCREL
   10185 	 or	   bltzal   rt, label ; 17_PCREL */
   10186 
   10187       /* Convert to complimentary conditional call.  */
   10188       *insn_len = 0;
   10189       insn = CONVERT_CONDITION_CALL (insn);
   10190       bfd_putb32 (insn, contents + em_irel->r_offset);
   10191 
   10192       em_irel->r_info =
   10193 	ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
   10194 
   10195       /* Set resolved relocation.  */
   10196       cond_irel =
   10197 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10198 				     R_NDS32_PTR_RESOLVED, irel->r_addend);
   10199       if (cond_irel == irelend)
   10200 	{
   10201 	  (*_bfd_error_handler)
   10202 	    ("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
   10203 	     "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10204 	  return FALSE;
   10205 	}
   10206       cond_irel->r_addend = 1;
   10207 
   10208       /* Clear relocations.  */
   10209 
   10210       irel->r_info =
   10211 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10212 
   10213       cond_irel =
   10214 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10215 				     R_NDS32_17_PCREL_RELA, laddr);
   10216       if (cond_irel != irelend)
   10217 	cond_irel->r_info =
   10218 	  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
   10219 
   10220       cond_irel =
   10221 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10222 				     R_NDS32_INSN16, irel->r_addend);
   10223       if (cond_irel != irelend)
   10224 	cond_irel->r_info =
   10225 	  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
   10226 
   10227     }
   10228   else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
   10229     {
   10230       /* Relax to the following instruction sequence
   10231 	 bltz  rt, .L1	; LONGCALL2/17_PCREL
   10232 	 jal   symbol	; 25_PCREL/PTR_RES
   10233 	 .L1  */
   10234       *insn_len = 4;
   10235       /* Convert instruction.  */
   10236       insn = INSN_JAL;
   10237       bfd_putb32 (insn, contents + em_irel->r_offset);
   10238 
   10239       /* Convert relocations.  */
   10240       em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
   10241 				      R_NDS32_25_PCREL_RELA);
   10242       irel->r_info =
   10243 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
   10244 
   10245       /* Set resolved relocation.  */
   10246       cond_irel =
   10247 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10248 				     R_NDS32_PTR_RESOLVED, irel->r_addend);
   10249       if (cond_irel == irelend)
   10250 	{
   10251 	  (*_bfd_error_handler)
   10252 	    ("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
   10253 	     "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10254 	  return FALSE;
   10255 	}
   10256       cond_irel->r_addend = 1;
   10257 
   10258       cond_irel =
   10259 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10260 				     R_NDS32_INSN16, irel->r_addend);
   10261       if (cond_irel != irelend)
   10262 	cond_irel->r_info =
   10263 	  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
   10264     }
   10265   return TRUE;
   10266 }
   10267 
   10268 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
   10269 
   10270 static bfd_boolean
   10271 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   10272 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   10273 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   10274 			   Elf_Internal_Shdr *symtab_hdr)
   10275 {
   10276   /* The pattern for LONGJUMP4.
   10277      sethi ta, hi20(symbol)	; LONGJUMP4/HI20
   10278      ori   ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
   10279      jr    ta			; PTR_RES/INSN16/EMPTY  */
   10280 
   10281   bfd_vma laddr;
   10282   int seq_len;	/* Original length of instruction sequence.  */
   10283   uint32_t insn;
   10284   Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
   10285   int pic_ext_target = 0;
   10286   bfd_signed_vma foff;
   10287 
   10288   irelend = internal_relocs + sec->reloc_count;
   10289   seq_len = GET_SEQ_LEN (irel->r_addend);
   10290   laddr = irel->r_offset;
   10291   *insn_len = seq_len;
   10292 
   10293   /* Get the reloc for the address from which the register is
   10294      being loaded.  This reloc will tell us which function is
   10295      actually being called.  */
   10296 
   10297   hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10298 					 R_NDS32_HI20_RELA, laddr);
   10299 
   10300   if (hi_irel == irelend)
   10301     {
   10302       (*_bfd_error_handler)
   10303 	("%B: warning: R_NDS32_LONGJUMP4 points to unrecognized"
   10304 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10305       return FALSE;
   10306     }
   10307 
   10308   /* Get the value of the symbol referred to by the reloc.  */
   10309   foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
   10310 			   &pic_ext_target);
   10311 
   10312   if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
   10313       || foff < -CONSERVATIVE_24BIT_S1)
   10314     return FALSE;
   10315 
   10316   /* Convert it to "j label", it may be converted to j8 in the final
   10317      pass of relaxation.  Therefore, we do not consider this currently.  */
   10318   ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10319 					  R_NDS32_PTR_RESOLVED, irel->r_addend);
   10320   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10321 					 R_NDS32_EMPTY, irel->r_addend);
   10322 
   10323   if (ptr_irel == irelend || em_irel == irelend)
   10324     {
   10325       (*_bfd_error_handler)
   10326 	("%B: warning: R_NDS32_LONGJUMP4 points to unrecognized"
   10327 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10328       return FALSE;
   10329     }
   10330 
   10331   em_irel->r_info =
   10332     ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
   10333   ptr_irel->r_addend = 1;
   10334 
   10335   /* Write instruction.  */
   10336   insn = INSN_J;
   10337   bfd_putb32 (insn, contents + em_irel->r_offset);
   10338 
   10339   /* Clear relocations.  */
   10340   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10341 
   10342   /* If there is function cse, HI20 can not remove now.  */
   10343   call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10344 					   R_NDS32_LONGJUMP4, laddr);
   10345   if (call_irel == irelend)
   10346     {
   10347       *insn_len = 0;
   10348       hi_irel->r_info =
   10349 	ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
   10350     }
   10351 
   10352   return TRUE;
   10353 }
   10354 
   10355 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
   10356 
   10357 static bfd_boolean
   10358 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   10359 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   10360 			   int *seq_len, bfd_byte *contents,
   10361 			   Elf_Internal_Sym *isymbuf,
   10362 			   Elf_Internal_Shdr *symtab_hdr)
   10363 {
   10364   /* There are 2 variations for LONGJUMP5
   10365      case 2-4;  1st insn convertible, 16-bit on.
   10366      bnes38  rt, ra, .L1	; LONGJUMP5/9_PCREL/INSN16
   10367      j       label		; 25_PCREL/INSN16
   10368      $1:
   10369 
   10370      case 4-4; 1st insn not convertible
   10371      bne  rt, ra, .L1	; LONGJUMP5/15_PCREL/INSN16
   10372      j    label		; 25_PCREL/INSN16
   10373      .L1:  */
   10374 
   10375   bfd_vma laddr;
   10376   Elf_Internal_Rela *cond_irel,  *irelend;
   10377   int pic_ext_target = 0;
   10378   unsigned int i;
   10379   bfd_signed_vma foff;
   10380   uint32_t insn, re_insn = 0;
   10381   uint16_t insn16, re_insn16 = 0;
   10382   unsigned long reloc;
   10383 
   10384   enum elf_nds32_reloc_type checked_types[] =
   10385     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
   10386       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
   10387 
   10388   irelend = internal_relocs + sec->reloc_count;
   10389   laddr = irel->r_offset;
   10390 
   10391   /* Get the reloc for the address from which the register is
   10392      being loaded.  This reloc will tell us which function is
   10393      actually being called.  */
   10394 
   10395   cond_irel =
   10396     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10397 				 R_NDS32_25_PCREL_RELA, irel->r_addend);
   10398   if (cond_irel == irelend)
   10399     {
   10400       (*_bfd_error_handler)
   10401 	("%B: warning: R_NDS32_LONGJUMP5 points to unrecognized"
   10402 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10403       return FALSE;
   10404     }
   10405 
   10406   /* Get the value of the symbol referred to by the reloc.  */
   10407   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
   10408 			   &pic_ext_target);
   10409 
   10410   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
   10411       || foff >= CONSERVATIVE_16BIT_S1)
   10412     return FALSE;
   10413 
   10414   /* Get the all corresponding instructions.  */
   10415   insn = bfd_getb32 (contents + laddr);
   10416   /* Check instruction size.  */
   10417   if (insn & 0x80000000)
   10418     {
   10419       *seq_len = 0;
   10420       insn16 = insn >> 16;
   10421       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
   10422     }
   10423   else
   10424     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
   10425 
   10426   if (N32_OP6 (re_insn) == N32_OP6_BR1
   10427       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
   10428     {
   10429       /* beqs label ; 15_PCREL.  */
   10430       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
   10431       reloc = R_NDS32_15_PCREL_RELA;
   10432     }
   10433   else if (N32_OP6 (re_insn) == N32_OP6_BR2
   10434 	   && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
   10435     {
   10436       /* beqz label ; 17_PCREL.  */
   10437       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
   10438       reloc = R_NDS32_17_PCREL_RELA;
   10439     }
   10440   else if ( N32_OP6 (re_insn) == N32_OP6_BR3
   10441 	   && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
   10442     {
   10443       /* beqc label ; 9_PCREL.  */
   10444       bfd_putb32 (re_insn, contents + cond_irel->r_offset);
   10445       reloc = R_NDS32_WORD_9_PCREL_RELA;
   10446     }
   10447   else
   10448     return FALSE;
   10449 
   10450   /* Set all relocations.  */
   10451   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
   10452 
   10453   /* Clean relocations.  */
   10454   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10455   for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
   10456     {
   10457       cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10458 					       checked_types[i], laddr);
   10459       if (cond_irel != irelend)
   10460 	{
   10461 	  if (*seq_len == 0
   10462 	      && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
   10463 	    {
   10464 	      /* If the branch instruction is 2 byte, it cannot remove
   10465 		 directly.  Only convert it to nop16 and remove it after
   10466 		 checking alignment issue.  */
   10467 	      insn16 = NDS32_NOP16;
   10468 	      bfd_putb16 (insn16, contents + laddr);
   10469 	      cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   10470 	    }
   10471 	  else
   10472 	    cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
   10473 					      R_NDS32_NONE);
   10474 	}
   10475     }
   10476   *insn_len = 0;
   10477 
   10478   return TRUE;
   10479 }
   10480 
   10481 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
   10482 
   10483 static bfd_boolean
   10484 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   10485 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   10486 			   int *seq_len, bfd_byte *contents,
   10487 			   Elf_Internal_Sym *isymbuf,
   10488 			   Elf_Internal_Shdr *symtab_hdr)
   10489 {
   10490   /* There are 5 variations for LONGJUMP6
   10491      case : 2-4-4-4; 1st insn convertible, 16-bit on.
   10492      bnes38   rt, ra, .L1		; LONGJUMP6/15_PCREL/INSN16
   10493      sethi    ta, hi20(symbol)		; HI20/PTR
   10494      ori      ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
   10495      jr       ta			; PTR_RES/INSN16/EMPTY
   10496      .L1:
   10497 
   10498      case : 4-4-4-4; 1st insn not convertible, 16-bit on.
   10499      bne   rt, ra, .L1		; LONGJUMP6/15_PCREL/INSN16
   10500      sethi ta, hi20(symbol)	; HI20/PTR
   10501      ori   ta, ta, lo12(symbol)	; LO12S0_ORI/PTR
   10502      jr    ta			; PTR_RES/INSN16/EMPTY
   10503      .L1:  */
   10504 
   10505   enum elf_nds32_reloc_type checked_types[] =
   10506     { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
   10507       R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
   10508 
   10509   int reloc_off = 0, cond_removed = 0;
   10510   bfd_vma laddr;
   10511   Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
   10512   int pic_ext_target = 0;
   10513   unsigned int i;
   10514   bfd_signed_vma foff;
   10515   uint32_t insn, re_insn = 0;
   10516   uint16_t insn16, re_insn16 = 0;
   10517   unsigned long reloc;
   10518 
   10519   irelend = internal_relocs + sec->reloc_count;
   10520   laddr = irel->r_offset;
   10521 
   10522   /* Get the reloc for the address from which the register is
   10523      being loaded.  This reloc will tell us which function is
   10524      actually being called.  */
   10525   em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10526 					 R_NDS32_EMPTY, irel->r_addend);
   10527 
   10528   if (em_irel == irelend)
   10529     {
   10530       (*_bfd_error_handler)
   10531 	("%B: warning: R_NDS32_LONGJUMP6 points to unrecognized"
   10532 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10533       return FALSE;
   10534     }
   10535 
   10536   /* Get the value of the symbol referred to by the reloc.  */
   10537   foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
   10538 			   &pic_ext_target);
   10539 
   10540   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
   10541       || foff >= CONSERVATIVE_24BIT_S1)
   10542     return FALSE;
   10543 
   10544   insn = bfd_getb32 (contents + laddr);
   10545   /* Check instruction size.  */
   10546   if (insn & 0x80000000)
   10547     {
   10548       *seq_len = 0;
   10549       insn16 = insn >> 16;
   10550       nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
   10551     }
   10552   else
   10553     nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
   10554 
   10555   /* For simplicity of coding, we are going to modify the section
   10556      contents, the section relocs, and the BFD symbol table.  We
   10557      must tell the rest of the code not to free up this
   10558      information.  It would be possible to instead create a table
   10559      of changes which have to be made, as is done in coff-mips.c;
   10560      that would be more work, but would require less memory when
   10561      the linker is run.  */
   10562 
   10563   if (N32_OP6 (re_insn) == N32_OP6_BR1
   10564       && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
   10565     {
   10566       /* beqs     label    ; 15_PCREL */
   10567       bfd_putb32 (re_insn, contents + em_irel->r_offset);
   10568       reloc = R_NDS32_15_PCREL_RELA;
   10569       cond_removed = 1;
   10570     }
   10571   else if (N32_OP6 (re_insn) == N32_OP6_BR2
   10572 	   && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
   10573     {
   10574       /* beqz     label ; 17_PCREL */
   10575       bfd_putb32 (re_insn, contents + em_irel->r_offset);
   10576       reloc = R_NDS32_17_PCREL_RELA;
   10577       cond_removed = 1;
   10578     }
   10579   else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
   10580 	   && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
   10581     {
   10582       /* Relax to one of the following 2 variations
   10583 
   10584 	 case 2-4;  1st insn convertible, 16-bit on.
   10585 	 bnes38  rt, ra, .L1	; LONGJUMP5/9_PCREL/INSN16
   10586 	 j       label		; 25_PCREL/INSN16
   10587 	 $1:
   10588 
   10589 	 case 4-4; 1st insn not convertible
   10590 	 bne  rt, ra, .L1	; LONGJUMP5/15_PCREL/INSN16
   10591 	 j    label		; 25_PCREL/INSN16
   10592 	 .L1:  */
   10593 
   10594       /* Use j label as second instruction.  */
   10595       insn = INSN_J;
   10596       reloc = R_NDS32_25_PCREL_RELA;
   10597       bfd_putb32 (insn, contents + em_irel->r_offset);
   10598     }
   10599   else
   10600     return FALSE;
   10601 
   10602   /* Set all relocations.  */
   10603   em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
   10604 
   10605   cond_irel =
   10606     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10607 				 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
   10608   cond_irel->r_addend = 1;
   10609 
   10610   /* Use INSN16 of first branch instruction to distinguish if keeping
   10611      INSN16 of final instruction or not.  */
   10612   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10613 					   R_NDS32_INSN16, irel->r_offset);
   10614   if (insn_irel == irelend)
   10615     {
   10616       /* Clean the final INSN16.  */
   10617       insn_irel =
   10618 	find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10619 				     R_NDS32_INSN16, em_irel->r_offset);
   10620       insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
   10621 					R_NDS32_NONE);
   10622     }
   10623 
   10624   if (cond_removed == 1)
   10625     {
   10626       *insn_len = 0;
   10627 
   10628       /* Clear relocations.  */
   10629       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10630 
   10631       for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
   10632 	{
   10633 	  cond_irel =
   10634 	    find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10635 					 checked_types[i], laddr);
   10636 	  if (cond_irel != irelend)
   10637 	    {
   10638 	      if (*seq_len == 0
   10639 		  && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
   10640 		{
   10641 		  /* If the branch instruction is 2 byte, it cannot remove
   10642 		     directly.  Only convert it to nop16 and remove it after
   10643 		     checking alignment issue.  */
   10644 		  insn16 = NDS32_NOP16;
   10645 		  bfd_putb16 (insn16, contents + laddr);
   10646 		  cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   10647 		}
   10648 	      else
   10649 		cond_irel->r_info =
   10650 		  ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
   10651 	    }
   10652 	}
   10653     }
   10654   else
   10655     {
   10656       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   10657 				   R_NDS32_LONGJUMP5);
   10658     }
   10659 
   10660   return TRUE;
   10661 }
   10662 
   10663 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
   10664 
   10665 static bfd_boolean
   10666 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   10667 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   10668 			   int *seq_len, bfd_byte *contents,
   10669 			   Elf_Internal_Sym *isymbuf,
   10670 			   Elf_Internal_Shdr *symtab_hdr)
   10671 {
   10672   /* There are 2 variations for LONGJUMP5
   10673      case 2-4;  1st insn convertible, 16-bit on.
   10674      movi55  ta, imm11		; LONGJUMP7/INSN16
   10675      beq     rt, ta, label	; 15_PCREL
   10676 
   10677      case 4-4; 1st insn not convertible
   10678      movi55  ta, imm11		; LONGJUMP7/INSN16
   10679      beq     rt, ta, label	; 15_PCREL  */
   10680 
   10681   bfd_vma laddr;
   10682   Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
   10683   int pic_ext_target = 0;
   10684   bfd_signed_vma foff;
   10685   uint32_t insn, re_insn = 0;
   10686   uint16_t insn16;
   10687   uint32_t imm11;
   10688 
   10689   irelend = internal_relocs + sec->reloc_count;
   10690   laddr = irel->r_offset;
   10691 
   10692   /* Get the reloc for the address from which the register is
   10693      being loaded.  This reloc will tell us which function is
   10694      actually being called.  */
   10695 
   10696   cond_irel =
   10697     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10698 				 R_NDS32_15_PCREL_RELA, irel->r_addend);
   10699   if (cond_irel == irelend)
   10700     {
   10701       (*_bfd_error_handler)
   10702 	("%B: warning: R_NDS32_LONGJUMP7 points to unrecognized"
   10703 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10704       return FALSE;
   10705     }
   10706 
   10707   /* Get the value of the symbol referred to by the reloc.  */
   10708   foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
   10709 			   &pic_ext_target);
   10710 
   10711   if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_8BIT_S1
   10712       || foff >= CONSERVATIVE_8BIT_S1)
   10713     return FALSE;
   10714 
   10715   /* Get the first instruction for its size.  */
   10716   insn = bfd_getb32 (contents + laddr);
   10717   if (insn & 0x80000000)
   10718     {
   10719       *seq_len = 0;
   10720       /* Get the immediate from movi55.  */
   10721       imm11 = N16_IMM5S (insn >> 16);
   10722     }
   10723   else
   10724     {
   10725       /* Get the immediate from movi.  */
   10726       imm11 = N32_IMM20S (insn);
   10727     }
   10728 
   10729   /* Get the branch instruction.  */
   10730   insn = bfd_getb32 (contents + irel->r_addend);
   10731   /* Convert instruction to BR3.  */
   10732   if ((insn >> 14) & 0x1)
   10733     re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
   10734   else
   10735     re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
   10736 
   10737   bfd_putb32 (re_insn, contents + cond_irel->r_offset);
   10738 
   10739   /* Set all relocations.  */
   10740   cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
   10741 				    R_NDS32_WORD_9_PCREL_RELA);
   10742 
   10743   /* Clean relocations.  */
   10744   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10745   insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10746 					   R_NDS32_INSN16, irel->r_offset);
   10747   if (insn_irel != irelend)
   10748     {
   10749       if (*seq_len == 0)
   10750 	{
   10751 	  /* If the first insntruction is 16bit, convert it to nop16.  */
   10752 	  insn16 = NDS32_NOP16;
   10753 	  bfd_putb16 (insn16, contents + laddr);
   10754 	  insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
   10755 	}
   10756       else
   10757 	cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
   10758 					  R_NDS32_NONE);
   10759     }
   10760   *insn_len = 0;
   10761 
   10762   return TRUE;
   10763 }
   10764 
   10765 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
   10766 
   10767 /* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
   10768 
   10769 static bfd_boolean
   10770 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
   10771 			   asection *sec, Elf_Internal_Rela *irel,
   10772 			   Elf_Internal_Rela *internal_relocs, int *insn_len,
   10773 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   10774 			   Elf_Internal_Shdr *symtab_hdr, int load_store_relax)
   10775 {
   10776   int eliminate_sethi = 0, range_type, i;
   10777   bfd_vma local_sda, laddr;
   10778   int seq_len;	/* Original length of instruction sequence.  */
   10779   uint32_t insn;
   10780   Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
   10781   bfd_vma access_addr = 0;
   10782   bfd_vma range_l = 0, range_h = 0;	/* Upper/lower bound.  */
   10783   enum elf_nds32_reloc_type checked_types[] =
   10784     { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
   10785       R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
   10786       R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
   10787       R_NDS32_TLS_LE_HI20
   10788     };
   10789 
   10790   irelend = internal_relocs + sec->reloc_count;
   10791   seq_len = GET_SEQ_LEN (irel->r_addend);
   10792   laddr = irel->r_offset;
   10793   *insn_len = seq_len;
   10794 
   10795   /* Get the high part relocation.  */
   10796   for (i = 0; (unsigned) i < sizeof (checked_types); i++)
   10797     {
   10798       hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
   10799 					       checked_types[i], laddr);
   10800       if (hi_irelfn != irelend)
   10801 	break;
   10802     }
   10803 
   10804   if (hi_irelfn == irelend)
   10805     {
   10806       (*_bfd_error_handler)
   10807 	("%B: warning: R_NDS32_LOADSTORE points to unrecognized"
   10808 	 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
   10809 	return FALSE;
   10810     }
   10811 
   10812   range_type = GET_LOADSTORE_RANGE (irel->r_addend);
   10813   nds32_elf_final_sda_base (sec->output_section->owner,
   10814 			    link_info, &local_sda, FALSE);
   10815 
   10816   switch (ELF32_R_TYPE (hi_irelfn->r_info))
   10817     {
   10818     case R_NDS32_HI20_RELA:
   10819       insn = bfd_getb32 (contents + laddr);
   10820       access_addr =
   10821 	calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
   10822 
   10823       if (range_type == NDS32_LOADSTORE_IMM)
   10824 	{
   10825 	  struct elf_link_hash_entry *h = NULL;
   10826 	  int indx;
   10827 
   10828 	  if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
   10829 	    {
   10830 	      indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
   10831 	      h = elf_sym_hashes (abfd)[indx];
   10832 	    }
   10833 
   10834 	  if ((access_addr < CONSERVATIVE_20BIT)
   10835 	      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
   10836 	    {
   10837 	      eliminate_sethi = 1;
   10838 	      break;
   10839 	    }
   10840 
   10841 	  /* This is avoid to relax symbol address which is fixed
   10842 	     relocations.  Ex: _stack.  */
   10843 	  if (h && bfd_is_abs_section (h->root.u.def.section))
   10844 	    return FALSE;
   10845 	}
   10846 
   10847       if (!load_store_relax)
   10848 	return FALSE;
   10849 
   10850       /* Case for set gp register.  */
   10851       if (N32_RT5 (insn) == REG_GP)
   10852 	break;
   10853 
   10854       if (range_type == NDS32_LOADSTORE_FLOAT_S
   10855 	  || range_type == NDS32_LOADSTORE_FLOAT_S)
   10856 	{
   10857 	  range_l = sdata_range[0][0];
   10858 	  range_h = sdata_range[0][1];
   10859 	}
   10860       else
   10861 	{
   10862 	  range_l = sdata_range[1][0];
   10863 	  range_h = sdata_range[1][1];
   10864 	}
   10865       break;
   10866 
   10867     case R_NDS32_GOT_HI20:
   10868       access_addr =
   10869 	calculate_got_memory_address (abfd, link_info, hi_irelfn, symtab_hdr);
   10870 
   10871       /* If this symbol is not in .got, the return value will be -1.
   10872 	 Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
   10873 	 a negative offset is allowed.  */
   10874       if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
   10875 	  && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
   10876 	eliminate_sethi = 1;
   10877       break;
   10878 
   10879     case R_NDS32_PLT_GOTREL_HI20:
   10880       access_addr = calculate_plt_memory_address (abfd, link_info, isymbuf,
   10881 						  hi_irelfn, symtab_hdr);
   10882 
   10883       if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
   10884 	  && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
   10885 	eliminate_sethi = 1;
   10886       break;
   10887 
   10888     case R_NDS32_GOTOFF_HI20:
   10889       access_addr =
   10890 	calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
   10891 
   10892       if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
   10893 	  && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
   10894 	eliminate_sethi = 1;
   10895       break;
   10896 
   10897     case R_NDS32_GOTPC_HI20:
   10898       /* The access_addr must consider r_addend of hi_irel.  */
   10899       access_addr = sec->output_section->vma + sec->output_offset
   10900 	+ irel->r_offset + hi_irelfn->r_addend;
   10901 
   10902       if ((bfd_signed_vma) (local_sda - access_addr) < CONSERVATIVE_20BIT
   10903 	  && (bfd_signed_vma) (local_sda - access_addr) >= -CONSERVATIVE_20BIT)
   10904 	eliminate_sethi = 1;
   10905       break;
   10906 
   10907     case R_NDS32_TLS_LE_HI20:
   10908       access_addr =
   10909 	calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
   10910       BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
   10911       access_addr -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
   10912       if ((range_type == NDS32_LOADSTORE_IMM)
   10913 	  && (bfd_signed_vma) (access_addr) < CONSERVATIVE_20BIT
   10914 	  && (bfd_signed_vma) (access_addr) >= -CONSERVATIVE_20BIT)
   10915 	eliminate_sethi = 1;
   10916       break;
   10917 
   10918     default:
   10919       return FALSE;
   10920     }
   10921 
   10922   /* Delete sethi instruction.  */
   10923   if (eliminate_sethi == 1
   10924       || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
   10925       || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
   10926     {
   10927       hi_irelfn->r_info =
   10928 	ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
   10929       irel->r_info =
   10930 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   10931       *insn_len = 0;
   10932     }
   10933   return TRUE;
   10934 }
   10935 
   10936 /* Relax LO12 relocation for nds32_elf_relax_section.  */
   10937 
   10938 static void
   10939 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
   10940 		      asection *sec, Elf_Internal_Rela *irel,
   10941 		      Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
   10942 		      Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
   10943 {
   10944   uint32_t insn;
   10945   bfd_vma local_sda, laddr;
   10946   unsigned long reloc;
   10947   bfd_vma access_addr;
   10948   bfd_vma range_l = 0, range_h = 0;	/* Upper/lower bound.  */
   10949   Elf_Internal_Rela *irelfn = NULL, *irelend;
   10950   struct elf_link_hash_entry *h = NULL;
   10951   int indx;
   10952 
   10953   /* For SDA base relative relaxation.  */
   10954   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
   10955 			    &local_sda, FALSE);
   10956 
   10957   irelend = internal_relocs + sec->reloc_count;
   10958   laddr = irel->r_offset;
   10959   insn = bfd_getb32 (contents + laddr);
   10960 
   10961   if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
   10962     return;
   10963 
   10964   access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
   10965 
   10966   if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
   10967     {
   10968       indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   10969       h = elf_sym_hashes (abfd)[indx];
   10970     }
   10971 
   10972   if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
   10973       && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
   10974     {
   10975       reloc = R_NDS32_20_RELA;
   10976       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
   10977       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
   10978       bfd_putb32 (insn, contents + laddr);
   10979     }
   10980   /* This is avoid to relax symbol address which is fixed
   10981      relocations.  Ex: _stack.  */
   10982   else if (N32_OP6 (insn) == N32_OP6_ORI
   10983 	   && h && bfd_is_abs_section (h->root.u.def.section))
   10984     return;
   10985   else
   10986     {
   10987       range_l = sdata_range[1][0];
   10988       range_h = sdata_range[1][1];
   10989       switch (ELF32_R_TYPE (irel->r_info))
   10990 	{
   10991 	case R_NDS32_LO12S0_RELA:
   10992 	  reloc = R_NDS32_SDA19S0_RELA;
   10993 	  break;
   10994 	case R_NDS32_LO12S1_RELA:
   10995 	  reloc = R_NDS32_SDA18S1_RELA;
   10996 	  break;
   10997 	case R_NDS32_LO12S2_RELA:
   10998 	  reloc = R_NDS32_SDA17S2_RELA;
   10999 	  break;
   11000 	case R_NDS32_LO12S2_DP_RELA:
   11001 	  range_l = sdata_range[0][0];
   11002 	  range_h = sdata_range[0][1];
   11003 	  reloc = R_NDS32_SDA12S2_DP_RELA;
   11004 	  break;
   11005 	case R_NDS32_LO12S2_SP_RELA:
   11006 	  range_l = sdata_range[0][0];
   11007 	  range_h = sdata_range[0][1];
   11008 	  reloc = R_NDS32_SDA12S2_SP_RELA;
   11009 	  break;
   11010 	default:
   11011 	  return;
   11012 	}
   11013 
   11014       /* There are range_h and range_l because linker has to promise
   11015 	 all sections move cross one page together.  */
   11016       if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
   11017 	  || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
   11018 	{
   11019 	  if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
   11020 	    {
   11021 	      /* Maybe we should add R_NDS32_INSN16 reloc type here
   11022 		 or manually do some optimization.  sethi can't be
   11023 		 eliminated when updating $gp so the relative ori
   11024 		 needs to be preserved.  */
   11025 	      return;
   11026 	    }
   11027 	  if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
   11028 					&insn))
   11029 	    return;
   11030 	  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
   11031 	  bfd_putb32 (insn, contents + laddr);
   11032 
   11033 	  irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
   11034 					   R_NDS32_INSN16);
   11035 	  /* SDA17 must keep INSN16 for converting fp_as_gp.  */
   11036 	  if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
   11037 	    irelfn->r_info =
   11038 	      ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
   11039 
   11040 	}
   11041     }
   11042   return;
   11043 }
   11044 
   11045 /* Relax low part of PIC instruction pattern.  */
   11046 
   11047 static void
   11048 nds32_elf_relax_piclo12 (struct bfd_link_info *link_info, bfd *abfd,
   11049 			 asection *sec, Elf_Internal_Rela *irel,
   11050 			 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   11051 			 Elf_Internal_Shdr *symtab_hdr)
   11052 {
   11053   uint32_t insn;
   11054   bfd_vma local_sda, laddr;
   11055   bfd_signed_vma foff;
   11056   unsigned long reloc;
   11057 
   11058   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
   11059 			    &local_sda, FALSE);
   11060   laddr = irel->r_offset;
   11061   insn = bfd_getb32 (contents + laddr);
   11062 
   11063   if (N32_OP6 (insn) != N32_OP6_ORI)
   11064     return;
   11065 
   11066   if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
   11067     {
   11068       foff = calculate_got_memory_address (abfd, link_info, irel,
   11069 					   symtab_hdr) - local_sda;
   11070       reloc = R_NDS32_GOT20;
   11071     }
   11072   else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
   11073     {
   11074       foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
   11075 					   symtab_hdr) - local_sda;
   11076       reloc = R_NDS32_PLT_GOTREL_LO20;
   11077     }
   11078   else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
   11079     {
   11080       foff = calculate_memory_address (abfd, irel, isymbuf,
   11081 				       symtab_hdr) - local_sda;
   11082       reloc = R_NDS32_GOTOFF;
   11083     }
   11084   else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12)
   11085     {
   11086       foff = local_sda - sec->output_section->vma + sec->output_offset
   11087 	+ irel->r_offset + irel->r_addend;
   11088       reloc = R_NDS32_GOTPC20;
   11089     }
   11090   else
   11091     return;
   11092 
   11093   if ((foff < CONSERVATIVE_20BIT) && (foff >= -CONSERVATIVE_20BIT))
   11094     {
   11095       /* Turn into MOVI.  */
   11096       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
   11097       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
   11098       bfd_putb32 (insn, contents + laddr);
   11099     }
   11100 }
   11101 
   11102 /* Relax low part of LE TLS instruction pattern.  */
   11103 
   11104 static void
   11105 nds32_elf_relax_letlslo12 (struct bfd_link_info *link_info, bfd *abfd,
   11106 			   Elf_Internal_Rela *irel,
   11107 			   bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   11108 			   Elf_Internal_Shdr *symtab_hdr)
   11109 {
   11110   uint32_t insn;
   11111   bfd_vma laddr;
   11112   bfd_signed_vma foff;
   11113   unsigned long reloc;
   11114 
   11115   laddr = irel->r_offset;
   11116   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
   11117   BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
   11118   foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
   11119   insn = bfd_getb32 (contents + laddr);
   11120 
   11121   if ( (bfd_signed_vma) (foff) < CONSERVATIVE_20BIT
   11122       && (bfd_signed_vma) (foff) >= -CONSERVATIVE_20BIT)
   11123     {
   11124       /* Pattern sethi-ori transform to movi.  */
   11125       reloc = R_NDS32_TLS_LE_20;
   11126       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
   11127       insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
   11128       bfd_putb32 (insn, contents + laddr);
   11129     }
   11130 }
   11131 
   11132 /* Relax LE TLS calculate address instruction pattern.  */
   11133 
   11134 static void
   11135 nds32_elf_relax_letlsadd (struct bfd_link_info *link_info, bfd *abfd,
   11136 			  asection *sec, Elf_Internal_Rela *irel,
   11137 			  Elf_Internal_Rela *internal_relocs,
   11138 			  bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   11139 			  Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
   11140 {
   11141   /* Local TLS non-pic
   11142      sethi    ta, hi20(symbol@tpoff)      ; TLS_LE_HI20
   11143      ori      ta, ta, lo12(symbol@tpoff)  ; TLS_LE_LO12
   11144      add      ra, ta, tp                  ; TLS_LE_ADD */
   11145 
   11146   uint32_t insn;
   11147   bfd_vma laddr;
   11148   bfd_signed_vma foff;
   11149   Elf_Internal_Rela *i1_irelfn, *irelend;
   11150 
   11151   irelend = internal_relocs + sec->reloc_count;
   11152   laddr = irel->r_offset;
   11153   insn = bfd_getb32 (contents + laddr);
   11154   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
   11155 				      R_NDS32_PTR_RESOLVED);
   11156   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
   11157   BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
   11158   foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
   11159 
   11160   /* The range is +/-16k.  */
   11161   if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
   11162       && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
   11163     {
   11164       /* Transform add to addi.  */
   11165       insn = N32_TYPE2 (ADDI, N32_RT5 (insn), N32_RB5 (insn), 0);
   11166       irel->r_info =
   11167 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
   11168 
   11169       bfd_putb32 (insn, contents + laddr);
   11170       if (i1_irelfn != irelend)
   11171 	{
   11172 	  i1_irelfn->r_addend |= 1;
   11173 	  *again = TRUE;
   11174 	}
   11175     }
   11176 }
   11177 
   11178 /* Relax LE TLS load store instruction pattern.  */
   11179 
   11180 static void
   11181 nds32_elf_relax_letlsls (struct bfd_link_info *link_info, bfd *abfd,
   11182 			 asection *sec, Elf_Internal_Rela *irel,
   11183 			 Elf_Internal_Rela *internal_relocs,
   11184 			 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   11185 			 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
   11186 {
   11187 
   11188   uint32_t insn;
   11189   bfd_vma laddr;
   11190   bfd_signed_vma foff;
   11191   Elf_Internal_Rela *i1_irelfn, *irelend;
   11192   int success = 0;
   11193 
   11194   irelend = internal_relocs + sec->reloc_count;
   11195   laddr = irel->r_offset;
   11196   insn = bfd_getb32 (contents + laddr);
   11197   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
   11198 				      R_NDS32_PTR_RESOLVED);
   11199   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
   11200   BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
   11201   foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
   11202 
   11203   switch ((N32_OP6 (insn) << 8) | (insn & 0xff))
   11204     {
   11205     case (N32_OP6_MEM << 8) | N32_MEM_LB:
   11206     case (N32_OP6_MEM << 8) | N32_MEM_SB:
   11207     case (N32_OP6_MEM << 8) | N32_MEM_LBS:
   11208       /* The range is +/-16k.  */
   11209       if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
   11210 	  && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
   11211 	{
   11212 	  insn =
   11213 	    ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
   11214 	  irel->r_info =
   11215 	    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
   11216 	  success = 1;
   11217 	  break;
   11218 	}
   11219     case (N32_OP6_MEM << 8) | N32_MEM_LH:
   11220     case (N32_OP6_MEM << 8) | N32_MEM_SH:
   11221     case (N32_OP6_MEM << 8) | N32_MEM_LHS:
   11222       /* The range is +/-32k.  */
   11223       if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S1
   11224 	  && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S1)
   11225 	{
   11226 	  insn =
   11227 	    ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
   11228 	  irel->r_info =
   11229 	    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S1);
   11230 	  success = 1;
   11231 	  break;
   11232 	}
   11233     case (N32_OP6_MEM << 8) | N32_MEM_LW:
   11234     case (N32_OP6_MEM << 8) | N32_MEM_SW:
   11235       /* The range is +/-64k.  */
   11236       if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S2
   11237 	  && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S2)
   11238 	{
   11239 	  insn =
   11240 	    ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
   11241 	  irel->r_info =
   11242 	    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S2);
   11243 	  success = 1;
   11244 	  break;
   11245 	}
   11246     default:
   11247       break;
   11248     }
   11249 
   11250   if (success)
   11251     {
   11252       bfd_putb32 (insn, contents + laddr);
   11253       if (i1_irelfn != irelend)
   11254 	{
   11255 	  i1_irelfn->r_addend |= 1;
   11256 	  *again = TRUE;
   11257 	}
   11258     }
   11259 }
   11260 
   11261 /* Relax PTR relocation for nds32_elf_relax_section.  */
   11262 
   11263 static bfd_boolean
   11264 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
   11265 		     Elf_Internal_Rela *internal_relocs, int *insn_len,
   11266 		     int *seq_len, bfd_byte *contents)
   11267 {
   11268   Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
   11269 
   11270   irelend = internal_relocs + sec->reloc_count;
   11271 
   11272   re_irel =
   11273     find_relocs_at_address_addr (irel, internal_relocs, irelend,
   11274 				 R_NDS32_PTR_RESOLVED, irel->r_addend);
   11275 
   11276   if (re_irel == irelend)
   11277     {
   11278       (*_bfd_error_handler)
   11279 	("%B: warning: R_NDS32_PTR points to unrecognized reloc at 0x%lx.",
   11280 	 abfd, (long) irel->r_offset);
   11281       return FALSE;
   11282     }
   11283 
   11284   if (re_irel->r_addend != 1)
   11285     return FALSE;
   11286 
   11287   /* Pointed target is relaxed and no longer needs this void *,
   11288      change the type to NONE.  */
   11289   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   11290 
   11291   /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
   11292      not exist, it means only count 1 and remove it directly.  */
   11293   /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
   11294   count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
   11295 				       R_NDS32_PTR_COUNT);
   11296   ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
   11297 				     R_NDS32_PTR);
   11298   if (count_irel != irelend)
   11299     {
   11300       if (--count_irel->r_addend > 0)
   11301 	return FALSE;
   11302     }
   11303 
   11304   if (ptr_irel != irelend)
   11305     return FALSE;
   11306 
   11307   /* If the PTR_COUNT is already 0, remove current instruction.  */
   11308   *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
   11309   *insn_len = 0;
   11310   return TRUE;
   11311 }
   11312 
   11313 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section.  */
   11314 
   11315 static void
   11316 nds32_elf_relax_pltgot_suff (struct bfd_link_info *link_info, bfd *abfd,
   11317 			     asection *sec, Elf_Internal_Rela *irel,
   11318 			     Elf_Internal_Rela *internal_relocs,
   11319 			     bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   11320 			     Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
   11321 {
   11322   uint32_t insn;
   11323   bfd_signed_vma foff;
   11324   Elf_Internal_Rela *i1_irelfn, *irelend;
   11325   bfd_vma local_sda, laddr;
   11326 
   11327   irelend = internal_relocs + sec->reloc_count;
   11328   laddr = irel->r_offset;
   11329   insn = bfd_getb32 (contents + laddr);
   11330 
   11331   /* FIXME: It's a little trouble to turn JRAL5 to JAL since
   11332      we need additional space.  It might be help if we could
   11333      borrow some space from instructions to be eliminated
   11334      such as sethi, ori, add.  */
   11335   if (insn & 0x80000000)
   11336     return;
   11337 
   11338   if (nds32_elf_check_dup_relocs
   11339       (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
   11340     return;
   11341 
   11342   i1_irelfn =
   11343     find_relocs_at_address (irel, internal_relocs, irelend,
   11344 			    R_NDS32_PTR_RESOLVED);
   11345 
   11346   /* FIXIT 090606
   11347      The boundary should be reduced since the .plt section hasn't
   11348      been created and the address of specific entry is still unknown
   11349      Maybe the range between the function call and the begin of the
   11350      .text section can be used to decide if the .plt is in the range
   11351      of function call.  */
   11352 
   11353   if (N32_OP6 (insn) == N32_OP6_ALU1
   11354       && N32_SUB5 (insn) == N32_ALU1_ADD)
   11355     {
   11356       /* Get the value of the symbol referred to by the reloc.  */
   11357       nds32_elf_final_sda_base (sec->output_section->owner, link_info,
   11358 				&local_sda, FALSE);
   11359       foff = (bfd_signed_vma) (calculate_plt_memory_address
   11360 			       (abfd, link_info, isymbuf, irel,
   11361 				symtab_hdr) - local_sda);
   11362       /* This condition only happened when symbol is undefined.  */
   11363       if (foff == 0)
   11364 	return;
   11365 
   11366       if (foff < -CONSERVATIVE_19BIT || foff >= CONSERVATIVE_19BIT)
   11367 	return;
   11368       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   11369 				   R_NDS32_PLT_GOTREL_LO19);
   11370       /* addi.gp */
   11371       insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
   11372     }
   11373   else if (N32_OP6 (insn) == N32_OP6_JREG
   11374 	   && N32_SUB5 (insn) == N32_JREG_JRAL)
   11375     {
   11376       /* Get the value of the symbol referred to by the reloc.  */
   11377       foff =
   11378 	calculate_plt_offset (abfd, sec, link_info, isymbuf, irel, symtab_hdr);
   11379       /* This condition only happened when symbol is undefined.  */
   11380       if (foff == 0)
   11381 	return;
   11382       if (foff < -CONSERVATIVE_24BIT_S1 || foff >= CONSERVATIVE_24BIT_S1)
   11383 	return;
   11384       irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
   11385       insn = INSN_JAL;
   11386     }
   11387   else
   11388     return;
   11389 
   11390   bfd_putb32 (insn, contents + laddr);
   11391   if (i1_irelfn != irelend)
   11392     {
   11393       i1_irelfn->r_addend |= 1;
   11394       *again = TRUE;
   11395     }
   11396 }
   11397 
   11398 /* Relax GOT_SUFF relocation for nds32_elf_relax_section.  */
   11399 
   11400 static void
   11401 nds32_elf_relax_got_suff (struct bfd_link_info *link_info, bfd *abfd,
   11402 			  asection *sec, Elf_Internal_Rela *irel,
   11403 			  Elf_Internal_Rela *internal_relocs,
   11404 			  bfd_byte *contents, Elf_Internal_Shdr *symtab_hdr,
   11405 			  bfd_boolean *again)
   11406 {
   11407   uint32_t insn;
   11408   bfd_signed_vma foff;
   11409   Elf_Internal_Rela *i1_irelfn, *irelend;
   11410   bfd_vma local_sda, laddr;
   11411 
   11412   irelend = internal_relocs + sec->reloc_count;
   11413   laddr = irel->r_offset;
   11414   insn = bfd_getb32 (contents + laddr);
   11415   if (insn & 0x80000000)
   11416     return;
   11417 
   11418   if (nds32_elf_check_dup_relocs
   11419       (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
   11420     return;
   11421 
   11422   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
   11423 				      R_NDS32_PTR_RESOLVED);
   11424 
   11425   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
   11426 			    &local_sda, FALSE);
   11427   foff = calculate_got_memory_address (abfd, link_info, irel,
   11428 				       symtab_hdr) - local_sda;
   11429 
   11430   if (foff < CONSERVATIVE_19BIT && foff >= -CONSERVATIVE_19BIT)
   11431     {
   11432       /* Turn LW to LWI.GP.  Change relocation type to R_NDS32_GOT_REL.  */
   11433       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
   11434       irel->r_info =
   11435 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
   11436       bfd_putb32 (insn, contents + laddr);
   11437       if (i1_irelfn != irelend)
   11438 	{
   11439 	  i1_irelfn->r_addend |= 1;
   11440 	  *again = TRUE;
   11441 	}
   11442     }
   11443 }
   11444 
   11445 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section.  */
   11446 
   11447 static void
   11448 nds32_elf_relax_gotoff_suff (struct bfd_link_info *link_info, bfd *abfd,
   11449 			     asection *sec, Elf_Internal_Rela *irel,
   11450 			     Elf_Internal_Rela *internal_relocs,
   11451 			     bfd_byte *contents, Elf_Internal_Sym *isymbuf,
   11452 			     Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
   11453 {
   11454   int opc_insn_gotoff;
   11455   uint32_t insn;
   11456   bfd_signed_vma foff;
   11457   Elf_Internal_Rela *i1_irelfn, *i2_irelfn, *irelend;
   11458   bfd_vma local_sda, laddr;
   11459 
   11460   irelend = internal_relocs + sec->reloc_count;
   11461   laddr = irel->r_offset;
   11462   insn = bfd_getb32 (contents + laddr);
   11463 
   11464   if (insn & 0x80000000)
   11465     return;
   11466 
   11467   if (nds32_elf_check_dup_relocs
   11468       (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
   11469     return;
   11470 
   11471   i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
   11472 				      R_NDS32_PTR_RESOLVED);
   11473   nds32_elf_final_sda_base (sec->output_section->owner, link_info,
   11474 			    &local_sda, FALSE);
   11475   foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
   11476   foff = foff - local_sda;
   11477 
   11478   if (foff >= CONSERVATIVE_19BIT || foff < -CONSERVATIVE_19BIT)
   11479     return;
   11480 
   11481   /* Concatenate opcode and sub-opcode for switch case.
   11482      It may be MEM or ALU1.  */
   11483   opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
   11484   switch (opc_insn_gotoff)
   11485     {
   11486     case (N32_OP6_MEM << 8) | N32_MEM_LW:
   11487       /* 4-byte aligned.  */
   11488       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
   11489       irel->r_info =
   11490 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
   11491       break;
   11492     case (N32_OP6_MEM << 8) | N32_MEM_SW:
   11493       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
   11494       irel->r_info =
   11495 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
   11496       break;
   11497     case (N32_OP6_MEM << 8) | N32_MEM_LH:
   11498       /* 2-byte aligned.  */
   11499       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
   11500       irel->r_info =
   11501 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
   11502       break;
   11503     case (N32_OP6_MEM << 8) | N32_MEM_LHS:
   11504       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
   11505       irel->r_info =
   11506 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
   11507       break;
   11508     case (N32_OP6_MEM << 8) | N32_MEM_SH:
   11509       insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
   11510       irel->r_info =
   11511 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
   11512       break;
   11513     case (N32_OP6_MEM << 8) | N32_MEM_LB:
   11514       /* 1-byte aligned.  */
   11515       insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
   11516       irel->r_info =
   11517 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
   11518       break;
   11519     case (N32_OP6_MEM << 8) | N32_MEM_LBS:
   11520       insn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
   11521       irel->r_info =
   11522 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
   11523       break;
   11524     case (N32_OP6_MEM << 8) | N32_MEM_SB:
   11525       insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
   11526       irel->r_info =
   11527 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
   11528       break;
   11529     case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD:
   11530       insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
   11531       irel->r_info =
   11532 	ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
   11533       break;
   11534     default:
   11535       return;
   11536     }
   11537 
   11538   bfd_putb32 (insn, contents + laddr);
   11539   if (i1_irelfn != irelend)
   11540     {
   11541       i1_irelfn->r_addend |= 1;
   11542       *again = TRUE;
   11543     }
   11544   if ((i2_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
   11545 					   R_NDS32_INSN16)) != irelend)
   11546     i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   11547 
   11548 }
   11549 
   11550 static bfd_boolean
   11551 nds32_relax_adjust_label (bfd *abfd, asection *sec,
   11552 			  Elf_Internal_Rela *internal_relocs,
   11553 			  bfd_byte *contents,
   11554 			  nds32_elf_blank_t **relax_blank_list,
   11555 			  int optimize, int opt_size)
   11556 {
   11557   /* This code block is used to adjust 4-byte alignment by relax a pair
   11558      of instruction a time.
   11559 
   11560      It recognizes three types of relocations.
   11561      1. R_NDS32_LABEL - a aligment.
   11562      2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
   11563      3. is_16bit_NOP () - remove a 16-bit instruction.  */
   11564 
   11565   /* TODO: It seems currently implementation only support 4-byte aligment.
   11566      We should handle any-aligment.  */
   11567 
   11568   Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
   11569   Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
   11570   Elf_Internal_Rela rel_temp;
   11571   Elf_Internal_Rela *irelend;
   11572   bfd_vma address;
   11573   uint16_t insn16;
   11574 
   11575   /* Checking for branch relaxation relies on the relocations to
   11576      be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
   11577   nds32_insertion_sort (internal_relocs, sec->reloc_count,
   11578 			sizeof (Elf_Internal_Rela), compar_reloc);
   11579 
   11580   irelend = internal_relocs + sec->reloc_count;
   11581 
   11582   /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
   11583   /* FIXME: Can we generate the right order in assembler?
   11584      So we don't have to swapping them here.  */
   11585 
   11586   for (label_rel = internal_relocs, insn_rel = internal_relocs;
   11587        label_rel < irelend; label_rel++)
   11588     {
   11589       if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
   11590 	continue;
   11591 
   11592       /* Find the first reloc has the same offset with label_rel.  */
   11593       while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
   11594 	insn_rel++;
   11595 
   11596       for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
   11597 	   insn_rel++)
   11598 	/* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
   11599 	   address.  */
   11600 	if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
   11601 	  break;
   11602 
   11603       if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
   11604 	  && insn_rel < label_rel)
   11605 	{
   11606 	  /* Swap the two reloc if the R_NDS32_INSN16 is
   11607 	     before R_NDS32_LABEL.  */
   11608 	  memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
   11609 	  memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
   11610 	  memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
   11611 	}
   11612     }
   11613 
   11614   label_rel = NULL;
   11615   insn_rel = NULL;
   11616   /* If there were a sequence of R_NDS32_LABEL end up with .align 2
   11617      or higher, remove other R_NDS32_LABEL with lower alignment.
   11618      If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
   11619      then the R_NDS32_LABEL sequence is broke.  */
   11620   for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
   11621     {
   11622       if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
   11623 	{
   11624 	  if (label_rel == NULL)
   11625 	    {
   11626 	      if (tmp_rel->r_addend < 2)
   11627 		label_rel = tmp_rel;
   11628 	      continue;
   11629 	    }
   11630 	  else if (tmp_rel->r_addend > 1)
   11631 	    {
   11632 	      /* Remove all LABEL relocation from label_rel to tmp_rel
   11633 		 including relocations with same offset as tmp_rel.  */
   11634 	      for (tmp2_rel = label_rel; tmp2_rel < tmp_rel
   11635 		   || tmp2_rel->r_offset == tmp_rel->r_offset; tmp2_rel++)
   11636 		{
   11637 		  if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
   11638 		      && tmp2_rel->r_addend < 2)
   11639 		    tmp2_rel->r_info =
   11640 		      ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
   11641 				    R_NDS32_NONE);
   11642 		}
   11643 	      label_rel = NULL;
   11644 	    }
   11645 	}
   11646       else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
   11647 	{
   11648 	  /* A new INSN16 which can be converted, so clear label_rel.  */
   11649 	  if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
   11650 				   irelend, &insn16)
   11651 	      || is_16bit_NOP (abfd, sec, tmp_rel))
   11652 	    label_rel = NULL;
   11653 	}
   11654     }
   11655 
   11656   label_rel = NULL;
   11657   insn_rel = NULL;
   11658   /* Optimized for speed and nothing has not been relaxed.
   11659      It's time to align labels.
   11660      We may convert a 16-bit instruction right before a label to
   11661      32-bit, in order to align the label if necessary
   11662      all reloc entries has been sorted by r_offset.  */
   11663   for (irel = internal_relocs; irel < irelend; irel++)
   11664     {
   11665       if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
   11666 	  && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
   11667 	continue;
   11668 
   11669       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
   11670 	{
   11671 	  /* A new INSN16 found, resize the old one.  */
   11672 	  if (is_convert_32_to_16
   11673 	      (abfd, sec, irel, internal_relocs, irelend, &insn16)
   11674 	      || is_16bit_NOP (abfd, sec, irel))
   11675 	    {
   11676 	      if (insn_rel)
   11677 		{
   11678 		  /* Previous INSN16 reloc exists, reduce its
   11679 		     size to 16-bit.  */
   11680 		  if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
   11681 					   irelend, &insn16))
   11682 		    {
   11683 		      nds32_elf_write_16 (abfd, contents, insn_rel,
   11684 					  internal_relocs, irelend, insn16);
   11685 
   11686 		      if (!insert_nds32_elf_blank_recalc_total
   11687 			  (relax_blank_list, insn_rel->r_offset + 2, 2))
   11688 			return FALSE;
   11689 		    }
   11690 		  else if (is_16bit_NOP (abfd, sec, insn_rel))
   11691 		    {
   11692 		      if (!insert_nds32_elf_blank_recalc_total
   11693 			  (relax_blank_list, insn_rel->r_offset, 2))
   11694 			return FALSE;
   11695 		    }
   11696 		  insn_rel->r_info =
   11697 		    ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
   11698 		}
   11699 	      /* Save the new one for later use.  */
   11700 	      insn_rel = irel;
   11701 	    }
   11702 	  else
   11703 	    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
   11704 					 R_NDS32_NONE);
   11705 	}
   11706       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
   11707 	{
   11708 	  /* Search for label.  */
   11709 	  int force_relax = 0;
   11710 
   11711 	  /* Label on 16-bit instruction or optimization
   11712 	     needless, just reset this reloc.  */
   11713 	  insn16 = bfd_getb16 (contents + irel->r_offset);
   11714 	  if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
   11715 	    {
   11716 	      irel->r_info =
   11717 		ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
   11718 	      continue;
   11719 	    }
   11720 
   11721 	  address =
   11722 	    irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
   11723 							irel->r_offset, 1);
   11724 
   11725 	  if (!insn_rel)
   11726 	    {
   11727 	      /* Check if there is case which can not be aligned.  */
   11728 	      if (irel->r_addend == 2 && address & 0x2)
   11729 		return FALSE;
   11730 	      continue;
   11731 	    }
   11732 
   11733 	  /* Try to align this label.  */
   11734 
   11735 	  if ((irel->r_addend & 0x1f) < 2)
   11736 	    {
   11737 	      /* Check if there is a INSN16 at the same address.
   11738 		 Label_rel always seats before insn_rel after
   11739 		 our sort.  */
   11740 
   11741 	      /* Search for INSN16 at LABEL location.  If INSN16 is at
   11742 		 same location and this LABEL alignment is lower than 2,
   11743 		 the INSN16 can be converted to 2-byte.  */
   11744 	      for (tmp_rel = irel;
   11745 		   tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
   11746 		   tmp_rel++)
   11747 		{
   11748 		  if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
   11749 		      && (is_convert_32_to_16
   11750 			  (abfd, sec, tmp_rel, internal_relocs,
   11751 			   irelend, &insn16)
   11752 			  || is_16bit_NOP (abfd, sec, tmp_rel)))
   11753 		    {
   11754 		      force_relax = 1;
   11755 		      break;
   11756 		    }
   11757 		}
   11758 	    }
   11759 
   11760 	  if (force_relax || irel->r_addend == 1 || address & 0x2)
   11761 	    {
   11762 	      /* Label not aligned.  */
   11763 	      /* Previous reloc exists, reduce its size to 16-bit.  */
   11764 	      if (is_convert_32_to_16 (abfd, sec, insn_rel,
   11765 				       internal_relocs, irelend, &insn16))
   11766 		{
   11767 		  nds32_elf_write_16 (abfd, contents, insn_rel,
   11768 				      internal_relocs, irelend, insn16);
   11769 
   11770 		  if (!insert_nds32_elf_blank_recalc_total
   11771 		      (relax_blank_list, insn_rel->r_offset + 2, 2))
   11772 		    return FALSE;
   11773 		}
   11774 	      else if (is_16bit_NOP (abfd, sec, insn_rel))
   11775 		{
   11776 		  if (!insert_nds32_elf_blank_recalc_total
   11777 		      (relax_blank_list, insn_rel->r_offset, 2))
   11778 		    return FALSE;
   11779 		}
   11780 
   11781 	    }
   11782 	  /* INSN16 reloc is used.  */
   11783 	  insn_rel = NULL;
   11784 	}
   11785     }
   11786 
   11787   address =
   11788     sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
   11789   if (insn_rel && (address & 0x2 || opt_size))
   11790     {
   11791       if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
   11792 			       irelend, &insn16))
   11793 	{
   11794 	  nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
   11795 			      irelend, insn16);
   11796 	  if (!insert_nds32_elf_blank_recalc_total
   11797 	      (relax_blank_list, insn_rel->r_offset + 2, 2))
   11798 	    return FALSE;
   11799 	  insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
   11800 					   R_NDS32_NONE);
   11801 	}
   11802       else if (is_16bit_NOP (abfd, sec, insn_rel))
   11803 	{
   11804 	  if (!insert_nds32_elf_blank_recalc_total
   11805 	      (relax_blank_list, insn_rel->r_offset, 2))
   11806 	    return FALSE;
   11807 	  insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
   11808 					   R_NDS32_NONE);
   11809 	}
   11810     }
   11811   insn_rel = NULL;
   11812   return TRUE;
   11813 }
   11814 
   11815 /* Pick relaxation round.  */
   11816 
   11817 static int
   11818 nds32_elf_pick_relax (bfd_boolean init, asection *sec, bfd_boolean *again,
   11819 		      struct elf_nds32_link_hash_table *table,
   11820 		      struct bfd_link_info *link_info)
   11821 {
   11822   static asection *final_sec;
   11823   static bfd_boolean set = FALSE;
   11824   static bfd_boolean first = TRUE;
   11825   int round_table[] = {
   11826       NDS32_RELAX_NORMAL_ROUND,
   11827       NDS32_RELAX_JUMP_IFC_ROUND,
   11828       NDS32_RELAX_EX9_BUILD_ROUND,
   11829       NDS32_RELAX_EX9_REPLACE_ROUND,
   11830   };
   11831   static int pass = 0;
   11832   static int relax_round;
   11833 
   11834   if (first)
   11835     {
   11836       /* Run an empty run to get the final section.  */
   11837       relax_round = NDS32_RELAX_EMPTY_ROUND;
   11838 
   11839       /* It has to enter relax again because we can
   11840 	 not make sure what the final turn is.  */
   11841       *again = TRUE;
   11842       first = FALSE;
   11843     }
   11844 
   11845   if (!set && *again)
   11846     {
   11847       /* It is reentered when again is FALSE.  */
   11848       final_sec = sec;
   11849       return relax_round;
   11850     }
   11851 
   11852   /* The second round begins.  */
   11853   set = TRUE;
   11854 
   11855   relax_round = round_table[pass];
   11856 
   11857   if (!init && final_sec == sec)
   11858     {
   11859       switch (relax_round)
   11860 	{
   11861 	case NDS32_RELAX_NORMAL_ROUND:
   11862 	  if (!*again)
   11863 	    {
   11864 	      /* Normal relaxation done.  */
   11865 	      if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
   11866 		{
   11867 		  pass++;
   11868 		  *again = TRUE;
   11869 		}
   11870 	      else if (table->target_optimize & NDS32_RELAX_EX9_ON)
   11871 		{
   11872 		  pass += 2;	/* NDS32_RELAX_EX9_BUILD_ROUND */
   11873 		  *again = TRUE;
   11874 		}
   11875 	      else if (table->ex9_import_file)
   11876 		{
   11877 		  /* Import ex9 table.  */
   11878 		  if (table->update_ex9_table)
   11879 		    pass += 2;	/* NDS32_RELAX_EX9_BUILD_ROUND */
   11880 		  else
   11881 		    pass += 3;	/* NDS32_RELAX_EX9_REPLACE_ROUND */
   11882 		  nds32_elf_ex9_import_table (link_info);
   11883 		  *again = TRUE;
   11884 		}
   11885 	    }
   11886 	  break;
   11887 	case NDS32_RELAX_JUMP_IFC_ROUND:
   11888 	  if (!nds32_elf_ifc_finish (link_info))
   11889 	    (*_bfd_error_handler) (_("error: Jump IFC Fail."));
   11890 	  if (table->target_optimize & NDS32_RELAX_EX9_ON)
   11891 	    {
   11892 	      pass++;
   11893 	      *again = TRUE;
   11894 	    }
   11895 	  break;
   11896 	case NDS32_RELAX_EX9_BUILD_ROUND:
   11897 	  nds32_elf_ex9_finish (link_info);
   11898 	  pass++;
   11899 	  *again = TRUE;
   11900 	  break;
   11901 	case NDS32_RELAX_EX9_REPLACE_ROUND:
   11902 	  if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
   11903 	    {
   11904 	      /* Do jump IFC optimization again.  */
   11905 	      if (!nds32_elf_ifc_finish (link_info))
   11906 		(*_bfd_error_handler) (_("error: Jump IFC Fail."));
   11907 	    }
   11908 	  break;
   11909 	default:
   11910 	  break;
   11911 	}
   11912     }
   11913 
   11914   return relax_round;
   11915 }
   11916 
   11917 static bfd_boolean
   11918 nds32_elf_relax_section (bfd *abfd, asection *sec,
   11919 			 struct bfd_link_info *link_info, bfd_boolean *again)
   11920 {
   11921   nds32_elf_blank_t *relax_blank_list = NULL;
   11922   Elf_Internal_Shdr *symtab_hdr;
   11923   Elf_Internal_Rela *internal_relocs;
   11924   Elf_Internal_Rela *irel;
   11925   Elf_Internal_Rela *irelend;
   11926   Elf_Internal_Sym *isymbuf = NULL;
   11927   bfd_byte *contents = NULL;
   11928   bfd_boolean result = TRUE;
   11929   int optimize = 0;
   11930   int opt_size = 0;
   11931   uint32_t insn;
   11932   uint16_t insn16;
   11933 
   11934   /* Target dependnet option.  */
   11935   struct elf_nds32_link_hash_table *table;
   11936   int load_store_relax;
   11937   int relax_round;
   11938 
   11939   relax_blank_list = NULL;
   11940 
   11941   *again = FALSE;
   11942 
   11943   /* Nothing to do for
   11944    * relocatable link or
   11945    * non-relocatable section or
   11946    * non-code section or
   11947    * empty content or
   11948    * no reloc entry.  */
   11949   if (link_info->relocatable
   11950       || (sec->flags & SEC_RELOC) == 0
   11951       || (sec->flags & SEC_EXCLUDE) == 1
   11952       || (sec->flags & SEC_CODE) == 0
   11953       || sec->size == 0)
   11954     return TRUE;
   11955 
   11956   /* 09.12.11 Workaround.  */
   11957   /*  We have to adjust align for R_NDS32_LABEL if needed.
   11958       The adjust approach only can fix 2-byte align once.  */
   11959   if (sec->alignment_power > 2)
   11960     return TRUE;
   11961 
   11962   /* The optimization type to do.  */
   11963 
   11964   table = nds32_elf_hash_table (link_info);
   11965   relax_round = nds32_elf_pick_relax (TRUE, sec, again, table, link_info);
   11966   switch (relax_round)
   11967     {
   11968     case NDS32_RELAX_JUMP_IFC_ROUND:
   11969       /* Here is the entrance of ifc jump relaxation.  */
   11970       if (!nds32_elf_ifc_calc (link_info, abfd, sec))
   11971 	return FALSE;
   11972       nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
   11973       return TRUE;
   11974 
   11975     case NDS32_RELAX_EX9_BUILD_ROUND:
   11976       /* Here is the entrance of ex9 relaxation.  There are two pass of
   11977 	 ex9 relaxation.  The one is to traverse all instructions and build
   11978 	 the hash table.  The other one is to compare instructions and replace
   11979 	 it by ex9.it.  */
   11980       if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
   11981 	return FALSE;
   11982       nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
   11983       return TRUE;
   11984 
   11985     case NDS32_RELAX_EX9_REPLACE_ROUND:
   11986       if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
   11987 	return FALSE;
   11988       return TRUE;
   11989 
   11990     case NDS32_RELAX_EMPTY_ROUND:
   11991       nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
   11992       return TRUE;
   11993 
   11994     case NDS32_RELAX_NORMAL_ROUND:
   11995     default:
   11996       if (sec->reloc_count == 0)
   11997 	return TRUE;
   11998       break;
   11999     }
   12000 
   12001   /* The begining of general relaxation.  */
   12002 
   12003   if (is_SDA_BASE_set == 0)
   12004     {
   12005       bfd_vma gp;
   12006       is_SDA_BASE_set = 1;
   12007       nds32_elf_final_sda_base (sec->output_section->owner, link_info,
   12008 				&gp, FALSE);
   12009       relax_range_measurement (abfd);
   12010     }
   12011 
   12012   if (is_ITB_BASE_set == 0)
   12013     {
   12014       /* Set the _ITB_BASE_.  */
   12015       if (!nds32_elf_ex9_itb_base (link_info))
   12016 	{
   12017 	  (*_bfd_error_handler) (_("%B: error: Cannot set _ITB_BASE_"), abfd);
   12018 	  bfd_set_error (bfd_error_bad_value);
   12019 	}
   12020     }
   12021 
   12022   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12023   /* Relocations MUST be kept in memory, because relaxation adjust them.  */
   12024   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   12025 					       TRUE /* keep_memory */);
   12026   if (internal_relocs == NULL)
   12027     goto error_return;
   12028 
   12029   irelend = internal_relocs + sec->reloc_count;
   12030   irel = find_relocs_at_address (internal_relocs, internal_relocs,
   12031 				 irelend, R_NDS32_RELAX_ENTRY);
   12032 
   12033   if (irel == irelend)
   12034     return TRUE;
   12035 
   12036   if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
   12037     {
   12038       if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
   12039 	return TRUE;
   12040 
   12041       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
   12042 	optimize = 1;
   12043 
   12044       if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
   12045 	opt_size = 1;
   12046     }
   12047 
   12048   load_store_relax = table->load_store_relax;
   12049 
   12050   /* Get symbol table and section content.  */
   12051   if (!nds32_get_section_contents (abfd, sec, &contents)
   12052       || !nds32_get_local_syms (abfd, sec, &isymbuf))
   12053     goto error_return;
   12054 
   12055   /* Do relax loop only when finalize is not done.
   12056      Take care of relaxable relocs except INSN16.  */
   12057   for (irel = internal_relocs; irel < irelend; irel++)
   12058     {
   12059       int seq_len;		/* Original length of instruction sequence.  */
   12060       int insn_len = 0;		/* Final length of instruction sequence.  */
   12061       bfd_boolean removed;
   12062 
   12063       insn = 0;
   12064       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
   12065 	  && (irel->r_addend & 0x1f) >= 2)
   12066 	optimize = 1;
   12067 
   12068       /* Relocation Types
   12069 	 R_NDS32_LONGCALL1	53
   12070 	 R_NDS32_LONGCALL2	54
   12071 	 R_NDS32_LONGCALL3	55
   12072 	 R_NDS32_LONGJUMP1	56
   12073 	 R_NDS32_LONGJUMP2	57
   12074 	 R_NDS32_LONGJUMP3	58
   12075 	 R_NDS32_LOADSTORE	59  */
   12076       if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
   12077 	  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
   12078 	seq_len = GET_SEQ_LEN (irel->r_addend);
   12079 
   12080       /* Relocation Types
   12081 	 R_NDS32_LONGCALL4	107
   12082 	 R_NDS32_LONGCALL5	108
   12083 	 R_NDS32_LONGCALL6	109
   12084 	 R_NDS32_LONGJUMP4	110
   12085 	 R_NDS32_LONGJUMP5	111
   12086 	 R_NDS32_LONGJUMP6	112
   12087 	 R_NDS32_LONGJUMP7	113  */
   12088       else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
   12089 	       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
   12090 	seq_len = 4;
   12091 
   12092 	/* Relocation Types
   12093 	 R_NDS32_LO12S0_RELA		30
   12094 	 R_NDS32_LO12S1_RELA		29
   12095 	 R_NDS32_LO12S2_RELA		28
   12096 	 R_NDS32_LO12S2_SP_RELA		71
   12097 	 R_NDS32_LO12S2_DP_RELA		70
   12098 	 R_NDS32_GOT_LO12		46
   12099 	 R_NDS32_GOTOFF_LO12		50
   12100 	 R_NDS32_PLTREL_LO12		65
   12101 	 R_NDS32_PLT_GOTREL_LO12	67
   12102 	 R_NDS32_17IFC_PCREL_RELA	96
   12103 	 R_NDS32_GOT_SUFF		193
   12104 	 R_NDS32_GOTOFF_SUFF		194
   12105 	 R_NDS32_PLT_GOT_SUFF		195
   12106 	 R_NDS32_MULCALL_SUFF		196
   12107 	 R_NDS32_PTR			197  */
   12108       else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
   12109 		&& ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
   12110 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
   12111 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
   12112 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
   12113 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
   12114 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
   12115 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
   12116 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
   12117 	       || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
   12118 		   && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
   12119 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
   12120 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
   12121 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
   12122 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS)
   12123 	seq_len = 0;
   12124       else
   12125 	continue;
   12126 
   12127       insn_len = seq_len;
   12128       removed = FALSE;
   12129 
   12130       switch (ELF32_R_TYPE (irel->r_info))
   12131 	{
   12132 	case R_NDS32_LONGCALL1:
   12133 	  removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
   12134 					       &insn_len, contents, isymbuf,
   12135 					       symtab_hdr);
   12136 	  break;
   12137 	case R_NDS32_LONGCALL2:
   12138 	  removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
   12139 					       &insn_len, contents, isymbuf,
   12140 					       symtab_hdr);
   12141 	  break;
   12142 	case R_NDS32_LONGCALL3:
   12143 	  removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
   12144 					       &insn_len, contents, isymbuf,
   12145 					       symtab_hdr);
   12146 	  break;
   12147 	case R_NDS32_LONGJUMP1:
   12148 	  removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
   12149 					       &insn_len, contents, isymbuf,
   12150 					       symtab_hdr);
   12151 	  break;
   12152 	case R_NDS32_LONGJUMP2:
   12153 	  removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
   12154 					       &insn_len, contents, isymbuf,
   12155 					       symtab_hdr);
   12156 	  break;
   12157 	case R_NDS32_LONGJUMP3:
   12158 	  removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
   12159 					       &insn_len, contents, isymbuf,
   12160 					       symtab_hdr);
   12161 	  break;
   12162 	case R_NDS32_LONGCALL4:
   12163 	  removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
   12164 					       &insn_len, contents, isymbuf,
   12165 					       symtab_hdr);
   12166 	  break;
   12167 	case R_NDS32_LONGCALL5:
   12168 	  removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
   12169 					       &insn_len, contents, isymbuf,
   12170 					       symtab_hdr);
   12171 	  break;
   12172 	case R_NDS32_LONGCALL6:
   12173 	  removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
   12174 					       &insn_len, contents, isymbuf,
   12175 					       symtab_hdr);
   12176 	  break;
   12177 	case R_NDS32_LONGJUMP4:
   12178 	  removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
   12179 					       &insn_len, contents, isymbuf,
   12180 					       symtab_hdr);
   12181 	  break;
   12182 	case R_NDS32_LONGJUMP5:
   12183 	  removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
   12184 					       &insn_len, &seq_len, contents,
   12185 					       isymbuf, symtab_hdr);
   12186 	  break;
   12187 	case R_NDS32_LONGJUMP6:
   12188 	  removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
   12189 					       &insn_len, &seq_len, contents,
   12190 					       isymbuf, symtab_hdr);
   12191 	  break;
   12192 	case R_NDS32_LONGJUMP7:
   12193 	  removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
   12194 					       &insn_len, &seq_len, contents,
   12195 					       isymbuf, symtab_hdr);
   12196 	  break;
   12197 	case R_NDS32_LOADSTORE:
   12198 	  removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
   12199 					       internal_relocs, &insn_len,
   12200 					       contents, isymbuf, symtab_hdr,
   12201 					       load_store_relax);
   12202 	  break;
   12203 	case R_NDS32_LO12S0_RELA:
   12204 	case R_NDS32_LO12S1_RELA:
   12205 	case R_NDS32_LO12S2_DP_RELA:
   12206 	case R_NDS32_LO12S2_SP_RELA:
   12207 	case R_NDS32_LO12S2_RELA:
   12208 	  /* Relax for low part.  */
   12209 	  nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
   12210 				contents, isymbuf, symtab_hdr);
   12211 
   12212 	  /* It is impossible to delete blank, so just continue.  */
   12213 	  continue;
   12214 	case R_NDS32_GOT_LO12:
   12215 	case R_NDS32_GOTOFF_LO12:
   12216 	case R_NDS32_PLTREL_LO12:
   12217 	case R_NDS32_PLT_GOTREL_LO12:
   12218 	case R_NDS32_GOTPC_LO12:
   12219 	  /* Relax for PIC gp-relative low part.  */
   12220 	  nds32_elf_relax_piclo12 (link_info, abfd, sec, irel, contents,
   12221 				   isymbuf, symtab_hdr);
   12222 
   12223 	  /* It is impossible to delete blank, so just continue.  */
   12224 	  continue;
   12225 	case R_NDS32_TLS_LE_LO12:
   12226 	  /* Relax for LE TLS low part.  */
   12227 	  nds32_elf_relax_letlslo12 (link_info, abfd, irel, contents,
   12228 				     isymbuf, symtab_hdr);
   12229 
   12230 	  /* It is impossible to delete blank, so just continue.  */
   12231 	  continue;
   12232 	case R_NDS32_TLS_LE_ADD:
   12233 	  nds32_elf_relax_letlsadd (link_info, abfd, sec, irel, internal_relocs,
   12234 				    contents, isymbuf, symtab_hdr, again);
   12235 	  /* It is impossible to delete blank, so just continue.  */
   12236 	  continue;
   12237 	case R_NDS32_TLS_LE_LS:
   12238 	  nds32_elf_relax_letlsls (link_info, abfd, sec, irel, internal_relocs,
   12239 				   contents, isymbuf, symtab_hdr, again);
   12240 	  continue;
   12241 	case R_NDS32_PTR:
   12242 	  removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
   12243 					 &insn_len, &seq_len, contents);
   12244 	  break;
   12245 	case R_NDS32_PLT_GOT_SUFF:
   12246 	  nds32_elf_relax_pltgot_suff (link_info, abfd, sec, irel,
   12247 				       internal_relocs, contents,
   12248 				       isymbuf, symtab_hdr, again);
   12249 	  /* It is impossible to delete blank, so just continue.  */
   12250 	  continue;
   12251 	case R_NDS32_GOT_SUFF:
   12252 	  nds32_elf_relax_got_suff (link_info, abfd, sec, irel,
   12253 				    internal_relocs, contents,
   12254 				    symtab_hdr, again);
   12255 	  /* It is impossible to delete blank, so just continue.  */
   12256 	  continue;
   12257 	case R_NDS32_GOTOFF_SUFF:
   12258 	  nds32_elf_relax_gotoff_suff (link_info, abfd, sec, irel,
   12259 				       internal_relocs, contents,
   12260 				       isymbuf, symtab_hdr, again);
   12261 	  /* It is impossible to delete blank, so just continue.  */
   12262 	  continue;
   12263 	default:
   12264 	  continue;
   12265 
   12266 	}
   12267       if (removed && seq_len - insn_len > 0)
   12268 	{
   12269 	  if (!insert_nds32_elf_blank
   12270 	      (&relax_blank_list, irel->r_offset + insn_len,
   12271 	       seq_len - insn_len))
   12272 	    goto error_return;
   12273 	  *again = TRUE;
   12274 	}
   12275     }
   12276 
   12277   calc_nds32_blank_total (relax_blank_list);
   12278 
   12279   if (table->relax_fp_as_gp)
   12280     {
   12281       if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
   12282 				 irelend, isymbuf))
   12283 	goto error_return;
   12284 
   12285       if (*again == FALSE)
   12286 	{
   12287 	  if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
   12288 					       irelend))
   12289 	    goto error_return;
   12290 	}
   12291     }
   12292 
   12293   nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
   12294 
   12295   if (*again == FALSE)
   12296     {
   12297       if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
   12298 				     &relax_blank_list, optimize, opt_size))
   12299 	goto error_return;
   12300     }
   12301 
   12302   /* It doesn't matter optimize_for_space_no_align anymore.
   12303        If object file is assembled with flag '-Os',
   12304        the we don't adjust jump-destination on 4-byte boundary.  */
   12305 
   12306   if (relax_blank_list)
   12307     {
   12308       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
   12309       relax_blank_list = NULL;
   12310     }
   12311 
   12312   if (*again == FALSE)
   12313     {
   12314       /* Closing the section, so we don't relax it anymore.  */
   12315       bfd_vma sec_size_align;
   12316       Elf_Internal_Rela *tmp_rel;
   12317 
   12318       /* Pad to alignment boundary.  Only handle current section alignment.  */
   12319       sec_size_align = (sec->size + (~((-1) << sec->alignment_power)))
   12320 		       & ((-1) << sec->alignment_power);
   12321       if ((sec_size_align - sec->size) & 0x2)
   12322 	{
   12323 	  insn16 = NDS32_NOP16;
   12324 	  bfd_putb16 (insn16, contents + sec->size);
   12325 	  sec->size += 2;
   12326 	}
   12327 
   12328       while (sec_size_align != sec->size)
   12329 	{
   12330 	  insn = NDS32_NOP32;
   12331 	  bfd_putb32 (insn, contents + sec->size);
   12332 	  sec->size += 4;
   12333 	}
   12334 
   12335       tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
   12336 					irelend, R_NDS32_RELAX_ENTRY);
   12337       if (tmp_rel != irelend)
   12338 	tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
   12339 
   12340       clean_nds32_elf_blank ();
   12341     }
   12342 
   12343 finish:
   12344   if (internal_relocs != NULL
   12345       && elf_section_data (sec)->relocs != internal_relocs)
   12346     free (internal_relocs);
   12347 
   12348   if (contents != NULL
   12349       && elf_section_data (sec)->this_hdr.contents != contents)
   12350     free (contents);
   12351 
   12352   if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
   12353     free (isymbuf);
   12354 
   12355   return result;
   12356 
   12357 error_return:
   12358   result = FALSE;
   12359   goto finish;
   12360 }
   12361 
   12362 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
   12363 {
   12364   {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
   12365   {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
   12366   {NULL, 0, 0, 0, 0}
   12367 };
   12368 
   12369 static bfd_boolean
   12370 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
   12371 			    struct bfd_link_info *info,
   12372 			    void *finfo ATTRIBUTE_UNUSED,
   12373 			    bfd_boolean (*func) (void *, const char *,
   12374 						 Elf_Internal_Sym *,
   12375 						 asection *,
   12376 						 struct elf_link_hash_entry *)
   12377 			    ATTRIBUTE_UNUSED)
   12378 {
   12379   FILE *sym_ld_script = NULL;
   12380   struct elf_nds32_link_hash_table *table;
   12381 
   12382   table = nds32_elf_hash_table (info);
   12383   sym_ld_script = table->sym_ld_script;
   12384 
   12385   if (check_start_export_sym)
   12386     fprintf (sym_ld_script, "}\n");
   12387 
   12388   return TRUE;
   12389 }
   12390 
   12391 static enum elf_reloc_type_class
   12392 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
   12393 			    const asection *rel_sec ATTRIBUTE_UNUSED,
   12394 			    const Elf_Internal_Rela *rela)
   12395 {
   12396   switch ((int) ELF32_R_TYPE (rela->r_info))
   12397     {
   12398     case R_NDS32_RELATIVE:
   12399       return reloc_class_relative;
   12400     case R_NDS32_JMP_SLOT:
   12401       return reloc_class_plt;
   12402     case R_NDS32_COPY:
   12403       return reloc_class_copy;
   12404     default:
   12405       return reloc_class_normal;
   12406     }
   12407 }
   12408 
   12409 /* Put target dependent option into info hash table.  */
   12410 void
   12411 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
   12412 				   int relax_fp_as_gp,
   12413 				   int eliminate_gc_relocs,
   12414 				   FILE * sym_ld_script, int load_store_relax,
   12415 				   int target_optimize, int relax_status,
   12416 				   int relax_round, FILE * ex9_export_file,
   12417 				   FILE * ex9_import_file,
   12418 				   int update_ex9_table, int ex9_limit,
   12419 				   bfd_boolean ex9_loop_aware,
   12420 				   bfd_boolean ifc_loop_aware)
   12421 {
   12422   struct elf_nds32_link_hash_table *table;
   12423 
   12424   table = nds32_elf_hash_table (link_info);
   12425   if (table == NULL)
   12426     return;
   12427 
   12428   table->relax_fp_as_gp = relax_fp_as_gp;
   12429   table->eliminate_gc_relocs = eliminate_gc_relocs;
   12430   table->sym_ld_script = sym_ld_script;
   12431   table ->load_store_relax = load_store_relax;
   12432   table->target_optimize = target_optimize;
   12433   table->relax_status = relax_status;
   12434   table->relax_round = relax_round;
   12435   table->ex9_export_file = ex9_export_file;
   12436   table->ex9_import_file = ex9_import_file;
   12437   table->update_ex9_table = update_ex9_table;
   12438   table->ex9_limit = ex9_limit;
   12439   table->ex9_loop_aware = ex9_loop_aware;
   12440   table->ifc_loop_aware = ifc_loop_aware;
   12441 }
   12442 
   12443 /* These functions and data-structures are used for fp-as-gp
   12445    optimization.  */
   12446 
   12447 #define FAG_THRESHOLD	3	/* At least 3 gp-access.  */
   12448 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
   12449    the read-only section and read-write section.  */
   12450 #define FAG_WINDOW	(508 - 32)
   12451 
   12452 /* An nds32_fag represent a gp-relative access.
   12453    We find best fp-base by using a sliding window
   12454    to find a base address which can cover most gp-access.  */
   12455 struct nds32_fag
   12456 {
   12457   struct nds32_fag *next;	/* NULL-teminated linked list.  */
   12458   bfd_vma addr;			/* The address of this fag.  */
   12459   Elf_Internal_Rela **relas;	/* The relocations associated with this fag.
   12460 				   It is used for applying FP7U2_FLAG.  */
   12461   int count;			/* How many times this address is referred.
   12462 				   There should be exactly `count' relocations
   12463 				   in relas.  */
   12464   int relas_capcity;		/* The buffer size of relas.
   12465 				   We use an array instead of linked-list,
   12466 				   and realloc is used to adjust buffer size.  */
   12467 };
   12468 
   12469 static void
   12470 nds32_fag_init (struct nds32_fag *head)
   12471 {
   12472   memset (head, 0, sizeof (struct nds32_fag));
   12473 }
   12474 
   12475 static void
   12476 nds32_fag_verify (struct nds32_fag *head)
   12477 {
   12478   struct nds32_fag *iter;
   12479   struct nds32_fag *prev;
   12480 
   12481   prev = NULL;
   12482   iter = head->next;
   12483   while (iter)
   12484     {
   12485       if (prev && prev->addr >= iter->addr)
   12486 	puts ("Bug in fp-as-gp insertion.");
   12487       prev = iter;
   12488       iter = iter->next;
   12489     }
   12490 }
   12491 
   12492 /* Insert a fag in ascending order.
   12493    If a fag of the same address already exists,
   12494    they are chained by relas array.  */
   12495 
   12496 static void
   12497 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
   12498 		  Elf_Internal_Rela * rel)
   12499 {
   12500   struct nds32_fag *iter;
   12501   struct nds32_fag *new_fag;
   12502   const int INIT_RELAS_CAP = 4;
   12503 
   12504   for (iter = head;
   12505        iter->next && iter->next->addr <= addr;
   12506        iter = iter->next)
   12507     /* Find somewhere to insert.  */ ;
   12508 
   12509   /* `iter' will be equal to `head' if the list is empty.  */
   12510   if (iter != head && iter->addr == addr)
   12511     {
   12512       /* The address exists in the list.
   12513 	 Insert `rel' into relocation list, relas.  */
   12514 
   12515       /* Check whether relas is big enough.  */
   12516       if (iter->count >= iter->relas_capcity)
   12517 	{
   12518 	  iter->relas_capcity *= 2;
   12519 	  iter->relas = bfd_realloc
   12520 	    (iter->relas, iter->relas_capcity * sizeof (void *));
   12521 	}
   12522       iter->relas[iter->count++] = rel;
   12523       return;
   12524     }
   12525 
   12526   /* This is a new address.  Create a fag node for it.  */
   12527   new_fag = bfd_malloc (sizeof (struct nds32_fag));
   12528   memset (new_fag, 0, sizeof (*new_fag));
   12529   new_fag->addr = addr;
   12530   new_fag->count = 1;
   12531   new_fag->next = iter->next;
   12532   new_fag->relas_capcity = INIT_RELAS_CAP;
   12533   new_fag->relas = (Elf_Internal_Rela **)
   12534     bfd_malloc (new_fag->relas_capcity * sizeof (void *));
   12535   new_fag->relas[0] = rel;
   12536   iter->next = new_fag;
   12537 
   12538   nds32_fag_verify (head);
   12539 }
   12540 
   12541 static void
   12542 nds32_fag_free_list (struct nds32_fag *head)
   12543 {
   12544   struct nds32_fag *iter;
   12545 
   12546   iter = head->next;
   12547   while (iter)
   12548     {
   12549       struct nds32_fag *tmp = iter;
   12550       iter = iter->next;
   12551       free (tmp->relas);
   12552       tmp->relas = NULL;
   12553       free (tmp);
   12554     }
   12555 }
   12556 
   12557 /* Find the best fp-base address.
   12558    The relocation associated with that address is returned,
   12559    so we can track the symbol instead of a fixed address.
   12560 
   12561    When relaxation, the address of an datum may change,
   12562    because a text section is shrinked, so the data section
   12563    moves forward.  If the aligments of text and data section
   12564    are different, their distance may change too.
   12565    Therefore, tracking a fixed address is not appriate.  */
   12566 
   12567 static int
   12568 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
   12569 {
   12570   struct nds32_fag *base;	/* First fag in the window.  */
   12571   struct nds32_fag *last;	/* First fag outside the window.  */
   12572   int accu = 0;			/* Usage accumulation.  */
   12573   struct nds32_fag *best;	/* Best fag.  */
   12574   int baccu = 0;		/* Best accumulation.  */
   12575 
   12576   /* Use first fag for initial, and find the last fag in the window.
   12577 
   12578      In each iteration, we could simply subtract previous fag
   12579      and accumulate following fags which are inside the window,
   12580      untill we each the end.  */
   12581 
   12582   if (head->next == NULL)
   12583     {
   12584       *bestpp = NULL;
   12585       return 0;
   12586     }
   12587 
   12588   /* Initialize base.  */
   12589   base = head->next;
   12590   best = base;
   12591   for (last = base;
   12592        last && last->addr < base->addr + FAG_WINDOW;
   12593        last = last->next)
   12594     accu += last->count;
   12595 
   12596   baccu = accu;
   12597 
   12598   /* Record the best base in each iteration.  */
   12599   while (base->next)
   12600     {
   12601       accu -= base->count;
   12602       base = base->next;
   12603       /* Account fags in window.  */
   12604       for (/* Nothing.  */;
   12605 	   last && last->addr < base->addr + FAG_WINDOW;
   12606 	   last = last->next)
   12607 	accu += last->count;
   12608 
   12609       /* A better fp-base?  */
   12610       if (accu > baccu)
   12611 	{
   12612 	  best = base;
   12613 	  baccu = accu;
   12614 	}
   12615     }
   12616 
   12617   if (bestpp)
   12618     *bestpp = best;
   12619   return baccu;
   12620 }
   12621 
   12622 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
   12623    so we can convert it fo fp-relative access later.
   12624    `best_fag' is the best fp-base.  Only those inside the window
   12625    of best_fag is applied the flag.  */
   12626 
   12627 static bfd_boolean
   12628 nds32_fag_mark_relax (struct bfd_link_info *link_info,
   12629 		      bfd *abfd, struct nds32_fag *best_fag,
   12630 		      Elf_Internal_Rela *internal_relocs,
   12631 		      Elf_Internal_Rela *irelend)
   12632 {
   12633   struct nds32_fag *ifag;
   12634   bfd_vma best_fpbase, gp;
   12635   bfd *output_bfd;
   12636 
   12637   output_bfd = abfd->sections->output_section->owner;
   12638   nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
   12639   best_fpbase = best_fag->addr;
   12640 
   12641   if (best_fpbase > gp + sdata_range[1][1]
   12642       || best_fpbase < gp - sdata_range[1][0])
   12643     return FALSE;
   12644 
   12645   /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
   12646      so we know they can be converted to lwi37.fp.   */
   12647   for (ifag = best_fag;
   12648        ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
   12649     {
   12650       int i;
   12651 
   12652       for (i = 0; i < ifag->count; i++)
   12653 	{
   12654 	  Elf_Internal_Rela *insn16_rel;
   12655 	  Elf_Internal_Rela *fag_rel;
   12656 
   12657 	  fag_rel = ifag->relas[i];
   12658 
   12659 	  /* Only if this is within the WINDOWS, FP7U2_FLAG
   12660 	     is applied.  */
   12661 
   12662 	  insn16_rel = find_relocs_at_address
   12663 	    (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
   12664 
   12665 	  if (insn16_rel != irelend)
   12666 	    insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
   12667 	}
   12668     }
   12669   return TRUE;
   12670 }
   12671 
   12672 /* Reset INSN16 to clean fp as gp.  */
   12673 
   12674 static void
   12675 nds32_fag_unmark_relax (struct nds32_fag *fag,
   12676 			Elf_Internal_Rela *internal_relocs,
   12677 			Elf_Internal_Rela *irelend)
   12678 {
   12679   struct nds32_fag *ifag;
   12680   int i;
   12681   Elf_Internal_Rela *insn16_rel;
   12682   Elf_Internal_Rela *fag_rel;
   12683 
   12684   for (ifag = fag; ifag; ifag = ifag->next)
   12685     {
   12686       for (i = 0; i < ifag->count; i++)
   12687 	{
   12688 	  fag_rel = ifag->relas[i];
   12689 
   12690 	  /* Restore the INSN16 relocation.  */
   12691 	  insn16_rel = find_relocs_at_address
   12692 	    (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
   12693 
   12694 	  if (insn16_rel != irelend)
   12695 	    insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
   12696 	}
   12697     }
   12698 }
   12699 
   12700 /* This is the main function of fp-as-gp optimization.
   12701    It should be called by relax_section.  */
   12702 
   12703 static bfd_boolean
   12704 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
   12705 		      bfd *abfd, asection *sec,
   12706 		      Elf_Internal_Rela *internal_relocs,
   12707 		      Elf_Internal_Rela *irelend,
   12708 		      Elf_Internal_Sym *isymbuf)
   12709 {
   12710   Elf_Internal_Rela *begin_rel = NULL;
   12711   Elf_Internal_Rela *irel;
   12712   struct nds32_fag fag_head;
   12713   Elf_Internal_Shdr *symtab_hdr;
   12714   bfd_byte *contents;
   12715   bfd_boolean ifc_inside = FALSE;
   12716 
   12717   /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
   12718 
   12719   /* Per-function fp-base selection.
   12720      1. Create a list for all the gp-relative access.
   12721      2. Base on those gp-relative address,
   12722 	find a fp-base which can cover most access.
   12723      3. Use the fp-base for fp-as-gp relaxation.
   12724 
   12725      NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
   12726      we should
   12727      1. delete the `la $fp, _FP_BASE_' instruction and
   12728      2. not convert lwi.gp to lwi37.fp.
   12729 
   12730      To delete the _FP_BASE_ instruction, we simply apply
   12731      R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
   12732 
   12733      To suppress the conversion, we simply NOT to apply
   12734      R_NDS32_INSN16_FP7U2_FLAG flag.  */
   12735 
   12736   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12737 
   12738   if (!nds32_get_section_contents (abfd, sec, &contents)
   12739       || !nds32_get_local_syms (abfd, sec, &isymbuf))
   12740     return FALSE;
   12741 
   12742   /* Check whether it is worth for fp-as-gp optimization,
   12743      i.e., at least 3 gp-load.
   12744 
   12745      Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
   12746      apply this optimization.  */
   12747 
   12748   for (irel = internal_relocs; irel < irelend; irel++)
   12749     {
   12750       /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
   12751 	 One we enter the begin of the region, we track all the LW/ST
   12752 	 instructions, so when we leave the region, we try to find
   12753 	 the best fp-base address for those LW/ST instructions.  */
   12754 
   12755       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
   12756 	  && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
   12757 	{
   12758 	  /* Begin of the region.  */
   12759 	  if (begin_rel)
   12760 	    (*_bfd_error_handler) (_("%B: Nested OMIT_FP in %A."), abfd, sec);
   12761 
   12762 	  begin_rel = irel;
   12763 	  nds32_fag_init (&fag_head);
   12764 	  ifc_inside = FALSE;
   12765 	}
   12766       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
   12767 	       && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
   12768 	{
   12769 	  int accu;
   12770 	  struct nds32_fag *best_fag, *tmp_fag;
   12771 	  int dist;
   12772 
   12773 	  /* End of the region.
   12774 	     Check whether it is worth to do fp-as-gp.  */
   12775 
   12776 	  if (begin_rel == NULL)
   12777 	    {
   12778 	      (*_bfd_error_handler) (_("%B: Unmatched OMIT_FP in %A."), abfd, sec);
   12779 	      continue;
   12780 	    }
   12781 
   12782 	  accu = nds32_fag_find_base (&fag_head, &best_fag);
   12783 
   12784 	  /* Clean FP7U2_FLAG because they may set ever.  */
   12785 	  tmp_fag = fag_head.next;
   12786 	  nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
   12787 
   12788 	  /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
   12789 	  if (accu < FAG_THRESHOLD
   12790 	      || !nds32_fag_mark_relax (link_info, abfd, best_fag,
   12791 					internal_relocs, irelend))
   12792 	    {
   12793 	      /* Not worth to do fp-as-gp.  */
   12794 	      begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
   12795 	      begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
   12796 	      irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
   12797 	      irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
   12798 	      nds32_fag_free_list (&fag_head);
   12799 	      begin_rel = NULL;
   12800 	      continue;
   12801 	    }
   12802 
   12803 	  /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
   12804 	     so we use it to record the distance to the reloction of best
   12805 	     fp-base.  */
   12806 	  dist = best_fag->relas[0] - begin_rel;
   12807 	  BFD_ASSERT (dist > 0 && dist < 0xffffff);
   12808 	  /* Use high 16 bits of addend to record the _FP_BASE_ matched
   12809 	     relocation.  And get the base value when relocating.  */
   12810 	  begin_rel->r_addend &= (0x1 << 16) - 1;
   12811 	  begin_rel->r_addend |= dist << 16;
   12812 
   12813 	  nds32_fag_free_list (&fag_head);
   12814 	  begin_rel = NULL;
   12815 	}
   12816 
   12817       if (begin_rel == NULL || ifc_inside)
   12818 	/* Skip if we are not in the region of fp-as-gp.  */
   12819 	continue;
   12820 
   12821       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
   12822 	  || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
   12823 	{
   12824 	  bfd_vma addr;
   12825 	  uint32_t insn;
   12826 
   12827 	  /* A gp-relative access is found.  Insert it to the fag-list.  */
   12828 
   12829 	  /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
   12830 	  insn = bfd_getb32 (contents + irel->r_offset);
   12831 	  if (!N32_IS_RT3 (insn))
   12832 	    continue;
   12833 
   12834 	  addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
   12835 	  nds32_fag_insert (&fag_head, addr, irel);
   12836 	}
   12837       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
   12838 	{
   12839 	  begin_rel = NULL;
   12840 	}
   12841       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
   12842 	       || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
   12843 	{
   12844 	  /* Suppress fp as gp when encounter ifc.  */
   12845 	  ifc_inside = TRUE;
   12846 	}
   12847     }
   12848 
   12849   return TRUE;
   12850 }
   12851 
   12852 /* Remove unused `la $fp, _FD_BASE_' instruction.  */
   12853 
   12854 static bfd_boolean
   12855 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
   12856 				Elf_Internal_Rela *internal_relocs,
   12857 				Elf_Internal_Rela *irelend)
   12858 {
   12859   Elf_Internal_Rela *irel;
   12860   Elf_Internal_Shdr *symtab_hdr;
   12861   bfd_byte *contents = NULL;
   12862   nds32_elf_blank_t *relax_blank_list = NULL;
   12863   bfd_boolean result = TRUE;
   12864   bfd_boolean unused_region = FALSE;
   12865 
   12866   /*
   12867      NOTE: Disable fp-as-gp if we encounter ifcall relocations.
   12868      * R_NDS32_17IFC_PCREL_RELA
   12869      * R_NDS32_10IFCU_PCREL_RELA
   12870 
   12871      CASE??????????????
   12872   */
   12873 
   12874   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   12875   nds32_get_section_contents (abfd, sec, &contents);
   12876 
   12877   for (irel = internal_relocs; irel < irelend; irel++)
   12878     {
   12879       /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
   12880 	 we marked to in previous pass.
   12881 	 DO NOT scan relocations again, since we've alreadly decided it
   12882 	 and set the flag.  */
   12883       const char *syname;
   12884       int syndx;
   12885       uint32_t insn;
   12886 
   12887       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
   12888 	  && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
   12889 	unused_region = TRUE;
   12890       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
   12891 	       && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
   12892 	unused_region = FALSE;
   12893 
   12894       /* We're not in the region.  */
   12895       if (!unused_region)
   12896 	continue;
   12897 
   12898       /* _FP_BASE_ must be a GLOBAL symbol.  */
   12899       syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
   12900       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
   12901 	continue;
   12902 
   12903       /* The symbol name must be _FP_BASE_.  */
   12904       syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
   12905       if (strcmp (syname, FP_BASE_NAME) != 0)
   12906 	continue;
   12907 
   12908       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
   12909 	{
   12910 	  /* addi.gp  $fp, -256  */
   12911 	  insn = bfd_getb32 (contents + irel->r_offset);
   12912 	  if (insn != INSN_ADDIGP_TO_FP)
   12913 	    continue;
   12914 	}
   12915       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
   12916 	{
   12917 	  /* addi  $fp, $gp, -256  */
   12918 	  insn = bfd_getb32 (contents + irel->r_offset);
   12919 	  if (insn != INSN_ADDI_GP_TO_FP)
   12920 	    continue;
   12921 	}
   12922       else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
   12923 	{
   12924 	  /* movi  $fp, FP_BASE  */
   12925 	  insn = bfd_getb32 (contents + irel->r_offset);
   12926 	  if (insn != INSN_MOVI_TO_FP)
   12927 	    continue;
   12928 	}
   12929       else
   12930 	continue;
   12931 
   12932       /* We got here because a FP_BASE instruction is found.  */
   12933       if (!insert_nds32_elf_blank_recalc_total
   12934 	  (&relax_blank_list, irel->r_offset, 4))
   12935 	goto error_return;
   12936     }
   12937 
   12938 finish:
   12939   if (relax_blank_list)
   12940     {
   12941       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
   12942       relax_blank_list = NULL;
   12943     }
   12944   return result;
   12945 
   12946 error_return:
   12947   result = FALSE;
   12948   goto finish;
   12949 }
   12950 
   12951 /* This is a version of bfd_generic_get_relocated_section_contents.
   12952    We need this variety because relaxation will modify the dwarf
   12953    infomation.  When there is undefined symbol reference error mesage,
   12954    linker need to dump line number where the symbol be used.  However
   12955    the address is be relaxed, it can not get the original dwarf contents.
   12956    The variety only modify function call for reading in the section.  */
   12957 
   12958 static bfd_byte *
   12959 nds32_elf_get_relocated_section_contents (bfd *abfd,
   12960 					  struct bfd_link_info *link_info,
   12961 					  struct bfd_link_order *link_order,
   12962 					  bfd_byte *data,
   12963 					  bfd_boolean relocatable,
   12964 					  asymbol **symbols)
   12965 {
   12966   bfd *input_bfd = link_order->u.indirect.section->owner;
   12967   asection *input_section = link_order->u.indirect.section;
   12968   long reloc_size;
   12969   arelent **reloc_vector;
   12970   long reloc_count;
   12971 
   12972   reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
   12973   if (reloc_size < 0)
   12974     return NULL;
   12975 
   12976   /* Read in the section.  */
   12977   if (!nds32_get_section_contents (input_bfd, input_section, &data))
   12978     return NULL;
   12979 
   12980   if (reloc_size == 0)
   12981     return data;
   12982 
   12983   reloc_vector = (arelent **) bfd_malloc (reloc_size);
   12984   if (reloc_vector == NULL)
   12985     return NULL;
   12986 
   12987   reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
   12988 					reloc_vector, symbols);
   12989   if (reloc_count < 0)
   12990     goto error_return;
   12991 
   12992   if (reloc_count > 0)
   12993     {
   12994       arelent **parent;
   12995       for (parent = reloc_vector; *parent != NULL; parent++)
   12996 	{
   12997 	  char *error_message = NULL;
   12998 	  asymbol *symbol;
   12999 	  bfd_reloc_status_type r;
   13000 
   13001 	  symbol = *(*parent)->sym_ptr_ptr;
   13002 	  if (symbol->section && discarded_section (symbol->section))
   13003 	    {
   13004 	      bfd_byte *p;
   13005 	      static reloc_howto_type none_howto
   13006 		= HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
   13007 			 "unused", FALSE, 0, 0, FALSE);
   13008 
   13009 	      p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
   13010 	      _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
   13011 				   p);
   13012 	      (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   13013 	      (*parent)->addend = 0;
   13014 	      (*parent)->howto = &none_howto;
   13015 	      r = bfd_reloc_ok;
   13016 	    }
   13017 	  else
   13018 	    r = bfd_perform_relocation (input_bfd, *parent, data,
   13019 					input_section,
   13020 					relocatable ? abfd : NULL,
   13021 					&error_message);
   13022 
   13023 	  if (relocatable)
   13024 	    {
   13025 	      asection *os = input_section->output_section;
   13026 
   13027 	      /* A partial link, so keep the relocs.  */
   13028 	      os->orelocation[os->reloc_count] = *parent;
   13029 	      os->reloc_count++;
   13030 	    }
   13031 
   13032 	  if (r != bfd_reloc_ok)
   13033 	    {
   13034 	      switch (r)
   13035 		{
   13036 		case bfd_reloc_undefined:
   13037 		  if (!((*link_info->callbacks->undefined_symbol)
   13038 			(link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   13039 			 input_bfd, input_section, (*parent)->address, TRUE)))
   13040 		    goto error_return;
   13041 		  break;
   13042 		case bfd_reloc_dangerous:
   13043 		  BFD_ASSERT (error_message != NULL);
   13044 		  if (!((*link_info->callbacks->reloc_dangerous)
   13045 			(link_info, error_message, input_bfd, input_section,
   13046 			 (*parent)->address)))
   13047 		    goto error_return;
   13048 		  break;
   13049 		case bfd_reloc_overflow:
   13050 		  if (!((*link_info->callbacks->reloc_overflow)
   13051 			(link_info, NULL,
   13052 			 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
   13053 			 (*parent)->howto->name, (*parent)->addend,
   13054 			 input_bfd, input_section, (*parent)->address)))
   13055 		    goto error_return;
   13056 		  break;
   13057 		case bfd_reloc_outofrange:
   13058 		  /* PR ld/13730:
   13059 		     This error can result when processing some partially
   13060 		     complete binaries.  Do not abort, but issue an error
   13061 		     message instead.  */
   13062 		  link_info->callbacks->einfo
   13063 		    (_("%X%P: %B(%A): relocation \"%R\" goes out of range\n"),
   13064 		     abfd, input_section, * parent);
   13065 		  goto error_return;
   13066 
   13067 		default:
   13068 		  abort ();
   13069 		  break;
   13070 		}
   13071 	    }
   13072 	}
   13073     }
   13074 
   13075   free (reloc_vector);
   13076   return data;
   13077 
   13078 error_return:
   13079   free (reloc_vector);
   13080   return NULL;
   13081 }
   13082 
   13083 /* Link-time IFC relaxation.
   13085    In this optimization, we chains jump instructions
   13086    of the same destination with ifcall.  */
   13087 
   13088 
   13089 /* List to save jal and j relocation.  */
   13090 struct elf_nds32_ifc_symbol_entry
   13091 {
   13092   asection *sec;
   13093   struct elf_link_hash_entry *h;
   13094   struct elf_nds32_ifc_irel_list *irel_head;
   13095   unsigned long insn;
   13096   int times;
   13097   int enable;		/* Apply ifc.  */
   13098   int ex9_enable;	/* Apply ifc after ex9.  */
   13099   struct elf_nds32_ifc_symbol_entry *next;
   13100 };
   13101 
   13102 struct elf_nds32_ifc_irel_list
   13103 {
   13104   Elf_Internal_Rela *irel;
   13105   asection *sec;
   13106   bfd_vma addr;
   13107   /* If this is set, then it is the last instruction for
   13108      ifc-chain, so it must be keep for the actual branching.  */
   13109   int keep;
   13110   struct elf_nds32_ifc_irel_list *next;
   13111 };
   13112 
   13113 static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;
   13114 
   13115 /* Insert symbol of jal and j for ifc.  */
   13116 
   13117 static void
   13118 nds32_elf_ifc_insert_symbol (asection *sec,
   13119 			     struct elf_link_hash_entry *h,
   13120 			     Elf_Internal_Rela *irel,
   13121 			     unsigned long insn)
   13122 {
   13123   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
   13124 
   13125   /* Check there is target of existing entry the same as the new one.  */
   13126   while (ptr != NULL)
   13127     {
   13128       if (((h == NULL && ptr->sec == sec
   13129 	    && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
   13130 	    && ptr->irel_head->irel->r_addend == irel->r_addend)
   13131 	   || h != NULL)
   13132 	  && ptr->h == h
   13133 	  && ptr->insn == insn)
   13134 	{
   13135 	  /* The same target exist, so insert into list.  */
   13136 	  struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;
   13137 
   13138 	  while (irel_list->next != NULL)
   13139 	    irel_list = irel_list->next;
   13140 	  irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
   13141 	  irel_list = irel_list->next;
   13142 	  irel_list->irel = irel;
   13143 	  irel_list->keep = 1;
   13144 
   13145 	  if (h == NULL)
   13146 	    irel_list->sec = NULL;
   13147 	  else
   13148 	    irel_list->sec = sec;
   13149 	  irel_list->next = NULL;
   13150 	  return;
   13151 	}
   13152       if (ptr->next == NULL)
   13153 	break;
   13154       ptr = ptr->next;
   13155     }
   13156 
   13157   /* There is no same target entry, so build a new one.  */
   13158   if (ifc_symbol_head == NULL)
   13159     {
   13160       ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
   13161       ptr = ifc_symbol_head;
   13162     }
   13163   else
   13164     {
   13165       ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
   13166       ptr = ptr->next;
   13167     }
   13168 
   13169   ptr->h = h;
   13170   ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
   13171   ptr->irel_head->irel = irel;
   13172   ptr->insn = insn;
   13173   ptr->irel_head->keep = 1;
   13174 
   13175   if (h == NULL)
   13176     {
   13177       /* Local symbols.  */
   13178       ptr->sec = sec;
   13179       ptr->irel_head->sec = NULL;
   13180     }
   13181   else
   13182     {
   13183       /* Global symbol.  */
   13184       ptr->sec = NULL;
   13185       ptr->irel_head->sec = sec;
   13186     }
   13187 
   13188   ptr->irel_head->next = NULL;
   13189   ptr->times = 0;
   13190   ptr->enable = 0;
   13191   ptr->ex9_enable = 0;
   13192   ptr->next = NULL;
   13193 }
   13194 
   13195 /* Gather all jal and j instructions.  */
   13196 
   13197 static bfd_boolean
   13198 nds32_elf_ifc_calc (struct bfd_link_info *info,
   13199 		    bfd *abfd, asection *sec)
   13200 {
   13201   Elf_Internal_Rela *internal_relocs;
   13202   Elf_Internal_Rela *irelend;
   13203   Elf_Internal_Rela *irel;
   13204   Elf_Internal_Shdr *symtab_hdr;
   13205   bfd_byte *contents = NULL;
   13206   uint32_t insn, insn_with_reg;
   13207   unsigned long r_symndx;
   13208   struct elf_link_hash_entry *h;
   13209   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
   13210   struct elf_nds32_link_hash_table *table;
   13211   bfd_boolean ifc_loop_aware;
   13212 
   13213   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   13214 					       TRUE /* keep_memory */);
   13215   irelend = internal_relocs + sec->reloc_count;
   13216   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   13217 
   13218   /* Check if the object enable ifc.  */
   13219   irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
   13220 				 R_NDS32_RELAX_ENTRY);
   13221 
   13222   if (irel == NULL
   13223       || irel >= irelend
   13224       || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
   13225       || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
   13226 	  && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
   13227     return TRUE;
   13228 
   13229   if (!nds32_get_section_contents (abfd, sec, &contents))
   13230     return FALSE;
   13231 
   13232   table = nds32_elf_hash_table (info);
   13233   ifc_loop_aware = table->ifc_loop_aware;
   13234   while (irel != NULL && irel < irelend)
   13235     {
   13236       /* Traverse all relocation and gather all of them to build the list.  */
   13237 
   13238       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
   13239 	{
   13240 	  if (ifc_loop_aware == 1
   13241 	      && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
   13242 	    {
   13243 	      /* Check the region if loop or not.  If it is true and
   13244 		 ifc-loop-aware is true, ignore the region till region end.  */
   13245 	      while (irel != NULL
   13246 		     && irel < irelend
   13247 		     && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
   13248 			 || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
   13249 		irel++;
   13250 	    }
   13251 	}
   13252 
   13253       if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
   13254 	{
   13255 	  insn = bfd_getb32 (contents + irel->r_offset);
   13256 	  nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
   13257 	  r_symndx = ELF32_R_SYM (irel->r_info);
   13258 	  if (r_symndx < symtab_hdr->sh_info)
   13259 	    {
   13260 	      /* Local symbol.  */
   13261 	      nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
   13262 	    }
   13263 	  else
   13264 	    {
   13265 	      /* External symbol.  */
   13266 	      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   13267 	      nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
   13268 	    }
   13269 	}
   13270       irel++;
   13271     }
   13272   return TRUE;
   13273 }
   13274 
   13275 /* Determine whether j and jal should be substituted.  */
   13276 
   13277 static void
   13278 nds32_elf_ifc_filter (struct bfd_link_info *info)
   13279 {
   13280   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
   13281   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
   13282   struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
   13283   struct elf_nds32_link_hash_table *table;
   13284   int target_optimize;
   13285   bfd_vma address;
   13286 
   13287   table = nds32_elf_hash_table (info);
   13288   target_optimize = table->target_optimize;
   13289   while (ptr)
   13290     {
   13291       irel_ptr = ptr->irel_head;
   13292       if (ptr->h == NULL)
   13293 	{
   13294 	  /* Local symbol.  */
   13295 	  irel_keeper = irel_ptr;
   13296 	  while (irel_ptr && irel_ptr->next)
   13297 	    {
   13298 	      /* Check there is jump target can be used.  */
   13299 	      if ((irel_ptr->next->irel->r_offset
   13300 		   - irel_keeper->irel->r_offset) > 1022)
   13301 		irel_keeper = irel_ptr->next;
   13302 	      else
   13303 		{
   13304 		  ptr->enable = 1;
   13305 		  irel_ptr->keep = 0;
   13306 		}
   13307 	      irel_ptr = irel_ptr->next;
   13308 	    }
   13309 	}
   13310       else
   13311 	{
   13312 	  /* Global symbol.  */
   13313 	  /* We have to get the absolute address and decide
   13314 	     whether to keep it or not.  */
   13315 	  while (irel_ptr)
   13316 	    {
   13317 	      address = (irel_ptr->irel->r_offset
   13318 			 + irel_ptr->sec->output_section->vma
   13319 			 + irel_ptr->sec->output_offset);
   13320 	      irel_ptr->addr = address;
   13321 	      irel_ptr = irel_ptr->next;
   13322 	    }
   13323 
   13324 	  irel_ptr = ptr->irel_head;
   13325 	  while (irel_ptr)
   13326 	    {
   13327 	      /* Sort by address.  */
   13328 	      struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
   13329 	      struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
   13330 	      struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
   13331 	      struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;
   13332 
   13333 	      /* Get the smallest one.  */
   13334 	      while (irel_temp->next)
   13335 		{
   13336 		  if (irel_temp->next->addr < irel_dest->addr)
   13337 		    {
   13338 		      irel_dest_prev = irel_temp;
   13339 		      irel_dest = irel_temp->next;
   13340 		    }
   13341 		  irel_temp = irel_temp->next;
   13342 		}
   13343 
   13344 	      if (irel_dest != irel_ptr)
   13345 		{
   13346 		  if (irel_ptr_prev)
   13347 		    irel_ptr_prev->next = irel_dest;
   13348 		  if (irel_dest_prev)
   13349 		    irel_dest_prev->next = irel_ptr;
   13350 		  irel_temp = irel_ptr->next;
   13351 		  irel_ptr->next = irel_dest->next;
   13352 		  irel_dest->next = irel_temp;
   13353 		}
   13354 	      irel_ptr_prev = irel_ptr;
   13355 	      irel_ptr = irel_ptr->next;
   13356 	    }
   13357 
   13358 	  irel_ptr = ptr->irel_head;
   13359 	  irel_keeper = irel_ptr;
   13360 	  while (irel_ptr && irel_ptr->next)
   13361 	    {
   13362 	      if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
   13363 		irel_keeper = irel_ptr->next;
   13364 	      else
   13365 		{
   13366 		  ptr->enable = 1;
   13367 		  irel_ptr->keep = 0;
   13368 		}
   13369 	      irel_ptr = irel_ptr->next;
   13370 	    }
   13371 	}
   13372 
   13373 	/* Ex9 enable.  Reserve it for ex9.  */
   13374       if ((target_optimize & NDS32_RELAX_EX9_ON)
   13375 	  && ptr->irel_head != irel_keeper)
   13376 	ptr->enable = 0;
   13377       ptr = ptr->next;
   13378     }
   13379 }
   13380 
   13381 /* Determine whether j and jal should be substituted after ex9 done.  */
   13382 
   13383 static void
   13384 nds32_elf_ifc_filter_after_ex9 (void)
   13385 {
   13386   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
   13387   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
   13388 
   13389   while (ptr)
   13390     {
   13391       if (ptr->enable == 0)
   13392 	{
   13393 	  /* Check whether ifc is applied or not.  */
   13394 	  irel_ptr = ptr->irel_head;
   13395 	  ptr->ex9_enable = 1;
   13396 	  while (irel_ptr)
   13397 	    {
   13398 	      if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
   13399 		{
   13400 		  /* Ex9 already.  */
   13401 		  ptr->ex9_enable = 0;
   13402 		  break;
   13403 		}
   13404 	      irel_ptr = irel_ptr->next;
   13405 	    }
   13406 	}
   13407       ptr = ptr->next;
   13408     }
   13409 }
   13410 
   13411 /* Wrapper to do ifc relaxation.  */
   13412 
   13413 bfd_boolean
   13414 nds32_elf_ifc_finish (struct bfd_link_info *info)
   13415 {
   13416   int relax_status;
   13417   struct elf_nds32_link_hash_table *table;
   13418 
   13419   table = nds32_elf_hash_table (info);
   13420   relax_status = table->relax_status;
   13421 
   13422   if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
   13423     nds32_elf_ifc_filter (info);
   13424   else
   13425     nds32_elf_ifc_filter_after_ex9 ();
   13426 
   13427   if (!nds32_elf_ifc_replace (info))
   13428     return FALSE;
   13429 
   13430   if (table)
   13431     table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
   13432   return TRUE;
   13433 }
   13434 
   13435 /* Traverse the result of ifc filter and replace it with ifcall9.  */
   13436 
   13437 static bfd_boolean
   13438 nds32_elf_ifc_replace (struct bfd_link_info *info)
   13439 {
   13440   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
   13441   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
   13442   nds32_elf_blank_t *relax_blank_list = NULL;
   13443   bfd_byte *contents = NULL;
   13444   Elf_Internal_Rela *internal_relocs;
   13445   Elf_Internal_Rela *irel;
   13446   Elf_Internal_Rela *irelend;
   13447   unsigned short insn16 = INSN_IFCALL9;
   13448   struct elf_nds32_link_hash_table *table;
   13449   int relax_status;
   13450 
   13451   table = nds32_elf_hash_table (info);
   13452   relax_status = table->relax_status;
   13453 
   13454   while (ptr)
   13455     {
   13456       /* Traverse the ifc gather list, and replace the
   13457 	 filter entries by ifcall9.  */
   13458       if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
   13459 	  || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE)
   13460 	      && ptr->ex9_enable == 1))
   13461 	{
   13462 	  irel_ptr = ptr->irel_head;
   13463 	  if (ptr->h == NULL)
   13464 	    {
   13465 	      /* Local symbol.  */
   13466 	      internal_relocs = _bfd_elf_link_read_relocs
   13467 		(ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
   13468 	      irelend = internal_relocs + ptr->sec->reloc_count;
   13469 
   13470 	      if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
   13471 					       &contents))
   13472 		return FALSE;
   13473 
   13474 	      while (irel_ptr)
   13475 		{
   13476 		  if (irel_ptr->keep == 0 && irel_ptr->next)
   13477 		    {
   13478 		      /* The one can be replaced.  We have to check whether
   13479 			 there is any alignment point in the region.  */
   13480 		      irel = irel_ptr->irel;
   13481 		      while (((irel_ptr->next->keep == 0
   13482 			       && irel < irel_ptr->next->irel)
   13483 			      || (irel_ptr->next->keep == 1 && irel < irelend))
   13484 			     && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
   13485 				  && (irel->r_addend & 0x1f) == 2))
   13486 			irel++;
   13487 		      if (irel >= irelend
   13488 			  || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
   13489 			       && (irel->r_addend & 0x1f) == 2
   13490 			       && ((irel->r_offset - get_nds32_elf_blank_total
   13491 				    (&relax_blank_list, irel->r_offset, 1))
   13492 				   & 0x02) == 0))
   13493 			{
   13494 			  /* Replace by ifcall9.  */
   13495 			  bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
   13496 			  if (!insert_nds32_elf_blank_recalc_total
   13497 			      (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
   13498 			    return FALSE;
   13499 			  irel_ptr->irel->r_info =
   13500 			    ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
   13501 					  R_NDS32_10IFCU_PCREL_RELA);
   13502 			}
   13503 		    }
   13504 		  irel_ptr = irel_ptr->next;
   13505 		}
   13506 
   13507 	      /* Delete the redundant code.  */
   13508 	      if (relax_blank_list)
   13509 		{
   13510 		  nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
   13511 						 relax_blank_list);
   13512 		  relax_blank_list = NULL;
   13513 		}
   13514 	    }
   13515 	  else
   13516 	    {
   13517 	      /* Global symbol.  */
   13518 	      while (irel_ptr)
   13519 		{
   13520 		  if (irel_ptr->keep == 0 && irel_ptr->next)
   13521 		    {
   13522 		      /* The one can be replaced, and we have to check
   13523 			 whether there is any alignment point in the region.  */
   13524 		      internal_relocs = _bfd_elf_link_read_relocs
   13525 			(irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
   13526 			 TRUE /* keep_memory */);
   13527 		      irelend = internal_relocs + irel_ptr->sec->reloc_count;
   13528 		      if (!nds32_get_section_contents
   13529 			     (irel_ptr->sec->owner, irel_ptr->sec, &contents))
   13530 			return FALSE;
   13531 
   13532 		      irel = irel_ptr->irel;
   13533 		      while (((irel_ptr->sec == irel_ptr->next->sec
   13534 			       && irel_ptr->next->keep == 0
   13535 			       && irel < irel_ptr->next->irel)
   13536 			      || ((irel_ptr->sec != irel_ptr->next->sec
   13537 				   || irel_ptr->next->keep == 1)
   13538 				  && irel < irelend))
   13539 			     && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
   13540 				  && (irel->r_addend & 0x1f) == 2))
   13541 			irel++;
   13542 		      if (irel >= irelend
   13543 			  || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
   13544 			       && (irel->r_addend & 0x1f) == 2
   13545 			       && ((irel->r_offset
   13546 				    - get_nds32_elf_blank_total (&relax_blank_list,
   13547 							    irel->r_offset, 1)) & 0x02) == 0))
   13548 			{
   13549 			  /* Replace by ifcall9.  */
   13550 			  bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
   13551 			  if (!insert_nds32_elf_blank_recalc_total
   13552 			      (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
   13553 			    return FALSE;
   13554 
   13555 			  /* Delete the redundant code, and clear the relocation.  */
   13556 			  nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
   13557 							 irel_ptr->sec,
   13558 							 relax_blank_list);
   13559 			  irel_ptr->irel->r_info =
   13560 			    ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
   13561 					  R_NDS32_10IFCU_PCREL_RELA);
   13562 			  relax_blank_list = NULL;
   13563 			}
   13564 		    }
   13565 
   13566 		  irel_ptr = irel_ptr->next;
   13567 		}
   13568 	    }
   13569 	}
   13570       ptr = ptr->next;
   13571     }
   13572 
   13573   return TRUE;
   13574 }
   13575 
   13576 /* Relocate ifcall.  */
   13577 
   13578 static bfd_boolean
   13579 nds32_elf_ifc_reloc (void)
   13580 {
   13581   struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
   13582   struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
   13583   struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
   13584   bfd_vma relocation, address;
   13585   unsigned short insn16;
   13586   bfd_byte *contents = NULL;
   13587   static bfd_boolean done = FALSE;
   13588 
   13589   if (done)
   13590     return TRUE;
   13591 
   13592   done = TRUE;
   13593 
   13594   while (ptr)
   13595     {
   13596       /* Check the entry is enable ifcall.  */
   13597       if (ptr->enable == 1 || ptr->ex9_enable == 1)
   13598 	{
   13599 	  /* Get the reserve jump.  */
   13600 	  irel_ptr = ptr->irel_head;
   13601 	  while (irel_ptr)
   13602 	    {
   13603 	      if (irel_ptr->keep == 1)
   13604 		{
   13605 		  irel_keeper = irel_ptr;
   13606 		  break;
   13607 		}
   13608 	      irel_ptr = irel_ptr->next;
   13609 	    }
   13610 
   13611 	  irel_ptr = ptr->irel_head;
   13612 	  if (ptr->h == NULL)
   13613 	    {
   13614 	      /* Local symbol.  */
   13615 	      if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec, &contents))
   13616 		return FALSE;
   13617 
   13618 	      while (irel_ptr)
   13619 		{
   13620 		  if (irel_ptr->keep == 0
   13621 		      && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
   13622 		    {
   13623 		      relocation = irel_keeper->irel->r_offset;
   13624 		      relocation = relocation - irel_ptr->irel->r_offset;
   13625 		      while (irel_keeper && relocation > 1022)
   13626 			{
   13627 			  irel_keeper = irel_keeper->next;
   13628 			  if (irel_keeper && irel_keeper->keep == 1)
   13629 			    {
   13630 			      relocation = irel_keeper->irel->r_offset;
   13631 			      relocation = relocation - irel_ptr->irel->r_offset;
   13632 			    }
   13633 			}
   13634 		      if (relocation > 1022)
   13635 			{
   13636 			  /* Double check.  */
   13637 			  irel_keeper = ptr->irel_head;
   13638 			  while (irel_keeper)
   13639 			    {
   13640 			      if (irel_keeper->keep == 1)
   13641 				{
   13642 				  relocation = irel_keeper->irel->r_offset;
   13643 				  relocation = relocation - irel_ptr->irel->r_offset;
   13644 				}
   13645 			      if (relocation <= 1022)
   13646 				break;
   13647 			      irel_keeper = irel_keeper->next;
   13648 			    }
   13649 			  if (!irel_keeper)
   13650 			    return FALSE;
   13651 			}
   13652 		      irel_ptr->irel->r_info =
   13653 			ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
   13654 				      R_NDS32_NONE);
   13655 		      insn16 = INSN_IFCALL9 | (relocation >> 1);
   13656 		      bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
   13657 		    }
   13658 		  irel_ptr = irel_ptr->next;
   13659 		}
   13660 	    }
   13661 	  else
   13662 	    {
   13663 	      /* Global symbol.  */
   13664 	      while (irel_ptr)
   13665 		{
   13666 		  if (irel_ptr->keep == 0
   13667 		      && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
   13668 		    {
   13669 		      /* Get the distance between ifcall and jump.  */
   13670 		      relocation = (irel_keeper->irel->r_offset
   13671 				    + irel_keeper->sec->output_section->vma
   13672 				    + irel_keeper->sec->output_offset);
   13673 		      address = (irel_ptr->irel->r_offset
   13674 				 + irel_ptr->sec->output_section->vma
   13675 				 + irel_ptr->sec->output_offset);
   13676 		      relocation = relocation - address;
   13677 
   13678 		      /* The distance is over ragne, find callee again.  */
   13679 		      while (irel_keeper && relocation > 1022)
   13680 			{
   13681 			  irel_keeper = irel_keeper->next;
   13682 			  if (irel_keeper && irel_keeper->keep ==1)
   13683 			    {
   13684 			      relocation = (irel_keeper->irel->r_offset
   13685 					    + irel_keeper->sec->output_section->vma
   13686 					    + irel_keeper->sec->output_offset);
   13687 			      relocation = relocation - address;
   13688 			    }
   13689 			}
   13690 
   13691 		      if (relocation > 1022)
   13692 			{
   13693 			  /* Double check.  */
   13694 			  irel_keeper = ptr->irel_head;
   13695 			  while (irel_keeper)
   13696 			    {
   13697 			      if (irel_keeper->keep == 1)
   13698 				{
   13699 
   13700 				  relocation = (irel_keeper->irel->r_offset
   13701 						+ irel_keeper->sec->output_section->vma
   13702 						+ irel_keeper->sec->output_offset);
   13703 				  relocation = relocation - address;
   13704 				}
   13705 			      if (relocation <= 1022)
   13706 				break;
   13707 			      irel_keeper = irel_keeper->next;
   13708 			    }
   13709 			  if (!irel_keeper)
   13710 			    return FALSE;
   13711 			}
   13712 		      if (!nds32_get_section_contents
   13713 			  (irel_ptr->sec->owner, irel_ptr->sec, &contents))
   13714 			return FALSE;
   13715 		      insn16 = INSN_IFCALL9 | (relocation >> 1);
   13716 		      bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
   13717 		      irel_ptr->irel->r_info =
   13718 			ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
   13719 				      R_NDS32_NONE);
   13720 		    }
   13721 		  irel_ptr =irel_ptr->next;
   13722 		}
   13723 	    }
   13724 	}
   13725       ptr = ptr->next;
   13726     }
   13727 
   13728   return TRUE;
   13729 }
   13730 
   13731 /* End of IFC relaxation.  */
   13732 
   13733 /* EX9 Instruction Table Relaxation.  */
   13735 
   13736 /* Global hash list.  */
   13737 struct elf_link_hash_entry_list
   13738 {
   13739   struct elf_link_hash_entry *h;
   13740   struct elf_link_hash_entry_list *next;
   13741 };
   13742 
   13743 /* Save different destination but same insn.  */
   13744 struct elf_link_hash_entry_mul_list
   13745 {
   13746   /* Global symbol times.  */
   13747   int times;
   13748   /* Save relocation for each global symbol but useful??  */
   13749   Elf_Internal_Rela *irel;
   13750   /* For sethi, two sethi may have the same high-part but different low-parts.  */
   13751   Elf_Internal_Rela rel_backup;
   13752   struct elf_link_hash_entry_list *h_list;
   13753   struct elf_link_hash_entry_mul_list *next;
   13754 };
   13755 
   13756 /* Instruction hash table.  */
   13757 struct elf_nds32_code_hash_entry
   13758 {
   13759   struct bfd_hash_entry root;
   13760   int times;
   13761   /* For insn that can use relocation or constant ex: sethi.  */
   13762   int const_insn;
   13763   asection *sec;
   13764   struct elf_link_hash_entry_mul_list *m_list;
   13765   /* Using r_addend.  */
   13766   Elf_Internal_Rela *irel;
   13767   /* Using r_info.  */
   13768   Elf_Internal_Rela rel_backup;
   13769 };
   13770 
   13771 /* Instruction count list.  */
   13772 struct elf_nds32_insn_times_entry
   13773 {
   13774   const char *string;
   13775   int times;
   13776   int order;
   13777   asection *sec;
   13778   struct elf_link_hash_entry_mul_list *m_list;
   13779   Elf_Internal_Rela *irel;
   13780   Elf_Internal_Rela rel_backup;
   13781   struct elf_nds32_insn_times_entry *next;
   13782 };
   13783 
   13784 /* J and JAL symbol list.  */
   13785 struct elf_nds32_symbol_entry
   13786 {
   13787   char *string;
   13788   unsigned long insn;
   13789   struct elf_nds32_symbol_entry *next;
   13790 };
   13791 
   13792 /* Relocation list.  */
   13793 struct elf_nds32_irel_entry
   13794 {
   13795   Elf_Internal_Rela *irel;
   13796   struct elf_nds32_irel_entry *next;
   13797 };
   13798 
   13799 /* ex9.it insn need to be fixed.  */
   13800 struct elf_nds32_ex9_refix
   13801 {
   13802   Elf_Internal_Rela *irel;
   13803   asection *sec;
   13804   struct elf_link_hash_entry *h;
   13805   int order;
   13806   struct elf_nds32_ex9_refix *next;
   13807 };
   13808 
   13809 static struct bfd_hash_table ex9_code_table;
   13810 static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
   13811 static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;
   13812 
   13813 /* EX9 hash function.  */
   13814 
   13815 static struct bfd_hash_entry *
   13816 nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
   13817 			     struct bfd_hash_table *table,
   13818 			     const char *string)
   13819 {
   13820   struct elf_nds32_code_hash_entry *ret;
   13821 
   13822   /* Allocate the structure if it has not already been allocated by a
   13823      subclass.  */
   13824   if (entry == NULL)
   13825     {
   13826       entry = (struct bfd_hash_entry *)
   13827 	bfd_hash_allocate (table, sizeof (*ret));
   13828       if (entry == NULL)
   13829 	return entry;
   13830     }
   13831 
   13832   /* Call the allocation method of the superclass.  */
   13833   entry = bfd_hash_newfunc (entry, table, string);
   13834   if (entry == NULL)
   13835     return entry;
   13836 
   13837   ret = (struct elf_nds32_code_hash_entry*) entry;
   13838   ret->times = 0;
   13839   ret->const_insn = 0;
   13840   ret->m_list = NULL;
   13841   ret->sec = NULL;
   13842   ret->irel = NULL;
   13843   return &ret->root;
   13844 }
   13845 
   13846 /* Insert ex9 entry
   13847    this insert must be stable sorted by times.  */
   13848 
   13849 static void
   13850 nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
   13851 {
   13852   struct elf_nds32_insn_times_entry *temp;
   13853   struct elf_nds32_insn_times_entry *temp2;
   13854 
   13855   if (ex9_insn_head == NULL)
   13856     {
   13857       ex9_insn_head = ptr;
   13858       ptr->next = NULL;
   13859     }
   13860   else
   13861     {
   13862       temp = ex9_insn_head;
   13863       temp2 = ex9_insn_head;
   13864       while (temp->next &&
   13865 	     (temp->next->times >= ptr->times
   13866 	      || temp->times == -1))
   13867 	{
   13868 	  if (temp->times == -1)
   13869 	    temp2 = temp;
   13870 	  temp = temp->next;
   13871 	}
   13872       if (ptr->times > temp->times && temp->times != -1)
   13873 	{
   13874 	  ptr->next = temp;
   13875 	  if (temp2->times == -1)
   13876 	    temp2->next = ptr;
   13877 	  else
   13878 	    ex9_insn_head = ptr;
   13879 	}
   13880       else if (temp->next == NULL)
   13881 	{
   13882 	  temp->next = ptr;
   13883 	  ptr->next = NULL;
   13884 	}
   13885       else
   13886 	{
   13887 	  ptr->next = temp->next;
   13888 	  temp->next = ptr;
   13889 	}
   13890     }
   13891 }
   13892 
   13893 /* Examine each insn times in hash table.
   13894    Handle multi-link hash entry.
   13895 
   13896    TODO: This function doesn't assign so much info since it is fake.  */
   13897 
   13898 static int
   13899 nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
   13900 {
   13901   struct elf_nds32_insn_times_entry *ptr;
   13902   int times;
   13903 
   13904   if (h->m_list == NULL)
   13905     {
   13906       /* Local symbol insn or insn without relocation.  */
   13907       if (h->times < 3)
   13908 	return TRUE;
   13909 
   13910       ptr = (struct elf_nds32_insn_times_entry *)
   13911 	bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   13912       ptr->times = h->times;
   13913       ptr->string = h->root.string;
   13914       ptr->m_list = NULL;
   13915       ptr->sec = h->sec;
   13916       ptr->irel = h->irel;
   13917       ptr->rel_backup = h->rel_backup;
   13918       nds32_elf_ex9_insert_entry (ptr);
   13919     }
   13920   else
   13921     {
   13922       /* Global symbol insn.  */
   13923       /* Only sethi insn has multiple m_list.  */
   13924       struct elf_link_hash_entry_mul_list *m_list = h->m_list;
   13925 
   13926       times = 0;
   13927       while (m_list)
   13928 	{
   13929 	  times += m_list->times;
   13930 	  m_list = m_list->next;
   13931 	}
   13932       if (times >= 3)
   13933 	{
   13934 	  m_list = h->m_list;
   13935 	  ptr = (struct elf_nds32_insn_times_entry *)
   13936 	    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   13937 	  ptr->times = times; /* Use the total times.  */
   13938 	  ptr->string = h->root.string;
   13939 	  ptr->m_list = m_list;
   13940 	  ptr->sec = h->sec;
   13941 	  ptr->irel = m_list->irel;
   13942 	  ptr->rel_backup = m_list->rel_backup;
   13943 	  nds32_elf_ex9_insert_entry (ptr);
   13944 	}
   13945       if (h->const_insn == 1)
   13946 	{
   13947 	  /* sethi with constant value.  */
   13948 	  if (h->times < 3)
   13949 	    return TRUE;
   13950 
   13951 	  ptr = (struct elf_nds32_insn_times_entry *)
   13952 	    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   13953 	  ptr->times = h->times;
   13954 	  ptr->string = h->root.string;
   13955 	  ptr->m_list = NULL;
   13956 	  ptr->sec = NULL;
   13957 	  ptr->irel = NULL;
   13958 	  ptr->rel_backup = h->rel_backup;
   13959 	  nds32_elf_ex9_insert_entry (ptr);
   13960 	}
   13961     }
   13962   return TRUE;
   13963 }
   13964 
   13965 /* Count each insn times in hash table.
   13966    Handle multi-link hash entry.  */
   13967 
   13968 static int
   13969 nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
   13970 {
   13971   int reservation, times;
   13972   unsigned long relocation, min_relocation;
   13973   struct elf_nds32_insn_times_entry *ptr;
   13974 
   13975   if (h->m_list == NULL)
   13976     {
   13977       /* Local symbol insn or insn without relocation.  */
   13978       if (h->times < 3)
   13979 	return TRUE;
   13980       ptr = (struct elf_nds32_insn_times_entry *)
   13981 	bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   13982       ptr->times = h->times;
   13983       ptr->string = h->root.string;
   13984       ptr->m_list = NULL;
   13985       ptr->sec = h->sec;
   13986       ptr->irel = h->irel;
   13987       ptr->rel_backup = h->rel_backup;
   13988       nds32_elf_ex9_insert_entry (ptr);
   13989     }
   13990   else
   13991     {
   13992       /* Global symbol insn.  */
   13993       /* Only sethi insn has multiple m_list.  */
   13994       struct elf_link_hash_entry_mul_list *m_list = h->m_list;
   13995 
   13996       if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
   13997 	  && m_list->next != NULL)
   13998 	{
   13999 	  /* Sethi insn has different symbol or addend but has same hi20.  */
   14000 	  times = 0;
   14001 	  reservation = 1;
   14002 	  relocation = 0;
   14003 	  min_relocation = 0xffffffff;
   14004 	  while (m_list)
   14005 	    {
   14006 	      /* Get the minimum sethi address
   14007 		 and calculate how many entry the sethi-list have to use.  */
   14008 	      if ((m_list->h_list->h->root.type == bfd_link_hash_defined
   14009 		   || m_list->h_list->h->root.type == bfd_link_hash_defweak)
   14010 		  && (m_list->h_list->h->root.u.def.section != NULL
   14011 		      && m_list->h_list->h->root.u.def.section->output_section != NULL))
   14012 		{
   14013 		  relocation = (m_list->h_list->h->root.u.def.value +
   14014 				m_list->h_list->h->root.u.def.section->output_section->vma +
   14015 				m_list->h_list->h->root.u.def.section->output_offset);
   14016 		  relocation += m_list->irel->r_addend;
   14017 		}
   14018 	      else
   14019 		relocation = 0;
   14020 	      if (relocation < min_relocation)
   14021 		min_relocation = relocation;
   14022 	      times += m_list->times;
   14023 	      m_list = m_list->next;
   14024 	    }
   14025 	  if (min_relocation < ex9_relax_size)
   14026 	    reservation = (min_relocation >> 12) + 1;
   14027 	  else
   14028 	    reservation = (min_relocation >> 12)
   14029 			  - ((min_relocation - ex9_relax_size) >> 12) + 1;
   14030 	  if (reservation < (times / 3))
   14031 	    {
   14032 	      /* Efficient enough to use ex9.  */
   14033 	      int i;
   14034 
   14035 	      for (i = reservation ; i > 0; i--)
   14036 		{
   14037 		  /* Allocate number of reservation ex9 entry.  */
   14038 		  ptr = (struct elf_nds32_insn_times_entry *)
   14039 		    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   14040 		  ptr->times = h->m_list->times / reservation;
   14041 		  ptr->string = h->root.string;
   14042 		  ptr->m_list = h->m_list;
   14043 		  ptr->sec = h->sec;
   14044 		  ptr->irel = h->m_list->irel;
   14045 		  ptr->rel_backup = h->m_list->rel_backup;
   14046 		  nds32_elf_ex9_insert_entry (ptr);
   14047 		}
   14048 	    }
   14049 	}
   14050       else
   14051 	{
   14052 	  /* Normal global symbol that means no different address symbol
   14053 	     using same ex9 entry.  */
   14054 	  if (m_list->times >= 3)
   14055 	    {
   14056 	      ptr = (struct elf_nds32_insn_times_entry *)
   14057 		bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   14058 	      ptr->times = m_list->times;
   14059 	      ptr->string = h->root.string;
   14060 	      ptr->m_list = h->m_list;
   14061 	      ptr->sec = h->sec;
   14062 	      ptr->irel = h->m_list->irel;
   14063 	      ptr->rel_backup = h->m_list->rel_backup;
   14064 	      nds32_elf_ex9_insert_entry (ptr);
   14065 	    }
   14066 	}
   14067 
   14068       if (h->const_insn == 1)
   14069 	{
   14070 	  /* sethi with constant value.  */
   14071 	  if (h->times < 3)
   14072 	    return TRUE;
   14073 
   14074 	  ptr = (struct elf_nds32_insn_times_entry *)
   14075 	    bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   14076 	  ptr->times = h->times;
   14077 	  ptr->string = h->root.string;
   14078 	  ptr->m_list = NULL;
   14079 	  ptr->sec = NULL;
   14080 	  ptr->irel = NULL;
   14081 	  ptr->rel_backup = h->rel_backup;
   14082 	  nds32_elf_ex9_insert_entry (ptr);
   14083 	}
   14084     }
   14085 
   14086   return TRUE;
   14087 }
   14088 
   14089 /* Hash table traverse function.  */
   14090 
   14091 static void
   14092 nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
   14093 {
   14094   unsigned int i;
   14095 
   14096   ex9_code_table.frozen = 1;
   14097   for (i = 0; i < ex9_code_table.size; i++)
   14098     {
   14099       struct bfd_hash_entry *p;
   14100 
   14101       for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
   14102 	if (!func ((struct elf_nds32_code_hash_entry *) p))
   14103 	  goto out;
   14104     }
   14105 out:
   14106   ex9_code_table.frozen = 0;
   14107 }
   14108 
   14109 
   14110 /* Give order number to insn list.  */
   14111 
   14112 static void
   14113 nds32_elf_order_insn_times (struct bfd_link_info *info)
   14114 {
   14115   struct elf_nds32_insn_times_entry *ex9_insn;
   14116   struct elf_nds32_insn_times_entry *temp = NULL;
   14117   struct elf_nds32_link_hash_table *table;
   14118   int ex9_limit;
   14119   int number = 0;
   14120 
   14121   if (ex9_insn_head == NULL)
   14122     return;
   14123 
   14124 /* The max number of entries is 512.  */
   14125   ex9_insn = ex9_insn_head;
   14126   table = nds32_elf_hash_table (info);
   14127   ex9_limit = table->ex9_limit;
   14128 
   14129   ex9_insn = ex9_insn_head;
   14130 
   14131   while (ex9_insn != NULL && number < ex9_limit)
   14132     {
   14133       ex9_insn->order = number;
   14134       number++;
   14135       temp = ex9_insn;
   14136       ex9_insn = ex9_insn->next;
   14137     }
   14138 
   14139   if (ex9_insn && temp)
   14140     temp->next = NULL;
   14141 
   14142   while (ex9_insn != NULL)
   14143     {
   14144       /* Free useless entry.  */
   14145       temp = ex9_insn;
   14146       ex9_insn = ex9_insn->next;
   14147       free (temp);
   14148     }
   14149 }
   14150 
   14151 /* Build .ex9.itable section.  */
   14152 
   14153 static void
   14154 nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
   14155 {
   14156   asection *table_sec;
   14157   struct elf_nds32_insn_times_entry *ptr;
   14158   bfd *it_abfd;
   14159   int number = 0;
   14160   bfd_byte *contents = NULL;
   14161 
   14162   for (it_abfd = link_info->input_bfds; it_abfd != NULL;
   14163        it_abfd = it_abfd->link.next)
   14164     {
   14165       /* Find the section .ex9.itable, and put all entries into it.  */
   14166       table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
   14167       if (table_sec != NULL)
   14168 	{
   14169 	  if (!nds32_get_section_contents (it_abfd, table_sec, &contents))
   14170 	    return;
   14171 
   14172 	  for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
   14173 	    number++;
   14174 
   14175 	  table_sec->size = number * 4;
   14176 
   14177 	  if (number == 0)
   14178 	    return;
   14179 
   14180 	  elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
   14181 	  number = 0;
   14182 	  for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
   14183 	    {
   14184 	      long val;
   14185 
   14186 	      val = strtol (ptr->string, NULL, 16);
   14187 	      bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
   14188 	      number++;
   14189 	    }
   14190 	  break;
   14191 	}
   14192     }
   14193 }
   14194 
   14195 /* Get insn with regs according to relocation type.  */
   14196 
   14197 static void
   14198 nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
   14199 			     uint32_t insn, uint32_t *insn_with_reg)
   14200 {
   14201   reloc_howto_type *howto = NULL;
   14202 
   14203   if (irel == NULL
   14204       || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
   14205 	  && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
   14206 	     >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
   14207     {
   14208       *insn_with_reg = insn;
   14209       return;
   14210     }
   14211 
   14212   howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
   14213   *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
   14214 }
   14215 
   14216 /* Mask number of address bits according to relocation.  */
   14217 
   14218 static unsigned long
   14219 nds32_elf_irel_mask (Elf_Internal_Rela *irel)
   14220 {
   14221   reloc_howto_type *howto = NULL;
   14222 
   14223   if (irel == NULL
   14224       || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
   14225 	  && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
   14226 	     >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
   14227     return 0;
   14228 
   14229   howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
   14230   return howto->dst_mask;
   14231 }
   14232 
   14233 static void
   14234 nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
   14235 			     struct elf_nds32_irel_entry *irel_ptr)
   14236 {
   14237   if (*irel_list == NULL)
   14238     {
   14239       *irel_list = irel_ptr;
   14240       irel_ptr->next = NULL;
   14241     }
   14242   else
   14243     {
   14244       irel_ptr->next = *irel_list;
   14245       *irel_list = irel_ptr;
   14246     }
   14247 }
   14248 
   14249 static void
   14250 nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
   14251 			  struct elf_link_hash_entry *h, int order)
   14252 {
   14253   struct elf_nds32_ex9_refix *ptr;
   14254 
   14255   ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
   14256   ptr->sec = sec;
   14257   ptr->irel = irel;
   14258   ptr->h = h;
   14259   ptr->order = order;
   14260   ptr->next = NULL;
   14261 
   14262   if (ex9_refix_head == NULL)
   14263     ex9_refix_head = ptr;
   14264   else
   14265     {
   14266       struct elf_nds32_ex9_refix *temp = ex9_refix_head;
   14267 
   14268       while (temp->next != NULL)
   14269 	temp = temp->next;
   14270       temp->next = ptr;
   14271     }
   14272 }
   14273 
   14274 enum
   14275 {
   14276   DATA_EXIST = 1,
   14277   CLEAN_PRE = 1 << 1,
   14278   PUSH_PRE = 1 << 2
   14279 };
   14280 
   14281 /* Check relocation type if supporting for ex9.  */
   14282 
   14283 static int
   14284 nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
   14285 				Elf_Internal_Rela **irel,
   14286 				Elf_Internal_Rela *irelend,
   14287 				nds32_elf_blank_t *relax_blank_list,
   14288 				asection *sec,bfd_vma *off,
   14289 				bfd_byte *contents)
   14290 {
   14291   /* Suppress ex9 if `.no_relax ex9' or inner loop.  */
   14292   bfd_boolean nested_ex9, nested_loop;
   14293   bfd_boolean ex9_loop_aware;
   14294   /* We use the highest 1 byte of result to record
   14295      how many bytes location counter has to move.  */
   14296   int result = 0;
   14297   Elf_Internal_Rela *irel_save = NULL;
   14298   struct elf_nds32_link_hash_table *table;
   14299 
   14300   table = nds32_elf_hash_table (info);
   14301   ex9_loop_aware = table->ex9_loop_aware;
   14302 
   14303   while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
   14304     {
   14305       switch (ELF32_R_TYPE ((*irel)->r_info))
   14306 	{
   14307 	case R_NDS32_RELAX_REGION_BEGIN:
   14308 	  /* Ignore code block.  */
   14309 	  nested_ex9 = FALSE;
   14310 	  nested_loop = FALSE;
   14311 	  if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
   14312 	      || (ex9_loop_aware
   14313 		  && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
   14314 	    {
   14315 	      /* Check the region if loop or not.  If it is true and
   14316 		 ex9-loop-aware is true, ignore the region till region end.  */
   14317 	      /* To save the status for in .no_relax ex9 region and
   14318 		 loop region to conform the block can do ex9 relaxation.  */
   14319 	      nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
   14320 	      nested_loop = (ex9_loop_aware
   14321 			     && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
   14322 	      while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
   14323 		{
   14324 		  (*irel)++;
   14325 		  if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
   14326 		    {
   14327 		      /* There may be nested region.  */
   14328 		      if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
   14329 			nested_ex9 = TRUE;
   14330 		      else if (ex9_loop_aware
   14331 			       && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
   14332 			nested_loop = TRUE;
   14333 		    }
   14334 		  else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
   14335 		    {
   14336 		      /* The end of region.  */
   14337 		      if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
   14338 			nested_ex9 = FALSE;
   14339 		      else if (ex9_loop_aware
   14340 			       && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
   14341 			nested_loop = FALSE;
   14342 		    }
   14343 		  else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
   14344 			   && ((*irel)->r_addend & 0x1f) == 2)
   14345 		    {
   14346 		      /* Alignment exist in the region.  */
   14347 		      result |= CLEAN_PRE;
   14348 		      if (((*irel)->r_offset -
   14349 			   get_nds32_elf_blank_total (&relax_blank_list,
   14350 						      (*irel)->r_offset, 0)) & 0x02)
   14351 			result |= PUSH_PRE;
   14352 		    }
   14353 		}
   14354 	      if ((*irel) >= irelend)
   14355 		*off = sec->size;
   14356 	      else
   14357 		*off = (*irel)->r_offset;
   14358 
   14359 	      /* The final instruction in the region, regard this one as data to ignore it.  */
   14360 	      result |= DATA_EXIST;
   14361 	      return result;
   14362 	    }
   14363 	  break;
   14364 
   14365 	case R_NDS32_LABEL:
   14366 	  if (((*irel)->r_addend & 0x1f) == 2)
   14367 	    {
   14368 	      /* Check this point is align and decide to do ex9 or not.  */
   14369 	      result |= CLEAN_PRE;
   14370 	      if (((*irel)->r_offset -
   14371 		   get_nds32_elf_blank_total (&relax_blank_list,
   14372 					      (*irel)->r_offset, 0)) & 0x02)
   14373 		result |= PUSH_PRE;
   14374 	    }
   14375 	  break;
   14376 	case R_NDS32_32_RELA:
   14377 	  /* Data.  */
   14378 	  result |= (4 << 24);
   14379 	  result |= DATA_EXIST;
   14380 	  break;
   14381 	case R_NDS32_16_RELA:
   14382 	  /* Data.  */
   14383 	  result |= (2 << 24);
   14384 	  result |= DATA_EXIST;
   14385 	  break;
   14386 	case R_NDS32_DATA:
   14387 	  /* Data.  */
   14388 	  /* The least code alignment is 2.  If the data is only one byte,
   14389 	     we have to shift one more byte.  */
   14390 	  if ((*irel)->r_addend == 1)
   14391 	    result |= ((*irel)->r_addend << 25) ;
   14392 	  else
   14393 	    result |= ((*irel)->r_addend << 24) ;
   14394 
   14395 	  result |= DATA_EXIST;
   14396 	  break;
   14397 
   14398 	case R_NDS32_25_PCREL_RELA:
   14399 	case R_NDS32_SDA16S3_RELA:
   14400 	case R_NDS32_SDA15S3_RELA:
   14401 	case R_NDS32_SDA15S3:
   14402 	case R_NDS32_SDA17S2_RELA:
   14403 	case R_NDS32_SDA15S2_RELA:
   14404 	case R_NDS32_SDA12S2_SP_RELA:
   14405 	case R_NDS32_SDA12S2_DP_RELA:
   14406 	case R_NDS32_SDA15S2:
   14407 	case R_NDS32_SDA18S1_RELA:
   14408 	case R_NDS32_SDA15S1_RELA:
   14409 	case R_NDS32_SDA15S1:
   14410 	case R_NDS32_SDA19S0_RELA:
   14411 	case R_NDS32_SDA15S0_RELA:
   14412 	case R_NDS32_SDA15S0:
   14413 	case R_NDS32_HI20_RELA:
   14414 	case R_NDS32_LO12S0_ORI_RELA:
   14415 	case R_NDS32_LO12S0_RELA:
   14416 	case R_NDS32_LO12S1_RELA:
   14417 	case R_NDS32_LO12S2_RELA:
   14418 	  /* These relocation is supported ex9 relaxation currently.  */
   14419 	  /* We have to save the relocation for using later, since we have
   14420 	     to check there is any alignment in the same address.  */
   14421 	  irel_save = *irel;
   14422 	  break;
   14423 	default:
   14424 	  /* Not support relocations.  */
   14425 	  if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
   14426 	      && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE
   14427 	      && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_INSN16)
   14428 	    {
   14429 	      /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
   14430 		 But we have to consider if there is any side-effect.  */
   14431 	      if (!(result & DATA_EXIST))
   14432 		{
   14433 		  /* We have to confirm there is no data relocation in the
   14434 		     same address.  In general case, this won't happen.  */
   14435 		  /* We have to do ex9 conservative, for those relocation not
   14436 		     considerd we ignore instruction.  */
   14437 		  result |= DATA_EXIST;
   14438 		  if (*(contents + *off) & 0x80)
   14439 		    result |= (2 << 24);
   14440 		  else
   14441 		    result |= (4 << 24);
   14442 		  break;
   14443 		}
   14444 	    }
   14445 	}
   14446       if ((*irel) < irelend
   14447 	  && ((*irel) + 1) < irelend
   14448 	  && (*irel)->r_offset == ((*irel) + 1)->r_offset)
   14449 	/* There are relocations pointing to the same address, we have to
   14450 	   check all of them.  */
   14451 	(*irel)++;
   14452       else
   14453 	{
   14454 	  if (irel_save)
   14455 	    *irel = irel_save;
   14456 	  return result;
   14457 	}
   14458     }
   14459   return result;
   14460 }
   14461 
   14462 /* Replace with ex9 instruction.  */
   14463 
   14464 static bfd_boolean
   14465 nds32_elf_ex9_push_insn (uint16_t insn16, bfd_byte *contents, bfd_vma pre_off,
   14466 			 nds32_elf_blank_t **relax_blank_list,
   14467 			 struct elf_nds32_irel_entry *pre_irel_ptr,
   14468 			 struct elf_nds32_irel_entry **irel_list)
   14469 {
   14470   if (insn16 != 0)
   14471     {
   14472       /* Implement the ex9 relaxation.  */
   14473       bfd_putb16 (insn16, contents + pre_off);
   14474       if (!insert_nds32_elf_blank_recalc_total (relax_blank_list,
   14475 						pre_off + 2, 2))
   14476 	return FALSE;
   14477       if (pre_irel_ptr != NULL)
   14478 	nds32_elf_insert_irel_entry (irel_list, pre_irel_ptr);
   14479     }
   14480   return TRUE;
   14481 }
   14482 
   14483 /* Replace input file instruction which is in ex9 itable.  */
   14484 
   14485 static bfd_boolean
   14486 nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
   14487 {
   14488   struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
   14489   bfd_byte *contents = NULL;
   14490   bfd_vma off;
   14491   uint16_t insn16, insn_ex9;
   14492   /* `pre_*' are used to track previous instruction that can use ex9.it.  */
   14493   bfd_vma pre_off = -1;
   14494   uint16_t pre_insn16 = 0;
   14495   struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
   14496   Elf_Internal_Rela *internal_relocs;
   14497   Elf_Internal_Rela *irel;
   14498   Elf_Internal_Rela *irelend;
   14499   Elf_Internal_Shdr *symtab_hdr;
   14500   Elf_Internal_Sym *isym = NULL;
   14501   nds32_elf_blank_t *relax_blank_list = NULL;
   14502   uint32_t insn = 0;
   14503   uint32_t insn_with_reg = 0;
   14504   uint32_t it_insn;
   14505   uint32_t it_insn_with_reg;
   14506   unsigned long r_symndx;
   14507   asection *isec;
   14508   struct elf_nds32_irel_entry *irel_list = NULL;
   14509   struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
   14510   int data_flag, do_replace, save_irel;
   14511   struct elf_link_hash_entry_list *h_list;
   14512 
   14513 
   14514   /* Load section instructions, relocations, and symbol table.  */
   14515   if (!nds32_get_section_contents (abfd, sec, &contents)
   14516       || !nds32_get_local_syms (abfd, sec, &isym))
   14517     return FALSE;
   14518   internal_relocs =
   14519     _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, TRUE /* keep_memory */);
   14520   irelend = internal_relocs + sec->reloc_count;
   14521   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   14522 
   14523   off = 0;
   14524 
   14525   /* Check if the object enable ex9.  */
   14526   irel = find_relocs_at_address (internal_relocs, internal_relocs,
   14527 				 irelend, R_NDS32_RELAX_ENTRY);
   14528 
   14529   /* Check this section trigger ex9 relaxation.  */
   14530   if (irel == NULL
   14531       || irel >= irelend
   14532       || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
   14533       || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
   14534 	  && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
   14535     return TRUE;
   14536 
   14537   irel = internal_relocs;
   14538 
   14539   /* Check alignment and fetch proper relocation.  */
   14540   while (off < sec->size)
   14541     {
   14542       struct elf_link_hash_entry *h = NULL;
   14543       struct elf_nds32_irel_entry *irel_ptr = NULL;
   14544 
   14545       /* Syn the instruction and the relocation.  */
   14546       while (irel != NULL && irel < irelend && irel->r_offset < off)
   14547 	irel++;
   14548 
   14549       data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
   14550 						  relax_blank_list, sec,
   14551 						  &off, contents);
   14552       if (data_flag & PUSH_PRE)
   14553 	if (!nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
   14554 				      &relax_blank_list, pre_irel_ptr,
   14555 				      &irel_list))
   14556 	  return FALSE;
   14557 
   14558       if (data_flag & CLEAN_PRE)
   14559 	{
   14560 	  pre_off = 0;
   14561 	  pre_insn16 = 0;
   14562 	  pre_irel_ptr = NULL;
   14563 	}
   14564       if (data_flag & DATA_EXIST)
   14565 	{
   14566 	  /* We save the move offset in the highest byte.  */
   14567 	  off += (data_flag >> 24);
   14568 	  continue;
   14569 	}
   14570 
   14571       if (*(contents + off) & 0x80)
   14572 	{
   14573 	  /* 2-byte instruction.  */
   14574 	  off += 2;
   14575 	  continue;
   14576 	}
   14577 
   14578       /* Load the instruction and its opcode with register for comparing.  */
   14579       ex9_insn = ex9_insn_head;
   14580       insn = bfd_getb32 (contents + off);
   14581       insn_with_reg = 0;
   14582       while (ex9_insn)
   14583 	{
   14584 	  it_insn = strtol (ex9_insn->string, NULL, 16);
   14585 	  it_insn_with_reg = 0;
   14586 	  do_replace = 0;
   14587 	  save_irel = 0;
   14588 
   14589 	  if (irel != NULL && irel < irelend && irel->r_offset == off)
   14590 	    {
   14591 	      /* Insn with relocation.  */
   14592 	      nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
   14593 
   14594 	      if (ex9_insn->irel != NULL)
   14595 		nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn,
   14596 					     &it_insn_with_reg);
   14597 
   14598 	      if (ex9_insn->irel != NULL
   14599 		  && (ELF32_R_TYPE (irel->r_info) ==
   14600 		      ELF32_R_TYPE (ex9_insn->irel->r_info))
   14601 		  && (insn_with_reg == it_insn_with_reg))
   14602 		{
   14603 		  /* Insn relocation and format is the same as table entry.  */
   14604 
   14605 		  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
   14606 		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
   14607 		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
   14608 		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
   14609 		      || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
   14610 		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
   14611 			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
   14612 		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
   14613 			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
   14614 		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
   14615 			  && ELF32_R_TYPE (irel->r_info) <=
   14616 			  R_NDS32_SDA12S2_SP_RELA)
   14617 		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
   14618 			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
   14619 		    {
   14620 		      r_symndx = ELF32_R_SYM (irel->r_info);
   14621 		      if (r_symndx < symtab_hdr->sh_info)
   14622 			{
   14623 			  /* Local symbol.  */
   14624 			  int shndx = isym[r_symndx].st_shndx;
   14625 
   14626 			  isec = elf_elfsections (abfd)[shndx]->bfd_section;
   14627 			  if (ex9_insn->sec == isec
   14628 			      && ex9_insn->irel->r_addend == irel->r_addend
   14629 			      && ex9_insn->irel->r_info == irel->r_info)
   14630 			    {
   14631 			      do_replace = 1;
   14632 			      save_irel = 1;
   14633 			    }
   14634 			}
   14635 		      else
   14636 			{
   14637 			  /* External symbol.  */
   14638 			  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   14639 			  if (ex9_insn->m_list)
   14640 			    {
   14641 			      h_list = ex9_insn->m_list->h_list;
   14642 			      while (h_list)
   14643 				{
   14644 				  if (h == h_list->h
   14645 				      && (ex9_insn->m_list->irel->r_addend ==
   14646 					  irel->r_addend))
   14647 				    {
   14648 				      do_replace = 1;
   14649 				      save_irel = 1;
   14650 				      break;
   14651 				    }
   14652 				  h_list = h_list->next;
   14653 				}
   14654 			    }
   14655 			}
   14656 		    }
   14657 		  else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
   14658 		    {
   14659 		      r_symndx = ELF32_R_SYM (irel->r_info);
   14660 		      if (r_symndx < symtab_hdr->sh_info)
   14661 			{
   14662 			  /* Local symbols.  Compare its base symbol and offset.  */
   14663 			  int shndx = isym[r_symndx].st_shndx;
   14664 
   14665 			  isec = elf_elfsections (abfd)[shndx]->bfd_section;
   14666 			  if (ex9_insn->sec == isec
   14667 			      && ex9_insn->irel->r_addend == irel->r_addend
   14668 			      && ex9_insn->irel->r_info == irel->r_info)
   14669 			    {
   14670 			      do_replace = 1;
   14671 			      save_irel = 1;
   14672 			    }
   14673 			}
   14674 		      else
   14675 			{
   14676 			  /* External symbol.  */
   14677 			  struct elf_link_hash_entry_mul_list *m_list;
   14678 
   14679 			  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   14680 			  m_list = ex9_insn->m_list;
   14681 
   14682 			  while (m_list)
   14683 			    {
   14684 			      h_list = m_list->h_list;
   14685 
   14686 			      while (h_list)
   14687 				{
   14688 				  if (h == h_list->h
   14689 				      && (m_list->irel->r_addend
   14690 					  == irel->r_addend))
   14691 				    {
   14692 				      do_replace = 1;
   14693 				      save_irel = 1;
   14694 				      if (ex9_insn->next
   14695 					  && ex9_insn->m_list
   14696 					  && ex9_insn->m_list == ex9_insn->next->m_list)
   14697 					{
   14698 					  /* sethi multiple entry must be fixed */
   14699 					  nds32_elf_ex9_insert_fix (sec, irel,
   14700 								    h, ex9_insn->order);
   14701 					}
   14702 				      break;
   14703 				    }
   14704 				  h_list = h_list->next;
   14705 				}
   14706 			      m_list = m_list->next;
   14707 			    }
   14708 			}
   14709 		    }
   14710 		}
   14711 
   14712 	      /* Import table: Check the symbol hash table and the
   14713 		 jump target.  Only R_NDS32_25_PCREL_RELA now.  */
   14714 	      else if (ex9_insn->times == -1
   14715 		       && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
   14716 		{
   14717 		  nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
   14718 		  if (insn_with_reg == it_insn_with_reg)
   14719 		    {
   14720 		      char code[10];
   14721 		      bfd_vma relocation;
   14722 
   14723 		      r_symndx = ELF32_R_SYM (irel->r_info);
   14724 		      if (r_symndx >= symtab_hdr->sh_info)
   14725 			{
   14726 			  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
   14727 			  if ((h->root.type == bfd_link_hash_defined
   14728 			       || h->root.type == bfd_link_hash_defweak)
   14729 			      && h->root.u.def.section != NULL
   14730 			      && h->root.u.def.section->output_section != NULL
   14731 			      && h->root.u.def.section->gc_mark == 1
   14732 			      && bfd_is_abs_section (h->root.u.def.section)
   14733 			      && h->root.u.def.value > sec->size)
   14734 			    {
   14735 			      relocation = h->root.u.def.value +
   14736 				h->root.u.def.section->output_section->vma +
   14737 				h->root.u.def.section->output_offset;
   14738 			      relocation += irel->r_addend;
   14739 			      insn = insn_with_reg
   14740 				| ((relocation >> 1) & 0xffffff);
   14741 			      snprintf (code, sizeof (code), "%08x", insn);
   14742 			      if (strcmp (code, ex9_insn->string) == 0)
   14743 				{
   14744 				  do_replace = 1;
   14745 				  save_irel = 1;
   14746 				}
   14747 			    }
   14748 			}
   14749 		    }
   14750 		}
   14751 	      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
   14752 		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
   14753 		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
   14754 		{
   14755 		  /* These relocations do not have to relocate contens, so it can
   14756 		     be regard as instruction without relocation.  */
   14757 		  if (insn == it_insn && ex9_insn->irel == NULL)
   14758 		    do_replace = 1;
   14759 		}
   14760 	    }
   14761 	  else
   14762 	    {
   14763 	      /* Instruction without relocation, we only
   14764 		 have to compare their byte code.  */
   14765 	      if (insn == it_insn && ex9_insn->irel == NULL)
   14766 		do_replace = 1;
   14767 	    }
   14768 
   14769 	  /* Insntruction match so replacing the code here.  */
   14770 	  if (do_replace == 1)
   14771 	    {
   14772 	      /* There are two formats of ex9 instruction.  */
   14773 	      if (ex9_insn->order < 32)
   14774 		insn_ex9 = INSN_EX9_IT_2;
   14775 	      else
   14776 		insn_ex9 = INSN_EX9_IT_1;
   14777 	      insn16 = insn_ex9 | ex9_insn->order;
   14778 
   14779 	      /* Insert ex9 instruction.  */
   14780 	      nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
   14781 				       &relax_blank_list, pre_irel_ptr,
   14782 				       &irel_list);
   14783 	      pre_off = off;
   14784 	      pre_insn16 = insn16;
   14785 
   14786 	      if (save_irel)
   14787 		{
   14788 		  /* For instuction with relocation do relax.  */
   14789 		  irel_ptr = (struct elf_nds32_irel_entry *)
   14790 		    bfd_malloc (sizeof (struct elf_nds32_irel_entry));
   14791 		  irel_ptr->irel = irel;
   14792 		  irel_ptr->next = NULL;
   14793 		  pre_irel_ptr = irel_ptr;
   14794 		}
   14795 	      else
   14796 		pre_irel_ptr = NULL;
   14797 	      break;
   14798 	    }
   14799 	  ex9_insn = ex9_insn->next;
   14800 	}
   14801       off += 4;
   14802     }
   14803 
   14804   /* Insert ex9 instruction.  */
   14805   nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
   14806 			   &relax_blank_list, pre_irel_ptr,
   14807 			   &irel_list);
   14808 
   14809   /* Delete the redundant code.  */
   14810   if (relax_blank_list)
   14811     {
   14812       nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
   14813       relax_blank_list = NULL;
   14814     }
   14815 
   14816   /* Clear the relocation that is replaced by ex9.  */
   14817   while (irel_list)
   14818     {
   14819       struct elf_nds32_irel_entry *irel_ptr;
   14820 
   14821       irel_ptr = irel_list;
   14822       irel_list = irel_ptr->next;
   14823       irel_ptr->irel->r_info =
   14824 	ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
   14825       free (irel_ptr);
   14826     }
   14827   return TRUE;
   14828 }
   14829 
   14830 /* Initialize ex9 hash table.  */
   14831 
   14832 int
   14833 nds32_elf_ex9_init (void)
   14834 {
   14835   if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
   14836 			      sizeof (struct elf_nds32_code_hash_entry),
   14837 			      1023))
   14838     {
   14839       (*_bfd_error_handler) (_("Linker: cannot init ex9 hash table error \n"));
   14840       return FALSE;
   14841     }
   14842   return TRUE;
   14843 }
   14844 
   14845 /* Predict how many bytes will be relaxed with ex9 and ifc.  */
   14846 
   14847 static void
   14848 nds32_elf_ex9_total_relax (struct bfd_link_info *info)
   14849 {
   14850   struct elf_nds32_insn_times_entry *ex9_insn;
   14851   struct elf_nds32_insn_times_entry *temp;
   14852   int target_optimize;
   14853   struct elf_nds32_link_hash_table *table;
   14854 
   14855   if (ex9_insn_head == NULL)
   14856     return;
   14857 
   14858   table = nds32_elf_hash_table (info);
   14859   target_optimize  = table->target_optimize;
   14860   ex9_insn = ex9_insn_head;
   14861   while (ex9_insn)
   14862     {
   14863       ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
   14864       temp = ex9_insn;
   14865       ex9_insn = ex9_insn->next;
   14866       free (temp);
   14867     }
   14868   ex9_insn_head = NULL;
   14869 
   14870   if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
   14871     {
   14872       /* Examine ifc reduce size.  */
   14873       struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
   14874       struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
   14875       int size = 0;
   14876 
   14877       while (ifc_ent)
   14878 	{
   14879 	  if (ifc_ent->enable == 0)
   14880 	    {
   14881 	      /* Not ifc yet.  */
   14882 	      irel_ptr = ifc_ent->irel_head;
   14883 	      while (irel_ptr)
   14884 		{
   14885 		  size += 2;
   14886 		  irel_ptr = irel_ptr->next;
   14887 		}
   14888 	    }
   14889 	  size -= 2;
   14890 	  ifc_ent = ifc_ent->next;
   14891 	}
   14892       ex9_relax_size += size;
   14893     }
   14894 }
   14895 
   14896 /* Finish ex9 table.  */
   14897 
   14898 void
   14899 nds32_elf_ex9_finish (struct bfd_link_info *link_info)
   14900 {
   14901   nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
   14902   nds32_elf_order_insn_times (link_info);
   14903   nds32_elf_ex9_total_relax (link_info);
   14904   /* Traverse the hash table and count its times.  */
   14905   nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
   14906   nds32_elf_order_insn_times (link_info);
   14907   nds32_elf_ex9_build_itable (link_info);
   14908 }
   14909 
   14910 /* Relocate the entries in ex9 table.  */
   14911 
   14912 static bfd_vma
   14913 nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
   14914 			  struct bfd_link_info *link_info)
   14915 {
   14916   Elf_Internal_Sym *isym = NULL;
   14917   bfd_vma relocation = -1;
   14918   struct elf_link_hash_entry *h;
   14919 
   14920   if (ptr->m_list != NULL)
   14921     {
   14922       /* Global symbol.  */
   14923       h = ptr->m_list->h_list->h;
   14924       if ((h->root.type == bfd_link_hash_defined
   14925 	   || h->root.type == bfd_link_hash_defweak)
   14926 	  && h->root.u.def.section != NULL
   14927 	  && h->root.u.def.section->output_section != NULL)
   14928 	{
   14929 
   14930 	  relocation = h->root.u.def.value +
   14931 	    h->root.u.def.section->output_section->vma +
   14932 	    h->root.u.def.section->output_offset;
   14933 	  relocation += ptr->m_list->irel->r_addend;
   14934 	}
   14935       else
   14936 	relocation = 0;
   14937     }
   14938   else if (ptr->sec !=NULL)
   14939     {
   14940       /* Local symbol.  */
   14941       Elf_Internal_Sym sym;
   14942       asection *sec = NULL;
   14943       asection isec;
   14944       asection *isec_ptr = &isec;
   14945       Elf_Internal_Rela irel_backup = *(ptr->irel);
   14946       asection *sec_backup = ptr->sec;
   14947       bfd *abfd = ptr->sec->owner;
   14948 
   14949       if (!nds32_get_local_syms (abfd, sec, &isym))
   14950 	return FALSE;
   14951       isym = isym + ELF32_R_SYM (ptr->irel->r_info);
   14952 
   14953       sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
   14954       if (sec != NULL)
   14955 	*isec_ptr = *sec;
   14956       sym = *isym;
   14957 
   14958       /* The purpose is same as elf_link_input_bfd.  */
   14959       if (isec_ptr != NULL
   14960 	  && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
   14961 	  && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
   14962 	{
   14963 	  sym.st_value =
   14964 	    _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
   14965 					elf_section_data (isec_ptr)->sec_info,
   14966 					isym->st_value);
   14967 	}
   14968       relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
   14969 					    &ptr->sec, ptr->irel);
   14970       if (ptr->irel != NULL)
   14971 	relocation += ptr->irel->r_addend;
   14972 
   14973       /* Restore origin value since there may be some insntructions that
   14974 	 could not be replaced with ex9.it.  */
   14975       *(ptr->irel) = irel_backup;
   14976       ptr->sec = sec_backup;
   14977     }
   14978 
   14979   return relocation;
   14980 }
   14981 
   14982 /* Import ex9 table and build list.  */
   14983 
   14984 void
   14985 nds32_elf_ex9_import_table (struct bfd_link_info *info)
   14986 {
   14987   int num = 0;
   14988   bfd_byte *contents;
   14989   unsigned long insn;
   14990   FILE *ex9_import_file;
   14991   int update_ex9_table;
   14992   struct elf_nds32_link_hash_table *table;
   14993 
   14994   table = nds32_elf_hash_table (info);
   14995   ex9_import_file = table->ex9_import_file;
   14996   rewind (table->ex9_import_file);
   14997 
   14998   contents = bfd_malloc (sizeof (bfd_byte) * 4);
   14999 
   15000   /* Read instructions from the input file and build the list.  */
   15001   while (!feof (ex9_import_file))
   15002     {
   15003       char *code;
   15004       struct elf_nds32_insn_times_entry *ptr;
   15005       size_t nread;
   15006 
   15007       nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
   15008       /* Ignore the final byte 0x0a.  */
   15009       if (nread < 1)
   15010 	break;
   15011       insn = bfd_getb32 (contents);
   15012       code = bfd_malloc (sizeof (char) * 9);
   15013       snprintf (code, 9, "%08lx", insn);
   15014       ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
   15015       ptr->string = code;
   15016       ptr->order = num;
   15017       ptr->times = -1;
   15018       ptr->sec = NULL;
   15019       ptr->m_list = NULL;
   15020       ptr->rel_backup.r_offset = 0;
   15021       ptr->rel_backup.r_info = 0;
   15022       ptr->rel_backup.r_addend = 0;
   15023       ptr->irel = NULL;
   15024       ptr->next = NULL;
   15025       nds32_elf_ex9_insert_entry (ptr);
   15026       num++;
   15027     }
   15028 
   15029   update_ex9_table = table->update_ex9_table;
   15030   if (update_ex9_table == 1)
   15031     {
   15032       /* It has to consider of sethi need to use multiple page
   15033 	 but it not be done yet.  */
   15034       nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
   15035       nds32_elf_order_insn_times (info);
   15036     }
   15037 }
   15038 
   15039 /* Export ex9 table.  */
   15040 
   15041 static void
   15042 nds32_elf_ex9_export (struct bfd_link_info *info,
   15043 		      bfd_byte *contents, int size)
   15044 {
   15045   FILE *ex9_export_file;
   15046   struct elf_nds32_link_hash_table *table;
   15047 
   15048   table = nds32_elf_hash_table (info);
   15049   ex9_export_file = table->ex9_export_file;
   15050   fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
   15051   fclose (ex9_export_file);
   15052 }
   15053 
   15054 /* Adjust relocations of J and JAL in ex9.itable.
   15055    Export ex9 table.  */
   15056 
   15057 static void
   15058 nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
   15059 {
   15060   asection *table_sec = NULL;
   15061   struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
   15062   struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
   15063   bfd *it_abfd;
   15064   uint32_t insn, insn_with_reg, source_insn;
   15065   bfd_byte *contents = NULL, *source_contents = NULL;
   15066   int size = 0;
   15067   bfd_vma gp;
   15068   int shift, update_ex9_table, offset = 0;
   15069   reloc_howto_type *howto = NULL;
   15070   Elf_Internal_Rela rel_backup;
   15071   unsigned short insn_ex9;
   15072   struct elf_nds32_link_hash_table *table;
   15073   FILE *ex9_export_file;
   15074   static bfd_boolean done = FALSE;
   15075 
   15076   if (done)
   15077     return;
   15078 
   15079   done = TRUE;
   15080 
   15081   table = nds32_elf_hash_table (link_info);
   15082   if (table)
   15083     table->relax_status |= NDS32_RELAX_EX9_DONE;
   15084 
   15085 
   15086   update_ex9_table = table->update_ex9_table;
   15087   /* Generated ex9.itable exactly.  */
   15088   if (update_ex9_table == 0)
   15089     {
   15090       for (it_abfd = link_info->input_bfds; it_abfd != NULL;
   15091 	   it_abfd = it_abfd->link.next)
   15092 	{
   15093 	  table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
   15094 	  if (table_sec != NULL)
   15095 	    break;
   15096 	}
   15097 
   15098       if (table_sec != NULL)
   15099 	{
   15100 	  bfd *output_bfd;
   15101 
   15102 	  output_bfd = table_sec->output_section->owner;
   15103 	  nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
   15104 	  if (table_sec->size == 0)
   15105 	    return;
   15106 
   15107 	  if (!nds32_get_section_contents (it_abfd, table_sec, &contents))
   15108 	    return;
   15109 	}
   15110     }
   15111   else
   15112     {
   15113       /* Set gp.  */
   15114       bfd *output_bfd;
   15115 
   15116       output_bfd = link_info->input_bfds->sections->output_section->owner;
   15117       nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
   15118       contents = bfd_malloc (sizeof (bfd_byte) * 2048);
   15119     }
   15120 
   15121   /* Relocate instruction.  */
   15122   while (ex9_insn)
   15123     {
   15124       bfd_vma relocation, min_relocation = 0xffffffff;
   15125 
   15126       insn = strtol (ex9_insn->string, NULL, 16);
   15127       insn_with_reg = 0;
   15128       if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
   15129 	{
   15130 	  if (ex9_insn->m_list)
   15131 	    rel_backup = ex9_insn->m_list->rel_backup;
   15132 	  else
   15133 	    rel_backup = ex9_insn->rel_backup;
   15134 
   15135 	  nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
   15136 	  howto =
   15137 	    bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
   15138 						   (rel_backup.r_info));
   15139 	  shift = howto->rightshift;
   15140 	  if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
   15141 	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
   15142 	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
   15143 	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
   15144 	      || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
   15145 	    {
   15146 	      relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
   15147 	      insn =
   15148 		insn_with_reg | ((relocation >> shift) &
   15149 				 nds32_elf_irel_mask (&rel_backup));
   15150 	      bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
   15151 	    }
   15152 	  else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
   15153 		    && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
   15154 		   || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
   15155 		       && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
   15156 		   || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
   15157 		       && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
   15158 		   || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
   15159 		       && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
   15160 	    {
   15161 	      relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
   15162 	      insn =
   15163 		insn_with_reg | (((relocation - gp) >> shift) &
   15164 				 nds32_elf_irel_mask (&rel_backup));
   15165 	      bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
   15166 	    }
   15167 	  else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
   15168 	    {
   15169 	      /* Sethi may be multiple entry for one insn.  */
   15170 	      if (ex9_insn->next && ex9_insn->m_list
   15171 		  && ex9_insn->m_list == ex9_insn->next->m_list)
   15172 		{
   15173 		  struct elf_link_hash_entry_mul_list *m_list;
   15174 		  struct elf_nds32_ex9_refix *fix_ptr;
   15175 		  struct elf_link_hash_entry *h;
   15176 
   15177 		  temp_ptr = ex9_insn;
   15178 		  temp_ptr2 = ex9_insn;
   15179 		  m_list = ex9_insn->m_list;
   15180 		  while (m_list)
   15181 		    {
   15182 		      h = m_list->h_list->h;
   15183 		      relocation = h->root.u.def.value +
   15184 			h->root.u.def.section->output_section->vma +
   15185 			h->root.u.def.section->output_offset;
   15186 		      relocation += m_list->irel->r_addend;
   15187 
   15188 		      if (relocation < min_relocation)
   15189 			min_relocation = relocation;
   15190 		      m_list = m_list->next;
   15191 		    }
   15192 		  relocation = min_relocation;
   15193 
   15194 		  /* Put insntruction into ex9 table.  */
   15195 		  insn = insn_with_reg
   15196 		    | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
   15197 		  bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
   15198 		  relocation = relocation + 0x1000;	/* hi20 */
   15199 
   15200 		  while (ex9_insn->next && ex9_insn->m_list
   15201 			 && ex9_insn->m_list == ex9_insn->next->m_list)
   15202 		    {
   15203 		      /* Multiple sethi.  */
   15204 		      ex9_insn = ex9_insn->next;
   15205 		      size += 4;
   15206 		      insn =
   15207 			insn_with_reg | ((relocation >> shift) &
   15208 					 nds32_elf_irel_mask (&rel_backup));
   15209 		      bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
   15210 		      relocation = relocation + 0x1000;	/* hi20 */
   15211 		    }
   15212 
   15213 		  fix_ptr = ex9_refix_head;
   15214 		  while (fix_ptr)
   15215 		    {
   15216 		      /* Fix ex9 insn.  */
   15217 		      /* temp_ptr2 points to the head of multiple sethi.  */
   15218 		      temp_ptr = temp_ptr2;
   15219 		      while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
   15220 			{
   15221 			  fix_ptr = fix_ptr->next;
   15222 			}
   15223 		      if (fix_ptr->order != temp_ptr->order)
   15224 			break;
   15225 
   15226 		      /* Set source insn.  */
   15227 		      relocation =
   15228 			fix_ptr->h->root.u.def.value +
   15229 			fix_ptr->h->root.u.def.section->output_section->vma +
   15230 			fix_ptr->h->root.u.def.section->output_offset;
   15231 		      relocation += fix_ptr->irel->r_addend;
   15232 		      /* sethi imm is imm20s.  */
   15233 		      source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);
   15234 
   15235 		      while (temp_ptr)
   15236 			{
   15237 			  /* Match entry and source code.  */
   15238 			  insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
   15239 			  if (insn == source_insn)
   15240 			    {
   15241 			      /* Fix the ex9 insn.  */
   15242 			      if (temp_ptr->order != fix_ptr->order)
   15243 				{
   15244 				  if (!nds32_get_section_contents
   15245 					 (fix_ptr->sec->owner, fix_ptr->sec,
   15246 					  &source_contents))
   15247 				    (*_bfd_error_handler)
   15248 				      (_("Linker: error cannot fixed ex9 relocation \n"));
   15249 				  if (temp_ptr->order < 32)
   15250 				    insn_ex9 = INSN_EX9_IT_2;
   15251 				  else
   15252 				    insn_ex9 = INSN_EX9_IT_1;
   15253 				  insn_ex9 = insn_ex9 | temp_ptr->order;
   15254 				  bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
   15255 				}
   15256 				break;
   15257 			    }
   15258 			  else
   15259 			    {
   15260 			      if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
   15261 				(*_bfd_error_handler)
   15262 				  (_("Linker: error cannot fixed ex9 relocation \n"));
   15263 			      else
   15264 				temp_ptr = temp_ptr->next;
   15265 			    }
   15266 			}
   15267 		      fix_ptr = fix_ptr->next;
   15268 		    }
   15269 		}
   15270 	      else
   15271 		{
   15272 		  relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
   15273 		  insn = insn_with_reg
   15274 			 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
   15275 		  bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
   15276 		}
   15277 	    }
   15278 	}
   15279       else
   15280 	{
   15281 	  /* Insn without relocation does not have to be fixed
   15282 	     if need to update export table.  */
   15283 	  if (update_ex9_table == 1)
   15284 	    bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
   15285 	}
   15286       ex9_insn = ex9_insn->next;
   15287       size += 4;
   15288     }
   15289 
   15290   ex9_export_file = table->ex9_export_file;
   15291   if (ex9_export_file != NULL)
   15292     nds32_elf_ex9_export (link_info, contents, table_sec->size);
   15293   else if (update_ex9_table == 1)
   15294     {
   15295       table->ex9_export_file = table->ex9_import_file;
   15296       rewind (table->ex9_export_file);
   15297       nds32_elf_ex9_export (link_info, contents, size);
   15298     }
   15299 }
   15300 
   15301 /* Generate ex9 hash table.  */
   15302 
   15303 static bfd_boolean
   15304 nds32_elf_ex9_build_hash_table (bfd *abfd, asection *sec,
   15305 				struct bfd_link_info *link_info)
   15306 {
   15307   Elf_Internal_Rela *internal_relocs;
   15308   Elf_Internal_Rela *irelend;
   15309   Elf_Internal_Rela *irel;
   15310   Elf_Internal_Rela *jrel;
   15311   Elf_Internal_Rela rel_backup;
   15312   Elf_Internal_Shdr *symtab_hdr;
   15313   Elf_Internal_Sym *isym = NULL;
   15314   asection *isec;
   15315   struct elf_link_hash_entry **sym_hashes;
   15316   bfd_byte *contents = NULL;
   15317   bfd_vma off = 0;
   15318   unsigned long r_symndx;
   15319   uint32_t insn, insn_with_reg;
   15320   struct elf_link_hash_entry *h;
   15321   int data_flag, shift, align;
   15322   bfd_vma relocation;
   15323   /* Suppress ex9 if `.no_relax ex9' or inner loop.  */
   15324   reloc_howto_type *howto = NULL;
   15325 
   15326   sym_hashes = elf_sym_hashes (abfd);
   15327   /* Load section instructions, relocations, and symbol table.  */
   15328   if (!nds32_get_section_contents (abfd, sec, &contents))
   15329     return FALSE;
   15330 
   15331   internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
   15332 					       TRUE /* keep_memory */);
   15333   irelend = internal_relocs + sec->reloc_count;
   15334   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   15335   if (!nds32_get_local_syms (abfd, sec, &isym))
   15336     return FALSE;
   15337 
   15338   /* Check the object if enable ex9.  */
   15339   irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
   15340 				 R_NDS32_RELAX_ENTRY);
   15341 
   15342   /* Check this section trigger ex9 relaxation.  */
   15343   if (irel == NULL
   15344       || irel >= irelend
   15345       || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
   15346       || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
   15347 	  && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
   15348     return TRUE;
   15349 
   15350   irel = internal_relocs;
   15351 
   15352   /* Push each insn into hash table.  */
   15353   while (off < sec->size)
   15354     {
   15355       char code[10];
   15356       struct elf_nds32_code_hash_entry *entry;
   15357 
   15358       while (irel != NULL && irel < irelend && irel->r_offset < off)
   15359 	irel++;
   15360 
   15361       data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend,
   15362 						  NULL, sec, &off, contents);
   15363       if (data_flag & DATA_EXIST)
   15364 	{
   15365 	  /* We save the move offset in the highest byte.  */
   15366 	  off += (data_flag >> 24);
   15367 	  continue;
   15368 	}
   15369 
   15370       if (*(contents + off) & 0x80)
   15371 	{
   15372 	  off += 2;
   15373 	}
   15374       else
   15375 	{
   15376 	  h = NULL;
   15377 	  isec = NULL;
   15378 	  jrel = NULL;
   15379 	  rel_backup.r_info = 0;
   15380 	  rel_backup.r_offset = 0;
   15381 	  rel_backup.r_addend = 0;
   15382 	  /* Load the instruction and its opcode with register for comparing.  */
   15383 	  insn = bfd_getb32 (contents + off);
   15384 	  insn_with_reg = 0;
   15385 	  if (irel != NULL && irel < irelend && irel->r_offset == off)
   15386 	    {
   15387 	      nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
   15388 	      howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
   15389 	      shift = howto->rightshift;
   15390 	      align = (1 << shift) - 1;
   15391 	      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
   15392 		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
   15393 		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
   15394 		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
   15395 		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
   15396 		  || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
   15397 		  ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
   15398 		     && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
   15399 		  || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
   15400 		      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
   15401 		  || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
   15402 		      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
   15403 		  || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
   15404 		      && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
   15405 		{
   15406 		  r_symndx = ELF32_R_SYM (irel->r_info);
   15407 		  jrel = irel;
   15408 		  rel_backup = *irel;
   15409 		  if (r_symndx < symtab_hdr->sh_info)
   15410 		    {
   15411 		      /* Local symbol.  */
   15412 		      int shndx = isym[r_symndx].st_shndx;
   15413 
   15414 		      bfd_vma st_value = (isym + r_symndx)->st_value;
   15415 		      isec = elf_elfsections (abfd)[shndx]->bfd_section;
   15416 		      relocation = (isec->output_section->vma + isec->output_offset
   15417 				    + st_value + irel->r_addend);
   15418 		    }
   15419 		  else
   15420 		    {
   15421 		      /* External symbol.  */
   15422 		      bfd_boolean warned ATTRIBUTE_UNUSED;
   15423 		      bfd_boolean ignored ATTRIBUTE_UNUSED;
   15424 		      bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
   15425 		      asection *sym_sec;
   15426 
   15427 		      /* Maybe there is a better way to get h and relocation */
   15428 		      RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
   15429 					       r_symndx, symtab_hdr, sym_hashes,
   15430 					       h, sym_sec, relocation,
   15431 					       unresolved_reloc, warned, ignored);
   15432 		      relocation += irel->r_addend;
   15433 		      if ((h->root.type != bfd_link_hash_defined
   15434 			   && h->root.type != bfd_link_hash_defweak)
   15435 			  || strcmp (h->root.root.string, "_FP_BASE_") == 0)
   15436 			{
   15437 			  off += 4;
   15438 			  continue;
   15439 			}
   15440 		    }
   15441 
   15442 		  /* Check for gp relative instruction alignment.  */
   15443 		  if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
   15444 		       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
   15445 		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
   15446 			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
   15447 		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
   15448 			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
   15449 		      || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
   15450 			  && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
   15451 		    {
   15452 		      bfd_vma gp;
   15453 		      bfd *output_bfd = sec->output_section->owner;
   15454 		      bfd_reloc_status_type r;
   15455 
   15456 		      /* If the symbol is in the abs section, the out_bfd will be null.
   15457 			 This happens when the relocation has a symbol@GOTOFF.  */
   15458 		      r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
   15459 		      if (r != bfd_reloc_ok)
   15460 			{
   15461 			  off += 4;
   15462 			  continue;
   15463 			}
   15464 
   15465 		      relocation -= gp;
   15466 
   15467 		      /* Make sure alignment is correct.  */
   15468 		      if (relocation & align)
   15469 			{
   15470 			  /* Incorrect alignment.  */
   15471 			  (*_bfd_error_handler)
   15472 			    (_("%s: warning: unaligned small data access. "
   15473 			       "For entry: {%d, %d, %d}, addr = 0x%x, align = 0x%x."),
   15474 			     bfd_get_filename (abfd), irel->r_offset,
   15475 			     irel->r_info, irel->r_addend, relocation, align);
   15476 			  off += 4;
   15477 			  continue;
   15478 			}
   15479 		    }
   15480 
   15481 		  insn = insn_with_reg
   15482 		    | ((relocation >> shift) & nds32_elf_irel_mask (irel));
   15483 		}
   15484 	      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
   15485 		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
   15486 		       || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
   15487 		{
   15488 		  /* These relocations do not have to relocate contens, so it can
   15489 		     be regard as instruction without relocation.  */
   15490 		}
   15491 	      else
   15492 		{
   15493 		  off += 4;
   15494 		  continue;
   15495 		}
   15496 	    }
   15497 
   15498 	  snprintf (code, sizeof (code), "%08x", insn);
   15499 	  /* Copy "code".  */
   15500 	  entry = (struct elf_nds32_code_hash_entry*)
   15501 	    bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
   15502 	  if (entry == NULL)
   15503 	    {
   15504 	      (*_bfd_error_handler)
   15505 		(_("%P%F: failed creating ex9.it %s hash table: %E\n"), code);
   15506 	      return FALSE;
   15507 	    }
   15508 	  if (h)
   15509 	    {
   15510 	      if (h->root.type == bfd_link_hash_undefined)
   15511 		return TRUE;
   15512 	      /* Global symbol.  */
   15513 	      /* In order to do sethi with different symbol but same value.  */
   15514 	      if (entry->m_list == NULL)
   15515 		{
   15516 		  struct elf_link_hash_entry_mul_list *m_list_new;
   15517 		  struct elf_link_hash_entry_list *h_list_new;
   15518 
   15519 		  m_list_new = (struct elf_link_hash_entry_mul_list *)
   15520 		    bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
   15521 		  h_list_new = (struct elf_link_hash_entry_list *)
   15522 		    bfd_malloc (sizeof (struct elf_link_hash_entry_list));
   15523 		  entry->m_list = m_list_new;
   15524 		  m_list_new->h_list = h_list_new;
   15525 		  m_list_new->rel_backup = rel_backup;
   15526 		  m_list_new->times = 1;
   15527 		  m_list_new->irel = jrel;
   15528 		  m_list_new->next = NULL;
   15529 		  h_list_new->h = h;
   15530 		  h_list_new->next = NULL;
   15531 		}
   15532 	      else
   15533 		{
   15534 		  struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
   15535 		  struct elf_link_hash_entry_list *h_list;
   15536 
   15537 		  while (m_list)
   15538 		    {
   15539 		      /* Build the different symbols that point to the same address.  */
   15540 		      h_list = m_list->h_list;
   15541 		      if (h_list->h->root.u.def.value == h->root.u.def.value
   15542 			  && h_list->h->root.u.def.section->output_section->vma
   15543 			     == h->root.u.def.section->output_section->vma
   15544 			  && h_list->h->root.u.def.section->output_offset
   15545 			     == h->root.u.def.section->output_offset
   15546 			  && m_list->rel_backup.r_addend == rel_backup.r_addend)
   15547 			{
   15548 			  m_list->times++;
   15549 			  m_list->irel = jrel;
   15550 			  while (h_list->h != h && h_list->next)
   15551 			    h_list = h_list->next;
   15552 			  if (h_list->h != h)
   15553 			    {
   15554 			      struct elf_link_hash_entry_list *h_list_new;
   15555 
   15556 			      h_list_new = (struct elf_link_hash_entry_list *)
   15557 				bfd_malloc (sizeof (struct elf_link_hash_entry_list));
   15558 			      h_list->next = h_list_new;
   15559 			      h_list_new->h = h;
   15560 			      h_list_new->next = NULL;
   15561 			    }
   15562 			  break;
   15563 			}
   15564 		      /* The sethi case may have different address but the
   15565 			 hi20 is the same.  */
   15566 		      else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
   15567 			       && m_list->next == NULL)
   15568 			{
   15569 			  struct elf_link_hash_entry_mul_list *m_list_new;
   15570 			  struct elf_link_hash_entry_list *h_list_new;
   15571 
   15572 			  m_list_new = (struct elf_link_hash_entry_mul_list *)
   15573 			    bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
   15574 			  h_list_new = (struct elf_link_hash_entry_list *)
   15575 			    bfd_malloc (sizeof (struct elf_link_hash_entry_list));
   15576 			  m_list->next = m_list_new;
   15577 			  m_list_new->h_list = h_list_new;
   15578 			  m_list_new->rel_backup = rel_backup;
   15579 			  m_list_new->times = 1;
   15580 			  m_list_new->irel = jrel;
   15581 			  m_list_new->next = NULL;
   15582 			  h_list_new->h = h;
   15583 			  h_list_new->next = NULL;
   15584 			  break;
   15585 			}
   15586 		      m_list = m_list->next;
   15587 		    }
   15588 		  if (!m_list)
   15589 		    {
   15590 		      off += 4;
   15591 		      continue;
   15592 		    }
   15593 		}
   15594 	    }
   15595 	  else
   15596 	    {
   15597 	      /* Local symbol and insn without relocation*/
   15598 	      entry->times++;
   15599 	      entry->rel_backup = rel_backup;
   15600 	    }
   15601 
   15602 	  /* Use in sethi insn with constant and global symbol in same format.  */
   15603 	  if (!jrel)
   15604 	    entry->const_insn = 1;
   15605 	  else
   15606 	    entry->irel = jrel;
   15607 	  entry->sec = isec;
   15608 	  off += 4;
   15609 	}
   15610     }
   15611   return TRUE;
   15612 }
   15613 
   15614 /* Set the _ITB_BASE, and point it to ex9 table.  */
   15615 
   15616 bfd_boolean
   15617 nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
   15618 {
   15619   bfd *abfd;
   15620   asection *sec;
   15621   bfd *output_bfd = NULL;
   15622   struct bfd_link_hash_entry *bh = NULL;
   15623 
   15624   if (is_ITB_BASE_set == 1)
   15625     return TRUE;
   15626 
   15627   is_ITB_BASE_set = 1;
   15628 
   15629   bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_", FALSE, FALSE, TRUE);
   15630 
   15631   if (bh && (bh->type == bfd_link_hash_defined
   15632 	     || bh->type == bfd_link_hash_defweak))
   15633     return TRUE;
   15634 
   15635   for (abfd = link_info->input_bfds; abfd != NULL;
   15636        abfd = abfd->link.next)
   15637     {
   15638       sec = bfd_get_section_by_name (abfd, ".ex9.itable");
   15639       if (sec != NULL)
   15640 	{
   15641 	  output_bfd = sec->output_section->owner;
   15642 	  break;
   15643 	}
   15644     }
   15645   if (output_bfd == NULL)
   15646     {
   15647       output_bfd = link_info->output_bfd;
   15648       if (output_bfd->sections == NULL)
   15649 	return TRUE;
   15650       else
   15651 	sec = bfd_abs_section_ptr;
   15652     }
   15653   bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
   15654 			     FALSE, FALSE, TRUE);
   15655   return (_bfd_generic_link_add_one_symbol
   15656 	  (link_info, output_bfd, "_ITB_BASE_",
   15657 	   BSF_GLOBAL | BSF_WEAK, sec, 0,
   15658 	   (const char *) NULL, FALSE, get_elf_backend_data
   15659 	   (output_bfd)->collect, &bh));
   15660 } /* End EX9.IT  */
   15661 
   15662 
   15664 #define ELF_ARCH				bfd_arch_nds32
   15665 #define ELF_MACHINE_CODE			EM_NDS32
   15666 #define ELF_MAXPAGESIZE				0x1000
   15667 #define ELF_TARGET_ID                           NDS32_ELF_DATA
   15668 
   15669 #define TARGET_BIG_SYM				nds32_elf32_be_vec
   15670 #define TARGET_BIG_NAME				"elf32-nds32be"
   15671 #define TARGET_LITTLE_SYM			nds32_elf32_le_vec
   15672 #define TARGET_LITTLE_NAME			"elf32-nds32le"
   15673 
   15674 #define elf_info_to_howto			nds32_info_to_howto
   15675 #define elf_info_to_howto_rel			nds32_info_to_howto_rel
   15676 
   15677 #define bfd_elf32_bfd_link_hash_table_create	nds32_elf_link_hash_table_create
   15678 #define bfd_elf32_bfd_merge_private_bfd_data	nds32_elf_merge_private_bfd_data
   15679 #define bfd_elf32_bfd_print_private_bfd_data	nds32_elf_print_private_bfd_data
   15680 #define bfd_elf32_bfd_relax_section		nds32_elf_relax_section
   15681 #define bfd_elf32_bfd_set_private_flags		nds32_elf_set_private_flags
   15682 
   15683 #define bfd_elf32_mkobject		        nds32_elf_mkobject
   15684 #define elf_backend_action_discarded		nds32_elf_action_discarded
   15685 #define elf_backend_add_symbol_hook		nds32_elf_add_symbol_hook
   15686 #define elf_backend_check_relocs		nds32_elf_check_relocs
   15687 #define elf_backend_adjust_dynamic_symbol	nds32_elf_adjust_dynamic_symbol
   15688 #define elf_backend_create_dynamic_sections	nds32_elf_create_dynamic_sections
   15689 #define elf_backend_finish_dynamic_sections	nds32_elf_finish_dynamic_sections
   15690 #define elf_backend_finish_dynamic_symbol	nds32_elf_finish_dynamic_symbol
   15691 #define elf_backend_size_dynamic_sections	nds32_elf_size_dynamic_sections
   15692 #define elf_backend_relocate_section		nds32_elf_relocate_section
   15693 #define elf_backend_gc_mark_hook		nds32_elf_gc_mark_hook
   15694 #define elf_backend_gc_sweep_hook		nds32_elf_gc_sweep_hook
   15695 #define elf_backend_grok_prstatus		nds32_elf_grok_prstatus
   15696 #define elf_backend_grok_psinfo			nds32_elf_grok_psinfo
   15697 #define elf_backend_reloc_type_class		nds32_elf_reloc_type_class
   15698 #define elf_backend_copy_indirect_symbol	nds32_elf_copy_indirect_symbol
   15699 #define elf_backend_link_output_symbol_hook	nds32_elf_output_symbol_hook
   15700 #define elf_backend_output_arch_syms		nds32_elf_output_arch_syms
   15701 #define elf_backend_object_p			nds32_elf_object_p
   15702 #define elf_backend_final_write_processing	nds32_elf_final_write_processing
   15703 #define elf_backend_special_sections		nds32_elf_special_sections
   15704 #define bfd_elf32_bfd_get_relocated_section_contents \
   15705                                 nds32_elf_get_relocated_section_contents
   15706 
   15707 #define elf_backend_can_gc_sections		1
   15708 #define elf_backend_can_refcount		1
   15709 #define elf_backend_want_got_plt		1
   15710 #define elf_backend_plt_readonly		1
   15711 #define elf_backend_want_plt_sym		0
   15712 #define elf_backend_got_header_size		12
   15713 #define elf_backend_may_use_rel_p		1
   15714 #define elf_backend_default_use_rela_p		1
   15715 #define elf_backend_may_use_rela_p		1
   15716 
   15717 #include "elf32-target.h"
   15718 
   15719 #undef ELF_MAXPAGESIZE
   15720 #define ELF_MAXPAGESIZE				0x2000
   15721 
   15722 #undef TARGET_BIG_SYM
   15723 #define TARGET_BIG_SYM				nds32_elf32_linux_be_vec
   15724 #undef TARGET_BIG_NAME
   15725 #define TARGET_BIG_NAME				"elf32-nds32be-linux"
   15726 #undef TARGET_LITTLE_SYM
   15727 #define TARGET_LITTLE_SYM			nds32_elf32_linux_le_vec
   15728 #undef TARGET_LITTLE_NAME
   15729 #define TARGET_LITTLE_NAME			"elf32-nds32le-linux"
   15730 #undef elf32_bed
   15731 #define elf32_bed				elf32_nds32_lin_bed
   15732 
   15733 #include "elf32-target.h"
   15734