Fallout from recent bfd_reloc_outofrange changes
[deliverable/binutils-gdb.git] / bfd / elf32-nds32.c
1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2015 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 3, /* size (0 = byte, 1 = short, 2 = long) */
325 0, /* bitsize */
326 FALSE, /* pc_relative */
327 0, /* bitpos */
328 complain_overflow_dont, /* 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 \f
2293 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2294 This prototype is the same as qsort (). */
2295
2296 void
2297 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2298 int (*compar) (const void *lhs, const void *rhs))
2299 {
2300 char *ptr = (char *) base;
2301 int i, j;
2302 char *tmp = alloca (size);
2303
2304 /* If i is less than j, i is inserted before j.
2305
2306 |---- j ----- i --------------|
2307 \ / \ /
2308 sorted unsorted
2309 */
2310
2311 for (i = 1; i < (int) nmemb; i++)
2312 {
2313 for (j = (i - 1); j >= 0; j--)
2314 if (compar (ptr + i * size, ptr + j * size) >= 0)
2315 break;
2316
2317 j++;
2318
2319 if (i == j)
2320 continue; /* i is in order. */
2321
2322 memcpy (tmp, ptr + i * size, size);
2323 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2324 memcpy (ptr + j * size, tmp, size);
2325 }
2326 }
2327
2328 /* Sort relocation by r_offset.
2329
2330 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2331 algorithm. Relocations at the same r_offset must keep their order.
2332 For example, RELAX_ENTRY must be the very first relocation entry.
2333
2334 Currently, this function implements insertion-sort.
2335
2336 FIXME: If we already sort them in assembler, why bother sort them
2337 here again? */
2338
2339 static int
2340 compar_reloc (const void *lhs, const void *rhs)
2341 {
2342 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2343 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2344
2345 if (l->r_offset > r->r_offset)
2346 return 1;
2347 else if (l->r_offset == r->r_offset)
2348 return 0;
2349 else
2350 return -1;
2351 }
2352
2353 /* Functions listed below are only used for old relocs.
2354 * nds32_elf_9_pcrel_reloc
2355 * nds32_elf_do_9_pcrel_reloc
2356 * nds32_elf_hi20_reloc
2357 * nds32_elf_relocate_hi20
2358 * nds32_elf_lo12_reloc
2359 * nds32_elf_sda15_reloc
2360 * nds32_elf_generic_reloc
2361 */
2362
2363 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2364
2365 static bfd_reloc_status_type
2366 nds32_elf_9_pcrel_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2367 void *data, asection *input_section, bfd *output_bfd,
2368 char **error_message ATTRIBUTE_UNUSED)
2369 {
2370 /* This part is from bfd_elf_generic_reloc. */
2371 if (output_bfd != (bfd *) NULL
2372 && (symbol->flags & BSF_SECTION_SYM) == 0
2373 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2374 {
2375 reloc_entry->address += input_section->output_offset;
2376 return bfd_reloc_ok;
2377 }
2378
2379 if (output_bfd != NULL)
2380 {
2381 /* FIXME: See bfd_perform_relocation. Is this right? */
2382 return bfd_reloc_continue;
2383 }
2384
2385 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2386 input_section,
2387 data, reloc_entry->address,
2388 symbol->section,
2389 (symbol->value
2390 + symbol->section->output_section->vma
2391 + symbol->section->output_offset),
2392 reloc_entry->addend);
2393 }
2394
2395 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2396 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2397
2398 static bfd_reloc_status_type
2399 nds32_elf_do_9_pcrel_reloc (bfd *abfd, reloc_howto_type *howto,
2400 asection *input_section, bfd_byte *data,
2401 bfd_vma offset,
2402 asection *symbol_section ATTRIBUTE_UNUSED,
2403 bfd_vma symbol_value, bfd_vma addend)
2404 {
2405 bfd_signed_vma relocation;
2406 unsigned short x;
2407 bfd_reloc_status_type status;
2408
2409 /* Sanity check the address (offset in section). */
2410 if (offset > bfd_get_section_limit (abfd, input_section))
2411 return bfd_reloc_outofrange;
2412
2413 relocation = symbol_value + addend;
2414 /* Make it pc relative. */
2415 relocation -= (input_section->output_section->vma
2416 + input_section->output_offset);
2417 /* These jumps mask off the lower two bits of the current address
2418 before doing pcrel calculations. */
2419 relocation -= (offset & -(bfd_vma) 2);
2420
2421 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2422 status = bfd_reloc_overflow;
2423 else
2424 status = bfd_reloc_ok;
2425
2426 x = bfd_getb16 (data + offset);
2427
2428 relocation >>= howto->rightshift;
2429 relocation <<= howto->bitpos;
2430 x = (x & ~howto->dst_mask)
2431 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2432
2433 bfd_putb16 ((bfd_vma) x, data + offset);
2434
2435 return status;
2436 }
2437
2438 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2439 HI20_SLO is for the add3 and load/store with displacement instructions.
2440 HI20 is for the or3 instruction.
2441 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2442 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2443 we must add one to the high 16 bytes (which will get subtracted off when
2444 the low 16 bits are added).
2445 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2446 because there is a carry from the LO12 to the HI20. Here we just save
2447 the information we need; we do the actual relocation when we see the LO12.
2448 This code is copied from the elf32-mips.c. We also support an arbitrary
2449 number of HI20 relocs to be associated with a single LO12 reloc. The
2450 assembler sorts the relocs to ensure each HI20 immediately precedes its
2451 LO12. However if there are multiple copies, the assembler may not find
2452 the real LO12 so it picks the first one it finds. */
2453
2454 struct nds32_hi20
2455 {
2456 struct nds32_hi20 *next;
2457 bfd_byte *addr;
2458 bfd_vma addend;
2459 };
2460
2461 static struct nds32_hi20 *nds32_hi20_list;
2462
2463 static bfd_reloc_status_type
2464 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2465 asymbol *symbol, void *data, asection *input_section,
2466 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2467 {
2468 bfd_reloc_status_type ret;
2469 bfd_vma relocation;
2470 struct nds32_hi20 *n;
2471
2472 /* This part is from bfd_elf_generic_reloc.
2473 If we're relocating, and this an external symbol, we don't want
2474 to change anything. */
2475 if (output_bfd != (bfd *) NULL
2476 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2477 {
2478 reloc_entry->address += input_section->output_offset;
2479 return bfd_reloc_ok;
2480 }
2481
2482 /* Sanity check the address (offset in section). */
2483 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2484 return bfd_reloc_outofrange;
2485
2486 ret = bfd_reloc_ok;
2487 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2488 ret = bfd_reloc_undefined;
2489
2490 if (bfd_is_com_section (symbol->section))
2491 relocation = 0;
2492 else
2493 relocation = symbol->value;
2494
2495 relocation += symbol->section->output_section->vma;
2496 relocation += symbol->section->output_offset;
2497 relocation += reloc_entry->addend;
2498
2499 /* Save the information, and let LO12 do the actual relocation. */
2500 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2501 if (n == NULL)
2502 return bfd_reloc_outofrange;
2503
2504 n->addr = (bfd_byte *) data + reloc_entry->address;
2505 n->addend = relocation;
2506 n->next = nds32_hi20_list;
2507 nds32_hi20_list = n;
2508
2509 if (output_bfd != (bfd *) NULL)
2510 reloc_entry->address += input_section->output_offset;
2511
2512 return ret;
2513 }
2514
2515 /* Handle an NDS32 ELF HI20 reloc. */
2516
2517 static void
2518 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2519 int type ATTRIBUTE_UNUSED, Elf_Internal_Rela *relhi,
2520 Elf_Internal_Rela *rello, bfd_byte *contents,
2521 bfd_vma addend)
2522 {
2523 unsigned long insn;
2524 bfd_vma addlo;
2525
2526 insn = bfd_getb32 (contents + relhi->r_offset);
2527
2528 addlo = bfd_getb32 (contents + rello->r_offset);
2529 addlo &= 0xfff;
2530
2531 addend += ((insn & 0xfffff) << 20) + addlo;
2532
2533 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2534 bfd_putb32 (insn, contents + relhi->r_offset);
2535 }
2536
2537 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2538 inplace relocation; this function exists in order to do the
2539 R_NDS32_HI20_[SU]LO relocation described above. */
2540
2541 static bfd_reloc_status_type
2542 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2543 void *data, asection *input_section, bfd *output_bfd,
2544 char **error_message)
2545 {
2546 /* This part is from bfd_elf_generic_reloc.
2547 If we're relocating, and this an external symbol, we don't want
2548 to change anything. */
2549 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2550 && reloc_entry->addend == 0)
2551 {
2552 reloc_entry->address += input_section->output_offset;
2553 return bfd_reloc_ok;
2554 }
2555
2556 if (nds32_hi20_list != NULL)
2557 {
2558 struct nds32_hi20 *l;
2559
2560 l = nds32_hi20_list;
2561 while (l != NULL)
2562 {
2563 unsigned long insn;
2564 unsigned long val;
2565 unsigned long vallo;
2566 struct nds32_hi20 *next;
2567
2568 /* Do the HI20 relocation. Note that we actually don't need
2569 to know anything about the LO12 itself, except where to
2570 find the low 12 bits of the addend needed by the LO12. */
2571 insn = bfd_getb32 (l->addr);
2572 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2573 vallo &= 0xfff;
2574 switch (reloc_entry->howto->type)
2575 {
2576 case R_NDS32_LO12S3:
2577 vallo <<= 3;
2578 break;
2579
2580 case R_NDS32_LO12S2:
2581 vallo <<= 2;
2582 break;
2583
2584 case R_NDS32_LO12S1:
2585 vallo <<= 1;
2586 break;
2587
2588 case R_NDS32_LO12S0:
2589 vallo <<= 0;
2590 break;
2591 }
2592
2593 val = ((insn & 0xfffff) << 12) + vallo;
2594 val += l->addend;
2595
2596 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2597 bfd_putb32 ((bfd_vma) insn, l->addr);
2598
2599 next = l->next;
2600 free (l);
2601 l = next;
2602 }
2603
2604 nds32_hi20_list = NULL;
2605 }
2606
2607 /* Now do the LO12 reloc in the usual way.
2608 ??? It would be nice to call bfd_elf_generic_reloc here,
2609 but we have partial_inplace set. bfd_elf_generic_reloc will
2610 pass the handling back to bfd_install_relocation which will install
2611 a section relative addend which is wrong. */
2612 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2613 input_section, output_bfd, error_message);
2614 }
2615
2616 /* Do generic partial_inplace relocation.
2617 This is a local replacement for bfd_elf_generic_reloc. */
2618
2619 static bfd_reloc_status_type
2620 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2621 asymbol *symbol, void *data, asection *input_section,
2622 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2623 {
2624 bfd_reloc_status_type ret;
2625 bfd_vma relocation;
2626 bfd_byte *inplace_address;
2627
2628 /* This part is from bfd_elf_generic_reloc.
2629 If we're relocating, and this an external symbol, we don't want
2630 to change anything. */
2631 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2632 && reloc_entry->addend == 0)
2633 {
2634 reloc_entry->address += input_section->output_offset;
2635 return bfd_reloc_ok;
2636 }
2637
2638 /* Now do the reloc in the usual way.
2639 ??? It would be nice to call bfd_elf_generic_reloc here,
2640 but we have partial_inplace set. bfd_elf_generic_reloc will
2641 pass the handling back to bfd_install_relocation which will install
2642 a section relative addend which is wrong. */
2643
2644 /* Sanity check the address (offset in section). */
2645 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2646 return bfd_reloc_outofrange;
2647
2648 ret = bfd_reloc_ok;
2649 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2650 ret = bfd_reloc_undefined;
2651
2652 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2653 relocation = 0;
2654 else
2655 relocation = symbol->value;
2656
2657 /* Only do this for a final link. */
2658 if (output_bfd == (bfd *) NULL)
2659 {
2660 relocation += symbol->section->output_section->vma;
2661 relocation += symbol->section->output_offset;
2662 }
2663
2664 relocation += reloc_entry->addend;
2665 switch (reloc_entry->howto->type)
2666 {
2667 case R_NDS32_LO12S3:
2668 relocation >>= 3;
2669 break;
2670
2671 case R_NDS32_LO12S2:
2672 relocation >>= 2;
2673 break;
2674
2675 case R_NDS32_LO12S1:
2676 relocation >>= 1;
2677 break;
2678
2679 case R_NDS32_LO12S0:
2680 default:
2681 relocation >>= 0;
2682 break;
2683 }
2684
2685 inplace_address = (bfd_byte *) data + reloc_entry->address;
2686
2687 #define DOIT(x) \
2688 x = ((x & ~reloc_entry->howto->dst_mask) | \
2689 (((x & reloc_entry->howto->src_mask) + relocation) & \
2690 reloc_entry->howto->dst_mask))
2691
2692 switch (reloc_entry->howto->size)
2693 {
2694 case 1:
2695 {
2696 short x = bfd_getb16 (inplace_address);
2697
2698 DOIT (x);
2699 bfd_putb16 ((bfd_vma) x, inplace_address);
2700 }
2701 break;
2702 case 2:
2703 {
2704 unsigned long x = bfd_getb32 (inplace_address);
2705
2706 DOIT (x);
2707 bfd_putb32 ((bfd_vma) x, inplace_address);
2708 }
2709 break;
2710 default:
2711 BFD_ASSERT (0);
2712 }
2713
2714 if (output_bfd != (bfd *) NULL)
2715 reloc_entry->address += input_section->output_offset;
2716
2717 return ret;
2718 }
2719
2720 /* Handle the R_NDS32_SDA15 reloc.
2721 This reloc is used to compute the address of objects in the small data area
2722 and to perform loads and stores from that area.
2723 The lower 15 bits are sign extended and added to the register specified
2724 in the instruction, which is assumed to point to _SDA_BASE_.
2725
2726 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2727 the access size, this must be taken care of. */
2728
2729 static bfd_reloc_status_type
2730 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2731 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2732 asection *input_section, bfd *output_bfd,
2733 char **error_message ATTRIBUTE_UNUSED)
2734 {
2735 /* This part is from bfd_elf_generic_reloc. */
2736 if (output_bfd != (bfd *) NULL
2737 && (symbol->flags & BSF_SECTION_SYM) == 0
2738 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2739 {
2740 reloc_entry->address += input_section->output_offset;
2741 return bfd_reloc_ok;
2742 }
2743
2744 if (output_bfd != NULL)
2745 {
2746 /* FIXME: See bfd_perform_relocation. Is this right? */
2747 return bfd_reloc_continue;
2748 }
2749
2750 /* FIXME: not sure what to do here yet. But then again, the linker
2751 may never call us. */
2752 abort ();
2753 }
2754
2755 /* nds32_elf_ignore_reloc is the special function for
2756 relocation types which don't need to be relocated
2757 like relaxation relocation types.
2758 This function simply return bfd_reloc_ok when it is
2759 invoked. */
2760
2761 static bfd_reloc_status_type
2762 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2763 asymbol *symbol ATTRIBUTE_UNUSED,
2764 void *data ATTRIBUTE_UNUSED, asection *input_section,
2765 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2766 {
2767 if (output_bfd != NULL)
2768 reloc_entry->address += input_section->output_offset;
2769
2770 return bfd_reloc_ok;
2771 }
2772 \f
2773
2774 /* Map BFD reloc types to NDS32 ELF reloc types. */
2775
2776 struct nds32_reloc_map_entry
2777 {
2778 bfd_reloc_code_real_type bfd_reloc_val;
2779 unsigned char elf_reloc_val;
2780 };
2781
2782 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
2783 {
2784 {BFD_RELOC_NONE, R_NDS32_NONE},
2785 {BFD_RELOC_16, R_NDS32_16_RELA},
2786 {BFD_RELOC_32, R_NDS32_32_RELA},
2787 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
2788 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
2789 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
2790 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
2791 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
2792 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
2793 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
2794 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
2795 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
2796 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
2797 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
2798 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
2799 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
2800 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
2801 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
2802 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
2803 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
2804 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
2805 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
2806 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
2807
2808 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
2809 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
2810 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
2811 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
2812 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
2813 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
2814 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
2815 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
2816 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
2817 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
2818 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
2819 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
2820 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
2821 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
2822 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
2823 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
2824 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
2825 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
2826 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
2827 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
2828 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
2829 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
2830 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
2831 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
2832 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
2833 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
2834 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
2835 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
2836 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
2837 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
2838 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
2839 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
2840 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
2841 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
2842 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
2843 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
2844 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
2845 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
2846 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
2847 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
2848 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
2849 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
2850 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
2851 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
2852 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
2853 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
2854 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
2855 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
2856 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
2857 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
2858 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
2859 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
2860 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
2861 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
2862 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
2863 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
2864 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
2865 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
2866 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
2867 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
2868 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
2869 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
2870 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
2871 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
2872 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
2873 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
2874 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
2875 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
2876 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
2877 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
2878 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
2879 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
2880 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
2881 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
2882 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
2883
2884 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
2885 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
2886 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
2887 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
2888 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
2889 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
2890 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
2891 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
2892 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
2893 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
2894 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
2895 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
2896 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
2897 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
2898 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
2899 {BFD_RELOC_NDS32_TLS_TPOFF, R_NDS32_TLS_TPOFF},
2900 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
2901 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
2902 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
2903 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
2904 };
2905
2906 /* Patch tag. */
2907
2908 static reloc_howto_type *
2909 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2910 const char *r_name)
2911 {
2912 unsigned int i;
2913
2914 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
2915 if (nds32_elf_howto_table[i].name != NULL
2916 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
2917 return &nds32_elf_howto_table[i];
2918
2919 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
2920 if (nds32_elf_relax_howto_table[i].name != NULL
2921 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
2922 return &nds32_elf_relax_howto_table[i];
2923
2924 return NULL;
2925 }
2926
2927 static reloc_howto_type *
2928 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
2929 {
2930 if (code < R_NDS32_RELAX_ENTRY)
2931 {
2932 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
2933 return &nds32_elf_howto_table[code];
2934 }
2935 else
2936 {
2937 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
2938 < ARRAY_SIZE (nds32_elf_relax_howto_table));
2939 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
2940 }
2941 }
2942
2943 static reloc_howto_type *
2944 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2945 bfd_reloc_code_real_type code)
2946 {
2947 unsigned int i;
2948
2949 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
2950 {
2951 if (nds32_reloc_map[i].bfd_reloc_val == code)
2952 return bfd_elf32_bfd_reloc_type_table_lookup
2953 (nds32_reloc_map[i].elf_reloc_val);
2954 }
2955
2956 return NULL;
2957 }
2958
2959 /* Set the howto pointer for an NDS32 ELF reloc. */
2960
2961 static void
2962 nds32_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2963 Elf_Internal_Rela *dst)
2964 {
2965 enum elf_nds32_reloc_type r_type;
2966
2967 r_type = ELF32_R_TYPE (dst->r_info);
2968 if (r_type > R_NDS32_GNU_VTENTRY)
2969 {
2970 _bfd_error_handler (_("%A: invalid NDS32 reloc number: %d"), abfd, r_type);
2971 r_type = 0;
2972 }
2973 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
2974 }
2975
2976 static void
2977 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2978 Elf_Internal_Rela *dst)
2979 {
2980 BFD_ASSERT ((ELF32_R_TYPE (dst->r_info) == R_NDS32_NONE)
2981 || ((ELF32_R_TYPE (dst->r_info) > R_NDS32_GNU_VTENTRY)
2982 && (ELF32_R_TYPE (dst->r_info) < R_NDS32_max)));
2983 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (dst->r_info));
2984 }
2985
2986 /* Support for core dump NOTE sections.
2987 Reference to include/linux/elfcore.h in Linux. */
2988
2989 static bfd_boolean
2990 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2991 {
2992 int offset;
2993 size_t size;
2994
2995 switch (note->descsz)
2996 {
2997 case 0x114:
2998 /* Linux/NDS32 32-bit, ABI1 */
2999
3000 /* pr_cursig */
3001 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3002
3003 /* pr_pid */
3004 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3005
3006 /* pr_reg */
3007 offset = 72;
3008 size = 200;
3009 break;
3010
3011 case 0xfc:
3012 /* Linux/NDS32 32-bit */
3013
3014 /* pr_cursig */
3015 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3016
3017 /* pr_pid */
3018 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3019
3020 /* pr_reg */
3021 offset = 72;
3022 size = 176;
3023 break;
3024
3025 default:
3026 return FALSE;
3027 }
3028
3029 /* Make a ".reg" section. */
3030 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3031 size, note->descpos + offset);
3032 }
3033
3034 static bfd_boolean
3035 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3036 {
3037 switch (note->descsz)
3038 {
3039 case 124:
3040 /* Linux/NDS32 */
3041
3042 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3043 elf_tdata (abfd)->core->program =
3044 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3045 elf_tdata (abfd)->core->command =
3046 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3047
3048 default:
3049 return FALSE;
3050 }
3051
3052 /* Note that for some reason, a spurious space is tacked
3053 onto the end of the args in some (at least one anyway)
3054 implementations, so strip it off if it exists. */
3055 {
3056 char *command = elf_tdata (abfd)->core->command;
3057 int n = strlen (command);
3058
3059 if (0 < n && command[n - 1] == ' ')
3060 command[n - 1] = '\0';
3061 }
3062
3063 return TRUE;
3064 }
3065
3066 /* Hook called by the linker routine which adds symbols from an object
3067 file. We must handle the special NDS32 section numbers here.
3068 We also keep watching for whether we need to create the sdata special
3069 linker sections. */
3070
3071 static bfd_boolean
3072 nds32_elf_add_symbol_hook (bfd *abfd,
3073 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3074 Elf_Internal_Sym *sym,
3075 const char **namep ATTRIBUTE_UNUSED,
3076 flagword *flagsp ATTRIBUTE_UNUSED,
3077 asection **secp, bfd_vma *valp)
3078 {
3079 switch (sym->st_shndx)
3080 {
3081 case SHN_COMMON:
3082 /* Common symbols less than the GP size are automatically
3083 treated as SHN_MIPS_SCOMMON symbols. */
3084 if (sym->st_size > elf_gp_size (abfd)
3085 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3086 break;
3087
3088 /* st_value is the alignemnt constraint.
3089 That might be its actual size if it is an array or structure. */
3090 switch (sym->st_value)
3091 {
3092 case 1:
3093 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3094 break;
3095 case 2:
3096 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3097 break;
3098 case 4:
3099 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3100 break;
3101 case 8:
3102 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3103 break;
3104 default:
3105 return TRUE;
3106 }
3107
3108 (*secp)->flags |= SEC_IS_COMMON;
3109 *valp = sym->st_size;
3110 break;
3111 }
3112
3113 return TRUE;
3114 }
3115
3116
3117 /* This function can figure out the best location for a base register to access
3118 data relative to this base register
3119 INPUT:
3120 sda_d0: size of first DOUBLE WORD data section
3121 sda_w0: size of first WORD data section
3122 sda_h0: size of first HALF WORD data section
3123 sda_b : size of BYTE data section
3124 sda_hi: size of second HALF WORD data section
3125 sda_w1: size of second WORD data section
3126 sda_d1: size of second DOUBLE WORD data section
3127 OUTPUT:
3128 offset (always positive) from the beginning of sda_d0 if OK
3129 a negative error value if fail
3130 NOTE:
3131 these 7 sections have to be located back to back if exist
3132 a pass in 0 value for non-existing section */
3133
3134 /* Due to the interpretation of simm15 field of load/store depending on
3135 data accessing size, the organization of base register relative data shall
3136 like the following figure
3137 -------------------------------------------
3138 | DOUBLE WORD sized data (range +/- 128K)
3139 -------------------------------------------
3140 | WORD sized data (range +/- 64K)
3141 -------------------------------------------
3142 | HALF WORD sized data (range +/- 32K)
3143 -------------------------------------------
3144 | BYTE sized data (range +/- 16K)
3145 -------------------------------------------
3146 | HALF WORD sized data (range +/- 32K)
3147 -------------------------------------------
3148 | WORD sized data (range +/- 64K)
3149 -------------------------------------------
3150 | DOUBLE WORD sized data (range +/- 128K)
3151 -------------------------------------------
3152 Its base register shall be set to access these data freely. */
3153
3154 /* We have to figure out the SDA_BASE value, so that we can adjust the
3155 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3156 BFD. If we can't find it, we're stuck. We cache it in the ELF
3157 target data. We don't need to adjust the symbol value for an
3158 external symbol if we are producing relocatable output. */
3159
3160 static asection *sda_rela_sec = NULL;
3161
3162 #define SDA_SECTION_NUM 10
3163
3164 static bfd_reloc_status_type
3165 nds32_elf_final_sda_base (bfd *output_bfd, struct bfd_link_info *info,
3166 bfd_vma *psb, bfd_boolean add_symbol)
3167 {
3168 int relax_fp_as_gp;
3169 struct elf_nds32_link_hash_table *table;
3170 struct bfd_link_hash_entry *h, *h2;
3171 long unsigned int total = 0;
3172
3173 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3174 if (!h || (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak))
3175 {
3176 asection *first = NULL, *final = NULL, *temp;
3177 bfd_vma sda_base;
3178 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3179 4 byte-aligned. Therefore, it has to set the first section ".data"
3180 4 byte-aligned. */
3181 static const char sec_name[SDA_SECTION_NUM][10] =
3182 {
3183 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3184 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3185 };
3186 size_t i = 0;
3187
3188 if (output_bfd->sections == NULL)
3189 {
3190 *psb = elf_gp (output_bfd);
3191 return bfd_reloc_ok;
3192 }
3193
3194 /* Get the first and final section. */
3195 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3196 {
3197 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3198 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3199 first = temp;
3200 if (temp && (temp->size != 0 || temp->rawsize != 0))
3201 final = temp;
3202
3203 /* Summarize the sections in order to check if joining .bss. */
3204 if (temp && temp->size != 0)
3205 total += temp->size;
3206 else if (temp && temp->rawsize != 0)
3207 total += temp->rawsize;
3208
3209 i++;
3210 }
3211
3212 /* Check .bss size. */
3213 temp = bfd_get_section_by_name (output_bfd, ".bss");
3214 if (temp)
3215 {
3216 if (temp->size != 0)
3217 total += temp->size;
3218 else if (temp->rawsize != 0)
3219 total += temp->rawsize;
3220
3221 if (total < 0x80000)
3222 {
3223 if (!first && (temp->size != 0 || temp->rawsize != 0))
3224 first = temp;
3225 if ((temp->size != 0 || temp->rawsize != 0))
3226 final = temp;
3227 }
3228 }
3229
3230 if (first && final)
3231 {
3232 /* The middle of data region. */
3233 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3234
3235 /* Find the section sda_base located. */
3236 i = 0;
3237 while (i < sizeof (sec_name) / sizeof (sec_name [0]))
3238 {
3239 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3240 if (final && (final->size != 0 || final->rawsize != 0)
3241 && sda_base >= final->vma)
3242 {
3243 first = final;
3244 i++;
3245 }
3246 else
3247 break;
3248 }
3249 }
3250 else
3251 {
3252 /* There is not any data section in output bfd, and set _SDA_BASE_ in
3253 first output section. */
3254 first = output_bfd->sections;
3255 while (first && first->size == 0 && first->rawsize == 0)
3256 first = first->next;
3257 if (!first)
3258 {
3259 *psb = elf_gp (output_bfd);
3260 return bfd_reloc_ok;
3261 }
3262 sda_base = first->vma + first->rawsize;
3263 }
3264
3265 sda_base -= first->vma;
3266 sda_base = sda_base & (~7);
3267
3268 if (!_bfd_generic_link_add_one_symbol
3269 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3270 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3271 get_elf_backend_data (output_bfd)->collect, &h))
3272 return FALSE;
3273
3274 sda_rela_sec = first;
3275
3276 table = nds32_elf_hash_table (info);
3277 relax_fp_as_gp = table->relax_fp_as_gp;
3278 if (relax_fp_as_gp)
3279 {
3280 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME,
3281 FALSE, FALSE, FALSE);
3282 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3283 And set FP equal to SDA_BASE to do relaxation for
3284 la $fp, _FP_BASE_. */
3285 if (!_bfd_generic_link_add_one_symbol
3286 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3287 first, (bfd_vma) sda_base, (const char *) NULL,
3288 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3289 return FALSE;
3290 }
3291 }
3292
3293 if (add_symbol == TRUE)
3294 {
3295 if (h)
3296 {
3297 /* Now set gp. */
3298 elf_gp (output_bfd) = (h->u.def.value
3299 + h->u.def.section->output_section->vma
3300 + h->u.def.section->output_offset);
3301 }
3302 else
3303 {
3304 (*_bfd_error_handler) (_("error: Can't find symbol: _SDA_BASE_."));
3305 return bfd_reloc_dangerous;
3306 }
3307 }
3308
3309 *psb = h->u.def.value + h->u.def.section->output_section->vma
3310 + h->u.def.section->output_offset;
3311 return bfd_reloc_ok;
3312 }
3313 \f
3314
3315 /* Return size of a PLT entry. */
3316 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3317
3318
3319 /* Create an entry in an nds32 ELF linker hash table. */
3320
3321 static struct bfd_hash_entry *
3322 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3323 struct bfd_hash_table *table,
3324 const char *string)
3325 {
3326 struct elf_nds32_link_hash_entry *ret;
3327
3328 ret = (struct elf_nds32_link_hash_entry *) entry;
3329
3330 /* Allocate the structure if it has not already been allocated by a
3331 subclass. */
3332 if (ret == NULL)
3333 ret = (struct elf_nds32_link_hash_entry *)
3334 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3335
3336 if (ret == NULL)
3337 return (struct bfd_hash_entry *) ret;
3338
3339 /* Call the allocation method of the superclass. */
3340 ret = (struct elf_nds32_link_hash_entry *)
3341 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3342
3343 if (ret != NULL)
3344 {
3345 struct elf_nds32_link_hash_entry *eh;
3346
3347 eh = (struct elf_nds32_link_hash_entry *) ret;
3348 eh->dyn_relocs = NULL;
3349 eh->tls_type = GOT_UNKNOWN;
3350 }
3351
3352 return (struct bfd_hash_entry *) ret;
3353 }
3354
3355 /* Create an nds32 ELF linker hash table. */
3356
3357 static struct bfd_link_hash_table *
3358 nds32_elf_link_hash_table_create (bfd *abfd)
3359 {
3360 struct elf_nds32_link_hash_table *ret;
3361
3362 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3363
3364 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3365 if (ret == NULL)
3366 return NULL;
3367
3368 /* patch tag. */
3369 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3370 nds32_elf_link_hash_newfunc,
3371 sizeof (struct elf_nds32_link_hash_entry),
3372 NDS32_ELF_DATA))
3373 {
3374 free (ret);
3375 return NULL;
3376 }
3377
3378 ret->sgot = NULL;
3379 ret->sgotplt = NULL;
3380 ret->srelgot = NULL;
3381 ret->splt = NULL;
3382 ret->srelplt = NULL;
3383 ret->sdynbss = NULL;
3384 ret->srelbss = NULL;
3385 ret->sym_ld_script = NULL;
3386 ret->ex9_export_file = NULL;
3387 ret->ex9_import_file = NULL;
3388
3389 return &ret->root.root;
3390 }
3391
3392 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3393 shortcuts to them in our hash table. */
3394
3395 static bfd_boolean
3396 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3397 {
3398 struct elf_nds32_link_hash_table *htab;
3399
3400 if (!_bfd_elf_create_got_section (dynobj, info))
3401 return FALSE;
3402
3403 htab = nds32_elf_hash_table (info);
3404 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
3405 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3406 if (!htab->sgot || !htab->sgotplt)
3407 abort ();
3408
3409 /* _bfd_elf_create_got_section will create it for us. */
3410 htab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3411 if (htab->srelgot == NULL
3412 || !bfd_set_section_flags (dynobj, htab->srelgot,
3413 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3414 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3415 | SEC_READONLY))
3416 || !bfd_set_section_alignment (dynobj, htab->srelgot, 2))
3417 return FALSE;
3418
3419 return TRUE;
3420 }
3421
3422 /* Create dynamic sections when linking against a dynamic object. */
3423
3424 static bfd_boolean
3425 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3426 {
3427 struct elf_nds32_link_hash_table *htab;
3428 flagword flags, pltflags;
3429 register asection *s;
3430 const struct elf_backend_data *bed;
3431 int ptralign = 2; /* 32-bit */
3432
3433 bed = get_elf_backend_data (abfd);
3434
3435 htab = nds32_elf_hash_table (info);
3436
3437 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3438 .rel[a].bss sections. */
3439
3440 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3441 | SEC_LINKER_CREATED);
3442
3443 pltflags = flags;
3444 pltflags |= SEC_CODE;
3445 if (bed->plt_not_loaded)
3446 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3447 if (bed->plt_readonly)
3448 pltflags |= SEC_READONLY;
3449
3450 s = bfd_make_section (abfd, ".plt");
3451 htab->splt = s;
3452 if (s == NULL
3453 || !bfd_set_section_flags (abfd, s, pltflags)
3454 || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3455 return FALSE;
3456
3457 if (bed->want_plt_sym)
3458 {
3459 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3460 .plt section. */
3461 struct bfd_link_hash_entry *bh = NULL;
3462 struct elf_link_hash_entry *h;
3463
3464 if (!(_bfd_generic_link_add_one_symbol
3465 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3466 (bfd_vma) 0, (const char *) NULL, FALSE,
3467 get_elf_backend_data (abfd)->collect, &bh)))
3468 return FALSE;
3469
3470 h = (struct elf_link_hash_entry *) bh;
3471 h->def_regular = 1;
3472 h->type = STT_OBJECT;
3473
3474 if (info->shared && !bfd_elf_link_record_dynamic_symbol (info, h))
3475 return FALSE;
3476 }
3477
3478 s = bfd_make_section (abfd,
3479 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3480 htab->srelplt = s;
3481 if (s == NULL
3482 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3483 || !bfd_set_section_alignment (abfd, s, ptralign))
3484 return FALSE;
3485
3486 if (htab->sgot == NULL && !create_got_section (abfd, info))
3487 return FALSE;
3488
3489 {
3490 const char *secname;
3491 char *relname;
3492 flagword secflags;
3493 asection *sec;
3494
3495 for (sec = abfd->sections; sec; sec = sec->next)
3496 {
3497 secflags = bfd_get_section_flags (abfd, sec);
3498 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3499 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3500 continue;
3501 secname = bfd_get_section_name (abfd, sec);
3502 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3503 strcpy (relname, ".rela");
3504 strcat (relname, secname);
3505 if (bfd_get_section_by_name (abfd, secname))
3506 continue;
3507 s = bfd_make_section (abfd, relname);
3508 if (s == NULL
3509 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3510 || !bfd_set_section_alignment (abfd, s, ptralign))
3511 return FALSE;
3512 }
3513 }
3514
3515 if (bed->want_dynbss)
3516 {
3517 /* The .dynbss section is a place to put symbols which are defined
3518 by dynamic objects, are referenced by regular objects, and are
3519 not functions. We must allocate space for them in the process
3520 image and use a R_*_COPY reloc to tell the dynamic linker to
3521 initialize them at run time. The linker script puts the .dynbss
3522 section into the .bss section of the final image. */
3523 s = bfd_make_section (abfd, ".dynbss");
3524 htab->sdynbss = s;
3525 if (s == NULL
3526 || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3527 return FALSE;
3528 /* The .rel[a].bss section holds copy relocs. This section is not
3529 normally needed. We need to create it here, though, so that the
3530 linker will map it to an output section. We can't just create it
3531 only if we need it, because we will not know whether we need it
3532 until we have seen all the input files, and the first time the
3533 main linker code calls BFD after examining all the input files
3534 (size_dynamic_sections) the input sections have already been
3535 mapped to the output sections. If the section turns out not to
3536 be needed, we can discard it later. We will never need this
3537 section when generating a shared object, since they do not use
3538 copy relocs. */
3539 if (!info->shared)
3540 {
3541 s = bfd_make_section (abfd, (bed->default_use_rela_p
3542 ? ".rela.bss" : ".rel.bss"));
3543 htab->srelbss = s;
3544 if (s == NULL
3545 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3546 || !bfd_set_section_alignment (abfd, s, ptralign))
3547 return FALSE;
3548 }
3549 }
3550
3551 return TRUE;
3552 }
3553
3554 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3555 static void
3556 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3557 struct elf_link_hash_entry *dir,
3558 struct elf_link_hash_entry *ind)
3559 {
3560 struct elf_nds32_link_hash_entry *edir, *eind;
3561
3562 edir = (struct elf_nds32_link_hash_entry *) dir;
3563 eind = (struct elf_nds32_link_hash_entry *) ind;
3564
3565 if (eind->dyn_relocs != NULL)
3566 {
3567 if (edir->dyn_relocs != NULL)
3568 {
3569 struct elf_nds32_dyn_relocs **pp;
3570 struct elf_nds32_dyn_relocs *p;
3571
3572 if (ind->root.type == bfd_link_hash_indirect)
3573 abort ();
3574
3575 /* Add reloc counts against the weak sym to the strong sym
3576 list. Merge any entries against the same section. */
3577 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3578 {
3579 struct elf_nds32_dyn_relocs *q;
3580
3581 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3582 if (q->sec == p->sec)
3583 {
3584 q->pc_count += p->pc_count;
3585 q->count += p->count;
3586 *pp = p->next;
3587 break;
3588 }
3589 if (q == NULL)
3590 pp = &p->next;
3591 }
3592 *pp = edir->dyn_relocs;
3593 }
3594
3595 edir->dyn_relocs = eind->dyn_relocs;
3596 eind->dyn_relocs = NULL;
3597 }
3598
3599 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3600 }
3601 \f
3602
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 abort ();
4190 break;
4191 case 0:
4192 return bfd_reloc_ok;
4193 case 2:
4194 x = bfd_getb16 (location);
4195 break;
4196 case 4:
4197 x = bfd_getb32 (location);
4198 break;
4199 }
4200
4201 /* Check for overflow. FIXME: We may drop bits during the addition
4202 which we don't check for. We must either check at every single
4203 operation, which would be tedious, or we must do the computations
4204 in a type larger than bfd_vma, which would be inefficient. */
4205 flag = bfd_reloc_ok;
4206 if (howto->complain_on_overflow != complain_overflow_dont)
4207 {
4208 bfd_vma addrmask, fieldmask, signmask, ss;
4209 bfd_vma a, b, sum;
4210
4211 /* Get the values to be added together. For signed and unsigned
4212 relocations, we assume that all values should be truncated to
4213 the size of an address. For bitfields, all the bits matter.
4214 See also bfd_check_overflow. */
4215 fieldmask = N_ONES (howto->bitsize);
4216 signmask = ~fieldmask;
4217 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4218 a = (relocation & addrmask) >> rightshift;
4219 b = (x & howto->src_mask & addrmask) >> bitpos;
4220
4221 switch (howto->complain_on_overflow)
4222 {
4223 case complain_overflow_signed:
4224 /* If any sign bits are set, all sign bits must be set.
4225 That is, A must be a valid negative address after
4226 shifting. */
4227 signmask = ~(fieldmask >> 1);
4228 /* Fall through. */
4229
4230 case complain_overflow_bitfield:
4231 /* Much like the signed check, but for a field one bit
4232 wider. We allow a bitfield to represent numbers in the
4233 range -2**n to 2**n-1, where n is the number of bits in the
4234 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4235 can't overflow, which is exactly what we want. */
4236 ss = a & signmask;
4237 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4238 flag = bfd_reloc_overflow;
4239
4240 /* We only need this next bit of code if the sign bit of B
4241 is below the sign bit of A. This would only happen if
4242 SRC_MASK had fewer bits than BITSIZE. Note that if
4243 SRC_MASK has more bits than BITSIZE, we can get into
4244 trouble; we would need to verify that B is in range, as
4245 we do for A above. */
4246 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4247 ss >>= bitpos;
4248
4249 /* Set all the bits above the sign bit. */
4250 b = (b ^ ss) - ss;
4251
4252 /* Now we can do the addition. */
4253 sum = a + b;
4254
4255 /* See if the result has the correct sign. Bits above the
4256 sign bit are junk now; ignore them. If the sum is
4257 positive, make sure we did not have all negative inputs;
4258 if the sum is negative, make sure we did not have all
4259 positive inputs. The test below looks only at the sign
4260 bits, and it really just
4261 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4262
4263 We mask with addrmask here to explicitly allow an address
4264 wrap-around. The Linux kernel relies on it, and it is
4265 the only way to write assembler code which can run when
4266 loaded at a location 0x80000000 away from the location at
4267 which it is linked. */
4268 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4269 flag = bfd_reloc_overflow;
4270
4271 break;
4272
4273 case complain_overflow_unsigned:
4274 /* Checking for an unsigned overflow is relatively easy:
4275 trim the addresses and add, and trim the result as well.
4276 Overflow is normally indicated when the result does not
4277 fit in the field. However, we also need to consider the
4278 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4279 input is 0x80000000, and bfd_vma is only 32 bits; then we
4280 will get sum == 0, but there is an overflow, since the
4281 inputs did not fit in the field. Instead of doing a
4282 separate test, we can check for this by or-ing in the
4283 operands when testing for the sum overflowing its final
4284 field. */
4285 sum = (a + b) & addrmask;
4286 if ((a | b | sum) & signmask)
4287 flag = bfd_reloc_overflow;
4288 break;
4289
4290 default:
4291 abort ();
4292 }
4293 }
4294
4295 /* Put RELOCATION in the right bits. */
4296 relocation >>= (bfd_vma) rightshift;
4297 relocation <<= (bfd_vma) bitpos;
4298
4299 /* Add RELOCATION to the right bits of X. */
4300 /* FIXME : 090616
4301 Because the relaxation may generate duplicate relocation at one address,
4302 an addition to immediate in the instruction may cause the relocation added
4303 several times.
4304 This bug should be fixed in assembler, but a check is also needed here. */
4305 if (howto->partial_inplace)
4306 x = ((x & ~howto->dst_mask)
4307 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4308 else
4309 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4310
4311
4312 /* Put the relocated value back in the object file. */
4313 switch (size)
4314 {
4315 default:
4316 case 0:
4317 case 1:
4318 case 8:
4319 abort ();
4320 break;
4321 case 2:
4322 bfd_putb16 (x, location);
4323 break;
4324 case 4:
4325 bfd_putb32 (x, location);
4326 break;
4327 }
4328
4329 return flag;
4330 }
4331
4332 static bfd_reloc_status_type
4333 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4334 asection *input_section, bfd_byte *contents,
4335 bfd_vma address, bfd_vma value, bfd_vma addend)
4336 {
4337 bfd_vma relocation;
4338
4339 /* Sanity check the address. */
4340 if (address > bfd_get_section_limit (input_bfd, input_section))
4341 return bfd_reloc_outofrange;
4342
4343 /* This function assumes that we are dealing with a basic relocation
4344 against a symbol. We want to compute the value of the symbol to
4345 relocate to. This is just VALUE, the value of the symbol, plus
4346 ADDEND, any addend associated with the reloc. */
4347 relocation = value + addend;
4348
4349 /* If the relocation is PC relative, we want to set RELOCATION to
4350 the distance between the symbol (currently in RELOCATION) and the
4351 location we are relocating. Some targets (e.g., i386-aout)
4352 arrange for the contents of the section to be the negative of the
4353 offset of the location within the section; for such targets
4354 pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
4355 simply leave the contents of the section as zero; for such
4356 targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
4357 need to subtract out the offset of the location within the
4358 section (which is just ADDRESS). */
4359 if (howto->pc_relative)
4360 {
4361 relocation -= (input_section->output_section->vma
4362 + input_section->output_offset);
4363 if (howto->pcrel_offset)
4364 relocation -= address;
4365 }
4366
4367 return nds32_relocate_contents (howto, input_bfd, relocation,
4368 contents + address);
4369 }
4370
4371 static bfd_boolean
4372 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4373 const char *name,
4374 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4375 asection *input_sec,
4376 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4377 {
4378 const char *source;
4379 FILE *sym_ld_script = NULL;
4380 struct elf_nds32_link_hash_table *table;
4381
4382 table = nds32_elf_hash_table (info);
4383 sym_ld_script = table->sym_ld_script;
4384 if (!sym_ld_script)
4385 return TRUE;
4386
4387 if (!h || !name || *name == '\0')
4388 return TRUE;
4389
4390 if (input_sec->flags & SEC_EXCLUDE)
4391 return TRUE;
4392
4393 if (!check_start_export_sym)
4394 {
4395 fprintf (sym_ld_script, "SECTIONS\n{\n");
4396 check_start_export_sym = 1;
4397 }
4398
4399 if (h->root.type == bfd_link_hash_defined
4400 || h->root.type == bfd_link_hash_defweak)
4401 {
4402 if (!h->root.u.def.section->output_section)
4403 return TRUE;
4404
4405 if (bfd_is_const_section (input_sec))
4406 source = input_sec->name;
4407 else
4408 source = input_sec->owner->filename;
4409
4410 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4411 h->root.root.string,
4412 (long) (h->root.u.def.value
4413 + h->root.u.def.section->output_section->vma
4414 + h->root.u.def.section->output_offset), source);
4415 }
4416
4417 return TRUE;
4418 }
4419
4420 /* Relocate an NDS32/D ELF section.
4421 There is some attempt to make this function usable for many architectures,
4422 both for RELA and REL type relocs, if only to serve as a learning tool.
4423
4424 The RELOCATE_SECTION function is called by the new ELF backend linker
4425 to handle the relocations for a section.
4426
4427 The relocs are always passed as Rela structures; if the section
4428 actually uses Rel structures, the r_addend field will always be
4429 zero.
4430
4431 This function is responsible for adjust the section contents as
4432 necessary, and (if using Rela relocs and generating a
4433 relocatable output file) adjusting the reloc addend as
4434 necessary.
4435
4436 This function does not have to worry about setting the reloc
4437 address or the reloc symbol index.
4438
4439 LOCAL_SYMS is a pointer to the swapped in local symbols.
4440
4441 LOCAL_SECTIONS is an array giving the section in the input file
4442 corresponding to the st_shndx field of each local symbol.
4443
4444 The global hash table entry for the global symbols can be found
4445 via elf_sym_hashes (input_bfd).
4446
4447 When generating relocatable output, this function must handle
4448 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4449 going to be the section symbol corresponding to the output
4450 section, which means that the addend must be adjusted
4451 accordingly. */
4452
4453 static bfd_vma
4454 dtpoff_base (struct bfd_link_info *info)
4455 {
4456 /* If tls_sec is NULL, we should have signalled an error already. */
4457 if (elf_hash_table (info)->tls_sec == NULL)
4458 return 0;
4459 return elf_hash_table (info)->tls_sec->vma;
4460 }
4461
4462 static bfd_boolean
4463 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
4464 struct bfd_link_info * info,
4465 bfd * input_bfd,
4466 asection * input_section,
4467 bfd_byte * contents,
4468 Elf_Internal_Rela * relocs,
4469 Elf_Internal_Sym * local_syms,
4470 asection ** local_sections)
4471 {
4472 Elf_Internal_Shdr *symtab_hdr;
4473 struct elf_link_hash_entry **sym_hashes;
4474 Elf_Internal_Rela *rel, *relend;
4475 bfd_boolean ret = TRUE; /* Assume success. */
4476 int align = 0;
4477 bfd_reloc_status_type r;
4478 const char *errmsg = NULL;
4479 bfd_vma gp;
4480 struct elf_nds32_link_hash_table *htab;
4481 bfd *dynobj;
4482 bfd_vma *local_got_offsets;
4483 asection *sgot, *splt, *sreloc;
4484 bfd_vma high_address;
4485 struct elf_nds32_link_hash_table *table;
4486 int eliminate_gc_relocs;
4487 bfd_vma fpbase_addr;
4488
4489 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4490 sym_hashes = elf_sym_hashes (input_bfd);
4491 htab = nds32_elf_hash_table (info);
4492 high_address = bfd_get_section_limit (input_bfd, input_section);
4493
4494 dynobj = htab->root.dynobj;
4495 local_got_offsets = elf_local_got_offsets (input_bfd);
4496
4497 sgot = htab->sgot;
4498 splt = htab->splt;
4499 sreloc = NULL;
4500
4501 rel = relocs;
4502 relend = relocs + input_section->reloc_count;
4503
4504 table = nds32_elf_hash_table (info);
4505 eliminate_gc_relocs = table->eliminate_gc_relocs;
4506 /* By this time, we can adjust the value of _SDA_BASE_. */
4507 if ((!info->relocatable))
4508 {
4509 is_SDA_BASE_set = 1;
4510 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
4511 if (r != bfd_reloc_ok)
4512 return FALSE;
4513 }
4514
4515 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
4516 if (!nds32_elf_ifc_reloc ())
4517 (*_bfd_error_handler) (_("error: IFC relocation error."));
4518
4519 /* Relocation for .ex9.itable. */
4520 if (table->target_optimize & NDS32_RELAX_EX9_ON
4521 || (table->ex9_import_file && table->update_ex9_table))
4522 nds32_elf_ex9_reloc_jmp (info);
4523
4524 /* Use gp as fp to prevent truncated fit. Because in relaxation time
4525 the fp value is set as gp, and it has be reverted for instruction
4526 setting fp. */
4527 fpbase_addr = elf_gp (output_bfd);
4528
4529 for (rel = relocs; rel < relend; rel++)
4530 {
4531 enum elf_nds32_reloc_type r_type;
4532 reloc_howto_type *howto = NULL;
4533 unsigned long r_symndx;
4534 struct elf_link_hash_entry *h = NULL;
4535 Elf_Internal_Sym *sym = NULL;
4536 asection *sec;
4537 bfd_vma relocation;
4538
4539 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
4540 ensure it's zero (we use REL relocs, not RELA). Therefore this
4541 should be assigning zero to `addend', but for clarity we use
4542 `r_addend'. */
4543
4544 bfd_vma addend = rel->r_addend;
4545 bfd_vma offset = rel->r_offset;
4546
4547 r_type = ELF32_R_TYPE (rel->r_info);
4548 if (r_type >= R_NDS32_max)
4549 {
4550 (*_bfd_error_handler) (_("%B: error: unknown relocation type %d."),
4551 input_bfd, r_type);
4552 bfd_set_error (bfd_error_bad_value);
4553 ret = FALSE;
4554 continue;
4555 }
4556
4557 if (r_type == R_NDS32_GNU_VTENTRY
4558 || r_type == R_NDS32_GNU_VTINHERIT
4559 || r_type == R_NDS32_NONE
4560 || r_type == R_NDS32_RELA_GNU_VTENTRY
4561 || r_type == R_NDS32_RELA_GNU_VTINHERIT
4562 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
4563 || r_type == R_NDS32_DATA
4564 || r_type == R_NDS32_TRAN
4565 || (r_type >= R_NDS32_LONGCALL4 && r_type <= R_NDS32_LONGJUMP6))
4566 continue;
4567
4568 /* If we enter the fp-as-gp region. Resolve the address of best fp-base. */
4569 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
4570 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4571 {
4572 int dist;
4573
4574 /* Distance to relocation of best fp-base is encoded in R_SYM. */
4575 dist = rel->r_addend >> 16;
4576 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
4577 local_syms, symtab_hdr);
4578 }
4579 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
4580 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
4581 {
4582 fpbase_addr = elf_gp (output_bfd);
4583 }
4584
4585 if (((r_type >= R_NDS32_DWARF2_OP1_RELA
4586 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4587 || r_type >= R_NDS32_RELAX_ENTRY) && !info->relocatable)
4588 continue;
4589
4590 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
4591 r_symndx = ELF32_R_SYM (rel->r_info);
4592
4593 /* This is a final link. */
4594 sym = NULL;
4595 sec = NULL;
4596 h = NULL;
4597
4598 if (r_symndx < symtab_hdr->sh_info)
4599 {
4600 /* Local symbol. */
4601 sym = local_syms + r_symndx;
4602 sec = local_sections[r_symndx];
4603
4604 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4605 addend = rel->r_addend;
4606 }
4607 else
4608 {
4609 /* External symbol. */
4610 bfd_boolean warned, ignored, unresolved_reloc;
4611 int symndx = r_symndx - symtab_hdr->sh_info;
4612
4613 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
4614 r_symndx, symtab_hdr, sym_hashes, h, sec,
4615 relocation, unresolved_reloc, warned,
4616 ignored);
4617
4618 /* la $fp, _FP_BASE_ is per-function (region).
4619 Handle it specially. */
4620 switch ((int) r_type)
4621 {
4622 case R_NDS32_SDA19S0_RELA:
4623 case R_NDS32_SDA15S0_RELA:
4624 case R_NDS32_20_RELA:
4625 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
4626 FP_BASE_NAME) == 0)
4627 {
4628 relocation = fpbase_addr;
4629 break;
4630 }
4631 }
4632
4633 }
4634
4635 if (info->relocatable)
4636 {
4637 /* This is a relocatable link. We don't have to change
4638 anything, unless the reloc is against a section symbol,
4639 in which case we have to adjust according to where the
4640 section symbol winds up in the output section. */
4641 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
4642 rel->r_addend += sec->output_offset + sym->st_value;
4643
4644 continue;
4645 }
4646
4647 /* Sanity check the address. */
4648 if (offset > high_address)
4649 {
4650 r = bfd_reloc_outofrange;
4651 goto check_reloc;
4652 }
4653
4654 if ((r_type >= R_NDS32_DWARF2_OP1_RELA
4655 && r_type <= R_NDS32_DWARF2_LEB_RELA)
4656 || r_type >= R_NDS32_RELAX_ENTRY)
4657 continue;
4658
4659 switch ((int) r_type)
4660 {
4661 case R_NDS32_GOTOFF:
4662 /* Relocation is relative to the start of the global offset
4663 table (for ld24 rx, #uimm24), e.g. access at label+addend
4664
4665 ld24 rx. #label@GOTOFF + addend
4666 sub rx, r12. */
4667 case R_NDS32_GOTOFF_HI20:
4668 case R_NDS32_GOTOFF_LO12:
4669 case R_NDS32_GOTOFF_LO15:
4670 case R_NDS32_GOTOFF_LO19:
4671 BFD_ASSERT (sgot != NULL);
4672
4673 relocation -= elf_gp (output_bfd);
4674 break;
4675
4676 case R_NDS32_9_PLTREL:
4677 case R_NDS32_25_PLTREL:
4678 /* Relocation is to the entry for this symbol in the
4679 procedure linkage table. */
4680
4681 /* The native assembler will generate a 25_PLTREL reloc
4682 for a local symbol if you assemble a call from one
4683 section to another when using -K pic. */
4684 if (h == NULL)
4685 break;
4686
4687 if (h->forced_local)
4688 break;
4689
4690 /* We didn't make a PLT entry for this symbol. This
4691 happens when statically linking PIC code, or when
4692 using -Bsymbolic. */
4693 if (h->plt.offset == (bfd_vma) - 1)
4694 break;
4695
4696 relocation = (splt->output_section->vma
4697 + splt->output_offset + h->plt.offset);
4698 break;
4699
4700 case R_NDS32_PLT_GOTREL_HI20:
4701 case R_NDS32_PLT_GOTREL_LO12:
4702 case R_NDS32_PLT_GOTREL_LO15:
4703 case R_NDS32_PLT_GOTREL_LO19:
4704 case R_NDS32_PLT_GOTREL_LO20:
4705 if (h == NULL || h->forced_local || h->plt.offset == (bfd_vma) - 1)
4706 {
4707 /* We didn't make a PLT entry for this symbol. This
4708 happens when statically linking PIC code, or when
4709 using -Bsymbolic. */
4710 relocation -= elf_gp (output_bfd);
4711 break;
4712 }
4713
4714 relocation = (splt->output_section->vma
4715 + splt->output_offset + h->plt.offset);
4716
4717 relocation -= elf_gp (output_bfd);
4718 break;
4719
4720 case R_NDS32_PLTREL_HI20:
4721 case R_NDS32_PLTREL_LO12:
4722
4723 /* Relocation is to the entry for this symbol in the
4724 procedure linkage table. */
4725
4726 /* The native assembler will generate a 25_PLTREL reloc
4727 for a local symbol if you assemble a call from one
4728 section to another when using -K pic. */
4729 if (h == NULL)
4730 break;
4731
4732 if (h->forced_local)
4733 break;
4734
4735 if (h->plt.offset == (bfd_vma) - 1)
4736 /* We didn't make a PLT entry for this symbol. This
4737 happens when statically linking PIC code, or when
4738 using -Bsymbolic. */
4739 break;
4740
4741 if (splt == NULL)
4742 break;
4743
4744 relocation = (splt->output_section->vma
4745 + splt->output_offset
4746 + h->plt.offset + 4)
4747 - (input_section->output_section->vma
4748 + input_section->output_offset
4749 + rel->r_offset);
4750
4751 break;
4752
4753 case R_NDS32_GOTPC20:
4754 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4755 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
4756 relocation = elf_gp (output_bfd);
4757 break;
4758
4759 case R_NDS32_GOTPC_HI20:
4760 case R_NDS32_GOTPC_LO12:
4761 {
4762 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
4763 bl .+4
4764 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
4765 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4766 or
4767 bl .+4
4768 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
4769 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
4770 */
4771 relocation = elf_gp (output_bfd);
4772 relocation -= (input_section->output_section->vma
4773 + input_section->output_offset + rel->r_offset);
4774 break;
4775 }
4776
4777 case R_NDS32_GOT20:
4778 /* Fall through. */
4779 case R_NDS32_GOT_HI20:
4780 case R_NDS32_GOT_LO12:
4781 case R_NDS32_GOT_LO15:
4782 case R_NDS32_GOT_LO19:
4783 /* Relocation is to the entry for this symbol in the global
4784 offset table. */
4785 BFD_ASSERT (sgot != NULL);
4786
4787 if (h != NULL)
4788 {
4789 bfd_boolean dyn;
4790 bfd_vma off;
4791
4792 off = h->got.offset;
4793 BFD_ASSERT (off != (bfd_vma) - 1);
4794 dyn = htab->root.dynamic_sections_created;
4795 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
4796 || (info->shared
4797 && (info->symbolic
4798 || h->dynindx == -1
4799 || h->forced_local) && h->def_regular))
4800 {
4801 /* This is actually a static link, or it is a
4802 -Bsymbolic link and the symbol is defined
4803 locally, or the symbol was forced to be local
4804 because of a version file. We must initialize
4805 this entry in the global offset table. Since the
4806 offset must always be a multiple of 4, we use the
4807 least significant bit to record whether we have
4808 initialized it already.
4809
4810 When doing a dynamic link, we create a .rela.got
4811 relocation entry to initialize the value. This
4812 is done in the finish_dynamic_symbol routine. */
4813 if ((off & 1) != 0)
4814 off &= ~1;
4815 else
4816 {
4817 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4818 h->got.offset |= 1;
4819 }
4820 }
4821 relocation = sgot->output_section->vma + sgot->output_offset + off
4822 - elf_gp (output_bfd);
4823 }
4824 else
4825 {
4826 bfd_vma off;
4827 bfd_byte *loc;
4828
4829 BFD_ASSERT (local_got_offsets != NULL
4830 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
4831
4832 off = local_got_offsets[r_symndx];
4833
4834 /* The offset must always be a multiple of 4. We use
4835 the least significant bit to record whether we have
4836 already processed this entry. */
4837 if ((off & 1) != 0)
4838 off &= ~1;
4839 else
4840 {
4841 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4842
4843 if (info->shared)
4844 {
4845 asection *srelgot;
4846 Elf_Internal_Rela outrel;
4847
4848 /* We need to generate a R_NDS32_RELATIVE reloc
4849 for the dynamic linker. */
4850 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
4851 BFD_ASSERT (srelgot != NULL);
4852
4853 outrel.r_offset = (elf_gp (output_bfd)
4854 + sgot->output_offset + off);
4855 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4856 outrel.r_addend = relocation;
4857 loc = srelgot->contents;
4858 loc +=
4859 srelgot->reloc_count * sizeof (Elf32_External_Rela);
4860 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4861 ++srelgot->reloc_count;
4862 }
4863 local_got_offsets[r_symndx] |= 1;
4864 }
4865 relocation = sgot->output_section->vma + sgot->output_offset + off
4866 - elf_gp (output_bfd);
4867 }
4868
4869 break;
4870
4871 case R_NDS32_16_RELA:
4872 case R_NDS32_20_RELA:
4873 case R_NDS32_5_RELA:
4874 case R_NDS32_32_RELA:
4875 case R_NDS32_9_PCREL_RELA:
4876 case R_NDS32_WORD_9_PCREL_RELA:
4877 case R_NDS32_10_UPCREL_RELA:
4878 case R_NDS32_15_PCREL_RELA:
4879 case R_NDS32_17_PCREL_RELA:
4880 case R_NDS32_25_PCREL_RELA:
4881 case R_NDS32_HI20_RELA:
4882 case R_NDS32_LO12S3_RELA:
4883 case R_NDS32_LO12S2_RELA:
4884 case R_NDS32_LO12S2_DP_RELA:
4885 case R_NDS32_LO12S2_SP_RELA:
4886 case R_NDS32_LO12S1_RELA:
4887 case R_NDS32_LO12S0_RELA:
4888 case R_NDS32_LO12S0_ORI_RELA:
4889 if (info->shared && r_symndx != 0
4890 && (input_section->flags & SEC_ALLOC) != 0
4891 && (eliminate_gc_relocs == 0
4892 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
4893 && ((r_type != R_NDS32_9_PCREL_RELA
4894 && r_type != R_NDS32_WORD_9_PCREL_RELA
4895 && r_type != R_NDS32_10_UPCREL_RELA
4896 && r_type != R_NDS32_15_PCREL_RELA
4897 && r_type != R_NDS32_17_PCREL_RELA
4898 && r_type != R_NDS32_25_PCREL_RELA
4899 && !(r_type == R_NDS32_32_RELA
4900 && strcmp (input_section->name, ".eh_frame") == 0))
4901 || (h != NULL && h->dynindx != -1
4902 && (!info->symbolic || !h->def_regular))))
4903 {
4904 Elf_Internal_Rela outrel;
4905 bfd_boolean skip, relocate;
4906 bfd_byte *loc;
4907
4908 /* When generating a shared object, these relocations
4909 are copied into the output file to be resolved at run
4910 time. */
4911
4912 if (sreloc == NULL)
4913 {
4914 const char *name;
4915
4916 name = bfd_elf_string_from_elf_section
4917 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
4918 elf_section_data (input_section)->rela.hdr->sh_name);
4919 if (name == NULL)
4920 return FALSE;
4921
4922 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
4923 && strcmp (bfd_get_section_name (input_bfd,
4924 input_section),
4925 name + 5) == 0);
4926
4927 sreloc = bfd_get_section_by_name (dynobj, name);
4928 BFD_ASSERT (sreloc != NULL);
4929 }
4930
4931 skip = FALSE;
4932 relocate = FALSE;
4933
4934 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4935 info,
4936 input_section,
4937 rel->r_offset);
4938 if (outrel.r_offset == (bfd_vma) - 1)
4939 skip = TRUE;
4940 else if (outrel.r_offset == (bfd_vma) - 2)
4941 skip = TRUE, relocate = TRUE;
4942 outrel.r_offset += (input_section->output_section->vma
4943 + input_section->output_offset);
4944
4945 if (skip)
4946 memset (&outrel, 0, sizeof outrel);
4947 else if (r_type == R_NDS32_17_PCREL_RELA
4948 || r_type == R_NDS32_15_PCREL_RELA
4949 || r_type == R_NDS32_25_PCREL_RELA)
4950 {
4951 BFD_ASSERT (h != NULL && h->dynindx != -1);
4952 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4953 outrel.r_addend = rel->r_addend;
4954 }
4955 else
4956 {
4957 /* h->dynindx may be -1 if this symbol was marked to
4958 become local. */
4959 if (h == NULL
4960 || ((info->symbolic || h->dynindx == -1)
4961 && h->def_regular))
4962 {
4963 relocate = TRUE;
4964 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
4965 outrel.r_addend = relocation + rel->r_addend;
4966 }
4967 else
4968 {
4969 BFD_ASSERT (h->dynindx != -1);
4970 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
4971 outrel.r_addend = rel->r_addend;
4972 }
4973 }
4974
4975 loc = sreloc->contents;
4976 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
4977 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4978 ++sreloc->reloc_count;
4979
4980 /* If this reloc is against an external symbol, we do
4981 not want to fiddle with the addend. Otherwise, we
4982 need to include the symbol value so that it becomes
4983 an addend for the dynamic reloc. */
4984 if (!relocate)
4985 continue;
4986 }
4987 break;
4988
4989 case R_NDS32_25_ABS_RELA:
4990 if (info->shared)
4991 {
4992 (*_bfd_error_handler)
4993 (_("%s: warning: cannot deal R_NDS32_25_ABS_RELA in shared "
4994 "mode."), bfd_get_filename (input_bfd));
4995 return FALSE;
4996 }
4997 break;
4998
4999 case R_NDS32_9_PCREL:
5000 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5001 contents, offset,
5002 sec, relocation, addend);
5003 goto check_reloc;
5004
5005 case R_NDS32_HI20:
5006 {
5007 Elf_Internal_Rela *lorel;
5008
5009 /* We allow an arbitrary number of HI20 relocs before the
5010 LO12 reloc. This permits gcc to emit the HI and LO relocs
5011 itself. */
5012 for (lorel = rel + 1;
5013 (lorel < relend
5014 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5015 continue;
5016 if (lorel < relend
5017 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5018 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5019 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5020 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5021 {
5022 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5023 contents, relocation + addend);
5024 r = bfd_reloc_ok;
5025 }
5026 else
5027 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5028 contents, offset, relocation,
5029 addend);
5030 }
5031
5032 goto check_reloc;
5033
5034 case R_NDS32_GOT17S2_RELA:
5035 case R_NDS32_GOT15S2_RELA:
5036 {
5037 bfd_vma off;
5038
5039 BFD_ASSERT (sgot != NULL);
5040
5041 if (h != NULL)
5042 {
5043 bfd_boolean dyn;
5044
5045 off = h->got.offset;
5046 BFD_ASSERT (off != (bfd_vma) - 1);
5047
5048 dyn = htab->root.dynamic_sections_created;
5049 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5050 (dyn, info->shared, h) || (info->shared
5051 && (info->symbolic
5052 || h->dynindx == -1
5053 || h->forced_local)
5054 && h->def_regular))
5055 {
5056 /* This is actually a static link, or it is a
5057 -Bsymbolic link and the symbol is defined
5058 locally, or the symbol was forced to be local
5059 because of a version file. We must initialize
5060 this entry in the global offset table. Since the
5061 offset must always be a multiple of 4, we use the
5062 least significant bit to record whether we have
5063 initialized it already.
5064
5065 When doing a dynamic link, we create a .rela.got
5066 relocation entry to initialize the value. This
5067 is done in the finish_dynamic_symbol routine. */
5068 if ((off & 1) != 0)
5069 off &= ~1;
5070 else
5071 {
5072 bfd_put_32 (output_bfd, relocation,
5073 sgot->contents + off);
5074 h->got.offset |= 1;
5075 }
5076 }
5077 }
5078 else
5079 {
5080 bfd_byte *loc;
5081
5082 BFD_ASSERT (local_got_offsets != NULL
5083 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5084
5085 off = local_got_offsets[r_symndx];
5086
5087 /* The offset must always be a multiple of 4. We use
5088 the least significant bit to record whether we have
5089 already processed this entry. */
5090 if ((off & 1) != 0)
5091 off &= ~1;
5092 else
5093 {
5094 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5095
5096 if (info->shared)
5097 {
5098 asection *srelgot;
5099 Elf_Internal_Rela outrel;
5100
5101 /* We need to generate a R_NDS32_RELATIVE reloc
5102 for the dynamic linker. */
5103 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5104 BFD_ASSERT (srelgot != NULL);
5105
5106 outrel.r_offset = (elf_gp (output_bfd)
5107 + sgot->output_offset + off);
5108 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5109 outrel.r_addend = relocation;
5110 loc = srelgot->contents;
5111 loc +=
5112 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5113 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5114 ++srelgot->reloc_count;
5115 }
5116 local_got_offsets[r_symndx] |= 1;
5117 }
5118 }
5119 relocation = sgot->output_section->vma + sgot->output_offset + off
5120 - elf_gp (output_bfd);
5121 }
5122 if (relocation & align)
5123 {
5124 /* Incorrect alignment. */
5125 (*_bfd_error_handler)
5126 (_("%B: warning: unaligned access to GOT entry."), input_bfd);
5127 ret = FALSE;
5128 r = bfd_reloc_dangerous;
5129 goto check_reloc;
5130 }
5131 break;
5132
5133 case R_NDS32_SDA16S3_RELA:
5134 case R_NDS32_SDA15S3_RELA:
5135 case R_NDS32_SDA15S3:
5136 align = 0x7;
5137 goto handle_sda;
5138
5139 case R_NDS32_SDA17S2_RELA:
5140 case R_NDS32_SDA15S2_RELA:
5141 case R_NDS32_SDA12S2_SP_RELA:
5142 case R_NDS32_SDA12S2_DP_RELA:
5143 case R_NDS32_SDA15S2:
5144 case R_NDS32_SDA_FP7U2_RELA:
5145 align = 0x3;
5146 goto handle_sda;
5147
5148 case R_NDS32_SDA18S1_RELA:
5149 case R_NDS32_SDA15S1_RELA:
5150 case R_NDS32_SDA15S1:
5151 align = 0x1;
5152 goto handle_sda;
5153
5154 case R_NDS32_SDA19S0_RELA:
5155 case R_NDS32_SDA15S0_RELA:
5156 case R_NDS32_SDA15S0:
5157 {
5158 align = 0x0;
5159 handle_sda:
5160 BFD_ASSERT (sec != NULL);
5161
5162 /* If the symbol is in the abs section, the out_bfd will be null.
5163 This happens when the relocation has a symbol@GOTOFF. */
5164 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5165 if (r != bfd_reloc_ok)
5166 {
5167 (*_bfd_error_handler)
5168 (_("%B: warning: relocate SDA_BASE failed."), input_bfd);
5169 ret = FALSE;
5170 goto check_reloc;
5171 }
5172
5173 /* At this point `relocation' contains the object's
5174 address. */
5175 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5176 {
5177 relocation -= fpbase_addr;
5178 }
5179 else
5180 relocation -= gp;
5181 /* Now it contains the offset from _SDA_BASE_. */
5182
5183 /* Make sure alignment is correct. */
5184
5185 if (relocation & align)
5186 {
5187 /* Incorrect alignment. */
5188 (*_bfd_error_handler)
5189 (_("%B(%A): warning: unaligned small data access of type %d."),
5190 input_bfd, input_section, r_type);
5191 ret = FALSE;
5192 goto check_reloc;
5193 }
5194 }
5195
5196 break;
5197 case R_NDS32_17IFC_PCREL_RELA:
5198 case R_NDS32_10IFCU_PCREL_RELA:
5199 /* do nothing */
5200 break;
5201
5202 case R_NDS32_TLS_LE_HI20:
5203 case R_NDS32_TLS_LE_LO12:
5204 case R_NDS32_TLS_LE_20:
5205 case R_NDS32_TLS_LE_15S0:
5206 case R_NDS32_TLS_LE_15S1:
5207 case R_NDS32_TLS_LE_15S2:
5208 if (elf_hash_table (info)->tls_sec != NULL)
5209 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5210 break;
5211 case R_NDS32_TLS_IE_HI20:
5212 case R_NDS32_TLS_IE_LO12S2:
5213 {
5214 /* Relocation is to the entry for this symbol in the global
5215 offset table. */
5216 unsigned int tls_type;
5217 asection *srelgot;
5218 Elf_Internal_Rela outrel;
5219 bfd_vma off;
5220 bfd_byte *loc;
5221 int indx = 0;
5222
5223 BFD_ASSERT (sgot != NULL);
5224 if (h != NULL)
5225 {
5226 bfd_boolean dyn;
5227
5228 off = h->got.offset;
5229 BFD_ASSERT (off != (bfd_vma) - 1);
5230 dyn = htab->root.dynamic_sections_created;
5231 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5232 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
5233 && (!info->shared
5234 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5235 indx = h->dynindx;
5236 }
5237 else
5238 {
5239 /* Never happen currently. */
5240 BFD_ASSERT (local_got_offsets != NULL
5241 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5242
5243 off = local_got_offsets[r_symndx];
5244
5245 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5246 }
5247 relocation = sgot->output_section->vma + sgot->output_offset + off;
5248
5249 if (r_type == R_NDS32_TLS_IE_LO12S2)
5250 break;
5251
5252 /* The offset must always be a multiple of 4. We use
5253 the least significant bit to record whether we have
5254 already processed this entry. */
5255 if ((off & 1) != 0)
5256 off &= ~1;
5257 else
5258 {
5259 bfd_boolean need_relocs = FALSE;
5260 srelgot = htab->srelgot;
5261 if ((info->shared || indx != 0)
5262 && (h == NULL
5263 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5264 || h->root.type != bfd_link_hash_undefweak))
5265 {
5266 need_relocs = TRUE;
5267 BFD_ASSERT (srelgot != NULL);
5268 }
5269 if (tls_type & GOT_TLS_IE)
5270 {
5271 if (need_relocs)
5272 {
5273 if (h->dynindx == 0)
5274 outrel.r_addend = relocation - dtpoff_base (info);
5275 else
5276 outrel.r_addend = 0;
5277 outrel.r_offset = (sgot->output_section->vma
5278 + sgot->output_offset
5279 + off);
5280 outrel.r_info =
5281 ELF32_R_INFO (h->dynindx, R_NDS32_TLS_TPOFF);
5282
5283 loc = srelgot->contents;
5284 loc +=
5285 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5286 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5287 ++srelgot->reloc_count;
5288 }
5289 else
5290 bfd_put_32 (output_bfd, h->root.u.def.value - TP_OFFSET,
5291 sgot->contents + off);
5292 }
5293 }
5294 }
5295 break;
5296
5297 /* DON'T fall through. */
5298
5299 default:
5300 /* OLD_NDS32_RELOC. */
5301
5302 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5303 contents, offset, relocation, addend);
5304 goto check_reloc;
5305 }
5306
5307 switch ((int) r_type)
5308 {
5309 case R_NDS32_20_RELA:
5310 case R_NDS32_5_RELA:
5311 case R_NDS32_9_PCREL_RELA:
5312 case R_NDS32_WORD_9_PCREL_RELA:
5313 case R_NDS32_10_UPCREL_RELA:
5314 case R_NDS32_15_PCREL_RELA:
5315 case R_NDS32_17_PCREL_RELA:
5316 case R_NDS32_25_PCREL_RELA:
5317 case R_NDS32_25_ABS_RELA:
5318 case R_NDS32_HI20_RELA:
5319 case R_NDS32_LO12S3_RELA:
5320 case R_NDS32_LO12S2_RELA:
5321 case R_NDS32_LO12S2_DP_RELA:
5322 case R_NDS32_LO12S2_SP_RELA:
5323 case R_NDS32_LO12S1_RELA:
5324 case R_NDS32_LO12S0_RELA:
5325 case R_NDS32_LO12S0_ORI_RELA:
5326 case R_NDS32_SDA16S3_RELA:
5327 case R_NDS32_SDA17S2_RELA:
5328 case R_NDS32_SDA18S1_RELA:
5329 case R_NDS32_SDA19S0_RELA:
5330 case R_NDS32_SDA15S3_RELA:
5331 case R_NDS32_SDA15S2_RELA:
5332 case R_NDS32_SDA12S2_DP_RELA:
5333 case R_NDS32_SDA12S2_SP_RELA:
5334 case R_NDS32_SDA15S1_RELA:
5335 case R_NDS32_SDA15S0_RELA:
5336 case R_NDS32_SDA_FP7U2_RELA:
5337 case R_NDS32_9_PLTREL:
5338 case R_NDS32_25_PLTREL:
5339 case R_NDS32_GOT20:
5340 case R_NDS32_GOT_HI20:
5341 case R_NDS32_GOT_LO12:
5342 case R_NDS32_GOT_LO15:
5343 case R_NDS32_GOT_LO19:
5344 case R_NDS32_GOT15S2_RELA:
5345 case R_NDS32_GOT17S2_RELA:
5346 case R_NDS32_GOTPC20:
5347 case R_NDS32_GOTPC_HI20:
5348 case R_NDS32_GOTPC_LO12:
5349 case R_NDS32_GOTOFF:
5350 case R_NDS32_GOTOFF_HI20:
5351 case R_NDS32_GOTOFF_LO12:
5352 case R_NDS32_GOTOFF_LO15:
5353 case R_NDS32_GOTOFF_LO19:
5354 case R_NDS32_PLTREL_HI20:
5355 case R_NDS32_PLTREL_LO12:
5356 case R_NDS32_PLT_GOTREL_HI20:
5357 case R_NDS32_PLT_GOTREL_LO12:
5358 case R_NDS32_PLT_GOTREL_LO15:
5359 case R_NDS32_PLT_GOTREL_LO19:
5360 case R_NDS32_PLT_GOTREL_LO20:
5361 case R_NDS32_17IFC_PCREL_RELA:
5362 case R_NDS32_10IFCU_PCREL_RELA:
5363 case R_NDS32_TLS_LE_HI20:
5364 case R_NDS32_TLS_LE_LO12:
5365 case R_NDS32_TLS_IE_HI20:
5366 case R_NDS32_TLS_IE_LO12S2:
5367 case R_NDS32_TLS_LE_20:
5368 case R_NDS32_TLS_LE_15S0:
5369 case R_NDS32_TLS_LE_15S1:
5370 case R_NDS32_TLS_LE_15S2:
5371 /* Instruction related relocs must handle endian properly. */
5372 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
5373 r = nds32_elf_final_link_relocate (howto, input_bfd,
5374 input_section, contents,
5375 rel->r_offset, relocation,
5376 rel->r_addend);
5377 break;
5378
5379 default:
5380 /* All other relocs can use default handler. */
5381 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5382 contents, rel->r_offset,
5383 relocation, rel->r_addend);
5384 break;
5385 }
5386
5387 check_reloc:
5388
5389 if (r != bfd_reloc_ok)
5390 {
5391 /* FIXME: This should be generic enough to go in a utility. */
5392 const char *name;
5393
5394 if (h != NULL)
5395 name = h->root.root.string;
5396 else
5397 {
5398 name = bfd_elf_string_from_elf_section
5399 (input_bfd, symtab_hdr->sh_link, sym->st_name);
5400 if (name == NULL || *name == '\0')
5401 name = bfd_section_name (input_bfd, sec);
5402 }
5403
5404 if (errmsg != NULL)
5405 goto common_error;
5406
5407 switch (r)
5408 {
5409 case bfd_reloc_overflow:
5410 if (!((*info->callbacks->reloc_overflow)
5411 (info, (h ? &h->root : NULL), name, howto->name,
5412 (bfd_vma) 0, input_bfd, input_section, offset)))
5413 return FALSE;
5414 break;
5415
5416 case bfd_reloc_undefined:
5417 if (!((*info->callbacks->undefined_symbol)
5418 (info, name, input_bfd, input_section, offset, TRUE)))
5419 return FALSE;
5420 break;
5421
5422 case bfd_reloc_outofrange:
5423 errmsg = _("internal error: out of range error");
5424 goto common_error;
5425
5426 case bfd_reloc_notsupported:
5427 errmsg = _("internal error: unsupported relocation error");
5428 goto common_error;
5429
5430 case bfd_reloc_dangerous:
5431 errmsg = _("internal error: dangerous error");
5432 goto common_error;
5433
5434 default:
5435 errmsg = _("internal error: unknown error");
5436 /* Fall through. */
5437
5438 common_error:
5439 if (!((*info->callbacks->warning)
5440 (info, errmsg, name, input_bfd, input_section, offset)))
5441 return FALSE;
5442 break;
5443 }
5444 }
5445 }
5446
5447 return ret;
5448 }
5449
5450 /* Finish up dynamic symbol handling. We set the contents of various
5451 dynamic sections here. */
5452
5453 static bfd_boolean
5454 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5455 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
5456 {
5457 struct elf_nds32_link_hash_table *htab;
5458 bfd_byte *loc;
5459
5460 htab = nds32_elf_hash_table (info);
5461
5462 if (h->plt.offset != (bfd_vma) - 1)
5463 {
5464 asection *splt;
5465 asection *sgot;
5466 asection *srela;
5467
5468 bfd_vma plt_index;
5469 bfd_vma got_offset;
5470 bfd_vma local_plt_offset;
5471 Elf_Internal_Rela rela;
5472
5473 /* This symbol has an entry in the procedure linkage table. Set
5474 it up. */
5475
5476 BFD_ASSERT (h->dynindx != -1);
5477
5478 splt = htab->splt;
5479 sgot = htab->sgotplt;
5480 srela = htab->srelplt;
5481 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
5482
5483 /* Get the index in the procedure linkage table which
5484 corresponds to this symbol. This is the index of this symbol
5485 in all the symbols for which we are making plt entries. The
5486 first entry in the procedure linkage table is reserved. */
5487 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
5488
5489 /* Get the offset into the .got table of the entry that
5490 corresponds to this function. Each .got entry is 4 bytes.
5491 The first three are reserved. */
5492 got_offset = (plt_index + 3) * 4;
5493
5494 /* Fill in the entry in the procedure linkage table. */
5495 if (!info->shared)
5496 {
5497 unsigned long insn;
5498
5499 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
5500 + sgot->output_offset + got_offset) >> 12)
5501 & 0xfffff);
5502 bfd_putb32 (insn, splt->contents + h->plt.offset);
5503
5504 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
5505 + sgot->output_offset + got_offset) & 0x0fff)
5506 >> 2);
5507 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5508
5509 insn = PLT_ENTRY_WORD2;
5510 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5511
5512 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
5513 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5514
5515 insn = PLT_ENTRY_WORD4
5516 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
5517 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5518 local_plt_offset = 12;
5519 }
5520 else
5521 {
5522 /* sda_base must be set at this time. */
5523 unsigned long insn;
5524 long offset;
5525
5526 /* FIXME, sda_base is 65536, it will damage opcode. */
5527 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5528 offset = sgot->output_section->vma + sgot->output_offset + got_offset
5529 - elf_gp (output_bfd);
5530 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
5531 bfd_putb32 (insn, splt->contents + h->plt.offset);
5532
5533 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
5534 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
5535
5536 insn = PLT_PIC_ENTRY_WORD2;
5537 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
5538
5539 insn = PLT_PIC_ENTRY_WORD3;
5540 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
5541
5542 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
5543 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
5544
5545 insn = PLT_PIC_ENTRY_WORD5
5546 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
5547 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
5548
5549 local_plt_offset = 16;
5550 }
5551
5552 /* Fill in the entry in the global offset table,
5553 so it will fall through to the next instruction for the first time. */
5554 bfd_put_32 (output_bfd,
5555 (splt->output_section->vma + splt->output_offset
5556 + h->plt.offset + local_plt_offset),
5557 sgot->contents + got_offset);
5558
5559 /* Fill in the entry in the .rela.plt section. */
5560 rela.r_offset = (sgot->output_section->vma
5561 + sgot->output_offset + got_offset);
5562 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
5563 rela.r_addend = 0;
5564 loc = srela->contents;
5565 loc += plt_index * sizeof (Elf32_External_Rela);
5566 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5567
5568 if (!h->def_regular)
5569 {
5570 /* Mark the symbol as undefined, rather than as defined in
5571 the .plt section. Leave the value alone. */
5572 sym->st_shndx = SHN_UNDEF;
5573 if (!h->ref_regular_nonweak)
5574 sym->st_value = 0;
5575 }
5576 }
5577
5578 if (h->got.offset != (bfd_vma) - 1)
5579 {
5580 asection *sgot;
5581 asection *srela;
5582 Elf_Internal_Rela rela;
5583
5584 /* This symbol has an entry in the global offset table.
5585 Set it up. */
5586
5587 sgot = htab->sgot;
5588 srela = htab->srelgot;
5589 BFD_ASSERT (sgot != NULL && srela != NULL);
5590
5591 rela.r_offset = (sgot->output_section->vma
5592 + sgot->output_offset + (h->got.offset & ~1));
5593
5594 /* If this is a -Bsymbolic link, and the symbol is defined
5595 locally, we just want to emit a RELATIVE reloc. Likewise if
5596 the symbol was forced to be local because of a version file.
5597 The entry in the global offset table will already have been
5598 initialized in the relocate_section function. */
5599 if (info->shared
5600 && (info->symbolic
5601 || h->dynindx == -1 || h->forced_local) && h->def_regular)
5602 {
5603 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5604 rela.r_addend = (h->root.u.def.value
5605 + h->root.u.def.section->output_section->vma
5606 + h->root.u.def.section->output_offset);
5607 }
5608 else
5609 {
5610 BFD_ASSERT ((h->got.offset & 1) == 0);
5611 bfd_put_32 (output_bfd, (bfd_vma) 0,
5612 sgot->contents + h->got.offset);
5613 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
5614 rela.r_addend = 0;
5615 }
5616
5617 loc = srela->contents;
5618 loc += srela->reloc_count * sizeof (Elf32_External_Rela);
5619 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5620 ++srela->reloc_count;
5621 }
5622
5623 if (h->needs_copy)
5624 {
5625 asection *s;
5626 Elf_Internal_Rela rela;
5627
5628 /* This symbols needs a copy reloc. Set it up. */
5629
5630 BFD_ASSERT (h->dynindx != -1
5631 && (h->root.type == bfd_link_hash_defined
5632 || h->root.type == bfd_link_hash_defweak));
5633
5634 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
5635 BFD_ASSERT (s != NULL);
5636
5637 rela.r_offset = (h->root.u.def.value
5638 + h->root.u.def.section->output_section->vma
5639 + h->root.u.def.section->output_offset);
5640 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
5641 rela.r_addend = 0;
5642 loc = s->contents;
5643 loc += s->reloc_count * sizeof (Elf32_External_Rela);
5644 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
5645 ++s->reloc_count;
5646 }
5647
5648 /* Mark some specially defined symbols as absolute. */
5649 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
5650 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
5651 sym->st_shndx = SHN_ABS;
5652
5653 return TRUE;
5654 }
5655
5656
5657 /* Finish up the dynamic sections. */
5658
5659 static bfd_boolean
5660 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
5661 {
5662 struct elf_nds32_link_hash_table *htab;
5663 bfd *dynobj;
5664 asection *sdyn;
5665 asection *sgot;
5666
5667 htab = nds32_elf_hash_table (info);
5668 dynobj = htab->root.dynobj;
5669
5670 sgot = htab->sgotplt;
5671 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
5672
5673 if (htab->root.dynamic_sections_created)
5674 {
5675 asection *splt;
5676 Elf32_External_Dyn *dyncon, *dynconend;
5677
5678 BFD_ASSERT (sgot != NULL && sdyn != NULL);
5679
5680 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5681 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5682
5683 for (; dyncon < dynconend; dyncon++)
5684 {
5685 Elf_Internal_Dyn dyn;
5686 asection *s;
5687
5688 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5689
5690 switch (dyn.d_tag)
5691 {
5692 default:
5693 break;
5694
5695 case DT_PLTGOT:
5696 /* name = ".got"; */
5697 s = htab->sgot->output_section;
5698 goto get_vma;
5699 case DT_JMPREL:
5700 s = htab->srelplt->output_section;
5701 get_vma:
5702 BFD_ASSERT (s != NULL);
5703 dyn.d_un.d_ptr = s->vma;
5704 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5705 break;
5706
5707 case DT_PLTRELSZ:
5708 s = htab->srelplt->output_section;
5709 BFD_ASSERT (s != NULL);
5710 dyn.d_un.d_val = s->size;
5711 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5712 break;
5713
5714 case DT_RELASZ:
5715 /* My reading of the SVR4 ABI indicates that the
5716 procedure linkage table relocs (DT_JMPREL) should be
5717 included in the overall relocs (DT_RELA). This is
5718 what Solaris does. However, UnixWare can not handle
5719 that case. Therefore, we override the DT_RELASZ entry
5720 here to make it not include the JMPREL relocs. Since
5721 the linker script arranges for .rela.plt to follow all
5722 other relocation sections, we don't have to worry
5723 about changing the DT_RELA entry. */
5724 if (htab->srelplt != NULL)
5725 {
5726 s = htab->srelplt->output_section;
5727 dyn.d_un.d_val -= s->size;
5728 }
5729 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5730 break;
5731 }
5732 }
5733
5734 /* Fill in the first entry in the procedure linkage table. */
5735 splt = htab->splt;
5736 if (splt && splt->size > 0)
5737 {
5738 if (info->shared)
5739 {
5740 unsigned long insn;
5741 long offset;
5742
5743 /* FIXME, sda_base is 65536, it will damage opcode. */
5744 /* insn = PLT_PIC_ENTRY_WORD0 + (((got_offset - sda_base) >> 2) & 0x7fff); */
5745 offset = sgot->output_section->vma + sgot->output_offset + 4
5746 - elf_gp (output_bfd);
5747 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
5748 bfd_putb32 (insn, splt->contents);
5749
5750 /* insn = PLT0_PIC_ENTRY_WORD0 | (((8 - sda_base) >> 2) & 0x7fff) ; */
5751 /* here has a typo? */
5752 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
5753 bfd_putb32 (insn, splt->contents + 4);
5754
5755 insn = PLT0_PIC_ENTRY_WORD2;
5756 bfd_putb32 (insn, splt->contents + 8);
5757
5758 insn = PLT0_PIC_ENTRY_WORD3;
5759 bfd_putb32 (insn, splt->contents + 12);
5760
5761 insn = PLT0_PIC_ENTRY_WORD4;
5762 bfd_putb32 (insn, splt->contents + 16);
5763
5764 insn = PLT0_PIC_ENTRY_WORD5;
5765 bfd_putb32 (insn, splt->contents + 20);
5766 }
5767 else
5768 {
5769 unsigned long insn;
5770 unsigned long addr;
5771
5772 /* addr = .got + 4 */
5773 addr = sgot->output_section->vma + sgot->output_offset + 4;
5774 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
5775 bfd_putb32 (insn, splt->contents);
5776
5777 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
5778 bfd_putb32 (insn, splt->contents + 4);
5779
5780 insn = PLT0_ENTRY_WORD2;
5781 bfd_putb32 (insn, splt->contents + 8);
5782
5783 insn = PLT0_ENTRY_WORD3;
5784 bfd_putb32 (insn, splt->contents + 12);
5785
5786 insn = PLT0_ENTRY_WORD4;
5787 bfd_putb32 (insn, splt->contents + 16);
5788 }
5789
5790 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
5791 PLT_ENTRY_SIZE;
5792 }
5793 }
5794
5795 /* Fill in the first three entries in the global offset table. */
5796 if (sgot && sgot->size > 0)
5797 {
5798 if (sdyn == NULL)
5799 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5800 else
5801 bfd_put_32 (output_bfd,
5802 sdyn->output_section->vma + sdyn->output_offset,
5803 sgot->contents);
5804 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5805 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5806
5807 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5808 }
5809
5810 return TRUE;
5811 }
5812 \f
5813
5814 /* Set the right machine number. */
5815
5816 static bfd_boolean
5817 nds32_elf_object_p (bfd *abfd)
5818 {
5819 static unsigned int cur_arch = 0;
5820
5821 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
5822 {
5823 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
5824 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
5825 }
5826
5827 switch (cur_arch)
5828 {
5829 default:
5830 case E_N1_ARCH:
5831 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
5832 break;
5833 case E_N1H_ARCH:
5834 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
5835 break;
5836 case E_NDS_ARCH_STAR_V2_0:
5837 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
5838 break;
5839 case E_NDS_ARCH_STAR_V3_0:
5840 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
5841 break;
5842 case E_NDS_ARCH_STAR_V3_M:
5843 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
5844 break;
5845 }
5846
5847 return TRUE;
5848 }
5849
5850 /* Store the machine number in the flags field. */
5851
5852 static void
5853 nds32_elf_final_write_processing (bfd *abfd,
5854 bfd_boolean linker ATTRIBUTE_UNUSED)
5855 {
5856 unsigned long val;
5857 static unsigned int cur_mach = 0;
5858
5859 if (bfd_mach_n1 != bfd_get_mach (abfd))
5860 {
5861 cur_mach = bfd_get_mach (abfd);
5862 }
5863
5864 switch (cur_mach)
5865 {
5866 case bfd_mach_n1:
5867 /* Only happen when object is empty, since the case is abandon. */
5868 val = E_N1_ARCH;
5869 val |= E_NDS_ABI_AABI;
5870 val |= E_NDS32_ELF_VER_1_4;
5871 break;
5872 case bfd_mach_n1h:
5873 val = E_N1H_ARCH;
5874 break;
5875 case bfd_mach_n1h_v2:
5876 val = E_NDS_ARCH_STAR_V2_0;
5877 break;
5878 case bfd_mach_n1h_v3:
5879 val = E_NDS_ARCH_STAR_V3_0;
5880 break;
5881 case bfd_mach_n1h_v3m:
5882 val = E_NDS_ARCH_STAR_V3_M;
5883 break;
5884 default:
5885 val = 0;
5886 break;
5887 }
5888
5889 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
5890 elf_elfheader (abfd)->e_flags |= val;
5891 }
5892
5893 /* Function to keep NDS32 specific file flags. */
5894
5895 static bfd_boolean
5896 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
5897 {
5898 BFD_ASSERT (!elf_flags_init (abfd)
5899 || elf_elfheader (abfd)->e_flags == flags);
5900
5901 elf_elfheader (abfd)->e_flags = flags;
5902 elf_flags_init (abfd) = TRUE;
5903 return TRUE;
5904 }
5905
5906 static unsigned int
5907 convert_e_flags (unsigned int e_flags, unsigned int arch)
5908 {
5909 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
5910 {
5911 /* From 0.9 to 1.0. */
5912 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
5913
5914 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5915 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5916 if (arch == E_NDS_ARCH_STAR_V1_0)
5917 {
5918 /* Done. */
5919 return e_flags;
5920 }
5921 }
5922
5923 /* From 1.0 to 2.0. */
5924 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
5925
5926 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
5927 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
5928
5929 /* Invert E_NDS32_HAS_NO_MAC_INST. */
5930 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
5931 return e_flags;
5932 }
5933
5934 static bfd_boolean
5935 nds32_check_vec_size (bfd *ibfd)
5936 {
5937 static unsigned int nds32_vec_size = 0;
5938
5939 asection *sec_t = NULL;
5940 bfd_byte *contents = NULL;
5941
5942 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
5943
5944 if (sec_t && sec_t->size >= 4)
5945 {
5946 /* Get vec_size in file. */
5947 unsigned int flag_t;
5948
5949 nds32_get_section_contents (ibfd, sec_t, &contents);
5950 flag_t = bfd_get_32 (ibfd, contents);
5951
5952 /* The value could only be 4 or 16. */
5953
5954 if (!nds32_vec_size)
5955 /* Set if not set yet. */
5956 nds32_vec_size = (flag_t & 0x3);
5957 else if (nds32_vec_size != (flag_t & 0x3))
5958 {
5959 (*_bfd_error_handler) (_("%B: ISR vector size mismatch"
5960 " with previous modules, previous %u-byte, current %u-byte"),
5961 ibfd,
5962 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
5963 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
5964 return FALSE;
5965 }
5966 else
5967 /* Only keep the first vec_size section. */
5968 sec_t->flags |= SEC_EXCLUDE;
5969 }
5970
5971 return TRUE;
5972 }
5973
5974 /* Merge backend specific data from an object file to the output
5975 object file when linking. */
5976
5977 static bfd_boolean
5978 nds32_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5979 {
5980 flagword out_flags;
5981 flagword in_flags;
5982 flagword out_16regs;
5983 flagword in_no_mac;
5984 flagword out_no_mac;
5985 flagword in_16regs;
5986 flagword out_version;
5987 flagword in_version;
5988 flagword out_fpu_config;
5989 flagword in_fpu_config;
5990
5991 /* TODO: Revise to use object-attributes instead. */
5992 if (!nds32_check_vec_size (ibfd))
5993 return FALSE;
5994
5995 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
5996 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
5997 return TRUE;
5998
5999 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6000 {
6001 (*_bfd_error_handler)
6002 (_("%B: warning: Endian mismatch with previous modules."), ibfd);
6003
6004 bfd_set_error (bfd_error_bad_value);
6005 return FALSE;
6006 }
6007
6008 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6009 if (in_version == E_NDS32_ELF_VER_1_2)
6010 {
6011 (*_bfd_error_handler)
6012 (_("%B: warning: Older version of object file encountered, "
6013 "Please recompile with current tool chain."), ibfd);
6014 }
6015
6016 /* We may need to merge V1 and V2 arch object files to V2. */
6017 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6018 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6019 {
6020 /* Need to convert version. */
6021 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6022 == E_NDS_ARCH_STAR_RESERVED)
6023 {
6024 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6025 }
6026 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9
6027 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6028 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6029 {
6030 elf_elfheader (obfd)->e_flags =
6031 convert_e_flags (elf_elfheader (obfd)->e_flags,
6032 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6033 }
6034 else
6035 {
6036 elf_elfheader (ibfd)->e_flags =
6037 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6038 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6039 }
6040 }
6041
6042 /* Extract some flags. */
6043 in_flags = elf_elfheader (ibfd)->e_flags
6044 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6045 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6046
6047 /* The following flags need special treatment. */
6048 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6049 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6050 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6051
6052 /* Extract some flags. */
6053 out_flags = elf_elfheader (obfd)->e_flags
6054 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6055 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6056
6057 /* The following flags need special treatment. */
6058 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6059 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6060 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6061 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6062 if (!elf_flags_init (obfd))
6063 {
6064 /* If the input is the default architecture then do not
6065 bother setting the flags for the output architecture,
6066 instead allow future merges to do this. If no future
6067 merges ever set these flags then they will retain their
6068 unitialised values, which surprise surprise, correspond
6069 to the default values. */
6070 if (bfd_get_arch_info (ibfd)->the_default)
6071 return TRUE;
6072
6073 elf_flags_init (obfd) = TRUE;
6074 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6075
6076 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6077 && bfd_get_arch_info (obfd)->the_default)
6078 {
6079 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6080 bfd_get_mach (ibfd));
6081 }
6082
6083 return TRUE;
6084 }
6085
6086 /* Check flag compatibility. */
6087 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6088 {
6089 (*_bfd_error_handler)
6090 (_("%B: error: ABI mismatch with previous modules."), ibfd);
6091
6092 bfd_set_error (bfd_error_bad_value);
6093 return FALSE;
6094 }
6095
6096 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6097 {
6098 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6099 {
6100 (*_bfd_error_handler)
6101 (_("%B: error: Instruction set mismatch with previous modules."), ibfd);
6102
6103 bfd_set_error (bfd_error_bad_value);
6104 return FALSE;
6105 }
6106 }
6107
6108 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6109 and perf ext1 and DIV are mergerd to perf ext1. */
6110 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6111 {
6112 elf_elfheader (obfd)->e_flags =
6113 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6114 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6115 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6116 ? E_NDS32_HAS_EXT_INST : 0)
6117 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6118 ? E_NDS32_HAS_EXT_INST : 0)
6119 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6120 | ((in_version > out_version) ? out_version : in_version);
6121 }
6122 else
6123 {
6124 if (in_version != out_version)
6125 (*_bfd_error_handler) (_("%B: warning: Incompatible elf-versions %s and %s."),
6126 ibfd, nds32_elfver_strtab[out_version],
6127 nds32_elfver_strtab[in_version]);
6128
6129 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6130 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6131 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6132 | (in_version > out_version ? out_version : in_version);
6133 }
6134
6135 return TRUE;
6136 }
6137
6138 /* Display the flags field. */
6139
6140 static bfd_boolean
6141 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6142 {
6143 FILE *file = (FILE *) ptr;
6144
6145 BFD_ASSERT (abfd != NULL && ptr != NULL);
6146
6147 _bfd_elf_print_private_bfd_data (abfd, ptr);
6148
6149 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6150
6151 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6152 {
6153 default:
6154 case E_N1_ARCH:
6155 fprintf (file, _(": n1 instructions"));
6156 break;
6157 case E_N1H_ARCH:
6158 fprintf (file, _(": n1h instructions"));
6159 break;
6160 }
6161
6162 fputc ('\n', file);
6163
6164 return TRUE;
6165 }
6166
6167 static unsigned int
6168 nds32_elf_action_discarded (asection *sec)
6169 {
6170
6171 if (strncmp
6172 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6173 return 0;
6174
6175 return _bfd_elf_default_action_discarded (sec);
6176 }
6177
6178 static asection *
6179 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6180 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6181 Elf_Internal_Sym *sym)
6182 {
6183 if (h != NULL)
6184 switch (ELF32_R_TYPE (rel->r_info))
6185 {
6186 case R_NDS32_GNU_VTINHERIT:
6187 case R_NDS32_GNU_VTENTRY:
6188 case R_NDS32_RELA_GNU_VTINHERIT:
6189 case R_NDS32_RELA_GNU_VTENTRY:
6190 return NULL;
6191 }
6192
6193 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6194 }
6195
6196 static bfd_boolean
6197 nds32_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info, asection *sec,
6198 const Elf_Internal_Rela *relocs)
6199 {
6200 /* Update the got entry reference counts for the section being removed. */
6201 Elf_Internal_Shdr *symtab_hdr;
6202 struct elf_link_hash_entry **sym_hashes;
6203 bfd_signed_vma *local_got_refcounts;
6204 const Elf_Internal_Rela *rel, *relend;
6205
6206 elf_section_data (sec)->local_dynrel = NULL;
6207
6208 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6209 sym_hashes = elf_sym_hashes (abfd);
6210 local_got_refcounts = elf_local_got_refcounts (abfd);
6211
6212 relend = relocs + sec->reloc_count;
6213 for (rel = relocs; rel < relend; rel++)
6214 {
6215 unsigned long r_symndx;
6216 struct elf_link_hash_entry *h = NULL;
6217
6218 r_symndx = ELF32_R_SYM (rel->r_info);
6219 if (r_symndx >= symtab_hdr->sh_info)
6220 {
6221 /* External symbol. */
6222 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6223 while (h->root.type == bfd_link_hash_indirect
6224 || h->root.type == bfd_link_hash_warning)
6225 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6226 }
6227
6228 switch (ELF32_R_TYPE (rel->r_info))
6229 {
6230 case R_NDS32_GOT_HI20:
6231 case R_NDS32_GOT_LO12:
6232 case R_NDS32_GOT_LO15:
6233 case R_NDS32_GOT_LO19:
6234 case R_NDS32_GOT17S2_RELA:
6235 case R_NDS32_GOT15S2_RELA:
6236 case R_NDS32_GOTOFF:
6237 case R_NDS32_GOTOFF_HI20:
6238 case R_NDS32_GOTOFF_LO12:
6239 case R_NDS32_GOTOFF_LO15:
6240 case R_NDS32_GOTOFF_LO19:
6241 case R_NDS32_GOT20:
6242 case R_NDS32_GOTPC_HI20:
6243 case R_NDS32_GOTPC_LO12:
6244 case R_NDS32_GOTPC20:
6245 if (h != NULL)
6246 {
6247 if (h->got.refcount > 0)
6248 h->got.refcount--;
6249 }
6250 else
6251 {
6252 if (local_got_refcounts && local_got_refcounts[r_symndx] > 0)
6253 local_got_refcounts[r_symndx]--;
6254 }
6255 break;
6256
6257 case R_NDS32_16_RELA:
6258 case R_NDS32_20_RELA:
6259 case R_NDS32_5_RELA:
6260 case R_NDS32_32_RELA:
6261 case R_NDS32_HI20_RELA:
6262 case R_NDS32_LO12S3_RELA:
6263 case R_NDS32_LO12S2_RELA:
6264 case R_NDS32_LO12S2_DP_RELA:
6265 case R_NDS32_LO12S2_SP_RELA:
6266 case R_NDS32_LO12S1_RELA:
6267 case R_NDS32_LO12S0_RELA:
6268 case R_NDS32_LO12S0_ORI_RELA:
6269 case R_NDS32_SDA16S3_RELA:
6270 case R_NDS32_SDA17S2_RELA:
6271 case R_NDS32_SDA18S1_RELA:
6272 case R_NDS32_SDA19S0_RELA:
6273 case R_NDS32_SDA15S3_RELA:
6274 case R_NDS32_SDA15S2_RELA:
6275 case R_NDS32_SDA12S2_DP_RELA:
6276 case R_NDS32_SDA12S2_SP_RELA:
6277 case R_NDS32_SDA15S1_RELA:
6278 case R_NDS32_SDA15S0_RELA:
6279 case R_NDS32_SDA_FP7U2_RELA:
6280 case R_NDS32_15_PCREL_RELA:
6281 case R_NDS32_17_PCREL_RELA:
6282 case R_NDS32_25_PCREL_RELA:
6283 if (h != NULL)
6284 {
6285 struct elf_nds32_link_hash_entry *eh;
6286 struct elf_nds32_dyn_relocs **pp;
6287 struct elf_nds32_dyn_relocs *p;
6288
6289 if (!info->shared && h->plt.refcount > 0)
6290 h->plt.refcount -= 1;
6291
6292 eh = (struct elf_nds32_link_hash_entry *) h;
6293
6294 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6295 if (p->sec == sec)
6296 {
6297 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6298 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
6299 || ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA)
6300 p->pc_count -= 1;
6301 p->count -= 1;
6302 if (p->count == 0)
6303 *pp = p->next;
6304 break;
6305 }
6306 }
6307 break;
6308
6309 case R_NDS32_9_PLTREL:
6310 case R_NDS32_25_PLTREL:
6311 if (h != NULL)
6312 {
6313 if (h->plt.refcount > 0)
6314 h->plt.refcount--;
6315 }
6316 break;
6317
6318 default:
6319 break;
6320 }
6321 }
6322
6323 return TRUE;
6324 }
6325
6326 /* Look through the relocs for a section during the first phase.
6327 Since we don't do .gots or .plts, we just need to consider the
6328 virtual table relocs for gc. */
6329
6330 static bfd_boolean
6331 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6332 asection *sec, const Elf_Internal_Rela *relocs)
6333 {
6334 Elf_Internal_Shdr *symtab_hdr;
6335 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6336 const Elf_Internal_Rela *rel;
6337 const Elf_Internal_Rela *rel_end;
6338 struct elf_nds32_link_hash_table *htab;
6339 bfd *dynobj;
6340 asection *sreloc = NULL;
6341
6342 if (info->relocatable)
6343 return TRUE;
6344
6345 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6346 sym_hashes = elf_sym_hashes (abfd);
6347 sym_hashes_end =
6348 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
6349 if (!elf_bad_symtab (abfd))
6350 sym_hashes_end -= symtab_hdr->sh_info;
6351
6352 htab = nds32_elf_hash_table (info);
6353 dynobj = htab->root.dynobj;
6354
6355 rel_end = relocs + sec->reloc_count;
6356 for (rel = relocs; rel < rel_end; rel++)
6357 {
6358 enum elf_nds32_reloc_type r_type;
6359 struct elf_link_hash_entry *h;
6360 unsigned long r_symndx;
6361 int tls_type, old_tls_type;
6362
6363 r_symndx = ELF32_R_SYM (rel->r_info);
6364 r_type = ELF32_R_TYPE (rel->r_info);
6365 if (r_symndx < symtab_hdr->sh_info)
6366 h = NULL;
6367 else
6368 {
6369 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6370 while (h->root.type == bfd_link_hash_indirect
6371 || h->root.type == bfd_link_hash_warning)
6372 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6373 }
6374
6375 /* Some relocs require a global offset table. We create
6376 got section here, since these relocation need got section
6377 and it is not created yet. */
6378 if (htab->sgot == NULL)
6379 {
6380 switch (r_type)
6381 {
6382 case R_NDS32_GOT_HI20:
6383 case R_NDS32_GOT_LO12:
6384 case R_NDS32_GOT_LO15:
6385 case R_NDS32_GOT_LO19:
6386 case R_NDS32_GOT17S2_RELA:
6387 case R_NDS32_GOT15S2_RELA:
6388 case R_NDS32_GOTOFF:
6389 case R_NDS32_GOTOFF_HI20:
6390 case R_NDS32_GOTOFF_LO12:
6391 case R_NDS32_GOTOFF_LO15:
6392 case R_NDS32_GOTOFF_LO19:
6393 case R_NDS32_GOTPC20:
6394 case R_NDS32_GOTPC_HI20:
6395 case R_NDS32_GOTPC_LO12:
6396 case R_NDS32_GOT20:
6397 case R_NDS32_TLS_IE_HI20:
6398 case R_NDS32_TLS_IE_LO12S2:
6399 if (dynobj == NULL)
6400 htab->root.dynobj = dynobj = abfd;
6401 if (!create_got_section (dynobj, info))
6402 return FALSE;
6403 break;
6404
6405 default:
6406 break;
6407 }
6408 }
6409
6410 switch ((int) r_type)
6411 {
6412 case R_NDS32_GOT_HI20:
6413 case R_NDS32_GOT_LO12:
6414 case R_NDS32_GOT_LO15:
6415 case R_NDS32_GOT_LO19:
6416 case R_NDS32_GOT20:
6417 case R_NDS32_TLS_IE_HI20:
6418 case R_NDS32_TLS_IE_LO12S2:
6419 switch (r_type)
6420 {
6421 case R_NDS32_TLS_IE_HI20:
6422 case R_NDS32_TLS_IE_LO12S2:
6423 tls_type = GOT_TLS_IE;
6424 break;
6425 default:
6426 tls_type = GOT_NORMAL;
6427 break;
6428 }
6429 if (h != NULL)
6430 {
6431 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
6432 h->got.refcount += 1;
6433 }
6434 else
6435 {
6436 bfd_signed_vma *local_got_refcounts;
6437
6438 /* This is a global offset table entry for a local
6439 symbol. */
6440 local_got_refcounts = elf_local_got_refcounts (abfd);
6441 if (local_got_refcounts == NULL)
6442 {
6443 bfd_size_type size;
6444
6445 size = symtab_hdr->sh_info;
6446 size *= sizeof (bfd_signed_vma);
6447 local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
6448 if (local_got_refcounts == NULL)
6449 return FALSE;
6450 elf_local_got_refcounts (abfd) = local_got_refcounts;
6451 }
6452 local_got_refcounts[r_symndx] += 1;
6453 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
6454 }
6455
6456 /* We will already have issued an error message if there
6457 is a TLS/non-TLS mismatch, based on the symbol
6458 type. So just combine any TLS types needed. */
6459 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
6460 && tls_type != GOT_NORMAL)
6461 tls_type |= old_tls_type;
6462
6463 if (old_tls_type != tls_type)
6464 {
6465 if (h != NULL)
6466 elf32_nds32_hash_entry (h)->tls_type = tls_type;
6467 else
6468 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
6469 }
6470 break;
6471 case R_NDS32_9_PLTREL:
6472 case R_NDS32_25_PLTREL:
6473 case R_NDS32_PLTREL_HI20:
6474 case R_NDS32_PLTREL_LO12:
6475 case R_NDS32_PLT_GOTREL_HI20:
6476 case R_NDS32_PLT_GOTREL_LO12:
6477 case R_NDS32_PLT_GOTREL_LO15:
6478 case R_NDS32_PLT_GOTREL_LO19:
6479 case R_NDS32_PLT_GOTREL_LO20:
6480
6481 /* This symbol requires a procedure linkage table entry. We
6482 actually build the entry in adjust_dynamic_symbol,
6483 because this might be a case of linking PIC code without
6484 linking in any dynamic objects, in which case we don't
6485 need to generate a procedure linkage table after all. */
6486
6487 /* If this is a local symbol, we resolve it directly without
6488 creating a procedure linkage table entry. */
6489 if (h == NULL)
6490 continue;
6491
6492 if (h->forced_local)
6493 break;
6494
6495 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
6496 h->needs_plt = 1;
6497 h->plt.refcount += 1;
6498 break;
6499
6500 case R_NDS32_16_RELA:
6501 case R_NDS32_20_RELA:
6502 case R_NDS32_5_RELA:
6503 case R_NDS32_32_RELA:
6504 case R_NDS32_HI20_RELA:
6505 case R_NDS32_LO12S3_RELA:
6506 case R_NDS32_LO12S2_RELA:
6507 case R_NDS32_LO12S2_DP_RELA:
6508 case R_NDS32_LO12S2_SP_RELA:
6509 case R_NDS32_LO12S1_RELA:
6510 case R_NDS32_LO12S0_RELA:
6511 case R_NDS32_LO12S0_ORI_RELA:
6512 case R_NDS32_SDA16S3_RELA:
6513 case R_NDS32_SDA17S2_RELA:
6514 case R_NDS32_SDA18S1_RELA:
6515 case R_NDS32_SDA19S0_RELA:
6516 case R_NDS32_SDA15S3_RELA:
6517 case R_NDS32_SDA15S2_RELA:
6518 case R_NDS32_SDA12S2_DP_RELA:
6519 case R_NDS32_SDA12S2_SP_RELA:
6520 case R_NDS32_SDA15S1_RELA:
6521 case R_NDS32_SDA15S0_RELA:
6522 case R_NDS32_SDA_FP7U2_RELA:
6523 case R_NDS32_15_PCREL_RELA:
6524 case R_NDS32_17_PCREL_RELA:
6525 case R_NDS32_25_PCREL_RELA:
6526
6527 if (h != NULL && !info->shared)
6528 {
6529 h->non_got_ref = 1;
6530 h->plt.refcount += 1;
6531 }
6532
6533 /* If we are creating a shared library, and this is a reloc against
6534 a global symbol, or a non PC relative reloc against a local
6535 symbol, then we need to copy the reloc into the shared library.
6536 However, if we are linking with -Bsymbolic, we do not need to
6537 copy a reloc against a global symbol which is defined in an
6538 object we are including in the link (i.e., DEF_REGULAR is set).
6539 At this point we have not seen all the input files, so it is
6540 possible that DEF_REGULAR is not set now but will be set later
6541 (it is never cleared). We account for that possibility below by
6542 storing information in the dyn_relocs field of the hash table
6543 entry. A similar situation occurs when creating shared libraries
6544 and symbol visibility changes render the symbol local.
6545
6546 If on the other hand, we are creating an executable, we may need
6547 to keep relocations for symbols satisfied by a dynamic library
6548 if we manage to avoid copy relocs for the symbol. */
6549 if ((info->shared
6550 && (sec->flags & SEC_ALLOC) != 0
6551 && ((r_type != R_NDS32_25_PCREL_RELA
6552 && r_type != R_NDS32_15_PCREL_RELA
6553 && r_type != R_NDS32_17_PCREL_RELA
6554 && !(r_type == R_NDS32_32_RELA
6555 && strcmp (sec->name, ".eh_frame") == 0))
6556 || (h != NULL
6557 && (!info->symbolic
6558 || h->root.type == bfd_link_hash_defweak
6559 || !h->def_regular))))
6560 || (!info->shared
6561 && (sec->flags & SEC_ALLOC) != 0
6562 && h != NULL
6563 && (h->root.type == bfd_link_hash_defweak
6564 || !h->def_regular)))
6565 {
6566 struct elf_nds32_dyn_relocs *p;
6567 struct elf_nds32_dyn_relocs **head;
6568
6569 if (dynobj == NULL)
6570 htab->root.dynobj = dynobj = abfd;
6571
6572 /* When creating a shared object, we must copy these
6573 relocs into the output file. We create a reloc
6574 section in dynobj and make room for the reloc. */
6575 if (sreloc == NULL)
6576 {
6577 const char *name;
6578
6579 name = bfd_elf_string_from_elf_section
6580 (abfd, elf_elfheader (abfd)->e_shstrndx,
6581 elf_section_data (sec)->rela.hdr->sh_name);
6582 if (name == NULL)
6583 return FALSE;
6584
6585 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
6586 && strcmp (bfd_get_section_name (abfd, sec),
6587 name + 5) == 0);
6588
6589 sreloc = bfd_get_section_by_name (dynobj, name);
6590 if (sreloc == NULL)
6591 {
6592 flagword flags;
6593
6594 sreloc = bfd_make_section (dynobj, name);
6595 flags = (SEC_HAS_CONTENTS | SEC_READONLY
6596 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
6597 if ((sec->flags & SEC_ALLOC) != 0)
6598 flags |= SEC_ALLOC | SEC_LOAD;
6599 if (sreloc == NULL
6600 || !bfd_set_section_flags (dynobj, sreloc, flags)
6601 || !bfd_set_section_alignment (dynobj, sreloc, 2))
6602 return FALSE;
6603
6604 elf_section_type (sreloc) = SHT_RELA;
6605 }
6606 elf_section_data (sec)->sreloc = sreloc;
6607 }
6608
6609 /* If this is a global symbol, we count the number of
6610 relocations we need for this symbol. */
6611 if (h != NULL)
6612 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
6613 else
6614 {
6615 asection *s;
6616
6617 Elf_Internal_Sym *isym;
6618 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
6619 if (isym == NULL)
6620 return FALSE;
6621
6622 /* Track dynamic relocs needed for local syms too. */
6623 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6624 if (s == NULL)
6625 return FALSE;
6626
6627 head = ((struct elf_nds32_dyn_relocs **)
6628 &elf_section_data (s)->local_dynrel);
6629 }
6630
6631 p = *head;
6632 if (p == NULL || p->sec != sec)
6633 {
6634 bfd_size_type amt = sizeof (*p);
6635 p = (struct elf_nds32_dyn_relocs *) bfd_alloc (dynobj, amt);
6636 if (p == NULL)
6637 return FALSE;
6638 p->next = *head;
6639 *head = p;
6640 p->sec = sec;
6641 p->count = 0;
6642 p->pc_count = 0;
6643 }
6644
6645 p->count += 1;
6646 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
6647 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
6648 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA)
6649 p->pc_count += 1;
6650 }
6651 break;
6652
6653 /* This relocation describes the C++ object vtable hierarchy.
6654 Reconstruct it for later use during GC. */
6655 case R_NDS32_RELA_GNU_VTINHERIT:
6656 case R_NDS32_GNU_VTINHERIT:
6657 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6658 return FALSE;
6659 break;
6660
6661 /* This relocation describes which C++ vtable entries are actually
6662 used. Record for later use during GC. */
6663 case R_NDS32_GNU_VTENTRY:
6664 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
6665 return FALSE;
6666 break;
6667 case R_NDS32_RELA_GNU_VTENTRY:
6668 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6669 return FALSE;
6670 break;
6671 }
6672 }
6673
6674 return TRUE;
6675 }
6676
6677 /* Write VAL in uleb128 format to P, returning a pointer to the
6678 following byte.
6679 This code is copied from elf-attr.c. */
6680
6681 static bfd_byte *
6682 write_uleb128 (bfd_byte *p, unsigned int val)
6683 {
6684 bfd_byte c;
6685 do
6686 {
6687 c = val & 0x7f;
6688 val >>= 7;
6689 if (val)
6690 c |= 0x80;
6691 *(p++) = c;
6692 }
6693 while (val);
6694 return p;
6695 }
6696
6697 static bfd_signed_vma
6698 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
6699 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
6700 int *pic_ext_target)
6701 {
6702 bfd_signed_vma foff;
6703 bfd_vma symval, addend;
6704 asection *sym_sec;
6705
6706 /* Get the value of the symbol referred to by the reloc. */
6707 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6708 {
6709 Elf_Internal_Sym *isym;
6710
6711 /* A local symbol. */
6712 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6713
6714 if (isym->st_shndx == SHN_UNDEF)
6715 sym_sec = bfd_und_section_ptr;
6716 else if (isym->st_shndx == SHN_ABS)
6717 sym_sec = bfd_abs_section_ptr;
6718 else if (isym->st_shndx == SHN_COMMON)
6719 sym_sec = bfd_com_section_ptr;
6720 else
6721 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6722 symval = isym->st_value + sym_sec->output_section->vma
6723 + sym_sec->output_offset;
6724 }
6725 else
6726 {
6727 unsigned long indx;
6728 struct elf_link_hash_entry *h;
6729 bfd *owner;
6730
6731 /* An external symbol. */
6732 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6733 h = elf_sym_hashes (abfd)[indx];
6734 BFD_ASSERT (h != NULL);
6735
6736 if (h->root.type != bfd_link_hash_defined
6737 && h->root.type != bfd_link_hash_defweak)
6738 /* This appears to be a reference to an undefined
6739 symbol. Just ignore it--it will be caught by the
6740 regular reloc processing. */
6741 return 0;
6742 owner = h->root.u.def.section->owner;
6743 if (owner && (elf_elfheader (owner)->e_flags & E_NDS32_HAS_PIC))
6744 *pic_ext_target = 1;
6745
6746 if (h->root.u.def.section->flags & SEC_MERGE)
6747 {
6748 sym_sec = h->root.u.def.section;
6749 symval = _bfd_merged_section_offset (abfd, &sym_sec,
6750 elf_section_data (sym_sec)->sec_info,
6751 h->root.u.def.value);
6752 symval = symval + sym_sec->output_section->vma
6753 + sym_sec->output_offset;
6754 }
6755 else
6756 symval = (h->root.u.def.value
6757 + h->root.u.def.section->output_section->vma
6758 + h->root.u.def.section->output_offset);
6759 }
6760
6761 addend = irel->r_addend;
6762
6763 foff = (symval + addend
6764 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
6765 return foff;
6766 }
6767
6768 static bfd_vma
6769 calculate_plt_memory_address (bfd *abfd, struct bfd_link_info *link_info,
6770 Elf_Internal_Sym *isymbuf,
6771 Elf_Internal_Rela *irel,
6772 Elf_Internal_Shdr *symtab_hdr)
6773 {
6774 bfd_vma symval;
6775
6776 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
6777 {
6778 Elf_Internal_Sym *isym;
6779 asection *sym_sec;
6780 /* A local symbol. */
6781 isym = isymbuf + ELF32_R_SYM (irel->r_info);
6782
6783 if (isym->st_shndx == SHN_UNDEF)
6784 sym_sec = bfd_und_section_ptr;
6785 else if (isym->st_shndx == SHN_ABS)
6786 sym_sec = bfd_abs_section_ptr;
6787 else if (isym->st_shndx == SHN_COMMON)
6788 sym_sec = bfd_com_section_ptr;
6789 else
6790 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
6791 symval = isym->st_value + sym_sec->output_section->vma
6792 + sym_sec->output_offset;
6793 }
6794 else
6795 {
6796 unsigned long indx;
6797 struct elf_link_hash_entry *h;
6798 struct elf_nds32_link_hash_table *htab;
6799 asection *splt;
6800
6801 /* An external symbol. */
6802 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
6803 h = elf_sym_hashes (abfd)[indx];
6804 BFD_ASSERT (h != NULL);
6805 htab = nds32_elf_hash_table (link_info);
6806 splt = htab->splt;
6807
6808 while (h->root.type == bfd_link_hash_indirect
6809 || h->root.type == bfd_link_hash_warning)
6810 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6811
6812 if (h->plt.offset == (bfd_vma) - 1)
6813 {
6814 if (h->root.type != bfd_link_hash_defined
6815 && h->root.type != bfd_link_hash_defweak)
6816 /* This appears to be a reference to an undefined
6817 * symbol. Just ignore it--it will be caught by the
6818 * regular reloc processing. */
6819 return 0;
6820 symval = (h->root.u.def.value
6821 + h->root.u.def.section->output_section->vma
6822 + h->root.u.def.section->output_offset);
6823 }
6824 else
6825 symval = splt->output_section->vma + h->plt.offset;
6826 }
6827
6828 return symval;
6829 }
6830
6831 static bfd_signed_vma
6832 calculate_plt_offset (bfd *abfd, asection *sec, struct bfd_link_info *link_info,
6833 Elf_Internal_Sym *isymbuf, Elf_Internal_Rela *irel,
6834 Elf_Internal_Shdr *symtab_hdr)
6835 {
6836 bfd_vma foff;
6837 if ((foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
6838 symtab_hdr)) == 0)
6839 return 0;
6840 else
6841 return foff - (irel->r_offset
6842 + sec->output_section->vma + sec->output_offset);
6843 }
6844 \f
6845 /* Convert a 32-bit instruction to 16-bit one.
6846 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
6847 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
6848 type of INSN16. Return 1 if successful. */
6849
6850 static int
6851 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
6852 int *pinsn_type)
6853 {
6854 uint16_t insn16 = 0;
6855 int insn_type;
6856 unsigned long mach = bfd_get_mach (abfd);
6857
6858 if (N32_SH5 (insn) != 0)
6859 return 0;
6860
6861 switch (N32_SUB5 (insn))
6862 {
6863 case N32_ALU1_ADD_SLLI:
6864 case N32_ALU1_ADD_SRLI:
6865 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6866 {
6867 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
6868 N32_RB5 (insn));
6869 insn_type = NDS32_INSN_ADD333;
6870 }
6871 else if (N32_IS_RT4 (insn))
6872 {
6873 if (N32_RT5 (insn) == N32_RA5 (insn))
6874 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
6875 else if (N32_RT5 (insn) == N32_RB5 (insn))
6876 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
6877 insn_type = NDS32_INSN_ADD45;
6878 }
6879 break;
6880
6881 case N32_ALU1_SUB_SLLI:
6882 case N32_ALU1_SUB_SRLI:
6883 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
6884 {
6885 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
6886 N32_RB5 (insn));
6887 insn_type = NDS32_INSN_SUB333;
6888 }
6889 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6890 {
6891 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
6892 insn_type = NDS32_INSN_SUB45;
6893 }
6894 break;
6895
6896 case N32_ALU1_AND_SLLI:
6897 case N32_ALU1_AND_SRLI:
6898 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
6899 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6900 && N32_IS_RB3 (insn))
6901 {
6902 if (N32_RT5 (insn) == N32_RA5 (insn))
6903 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
6904 else if (N32_RT5 (insn) == N32_RB5 (insn))
6905 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
6906 if (insn16)
6907 insn_type = NDS32_INSN_AND33;
6908 }
6909 break;
6910
6911 case N32_ALU1_XOR_SLLI:
6912 case N32_ALU1_XOR_SRLI:
6913 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
6914 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6915 && N32_IS_RB3 (insn))
6916 {
6917 if (N32_RT5 (insn) == N32_RA5 (insn))
6918 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
6919 else if (N32_RT5 (insn) == N32_RB5 (insn))
6920 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
6921 if (insn16)
6922 insn_type = NDS32_INSN_XOR33;
6923 }
6924 break;
6925
6926 case N32_ALU1_OR_SLLI:
6927 case N32_ALU1_OR_SRLI:
6928 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
6929 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
6930 && N32_IS_RB3 (insn))
6931 {
6932 if (N32_RT5 (insn) == N32_RA5 (insn))
6933 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
6934 else if (N32_RT5 (insn) == N32_RB5 (insn))
6935 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
6936 if (insn16)
6937 insn_type = NDS32_INSN_OR33;
6938 }
6939 break;
6940 case N32_ALU1_NOR:
6941 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
6942 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
6943 && N32_RA5 (insn) == N32_RB5 (insn))
6944 {
6945 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
6946 insn_type = NDS32_INSN_NOT33;
6947 }
6948 break;
6949 case N32_ALU1_SRAI:
6950 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6951 {
6952 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
6953 insn_type = NDS32_INSN_SRAI45;
6954 }
6955 break;
6956
6957 case N32_ALU1_SRLI:
6958 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
6959 {
6960 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
6961 insn_type = NDS32_INSN_SRLI45;
6962 }
6963 break;
6964
6965 case N32_ALU1_SLLI:
6966 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
6967 {
6968 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
6969 N32_UB5 (insn));
6970 insn_type = NDS32_INSN_SLLI333;
6971 }
6972 break;
6973
6974 case N32_ALU1_ZEH:
6975 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6976 {
6977 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
6978 insn_type = NDS32_INSN_ZEH33;
6979 }
6980 break;
6981
6982 case N32_ALU1_SEB:
6983 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6984 {
6985 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
6986 insn_type = NDS32_INSN_SEB33;
6987 }
6988 break;
6989
6990 case N32_ALU1_SEH:
6991 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
6992 {
6993 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
6994 insn_type = NDS32_INSN_SEH33;
6995 }
6996 break;
6997
6998 case N32_ALU1_SLT:
6999 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7000 {
7001 /* Implicit r15. */
7002 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7003 insn_type = NDS32_INSN_SLT45;
7004 }
7005 break;
7006
7007 case N32_ALU1_SLTS:
7008 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7009 {
7010 /* Implicit r15. */
7011 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7012 insn_type = NDS32_INSN_SLTS45;
7013 }
7014 break;
7015 }
7016
7017 if ((insn16 & 0x8000) == 0)
7018 return 0;
7019
7020 if (pinsn16)
7021 *pinsn16 = insn16;
7022 if (pinsn_type)
7023 *pinsn_type = insn_type;
7024 return 1;
7025 }
7026
7027 static int
7028 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7029 int *pinsn_type)
7030 {
7031 uint16_t insn16 = 0;
7032 int insn_type;
7033 unsigned long mach = bfd_get_mach (abfd);
7034
7035 /* TODO: bset, bclr, btgl, btst. */
7036 if (__GF (insn, 6, 4) != 0)
7037 return 0;
7038
7039 switch (N32_IMMU (insn, 6))
7040 {
7041 case N32_ALU2_MUL:
7042 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7043 && N32_IS_RB3 (insn))
7044 {
7045 if (N32_RT5 (insn) == N32_RA5 (insn))
7046 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7047 else if (N32_RT5 (insn) == N32_RB5 (insn))
7048 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7049 if (insn16)
7050 insn_type = NDS32_INSN_MUL33;
7051 }
7052 }
7053
7054 if ((insn16 & 0x8000) == 0)
7055 return 0;
7056
7057 if (pinsn16)
7058 *pinsn16 = insn16;
7059 if (pinsn_type)
7060 *pinsn_type = insn_type;
7061 return 1;
7062 }
7063
7064 int
7065 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7066 int *pinsn_type)
7067 {
7068 int op6;
7069 uint16_t insn16 = 0;
7070 int insn_type;
7071 unsigned long mach = bfd_get_mach (abfd);
7072
7073 /* Decode 32-bit instruction. */
7074 if (insn & 0x80000000)
7075 {
7076 /* Not 32-bit insn. */
7077 return 0;
7078 }
7079
7080 op6 = N32_OP6 (insn);
7081
7082 /* Convert it to 16-bit instruction. */
7083 switch (op6)
7084 {
7085 case N32_OP6_MOVI:
7086 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7087 {
7088 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7089 insn_type = NDS32_INSN_MOVI55;
7090 }
7091 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7092 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7093 {
7094 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7095 N32_IMM20S (insn) - 16);
7096 insn_type = NDS32_INSN_MOVPI45;
7097 }
7098 break;
7099
7100 case N32_OP6_ADDI:
7101 if (N32_IMM15S (insn) == 0)
7102 {
7103 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7104 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7105 if (mach <= MACH_V2
7106 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7107 {
7108 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7109 insn_type = NDS32_INSN_MOV55;
7110 }
7111 }
7112 else if (N32_IMM15S (insn) > 0)
7113 {
7114 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7115 {
7116 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7117 N32_IMM15S (insn));
7118 insn_type = NDS32_INSN_ADDI333;
7119 }
7120 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7121 && N32_IMM15S (insn) < 32)
7122 {
7123 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7124 insn_type = NDS32_INSN_ADDI45;
7125 }
7126 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7127 && N32_RT5 (insn) == N32_RA5 (insn)
7128 && N32_IMM15S (insn) < 512)
7129 {
7130 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7131 insn_type = NDS32_INSN_ADDI10_SP;
7132 }
7133 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7134 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7135 && (N32_IMM15S (insn) % 4 == 0))
7136 {
7137 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7138 N32_IMM15S (insn) >> 2);
7139 insn_type = NDS32_INSN_ADDRI36_SP;
7140 }
7141 }
7142 else
7143 {
7144 /* Less than 0. */
7145 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7146 {
7147 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7148 0 - N32_IMM15S (insn));
7149 insn_type = NDS32_INSN_SUBI333;
7150 }
7151 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7152 && N32_IMM15S (insn) > -32)
7153 {
7154 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7155 0 - N32_IMM15S (insn));
7156 insn_type = NDS32_INSN_SUBI45;
7157 }
7158 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7159 && N32_RT5 (insn) == N32_RA5 (insn)
7160 && N32_IMM15S (insn) >= -512)
7161 {
7162 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7163 insn_type = NDS32_INSN_ADDI10_SP;
7164 }
7165 }
7166 break;
7167
7168 case N32_OP6_ORI:
7169 if (N32_IMM15S (insn) == 0)
7170 {
7171 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7172 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7173 if (mach <= MACH_V2
7174 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7175 {
7176 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7177 insn_type = NDS32_INSN_MOV55;
7178 }
7179 }
7180 break;
7181
7182 case N32_OP6_SUBRI:
7183 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7184 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7185 {
7186 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7187 insn_type = NDS32_INSN_NEG33;
7188 }
7189 break;
7190
7191 case N32_OP6_ANDI:
7192 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7193 {
7194 if (N32_IMM15U (insn) == 1)
7195 {
7196 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7197 insn_type = NDS32_INSN_XLSB33;
7198 }
7199 else if (N32_IMM15U (insn) == 0x7ff)
7200 {
7201 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7202 insn_type = NDS32_INSN_X11B33;
7203 }
7204 else if (N32_IMM15U (insn) == 0xff)
7205 {
7206 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7207 insn_type = NDS32_INSN_ZEB33;
7208 }
7209 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7210 && N32_IMM15U (insn) < 256)
7211 {
7212 int imm15u = N32_IMM15U (insn);
7213
7214 if (__builtin_popcount (imm15u) == 1)
7215 {
7216 /* BMSKI33 */
7217 int imm3u = __builtin_ctz (imm15u);
7218
7219 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7220 insn_type = NDS32_INSN_BMSKI33;
7221 }
7222 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7223 {
7224 /* FEXTI33 */
7225 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7226
7227 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7228 insn_type = NDS32_INSN_FEXTI33;
7229 }
7230 }
7231 }
7232 break;
7233
7234 case N32_OP6_SLTI:
7235 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7236 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7237 {
7238 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7239 insn_type = NDS32_INSN_SLTI45;
7240 }
7241 break;
7242
7243 case N32_OP6_SLTSI:
7244 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7245 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7246 {
7247 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7248 insn_type = NDS32_INSN_SLTSI45;
7249 }
7250 break;
7251
7252 case N32_OP6_LWI:
7253 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7254 {
7255 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7256 insn_type = NDS32_INSN_LWI450;
7257 }
7258 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7259 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7260 {
7261 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7262 N32_IMM15S (insn));
7263 insn_type = NDS32_INSN_LWI333;
7264 }
7265 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7266 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7267 {
7268 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7269 insn_type = NDS32_INSN_LWI37;
7270 }
7271 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7272 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7273 {
7274 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7275 insn_type = NDS32_INSN_LWI37_SP;
7276 }
7277 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7278 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7279 {
7280 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7281 N32_IMM15S (insn) + 32);
7282 insn_type = NDS32_INSN_LWI45_FE;
7283 }
7284 break;
7285
7286 case N32_OP6_SWI:
7287 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7288 {
7289 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7290 insn_type = NDS32_INSN_SWI450;
7291 }
7292 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7293 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7294 {
7295 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7296 N32_IMM15S (insn));
7297 insn_type = NDS32_INSN_SWI333;
7298 }
7299 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7300 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7301 {
7302 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7303 insn_type = NDS32_INSN_SWI37;
7304 }
7305 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7306 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7307 {
7308 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7309 insn_type = NDS32_INSN_SWI37_SP;
7310 }
7311 break;
7312
7313 case N32_OP6_LWI_BI:
7314 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7315 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7316 {
7317 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7318 N32_IMM15S (insn));
7319 insn_type = NDS32_INSN_LWI333_BI;
7320 }
7321 break;
7322
7323 case N32_OP6_SWI_BI:
7324 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7325 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7326 {
7327 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7328 N32_IMM15S (insn));
7329 insn_type = NDS32_INSN_SWI333_BI;
7330 }
7331 break;
7332
7333 case N32_OP6_LHI:
7334 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7335 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7336 {
7337 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7338 N32_IMM15S (insn));
7339 insn_type = NDS32_INSN_LHI333;
7340 }
7341 break;
7342
7343 case N32_OP6_SHI:
7344 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7345 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7346 {
7347 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
7348 N32_IMM15S (insn));
7349 insn_type = NDS32_INSN_SHI333;
7350 }
7351 break;
7352
7353 case N32_OP6_LBI:
7354 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7355 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7356 {
7357 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
7358 N32_IMM15S (insn));
7359 insn_type = NDS32_INSN_LBI333;
7360 }
7361 break;
7362
7363 case N32_OP6_SBI:
7364 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7365 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7366 {
7367 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
7368 N32_IMM15S (insn));
7369 insn_type = NDS32_INSN_SBI333;
7370 }
7371 break;
7372
7373 case N32_OP6_ALU1:
7374 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
7375
7376 case N32_OP6_ALU2:
7377 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
7378
7379 case N32_OP6_BR1:
7380 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
7381 goto done;
7382
7383 if ((insn & __BIT (14)) == 0)
7384 {
7385 /* N32_BR1_BEQ */
7386 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7387 && N32_RT5 (insn) != REG_R5)
7388 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
7389 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7390 && N32_RA5 (insn) != REG_R5)
7391 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
7392 insn_type = NDS32_INSN_BEQS38;
7393 break;
7394 }
7395 else
7396 {
7397 /* N32_BR1_BNE */
7398 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
7399 && N32_RT5 (insn) != REG_R5)
7400 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
7401 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
7402 && N32_RA5 (insn) != REG_R5)
7403 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
7404 insn_type = NDS32_INSN_BNES38;
7405 break;
7406 }
7407 break;
7408
7409 case N32_OP6_BR2:
7410 switch (N32_BR2_SUB (insn))
7411 {
7412 case N32_BR2_BEQZ:
7413 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7414 {
7415 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
7416 insn_type = NDS32_INSN_BEQZ38;
7417 }
7418 else if (N32_RT5 (insn) == REG_R15
7419 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7420 {
7421 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
7422 insn_type = NDS32_INSN_BEQZS8;
7423 }
7424 break;
7425
7426 case N32_BR2_BNEZ:
7427 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
7428 {
7429 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
7430 insn_type = NDS32_INSN_BNEZ38;
7431 }
7432 else if (N32_RT5 (insn) == REG_R15
7433 && IS_WITHIN_S (N32_IMM16S (insn), 8))
7434 {
7435 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
7436 insn_type = NDS32_INSN_BNEZS8;
7437 }
7438 break;
7439
7440 case N32_BR2_IFCALL:
7441 if (IS_WITHIN_U (N32_IMM16S (insn), 9))
7442 {
7443 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
7444 insn_type = NDS32_INSN_IFCALL9;
7445 }
7446 break;
7447 }
7448 break;
7449
7450 case N32_OP6_JI:
7451 if ((insn & __BIT (24)) == 0)
7452 {
7453 /* N32_JI_J */
7454 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
7455 {
7456 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
7457 insn_type = NDS32_INSN_J8;
7458 }
7459 }
7460 break;
7461
7462 case N32_OP6_JREG:
7463 if (__GF (insn, 8, 2) != 0)
7464 goto done;
7465
7466 switch (N32_IMMU (insn, 5))
7467 {
7468 case N32_JREG_JR:
7469 if (N32_JREG_HINT (insn) == 0)
7470 {
7471 /* jr */
7472 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
7473 insn_type = NDS32_INSN_JR5;
7474 }
7475 else if (N32_JREG_HINT (insn) == 1)
7476 {
7477 /* ret */
7478 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
7479 insn_type = NDS32_INSN_RET5;
7480 }
7481 else if (N32_JREG_HINT (insn) == 3)
7482 {
7483 /* ifret = mov55 $sp, $sp */
7484 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
7485 insn_type = NDS32_INSN_IFRET;
7486 }
7487 break;
7488
7489 case N32_JREG_JRAL:
7490 /* It's convertible when return rt5 is $lp and address
7491 translation is kept. */
7492 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
7493 {
7494 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
7495 insn_type = NDS32_INSN_JRAL5;
7496 }
7497 break;
7498 }
7499 break;
7500
7501 case N32_OP6_MISC:
7502 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
7503 {
7504 /* For v3, swid above 31 are used for ex9.it. */
7505 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
7506 insn_type = NDS32_INSN_BREAK16;
7507 }
7508 break;
7509
7510 default:
7511 /* This instruction has no 16-bit variant. */
7512 goto done;
7513 }
7514
7515 done:
7516 /* Bit-15 of insn16 should be set for a valid instruction. */
7517 if ((insn16 & 0x8000) == 0)
7518 return 0;
7519
7520 if (pinsn16)
7521 *pinsn16 = insn16;
7522 if (pinsn_type)
7523 *pinsn_type = insn_type;
7524 return 1;
7525 }
7526
7527 static int
7528 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
7529 Elf_Internal_Rela *reloc)
7530 {
7531 uint16_t insn16 = 0;
7532
7533 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
7534 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
7535 return 0;
7536
7537 if (!N32_IS_RT3 (insn))
7538 return 0;
7539
7540 switch (N32_OP6 (insn))
7541 {
7542 case N32_OP6_LWI:
7543 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7544 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7545 break;
7546 case N32_OP6_SWI:
7547 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
7548 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7549 break;
7550 case N32_OP6_HWGP:
7551 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
7552 break;
7553
7554 if (__GF (insn, 17, 3) == 6)
7555 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
7556 else if (__GF (insn, 17, 3) == 7)
7557 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
7558 break;
7559 }
7560
7561 if ((insn16 & 0x8000) == 0)
7562 return 0;
7563
7564 *pinsn16 = insn16;
7565 return 1;
7566 }
7567
7568 /* Convert a 16-bit instruction to 32-bit one.
7569 INSN16 it the input and PINSN it the point to output.
7570 Return non-zero on successful. Otherwise 0 is returned. */
7571
7572 int
7573 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
7574 {
7575 uint32_t insn = 0xffffffff;
7576 unsigned long mach = bfd_get_mach (abfd);
7577
7578 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
7579
7580 switch (__GF (insn16, 9, 6))
7581 {
7582 case 0x4: /* add45 */
7583 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
7584 N16_RA5 (insn16));
7585 goto done;
7586 case 0x5: /* sub45 */
7587 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
7588 N16_RA5 (insn16));
7589 goto done;
7590 case 0x6: /* addi45 */
7591 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7592 N16_IMM5U (insn16));
7593 goto done;
7594 case 0x7: /* subi45 */
7595 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
7596 -N16_IMM5U (insn16));
7597 goto done;
7598 case 0x8: /* srai45 */
7599 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
7600 N16_IMM5U (insn16));
7601 goto done;
7602 case 0x9: /* srli45 */
7603 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
7604 N16_IMM5U (insn16));
7605 goto done;
7606 case 0xa: /* slli333 */
7607 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
7608 N16_IMM3U (insn16));
7609 goto done;
7610 case 0xc: /* add333 */
7611 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
7612 N16_RB3 (insn16));
7613 goto done;
7614 case 0xd: /* sub333 */
7615 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
7616 N16_RB3 (insn16));
7617 goto done;
7618 case 0xe: /* addi333 */
7619 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7620 N16_IMM3U (insn16));
7621 goto done;
7622 case 0xf: /* subi333 */
7623 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
7624 -N16_IMM3U (insn16));
7625 goto done;
7626 case 0x10: /* lwi333 */
7627 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
7628 N16_IMM3U (insn16));
7629 goto done;
7630 case 0x12: /* lhi333 */
7631 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
7632 N16_IMM3U (insn16));
7633 goto done;
7634 case 0x13: /* lbi333 */
7635 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
7636 N16_IMM3U (insn16));
7637 goto done;
7638 case 0x11: /* lwi333.bi */
7639 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7640 N16_IMM3U (insn16));
7641 goto done;
7642 case 0x14: /* swi333 */
7643 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
7644 N16_IMM3U (insn16));
7645 goto done;
7646 case 0x16: /* shi333 */
7647 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
7648 N16_IMM3U (insn16));
7649 goto done;
7650 case 0x17: /* sbi333 */
7651 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
7652 N16_IMM3U (insn16));
7653 goto done;
7654 case 0x15: /* swi333.bi */
7655 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
7656 N16_IMM3U (insn16));
7657 goto done;
7658 case 0x18: /* addri36.sp */
7659 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
7660 N16_IMM6U (insn16) << 2);
7661 goto done;
7662 case 0x19: /* lwi45.fe */
7663 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
7664 (N16_IMM5U (insn16) - 32));
7665 goto done;
7666 case 0x1a: /* lwi450 */
7667 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7668 goto done;
7669 case 0x1b: /* swi450 */
7670 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
7671 goto done;
7672
7673 /* These are r15 implied instructions. */
7674 case 0x30: /* slts45 */
7675 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7676 goto done;
7677 case 0x31: /* slt45 */
7678 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
7679 goto done;
7680 case 0x32: /* sltsi45 */
7681 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7682 goto done;
7683 case 0x33: /* slti45 */
7684 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
7685 goto done;
7686 case 0x34: /* beqzs8, bnezs8 */
7687 if (insn16 & __BIT (8))
7688 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
7689 else
7690 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
7691 goto done;
7692
7693 case 0x35: /* break16, ex9.it */
7694 /* Only consider range of v3 break16. */
7695 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
7696 goto done;
7697
7698 case 0x3c: /* ifcall9 */
7699 insn = N32_BR2 (IFCALL, 0, N16_IMM9U (insn16));
7700 goto done;
7701 case 0x3d: /* movpi45 */
7702 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
7703 goto done;
7704
7705 case 0x3f: /* MISC33 */
7706 switch (insn16 & 0x7)
7707 {
7708 case 2: /* neg33 */
7709 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7710 break;
7711 case 3: /* not33 */
7712 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
7713 N16_RA3 (insn16));
7714 break;
7715 case 4: /* mul33 */
7716 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
7717 N16_RA3 (insn16));
7718 break;
7719 case 5: /* xor33 */
7720 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
7721 N16_RA3 (insn16));
7722 break;
7723 case 6: /* and33 */
7724 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
7725 N16_RA3 (insn16));
7726 break;
7727 case 7: /* or33 */
7728 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
7729 N16_RA3 (insn16));
7730 break;
7731 }
7732 goto done;
7733
7734 case 0xb:
7735 switch (insn16 & 0x7)
7736 {
7737 case 0: /* zeb33 */
7738 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
7739 break;
7740 case 1: /* zeh33 */
7741 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7742 break;
7743 case 2: /* seb33 */
7744 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7745 break;
7746 case 3: /* seh33 */
7747 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
7748 break;
7749 case 4: /* xlsb33 */
7750 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
7751 break;
7752 case 5: /* x11b33 */
7753 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
7754 break;
7755 case 6: /* bmski33 */
7756 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7757 1 << __GF (insn16, 3, 3));
7758 break;
7759 case 7: /* fexti33 */
7760 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
7761 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
7762 break;
7763 }
7764 goto done;
7765 }
7766
7767 switch (__GF (insn16, 10, 5))
7768 {
7769 case 0x0: /* mov55 or ifret16 */
7770 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
7771 && N16_RT5 (insn16) == N16_RA5 (insn16))
7772 insn = N32_JREG (JR, 0, 0, 0, 3);
7773 else
7774 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
7775 goto done;
7776 case 0x1: /* movi55 */
7777 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
7778 goto done;
7779 case 0x1b: /* addi10s (V2) */
7780 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
7781 goto done;
7782 }
7783
7784 switch (__GF (insn16, 11, 4))
7785 {
7786 case 0x7: /* lwi37.fp/swi37.fp */
7787 if (insn16 & __BIT (7)) /* swi37.fp */
7788 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7789 else /* lwi37.fp */
7790 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
7791 goto done;
7792 case 0x8: /* beqz38 */
7793 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7794 goto done;
7795 case 0x9: /* bnez38 */
7796 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
7797 goto done;
7798 case 0xa: /* beqs38/j8, implied r5 */
7799 if (N16_RT38 (insn16) == 5)
7800 insn = N32_JI (J, N16_IMM8S (insn16));
7801 else
7802 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7803 goto done;
7804 case 0xb: /* bnes38 and others */
7805 if (N16_RT38 (insn16) == 5)
7806 {
7807 switch (__GF (insn16, 5, 3))
7808 {
7809 case 0: /* jr5 */
7810 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
7811 break;
7812 case 4: /* ret5 */
7813 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
7814 break;
7815 case 1: /* jral5 */
7816 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
7817 break;
7818 case 2: /* ex9.it imm5 */
7819 /* ex9.it had no 32-bit variantl. */
7820 break;
7821 case 5: /* add5.pc */
7822 /* add5.pc had no 32-bit variantl. */
7823 break;
7824 }
7825 }
7826 else /* bnes38 */
7827 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
7828 goto done;
7829 case 0xe: /* lwi37/swi37 */
7830 if (insn16 & (1 << 7)) /* swi37.sp */
7831 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7832 else /* lwi37.sp */
7833 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
7834 goto done;
7835 }
7836
7837 done:
7838 if (insn & 0x80000000)
7839 return 0;
7840
7841 if (pinsn)
7842 *pinsn = insn;
7843 return 1;
7844 }
7845 \f
7846 static bfd_boolean
7847 is_sda_access_insn (unsigned long insn)
7848 {
7849 switch (N32_OP6 (insn))
7850 {
7851 case N32_OP6_LWI:
7852 case N32_OP6_LHI:
7853 case N32_OP6_LHSI:
7854 case N32_OP6_LBI:
7855 case N32_OP6_LBSI:
7856 case N32_OP6_SWI:
7857 case N32_OP6_SHI:
7858 case N32_OP6_SBI:
7859 case N32_OP6_LWC:
7860 case N32_OP6_LDC:
7861 case N32_OP6_SWC:
7862 case N32_OP6_SDC:
7863 return TRUE;
7864 default:
7865 ;
7866 }
7867 return FALSE;
7868 }
7869
7870 static unsigned long
7871 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
7872 {
7873 uint32_t oinsn = 0;
7874
7875 switch (type)
7876 {
7877 case R_NDS32_GOT_LO12:
7878 case R_NDS32_GOTOFF_LO12:
7879 case R_NDS32_PLTREL_LO12:
7880 case R_NDS32_PLT_GOTREL_LO12:
7881 case R_NDS32_LO12S0_RELA:
7882 switch (N32_OP6 (insn))
7883 {
7884 case N32_OP6_LBI:
7885 /* lbi.gp */
7886 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
7887 break;
7888 case N32_OP6_LBSI:
7889 /* lbsi.gp */
7890 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
7891 break;
7892 case N32_OP6_SBI:
7893 /* sbi.gp */
7894 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
7895 break;
7896 case N32_OP6_ORI:
7897 /* addi.gp */
7898 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
7899 break;
7900 }
7901 break;
7902
7903 case R_NDS32_LO12S1_RELA:
7904 switch (N32_OP6 (insn))
7905 {
7906 case N32_OP6_LHI:
7907 /* lhi.gp */
7908 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
7909 break;
7910 case N32_OP6_LHSI:
7911 /* lhsi.gp */
7912 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
7913 break;
7914 case N32_OP6_SHI:
7915 /* shi.gp */
7916 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
7917 break;
7918 }
7919 break;
7920
7921 case R_NDS32_LO12S2_RELA:
7922 switch (N32_OP6 (insn))
7923 {
7924 case N32_OP6_LWI:
7925 /* lwi.gp */
7926 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
7927 break;
7928 case N32_OP6_SWI:
7929 /* swi.gp */
7930 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
7931 break;
7932 }
7933 break;
7934
7935 case R_NDS32_LO12S2_DP_RELA:
7936 case R_NDS32_LO12S2_SP_RELA:
7937 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
7938 break;
7939 }
7940
7941 if (oinsn)
7942 *pinsn = oinsn;
7943
7944 return oinsn != 0;
7945 }
7946
7947 /* Linker hasn't found the correct merge section for non-section symbol
7948 in relax time, this work is left to the function elf_link_input_bfd().
7949 So for non-section symbol, _bfd_merged_section_offset is also needed
7950 to find the correct symbol address. */
7951
7952 static bfd_vma
7953 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
7954 asection **psec, Elf_Internal_Rela *rel)
7955 {
7956 asection *sec = *psec;
7957 bfd_vma relocation;
7958
7959 relocation = (sec->output_section->vma
7960 + sec->output_offset + sym->st_value);
7961 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
7962 {
7963 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
7964 rel->r_addend =
7965 _bfd_merged_section_offset (abfd, psec,
7966 elf_section_data (sec)->sec_info,
7967 sym->st_value + rel->r_addend);
7968 else
7969 rel->r_addend =
7970 _bfd_merged_section_offset (abfd, psec,
7971 elf_section_data (sec)->sec_info,
7972 sym->st_value) + rel->r_addend;
7973
7974 if (sec != *psec)
7975 {
7976 /* If we have changed the section, and our original section is
7977 marked with SEC_EXCLUDE, it means that the original
7978 SEC_MERGE section has been completely subsumed in some
7979 other SEC_MERGE section. In this case, we need to leave
7980 some info around for --emit-relocs. */
7981 if ((sec->flags & SEC_EXCLUDE) != 0)
7982 sec->kept_section = *psec;
7983 sec = *psec;
7984 }
7985 rel->r_addend -= relocation;
7986 rel->r_addend += sec->output_section->vma + sec->output_offset;
7987 }
7988 return relocation;
7989 }
7990
7991 static bfd_vma
7992 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
7993 Elf_Internal_Sym *isymbuf,
7994 Elf_Internal_Shdr *symtab_hdr)
7995 {
7996 bfd_signed_vma foff;
7997 bfd_vma symval, addend;
7998 Elf_Internal_Rela irel_fn;
7999 Elf_Internal_Sym *isym;
8000 asection *sym_sec;
8001
8002 /* Get the value of the symbol referred to by the reloc. */
8003 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8004 {
8005 /* A local symbol. */
8006 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8007
8008 if (isym->st_shndx == SHN_UNDEF)
8009 sym_sec = bfd_und_section_ptr;
8010 else if (isym->st_shndx == SHN_ABS)
8011 sym_sec = bfd_abs_section_ptr;
8012 else if (isym->st_shndx == SHN_COMMON)
8013 sym_sec = bfd_com_section_ptr;
8014 else
8015 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8016 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8017 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8018 addend = irel_fn.r_addend;
8019 }
8020 else
8021 {
8022 unsigned long indx;
8023 struct elf_link_hash_entry *h;
8024
8025 /* An external symbol. */
8026 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8027 h = elf_sym_hashes (abfd)[indx];
8028 BFD_ASSERT (h != NULL);
8029
8030 while (h->root.type == bfd_link_hash_indirect
8031 || h->root.type == bfd_link_hash_warning)
8032 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8033
8034 if (h->root.type != bfd_link_hash_defined
8035 && h->root.type != bfd_link_hash_defweak)
8036 /* This appears to be a reference to an undefined
8037 symbol. Just ignore it--it will be caught by the
8038 regular reloc processing. */
8039 return 0;
8040
8041 if (h->root.u.def.section->flags & SEC_MERGE)
8042 {
8043 sym_sec = h->root.u.def.section;
8044 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8045 (sym_sec)->sec_info, h->root.u.def.value);
8046 symval = symval + sym_sec->output_section->vma
8047 + sym_sec->output_offset;
8048 }
8049 else
8050 symval = (h->root.u.def.value
8051 + h->root.u.def.section->output_section->vma
8052 + h->root.u.def.section->output_offset);
8053 addend = irel->r_addend;
8054 }
8055
8056 foff = symval + addend;
8057
8058 return foff;
8059 }
8060
8061 static bfd_vma
8062 calculate_got_memory_address (bfd *abfd, struct bfd_link_info *link_info,
8063 Elf_Internal_Rela *irel,
8064 Elf_Internal_Shdr *symtab_hdr)
8065 {
8066 int symndx;
8067 bfd_vma *local_got_offsets;
8068 /* Get the value of the symbol referred to by the reloc. */
8069 struct elf_link_hash_entry *h;
8070 struct elf_nds32_link_hash_table *htab = nds32_elf_hash_table (link_info);
8071
8072 /* An external symbol. */
8073 symndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8074 h = elf_sym_hashes (abfd)[symndx];
8075 while (h->root.type == bfd_link_hash_indirect
8076 || h->root.type == bfd_link_hash_warning)
8077 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8078
8079 if (symndx >= 0)
8080 {
8081 BFD_ASSERT (h != NULL);
8082 return htab->sgot->output_section->vma + htab->sgot->output_offset
8083 + h->got.offset;
8084 }
8085 else
8086 {
8087 local_got_offsets = elf_local_got_offsets (abfd);
8088 BFD_ASSERT (local_got_offsets != NULL);
8089 return htab->sgot->output_section->vma + htab->sgot->output_offset
8090 + local_got_offsets[ELF32_R_SYM (irel->r_info)];
8091 }
8092
8093 /* The _GLOBAL_OFFSET_TABLE_ may be undefweak(or should be?). */
8094 /* The check of h->root.type is passed. */
8095 }
8096
8097 static int
8098 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8099 asection *sec, Elf_Internal_Rela *rel)
8100 {
8101 bfd_byte *contents;
8102 unsigned short insn16;
8103
8104 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8105 return FALSE;
8106 contents = elf_section_data (sec)->this_hdr.contents;
8107 insn16 = bfd_getb16 (contents + rel->r_offset);
8108 if (insn16 == NDS32_NOP16)
8109 return TRUE;
8110 return FALSE;
8111 }
8112
8113 /* It checks whether the instruction could be converted to
8114 16-bit form and returns the converted one.
8115
8116 `internal_relocs' is supposed to be sorted. */
8117
8118 static int
8119 is_convert_32_to_16 (bfd *abfd, asection *sec,
8120 Elf_Internal_Rela *reloc,
8121 Elf_Internal_Rela *internal_relocs,
8122 Elf_Internal_Rela *irelend,
8123 uint16_t *insn16)
8124 {
8125 #define NORMAL_32_TO_16 (1 << 0)
8126 #define SPECIAL_32_TO_16 (1 << 1)
8127 bfd_byte *contents = NULL;
8128 bfd_signed_vma off;
8129 bfd_vma mem_addr;
8130 uint32_t insn = 0;
8131 Elf_Internal_Rela *pc_rel;
8132 int pic_ext_target = 0;
8133 Elf_Internal_Shdr *symtab_hdr;
8134 Elf_Internal_Sym *isymbuf = NULL;
8135 int convert_type;
8136 bfd_vma offset;
8137
8138 if (reloc->r_offset + 4 > sec->size)
8139 return FALSE;
8140
8141 offset = reloc->r_offset;
8142
8143 if (!nds32_get_section_contents (abfd, sec, &contents))
8144 return FALSE;
8145 insn = bfd_getb32 (contents + offset);
8146
8147 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8148 convert_type = NORMAL_32_TO_16;
8149 else if (special_convert_32_to_16 (insn, insn16, reloc))
8150 convert_type = SPECIAL_32_TO_16;
8151 else
8152 return FALSE;
8153
8154 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8155 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8156 return FALSE;
8157
8158 /* Find the first relocation of the same relocation-type,
8159 so we iteratie them forward. */
8160 pc_rel = reloc;
8161 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8162 pc_rel--;
8163
8164 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8165 {
8166 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8167 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8168 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8169 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8170 {
8171 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
8172 &pic_ext_target);
8173 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8174 || off == 0)
8175 return FALSE;
8176 break;
8177 }
8178 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8179 {
8180 /* movi => movi55 */
8181 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8182 symtab_hdr);
8183 /* mem_addr is unsigned, but the value should
8184 be between [-16, 15]. */
8185 if ((mem_addr + 0x10) >> 5)
8186 return FALSE;
8187 break;
8188 }
8189 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8190 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8191 {
8192 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8193 because it can be relaxed to addi for TLS_LE_ADD. */
8194 return FALSE;
8195 }
8196 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8197 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8198 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8199 && convert_type == SPECIAL_32_TO_16)
8200 {
8201 /* fp-as-gp
8202 We've selected a best fp-base for this access, so we can
8203 always resolve it anyway. Do nothing. */
8204 break;
8205 }
8206 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8207 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8208 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8209 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8210 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8211 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8212 {
8213 /* Prevent unresolved addi instruction translate
8214 to addi45 or addi333. */
8215 return FALSE;
8216 }
8217 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8218 {
8219 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr,
8220 &pic_ext_target);
8221 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8222 return FALSE;
8223 break;
8224 }
8225 }
8226
8227 return TRUE;
8228 }
8229
8230 static void
8231 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8232 Elf_Internal_Rela *reloc,
8233 Elf_Internal_Rela *internal_relocs,
8234 Elf_Internal_Rela *irelend,
8235 unsigned short insn16)
8236 {
8237 Elf_Internal_Rela *pc_rel;
8238 bfd_vma offset;
8239
8240 offset = reloc->r_offset;
8241 bfd_putb16 (insn16, contents + offset);
8242 /* Find the first relocation of the same relocation-type,
8243 so we iteratie them forward. */
8244 pc_rel = reloc;
8245 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8246 pc_rel--;
8247
8248 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8249 {
8250 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8251 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8252 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8253 {
8254 pc_rel->r_info =
8255 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8256 }
8257 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8258 pc_rel->r_info =
8259 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8260 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8261 pc_rel->r_info =
8262 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8263 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8264 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8265 pc_rel->r_info =
8266 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8267 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8268 pc_rel->r_info =
8269 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8270 }
8271 }
8272
8273 /* Find a relocation of type specified by `reloc_type'
8274 of the same r_offset with reloc.
8275 If not found, return irelend.
8276
8277 Assuming relocations are sorted by r_offset,
8278 we find the relocation from `reloc' backward untill relocs,
8279 or find it from `reloc' forward untill irelend. */
8280
8281 static Elf_Internal_Rela *
8282 find_relocs_at_address (Elf_Internal_Rela *reloc,
8283 Elf_Internal_Rela *relocs,
8284 Elf_Internal_Rela *irelend,
8285 enum elf_nds32_reloc_type reloc_type)
8286 {
8287 Elf_Internal_Rela *rel_t;
8288
8289 /* Find backward. */
8290 for (rel_t = reloc;
8291 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8292 rel_t--)
8293 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8294 return rel_t;
8295
8296 /* We didn't find it backward. Try find it forward. */
8297 for (rel_t = reloc;
8298 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8299 rel_t++)
8300 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8301 return rel_t;
8302
8303 return irelend;
8304 }
8305
8306 /* Find a relocation of specified type and offset.
8307 `reloc' is just a refence point to find a relocation at specified offset.
8308 If not found, return irelend.
8309
8310 Assuming relocations are sorted by r_offset,
8311 we find the relocation from `reloc' backward untill relocs,
8312 or find it from `reloc' forward untill irelend. */
8313
8314 static Elf_Internal_Rela *
8315 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8316 Elf_Internal_Rela *relocs,
8317 Elf_Internal_Rela *irelend,
8318 unsigned char reloc_type,
8319 bfd_vma offset_p)
8320 {
8321 Elf_Internal_Rela *rel_t = NULL;
8322
8323 /* First, we try to find a relocation of offset `offset_p',
8324 and then we use find_relocs_at_address to find specific type. */
8325
8326 if (reloc->r_offset > offset_p)
8327 {
8328 /* Find backward. */
8329 for (rel_t = reloc;
8330 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8331 /* Do nothing. */;
8332 }
8333 else if (reloc->r_offset < offset_p)
8334 {
8335 /* Find forward. */
8336 for (rel_t = reloc;
8337 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8338 /* Do nothing. */;
8339 }
8340 else
8341 rel_t = reloc;
8342
8343 /* Not found? */
8344 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8345 return irelend;
8346
8347 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8348 }
8349
8350 static bfd_boolean
8351 nds32_elf_check_dup_relocs (Elf_Internal_Rela *reloc,
8352 Elf_Internal_Rela *internal_relocs,
8353 Elf_Internal_Rela *irelend,
8354 unsigned char reloc_type)
8355 {
8356 Elf_Internal_Rela *rel_t;
8357
8358 for (rel_t = reloc;
8359 rel_t >= internal_relocs && rel_t->r_offset == reloc->r_offset;
8360 rel_t--)
8361 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8362 {
8363 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8364 && rel_t->r_addend == reloc->r_addend)
8365 continue;
8366 return TRUE;
8367 }
8368
8369 for (rel_t = reloc; rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8370 rel_t++)
8371 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8372 {
8373 if (ELF32_R_SYM (rel_t->r_info) == ELF32_R_SYM (reloc->r_info)
8374 && rel_t->r_addend == reloc->r_addend)
8375 continue;
8376 return TRUE;
8377 }
8378
8379 return FALSE;
8380 }
8381
8382 typedef struct nds32_elf_blank nds32_elf_blank_t;
8383 struct nds32_elf_blank
8384 {
8385 /* Where the blank begins. */
8386 bfd_vma offset;
8387 /* The size of the blank. */
8388 bfd_vma size;
8389 /* The accumulative size before this blank. */
8390 bfd_vma total_size;
8391 nds32_elf_blank_t *next;
8392 nds32_elf_blank_t *prev;
8393 };
8394
8395 static nds32_elf_blank_t *blank_free_list = NULL;
8396
8397 static nds32_elf_blank_t *
8398 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8399 {
8400 nds32_elf_blank_t *blank_t;
8401
8402 if (blank_free_list)
8403 {
8404 blank_t = blank_free_list;
8405 blank_free_list = blank_free_list->next;
8406 }
8407 else
8408 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
8409
8410 if (blank_t == NULL)
8411 return NULL;
8412
8413 blank_t->offset = offset_p;
8414 blank_t->size = size_p;
8415 blank_t->total_size = 0;
8416 blank_t->next = NULL;
8417 blank_t->prev = NULL;
8418
8419 return blank_t;
8420 }
8421
8422 static void
8423 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
8424 {
8425 if (blank_free_list)
8426 {
8427 blank_free_list->prev = blank_p;
8428 blank_p->next = blank_free_list;
8429 }
8430 else
8431 blank_p->next = NULL;
8432
8433 blank_p->prev = NULL;
8434 blank_free_list = blank_p;
8435 }
8436
8437 static void
8438 clean_nds32_elf_blank (void)
8439 {
8440 nds32_elf_blank_t *blank_t;
8441
8442 while (blank_free_list)
8443 {
8444 blank_t = blank_free_list;
8445 blank_free_list = blank_free_list->next;
8446 free (blank_t);
8447 }
8448 }
8449
8450 static nds32_elf_blank_t *
8451 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
8452 {
8453 nds32_elf_blank_t *blank_t;
8454
8455 if (!blank_p)
8456 return NULL;
8457 blank_t = blank_p;
8458
8459 while (blank_t && addr < blank_t->offset)
8460 blank_t = blank_t->prev;
8461 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
8462 blank_t = blank_t->next;
8463
8464 return blank_t;
8465 }
8466
8467 static bfd_vma
8468 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8469 int overwrite)
8470 {
8471 nds32_elf_blank_t *blank_t;
8472
8473 blank_t = search_nds32_elf_blank (*blank_p, addr);
8474 if (!blank_t)
8475 return 0;
8476
8477 if (overwrite)
8478 *blank_p = blank_t;
8479
8480 if (addr < blank_t->offset + blank_t->size)
8481 return blank_t->total_size + (addr - blank_t->offset);
8482 else
8483 return blank_t->total_size + blank_t->size;
8484 }
8485
8486 static bfd_boolean
8487 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
8488 {
8489 nds32_elf_blank_t *blank_t, *blank_t2;
8490
8491 if (!*blank_p)
8492 {
8493 *blank_p = create_nds32_elf_blank (addr, len);
8494 return *blank_p ? TRUE : FALSE;
8495 }
8496
8497 blank_t = search_nds32_elf_blank (*blank_p, addr);
8498
8499 if (blank_t == NULL)
8500 {
8501 blank_t = create_nds32_elf_blank (addr, len);
8502 if (!blank_t)
8503 return FALSE;
8504 while ((*blank_p)->prev != NULL)
8505 *blank_p = (*blank_p)->prev;
8506 blank_t->next = *blank_p;
8507 (*blank_p)->prev = blank_t;
8508 (*blank_p) = blank_t;
8509 return TRUE;
8510 }
8511
8512 if (addr < blank_t->offset + blank_t->size)
8513 {
8514 if (addr > blank_t->offset + blank_t->size)
8515 blank_t->size = addr - blank_t->offset;
8516 }
8517 else
8518 {
8519 blank_t2 = create_nds32_elf_blank (addr, len);
8520 if (!blank_t2)
8521 return FALSE;
8522 if (blank_t->next)
8523 {
8524 blank_t->next->prev = blank_t2;
8525 blank_t2->next = blank_t->next;
8526 }
8527 blank_t2->prev = blank_t;
8528 blank_t->next = blank_t2;
8529 *blank_p = blank_t2;
8530 }
8531
8532 return TRUE;
8533 }
8534
8535 static bfd_boolean
8536 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
8537 bfd_vma len)
8538 {
8539 nds32_elf_blank_t *blank_t;
8540
8541 if (!insert_nds32_elf_blank (blank_p, addr, len))
8542 return FALSE;
8543
8544 blank_t = *blank_p;
8545
8546 if (!blank_t->prev)
8547 {
8548 blank_t->total_size = 0;
8549 blank_t = blank_t->next;
8550 }
8551
8552 while (blank_t)
8553 {
8554 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
8555 blank_t = blank_t->next;
8556 }
8557
8558 return TRUE;
8559 }
8560
8561 static void
8562 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
8563 {
8564 nds32_elf_blank_t *blank_t;
8565 bfd_vma total_size = 0;
8566
8567 if (!blank_p)
8568 return;
8569
8570 blank_t = blank_p;
8571 while (blank_t->prev)
8572 blank_t = blank_t->prev;
8573 while (blank_t)
8574 {
8575 blank_t->total_size = total_size;
8576 total_size += blank_t->size;
8577 blank_t = blank_t->next;
8578 }
8579 }
8580
8581 static bfd_boolean
8582 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
8583 nds32_elf_blank_t *blank_p)
8584 {
8585 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
8586 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
8587 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
8588 unsigned int sec_shndx; /* The section the be relaxed. */
8589 bfd_byte *contents; /* Contents data of iterating section. */
8590 Elf_Internal_Rela *internal_relocs;
8591 Elf_Internal_Rela *irel;
8592 Elf_Internal_Rela *irelend;
8593 struct elf_link_hash_entry **sym_hashes;
8594 struct elf_link_hash_entry **end_hashes;
8595 unsigned int symcount;
8596 asection *sect;
8597 nds32_elf_blank_t *blank_t;
8598 nds32_elf_blank_t *blank_t2;
8599 nds32_elf_blank_t *blank_head;
8600
8601 blank_head = blank_t = blank_p;
8602 while (blank_head->prev != NULL)
8603 blank_head = blank_head->prev;
8604 while (blank_t->next != NULL)
8605 blank_t = blank_t->next;
8606
8607 if (blank_t->offset + blank_t->size <= sec->size)
8608 {
8609 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
8610 blank_t->next->prev = blank_t;
8611 }
8612 if (blank_head->offset > 0)
8613 {
8614 blank_head->prev = create_nds32_elf_blank (0, 0);
8615 blank_head->prev->next = blank_head;
8616 blank_head = blank_head->prev;
8617 }
8618
8619 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
8620
8621 /* The deletion must stop at the next ALIGN reloc for an alignment
8622 power larger than the number of bytes we are deleting. */
8623
8624 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8625 if (!nds32_get_local_syms (abfd, sec, &isym))
8626 return FALSE;
8627
8628 if (isym == NULL)
8629 {
8630 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8631 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
8632 symtab_hdr->contents = (bfd_byte *) isym;
8633 }
8634
8635 if (isym == NULL || symtab_hdr->sh_info == 0)
8636 return FALSE;
8637
8638 blank_t = blank_head;
8639 calc_nds32_blank_total (blank_head);
8640
8641 for (sect = abfd->sections; sect != NULL; sect = sect->next)
8642 {
8643 /* Adjust all the relocs. */
8644
8645 /* Relocations MUST be kept in memory, because relaxation adjust them. */
8646 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
8647 TRUE /* keep_memory */);
8648 irelend = internal_relocs + sect->reloc_count;
8649
8650 blank_t = blank_head;
8651 blank_t2 = blank_head;
8652
8653 if (!(sect->flags & SEC_RELOC))
8654 continue;
8655
8656 nds32_get_section_contents (abfd, sect, &contents);
8657
8658 for (irel = internal_relocs; irel < irelend; irel++)
8659 {
8660 bfd_vma raddr;
8661
8662 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
8663 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
8664 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8665 {
8666 unsigned long val = 0;
8667 unsigned long mask;
8668 long before, between;
8669 long offset;
8670
8671 switch (ELF32_R_TYPE (irel->r_info))
8672 {
8673 case R_NDS32_DIFF8:
8674 offset = bfd_get_8 (abfd, contents + irel->r_offset);
8675 break;
8676 case R_NDS32_DIFF16:
8677 offset = bfd_get_16 (abfd, contents + irel->r_offset);
8678 break;
8679 case R_NDS32_DIFF32:
8680 val = bfd_get_32 (abfd, contents + irel->r_offset);
8681 /* Get the signed bit and mask for the high part. The
8682 gcc will alarm when right shift 32-bit since the
8683 type size of long may be 32-bit. */
8684 mask = 0 - (val >> 31);
8685 if (mask)
8686 offset = (val | (mask - 0xffffffff));
8687 else
8688 offset = val;
8689 break;
8690 default:
8691 BFD_ASSERT (0);
8692 }
8693
8694 /* DIFF value
8695 0 |encoded in location|
8696 |------------|-------------------|---------
8697 sym+off(addend)
8698 -- before ---| *****************
8699 --------------------- between ---|
8700
8701 We only care how much data are relax between DIFF,
8702 marked as ***. */
8703
8704 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8705 between = get_nds32_elf_blank_total (&blank_t,
8706 irel->r_addend + offset, 0);
8707 if (between == before)
8708 goto done_adjust_diff;
8709
8710 switch (ELF32_R_TYPE (irel->r_info))
8711 {
8712 case R_NDS32_DIFF8:
8713 bfd_put_8 (abfd, offset - (between - before),
8714 contents + irel->r_offset);
8715 break;
8716 case R_NDS32_DIFF16:
8717 bfd_put_16 (abfd, offset - (between - before),
8718 contents + irel->r_offset);
8719 break;
8720 case R_NDS32_DIFF32:
8721 bfd_put_32 (abfd, offset - (between - before),
8722 contents + irel->r_offset);
8723 break;
8724 }
8725 }
8726 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
8727 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
8728 {
8729 bfd_vma val = 0;
8730 unsigned int len = 0;
8731 unsigned long before, between;
8732 bfd_byte *endp, *p;
8733
8734 val = read_unsigned_leb128 (abfd, contents + irel->r_offset,
8735 &len);
8736
8737 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
8738 between = get_nds32_elf_blank_total (&blank_t,
8739 irel->r_addend + val, 0);
8740 if (between == before)
8741 goto done_adjust_diff;
8742
8743 p = contents + irel->r_offset;
8744 endp = p + len -1;
8745 memset (p, 0x80, len);
8746 *(endp) = 0;
8747 p = write_uleb128 (p, val - (between - before)) - 1;
8748 if (p < endp)
8749 *p |= 0x80;
8750 }
8751 done_adjust_diff:
8752
8753 if (sec == sect)
8754 {
8755 raddr = irel->r_offset;
8756 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
8757 irel->r_offset, 1);
8758
8759 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
8760 continue;
8761 if (blank_t2 && blank_t2->next
8762 && (blank_t2->offset > raddr
8763 || blank_t2->next->offset <= raddr))
8764 (*_bfd_error_handler)
8765 (_("%B: %s\n"), abfd,
8766 "Error: search_nds32_elf_blank reports wrong node");
8767
8768 /* Mark reloc in deleted portion as NONE.
8769 For some relocs like R_NDS32_LABEL that doesn't modify the
8770 content in the section. R_NDS32_LABEL doesn't belong to the
8771 instruction in the section, so we should preserve it. */
8772 if (raddr >= blank_t2->offset
8773 && raddr < blank_t2->offset + blank_t2->size
8774 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
8775 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
8776 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
8777 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
8778 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
8779 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
8780 {
8781 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
8782 R_NDS32_NONE);
8783 continue;
8784 }
8785 }
8786
8787 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
8788 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
8789 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
8790 continue;
8791
8792 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
8793 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
8794 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
8795 {
8796 if (irel->r_addend <= sec->size)
8797 irel->r_addend -=
8798 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
8799 }
8800 }
8801 }
8802
8803 /* Adjust the local symbols defined in this section. */
8804 blank_t = blank_head;
8805 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
8806 {
8807 if (isym->st_shndx == sec_shndx)
8808 {
8809 if (isym->st_value <= sec->size)
8810 {
8811 bfd_vma ahead;
8812 bfd_vma orig_addr = isym->st_value;
8813
8814 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
8815 isym->st_value -= ahead;
8816
8817 /* Adjust function size. */
8818 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
8819 && isym->st_size > 0)
8820 isym->st_size -=
8821 get_nds32_elf_blank_total
8822 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
8823 }
8824 }
8825 }
8826
8827 /* Now adjust the global symbols defined in this section. */
8828 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
8829 - symtab_hdr->sh_info);
8830 sym_hashes = elf_sym_hashes (abfd);
8831 end_hashes = sym_hashes + symcount;
8832 blank_t = blank_head;
8833 for (; sym_hashes < end_hashes; sym_hashes++)
8834 {
8835 struct elf_link_hash_entry *sym_hash = *sym_hashes;
8836
8837 if ((sym_hash->root.type == bfd_link_hash_defined
8838 || sym_hash->root.type == bfd_link_hash_defweak)
8839 && sym_hash->root.u.def.section == sec)
8840 {
8841 if (sym_hash->root.u.def.value <= sec->size)
8842 {
8843 bfd_vma ahead;
8844 bfd_vma orig_addr = sym_hash->root.u.def.value;
8845
8846 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
8847 sym_hash->root.u.def.value -= ahead;
8848
8849 /* Adjust function size. */
8850 if (sym_hash->type == STT_FUNC)
8851 sym_hash->size -=
8852 get_nds32_elf_blank_total
8853 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
8854
8855 }
8856 }
8857 }
8858
8859 contents = elf_section_data (sec)->this_hdr.contents;
8860 blank_t = blank_head;
8861 while (blank_t->next)
8862 {
8863 /* Actually delete the bytes. */
8864
8865 /* If current blank is the last blank overlap with current section,
8866 go to finish process. */
8867 if (sec->size <= (blank_t->next->offset))
8868 break;
8869
8870 memmove (contents + blank_t->offset - blank_t->total_size,
8871 contents + blank_t->offset + blank_t->size,
8872 blank_t->next->offset - (blank_t->offset + blank_t->size));
8873
8874 blank_t = blank_t->next;
8875 }
8876
8877 if (sec->size > (blank_t->offset + blank_t->size))
8878 {
8879 /* There are remaining code between blank and section boundary.
8880 Move the remaining code to appropriate location. */
8881 memmove (contents + blank_t->offset - blank_t->total_size,
8882 contents + blank_t->offset + blank_t->size,
8883 sec->size - (blank_t->offset + blank_t->size));
8884 sec->size -= blank_t->total_size + blank_t->size;
8885 }
8886 else
8887 /* This blank is not entirely included in the section,
8888 reduce the section size by only part of the blank size. */
8889 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
8890
8891 while (blank_head)
8892 {
8893 blank_t = blank_head;
8894 blank_head = blank_head->next;
8895 remove_nds32_elf_blank (blank_t);
8896 }
8897
8898 return TRUE;
8899 }
8900
8901 /* Get the contents of a section. */
8902
8903 static int
8904 nds32_get_section_contents (bfd *abfd, asection *sec, bfd_byte **contents_p)
8905 {
8906 /* Get the section contents. */
8907 if (elf_section_data (sec)->this_hdr.contents != NULL)
8908 *contents_p = elf_section_data (sec)->this_hdr.contents;
8909 else
8910 {
8911 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
8912 return FALSE;
8913 elf_section_data (sec)->this_hdr.contents = *contents_p;
8914 }
8915
8916 return TRUE;
8917 }
8918
8919 /* Get the contents of the internal symbol of abfd. */
8920
8921 static int
8922 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
8923 Elf_Internal_Sym **isymbuf_p)
8924 {
8925 Elf_Internal_Shdr *symtab_hdr;
8926 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8927
8928 /* Read this BFD's local symbols if we haven't done so already. */
8929 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
8930 {
8931 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
8932 if (*isymbuf_p == NULL)
8933 {
8934 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
8935 symtab_hdr->sh_info, 0,
8936 NULL, NULL, NULL);
8937 if (*isymbuf_p == NULL)
8938 return FALSE;
8939 }
8940 }
8941 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
8942
8943 return TRUE;
8944 }
8945
8946 /* Range of small data. */
8947 static bfd_vma sdata_range[2][2];
8948 static bfd_vma const sdata_init_range[2] =
8949 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
8950
8951 static int
8952 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
8953 bfd_byte *contents, bfd_vma addr)
8954 {
8955 unsigned long insn = bfd_getb32 (contents + addr);
8956
8957 if (insn & 0x80000000)
8958 return 2;
8959
8960 return 4;
8961 }
8962
8963 /* Set the gp relax range. We have to measure the safe range
8964 to do gp relaxation. */
8965
8966 static void
8967 relax_range_measurement (bfd *abfd)
8968 {
8969 asection *sec_f, *sec_b;
8970 /* For upper bound. */
8971 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
8972 bfd_vma align;
8973 static int decide_relax_range = 0;
8974 int i;
8975 int range_number = sizeof (sdata_init_range) / sizeof (sdata_init_range[0]);
8976
8977 if (decide_relax_range)
8978 return;
8979 decide_relax_range = 1;
8980
8981 if (sda_rela_sec == NULL)
8982 {
8983 /* Since there is no data sections, we assume the range is page size. */
8984 for (i = 0; i < range_number; i++)
8985 {
8986 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
8987 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
8988 }
8989 return;
8990 }
8991
8992 /* Get the biggest alignment power after the gp located section. */
8993 sec_f = sda_rela_sec->output_section;
8994 sec_b = sec_f->next;
8995 align = 0;
8996 while (sec_b != NULL)
8997 {
8998 if ((unsigned)(1 << sec_b->alignment_power) > align)
8999 align = (1 << sec_b->alignment_power);
9000 sec_b = sec_b->next;
9001 }
9002
9003 /* I guess we can not determine the section before
9004 gp located section, so we assume the align is max page size. */
9005 for (i = 0; i < range_number; i++)
9006 {
9007 sdata_range[i][1] = sdata_init_range[i] - align;
9008 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9009 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9010 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9011 }
9012 }
9013
9014 /* These are macros used to check flags encoded in r_addend.
9015 They are only used by nds32_elf_relax_section (). */
9016 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9017 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9018 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9019 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9020
9021 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9022
9023 static bfd_boolean
9024 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9025 Elf_Internal_Rela *internal_relocs, int *insn_len,
9026 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9027 Elf_Internal_Shdr *symtab_hdr)
9028 {
9029 /* There are 3 variations for LONGCALL1
9030 case 4-4-2; 16-bit on, optimize off or optimize for space
9031 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9032 ori ta, ta, lo12(symbol) ; LO12S0
9033 jral5 ta ;
9034
9035 case 4-4-4; 16-bit off, optimize don't care
9036 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9037 ori ta, ta, lo12(symbol) ; LO12S0
9038 jral ta ;
9039
9040 case 4-4-4; 16-bit on, optimize for speed
9041 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9042 ori ta, ta, lo12(symbol) ; LO12S0
9043 jral ta ;
9044 Check code for -mlong-calls output. */
9045
9046 /* Get the reloc for the address from which the register is
9047 being loaded. This reloc will tell us which function is
9048 actually being called. */
9049
9050 bfd_vma laddr;
9051 int seq_len; /* Original length of instruction sequence. */
9052 uint32_t insn;
9053 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9054 int pic_ext_target = 0;
9055 bfd_signed_vma foff;
9056 uint16_t insn16;
9057
9058 irelend = internal_relocs + sec->reloc_count;
9059 seq_len = GET_SEQ_LEN (irel->r_addend);
9060 laddr = irel->r_offset;
9061 *insn_len = seq_len;
9062
9063 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9064 R_NDS32_HI20_RELA, laddr);
9065 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9066 R_NDS32_LO12S0_ORI_RELA,
9067 laddr + 4);
9068
9069 if (hi_irelfn == irelend || lo_irelfn == irelend)
9070 {
9071 (*_bfd_error_handler)
9072 ("%B: warning: R_NDS32_LONGCALL1 points to unrecognized"
9073 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
9074 return FALSE;
9075 }
9076
9077 /* Get the value of the symbol referred to by the reloc. */
9078 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9079 &pic_ext_target);
9080
9081 /* This condition only happened when symbol is undefined. */
9082 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9083 || foff >= CONSERVATIVE_24BIT_S1)
9084 return FALSE;
9085
9086 /* Relax to: jal symbol; 25_PCREL */
9087 /* For simplicity of coding, we are going to modify the section
9088 contents, the section relocs, and the BFD symbol table. We
9089 must tell the rest of the code not to free up this
9090 information. It would be possible to instead create a table
9091 of changes which have to be made, as is done in coff-mips.c;
9092 that would be more work, but would require less memory when
9093 the linker is run. */
9094
9095 /* Replace the long call with a jal. */
9096 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9097 R_NDS32_25_PCREL_RELA);
9098 irel->r_addend = hi_irelfn->r_addend;
9099
9100 /* We don't resolve this here but resolve it in relocate_section. */
9101 insn = INSN_JAL;
9102 bfd_putb32 (insn, contents + irel->r_offset);
9103
9104 hi_irelfn->r_info =
9105 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9106 lo_irelfn->r_info =
9107 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9108 *insn_len = 4;
9109
9110 if (seq_len & 0x2)
9111 {
9112 insn16 = NDS32_NOP16;
9113 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9114 lo_irelfn->r_info =
9115 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9116 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9117 *insn_len += 2;
9118 }
9119 return TRUE;
9120 }
9121
9122 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9123 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
9124
9125 static bfd_boolean
9126 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9127 Elf_Internal_Rela *internal_relocs, int *insn_len,
9128 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9129 Elf_Internal_Shdr *symtab_hdr)
9130 {
9131 /* bltz rt, .L1 ; LONGCALL2
9132 jal symbol ; 25_PCREL
9133 .L1: */
9134
9135 /* Get the reloc for the address from which the register is
9136 being loaded. This reloc will tell us which function is
9137 actually being called. */
9138
9139 bfd_vma laddr;
9140 uint32_t insn;
9141 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9142 int pic_ext_target = 0;
9143 bfd_signed_vma foff;
9144
9145 irelend = internal_relocs + sec->reloc_count;
9146 laddr = irel->r_offset;
9147 i1_irelfn =
9148 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9149 R_NDS32_25_PCREL_RELA, laddr + 4);
9150
9151 if (i1_irelfn == irelend)
9152 {
9153 (*_bfd_error_handler)
9154 ("%B: warning: R_NDS32_LONGCALL2 points to unrecognized"
9155 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
9156 return FALSE;
9157 }
9158
9159 insn = bfd_getb32 (contents + laddr);
9160
9161 /* Get the value of the symbol referred to by the reloc. */
9162 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr,
9163 &pic_ext_target);
9164
9165 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9166 || foff >= CONSERVATIVE_16BIT_S1)
9167 return FALSE;
9168
9169 /* Relax to bgezal rt, label ; 17_PCREL
9170 or bltzal rt, label ; 17_PCREL */
9171
9172 /* Convert to complimentary conditional call. */
9173 insn = CONVERT_CONDITION_CALL (insn);
9174
9175 /* For simplicity of coding, we are going to modify the section
9176 contents, the section relocs, and the BFD symbol table. We
9177 must tell the rest of the code not to free up this
9178 information. It would be possible to instead create a table
9179 of changes which have to be made, as is done in coff-mips.c;
9180 that would be more work, but would require less memory when
9181 the linker is run. */
9182
9183 /* Clean unnessary relocations. */
9184 i1_irelfn->r_info =
9185 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9186 cond_irelfn =
9187 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9188 R_NDS32_17_PCREL_RELA, laddr);
9189 if (cond_irelfn != irelend)
9190 cond_irelfn->r_info =
9191 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9192
9193 /* Replace the long call with a bgezal. */
9194 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9195 R_NDS32_17_PCREL_RELA);
9196 irel->r_addend = i1_irelfn->r_addend;
9197
9198 bfd_putb32 (insn, contents + irel->r_offset);
9199
9200 *insn_len = 4;
9201 return TRUE;
9202 }
9203
9204 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9205
9206 static bfd_boolean
9207 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9208 Elf_Internal_Rela *internal_relocs, int *insn_len,
9209 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9210 Elf_Internal_Shdr *symtab_hdr)
9211 {
9212 /* There are 3 variations for LONGCALL3
9213 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9214 bltz rt, $1 ; LONGCALL3
9215 sethi ta, hi20(symbol) ; HI20
9216 ori ta, ta, lo12(symbol) ; LO12S0
9217 jral5 ta ;
9218 $1
9219
9220 case 4-4-4-4; 16-bit off, optimize don't care
9221 bltz rt, $1 ; LONGCALL3
9222 sethi ta, hi20(symbol) ; HI20
9223 ori ta, ta, lo12(symbol) ; LO12S0
9224 jral ta ;
9225 $1
9226
9227 case 4-4-4-4; 16-bit on, optimize for speed
9228 bltz rt, $1 ; LONGCALL3
9229 sethi ta, hi20(symbol) ; HI20
9230 ori ta, ta, lo12(symbol) ; LO12S0
9231 jral ta ;
9232 $1 */
9233
9234 /* Get the reloc for the address from which the register is
9235 being loaded. This reloc will tell us which function is
9236 actually being called. */
9237
9238 bfd_vma laddr;
9239 int seq_len; /* Original length of instruction sequence. */
9240 uint32_t insn;
9241 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9242 int pic_ext_target = 0;
9243 bfd_signed_vma foff;
9244 uint16_t insn16;
9245
9246 irelend = internal_relocs + sec->reloc_count;
9247 seq_len = GET_SEQ_LEN (irel->r_addend);
9248 laddr = irel->r_offset;
9249 *insn_len = seq_len;
9250
9251 hi_irelfn =
9252 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9253 R_NDS32_HI20_RELA, laddr + 4);
9254 lo_irelfn =
9255 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9256 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9257
9258 if (hi_irelfn == irelend || lo_irelfn == irelend)
9259 {
9260 (*_bfd_error_handler)
9261 ("%B: warning: R_NDS32_LONGCALL3 points to unrecognized"
9262 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
9263 return FALSE;
9264 }
9265
9266 /* Get the value of the symbol referred to by the reloc. */
9267 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9268 &pic_ext_target);
9269
9270 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9271 || foff >= CONSERVATIVE_24BIT_S1)
9272 return FALSE;
9273
9274 insn = bfd_getb32 (contents + laddr);
9275 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9276 {
9277 /* Relax to bgezal rt, label ; 17_PCREL
9278 or bltzal rt, label ; 17_PCREL */
9279
9280 /* Convert to complimentary conditional call. */
9281 insn = CONVERT_CONDITION_CALL (insn);
9282 bfd_putb32 (insn, contents + irel->r_offset);
9283
9284 *insn_len = 4;
9285 irel->r_info =
9286 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9287 hi_irelfn->r_info =
9288 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9289 lo_irelfn->r_info =
9290 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9291
9292 cond_irelfn =
9293 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9294 R_NDS32_17_PCREL_RELA, laddr);
9295 if (cond_irelfn != irelend)
9296 {
9297 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9298 R_NDS32_17_PCREL_RELA);
9299 cond_irelfn->r_addend = hi_irelfn->r_addend;
9300 }
9301
9302 if (seq_len & 0x2)
9303 {
9304 insn16 = NDS32_NOP16;
9305 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9306 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9307 R_NDS32_INSN16);
9308 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9309 insn_len += 2;
9310 }
9311 }
9312 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9313 {
9314 /* Relax to the following instruction sequence
9315 bltz rt, $1 ; LONGCALL2
9316 jal symbol ; 25_PCREL
9317 $1 */
9318 *insn_len = 8;
9319 insn = INSN_JAL;
9320 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9321
9322 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9323 R_NDS32_25_PCREL_RELA);
9324 irel->r_info =
9325 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9326
9327 lo_irelfn->r_info =
9328 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9329
9330 if (seq_len & 0x2)
9331 {
9332 insn16 = NDS32_NOP16;
9333 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9334 lo_irelfn->r_info =
9335 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9336 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9337 insn_len += 2;
9338 }
9339 }
9340 return TRUE;
9341 }
9342
9343 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
9344
9345 static bfd_boolean
9346 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9347 Elf_Internal_Rela *internal_relocs, int *insn_len,
9348 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9349 Elf_Internal_Shdr *symtab_hdr)
9350 {
9351 /* There are 3 variations for LONGJUMP1
9352 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9353 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9354 ori ta, ta, lo12(symbol) ; LO12S0
9355 jr5 ta ;
9356
9357 case 4-4-4; 16-bit off, optimize don't care
9358 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9359 ori ta, ta, lo12(symbol) ; LO12S0
9360 jr ta ;
9361
9362 case 4-4-4; 16-bit on, optimize for speed
9363 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9364 ori ta, ta, lo12(symbol) ; LO12S0
9365 jr ta ; */
9366
9367 /* Get the reloc for the address from which the register is
9368 being loaded. This reloc will tell us which function is
9369 actually being called. */
9370
9371 bfd_vma laddr;
9372 int seq_len; /* Original length of instruction sequence. */
9373 int insn16_on; /* 16-bit on/off. */
9374 uint32_t insn;
9375 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9376 int pic_ext_target = 0;
9377 bfd_signed_vma foff;
9378 uint16_t insn16;
9379 unsigned long reloc;
9380
9381 irelend = internal_relocs + sec->reloc_count;
9382 seq_len = GET_SEQ_LEN (irel->r_addend);
9383 laddr = irel->r_offset;
9384 *insn_len = seq_len;
9385 insn16_on = IS_16BIT_ON (irel->r_addend);
9386
9387 hi_irelfn =
9388 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9389 R_NDS32_HI20_RELA, laddr);
9390 lo_irelfn =
9391 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9392 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9393 if (hi_irelfn == irelend || lo_irelfn == irelend)
9394 {
9395 (*_bfd_error_handler)
9396 ("%B: warning: R_NDS32_LONGJUMP1 points to unrecognized"
9397 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
9398 return FALSE;
9399 }
9400
9401 /* Get the value of the symbol referred to by the reloc. */
9402 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9403 &pic_ext_target);
9404
9405 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
9406 || foff < -CONSERVATIVE_24BIT_S1)
9407 return FALSE;
9408
9409 if (insn16_on && foff >= -ACCURATE_8BIT_S1
9410 && foff < ACCURATE_8BIT_S1 && (seq_len & 0x2))
9411 {
9412 /* j8 label */
9413 /* 16-bit on, but not optimized for speed. */
9414 reloc = R_NDS32_9_PCREL_RELA;
9415 insn16 = INSN_J8;
9416 bfd_putb16 (insn16, contents + irel->r_offset);
9417 *insn_len = 2;
9418 irel->r_info =
9419 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9420 }
9421 else
9422 {
9423 /* j label */
9424 reloc = R_NDS32_25_PCREL_RELA;
9425 insn = INSN_J;
9426 bfd_putb32 (insn, contents + irel->r_offset);
9427 *insn_len = 4;
9428 irel->r_info =
9429 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9430 irel->r_addend = 0;
9431 }
9432
9433 hi_irelfn->r_info =
9434 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9435 lo_irelfn->r_info =
9436 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9437
9438 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
9439 {
9440 insn16 = NDS32_NOP16;
9441 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9442 lo_irelfn->r_info =
9443 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9444 R_NDS32_INSN16);
9445 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9446 *insn_len += 2;
9447 }
9448 return TRUE;
9449 }
9450
9451 /* Revert condition branch. This function does not check if the input
9452 instruction is condition branch or not. */
9453
9454 static void
9455 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
9456 uint16_t *re_insn16, uint32_t *re_insn)
9457 {
9458 uint32_t comp_insn = 0;
9459 uint16_t comp_insn16 = 0;
9460
9461 if (insn)
9462 {
9463 if (N32_OP6 (insn) == N32_OP6_BR1)
9464 {
9465 /* beqs label. */
9466 comp_insn = (insn ^ 0x4000) & 0xffffc000;
9467 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
9468 {
9469 /* Insn can be contracted to 16-bit implied r5. */
9470 comp_insn16 =
9471 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
9472 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9473 }
9474 }
9475 else if (N32_OP6 (insn) == N32_OP6_BR3)
9476 {
9477 /* bnec $ta, imm11, label. */
9478 comp_insn = (insn ^ 0x80000) & 0xffffff00;
9479 }
9480 else
9481 {
9482 comp_insn = (insn ^ 0x10000) & 0xffffc000;
9483 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
9484 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
9485 {
9486 if (N32_IS_RT3 (insn))
9487 {
9488 /* Insn can be contracted to 16-bit. */
9489 comp_insn16 =
9490 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
9491 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
9492 }
9493 else if (N32_RT5 (insn) == REG_R15)
9494 {
9495 /* Insn can be contracted to 16-bit. */
9496 comp_insn16 =
9497 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
9498 }
9499 }
9500 }
9501 }
9502 else
9503 {
9504 switch ((insn16 & 0xf000) >> 12)
9505 {
9506 case 0xc:
9507 /* beqz38 or bnez38 */
9508 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9509 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
9510 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
9511 break;
9512
9513 case 0xd:
9514 /* beqs38 or bnes38 */
9515 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
9516 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
9517 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
9518 | (REG_R5 << 15);
9519 break;
9520
9521 case 0xe:
9522 /* beqzS8 or bnezS8 */
9523 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
9524 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
9525 comp_insn |= REG_R15 << 20;
9526 break;
9527
9528 default:
9529 break;
9530 }
9531 }
9532 if (comp_insn && re_insn)
9533 *re_insn = comp_insn;
9534 if (comp_insn16 && re_insn16)
9535 *re_insn16 = comp_insn16;
9536 }
9537
9538 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
9539
9540 static bfd_boolean
9541 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9542 Elf_Internal_Rela *internal_relocs, int *insn_len,
9543 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9544 Elf_Internal_Shdr *symtab_hdr)
9545 {
9546 /* There are 3 variations for LONGJUMP2
9547 case 2-4; 1st insn convertible, 16-bit on,
9548 optimize off or optimize for space
9549 bnes38 rt, ra, $1 ; LONGJUMP2
9550 j label ; 25_PCREL
9551 $1:
9552
9553 case 4-4; 1st insn not convertible
9554 bne rt, ra, $1 ; LONGJUMP2
9555 j label ; 25_PCREL
9556 $1:
9557
9558 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9559 bne rt, ra, $1 ; LONGJUMP2
9560 j label ; 25_PCREL
9561 $1: */
9562
9563 /* Get the reloc for the address from which the register is
9564 being loaded. This reloc will tell us which function is
9565 actually being called. */
9566
9567 bfd_vma laddr;
9568 int seq_len; /* Original length of instruction sequence. */
9569 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
9570 int pic_ext_target = 0, first_size;
9571 unsigned int i;
9572 bfd_signed_vma foff;
9573 uint32_t insn, re_insn = 0;
9574 uint16_t insn16, re_insn16 = 0;
9575 unsigned long reloc, cond_reloc;
9576
9577 enum elf_nds32_reloc_type checked_types[] =
9578 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9579
9580 irelend = internal_relocs + sec->reloc_count;
9581 seq_len = GET_SEQ_LEN (irel->r_addend);
9582 laddr = irel->r_offset;
9583 *insn_len = seq_len;
9584 first_size = (seq_len == 6) ? 2 : 4;
9585
9586 i2_irelfn =
9587 find_relocs_at_address_addr (irel, internal_relocs,
9588 irelend, R_NDS32_25_PCREL_RELA,
9589 laddr + first_size);
9590
9591 for (i = 0; i < sizeof (checked_types) / sizeof(checked_types[0]); i++)
9592 {
9593 cond_irelfn =
9594 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9595 checked_types[i], laddr);
9596 if (cond_irelfn != irelend)
9597 break;
9598 }
9599
9600 if (i2_irelfn == irelend || cond_irelfn == irelend)
9601 {
9602 (*_bfd_error_handler)
9603 ("%B: warning: R_NDS32_LONGJUMP2 points to unrecognized"
9604 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
9605 return FALSE;
9606 }
9607
9608 /* Get the value of the symbol referred to by the reloc. */
9609 foff =
9610 calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr,
9611 &pic_ext_target);
9612 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
9613 || foff >= CONSERVATIVE_16BIT_S1)
9614 return FALSE;
9615
9616 /* Get the all corresponding instructions. */
9617 if (first_size == 4)
9618 {
9619 insn = bfd_getb32 (contents + laddr);
9620 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9621 }
9622 else
9623 {
9624 insn16 = bfd_getb16 (contents + laddr);
9625 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9626 }
9627
9628 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
9629 && foff < ACCURATE_8BIT_S1 - first_size)
9630 {
9631 if (first_size == 4)
9632 {
9633 /* Don't convert it to 16-bit now, keep this as relaxable for
9634 ``label reloc; INSN16''. */
9635
9636 /* Save comp_insn32 to buffer. */
9637 bfd_putb32 (re_insn, contents + irel->r_offset);
9638 *insn_len = 4;
9639 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9640 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9641 cond_reloc = R_NDS32_INSN16;
9642 }
9643 else
9644 {
9645 bfd_putb16 (re_insn16, contents + irel->r_offset);
9646 *insn_len = 2;
9647 reloc = R_NDS32_9_PCREL_RELA;
9648 cond_reloc = R_NDS32_NONE;
9649 }
9650 }
9651 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9652 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9653 && foff < ACCURATE_14BIT_S1 - first_size))
9654 {
9655 /* beqs label ; 15_PCREL */
9656 bfd_putb32 (re_insn, contents + irel->r_offset);
9657 *insn_len = 4;
9658 reloc = R_NDS32_15_PCREL_RELA;
9659 cond_reloc = R_NDS32_NONE;
9660 }
9661 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9662 && foff >= -CONSERVATIVE_16BIT_S1
9663 && foff < CONSERVATIVE_16BIT_S1)
9664 {
9665 /* beqz label ; 17_PCREL */
9666 bfd_putb32 (re_insn, contents + irel->r_offset);
9667 *insn_len = 4;
9668 reloc = R_NDS32_17_PCREL_RELA;
9669 cond_reloc = R_NDS32_NONE;
9670 }
9671 else
9672 return FALSE;
9673
9674 /* Set all relocations. */
9675 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
9676 irel->r_addend = i2_irelfn->r_addend;
9677
9678 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9679 cond_reloc);
9680 cond_irelfn->r_addend = 0;
9681
9682 if ((seq_len ^ *insn_len ) & 0x2)
9683 {
9684 insn16 = NDS32_NOP16;
9685 bfd_putb16 (insn16, contents + irel->r_offset + 4);
9686 i2_irelfn->r_offset = 4;
9687 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9688 R_NDS32_INSN16);
9689 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9690 *insn_len += 2;
9691 }
9692 else
9693 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
9694 R_NDS32_NONE);
9695 return TRUE;
9696 }
9697
9698 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
9699
9700 static bfd_boolean
9701 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9702 Elf_Internal_Rela *internal_relocs, int *insn_len,
9703 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9704 Elf_Internal_Shdr *symtab_hdr)
9705 {
9706 /* There are 5 variations for LONGJUMP3
9707 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
9708 optimize off or optimize for space
9709 bnes38 rt, ra, $1 ; LONGJUMP3
9710 sethi ta, hi20(symbol) ; HI20
9711 ori ta, ta, lo12(symbol) ; LO12S0
9712 jr5 ta ;
9713 $1: ;
9714
9715 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
9716 bnes38 rt, ra, $1 ; LONGJUMP3
9717 sethi ta, hi20(symbol) ; HI20
9718 ori ta, ta, lo12(symbol) ; LO12S0
9719 jr5 ta ;
9720 $1: ; LABEL
9721
9722 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
9723 optimize off or optimize for space
9724 bne rt, ra, $1 ; LONGJUMP3
9725 sethi ta, hi20(symbol) ; HI20
9726 ori ta, ta, lo12(symbol) ; LO12S0
9727 jr5 ta ;
9728 $1: ;
9729
9730 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
9731 16-bit off if no INSN16
9732 bne rt, ra, $1 ; LONGJUMP3
9733 sethi ta, hi20(symbol) ; HI20
9734 ori ta, ta, lo12(symbol) ; LO12S0
9735 jr ta ;
9736 $1: ;
9737
9738 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
9739 16-bit off if no INSN16
9740 bne rt, ra, $1 ; LONGJUMP3
9741 sethi ta, hi20(symbol) ; HI20
9742 ori ta, ta, lo12(symbol) ; LO12S0
9743 jr ta ;
9744 $1: ; LABEL */
9745
9746 /* Get the reloc for the address from which the register is
9747 being loaded. This reloc will tell us which function is
9748 actually being called. */
9749 enum elf_nds32_reloc_type checked_types[] =
9750 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
9751
9752 int reloc_off = 0, cond_removed = 0, convertible;
9753 bfd_vma laddr;
9754 int seq_len; /* Original length of instruction sequence. */
9755 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9756 int pic_ext_target = 0, first_size;
9757 unsigned int i;
9758 bfd_signed_vma foff;
9759 uint32_t insn, re_insn = 0;
9760 uint16_t insn16, re_insn16 = 0;
9761 unsigned long reloc, cond_reloc;
9762
9763 irelend = internal_relocs + sec->reloc_count;
9764 seq_len = GET_SEQ_LEN (irel->r_addend);
9765 laddr = irel->r_offset;
9766 *insn_len = seq_len;
9767
9768 convertible = IS_1ST_CONVERT (irel->r_addend);
9769
9770 if (convertible)
9771 first_size = 2;
9772 else
9773 first_size = 4;
9774
9775 /* Get all needed relocations. */
9776 hi_irelfn =
9777 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9778 R_NDS32_HI20_RELA, laddr + first_size);
9779 lo_irelfn =
9780 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9781 R_NDS32_LO12S0_ORI_RELA,
9782 laddr + first_size + 4);
9783
9784 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
9785 {
9786 cond_irelfn =
9787 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9788 checked_types[i], laddr);
9789 if (cond_irelfn != irelend)
9790 break;
9791 }
9792
9793 if (hi_irelfn == irelend || lo_irelfn == irelend || cond_irelfn == irelend)
9794 {
9795 (*_bfd_error_handler)
9796 ("%B: warning: R_NDS32_LONGJUMP3 points to unrecognized"
9797 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
9798 return FALSE;
9799 }
9800
9801 /* Get the value of the symbol referred to by the reloc. */
9802 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr,
9803 &pic_ext_target);
9804
9805 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9806 || foff >= CONSERVATIVE_24BIT_S1)
9807 return FALSE;
9808
9809 /* Get the all corresponding instructions. */
9810 if (first_size == 4)
9811 {
9812 insn = bfd_getb32 (contents + laddr);
9813 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
9814 }
9815 else
9816 {
9817 insn16 = bfd_getb16 (contents + laddr);
9818 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
9819 }
9820
9821 /* For simplicity of coding, we are going to modify the section
9822 contents, the section relocs, and the BFD symbol table. We
9823 must tell the rest of the code not to free up this
9824 information. It would be possible to instead create a table
9825 of changes which have to be made, as is done in coff-mips.c;
9826 that would be more work, but would require less memory when
9827 the linker is run. */
9828
9829 if (re_insn16 && foff >= -ACCURATE_8BIT_S1 - first_size
9830 && foff < ACCURATE_8BIT_S1 - first_size)
9831 {
9832 if (!(seq_len & 0x2))
9833 {
9834 /* Don't convert it to 16-bit now, keep this as relaxable
9835 for ``label reloc; INSN1a''6. */
9836 /* Save comp_insn32 to buffer. */
9837 bfd_putb32 (re_insn, contents + irel->r_offset);
9838 *insn_len = 4;
9839 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
9840 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
9841 cond_reloc = R_NDS32_INSN16;
9842 }
9843 else
9844 {
9845 /* Not optimize for speed; convert sequence to 16-bit. */
9846 /* Save comp_insn16 to buffer. */
9847 bfd_putb16 (re_insn16, contents + irel->r_offset);
9848 *insn_len = 2;
9849 reloc = R_NDS32_9_PCREL_RELA;
9850 cond_reloc = R_NDS32_NONE;
9851 }
9852 cond_removed = 1;
9853 }
9854 else if (N32_OP6 (re_insn) == N32_OP6_BR1
9855 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
9856 && foff < ACCURATE_14BIT_S1 - first_size))
9857 {
9858 /* beqs label ; 15_PCREL */
9859 bfd_putb32 (re_insn, contents + irel->r_offset);
9860 *insn_len = 4;
9861 reloc = R_NDS32_15_PCREL_RELA;
9862 cond_reloc = R_NDS32_NONE;
9863 cond_removed = 1;
9864 }
9865 else if (N32_OP6 (re_insn) == N32_OP6_BR2
9866 && foff >= -CONSERVATIVE_16BIT_S1
9867 && foff < CONSERVATIVE_16BIT_S1)
9868 {
9869 /* beqz label ; 17_PCREL */
9870 bfd_putb32 (re_insn, contents + irel->r_offset);
9871 *insn_len = 4;
9872 reloc = R_NDS32_17_PCREL_RELA;
9873 cond_reloc = R_NDS32_NONE;
9874 cond_removed = 1;
9875 }
9876 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
9877 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
9878 {
9879 /* Relax to one of the following 3 variations
9880
9881 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
9882 for space
9883 bnes38 rt, $1 ; LONGJUMP2
9884 j label ; 25_PCREL
9885 $1
9886
9887 case 4-4; 1st insn not convertible, others don't care
9888 bne rt, ra, $1 ; LONGJUMP2
9889 j label ; 25_PCREL
9890 $1
9891
9892 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
9893 bne rt, ra, $1 ; LONGJUMP2
9894 j label ; 25_PCREL
9895 $1 */
9896
9897 /* Offset for first instruction. */
9898
9899 /* Use j label as second instruction. */
9900 *insn_len = 4 + first_size;
9901 insn = INSN_J;
9902 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9903 reloc = R_NDS32_LONGJUMP2;
9904 cond_reloc = R_NDS32_25_PLTREL;
9905 }
9906 else
9907 return FALSE;
9908
9909 if (cond_removed == 1)
9910 {
9911 /* Set all relocations. */
9912 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9913 irel->r_addend = hi_irelfn->r_addend;
9914
9915 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
9916 cond_reloc);
9917 cond_irelfn->r_addend = 0;
9918 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9919 R_NDS32_NONE);
9920 }
9921 else
9922 {
9923 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
9924 irel->r_addend = irel->r_addend;
9925 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9926 cond_reloc);
9927 }
9928
9929 if ((seq_len ^ *insn_len ) & 0x2)
9930 {
9931 insn16 = NDS32_NOP16;
9932 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9933 lo_irelfn->r_offset = *insn_len;
9934 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9935 R_NDS32_INSN16);
9936 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9937 *insn_len += 2;
9938 }
9939 else
9940 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9941 R_NDS32_NONE);
9942 return TRUE;
9943 }
9944
9945 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
9946
9947 static bfd_boolean
9948 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9949 Elf_Internal_Rela *internal_relocs, int *insn_len,
9950 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9951 Elf_Internal_Shdr *symtab_hdr)
9952 {
9953 /* The pattern for LONGCALL4. Support for function cse.
9954 sethi ta, hi20(symbol) ; LONGCALL4/HI20
9955 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
9956 jral ta ; PTR_RES/EMPTY/INSN16 */
9957
9958 bfd_vma laddr;
9959 uint32_t insn;
9960 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
9961 Elf_Internal_Rela *irelend;
9962 int pic_ext_target = 0;
9963 bfd_signed_vma foff;
9964
9965 irelend = internal_relocs + sec->reloc_count;
9966 laddr = irel->r_offset;
9967
9968 /* Get the reloc for the address from which the register is
9969 being loaded. This reloc will tell us which function is
9970 actually being called. */
9971 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9972 R_NDS32_HI20_RELA, laddr);
9973
9974 if (hi_irel == irelend)
9975 {
9976 (*_bfd_error_handler)
9977 ("%B: warning: R_NDS32_LONGCALL4 points to unrecognized"
9978 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
9979 return FALSE;
9980 }
9981
9982 /* Get the value of the symbol referred to by the reloc. */
9983 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
9984 &pic_ext_target);
9985
9986 /* This condition only happened when symbol is undefined. */
9987 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
9988 || foff >= CONSERVATIVE_24BIT_S1)
9989 return FALSE;
9990
9991 /* Relax to: jal symbol; 25_PCREL */
9992 /* For simplicity of coding, we are going to modify the section
9993 contents, the section relocs, and the BFD symbol table. We
9994 must tell the rest of the code not to free up this
9995 information. It would be possible to instead create a table
9996 of changes which have to be made, as is done in coff-mips.c;
9997 that would be more work, but would require less memory when
9998 the linker is run. */
9999
10000 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10001 R_NDS32_PTR_RESOLVED, irel->r_addend);
10002 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10003 R_NDS32_EMPTY, irel->r_addend);
10004
10005 if (ptr_irel == irelend || em_irel == irelend)
10006 {
10007 (*_bfd_error_handler)
10008 ("%B: warning: R_NDS32_LONGCALL4 points to unrecognized"
10009 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10010 return FALSE;
10011 }
10012 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10013 insn = bfd_getb32 (contents + irel->r_addend);
10014 if (insn & 0x80000000)
10015 return FALSE;
10016
10017 /* Replace the long call with a jal. */
10018 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10019 R_NDS32_25_PCREL_RELA);
10020 ptr_irel->r_addend = 1;
10021
10022 /* We don't resolve this here but resolve it in relocate_section. */
10023 insn = INSN_JAL;
10024 bfd_putb32 (insn, contents + em_irel->r_offset);
10025
10026 irel->r_info =
10027 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10028
10029 /* If there is function cse, HI20 can not remove now. */
10030 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10031 R_NDS32_LONGCALL4, laddr);
10032 if (call_irel == irelend)
10033 {
10034 *insn_len = 0;
10035 hi_irel->r_info =
10036 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10037 }
10038
10039 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10040 R_NDS32_INSN16, irel->r_addend);
10041 if (insn_irel != irelend)
10042 insn_irel->r_info =
10043 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10044
10045 return TRUE;
10046 }
10047
10048 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
10049
10050 static bfd_boolean
10051 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10052 Elf_Internal_Rela *internal_relocs, int *insn_len,
10053 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10054 Elf_Internal_Shdr *symtab_hdr)
10055 {
10056 /* The pattern for LONGCALL5.
10057 bltz rt, .L1 ; LONGCALL5/17_PCREL
10058 jal symbol ; 25_PCREL
10059 .L1: */
10060
10061 bfd_vma laddr;
10062 uint32_t insn;
10063 Elf_Internal_Rela *cond_irel, *irelend;
10064 int pic_ext_target = 0;
10065 bfd_signed_vma foff;
10066
10067 irelend = internal_relocs + sec->reloc_count;
10068 laddr = irel->r_offset;
10069 insn = bfd_getb32 (contents + laddr);
10070
10071 /* Get the reloc for the address from which the register is
10072 being loaded. This reloc will tell us which function is
10073 actually being called. */
10074 cond_irel =
10075 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10076 R_NDS32_25_PCREL_RELA, irel->r_addend);
10077 if (cond_irel == irelend)
10078 {
10079 (*_bfd_error_handler)
10080 ("%B: warning: R_NDS32_LONGCALL5 points to unrecognized"
10081 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10082 return FALSE;
10083 }
10084
10085 /* Get the value of the symbol referred to by the reloc. */
10086 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10087 &pic_ext_target);
10088
10089 if (foff == 0 || foff < -CONSERVATIVE_16BIT_S1
10090 || foff >= CONSERVATIVE_16BIT_S1)
10091 return FALSE;
10092
10093 /* Relax to bgezal rt, label ; 17_PCREL
10094 or bltzal rt, label ; 17_PCREL */
10095
10096 /* Convert to complimentary conditional call. */
10097 insn = CONVERT_CONDITION_CALL (insn);
10098
10099 /* For simplicity of coding, we are going to modify the section
10100 contents, the section relocs, and the BFD symbol table. We
10101 must tell the rest of the code not to free up this
10102 information. It would be possible to instead create a table
10103 of changes which have to be made, as is done in coff-mips.c;
10104 that would be more work, but would require less memory when
10105 the linker is run. */
10106
10107 /* Modify relocation and contents. */
10108 cond_irel->r_info =
10109 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10110
10111 /* Replace the long call with a bgezal. */
10112 bfd_putb32 (insn, contents + cond_irel->r_offset);
10113 *insn_len = 0;
10114
10115 /* Clean unnessary relocations. */
10116 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10117
10118 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10119 R_NDS32_17_PCREL_RELA, laddr);
10120 cond_irel->r_info =
10121 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10122
10123 return TRUE;
10124 }
10125
10126 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
10127
10128 static bfd_boolean
10129 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10130 Elf_Internal_Rela *internal_relocs, int *insn_len,
10131 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10132 Elf_Internal_Shdr *symtab_hdr)
10133 {
10134 /* The pattern for LONGCALL6.
10135 bltz rt, .L1 ; LONGCALL6/17_PCREL
10136 sethi ta, hi20(symbol) ; HI20/PTR
10137 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10138 jral ta ; PTR_RES/EMPTY/INSN16
10139 .L1 */
10140
10141 bfd_vma laddr;
10142 uint32_t insn;
10143 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10144 int pic_ext_target = 0;
10145 bfd_signed_vma foff;
10146
10147 irelend = internal_relocs + sec->reloc_count;
10148 laddr = irel->r_offset;
10149
10150 /* Get the reloc for the address from which the register is
10151 being loaded. This reloc will tell us which function is
10152 actually being called. */
10153 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10154 R_NDS32_EMPTY, irel->r_addend);
10155
10156 if (em_irel == irelend)
10157 {
10158 (*_bfd_error_handler)
10159 ("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
10160 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10161 return FALSE;
10162 }
10163
10164 /* Get the value of the symbol referred to by the reloc. */
10165 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
10166 &pic_ext_target);
10167
10168 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
10169 || foff >= CONSERVATIVE_24BIT_S1)
10170 return FALSE;
10171
10172 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10173 insn = bfd_getb32 (contents + irel->r_addend);
10174 if (insn & 0x80000000)
10175 return FALSE;
10176
10177 insn = bfd_getb32 (contents + laddr);
10178 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10179 {
10180 /* Relax to bgezal rt, label ; 17_PCREL
10181 or bltzal rt, label ; 17_PCREL */
10182
10183 /* Convert to complimentary conditional call. */
10184 *insn_len = 0;
10185 insn = CONVERT_CONDITION_CALL (insn);
10186 bfd_putb32 (insn, contents + em_irel->r_offset);
10187
10188 em_irel->r_info =
10189 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10190
10191 /* Set resolved relocation. */
10192 cond_irel =
10193 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10194 R_NDS32_PTR_RESOLVED, irel->r_addend);
10195 if (cond_irel == irelend)
10196 {
10197 (*_bfd_error_handler)
10198 ("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
10199 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10200 return FALSE;
10201 }
10202 cond_irel->r_addend = 1;
10203
10204 /* Clear relocations. */
10205
10206 irel->r_info =
10207 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10208
10209 cond_irel =
10210 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10211 R_NDS32_17_PCREL_RELA, laddr);
10212 if (cond_irel != irelend)
10213 cond_irel->r_info =
10214 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10215
10216 cond_irel =
10217 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10218 R_NDS32_INSN16, irel->r_addend);
10219 if (cond_irel != irelend)
10220 cond_irel->r_info =
10221 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10222
10223 }
10224 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10225 {
10226 /* Relax to the following instruction sequence
10227 bltz rt, .L1 ; LONGCALL2/17_PCREL
10228 jal symbol ; 25_PCREL/PTR_RES
10229 .L1 */
10230 *insn_len = 4;
10231 /* Convert instruction. */
10232 insn = INSN_JAL;
10233 bfd_putb32 (insn, contents + em_irel->r_offset);
10234
10235 /* Convert relocations. */
10236 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10237 R_NDS32_25_PCREL_RELA);
10238 irel->r_info =
10239 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10240
10241 /* Set resolved relocation. */
10242 cond_irel =
10243 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10244 R_NDS32_PTR_RESOLVED, irel->r_addend);
10245 if (cond_irel == irelend)
10246 {
10247 (*_bfd_error_handler)
10248 ("%B: warning: R_NDS32_LONGCALL6 points to unrecognized"
10249 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10250 return FALSE;
10251 }
10252 cond_irel->r_addend = 1;
10253
10254 cond_irel =
10255 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10256 R_NDS32_INSN16, irel->r_addend);
10257 if (cond_irel != irelend)
10258 cond_irel->r_info =
10259 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10260 }
10261 return TRUE;
10262 }
10263
10264 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10265
10266 static bfd_boolean
10267 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10268 Elf_Internal_Rela *internal_relocs, int *insn_len,
10269 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10270 Elf_Internal_Shdr *symtab_hdr)
10271 {
10272 /* The pattern for LONGJUMP4.
10273 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10274 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10275 jr ta ; PTR_RES/INSN16/EMPTY */
10276
10277 bfd_vma laddr;
10278 int seq_len; /* Original length of instruction sequence. */
10279 uint32_t insn;
10280 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10281 int pic_ext_target = 0;
10282 bfd_signed_vma foff;
10283
10284 irelend = internal_relocs + sec->reloc_count;
10285 seq_len = GET_SEQ_LEN (irel->r_addend);
10286 laddr = irel->r_offset;
10287 *insn_len = seq_len;
10288
10289 /* Get the reloc for the address from which the register is
10290 being loaded. This reloc will tell us which function is
10291 actually being called. */
10292
10293 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10294 R_NDS32_HI20_RELA, laddr);
10295
10296 if (hi_irel == irelend)
10297 {
10298 (*_bfd_error_handler)
10299 ("%B: warning: R_NDS32_LONGJUMP4 points to unrecognized"
10300 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10301 return FALSE;
10302 }
10303
10304 /* Get the value of the symbol referred to by the reloc. */
10305 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr,
10306 &pic_ext_target);
10307
10308 if (pic_ext_target || foff == 0 || foff >= CONSERVATIVE_24BIT_S1
10309 || foff < -CONSERVATIVE_24BIT_S1)
10310 return FALSE;
10311
10312 /* Convert it to "j label", it may be converted to j8 in the final
10313 pass of relaxation. Therefore, we do not consider this currently. */
10314 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10315 R_NDS32_PTR_RESOLVED, irel->r_addend);
10316 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10317 R_NDS32_EMPTY, irel->r_addend);
10318
10319 if (ptr_irel == irelend || em_irel == irelend)
10320 {
10321 (*_bfd_error_handler)
10322 ("%B: warning: R_NDS32_LONGJUMP4 points to unrecognized"
10323 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10324 return FALSE;
10325 }
10326
10327 em_irel->r_info =
10328 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10329 ptr_irel->r_addend = 1;
10330
10331 /* Write instruction. */
10332 insn = INSN_J;
10333 bfd_putb32 (insn, contents + em_irel->r_offset);
10334
10335 /* Clear relocations. */
10336 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10337
10338 /* If there is function cse, HI20 can not remove now. */
10339 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10340 R_NDS32_LONGJUMP4, laddr);
10341 if (call_irel == irelend)
10342 {
10343 *insn_len = 0;
10344 hi_irel->r_info =
10345 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10346 }
10347
10348 return TRUE;
10349 }
10350
10351 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
10352
10353 static bfd_boolean
10354 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10355 Elf_Internal_Rela *internal_relocs, int *insn_len,
10356 int *seq_len, bfd_byte *contents,
10357 Elf_Internal_Sym *isymbuf,
10358 Elf_Internal_Shdr *symtab_hdr)
10359 {
10360 /* There are 2 variations for LONGJUMP5
10361 case 2-4; 1st insn convertible, 16-bit on.
10362 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10363 j label ; 25_PCREL/INSN16
10364 $1:
10365
10366 case 4-4; 1st insn not convertible
10367 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10368 j label ; 25_PCREL/INSN16
10369 .L1: */
10370
10371 bfd_vma laddr;
10372 Elf_Internal_Rela *cond_irel, *irelend;
10373 int pic_ext_target = 0;
10374 unsigned int i;
10375 bfd_signed_vma foff;
10376 uint32_t insn, re_insn = 0;
10377 uint16_t insn16, re_insn16 = 0;
10378 unsigned long reloc;
10379
10380 enum elf_nds32_reloc_type checked_types[] =
10381 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10382 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10383
10384 irelend = internal_relocs + sec->reloc_count;
10385 laddr = irel->r_offset;
10386
10387 /* Get the reloc for the address from which the register is
10388 being loaded. This reloc will tell us which function is
10389 actually being called. */
10390
10391 cond_irel =
10392 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10393 R_NDS32_25_PCREL_RELA, irel->r_addend);
10394 if (cond_irel == irelend)
10395 {
10396 (*_bfd_error_handler)
10397 ("%B: warning: R_NDS32_LONGJUMP5 points to unrecognized"
10398 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10399 return FALSE;
10400 }
10401
10402 /* Get the value of the symbol referred to by the reloc. */
10403 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10404 &pic_ext_target);
10405
10406 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_16BIT_S1
10407 || foff >= CONSERVATIVE_16BIT_S1)
10408 return FALSE;
10409
10410 /* Get the all corresponding instructions. */
10411 insn = bfd_getb32 (contents + laddr);
10412 /* Check instruction size. */
10413 if (insn & 0x80000000)
10414 {
10415 *seq_len = 0;
10416 insn16 = insn >> 16;
10417 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10418 }
10419 else
10420 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10421
10422 if (N32_OP6 (re_insn) == N32_OP6_BR1
10423 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10424 {
10425 /* beqs label ; 15_PCREL. */
10426 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10427 reloc = R_NDS32_15_PCREL_RELA;
10428 }
10429 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10430 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10431 {
10432 /* beqz label ; 17_PCREL. */
10433 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10434 reloc = R_NDS32_17_PCREL_RELA;
10435 }
10436 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
10437 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
10438 {
10439 /* beqc label ; 9_PCREL. */
10440 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10441 reloc = R_NDS32_WORD_9_PCREL_RELA;
10442 }
10443 else
10444 return FALSE;
10445
10446 /* Set all relocations. */
10447 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
10448
10449 /* Clean relocations. */
10450 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10451 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10452 {
10453 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10454 checked_types[i], laddr);
10455 if (cond_irel != irelend)
10456 {
10457 if (*seq_len == 0
10458 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10459 {
10460 /* If the branch instruction is 2 byte, it cannot remove
10461 directly. Only convert it to nop16 and remove it after
10462 checking alignment issue. */
10463 insn16 = NDS32_NOP16;
10464 bfd_putb16 (insn16, contents + laddr);
10465 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10466 }
10467 else
10468 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10469 R_NDS32_NONE);
10470 }
10471 }
10472 *insn_len = 0;
10473
10474 return TRUE;
10475 }
10476
10477 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
10478
10479 static bfd_boolean
10480 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10481 Elf_Internal_Rela *internal_relocs, int *insn_len,
10482 int *seq_len, bfd_byte *contents,
10483 Elf_Internal_Sym *isymbuf,
10484 Elf_Internal_Shdr *symtab_hdr)
10485 {
10486 /* There are 5 variations for LONGJUMP6
10487 case : 2-4-4-4; 1st insn convertible, 16-bit on.
10488 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10489 sethi ta, hi20(symbol) ; HI20/PTR
10490 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10491 jr ta ; PTR_RES/INSN16/EMPTY
10492 .L1:
10493
10494 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
10495 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
10496 sethi ta, hi20(symbol) ; HI20/PTR
10497 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10498 jr ta ; PTR_RES/INSN16/EMPTY
10499 .L1: */
10500
10501 enum elf_nds32_reloc_type checked_types[] =
10502 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10503 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10504
10505 int reloc_off = 0, cond_removed = 0;
10506 bfd_vma laddr;
10507 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
10508 int pic_ext_target = 0;
10509 unsigned int i;
10510 bfd_signed_vma foff;
10511 uint32_t insn, re_insn = 0;
10512 uint16_t insn16, re_insn16 = 0;
10513 unsigned long reloc;
10514
10515 irelend = internal_relocs + sec->reloc_count;
10516 laddr = irel->r_offset;
10517
10518 /* Get the reloc for the address from which the register is
10519 being loaded. This reloc will tell us which function is
10520 actually being called. */
10521 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10522 R_NDS32_EMPTY, irel->r_addend);
10523
10524 if (em_irel == irelend)
10525 {
10526 (*_bfd_error_handler)
10527 ("%B: warning: R_NDS32_LONGJUMP6 points to unrecognized"
10528 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10529 return FALSE;
10530 }
10531
10532 /* Get the value of the symbol referred to by the reloc. */
10533 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr,
10534 &pic_ext_target);
10535
10536 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_24BIT_S1
10537 || foff >= CONSERVATIVE_24BIT_S1)
10538 return FALSE;
10539
10540 insn = bfd_getb32 (contents + laddr);
10541 /* Check instruction size. */
10542 if (insn & 0x80000000)
10543 {
10544 *seq_len = 0;
10545 insn16 = insn >> 16;
10546 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10547 }
10548 else
10549 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10550
10551 /* For simplicity of coding, we are going to modify the section
10552 contents, the section relocs, and the BFD symbol table. We
10553 must tell the rest of the code not to free up this
10554 information. It would be possible to instead create a table
10555 of changes which have to be made, as is done in coff-mips.c;
10556 that would be more work, but would require less memory when
10557 the linker is run. */
10558
10559 if (N32_OP6 (re_insn) == N32_OP6_BR1
10560 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10561 {
10562 /* beqs label ; 15_PCREL */
10563 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10564 reloc = R_NDS32_15_PCREL_RELA;
10565 cond_removed = 1;
10566 }
10567 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10568 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10569 {
10570 /* beqz label ; 17_PCREL */
10571 bfd_putb32 (re_insn, contents + em_irel->r_offset);
10572 reloc = R_NDS32_17_PCREL_RELA;
10573 cond_removed = 1;
10574 }
10575 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10576 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10577 {
10578 /* Relax to one of the following 2 variations
10579
10580 case 2-4; 1st insn convertible, 16-bit on.
10581 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10582 j label ; 25_PCREL/INSN16
10583 $1:
10584
10585 case 4-4; 1st insn not convertible
10586 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10587 j label ; 25_PCREL/INSN16
10588 .L1: */
10589
10590 /* Use j label as second instruction. */
10591 insn = INSN_J;
10592 reloc = R_NDS32_25_PCREL_RELA;
10593 bfd_putb32 (insn, contents + em_irel->r_offset);
10594 }
10595 else
10596 return FALSE;
10597
10598 /* Set all relocations. */
10599 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
10600
10601 cond_irel =
10602 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10603 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
10604 cond_irel->r_addend = 1;
10605
10606 /* Use INSN16 of first branch instruction to distinguish if keeping
10607 INSN16 of final instruction or not. */
10608 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10609 R_NDS32_INSN16, irel->r_offset);
10610 if (insn_irel == irelend)
10611 {
10612 /* Clean the final INSN16. */
10613 insn_irel =
10614 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10615 R_NDS32_INSN16, em_irel->r_offset);
10616 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10617 R_NDS32_NONE);
10618 }
10619
10620 if (cond_removed == 1)
10621 {
10622 *insn_len = 0;
10623
10624 /* Clear relocations. */
10625 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10626
10627 for (i = 0; i < sizeof (checked_types) / sizeof (checked_types[0]); i++)
10628 {
10629 cond_irel =
10630 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10631 checked_types[i], laddr);
10632 if (cond_irel != irelend)
10633 {
10634 if (*seq_len == 0
10635 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
10636 {
10637 /* If the branch instruction is 2 byte, it cannot remove
10638 directly. Only convert it to nop16 and remove it after
10639 checking alignment issue. */
10640 insn16 = NDS32_NOP16;
10641 bfd_putb16 (insn16, contents + laddr);
10642 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10643 }
10644 else
10645 cond_irel->r_info =
10646 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10647 }
10648 }
10649 }
10650 else
10651 {
10652 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
10653 R_NDS32_LONGJUMP5);
10654 }
10655
10656 return TRUE;
10657 }
10658
10659 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
10660
10661 static bfd_boolean
10662 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10663 Elf_Internal_Rela *internal_relocs, int *insn_len,
10664 int *seq_len, bfd_byte *contents,
10665 Elf_Internal_Sym *isymbuf,
10666 Elf_Internal_Shdr *symtab_hdr)
10667 {
10668 /* There are 2 variations for LONGJUMP5
10669 case 2-4; 1st insn convertible, 16-bit on.
10670 movi55 ta, imm11 ; LONGJUMP7/INSN16
10671 beq rt, ta, label ; 15_PCREL
10672
10673 case 4-4; 1st insn not convertible
10674 movi55 ta, imm11 ; LONGJUMP7/INSN16
10675 beq rt, ta, label ; 15_PCREL */
10676
10677 bfd_vma laddr;
10678 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
10679 int pic_ext_target = 0;
10680 bfd_signed_vma foff;
10681 uint32_t insn, re_insn = 0;
10682 uint16_t insn16;
10683 uint32_t imm11;
10684
10685 irelend = internal_relocs + sec->reloc_count;
10686 laddr = irel->r_offset;
10687
10688 /* Get the reloc for the address from which the register is
10689 being loaded. This reloc will tell us which function is
10690 actually being called. */
10691
10692 cond_irel =
10693 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10694 R_NDS32_15_PCREL_RELA, irel->r_addend);
10695 if (cond_irel == irelend)
10696 {
10697 (*_bfd_error_handler)
10698 ("%B: warning: R_NDS32_LONGJUMP7 points to unrecognized"
10699 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10700 return FALSE;
10701 }
10702
10703 /* Get the value of the symbol referred to by the reloc. */
10704 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr,
10705 &pic_ext_target);
10706
10707 if (pic_ext_target || foff == 0 || foff < -CONSERVATIVE_8BIT_S1
10708 || foff >= CONSERVATIVE_8BIT_S1)
10709 return FALSE;
10710
10711 /* Get the first instruction for its size. */
10712 insn = bfd_getb32 (contents + laddr);
10713 if (insn & 0x80000000)
10714 {
10715 *seq_len = 0;
10716 /* Get the immediate from movi55. */
10717 imm11 = N16_IMM5S (insn >> 16);
10718 }
10719 else
10720 {
10721 /* Get the immediate from movi. */
10722 imm11 = N32_IMM20S (insn);
10723 }
10724
10725 /* Get the branch instruction. */
10726 insn = bfd_getb32 (contents + irel->r_addend);
10727 /* Convert instruction to BR3. */
10728 if ((insn >> 14) & 0x1)
10729 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
10730 else
10731 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
10732
10733 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10734
10735 /* Set all relocations. */
10736 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10737 R_NDS32_WORD_9_PCREL_RELA);
10738
10739 /* Clean relocations. */
10740 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10741 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10742 R_NDS32_INSN16, irel->r_offset);
10743 if (insn_irel != irelend)
10744 {
10745 if (*seq_len == 0)
10746 {
10747 /* If the first insntruction is 16bit, convert it to nop16. */
10748 insn16 = NDS32_NOP16;
10749 bfd_putb16 (insn16, contents + laddr);
10750 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10751 }
10752 else
10753 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
10754 R_NDS32_NONE);
10755 }
10756 *insn_len = 0;
10757
10758 return TRUE;
10759 }
10760
10761 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
10762
10763 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
10764
10765 static bfd_boolean
10766 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
10767 asection *sec, Elf_Internal_Rela *irel,
10768 Elf_Internal_Rela *internal_relocs, int *insn_len,
10769 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10770 Elf_Internal_Shdr *symtab_hdr, int load_store_relax)
10771 {
10772 int eliminate_sethi = 0, range_type, i;
10773 bfd_vma local_sda, laddr;
10774 int seq_len; /* Original length of instruction sequence. */
10775 uint32_t insn;
10776 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
10777 bfd_vma access_addr = 0;
10778 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10779 enum elf_nds32_reloc_type checked_types[] =
10780 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
10781 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
10782 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
10783 R_NDS32_TLS_LE_HI20
10784 };
10785
10786 irelend = internal_relocs + sec->reloc_count;
10787 seq_len = GET_SEQ_LEN (irel->r_addend);
10788 laddr = irel->r_offset;
10789 *insn_len = seq_len;
10790
10791 /* Get the high part relocation. */
10792 for (i = 0; (unsigned) i < sizeof (checked_types); i++)
10793 {
10794 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10795 checked_types[i], laddr);
10796 if (hi_irelfn != irelend)
10797 break;
10798 }
10799
10800 if (hi_irelfn == irelend)
10801 {
10802 (*_bfd_error_handler)
10803 ("%B: warning: R_NDS32_LOADSTORE points to unrecognized"
10804 "reloc at 0x%lx.", abfd, (long) irel->r_offset);
10805 return FALSE;
10806 }
10807
10808 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
10809 nds32_elf_final_sda_base (sec->output_section->owner,
10810 link_info, &local_sda, FALSE);
10811
10812 switch (ELF32_R_TYPE (hi_irelfn->r_info))
10813 {
10814 case R_NDS32_HI20_RELA:
10815 insn = bfd_getb32 (contents + laddr);
10816 access_addr =
10817 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10818
10819 if (range_type == NDS32_LOADSTORE_IMM)
10820 {
10821 struct elf_link_hash_entry *h = NULL;
10822 int indx;
10823
10824 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
10825 {
10826 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
10827 h = elf_sym_hashes (abfd)[indx];
10828 }
10829
10830 if ((access_addr < CONSERVATIVE_20BIT)
10831 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10832 {
10833 eliminate_sethi = 1;
10834 break;
10835 }
10836
10837 /* This is avoid to relax symbol address which is fixed
10838 relocations. Ex: _stack. */
10839 if (h && bfd_is_abs_section (h->root.u.def.section))
10840 return FALSE;
10841 }
10842
10843 if (!load_store_relax)
10844 return FALSE;
10845
10846 /* Case for set gp register. */
10847 if (N32_RT5 (insn) == REG_GP)
10848 break;
10849
10850 if (range_type == NDS32_LOADSTORE_FLOAT_S
10851 || range_type == NDS32_LOADSTORE_FLOAT_S)
10852 {
10853 range_l = sdata_range[0][0];
10854 range_h = sdata_range[0][1];
10855 }
10856 else
10857 {
10858 range_l = sdata_range[1][0];
10859 range_h = sdata_range[1][1];
10860 }
10861 break;
10862
10863 case R_NDS32_GOT_HI20:
10864 access_addr =
10865 calculate_got_memory_address (abfd, link_info, hi_irelfn, symtab_hdr);
10866
10867 /* If this symbol is not in .got, the return value will be -1.
10868 Since the gp value is set to SDA_BASE but not GLOBAL_OFFSET_TABLE,
10869 a negative offset is allowed. */
10870 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10871 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10872 eliminate_sethi = 1;
10873 break;
10874
10875 case R_NDS32_PLT_GOTREL_HI20:
10876 access_addr = calculate_plt_memory_address (abfd, link_info, isymbuf,
10877 hi_irelfn, symtab_hdr);
10878
10879 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10880 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10881 eliminate_sethi = 1;
10882 break;
10883
10884 case R_NDS32_GOTOFF_HI20:
10885 access_addr =
10886 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10887
10888 if ((bfd_signed_vma) (access_addr - local_sda) < CONSERVATIVE_20BIT
10889 && (bfd_signed_vma) (access_addr - local_sda) >= -CONSERVATIVE_20BIT)
10890 eliminate_sethi = 1;
10891 break;
10892
10893 case R_NDS32_GOTPC_HI20:
10894 /* The access_addr must consider r_addend of hi_irel. */
10895 access_addr = sec->output_section->vma + sec->output_offset
10896 + irel->r_offset + hi_irelfn->r_addend;
10897
10898 if ((bfd_signed_vma) (local_sda - access_addr) < CONSERVATIVE_20BIT
10899 && (bfd_signed_vma) (local_sda - access_addr) >= -CONSERVATIVE_20BIT)
10900 eliminate_sethi = 1;
10901 break;
10902
10903 case R_NDS32_TLS_LE_HI20:
10904 access_addr =
10905 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
10906 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
10907 access_addr -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
10908 if ((range_type == NDS32_LOADSTORE_IMM)
10909 && (bfd_signed_vma) (access_addr) < CONSERVATIVE_20BIT
10910 && (bfd_signed_vma) (access_addr) >= -CONSERVATIVE_20BIT)
10911 eliminate_sethi = 1;
10912 break;
10913
10914 default:
10915 return FALSE;
10916 }
10917
10918 /* Delete sethi instruction. */
10919 if (eliminate_sethi == 1
10920 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
10921 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
10922 {
10923 hi_irelfn->r_info =
10924 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
10925 irel->r_info =
10926 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10927 *insn_len = 0;
10928 }
10929 return TRUE;
10930 }
10931
10932 /* Relax LO12 relocation for nds32_elf_relax_section. */
10933
10934 static void
10935 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
10936 asection *sec, Elf_Internal_Rela *irel,
10937 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
10938 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
10939 {
10940 uint32_t insn;
10941 bfd_vma local_sda, laddr;
10942 unsigned long reloc;
10943 bfd_vma access_addr;
10944 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
10945 Elf_Internal_Rela *irelfn = NULL, *irelend;
10946 struct elf_link_hash_entry *h = NULL;
10947 int indx;
10948
10949 /* For SDA base relative relaxation. */
10950 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
10951 &local_sda, FALSE);
10952
10953 irelend = internal_relocs + sec->reloc_count;
10954 laddr = irel->r_offset;
10955 insn = bfd_getb32 (contents + laddr);
10956
10957 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
10958 return;
10959
10960 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
10961
10962 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
10963 {
10964 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
10965 h = elf_sym_hashes (abfd)[indx];
10966 }
10967
10968 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
10969 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
10970 {
10971 reloc = R_NDS32_20_RELA;
10972 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10973 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
10974 bfd_putb32 (insn, contents + laddr);
10975 }
10976 /* This is avoid to relax symbol address which is fixed
10977 relocations. Ex: _stack. */
10978 else if (N32_OP6 (insn) == N32_OP6_ORI
10979 && h && bfd_is_abs_section (h->root.u.def.section))
10980 return;
10981 else
10982 {
10983 range_l = sdata_range[1][0];
10984 range_h = sdata_range[1][1];
10985 switch (ELF32_R_TYPE (irel->r_info))
10986 {
10987 case R_NDS32_LO12S0_RELA:
10988 reloc = R_NDS32_SDA19S0_RELA;
10989 break;
10990 case R_NDS32_LO12S1_RELA:
10991 reloc = R_NDS32_SDA18S1_RELA;
10992 break;
10993 case R_NDS32_LO12S2_RELA:
10994 reloc = R_NDS32_SDA17S2_RELA;
10995 break;
10996 case R_NDS32_LO12S2_DP_RELA:
10997 range_l = sdata_range[0][0];
10998 range_h = sdata_range[0][1];
10999 reloc = R_NDS32_SDA12S2_DP_RELA;
11000 break;
11001 case R_NDS32_LO12S2_SP_RELA:
11002 range_l = sdata_range[0][0];
11003 range_h = sdata_range[0][1];
11004 reloc = R_NDS32_SDA12S2_SP_RELA;
11005 break;
11006 default:
11007 return;
11008 }
11009
11010 /* There are range_h and range_l because linker has to promise
11011 all sections move cross one page together. */
11012 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11013 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11014 {
11015 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11016 {
11017 /* Maybe we should add R_NDS32_INSN16 reloc type here
11018 or manually do some optimization. sethi can't be
11019 eliminated when updating $gp so the relative ori
11020 needs to be preserved. */
11021 return;
11022 }
11023 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11024 &insn))
11025 return;
11026 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11027 bfd_putb32 (insn, contents + laddr);
11028
11029 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11030 R_NDS32_INSN16);
11031 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11032 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11033 irelfn->r_info =
11034 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11035
11036 }
11037 }
11038 return;
11039 }
11040
11041 /* Relax low part of PIC instruction pattern. */
11042
11043 static void
11044 nds32_elf_relax_piclo12 (struct bfd_link_info *link_info, bfd *abfd,
11045 asection *sec, Elf_Internal_Rela *irel,
11046 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11047 Elf_Internal_Shdr *symtab_hdr)
11048 {
11049 uint32_t insn;
11050 bfd_vma local_sda, laddr;
11051 bfd_signed_vma foff;
11052 unsigned long reloc;
11053
11054 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11055 &local_sda, FALSE);
11056 laddr = irel->r_offset;
11057 insn = bfd_getb32 (contents + laddr);
11058
11059 if (N32_OP6 (insn) != N32_OP6_ORI)
11060 return;
11061
11062 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12)
11063 {
11064 foff = calculate_got_memory_address (abfd, link_info, irel,
11065 symtab_hdr) - local_sda;
11066 reloc = R_NDS32_GOT20;
11067 }
11068 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12)
11069 {
11070 foff = calculate_plt_memory_address (abfd, link_info, isymbuf, irel,
11071 symtab_hdr) - local_sda;
11072 reloc = R_NDS32_PLT_GOTREL_LO20;
11073 }
11074 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12)
11075 {
11076 foff = calculate_memory_address (abfd, irel, isymbuf,
11077 symtab_hdr) - local_sda;
11078 reloc = R_NDS32_GOTOFF;
11079 }
11080 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12)
11081 {
11082 foff = local_sda - sec->output_section->vma + sec->output_offset
11083 + irel->r_offset + irel->r_addend;
11084 reloc = R_NDS32_GOTPC20;
11085 }
11086 else
11087 return;
11088
11089 if ((foff < CONSERVATIVE_20BIT) && (foff >= -CONSERVATIVE_20BIT))
11090 {
11091 /* Turn into MOVI. */
11092 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11093 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11094 bfd_putb32 (insn, contents + laddr);
11095 }
11096 }
11097
11098 /* Relax low part of LE TLS instruction pattern. */
11099
11100 static void
11101 nds32_elf_relax_letlslo12 (struct bfd_link_info *link_info, bfd *abfd,
11102 Elf_Internal_Rela *irel,
11103 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11104 Elf_Internal_Shdr *symtab_hdr)
11105 {
11106 uint32_t insn;
11107 bfd_vma laddr;
11108 bfd_signed_vma foff;
11109 unsigned long reloc;
11110
11111 laddr = irel->r_offset;
11112 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11113 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
11114 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
11115 insn = bfd_getb32 (contents + laddr);
11116
11117 if ( (bfd_signed_vma) (foff) < CONSERVATIVE_20BIT
11118 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_20BIT)
11119 {
11120 /* Pattern sethi-ori transform to movi. */
11121 reloc = R_NDS32_TLS_LE_20;
11122 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11123 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11124 bfd_putb32 (insn, contents + laddr);
11125 }
11126 }
11127
11128 /* Relax LE TLS calculate address instruction pattern. */
11129
11130 static void
11131 nds32_elf_relax_letlsadd (struct bfd_link_info *link_info, bfd *abfd,
11132 asection *sec, Elf_Internal_Rela *irel,
11133 Elf_Internal_Rela *internal_relocs,
11134 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11135 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11136 {
11137 /* Local TLS non-pic
11138 sethi ta, hi20(symbol@tpoff) ; TLS_LE_HI20
11139 ori ta, ta, lo12(symbol@tpoff) ; TLS_LE_LO12
11140 add ra, ta, tp ; TLS_LE_ADD */
11141
11142 uint32_t insn;
11143 bfd_vma laddr;
11144 bfd_signed_vma foff;
11145 Elf_Internal_Rela *i1_irelfn, *irelend;
11146
11147 irelend = internal_relocs + sec->reloc_count;
11148 laddr = irel->r_offset;
11149 insn = bfd_getb32 (contents + laddr);
11150 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11151 R_NDS32_PTR_RESOLVED);
11152 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11153 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
11154 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
11155
11156 /* The range is +/-16k. */
11157 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
11158 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
11159 {
11160 /* Transform add to addi. */
11161 insn = N32_TYPE2 (ADDI, N32_RT5 (insn), N32_RB5 (insn), 0);
11162 irel->r_info =
11163 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
11164
11165 bfd_putb32 (insn, contents + laddr);
11166 if (i1_irelfn != irelend)
11167 {
11168 i1_irelfn->r_addend |= 1;
11169 *again = TRUE;
11170 }
11171 }
11172 }
11173
11174 /* Relax LE TLS load store instruction pattern. */
11175
11176 static void
11177 nds32_elf_relax_letlsls (struct bfd_link_info *link_info, bfd *abfd,
11178 asection *sec, Elf_Internal_Rela *irel,
11179 Elf_Internal_Rela *internal_relocs,
11180 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11181 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11182 {
11183
11184 uint32_t insn;
11185 bfd_vma laddr;
11186 bfd_signed_vma foff;
11187 Elf_Internal_Rela *i1_irelfn, *irelend;
11188 int success = 0;
11189
11190 irelend = internal_relocs + sec->reloc_count;
11191 laddr = irel->r_offset;
11192 insn = bfd_getb32 (contents + laddr);
11193 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11194 R_NDS32_PTR_RESOLVED);
11195 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11196 BFD_ASSERT (elf_hash_table (link_info)->tls_sec != NULL);
11197 foff -= (elf_hash_table (link_info)->tls_sec->vma + TP_OFFSET);
11198
11199 switch ((N32_OP6 (insn) << 8) | (insn & 0xff))
11200 {
11201 case (N32_OP6_MEM << 8) | N32_MEM_LB:
11202 case (N32_OP6_MEM << 8) | N32_MEM_SB:
11203 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11204 /* The range is +/-16k. */
11205 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT
11206 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT)
11207 {
11208 insn =
11209 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11210 irel->r_info =
11211 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S0);
11212 success = 1;
11213 break;
11214 }
11215 case (N32_OP6_MEM << 8) | N32_MEM_LH:
11216 case (N32_OP6_MEM << 8) | N32_MEM_SH:
11217 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11218 /* The range is +/-32k. */
11219 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S1
11220 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S1)
11221 {
11222 insn =
11223 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11224 irel->r_info =
11225 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S1);
11226 success = 1;
11227 break;
11228 }
11229 case (N32_OP6_MEM << 8) | N32_MEM_LW:
11230 case (N32_OP6_MEM << 8) | N32_MEM_SW:
11231 /* The range is +/-64k. */
11232 if ((bfd_signed_vma) (foff) < CONSERVATIVE_15BIT_S2
11233 && (bfd_signed_vma) (foff) >= -CONSERVATIVE_15BIT_S2)
11234 {
11235 insn =
11236 ((insn & 0xff) << 25) | (insn & 0x1f00000) | ((insn & 0x7c00) << 5);
11237 irel->r_info =
11238 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_TLS_LE_15S2);
11239 success = 1;
11240 break;
11241 }
11242 default:
11243 break;
11244 }
11245
11246 if (success)
11247 {
11248 bfd_putb32 (insn, contents + laddr);
11249 if (i1_irelfn != irelend)
11250 {
11251 i1_irelfn->r_addend |= 1;
11252 *again = TRUE;
11253 }
11254 }
11255 }
11256
11257 /* Relax PTR relocation for nds32_elf_relax_section. */
11258
11259 static bfd_boolean
11260 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11261 Elf_Internal_Rela *internal_relocs, int *insn_len,
11262 int *seq_len, bfd_byte *contents)
11263 {
11264 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11265
11266 irelend = internal_relocs + sec->reloc_count;
11267
11268 re_irel =
11269 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11270 R_NDS32_PTR_RESOLVED, irel->r_addend);
11271
11272 if (re_irel == irelend)
11273 {
11274 (*_bfd_error_handler)
11275 ("%B: warning: R_NDS32_PTR points to unrecognized reloc at 0x%lx.",
11276 abfd, (long) irel->r_offset);
11277 return FALSE;
11278 }
11279
11280 if (re_irel->r_addend != 1)
11281 return FALSE;
11282
11283 /* Pointed target is relaxed and no longer needs this void *,
11284 change the type to NONE. */
11285 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11286
11287 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11288 not exist, it means only count 1 and remove it directly. */
11289 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11290 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11291 R_NDS32_PTR_COUNT);
11292 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11293 R_NDS32_PTR);
11294 if (count_irel != irelend)
11295 {
11296 if (--count_irel->r_addend > 0)
11297 return FALSE;
11298 }
11299
11300 if (ptr_irel != irelend)
11301 return FALSE;
11302
11303 /* If the PTR_COUNT is already 0, remove current instruction. */
11304 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11305 *insn_len = 0;
11306 return TRUE;
11307 }
11308
11309 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11310
11311 static void
11312 nds32_elf_relax_pltgot_suff (struct bfd_link_info *link_info, bfd *abfd,
11313 asection *sec, Elf_Internal_Rela *irel,
11314 Elf_Internal_Rela *internal_relocs,
11315 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11316 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11317 {
11318 uint32_t insn;
11319 bfd_signed_vma foff;
11320 Elf_Internal_Rela *i1_irelfn, *irelend;
11321 bfd_vma local_sda, laddr;
11322
11323 irelend = internal_relocs + sec->reloc_count;
11324 laddr = irel->r_offset;
11325 insn = bfd_getb32 (contents + laddr);
11326
11327 /* FIXME: It's a little trouble to turn JRAL5 to JAL since
11328 we need additional space. It might be help if we could
11329 borrow some space from instructions to be eliminated
11330 such as sethi, ori, add. */
11331 if (insn & 0x80000000)
11332 return;
11333
11334 if (nds32_elf_check_dup_relocs
11335 (irel, internal_relocs, irelend, R_NDS32_PLT_GOT_SUFF))
11336 return;
11337
11338 i1_irelfn =
11339 find_relocs_at_address (irel, internal_relocs, irelend,
11340 R_NDS32_PTR_RESOLVED);
11341
11342 /* FIXIT 090606
11343 The boundary should be reduced since the .plt section hasn't
11344 been created and the address of specific entry is still unknown
11345 Maybe the range between the function call and the begin of the
11346 .text section can be used to decide if the .plt is in the range
11347 of function call. */
11348
11349 if (N32_OP6 (insn) == N32_OP6_ALU1
11350 && N32_SUB5 (insn) == N32_ALU1_ADD)
11351 {
11352 /* Get the value of the symbol referred to by the reloc. */
11353 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11354 &local_sda, FALSE);
11355 foff = (bfd_signed_vma) (calculate_plt_memory_address
11356 (abfd, link_info, isymbuf, irel,
11357 symtab_hdr) - local_sda);
11358 /* This condition only happened when symbol is undefined. */
11359 if (foff == 0)
11360 return;
11361
11362 if (foff < -CONSERVATIVE_19BIT || foff >= CONSERVATIVE_19BIT)
11363 return;
11364 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11365 R_NDS32_PLT_GOTREL_LO19);
11366 /* addi.gp */
11367 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
11368 }
11369 else if (N32_OP6 (insn) == N32_OP6_JREG
11370 && N32_SUB5 (insn) == N32_JREG_JRAL)
11371 {
11372 /* Get the value of the symbol referred to by the reloc. */
11373 foff =
11374 calculate_plt_offset (abfd, sec, link_info, isymbuf, irel, symtab_hdr);
11375 /* This condition only happened when symbol is undefined. */
11376 if (foff == 0)
11377 return;
11378 if (foff < -CONSERVATIVE_24BIT_S1 || foff >= CONSERVATIVE_24BIT_S1)
11379 return;
11380 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_25_PLTREL);
11381 insn = INSN_JAL;
11382 }
11383 else
11384 return;
11385
11386 bfd_putb32 (insn, contents + laddr);
11387 if (i1_irelfn != irelend)
11388 {
11389 i1_irelfn->r_addend |= 1;
11390 *again = TRUE;
11391 }
11392 }
11393
11394 /* Relax GOT_SUFF relocation for nds32_elf_relax_section. */
11395
11396 static void
11397 nds32_elf_relax_got_suff (struct bfd_link_info *link_info, bfd *abfd,
11398 asection *sec, Elf_Internal_Rela *irel,
11399 Elf_Internal_Rela *internal_relocs,
11400 bfd_byte *contents, Elf_Internal_Shdr *symtab_hdr,
11401 bfd_boolean *again)
11402 {
11403 uint32_t insn;
11404 bfd_signed_vma foff;
11405 Elf_Internal_Rela *i1_irelfn, *irelend;
11406 bfd_vma local_sda, laddr;
11407
11408 irelend = internal_relocs + sec->reloc_count;
11409 laddr = irel->r_offset;
11410 insn = bfd_getb32 (contents + laddr);
11411 if (insn & 0x80000000)
11412 return;
11413
11414 if (nds32_elf_check_dup_relocs
11415 (irel, internal_relocs, irelend, R_NDS32_GOT_SUFF))
11416 return;
11417
11418 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11419 R_NDS32_PTR_RESOLVED);
11420
11421 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11422 &local_sda, FALSE);
11423 foff = calculate_got_memory_address (abfd, link_info, irel,
11424 symtab_hdr) - local_sda;
11425
11426 if (foff < CONSERVATIVE_19BIT && foff >= -CONSERVATIVE_19BIT)
11427 {
11428 /* Turn LW to LWI.GP. Change relocation type to R_NDS32_GOT_REL. */
11429 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11430 irel->r_info =
11431 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_GOT17S2_RELA);
11432 bfd_putb32 (insn, contents + laddr);
11433 if (i1_irelfn != irelend)
11434 {
11435 i1_irelfn->r_addend |= 1;
11436 *again = TRUE;
11437 }
11438 }
11439 }
11440
11441 /* Relax PLT_GOT_SUFF relocation for nds32_elf_relax_section. */
11442
11443 static void
11444 nds32_elf_relax_gotoff_suff (struct bfd_link_info *link_info, bfd *abfd,
11445 asection *sec, Elf_Internal_Rela *irel,
11446 Elf_Internal_Rela *internal_relocs,
11447 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11448 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11449 {
11450 int opc_insn_gotoff;
11451 uint32_t insn;
11452 bfd_signed_vma foff;
11453 Elf_Internal_Rela *i1_irelfn, *i2_irelfn, *irelend;
11454 bfd_vma local_sda, laddr;
11455
11456 irelend = internal_relocs + sec->reloc_count;
11457 laddr = irel->r_offset;
11458 insn = bfd_getb32 (contents + laddr);
11459
11460 if (insn & 0x80000000)
11461 return;
11462
11463 if (nds32_elf_check_dup_relocs
11464 (irel, internal_relocs, irelend, R_NDS32_GOTOFF_SUFF))
11465 return;
11466
11467 i1_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11468 R_NDS32_PTR_RESOLVED);
11469 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11470 &local_sda, FALSE);
11471 foff = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11472 foff = foff - local_sda;
11473
11474 if (foff >= CONSERVATIVE_19BIT || foff < -CONSERVATIVE_19BIT)
11475 return;
11476
11477 /* Concatenate opcode and sub-opcode for switch case.
11478 It may be MEM or ALU1. */
11479 opc_insn_gotoff = (N32_OP6 (insn) << 8) | (insn & 0xff);
11480 switch (opc_insn_gotoff)
11481 {
11482 case (N32_OP6_MEM << 8) | N32_MEM_LW:
11483 /* 4-byte aligned. */
11484 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
11485 irel->r_info =
11486 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11487 break;
11488 case (N32_OP6_MEM << 8) | N32_MEM_SW:
11489 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
11490 irel->r_info =
11491 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA17S2_RELA);
11492 break;
11493 case (N32_OP6_MEM << 8) | N32_MEM_LH:
11494 /* 2-byte aligned. */
11495 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
11496 irel->r_info =
11497 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11498 break;
11499 case (N32_OP6_MEM << 8) | N32_MEM_LHS:
11500 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (18));
11501 irel->r_info =
11502 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11503 break;
11504 case (N32_OP6_MEM << 8) | N32_MEM_SH:
11505 insn = N32_TYPE1 (HWGP, N32_RT5 (insn), __BIT (19));
11506 irel->r_info =
11507 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA18S1_RELA);
11508 break;
11509 case (N32_OP6_MEM << 8) | N32_MEM_LB:
11510 /* 1-byte aligned. */
11511 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
11512 irel->r_info =
11513 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11514 break;
11515 case (N32_OP6_MEM << 8) | N32_MEM_LBS:
11516 insn = N32_TYPE1 (LBGP, N32_RT5 (insn), __BIT (19));
11517 irel->r_info =
11518 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11519 break;
11520 case (N32_OP6_MEM << 8) | N32_MEM_SB:
11521 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
11522 irel->r_info =
11523 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11524 break;
11525 case (N32_OP6_ALU1 << 8) | N32_ALU1_ADD:
11526 insn = N32_TYPE1 (SBGP, N32_RT5 (insn), __BIT (19));
11527 irel->r_info =
11528 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_SDA19S0_RELA);
11529 break;
11530 default:
11531 return;
11532 }
11533
11534 bfd_putb32 (insn, contents + laddr);
11535 if (i1_irelfn != irelend)
11536 {
11537 i1_irelfn->r_addend |= 1;
11538 *again = TRUE;
11539 }
11540 if ((i2_irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11541 R_NDS32_INSN16)) != irelend)
11542 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11543
11544 }
11545
11546 static bfd_boolean
11547 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11548 Elf_Internal_Rela *internal_relocs,
11549 bfd_byte *contents,
11550 nds32_elf_blank_t **relax_blank_list,
11551 int optimize, int opt_size)
11552 {
11553 /* This code block is used to adjust 4-byte alignment by relax a pair
11554 of instruction a time.
11555
11556 It recognizes three types of relocations.
11557 1. R_NDS32_LABEL - a aligment.
11558 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11559 3. is_16bit_NOP () - remove a 16-bit instruction. */
11560
11561 /* TODO: It seems currently implementation only support 4-byte aligment.
11562 We should handle any-aligment. */
11563
11564 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11565 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11566 Elf_Internal_Rela rel_temp;
11567 Elf_Internal_Rela *irelend;
11568 bfd_vma address;
11569 uint16_t insn16;
11570
11571 /* Checking for branch relaxation relies on the relocations to
11572 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11573 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11574 sizeof (Elf_Internal_Rela), compar_reloc);
11575
11576 irelend = internal_relocs + sec->reloc_count;
11577
11578 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11579 /* FIXME: Can we generate the right order in assembler?
11580 So we don't have to swapping them here. */
11581
11582 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11583 label_rel < irelend; label_rel++)
11584 {
11585 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11586 continue;
11587
11588 /* Find the first reloc has the same offset with label_rel. */
11589 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11590 insn_rel++;
11591
11592 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11593 insn_rel++)
11594 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11595 address. */
11596 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11597 break;
11598
11599 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11600 && insn_rel < label_rel)
11601 {
11602 /* Swap the two reloc if the R_NDS32_INSN16 is
11603 before R_NDS32_LABEL. */
11604 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11605 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11606 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11607 }
11608 }
11609
11610 label_rel = NULL;
11611 insn_rel = NULL;
11612 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11613 or higher, remove other R_NDS32_LABEL with lower alignment.
11614 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11615 then the R_NDS32_LABEL sequence is broke. */
11616 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11617 {
11618 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11619 {
11620 if (label_rel == NULL)
11621 {
11622 if (tmp_rel->r_addend < 2)
11623 label_rel = tmp_rel;
11624 continue;
11625 }
11626 else if (tmp_rel->r_addend > 1)
11627 {
11628 /* Remove all LABEL relocation from label_rel to tmp_rel
11629 including relocations with same offset as tmp_rel. */
11630 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel
11631 || tmp2_rel->r_offset == tmp_rel->r_offset; tmp2_rel++)
11632 {
11633 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11634 && tmp2_rel->r_addend < 2)
11635 tmp2_rel->r_info =
11636 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11637 R_NDS32_NONE);
11638 }
11639 label_rel = NULL;
11640 }
11641 }
11642 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11643 {
11644 /* A new INSN16 which can be converted, so clear label_rel. */
11645 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11646 irelend, &insn16)
11647 || is_16bit_NOP (abfd, sec, tmp_rel))
11648 label_rel = NULL;
11649 }
11650 }
11651
11652 label_rel = NULL;
11653 insn_rel = NULL;
11654 /* Optimized for speed and nothing has not been relaxed.
11655 It's time to align labels.
11656 We may convert a 16-bit instruction right before a label to
11657 32-bit, in order to align the label if necessary
11658 all reloc entries has been sorted by r_offset. */
11659 for (irel = internal_relocs; irel < irelend; irel++)
11660 {
11661 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11662 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11663 continue;
11664
11665 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11666 {
11667 /* A new INSN16 found, resize the old one. */
11668 if (is_convert_32_to_16
11669 (abfd, sec, irel, internal_relocs, irelend, &insn16)
11670 || is_16bit_NOP (abfd, sec, irel))
11671 {
11672 if (insn_rel)
11673 {
11674 /* Previous INSN16 reloc exists, reduce its
11675 size to 16-bit. */
11676 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11677 irelend, &insn16))
11678 {
11679 nds32_elf_write_16 (abfd, contents, insn_rel,
11680 internal_relocs, irelend, insn16);
11681
11682 if (!insert_nds32_elf_blank_recalc_total
11683 (relax_blank_list, insn_rel->r_offset + 2, 2))
11684 return FALSE;
11685 }
11686 else if (is_16bit_NOP (abfd, sec, insn_rel))
11687 {
11688 if (!insert_nds32_elf_blank_recalc_total
11689 (relax_blank_list, insn_rel->r_offset, 2))
11690 return FALSE;
11691 }
11692 insn_rel->r_info =
11693 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11694 }
11695 /* Save the new one for later use. */
11696 insn_rel = irel;
11697 }
11698 else
11699 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11700 R_NDS32_NONE);
11701 }
11702 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11703 {
11704 /* Search for label. */
11705 int force_relax = 0;
11706
11707 /* Label on 16-bit instruction or optimization
11708 needless, just reset this reloc. */
11709 insn16 = bfd_getb16 (contents + irel->r_offset);
11710 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
11711 {
11712 irel->r_info =
11713 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11714 continue;
11715 }
11716
11717 address =
11718 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
11719 irel->r_offset, 1);
11720
11721 if (!insn_rel)
11722 {
11723 /* Check if there is case which can not be aligned. */
11724 if (irel->r_addend == 2 && address & 0x2)
11725 return FALSE;
11726 continue;
11727 }
11728
11729 /* Try to align this label. */
11730
11731 if ((irel->r_addend & 0x1f) < 2)
11732 {
11733 /* Check if there is a INSN16 at the same address.
11734 Label_rel always seats before insn_rel after
11735 our sort. */
11736
11737 /* Search for INSN16 at LABEL location. If INSN16 is at
11738 same location and this LABEL alignment is lower than 2,
11739 the INSN16 can be converted to 2-byte. */
11740 for (tmp_rel = irel;
11741 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
11742 tmp_rel++)
11743 {
11744 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
11745 && (is_convert_32_to_16
11746 (abfd, sec, tmp_rel, internal_relocs,
11747 irelend, &insn16)
11748 || is_16bit_NOP (abfd, sec, tmp_rel)))
11749 {
11750 force_relax = 1;
11751 break;
11752 }
11753 }
11754 }
11755
11756 if (force_relax || irel->r_addend == 1 || address & 0x2)
11757 {
11758 /* Label not aligned. */
11759 /* Previous reloc exists, reduce its size to 16-bit. */
11760 if (is_convert_32_to_16 (abfd, sec, insn_rel,
11761 internal_relocs, irelend, &insn16))
11762 {
11763 nds32_elf_write_16 (abfd, contents, insn_rel,
11764 internal_relocs, irelend, insn16);
11765
11766 if (!insert_nds32_elf_blank_recalc_total
11767 (relax_blank_list, insn_rel->r_offset + 2, 2))
11768 return FALSE;
11769 }
11770 else if (is_16bit_NOP (abfd, sec, insn_rel))
11771 {
11772 if (!insert_nds32_elf_blank_recalc_total
11773 (relax_blank_list, insn_rel->r_offset, 2))
11774 return FALSE;
11775 }
11776
11777 }
11778 /* INSN16 reloc is used. */
11779 insn_rel = NULL;
11780 }
11781 }
11782
11783 address =
11784 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
11785 if (insn_rel && (address & 0x2 || opt_size))
11786 {
11787 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11788 irelend, &insn16))
11789 {
11790 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
11791 irelend, insn16);
11792 if (!insert_nds32_elf_blank_recalc_total
11793 (relax_blank_list, insn_rel->r_offset + 2, 2))
11794 return FALSE;
11795 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11796 R_NDS32_NONE);
11797 }
11798 else if (is_16bit_NOP (abfd, sec, insn_rel))
11799 {
11800 if (!insert_nds32_elf_blank_recalc_total
11801 (relax_blank_list, insn_rel->r_offset, 2))
11802 return FALSE;
11803 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
11804 R_NDS32_NONE);
11805 }
11806 }
11807 insn_rel = NULL;
11808 return TRUE;
11809 }
11810
11811 /* Pick relaxation round. */
11812
11813 static int
11814 nds32_elf_pick_relax (bfd_boolean init, asection *sec, bfd_boolean *again,
11815 struct elf_nds32_link_hash_table *table,
11816 struct bfd_link_info *link_info)
11817 {
11818 static asection *final_sec;
11819 static bfd_boolean set = FALSE;
11820 static bfd_boolean first = TRUE;
11821 int round_table[] = {
11822 NDS32_RELAX_NORMAL_ROUND,
11823 NDS32_RELAX_JUMP_IFC_ROUND,
11824 NDS32_RELAX_EX9_BUILD_ROUND,
11825 NDS32_RELAX_EX9_REPLACE_ROUND,
11826 };
11827 static int pass = 0;
11828 static int relax_round;
11829
11830 if (first)
11831 {
11832 /* Run an empty run to get the final section. */
11833 relax_round = NDS32_RELAX_EMPTY_ROUND;
11834
11835 /* It has to enter relax again because we can
11836 not make sure what the final turn is. */
11837 *again = TRUE;
11838 first = FALSE;
11839 }
11840
11841 if (!set && *again)
11842 {
11843 /* It is reentered when again is FALSE. */
11844 final_sec = sec;
11845 return relax_round;
11846 }
11847
11848 /* The second round begins. */
11849 set = TRUE;
11850
11851 relax_round = round_table[pass];
11852
11853 if (!init && final_sec == sec)
11854 {
11855 switch (relax_round)
11856 {
11857 case NDS32_RELAX_NORMAL_ROUND:
11858 if (!*again)
11859 {
11860 /* Normal relaxation done. */
11861 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11862 {
11863 pass++;
11864 *again = TRUE;
11865 }
11866 else if (table->target_optimize & NDS32_RELAX_EX9_ON)
11867 {
11868 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11869 *again = TRUE;
11870 }
11871 else if (table->ex9_import_file)
11872 {
11873 /* Import ex9 table. */
11874 if (table->update_ex9_table)
11875 pass += 2; /* NDS32_RELAX_EX9_BUILD_ROUND */
11876 else
11877 pass += 3; /* NDS32_RELAX_EX9_REPLACE_ROUND */
11878 nds32_elf_ex9_import_table (link_info);
11879 *again = TRUE;
11880 }
11881 }
11882 break;
11883 case NDS32_RELAX_JUMP_IFC_ROUND:
11884 if (!nds32_elf_ifc_finish (link_info))
11885 (*_bfd_error_handler) (_("error: Jump IFC Fail."));
11886 if (table->target_optimize & NDS32_RELAX_EX9_ON)
11887 {
11888 pass++;
11889 *again = TRUE;
11890 }
11891 break;
11892 case NDS32_RELAX_EX9_BUILD_ROUND:
11893 nds32_elf_ex9_finish (link_info);
11894 pass++;
11895 *again = TRUE;
11896 break;
11897 case NDS32_RELAX_EX9_REPLACE_ROUND:
11898 if (table->target_optimize & NDS32_RELAX_JUMP_IFC_ON)
11899 {
11900 /* Do jump IFC optimization again. */
11901 if (!nds32_elf_ifc_finish (link_info))
11902 (*_bfd_error_handler) (_("error: Jump IFC Fail."));
11903 }
11904 break;
11905 default:
11906 break;
11907 }
11908 }
11909
11910 return relax_round;
11911 }
11912
11913 static bfd_boolean
11914 nds32_elf_relax_section (bfd *abfd, asection *sec,
11915 struct bfd_link_info *link_info, bfd_boolean *again)
11916 {
11917 nds32_elf_blank_t *relax_blank_list = NULL;
11918 Elf_Internal_Shdr *symtab_hdr;
11919 Elf_Internal_Rela *internal_relocs;
11920 Elf_Internal_Rela *irel;
11921 Elf_Internal_Rela *irelend;
11922 Elf_Internal_Sym *isymbuf = NULL;
11923 bfd_byte *contents = NULL;
11924 bfd_boolean result = TRUE;
11925 int optimize = 0;
11926 int opt_size = 0;
11927 uint32_t insn;
11928 uint16_t insn16;
11929
11930 /* Target dependnet option. */
11931 struct elf_nds32_link_hash_table *table;
11932 int load_store_relax;
11933 int relax_round;
11934
11935 relax_blank_list = NULL;
11936
11937 *again = FALSE;
11938
11939 /* Nothing to do for
11940 * relocatable link or
11941 * non-relocatable section or
11942 * non-code section or
11943 * empty content or
11944 * no reloc entry. */
11945 if (link_info->relocatable
11946 || (sec->flags & SEC_RELOC) == 0
11947 || (sec->flags & SEC_EXCLUDE) == 1
11948 || (sec->flags & SEC_CODE) == 0
11949 || sec->size == 0)
11950 return TRUE;
11951
11952 /* 09.12.11 Workaround. */
11953 /* We have to adjust align for R_NDS32_LABEL if needed.
11954 The adjust approach only can fix 2-byte align once. */
11955 if (sec->alignment_power > 2)
11956 return TRUE;
11957
11958 /* The optimization type to do. */
11959
11960 table = nds32_elf_hash_table (link_info);
11961 relax_round = nds32_elf_pick_relax (TRUE, sec, again, table, link_info);
11962 switch (relax_round)
11963 {
11964 case NDS32_RELAX_JUMP_IFC_ROUND:
11965 /* Here is the entrance of ifc jump relaxation. */
11966 if (!nds32_elf_ifc_calc (link_info, abfd, sec))
11967 return FALSE;
11968 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11969 return TRUE;
11970
11971 case NDS32_RELAX_EX9_BUILD_ROUND:
11972 /* Here is the entrance of ex9 relaxation. There are two pass of
11973 ex9 relaxation. The one is to traverse all instructions and build
11974 the hash table. The other one is to compare instructions and replace
11975 it by ex9.it. */
11976 if (!nds32_elf_ex9_build_hash_table (abfd, sec, link_info))
11977 return FALSE;
11978 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11979 return TRUE;
11980
11981 case NDS32_RELAX_EX9_REPLACE_ROUND:
11982 if (!nds32_elf_ex9_replace_instruction (link_info, abfd, sec))
11983 return FALSE;
11984 return TRUE;
11985
11986 case NDS32_RELAX_EMPTY_ROUND:
11987 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
11988 return TRUE;
11989
11990 case NDS32_RELAX_NORMAL_ROUND:
11991 default:
11992 if (sec->reloc_count == 0)
11993 return TRUE;
11994 break;
11995 }
11996
11997 /* The begining of general relaxation. */
11998
11999 if (is_SDA_BASE_set == 0)
12000 {
12001 bfd_vma gp;
12002 is_SDA_BASE_set = 1;
12003 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12004 &gp, FALSE);
12005 relax_range_measurement (abfd);
12006 }
12007
12008 if (is_ITB_BASE_set == 0)
12009 {
12010 /* Set the _ITB_BASE_. */
12011 if (!nds32_elf_ex9_itb_base (link_info))
12012 {
12013 (*_bfd_error_handler) (_("%B: error: Cannot set _ITB_BASE_"), abfd);
12014 bfd_set_error (bfd_error_bad_value);
12015 }
12016 }
12017
12018 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12019 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12020 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12021 TRUE /* keep_memory */);
12022 if (internal_relocs == NULL)
12023 goto error_return;
12024
12025 irelend = internal_relocs + sec->reloc_count;
12026 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12027 irelend, R_NDS32_RELAX_ENTRY);
12028
12029 if (irel == irelend)
12030 return TRUE;
12031
12032 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12033 {
12034 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12035 return TRUE;
12036
12037 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12038 optimize = 1;
12039
12040 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12041 opt_size = 1;
12042 }
12043
12044 load_store_relax = table->load_store_relax;
12045
12046 /* Get symbol table and section content. */
12047 if (!nds32_get_section_contents (abfd, sec, &contents)
12048 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12049 goto error_return;
12050
12051 /* Do relax loop only when finalize is not done.
12052 Take care of relaxable relocs except INSN16. */
12053 for (irel = internal_relocs; irel < irelend; irel++)
12054 {
12055 int seq_len; /* Original length of instruction sequence. */
12056 int insn_len = 0; /* Final length of instruction sequence. */
12057 bfd_boolean removed;
12058
12059 insn = 0;
12060 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12061 && (irel->r_addend & 0x1f) >= 2)
12062 optimize = 1;
12063
12064 /* Relocation Types
12065 R_NDS32_LONGCALL1 53
12066 R_NDS32_LONGCALL2 54
12067 R_NDS32_LONGCALL3 55
12068 R_NDS32_LONGJUMP1 56
12069 R_NDS32_LONGJUMP2 57
12070 R_NDS32_LONGJUMP3 58
12071 R_NDS32_LOADSTORE 59 */
12072 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12073 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12074 seq_len = GET_SEQ_LEN (irel->r_addend);
12075
12076 /* Relocation Types
12077 R_NDS32_LONGCALL4 107
12078 R_NDS32_LONGCALL5 108
12079 R_NDS32_LONGCALL6 109
12080 R_NDS32_LONGJUMP4 110
12081 R_NDS32_LONGJUMP5 111
12082 R_NDS32_LONGJUMP6 112
12083 R_NDS32_LONGJUMP7 113 */
12084 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12085 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12086 seq_len = 4;
12087
12088 /* Relocation Types
12089 R_NDS32_LO12S0_RELA 30
12090 R_NDS32_LO12S1_RELA 29
12091 R_NDS32_LO12S2_RELA 28
12092 R_NDS32_LO12S2_SP_RELA 71
12093 R_NDS32_LO12S2_DP_RELA 70
12094 R_NDS32_GOT_LO12 46
12095 R_NDS32_GOTOFF_LO12 50
12096 R_NDS32_PLTREL_LO12 65
12097 R_NDS32_PLT_GOTREL_LO12 67
12098 R_NDS32_17IFC_PCREL_RELA 96
12099 R_NDS32_GOT_SUFF 193
12100 R_NDS32_GOTOFF_SUFF 194
12101 R_NDS32_PLT_GOT_SUFF 195
12102 R_NDS32_MULCALL_SUFF 196
12103 R_NDS32_PTR 197 */
12104 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12105 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12106 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12107 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12108 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12109 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12110 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12111 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12112 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12113 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12114 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12115 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12116 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12117 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12118 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS)
12119 seq_len = 0;
12120 else
12121 continue;
12122
12123 insn_len = seq_len;
12124 removed = FALSE;
12125
12126 switch (ELF32_R_TYPE (irel->r_info))
12127 {
12128 case R_NDS32_LONGCALL1:
12129 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12130 &insn_len, contents, isymbuf,
12131 symtab_hdr);
12132 break;
12133 case R_NDS32_LONGCALL2:
12134 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12135 &insn_len, contents, isymbuf,
12136 symtab_hdr);
12137 break;
12138 case R_NDS32_LONGCALL3:
12139 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12140 &insn_len, contents, isymbuf,
12141 symtab_hdr);
12142 break;
12143 case R_NDS32_LONGJUMP1:
12144 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12145 &insn_len, contents, isymbuf,
12146 symtab_hdr);
12147 break;
12148 case R_NDS32_LONGJUMP2:
12149 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12150 &insn_len, contents, isymbuf,
12151 symtab_hdr);
12152 break;
12153 case R_NDS32_LONGJUMP3:
12154 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12155 &insn_len, contents, isymbuf,
12156 symtab_hdr);
12157 break;
12158 case R_NDS32_LONGCALL4:
12159 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12160 &insn_len, contents, isymbuf,
12161 symtab_hdr);
12162 break;
12163 case R_NDS32_LONGCALL5:
12164 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12165 &insn_len, contents, isymbuf,
12166 symtab_hdr);
12167 break;
12168 case R_NDS32_LONGCALL6:
12169 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12170 &insn_len, contents, isymbuf,
12171 symtab_hdr);
12172 break;
12173 case R_NDS32_LONGJUMP4:
12174 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12175 &insn_len, contents, isymbuf,
12176 symtab_hdr);
12177 break;
12178 case R_NDS32_LONGJUMP5:
12179 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12180 &insn_len, &seq_len, contents,
12181 isymbuf, symtab_hdr);
12182 break;
12183 case R_NDS32_LONGJUMP6:
12184 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12185 &insn_len, &seq_len, contents,
12186 isymbuf, symtab_hdr);
12187 break;
12188 case R_NDS32_LONGJUMP7:
12189 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12190 &insn_len, &seq_len, contents,
12191 isymbuf, symtab_hdr);
12192 break;
12193 case R_NDS32_LOADSTORE:
12194 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12195 internal_relocs, &insn_len,
12196 contents, isymbuf, symtab_hdr,
12197 load_store_relax);
12198 break;
12199 case R_NDS32_LO12S0_RELA:
12200 case R_NDS32_LO12S1_RELA:
12201 case R_NDS32_LO12S2_DP_RELA:
12202 case R_NDS32_LO12S2_SP_RELA:
12203 case R_NDS32_LO12S2_RELA:
12204 /* Relax for low part. */
12205 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12206 contents, isymbuf, symtab_hdr);
12207
12208 /* It is impossible to delete blank, so just continue. */
12209 continue;
12210 case R_NDS32_GOT_LO12:
12211 case R_NDS32_GOTOFF_LO12:
12212 case R_NDS32_PLTREL_LO12:
12213 case R_NDS32_PLT_GOTREL_LO12:
12214 case R_NDS32_GOTPC_LO12:
12215 /* Relax for PIC gp-relative low part. */
12216 nds32_elf_relax_piclo12 (link_info, abfd, sec, irel, contents,
12217 isymbuf, symtab_hdr);
12218
12219 /* It is impossible to delete blank, so just continue. */
12220 continue;
12221 case R_NDS32_TLS_LE_LO12:
12222 /* Relax for LE TLS low part. */
12223 nds32_elf_relax_letlslo12 (link_info, abfd, irel, contents,
12224 isymbuf, symtab_hdr);
12225
12226 /* It is impossible to delete blank, so just continue. */
12227 continue;
12228 case R_NDS32_TLS_LE_ADD:
12229 nds32_elf_relax_letlsadd (link_info, abfd, sec, irel, internal_relocs,
12230 contents, isymbuf, symtab_hdr, again);
12231 /* It is impossible to delete blank, so just continue. */
12232 continue;
12233 case R_NDS32_TLS_LE_LS:
12234 nds32_elf_relax_letlsls (link_info, abfd, sec, irel, internal_relocs,
12235 contents, isymbuf, symtab_hdr, again);
12236 continue;
12237 case R_NDS32_PTR:
12238 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12239 &insn_len, &seq_len, contents);
12240 break;
12241 case R_NDS32_PLT_GOT_SUFF:
12242 nds32_elf_relax_pltgot_suff (link_info, abfd, sec, irel,
12243 internal_relocs, contents,
12244 isymbuf, symtab_hdr, again);
12245 /* It is impossible to delete blank, so just continue. */
12246 continue;
12247 case R_NDS32_GOT_SUFF:
12248 nds32_elf_relax_got_suff (link_info, abfd, sec, irel,
12249 internal_relocs, contents,
12250 symtab_hdr, again);
12251 /* It is impossible to delete blank, so just continue. */
12252 continue;
12253 case R_NDS32_GOTOFF_SUFF:
12254 nds32_elf_relax_gotoff_suff (link_info, abfd, sec, irel,
12255 internal_relocs, contents,
12256 isymbuf, symtab_hdr, again);
12257 /* It is impossible to delete blank, so just continue. */
12258 continue;
12259 default:
12260 continue;
12261
12262 }
12263 if (removed && seq_len - insn_len > 0)
12264 {
12265 if (!insert_nds32_elf_blank
12266 (&relax_blank_list, irel->r_offset + insn_len,
12267 seq_len - insn_len))
12268 goto error_return;
12269 *again = TRUE;
12270 }
12271 }
12272
12273 calc_nds32_blank_total (relax_blank_list);
12274
12275 if (table->relax_fp_as_gp)
12276 {
12277 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12278 irelend, isymbuf))
12279 goto error_return;
12280
12281 if (*again == FALSE)
12282 {
12283 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12284 irelend))
12285 goto error_return;
12286 }
12287 }
12288
12289 nds32_elf_pick_relax (FALSE, sec, again, table, link_info);
12290
12291 if (*again == FALSE)
12292 {
12293 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12294 &relax_blank_list, optimize, opt_size))
12295 goto error_return;
12296 }
12297
12298 /* It doesn't matter optimize_for_space_no_align anymore.
12299 If object file is assembled with flag '-Os',
12300 the we don't adjust jump-destination on 4-byte boundary. */
12301
12302 if (relax_blank_list)
12303 {
12304 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12305 relax_blank_list = NULL;
12306 }
12307
12308 if (*again == FALSE)
12309 {
12310 /* Closing the section, so we don't relax it anymore. */
12311 bfd_vma sec_size_align;
12312 Elf_Internal_Rela *tmp_rel;
12313
12314 /* Pad to alignment boundary. Only handle current section alignment. */
12315 sec_size_align = (sec->size + (~((-1) << sec->alignment_power)))
12316 & ((-1) << sec->alignment_power);
12317 if ((sec_size_align - sec->size) & 0x2)
12318 {
12319 insn16 = NDS32_NOP16;
12320 bfd_putb16 (insn16, contents + sec->size);
12321 sec->size += 2;
12322 }
12323
12324 while (sec_size_align != sec->size)
12325 {
12326 insn = NDS32_NOP32;
12327 bfd_putb32 (insn, contents + sec->size);
12328 sec->size += 4;
12329 }
12330
12331 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12332 irelend, R_NDS32_RELAX_ENTRY);
12333 if (tmp_rel != irelend)
12334 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12335
12336 clean_nds32_elf_blank ();
12337 }
12338
12339 finish:
12340 if (internal_relocs != NULL
12341 && elf_section_data (sec)->relocs != internal_relocs)
12342 free (internal_relocs);
12343
12344 if (contents != NULL
12345 && elf_section_data (sec)->this_hdr.contents != contents)
12346 free (contents);
12347
12348 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12349 free (isymbuf);
12350
12351 return result;
12352
12353 error_return:
12354 result = FALSE;
12355 goto finish;
12356 }
12357
12358 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12359 {
12360 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12361 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12362 {NULL, 0, 0, 0, 0}
12363 };
12364
12365 static bfd_boolean
12366 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12367 struct bfd_link_info *info,
12368 void *finfo ATTRIBUTE_UNUSED,
12369 bfd_boolean (*func) (void *, const char *,
12370 Elf_Internal_Sym *,
12371 asection *,
12372 struct elf_link_hash_entry *)
12373 ATTRIBUTE_UNUSED)
12374 {
12375 FILE *sym_ld_script = NULL;
12376 struct elf_nds32_link_hash_table *table;
12377
12378 table = nds32_elf_hash_table (info);
12379 sym_ld_script = table->sym_ld_script;
12380
12381 if (check_start_export_sym)
12382 fprintf (sym_ld_script, "}\n");
12383
12384 return TRUE;
12385 }
12386
12387 static enum elf_reloc_type_class
12388 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12389 const asection *rel_sec ATTRIBUTE_UNUSED,
12390 const Elf_Internal_Rela *rela)
12391 {
12392 switch ((int) ELF32_R_TYPE (rela->r_info))
12393 {
12394 case R_NDS32_RELATIVE:
12395 return reloc_class_relative;
12396 case R_NDS32_JMP_SLOT:
12397 return reloc_class_plt;
12398 case R_NDS32_COPY:
12399 return reloc_class_copy;
12400 default:
12401 return reloc_class_normal;
12402 }
12403 }
12404
12405 /* Put target dependent option into info hash table. */
12406 void
12407 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12408 int relax_fp_as_gp,
12409 int eliminate_gc_relocs,
12410 FILE * sym_ld_script, int load_store_relax,
12411 int target_optimize, int relax_status,
12412 int relax_round, FILE * ex9_export_file,
12413 FILE * ex9_import_file,
12414 int update_ex9_table, int ex9_limit,
12415 bfd_boolean ex9_loop_aware,
12416 bfd_boolean ifc_loop_aware)
12417 {
12418 struct elf_nds32_link_hash_table *table;
12419
12420 table = nds32_elf_hash_table (link_info);
12421 if (table == NULL)
12422 return;
12423
12424 table->relax_fp_as_gp = relax_fp_as_gp;
12425 table->eliminate_gc_relocs = eliminate_gc_relocs;
12426 table->sym_ld_script = sym_ld_script;
12427 table ->load_store_relax = load_store_relax;
12428 table->target_optimize = target_optimize;
12429 table->relax_status = relax_status;
12430 table->relax_round = relax_round;
12431 table->ex9_export_file = ex9_export_file;
12432 table->ex9_import_file = ex9_import_file;
12433 table->update_ex9_table = update_ex9_table;
12434 table->ex9_limit = ex9_limit;
12435 table->ex9_loop_aware = ex9_loop_aware;
12436 table->ifc_loop_aware = ifc_loop_aware;
12437 }
12438 \f
12439 /* These functions and data-structures are used for fp-as-gp
12440 optimization. */
12441
12442 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12443 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12444 the read-only section and read-write section. */
12445 #define FAG_WINDOW (508 - 32)
12446
12447 /* An nds32_fag represent a gp-relative access.
12448 We find best fp-base by using a sliding window
12449 to find a base address which can cover most gp-access. */
12450 struct nds32_fag
12451 {
12452 struct nds32_fag *next; /* NULL-teminated linked list. */
12453 bfd_vma addr; /* The address of this fag. */
12454 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12455 It is used for applying FP7U2_FLAG. */
12456 int count; /* How many times this address is referred.
12457 There should be exactly `count' relocations
12458 in relas. */
12459 int relas_capcity; /* The buffer size of relas.
12460 We use an array instead of linked-list,
12461 and realloc is used to adjust buffer size. */
12462 };
12463
12464 static void
12465 nds32_fag_init (struct nds32_fag *head)
12466 {
12467 memset (head, 0, sizeof (struct nds32_fag));
12468 }
12469
12470 static void
12471 nds32_fag_verify (struct nds32_fag *head)
12472 {
12473 struct nds32_fag *iter;
12474 struct nds32_fag *prev;
12475
12476 prev = NULL;
12477 iter = head->next;
12478 while (iter)
12479 {
12480 if (prev && prev->addr >= iter->addr)
12481 puts ("Bug in fp-as-gp insertion.");
12482 prev = iter;
12483 iter = iter->next;
12484 }
12485 }
12486
12487 /* Insert a fag in ascending order.
12488 If a fag of the same address already exists,
12489 they are chained by relas array. */
12490
12491 static void
12492 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12493 Elf_Internal_Rela * rel)
12494 {
12495 struct nds32_fag *iter;
12496 struct nds32_fag *new_fag;
12497 const int INIT_RELAS_CAP = 4;
12498
12499 for (iter = head;
12500 iter->next && iter->next->addr <= addr;
12501 iter = iter->next)
12502 /* Find somewhere to insert. */ ;
12503
12504 /* `iter' will be equal to `head' if the list is empty. */
12505 if (iter != head && iter->addr == addr)
12506 {
12507 /* The address exists in the list.
12508 Insert `rel' into relocation list, relas. */
12509
12510 /* Check whether relas is big enough. */
12511 if (iter->count >= iter->relas_capcity)
12512 {
12513 iter->relas_capcity *= 2;
12514 iter->relas = bfd_realloc
12515 (iter->relas, iter->relas_capcity * sizeof (void *));
12516 }
12517 iter->relas[iter->count++] = rel;
12518 return;
12519 }
12520
12521 /* This is a new address. Create a fag node for it. */
12522 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12523 memset (new_fag, 0, sizeof (*new_fag));
12524 new_fag->addr = addr;
12525 new_fag->count = 1;
12526 new_fag->next = iter->next;
12527 new_fag->relas_capcity = INIT_RELAS_CAP;
12528 new_fag->relas = (Elf_Internal_Rela **)
12529 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12530 new_fag->relas[0] = rel;
12531 iter->next = new_fag;
12532
12533 nds32_fag_verify (head);
12534 }
12535
12536 static void
12537 nds32_fag_free_list (struct nds32_fag *head)
12538 {
12539 struct nds32_fag *iter;
12540
12541 iter = head->next;
12542 while (iter)
12543 {
12544 struct nds32_fag *tmp = iter;
12545 iter = iter->next;
12546 free (tmp->relas);
12547 tmp->relas = NULL;
12548 free (tmp);
12549 }
12550 }
12551
12552 /* Find the best fp-base address.
12553 The relocation associated with that address is returned,
12554 so we can track the symbol instead of a fixed address.
12555
12556 When relaxation, the address of an datum may change,
12557 because a text section is shrinked, so the data section
12558 moves forward. If the aligments of text and data section
12559 are different, their distance may change too.
12560 Therefore, tracking a fixed address is not appriate. */
12561
12562 static int
12563 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12564 {
12565 struct nds32_fag *base; /* First fag in the window. */
12566 struct nds32_fag *last; /* First fag outside the window. */
12567 int accu = 0; /* Usage accumulation. */
12568 struct nds32_fag *best; /* Best fag. */
12569 int baccu = 0; /* Best accumulation. */
12570
12571 /* Use first fag for initial, and find the last fag in the window.
12572
12573 In each iteration, we could simply subtract previous fag
12574 and accumulate following fags which are inside the window,
12575 untill we each the end. */
12576
12577 if (head->next == NULL)
12578 {
12579 *bestpp = NULL;
12580 return 0;
12581 }
12582
12583 /* Initialize base. */
12584 base = head->next;
12585 best = base;
12586 for (last = base;
12587 last && last->addr < base->addr + FAG_WINDOW;
12588 last = last->next)
12589 accu += last->count;
12590
12591 baccu = accu;
12592
12593 /* Record the best base in each iteration. */
12594 while (base->next)
12595 {
12596 accu -= base->count;
12597 base = base->next;
12598 /* Account fags in window. */
12599 for (/* Nothing. */;
12600 last && last->addr < base->addr + FAG_WINDOW;
12601 last = last->next)
12602 accu += last->count;
12603
12604 /* A better fp-base? */
12605 if (accu > baccu)
12606 {
12607 best = base;
12608 baccu = accu;
12609 }
12610 }
12611
12612 if (bestpp)
12613 *bestpp = best;
12614 return baccu;
12615 }
12616
12617 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12618 so we can convert it fo fp-relative access later.
12619 `best_fag' is the best fp-base. Only those inside the window
12620 of best_fag is applied the flag. */
12621
12622 static bfd_boolean
12623 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12624 bfd *abfd, struct nds32_fag *best_fag,
12625 Elf_Internal_Rela *internal_relocs,
12626 Elf_Internal_Rela *irelend)
12627 {
12628 struct nds32_fag *ifag;
12629 bfd_vma best_fpbase, gp;
12630 bfd *output_bfd;
12631
12632 output_bfd = abfd->sections->output_section->owner;
12633 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12634 best_fpbase = best_fag->addr;
12635
12636 if (best_fpbase > gp + sdata_range[1][1]
12637 || best_fpbase < gp - sdata_range[1][0])
12638 return FALSE;
12639
12640 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12641 so we know they can be converted to lwi37.fp. */
12642 for (ifag = best_fag;
12643 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12644 {
12645 int i;
12646
12647 for (i = 0; i < ifag->count; i++)
12648 {
12649 Elf_Internal_Rela *insn16_rel;
12650 Elf_Internal_Rela *fag_rel;
12651
12652 fag_rel = ifag->relas[i];
12653
12654 /* Only if this is within the WINDOWS, FP7U2_FLAG
12655 is applied. */
12656
12657 insn16_rel = find_relocs_at_address
12658 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12659
12660 if (insn16_rel != irelend)
12661 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12662 }
12663 }
12664 return TRUE;
12665 }
12666
12667 /* Reset INSN16 to clean fp as gp. */
12668
12669 static void
12670 nds32_fag_unmark_relax (struct nds32_fag *fag,
12671 Elf_Internal_Rela *internal_relocs,
12672 Elf_Internal_Rela *irelend)
12673 {
12674 struct nds32_fag *ifag;
12675 int i;
12676 Elf_Internal_Rela *insn16_rel;
12677 Elf_Internal_Rela *fag_rel;
12678
12679 for (ifag = fag; ifag; ifag = ifag->next)
12680 {
12681 for (i = 0; i < ifag->count; i++)
12682 {
12683 fag_rel = ifag->relas[i];
12684
12685 /* Restore the INSN16 relocation. */
12686 insn16_rel = find_relocs_at_address
12687 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12688
12689 if (insn16_rel != irelend)
12690 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12691 }
12692 }
12693 }
12694
12695 /* This is the main function of fp-as-gp optimization.
12696 It should be called by relax_section. */
12697
12698 static bfd_boolean
12699 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12700 bfd *abfd, asection *sec,
12701 Elf_Internal_Rela *internal_relocs,
12702 Elf_Internal_Rela *irelend,
12703 Elf_Internal_Sym *isymbuf)
12704 {
12705 Elf_Internal_Rela *begin_rel = NULL;
12706 Elf_Internal_Rela *irel;
12707 struct nds32_fag fag_head;
12708 Elf_Internal_Shdr *symtab_hdr;
12709 bfd_byte *contents;
12710 bfd_boolean ifc_inside = FALSE;
12711
12712 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12713
12714 /* Per-function fp-base selection.
12715 1. Create a list for all the gp-relative access.
12716 2. Base on those gp-relative address,
12717 find a fp-base which can cover most access.
12718 3. Use the fp-base for fp-as-gp relaxation.
12719
12720 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12721 we should
12722 1. delete the `la $fp, _FP_BASE_' instruction and
12723 2. not convert lwi.gp to lwi37.fp.
12724
12725 To delete the _FP_BASE_ instruction, we simply apply
12726 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12727
12728 To suppress the conversion, we simply NOT to apply
12729 R_NDS32_INSN16_FP7U2_FLAG flag. */
12730
12731 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12732
12733 if (!nds32_get_section_contents (abfd, sec, &contents)
12734 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12735 return FALSE;
12736
12737 /* Check whether it is worth for fp-as-gp optimization,
12738 i.e., at least 3 gp-load.
12739
12740 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12741 apply this optimization. */
12742
12743 for (irel = internal_relocs; irel < irelend; irel++)
12744 {
12745 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12746 One we enter the begin of the region, we track all the LW/ST
12747 instructions, so when we leave the region, we try to find
12748 the best fp-base address for those LW/ST instructions. */
12749
12750 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12751 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12752 {
12753 /* Begin of the region. */
12754 if (begin_rel)
12755 (*_bfd_error_handler) (_("%B: Nested OMIT_FP in %A."), abfd, sec);
12756
12757 begin_rel = irel;
12758 nds32_fag_init (&fag_head);
12759 ifc_inside = FALSE;
12760 }
12761 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12762 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12763 {
12764 int accu;
12765 struct nds32_fag *best_fag, *tmp_fag;
12766 int dist;
12767
12768 /* End of the region.
12769 Check whether it is worth to do fp-as-gp. */
12770
12771 if (begin_rel == NULL)
12772 {
12773 (*_bfd_error_handler) (_("%B: Unmatched OMIT_FP in %A."), abfd, sec);
12774 continue;
12775 }
12776
12777 accu = nds32_fag_find_base (&fag_head, &best_fag);
12778
12779 /* Clean FP7U2_FLAG because they may set ever. */
12780 tmp_fag = fag_head.next;
12781 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12782
12783 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12784 if (accu < FAG_THRESHOLD
12785 || !nds32_fag_mark_relax (link_info, abfd, best_fag,
12786 internal_relocs, irelend))
12787 {
12788 /* Not worth to do fp-as-gp. */
12789 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12790 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12791 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12792 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12793 nds32_fag_free_list (&fag_head);
12794 begin_rel = NULL;
12795 continue;
12796 }
12797
12798 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12799 so we use it to record the distance to the reloction of best
12800 fp-base. */
12801 dist = best_fag->relas[0] - begin_rel;
12802 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12803 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12804 relocation. And get the base value when relocating. */
12805 begin_rel->r_addend &= (0x1 << 16) - 1;
12806 begin_rel->r_addend |= dist << 16;
12807
12808 nds32_fag_free_list (&fag_head);
12809 begin_rel = NULL;
12810 }
12811
12812 if (begin_rel == NULL || ifc_inside)
12813 /* Skip if we are not in the region of fp-as-gp. */
12814 continue;
12815
12816 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12817 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12818 {
12819 bfd_vma addr;
12820 uint32_t insn;
12821
12822 /* A gp-relative access is found. Insert it to the fag-list. */
12823
12824 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
12825 insn = bfd_getb32 (contents + irel->r_offset);
12826 if (!N32_IS_RT3 (insn))
12827 continue;
12828
12829 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12830 nds32_fag_insert (&fag_head, addr, irel);
12831 }
12832 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12833 {
12834 begin_rel = NULL;
12835 }
12836 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12837 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12838 {
12839 /* Suppress fp as gp when encounter ifc. */
12840 ifc_inside = TRUE;
12841 }
12842 }
12843
12844 return TRUE;
12845 }
12846
12847 /* Remove unused `la $fp, _FD_BASE_' instruction. */
12848
12849 static bfd_boolean
12850 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12851 Elf_Internal_Rela *internal_relocs,
12852 Elf_Internal_Rela *irelend)
12853 {
12854 Elf_Internal_Rela *irel;
12855 Elf_Internal_Shdr *symtab_hdr;
12856 bfd_byte *contents = NULL;
12857 nds32_elf_blank_t *relax_blank_list = NULL;
12858 bfd_boolean result = TRUE;
12859 bfd_boolean unused_region = FALSE;
12860
12861 /*
12862 NOTE: Disable fp-as-gp if we encounter ifcall relocations.
12863 * R_NDS32_17IFC_PCREL_RELA
12864 * R_NDS32_10IFCU_PCREL_RELA
12865
12866 CASE??????????????
12867 */
12868
12869 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12870 nds32_get_section_contents (abfd, sec, &contents);
12871
12872 for (irel = internal_relocs; irel < irelend; irel++)
12873 {
12874 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
12875 we marked to in previous pass.
12876 DO NOT scan relocations again, since we've alreadly decided it
12877 and set the flag. */
12878 const char *syname;
12879 int syndx;
12880 uint32_t insn;
12881
12882 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12883 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12884 unused_region = TRUE;
12885 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12886 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12887 unused_region = FALSE;
12888
12889 /* We're not in the region. */
12890 if (!unused_region)
12891 continue;
12892
12893 /* _FP_BASE_ must be a GLOBAL symbol. */
12894 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
12895 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
12896 continue;
12897
12898 /* The symbol name must be _FP_BASE_. */
12899 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
12900 if (strcmp (syname, FP_BASE_NAME) != 0)
12901 continue;
12902
12903 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
12904 {
12905 /* addi.gp $fp, -256 */
12906 insn = bfd_getb32 (contents + irel->r_offset);
12907 if (insn != INSN_ADDIGP_TO_FP)
12908 continue;
12909 }
12910 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
12911 {
12912 /* addi $fp, $gp, -256 */
12913 insn = bfd_getb32 (contents + irel->r_offset);
12914 if (insn != INSN_ADDI_GP_TO_FP)
12915 continue;
12916 }
12917 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
12918 {
12919 /* movi $fp, FP_BASE */
12920 insn = bfd_getb32 (contents + irel->r_offset);
12921 if (insn != INSN_MOVI_TO_FP)
12922 continue;
12923 }
12924 else
12925 continue;
12926
12927 /* We got here because a FP_BASE instruction is found. */
12928 if (!insert_nds32_elf_blank_recalc_total
12929 (&relax_blank_list, irel->r_offset, 4))
12930 goto error_return;
12931 }
12932
12933 finish:
12934 if (relax_blank_list)
12935 {
12936 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12937 relax_blank_list = NULL;
12938 }
12939 return result;
12940
12941 error_return:
12942 result = FALSE;
12943 goto finish;
12944 }
12945
12946 /* This is a version of bfd_generic_get_relocated_section_contents.
12947 We need this variety because relaxation will modify the dwarf
12948 infomation. When there is undefined symbol reference error mesage,
12949 linker need to dump line number where the symbol be used. However
12950 the address is be relaxed, it can not get the original dwarf contents.
12951 The variety only modify function call for reading in the section. */
12952
12953 static bfd_byte *
12954 nds32_elf_get_relocated_section_contents (bfd *abfd,
12955 struct bfd_link_info *link_info,
12956 struct bfd_link_order *link_order,
12957 bfd_byte *data,
12958 bfd_boolean relocatable,
12959 asymbol **symbols)
12960 {
12961 bfd *input_bfd = link_order->u.indirect.section->owner;
12962 asection *input_section = link_order->u.indirect.section;
12963 long reloc_size;
12964 arelent **reloc_vector;
12965 long reloc_count;
12966
12967 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
12968 if (reloc_size < 0)
12969 return NULL;
12970
12971 /* Read in the section. */
12972 if (!nds32_get_section_contents (input_bfd, input_section, &data))
12973 return NULL;
12974
12975 if (reloc_size == 0)
12976 return data;
12977
12978 reloc_vector = (arelent **) bfd_malloc (reloc_size);
12979 if (reloc_vector == NULL)
12980 return NULL;
12981
12982 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
12983 reloc_vector, symbols);
12984 if (reloc_count < 0)
12985 goto error_return;
12986
12987 if (reloc_count > 0)
12988 {
12989 arelent **parent;
12990 for (parent = reloc_vector; *parent != NULL; parent++)
12991 {
12992 char *error_message = NULL;
12993 asymbol *symbol;
12994 bfd_reloc_status_type r;
12995
12996 symbol = *(*parent)->sym_ptr_ptr;
12997 if (symbol->section && discarded_section (symbol->section))
12998 {
12999 bfd_byte *p;
13000 static reloc_howto_type none_howto
13001 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13002 "unused", FALSE, 0, 0, FALSE);
13003
13004 p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
13005 _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
13006 p);
13007 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13008 (*parent)->addend = 0;
13009 (*parent)->howto = &none_howto;
13010 r = bfd_reloc_ok;
13011 }
13012 else
13013 r = bfd_perform_relocation (input_bfd, *parent, data,
13014 input_section,
13015 relocatable ? abfd : NULL,
13016 &error_message);
13017
13018 if (relocatable)
13019 {
13020 asection *os = input_section->output_section;
13021
13022 /* A partial link, so keep the relocs. */
13023 os->orelocation[os->reloc_count] = *parent;
13024 os->reloc_count++;
13025 }
13026
13027 if (r != bfd_reloc_ok)
13028 {
13029 switch (r)
13030 {
13031 case bfd_reloc_undefined:
13032 if (!((*link_info->callbacks->undefined_symbol)
13033 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13034 input_bfd, input_section, (*parent)->address, TRUE)))
13035 goto error_return;
13036 break;
13037 case bfd_reloc_dangerous:
13038 BFD_ASSERT (error_message != NULL);
13039 if (!((*link_info->callbacks->reloc_dangerous)
13040 (link_info, error_message, input_bfd, input_section,
13041 (*parent)->address)))
13042 goto error_return;
13043 break;
13044 case bfd_reloc_overflow:
13045 if (!((*link_info->callbacks->reloc_overflow)
13046 (link_info, NULL,
13047 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13048 (*parent)->howto->name, (*parent)->addend,
13049 input_bfd, input_section, (*parent)->address)))
13050 goto error_return;
13051 break;
13052 case bfd_reloc_outofrange:
13053 /* PR ld/13730:
13054 This error can result when processing some partially
13055 complete binaries. Do not abort, but issue an error
13056 message instead. */
13057 link_info->callbacks->einfo
13058 (_("%X%P: %B(%A): relocation \"%R\" goes out of range\n"),
13059 abfd, input_section, * parent);
13060 goto error_return;
13061
13062 default:
13063 abort ();
13064 break;
13065 }
13066 }
13067 }
13068 }
13069
13070 free (reloc_vector);
13071 return data;
13072
13073 error_return:
13074 free (reloc_vector);
13075 return NULL;
13076 }
13077 \f
13078 /* Link-time IFC relaxation.
13079 In this optimization, we chains jump instructions
13080 of the same destination with ifcall. */
13081
13082
13083 /* List to save jal and j relocation. */
13084 struct elf_nds32_ifc_symbol_entry
13085 {
13086 asection *sec;
13087 struct elf_link_hash_entry *h;
13088 struct elf_nds32_ifc_irel_list *irel_head;
13089 unsigned long insn;
13090 int times;
13091 int enable; /* Apply ifc. */
13092 int ex9_enable; /* Apply ifc after ex9. */
13093 struct elf_nds32_ifc_symbol_entry *next;
13094 };
13095
13096 struct elf_nds32_ifc_irel_list
13097 {
13098 Elf_Internal_Rela *irel;
13099 asection *sec;
13100 bfd_vma addr;
13101 /* If this is set, then it is the last instruction for
13102 ifc-chain, so it must be keep for the actual branching. */
13103 int keep;
13104 struct elf_nds32_ifc_irel_list *next;
13105 };
13106
13107 static struct elf_nds32_ifc_symbol_entry *ifc_symbol_head = NULL;
13108
13109 /* Insert symbol of jal and j for ifc. */
13110
13111 static void
13112 nds32_elf_ifc_insert_symbol (asection *sec,
13113 struct elf_link_hash_entry *h,
13114 Elf_Internal_Rela *irel,
13115 unsigned long insn)
13116 {
13117 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13118
13119 /* Check there is target of existing entry the same as the new one. */
13120 while (ptr != NULL)
13121 {
13122 if (((h == NULL && ptr->sec == sec
13123 && ELF32_R_SYM (ptr->irel_head->irel->r_info) == ELF32_R_SYM (irel->r_info)
13124 && ptr->irel_head->irel->r_addend == irel->r_addend)
13125 || h != NULL)
13126 && ptr->h == h
13127 && ptr->insn == insn)
13128 {
13129 /* The same target exist, so insert into list. */
13130 struct elf_nds32_ifc_irel_list *irel_list = ptr->irel_head;
13131
13132 while (irel_list->next != NULL)
13133 irel_list = irel_list->next;
13134 irel_list->next = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
13135 irel_list = irel_list->next;
13136 irel_list->irel = irel;
13137 irel_list->keep = 1;
13138
13139 if (h == NULL)
13140 irel_list->sec = NULL;
13141 else
13142 irel_list->sec = sec;
13143 irel_list->next = NULL;
13144 return;
13145 }
13146 if (ptr->next == NULL)
13147 break;
13148 ptr = ptr->next;
13149 }
13150
13151 /* There is no same target entry, so build a new one. */
13152 if (ifc_symbol_head == NULL)
13153 {
13154 ifc_symbol_head = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
13155 ptr = ifc_symbol_head;
13156 }
13157 else
13158 {
13159 ptr->next = bfd_malloc (sizeof (struct elf_nds32_ifc_symbol_entry));
13160 ptr = ptr->next;
13161 }
13162
13163 ptr->h = h;
13164 ptr->irel_head = bfd_malloc (sizeof (struct elf_nds32_ifc_irel_list));
13165 ptr->irel_head->irel = irel;
13166 ptr->insn = insn;
13167 ptr->irel_head->keep = 1;
13168
13169 if (h == NULL)
13170 {
13171 /* Local symbols. */
13172 ptr->sec = sec;
13173 ptr->irel_head->sec = NULL;
13174 }
13175 else
13176 {
13177 /* Global symbol. */
13178 ptr->sec = NULL;
13179 ptr->irel_head->sec = sec;
13180 }
13181
13182 ptr->irel_head->next = NULL;
13183 ptr->times = 0;
13184 ptr->enable = 0;
13185 ptr->ex9_enable = 0;
13186 ptr->next = NULL;
13187 }
13188
13189 /* Gather all jal and j instructions. */
13190
13191 static bfd_boolean
13192 nds32_elf_ifc_calc (struct bfd_link_info *info,
13193 bfd *abfd, asection *sec)
13194 {
13195 Elf_Internal_Rela *internal_relocs;
13196 Elf_Internal_Rela *irelend;
13197 Elf_Internal_Rela *irel;
13198 Elf_Internal_Shdr *symtab_hdr;
13199 bfd_byte *contents = NULL;
13200 uint32_t insn, insn_with_reg;
13201 unsigned long r_symndx;
13202 struct elf_link_hash_entry *h;
13203 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
13204 struct elf_nds32_link_hash_table *table;
13205 bfd_boolean ifc_loop_aware;
13206
13207 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
13208 TRUE /* keep_memory */);
13209 irelend = internal_relocs + sec->reloc_count;
13210 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13211
13212 /* Check if the object enable ifc. */
13213 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
13214 R_NDS32_RELAX_ENTRY);
13215
13216 if (irel == NULL
13217 || irel >= irelend
13218 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
13219 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
13220 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_IFC_FLAG)))
13221 return TRUE;
13222
13223 if (!nds32_get_section_contents (abfd, sec, &contents))
13224 return FALSE;
13225
13226 table = nds32_elf_hash_table (info);
13227 ifc_loop_aware = table->ifc_loop_aware;
13228 while (irel != NULL && irel < irelend)
13229 {
13230 /* Traverse all relocation and gather all of them to build the list. */
13231
13232 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN)
13233 {
13234 if (ifc_loop_aware == 1
13235 && (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0)
13236 {
13237 /* Check the region if loop or not. If it is true and
13238 ifc-loop-aware is true, ignore the region till region end. */
13239 while (irel != NULL
13240 && irel < irelend
13241 && (ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
13242 || (irel->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG) != 0))
13243 irel++;
13244 }
13245 }
13246
13247 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
13248 {
13249 insn = bfd_getb32 (contents + irel->r_offset);
13250 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
13251 r_symndx = ELF32_R_SYM (irel->r_info);
13252 if (r_symndx < symtab_hdr->sh_info)
13253 {
13254 /* Local symbol. */
13255 nds32_elf_ifc_insert_symbol (sec, NULL, irel, insn_with_reg);
13256 }
13257 else
13258 {
13259 /* External symbol. */
13260 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13261 nds32_elf_ifc_insert_symbol (sec, h, irel, insn_with_reg);
13262 }
13263 }
13264 irel++;
13265 }
13266 return TRUE;
13267 }
13268
13269 /* Determine whether j and jal should be substituted. */
13270
13271 static void
13272 nds32_elf_ifc_filter (struct bfd_link_info *info)
13273 {
13274 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13275 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13276 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13277 struct elf_nds32_link_hash_table *table;
13278 int target_optimize;
13279 bfd_vma address;
13280
13281 table = nds32_elf_hash_table (info);
13282 target_optimize = table->target_optimize;
13283 while (ptr)
13284 {
13285 irel_ptr = ptr->irel_head;
13286 if (ptr->h == NULL)
13287 {
13288 /* Local symbol. */
13289 irel_keeper = irel_ptr;
13290 while (irel_ptr && irel_ptr->next)
13291 {
13292 /* Check there is jump target can be used. */
13293 if ((irel_ptr->next->irel->r_offset
13294 - irel_keeper->irel->r_offset) > 1022)
13295 irel_keeper = irel_ptr->next;
13296 else
13297 {
13298 ptr->enable = 1;
13299 irel_ptr->keep = 0;
13300 }
13301 irel_ptr = irel_ptr->next;
13302 }
13303 }
13304 else
13305 {
13306 /* Global symbol. */
13307 /* We have to get the absolute address and decide
13308 whether to keep it or not. */
13309 while (irel_ptr)
13310 {
13311 address = (irel_ptr->irel->r_offset
13312 + irel_ptr->sec->output_section->vma
13313 + irel_ptr->sec->output_offset);
13314 irel_ptr->addr = address;
13315 irel_ptr = irel_ptr->next;
13316 }
13317
13318 irel_ptr = ptr->irel_head;
13319 while (irel_ptr)
13320 {
13321 /* Sort by address. */
13322 struct elf_nds32_ifc_irel_list *irel_dest = irel_ptr;
13323 struct elf_nds32_ifc_irel_list *irel_temp = irel_ptr;
13324 struct elf_nds32_ifc_irel_list *irel_ptr_prev = NULL;
13325 struct elf_nds32_ifc_irel_list *irel_dest_prev = NULL;
13326
13327 /* Get the smallest one. */
13328 while (irel_temp->next)
13329 {
13330 if (irel_temp->next->addr < irel_dest->addr)
13331 {
13332 irel_dest_prev = irel_temp;
13333 irel_dest = irel_temp->next;
13334 }
13335 irel_temp = irel_temp->next;
13336 }
13337
13338 if (irel_dest != irel_ptr)
13339 {
13340 if (irel_ptr_prev)
13341 irel_ptr_prev->next = irel_dest;
13342 if (irel_dest_prev)
13343 irel_dest_prev->next = irel_ptr;
13344 irel_temp = irel_ptr->next;
13345 irel_ptr->next = irel_dest->next;
13346 irel_dest->next = irel_temp;
13347 }
13348 irel_ptr_prev = irel_ptr;
13349 irel_ptr = irel_ptr->next;
13350 }
13351
13352 irel_ptr = ptr->irel_head;
13353 irel_keeper = irel_ptr;
13354 while (irel_ptr && irel_ptr->next)
13355 {
13356 if ((irel_ptr->next->addr - irel_keeper->addr) > 1022)
13357 irel_keeper = irel_ptr->next;
13358 else
13359 {
13360 ptr->enable = 1;
13361 irel_ptr->keep = 0;
13362 }
13363 irel_ptr = irel_ptr->next;
13364 }
13365 }
13366
13367 /* Ex9 enable. Reserve it for ex9. */
13368 if ((target_optimize & NDS32_RELAX_EX9_ON)
13369 && ptr->irel_head != irel_keeper)
13370 ptr->enable = 0;
13371 ptr = ptr->next;
13372 }
13373 }
13374
13375 /* Determine whether j and jal should be substituted after ex9 done. */
13376
13377 static void
13378 nds32_elf_ifc_filter_after_ex9 (void)
13379 {
13380 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13381 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13382
13383 while (ptr)
13384 {
13385 if (ptr->enable == 0)
13386 {
13387 /* Check whether ifc is applied or not. */
13388 irel_ptr = ptr->irel_head;
13389 ptr->ex9_enable = 1;
13390 while (irel_ptr)
13391 {
13392 if (ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_TRAN)
13393 {
13394 /* Ex9 already. */
13395 ptr->ex9_enable = 0;
13396 break;
13397 }
13398 irel_ptr = irel_ptr->next;
13399 }
13400 }
13401 ptr = ptr->next;
13402 }
13403 }
13404
13405 /* Wrapper to do ifc relaxation. */
13406
13407 bfd_boolean
13408 nds32_elf_ifc_finish (struct bfd_link_info *info)
13409 {
13410 int relax_status;
13411 struct elf_nds32_link_hash_table *table;
13412
13413 table = nds32_elf_hash_table (info);
13414 relax_status = table->relax_status;
13415
13416 if (!(relax_status & NDS32_RELAX_JUMP_IFC_DONE))
13417 nds32_elf_ifc_filter (info);
13418 else
13419 nds32_elf_ifc_filter_after_ex9 ();
13420
13421 if (!nds32_elf_ifc_replace (info))
13422 return FALSE;
13423
13424 if (table)
13425 table->relax_status |= NDS32_RELAX_JUMP_IFC_DONE;
13426 return TRUE;
13427 }
13428
13429 /* Traverse the result of ifc filter and replace it with ifcall9. */
13430
13431 static bfd_boolean
13432 nds32_elf_ifc_replace (struct bfd_link_info *info)
13433 {
13434 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13435 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13436 nds32_elf_blank_t *relax_blank_list = NULL;
13437 bfd_byte *contents = NULL;
13438 Elf_Internal_Rela *internal_relocs;
13439 Elf_Internal_Rela *irel;
13440 Elf_Internal_Rela *irelend;
13441 unsigned short insn16 = INSN_IFCALL9;
13442 struct elf_nds32_link_hash_table *table;
13443 int relax_status;
13444
13445 table = nds32_elf_hash_table (info);
13446 relax_status = table->relax_status;
13447
13448 while (ptr)
13449 {
13450 /* Traverse the ifc gather list, and replace the
13451 filter entries by ifcall9. */
13452 if ((!(relax_status & NDS32_RELAX_JUMP_IFC_DONE) && ptr->enable == 1)
13453 || ((relax_status & NDS32_RELAX_JUMP_IFC_DONE)
13454 && ptr->ex9_enable == 1))
13455 {
13456 irel_ptr = ptr->irel_head;
13457 if (ptr->h == NULL)
13458 {
13459 /* Local symbol. */
13460 internal_relocs = _bfd_elf_link_read_relocs
13461 (ptr->sec->owner, ptr->sec, NULL, NULL, TRUE /* keep_memory */);
13462 irelend = internal_relocs + ptr->sec->reloc_count;
13463
13464 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec,
13465 &contents))
13466 return FALSE;
13467
13468 while (irel_ptr)
13469 {
13470 if (irel_ptr->keep == 0 && irel_ptr->next)
13471 {
13472 /* The one can be replaced. We have to check whether
13473 there is any alignment point in the region. */
13474 irel = irel_ptr->irel;
13475 while (((irel_ptr->next->keep == 0
13476 && irel < irel_ptr->next->irel)
13477 || (irel_ptr->next->keep == 1 && irel < irelend))
13478 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13479 && (irel->r_addend & 0x1f) == 2))
13480 irel++;
13481 if (irel >= irelend
13482 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13483 && (irel->r_addend & 0x1f) == 2
13484 && ((irel->r_offset - get_nds32_elf_blank_total
13485 (&relax_blank_list, irel->r_offset, 1))
13486 & 0x02) == 0))
13487 {
13488 /* Replace by ifcall9. */
13489 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13490 if (!insert_nds32_elf_blank_recalc_total
13491 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13492 return FALSE;
13493 irel_ptr->irel->r_info =
13494 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13495 R_NDS32_10IFCU_PCREL_RELA);
13496 }
13497 }
13498 irel_ptr = irel_ptr->next;
13499 }
13500
13501 /* Delete the redundant code. */
13502 if (relax_blank_list)
13503 {
13504 nds32_elf_relax_delete_blanks (ptr->sec->owner, ptr->sec,
13505 relax_blank_list);
13506 relax_blank_list = NULL;
13507 }
13508 }
13509 else
13510 {
13511 /* Global symbol. */
13512 while (irel_ptr)
13513 {
13514 if (irel_ptr->keep == 0 && irel_ptr->next)
13515 {
13516 /* The one can be replaced, and we have to check
13517 whether there is any alignment point in the region. */
13518 internal_relocs = _bfd_elf_link_read_relocs
13519 (irel_ptr->sec->owner, irel_ptr->sec, NULL, NULL,
13520 TRUE /* keep_memory */);
13521 irelend = internal_relocs + irel_ptr->sec->reloc_count;
13522 if (!nds32_get_section_contents
13523 (irel_ptr->sec->owner, irel_ptr->sec, &contents))
13524 return FALSE;
13525
13526 irel = irel_ptr->irel;
13527 while (((irel_ptr->sec == irel_ptr->next->sec
13528 && irel_ptr->next->keep == 0
13529 && irel < irel_ptr->next->irel)
13530 || ((irel_ptr->sec != irel_ptr->next->sec
13531 || irel_ptr->next->keep == 1)
13532 && irel < irelend))
13533 && !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13534 && (irel->r_addend & 0x1f) == 2))
13535 irel++;
13536 if (irel >= irelend
13537 || !(ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
13538 && (irel->r_addend & 0x1f) == 2
13539 && ((irel->r_offset
13540 - get_nds32_elf_blank_total (&relax_blank_list,
13541 irel->r_offset, 1)) & 0x02) == 0))
13542 {
13543 /* Replace by ifcall9. */
13544 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13545 if (!insert_nds32_elf_blank_recalc_total
13546 (&relax_blank_list, irel_ptr->irel->r_offset + 2, 2))
13547 return FALSE;
13548
13549 /* Delete the redundant code, and clear the relocation. */
13550 nds32_elf_relax_delete_blanks (irel_ptr->sec->owner,
13551 irel_ptr->sec,
13552 relax_blank_list);
13553 irel_ptr->irel->r_info =
13554 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13555 R_NDS32_10IFCU_PCREL_RELA);
13556 relax_blank_list = NULL;
13557 }
13558 }
13559
13560 irel_ptr = irel_ptr->next;
13561 }
13562 }
13563 }
13564 ptr = ptr->next;
13565 }
13566
13567 return TRUE;
13568 }
13569
13570 /* Relocate ifcall. */
13571
13572 static bfd_boolean
13573 nds32_elf_ifc_reloc (void)
13574 {
13575 struct elf_nds32_ifc_symbol_entry *ptr = ifc_symbol_head;
13576 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
13577 struct elf_nds32_ifc_irel_list *irel_keeper = NULL;
13578 bfd_vma relocation, address;
13579 unsigned short insn16;
13580 bfd_byte *contents = NULL;
13581 static bfd_boolean done = FALSE;
13582
13583 if (done)
13584 return TRUE;
13585
13586 done = TRUE;
13587
13588 while (ptr)
13589 {
13590 /* Check the entry is enable ifcall. */
13591 if (ptr->enable == 1 || ptr->ex9_enable == 1)
13592 {
13593 /* Get the reserve jump. */
13594 irel_ptr = ptr->irel_head;
13595 while (irel_ptr)
13596 {
13597 if (irel_ptr->keep == 1)
13598 {
13599 irel_keeper = irel_ptr;
13600 break;
13601 }
13602 irel_ptr = irel_ptr->next;
13603 }
13604
13605 irel_ptr = ptr->irel_head;
13606 if (ptr->h == NULL)
13607 {
13608 /* Local symbol. */
13609 if (!nds32_get_section_contents (ptr->sec->owner, ptr->sec, &contents))
13610 return FALSE;
13611
13612 while (irel_ptr)
13613 {
13614 if (irel_ptr->keep == 0
13615 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13616 {
13617 relocation = irel_keeper->irel->r_offset;
13618 relocation = relocation - irel_ptr->irel->r_offset;
13619 while (irel_keeper && relocation > 1022)
13620 {
13621 irel_keeper = irel_keeper->next;
13622 if (irel_keeper && irel_keeper->keep == 1)
13623 {
13624 relocation = irel_keeper->irel->r_offset;
13625 relocation = relocation - irel_ptr->irel->r_offset;
13626 }
13627 }
13628 if (relocation > 1022)
13629 {
13630 /* Double check. */
13631 irel_keeper = ptr->irel_head;
13632 while (irel_keeper)
13633 {
13634 if (irel_keeper->keep == 1)
13635 {
13636 relocation = irel_keeper->irel->r_offset;
13637 relocation = relocation - irel_ptr->irel->r_offset;
13638 }
13639 if (relocation <= 1022)
13640 break;
13641 irel_keeper = irel_keeper->next;
13642 }
13643 if (!irel_keeper)
13644 return FALSE;
13645 }
13646 irel_ptr->irel->r_info =
13647 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13648 R_NDS32_NONE);
13649 insn16 = INSN_IFCALL9 | (relocation >> 1);
13650 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13651 }
13652 irel_ptr = irel_ptr->next;
13653 }
13654 }
13655 else
13656 {
13657 /* Global symbol. */
13658 while (irel_ptr)
13659 {
13660 if (irel_ptr->keep == 0
13661 && ELF32_R_TYPE (irel_ptr->irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13662 {
13663 /* Get the distance between ifcall and jump. */
13664 relocation = (irel_keeper->irel->r_offset
13665 + irel_keeper->sec->output_section->vma
13666 + irel_keeper->sec->output_offset);
13667 address = (irel_ptr->irel->r_offset
13668 + irel_ptr->sec->output_section->vma
13669 + irel_ptr->sec->output_offset);
13670 relocation = relocation - address;
13671
13672 /* The distance is over ragne, find callee again. */
13673 while (irel_keeper && relocation > 1022)
13674 {
13675 irel_keeper = irel_keeper->next;
13676 if (irel_keeper && irel_keeper->keep ==1)
13677 {
13678 relocation = (irel_keeper->irel->r_offset
13679 + irel_keeper->sec->output_section->vma
13680 + irel_keeper->sec->output_offset);
13681 relocation = relocation - address;
13682 }
13683 }
13684
13685 if (relocation > 1022)
13686 {
13687 /* Double check. */
13688 irel_keeper = ptr->irel_head;
13689 while (irel_keeper)
13690 {
13691 if (irel_keeper->keep == 1)
13692 {
13693
13694 relocation = (irel_keeper->irel->r_offset
13695 + irel_keeper->sec->output_section->vma
13696 + irel_keeper->sec->output_offset);
13697 relocation = relocation - address;
13698 }
13699 if (relocation <= 1022)
13700 break;
13701 irel_keeper = irel_keeper->next;
13702 }
13703 if (!irel_keeper)
13704 return FALSE;
13705 }
13706 if (!nds32_get_section_contents
13707 (irel_ptr->sec->owner, irel_ptr->sec, &contents))
13708 return FALSE;
13709 insn16 = INSN_IFCALL9 | (relocation >> 1);
13710 bfd_putb16 (insn16, contents + irel_ptr->irel->r_offset);
13711 irel_ptr->irel->r_info =
13712 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info),
13713 R_NDS32_NONE);
13714 }
13715 irel_ptr =irel_ptr->next;
13716 }
13717 }
13718 }
13719 ptr = ptr->next;
13720 }
13721
13722 return TRUE;
13723 }
13724
13725 /* End of IFC relaxation. */
13726 \f
13727 /* EX9 Instruction Table Relaxation. */
13728
13729 /* Global hash list. */
13730 struct elf_link_hash_entry_list
13731 {
13732 struct elf_link_hash_entry *h;
13733 struct elf_link_hash_entry_list *next;
13734 };
13735
13736 /* Save different destination but same insn. */
13737 struct elf_link_hash_entry_mul_list
13738 {
13739 /* Global symbol times. */
13740 int times;
13741 /* Save relocation for each global symbol but useful?? */
13742 Elf_Internal_Rela *irel;
13743 /* For sethi, two sethi may have the same high-part but different low-parts. */
13744 Elf_Internal_Rela rel_backup;
13745 struct elf_link_hash_entry_list *h_list;
13746 struct elf_link_hash_entry_mul_list *next;
13747 };
13748
13749 /* Instruction hash table. */
13750 struct elf_nds32_code_hash_entry
13751 {
13752 struct bfd_hash_entry root;
13753 int times;
13754 /* For insn that can use relocation or constant ex: sethi. */
13755 int const_insn;
13756 asection *sec;
13757 struct elf_link_hash_entry_mul_list *m_list;
13758 /* Using r_addend. */
13759 Elf_Internal_Rela *irel;
13760 /* Using r_info. */
13761 Elf_Internal_Rela rel_backup;
13762 };
13763
13764 /* Instruction count list. */
13765 struct elf_nds32_insn_times_entry
13766 {
13767 const char *string;
13768 int times;
13769 int order;
13770 asection *sec;
13771 struct elf_link_hash_entry_mul_list *m_list;
13772 Elf_Internal_Rela *irel;
13773 Elf_Internal_Rela rel_backup;
13774 struct elf_nds32_insn_times_entry *next;
13775 };
13776
13777 /* J and JAL symbol list. */
13778 struct elf_nds32_symbol_entry
13779 {
13780 char *string;
13781 unsigned long insn;
13782 struct elf_nds32_symbol_entry *next;
13783 };
13784
13785 /* Relocation list. */
13786 struct elf_nds32_irel_entry
13787 {
13788 Elf_Internal_Rela *irel;
13789 struct elf_nds32_irel_entry *next;
13790 };
13791
13792 /* ex9.it insn need to be fixed. */
13793 struct elf_nds32_ex9_refix
13794 {
13795 Elf_Internal_Rela *irel;
13796 asection *sec;
13797 struct elf_link_hash_entry *h;
13798 int order;
13799 struct elf_nds32_ex9_refix *next;
13800 };
13801
13802 static struct bfd_hash_table ex9_code_table;
13803 static struct elf_nds32_insn_times_entry *ex9_insn_head = NULL;
13804 static struct elf_nds32_ex9_refix *ex9_refix_head = NULL;
13805
13806 /* EX9 hash function. */
13807
13808 static struct bfd_hash_entry *
13809 nds32_elf_code_hash_newfunc (struct bfd_hash_entry *entry,
13810 struct bfd_hash_table *table,
13811 const char *string)
13812 {
13813 struct elf_nds32_code_hash_entry *ret;
13814
13815 /* Allocate the structure if it has not already been allocated by a
13816 subclass. */
13817 if (entry == NULL)
13818 {
13819 entry = (struct bfd_hash_entry *)
13820 bfd_hash_allocate (table, sizeof (*ret));
13821 if (entry == NULL)
13822 return entry;
13823 }
13824
13825 /* Call the allocation method of the superclass. */
13826 entry = bfd_hash_newfunc (entry, table, string);
13827 if (entry == NULL)
13828 return entry;
13829
13830 ret = (struct elf_nds32_code_hash_entry*) entry;
13831 ret->times = 0;
13832 ret->const_insn = 0;
13833 ret->m_list = NULL;
13834 ret->sec = NULL;
13835 ret->irel = NULL;
13836 return &ret->root;
13837 }
13838
13839 /* Insert ex9 entry
13840 this insert must be stable sorted by times. */
13841
13842 static void
13843 nds32_elf_ex9_insert_entry (struct elf_nds32_insn_times_entry *ptr)
13844 {
13845 struct elf_nds32_insn_times_entry *temp;
13846 struct elf_nds32_insn_times_entry *temp2;
13847
13848 if (ex9_insn_head == NULL)
13849 {
13850 ex9_insn_head = ptr;
13851 ptr->next = NULL;
13852 }
13853 else
13854 {
13855 temp = ex9_insn_head;
13856 temp2 = ex9_insn_head;
13857 while (temp->next &&
13858 (temp->next->times >= ptr->times
13859 || temp->times == -1))
13860 {
13861 if (temp->times == -1)
13862 temp2 = temp;
13863 temp = temp->next;
13864 }
13865 if (ptr->times > temp->times && temp->times != -1)
13866 {
13867 ptr->next = temp;
13868 if (temp2->times == -1)
13869 temp2->next = ptr;
13870 else
13871 ex9_insn_head = ptr;
13872 }
13873 else if (temp->next == NULL)
13874 {
13875 temp->next = ptr;
13876 ptr->next = NULL;
13877 }
13878 else
13879 {
13880 ptr->next = temp->next;
13881 temp->next = ptr;
13882 }
13883 }
13884 }
13885
13886 /* Examine each insn times in hash table.
13887 Handle multi-link hash entry.
13888
13889 TODO: This function doesn't assign so much info since it is fake. */
13890
13891 static int
13892 nds32_elf_examine_insn_times (struct elf_nds32_code_hash_entry *h)
13893 {
13894 struct elf_nds32_insn_times_entry *ptr;
13895 int times;
13896
13897 if (h->m_list == NULL)
13898 {
13899 /* Local symbol insn or insn without relocation. */
13900 if (h->times < 3)
13901 return TRUE;
13902
13903 ptr = (struct elf_nds32_insn_times_entry *)
13904 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13905 ptr->times = h->times;
13906 ptr->string = h->root.string;
13907 ptr->m_list = NULL;
13908 ptr->sec = h->sec;
13909 ptr->irel = h->irel;
13910 ptr->rel_backup = h->rel_backup;
13911 nds32_elf_ex9_insert_entry (ptr);
13912 }
13913 else
13914 {
13915 /* Global symbol insn. */
13916 /* Only sethi insn has multiple m_list. */
13917 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13918
13919 times = 0;
13920 while (m_list)
13921 {
13922 times += m_list->times;
13923 m_list = m_list->next;
13924 }
13925 if (times >= 3)
13926 {
13927 m_list = h->m_list;
13928 ptr = (struct elf_nds32_insn_times_entry *)
13929 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13930 ptr->times = times; /* Use the total times. */
13931 ptr->string = h->root.string;
13932 ptr->m_list = m_list;
13933 ptr->sec = h->sec;
13934 ptr->irel = m_list->irel;
13935 ptr->rel_backup = m_list->rel_backup;
13936 nds32_elf_ex9_insert_entry (ptr);
13937 }
13938 if (h->const_insn == 1)
13939 {
13940 /* sethi with constant value. */
13941 if (h->times < 3)
13942 return TRUE;
13943
13944 ptr = (struct elf_nds32_insn_times_entry *)
13945 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13946 ptr->times = h->times;
13947 ptr->string = h->root.string;
13948 ptr->m_list = NULL;
13949 ptr->sec = NULL;
13950 ptr->irel = NULL;
13951 ptr->rel_backup = h->rel_backup;
13952 nds32_elf_ex9_insert_entry (ptr);
13953 }
13954 }
13955 return TRUE;
13956 }
13957
13958 /* Count each insn times in hash table.
13959 Handle multi-link hash entry. */
13960
13961 static int
13962 nds32_elf_count_insn_times (struct elf_nds32_code_hash_entry *h)
13963 {
13964 int reservation, times;
13965 unsigned long relocation, min_relocation;
13966 struct elf_nds32_insn_times_entry *ptr;
13967
13968 if (h->m_list == NULL)
13969 {
13970 /* Local symbol insn or insn without relocation. */
13971 if (h->times < 3)
13972 return TRUE;
13973 ptr = (struct elf_nds32_insn_times_entry *)
13974 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
13975 ptr->times = h->times;
13976 ptr->string = h->root.string;
13977 ptr->m_list = NULL;
13978 ptr->sec = h->sec;
13979 ptr->irel = h->irel;
13980 ptr->rel_backup = h->rel_backup;
13981 nds32_elf_ex9_insert_entry (ptr);
13982 }
13983 else
13984 {
13985 /* Global symbol insn. */
13986 /* Only sethi insn has multiple m_list. */
13987 struct elf_link_hash_entry_mul_list *m_list = h->m_list;
13988
13989 if (ELF32_R_TYPE (m_list->rel_backup.r_info) == R_NDS32_HI20_RELA
13990 && m_list->next != NULL)
13991 {
13992 /* Sethi insn has different symbol or addend but has same hi20. */
13993 times = 0;
13994 reservation = 1;
13995 relocation = 0;
13996 min_relocation = 0xffffffff;
13997 while (m_list)
13998 {
13999 /* Get the minimum sethi address
14000 and calculate how many entry the sethi-list have to use. */
14001 if ((m_list->h_list->h->root.type == bfd_link_hash_defined
14002 || m_list->h_list->h->root.type == bfd_link_hash_defweak)
14003 && (m_list->h_list->h->root.u.def.section != NULL
14004 && m_list->h_list->h->root.u.def.section->output_section != NULL))
14005 {
14006 relocation = (m_list->h_list->h->root.u.def.value +
14007 m_list->h_list->h->root.u.def.section->output_section->vma +
14008 m_list->h_list->h->root.u.def.section->output_offset);
14009 relocation += m_list->irel->r_addend;
14010 }
14011 else
14012 relocation = 0;
14013 if (relocation < min_relocation)
14014 min_relocation = relocation;
14015 times += m_list->times;
14016 m_list = m_list->next;
14017 }
14018 if (min_relocation < ex9_relax_size)
14019 reservation = (min_relocation >> 12) + 1;
14020 else
14021 reservation = (min_relocation >> 12)
14022 - ((min_relocation - ex9_relax_size) >> 12) + 1;
14023 if (reservation < (times / 3))
14024 {
14025 /* Efficient enough to use ex9. */
14026 int i;
14027
14028 for (i = reservation ; i > 0; i--)
14029 {
14030 /* Allocate number of reservation ex9 entry. */
14031 ptr = (struct elf_nds32_insn_times_entry *)
14032 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14033 ptr->times = h->m_list->times / reservation;
14034 ptr->string = h->root.string;
14035 ptr->m_list = h->m_list;
14036 ptr->sec = h->sec;
14037 ptr->irel = h->m_list->irel;
14038 ptr->rel_backup = h->m_list->rel_backup;
14039 nds32_elf_ex9_insert_entry (ptr);
14040 }
14041 }
14042 }
14043 else
14044 {
14045 /* Normal global symbol that means no different address symbol
14046 using same ex9 entry. */
14047 if (m_list->times >= 3)
14048 {
14049 ptr = (struct elf_nds32_insn_times_entry *)
14050 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14051 ptr->times = m_list->times;
14052 ptr->string = h->root.string;
14053 ptr->m_list = h->m_list;
14054 ptr->sec = h->sec;
14055 ptr->irel = h->m_list->irel;
14056 ptr->rel_backup = h->m_list->rel_backup;
14057 nds32_elf_ex9_insert_entry (ptr);
14058 }
14059 }
14060
14061 if (h->const_insn == 1)
14062 {
14063 /* sethi with constant value. */
14064 if (h->times < 3)
14065 return TRUE;
14066
14067 ptr = (struct elf_nds32_insn_times_entry *)
14068 bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
14069 ptr->times = h->times;
14070 ptr->string = h->root.string;
14071 ptr->m_list = NULL;
14072 ptr->sec = NULL;
14073 ptr->irel = NULL;
14074 ptr->rel_backup = h->rel_backup;
14075 nds32_elf_ex9_insert_entry (ptr);
14076 }
14077 }
14078
14079 return TRUE;
14080 }
14081
14082 /* Hash table traverse function. */
14083
14084 static void
14085 nds32_elf_code_hash_traverse (int (*func) (struct elf_nds32_code_hash_entry*))
14086 {
14087 unsigned int i;
14088
14089 ex9_code_table.frozen = 1;
14090 for (i = 0; i < ex9_code_table.size; i++)
14091 {
14092 struct bfd_hash_entry *p;
14093
14094 for (p = ex9_code_table.table[i]; p != NULL; p = p->next)
14095 if (!func ((struct elf_nds32_code_hash_entry *) p))
14096 goto out;
14097 }
14098 out:
14099 ex9_code_table.frozen = 0;
14100 }
14101
14102
14103 /* Give order number to insn list. */
14104
14105 static void
14106 nds32_elf_order_insn_times (struct bfd_link_info *info)
14107 {
14108 struct elf_nds32_insn_times_entry *ex9_insn;
14109 struct elf_nds32_insn_times_entry *temp = NULL;
14110 struct elf_nds32_link_hash_table *table;
14111 int ex9_limit;
14112 int number = 0;
14113
14114 if (ex9_insn_head == NULL)
14115 return;
14116
14117 /* The max number of entries is 512. */
14118 ex9_insn = ex9_insn_head;
14119 table = nds32_elf_hash_table (info);
14120 ex9_limit = table->ex9_limit;
14121
14122 ex9_insn = ex9_insn_head;
14123
14124 while (ex9_insn != NULL && number < ex9_limit)
14125 {
14126 ex9_insn->order = number;
14127 number++;
14128 temp = ex9_insn;
14129 ex9_insn = ex9_insn->next;
14130 }
14131
14132 if (ex9_insn && temp)
14133 temp->next = NULL;
14134
14135 while (ex9_insn != NULL)
14136 {
14137 /* Free useless entry. */
14138 temp = ex9_insn;
14139 ex9_insn = ex9_insn->next;
14140 free (temp);
14141 }
14142 }
14143
14144 /* Build .ex9.itable section. */
14145
14146 static void
14147 nds32_elf_ex9_build_itable (struct bfd_link_info *link_info)
14148 {
14149 asection *table_sec;
14150 struct elf_nds32_insn_times_entry *ptr;
14151 bfd *it_abfd;
14152 int number = 0;
14153 bfd_byte *contents = NULL;
14154
14155 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
14156 it_abfd = it_abfd->link.next)
14157 {
14158 /* Find the section .ex9.itable, and put all entries into it. */
14159 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
14160 if (table_sec != NULL)
14161 {
14162 if (!nds32_get_section_contents (it_abfd, table_sec, &contents))
14163 return;
14164
14165 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
14166 number++;
14167
14168 table_sec->size = number * 4;
14169
14170 if (number == 0)
14171 return;
14172
14173 elf_elfheader (link_info->output_bfd)->e_flags |= E_NDS32_HAS_EX9_INST;
14174 number = 0;
14175 for (ptr = ex9_insn_head; ptr !=NULL ; ptr = ptr->next)
14176 {
14177 long val;
14178
14179 val = strtol (ptr->string, NULL, 16);
14180 bfd_putb32 ((bfd_vma) val, (char *) contents + (number * 4));
14181 number++;
14182 }
14183 break;
14184 }
14185 }
14186 }
14187
14188 /* Get insn with regs according to relocation type. */
14189
14190 static void
14191 nds32_elf_get_insn_with_reg (Elf_Internal_Rela *irel,
14192 uint32_t insn, uint32_t *insn_with_reg)
14193 {
14194 reloc_howto_type *howto = NULL;
14195
14196 if (irel == NULL
14197 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
14198 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
14199 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
14200 {
14201 *insn_with_reg = insn;
14202 return;
14203 }
14204
14205 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14206 *insn_with_reg = insn & (0xffffffff ^ howto->dst_mask);
14207 }
14208
14209 /* Mask number of address bits according to relocation. */
14210
14211 static unsigned long
14212 nds32_elf_irel_mask (Elf_Internal_Rela *irel)
14213 {
14214 reloc_howto_type *howto = NULL;
14215
14216 if (irel == NULL
14217 || (ELF32_R_TYPE (irel->r_info) >= (int) ARRAY_SIZE (nds32_elf_howto_table)
14218 && (ELF32_R_TYPE (irel->r_info) - R_NDS32_RELAX_ENTRY)
14219 >= (int) ARRAY_SIZE (nds32_elf_relax_howto_table)))
14220 return 0;
14221
14222 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
14223 return howto->dst_mask;
14224 }
14225
14226 static void
14227 nds32_elf_insert_irel_entry (struct elf_nds32_irel_entry **irel_list,
14228 struct elf_nds32_irel_entry *irel_ptr)
14229 {
14230 if (*irel_list == NULL)
14231 {
14232 *irel_list = irel_ptr;
14233 irel_ptr->next = NULL;
14234 }
14235 else
14236 {
14237 irel_ptr->next = *irel_list;
14238 *irel_list = irel_ptr;
14239 }
14240 }
14241
14242 static void
14243 nds32_elf_ex9_insert_fix (asection * sec, Elf_Internal_Rela * irel,
14244 struct elf_link_hash_entry *h, int order)
14245 {
14246 struct elf_nds32_ex9_refix *ptr;
14247
14248 ptr = bfd_malloc (sizeof (struct elf_nds32_ex9_refix));
14249 ptr->sec = sec;
14250 ptr->irel = irel;
14251 ptr->h = h;
14252 ptr->order = order;
14253 ptr->next = NULL;
14254
14255 if (ex9_refix_head == NULL)
14256 ex9_refix_head = ptr;
14257 else
14258 {
14259 struct elf_nds32_ex9_refix *temp = ex9_refix_head;
14260
14261 while (temp->next != NULL)
14262 temp = temp->next;
14263 temp->next = ptr;
14264 }
14265 }
14266
14267 enum
14268 {
14269 DATA_EXIST = 1,
14270 CLEAN_PRE = 1 << 1,
14271 PUSH_PRE = 1 << 2
14272 };
14273
14274 /* Check relocation type if supporting for ex9. */
14275
14276 static int
14277 nds32_elf_ex9_relocation_check (struct bfd_link_info *info,
14278 Elf_Internal_Rela **irel,
14279 Elf_Internal_Rela *irelend,
14280 nds32_elf_blank_t *relax_blank_list,
14281 asection *sec,bfd_vma *off,
14282 bfd_byte *contents)
14283 {
14284 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
14285 bfd_boolean nested_ex9, nested_loop;
14286 bfd_boolean ex9_loop_aware;
14287 /* We use the highest 1 byte of result to record
14288 how many bytes location counter has to move. */
14289 int result = 0;
14290 Elf_Internal_Rela *irel_save = NULL;
14291 struct elf_nds32_link_hash_table *table;
14292
14293 table = nds32_elf_hash_table (info);
14294 ex9_loop_aware = table->ex9_loop_aware;
14295
14296 while ((*irel) != NULL && (*irel) < irelend && *off == (*irel)->r_offset)
14297 {
14298 switch (ELF32_R_TYPE ((*irel)->r_info))
14299 {
14300 case R_NDS32_RELAX_REGION_BEGIN:
14301 /* Ignore code block. */
14302 nested_ex9 = FALSE;
14303 nested_loop = FALSE;
14304 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG)
14305 || (ex9_loop_aware
14306 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG)))
14307 {
14308 /* Check the region if loop or not. If it is true and
14309 ex9-loop-aware is true, ignore the region till region end. */
14310 /* To save the status for in .no_relax ex9 region and
14311 loop region to conform the block can do ex9 relaxation. */
14312 nested_ex9 = ((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG);
14313 nested_loop = (ex9_loop_aware
14314 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG));
14315 while ((*irel) && (*irel) < irelend && (nested_ex9 || nested_loop))
14316 {
14317 (*irel)++;
14318 if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_BEGIN)
14319 {
14320 /* There may be nested region. */
14321 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14322 nested_ex9 = TRUE;
14323 else if (ex9_loop_aware
14324 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14325 nested_loop = TRUE;
14326 }
14327 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_RELAX_REGION_END)
14328 {
14329 /* The end of region. */
14330 if (((*irel)->r_addend & R_NDS32_RELAX_REGION_NO_EX9_FLAG) != 0)
14331 nested_ex9 = FALSE;
14332 else if (ex9_loop_aware
14333 && ((*irel)->r_addend & R_NDS32_RELAX_REGION_INNERMOST_LOOP_FLAG))
14334 nested_loop = FALSE;
14335 }
14336 else if (ELF32_R_TYPE ((*irel)->r_info) == R_NDS32_LABEL
14337 && ((*irel)->r_addend & 0x1f) == 2)
14338 {
14339 /* Alignment exist in the region. */
14340 result |= CLEAN_PRE;
14341 if (((*irel)->r_offset -
14342 get_nds32_elf_blank_total (&relax_blank_list,
14343 (*irel)->r_offset, 0)) & 0x02)
14344 result |= PUSH_PRE;
14345 }
14346 }
14347 if ((*irel) >= irelend)
14348 *off = sec->size;
14349 else
14350 *off = (*irel)->r_offset;
14351
14352 /* The final instruction in the region, regard this one as data to ignore it. */
14353 result |= DATA_EXIST;
14354 return result;
14355 }
14356 break;
14357
14358 case R_NDS32_LABEL:
14359 if (((*irel)->r_addend & 0x1f) == 2)
14360 {
14361 /* Check this point is align and decide to do ex9 or not. */
14362 result |= CLEAN_PRE;
14363 if (((*irel)->r_offset -
14364 get_nds32_elf_blank_total (&relax_blank_list,
14365 (*irel)->r_offset, 0)) & 0x02)
14366 result |= PUSH_PRE;
14367 }
14368 break;
14369 case R_NDS32_32_RELA:
14370 /* Data. */
14371 result |= (4 << 24);
14372 result |= DATA_EXIST;
14373 break;
14374 case R_NDS32_16_RELA:
14375 /* Data. */
14376 result |= (2 << 24);
14377 result |= DATA_EXIST;
14378 break;
14379 case R_NDS32_DATA:
14380 /* Data. */
14381 /* The least code alignment is 2. If the data is only one byte,
14382 we have to shift one more byte. */
14383 if ((*irel)->r_addend == 1)
14384 result |= ((*irel)->r_addend << 25) ;
14385 else
14386 result |= ((*irel)->r_addend << 24) ;
14387
14388 result |= DATA_EXIST;
14389 break;
14390
14391 case R_NDS32_25_PCREL_RELA:
14392 case R_NDS32_SDA16S3_RELA:
14393 case R_NDS32_SDA15S3_RELA:
14394 case R_NDS32_SDA15S3:
14395 case R_NDS32_SDA17S2_RELA:
14396 case R_NDS32_SDA15S2_RELA:
14397 case R_NDS32_SDA12S2_SP_RELA:
14398 case R_NDS32_SDA12S2_DP_RELA:
14399 case R_NDS32_SDA15S2:
14400 case R_NDS32_SDA18S1_RELA:
14401 case R_NDS32_SDA15S1_RELA:
14402 case R_NDS32_SDA15S1:
14403 case R_NDS32_SDA19S0_RELA:
14404 case R_NDS32_SDA15S0_RELA:
14405 case R_NDS32_SDA15S0:
14406 case R_NDS32_HI20_RELA:
14407 case R_NDS32_LO12S0_ORI_RELA:
14408 case R_NDS32_LO12S0_RELA:
14409 case R_NDS32_LO12S1_RELA:
14410 case R_NDS32_LO12S2_RELA:
14411 /* These relocation is supported ex9 relaxation currently. */
14412 /* We have to save the relocation for using later, since we have
14413 to check there is any alignment in the same address. */
14414 irel_save = *irel;
14415 break;
14416 default:
14417 /* Not support relocations. */
14418 if (ELF32_R_TYPE ((*irel)->r_info) < ARRAY_SIZE (nds32_elf_howto_table)
14419 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_NONE
14420 && ELF32_R_TYPE ((*irel)->r_info) != R_NDS32_INSN16)
14421 {
14422 /* Note: To optimize aggressively, it maybe can ignore R_NDS32_INSN16 here.
14423 But we have to consider if there is any side-effect. */
14424 if (!(result & DATA_EXIST))
14425 {
14426 /* We have to confirm there is no data relocation in the
14427 same address. In general case, this won't happen. */
14428 /* We have to do ex9 conservative, for those relocation not
14429 considerd we ignore instruction. */
14430 result |= DATA_EXIST;
14431 if (*(contents + *off) & 0x80)
14432 result |= (2 << 24);
14433 else
14434 result |= (4 << 24);
14435 break;
14436 }
14437 }
14438 }
14439 if ((*irel) < irelend
14440 && ((*irel) + 1) < irelend
14441 && (*irel)->r_offset == ((*irel) + 1)->r_offset)
14442 /* There are relocations pointing to the same address, we have to
14443 check all of them. */
14444 (*irel)++;
14445 else
14446 {
14447 if (irel_save)
14448 *irel = irel_save;
14449 return result;
14450 }
14451 }
14452 return result;
14453 }
14454
14455 /* Replace with ex9 instruction. */
14456
14457 static bfd_boolean
14458 nds32_elf_ex9_push_insn (uint16_t insn16, bfd_byte *contents, bfd_vma pre_off,
14459 nds32_elf_blank_t **relax_blank_list,
14460 struct elf_nds32_irel_entry *pre_irel_ptr,
14461 struct elf_nds32_irel_entry **irel_list)
14462 {
14463 if (insn16 != 0)
14464 {
14465 /* Implement the ex9 relaxation. */
14466 bfd_putb16 (insn16, contents + pre_off);
14467 if (!insert_nds32_elf_blank_recalc_total (relax_blank_list,
14468 pre_off + 2, 2))
14469 return FALSE;
14470 if (pre_irel_ptr != NULL)
14471 nds32_elf_insert_irel_entry (irel_list, pre_irel_ptr);
14472 }
14473 return TRUE;
14474 }
14475
14476 /* Replace input file instruction which is in ex9 itable. */
14477
14478 static bfd_boolean
14479 nds32_elf_ex9_replace_instruction (struct bfd_link_info *info, bfd *abfd, asection *sec)
14480 {
14481 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
14482 bfd_byte *contents = NULL;
14483 bfd_vma off;
14484 uint16_t insn16, insn_ex9;
14485 /* `pre_*' are used to track previous instruction that can use ex9.it. */
14486 bfd_vma pre_off = -1;
14487 uint16_t pre_insn16 = 0;
14488 struct elf_nds32_irel_entry *pre_irel_ptr = NULL;
14489 Elf_Internal_Rela *internal_relocs;
14490 Elf_Internal_Rela *irel;
14491 Elf_Internal_Rela *irelend;
14492 Elf_Internal_Shdr *symtab_hdr;
14493 Elf_Internal_Sym *isym = NULL;
14494 nds32_elf_blank_t *relax_blank_list = NULL;
14495 uint32_t insn = 0;
14496 uint32_t insn_with_reg = 0;
14497 uint32_t it_insn;
14498 uint32_t it_insn_with_reg;
14499 unsigned long r_symndx;
14500 asection *isec;
14501 struct elf_nds32_irel_entry *irel_list = NULL;
14502 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (abfd);
14503 int data_flag, do_replace, save_irel;
14504 struct elf_link_hash_entry_list *h_list;
14505
14506
14507 /* Load section instructions, relocations, and symbol table. */
14508 if (!nds32_get_section_contents (abfd, sec, &contents)
14509 || !nds32_get_local_syms (abfd, sec, &isym))
14510 return FALSE;
14511 internal_relocs =
14512 _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, TRUE /* keep_memory */);
14513 irelend = internal_relocs + sec->reloc_count;
14514 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
14515
14516 off = 0;
14517
14518 /* Check if the object enable ex9. */
14519 irel = find_relocs_at_address (internal_relocs, internal_relocs,
14520 irelend, R_NDS32_RELAX_ENTRY);
14521
14522 /* Check this section trigger ex9 relaxation. */
14523 if (irel == NULL
14524 || irel >= irelend
14525 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
14526 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
14527 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
14528 return TRUE;
14529
14530 irel = internal_relocs;
14531
14532 /* Check alignment and fetch proper relocation. */
14533 while (off < sec->size)
14534 {
14535 struct elf_link_hash_entry *h = NULL;
14536 struct elf_nds32_irel_entry *irel_ptr = NULL;
14537
14538 /* Syn the instruction and the relocation. */
14539 while (irel != NULL && irel < irelend && irel->r_offset < off)
14540 irel++;
14541
14542 data_flag = nds32_elf_ex9_relocation_check (info, &irel, irelend,
14543 relax_blank_list, sec,
14544 &off, contents);
14545 if (data_flag & PUSH_PRE)
14546 if (!nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14547 &relax_blank_list, pre_irel_ptr,
14548 &irel_list))
14549 return FALSE;
14550
14551 if (data_flag & CLEAN_PRE)
14552 {
14553 pre_off = 0;
14554 pre_insn16 = 0;
14555 pre_irel_ptr = NULL;
14556 }
14557 if (data_flag & DATA_EXIST)
14558 {
14559 /* We save the move offset in the highest byte. */
14560 off += (data_flag >> 24);
14561 continue;
14562 }
14563
14564 if (*(contents + off) & 0x80)
14565 {
14566 /* 2-byte instruction. */
14567 off += 2;
14568 continue;
14569 }
14570
14571 /* Load the instruction and its opcode with register for comparing. */
14572 ex9_insn = ex9_insn_head;
14573 insn = bfd_getb32 (contents + off);
14574 insn_with_reg = 0;
14575 while (ex9_insn)
14576 {
14577 it_insn = strtol (ex9_insn->string, NULL, 16);
14578 it_insn_with_reg = 0;
14579 do_replace = 0;
14580 save_irel = 0;
14581
14582 if (irel != NULL && irel < irelend && irel->r_offset == off)
14583 {
14584 /* Insn with relocation. */
14585 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
14586
14587 if (ex9_insn->irel != NULL)
14588 nds32_elf_get_insn_with_reg (ex9_insn->irel, it_insn,
14589 &it_insn_with_reg);
14590
14591 if (ex9_insn->irel != NULL
14592 && (ELF32_R_TYPE (irel->r_info) ==
14593 ELF32_R_TYPE (ex9_insn->irel->r_info))
14594 && (insn_with_reg == it_insn_with_reg))
14595 {
14596 /* Insn relocation and format is the same as table entry. */
14597
14598 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
14599 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
14600 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
14601 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
14602 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
14603 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
14604 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
14605 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
14606 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
14607 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
14608 && ELF32_R_TYPE (irel->r_info) <=
14609 R_NDS32_SDA12S2_SP_RELA)
14610 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
14611 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
14612 {
14613 r_symndx = ELF32_R_SYM (irel->r_info);
14614 if (r_symndx < symtab_hdr->sh_info)
14615 {
14616 /* Local symbol. */
14617 int shndx = isym[r_symndx].st_shndx;
14618
14619 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14620 if (ex9_insn->sec == isec
14621 && ex9_insn->irel->r_addend == irel->r_addend
14622 && ex9_insn->irel->r_info == irel->r_info)
14623 {
14624 do_replace = 1;
14625 save_irel = 1;
14626 }
14627 }
14628 else
14629 {
14630 /* External symbol. */
14631 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14632 if (ex9_insn->m_list)
14633 {
14634 h_list = ex9_insn->m_list->h_list;
14635 while (h_list)
14636 {
14637 if (h == h_list->h
14638 && (ex9_insn->m_list->irel->r_addend ==
14639 irel->r_addend))
14640 {
14641 do_replace = 1;
14642 save_irel = 1;
14643 break;
14644 }
14645 h_list = h_list->next;
14646 }
14647 }
14648 }
14649 }
14650 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA)
14651 {
14652 r_symndx = ELF32_R_SYM (irel->r_info);
14653 if (r_symndx < symtab_hdr->sh_info)
14654 {
14655 /* Local symbols. Compare its base symbol and offset. */
14656 int shndx = isym[r_symndx].st_shndx;
14657
14658 isec = elf_elfsections (abfd)[shndx]->bfd_section;
14659 if (ex9_insn->sec == isec
14660 && ex9_insn->irel->r_addend == irel->r_addend
14661 && ex9_insn->irel->r_info == irel->r_info)
14662 {
14663 do_replace = 1;
14664 save_irel = 1;
14665 }
14666 }
14667 else
14668 {
14669 /* External symbol. */
14670 struct elf_link_hash_entry_mul_list *m_list;
14671
14672 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14673 m_list = ex9_insn->m_list;
14674
14675 while (m_list)
14676 {
14677 h_list = m_list->h_list;
14678
14679 while (h_list)
14680 {
14681 if (h == h_list->h
14682 && (m_list->irel->r_addend
14683 == irel->r_addend))
14684 {
14685 do_replace = 1;
14686 save_irel = 1;
14687 if (ex9_insn->next
14688 && ex9_insn->m_list
14689 && ex9_insn->m_list == ex9_insn->next->m_list)
14690 {
14691 /* sethi multiple entry must be fixed */
14692 nds32_elf_ex9_insert_fix (sec, irel,
14693 h, ex9_insn->order);
14694 }
14695 break;
14696 }
14697 h_list = h_list->next;
14698 }
14699 m_list = m_list->next;
14700 }
14701 }
14702 }
14703 }
14704
14705 /* Import table: Check the symbol hash table and the
14706 jump target. Only R_NDS32_25_PCREL_RELA now. */
14707 else if (ex9_insn->times == -1
14708 && ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA)
14709 {
14710 nds32_elf_get_insn_with_reg (irel, it_insn, &it_insn_with_reg);
14711 if (insn_with_reg == it_insn_with_reg)
14712 {
14713 char code[10];
14714 bfd_vma relocation;
14715
14716 r_symndx = ELF32_R_SYM (irel->r_info);
14717 if (r_symndx >= symtab_hdr->sh_info)
14718 {
14719 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
14720 if ((h->root.type == bfd_link_hash_defined
14721 || h->root.type == bfd_link_hash_defweak)
14722 && h->root.u.def.section != NULL
14723 && h->root.u.def.section->output_section != NULL
14724 && h->root.u.def.section->gc_mark == 1
14725 && bfd_is_abs_section (h->root.u.def.section)
14726 && h->root.u.def.value > sec->size)
14727 {
14728 relocation = h->root.u.def.value +
14729 h->root.u.def.section->output_section->vma +
14730 h->root.u.def.section->output_offset;
14731 relocation += irel->r_addend;
14732 insn = insn_with_reg
14733 | ((relocation >> 1) & 0xffffff);
14734 snprintf (code, sizeof (code), "%08x", insn);
14735 if (strcmp (code, ex9_insn->string) == 0)
14736 {
14737 do_replace = 1;
14738 save_irel = 1;
14739 }
14740 }
14741 }
14742 }
14743 }
14744 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
14745 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
14746 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
14747 {
14748 /* These relocations do not have to relocate contens, so it can
14749 be regard as instruction without relocation. */
14750 if (insn == it_insn && ex9_insn->irel == NULL)
14751 do_replace = 1;
14752 }
14753 }
14754 else
14755 {
14756 /* Instruction without relocation, we only
14757 have to compare their byte code. */
14758 if (insn == it_insn && ex9_insn->irel == NULL)
14759 do_replace = 1;
14760 }
14761
14762 /* Insntruction match so replacing the code here. */
14763 if (do_replace == 1)
14764 {
14765 /* There are two formats of ex9 instruction. */
14766 if (ex9_insn->order < 32)
14767 insn_ex9 = INSN_EX9_IT_2;
14768 else
14769 insn_ex9 = INSN_EX9_IT_1;
14770 insn16 = insn_ex9 | ex9_insn->order;
14771
14772 /* Insert ex9 instruction. */
14773 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14774 &relax_blank_list, pre_irel_ptr,
14775 &irel_list);
14776 pre_off = off;
14777 pre_insn16 = insn16;
14778
14779 if (save_irel)
14780 {
14781 /* For instuction with relocation do relax. */
14782 irel_ptr = (struct elf_nds32_irel_entry *)
14783 bfd_malloc (sizeof (struct elf_nds32_irel_entry));
14784 irel_ptr->irel = irel;
14785 irel_ptr->next = NULL;
14786 pre_irel_ptr = irel_ptr;
14787 }
14788 else
14789 pre_irel_ptr = NULL;
14790 break;
14791 }
14792 ex9_insn = ex9_insn->next;
14793 }
14794 off += 4;
14795 }
14796
14797 /* Insert ex9 instruction. */
14798 nds32_elf_ex9_push_insn (pre_insn16, contents, pre_off,
14799 &relax_blank_list, pre_irel_ptr,
14800 &irel_list);
14801
14802 /* Delete the redundant code. */
14803 if (relax_blank_list)
14804 {
14805 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
14806 relax_blank_list = NULL;
14807 }
14808
14809 /* Clear the relocation that is replaced by ex9. */
14810 while (irel_list)
14811 {
14812 struct elf_nds32_irel_entry *irel_ptr;
14813
14814 irel_ptr = irel_list;
14815 irel_list = irel_ptr->next;
14816 irel_ptr->irel->r_info =
14817 ELF32_R_INFO (ELF32_R_SYM (irel_ptr->irel->r_info), R_NDS32_TRAN);
14818 free (irel_ptr);
14819 }
14820 return TRUE;
14821 }
14822
14823 /* Initialize ex9 hash table. */
14824
14825 int
14826 nds32_elf_ex9_init (void)
14827 {
14828 if (!bfd_hash_table_init_n (&ex9_code_table, nds32_elf_code_hash_newfunc,
14829 sizeof (struct elf_nds32_code_hash_entry),
14830 1023))
14831 {
14832 (*_bfd_error_handler) (_("Linker: cannot init ex9 hash table error \n"));
14833 return FALSE;
14834 }
14835 return TRUE;
14836 }
14837
14838 /* Predict how many bytes will be relaxed with ex9 and ifc. */
14839
14840 static void
14841 nds32_elf_ex9_total_relax (struct bfd_link_info *info)
14842 {
14843 struct elf_nds32_insn_times_entry *ex9_insn;
14844 struct elf_nds32_insn_times_entry *temp;
14845 int target_optimize;
14846 struct elf_nds32_link_hash_table *table;
14847
14848 if (ex9_insn_head == NULL)
14849 return;
14850
14851 table = nds32_elf_hash_table (info);
14852 target_optimize = table->target_optimize;
14853 ex9_insn = ex9_insn_head;
14854 while (ex9_insn)
14855 {
14856 ex9_relax_size = ex9_insn->times * 2 + ex9_relax_size;
14857 temp = ex9_insn;
14858 ex9_insn = ex9_insn->next;
14859 free (temp);
14860 }
14861 ex9_insn_head = NULL;
14862
14863 if ((target_optimize & NDS32_RELAX_JUMP_IFC_ON))
14864 {
14865 /* Examine ifc reduce size. */
14866 struct elf_nds32_ifc_symbol_entry *ifc_ent = ifc_symbol_head;
14867 struct elf_nds32_ifc_irel_list *irel_ptr = NULL;
14868 int size = 0;
14869
14870 while (ifc_ent)
14871 {
14872 if (ifc_ent->enable == 0)
14873 {
14874 /* Not ifc yet. */
14875 irel_ptr = ifc_ent->irel_head;
14876 while (irel_ptr)
14877 {
14878 size += 2;
14879 irel_ptr = irel_ptr->next;
14880 }
14881 }
14882 size -= 2;
14883 ifc_ent = ifc_ent->next;
14884 }
14885 ex9_relax_size += size;
14886 }
14887 }
14888
14889 /* Finish ex9 table. */
14890
14891 void
14892 nds32_elf_ex9_finish (struct bfd_link_info *link_info)
14893 {
14894 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
14895 nds32_elf_order_insn_times (link_info);
14896 nds32_elf_ex9_total_relax (link_info);
14897 /* Traverse the hash table and count its times. */
14898 nds32_elf_code_hash_traverse (nds32_elf_count_insn_times);
14899 nds32_elf_order_insn_times (link_info);
14900 nds32_elf_ex9_build_itable (link_info);
14901 }
14902
14903 /* Relocate the entries in ex9 table. */
14904
14905 static bfd_vma
14906 nds32_elf_ex9_reloc_insn (struct elf_nds32_insn_times_entry *ptr,
14907 struct bfd_link_info *link_info)
14908 {
14909 Elf_Internal_Sym *isym = NULL;
14910 bfd_vma relocation = -1;
14911 struct elf_link_hash_entry *h;
14912
14913 if (ptr->m_list != NULL)
14914 {
14915 /* Global symbol. */
14916 h = ptr->m_list->h_list->h;
14917 if ((h->root.type == bfd_link_hash_defined
14918 || h->root.type == bfd_link_hash_defweak)
14919 && h->root.u.def.section != NULL
14920 && h->root.u.def.section->output_section != NULL)
14921 {
14922
14923 relocation = h->root.u.def.value +
14924 h->root.u.def.section->output_section->vma +
14925 h->root.u.def.section->output_offset;
14926 relocation += ptr->m_list->irel->r_addend;
14927 }
14928 else
14929 relocation = 0;
14930 }
14931 else if (ptr->sec !=NULL)
14932 {
14933 /* Local symbol. */
14934 Elf_Internal_Sym sym;
14935 asection *sec = NULL;
14936 asection isec;
14937 asection *isec_ptr = &isec;
14938 Elf_Internal_Rela irel_backup = *(ptr->irel);
14939 asection *sec_backup = ptr->sec;
14940 bfd *abfd = ptr->sec->owner;
14941
14942 if (!nds32_get_local_syms (abfd, sec, &isym))
14943 return FALSE;
14944 isym = isym + ELF32_R_SYM (ptr->irel->r_info);
14945
14946 sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
14947 if (sec != NULL)
14948 *isec_ptr = *sec;
14949 sym = *isym;
14950
14951 /* The purpose is same as elf_link_input_bfd. */
14952 if (isec_ptr != NULL
14953 && isec_ptr->sec_info_type == SEC_INFO_TYPE_MERGE
14954 && ELF_ST_TYPE (isym->st_info) != STT_SECTION)
14955 {
14956 sym.st_value =
14957 _bfd_merged_section_offset (ptr->sec->output_section->owner, &isec_ptr,
14958 elf_section_data (isec_ptr)->sec_info,
14959 isym->st_value);
14960 }
14961 relocation = _bfd_elf_rela_local_sym (link_info->output_bfd, &sym,
14962 &ptr->sec, ptr->irel);
14963 if (ptr->irel != NULL)
14964 relocation += ptr->irel->r_addend;
14965
14966 /* Restore origin value since there may be some insntructions that
14967 could not be replaced with ex9.it. */
14968 *(ptr->irel) = irel_backup;
14969 ptr->sec = sec_backup;
14970 }
14971
14972 return relocation;
14973 }
14974
14975 /* Import ex9 table and build list. */
14976
14977 void
14978 nds32_elf_ex9_import_table (struct bfd_link_info *info)
14979 {
14980 int num = 0;
14981 bfd_byte *contents;
14982 unsigned long insn;
14983 FILE *ex9_import_file;
14984 int update_ex9_table;
14985 struct elf_nds32_link_hash_table *table;
14986
14987 table = nds32_elf_hash_table (info);
14988 ex9_import_file = table->ex9_import_file;
14989 rewind (table->ex9_import_file);
14990
14991 contents = bfd_malloc (sizeof (bfd_byte) * 4);
14992
14993 /* Read instructions from the input file and build the list. */
14994 while (!feof (ex9_import_file))
14995 {
14996 char *code;
14997 struct elf_nds32_insn_times_entry *ptr;
14998 size_t nread;
14999
15000 nread = fread (contents, sizeof (bfd_byte) * 4, 1, ex9_import_file);
15001 /* Ignore the final byte 0x0a. */
15002 if (nread < 1)
15003 break;
15004 insn = bfd_getb32 (contents);
15005 code = bfd_malloc (sizeof (char) * 9);
15006 snprintf (code, 9, "%08lx", insn);
15007 ptr = bfd_malloc (sizeof (struct elf_nds32_insn_times_entry));
15008 ptr->string = code;
15009 ptr->order = num;
15010 ptr->times = -1;
15011 ptr->sec = NULL;
15012 ptr->m_list = NULL;
15013 ptr->rel_backup.r_offset = 0;
15014 ptr->rel_backup.r_info = 0;
15015 ptr->rel_backup.r_addend = 0;
15016 ptr->irel = NULL;
15017 ptr->next = NULL;
15018 nds32_elf_ex9_insert_entry (ptr);
15019 num++;
15020 }
15021
15022 update_ex9_table = table->update_ex9_table;
15023 if (update_ex9_table == 1)
15024 {
15025 /* It has to consider of sethi need to use multiple page
15026 but it not be done yet. */
15027 nds32_elf_code_hash_traverse (nds32_elf_examine_insn_times);
15028 nds32_elf_order_insn_times (info);
15029 }
15030 }
15031
15032 /* Export ex9 table. */
15033
15034 static void
15035 nds32_elf_ex9_export (struct bfd_link_info *info,
15036 bfd_byte *contents, int size)
15037 {
15038 FILE *ex9_export_file;
15039 struct elf_nds32_link_hash_table *table;
15040
15041 table = nds32_elf_hash_table (info);
15042 ex9_export_file = table->ex9_export_file;
15043 fwrite (contents, sizeof (bfd_byte), size, ex9_export_file);
15044 fclose (ex9_export_file);
15045 }
15046
15047 /* Adjust relocations of J and JAL in ex9.itable.
15048 Export ex9 table. */
15049
15050 static void
15051 nds32_elf_ex9_reloc_jmp (struct bfd_link_info *link_info)
15052 {
15053 asection *table_sec = NULL;
15054 struct elf_nds32_insn_times_entry *ex9_insn = ex9_insn_head;
15055 struct elf_nds32_insn_times_entry *temp_ptr, *temp_ptr2;
15056 bfd *it_abfd;
15057 uint32_t insn, insn_with_reg, source_insn;
15058 bfd_byte *contents = NULL, *source_contents = NULL;
15059 int size = 0;
15060 bfd_vma gp;
15061 int shift, update_ex9_table, offset = 0;
15062 reloc_howto_type *howto = NULL;
15063 Elf_Internal_Rela rel_backup;
15064 unsigned short insn_ex9;
15065 struct elf_nds32_link_hash_table *table;
15066 FILE *ex9_export_file;
15067 static bfd_boolean done = FALSE;
15068
15069 if (done)
15070 return;
15071
15072 done = TRUE;
15073
15074 table = nds32_elf_hash_table (link_info);
15075 if (table)
15076 table->relax_status |= NDS32_RELAX_EX9_DONE;
15077
15078
15079 update_ex9_table = table->update_ex9_table;
15080 /* Generated ex9.itable exactly. */
15081 if (update_ex9_table == 0)
15082 {
15083 for (it_abfd = link_info->input_bfds; it_abfd != NULL;
15084 it_abfd = it_abfd->link.next)
15085 {
15086 table_sec = bfd_get_section_by_name (it_abfd, ".ex9.itable");
15087 if (table_sec != NULL)
15088 break;
15089 }
15090
15091 if (table_sec != NULL)
15092 {
15093 bfd *output_bfd;
15094
15095 output_bfd = table_sec->output_section->owner;
15096 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15097 if (table_sec->size == 0)
15098 return;
15099
15100 if (!nds32_get_section_contents (it_abfd, table_sec, &contents))
15101 return;
15102 }
15103 }
15104 else
15105 {
15106 /* Set gp. */
15107 bfd *output_bfd;
15108
15109 output_bfd = link_info->input_bfds->sections->output_section->owner;
15110 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15111 contents = bfd_malloc (sizeof (bfd_byte) * 2048);
15112 }
15113
15114 /* Relocate instruction. */
15115 while (ex9_insn)
15116 {
15117 bfd_vma relocation, min_relocation = 0xffffffff;
15118
15119 insn = strtol (ex9_insn->string, NULL, 16);
15120 insn_with_reg = 0;
15121 if (ex9_insn->m_list != NULL || ex9_insn->sec != NULL)
15122 {
15123 if (ex9_insn->m_list)
15124 rel_backup = ex9_insn->m_list->rel_backup;
15125 else
15126 rel_backup = ex9_insn->rel_backup;
15127
15128 nds32_elf_get_insn_with_reg (&rel_backup, insn, &insn_with_reg);
15129 howto =
15130 bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE
15131 (rel_backup.r_info));
15132 shift = howto->rightshift;
15133 if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_25_PCREL_RELA
15134 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_ORI_RELA
15135 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S0_RELA
15136 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S1_RELA
15137 || ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_LO12S2_RELA)
15138 {
15139 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15140 insn =
15141 insn_with_reg | ((relocation >> shift) &
15142 nds32_elf_irel_mask (&rel_backup));
15143 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15144 }
15145 else if ((ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3
15146 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0)
15147 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA15S3_RELA
15148 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA15S0_RELA)
15149 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA12S2_DP_RELA
15150 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA12S2_SP_RELA)
15151 || (ELF32_R_TYPE (rel_backup.r_info) >= R_NDS32_SDA16S3_RELA
15152 && ELF32_R_TYPE (rel_backup.r_info) <= R_NDS32_SDA19S0_RELA))
15153 {
15154 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15155 insn =
15156 insn_with_reg | (((relocation - gp) >> shift) &
15157 nds32_elf_irel_mask (&rel_backup));
15158 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15159 }
15160 else if (ELF32_R_TYPE (rel_backup.r_info) == R_NDS32_HI20_RELA)
15161 {
15162 /* Sethi may be multiple entry for one insn. */
15163 if (ex9_insn->next && ex9_insn->m_list
15164 && ex9_insn->m_list == ex9_insn->next->m_list)
15165 {
15166 struct elf_link_hash_entry_mul_list *m_list;
15167 struct elf_nds32_ex9_refix *fix_ptr;
15168 struct elf_link_hash_entry *h;
15169
15170 temp_ptr = ex9_insn;
15171 temp_ptr2 = ex9_insn;
15172 m_list = ex9_insn->m_list;
15173 while (m_list)
15174 {
15175 h = m_list->h_list->h;
15176 relocation = h->root.u.def.value +
15177 h->root.u.def.section->output_section->vma +
15178 h->root.u.def.section->output_offset;
15179 relocation += m_list->irel->r_addend;
15180
15181 if (relocation < min_relocation)
15182 min_relocation = relocation;
15183 m_list = m_list->next;
15184 }
15185 relocation = min_relocation;
15186
15187 /* Put insntruction into ex9 table. */
15188 insn = insn_with_reg
15189 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15190 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15191 relocation = relocation + 0x1000; /* hi20 */
15192
15193 while (ex9_insn->next && ex9_insn->m_list
15194 && ex9_insn->m_list == ex9_insn->next->m_list)
15195 {
15196 /* Multiple sethi. */
15197 ex9_insn = ex9_insn->next;
15198 size += 4;
15199 insn =
15200 insn_with_reg | ((relocation >> shift) &
15201 nds32_elf_irel_mask (&rel_backup));
15202 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15203 relocation = relocation + 0x1000; /* hi20 */
15204 }
15205
15206 fix_ptr = ex9_refix_head;
15207 while (fix_ptr)
15208 {
15209 /* Fix ex9 insn. */
15210 /* temp_ptr2 points to the head of multiple sethi. */
15211 temp_ptr = temp_ptr2;
15212 while (fix_ptr->order != temp_ptr->order && fix_ptr->next)
15213 {
15214 fix_ptr = fix_ptr->next;
15215 }
15216 if (fix_ptr->order != temp_ptr->order)
15217 break;
15218
15219 /* Set source insn. */
15220 relocation =
15221 fix_ptr->h->root.u.def.value +
15222 fix_ptr->h->root.u.def.section->output_section->vma +
15223 fix_ptr->h->root.u.def.section->output_offset;
15224 relocation += fix_ptr->irel->r_addend;
15225 /* sethi imm is imm20s. */
15226 source_insn = insn_with_reg | ((relocation >> shift) & 0xfffff);
15227
15228 while (temp_ptr)
15229 {
15230 /* Match entry and source code. */
15231 insn = bfd_getb32 (contents + (temp_ptr->order) * 4 + offset);
15232 if (insn == source_insn)
15233 {
15234 /* Fix the ex9 insn. */
15235 if (temp_ptr->order != fix_ptr->order)
15236 {
15237 if (!nds32_get_section_contents
15238 (fix_ptr->sec->owner, fix_ptr->sec,
15239 &source_contents))
15240 (*_bfd_error_handler)
15241 (_("Linker: error cannot fixed ex9 relocation \n"));
15242 if (temp_ptr->order < 32)
15243 insn_ex9 = INSN_EX9_IT_2;
15244 else
15245 insn_ex9 = INSN_EX9_IT_1;
15246 insn_ex9 = insn_ex9 | temp_ptr->order;
15247 bfd_putb16 (insn_ex9, source_contents + fix_ptr->irel->r_offset);
15248 }
15249 break;
15250 }
15251 else
15252 {
15253 if (!temp_ptr->next || temp_ptr->m_list != temp_ptr->next->m_list)
15254 (*_bfd_error_handler)
15255 (_("Linker: error cannot fixed ex9 relocation \n"));
15256 else
15257 temp_ptr = temp_ptr->next;
15258 }
15259 }
15260 fix_ptr = fix_ptr->next;
15261 }
15262 }
15263 else
15264 {
15265 relocation = nds32_elf_ex9_reloc_insn (ex9_insn, link_info);
15266 insn = insn_with_reg
15267 | ((relocation >> shift) & nds32_elf_irel_mask (&rel_backup));
15268 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15269 }
15270 }
15271 }
15272 else
15273 {
15274 /* Insn without relocation does not have to be fixed
15275 if need to update export table. */
15276 if (update_ex9_table == 1)
15277 bfd_putb32 (insn, contents + (ex9_insn->order) * 4);
15278 }
15279 ex9_insn = ex9_insn->next;
15280 size += 4;
15281 }
15282
15283 ex9_export_file = table->ex9_export_file;
15284 if (ex9_export_file != NULL)
15285 nds32_elf_ex9_export (link_info, contents, table_sec->size);
15286 else if (update_ex9_table == 1)
15287 {
15288 table->ex9_export_file = table->ex9_import_file;
15289 rewind (table->ex9_export_file);
15290 nds32_elf_ex9_export (link_info, contents, size);
15291 }
15292 }
15293
15294 /* Generate ex9 hash table. */
15295
15296 static bfd_boolean
15297 nds32_elf_ex9_build_hash_table (bfd *abfd, asection *sec,
15298 struct bfd_link_info *link_info)
15299 {
15300 Elf_Internal_Rela *internal_relocs;
15301 Elf_Internal_Rela *irelend;
15302 Elf_Internal_Rela *irel;
15303 Elf_Internal_Rela *jrel;
15304 Elf_Internal_Rela rel_backup;
15305 Elf_Internal_Shdr *symtab_hdr;
15306 Elf_Internal_Sym *isym = NULL;
15307 asection *isec;
15308 struct elf_link_hash_entry **sym_hashes;
15309 bfd_byte *contents = NULL;
15310 bfd_vma off = 0;
15311 unsigned long r_symndx;
15312 uint32_t insn, insn_with_reg;
15313 struct elf_link_hash_entry *h;
15314 int data_flag, shift, align;
15315 bfd_vma relocation;
15316 /* Suppress ex9 if `.no_relax ex9' or inner loop. */
15317 reloc_howto_type *howto = NULL;
15318
15319 sym_hashes = elf_sym_hashes (abfd);
15320 /* Load section instructions, relocations, and symbol table. */
15321 if (!nds32_get_section_contents (abfd, sec, &contents))
15322 return FALSE;
15323
15324 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
15325 TRUE /* keep_memory */);
15326 irelend = internal_relocs + sec->reloc_count;
15327 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
15328 if (!nds32_get_local_syms (abfd, sec, &isym))
15329 return FALSE;
15330
15331 /* Check the object if enable ex9. */
15332 irel = find_relocs_at_address (internal_relocs, internal_relocs, irelend,
15333 R_NDS32_RELAX_ENTRY);
15334
15335 /* Check this section trigger ex9 relaxation. */
15336 if (irel == NULL
15337 || irel >= irelend
15338 || ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
15339 || (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY
15340 && !(irel->r_addend & R_NDS32_RELAX_ENTRY_EX9_FLAG)))
15341 return TRUE;
15342
15343 irel = internal_relocs;
15344
15345 /* Push each insn into hash table. */
15346 while (off < sec->size)
15347 {
15348 char code[10];
15349 struct elf_nds32_code_hash_entry *entry;
15350
15351 while (irel != NULL && irel < irelend && irel->r_offset < off)
15352 irel++;
15353
15354 data_flag = nds32_elf_ex9_relocation_check (link_info, &irel, irelend,
15355 NULL, sec, &off, contents);
15356 if (data_flag & DATA_EXIST)
15357 {
15358 /* We save the move offset in the highest byte. */
15359 off += (data_flag >> 24);
15360 continue;
15361 }
15362
15363 if (*(contents + off) & 0x80)
15364 {
15365 off += 2;
15366 }
15367 else
15368 {
15369 h = NULL;
15370 isec = NULL;
15371 jrel = NULL;
15372 rel_backup.r_info = 0;
15373 rel_backup.r_offset = 0;
15374 rel_backup.r_addend = 0;
15375 /* Load the instruction and its opcode with register for comparing. */
15376 insn = bfd_getb32 (contents + off);
15377 insn_with_reg = 0;
15378 if (irel != NULL && irel < irelend && irel->r_offset == off)
15379 {
15380 nds32_elf_get_insn_with_reg (irel, insn, &insn_with_reg);
15381 howto = bfd_elf32_bfd_reloc_type_table_lookup (ELF32_R_TYPE (irel->r_info));
15382 shift = howto->rightshift;
15383 align = (1 << shift) - 1;
15384 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_25_PCREL_RELA
15385 || ELF32_R_TYPE (irel->r_info) == R_NDS32_HI20_RELA
15386 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_ORI_RELA
15387 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S0_RELA
15388 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S1_RELA
15389 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_RELA
15390 ||(ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15391 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15392 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15393 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15394 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15395 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15396 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15397 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15398 {
15399 r_symndx = ELF32_R_SYM (irel->r_info);
15400 jrel = irel;
15401 rel_backup = *irel;
15402 if (r_symndx < symtab_hdr->sh_info)
15403 {
15404 /* Local symbol. */
15405 int shndx = isym[r_symndx].st_shndx;
15406
15407 bfd_vma st_value = (isym + r_symndx)->st_value;
15408 isec = elf_elfsections (abfd)[shndx]->bfd_section;
15409 relocation = (isec->output_section->vma + isec->output_offset
15410 + st_value + irel->r_addend);
15411 }
15412 else
15413 {
15414 /* External symbol. */
15415 bfd_boolean warned ATTRIBUTE_UNUSED;
15416 bfd_boolean ignored ATTRIBUTE_UNUSED;
15417 bfd_boolean unresolved_reloc ATTRIBUTE_UNUSED;
15418 asection *sym_sec;
15419
15420 /* Maybe there is a better way to get h and relocation */
15421 RELOC_FOR_GLOBAL_SYMBOL (link_info, abfd, sec, irel,
15422 r_symndx, symtab_hdr, sym_hashes,
15423 h, sym_sec, relocation,
15424 unresolved_reloc, warned, ignored);
15425 relocation += irel->r_addend;
15426 if ((h->root.type != bfd_link_hash_defined
15427 && h->root.type != bfd_link_hash_defweak)
15428 || strcmp (h->root.root.string, "_FP_BASE_") == 0)
15429 {
15430 off += 4;
15431 continue;
15432 }
15433 }
15434
15435 /* Check for gp relative instruction alignment. */
15436 if ((ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3
15437 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0)
15438 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA15S3_RELA
15439 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA15S0_RELA)
15440 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA12S2_DP_RELA
15441 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA12S2_SP_RELA)
15442 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_SDA16S3_RELA
15443 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_SDA19S0_RELA))
15444 {
15445 bfd_vma gp;
15446 bfd *output_bfd = sec->output_section->owner;
15447 bfd_reloc_status_type r;
15448
15449 /* If the symbol is in the abs section, the out_bfd will be null.
15450 This happens when the relocation has a symbol@GOTOFF. */
15451 r = nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
15452 if (r != bfd_reloc_ok)
15453 {
15454 off += 4;
15455 continue;
15456 }
15457
15458 relocation -= gp;
15459
15460 /* Make sure alignment is correct. */
15461 if (relocation & align)
15462 {
15463 /* Incorrect alignment. */
15464 (*_bfd_error_handler)
15465 (_("%s: warning: unaligned small data access. "
15466 "For entry: {%d, %d, %d}, addr = 0x%x, align = 0x%x."),
15467 bfd_get_filename (abfd), irel->r_offset,
15468 irel->r_info, irel->r_addend, relocation, align);
15469 off += 4;
15470 continue;
15471 }
15472 }
15473
15474 insn = insn_with_reg
15475 | ((relocation >> shift) & nds32_elf_irel_mask (irel));
15476 }
15477 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
15478 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
15479 || ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
15480 {
15481 /* These relocations do not have to relocate contens, so it can
15482 be regard as instruction without relocation. */
15483 }
15484 else
15485 {
15486 off += 4;
15487 continue;
15488 }
15489 }
15490
15491 snprintf (code, sizeof (code), "%08x", insn);
15492 /* Copy "code". */
15493 entry = (struct elf_nds32_code_hash_entry*)
15494 bfd_hash_lookup (&ex9_code_table, code, TRUE, TRUE);
15495 if (entry == NULL)
15496 {
15497 (*_bfd_error_handler)
15498 (_("%P%F: failed creating ex9.it %s hash table: %E\n"), code);
15499 return FALSE;
15500 }
15501 if (h)
15502 {
15503 if (h->root.type == bfd_link_hash_undefined)
15504 return TRUE;
15505 /* Global symbol. */
15506 /* In order to do sethi with different symbol but same value. */
15507 if (entry->m_list == NULL)
15508 {
15509 struct elf_link_hash_entry_mul_list *m_list_new;
15510 struct elf_link_hash_entry_list *h_list_new;
15511
15512 m_list_new = (struct elf_link_hash_entry_mul_list *)
15513 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15514 h_list_new = (struct elf_link_hash_entry_list *)
15515 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15516 entry->m_list = m_list_new;
15517 m_list_new->h_list = h_list_new;
15518 m_list_new->rel_backup = rel_backup;
15519 m_list_new->times = 1;
15520 m_list_new->irel = jrel;
15521 m_list_new->next = NULL;
15522 h_list_new->h = h;
15523 h_list_new->next = NULL;
15524 }
15525 else
15526 {
15527 struct elf_link_hash_entry_mul_list *m_list = entry->m_list;
15528 struct elf_link_hash_entry_list *h_list;
15529
15530 while (m_list)
15531 {
15532 /* Build the different symbols that point to the same address. */
15533 h_list = m_list->h_list;
15534 if (h_list->h->root.u.def.value == h->root.u.def.value
15535 && h_list->h->root.u.def.section->output_section->vma
15536 == h->root.u.def.section->output_section->vma
15537 && h_list->h->root.u.def.section->output_offset
15538 == h->root.u.def.section->output_offset
15539 && m_list->rel_backup.r_addend == rel_backup.r_addend)
15540 {
15541 m_list->times++;
15542 m_list->irel = jrel;
15543 while (h_list->h != h && h_list->next)
15544 h_list = h_list->next;
15545 if (h_list->h != h)
15546 {
15547 struct elf_link_hash_entry_list *h_list_new;
15548
15549 h_list_new = (struct elf_link_hash_entry_list *)
15550 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15551 h_list->next = h_list_new;
15552 h_list_new->h = h;
15553 h_list_new->next = NULL;
15554 }
15555 break;
15556 }
15557 /* The sethi case may have different address but the
15558 hi20 is the same. */
15559 else if (ELF32_R_TYPE (jrel->r_info) == R_NDS32_HI20_RELA
15560 && m_list->next == NULL)
15561 {
15562 struct elf_link_hash_entry_mul_list *m_list_new;
15563 struct elf_link_hash_entry_list *h_list_new;
15564
15565 m_list_new = (struct elf_link_hash_entry_mul_list *)
15566 bfd_malloc (sizeof (struct elf_link_hash_entry_mul_list));
15567 h_list_new = (struct elf_link_hash_entry_list *)
15568 bfd_malloc (sizeof (struct elf_link_hash_entry_list));
15569 m_list->next = m_list_new;
15570 m_list_new->h_list = h_list_new;
15571 m_list_new->rel_backup = rel_backup;
15572 m_list_new->times = 1;
15573 m_list_new->irel = jrel;
15574 m_list_new->next = NULL;
15575 h_list_new->h = h;
15576 h_list_new->next = NULL;
15577 break;
15578 }
15579 m_list = m_list->next;
15580 }
15581 if (!m_list)
15582 {
15583 off += 4;
15584 continue;
15585 }
15586 }
15587 }
15588 else
15589 {
15590 /* Local symbol and insn without relocation*/
15591 entry->times++;
15592 entry->rel_backup = rel_backup;
15593 }
15594
15595 /* Use in sethi insn with constant and global symbol in same format. */
15596 if (!jrel)
15597 entry->const_insn = 1;
15598 else
15599 entry->irel = jrel;
15600 entry->sec = isec;
15601 off += 4;
15602 }
15603 }
15604 return TRUE;
15605 }
15606
15607 /* Set the _ITB_BASE, and point it to ex9 table. */
15608
15609 bfd_boolean
15610 nds32_elf_ex9_itb_base (struct bfd_link_info *link_info)
15611 {
15612 bfd *abfd;
15613 asection *sec;
15614 bfd *output_bfd = NULL;
15615 struct bfd_link_hash_entry *bh = NULL;
15616
15617 if (is_ITB_BASE_set == 1)
15618 return TRUE;
15619
15620 is_ITB_BASE_set = 1;
15621
15622 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_", FALSE, FALSE, TRUE);
15623
15624 if (bh && (bh->type == bfd_link_hash_defined
15625 || bh->type == bfd_link_hash_defweak))
15626 return TRUE;
15627
15628 for (abfd = link_info->input_bfds; abfd != NULL;
15629 abfd = abfd->link.next)
15630 {
15631 sec = bfd_get_section_by_name (abfd, ".ex9.itable");
15632 if (sec != NULL)
15633 {
15634 output_bfd = sec->output_section->owner;
15635 break;
15636 }
15637 }
15638 if (output_bfd == NULL)
15639 {
15640 output_bfd = link_info->output_bfd;
15641 if (output_bfd->sections == NULL)
15642 return TRUE;
15643 else
15644 sec = bfd_abs_section_ptr;
15645 }
15646 bh = bfd_link_hash_lookup (link_info->hash, "_ITB_BASE_",
15647 FALSE, FALSE, TRUE);
15648 return (_bfd_generic_link_add_one_symbol
15649 (link_info, output_bfd, "_ITB_BASE_",
15650 BSF_GLOBAL | BSF_WEAK, sec, 0,
15651 (const char *) NULL, FALSE, get_elf_backend_data
15652 (output_bfd)->collect, &bh));
15653 } /* End EX9.IT */
15654 \f
15655
15656 #define ELF_ARCH bfd_arch_nds32
15657 #define ELF_MACHINE_CODE EM_NDS32
15658 #define ELF_MAXPAGESIZE 0x1000
15659 #define ELF_TARGET_ID NDS32_ELF_DATA
15660
15661 #define TARGET_BIG_SYM nds32_elf32_be_vec
15662 #define TARGET_BIG_NAME "elf32-nds32be"
15663 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
15664 #define TARGET_LITTLE_NAME "elf32-nds32le"
15665
15666 #define elf_info_to_howto nds32_info_to_howto
15667 #define elf_info_to_howto_rel nds32_info_to_howto_rel
15668
15669 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
15670 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
15671 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
15672 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
15673 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
15674
15675 #define bfd_elf32_mkobject nds32_elf_mkobject
15676 #define elf_backend_action_discarded nds32_elf_action_discarded
15677 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
15678 #define elf_backend_check_relocs nds32_elf_check_relocs
15679 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
15680 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
15681 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
15682 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
15683 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
15684 #define elf_backend_relocate_section nds32_elf_relocate_section
15685 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
15686 #define elf_backend_gc_sweep_hook nds32_elf_gc_sweep_hook
15687 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
15688 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
15689 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
15690 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
15691 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
15692 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
15693 #define elf_backend_object_p nds32_elf_object_p
15694 #define elf_backend_final_write_processing nds32_elf_final_write_processing
15695 #define elf_backend_special_sections nds32_elf_special_sections
15696 #define bfd_elf32_bfd_get_relocated_section_contents \
15697 nds32_elf_get_relocated_section_contents
15698
15699 #define elf_backend_can_gc_sections 1
15700 #define elf_backend_can_refcount 1
15701 #define elf_backend_want_got_plt 1
15702 #define elf_backend_plt_readonly 1
15703 #define elf_backend_want_plt_sym 0
15704 #define elf_backend_got_header_size 12
15705 #define elf_backend_may_use_rel_p 1
15706 #define elf_backend_default_use_rela_p 1
15707 #define elf_backend_may_use_rela_p 1
15708
15709 #include "elf32-target.h"
15710
15711 #undef ELF_MAXPAGESIZE
15712 #define ELF_MAXPAGESIZE 0x2000
15713
15714 #undef TARGET_BIG_SYM
15715 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
15716 #undef TARGET_BIG_NAME
15717 #define TARGET_BIG_NAME "elf32-nds32be-linux"
15718 #undef TARGET_LITTLE_SYM
15719 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
15720 #undef TARGET_LITTLE_NAME
15721 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
15722 #undef elf32_bed
15723 #define elf32_bed elf32_nds32_lin_bed
15724
15725 #include "elf32-target.h"
This page took 0.551652 seconds and 5 git commands to generate.