Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf32-nds32.c
1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2019 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 "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "libiberty.h"
29 #include "elf/nds32.h"
30 #include "opcode/nds32.h"
31 #include "elf32-nds32.h"
32 #include "opcode/cgen.h"
33 #include "../opcodes/nds32-opc.h"
34
35 /* Relocation HOWTO functions. */
36 static bfd_reloc_status_type nds32_elf_ignore_reloc
37 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
38 static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type nds32_elf_hi20_reloc
41 (bfd *, arelent *, asymbol *, void *,
42 asection *, bfd *, char **);
43 static bfd_reloc_status_type nds32_elf_lo12_reloc
44 (bfd *, arelent *, asymbol *, void *,
45 asection *, bfd *, char **);
46 static bfd_reloc_status_type nds32_elf_generic_reloc
47 (bfd *, arelent *, asymbol *, void *,
48 asection *, bfd *, char **);
49 static bfd_reloc_status_type nds32_elf_sda15_reloc
50 (bfd *, arelent *, asymbol *, void *,
51 asection *, bfd *, char **);
52
53 /* Helper functions for HOWTO. */
54 static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
55 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
56 asection *, bfd_vma, bfd_vma);
57
58 /* Nds32 helper functions. */
59 static bfd_vma calculate_memory_address
60 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
61 static int nds32_get_section_contents (bfd *, asection *,
62 bfd_byte **, bfd_boolean);
63 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
64 Elf_Internal_Sym **);
65 static bfd_boolean nds32_relax_fp_as_gp
66 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
67 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
68 Elf_Internal_Sym *isymbuf);
69 static bfd_boolean nds32_fag_remove_unused_fpbase
70 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
71 Elf_Internal_Rela *irelend);
72
73 enum
74 {
75 MACH_V1 = bfd_mach_n1h,
76 MACH_V2 = bfd_mach_n1h_v2,
77 MACH_V3 = bfd_mach_n1h_v3,
78 MACH_V3M = bfd_mach_n1h_v3m
79 };
80
81 #define MIN(a, b) ((a) > (b) ? (b) : (a))
82 #define MAX(a, b) ((a) > (b) ? (a) : (b))
83
84 /* The name of the dynamic interpreter. This is put in the .interp
85 section. */
86 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
87
88 #define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
89 && (flags) & SEC_LOAD \
90 && (flags) & SEC_READONLY)
91
92 /* The nop opcode we use. */
93 #define NDS32_NOP32 0x40000009
94 #define NDS32_NOP16 0x9200
95
96 /* The size in bytes of an entry in the procedure linkage table. */
97 #define PLT_ENTRY_SIZE 24
98 #define PLT_HEADER_SIZE 24
99
100 /* The first entry in a procedure linkage table are reserved,
101 and the initial contents are unimportant (we zero them out).
102 Subsequent entries look like this. */
103 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
104 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
105 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
106 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
107 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
108
109 /* $ta is change to $r15 (from $r25). */
110 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
111 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
112 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
113 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
114 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
115 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
116
117 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
118 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
119 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
120 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
121 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
122
123 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
124 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
125 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
126 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
127 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
128 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
129
130 /* These are macros used to get the relocation accurate value. */
131 #define ACCURATE_8BIT_S1 (0x100)
132 #define ACCURATE_U9BIT_S1 (0x400)
133 #define ACCURATE_12BIT_S1 (0x2000)
134 #define ACCURATE_14BIT_S1 (0x4000)
135 #define ACCURATE_19BIT (0x40000)
136
137 /* These are macros used to get the relocation conservative value. */
138 #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
139 #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
140 #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
141 #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
142 /* These must be more conservative because the address may be in
143 different segment. */
144 #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
145 #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
146 #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
147 #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
148 #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
149
150 /* Size of small data/bss sections, used to calculate SDA_BASE. */
151 static long got_size = 0;
152 static int is_SDA_BASE_set = 0;
153
154 /* Convert ELF-VER in eflags to string for debugging purpose. */
155 static const char *const nds32_elfver_strtab[] =
156 {
157 "ELF-1.2",
158 "ELF-1.3",
159 "ELF-1.4",
160 };
161
162 /* The nds32 linker needs to keep track of the number of relocs that it
163 decides to copy in check_relocs for each symbol. This is so that
164 it can discard PC relative relocs if it doesn't need them when
165 linking with -Bsymbolic. We store the information in a field
166 extending the regular ELF linker hash table. */
167
168 /* This structure keeps track of the number of PC relative relocs we
169 have copied for a given symbol. */
170
171 struct elf_nds32_pcrel_relocs_copied
172 {
173 /* Next section. */
174 struct elf_nds32_pcrel_relocs_copied *next;
175 /* A section in dynobj. */
176 asection *section;
177 /* Number of relocs copied in this section. */
178 bfd_size_type count;
179 };
180
181 enum elf_nds32_tls_type
182 {
183 GOT_UNKNOWN = (0),
184 GOT_NORMAL = (1 << 0),
185 GOT_TLS_LE = (1 << 1),
186 GOT_TLS_IE = (1 << 2),
187 GOT_TLS_IEGP = (1 << 3),
188 GOT_TLS_LD = (1 << 4),
189 GOT_TLS_GD = (1 << 5),
190 GOT_TLS_DESC = (1 << 6),
191 };
192
193 /* Nds32 ELF linker hash entry. */
194
195 struct elf_nds32_link_hash_entry
196 {
197 struct elf_link_hash_entry root;
198
199 /* Track dynamic relocs copied for this symbol. */
200 struct elf_dyn_relocs *dyn_relocs;
201
202 /* For checking relocation type. */
203 enum elf_nds32_tls_type tls_type;
204
205 int offset_to_gp;
206 };
207
208 /* Get the nds32 ELF linker hash table from a link_info structure. */
209
210 #define FP_BASE_NAME "_FP_BASE_"
211 static int check_start_export_sym = 0;
212
213 /* The offset for executable tls relaxation. */
214 #define TP_OFFSET 0x0
215
216 typedef struct
217 {
218 int min_id;
219 int max_id;
220 int count;
221 int bias;
222 int init;
223 } elf32_nds32_relax_group_t;
224
225 struct elf_nds32_obj_tdata
226 {
227 struct elf_obj_tdata root;
228
229 /* tls_type for each local got entry. */
230 char *local_got_tls_type;
231
232 /* GOTPLT entries for TLS descriptors. */
233 bfd_vma *local_tlsdesc_gotent;
234
235 /* for R_NDS32_RELAX_GROUP handling. */
236 elf32_nds32_relax_group_t relax_group;
237
238 unsigned int hdr_size;
239 int* offset_to_gp;
240 };
241
242 #define elf_nds32_tdata(bfd) \
243 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
244
245 #define elf32_nds32_local_got_tls_type(bfd) \
246 (elf_nds32_tdata (bfd)->local_got_tls_type)
247
248 #define elf32_nds32_local_gp_offset(bfd) \
249 (elf_nds32_tdata (bfd)->offset_to_gp)
250
251 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
252
253 #define elf32_nds32_relax_group_ptr(bfd) \
254 &(elf_nds32_tdata (bfd)->relax_group)
255
256 static bfd_boolean
257 nds32_elf_mkobject (bfd *abfd)
258 {
259 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
260 NDS32_ELF_DATA);
261 }
262
263 /* Relocations used for relocation. */
264 /* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
265 initialize array nds32_elf_howto_table in any order. The benefit
266 is that we can add any new relocations with any numbers and don't
267 need to fill the gap by lots of EMPTY_HOWTO. */
268 #define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
269 [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
270
271 static reloc_howto_type nds32_elf_howto_table[] =
272 {
273 /* This reloc does nothing. */
274 HOWTO2 (R_NDS32_NONE, /* type */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 32, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_bitfield,/* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_NDS32_NONE", /* name */
283 FALSE, /* partial_inplace */
284 0, /* src_mask */
285 0, /* dst_mask */
286 FALSE), /* pcrel_offset */
287
288 /* A 16 bit absolute relocation. */
289 HOWTO2 (R_NDS32_16, /* type */
290 0, /* rightshift */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
292 16, /* bitsize */
293 FALSE, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_bitfield,/* complain_on_overflow */
296 nds32_elf_generic_reloc,/* special_function */
297 "R_NDS32_16", /* name */
298 FALSE, /* partial_inplace */
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
301 FALSE), /* pcrel_offset */
302
303 /* A 32 bit absolute relocation. */
304 HOWTO2 (R_NDS32_32, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 32, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield,/* complain_on_overflow */
311 nds32_elf_generic_reloc,/* special_function */
312 "R_NDS32_32", /* name */
313 FALSE, /* partial_inplace */
314 0xffffffff, /* src_mask */
315 0xffffffff, /* dst_mask */
316 FALSE), /* pcrel_offset */
317
318 /* A 20 bit address. */
319 HOWTO2 (R_NDS32_20, /* type */
320 0, /* rightshift */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
322 20, /* bitsize */
323 FALSE, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_unsigned,/* complain_on_overflow */
326 nds32_elf_generic_reloc,/* special_function */
327 "R_NDS32_20", /* name */
328 FALSE, /* partial_inplace */
329 0xfffff, /* src_mask */
330 0xfffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
332
333 /* An PC Relative 9-bit relocation, shifted by 2.
334 This reloc is complicated because relocations are relative to pc & -4.
335 i.e. branches in the right insn slot use the address of the left insn
336 slot for pc. */
337 /* It's not clear whether this should have partial_inplace set or not.
338 Branch relaxing in the assembler can store the addend in the insn,
339 and if bfd_install_relocation gets called the addend may get added
340 again. */
341 HOWTO2 (R_NDS32_9_PCREL, /* type */
342 1, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 8, /* bitsize */
345 TRUE, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_signed,/* complain_on_overflow */
348 nds32_elf_9_pcrel_reloc,/* special_function */
349 "R_NDS32_9_PCREL", /* name */
350 FALSE, /* partial_inplace */
351 0xff, /* src_mask */
352 0xff, /* dst_mask */
353 TRUE), /* pcrel_offset */
354
355 /* A relative 15 bit relocation, right shifted by 1. */
356 HOWTO2 (R_NDS32_15_PCREL, /* type */
357 1, /* rightshift */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
359 14, /* bitsize */
360 TRUE, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_signed,/* complain_on_overflow */
363 bfd_elf_generic_reloc, /* special_function */
364 "R_NDS32_15_PCREL", /* name */
365 FALSE, /* partial_inplace */
366 0x3fff, /* src_mask */
367 0x3fff, /* dst_mask */
368 TRUE), /* pcrel_offset */
369
370 /* A relative 17 bit relocation, right shifted by 1. */
371 HOWTO2 (R_NDS32_17_PCREL, /* type */
372 1, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 16, /* bitsize */
375 TRUE, /* pc_relative */
376 0, /* bitpos */
377 complain_overflow_signed,/* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 "R_NDS32_17_PCREL", /* name */
380 FALSE, /* partial_inplace */
381 0xffff, /* src_mask */
382 0xffff, /* dst_mask */
383 TRUE), /* pcrel_offset */
384
385 /* A relative 25 bit relocation, right shifted by 1. */
386 /* It's not clear whether this should have partial_inplace set or not.
387 Branch relaxing in the assembler can store the addend in the insn,
388 and if bfd_install_relocation gets called the addend may get added
389 again. */
390 HOWTO2 (R_NDS32_25_PCREL, /* type */
391 1, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 24, /* bitsize */
394 TRUE, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_signed,/* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 "R_NDS32_25_PCREL", /* name */
399 FALSE, /* partial_inplace */
400 0xffffff, /* src_mask */
401 0xffffff, /* dst_mask */
402 TRUE), /* pcrel_offset */
403
404 /* High 20 bits of address when lower 12 is or'd in. */
405 HOWTO2 (R_NDS32_HI20, /* type */
406 12, /* rightshift */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
408 20, /* bitsize */
409 FALSE, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_dont,/* complain_on_overflow */
412 nds32_elf_hi20_reloc, /* special_function */
413 "R_NDS32_HI20", /* name */
414 FALSE, /* partial_inplace */
415 0x000fffff, /* src_mask */
416 0x000fffff, /* dst_mask */
417 FALSE), /* pcrel_offset */
418
419 /* Lower 12 bits of address. */
420 HOWTO2 (R_NDS32_LO12S3, /* type */
421 3, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 9, /* bitsize */
424 FALSE, /* pc_relative */
425 0, /* bitpos */
426 complain_overflow_dont,/* complain_on_overflow */
427 nds32_elf_lo12_reloc, /* special_function */
428 "R_NDS32_LO12S3", /* name */
429 FALSE, /* partial_inplace */
430 0x000001ff, /* src_mask */
431 0x000001ff, /* dst_mask */
432 FALSE), /* pcrel_offset */
433
434 /* Lower 12 bits of address. */
435 HOWTO2 (R_NDS32_LO12S2, /* type */
436 2, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 10, /* bitsize */
439 FALSE, /* pc_relative */
440 0, /* bitpos */
441 complain_overflow_dont,/* complain_on_overflow */
442 nds32_elf_lo12_reloc, /* special_function */
443 "R_NDS32_LO12S2", /* name */
444 FALSE, /* partial_inplace */
445 0x000003ff, /* src_mask */
446 0x000003ff, /* dst_mask */
447 FALSE), /* pcrel_offset */
448
449 /* Lower 12 bits of address. */
450 HOWTO2 (R_NDS32_LO12S1, /* type */
451 1, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 11, /* bitsize */
454 FALSE, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_dont,/* complain_on_overflow */
457 nds32_elf_lo12_reloc, /* special_function */
458 "R_NDS32_LO12S1", /* name */
459 FALSE, /* partial_inplace */
460 0x000007ff, /* src_mask */
461 0x000007ff, /* dst_mask */
462 FALSE), /* pcrel_offset */
463
464 /* Lower 12 bits of address. */
465 HOWTO2 (R_NDS32_LO12S0, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 12, /* bitsize */
469 FALSE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_dont,/* complain_on_overflow */
472 nds32_elf_lo12_reloc, /* special_function */
473 "R_NDS32_LO12S0", /* name */
474 FALSE, /* partial_inplace */
475 0x00000fff, /* src_mask */
476 0x00000fff, /* dst_mask */
477 FALSE), /* pcrel_offset */
478
479 /* Small data area 15 bits offset. */
480 HOWTO2 (R_NDS32_SDA15S3, /* type */
481 3, /* rightshift */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
483 15, /* bitsize */
484 FALSE, /* pc_relative */
485 0, /* bitpos */
486 complain_overflow_signed,/* complain_on_overflow */
487 nds32_elf_sda15_reloc, /* special_function */
488 "R_NDS32_SDA15S3", /* name */
489 FALSE, /* partial_inplace */
490 0x00007fff, /* src_mask */
491 0x00007fff, /* dst_mask */
492 FALSE), /* pcrel_offset */
493
494 /* Small data area 15 bits offset. */
495 HOWTO2 (R_NDS32_SDA15S2, /* type */
496 2, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 15, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_signed,/* complain_on_overflow */
502 nds32_elf_sda15_reloc, /* special_function */
503 "R_NDS32_SDA15S2", /* name */
504 FALSE, /* partial_inplace */
505 0x00007fff, /* src_mask */
506 0x00007fff, /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 /* Small data area 15 bits offset. */
510 HOWTO2 (R_NDS32_SDA15S1, /* type */
511 1, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 15, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_signed,/* complain_on_overflow */
517 nds32_elf_sda15_reloc, /* special_function */
518 "R_NDS32_SDA15S1", /* name */
519 FALSE, /* partial_inplace */
520 0x00007fff, /* src_mask */
521 0x00007fff, /* dst_mask */
522 FALSE), /* pcrel_offset */
523
524 /* Small data area 15 bits offset. */
525 HOWTO2 (R_NDS32_SDA15S0, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 15, /* bitsize */
529 FALSE, /* pc_relative */
530 0, /* bitpos */
531 complain_overflow_signed,/* complain_on_overflow */
532 nds32_elf_sda15_reloc, /* special_function */
533 "R_NDS32_SDA15S0", /* name */
534 FALSE, /* partial_inplace */
535 0x00007fff, /* src_mask */
536 0x00007fff, /* dst_mask */
537 FALSE), /* pcrel_offset */
538
539 /* GNU extension to record C++ vtable hierarchy */
540 HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
541 0, /* rightshift */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
543 0, /* bitsize */
544 FALSE, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_dont,/* complain_on_overflow */
547 NULL, /* special_function */
548 "R_NDS32_GNU_VTINHERIT",/* name */
549 FALSE, /* partial_inplace */
550 0, /* src_mask */
551 0, /* dst_mask */
552 FALSE), /* pcrel_offset */
553
554 /* GNU extension to record C++ vtable member usage */
555 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 0, /* bitsize */
559 FALSE, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_dont,/* complain_on_overflow */
562 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
563 "R_NDS32_GNU_VTENTRY", /* name */
564 FALSE, /* partial_inplace */
565 0, /* src_mask */
566 0, /* dst_mask */
567 FALSE), /* pcrel_offset */
568
569 /* A 16 bit absolute relocation. */
570 HOWTO2 (R_NDS32_16_RELA, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 FALSE, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_bitfield,/* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_NDS32_16_RELA", /* name */
579 FALSE, /* partial_inplace */
580 0xffff, /* src_mask */
581 0xffff, /* dst_mask */
582 FALSE), /* pcrel_offset */
583
584 /* A 32 bit absolute relocation. */
585 HOWTO2 (R_NDS32_32_RELA, /* type */
586 0, /* rightshift */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
588 32, /* bitsize */
589 FALSE, /* pc_relative */
590 0, /* bitpos */
591 complain_overflow_bitfield,/* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_NDS32_32_RELA", /* name */
594 FALSE, /* partial_inplace */
595 0xffffffff, /* src_mask */
596 0xffffffff, /* dst_mask */
597 FALSE), /* pcrel_offset */
598
599 /* A 20 bit address. */
600 HOWTO2 (R_NDS32_20_RELA, /* type */
601 0, /* rightshift */
602 2, /* size (0 = byte, 1 = short, 2 = long) */
603 20, /* bitsize */
604 FALSE, /* pc_relative */
605 0, /* bitpos */
606 complain_overflow_signed,/* complain_on_overflow */
607 bfd_elf_generic_reloc, /* special_function */
608 "R_NDS32_20_RELA", /* name */
609 FALSE, /* partial_inplace */
610 0xfffff, /* src_mask */
611 0xfffff, /* dst_mask */
612 FALSE), /* pcrel_offset */
613
614 HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
615 1, /* rightshift */
616 1, /* size (0 = byte, 1 = short, 2 = long) */
617 8, /* bitsize */
618 TRUE, /* pc_relative */
619 0, /* bitpos */
620 complain_overflow_signed,/* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_NDS32_9_PCREL_RELA",/* name */
623 FALSE, /* partial_inplace */
624 0xff, /* src_mask */
625 0xff, /* dst_mask */
626 TRUE), /* pcrel_offset */
627
628 /* A relative 15 bit relocation, right shifted by 1. */
629 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
630 1, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 14, /* bitsize */
633 TRUE, /* pc_relative */
634 0, /* bitpos */
635 complain_overflow_signed,/* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_NDS32_15_PCREL_RELA",/* name */
638 FALSE, /* partial_inplace */
639 0x3fff, /* src_mask */
640 0x3fff, /* dst_mask */
641 TRUE), /* pcrel_offset */
642
643 /* A relative 17 bit relocation, right shifted by 1. */
644 HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
645 1, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 16, /* bitsize */
648 TRUE, /* pc_relative */
649 0, /* bitpos */
650 complain_overflow_signed,/* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_NDS32_17_PCREL_RELA",/* name */
653 FALSE, /* partial_inplace */
654 0xffff, /* src_mask */
655 0xffff, /* dst_mask */
656 TRUE), /* pcrel_offset */
657
658 /* A relative 25 bit relocation, right shifted by 2. */
659 HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
660 1, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 24, /* bitsize */
663 TRUE, /* pc_relative */
664 0, /* bitpos */
665 complain_overflow_signed,/* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_NDS32_25_PCREL_RELA",/* name */
668 FALSE, /* partial_inplace */
669 0xffffff, /* src_mask */
670 0xffffff, /* dst_mask */
671 TRUE), /* pcrel_offset */
672
673 /* High 20 bits of address when lower 16 is or'd in. */
674 HOWTO2 (R_NDS32_HI20_RELA, /* type */
675 12, /* rightshift */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
677 20, /* bitsize */
678 FALSE, /* pc_relative */
679 0, /* bitpos */
680 complain_overflow_dont,/* complain_on_overflow */
681 bfd_elf_generic_reloc, /* special_function */
682 "R_NDS32_HI20_RELA", /* name */
683 FALSE, /* partial_inplace */
684 0x000fffff, /* src_mask */
685 0x000fffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
687
688 /* Lower 12 bits of address. */
689 HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
690 3, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 9, /* bitsize */
693 FALSE, /* pc_relative */
694 0, /* bitpos */
695 complain_overflow_dont,/* complain_on_overflow */
696 bfd_elf_generic_reloc, /* special_function */
697 "R_NDS32_LO12S3_RELA", /* name */
698 FALSE, /* partial_inplace */
699 0x000001ff, /* src_mask */
700 0x000001ff, /* dst_mask */
701 FALSE), /* pcrel_offset */
702
703 /* Lower 12 bits of address. */
704 HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
705 2, /* rightshift */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
707 10, /* bitsize */
708 FALSE, /* pc_relative */
709 0, /* bitpos */
710 complain_overflow_dont,/* complain_on_overflow */
711 bfd_elf_generic_reloc, /* special_function */
712 "R_NDS32_LO12S2_RELA", /* name */
713 FALSE, /* partial_inplace */
714 0x000003ff, /* src_mask */
715 0x000003ff, /* dst_mask */
716 FALSE), /* pcrel_offset */
717
718 /* Lower 12 bits of address. */
719 HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
720 1, /* rightshift */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
722 11, /* bitsize */
723 FALSE, /* pc_relative */
724 0, /* bitpos */
725 complain_overflow_dont,/* complain_on_overflow */
726 bfd_elf_generic_reloc, /* special_function */
727 "R_NDS32_LO12S1_RELA", /* name */
728 FALSE, /* partial_inplace */
729 0x000007ff, /* src_mask */
730 0x000007ff, /* dst_mask */
731 FALSE), /* pcrel_offset */
732
733 /* Lower 12 bits of address. */
734 HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
735 0, /* rightshift */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
737 12, /* bitsize */
738 FALSE, /* pc_relative */
739 0, /* bitpos */
740 complain_overflow_dont,/* complain_on_overflow */
741 bfd_elf_generic_reloc, /* special_function */
742 "R_NDS32_LO12S0_RELA", /* name */
743 FALSE, /* partial_inplace */
744 0x00000fff, /* src_mask */
745 0x00000fff, /* dst_mask */
746 FALSE), /* pcrel_offset */
747
748 /* Small data area 15 bits offset. */
749 HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
750 3, /* rightshift */
751 2, /* size (0 = byte, 1 = short, 2 = long) */
752 15, /* bitsize */
753 FALSE, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_signed,/* complain_on_overflow */
756 bfd_elf_generic_reloc, /* special_function */
757 "R_NDS32_SDA15S3_RELA",/* name */
758 FALSE, /* partial_inplace */
759 0x00007fff, /* src_mask */
760 0x00007fff, /* dst_mask */
761 FALSE), /* pcrel_offset */
762
763 /* Small data area 15 bits offset. */
764 HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
765 2, /* rightshift */
766 2, /* size (0 = byte, 1 = short, 2 = long) */
767 15, /* bitsize */
768 FALSE, /* pc_relative */
769 0, /* bitpos */
770 complain_overflow_signed,/* complain_on_overflow */
771 bfd_elf_generic_reloc, /* special_function */
772 "R_NDS32_SDA15S2_RELA",/* name */
773 FALSE, /* partial_inplace */
774 0x00007fff, /* src_mask */
775 0x00007fff, /* dst_mask */
776 FALSE), /* pcrel_offset */
777
778 HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
779 1, /* rightshift */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
781 15, /* bitsize */
782 FALSE, /* pc_relative */
783 0, /* bitpos */
784 complain_overflow_signed,/* complain_on_overflow */
785 bfd_elf_generic_reloc, /* special_function */
786 "R_NDS32_SDA15S1_RELA",/* name */
787 FALSE, /* partial_inplace */
788 0x00007fff, /* src_mask */
789 0x00007fff, /* dst_mask */
790 FALSE), /* pcrel_offset */
791
792 HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
793 0, /* rightshift */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
795 15, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_signed,/* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_NDS32_SDA15S0_RELA",/* name */
801 FALSE, /* partial_inplace */
802 0x00007fff, /* src_mask */
803 0x00007fff, /* dst_mask */
804 FALSE), /* pcrel_offset */
805
806 /* GNU extension to record C++ vtable hierarchy */
807 HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
808 0, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 0, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont,/* complain_on_overflow */
814 NULL, /* special_function */
815 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0, /* dst_mask */
819 FALSE), /* pcrel_offset */
820
821 /* GNU extension to record C++ vtable member usage */
822 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
823 0, /* rightshift */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
825 0, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont,/* complain_on_overflow */
829 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
830 "R_NDS32_RELA_GNU_VTENTRY",/* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0, /* dst_mask */
834 FALSE), /* pcrel_offset */
835
836 /* Like R_NDS32_20, but referring to the GOT table entry for
837 the symbol. */
838 HOWTO2 (R_NDS32_GOT20, /* type */
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 20, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_signed,/* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_NDS32_GOT20", /* name */
847 FALSE, /* partial_inplace */
848 0xfffff, /* src_mask */
849 0xfffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
851
852 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
853 entry for the symbol. */
854 HOWTO2 (R_NDS32_25_PLTREL, /* type */
855 1, /* rightshift */
856 2, /* size (0 = byte, 1 = short, 2 = long) */
857 24, /* bitsize */
858 TRUE, /* pc_relative */
859 0, /* bitpos */
860 complain_overflow_signed,/* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_NDS32_25_PLTREL", /* name */
863 FALSE, /* partial_inplace */
864 0xffffff, /* src_mask */
865 0xffffff, /* dst_mask */
866 TRUE), /* pcrel_offset */
867
868 /* This is used only by the dynamic linker. The symbol should exist
869 both in the object being run and in some shared library. The
870 dynamic linker copies the data addressed by the symbol from the
871 shared library into the object, because the object being
872 run has to have the data at some particular address. */
873 HOWTO2 (R_NDS32_COPY, /* type */
874 0, /* rightshift */
875 2, /* size (0 = byte, 1 = short, 2 = long) */
876 32, /* bitsize */
877 FALSE, /* pc_relative */
878 0, /* bitpos */
879 complain_overflow_bitfield,/* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_NDS32_COPY", /* name */
882 FALSE, /* partial_inplace */
883 0xffffffff, /* src_mask */
884 0xffffffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
886
887 /* Like R_NDS32_20, but used when setting global offset table
888 entries. */
889 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
890 0, /* rightshift */
891 2, /* size (0 = byte, 1 = short, 2 = long) */
892 32, /* bitsize */
893 FALSE, /* pc_relative */
894 0, /* bitpos */
895 complain_overflow_bitfield,/* complain_on_overflow */
896 bfd_elf_generic_reloc, /* special_function */
897 "R_NDS32_GLOB_DAT", /* name */
898 FALSE, /* partial_inplace */
899 0xffffffff, /* src_mask */
900 0xffffffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
902
903 /* Marks a procedure linkage table entry for a symbol. */
904 HOWTO2 (R_NDS32_JMP_SLOT, /* type */
905 0, /* rightshift */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
907 32, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_bitfield,/* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_NDS32_JMP_SLOT", /* name */
913 FALSE, /* partial_inplace */
914 0xffffffff, /* src_mask */
915 0xffffffff, /* dst_mask */
916 FALSE), /* pcrel_offset */
917
918 /* Used only by the dynamic linker. When the object is run, this
919 longword is set to the load address of the object, plus the
920 addend. */
921 HOWTO2 (R_NDS32_RELATIVE, /* 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_RELATIVE", /* name */
930 FALSE, /* partial_inplace */
931 0xffffffff, /* src_mask */
932 0xffffffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
934
935 HOWTO2 (R_NDS32_GOTOFF, /* type */
936 0, /* rightshift */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
938 20, /* bitsize */
939 FALSE, /* pc_relative */
940 0, /* bitpos */
941 complain_overflow_signed,/* complain_on_overflow */
942 bfd_elf_generic_reloc, /* special_function */
943 "R_NDS32_GOTOFF", /* name */
944 FALSE, /* partial_inplace */
945 0xfffff, /* src_mask */
946 0xfffff, /* dst_mask */
947 FALSE), /* pcrel_offset */
948
949 /* An PC Relative 20-bit relocation used when setting PIC offset
950 table register. */
951 HOWTO2 (R_NDS32_GOTPC20, /* type */
952 0, /* rightshift */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
954 20, /* bitsize */
955 TRUE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_signed,/* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_NDS32_GOTPC20", /* name */
960 FALSE, /* partial_inplace */
961 0xfffff, /* src_mask */
962 0xfffff, /* dst_mask */
963 TRUE), /* pcrel_offset */
964
965 /* Like R_NDS32_HI20, but referring to the GOT table entry for
966 the symbol. */
967 HOWTO2 (R_NDS32_GOT_HI20, /* type */
968 12, /* rightshift */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
970 20, /* bitsize */
971 FALSE, /* pc_relative */
972 0, /* bitpos */
973 complain_overflow_dont,/* complain_on_overflow */
974 bfd_elf_generic_reloc, /* special_function */
975 "R_NDS32_GOT_HI20", /* name */
976 FALSE, /* partial_inplace */
977 0x000fffff, /* src_mask */
978 0x000fffff, /* dst_mask */
979 FALSE), /* pcrel_offset */
980 HOWTO2 (R_NDS32_GOT_LO12, /* type */
981 0, /* rightshift */
982 2, /* size (0 = byte, 1 = short, 2 = long) */
983 12, /* bitsize */
984 FALSE, /* pc_relative */
985 0, /* bitpos */
986 complain_overflow_dont,/* complain_on_overflow */
987 bfd_elf_generic_reloc, /* special_function */
988 "R_NDS32_GOT_LO12", /* name */
989 FALSE, /* partial_inplace */
990 0x00000fff, /* src_mask */
991 0x00000fff, /* dst_mask */
992 FALSE), /* pcrel_offset */
993
994 /* An PC Relative relocation used when setting PIC offset table register.
995 Like R_NDS32_HI20, but referring to the GOT table entry for
996 the symbol. */
997 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
998 12, /* rightshift */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1000 20, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_dont,/* complain_on_overflow */
1004 bfd_elf_generic_reloc, /* special_function */
1005 "R_NDS32_GOTPC_HI20", /* name */
1006 FALSE, /* partial_inplace */
1007 0x000fffff, /* src_mask */
1008 0x000fffff, /* dst_mask */
1009 TRUE), /* pcrel_offset */
1010 HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
1011 0, /* rightshift */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1013 12, /* bitsize */
1014 FALSE, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_dont,/* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_NDS32_GOTPC_LO12", /* name */
1019 FALSE, /* partial_inplace */
1020 0x00000fff, /* src_mask */
1021 0x00000fff, /* dst_mask */
1022 TRUE), /* pcrel_offset */
1023
1024 HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
1025 12, /* rightshift */
1026 2, /* size (0 = byte, 1 = short, 2 = long) */
1027 20, /* bitsize */
1028 FALSE, /* pc_relative */
1029 0, /* bitpos */
1030 complain_overflow_dont,/* complain_on_overflow */
1031 bfd_elf_generic_reloc, /* special_function */
1032 "R_NDS32_GOTOFF_HI20", /* name */
1033 FALSE, /* partial_inplace */
1034 0x000fffff, /* src_mask */
1035 0x000fffff, /* dst_mask */
1036 FALSE), /* pcrel_offset */
1037 HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
1038 0, /* rightshift */
1039 2, /* size (0 = byte, 1 = short, 2 = long) */
1040 12, /* bitsize */
1041 FALSE, /* pc_relative */
1042 0, /* bitpos */
1043 complain_overflow_dont,/* complain_on_overflow */
1044 bfd_elf_generic_reloc, /* special_function */
1045 "R_NDS32_GOTOFF_LO12", /* name */
1046 FALSE, /* partial_inplace */
1047 0x00000fff, /* src_mask */
1048 0x00000fff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
1050
1051 /* Alignment hint for relaxable instruction. This is used with
1052 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1053 in order to make next label aligned on word boundary. */
1054 HOWTO2 (R_NDS32_INSN16, /* type */
1055 0, /* rightshift */
1056 2, /* size (0 = byte, 1 = short, 2 = long) */
1057 32, /* bitsize */
1058 FALSE, /* pc_relative */
1059 0, /* bitpos */
1060 complain_overflow_dont,/* complain_on_overflow */
1061 nds32_elf_ignore_reloc,/* special_function */
1062 "R_NDS32_INSN16", /* name */
1063 FALSE, /* partial_inplace */
1064 0x00000fff, /* src_mask */
1065 0x00000fff, /* dst_mask */
1066 FALSE), /* pcrel_offset */
1067
1068 /* Alignment hint for label. */
1069 HOWTO2 (R_NDS32_LABEL, /* type */
1070 0, /* rightshift */
1071 2, /* size (0 = byte, 1 = short, 2 = long) */
1072 32, /* bitsize */
1073 FALSE, /* pc_relative */
1074 0, /* bitpos */
1075 complain_overflow_dont,/* complain_on_overflow */
1076 nds32_elf_ignore_reloc,/* special_function */
1077 "R_NDS32_LABEL", /* name */
1078 FALSE, /* partial_inplace */
1079 0xffffffff, /* src_mask */
1080 0xffffffff, /* dst_mask */
1081 FALSE), /* pcrel_offset */
1082
1083 /* Relax hint for unconditional call sequence */
1084 HOWTO2 (R_NDS32_LONGCALL1, /* type */
1085 0, /* rightshift */
1086 2, /* size (0 = byte, 1 = short, 2 = long) */
1087 32, /* bitsize */
1088 FALSE, /* pc_relative */
1089 0, /* bitpos */
1090 complain_overflow_dont,/* complain_on_overflow */
1091 nds32_elf_ignore_reloc,/* special_function */
1092 "R_NDS32_LONGCALL1", /* name */
1093 FALSE, /* partial_inplace */
1094 0xffffffff, /* src_mask */
1095 0xffffffff, /* dst_mask */
1096 FALSE), /* pcrel_offset */
1097
1098 /* Relax hint for conditional call sequence. */
1099 HOWTO2 (R_NDS32_LONGCALL2, /* type */
1100 0, /* rightshift */
1101 2, /* size (0 = byte, 1 = short, 2 = long) */
1102 32, /* bitsize */
1103 FALSE, /* pc_relative */
1104 0, /* bitpos */
1105 complain_overflow_dont,/* complain_on_overflow */
1106 nds32_elf_ignore_reloc,/* special_function */
1107 "R_NDS32_LONGCALL2", /* name */
1108 FALSE, /* partial_inplace */
1109 0xffffffff, /* src_mask */
1110 0xffffffff, /* dst_mask */
1111 FALSE), /* pcrel_offset */
1112
1113 /* Relax hint for conditional call sequence. */
1114 HOWTO2 (R_NDS32_LONGCALL3, /* type */
1115 0, /* rightshift */
1116 2, /* size (0 = byte, 1 = short, 2 = long) */
1117 32, /* bitsize */
1118 FALSE, /* pc_relative */
1119 0, /* bitpos */
1120 complain_overflow_dont,/* complain_on_overflow */
1121 nds32_elf_ignore_reloc,/* special_function */
1122 "R_NDS32_LONGCALL3", /* name */
1123 FALSE, /* partial_inplace */
1124 0xffffffff, /* src_mask */
1125 0xffffffff, /* dst_mask */
1126 FALSE), /* pcrel_offset */
1127
1128 /* Relax hint for unconditional branch sequence. */
1129 HOWTO2 (R_NDS32_LONGJUMP1, /* type */
1130 0, /* rightshift */
1131 2, /* size (0 = byte, 1 = short, 2 = long) */
1132 32, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_dont,/* complain_on_overflow */
1136 nds32_elf_ignore_reloc,/* special_function */
1137 "R_NDS32_LONGJUMP1", /* name */
1138 FALSE, /* partial_inplace */
1139 0xffffffff, /* src_mask */
1140 0xffffffff, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1142
1143 /* Relax hint for conditional branch sequence. */
1144 HOWTO2 (R_NDS32_LONGJUMP2, /* type */
1145 0, /* rightshift */
1146 2, /* size (0 = byte, 1 = short, 2 = long) */
1147 32, /* bitsize */
1148 FALSE, /* pc_relative */
1149 0, /* bitpos */
1150 complain_overflow_dont,/* complain_on_overflow */
1151 nds32_elf_ignore_reloc,/* special_function */
1152 "R_NDS32_LONGJUMP2", /* name */
1153 FALSE, /* partial_inplace */
1154 0xffffffff, /* src_mask */
1155 0xffffffff, /* dst_mask */
1156 FALSE), /* pcrel_offset */
1157
1158 /* Relax hint for conditional branch sequence. */
1159 HOWTO2 (R_NDS32_LONGJUMP3, /* type */
1160 0, /* rightshift */
1161 2, /* size (0 = byte, 1 = short, 2 = long) */
1162 32, /* bitsize */
1163 FALSE, /* pc_relative */
1164 0, /* bitpos */
1165 complain_overflow_dont,/* complain_on_overflow */
1166 nds32_elf_ignore_reloc,/* special_function */
1167 "R_NDS32_LONGJUMP3", /* name */
1168 FALSE, /* partial_inplace */
1169 0xffffffff, /* src_mask */
1170 0xffffffff, /* dst_mask */
1171 FALSE), /* pcrel_offset */
1172
1173 /* Relax hint for load/store sequence. */
1174 HOWTO2 (R_NDS32_LOADSTORE, /* type */
1175 0, /* rightshift */
1176 2, /* size (0 = byte, 1 = short, 2 = long) */
1177 32, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_dont,/* complain_on_overflow */
1181 nds32_elf_ignore_reloc,/* special_function */
1182 "R_NDS32_LOADSTORE", /* name */
1183 FALSE, /* partial_inplace */
1184 0xffffffff, /* src_mask */
1185 0xffffffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1187
1188 /* Relax hint for load/store sequence. */
1189 HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
1190 0, /* rightshift */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 16, /* bitsize */
1193 FALSE, /* pc_relative */
1194 0, /* bitpos */
1195 complain_overflow_dont,/* complain_on_overflow */
1196 nds32_elf_ignore_reloc,/* special_function */
1197 "R_NDS32_9_FIXED_RELA",/* name */
1198 FALSE, /* partial_inplace */
1199 0x000000ff, /* src_mask */
1200 0x000000ff, /* dst_mask */
1201 FALSE), /* pcrel_offset */
1202
1203 /* Relax hint for load/store sequence. */
1204 HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
1205 0, /* rightshift */
1206 2, /* size (0 = byte, 1 = short, 2 = long) */
1207 32, /* bitsize */
1208 FALSE, /* pc_relative */
1209 0, /* bitpos */
1210 complain_overflow_dont,/* complain_on_overflow */
1211 nds32_elf_ignore_reloc,/* special_function */
1212 "R_NDS32_15_FIXED_RELA",/* name */
1213 FALSE, /* partial_inplace */
1214 0x00003fff, /* src_mask */
1215 0x00003fff, /* dst_mask */
1216 FALSE), /* pcrel_offset */
1217
1218 /* Relax hint for load/store sequence. */
1219 HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
1220 0, /* rightshift */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 32, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_dont,/* complain_on_overflow */
1226 nds32_elf_ignore_reloc,/* special_function */
1227 "R_NDS32_17_FIXED_RELA",/* name */
1228 FALSE, /* partial_inplace */
1229 0x0000ffff, /* src_mask */
1230 0x0000ffff, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1232
1233 /* Relax hint for load/store sequence. */
1234 HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
1235 0, /* rightshift */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 32, /* bitsize */
1238 FALSE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_dont,/* complain_on_overflow */
1241 nds32_elf_ignore_reloc,/* special_function */
1242 "R_NDS32_25_FIXED_RELA",/* name */
1243 FALSE, /* partial_inplace */
1244 0x00ffffff, /* src_mask */
1245 0x00ffffff, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1247
1248 /* High 20 bits of PLT symbol offset relative to PC. */
1249 HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
1250 12, /* rightshift */
1251 2, /* size (0 = byte, 1 = short, 2 = long) */
1252 20, /* bitsize */
1253 FALSE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_dont,/* complain_on_overflow */
1256 bfd_elf_generic_reloc, /* special_function */
1257 "R_NDS32_PLTREL_HI20", /* name */
1258 FALSE, /* partial_inplace */
1259 0x000fffff, /* src_mask */
1260 0x000fffff, /* dst_mask */
1261 FALSE), /* pcrel_offset */
1262
1263 /* Low 12 bits of PLT symbol offset relative to PC. */
1264 HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
1265 0, /* rightshift */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 12, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_dont,/* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 "R_NDS32_PLTREL_LO12", /* name */
1273 FALSE, /* partial_inplace */
1274 0x00000fff, /* src_mask */
1275 0x00000fff, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1277
1278 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1279 HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
1280 12, /* rightshift */
1281 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 20, /* bitsize */
1283 FALSE, /* pc_relative */
1284 0, /* bitpos */
1285 complain_overflow_dont,/* complain_on_overflow */
1286 bfd_elf_generic_reloc, /* special_function */
1287 "R_NDS32_PLT_GOTREL_HI20",/* name */
1288 FALSE, /* partial_inplace */
1289 0x000fffff, /* src_mask */
1290 0x000fffff, /* dst_mask */
1291 FALSE), /* pcrel_offset */
1292
1293 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1294 HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
1295 0, /* rightshift */
1296 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 12, /* bitsize */
1298 FALSE, /* pc_relative */
1299 0, /* bitpos */
1300 complain_overflow_dont,/* complain_on_overflow */
1301 bfd_elf_generic_reloc, /* special_function */
1302 "R_NDS32_PLT_GOTREL_LO12",/* name */
1303 FALSE, /* partial_inplace */
1304 0x00000fff, /* src_mask */
1305 0x00000fff, /* dst_mask */
1306 FALSE), /* pcrel_offset */
1307
1308 /* Small data area 12 bits offset. */
1309 HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
1310 2, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 12, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_signed,/* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_NDS32_SDA12S2_DP_RELA",/* name */
1318 FALSE, /* partial_inplace */
1319 0x00000fff, /* src_mask */
1320 0x00000fff, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1322
1323 /* Small data area 12 bits offset. */
1324 HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
1325 2, /* rightshift */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 12, /* bitsize */
1328 FALSE, /* pc_relative */
1329 0, /* bitpos */
1330 complain_overflow_signed,/* complain_on_overflow */
1331 bfd_elf_generic_reloc, /* special_function */
1332 "R_NDS32_SDA12S2_SP_RELA",/* name */
1333 FALSE, /* partial_inplace */
1334 0x00000fff, /* src_mask */
1335 0x00000fff, /* dst_mask */
1336 FALSE), /* pcrel_offset */
1337 /* Lower 12 bits of address. */
1338
1339 HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
1340 2, /* rightshift */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 10, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont,/* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
1347 "R_NDS32_LO12S2_DP_RELA",/* name */
1348 FALSE, /* partial_inplace */
1349 0x000003ff, /* src_mask */
1350 0x000003ff, /* dst_mask */
1351 FALSE), /* pcrel_offset */
1352
1353 /* Lower 12 bits of address. */
1354 HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
1355 2, /* rightshift */
1356 2, /* size (0 = byte, 1 = short, 2 = long) */
1357 10, /* bitsize */
1358 FALSE, /* pc_relative */
1359 0, /* bitpos */
1360 complain_overflow_dont,/* complain_on_overflow */
1361 bfd_elf_generic_reloc, /* special_function */
1362 "R_NDS32_LO12S2_SP_RELA",/* name */
1363 FALSE, /* partial_inplace */
1364 0x000003ff, /* src_mask */
1365 0x000003ff, /* dst_mask */
1366 FALSE), /* pcrel_offset */
1367 /* Lower 12 bits of address. Special identity for or case. */
1368 HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
1369 0, /* rightshift */
1370 2, /* size (0 = byte, 1 = short, 2 = long) */
1371 12, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_dont,/* complain_on_overflow */
1375 bfd_elf_generic_reloc, /* special_function */
1376 "R_NDS32_LO12S0_ORI_RELA",/* name */
1377 FALSE, /* partial_inplace */
1378 0x00000fff, /* src_mask */
1379 0x00000fff, /* dst_mask */
1380 FALSE), /* pcrel_offset */
1381 /* Small data area 19 bits offset. */
1382 HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
1383 3, /* rightshift */
1384 2, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_signed,/* complain_on_overflow */
1389 bfd_elf_generic_reloc, /* special_function */
1390 "R_NDS32_SDA16S3_RELA",/* name */
1391 FALSE, /* partial_inplace */
1392 0x0000ffff, /* src_mask */
1393 0x0000ffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1395
1396 /* Small data area 15 bits offset. */
1397 HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
1398 2, /* rightshift */
1399 2, /* size (0 = byte, 1 = short, 2 = long) */
1400 17, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_signed,/* complain_on_overflow */
1404 bfd_elf_generic_reloc, /* special_function */
1405 "R_NDS32_SDA17S2_RELA",/* name */
1406 FALSE, /* partial_inplace */
1407 0x0001ffff, /* src_mask */
1408 0x0001ffff, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1410
1411 HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
1412 1, /* rightshift */
1413 2, /* size (0 = byte, 1 = short, 2 = long) */
1414 18, /* bitsize */
1415 FALSE, /* pc_relative */
1416 0, /* bitpos */
1417 complain_overflow_signed,/* complain_on_overflow */
1418 bfd_elf_generic_reloc, /* special_function */
1419 "R_NDS32_SDA18S1_RELA",/* name */
1420 FALSE, /* partial_inplace */
1421 0x0003ffff, /* src_mask */
1422 0x0003ffff, /* dst_mask */
1423 FALSE), /* pcrel_offset */
1424
1425 HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
1426 0, /* rightshift */
1427 2, /* size (0 = byte, 1 = short, 2 = long) */
1428 19, /* bitsize */
1429 FALSE, /* pc_relative */
1430 0, /* bitpos */
1431 complain_overflow_signed,/* complain_on_overflow */
1432 bfd_elf_generic_reloc, /* special_function */
1433 "R_NDS32_SDA19S0_RELA",/* name */
1434 FALSE, /* partial_inplace */
1435 0x0007ffff, /* src_mask */
1436 0x0007ffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438 HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
1439 0, /* rightshift */
1440 0, /* size (0 = byte, 1 = short, 2 = long) */
1441 8, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont,/* complain_on_overflow */
1445 nds32_elf_ignore_reloc,/* special_function */
1446 "R_NDS32_DWARF2_OP1_RELA",/* name */
1447 FALSE, /* partial_inplace */
1448 0xff, /* src_mask */
1449 0xff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451 HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
1452 0, /* rightshift */
1453 1, /* size (0 = byte, 1 = short, 2 = long) */
1454 16, /* bitsize */
1455 FALSE, /* pc_relative */
1456 0, /* bitpos */
1457 complain_overflow_dont,/* complain_on_overflow */
1458 nds32_elf_ignore_reloc,/* special_function */
1459 "R_NDS32_DWARF2_OP2_RELA",/* name */
1460 FALSE, /* partial_inplace */
1461 0xffff, /* src_mask */
1462 0xffff, /* dst_mask */
1463 FALSE), /* pcrel_offset */
1464 HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
1465 0, /* rightshift */
1466 2, /* size (0 = byte, 1 = short, 2 = long) */
1467 32, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_dont,/* complain_on_overflow */
1471 nds32_elf_ignore_reloc,/* special_function */
1472 "R_NDS32_DWARF2_LEB_RELA",/* name */
1473 FALSE, /* partial_inplace */
1474 0xffffffff, /* src_mask */
1475 0xffffffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477 HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
1478 0, /* rightshift */
1479 1, /* size (0 = byte, 1 = short, 2 = long) */
1480 16, /* bitsize */
1481 FALSE, /* pc_relative */
1482 0, /* bitpos */
1483 complain_overflow_dont,/* complain_on_overflow */
1484 nds32_elf_ignore_reloc,/* special_function */
1485 "R_NDS32_UPDATE_TA_RELA",/* name */
1486 FALSE, /* partial_inplace */
1487 0xffff, /* src_mask */
1488 0xffff, /* dst_mask */
1489 FALSE), /* pcrel_offset */
1490 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1491 entry for the symbol. */
1492 HOWTO2 (R_NDS32_9_PLTREL, /* type */
1493 1, /* rightshift */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1495 8, /* bitsize */
1496 TRUE, /* pc_relative */
1497 0, /* bitpos */
1498 complain_overflow_signed,/* complain_on_overflow */
1499 bfd_elf_generic_reloc, /* special_function */
1500 "R_NDS32_9_PLTREL", /* name */
1501 FALSE, /* partial_inplace */
1502 0xff, /* src_mask */
1503 0xff, /* dst_mask */
1504 TRUE), /* pcrel_offset */
1505 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1506 HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
1507 0, /* rightshift */
1508 2, /* size (0 = byte, 1 = short, 2 = long) */
1509 20, /* bitsize */
1510 FALSE, /* pc_relative */
1511 0, /* bitpos */
1512 complain_overflow_dont,/* complain_on_overflow */
1513 bfd_elf_generic_reloc, /* special_function */
1514 "R_NDS32_PLT_GOTREL_LO20",/* name */
1515 FALSE, /* partial_inplace */
1516 0x000fffff, /* src_mask */
1517 0x000fffff, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1520 HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
1521 0, /* rightshift */
1522 2, /* size (0 = byte, 1 = short, 2 = long) */
1523 15, /* bitsize */
1524 FALSE, /* pc_relative */
1525 0, /* bitpos */
1526 complain_overflow_dont,/* complain_on_overflow */
1527 bfd_elf_generic_reloc, /* special_function */
1528 "R_NDS32_PLT_GOTREL_LO15",/* name */
1529 FALSE, /* partial_inplace */
1530 0x00007fff, /* src_mask */
1531 0x00007fff, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1533 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1534 HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
1535 0, /* rightshift */
1536 2, /* size (0 = byte, 1 = short, 2 = long) */
1537 19, /* bitsize */
1538 FALSE, /* pc_relative */
1539 0, /* bitpos */
1540 complain_overflow_dont,/* complain_on_overflow */
1541 bfd_elf_generic_reloc, /* special_function */
1542 "R_NDS32_PLT_GOTREL_LO19",/* name */
1543 FALSE, /* partial_inplace */
1544 0x0007ffff, /* src_mask */
1545 0x0007ffff, /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547 HOWTO2 (R_NDS32_GOT_LO15, /* type */
1548 0, /* rightshift */
1549 2, /* size (0 = byte, 1 = short, 2 = long) */
1550 15, /* bitsize */
1551 FALSE, /* pc_relative */
1552 0, /* bitpos */
1553 complain_overflow_dont,/* complain_on_overflow */
1554 bfd_elf_generic_reloc, /* special_function */
1555 "R_NDS32_GOT_LO15", /* name */
1556 FALSE, /* partial_inplace */
1557 0x00007fff, /* src_mask */
1558 0x00007fff, /* dst_mask */
1559 FALSE), /* pcrel_offset */
1560 HOWTO2 (R_NDS32_GOT_LO19, /* type */
1561 0, /* rightshift */
1562 2, /* size (0 = byte, 1 = short, 2 = long) */
1563 19, /* bitsize */
1564 FALSE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont,/* complain_on_overflow */
1567 bfd_elf_generic_reloc, /* special_function */
1568 "R_NDS32_GOT_LO19", /* name */
1569 FALSE, /* partial_inplace */
1570 0x0007ffff, /* src_mask */
1571 0x0007ffff, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1573 HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
1574 0, /* rightshift */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1576 15, /* bitsize */
1577 FALSE, /* pc_relative */
1578 0, /* bitpos */
1579 complain_overflow_dont,/* complain_on_overflow */
1580 bfd_elf_generic_reloc, /* special_function */
1581 "R_NDS32_GOTOFF_LO15", /* name */
1582 FALSE, /* partial_inplace */
1583 0x00007fff, /* src_mask */
1584 0x00007fff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586 HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
1587 0, /* rightshift */
1588 2, /* size (0 = byte, 1 = short, 2 = long) */
1589 19, /* bitsize */
1590 FALSE, /* pc_relative */
1591 0, /* bitpos */
1592 complain_overflow_dont,/* complain_on_overflow */
1593 bfd_elf_generic_reloc, /* special_function */
1594 "R_NDS32_GOTOFF_LO19", /* name */
1595 FALSE, /* partial_inplace */
1596 0x0007ffff, /* src_mask */
1597 0x0007ffff, /* dst_mask */
1598 FALSE), /* pcrel_offset */
1599 /* GOT 15 bits offset. */
1600 HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
1601 2, /* rightshift */
1602 2, /* size (0 = byte, 1 = short, 2 = long) */
1603 15, /* bitsize */
1604 FALSE, /* pc_relative */
1605 0, /* bitpos */
1606 complain_overflow_signed,/* complain_on_overflow */
1607 bfd_elf_generic_reloc, /* special_function */
1608 "R_NDS32_GOT15S2_RELA",/* name */
1609 FALSE, /* partial_inplace */
1610 0x00007fff, /* src_mask */
1611 0x00007fff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
1613 /* GOT 17 bits offset. */
1614 HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
1615 2, /* rightshift */
1616 2, /* size (0 = byte, 1 = short, 2 = long) */
1617 17, /* bitsize */
1618 FALSE, /* pc_relative */
1619 0, /* bitpos */
1620 complain_overflow_signed,/* complain_on_overflow */
1621 bfd_elf_generic_reloc, /* special_function */
1622 "R_NDS32_GOT17S2_RELA",/* name */
1623 FALSE, /* partial_inplace */
1624 0x0001ffff, /* src_mask */
1625 0x0001ffff, /* dst_mask */
1626 FALSE), /* pcrel_offset */
1627 /* A 5 bit address. */
1628 HOWTO2 (R_NDS32_5_RELA, /* type */
1629 0, /* rightshift */
1630 1, /* size (0 = byte, 1 = short, 2 = long) */
1631 5, /* bitsize */
1632 FALSE, /* pc_relative */
1633 0, /* bitpos */
1634 complain_overflow_signed,/* complain_on_overflow */
1635 bfd_elf_generic_reloc, /* special_function */
1636 "R_NDS32_5_RELA", /* name */
1637 FALSE, /* partial_inplace */
1638 0x1f, /* src_mask */
1639 0x1f, /* dst_mask */
1640 FALSE), /* pcrel_offset */
1641 HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
1642 1, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 9, /* bitsize */
1645 TRUE, /* pc_relative */
1646 0, /* bitpos */
1647 complain_overflow_unsigned,/* complain_on_overflow */
1648 bfd_elf_generic_reloc, /* special_function */
1649 "R_NDS32_10_UPCREL_RELA",/* name */
1650 FALSE, /* partial_inplace */
1651 0x1ff, /* src_mask */
1652 0x1ff, /* dst_mask */
1653 TRUE), /* pcrel_offset */
1654 HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
1655 2, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 7, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_unsigned,/* complain_on_overflow */
1661 bfd_elf_generic_reloc, /* special_function */
1662 "R_NDS32_SDA_FP7U2_RELA",/* name */
1663 FALSE, /* partial_inplace */
1664 0x0000007f, /* src_mask */
1665 0x0000007f, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667 HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
1668 1, /* rightshift */
1669 2, /* size (0 = byte, 1 = short, 2 = long) */
1670 8, /* bitsize */
1671 TRUE, /* pc_relative */
1672 0, /* bitpos */
1673 complain_overflow_signed,/* complain_on_overflow */
1674 bfd_elf_generic_reloc, /* special_function */
1675 "R_NDS32_WORD_9_PCREL_RELA",/* name */
1676 FALSE, /* partial_inplace */
1677 0xff, /* src_mask */
1678 0xff, /* dst_mask */
1679 TRUE), /* pcrel_offset */
1680 HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
1681 1, /* rightshift */
1682 2, /* size (0 = byte, 1 = short, 2 = long) */
1683 24, /* bitsize */
1684 FALSE, /* pc_relative */
1685 0, /* bitpos */
1686 complain_overflow_dont,/* complain_on_overflow */
1687 bfd_elf_generic_reloc, /* special_function */
1688 "R_NDS32_25_ABS_RELA", /* name */
1689 FALSE, /* partial_inplace */
1690 0xffffff, /* src_mask */
1691 0xffffff, /* dst_mask */
1692 FALSE), /* pcrel_offset */
1693
1694 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1695 HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
1696 1, /* rightshift */
1697 2, /* size (0 = byte, 1 = short, 2 = long) */
1698 16, /* bitsize */
1699 TRUE, /* pc_relative */
1700 0, /* bitpos */
1701 complain_overflow_signed,/* complain_on_overflow */
1702 bfd_elf_generic_reloc, /* special_function */
1703 "R_NDS32_17IFC_PCREL_RELA",/* name */
1704 FALSE, /* partial_inplace */
1705 0xffff, /* src_mask */
1706 0xffff, /* dst_mask */
1707 TRUE), /* pcrel_offset */
1708
1709 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1710 HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
1711 1, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 9, /* bitsize */
1714 TRUE, /* pc_relative */
1715 0, /* bitpos */
1716 complain_overflow_unsigned,/* complain_on_overflow */
1717 bfd_elf_generic_reloc, /* special_function */
1718 "R_NDS32_10IFCU_PCREL_RELA",/* name */
1719 FALSE, /* partial_inplace */
1720 0x1ff, /* src_mask */
1721 0x1ff, /* dst_mask */
1722 TRUE), /* pcrel_offset */
1723
1724 /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
1725 HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
1726 12, /* rightshift */
1727 2, /* size (0 = byte, 1 = short, 2 = long) */
1728 20, /* bitsize */
1729 FALSE, /* pc_relative */
1730 0, /* bitpos */
1731 complain_overflow_dont,/* complain_on_overflow */
1732 bfd_elf_generic_reloc, /* special_function */
1733 "R_NDS32_TLS_LE_HI20", /* name */
1734 FALSE, /* partial_inplace */
1735 0x000fffff, /* src_mask */
1736 0x000fffff, /* dst_mask */
1737 FALSE), /* pcrel_offset */
1738
1739 HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
1740 0, /* rightshift */
1741 2, /* size (0 = byte, 1 = short, 2 = long) */
1742 12, /* bitsize */
1743 FALSE, /* pc_relative */
1744 0, /* bitpos */
1745 complain_overflow_dont,/* complain_on_overflow */
1746 bfd_elf_generic_reloc, /* special_function */
1747 "R_NDS32_TLS_LE_LO12", /* name */
1748 FALSE, /* partial_inplace */
1749 0x00000fff, /* src_mask */
1750 0x00000fff, /* dst_mask */
1751 FALSE), /* pcrel_offset */
1752
1753 /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
1754 HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
1755 12, /* rightshift */
1756 2, /* size (0 = byte, 1 = short, 2 = long) */
1757 20, /* bitsize */
1758 FALSE, /* pc_relative */
1759 0, /* bitpos */
1760 complain_overflow_dont,/* complain_on_overflow */
1761 bfd_elf_generic_reloc, /* special_function */
1762 "R_NDS32_TLS_IE_HI20", /* name */
1763 FALSE, /* partial_inplace */
1764 0x000fffff, /* src_mask */
1765 0x000fffff, /* dst_mask */
1766 FALSE), /* pcrel_offset */
1767
1768 HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
1769 2, /* rightshift */
1770 2, /* size (0 = byte, 1 = short, 2 = long) */
1771 10, /* bitsize */
1772 FALSE, /* pc_relative */
1773 0, /* bitpos */
1774 complain_overflow_dont,/* complain_on_overflow */
1775 bfd_elf_generic_reloc, /* special_function */
1776 "R_NDS32_TLS_IE_LO12S2",/* name */
1777 FALSE, /* partial_inplace */
1778 0x000003ff, /* src_mask */
1779 0x000003ff, /* dst_mask */
1780 FALSE), /* pcrel_offset */
1781
1782 /* TLS LE TP offset relocation */
1783 HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
1784 0, /* rightshift */
1785 2, /* size (0 = byte, 1 = short, 2 = long) */
1786 32, /* bitsize */
1787 FALSE, /* pc_relative */
1788 0, /* bitpos */
1789 complain_overflow_bitfield,/* complain_on_overflow */
1790 bfd_elf_generic_reloc, /* special_function */
1791 "R_NDS32_TLS_TPOFF", /* name */
1792 FALSE, /* partial_inplace */
1793 0xffffffff, /* src_mask */
1794 0xffffffff, /* dst_mask */
1795 FALSE), /* pcrel_offset */
1796
1797 /* A 20 bit address. */
1798 HOWTO2 (R_NDS32_TLS_LE_20, /* type */
1799 0, /* rightshift */
1800 2, /* size (0 = byte, 1 = short, 2 = long) */
1801 20, /* bitsize */
1802 FALSE, /* pc_relative */
1803 0, /* bitpos */
1804 complain_overflow_signed,/* complain_on_overflow */
1805 bfd_elf_generic_reloc, /* special_function */
1806 "R_NDS32_TLS_LE_20", /* name */
1807 FALSE, /* partial_inplace */
1808 0xfffff, /* src_mask */
1809 0xfffff, /* dst_mask */
1810 FALSE), /* pcrel_offset */
1811
1812 HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
1813 0, /* rightshift */
1814 2, /* size (0 = byte, 1 = short, 2 = long) */
1815 15, /* bitsize */
1816 FALSE, /* pc_relative */
1817 0, /* bitpos */
1818 complain_overflow_signed,/* complain_on_overflow */
1819 bfd_elf_generic_reloc, /* special_function */
1820 "R_NDS32_TLS_LE_15S0", /* name */
1821 FALSE, /* partial_inplace */
1822 0x7fff, /* src_mask */
1823 0x7fff, /* dst_mask */
1824 FALSE), /* pcrel_offset */
1825 HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
1826 1, /* rightshift */
1827 2, /* size (0 = byte, 1 = short, 2 = long) */
1828 15, /* bitsize */
1829 FALSE, /* pc_relative */
1830 0, /* bitpos */
1831 complain_overflow_signed,/* complain_on_overflow */
1832 bfd_elf_generic_reloc, /* special_function */
1833 "R_NDS32_TLS_LE_15S1", /* name */
1834 FALSE, /* partial_inplace */
1835 0x7fff, /* src_mask */
1836 0x7fff, /* dst_mask */
1837 FALSE), /* pcrel_offset */
1838 HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
1839 2, /* rightshift */
1840 2, /* size (0 = byte, 1 = short, 2 = long) */
1841 15, /* bitsize */
1842 FALSE, /* pc_relative */
1843 0, /* bitpos */
1844 complain_overflow_signed,/* complain_on_overflow */
1845 bfd_elf_generic_reloc, /* special_function */
1846 "R_NDS32_TLS_LE_15S2", /* name */
1847 FALSE, /* partial_inplace */
1848 0x7fff, /* src_mask */
1849 0x7fff, /* dst_mask */
1850 FALSE), /* pcrel_offset */
1851
1852 /* Relax hint for unconditional call sequence */
1853 HOWTO2 (R_NDS32_LONGCALL4, /* type */
1854 0, /* rightshift */
1855 2, /* size (0 = byte, 1 = short, 2 = long) */
1856 32, /* bitsize */
1857 FALSE, /* pc_relative */
1858 0, /* bitpos */
1859 complain_overflow_dont,/* complain_on_overflow */
1860 nds32_elf_ignore_reloc,/* special_function */
1861 "R_NDS32_LONGCALL4", /* name */
1862 FALSE, /* partial_inplace */
1863 0xffffffff, /* src_mask */
1864 0xffffffff, /* dst_mask */
1865 FALSE), /* pcrel_offset */
1866
1867 /* Relax hint for conditional call sequence. */
1868 HOWTO2 (R_NDS32_LONGCALL5, /* type */
1869 0, /* rightshift */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1871 32, /* bitsize */
1872 FALSE, /* pc_relative */
1873 0, /* bitpos */
1874 complain_overflow_dont,/* complain_on_overflow */
1875 nds32_elf_ignore_reloc,/* special_function */
1876 "R_NDS32_LONGCALL5", /* name */
1877 FALSE, /* partial_inplace */
1878 0xffffffff, /* src_mask */
1879 0xffffffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1881
1882 /* Relax hint for conditional call sequence. */
1883 HOWTO2 (R_NDS32_LONGCALL6, /* type */
1884 0, /* rightshift */
1885 2, /* size (0 = byte, 1 = short, 2 = long) */
1886 32, /* bitsize */
1887 FALSE, /* pc_relative */
1888 0, /* bitpos */
1889 complain_overflow_dont,/* complain_on_overflow */
1890 nds32_elf_ignore_reloc,/* special_function */
1891 "R_NDS32_LONGCALL6", /* name */
1892 FALSE, /* partial_inplace */
1893 0xffffffff, /* src_mask */
1894 0xffffffff, /* dst_mask */
1895 FALSE), /* pcrel_offset */
1896
1897 /* Relax hint for unconditional branch sequence. */
1898 HOWTO2 (R_NDS32_LONGJUMP4, /* type */
1899 0, /* rightshift */
1900 2, /* size (0 = byte, 1 = short, 2 = long) */
1901 32, /* bitsize */
1902 FALSE, /* pc_relative */
1903 0, /* bitpos */
1904 complain_overflow_dont,/* complain_on_overflow */
1905 nds32_elf_ignore_reloc,/* special_function */
1906 "R_NDS32_LONGJUMP4", /* name */
1907 FALSE, /* partial_inplace */
1908 0xffffffff, /* src_mask */
1909 0xffffffff, /* dst_mask */
1910 FALSE), /* pcrel_offset */
1911
1912 /* Relax hint for conditional branch sequence. */
1913 HOWTO2 (R_NDS32_LONGJUMP5, /* type */
1914 0, /* rightshift */
1915 2, /* size (0 = byte, 1 = short, 2 = long) */
1916 32, /* bitsize */
1917 FALSE, /* pc_relative */
1918 0, /* bitpos */
1919 complain_overflow_dont,/* complain_on_overflow */
1920 nds32_elf_ignore_reloc,/* special_function */
1921 "R_NDS32_LONGJUMP5", /* name */
1922 FALSE, /* partial_inplace */
1923 0xffffffff, /* src_mask */
1924 0xffffffff, /* dst_mask */
1925 FALSE), /* pcrel_offset */
1926
1927 /* Relax hint for conditional branch sequence. */
1928 HOWTO2 (R_NDS32_LONGJUMP6, /* type */
1929 0, /* rightshift */
1930 2, /* size (0 = byte, 1 = short, 2 = long) */
1931 32, /* bitsize */
1932 FALSE, /* pc_relative */
1933 0, /* bitpos */
1934 complain_overflow_dont,/* complain_on_overflow */
1935 nds32_elf_ignore_reloc,/* special_function */
1936 "R_NDS32_LONGJUMP6", /* name */
1937 FALSE, /* partial_inplace */
1938 0xffffffff, /* src_mask */
1939 0xffffffff, /* dst_mask */
1940 FALSE), /* pcrel_offset */
1941
1942 /* Relax hint for conditional branch sequence. */
1943 HOWTO2 (R_NDS32_LONGJUMP7, /* type */
1944 0, /* rightshift */
1945 2, /* size (0 = byte, 1 = short, 2 = long) */
1946 32, /* bitsize */
1947 FALSE, /* pc_relative */
1948 0, /* bitpos */
1949 complain_overflow_dont,/* complain_on_overflow */
1950 nds32_elf_ignore_reloc,/* special_function */
1951 "R_NDS32_LONGJUMP7", /* name */
1952 FALSE, /* partial_inplace */
1953 0xffffffff, /* src_mask */
1954 0xffffffff, /* dst_mask */
1955 FALSE), /* pcrel_offset */
1956
1957 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1958 0, /* rightshift */
1959 2, /* size (0 = byte, 1 = short, 2 = long) */
1960 12, /* bitsize */
1961 FALSE, /* pc_relative */
1962 0, /* bitpos */
1963 complain_overflow_dont,/* complain_on_overflow */
1964 bfd_elf_generic_reloc, /* special_function */
1965 "R_NDS32_TLS_IE_LO12", /* name */
1966 FALSE, /* partial_inplace */
1967 0x00000fff, /* src_mask */
1968 0x00000fff, /* dst_mask */
1969 FALSE), /* pcrel_offset */
1970
1971 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1972 entry for the symbol. */
1973 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1974 12, /* rightshift */
1975 2, /* size (0 = byte, 1 = short, 2 = long) */
1976 20, /* bitsize */
1977 FALSE, /* pc_relative */
1978 0, /* bitpos */
1979 complain_overflow_dont,/* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_NDS32_TLS_IEGP_HI20",/* name */
1982 FALSE, /* partial_inplace */
1983 0x000fffff, /* src_mask */
1984 0x000fffff, /* dst_mask */
1985 FALSE), /* pcrel_offset */
1986
1987 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1988 0, /* rightshift */
1989 2, /* size (0 = byte, 1 = short, 2 = long) */
1990 12, /* bitsize */
1991 FALSE, /* pc_relative */
1992 0, /* bitpos */
1993 complain_overflow_dont,/* complain_on_overflow */
1994 bfd_elf_generic_reloc, /* special_function */
1995 "R_NDS32_TLS_IEGP_LO12",/* name */
1996 FALSE, /* partial_inplace */
1997 0x00000fff, /* src_mask */
1998 0x00000fff, /* dst_mask */
1999 FALSE), /* pcrel_offset */
2000
2001 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2002 2, /* rightshift */
2003 2, /* size (0 = byte, 1 = short, 2 = long) */
2004 10, /* bitsize */
2005 FALSE, /* pc_relative */
2006 0, /* bitpos */
2007 complain_overflow_dont,/* complain_on_overflow */
2008 bfd_elf_generic_reloc, /* special_function */
2009 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2010 FALSE, /* partial_inplace */
2011 0x000003ff, /* src_mask */
2012 0x000003ff, /* dst_mask */
2013 FALSE), /* pcrel_offset */
2014
2015 /* TLS description relocation */
2016 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2017 12, /* rightshift */
2018 2, /* size (0 = byte, 1 = short, 2 = long) */
2019 20, /* bitsize */
2020 FALSE, /* pc_relative */
2021 0, /* bitpos */
2022 complain_overflow_dont,/* complain_on_overflow */
2023 nds32_elf_hi20_reloc, /* special_function */
2024 "R_NDS32_TLS_DESC_HI20",/* name */
2025 FALSE, /* partial_inplace */
2026 0x000fffff, /* src_mask */
2027 0x000fffff, /* dst_mask */
2028 FALSE), /* pcrel_offset */
2029
2030 /* TLS GD/LD description offset high part. */
2031 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2032 12, /* rightshift */
2033 2, /* size (0 = byte, 1 = short, 2 = long) */
2034 20, /* bitsize */
2035 FALSE, /* pc_relative */
2036 0, /* bitpos */
2037 complain_overflow_dont,/* complain_on_overflow */
2038 nds32_elf_hi20_reloc, /* special_function */
2039 "R_NDS32_TLS_DESC_HI20",/* name */
2040 FALSE, /* partial_inplace */
2041 0x000fffff, /* src_mask */
2042 0x000fffff, /* dst_mask */
2043 FALSE), /* pcrel_offset */
2044
2045 /* TLS GD/LD description offset low part. */
2046 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2047 0, /* rightshift */
2048 2, /* size (0 = byte, 1 = short, 2 = long) */
2049 12, /* bitsize */
2050 FALSE, /* pc_relative */
2051 0, /* bitpos */
2052 complain_overflow_dont,/* complain_on_overflow */
2053 nds32_elf_lo12_reloc, /* special_function */
2054 "R_NDS32_TLS_DESC_LO12",/* name */
2055 FALSE, /* partial_inplace */
2056 0x00000fff, /* src_mask */
2057 0x00000fff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2059
2060 /* TLS GD/LD description offset set (movi). */
2061 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2062 0, /* rightshift */
2063 2, /* size (0 = byte, 1 = short, 2 = long) */
2064 20, /* bitsize */
2065 FALSE, /* pc_relative */
2066 0, /* bitpos */
2067 complain_overflow_signed,/* complain_on_overflow */
2068 bfd_elf_generic_reloc, /* special_function */
2069 "R_NDS32_TLS_DESC_20", /* name */
2070 FALSE, /* partial_inplace */
2071 0x000fffff, /* src_mask */
2072 0x000fffff, /* dst_mask */
2073 FALSE), /* pcrel_offset */
2074
2075 /* TLS GD/LD description offset set (lwi.gp). */
2076 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2077 2, /* rightshift */
2078 2, /* size (0 = byte, 1 = short, 2 = long) */
2079 17, /* bitsize */
2080 FALSE, /* pc_relative */
2081 0, /* bitpos */
2082 complain_overflow_signed,/* complain_on_overflow */
2083 bfd_elf_generic_reloc, /* special_function */
2084 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2085 FALSE, /* partial_inplace */
2086 0x0001ffff, /* src_mask */
2087 0x0001ffff, /* dst_mask */
2088 FALSE), /* pcrel_offset */
2089 };
2090
2091 /* Relocations used for relaxation. */
2092 #define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2093 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2094
2095 static reloc_howto_type nds32_elf_relax_howto_table[] = {
2096 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2097 0, /* rightshift */
2098 2, /* size (0 = byte, 1 = short, 2 = long) */
2099 32, /* bitsize */
2100 FALSE, /* pc_relative */
2101 0, /* bitpos */
2102 complain_overflow_dont,/* complain_on_overflow */
2103 nds32_elf_ignore_reloc,/* special_function */
2104 "R_NDS32_RELAX_ENTRY", /* name */
2105 FALSE, /* partial_inplace */
2106 0xffffffff, /* src_mask */
2107 0xffffffff, /* dst_mask */
2108 FALSE), /* pcrel_offset */
2109 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2110 0, /* rightshift */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2112 32, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_dont,/* complain_on_overflow */
2116 nds32_elf_ignore_reloc,/* special_function */
2117 "R_NDS32_GOT_SUFF", /* name */
2118 FALSE, /* partial_inplace */
2119 0xffffffff, /* src_mask */
2120 0xffffffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2123 0, /* rightshift */
2124 2, /* size (0 = byte, 1 = short, 2 = long) */
2125 32, /* bitsize */
2126 FALSE, /* pc_relative */
2127 0, /* bitpos */
2128 complain_overflow_bitfield,/* complain_on_overflow */
2129 nds32_elf_ignore_reloc,/* special_function */
2130 "R_NDS32_GOTOFF_SUFF", /* name */
2131 FALSE, /* partial_inplace */
2132 0xffffffff, /* src_mask */
2133 0xffffffff, /* dst_mask */
2134 FALSE), /* pcrel_offset */
2135 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2136 0, /* rightshift */
2137 2, /* size (0 = byte, 1 = short, 2 = long) */
2138 32, /* bitsize */
2139 FALSE, /* pc_relative */
2140 0, /* bitpos */
2141 complain_overflow_dont,/* complain_on_overflow */
2142 nds32_elf_ignore_reloc,/* special_function */
2143 "R_NDS32_PLT_GOT_SUFF",/* name */
2144 FALSE, /* partial_inplace */
2145 0xffffffff, /* src_mask */
2146 0xffffffff, /* dst_mask */
2147 FALSE), /* pcrel_offset */
2148 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2149 0, /* rightshift */
2150 2, /* size (0 = byte, 1 = short, 2 = long) */
2151 32, /* bitsize */
2152 FALSE, /* pc_relative */
2153 0, /* bitpos */
2154 complain_overflow_dont,/* complain_on_overflow */
2155 nds32_elf_ignore_reloc,/* special_function */
2156 "R_NDS32_MULCALL_SUFF",/* name */
2157 FALSE, /* partial_inplace */
2158 0xffffffff, /* src_mask */
2159 0xffffffff, /* dst_mask */
2160 FALSE), /* pcrel_offset */
2161 HOWTO3 (R_NDS32_PTR, /* type */
2162 0, /* rightshift */
2163 2, /* size (0 = byte, 1 = short, 2 = long) */
2164 32, /* bitsize */
2165 FALSE, /* pc_relative */
2166 0, /* bitpos */
2167 complain_overflow_dont,/* complain_on_overflow */
2168 nds32_elf_ignore_reloc,/* special_function */
2169 "R_NDS32_PTR", /* name */
2170 FALSE, /* partial_inplace */
2171 0xffffffff, /* src_mask */
2172 0xffffffff, /* dst_mask */
2173 FALSE), /* pcrel_offset */
2174 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2175 0, /* rightshift */
2176 2, /* size (0 = byte, 1 = short, 2 = long) */
2177 32, /* bitsize */
2178 FALSE, /* pc_relative */
2179 0, /* bitpos */
2180 complain_overflow_dont,/* complain_on_overflow */
2181 nds32_elf_ignore_reloc,/* special_function */
2182 "R_NDS32_PTR_COUNT", /* name */
2183 FALSE, /* partial_inplace */
2184 0xffffffff, /* src_mask */
2185 0xffffffff, /* dst_mask */
2186 FALSE), /* pcrel_offset */
2187 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2188 0, /* rightshift */
2189 2, /* size (0 = byte, 1 = short, 2 = long) */
2190 32, /* bitsize */
2191 FALSE, /* pc_relative */
2192 0, /* bitpos */
2193 complain_overflow_dont,/* complain_on_overflow */
2194 nds32_elf_ignore_reloc,/* special_function */
2195 "R_NDS32_PTR_RESOLVED",/* name */
2196 FALSE, /* partial_inplace */
2197 0xffffffff, /* src_mask */
2198 0xffffffff, /* dst_mask */
2199 FALSE), /* pcrel_offset */
2200 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2201 0, /* rightshift */
2202 2, /* size (0 = byte, 1 = short, 2 = long) */
2203 32, /* bitsize */
2204 FALSE, /* pc_relative */
2205 0, /* bitpos */
2206 complain_overflow_dont,/* complain_on_overflow */
2207 nds32_elf_ignore_reloc,/* special_function */
2208 "R_NDS32_PLTBLOCK", /* name */
2209 FALSE, /* partial_inplace */
2210 0xffffffff, /* src_mask */
2211 0xffffffff, /* dst_mask */
2212 FALSE), /* pcrel_offset */
2213 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2214 0, /* rightshift */
2215 2, /* size (0 = byte, 1 = short, 2 = long) */
2216 32, /* bitsize */
2217 FALSE, /* pc_relative */
2218 0, /* bitpos */
2219 complain_overflow_dont,/* complain_on_overflow */
2220 nds32_elf_ignore_reloc,/* special_function */
2221 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2222 FALSE, /* partial_inplace */
2223 0xffffffff, /* src_mask */
2224 0xffffffff, /* dst_mask */
2225 FALSE), /* pcrel_offset */
2226 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2227 0, /* rightshift */
2228 2, /* size (0 = byte, 1 = short, 2 = long) */
2229 32, /* bitsize */
2230 FALSE, /* pc_relative */
2231 0, /* bitpos */
2232 complain_overflow_dont,/* complain_on_overflow */
2233 nds32_elf_ignore_reloc,/* special_function */
2234 "R_NDS32_RELAX_REGION_END",/* name */
2235 FALSE, /* partial_inplace */
2236 0xffffffff, /* src_mask */
2237 0xffffffff, /* dst_mask */
2238 FALSE), /* pcrel_offset */
2239 HOWTO3 (R_NDS32_MINUEND, /* type */
2240 0, /* rightshift */
2241 2, /* size (0 = byte, 1 = short, 2 = long) */
2242 32, /* bitsize */
2243 FALSE, /* pc_relative */
2244 0, /* bitpos */
2245 complain_overflow_dont,/* complain_on_overflow */
2246 nds32_elf_ignore_reloc,/* special_function */
2247 "R_NDS32_MINUEND", /* name */
2248 FALSE, /* partial_inplace */
2249 0xffffffff, /* src_mask */
2250 0xffffffff, /* dst_mask */
2251 FALSE), /* pcrel_offset */
2252 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2253 0, /* rightshift */
2254 2, /* size (0 = byte, 1 = short, 2 = long) */
2255 32, /* bitsize */
2256 FALSE, /* pc_relative */
2257 0, /* bitpos */
2258 complain_overflow_dont,/* complain_on_overflow */
2259 nds32_elf_ignore_reloc,/* special_function */
2260 "R_NDS32_SUBTRAHEND", /* name */
2261 FALSE, /* partial_inplace */
2262 0xffffffff, /* src_mask */
2263 0xffffffff, /* dst_mask */
2264 FALSE), /* pcrel_offset */
2265 HOWTO3 (R_NDS32_DIFF8, /* type */
2266 0, /* rightshift */
2267 0, /* size (0 = byte, 1 = short, 2 = long) */
2268 8, /* bitsize */
2269 FALSE, /* pc_relative */
2270 0, /* bitpos */
2271 complain_overflow_dont,/* complain_on_overflow */
2272 nds32_elf_ignore_reloc,/* special_function */
2273 "R_NDS32_DIFF8", /* name */
2274 FALSE, /* partial_inplace */
2275 0x000000ff, /* src_mask */
2276 0x000000ff, /* dst_mask */
2277 FALSE), /* pcrel_offset */
2278 HOWTO3 (R_NDS32_DIFF16, /* type */
2279 0, /* rightshift */
2280 1, /* size (0 = byte, 1 = short, 2 = long) */
2281 16, /* bitsize */
2282 FALSE, /* pc_relative */
2283 0, /* bitpos */
2284 complain_overflow_dont,/* complain_on_overflow */
2285 nds32_elf_ignore_reloc,/* special_function */
2286 "R_NDS32_DIFF16", /* name */
2287 FALSE, /* partial_inplace */
2288 0x0000ffff, /* src_mask */
2289 0x0000ffff, /* dst_mask */
2290 FALSE), /* pcrel_offset */
2291 HOWTO3 (R_NDS32_DIFF32, /* type */
2292 0, /* rightshift */
2293 2, /* size (0 = byte, 1 = short, 2 = long) */
2294 32, /* bitsize */
2295 FALSE, /* pc_relative */
2296 0, /* bitpos */
2297 complain_overflow_dont,/* complain_on_overflow */
2298 nds32_elf_ignore_reloc,/* special_function */
2299 "R_NDS32_DIFF32", /* name */
2300 FALSE, /* partial_inplace */
2301 0xffffffff, /* src_mask */
2302 0xffffffff, /* dst_mask */
2303 FALSE), /* pcrel_offset */
2304 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2305 0, /* rightshift */
2306 0, /* size (0 = byte, 1 = short, 2 = long) */
2307 0, /* bitsize */
2308 FALSE, /* pc_relative */
2309 0, /* bitpos */
2310 complain_overflow_dont,/* complain_on_overflow */
2311 nds32_elf_ignore_reloc,/* special_function */
2312 "R_NDS32_DIFF_ULEB128",/* name */
2313 FALSE, /* partial_inplace */
2314 0xffffffff, /* src_mask */
2315 0xffffffff, /* dst_mask */
2316 FALSE), /* pcrel_offset */
2317 HOWTO3 (R_NDS32_DATA, /* type */
2318 0, /* rightshift */
2319 2, /* size (0 = byte, 1 = short, 2 = long) */
2320 32, /* bitsize */
2321 FALSE, /* pc_relative */
2322 0, /* bitpos */
2323 complain_overflow_dont,/* complain_on_overflow */
2324 nds32_elf_ignore_reloc,/* special_function */
2325 "R_NDS32_DATA", /* name */
2326 FALSE, /* partial_inplace */
2327 0xffffffff, /* src_mask */
2328 0xffffffff, /* dst_mask */
2329 FALSE), /* pcrel_offset */
2330 HOWTO3 (R_NDS32_TRAN, /* type */
2331 0, /* rightshift */
2332 2, /* size (0 = byte, 1 = short, 2 = long) */
2333 32, /* bitsize */
2334 FALSE, /* pc_relative */
2335 0, /* bitpos */
2336 complain_overflow_dont,/* complain_on_overflow */
2337 nds32_elf_ignore_reloc,/* special_function */
2338 "R_NDS32_TRAN", /* name */
2339 FALSE, /* partial_inplace */
2340 0xffffffff, /* src_mask */
2341 0xffffffff, /* dst_mask */
2342 FALSE), /* pcrel_offset */
2343 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2344 0, /* rightshift */
2345 2, /* size (0 = byte, 1 = short, 2 = long) */
2346 32, /* bitsize */
2347 FALSE, /* pc_relative */
2348 0, /* bitpos */
2349 complain_overflow_dont,/* complain_on_overflow */
2350 nds32_elf_ignore_reloc,/* special_function */
2351 "R_NDS32_TLS_LE_ADD", /* name */
2352 FALSE, /* partial_inplace */
2353 0xffffffff, /* src_mask */
2354 0xffffffff, /* dst_mask */
2355 FALSE), /* pcrel_offset */
2356 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2357 0, /* rightshift */
2358 2, /* size (0 = byte, 1 = short, 2 = long) */
2359 32, /* bitsize */
2360 FALSE, /* pc_relative */
2361 0, /* bitpos */
2362 complain_overflow_dont,/* complain_on_overflow */
2363 nds32_elf_ignore_reloc,/* special_function */
2364 "R_NDS32_TLS_LE_LS", /* name */
2365 FALSE, /* partial_inplace */
2366 0xffffffff, /* src_mask */
2367 0xffffffff, /* dst_mask */
2368 FALSE), /* pcrel_offset */
2369 HOWTO3 (R_NDS32_EMPTY, /* type */
2370 0, /* rightshift */
2371 2, /* size (0 = byte, 1 = short, 2 = long) */
2372 32, /* bitsize */
2373 FALSE, /* pc_relative */
2374 0, /* bitpos */
2375 complain_overflow_dont,/* complain_on_overflow */
2376 nds32_elf_ignore_reloc,/* special_function */
2377 "R_NDS32_EMPTY", /* name */
2378 FALSE, /* partial_inplace */
2379 0xffffffff, /* src_mask */
2380 0xffffffff, /* dst_mask */
2381 FALSE), /* pcrel_offset */
2382 /* TLS GD/LD description address base addition. */
2383 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2384 0, /* rightshift */
2385 2, /* size (0 = byte, 1 = short, 2 = long) */
2386 32, /* bitsize */
2387 FALSE, /* pc_relative */
2388 0, /* bitpos */
2389 complain_overflow_dont,/* complain_on_overflow */
2390 nds32_elf_ignore_reloc,/* special_function */
2391 "R_NDS32_TLS_DESC_ADD",/* name */
2392 FALSE, /* partial_inplace */
2393 0xffffffff, /* src_mask */
2394 0xffffffff, /* dst_mask */
2395 FALSE), /* pcrel_offset */
2396 /* TLS GD/LD description function load. */
2397 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2398 0, /* rightshift */
2399 2, /* size (0 = byte, 1 = short, 2 = long) */
2400 32, /* bitsize */
2401 FALSE, /* pc_relative */
2402 0, /* bitpos */
2403 complain_overflow_dont,/* complain_on_overflow */
2404 nds32_elf_ignore_reloc,/* special_function */
2405 "R_NDS32_TLS_DESC_FUNC",/* name */
2406 FALSE, /* partial_inplace */
2407 0xffffffff, /* src_mask */
2408 0xffffffff, /* dst_mask */
2409 FALSE), /* pcrel_offset */
2410 /* TLS DESC resolve function call. */
2411 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2412 0, /* rightshift */
2413 2, /* size (0 = byte, 1 = short, 2 = long) */
2414 32, /* bitsize */
2415 FALSE, /* pc_relative */
2416 0, /* bitpos */
2417 complain_overflow_dont,/* complain_on_overflow */
2418 nds32_elf_ignore_reloc,/* special_function */
2419 "R_NDS32_TLS_DESC_CALL",/* name */
2420 FALSE, /* partial_inplace */
2421 0xffffffff, /* src_mask */
2422 0xffffffff, /* dst_mask */
2423 FALSE), /* pcrel_offset */
2424 /* TLS DESC variable access. */
2425 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2426 0, /* rightshift */
2427 2, /* size (0 = byte, 1 = short, 2 = long) */
2428 32, /* bitsize */
2429 FALSE, /* pc_relative */
2430 0, /* bitpos */
2431 complain_overflow_dont,/* complain_on_overflow */
2432 nds32_elf_ignore_reloc,/* special_function */
2433 "R_NDS32_TLS_DESC_MEM",/* name */
2434 FALSE, /* partial_inplace */
2435 0xffffffff, /* src_mask */
2436 0xffffffff, /* dst_mask */
2437 FALSE), /* pcrel_offset */
2438 /* TLS GD/LD description mark (@tlsdec). */
2439 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2440 0, /* rightshift */
2441 2, /* size (0 = byte, 1 = short, 2 = long) */
2442 32, /* bitsize */
2443 FALSE, /* pc_relative */
2444 0, /* bitpos */
2445 complain_overflow_dont,/* complain_on_overflow */
2446 nds32_elf_ignore_reloc,/* special_function */
2447 "R_NDS32_REMOVE", /* name */
2448 FALSE, /* partial_inplace */
2449 0xffffffff, /* src_mask */
2450 0xffffffff, /* dst_mask */
2451 FALSE), /* pcrel_offset */
2452 /* TLS GD/LD description mark (@tlsdec). */
2453 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2454 0, /* rightshift */
2455 2, /* size (0 = byte, 1 = short, 2 = long) */
2456 32, /* bitsize */
2457 FALSE, /* pc_relative */
2458 0, /* bitpos */
2459 complain_overflow_dont,/* complain_on_overflow */
2460 nds32_elf_ignore_reloc,/* special_function */
2461 "R_NDS32_GROUP", /* name */
2462 FALSE, /* partial_inplace */
2463 0xffffffff, /* src_mask */
2464 0xffffffff, /* dst_mask */
2465 FALSE), /* pcrel_offset */
2466 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2467 0, /* rightshift */
2468 2, /* size (0 = byte, 1 = short, 2 = long) */
2469 32, /* bitsize */
2470 FALSE, /* pc_relative */
2471 0, /* bitpos */
2472 complain_overflow_dont,/* complain_on_overflow */
2473 nds32_elf_ignore_reloc,/* special_function */
2474 "R_NDS32_TLS_IEGP_LW", /* name */
2475 FALSE, /* partial_inplace */
2476 0xffffffff, /* src_mask */
2477 0xffffffff, /* dst_mask */
2478 FALSE), /* pcrel_offset */
2479 /* LA and FLSI relaxation. */
2480 HOWTO3 (R_NDS32_LSI, /* type */
2481 0, /* rightshift */
2482 2, /* size (0 = byte, 1 = short, 2 = long) */
2483 32, /* bitsize */
2484 FALSE, /* pc_relative */
2485 0, /* bitpos */
2486 complain_overflow_dont,/* complain_on_overflow */
2487 nds32_elf_ignore_reloc,/* special_function */
2488 "R_NDS32_LSI", /* name */
2489 FALSE, /* partial_inplace */
2490 0xffffffff, /* src_mask */
2491 0xffffffff, /* dst_mask */
2492 FALSE),
2493 };
2494
2495 static unsigned long dl_tlsdesc_lazy_trampoline[] =
2496 {
2497 0x46200000, /* sethi $r2,#0x0 */
2498 0x58210000, /* ori $r2,$r2,#0x0 */
2499 0x40217400, /* add $r2,$r2,$gp */
2500 0x04210000, /* lwi $r2,[$r2+#0x0] */
2501 0x46300000, /* sethi $r3,#0x0 */
2502 0x58318000, /* ori $r3,$r3,#0x0 */
2503 0x4031f400, /* add $r3,$r3,$gp */
2504 0x4a000800, /* jr $r2 */
2505 };
2506
2507 static void
2508 nds32_put_trampoline (void *contents, const unsigned long *template,
2509 unsigned count)
2510 {
2511 unsigned ix;
2512
2513 for (ix = 0; ix != count; ix++)
2514 {
2515 unsigned long insn = template[ix];
2516 bfd_putb32 (insn, (char *) contents + ix * 4);
2517 }
2518 }
2519
2520 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2521 This prototype is the same as qsort (). */
2522
2523 void
2524 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2525 int (*compar) (const void *lhs, const void *rhs))
2526 {
2527 char *ptr = (char *) base;
2528 int i, j;
2529 char *tmp = xmalloc (size);
2530
2531 /* If i is less than j, i is inserted before j.
2532
2533 |---- j ----- i --------------|
2534 \ / \ /
2535 sorted unsorted
2536 */
2537
2538 for (i = 1; i < (int) nmemb; i++)
2539 {
2540 for (j = (i - 1); j >= 0; j--)
2541 if (compar (ptr + i * size, ptr + j * size) >= 0)
2542 break;
2543
2544 j++;
2545
2546 if (i == j)
2547 continue; /* i is in order. */
2548
2549 memcpy (tmp, ptr + i * size, size);
2550 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2551 memcpy (ptr + j * size, tmp, size);
2552 }
2553 free (tmp);
2554 }
2555
2556 /* Sort relocation by r_offset.
2557
2558 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2559 algorithm. Relocations at the same r_offset must keep their order.
2560 For example, RELAX_ENTRY must be the very first relocation entry.
2561
2562 Currently, this function implements insertion-sort.
2563
2564 FIXME: If we already sort them in assembler, why bother sort them
2565 here again? */
2566
2567 static int
2568 compar_reloc (const void *lhs, const void *rhs)
2569 {
2570 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2571 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2572
2573 if (l->r_offset > r->r_offset)
2574 return 1;
2575 else if (l->r_offset == r->r_offset)
2576 return 0;
2577 else
2578 return -1;
2579 }
2580
2581 /* Functions listed below are only used for old relocs.
2582 nds32_elf_9_pcrel_reloc
2583 nds32_elf_do_9_pcrel_reloc
2584 nds32_elf_hi20_reloc
2585 nds32_elf_relocate_hi20
2586 nds32_elf_lo12_reloc
2587 nds32_elf_sda15_reloc
2588 nds32_elf_generic_reloc. */
2589
2590 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2591
2592 static bfd_reloc_status_type
2593 nds32_elf_9_pcrel_reloc (bfd * abfd,
2594 arelent * reloc_entry,
2595 asymbol * symbol,
2596 void * data,
2597 asection * input_section,
2598 bfd * output_bfd,
2599 char ** error_message ATTRIBUTE_UNUSED)
2600 {
2601 /* This part is from bfd_elf_generic_reloc. */
2602 if (output_bfd != (bfd *) NULL
2603 && (symbol->flags & BSF_SECTION_SYM) == 0
2604 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2605 {
2606 reloc_entry->address += input_section->output_offset;
2607 return bfd_reloc_ok;
2608 }
2609
2610 if (output_bfd != NULL)
2611 {
2612 /* FIXME: See bfd_perform_relocation. Is this right? */
2613 return bfd_reloc_continue;
2614 }
2615
2616 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2617 input_section,
2618 data, reloc_entry->address,
2619 symbol->section,
2620 (symbol->value
2621 + symbol->section->output_section->vma
2622 + symbol->section->output_offset),
2623 reloc_entry->addend);
2624 }
2625
2626 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2627 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2628
2629 static bfd_reloc_status_type
2630 nds32_elf_do_9_pcrel_reloc (bfd * abfd,
2631 reloc_howto_type * howto,
2632 asection * input_section,
2633 bfd_byte * data,
2634 bfd_vma offset,
2635 asection * symbol_section ATTRIBUTE_UNUSED,
2636 bfd_vma symbol_value,
2637 bfd_vma addend)
2638 {
2639 bfd_signed_vma relocation;
2640 unsigned short x;
2641 bfd_reloc_status_type status;
2642
2643 /* Sanity check the address (offset in section). */
2644 if (offset > bfd_get_section_limit (abfd, input_section))
2645 return bfd_reloc_outofrange;
2646
2647 relocation = symbol_value + addend;
2648 /* Make it pc relative. */
2649 relocation -= (input_section->output_section->vma
2650 + input_section->output_offset);
2651 /* These jumps mask off the lower two bits of the current address
2652 before doing pcrel calculations. */
2653 relocation -= (offset & -(bfd_vma) 2);
2654
2655 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2656 status = bfd_reloc_overflow;
2657 else
2658 status = bfd_reloc_ok;
2659
2660 x = bfd_getb16 (data + offset);
2661
2662 relocation >>= howto->rightshift;
2663 relocation <<= howto->bitpos;
2664 x = (x & ~howto->dst_mask)
2665 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2666
2667 bfd_putb16 ((bfd_vma) x, data + offset);
2668
2669 return status;
2670 }
2671
2672 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2673 HI20_SLO is for the add3 and load/store with displacement instructions.
2674 HI20 is for the or3 instruction.
2675 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2676 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2677 we must add one to the high 16 bytes (which will get subtracted off when
2678 the low 16 bits are added).
2679 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2680 because there is a carry from the LO12 to the HI20. Here we just save
2681 the information we need; we do the actual relocation when we see the LO12.
2682 This code is copied from the elf32-mips.c. We also support an arbitrary
2683 number of HI20 relocs to be associated with a single LO12 reloc. The
2684 assembler sorts the relocs to ensure each HI20 immediately precedes its
2685 LO12. However if there are multiple copies, the assembler may not find
2686 the real LO12 so it picks the first one it finds. */
2687
2688 struct nds32_hi20
2689 {
2690 struct nds32_hi20 *next;
2691 bfd_byte *addr;
2692 bfd_vma addend;
2693 };
2694
2695 static struct nds32_hi20 *nds32_hi20_list;
2696
2697 static bfd_reloc_status_type
2698 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2699 arelent *reloc_entry,
2700 asymbol *symbol,
2701 void *data,
2702 asection *input_section,
2703 bfd *output_bfd,
2704 char **error_message ATTRIBUTE_UNUSED)
2705 {
2706 bfd_reloc_status_type ret;
2707 bfd_vma relocation;
2708 struct nds32_hi20 *n;
2709
2710 /* This part is from bfd_elf_generic_reloc.
2711 If we're relocating, and this an external symbol, we don't want
2712 to change anything. */
2713 if (output_bfd != (bfd *) NULL
2714 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2715 {
2716 reloc_entry->address += input_section->output_offset;
2717 return bfd_reloc_ok;
2718 }
2719
2720 /* Sanity check the address (offset in section). */
2721 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2722 return bfd_reloc_outofrange;
2723
2724 ret = bfd_reloc_ok;
2725 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2726 ret = bfd_reloc_undefined;
2727
2728 if (bfd_is_com_section (symbol->section))
2729 relocation = 0;
2730 else
2731 relocation = symbol->value;
2732
2733 relocation += symbol->section->output_section->vma;
2734 relocation += symbol->section->output_offset;
2735 relocation += reloc_entry->addend;
2736
2737 /* Save the information, and let LO12 do the actual relocation. */
2738 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2739 if (n == NULL)
2740 return bfd_reloc_outofrange;
2741
2742 n->addr = (bfd_byte *) data + reloc_entry->address;
2743 n->addend = relocation;
2744 n->next = nds32_hi20_list;
2745 nds32_hi20_list = n;
2746
2747 if (output_bfd != (bfd *) NULL)
2748 reloc_entry->address += input_section->output_offset;
2749
2750 return ret;
2751 }
2752
2753 /* Handle an NDS32 ELF HI20 reloc. */
2754
2755 static void
2756 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2757 int type ATTRIBUTE_UNUSED,
2758 Elf_Internal_Rela *relhi,
2759 Elf_Internal_Rela *rello,
2760 bfd_byte *contents,
2761 bfd_vma addend)
2762 {
2763 unsigned long insn;
2764 bfd_vma addlo;
2765
2766 insn = bfd_getb32 (contents + relhi->r_offset);
2767
2768 addlo = bfd_getb32 (contents + rello->r_offset);
2769 addlo &= 0xfff;
2770
2771 addend += ((insn & 0xfffff) << 20) + addlo;
2772
2773 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2774 bfd_putb32 (insn, contents + relhi->r_offset);
2775 }
2776
2777 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2778 inplace relocation; this function exists in order to do the
2779 R_NDS32_HI20_[SU]LO relocation described above. */
2780
2781 static bfd_reloc_status_type
2782 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2783 void *data, asection *input_section, bfd *output_bfd,
2784 char **error_message)
2785 {
2786 /* This part is from bfd_elf_generic_reloc.
2787 If we're relocating, and this an external symbol, we don't want
2788 to change anything. */
2789 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2790 && reloc_entry->addend == 0)
2791 {
2792 reloc_entry->address += input_section->output_offset;
2793 return bfd_reloc_ok;
2794 }
2795
2796 if (nds32_hi20_list != NULL)
2797 {
2798 struct nds32_hi20 *l;
2799
2800 l = nds32_hi20_list;
2801 while (l != NULL)
2802 {
2803 unsigned long insn;
2804 unsigned long val;
2805 unsigned long vallo;
2806 struct nds32_hi20 *next;
2807
2808 /* Do the HI20 relocation. Note that we actually don't need
2809 to know anything about the LO12 itself, except where to
2810 find the low 12 bits of the addend needed by the LO12. */
2811 insn = bfd_getb32 (l->addr);
2812 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2813 vallo &= 0xfff;
2814 switch (reloc_entry->howto->type)
2815 {
2816 case R_NDS32_LO12S3:
2817 vallo <<= 3;
2818 break;
2819
2820 case R_NDS32_LO12S2:
2821 vallo <<= 2;
2822 break;
2823
2824 case R_NDS32_LO12S1:
2825 vallo <<= 1;
2826 break;
2827
2828 case R_NDS32_LO12S0:
2829 vallo <<= 0;
2830 break;
2831 }
2832
2833 val = ((insn & 0xfffff) << 12) + vallo;
2834 val += l->addend;
2835
2836 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2837 bfd_putb32 ((bfd_vma) insn, l->addr);
2838
2839 next = l->next;
2840 free (l);
2841 l = next;
2842 }
2843
2844 nds32_hi20_list = NULL;
2845 }
2846
2847 /* Now do the LO12 reloc in the usual way.
2848 ??? It would be nice to call bfd_elf_generic_reloc here,
2849 but we have partial_inplace set. bfd_elf_generic_reloc will
2850 pass the handling back to bfd_install_relocation which will install
2851 a section relative addend which is wrong. */
2852 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2853 input_section, output_bfd, error_message);
2854 }
2855
2856 /* Do generic partial_inplace relocation.
2857 This is a local replacement for bfd_elf_generic_reloc. */
2858
2859 static bfd_reloc_status_type
2860 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2861 asymbol *symbol, void *data, asection *input_section,
2862 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2863 {
2864 bfd_reloc_status_type ret;
2865 bfd_vma relocation;
2866 bfd_byte *inplace_address;
2867
2868 /* This part is from bfd_elf_generic_reloc.
2869 If we're relocating, and this an external symbol, we don't want
2870 to change anything. */
2871 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2872 && reloc_entry->addend == 0)
2873 {
2874 reloc_entry->address += input_section->output_offset;
2875 return bfd_reloc_ok;
2876 }
2877
2878 /* Now do the reloc in the usual way.
2879 ??? It would be nice to call bfd_elf_generic_reloc here,
2880 but we have partial_inplace set. bfd_elf_generic_reloc will
2881 pass the handling back to bfd_install_relocation which will install
2882 a section relative addend which is wrong. */
2883
2884 /* Sanity check the address (offset in section). */
2885 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2886 return bfd_reloc_outofrange;
2887
2888 ret = bfd_reloc_ok;
2889 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2890 ret = bfd_reloc_undefined;
2891
2892 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2893 relocation = 0;
2894 else
2895 relocation = symbol->value;
2896
2897 /* Only do this for a final link. */
2898 if (output_bfd == (bfd *) NULL)
2899 {
2900 relocation += symbol->section->output_section->vma;
2901 relocation += symbol->section->output_offset;
2902 }
2903
2904 relocation += reloc_entry->addend;
2905 switch (reloc_entry->howto->type)
2906 {
2907 case R_NDS32_LO12S3:
2908 relocation >>= 3;
2909 break;
2910
2911 case R_NDS32_LO12S2:
2912 relocation >>= 2;
2913 break;
2914
2915 case R_NDS32_LO12S1:
2916 relocation >>= 1;
2917 break;
2918
2919 case R_NDS32_LO12S0:
2920 default:
2921 relocation >>= 0;
2922 break;
2923 }
2924
2925 inplace_address = (bfd_byte *) data + reloc_entry->address;
2926
2927 #define DOIT(x) \
2928 x = ((x & ~reloc_entry->howto->dst_mask) | \
2929 (((x & reloc_entry->howto->src_mask) + relocation) & \
2930 reloc_entry->howto->dst_mask))
2931
2932 switch (reloc_entry->howto->size)
2933 {
2934 case 1:
2935 {
2936 short x = bfd_getb16 (inplace_address);
2937
2938 DOIT (x);
2939 bfd_putb16 ((bfd_vma) x, inplace_address);
2940 }
2941 break;
2942 case 2:
2943 {
2944 unsigned long x = bfd_getb32 (inplace_address);
2945
2946 DOIT (x);
2947 bfd_putb32 ((bfd_vma) x, inplace_address);
2948 }
2949 break;
2950 default:
2951 BFD_ASSERT (0);
2952 }
2953
2954 if (output_bfd != (bfd *) NULL)
2955 reloc_entry->address += input_section->output_offset;
2956
2957 return ret;
2958 }
2959
2960 /* Handle the R_NDS32_SDA15 reloc.
2961 This reloc is used to compute the address of objects in the small data area
2962 and to perform loads and stores from that area.
2963 The lower 15 bits are sign extended and added to the register specified
2964 in the instruction, which is assumed to point to _SDA_BASE_.
2965
2966 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2967 the access size, this must be taken care of. */
2968
2969 static bfd_reloc_status_type
2970 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2971 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2972 asection *input_section, bfd *output_bfd,
2973 char **error_message ATTRIBUTE_UNUSED)
2974 {
2975 /* This part is from bfd_elf_generic_reloc. */
2976 if (output_bfd != (bfd *) NULL
2977 && (symbol->flags & BSF_SECTION_SYM) == 0
2978 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2979 {
2980 reloc_entry->address += input_section->output_offset;
2981 return bfd_reloc_ok;
2982 }
2983
2984 if (output_bfd != NULL)
2985 {
2986 /* FIXME: See bfd_perform_relocation. Is this right? */
2987 return bfd_reloc_continue;
2988 }
2989
2990 /* FIXME: not sure what to do here yet. But then again, the linker
2991 may never call us. */
2992 abort ();
2993 }
2994
2995 /* nds32_elf_ignore_reloc is the special function for
2996 relocation types which don't need to be relocated
2997 like relaxation relocation types.
2998 This function simply return bfd_reloc_ok when it is
2999 invoked. */
3000
3001 static bfd_reloc_status_type
3002 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3003 asymbol *symbol ATTRIBUTE_UNUSED,
3004 void *data ATTRIBUTE_UNUSED, asection *input_section,
3005 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3006 {
3007 if (output_bfd != NULL)
3008 reloc_entry->address += input_section->output_offset;
3009
3010 return bfd_reloc_ok;
3011 }
3012 \f
3013
3014 /* Map BFD reloc types to NDS32 ELF reloc types. */
3015
3016 struct nds32_reloc_map_entry
3017 {
3018 bfd_reloc_code_real_type bfd_reloc_val;
3019 unsigned char elf_reloc_val;
3020 };
3021
3022 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3023 {
3024 {BFD_RELOC_NONE, R_NDS32_NONE},
3025 {BFD_RELOC_16, R_NDS32_16_RELA},
3026 {BFD_RELOC_32, R_NDS32_32_RELA},
3027 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3028 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3029
3030 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3031 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3032 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3033 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3034 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3035 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3036 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3037 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3038 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3039 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3040 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3041 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3042 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3043 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3044 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3045 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3046 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3047 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3048 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3049 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3050 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3051 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3052 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3053 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3054 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3055 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3056 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3057 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3058 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3059 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3060 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3061 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3062 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3063 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3064 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3065 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3066 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3067 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3068 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3069 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3070 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3071 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3072 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3073 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3074 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3075 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3076 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3077 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3078 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3079 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3080 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3081 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3082 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3083 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3084 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3085 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3086 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3087 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3088 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3089 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3090 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3091 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3092 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3093 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3094 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3095 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3096 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3097 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3098 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3099 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3100 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3101 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3102 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3103 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3104 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3105 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3106 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3107 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3108 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3109 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3110 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3111 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3112 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3113 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3114 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3115 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3116 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3117 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3118 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3119 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3120 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3121 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3122 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3123 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3124 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3125 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3126 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3127 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3128 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3129 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3130 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3131 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3132 /* Not sure. */
3133 {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3134 /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
3135 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3136 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3137 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3138 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3139 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3140 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3141 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3142 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3143 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3144 {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3145 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3146 {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3147 {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3148 {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3149 {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3150 {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3151 {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3152 {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3153 {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3154 {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3155 {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3156 {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3157 {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3158 {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3159 {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3160 {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3161 {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3162 };
3163
3164 /* Patch tag. */
3165
3166 static reloc_howto_type *
3167 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3168 const char *r_name)
3169 {
3170 unsigned int i;
3171
3172 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3173 if (nds32_elf_howto_table[i].name != NULL
3174 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3175 return &nds32_elf_howto_table[i];
3176
3177 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3178 if (nds32_elf_relax_howto_table[i].name != NULL
3179 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3180 return &nds32_elf_relax_howto_table[i];
3181
3182 return NULL;
3183 }
3184
3185 static reloc_howto_type *
3186 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3187 {
3188 if (code < R_NDS32_RELAX_ENTRY)
3189 {
3190 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3191 return &nds32_elf_howto_table[code];
3192 }
3193 else
3194 {
3195 if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3196 >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3197 {
3198 int i = code;
3199 i += 1;
3200 }
3201
3202 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3203 < ARRAY_SIZE (nds32_elf_relax_howto_table));
3204 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3205 }
3206 }
3207
3208 static reloc_howto_type *
3209 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3210 bfd_reloc_code_real_type code)
3211 {
3212 unsigned int i;
3213
3214 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3215 {
3216 if (nds32_reloc_map[i].bfd_reloc_val == code)
3217 return bfd_elf32_bfd_reloc_type_table_lookup
3218 (nds32_reloc_map[i].elf_reloc_val);
3219 }
3220
3221 return NULL;
3222 }
3223
3224 /* Set the howto pointer for an NDS32 ELF reloc. */
3225
3226 static bfd_boolean
3227 nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3228 Elf_Internal_Rela *dst)
3229 {
3230 enum elf_nds32_reloc_type r_type;
3231
3232 r_type = ELF32_R_TYPE (dst->r_info);
3233 if (r_type > R_NDS32_GNU_VTENTRY)
3234 {
3235 /* xgettext:c-format */
3236 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3237 abfd, r_type);
3238 bfd_set_error (bfd_error_bad_value);
3239 return FALSE;
3240 }
3241
3242 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
3243 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3244 return TRUE;
3245 }
3246
3247 static bfd_boolean
3248 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3249 Elf_Internal_Rela *dst)
3250 {
3251 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3252
3253 if ((r_type == R_NDS32_NONE)
3254 || ((r_type > R_NDS32_GNU_VTENTRY)
3255 && (r_type < R_NDS32_max)))
3256 {
3257 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3258 return TRUE;
3259 }
3260
3261 /* xgettext:c-format */
3262 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3263 bfd_set_error (bfd_error_bad_value);
3264 return FALSE;
3265 }
3266
3267 /* Support for core dump NOTE sections.
3268 Reference to include/linux/elfcore.h in Linux. */
3269
3270 static bfd_boolean
3271 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3272 {
3273 int offset;
3274 size_t size;
3275
3276 switch (note->descsz)
3277 {
3278 case 0x114:
3279 /* Linux/NDS32 32-bit, ABI1. */
3280
3281 /* pr_cursig */
3282 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3283
3284 /* pr_pid */
3285 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3286
3287 /* pr_reg */
3288 offset = 72;
3289 size = 200;
3290 break;
3291
3292 case 0xfc:
3293 /* Linux/NDS32 32-bit. */
3294
3295 /* pr_cursig */
3296 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3297
3298 /* pr_pid */
3299 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3300
3301 /* pr_reg */
3302 offset = 72;
3303 size = 176;
3304 break;
3305
3306 default:
3307 return FALSE;
3308 }
3309
3310 /* Make a ".reg" section. */
3311 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3312 size, note->descpos + offset);
3313 }
3314
3315 static bfd_boolean
3316 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3317 {
3318 switch (note->descsz)
3319 {
3320 case 124:
3321 /* Linux/NDS32. */
3322
3323 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3324 elf_tdata (abfd)->core->program =
3325 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3326 elf_tdata (abfd)->core->command =
3327 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3328 break;
3329
3330 default:
3331 return FALSE;
3332 }
3333
3334 /* Note that for some reason, a spurious space is tacked
3335 onto the end of the args in some (at least one anyway)
3336 implementations, so strip it off if it exists. */
3337 {
3338 char *command = elf_tdata (abfd)->core->command;
3339 int n = strlen (command);
3340
3341 if (0 < n && command[n - 1] == ' ')
3342 command[n - 1] = '\0';
3343 }
3344
3345 return TRUE;
3346 }
3347
3348 /* Hook called by the linker routine which adds symbols from an object
3349 file. We must handle the special NDS32 section numbers here.
3350 We also keep watching for whether we need to create the sdata special
3351 linker sections. */
3352
3353 static bfd_boolean
3354 nds32_elf_add_symbol_hook (bfd *abfd,
3355 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3356 Elf_Internal_Sym *sym,
3357 const char **namep ATTRIBUTE_UNUSED,
3358 flagword *flagsp ATTRIBUTE_UNUSED,
3359 asection **secp, bfd_vma *valp)
3360 {
3361 switch (sym->st_shndx)
3362 {
3363 case SHN_COMMON:
3364 /* Common symbols less than the GP size are automatically
3365 treated as SHN_MIPS_SCOMMON symbols. */
3366 if (sym->st_size > elf_gp_size (abfd)
3367 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3368 break;
3369
3370 /* st_value is the alignemnt constraint.
3371 That might be its actual size if it is an array or structure. */
3372 switch (sym->st_value)
3373 {
3374 case 1:
3375 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3376 break;
3377 case 2:
3378 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3379 break;
3380 case 4:
3381 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3382 break;
3383 case 8:
3384 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3385 break;
3386 default:
3387 return TRUE;
3388 }
3389
3390 (*secp)->flags |= SEC_IS_COMMON;
3391 *valp = sym->st_size;
3392 break;
3393 }
3394
3395 return TRUE;
3396 }
3397
3398 /* This function can figure out the best location for a base register to access
3399 data relative to this base register
3400 INPUT:
3401 sda_d0: size of first DOUBLE WORD data section
3402 sda_w0: size of first WORD data section
3403 sda_h0: size of first HALF WORD data section
3404 sda_b : size of BYTE data section
3405 sda_hi: size of second HALF WORD data section
3406 sda_w1: size of second WORD data section
3407 sda_d1: size of second DOUBLE WORD data section
3408 OUTPUT:
3409 offset (always positive) from the beginning of sda_d0 if OK
3410 a negative error value if fail
3411 NOTE:
3412 these 7 sections have to be located back to back if exist
3413 a pass in 0 value for non-existing section */
3414
3415 /* Due to the interpretation of simm15 field of load/store depending on
3416 data accessing size, the organization of base register relative data shall
3417 like the following figure
3418 -------------------------------------------
3419 | DOUBLE WORD sized data (range +/- 128K)
3420 -------------------------------------------
3421 | WORD sized data (range +/- 64K)
3422 -------------------------------------------
3423 | HALF WORD sized data (range +/- 32K)
3424 -------------------------------------------
3425 | BYTE sized data (range +/- 16K)
3426 -------------------------------------------
3427 | HALF WORD sized data (range +/- 32K)
3428 -------------------------------------------
3429 | WORD sized data (range +/- 64K)
3430 -------------------------------------------
3431 | DOUBLE WORD sized data (range +/- 128K)
3432 -------------------------------------------
3433 Its base register shall be set to access these data freely. */
3434
3435 /* We have to figure out the SDA_BASE value, so that we can adjust the
3436 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3437 BFD. If we can't find it, we're stuck. We cache it in the ELF
3438 target data. We don't need to adjust the symbol value for an
3439 external symbol if we are producing relocatable output. */
3440
3441 static asection *sda_rela_sec = NULL;
3442
3443 #define SDA_SECTION_NUM 10
3444
3445 static bfd_reloc_status_type
3446 nds32_elf_final_sda_base (bfd * output_bfd,
3447 struct bfd_link_info * info,
3448 bfd_vma * psb,
3449 bfd_boolean add_symbol)
3450 {
3451 int relax_fp_as_gp;
3452 struct elf_nds32_link_hash_table *table;
3453 struct bfd_link_hash_entry *h, *h2;
3454 long unsigned int total = 0;
3455 asection *first = NULL, *final = NULL, *temp;
3456 bfd_vma sda_base = 0;
3457
3458 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3459 if (!h || (h->type != bfd_link_hash_defined
3460 && h->type != bfd_link_hash_defweak))
3461 {
3462 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3463 4 byte-aligned. Therefore, it has to set the first section ".data"
3464 4 byte-aligned. */
3465 static const char sec_name[SDA_SECTION_NUM][10] =
3466 {
3467 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3468 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3469 };
3470 size_t i = 0;
3471
3472 if (output_bfd->sections == NULL)
3473 {
3474 *psb = elf_gp (output_bfd);
3475 return bfd_reloc_ok;
3476 }
3477
3478 /* Get the first and final section. */
3479 while (i < ARRAY_SIZE (sec_name))
3480 {
3481 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3482 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3483 first = temp;
3484 if (temp && (temp->size != 0 || temp->rawsize != 0))
3485 final = temp;
3486
3487 /* Summarize the sections in order to check if joining .bss. */
3488 if (temp && temp->size != 0)
3489 total += temp->size;
3490 else if (temp && temp->rawsize != 0)
3491 total += temp->rawsize;
3492
3493 i++;
3494 }
3495
3496 /* Check .bss size. */
3497 temp = bfd_get_section_by_name (output_bfd, ".bss");
3498 if (temp)
3499 {
3500 if (temp->size != 0)
3501 total += temp->size;
3502 else if (temp->rawsize != 0)
3503 total += temp->rawsize;
3504
3505 if (total < 0x80000)
3506 {
3507 if (!first && (temp->size != 0 || temp->rawsize != 0))
3508 first = temp;
3509 if ((temp->size != 0 || temp->rawsize != 0))
3510 final = temp;
3511 }
3512 }
3513
3514 if (first && final)
3515 {
3516 /* The middle of data region. */
3517 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3518
3519 /* Find the section sda_base located. */
3520 i = 0;
3521 while (i < ARRAY_SIZE (sec_name))
3522 {
3523 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3524 if (final && (final->size != 0 || final->rawsize != 0)
3525 && sda_base >= final->vma)
3526 {
3527 first = final;
3528 i++;
3529 }
3530 else
3531 break;
3532 }
3533 }
3534 else
3535 {
3536 /* If there is not any default data section in output bfd, try to find
3537 the first data section. If no data section be found, just simplily
3538 choose the first output section. */
3539 temp = output_bfd->sections;
3540 while (temp)
3541 {
3542 if (temp->flags & SEC_ALLOC
3543 && (((temp->flags & SEC_DATA)
3544 && ((temp->flags & SEC_READONLY) == 0))
3545 || (temp->flags & SEC_LOAD) == 0)
3546 && (temp->size != 0 || temp->rawsize != 0))
3547 {
3548 if (!first)
3549 first = temp;
3550 final = temp;
3551 }
3552 temp = temp->next;
3553 }
3554
3555 /* There is no data or bss section. */
3556 if (!first || (first->size == 0 && first->rawsize == 0))
3557 {
3558 first = output_bfd->sections;
3559 while (first && first->size == 0 && first->rawsize == 0)
3560 first = first->next;
3561 }
3562
3563 /* There is no concrete section. */
3564 if (!first)
3565 {
3566 *psb = elf_gp (output_bfd);
3567 return bfd_reloc_ok;
3568 }
3569
3570 if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3571 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3572 else
3573 sda_base = first->vma + 0x2000;
3574 }
3575
3576 sda_base -= first->vma;
3577 sda_base = sda_base & (~7);
3578
3579 if (!_bfd_generic_link_add_one_symbol
3580 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3581 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3582 get_elf_backend_data (output_bfd)->collect, &h))
3583 return FALSE;
3584
3585 sda_rela_sec = first;
3586 }
3587
3588 /* Set _FP_BASE_ to _SDA_BASE_. */
3589 table = nds32_elf_hash_table (info);
3590 relax_fp_as_gp = table->relax_fp_as_gp;
3591 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3592 /* _SDA_BASE_ is difined in linker script. */
3593 if (!first)
3594 {
3595 first = h->u.def.section;
3596 sda_base = h->u.def.value;
3597 }
3598
3599 if (relax_fp_as_gp && h2
3600 && (h2->type == bfd_link_hash_undefweak
3601 || h2->type == bfd_link_hash_undefined))
3602 {
3603 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3604 And set FP equal to SDA_BASE to do relaxation for
3605 la $fp, _FP_BASE_. */
3606 if (!_bfd_generic_link_add_one_symbol
3607 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3608 first, sda_base, (const char *) NULL,
3609 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3610 return FALSE;
3611 }
3612
3613 if (add_symbol)
3614 {
3615 if (h)
3616 {
3617 /* Now set gp. */
3618 elf_gp (output_bfd) = (h->u.def.value
3619 + h->u.def.section->output_section->vma
3620 + h->u.def.section->output_offset);
3621 }
3622 else
3623 {
3624 _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3625 return bfd_reloc_dangerous;
3626 }
3627 }
3628
3629 *psb = h->u.def.value
3630 + h->u.def.section->output_section->vma
3631 + h->u.def.section->output_offset;
3632 return bfd_reloc_ok;
3633 }
3634 \f
3635
3636 /* Return size of a PLT entry. */
3637 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3638
3639 /* Create an entry in an nds32 ELF linker hash table. */
3640
3641 static struct bfd_hash_entry *
3642 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3643 struct bfd_hash_table *table,
3644 const char *string)
3645 {
3646 struct elf_nds32_link_hash_entry *ret;
3647
3648 ret = (struct elf_nds32_link_hash_entry *) entry;
3649
3650 /* Allocate the structure if it has not already been allocated by a
3651 subclass. */
3652 if (ret == NULL)
3653 ret = (struct elf_nds32_link_hash_entry *)
3654 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3655
3656 if (ret == NULL)
3657 return (struct bfd_hash_entry *) ret;
3658
3659 /* Call the allocation method of the superclass. */
3660 ret = (struct elf_nds32_link_hash_entry *)
3661 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3662
3663 if (ret != NULL)
3664 {
3665 struct elf_nds32_link_hash_entry *eh;
3666
3667 eh = (struct elf_nds32_link_hash_entry *) ret;
3668 eh->dyn_relocs = NULL;
3669 eh->tls_type = GOT_UNKNOWN;
3670 eh->offset_to_gp = 0;
3671 }
3672
3673 return (struct bfd_hash_entry *) ret;
3674 }
3675
3676 /* Create an nds32 ELF linker hash table. */
3677
3678 static struct bfd_link_hash_table *
3679 nds32_elf_link_hash_table_create (bfd *abfd)
3680 {
3681 struct elf_nds32_link_hash_table *ret;
3682
3683 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3684
3685 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3686 if (ret == NULL)
3687 return NULL;
3688
3689 /* Patch tag. */
3690 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3691 nds32_elf_link_hash_newfunc,
3692 sizeof (struct elf_nds32_link_hash_entry),
3693 NDS32_ELF_DATA))
3694 {
3695 free (ret);
3696 return NULL;
3697 }
3698
3699 ret->sdynbss = NULL;
3700 ret->srelbss = NULL;
3701 ret->sym_ld_script = NULL;
3702
3703 return &ret->root.root;
3704 }
3705
3706 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3707 shortcuts to them in our hash table. */
3708
3709 static bfd_boolean
3710 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3711 {
3712 struct elf_link_hash_table *ehtab;
3713
3714 if (!_bfd_elf_create_got_section (dynobj, info))
3715 return FALSE;
3716
3717 ehtab = elf_hash_table (info);
3718 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3719 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3720 if (!ehtab->sgot || !ehtab->sgotplt)
3721 abort ();
3722
3723 /* _bfd_elf_create_got_section will create it for us. */
3724 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3725 if (ehtab->srelgot == NULL
3726 || !bfd_set_section_flags (ehtab->srelgot,
3727 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3728 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3729 | SEC_READONLY))
3730 || !bfd_set_section_alignment (ehtab->srelgot, 2))
3731 return FALSE;
3732
3733 return TRUE;
3734 }
3735
3736 /* Create dynamic sections when linking against a dynamic object. */
3737
3738 static bfd_boolean
3739 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3740 {
3741 struct elf_link_hash_table *ehtab;
3742 struct elf_nds32_link_hash_table *htab;
3743 flagword flags, pltflags;
3744 register asection *s;
3745 const struct elf_backend_data *bed;
3746 int ptralign = 2; /* 32-bit */
3747 const char *secname;
3748 char *relname;
3749 flagword secflags;
3750 asection *sec;
3751
3752 bed = get_elf_backend_data (abfd);
3753 ehtab = elf_hash_table (info);
3754 htab = nds32_elf_hash_table (info);
3755
3756 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3757 .rel[a].bss sections. */
3758
3759 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3760 | SEC_LINKER_CREATED);
3761
3762 pltflags = flags;
3763 pltflags |= SEC_CODE;
3764 if (bed->plt_not_loaded)
3765 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3766 if (bed->plt_readonly)
3767 pltflags |= SEC_READONLY;
3768
3769 s = bfd_make_section (abfd, ".plt");
3770 ehtab->splt = s;
3771 if (s == NULL
3772 || !bfd_set_section_flags (s, pltflags)
3773 || !bfd_set_section_alignment (s, bed->plt_alignment))
3774 return FALSE;
3775
3776 if (bed->want_plt_sym)
3777 {
3778 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3779 .plt section. */
3780 struct bfd_link_hash_entry *bh = NULL;
3781 struct elf_link_hash_entry *h;
3782
3783 if (!(_bfd_generic_link_add_one_symbol
3784 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3785 (bfd_vma) 0, (const char *) NULL, FALSE,
3786 get_elf_backend_data (abfd)->collect, &bh)))
3787 return FALSE;
3788
3789 h = (struct elf_link_hash_entry *) bh;
3790 h->def_regular = 1;
3791 h->type = STT_OBJECT;
3792
3793 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3794 return FALSE;
3795 }
3796
3797 s = bfd_make_section (abfd,
3798 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3799 ehtab->srelplt = s;
3800 if (s == NULL
3801 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3802 || !bfd_set_section_alignment (s, ptralign))
3803 return FALSE;
3804
3805 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3806 return FALSE;
3807
3808 for (sec = abfd->sections; sec; sec = sec->next)
3809 {
3810 secflags = bfd_section_flags (sec);
3811 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3812 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3813 continue;
3814 secname = bfd_section_name (sec);
3815 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3816 strcpy (relname, ".rela");
3817 strcat (relname, secname);
3818 if (bfd_get_section_by_name (abfd, secname))
3819 continue;
3820 s = bfd_make_section (abfd, relname);
3821 if (s == NULL
3822 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3823 || !bfd_set_section_alignment (s, ptralign))
3824 return FALSE;
3825 }
3826
3827 if (bed->want_dynbss)
3828 {
3829 /* The .dynbss section is a place to put symbols which are defined
3830 by dynamic objects, are referenced by regular objects, and are
3831 not functions. We must allocate space for them in the process
3832 image and use a R_*_COPY reloc to tell the dynamic linker to
3833 initialize them at run time. The linker script puts the .dynbss
3834 section into the .bss section of the final image. */
3835 s = bfd_make_section (abfd, ".dynbss");
3836 htab->sdynbss = s;
3837 if (s == NULL
3838 || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3839 return FALSE;
3840 /* The .rel[a].bss section holds copy relocs. This section is not
3841 normally needed. We need to create it here, though, so that the
3842 linker will map it to an output section. We can't just create it
3843 only if we need it, because we will not know whether we need it
3844 until we have seen all the input files, and the first time the
3845 main linker code calls BFD after examining all the input files
3846 (size_dynamic_sections) the input sections have already been
3847 mapped to the output sections. If the section turns out not to
3848 be needed, we can discard it later. We will never need this
3849 section when generating a shared object, since they do not use
3850 copy relocs. */
3851 if (!bfd_link_pic (info))
3852 {
3853 s = bfd_make_section (abfd, (bed->default_use_rela_p
3854 ? ".rela.bss" : ".rel.bss"));
3855 htab->srelbss = s;
3856 if (s == NULL
3857 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3858 || !bfd_set_section_alignment (s, ptralign))
3859 return FALSE;
3860 }
3861 }
3862
3863 return TRUE;
3864 }
3865
3866 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3867 static void
3868 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3869 struct elf_link_hash_entry *dir,
3870 struct elf_link_hash_entry *ind)
3871 {
3872 struct elf_nds32_link_hash_entry *edir, *eind;
3873
3874 edir = (struct elf_nds32_link_hash_entry *) dir;
3875 eind = (struct elf_nds32_link_hash_entry *) ind;
3876
3877 if (eind->dyn_relocs != NULL)
3878 {
3879 if (edir->dyn_relocs != NULL)
3880 {
3881 struct elf_dyn_relocs **pp;
3882 struct elf_dyn_relocs *p;
3883
3884 if (ind->root.type == bfd_link_hash_indirect)
3885 abort ();
3886
3887 /* Add reloc counts against the weak sym to the strong sym
3888 list. Merge any entries against the same section. */
3889 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3890 {
3891 struct elf_dyn_relocs *q;
3892
3893 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3894 if (q->sec == p->sec)
3895 {
3896 q->pc_count += p->pc_count;
3897 q->count += p->count;
3898 *pp = p->next;
3899 break;
3900 }
3901 if (q == NULL)
3902 pp = &p->next;
3903 }
3904 *pp = edir->dyn_relocs;
3905 }
3906
3907 edir->dyn_relocs = eind->dyn_relocs;
3908 eind->dyn_relocs = NULL;
3909 }
3910
3911 if (ind->root.type == bfd_link_hash_indirect)
3912 {
3913 if (dir->got.refcount <= 0)
3914 {
3915 edir->tls_type = eind->tls_type;
3916 eind->tls_type = GOT_UNKNOWN;
3917 }
3918 }
3919
3920 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3921 }
3922 \f
3923
3924 /* Find dynamic relocs for H that apply to read-only sections. */
3925
3926 static asection *
3927 readonly_dynrelocs (struct elf_link_hash_entry *h)
3928 {
3929 struct elf_dyn_relocs *p;
3930
3931 for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3932 {
3933 asection *s = p->sec->output_section;
3934
3935 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3936 return p->sec;
3937 }
3938 return NULL;
3939 }
3940
3941 /* Adjust a symbol defined by a dynamic object and referenced by a
3942 regular object. The current definition is in some section of the
3943 dynamic object, but we're not including those sections. We have to
3944 change the definition to something the rest of the link can
3945 understand. */
3946
3947 static bfd_boolean
3948 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3949 struct elf_link_hash_entry *h)
3950 {
3951 struct elf_nds32_link_hash_table *htab;
3952 bfd *dynobj;
3953 asection *s;
3954 unsigned int power_of_two;
3955
3956 dynobj = elf_hash_table (info)->dynobj;
3957
3958 /* Make sure we know what is going on here. */
3959 BFD_ASSERT (dynobj != NULL
3960 && (h->needs_plt
3961 || h->is_weakalias
3962 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3963
3964
3965 /* If this is a function, put it in the procedure linkage table. We
3966 will fill in the contents of the procedure linkage table later,
3967 when we know the address of the .got section. */
3968 if (h->type == STT_FUNC || h->needs_plt)
3969 {
3970 if (!bfd_link_pic (info)
3971 && !h->def_dynamic
3972 && !h->ref_dynamic
3973 && h->root.type != bfd_link_hash_undefweak
3974 && h->root.type != bfd_link_hash_undefined)
3975 {
3976 /* This case can occur if we saw a PLT reloc in an input
3977 file, but the symbol was never referred to by a dynamic
3978 object. In such a case, we don't actually need to build
3979 a procedure linkage table, and we can just do a PCREL
3980 reloc instead. */
3981 h->plt.offset = (bfd_vma) - 1;
3982 h->needs_plt = 0;
3983 }
3984
3985 return TRUE;
3986 }
3987 else
3988 h->plt.offset = (bfd_vma) - 1;
3989
3990 /* If this is a weak symbol, and there is a real definition, the
3991 processor independent code will have arranged for us to see the
3992 real definition first, and we can just use the same value. */
3993 if (h->is_weakalias)
3994 {
3995 struct elf_link_hash_entry *def = weakdef (h);
3996 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3997 h->root.u.def.section = def->root.u.def.section;
3998 h->root.u.def.value = def->root.u.def.value;
3999 return TRUE;
4000 }
4001
4002 /* This is a reference to a symbol defined by a dynamic object which
4003 is not a function. */
4004
4005 /* If we are creating a shared library, we must presume that the
4006 only references to the symbol are via the global offset table.
4007 For such cases we need not do anything here; the relocations will
4008 be handled correctly by relocate_section. */
4009 if (bfd_link_pic (info))
4010 return TRUE;
4011
4012 /* If there are no references to this symbol that do not use the
4013 GOT, we don't need to generate a copy reloc. */
4014 if (!h->non_got_ref)
4015 return TRUE;
4016
4017 /* If -z nocopyreloc was given, we won't generate them either. */
4018 if (0 && info->nocopyreloc)
4019 {
4020 h->non_got_ref = 0;
4021 return TRUE;
4022 }
4023
4024 /* If we don't find any dynamic relocs in read-only sections, then
4025 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
4026 if (!readonly_dynrelocs (h))
4027 {
4028 h->non_got_ref = 0;
4029 return TRUE;
4030 }
4031
4032 /* We must allocate the symbol in our .dynbss section, which will
4033 become part of the .bss section of the executable. There will be
4034 an entry for this symbol in the .dynsym section. The dynamic
4035 object will contain position independent code, so all references
4036 from the dynamic object to this symbol will go through the global
4037 offset table. The dynamic linker will use the .dynsym entry to
4038 determine the address it must put in the global offset table, so
4039 both the dynamic object and the regular object will refer to the
4040 same memory location for the variable. */
4041
4042 htab = nds32_elf_hash_table (info);
4043 s = htab->sdynbss;
4044 BFD_ASSERT (s != NULL);
4045
4046 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4047 to copy the initial value out of the dynamic object and into the
4048 runtime process image. We need to remember the offset into the
4049 .rela.bss section we are going to use. */
4050 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4051 {
4052 asection *srel;
4053
4054 srel = htab->srelbss;
4055 BFD_ASSERT (srel != NULL);
4056 srel->size += sizeof (Elf32_External_Rela);
4057 h->needs_copy = 1;
4058 }
4059
4060 /* We need to figure out the alignment required for this symbol. I
4061 have no idea how ELF linkers handle this. */
4062 power_of_two = bfd_log2 (h->size);
4063 if (power_of_two > 3)
4064 power_of_two = 3;
4065
4066 /* Apply the required alignment. */
4067 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4068 if (power_of_two > bfd_section_alignment (s))
4069 {
4070 if (!bfd_set_section_alignment (s, power_of_two))
4071 return FALSE;
4072 }
4073
4074 /* Define the symbol as being at this point in the section. */
4075 h->root.u.def.section = s;
4076 h->root.u.def.value = s->size;
4077
4078 /* Increment the section size to make room for the symbol. */
4079 s->size += h->size;
4080
4081 return TRUE;
4082 }
4083
4084 /* Allocate space in .plt, .got and associated reloc sections for
4085 dynamic relocs. */
4086
4087 static bfd_boolean
4088 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4089 {
4090 struct bfd_link_info *info;
4091 struct elf_link_hash_table *ehtab;
4092 struct elf_nds32_link_hash_table *htab;
4093 struct elf_nds32_link_hash_entry *eh;
4094 struct elf_dyn_relocs *p;
4095
4096 if (h->root.type == bfd_link_hash_indirect)
4097 return TRUE;
4098
4099 /* When warning symbols are created, they **replace** the "real"
4100 entry in the hash table, thus we never get to see the real
4101 symbol in a hash traversal. So look at it now. */
4102 if (h->root.type == bfd_link_hash_warning)
4103 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4104
4105 eh = (struct elf_nds32_link_hash_entry *) h;
4106
4107 info = (struct bfd_link_info *) inf;
4108 ehtab = elf_hash_table (info);
4109 htab = nds32_elf_hash_table (info);
4110 if (htab == NULL)
4111 return FALSE;
4112
4113 eh = (struct elf_nds32_link_hash_entry *) h;
4114
4115 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4116 && h->plt.refcount > 0
4117 && !(bfd_link_pie (info) && h->def_regular))
4118 {
4119 /* Make sure this symbol is output as a dynamic symbol.
4120 Undefined weak syms won't yet be marked as dynamic. */
4121 if (h->dynindx == -1 && !h->forced_local)
4122 {
4123 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4124 return FALSE;
4125 }
4126
4127 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4128 {
4129 asection *s = ehtab->splt;
4130
4131 /* If this is the first .plt entry, make room for the special
4132 first entry. */
4133 if (s->size == 0)
4134 s->size += PLT_ENTRY_SIZE;
4135
4136 h->plt.offset = s->size;
4137
4138 /* If this symbol is not defined in a regular file, and we are
4139 not generating a shared library, then set the symbol to this
4140 location in the .plt. This is required to make function
4141 pointers compare as equal between the normal executable and
4142 the shared library. */
4143 if (!bfd_link_pic (info) && !h->def_regular)
4144 {
4145 h->root.u.def.section = s;
4146 h->root.u.def.value = h->plt.offset;
4147 }
4148
4149 /* Make room for this entry. */
4150 s->size += PLT_ENTRY_SIZE;
4151
4152 /* We also need to make an entry in the .got.plt section, which
4153 will be placed in the .got section by the linker script. */
4154 ehtab->sgotplt->size += 4;
4155
4156 /* We also need to make an entry in the .rel.plt section. */
4157 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4158 if (htab->tls_desc_trampoline)
4159 htab->next_tls_desc_index++;
4160 }
4161 else
4162 {
4163 h->plt.offset = (bfd_vma) - 1;
4164 h->needs_plt = 0;
4165 }
4166 }
4167 else
4168 {
4169 h->plt.offset = (bfd_vma) - 1;
4170 h->needs_plt = 0;
4171 }
4172
4173 if (h->got.refcount > 0)
4174 {
4175 asection *sgot;
4176 bfd_boolean dyn;
4177 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4178
4179 /* Make sure this symbol is output as a dynamic symbol.
4180 Undefined weak syms won't yet be marked as dynamic. */
4181 if (h->dynindx == -1 && !h->forced_local)
4182 {
4183 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4184 return FALSE;
4185 }
4186
4187 sgot = elf_hash_table (info)->sgot;
4188 h->got.offset = sgot->size;
4189
4190 if (tls_type == GOT_UNKNOWN)
4191 abort ();
4192
4193 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4194 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4195 sgot->size += 4;
4196 else
4197 {
4198 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4199 if (tls_type & GOT_TLS_DESC)
4200 sgot->size += 8;
4201 }
4202
4203 dyn = htab->root.dynamic_sections_created;
4204
4205 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4206 {
4207 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4208 {
4209 /* TLS_DESC with trampoline needs a relocation slot
4210 within .rela.plt. */
4211 htab->num_tls_desc++;
4212 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4213 htab->tls_trampoline = -1;
4214 }
4215 else
4216 {
4217 /* other relocations, including TLS_DESC without trampoline, need
4218 a relocation slot within .rela.got. */
4219 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4220 }
4221 }
4222 }
4223 else
4224 h->got.offset = (bfd_vma)-1;
4225
4226 if (eh->dyn_relocs == NULL)
4227 return TRUE;
4228
4229 /* In the shared -Bsymbolic case, discard space allocated for
4230 dynamic pc-relative relocs against symbols which turn out to be
4231 defined in regular objects. For the normal shared case, discard
4232 space for pc-relative relocs that have become local due to symbol
4233 visibility changes. */
4234
4235 if (bfd_link_pic (info))
4236 {
4237 if (h->def_regular && (h->forced_local || info->symbolic))
4238 {
4239 struct elf_dyn_relocs **pp;
4240
4241 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
4242 {
4243 p->count -= p->pc_count;
4244 p->pc_count = 0;
4245 if (p->count == 0)
4246 *pp = p->next;
4247 else
4248 pp = &p->next;
4249 }
4250 }
4251 }
4252 else
4253 {
4254 /* For the non-shared case, discard space for relocs against
4255 symbols which turn out to need copy relocs or are not dynamic. */
4256
4257 if (!h->non_got_ref
4258 && ((h->def_dynamic
4259 && !h->def_regular)
4260 || (htab->root.dynamic_sections_created
4261 && (h->root.type == bfd_link_hash_undefweak
4262 || h->root.type == bfd_link_hash_undefined))))
4263 {
4264 /* Make sure this symbol is output as a dynamic symbol.
4265 Undefined weak syms won't yet be marked as dynamic. */
4266 if (h->dynindx == -1 && !h->forced_local)
4267 {
4268 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4269 return FALSE;
4270 }
4271
4272 /* If that succeeded, we know we'll be keeping all the
4273 relocs. */
4274 if (h->dynindx != -1)
4275 goto keep;
4276 }
4277
4278 eh->dyn_relocs = NULL;
4279
4280 keep:;
4281 }
4282
4283 /* Finally, allocate space. */
4284 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4285 {
4286 asection *sreloc = elf_section_data (p->sec)->sreloc;
4287 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4288 }
4289
4290 return TRUE;
4291 }
4292
4293 /* Add relocation REL to the end of relocation section SRELOC. */
4294
4295 static void
4296 elf32_nds32_add_dynreloc (bfd *output_bfd,
4297 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4298 asection *sreloc, Elf_Internal_Rela *rel)
4299 {
4300 bfd_byte *loc;
4301 if (sreloc == NULL)
4302 abort ();
4303
4304 loc = sreloc->contents;
4305 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4306 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4307 abort ();
4308
4309 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4310 }
4311
4312 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
4313 read-only sections. */
4314
4315 static bfd_boolean
4316 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
4317 {
4318 asection *sec;
4319
4320 if (h->root.type == bfd_link_hash_indirect)
4321 return TRUE;
4322
4323 sec = readonly_dynrelocs (h);
4324 if (sec != NULL)
4325 {
4326 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
4327
4328 info->flags |= DF_TEXTREL;
4329 info->callbacks->minfo
4330 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
4331 sec->owner, h->root.root.string, sec);
4332
4333 /* Not an error, just cut short the traversal. */
4334 return FALSE;
4335 }
4336 return TRUE;
4337 }
4338
4339 /* Set the sizes of the dynamic sections. */
4340
4341 static bfd_boolean
4342 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4343 struct bfd_link_info *info)
4344 {
4345 struct elf_nds32_link_hash_table *htab;
4346 bfd *dynobj;
4347 asection *s;
4348 bfd_boolean relocs;
4349 bfd_boolean plt;
4350 bfd *ibfd;
4351
4352 htab = nds32_elf_hash_table (info);
4353 if (htab == NULL)
4354 return FALSE;
4355
4356 dynobj = elf_hash_table (info)->dynobj;
4357 BFD_ASSERT (dynobj != NULL);
4358
4359 if (elf_hash_table (info)->dynamic_sections_created)
4360 {
4361 /* Set the contents of the .interp section to the interpreter. */
4362 if (bfd_link_executable (info) && !info->nointerp)
4363 {
4364 s = bfd_get_section_by_name (dynobj, ".interp");
4365 BFD_ASSERT (s != NULL);
4366 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4367 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4368 }
4369 }
4370
4371 /* Set up .got offsets for local syms, and space for local dynamic
4372 relocs. */
4373 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4374 {
4375 bfd_signed_vma *local_got;
4376 bfd_signed_vma *end_local_got;
4377 bfd_size_type locsymcount;
4378 Elf_Internal_Shdr *symtab_hdr;
4379 asection *sgot;
4380 char *local_tls_type;
4381 unsigned long symndx;
4382 bfd_vma *local_tlsdesc_gotent;
4383
4384 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4385 continue;
4386
4387 for (s = ibfd->sections; s != NULL; s = s->next)
4388 {
4389 struct elf_dyn_relocs *p;
4390
4391 for (p = ((struct elf_dyn_relocs *)
4392 elf_section_data (s)->local_dynrel);
4393 p != NULL; p = p->next)
4394 {
4395 if (!bfd_is_abs_section (p->sec)
4396 && bfd_is_abs_section (p->sec->output_section))
4397 {
4398 /* Input section has been discarded, either because
4399 it is a copy of a linkonce section or due to
4400 linker script /DISCARD/, so we'll be discarding
4401 the relocs too. */
4402 }
4403 else if (p->count != 0)
4404 {
4405 asection *sreloc = elf_section_data (p->sec)->sreloc;
4406 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4407 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4408 info->flags |= DF_TEXTREL;
4409 }
4410 }
4411 }
4412
4413 local_got = elf_local_got_refcounts (ibfd);
4414 if (!local_got)
4415 continue;
4416
4417 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4418 locsymcount = symtab_hdr->sh_info;
4419 end_local_got = local_got + locsymcount;
4420 sgot = elf_hash_table (info)->sgot;
4421 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4422 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4423 for (symndx = 0; local_got < end_local_got;
4424 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4425 {
4426 if (*local_got > 0)
4427 {
4428 int num_of_got_entry_needed = 0;
4429 *local_got = sgot->size;
4430 *local_tlsdesc_gotent = sgot->size;
4431
4432 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4433 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4434 num_of_got_entry_needed = 1;
4435 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4436 else if (*local_tls_type & GOT_TLS_DESC)
4437 num_of_got_entry_needed = 2;
4438
4439 sgot->size += (num_of_got_entry_needed << 2);
4440
4441 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4442 others need a slot in .rela.got. */
4443 if (*local_tls_type == GOT_TLS_DESC)
4444 {
4445 if (bfd_link_pic (info))
4446 {
4447 if (htab->tls_desc_trampoline)
4448 {
4449 htab->num_tls_desc++;
4450 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4451 htab->tls_trampoline = -1;
4452 }
4453 else
4454 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4455 }
4456 else
4457 {
4458 /* TLS_DESC -> TLS_LE */
4459 }
4460 }
4461 else
4462 {
4463 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4464 }
4465 }
4466 else
4467 {
4468 *local_got = (bfd_vma) -1;
4469 *local_tlsdesc_gotent = (bfd_vma) -1;
4470 }
4471 }
4472 }
4473
4474 /* Allocate global sym .plt and .got entries, and space for global
4475 sym dynamic relocs. */
4476 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4477
4478 /* For every jump slot reserved in the sgotplt, reloc_count is
4479 incremented. However, when we reserve space for TLS descriptors,
4480 it's not incremented, so in order to compute the space reserved
4481 for them, it suffices to multiply the reloc count by the jump
4482 slot size. */
4483 if (htab->tls_desc_trampoline && htab->root.srelplt)
4484 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4485
4486 if (htab->tls_trampoline)
4487 {
4488 htab->tls_trampoline = htab->root.splt->size;
4489
4490 /* If we're not using lazy TLS relocations, don't generate the
4491 PLT and GOT entries they require. */
4492 if (!(info->flags & DF_BIND_NOW))
4493 {
4494 htab->dt_tlsdesc_got = htab->root.sgot->size;
4495 htab->root.sgot->size += 4;
4496
4497 htab->dt_tlsdesc_plt = htab->root.splt->size;
4498 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4499 }
4500 }
4501
4502 /* We now have determined the sizes of the various dynamic sections.
4503 Allocate memory for them. */
4504 /* The check_relocs and adjust_dynamic_symbol entry points have
4505 determined the sizes of the various dynamic sections. Allocate
4506 memory for them. */
4507 plt = FALSE;
4508 relocs = FALSE;
4509 for (s = dynobj->sections; s != NULL; s = s->next)
4510 {
4511 if ((s->flags & SEC_LINKER_CREATED) == 0)
4512 continue;
4513
4514 if (s == htab->root.splt)
4515 {
4516 /* Strip this section if we don't need it; see the
4517 comment below. */
4518 plt = s->size != 0;
4519 }
4520 else if (s == elf_hash_table (info)->sgot)
4521 {
4522 got_size += s->size;
4523 }
4524 else if (s == elf_hash_table (info)->sgotplt)
4525 {
4526 got_size += s->size;
4527 }
4528 else if (strncmp (bfd_section_name (s), ".rela", 5) == 0)
4529 {
4530 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4531 relocs = TRUE;
4532
4533 /* We use the reloc_count field as a counter if we need
4534 to copy relocs into the output file. */
4535 s->reloc_count = 0;
4536 }
4537 else
4538 {
4539 /* It's not one of our sections, so don't allocate space. */
4540 continue;
4541 }
4542
4543 if (s->size == 0)
4544 {
4545 /* If we don't need this section, strip it from the
4546 output file. This is mostly to handle .rela.bss and
4547 .rela.plt. We must create both sections in
4548 create_dynamic_sections, because they must be created
4549 before the linker maps input sections to output
4550 sections. The linker does that before
4551 adjust_dynamic_symbol is called, and it is that
4552 function which decides whether anything needs to go
4553 into these sections. */
4554 s->flags |= SEC_EXCLUDE;
4555 continue;
4556 }
4557
4558 /* Allocate memory for the section contents. We use bfd_zalloc
4559 here in case unused entries are not reclaimed before the
4560 section's contents are written out. This should not happen,
4561 but this way if it does, we get a R_NDS32_NONE reloc instead
4562 of garbage. */
4563 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4564 if (s->contents == NULL)
4565 return FALSE;
4566 }
4567
4568
4569 if (htab->root.dynamic_sections_created)
4570 {
4571 /* Add some entries to the .dynamic section. We fill in the
4572 values later, in nds32_elf_finish_dynamic_sections, but we
4573 must add the entries now so that we get the correct size for
4574 the .dynamic section. The DT_DEBUG entry is filled in by the
4575 dynamic linker and used by the debugger. */
4576 #define add_dynamic_entry(TAG, VAL) \
4577 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4578
4579 if (bfd_link_executable (info))
4580 {
4581 if (!add_dynamic_entry (DT_DEBUG, 0))
4582 return FALSE;
4583 }
4584
4585 if (elf_hash_table (info)->splt->size != 0)
4586 {
4587 if (!add_dynamic_entry (DT_PLTGOT, 0)
4588 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4589 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4590 || !add_dynamic_entry (DT_JMPREL, 0))
4591 return FALSE;
4592 }
4593
4594 if (htab->tls_desc_trampoline && plt)
4595 {
4596 if (htab->dt_tlsdesc_plt
4597 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4598 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4599 return FALSE;
4600 }
4601
4602 if (relocs)
4603 {
4604 if (!add_dynamic_entry (DT_RELA, 0)
4605 || !add_dynamic_entry (DT_RELASZ, 0)
4606 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4607 return FALSE;
4608
4609 /* If any dynamic relocs apply to a read-only section,
4610 then we need a DT_TEXTREL entry. */
4611 if ((info->flags & DF_TEXTREL) == 0)
4612 elf_link_hash_traverse (&htab->root, maybe_set_textrel,
4613 (void *) info);
4614
4615 if ((info->flags & DF_TEXTREL) != 0)
4616 {
4617 if (!add_dynamic_entry (DT_TEXTREL, 0))
4618 return FALSE;
4619 }
4620 }
4621 }
4622 #undef add_dynamic_entry
4623
4624 return TRUE;
4625 }
4626
4627 static bfd_reloc_status_type
4628 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4629 bfd_vma relocation, bfd_byte *location)
4630 {
4631 int size;
4632 bfd_vma x = 0;
4633 bfd_reloc_status_type flag;
4634 unsigned int rightshift = howto->rightshift;
4635 unsigned int bitpos = howto->bitpos;
4636
4637 /* If the size is negative, negate RELOCATION. This isn't very
4638 general. */
4639 if (howto->size < 0)
4640 relocation = -relocation;
4641
4642 /* Get the value we are going to relocate. */
4643 size = bfd_get_reloc_size (howto);
4644 switch (size)
4645 {
4646 default:
4647 abort ();
4648 break;
4649 case 0:
4650 return bfd_reloc_ok;
4651 case 2:
4652 x = bfd_getb16 (location);
4653 break;
4654 case 4:
4655 x = bfd_getb32 (location);
4656 break;
4657 }
4658
4659 /* Check for overflow. FIXME: We may drop bits during the addition
4660 which we don't check for. We must either check at every single
4661 operation, which would be tedious, or we must do the computations
4662 in a type larger than bfd_vma, which would be inefficient. */
4663 flag = bfd_reloc_ok;
4664 if (howto->complain_on_overflow != complain_overflow_dont)
4665 {
4666 bfd_vma addrmask, fieldmask, signmask, ss;
4667 bfd_vma a, b, sum;
4668
4669 /* Get the values to be added together. For signed and unsigned
4670 relocations, we assume that all values should be truncated to
4671 the size of an address. For bitfields, all the bits matter.
4672 See also bfd_check_overflow. */
4673 fieldmask = N_ONES (howto->bitsize);
4674 signmask = ~fieldmask;
4675 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4676 a = (relocation & addrmask) >> rightshift;
4677 b = (x & howto->src_mask & addrmask) >> bitpos;
4678
4679 switch (howto->complain_on_overflow)
4680 {
4681 case complain_overflow_signed:
4682 /* If any sign bits are set, all sign bits must be set.
4683 That is, A must be a valid negative address after
4684 shifting. */
4685 signmask = ~(fieldmask >> 1);
4686 /* Fall through. */
4687
4688 case complain_overflow_bitfield:
4689 /* Much like the signed check, but for a field one bit
4690 wider. We allow a bitfield to represent numbers in the
4691 range -2**n to 2**n-1, where n is the number of bits in the
4692 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4693 can't overflow, which is exactly what we want. */
4694 ss = a & signmask;
4695 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4696 flag = bfd_reloc_overflow;
4697
4698 /* We only need this next bit of code if the sign bit of B
4699 is below the sign bit of A. This would only happen if
4700 SRC_MASK had fewer bits than BITSIZE. Note that if
4701 SRC_MASK has more bits than BITSIZE, we can get into
4702 trouble; we would need to verify that B is in range, as
4703 we do for A above. */
4704 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4705 ss >>= bitpos;
4706
4707 /* Set all the bits above the sign bit. */
4708 b = (b ^ ss) - ss;
4709
4710 /* Now we can do the addition. */
4711 sum = a + b;
4712
4713 /* See if the result has the correct sign. Bits above the
4714 sign bit are junk now; ignore them. If the sum is
4715 positive, make sure we did not have all negative inputs;
4716 if the sum is negative, make sure we did not have all
4717 positive inputs. The test below looks only at the sign
4718 bits, and it really just
4719 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4720
4721 We mask with addrmask here to explicitly allow an address
4722 wrap-around. The Linux kernel relies on it, and it is
4723 the only way to write assembler code which can run when
4724 loaded at a location 0x80000000 away from the location at
4725 which it is linked. */
4726 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4727 flag = bfd_reloc_overflow;
4728
4729 break;
4730
4731 case complain_overflow_unsigned:
4732 /* Checking for an unsigned overflow is relatively easy:
4733 trim the addresses and add, and trim the result as well.
4734 Overflow is normally indicated when the result does not
4735 fit in the field. However, we also need to consider the
4736 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4737 input is 0x80000000, and bfd_vma is only 32 bits; then we
4738 will get sum == 0, but there is an overflow, since the
4739 inputs did not fit in the field. Instead of doing a
4740 separate test, we can check for this by or-ing in the
4741 operands when testing for the sum overflowing its final
4742 field. */
4743 sum = (a + b) & addrmask;
4744 if ((a | b | sum) & signmask)
4745 flag = bfd_reloc_overflow;
4746 break;
4747
4748 default:
4749 abort ();
4750 }
4751 }
4752
4753 /* Put RELOCATION in the right bits. */
4754 relocation >>= (bfd_vma) rightshift;
4755 relocation <<= (bfd_vma) bitpos;
4756
4757 /* Add RELOCATION to the right bits of X. */
4758 /* FIXME : 090616
4759 Because the relaxation may generate duplicate relocation at one address,
4760 an addition to immediate in the instruction may cause the relocation added
4761 several times.
4762 This bug should be fixed in assembler, but a check is also needed here. */
4763 if (howto->partial_inplace)
4764 x = ((x & ~howto->dst_mask)
4765 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4766 else
4767 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4768
4769
4770 /* Put the relocated value back in the object file. */
4771 switch (size)
4772 {
4773 default:
4774 case 0:
4775 case 1:
4776 case 8:
4777 abort ();
4778 break;
4779 case 2:
4780 bfd_putb16 (x, location);
4781 break;
4782 case 4:
4783 bfd_putb32 (x, location);
4784 break;
4785 }
4786
4787 return flag;
4788 }
4789
4790 static bfd_reloc_status_type
4791 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4792 asection *input_section, bfd_byte *contents,
4793 bfd_vma address, bfd_vma value, bfd_vma addend)
4794 {
4795 bfd_vma relocation;
4796
4797 /* Sanity check the address. */
4798 if (address > bfd_get_section_limit (input_bfd, input_section))
4799 return bfd_reloc_outofrange;
4800
4801 /* This function assumes that we are dealing with a basic relocation
4802 against a symbol. We want to compute the value of the symbol to
4803 relocate to. This is just VALUE, the value of the symbol, plus
4804 ADDEND, any addend associated with the reloc. */
4805 relocation = value + addend;
4806
4807 /* If the relocation is PC relative, we want to set RELOCATION to
4808 the distance between the symbol (currently in RELOCATION) and the
4809 location we are relocating. If pcrel_offset is FALSE we do not
4810 need to subtract out the offset of the location within the
4811 section (which is just ADDRESS). */
4812 if (howto->pc_relative)
4813 {
4814 relocation -= (input_section->output_section->vma
4815 + input_section->output_offset);
4816 if (howto->pcrel_offset)
4817 relocation -= address;
4818 }
4819
4820 return nds32_relocate_contents (howto, input_bfd, relocation,
4821 contents + address);
4822 }
4823
4824 static bfd_boolean
4825 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4826 const char *name,
4827 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4828 asection *input_sec,
4829 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4830 {
4831 const char *source;
4832 FILE *sym_ld_script = NULL;
4833 struct elf_nds32_link_hash_table *table;
4834
4835 table = nds32_elf_hash_table (info);
4836 sym_ld_script = table->sym_ld_script;
4837 if (!sym_ld_script)
4838 return TRUE;
4839
4840 if (!h || !name || *name == '\0')
4841 return TRUE;
4842
4843 if (input_sec->flags & SEC_EXCLUDE)
4844 return TRUE;
4845
4846 if (!check_start_export_sym)
4847 {
4848 fprintf (sym_ld_script, "SECTIONS\n{\n");
4849 check_start_export_sym = 1;
4850 }
4851
4852 if (h->root.type == bfd_link_hash_defined
4853 || h->root.type == bfd_link_hash_defweak)
4854 {
4855 if (!h->root.u.def.section->output_section)
4856 return TRUE;
4857
4858 if (bfd_is_const_section (input_sec))
4859 source = input_sec->name;
4860 else
4861 source = input_sec->owner->filename;
4862
4863 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4864 h->root.root.string,
4865 (long) (h->root.u.def.value
4866 + h->root.u.def.section->output_section->vma
4867 + h->root.u.def.section->output_offset), source);
4868 }
4869
4870 return TRUE;
4871 }
4872
4873 /* Relocate an NDS32/D ELF section.
4874 There is some attempt to make this function usable for many architectures,
4875 both for RELA and REL type relocs, if only to serve as a learning tool.
4876
4877 The RELOCATE_SECTION function is called by the new ELF backend linker
4878 to handle the relocations for a section.
4879
4880 The relocs are always passed as Rela structures; if the section
4881 actually uses Rel structures, the r_addend field will always be
4882 zero.
4883
4884 This function is responsible for adjust the section contents as
4885 necessary, and (if using Rela relocs and generating a
4886 relocatable output file) adjusting the reloc addend as
4887 necessary.
4888
4889 This function does not have to worry about setting the reloc
4890 address or the reloc symbol index.
4891
4892 LOCAL_SYMS is a pointer to the swapped in local symbols.
4893
4894 LOCAL_SECTIONS is an array giving the section in the input file
4895 corresponding to the st_shndx field of each local symbol.
4896
4897 The global hash table entry for the global symbols can be found
4898 via elf_sym_hashes (input_bfd).
4899
4900 When generating relocatable output, this function must handle
4901 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4902 going to be the section symbol corresponding to the output
4903 section, which means that the addend must be adjusted
4904 accordingly. */
4905
4906 /* Return the base VMA address which should be subtracted from real addresses
4907 when resolving @dtpoff relocation.
4908 This is PT_TLS segment p_vaddr. */
4909
4910 /* Return the relocation value for @tpoff relocation
4911 if STT_TLS virtual address is ADDRESS. */
4912
4913 /* Return the relocation value for @gottpoff relocation
4914 if STT_TLS virtual address is ADDRESS. */
4915
4916 static bfd_vma
4917 gottpoff (struct bfd_link_info *info, bfd_vma address)
4918 {
4919 bfd_vma tp_base;
4920 bfd_vma tp_offset;
4921
4922 /* If tls_sec is NULL, we should have signalled an error already. */
4923 if (elf_hash_table (info)->tls_sec == NULL)
4924 return 0;
4925
4926 tp_base = elf_hash_table (info)->tls_sec->vma;
4927 tp_offset = address - tp_base;
4928
4929 return tp_offset;
4930 }
4931
4932 static bfd_boolean
4933 patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4934 {
4935 /* TLS_GD/TLS_LD model #1
4936 46 00 00 00 sethi $r0,#0x0
4937 58 00 00 00 ori $r0,$r0,#0x0
4938 40 00 74 00 add $r0,$r0,$gp
4939 04 10 00 00 lwi $r1,[$r0+#0x0]
4940 4b e0 04 01 jral $lp,$r1 */
4941
4942 /* TLS_GD/TLS_LD model #2
4943 46 00 00 00 sethi $r0,#0x0
4944 58 00 00 00 ori $r0,$r0,#0x0
4945 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4946 40 00 74 00 add $r0,$r0,$gp
4947 4b e0 04 01 jral $lp,$r1 */
4948
4949 /* TLS_IE model (non-PIC)
4950 46 00 00 00 sethi $r0,#0x0
4951 04 00 00 00 lwi $r0,[$r0+#0x0]
4952 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4953
4954 /* TLS_IE model (PIC)
4955 46 00 00 00 sethi $r0,#0x0
4956 58 00 00 00 ori $r0,$r0,#0x0
4957 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4958 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4959
4960 /* TLS_GD_TO_IE model
4961 46 00 00 00 sethi $r0,#0x0
4962 58 00 00 00 ori $r0,$r0,#0x0
4963 40 00 74 00 add $r0,$rM,$gp
4964 04 00 00 01 lwi $r0,[$r0+#0x4]
4965 40 00 64 00 add $r0,$r0,$r25 */
4966
4967 bfd_boolean rz = FALSE;
4968
4969 typedef struct
4970 {
4971 uint32_t opcode;
4972 uint32_t mask;
4973 } pat_t;
4974
4975 uint32_t patch[3] =
4976 {
4977 0x40007400, /* add $r0,$rM,$gp */
4978 0x04000001, /* lwi $r0,[$r0+#0x4] */
4979 0x40006400, /* add $r0,$r0,$r25 */
4980 };
4981
4982 pat_t mode0[3] =
4983 {
4984 { 0x40000000, 0xfe0003ff },
4985 { 0x04000000, 0xfe000000 },
4986 { 0x4be00001, 0xffff83ff },
4987 };
4988
4989 pat_t mode1[3] =
4990 {
4991 { 0x38007402, 0xfe007fff },
4992 { 0x40007400, 0xfe007fff },
4993 { 0x4be00001, 0xffff83ff },
4994 };
4995
4996 unsigned char *p = contents + rel->r_offset;
4997
4998 uint32_t insn;
4999 uint32_t regidx = 0;
5000 insn = bfd_getb32 (p);
5001 if (INSN_SETHI == (0xfe0fffffu & insn))
5002 {
5003 regidx = 0x1f & (insn >> 20);
5004 p += 4;
5005 }
5006
5007 insn = bfd_getb32 (p);
5008 if (INSN_ORI == (0xfe007fffu & insn))
5009 {
5010 regidx = 0x1f & (insn >> 20);
5011 p += 4;
5012 }
5013
5014 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
5015 {
5016 /* already patched? */
5017 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
5018 (patch[1] == bfd_getb32 (p + 4)))
5019 rz = TRUE;
5020 }
5021 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
5022 {
5023 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
5024 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
5025 {
5026 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5027 bfd_putb32 (patch[1], p + 4);
5028 bfd_putb32 (patch[2], p + 8);
5029 rz = TRUE;
5030 }
5031 }
5032 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
5033 {
5034 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
5035 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5036 {
5037 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5038 bfd_putb32 (patch[1], p + 4);
5039 bfd_putb32 (patch[2], p + 8);
5040 rz = TRUE;
5041 }
5042 }
5043
5044 if (!rz)
5045 {
5046 printf ("%s: %s @ 0x%08x\n", __func__, ibfd->filename,
5047 (int) rel->r_offset);
5048 BFD_ASSERT(0); /* Unsupported pattern. */
5049 }
5050
5051 return rz;
5052 }
5053
5054 static enum elf_nds32_tls_type
5055 get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5056
5057 static unsigned int
5058 ones32 (register unsigned int x)
5059 {
5060 /* 32-bit recursive reduction using SWAR...
5061 but first step is mapping 2-bit values
5062 into sum of 2 1-bit values in sneaky way. */
5063 x -= ((x >> 1) & 0x55555555);
5064 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5065 x = (((x >> 4) + x) & 0x0f0f0f0f);
5066 x += (x >> 8);
5067 x += (x >> 16);
5068 return (x & 0x0000003f);
5069 }
5070
5071 #if !HAVE_FLS
5072 static unsigned int
5073 fls (register unsigned int x)
5074 {
5075 return ffs (x & (-x));
5076 }
5077 #endif /* !HAVE_FLS */
5078
5079 #define nds32_elf_local_tlsdesc_gotent(bfd) \
5080 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5081
5082 static bfd_boolean
5083 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
5084 struct bfd_link_info * info,
5085 bfd * input_bfd,
5086 asection * input_section,
5087 bfd_byte * contents,
5088 Elf_Internal_Rela * relocs,
5089 Elf_Internal_Sym * local_syms,
5090 asection ** local_sections)
5091 {
5092 Elf_Internal_Shdr *symtab_hdr;
5093 struct elf_link_hash_entry **sym_hashes;
5094 Elf_Internal_Rela *rel, *relend;
5095 bfd_boolean ret = TRUE; /* Assume success. */
5096 int align = 0;
5097 bfd_reloc_status_type r;
5098 const char *errmsg = NULL;
5099 bfd_vma gp;
5100 struct elf_link_hash_table *ehtab;
5101 struct elf_nds32_link_hash_table *htab;
5102 bfd *dynobj;
5103 bfd_vma *local_got_offsets;
5104 asection *sgot, *splt, *sreloc;
5105 bfd_vma high_address;
5106 struct elf_nds32_link_hash_table *table;
5107 int eliminate_gc_relocs;
5108 bfd_vma fpbase_addr;
5109
5110 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5111 sym_hashes = elf_sym_hashes (input_bfd);
5112 ehtab = elf_hash_table (info);
5113 htab = nds32_elf_hash_table (info);
5114 high_address = bfd_get_section_limit (input_bfd, input_section);
5115
5116 dynobj = htab->root.dynobj;
5117 local_got_offsets = elf_local_got_offsets (input_bfd);
5118
5119 sgot = ehtab->sgot;
5120 splt = ehtab->splt;
5121 sreloc = NULL;
5122
5123 rel = relocs;
5124 relend = relocs + input_section->reloc_count;
5125
5126 table = nds32_elf_hash_table (info);
5127 eliminate_gc_relocs = table->eliminate_gc_relocs;
5128
5129 /* By this time, we can adjust the value of _SDA_BASE_. */
5130 /* Explain _SDA_BASE_ */
5131 if ((!bfd_link_relocatable (info)))
5132 {
5133 is_SDA_BASE_set = 1;
5134 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5135 if (r != bfd_reloc_ok)
5136 return FALSE;
5137 }
5138
5139 /* Do TLS model conversion once at first. */
5140 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5141
5142 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5143 the fp value is set as gp, and it has be reverted for instruction
5144 setting fp. */
5145 fpbase_addr = elf_gp (output_bfd);
5146
5147 /* Deal with (dynamic) relocations. */
5148 for (rel = relocs; rel < relend; rel++)
5149 {
5150 enum elf_nds32_reloc_type r_type;
5151 reloc_howto_type *howto = NULL;
5152 unsigned long r_symndx;
5153 struct elf_link_hash_entry *h = NULL;
5154 Elf_Internal_Sym *sym = NULL;
5155 asection *sec;
5156 bfd_vma relocation;
5157 bfd_vma relocation_sym = 0xdeadbeef;
5158 Elf_Internal_Rela *lorel;
5159 bfd_vma off;
5160
5161 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5162 ensure it's zero (we use REL relocs, not RELA). Therefore this
5163 should be assigning zero to `addend', but for clarity we use
5164 `r_addend'. */
5165
5166 bfd_vma addend = rel->r_addend;
5167 bfd_vma offset = rel->r_offset;
5168
5169 r_type = ELF32_R_TYPE (rel->r_info);
5170 if (r_type >= R_NDS32_max)
5171 {
5172 /* xgettext:c-format */
5173 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5174 input_bfd, r_type);
5175 bfd_set_error (bfd_error_bad_value);
5176 ret = FALSE;
5177 continue;
5178 }
5179
5180 if (r_type == R_NDS32_GNU_VTENTRY
5181 || r_type == R_NDS32_GNU_VTINHERIT
5182 || r_type == R_NDS32_NONE
5183 || r_type == R_NDS32_RELA_GNU_VTENTRY
5184 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5185 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5186 || r_type == R_NDS32_DATA
5187 || r_type == R_NDS32_TRAN)
5188 continue;
5189
5190 /* If we enter the fp-as-gp region. Resolve the address
5191 of best fp-base. */
5192 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5193 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5194 {
5195 int dist;
5196
5197 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5198 dist = rel->r_addend >> 16;
5199 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5200 local_syms, symtab_hdr);
5201 }
5202 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5203 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5204 {
5205 fpbase_addr = elf_gp (output_bfd);
5206 }
5207
5208 /* Skip the relocations used for relaxation. */
5209 /* We have to update LONGCALL and LONGJUMP
5210 relocations when generating the relocatable files. */
5211 if (!bfd_link_relocatable (info)
5212 && (r_type >= R_NDS32_RELAX_ENTRY
5213 || (r_type >= R_NDS32_LONGCALL4
5214 && r_type <= R_NDS32_LONGJUMP7)))
5215 continue;
5216
5217 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5218 r_symndx = ELF32_R_SYM (rel->r_info);
5219
5220 /* This is a final link. */
5221 sym = NULL;
5222 sec = NULL;
5223 h = NULL;
5224
5225 if (r_symndx < symtab_hdr->sh_info)
5226 {
5227 /* Local symbol. */
5228 sym = local_syms + r_symndx;
5229 sec = local_sections[r_symndx];
5230
5231 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5232 addend = rel->r_addend;
5233
5234 /* keep symbol location for static TLS_IE GOT entry */
5235 relocation_sym = relocation;
5236 if (bfd_link_relocatable (info))
5237 {
5238 /* This is a relocatable link. We don't have to change
5239 anything, unless the reloc is against a section symbol,
5240 in which case we have to adjust according to where the
5241 section symbol winds up in the output section. */
5242 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5243 rel->r_addend += sec->output_offset + sym->st_value;
5244
5245 continue;
5246 }
5247 }
5248 else
5249 {
5250 /* External symbol. */
5251 if (bfd_link_relocatable (info))
5252 continue;
5253 bfd_boolean warned, ignored, unresolved_reloc;
5254 int symndx = r_symndx - symtab_hdr->sh_info;
5255
5256 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5257 r_symndx, symtab_hdr, sym_hashes, h, sec,
5258 relocation, unresolved_reloc, warned,
5259 ignored);
5260
5261 /* keep symbol location for static TLS_IE GOT entry */
5262 relocation_sym = relocation;
5263
5264 /* la $fp, _FP_BASE_ is per-function (region).
5265 Handle it specially. */
5266 switch ((int) r_type)
5267 {
5268 case R_NDS32_HI20_RELA:
5269 case R_NDS32_LO12S0_RELA:
5270 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5271 FP_BASE_NAME) == 0)
5272 {
5273 if (!bfd_link_pie (info))
5274 {
5275 _bfd_error_handler
5276 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5277 input_bfd);
5278 }
5279 relocation = fpbase_addr;
5280 }
5281 break;
5282 case R_NDS32_SDA19S0_RELA:
5283 case R_NDS32_SDA15S0_RELA:
5284 case R_NDS32_20_RELA:
5285 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5286 FP_BASE_NAME) == 0)
5287 {
5288 relocation = fpbase_addr;
5289 break;
5290 }
5291 }
5292 }
5293
5294 /* Sanity check the address. */
5295 if (offset > high_address)
5296 {
5297 r = bfd_reloc_outofrange;
5298 goto check_reloc;
5299 }
5300
5301 if (r_type >= R_NDS32_RELAX_ENTRY)
5302 continue;
5303
5304 switch ((int) r_type)
5305 {
5306 case R_NDS32_GOTOFF:
5307 /* Relocation is relative to the start of the global offset
5308 table (for ld24 rx, #uimm24), e.g. access at label+addend
5309
5310 ld24 rx. #label@GOTOFF + addend
5311 sub rx, r12. */
5312 case R_NDS32_GOTOFF_HI20:
5313 case R_NDS32_GOTOFF_LO12:
5314 case R_NDS32_GOTOFF_LO15:
5315 case R_NDS32_GOTOFF_LO19:
5316 BFD_ASSERT (sgot != NULL);
5317
5318 relocation -= elf_gp (output_bfd);
5319 break;
5320
5321 case R_NDS32_9_PLTREL:
5322 case R_NDS32_25_PLTREL:
5323 /* Relocation is to the entry for this symbol in the
5324 procedure linkage table. */
5325
5326 /* The native assembler will generate a 25_PLTREL reloc
5327 for a local symbol if you assemble a call from one
5328 section to another when using -K pic. */
5329 if (h == NULL)
5330 break;
5331
5332 if (h->forced_local)
5333 break;
5334
5335 /* We didn't make a PLT entry for this symbol. This
5336 happens when statically linking PIC code, or when
5337 using -Bsymbolic. */
5338 if (h->plt.offset == (bfd_vma) - 1)
5339 break;
5340
5341 relocation = (splt->output_section->vma
5342 + splt->output_offset + h->plt.offset);
5343 break;
5344
5345 case R_NDS32_PLT_GOTREL_HI20:
5346 case R_NDS32_PLT_GOTREL_LO12:
5347 case R_NDS32_PLT_GOTREL_LO15:
5348 case R_NDS32_PLT_GOTREL_LO19:
5349 case R_NDS32_PLT_GOTREL_LO20:
5350 if (h == NULL
5351 || h->forced_local
5352 || h->plt.offset == (bfd_vma) -1
5353 || (bfd_link_pie (info) && h->def_regular))
5354 {
5355 /* Maybe we should find better checking to optimize
5356 PIE PLT relocations. */
5357 /* We didn't make a PLT entry for this symbol. This
5358 happens when statically linking PIC code, or when
5359 using -Bsymbolic. */
5360 if (h)
5361 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
5362 relocation -= elf_gp (output_bfd);
5363 break;
5364 }
5365
5366 relocation = (splt->output_section->vma
5367 + splt->output_offset + h->plt.offset);
5368
5369 relocation -= elf_gp (output_bfd);
5370 break;
5371
5372 case R_NDS32_PLTREL_HI20:
5373 case R_NDS32_PLTREL_LO12:
5374
5375 /* Relocation is to the entry for this symbol in the
5376 procedure linkage table. */
5377
5378 /* The native assembler will generate a 25_PLTREL reloc
5379 for a local symbol if you assemble a call from one
5380 section to another when using -K pic. */
5381 if (h == NULL)
5382 break;
5383
5384 if (h->forced_local)
5385 break;
5386
5387 if (h->plt.offset == (bfd_vma) - 1)
5388 /* We didn't make a PLT entry for this symbol. This
5389 happens when statically linking PIC code, or when
5390 using -Bsymbolic. */
5391 break;
5392
5393 if (splt == NULL)
5394 break;
5395
5396 relocation = (splt->output_section->vma
5397 + splt->output_offset
5398 + h->plt.offset + 4)
5399 - (input_section->output_section->vma
5400 + input_section->output_offset
5401 + rel->r_offset);
5402
5403 break;
5404
5405 case R_NDS32_GOTPC20:
5406 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5407 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5408 relocation = elf_gp (output_bfd);
5409 break;
5410
5411 case R_NDS32_GOTPC_HI20:
5412 case R_NDS32_GOTPC_LO12:
5413 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5414 bl .+4
5415 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5416 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5417 or
5418 bl .+4
5419 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5420 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5421 relocation = elf_gp (output_bfd);
5422 relocation -= (input_section->output_section->vma
5423 + input_section->output_offset + rel->r_offset);
5424 break;
5425
5426 case R_NDS32_GOT20:
5427 /* Fall through. */
5428 case R_NDS32_GOT_HI20:
5429 case R_NDS32_GOT_LO12:
5430 case R_NDS32_GOT_LO15:
5431 case R_NDS32_GOT_LO19:
5432 /* Relocation is to the entry for this symbol in the global
5433 offset table. */
5434 BFD_ASSERT (sgot != NULL);
5435
5436 if (h != NULL)
5437 {
5438 /* External symbol */
5439 bfd_boolean dyn;
5440
5441 off = h->got.offset;
5442 BFD_ASSERT (off != (bfd_vma) - 1);
5443 dyn = htab->root.dynamic_sections_created;
5444 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5445 bfd_link_pic (info),
5446 h)
5447 || (bfd_link_pic (info)
5448 && (info->symbolic
5449 || h->dynindx == -1
5450 || h->forced_local) && h->def_regular))
5451 {
5452 /* This is actually a static link, or it is a
5453 -Bsymbolic link and the symbol is defined
5454 locally, or the symbol was forced to be local
5455 because of a version file. We must initialize
5456 this entry in the global offset table. Since the
5457 offset must always be a multiple of 4, we use the
5458 least significant bit to record whether we have
5459 initialized it already.
5460
5461 When doing a dynamic link, we create a .rela.got
5462 relocation entry to initialize the value. This
5463 is done in the finish_dynamic_symbol routine. */
5464 if ((off & 1) != 0) /* clear LSB */
5465 off &= ~1;
5466 else
5467 {
5468 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5469 h->got.offset |= 1;
5470 }
5471 }
5472 relocation = sgot->output_section->vma + sgot->output_offset + off
5473 - elf_gp (output_bfd);
5474 }
5475 else
5476 {
5477 /* Local symbol */
5478 bfd_byte *loc;
5479
5480 BFD_ASSERT (local_got_offsets != NULL
5481 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5482
5483 off = local_got_offsets[r_symndx];
5484
5485 /* The offset must always be a multiple of 4. We use
5486 the least significant bit to record whether we have
5487 already processed this entry. */
5488 if ((off & 1) != 0) /* clear LSB */
5489 off &= ~1;
5490 else
5491 {
5492 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5493
5494 if (bfd_link_pic (info))
5495 {
5496 asection *srelgot;
5497 Elf_Internal_Rela outrel;
5498
5499 /* We need to generate a R_NDS32_RELATIVE reloc
5500 for the dynamic linker. */
5501 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5502 BFD_ASSERT (srelgot != NULL);
5503
5504 outrel.r_offset = (elf_gp (output_bfd)
5505 + sgot->output_offset + off);
5506 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5507 outrel.r_addend = relocation;
5508 loc = srelgot->contents;
5509 loc +=
5510 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5511 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5512 ++srelgot->reloc_count;
5513 }
5514 local_got_offsets[r_symndx] |= 1;
5515 }
5516 relocation = sgot->output_section->vma + sgot->output_offset + off
5517 - elf_gp (output_bfd);
5518 }
5519
5520 break;
5521
5522 case R_NDS32_16_RELA:
5523 case R_NDS32_20_RELA:
5524 case R_NDS32_5_RELA:
5525 case R_NDS32_32_RELA:
5526 case R_NDS32_9_PCREL_RELA:
5527 case R_NDS32_WORD_9_PCREL_RELA:
5528 case R_NDS32_10_UPCREL_RELA:
5529 case R_NDS32_15_PCREL_RELA:
5530 case R_NDS32_17_PCREL_RELA:
5531 case R_NDS32_25_PCREL_RELA:
5532 case R_NDS32_HI20_RELA:
5533 case R_NDS32_LO12S3_RELA:
5534 case R_NDS32_LO12S2_RELA:
5535 case R_NDS32_LO12S2_DP_RELA:
5536 case R_NDS32_LO12S2_SP_RELA:
5537 case R_NDS32_LO12S1_RELA:
5538 case R_NDS32_LO12S0_RELA:
5539 case R_NDS32_LO12S0_ORI_RELA:
5540 if (bfd_link_pic (info) && r_symndx != 0
5541 && (input_section->flags & SEC_ALLOC) != 0
5542 && (eliminate_gc_relocs == 0
5543 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5544 && ((r_type != R_NDS32_9_PCREL_RELA
5545 && r_type != R_NDS32_WORD_9_PCREL_RELA
5546 && r_type != R_NDS32_10_UPCREL_RELA
5547 && r_type != R_NDS32_15_PCREL_RELA
5548 && r_type != R_NDS32_17_PCREL_RELA
5549 && r_type != R_NDS32_25_PCREL_RELA
5550 && !(r_type == R_NDS32_32_RELA
5551 && strcmp (input_section->name, ".eh_frame") == 0))
5552 || (h != NULL && h->dynindx != -1
5553 && (!info->symbolic || !h->def_regular))))
5554 {
5555 Elf_Internal_Rela outrel;
5556 bfd_boolean skip, relocate;
5557 bfd_byte *loc;
5558
5559 /* When generating a shared object, these relocations
5560 are copied into the output file to be resolved at run
5561 time. */
5562
5563 if (sreloc == NULL)
5564 {
5565 const char *name;
5566
5567 name = bfd_elf_string_from_elf_section
5568 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5569 elf_section_data (input_section)->rela.hdr->sh_name);
5570 if (name == NULL)
5571 return FALSE;
5572
5573 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5574 && strcmp (bfd_section_name (input_section),
5575 name + 5) == 0);
5576
5577 sreloc = bfd_get_section_by_name (dynobj, name);
5578 BFD_ASSERT (sreloc != NULL);
5579 }
5580
5581 skip = FALSE;
5582 relocate = FALSE;
5583
5584 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5585 info,
5586 input_section,
5587 rel->r_offset);
5588 if (outrel.r_offset == (bfd_vma) - 1)
5589 skip = TRUE;
5590 else if (outrel.r_offset == (bfd_vma) - 2)
5591 skip = TRUE, relocate = TRUE;
5592 outrel.r_offset += (input_section->output_section->vma
5593 + input_section->output_offset);
5594
5595 if (skip)
5596 memset (&outrel, 0, sizeof outrel);
5597 else if (r_type == R_NDS32_17_PCREL_RELA
5598 || r_type == R_NDS32_15_PCREL_RELA
5599 || r_type == R_NDS32_25_PCREL_RELA)
5600 {
5601 BFD_ASSERT (h != NULL && h->dynindx != -1);
5602 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5603 outrel.r_addend = rel->r_addend;
5604 }
5605 else
5606 {
5607 /* h->dynindx may be -1 if this symbol was marked to
5608 become local. */
5609 if (h == NULL
5610 || ((info->symbolic || h->dynindx == -1)
5611 && h->def_regular)
5612 || (bfd_link_pie (info) && h->def_regular))
5613 {
5614 relocate = TRUE;
5615 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5616 outrel.r_addend = relocation + rel->r_addend;
5617
5618 if (h)
5619 {
5620 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5621
5622 BFD_ASSERT (sgot != NULL);
5623 /* If we did not allocate got entry for the symbol,
5624 we can not fill the nonexistent got entry. */
5625 if (h->got.offset != (bfd_vma) -1
5626 && (h->got.offset & 1) == 0)
5627 {
5628 bfd_put_32 (output_bfd, outrel.r_addend,
5629 sgot->contents + h->got.offset);
5630 }
5631 }
5632 }
5633 else
5634 {
5635 if (h->dynindx == -1)
5636 {
5637 _bfd_error_handler
5638 (_("%pB: relocation %s against `%s' can not be used when "
5639 "making a shared object; recompile with -fPIC"),
5640 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5641 bfd_set_error (bfd_error_bad_value);
5642 return FALSE;
5643 }
5644
5645 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5646 outrel.r_addend = rel->r_addend;
5647 }
5648 }
5649
5650 loc = sreloc->contents;
5651 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5652 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5653 ++sreloc->reloc_count;
5654
5655 /* If this reloc is against an external symbol, we do
5656 not want to fiddle with the addend. Otherwise, we
5657 need to include the symbol value so that it becomes
5658 an addend for the dynamic reloc. */
5659 if (!relocate)
5660 continue;
5661 }
5662 break;
5663
5664 case R_NDS32_25_ABS_RELA:
5665 if (bfd_link_pic (info))
5666 {
5667 _bfd_error_handler
5668 (_("%pB: warning: %s unsupported in shared mode"),
5669 input_bfd, "R_NDS32_25_ABS_RELA");
5670 return FALSE;
5671 }
5672 break;
5673
5674 case R_NDS32_9_PCREL:
5675 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5676 contents, offset,
5677 sec, relocation, addend);
5678 goto check_reloc;
5679
5680 case R_NDS32_HI20:
5681 /* We allow an arbitrary number of HI20 relocs before the
5682 LO12 reloc. This permits gcc to emit the HI and LO relocs
5683 itself. */
5684 for (lorel = rel + 1;
5685 (lorel < relend
5686 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5687 continue;
5688 if (lorel < relend
5689 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5690 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5691 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5692 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5693 {
5694 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5695 contents, relocation + addend);
5696 r = bfd_reloc_ok;
5697 }
5698 else
5699 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5700 contents, offset, relocation,
5701 addend);
5702 goto check_reloc;
5703
5704 case R_NDS32_GOT17S2_RELA:
5705 case R_NDS32_GOT15S2_RELA:
5706 BFD_ASSERT (sgot != NULL);
5707
5708 if (h != NULL)
5709 {
5710 bfd_boolean dyn;
5711
5712 off = h->got.offset;
5713 BFD_ASSERT (off != (bfd_vma) - 1);
5714
5715 dyn = htab->root.dynamic_sections_created;
5716 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5717 (dyn, bfd_link_pic (info), h)
5718 || (bfd_link_pic (info)
5719 && (info->symbolic
5720 || h->dynindx == -1
5721 || h->forced_local)
5722 && h->def_regular))
5723 {
5724 /* This is actually a static link, or it is a
5725 -Bsymbolic link and the symbol is defined
5726 locally, or the symbol was forced to be local
5727 because of a version file. We must initialize
5728 this entry in the global offset table. Since the
5729 offset must always be a multiple of 4, we use the
5730 least significant bit to record whether we have
5731 initialized it already.
5732
5733 When doing a dynamic link, we create a .rela.got
5734 relocation entry to initialize the value. This
5735 is done in the finish_dynamic_symbol routine. */
5736 if ((off & 1) != 0)
5737 off &= ~1;
5738 else
5739 {
5740 bfd_put_32 (output_bfd, relocation,
5741 sgot->contents + off);
5742 h->got.offset |= 1;
5743 }
5744 }
5745 }
5746 else
5747 {
5748 bfd_byte *loc;
5749
5750 BFD_ASSERT (local_got_offsets != NULL
5751 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5752
5753 off = local_got_offsets[r_symndx];
5754
5755 /* The offset must always be a multiple of 4. We use
5756 the least significant bit to record whether we have
5757 already processed this entry. */
5758 if ((off & 1) != 0)
5759 off &= ~1;
5760 else
5761 {
5762 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5763
5764 if (bfd_link_pic (info))
5765 {
5766 asection *srelgot;
5767 Elf_Internal_Rela outrel;
5768
5769 /* We need to generate a R_NDS32_RELATIVE reloc
5770 for the dynamic linker. */
5771 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5772 BFD_ASSERT (srelgot != NULL);
5773
5774 outrel.r_offset = (elf_gp (output_bfd)
5775 + sgot->output_offset + off);
5776 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5777 outrel.r_addend = relocation;
5778 loc = srelgot->contents;
5779 loc +=
5780 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5781 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5782 ++srelgot->reloc_count;
5783 }
5784 local_got_offsets[r_symndx] |= 1;
5785 }
5786 }
5787 relocation = sgot->output_section->vma + sgot->output_offset + off
5788 - elf_gp (output_bfd);
5789
5790 if (relocation & align)
5791 {
5792 /* Incorrect alignment. */
5793 _bfd_error_handler
5794 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5795 ret = FALSE;
5796 r = bfd_reloc_dangerous;
5797 goto check_reloc;
5798 }
5799 break;
5800
5801 case R_NDS32_SDA16S3_RELA:
5802 case R_NDS32_SDA15S3_RELA:
5803 case R_NDS32_SDA15S3:
5804 align = 0x7;
5805 goto handle_sda;
5806
5807 case R_NDS32_SDA17S2_RELA:
5808 case R_NDS32_SDA15S2_RELA:
5809 case R_NDS32_SDA12S2_SP_RELA:
5810 case R_NDS32_SDA12S2_DP_RELA:
5811 case R_NDS32_SDA15S2:
5812 case R_NDS32_SDA_FP7U2_RELA:
5813 align = 0x3;
5814 goto handle_sda;
5815
5816 case R_NDS32_SDA18S1_RELA:
5817 case R_NDS32_SDA15S1_RELA:
5818 case R_NDS32_SDA15S1:
5819 align = 0x1;
5820 goto handle_sda;
5821
5822 case R_NDS32_SDA19S0_RELA:
5823 case R_NDS32_SDA15S0_RELA:
5824 case R_NDS32_SDA15S0:
5825 align = 0x0;
5826 handle_sda:
5827 BFD_ASSERT (sec != NULL);
5828
5829 /* If the symbol is in the abs section, the out_bfd will be null.
5830 This happens when the relocation has a symbol@GOTOFF. */
5831 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5832 if (r != bfd_reloc_ok)
5833 {
5834 _bfd_error_handler
5835 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5836 ret = FALSE;
5837 goto check_reloc;
5838 }
5839
5840 /* At this point `relocation' contains the object's
5841 address. */
5842 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5843 {
5844 relocation -= fpbase_addr;
5845 }
5846 else
5847 relocation -= gp;
5848 /* Now it contains the offset from _SDA_BASE_. */
5849
5850 /* Make sure alignment is correct. */
5851
5852 if (relocation & align)
5853 {
5854 /* Incorrect alignment. */
5855 _bfd_error_handler
5856 /* xgettext:c-format */
5857 (_("%pB(%pA): warning: unaligned small data access"
5858 " of type %d"),
5859 input_bfd, input_section, r_type);
5860 ret = FALSE;
5861 goto check_reloc;
5862 }
5863 break;
5864
5865 case R_NDS32_17IFC_PCREL_RELA:
5866 case R_NDS32_10IFCU_PCREL_RELA:
5867 /* Do nothing. */
5868 break;
5869
5870 case R_NDS32_TLS_LE_HI20:
5871 case R_NDS32_TLS_LE_LO12:
5872 case R_NDS32_TLS_LE_20:
5873 case R_NDS32_TLS_LE_15S0:
5874 case R_NDS32_TLS_LE_15S1:
5875 case R_NDS32_TLS_LE_15S2:
5876 /* We do not have garbage collection for got entries.
5877 Therefore, IE to LE may have one empty entry, and DESC to
5878 LE may have two. */
5879 if (elf_hash_table (info)->tls_sec != NULL)
5880 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5881 break;
5882
5883 case R_NDS32_TLS_IE_HI20:
5884 case R_NDS32_TLS_IE_LO12S2:
5885 case R_NDS32_TLS_DESC_HI20:
5886 case R_NDS32_TLS_DESC_LO12:
5887 case R_NDS32_TLS_IE_LO12:
5888 case R_NDS32_TLS_IEGP_HI20:
5889 case R_NDS32_TLS_IEGP_LO12:
5890 case R_NDS32_TLS_IEGP_LO12S2:
5891 {
5892 /* Relocation is to the entry for this symbol in the global
5893 offset table. */
5894 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5895 asection *srelgot;
5896 Elf_Internal_Rela outrel;
5897 bfd_byte *loc;
5898 int indx = 0;
5899
5900 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5901
5902 BFD_ASSERT (sgot != NULL);
5903 if (h != NULL)
5904 {
5905 bfd_boolean dyn;
5906
5907 off = h->got.offset;
5908 BFD_ASSERT (off != (bfd_vma) -1);
5909 dyn = htab->root.dynamic_sections_created;
5910 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5911 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5912 && (!bfd_link_pic (info)
5913 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5914 indx = h->dynindx;
5915 }
5916 else
5917 {
5918 BFD_ASSERT (local_got_offsets != NULL
5919 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5920 off = local_got_offsets[r_symndx];
5921 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5922 }
5923
5924 relocation = sgot->output_section->vma + sgot->output_offset + off;
5925
5926 if (1 < ones32 (tls_type))
5927 {
5928 eff_tls_type = 1 << (fls (tls_type) - 1);
5929 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5930
5931 /* TLS model X -> LE is not implement yet!
5932 workaround here! */
5933 if (eff_tls_type == GOT_TLS_LE)
5934 {
5935 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5936 }
5937 }
5938
5939 /* The offset must always be a multiple of 4. We use
5940 the least significant bit to record whether we have
5941 already processed this entry. */
5942 bfd_boolean need_relocs = FALSE;
5943 srelgot = ehtab->srelgot;
5944 if ((bfd_link_pic (info) || indx != 0)
5945 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5946 || h->root.type != bfd_link_hash_undefweak))
5947 {
5948 need_relocs = TRUE;
5949 BFD_ASSERT (srelgot != NULL);
5950 }
5951
5952 if (off & 1)
5953 {
5954 off &= ~1;
5955 relocation &= ~1;
5956
5957 if (eff_tls_type & GOT_TLS_DESC)
5958 {
5959 relocation -= elf_gp (output_bfd);
5960 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5961 {
5962 /* TLS model shall be converted. */
5963 BFD_ASSERT(0);
5964 }
5965 }
5966 else if (eff_tls_type & GOT_TLS_IEGP)
5967 {
5968 relocation -= elf_gp (output_bfd);
5969 }
5970 }
5971 else
5972 {
5973 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5974 {
5975 /* TLS model workaround shall be applied. */
5976 BFD_ASSERT(0);
5977 }
5978 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5979 {
5980 if (eff_tls_type & GOT_TLS_IEGP)
5981 relocation -= elf_gp(output_bfd);
5982
5983 if (need_relocs)
5984 {
5985 if (indx == 0)
5986 outrel.r_addend = gottpoff (info, relocation_sym);
5987 else
5988 outrel.r_addend = 0;
5989 outrel.r_offset = (sgot->output_section->vma
5990 + sgot->output_offset + off);
5991 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5992
5993 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5994 &outrel);
5995 }
5996 else
5997 {
5998 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5999 sgot->contents + off);
6000 }
6001 }
6002 else if (eff_tls_type & GOT_TLS_DESC)
6003 {
6004 relocation -= elf_gp (output_bfd);
6005 if (need_relocs)
6006 {
6007 if (indx == 0)
6008 outrel.r_addend = gottpoff (info, relocation_sym);
6009 else
6010 outrel.r_addend = 0;
6011 outrel.r_offset = (sgot->output_section->vma
6012 + sgot->output_offset + off);
6013 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
6014
6015 if (htab->tls_desc_trampoline)
6016 {
6017 asection *srelplt;
6018 srelplt = ehtab->srelplt;
6019 loc = srelplt->contents;
6020 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
6021 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
6022 <= srelplt->contents + srelplt->size);
6023
6024 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6025 }
6026 else
6027 {
6028 loc = srelgot->contents;
6029 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
6030 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6031 ++srelgot->reloc_count;
6032 }
6033 }
6034 else
6035 {
6036 /* feed me! */
6037 bfd_put_32 (output_bfd, 0xdeadbeef,
6038 sgot->contents + off);
6039 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6040 sgot->contents + off + 4);
6041 patch_tls_desc_to_ie (contents, rel, input_bfd);
6042 BFD_ASSERT(0);
6043 }
6044 }
6045 else
6046 {
6047 /* TLS model workaround shall be applied. */
6048 BFD_ASSERT(0);
6049 }
6050
6051 if (h != NULL)
6052 h->got.offset |= 1;
6053 else
6054 local_got_offsets[r_symndx] |= 1;
6055 }
6056 }
6057 break;
6058 /* DON'T fall through. */
6059
6060 default:
6061 /* OLD_NDS32_RELOC. */
6062
6063 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6064 contents, offset, relocation, addend);
6065 goto check_reloc;
6066 }
6067
6068 switch ((int) r_type)
6069 {
6070 case R_NDS32_20_RELA:
6071 case R_NDS32_5_RELA:
6072 case R_NDS32_9_PCREL_RELA:
6073 case R_NDS32_WORD_9_PCREL_RELA:
6074 case R_NDS32_10_UPCREL_RELA:
6075 case R_NDS32_15_PCREL_RELA:
6076 case R_NDS32_17_PCREL_RELA:
6077 case R_NDS32_25_PCREL_RELA:
6078 case R_NDS32_25_ABS_RELA:
6079 case R_NDS32_HI20_RELA:
6080 case R_NDS32_LO12S3_RELA:
6081 case R_NDS32_LO12S2_RELA:
6082 case R_NDS32_LO12S2_DP_RELA:
6083 case R_NDS32_LO12S2_SP_RELA:
6084 case R_NDS32_LO12S1_RELA:
6085 case R_NDS32_LO12S0_RELA:
6086 case R_NDS32_LO12S0_ORI_RELA:
6087 case R_NDS32_SDA16S3_RELA:
6088 case R_NDS32_SDA17S2_RELA:
6089 case R_NDS32_SDA18S1_RELA:
6090 case R_NDS32_SDA19S0_RELA:
6091 case R_NDS32_SDA15S3_RELA:
6092 case R_NDS32_SDA15S2_RELA:
6093 case R_NDS32_SDA12S2_DP_RELA:
6094 case R_NDS32_SDA12S2_SP_RELA:
6095 case R_NDS32_SDA15S1_RELA:
6096 case R_NDS32_SDA15S0_RELA:
6097 case R_NDS32_SDA_FP7U2_RELA:
6098 case R_NDS32_9_PLTREL:
6099 case R_NDS32_25_PLTREL:
6100 case R_NDS32_GOT20:
6101 case R_NDS32_GOT_HI20:
6102 case R_NDS32_GOT_LO12:
6103 case R_NDS32_GOT_LO15:
6104 case R_NDS32_GOT_LO19:
6105 case R_NDS32_GOT15S2_RELA:
6106 case R_NDS32_GOT17S2_RELA:
6107 case R_NDS32_GOTPC20:
6108 case R_NDS32_GOTPC_HI20:
6109 case R_NDS32_GOTPC_LO12:
6110 case R_NDS32_GOTOFF:
6111 case R_NDS32_GOTOFF_HI20:
6112 case R_NDS32_GOTOFF_LO12:
6113 case R_NDS32_GOTOFF_LO15:
6114 case R_NDS32_GOTOFF_LO19:
6115 case R_NDS32_PLTREL_HI20:
6116 case R_NDS32_PLTREL_LO12:
6117 case R_NDS32_PLT_GOTREL_HI20:
6118 case R_NDS32_PLT_GOTREL_LO12:
6119 case R_NDS32_PLT_GOTREL_LO15:
6120 case R_NDS32_PLT_GOTREL_LO19:
6121 case R_NDS32_PLT_GOTREL_LO20:
6122 case R_NDS32_17IFC_PCREL_RELA:
6123 case R_NDS32_10IFCU_PCREL_RELA:
6124 case R_NDS32_TLS_LE_HI20:
6125 case R_NDS32_TLS_LE_LO12:
6126 case R_NDS32_TLS_IE_HI20:
6127 case R_NDS32_TLS_IE_LO12S2:
6128 case R_NDS32_TLS_LE_20:
6129 case R_NDS32_TLS_LE_15S0:
6130 case R_NDS32_TLS_LE_15S1:
6131 case R_NDS32_TLS_LE_15S2:
6132 case R_NDS32_TLS_DESC_HI20:
6133 case R_NDS32_TLS_DESC_LO12:
6134 case R_NDS32_TLS_IE_LO12:
6135 case R_NDS32_TLS_IEGP_HI20:
6136 case R_NDS32_TLS_IEGP_LO12:
6137 case R_NDS32_TLS_IEGP_LO12S2:
6138 /* Instruction related relocs must handle endian properly. */
6139 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
6140 r = nds32_elf_final_link_relocate (howto, input_bfd,
6141 input_section, contents,
6142 rel->r_offset, relocation,
6143 rel->r_addend);
6144 break;
6145
6146 default:
6147 /* All other relocs can use default handler. */
6148 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6149 contents, rel->r_offset,
6150 relocation, rel->r_addend);
6151 break;
6152 }
6153
6154 check_reloc:
6155
6156 if (r != bfd_reloc_ok)
6157 {
6158 /* FIXME: This should be generic enough to go in a utility. */
6159 const char *name;
6160
6161 if (h != NULL)
6162 name = h->root.root.string;
6163 else
6164 {
6165 name = bfd_elf_string_from_elf_section
6166 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6167 if (name == NULL || *name == '\0')
6168 name = bfd_section_name (sec);
6169 }
6170
6171 if (errmsg != NULL)
6172 goto common_error;
6173
6174 switch (r)
6175 {
6176 case bfd_reloc_overflow:
6177 (*info->callbacks->reloc_overflow)
6178 (info, (h ? &h->root : NULL), name, howto->name,
6179 (bfd_vma) 0, input_bfd, input_section, offset);
6180 break;
6181
6182 case bfd_reloc_undefined:
6183 (*info->callbacks->undefined_symbol)
6184 (info, name, input_bfd, input_section, offset, TRUE);
6185 break;
6186
6187 case bfd_reloc_outofrange:
6188 errmsg = _("internal error: out of range error");
6189 goto common_error;
6190
6191 case bfd_reloc_notsupported:
6192 errmsg = _("internal error: unsupported relocation error");
6193 goto common_error;
6194
6195 case bfd_reloc_dangerous:
6196 errmsg = _("internal error: dangerous error");
6197 goto common_error;
6198
6199 default:
6200 errmsg = _("internal error: unknown error");
6201 /* Fall through. */
6202
6203 common_error:
6204 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6205 input_section, offset);
6206 break;
6207 }
6208 }
6209 }
6210
6211 /* Resotre header size to avoid overflow load. */
6212 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6213 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6214
6215 return ret;
6216 }
6217
6218 /* Finish up dynamic symbol handling. We set the contents of various
6219 dynamic sections here. */
6220
6221 static bfd_boolean
6222 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6223 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6224 {
6225 struct elf_link_hash_table *ehtab;
6226 struct elf_nds32_link_hash_entry *hent;
6227 bfd_byte *loc;
6228
6229 ehtab = elf_hash_table (info);
6230 hent = (struct elf_nds32_link_hash_entry *) h;
6231
6232 if (h->plt.offset != (bfd_vma) - 1)
6233 {
6234 asection *splt;
6235 asection *sgot;
6236 asection *srela;
6237
6238 bfd_vma plt_index;
6239 bfd_vma got_offset;
6240 bfd_vma local_plt_offset;
6241 Elf_Internal_Rela rela;
6242
6243 /* This symbol has an entry in the procedure linkage table. Set
6244 it up. */
6245
6246 BFD_ASSERT (h->dynindx != -1);
6247
6248 splt = ehtab->splt;
6249 sgot = ehtab->sgotplt;
6250 srela = ehtab->srelplt;
6251 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6252
6253 /* Get the index in the procedure linkage table which
6254 corresponds to this symbol. This is the index of this symbol
6255 in all the symbols for which we are making plt entries. The
6256 first entry in the procedure linkage table is reserved. */
6257 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6258
6259 /* Get the offset into the .got table of the entry that
6260 corresponds to this function. Each .got entry is 4 bytes.
6261 The first three are reserved. */
6262 got_offset = (plt_index + 3) * 4;
6263
6264 /* Fill in the entry in the procedure linkage table. */
6265 if (!bfd_link_pic (info))
6266 {
6267 unsigned long insn;
6268
6269 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6270 + sgot->output_offset + got_offset) >> 12)
6271 & 0xfffff);
6272 bfd_putb32 (insn, splt->contents + h->plt.offset);
6273
6274 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6275 + sgot->output_offset + got_offset) & 0x0fff)
6276 >> 2);
6277 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6278
6279 insn = PLT_ENTRY_WORD2;
6280 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6281
6282 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6283 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6284
6285 insn = PLT_ENTRY_WORD4
6286 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6287 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6288 local_plt_offset = 12;
6289 }
6290 else
6291 {
6292 /* sda_base must be set at this time. */
6293 unsigned long insn;
6294 long offset;
6295
6296 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6297 - elf_gp (output_bfd);
6298 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6299 bfd_putb32 (insn, splt->contents + h->plt.offset);
6300
6301 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6302 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6303
6304 insn = PLT_PIC_ENTRY_WORD2;
6305 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6306
6307 insn = PLT_PIC_ENTRY_WORD3;
6308 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6309
6310 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6311 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6312
6313 insn = PLT_PIC_ENTRY_WORD5
6314 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6315 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6316
6317 local_plt_offset = 16;
6318 }
6319
6320 /* Fill in the entry in the global offset table,
6321 so it will fall through to the next instruction for the first time. */
6322 bfd_put_32 (output_bfd,
6323 (splt->output_section->vma + splt->output_offset
6324 + h->plt.offset + local_plt_offset),
6325 sgot->contents + got_offset);
6326
6327 /* Fill in the entry in the .rela.plt section. */
6328 rela.r_offset = (sgot->output_section->vma
6329 + sgot->output_offset + got_offset);
6330 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6331 rela.r_addend = 0;
6332 loc = srela->contents;
6333 loc += plt_index * sizeof (Elf32_External_Rela);
6334 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6335
6336 if (!h->def_regular)
6337 {
6338 /* Mark the symbol as undefined, rather than as defined in
6339 the .plt section. Leave the value alone. */
6340 sym->st_shndx = SHN_UNDEF;
6341 if (!h->ref_regular_nonweak)
6342 sym->st_value = 0;
6343 }
6344 }
6345
6346 if (h->got.offset != (bfd_vma) - 1
6347 && hent->tls_type == GOT_NORMAL)
6348 {
6349 asection *sgot;
6350 asection *srelagot;
6351 Elf_Internal_Rela rela;
6352
6353 /* This symbol has an entry in the global offset table.
6354 Set it up. */
6355
6356 sgot = ehtab->sgot;
6357 srelagot = ehtab->srelgot;
6358 BFD_ASSERT (sgot != NULL && srelagot != NULL);
6359
6360 rela.r_offset = (sgot->output_section->vma
6361 + sgot->output_offset + (h->got.offset & ~1));
6362
6363 /* If this is a -Bsymbolic link, and the symbol is defined
6364 locally, we just want to emit a RELATIVE reloc. Likewise if
6365 the symbol was forced to be local because of a version file.
6366 The entry in the global offset table will already have been
6367 initialized in the relocate_section function. */
6368 if ((bfd_link_pic (info)
6369 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6370 && h->def_regular)
6371 || (bfd_link_pie (info) && h->def_regular))
6372 {
6373 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6374 rela.r_addend = (h->root.u.def.value
6375 + h->root.u.def.section->output_section->vma
6376 + h->root.u.def.section->output_offset);
6377
6378 if ((h->got.offset & 1) == 0)
6379 {
6380 bfd_put_32 (output_bfd, rela.r_addend,
6381 sgot->contents + h->got.offset);
6382 }
6383 }
6384 else
6385 {
6386 BFD_ASSERT ((h->got.offset & 1) == 0);
6387 bfd_put_32 (output_bfd, (bfd_vma) 0,
6388 sgot->contents + h->got.offset);
6389 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6390 rela.r_addend = 0;
6391 }
6392
6393 loc = srelagot->contents;
6394 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6395 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6396 ++srelagot->reloc_count;
6397 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6398 }
6399
6400 if (h->needs_copy)
6401 {
6402 asection *s;
6403 Elf_Internal_Rela rela;
6404
6405 /* This symbols needs a copy reloc. Set it up. */
6406
6407 BFD_ASSERT (h->dynindx != -1
6408 && (h->root.type == bfd_link_hash_defined
6409 || h->root.type == bfd_link_hash_defweak));
6410
6411 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6412 BFD_ASSERT (s != NULL);
6413
6414 rela.r_offset = (h->root.u.def.value
6415 + h->root.u.def.section->output_section->vma
6416 + h->root.u.def.section->output_offset);
6417 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6418 rela.r_addend = 0;
6419 loc = s->contents;
6420 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6421 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6422 ++s->reloc_count;
6423 }
6424
6425 /* Mark some specially defined symbols as absolute. */
6426 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6427 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6428 sym->st_shndx = SHN_ABS;
6429
6430 return TRUE;
6431 }
6432
6433
6434 /* Finish up the dynamic sections. */
6435
6436 static bfd_boolean
6437 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6438 {
6439 bfd *dynobj;
6440 asection *sdyn;
6441 asection *sgotplt;
6442 struct elf_link_hash_table *ehtab;
6443 struct elf_nds32_link_hash_table *htab;
6444
6445 ehtab = elf_hash_table (info);
6446 htab = nds32_elf_hash_table (info);
6447 if (htab == NULL)
6448 return FALSE;
6449
6450 dynobj = elf_hash_table (info)->dynobj;
6451
6452 sgotplt = ehtab->sgotplt;
6453 /* A broken linker script might have discarded the dynamic sections.
6454 Catch this here so that we do not seg-fault later on. */
6455 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6456 return FALSE;
6457 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6458
6459 if (elf_hash_table (info)->dynamic_sections_created)
6460 {
6461 asection *splt;
6462 Elf32_External_Dyn *dyncon, *dynconend;
6463
6464 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6465
6466 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6467 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6468
6469 for (; dyncon < dynconend; dyncon++)
6470 {
6471 Elf_Internal_Dyn dyn;
6472 asection *s;
6473
6474 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6475
6476 switch (dyn.d_tag)
6477 {
6478 default:
6479 break;
6480
6481 case DT_PLTGOT:
6482 /* name = ".got"; */
6483 s = ehtab->sgot->output_section;
6484 goto get_vma;
6485 case DT_JMPREL:
6486 s = ehtab->srelplt->output_section;
6487 get_vma:
6488 BFD_ASSERT (s != NULL);
6489 dyn.d_un.d_ptr = s->vma;
6490 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6491 break;
6492
6493 case DT_PLTRELSZ:
6494 s = ehtab->srelplt->output_section;
6495 BFD_ASSERT (s != NULL);
6496 dyn.d_un.d_val = s->size;
6497 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6498 break;
6499
6500 case DT_RELASZ:
6501 /* My reading of the SVR4 ABI indicates that the
6502 procedure linkage table relocs (DT_JMPREL) should be
6503 included in the overall relocs (DT_RELA). This is
6504 what Solaris does. However, UnixWare can not handle
6505 that case. Therefore, we override the DT_RELASZ entry
6506 here to make it not include the JMPREL relocs. Since
6507 the linker script arranges for .rela.plt to follow all
6508 other relocation sections, we don't have to worry
6509 about changing the DT_RELA entry. */
6510 if (ehtab->srelplt != NULL)
6511 {
6512 s = ehtab->srelplt->output_section;
6513 dyn.d_un.d_val -= s->size;
6514 }
6515 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6516 break;
6517
6518 case DT_TLSDESC_PLT:
6519 s = htab->root.splt;
6520 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6521 + htab->dt_tlsdesc_plt);
6522 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6523 break;
6524
6525 case DT_TLSDESC_GOT:
6526 s = htab->root.sgot;
6527 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6528 + htab->dt_tlsdesc_got);
6529 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6530 break;
6531 }
6532 }
6533
6534 /* Fill in the first entry in the procedure linkage table. */
6535 splt = ehtab->splt;
6536 if (splt && splt->size > 0)
6537 {
6538 if (bfd_link_pic (info))
6539 {
6540 unsigned long insn;
6541 long offset;
6542
6543 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6544 - elf_gp (output_bfd);
6545 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6546 bfd_putb32 (insn, splt->contents);
6547
6548 /* here has a typo? */
6549 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6550 bfd_putb32 (insn, splt->contents + 4);
6551
6552 insn = PLT0_PIC_ENTRY_WORD2;
6553 bfd_putb32 (insn, splt->contents + 8);
6554
6555 insn = PLT0_PIC_ENTRY_WORD3;
6556 bfd_putb32 (insn, splt->contents + 12);
6557
6558 insn = PLT0_PIC_ENTRY_WORD4;
6559 bfd_putb32 (insn, splt->contents + 16);
6560
6561 insn = PLT0_PIC_ENTRY_WORD5;
6562 bfd_putb32 (insn, splt->contents + 20);
6563 }
6564 else
6565 {
6566 unsigned long insn;
6567 unsigned long addr;
6568
6569 /* addr = .got + 4 */
6570 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6571 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6572 bfd_putb32 (insn, splt->contents);
6573
6574 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6575 bfd_putb32 (insn, splt->contents + 4);
6576
6577 insn = PLT0_ENTRY_WORD2;
6578 bfd_putb32 (insn, splt->contents + 8);
6579
6580 insn = PLT0_ENTRY_WORD3;
6581 bfd_putb32 (insn, splt->contents + 12);
6582
6583 insn = PLT0_ENTRY_WORD4;
6584 bfd_putb32 (insn, splt->contents + 16);
6585 }
6586
6587 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6588 PLT_ENTRY_SIZE;
6589 }
6590
6591 if (htab->dt_tlsdesc_plt)
6592 {
6593 /* Calculate addresses. */
6594 asection *sgot = sgot = ehtab->sgot;
6595 bfd_vma pltgot = sgotplt->output_section->vma
6596 + sgotplt->output_offset;
6597 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6598 + htab->dt_tlsdesc_got;
6599
6600 /* Get GP offset. */
6601 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6602 tlsdesc_got -= elf_gp (output_bfd);
6603
6604 /* Do relocation. */
6605 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6606 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6607 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6608 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6609
6610 /* Insert .plt. */
6611 nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6612 dl_tlsdesc_lazy_trampoline,
6613 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6614 }
6615 }
6616
6617 /* Fill in the first three entries in the global offset table. */
6618 if (sgotplt && sgotplt->size > 0)
6619 {
6620 if (sdyn == NULL)
6621 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6622 else
6623 bfd_put_32 (output_bfd,
6624 sdyn->output_section->vma + sdyn->output_offset,
6625 sgotplt->contents);
6626 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6627 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6628
6629 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6630 }
6631
6632 return TRUE;
6633 }
6634 \f
6635
6636 /* Set the right machine number. */
6637
6638 static bfd_boolean
6639 nds32_elf_object_p (bfd *abfd)
6640 {
6641 static unsigned int cur_arch = 0;
6642
6643 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6644 {
6645 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6646 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6647 }
6648
6649 switch (cur_arch)
6650 {
6651 default:
6652 case E_N1_ARCH:
6653 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6654 break;
6655 case E_N1H_ARCH:
6656 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6657 break;
6658 case E_NDS_ARCH_STAR_V2_0:
6659 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6660 break;
6661 case E_NDS_ARCH_STAR_V3_0:
6662 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6663 break;
6664 case E_NDS_ARCH_STAR_V3_M:
6665 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6666 break;
6667 }
6668
6669 return TRUE;
6670 }
6671
6672 /* Store the machine number in the flags field. */
6673
6674 static bfd_boolean
6675 nds32_elf_final_write_processing (bfd *abfd)
6676 {
6677 unsigned long val;
6678 static unsigned int cur_mach = 0;
6679
6680 if (bfd_mach_n1 != bfd_get_mach (abfd))
6681 {
6682 cur_mach = bfd_get_mach (abfd);
6683 }
6684
6685 switch (cur_mach)
6686 {
6687 case bfd_mach_n1:
6688 /* Only happen when object is empty, since the case is abandon. */
6689 val = E_N1_ARCH;
6690 val |= E_NDS_ABI_AABI;
6691 val |= E_NDS32_ELF_VER_1_4;
6692 break;
6693 case bfd_mach_n1h:
6694 val = E_N1H_ARCH;
6695 break;
6696 case bfd_mach_n1h_v2:
6697 val = E_NDS_ARCH_STAR_V2_0;
6698 break;
6699 case bfd_mach_n1h_v3:
6700 val = E_NDS_ARCH_STAR_V3_0;
6701 break;
6702 case bfd_mach_n1h_v3m:
6703 val = E_NDS_ARCH_STAR_V3_M;
6704 break;
6705 default:
6706 val = 0;
6707 break;
6708 }
6709
6710 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6711 elf_elfheader (abfd)->e_flags |= val;
6712 return _bfd_elf_final_write_processing (abfd);
6713 }
6714
6715 /* Function to keep NDS32 specific file flags. */
6716
6717 static bfd_boolean
6718 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6719 {
6720 BFD_ASSERT (!elf_flags_init (abfd)
6721 || elf_elfheader (abfd)->e_flags == flags);
6722
6723 elf_elfheader (abfd)->e_flags = flags;
6724 elf_flags_init (abfd) = TRUE;
6725 return TRUE;
6726 }
6727
6728 static unsigned int
6729 convert_e_flags (unsigned int e_flags, unsigned int arch)
6730 {
6731 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6732 {
6733 /* From 0.9 to 1.0. */
6734 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6735
6736 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6737 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6738 if (arch == E_NDS_ARCH_STAR_V1_0)
6739 {
6740 /* Done. */
6741 return e_flags;
6742 }
6743 }
6744
6745 /* From 1.0 to 2.0. */
6746 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6747
6748 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6749 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6750
6751 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6752 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6753 return e_flags;
6754 }
6755
6756 static bfd_boolean
6757 nds32_check_vec_size (bfd *ibfd)
6758 {
6759 static unsigned int nds32_vec_size = 0;
6760
6761 asection *sec_t = NULL;
6762 bfd_byte *contents = NULL;
6763
6764 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6765
6766 if (sec_t && sec_t->size >= 4)
6767 {
6768 /* Get vec_size in file. */
6769 unsigned int flag_t;
6770
6771 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
6772 flag_t = bfd_get_32 (ibfd, contents);
6773
6774 /* The value could only be 4 or 16. */
6775
6776 if (!nds32_vec_size)
6777 /* Set if not set yet. */
6778 nds32_vec_size = (flag_t & 0x3);
6779 else if (nds32_vec_size != (flag_t & 0x3))
6780 {
6781 _bfd_error_handler
6782 /* xgettext:c-format */
6783 (_("%pB: ISR vector size mismatch"
6784 " with previous modules, previous %u-byte, current %u-byte"),
6785 ibfd,
6786 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6787 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6788 return FALSE;
6789 }
6790 else
6791 /* Only keep the first vec_size section. */
6792 sec_t->flags |= SEC_EXCLUDE;
6793 }
6794
6795 return TRUE;
6796 }
6797
6798 /* Merge backend specific data from an object file to the output
6799 object file when linking. */
6800
6801 static bfd_boolean
6802 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6803 {
6804 bfd *obfd = info->output_bfd;
6805 flagword out_flags;
6806 flagword in_flags;
6807 flagword out_16regs;
6808 flagword in_no_mac;
6809 flagword out_no_mac;
6810 flagword in_16regs;
6811 flagword out_version;
6812 flagword in_version;
6813 flagword out_fpu_config;
6814 flagword in_fpu_config;
6815
6816 /* TODO: Revise to use object-attributes instead. */
6817 if (!nds32_check_vec_size (ibfd))
6818 return FALSE;
6819
6820 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6821 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6822 return TRUE;
6823
6824 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6825 {
6826 _bfd_error_handler
6827 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6828
6829 bfd_set_error (bfd_error_bad_value);
6830 return FALSE;
6831 }
6832
6833 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6834 treat as generic one without checking and merging. */
6835 if (elf_elfheader (ibfd)->e_flags)
6836 {
6837 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6838 if (in_version == E_NDS32_ELF_VER_1_2)
6839 {
6840 _bfd_error_handler
6841 (_("%pB: warning: older version of object file encountered, "
6842 "please recompile with current tool chain"), ibfd);
6843 }
6844
6845 /* We may need to merge V1 and V2 arch object files to V2. */
6846 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6847 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6848 {
6849 /* Need to convert version. */
6850 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6851 == E_NDS_ARCH_STAR_RESERVED)
6852 {
6853 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6854 }
6855 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6856 == E_NDS_ARCH_STAR_V3_M
6857 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6858 == E_NDS_ARCH_STAR_V3_0)
6859 {
6860 elf_elfheader (ibfd)->e_flags =
6861 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6862 | E_NDS_ARCH_STAR_V3_0;
6863 }
6864 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6865 == E_NDS_ARCH_STAR_V0_9
6866 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6867 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6868 {
6869 elf_elfheader (obfd)->e_flags =
6870 convert_e_flags (elf_elfheader (obfd)->e_flags,
6871 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6872 }
6873 else
6874 {
6875 elf_elfheader (ibfd)->e_flags =
6876 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6877 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6878 }
6879 }
6880
6881 /* Extract some flags. */
6882 in_flags = elf_elfheader (ibfd)->e_flags
6883 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6884 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6885
6886 /* The following flags need special treatment. */
6887 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6888 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6889 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6890
6891 /* Extract some flags. */
6892 out_flags = elf_elfheader (obfd)->e_flags
6893 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6894 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6895
6896 /* The following flags need special treatment. */
6897 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6898 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6899 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6900 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6901 if (!elf_flags_init (obfd))
6902 {
6903 /* If the input is the default architecture then do not
6904 bother setting the flags for the output architecture,
6905 instead allow future merges to do this. If no future
6906 merges ever set these flags then they will retain their
6907 unitialised values, which surprise surprise, correspond
6908 to the default values. */
6909 if (bfd_get_arch_info (ibfd)->the_default)
6910 return TRUE;
6911
6912 elf_flags_init (obfd) = TRUE;
6913 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6914
6915 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6916 && bfd_get_arch_info (obfd)->the_default)
6917 {
6918 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6919 bfd_get_mach (ibfd));
6920 }
6921
6922 return TRUE;
6923 }
6924
6925 /* Check flag compatibility. */
6926 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6927 {
6928 _bfd_error_handler
6929 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6930 bfd_set_error (bfd_error_bad_value);
6931 return FALSE;
6932 }
6933
6934 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6935 {
6936 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6937 {
6938 _bfd_error_handler
6939 (_("%pB: error: instruction set mismatch with previous modules"),
6940 ibfd);
6941
6942 bfd_set_error (bfd_error_bad_value);
6943 return FALSE;
6944 }
6945 }
6946
6947 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6948 and perf ext1 and DIV are mergerd to perf ext1. */
6949 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6950 {
6951 elf_elfheader (obfd)->e_flags =
6952 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6953 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6954 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6955 ? E_NDS32_HAS_EXT_INST : 0)
6956 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6957 ? E_NDS32_HAS_EXT_INST : 0)
6958 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6959 | ((in_version > out_version) ? out_version : in_version);
6960 }
6961 else
6962 {
6963 if (in_version != out_version)
6964 _bfd_error_handler
6965 /* xgettext:c-format */
6966 (_("%pB: warning: incompatible elf-versions %s and %s"),
6967 ibfd, nds32_elfver_strtab[out_version],
6968 nds32_elfver_strtab[in_version]);
6969
6970 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6971 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6972 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6973 | (in_version > out_version ? out_version : in_version);
6974 }
6975 }
6976
6977 return TRUE;
6978 }
6979
6980 /* Display the flags field. */
6981
6982 static bfd_boolean
6983 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6984 {
6985 FILE *file = (FILE *) ptr;
6986
6987 BFD_ASSERT (abfd != NULL && ptr != NULL);
6988
6989 _bfd_elf_print_private_bfd_data (abfd, ptr);
6990
6991 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6992
6993 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6994 {
6995 default:
6996 case E_N1_ARCH:
6997 fprintf (file, _(": n1 instructions"));
6998 break;
6999 case E_N1H_ARCH:
7000 fprintf (file, _(": n1h instructions"));
7001 break;
7002 }
7003
7004 fputc ('\n', file);
7005
7006 return TRUE;
7007 }
7008
7009 static unsigned int
7010 nds32_elf_action_discarded (asection *sec)
7011 {
7012
7013 if (strncmp
7014 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7015 return 0;
7016
7017 return _bfd_elf_default_action_discarded (sec);
7018 }
7019
7020 static asection *
7021 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7022 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7023 Elf_Internal_Sym *sym)
7024 {
7025 if (h != NULL)
7026 switch (ELF32_R_TYPE (rel->r_info))
7027 {
7028 case R_NDS32_GNU_VTINHERIT:
7029 case R_NDS32_GNU_VTENTRY:
7030 case R_NDS32_RELA_GNU_VTINHERIT:
7031 case R_NDS32_RELA_GNU_VTENTRY:
7032 return NULL;
7033 }
7034
7035 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7036 }
7037
7038 static enum elf_nds32_tls_type
7039 get_tls_type (enum elf_nds32_reloc_type r_type,
7040 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7041 {
7042 enum elf_nds32_tls_type tls_type;
7043
7044 switch (r_type)
7045 {
7046 case R_NDS32_TLS_LE_HI20:
7047 case R_NDS32_TLS_LE_LO12:
7048 tls_type = GOT_TLS_LE;
7049 break;
7050 case R_NDS32_TLS_IE_HI20:
7051 case R_NDS32_TLS_IE_LO12S2:
7052 case R_NDS32_TLS_IE_LO12:
7053 tls_type = GOT_TLS_IE;
7054 break;
7055 case R_NDS32_TLS_IEGP_HI20:
7056 case R_NDS32_TLS_IEGP_LO12:
7057 case R_NDS32_TLS_IEGP_LO12S2:
7058 tls_type = GOT_TLS_IEGP;
7059 break;
7060 case R_NDS32_TLS_DESC_HI20:
7061 case R_NDS32_TLS_DESC_LO12:
7062 case R_NDS32_TLS_DESC_ADD:
7063 case R_NDS32_TLS_DESC_FUNC:
7064 case R_NDS32_TLS_DESC_CALL:
7065 tls_type = GOT_TLS_DESC;
7066 break;
7067 default:
7068 tls_type = GOT_NORMAL;
7069 break;
7070 }
7071
7072 return tls_type;
7073 }
7074
7075 /* Ensure that we have allocated bookkeeping structures for ABFD's local
7076 symbols. */
7077
7078 static bfd_boolean
7079 elf32_nds32_allocate_local_sym_info (bfd *abfd)
7080 {
7081 if (elf_local_got_refcounts (abfd) == NULL)
7082 {
7083 bfd_size_type num_syms;
7084 bfd_size_type size;
7085 char *data;
7086
7087 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7088 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7089 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7090 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7091 + sizeof (bfd_vma) + sizeof (int)
7092 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7093 data = bfd_zalloc (abfd, size);
7094 if (data == NULL)
7095 return FALSE;
7096
7097 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7098 data += num_syms * sizeof (bfd_signed_vma);
7099
7100 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7101 data += num_syms * sizeof (char);
7102
7103 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7104 data += num_syms * sizeof (bfd_vma);
7105
7106 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7107 data += num_syms * sizeof (int);
7108 }
7109
7110 return TRUE;
7111 }
7112
7113 /* Look through the relocs for a section during the first phase.
7114 Since we don't do .gots or .plts, we just need to consider the
7115 virtual table relocs for gc. */
7116
7117 static bfd_boolean
7118 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7119 asection *sec, const Elf_Internal_Rela *relocs)
7120 {
7121 Elf_Internal_Shdr *symtab_hdr;
7122 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7123 const Elf_Internal_Rela *rel;
7124 const Elf_Internal_Rela *rel_end;
7125 struct elf_link_hash_table *ehtab;
7126 struct elf_nds32_link_hash_table *htab;
7127 bfd *dynobj;
7128 asection *sreloc = NULL;
7129
7130 /* No need for relocation if relocatable already. */
7131 if (bfd_link_relocatable (info))
7132 {
7133 elf32_nds32_check_relax_group (abfd, sec);
7134 return TRUE;
7135 }
7136
7137 /* Don't do anything special with non-loaded, non-alloced sections.
7138 In particular, any relocs in such sections should not affect GOT
7139 and PLT reference counting (ie. we don't allow them to create GOT
7140 or PLT entries), there's no possibility or desire to optimize TLS
7141 relocs, and there's not much point in propagating relocs to shared
7142 libs that the dynamic linker won't relocate. */
7143 if ((sec->flags & SEC_ALLOC) == 0)
7144 return TRUE;
7145
7146 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7147 sym_hashes = elf_sym_hashes (abfd);
7148 sym_hashes_end =
7149 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7150 if (!elf_bad_symtab (abfd))
7151 sym_hashes_end -= symtab_hdr->sh_info;
7152
7153 ehtab = elf_hash_table (info);
7154 htab = nds32_elf_hash_table (info);
7155 dynobj = htab->root.dynobj;
7156
7157 rel_end = relocs + sec->reloc_count;
7158 for (rel = relocs; rel < rel_end; rel++)
7159 {
7160 enum elf_nds32_reloc_type r_type;
7161 struct elf_link_hash_entry *h;
7162 unsigned long r_symndx;
7163 enum elf_nds32_tls_type tls_type, old_tls_type;
7164
7165 r_symndx = ELF32_R_SYM (rel->r_info);
7166 r_type = ELF32_R_TYPE (rel->r_info);
7167 if (r_symndx < symtab_hdr->sh_info)
7168 h = NULL;
7169 else
7170 {
7171 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7172 while (h->root.type == bfd_link_hash_indirect
7173 || h->root.type == bfd_link_hash_warning)
7174 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7175 }
7176
7177 /* Create .got section if necessary.
7178 Some relocs require a global offset table. We create
7179 got section here, since these relocation need a got section
7180 and if it is not created yet. */
7181 if (ehtab->sgot == NULL)
7182 {
7183 switch (r_type)
7184 {
7185 case R_NDS32_GOT_HI20:
7186 case R_NDS32_GOT_LO12:
7187 case R_NDS32_GOT_LO15:
7188 case R_NDS32_GOT_LO19:
7189 case R_NDS32_GOT17S2_RELA:
7190 case R_NDS32_GOT15S2_RELA:
7191 case R_NDS32_GOTOFF:
7192 case R_NDS32_GOTOFF_HI20:
7193 case R_NDS32_GOTOFF_LO12:
7194 case R_NDS32_GOTOFF_LO15:
7195 case R_NDS32_GOTOFF_LO19:
7196 case R_NDS32_GOTPC20:
7197 case R_NDS32_GOTPC_HI20:
7198 case R_NDS32_GOTPC_LO12:
7199 case R_NDS32_GOT20:
7200 case R_NDS32_TLS_IE_HI20:
7201 case R_NDS32_TLS_IE_LO12:
7202 case R_NDS32_TLS_IE_LO12S2:
7203 case R_NDS32_TLS_IEGP_HI20:
7204 case R_NDS32_TLS_IEGP_LO12:
7205 case R_NDS32_TLS_IEGP_LO12S2:
7206 case R_NDS32_TLS_DESC_HI20:
7207 case R_NDS32_TLS_DESC_LO12:
7208 if (dynobj == NULL)
7209 htab->root.dynobj = dynobj = abfd;
7210 if (!create_got_section (dynobj, info))
7211 return FALSE;
7212 break;
7213
7214 default:
7215 break;
7216 }
7217 }
7218
7219 /* Check relocation type. */
7220 switch ((int) r_type)
7221 {
7222 case R_NDS32_GOT_HI20:
7223 case R_NDS32_GOT_LO12:
7224 case R_NDS32_GOT_LO15:
7225 case R_NDS32_GOT_LO19:
7226 case R_NDS32_GOT20:
7227 case R_NDS32_TLS_LE_HI20:
7228 case R_NDS32_TLS_LE_LO12:
7229 case R_NDS32_TLS_IE_HI20:
7230 case R_NDS32_TLS_IE_LO12:
7231 case R_NDS32_TLS_IE_LO12S2:
7232 case R_NDS32_TLS_IEGP_HI20:
7233 case R_NDS32_TLS_IEGP_LO12:
7234 case R_NDS32_TLS_IEGP_LO12S2:
7235 case R_NDS32_TLS_DESC_HI20:
7236 case R_NDS32_TLS_DESC_LO12:
7237 tls_type = get_tls_type (r_type, h);
7238 if (h)
7239 {
7240 if (tls_type != GOT_TLS_LE)
7241 h->got.refcount += 1;
7242 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7243 }
7244 else
7245 {
7246 /* This is a global offset table entry for a local symbol. */
7247 if (!elf32_nds32_allocate_local_sym_info (abfd))
7248 return FALSE;
7249
7250 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7251 if (tls_type != GOT_TLS_LE)
7252 elf_local_got_refcounts (abfd)[r_symndx] += 1;
7253 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7254 }
7255
7256 /* We would already have issued an error message if there
7257 is a TLS/non-TLS mismatch, based on the symbol
7258 type. So just combine any TLS types needed. */
7259 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7260 && tls_type != GOT_NORMAL)
7261 tls_type |= old_tls_type;
7262
7263 /* DESC to IE/IEGP if link to executable. */
7264 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7265 && (bfd_link_executable (info)))
7266 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7267
7268 if (old_tls_type != tls_type)
7269 {
7270 if (h != NULL)
7271 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7272 else
7273 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7274 }
7275 break;
7276 case R_NDS32_9_PLTREL:
7277 case R_NDS32_25_PLTREL:
7278 case R_NDS32_PLTREL_HI20:
7279 case R_NDS32_PLTREL_LO12:
7280 case R_NDS32_PLT_GOTREL_HI20:
7281 case R_NDS32_PLT_GOTREL_LO12:
7282 case R_NDS32_PLT_GOTREL_LO15:
7283 case R_NDS32_PLT_GOTREL_LO19:
7284 case R_NDS32_PLT_GOTREL_LO20:
7285
7286 /* This symbol requires a procedure linkage table entry. We
7287 actually build the entry in adjust_dynamic_symbol,
7288 because this might be a case of linking PIC code without
7289 linking in any dynamic objects, in which case we don't
7290 need to generate a procedure linkage table after all. */
7291
7292 /* If this is a local symbol, we resolve it directly without
7293 creating a procedure linkage table entry. */
7294 if (h == NULL)
7295 continue;
7296
7297 if (h->forced_local
7298 || (bfd_link_pie (info) && h->def_regular))
7299 break;
7300
7301 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7302 h->needs_plt = 1;
7303 h->plt.refcount += 1;
7304 break;
7305
7306 case R_NDS32_16_RELA:
7307 case R_NDS32_20_RELA:
7308 case R_NDS32_5_RELA:
7309 case R_NDS32_32_RELA:
7310 case R_NDS32_HI20_RELA:
7311 case R_NDS32_LO12S3_RELA:
7312 case R_NDS32_LO12S2_RELA:
7313 case R_NDS32_LO12S2_DP_RELA:
7314 case R_NDS32_LO12S2_SP_RELA:
7315 case R_NDS32_LO12S1_RELA:
7316 case R_NDS32_LO12S0_RELA:
7317 case R_NDS32_LO12S0_ORI_RELA:
7318 case R_NDS32_SDA16S3_RELA:
7319 case R_NDS32_SDA17S2_RELA:
7320 case R_NDS32_SDA18S1_RELA:
7321 case R_NDS32_SDA19S0_RELA:
7322 case R_NDS32_SDA15S3_RELA:
7323 case R_NDS32_SDA15S2_RELA:
7324 case R_NDS32_SDA12S2_DP_RELA:
7325 case R_NDS32_SDA12S2_SP_RELA:
7326 case R_NDS32_SDA15S1_RELA:
7327 case R_NDS32_SDA15S0_RELA:
7328 case R_NDS32_SDA_FP7U2_RELA:
7329 case R_NDS32_15_PCREL_RELA:
7330 case R_NDS32_17_PCREL_RELA:
7331 case R_NDS32_25_PCREL_RELA:
7332
7333 if (h != NULL && !bfd_link_pic (info))
7334 {
7335 h->non_got_ref = 1;
7336 h->plt.refcount += 1;
7337 }
7338
7339 /* If we are creating a shared library, and this is a reloc against
7340 a global symbol, or a non PC relative reloc against a local
7341 symbol, then we need to copy the reloc into the shared library.
7342 However, if we are linking with -Bsymbolic, we do not need to
7343 copy a reloc against a global symbol which is defined in an
7344 object we are including in the link (i.e., DEF_REGULAR is set).
7345 At this point we have not seen all the input files, so it is
7346 possible that DEF_REGULAR is not set now but will be set later
7347 (it is never cleared). We account for that possibility below by
7348 storing information in the dyn_relocs field of the hash table
7349 entry. A similar situation occurs when creating shared libraries
7350 and symbol visibility changes render the symbol local.
7351
7352 If on the other hand, we are creating an executable, we may need
7353 to keep relocations for symbols satisfied by a dynamic library
7354 if we manage to avoid copy relocs for the symbol. */
7355 if ((bfd_link_pic (info)
7356 && (sec->flags & SEC_ALLOC) != 0
7357 && ((r_type != R_NDS32_25_PCREL_RELA
7358 && r_type != R_NDS32_15_PCREL_RELA
7359 && r_type != R_NDS32_17_PCREL_RELA
7360 && !(r_type == R_NDS32_32_RELA
7361 && strcmp (sec->name, ".eh_frame") == 0))
7362 || (h != NULL
7363 && (!info->symbolic
7364 || h->root.type == bfd_link_hash_defweak
7365 || !h->def_regular))))
7366 || (!bfd_link_pic (info)
7367 && (sec->flags & SEC_ALLOC) != 0
7368 && h != NULL
7369 && (h->root.type == bfd_link_hash_defweak
7370 || !h->def_regular)))
7371 {
7372 struct elf_dyn_relocs *p;
7373 struct elf_dyn_relocs **head;
7374
7375 if (dynobj == NULL)
7376 htab->root.dynobj = dynobj = abfd;
7377
7378 /* When creating a shared object, we must copy these
7379 relocs into the output file. We create a reloc
7380 section in dynobj and make room for the reloc. */
7381 if (sreloc == NULL)
7382 {
7383 const char *name;
7384
7385 name = bfd_elf_string_from_elf_section
7386 (abfd, elf_elfheader (abfd)->e_shstrndx,
7387 elf_section_data (sec)->rela.hdr->sh_name);
7388 if (name == NULL)
7389 return FALSE;
7390
7391 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
7392 && strcmp (bfd_section_name (sec),
7393 name + 5) == 0);
7394
7395 sreloc = bfd_get_section_by_name (dynobj, name);
7396 if (sreloc == NULL)
7397 {
7398 flagword flags;
7399
7400 sreloc = bfd_make_section (dynobj, name);
7401 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7402 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7403 if ((sec->flags & SEC_ALLOC) != 0)
7404 flags |= SEC_ALLOC | SEC_LOAD;
7405 if (sreloc == NULL
7406 || !bfd_set_section_flags (sreloc, flags)
7407 || !bfd_set_section_alignment (sreloc, 2))
7408 return FALSE;
7409
7410 elf_section_type (sreloc) = SHT_RELA;
7411 }
7412 elf_section_data (sec)->sreloc = sreloc;
7413 }
7414
7415 /* If this is a global symbol, we count the number of
7416 relocations we need for this symbol. */
7417 if (h != NULL)
7418 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
7419 else
7420 {
7421 asection *s;
7422 void *vpp;
7423
7424 Elf_Internal_Sym *isym;
7425 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7426 if (isym == NULL)
7427 return FALSE;
7428
7429 /* Track dynamic relocs needed for local syms too. */
7430 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7431 if (s == NULL)
7432 return FALSE;
7433
7434 vpp = &elf_section_data (s)->local_dynrel;
7435 head = (struct elf_dyn_relocs **) vpp;
7436 }
7437
7438 p = *head;
7439 if (p == NULL || p->sec != sec)
7440 {
7441 bfd_size_type amt = sizeof (*p);
7442 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7443 if (p == NULL)
7444 return FALSE;
7445 p->next = *head;
7446 *head = p;
7447 p->sec = sec;
7448 p->count = 0;
7449 p->pc_count = 0;
7450 }
7451
7452 p->count += 1;
7453
7454 /* Since eh_frame is readonly, R_NDS32_32_RELA
7455 reloc for eh_frame will cause shared library has
7456 TEXTREL entry in the dynamic section. This lead glibc
7457 testsuites to failure (bug-13092) and cause kernel fail
7458 (bug-11819). I think the best solution is to replace
7459 absolute reloc with pc relative reloc in the eh_frame.
7460 To do that, we need to support the following issues:
7461
7462 === For GCC ===
7463 * gcc/config/nds32/nds32.h: Define
7464 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7465 and DW_EH_PE_sdata4 into DWARF exception header when
7466 option have '-fpic'.
7467
7468 === For binutils ===
7469 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7470 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7471 may break our nds DIFF mechanism, therefore, we
7472 must disable all linker relaxations to ensure
7473 correctness.
7474 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7475 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7476 do the necessary modification.
7477
7478 Unfortunately, it still have some problems for nds32
7479 to support pc relative reloc in the eh_frame. So I use
7480 another solution to fix this issue.
7481
7482 However, I find that ld always emit TEXTREL marker for
7483 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7484 correspond to R_NDS32_32_RELA for .eh_frame section.
7485 It means that we always reserve redundant entries of rel.dyn
7486 for these relocs which actually do nothing in dynamic linker.
7487
7488 Therefore, we regard these relocs as pc relative relocs
7489 here and increase the pc_count. */
7490 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7491 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7492 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7493 || (r_type == R_NDS32_32_RELA
7494 && strcmp (sec->name, ".eh_frame") == 0))
7495 p->pc_count += 1;
7496 }
7497 break;
7498
7499 /* This relocation describes the C++ object vtable hierarchy.
7500 Reconstruct it for later use during GC. */
7501 case R_NDS32_RELA_GNU_VTINHERIT:
7502 case R_NDS32_GNU_VTINHERIT:
7503 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7504 return FALSE;
7505 break;
7506
7507 /* This relocation describes which C++ vtable entries are actually
7508 used. Record for later use during GC. */
7509 case R_NDS32_GNU_VTENTRY:
7510 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7511 return FALSE;
7512 break;
7513 case R_NDS32_RELA_GNU_VTENTRY:
7514 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7515 return FALSE;
7516 break;
7517 }
7518 }
7519
7520 return TRUE;
7521 }
7522
7523 /* Write VAL in uleb128 format to P, returning a pointer to the
7524 following byte.
7525 This code is copied from elf-attr.c. */
7526
7527 static bfd_byte *
7528 write_uleb128 (bfd_byte *p, unsigned int val)
7529 {
7530 bfd_byte c;
7531 do
7532 {
7533 c = val & 0x7f;
7534 val >>= 7;
7535 if (val)
7536 c |= 0x80;
7537 *(p++) = c;
7538 }
7539 while (val);
7540 return p;
7541 }
7542
7543 static bfd_signed_vma
7544 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7545 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7546 {
7547 bfd_signed_vma foff;
7548 bfd_vma symval, addend;
7549 asection *sym_sec;
7550
7551 /* Get the value of the symbol referred to by the reloc. */
7552 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7553 {
7554 Elf_Internal_Sym *isym;
7555
7556 /* A local symbol. */
7557 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7558
7559 if (isym->st_shndx == SHN_UNDEF)
7560 sym_sec = bfd_und_section_ptr;
7561 else if (isym->st_shndx == SHN_ABS)
7562 sym_sec = bfd_abs_section_ptr;
7563 else if (isym->st_shndx == SHN_COMMON)
7564 sym_sec = bfd_com_section_ptr;
7565 else
7566 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7567 symval = isym->st_value + sym_sec->output_section->vma
7568 + sym_sec->output_offset;
7569 }
7570 else
7571 {
7572 unsigned long indx;
7573 struct elf_link_hash_entry *h;
7574
7575 /* An external symbol. */
7576 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7577 h = elf_sym_hashes (abfd)[indx];
7578 BFD_ASSERT (h != NULL);
7579
7580 if (h->root.type != bfd_link_hash_defined
7581 && h->root.type != bfd_link_hash_defweak)
7582 /* This appears to be a reference to an undefined
7583 symbol. Just ignore it--it will be caught by the
7584 regular reloc processing. */
7585 return 0;
7586
7587 if (h->root.u.def.section->flags & SEC_MERGE)
7588 {
7589 sym_sec = h->root.u.def.section;
7590 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7591 elf_section_data (sym_sec)->sec_info,
7592 h->root.u.def.value);
7593 symval = symval + sym_sec->output_section->vma
7594 + sym_sec->output_offset;
7595 }
7596 else
7597 symval = (h->root.u.def.value
7598 + h->root.u.def.section->output_section->vma
7599 + h->root.u.def.section->output_offset);
7600 }
7601
7602 addend = irel->r_addend;
7603
7604 foff = (symval + addend
7605 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7606 return foff;
7607 }
7608 \f
7609
7610 /* Convert a 32-bit instruction to 16-bit one.
7611 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7612 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7613 type of INSN16. Return 1 if successful. */
7614
7615 static int
7616 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7617 int *pinsn_type)
7618 {
7619 uint16_t insn16 = 0;
7620 int insn_type = 0;
7621 unsigned long mach = bfd_get_mach (abfd);
7622
7623 if (N32_SH5 (insn) != 0)
7624 return 0;
7625
7626 switch (N32_SUB5 (insn))
7627 {
7628 case N32_ALU1_ADD_SLLI:
7629 case N32_ALU1_ADD_SRLI:
7630 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7631 {
7632 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7633 N32_RB5 (insn));
7634 insn_type = NDS32_INSN_ADD333;
7635 }
7636 else if (N32_IS_RT4 (insn))
7637 {
7638 if (N32_RT5 (insn) == N32_RA5 (insn))
7639 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7640 else if (N32_RT5 (insn) == N32_RB5 (insn))
7641 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7642 insn_type = NDS32_INSN_ADD45;
7643 }
7644 break;
7645
7646 case N32_ALU1_SUB_SLLI:
7647 case N32_ALU1_SUB_SRLI:
7648 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7649 {
7650 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7651 N32_RB5 (insn));
7652 insn_type = NDS32_INSN_SUB333;
7653 }
7654 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7655 {
7656 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7657 insn_type = NDS32_INSN_SUB45;
7658 }
7659 break;
7660
7661 case N32_ALU1_AND_SLLI:
7662 case N32_ALU1_AND_SRLI:
7663 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7664 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7665 && N32_IS_RB3 (insn))
7666 {
7667 if (N32_RT5 (insn) == N32_RA5 (insn))
7668 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7669 else if (N32_RT5 (insn) == N32_RB5 (insn))
7670 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7671 if (insn16)
7672 insn_type = NDS32_INSN_AND33;
7673 }
7674 break;
7675
7676 case N32_ALU1_XOR_SLLI:
7677 case N32_ALU1_XOR_SRLI:
7678 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7679 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7680 && N32_IS_RB3 (insn))
7681 {
7682 if (N32_RT5 (insn) == N32_RA5 (insn))
7683 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7684 else if (N32_RT5 (insn) == N32_RB5 (insn))
7685 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7686 if (insn16)
7687 insn_type = NDS32_INSN_XOR33;
7688 }
7689 break;
7690
7691 case N32_ALU1_OR_SLLI:
7692 case N32_ALU1_OR_SRLI:
7693 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7694 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7695 && N32_IS_RB3 (insn))
7696 {
7697 if (N32_RT5 (insn) == N32_RA5 (insn))
7698 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7699 else if (N32_RT5 (insn) == N32_RB5 (insn))
7700 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7701 if (insn16)
7702 insn_type = NDS32_INSN_OR33;
7703 }
7704 break;
7705 case N32_ALU1_NOR:
7706 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7707 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7708 && N32_RA5 (insn) == N32_RB5 (insn))
7709 {
7710 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7711 insn_type = NDS32_INSN_NOT33;
7712 }
7713 break;
7714 case N32_ALU1_SRAI:
7715 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7716 {
7717 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7718 insn_type = NDS32_INSN_SRAI45;
7719 }
7720 break;
7721
7722 case N32_ALU1_SRLI:
7723 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7724 {
7725 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7726 insn_type = NDS32_INSN_SRLI45;
7727 }
7728 break;
7729
7730 case N32_ALU1_SLLI:
7731 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7732 {
7733 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7734 N32_UB5 (insn));
7735 insn_type = NDS32_INSN_SLLI333;
7736 }
7737 break;
7738
7739 case N32_ALU1_ZEH:
7740 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7741 {
7742 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7743 insn_type = NDS32_INSN_ZEH33;
7744 }
7745 break;
7746
7747 case N32_ALU1_SEB:
7748 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7749 {
7750 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7751 insn_type = NDS32_INSN_SEB33;
7752 }
7753 break;
7754
7755 case N32_ALU1_SEH:
7756 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7757 {
7758 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7759 insn_type = NDS32_INSN_SEH33;
7760 }
7761 break;
7762
7763 case N32_ALU1_SLT:
7764 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7765 {
7766 /* Implicit r15. */
7767 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7768 insn_type = NDS32_INSN_SLT45;
7769 }
7770 break;
7771
7772 case N32_ALU1_SLTS:
7773 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7774 {
7775 /* Implicit r15. */
7776 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7777 insn_type = NDS32_INSN_SLTS45;
7778 }
7779 break;
7780 }
7781
7782 if ((insn16 & 0x8000) == 0)
7783 return 0;
7784
7785 if (pinsn16)
7786 *pinsn16 = insn16;
7787 if (pinsn_type)
7788 *pinsn_type = insn_type;
7789 return 1;
7790 }
7791
7792 static int
7793 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7794 int *pinsn_type)
7795 {
7796 uint16_t insn16 = 0;
7797 int insn_type;
7798 unsigned long mach = bfd_get_mach (abfd);
7799
7800 /* TODO: bset, bclr, btgl, btst. */
7801 if (__GF (insn, 6, 4) != 0)
7802 return 0;
7803
7804 switch (N32_IMMU (insn, 6))
7805 {
7806 case N32_ALU2_MUL:
7807 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7808 && N32_IS_RB3 (insn))
7809 {
7810 if (N32_RT5 (insn) == N32_RA5 (insn))
7811 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7812 else if (N32_RT5 (insn) == N32_RB5 (insn))
7813 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7814 if (insn16)
7815 insn_type = NDS32_INSN_MUL33;
7816 }
7817 }
7818
7819 if ((insn16 & 0x8000) == 0)
7820 return 0;
7821
7822 if (pinsn16)
7823 *pinsn16 = insn16;
7824 if (pinsn_type)
7825 *pinsn_type = insn_type;
7826 return 1;
7827 }
7828
7829 int
7830 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7831 int *pinsn_type)
7832 {
7833 int op6;
7834 uint16_t insn16 = 0;
7835 int insn_type = 0;
7836 unsigned long mach = bfd_get_mach (abfd);
7837
7838 /* Decode 32-bit instruction. */
7839 if (insn & 0x80000000)
7840 {
7841 /* Not 32-bit insn. */
7842 return 0;
7843 }
7844
7845 op6 = N32_OP6 (insn);
7846
7847 /* Convert it to 16-bit instruction. */
7848 switch (op6)
7849 {
7850 case N32_OP6_MOVI:
7851 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7852 {
7853 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7854 insn_type = NDS32_INSN_MOVI55;
7855 }
7856 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7857 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7858 {
7859 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7860 N32_IMM20S (insn) - 16);
7861 insn_type = NDS32_INSN_MOVPI45;
7862 }
7863 break;
7864
7865 case N32_OP6_ADDI:
7866 if (N32_IMM15S (insn) == 0)
7867 {
7868 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7869 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7870 if (mach <= MACH_V2
7871 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7872 {
7873 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7874 insn_type = NDS32_INSN_MOV55;
7875 }
7876 }
7877 else if (N32_IMM15S (insn) > 0)
7878 {
7879 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7880 {
7881 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7882 N32_IMM15S (insn));
7883 insn_type = NDS32_INSN_ADDI333;
7884 }
7885 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7886 && N32_IMM15S (insn) < 32)
7887 {
7888 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7889 insn_type = NDS32_INSN_ADDI45;
7890 }
7891 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7892 && N32_RT5 (insn) == N32_RA5 (insn)
7893 && N32_IMM15S (insn) < 512)
7894 {
7895 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7896 insn_type = NDS32_INSN_ADDI10_SP;
7897 }
7898 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7899 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7900 && (N32_IMM15S (insn) % 4 == 0))
7901 {
7902 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7903 N32_IMM15S (insn) >> 2);
7904 insn_type = NDS32_INSN_ADDRI36_SP;
7905 }
7906 }
7907 else
7908 {
7909 /* Less than 0. */
7910 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7911 {
7912 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7913 0 - N32_IMM15S (insn));
7914 insn_type = NDS32_INSN_SUBI333;
7915 }
7916 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7917 && N32_IMM15S (insn) > -32)
7918 {
7919 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7920 0 - N32_IMM15S (insn));
7921 insn_type = NDS32_INSN_SUBI45;
7922 }
7923 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7924 && N32_RT5 (insn) == N32_RA5 (insn)
7925 && N32_IMM15S (insn) >= -512)
7926 {
7927 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7928 insn_type = NDS32_INSN_ADDI10_SP;
7929 }
7930 }
7931 break;
7932
7933 case N32_OP6_ORI:
7934 if (N32_IMM15S (insn) == 0)
7935 {
7936 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7937 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7938 if (mach <= MACH_V2
7939 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7940 {
7941 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7942 insn_type = NDS32_INSN_MOV55;
7943 }
7944 }
7945 break;
7946
7947 case N32_OP6_SUBRI:
7948 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7949 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7950 {
7951 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7952 insn_type = NDS32_INSN_NEG33;
7953 }
7954 break;
7955
7956 case N32_OP6_ANDI:
7957 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7958 {
7959 if (N32_IMM15U (insn) == 1)
7960 {
7961 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7962 insn_type = NDS32_INSN_XLSB33;
7963 }
7964 else if (N32_IMM15U (insn) == 0x7ff)
7965 {
7966 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7967 insn_type = NDS32_INSN_X11B33;
7968 }
7969 else if (N32_IMM15U (insn) == 0xff)
7970 {
7971 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7972 insn_type = NDS32_INSN_ZEB33;
7973 }
7974 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7975 && N32_IMM15U (insn) < 256)
7976 {
7977 int imm15u = N32_IMM15U (insn);
7978
7979 if (__builtin_popcount (imm15u) == 1)
7980 {
7981 /* BMSKI33 */
7982 int imm3u = __builtin_ctz (imm15u);
7983
7984 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7985 insn_type = NDS32_INSN_BMSKI33;
7986 }
7987 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7988 {
7989 /* FEXTI33 */
7990 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7991
7992 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7993 insn_type = NDS32_INSN_FEXTI33;
7994 }
7995 }
7996 }
7997 break;
7998
7999 case N32_OP6_SLTI:
8000 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8001 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8002 {
8003 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8004 insn_type = NDS32_INSN_SLTI45;
8005 }
8006 break;
8007
8008 case N32_OP6_SLTSI:
8009 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8010 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8011 {
8012 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8013 insn_type = NDS32_INSN_SLTSI45;
8014 }
8015 break;
8016
8017 case N32_OP6_LWI:
8018 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8019 {
8020 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8021 insn_type = NDS32_INSN_LWI450;
8022 }
8023 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8024 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8025 {
8026 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8027 N32_IMM15S (insn));
8028 insn_type = NDS32_INSN_LWI333;
8029 }
8030 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8031 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8032 {
8033 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8034 insn_type = NDS32_INSN_LWI37;
8035 }
8036 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8037 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8038 {
8039 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8040 insn_type = NDS32_INSN_LWI37_SP;
8041 }
8042 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8043 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8044 {
8045 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8046 N32_IMM15S (insn) + 32);
8047 insn_type = NDS32_INSN_LWI45_FE;
8048 }
8049 break;
8050
8051 case N32_OP6_SWI:
8052 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8053 {
8054 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8055 insn_type = NDS32_INSN_SWI450;
8056 }
8057 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8058 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8059 {
8060 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8061 N32_IMM15S (insn));
8062 insn_type = NDS32_INSN_SWI333;
8063 }
8064 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8065 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8066 {
8067 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8068 insn_type = NDS32_INSN_SWI37;
8069 }
8070 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8071 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8072 {
8073 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8074 insn_type = NDS32_INSN_SWI37_SP;
8075 }
8076 break;
8077
8078 case N32_OP6_LWI_BI:
8079 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8080 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8081 {
8082 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8083 N32_IMM15S (insn));
8084 insn_type = NDS32_INSN_LWI333_BI;
8085 }
8086 break;
8087
8088 case N32_OP6_SWI_BI:
8089 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8090 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8091 {
8092 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8093 N32_IMM15S (insn));
8094 insn_type = NDS32_INSN_SWI333_BI;
8095 }
8096 break;
8097
8098 case N32_OP6_LHI:
8099 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8100 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8101 {
8102 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8103 N32_IMM15S (insn));
8104 insn_type = NDS32_INSN_LHI333;
8105 }
8106 break;
8107
8108 case N32_OP6_SHI:
8109 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8110 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8111 {
8112 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8113 N32_IMM15S (insn));
8114 insn_type = NDS32_INSN_SHI333;
8115 }
8116 break;
8117
8118 case N32_OP6_LBI:
8119 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8120 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8121 {
8122 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8123 N32_IMM15S (insn));
8124 insn_type = NDS32_INSN_LBI333;
8125 }
8126 break;
8127
8128 case N32_OP6_SBI:
8129 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8130 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8131 {
8132 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8133 N32_IMM15S (insn));
8134 insn_type = NDS32_INSN_SBI333;
8135 }
8136 break;
8137
8138 case N32_OP6_ALU1:
8139 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8140
8141 case N32_OP6_ALU2:
8142 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8143
8144 case N32_OP6_BR1:
8145 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8146 goto done;
8147
8148 if ((insn & N32_BIT (14)) == 0)
8149 {
8150 /* N32_BR1_BEQ */
8151 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8152 && N32_RT5 (insn) != REG_R5)
8153 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8154 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8155 && N32_RA5 (insn) != REG_R5)
8156 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8157 insn_type = NDS32_INSN_BEQS38;
8158 break;
8159 }
8160 else
8161 {
8162 /* N32_BR1_BNE */
8163 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8164 && N32_RT5 (insn) != REG_R5)
8165 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8166 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8167 && N32_RA5 (insn) != REG_R5)
8168 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8169 insn_type = NDS32_INSN_BNES38;
8170 break;
8171 }
8172 break;
8173
8174 case N32_OP6_BR2:
8175 switch (N32_BR2_SUB (insn))
8176 {
8177 case N32_BR2_BEQZ:
8178 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8179 {
8180 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8181 insn_type = NDS32_INSN_BEQZ38;
8182 }
8183 else if (N32_RT5 (insn) == REG_R15
8184 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8185 {
8186 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8187 insn_type = NDS32_INSN_BEQZS8;
8188 }
8189 break;
8190
8191 case N32_BR2_BNEZ:
8192 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8193 {
8194 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8195 insn_type = NDS32_INSN_BNEZ38;
8196 }
8197 else if (N32_RT5 (insn) == REG_R15
8198 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8199 {
8200 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8201 insn_type = NDS32_INSN_BNEZS8;
8202 }
8203 break;
8204
8205 case N32_BR2_SOP0:
8206 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8207 {
8208 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8209 insn_type = NDS32_INSN_IFCALL9;
8210 }
8211 break;
8212 }
8213 break;
8214
8215 case N32_OP6_JI:
8216 if ((insn & N32_BIT (24)) == 0)
8217 {
8218 /* N32_JI_J */
8219 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8220 {
8221 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8222 insn_type = NDS32_INSN_J8;
8223 }
8224 }
8225 break;
8226
8227 case N32_OP6_JREG:
8228 if (__GF (insn, 8, 2) != 0)
8229 goto done;
8230
8231 switch (N32_IMMU (insn, 5))
8232 {
8233 case N32_JREG_JR:
8234 if (N32_JREG_HINT (insn) == 0)
8235 {
8236 /* jr */
8237 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8238 insn_type = NDS32_INSN_JR5;
8239 }
8240 else if (N32_JREG_HINT (insn) == 1)
8241 {
8242 /* ret */
8243 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8244 insn_type = NDS32_INSN_RET5;
8245 }
8246 else if (N32_JREG_HINT (insn) == 3)
8247 {
8248 /* ifret = mov55 $sp, $sp */
8249 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8250 insn_type = NDS32_INSN_IFRET;
8251 }
8252 break;
8253
8254 case N32_JREG_JRAL:
8255 /* It's convertible when return rt5 is $lp and address
8256 translation is kept. */
8257 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8258 {
8259 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8260 insn_type = NDS32_INSN_JRAL5;
8261 }
8262 break;
8263 }
8264 break;
8265
8266 case N32_OP6_MISC:
8267 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8268 {
8269 /* For v3, swid above 31 are used for ex9.it. */
8270 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8271 insn_type = NDS32_INSN_BREAK16;
8272 }
8273 break;
8274
8275 default:
8276 /* This instruction has no 16-bit variant. */
8277 goto done;
8278 }
8279
8280 done:
8281 /* Bit-15 of insn16 should be set for a valid instruction. */
8282 if ((insn16 & 0x8000) == 0)
8283 return 0;
8284
8285 if (pinsn16)
8286 *pinsn16 = insn16;
8287 if (pinsn_type)
8288 *pinsn_type = insn_type;
8289 return 1;
8290 }
8291
8292 static int
8293 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8294 Elf_Internal_Rela *reloc)
8295 {
8296 uint16_t insn16 = 0;
8297
8298 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8299 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8300 return 0;
8301
8302 if (!N32_IS_RT3 (insn))
8303 return 0;
8304
8305 switch (N32_OP6 (insn))
8306 {
8307 case N32_OP6_LWI:
8308 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8309 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8310 break;
8311 case N32_OP6_SWI:
8312 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8313 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8314 break;
8315 case N32_OP6_HWGP:
8316 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8317 break;
8318
8319 if (__GF (insn, 17, 3) == 6)
8320 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8321 else if (__GF (insn, 17, 3) == 7)
8322 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8323 break;
8324 }
8325
8326 if ((insn16 & 0x8000) == 0)
8327 return 0;
8328
8329 *pinsn16 = insn16;
8330 return 1;
8331 }
8332
8333 /* Convert a 16-bit instruction to 32-bit one.
8334 INSN16 it the input and PINSN it the point to output.
8335 Return non-zero on successful. Otherwise 0 is returned. */
8336
8337 int
8338 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8339 {
8340 uint32_t insn = 0xffffffff;
8341 unsigned long mach = bfd_get_mach (abfd);
8342
8343 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8344
8345 switch (__GF (insn16, 9, 6))
8346 {
8347 case 0x4: /* add45 */
8348 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8349 N16_RA5 (insn16));
8350 goto done;
8351 case 0x5: /* sub45 */
8352 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8353 N16_RA5 (insn16));
8354 goto done;
8355 case 0x6: /* addi45 */
8356 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8357 N16_IMM5U (insn16));
8358 goto done;
8359 case 0x7: /* subi45 */
8360 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8361 -N16_IMM5U (insn16));
8362 goto done;
8363 case 0x8: /* srai45 */
8364 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8365 N16_IMM5U (insn16));
8366 goto done;
8367 case 0x9: /* srli45 */
8368 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8369 N16_IMM5U (insn16));
8370 goto done;
8371 case 0xa: /* slli333 */
8372 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8373 N16_IMM3U (insn16));
8374 goto done;
8375 case 0xc: /* add333 */
8376 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8377 N16_RB3 (insn16));
8378 goto done;
8379 case 0xd: /* sub333 */
8380 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8381 N16_RB3 (insn16));
8382 goto done;
8383 case 0xe: /* addi333 */
8384 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8385 N16_IMM3U (insn16));
8386 goto done;
8387 case 0xf: /* subi333 */
8388 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8389 -N16_IMM3U (insn16));
8390 goto done;
8391 case 0x10: /* lwi333 */
8392 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8393 N16_IMM3U (insn16));
8394 goto done;
8395 case 0x12: /* lhi333 */
8396 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8397 N16_IMM3U (insn16));
8398 goto done;
8399 case 0x13: /* lbi333 */
8400 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8401 N16_IMM3U (insn16));
8402 goto done;
8403 case 0x11: /* lwi333.bi */
8404 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8405 N16_IMM3U (insn16));
8406 goto done;
8407 case 0x14: /* swi333 */
8408 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8409 N16_IMM3U (insn16));
8410 goto done;
8411 case 0x16: /* shi333 */
8412 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8413 N16_IMM3U (insn16));
8414 goto done;
8415 case 0x17: /* sbi333 */
8416 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8417 N16_IMM3U (insn16));
8418 goto done;
8419 case 0x15: /* swi333.bi */
8420 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8421 N16_IMM3U (insn16));
8422 goto done;
8423 case 0x18: /* addri36.sp */
8424 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8425 N16_IMM6U (insn16) << 2);
8426 goto done;
8427 case 0x19: /* lwi45.fe */
8428 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8429 (N16_IMM5U (insn16) - 32));
8430 goto done;
8431 case 0x1a: /* lwi450 */
8432 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8433 goto done;
8434 case 0x1b: /* swi450 */
8435 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8436 goto done;
8437
8438 /* These are r15 implied instructions. */
8439 case 0x30: /* slts45 */
8440 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8441 goto done;
8442 case 0x31: /* slt45 */
8443 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8444 goto done;
8445 case 0x32: /* sltsi45 */
8446 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8447 goto done;
8448 case 0x33: /* slti45 */
8449 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8450 goto done;
8451 case 0x34: /* beqzs8, bnezs8 */
8452 if (insn16 & N32_BIT (8))
8453 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8454 else
8455 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8456 goto done;
8457
8458 case 0x35: /* break16, ex9.it */
8459 /* Only consider range of v3 break16. */
8460 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8461 goto done;
8462
8463 case 0x3c: /* ifcall9 */
8464 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8465 goto done;
8466 case 0x3d: /* movpi45 */
8467 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8468 goto done;
8469
8470 case 0x3f: /* MISC33 */
8471 switch (insn16 & 0x7)
8472 {
8473 case 2: /* neg33 */
8474 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8475 break;
8476 case 3: /* not33 */
8477 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8478 N16_RA3 (insn16));
8479 break;
8480 case 4: /* mul33 */
8481 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8482 N16_RA3 (insn16));
8483 break;
8484 case 5: /* xor33 */
8485 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8486 N16_RA3 (insn16));
8487 break;
8488 case 6: /* and33 */
8489 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8490 N16_RA3 (insn16));
8491 break;
8492 case 7: /* or33 */
8493 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8494 N16_RA3 (insn16));
8495 break;
8496 }
8497 goto done;
8498
8499 case 0xb:
8500 switch (insn16 & 0x7)
8501 {
8502 case 0: /* zeb33 */
8503 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8504 break;
8505 case 1: /* zeh33 */
8506 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8507 break;
8508 case 2: /* seb33 */
8509 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8510 break;
8511 case 3: /* seh33 */
8512 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8513 break;
8514 case 4: /* xlsb33 */
8515 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8516 break;
8517 case 5: /* x11b33 */
8518 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8519 break;
8520 case 6: /* bmski33 */
8521 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8522 1 << __GF (insn16, 3, 3));
8523 break;
8524 case 7: /* fexti33 */
8525 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8526 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8527 break;
8528 }
8529 goto done;
8530 }
8531
8532 switch (__GF (insn16, 10, 5))
8533 {
8534 case 0x0: /* mov55 or ifret16 */
8535 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8536 && N16_RT5 (insn16) == N16_RA5 (insn16))
8537 insn = N32_JREG (JR, 0, 0, 0, 3);
8538 else
8539 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8540 goto done;
8541 case 0x1: /* movi55 */
8542 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8543 goto done;
8544 case 0x1b: /* addi10s (V2) */
8545 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8546 goto done;
8547 }
8548
8549 switch (__GF (insn16, 11, 4))
8550 {
8551 case 0x7: /* lwi37.fp/swi37.fp */
8552 if (insn16 & N32_BIT (7)) /* swi37.fp */
8553 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8554 else /* lwi37.fp */
8555 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8556 goto done;
8557 case 0x8: /* beqz38 */
8558 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8559 goto done;
8560 case 0x9: /* bnez38 */
8561 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8562 goto done;
8563 case 0xa: /* beqs38/j8, implied r5 */
8564 if (N16_RT38 (insn16) == 5)
8565 insn = N32_JI (J, N16_IMM8S (insn16));
8566 else
8567 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8568 goto done;
8569 case 0xb: /* bnes38 and others. */
8570 if (N16_RT38 (insn16) == 5)
8571 {
8572 switch (__GF (insn16, 5, 3))
8573 {
8574 case 0: /* jr5 */
8575 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8576 break;
8577 case 4: /* ret5 */
8578 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8579 break;
8580 case 1: /* jral5 */
8581 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8582 break;
8583 case 2: /* ex9.it imm5 */
8584 /* ex9.it had no 32-bit variantl. */
8585 break;
8586 case 5: /* add5.pc */
8587 /* add5.pc had no 32-bit variantl. */
8588 break;
8589 }
8590 }
8591 else /* bnes38 */
8592 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8593 goto done;
8594 case 0xe: /* lwi37/swi37 */
8595 if (insn16 & (1 << 7)) /* swi37.sp */
8596 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8597 else /* lwi37.sp */
8598 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8599 goto done;
8600 }
8601
8602 done:
8603 if (insn & 0x80000000)
8604 return 0;
8605
8606 if (pinsn)
8607 *pinsn = insn;
8608 return 1;
8609 }
8610 \f
8611
8612 static bfd_boolean
8613 is_sda_access_insn (unsigned long insn)
8614 {
8615 switch (N32_OP6 (insn))
8616 {
8617 case N32_OP6_LWI:
8618 case N32_OP6_LHI:
8619 case N32_OP6_LHSI:
8620 case N32_OP6_LBI:
8621 case N32_OP6_LBSI:
8622 case N32_OP6_SWI:
8623 case N32_OP6_SHI:
8624 case N32_OP6_SBI:
8625 case N32_OP6_LWC:
8626 case N32_OP6_LDC:
8627 case N32_OP6_SWC:
8628 case N32_OP6_SDC:
8629 return TRUE;
8630 default:
8631 ;
8632 }
8633 return FALSE;
8634 }
8635
8636 static unsigned long
8637 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8638 {
8639 uint32_t oinsn = 0;
8640
8641 switch (type)
8642 {
8643 case R_NDS32_GOT_LO12:
8644 case R_NDS32_GOTOFF_LO12:
8645 case R_NDS32_PLTREL_LO12:
8646 case R_NDS32_PLT_GOTREL_LO12:
8647 case R_NDS32_LO12S0_RELA:
8648 switch (N32_OP6 (insn))
8649 {
8650 case N32_OP6_LBI:
8651 /* lbi.gp */
8652 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8653 break;
8654 case N32_OP6_LBSI:
8655 /* lbsi.gp */
8656 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8657 break;
8658 case N32_OP6_SBI:
8659 /* sbi.gp */
8660 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8661 break;
8662 case N32_OP6_ORI:
8663 /* addi.gp */
8664 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8665 break;
8666 }
8667 break;
8668
8669 case R_NDS32_LO12S1_RELA:
8670 switch (N32_OP6 (insn))
8671 {
8672 case N32_OP6_LHI:
8673 /* lhi.gp */
8674 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8675 break;
8676 case N32_OP6_LHSI:
8677 /* lhsi.gp */
8678 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8679 break;
8680 case N32_OP6_SHI:
8681 /* shi.gp */
8682 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8683 break;
8684 }
8685 break;
8686
8687 case R_NDS32_LO12S2_RELA:
8688 switch (N32_OP6 (insn))
8689 {
8690 case N32_OP6_LWI:
8691 /* lwi.gp */
8692 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8693 break;
8694 case N32_OP6_SWI:
8695 /* swi.gp */
8696 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8697 break;
8698 }
8699 break;
8700
8701 case R_NDS32_LO12S2_DP_RELA:
8702 case R_NDS32_LO12S2_SP_RELA:
8703 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8704 break;
8705 }
8706
8707 if (oinsn)
8708 *pinsn = oinsn;
8709
8710 return oinsn != 0;
8711 }
8712
8713 /* Linker hasn't found the correct merge section for non-section symbol
8714 in relax time, this work is left to the function elf_link_input_bfd().
8715 So for non-section symbol, _bfd_merged_section_offset is also needed
8716 to find the correct symbol address. */
8717
8718 static bfd_vma
8719 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8720 asection **psec, Elf_Internal_Rela *rel)
8721 {
8722 asection *sec = *psec;
8723 bfd_vma relocation;
8724
8725 relocation = (sec->output_section->vma
8726 + sec->output_offset + sym->st_value);
8727 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8728 {
8729 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8730 rel->r_addend =
8731 _bfd_merged_section_offset (abfd, psec,
8732 elf_section_data (sec)->sec_info,
8733 sym->st_value + rel->r_addend);
8734 else
8735 rel->r_addend =
8736 _bfd_merged_section_offset (abfd, psec,
8737 elf_section_data (sec)->sec_info,
8738 sym->st_value) + rel->r_addend;
8739
8740 if (sec != *psec)
8741 {
8742 /* If we have changed the section, and our original section is
8743 marked with SEC_EXCLUDE, it means that the original
8744 SEC_MERGE section has been completely subsumed in some
8745 other SEC_MERGE section. In this case, we need to leave
8746 some info around for --emit-relocs. */
8747 if ((sec->flags & SEC_EXCLUDE) != 0)
8748 sec->kept_section = *psec;
8749 sec = *psec;
8750 }
8751 rel->r_addend -= relocation;
8752 rel->r_addend += sec->output_section->vma + sec->output_offset;
8753 }
8754 return relocation;
8755 }
8756
8757 static bfd_vma
8758 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8759 Elf_Internal_Sym *isymbuf,
8760 Elf_Internal_Shdr *symtab_hdr)
8761 {
8762 bfd_signed_vma foff;
8763 bfd_vma symval, addend;
8764 Elf_Internal_Rela irel_fn;
8765 Elf_Internal_Sym *isym;
8766 asection *sym_sec;
8767
8768 /* Get the value of the symbol referred to by the reloc. */
8769 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8770 {
8771 /* A local symbol. */
8772 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8773
8774 if (isym->st_shndx == SHN_UNDEF)
8775 sym_sec = bfd_und_section_ptr;
8776 else if (isym->st_shndx == SHN_ABS)
8777 sym_sec = bfd_abs_section_ptr;
8778 else if (isym->st_shndx == SHN_COMMON)
8779 sym_sec = bfd_com_section_ptr;
8780 else
8781 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8782 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8783 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8784 addend = irel_fn.r_addend;
8785 }
8786 else
8787 {
8788 unsigned long indx;
8789 struct elf_link_hash_entry *h;
8790
8791 /* An external symbol. */
8792 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8793 h = elf_sym_hashes (abfd)[indx];
8794 BFD_ASSERT (h != NULL);
8795
8796 while (h->root.type == bfd_link_hash_indirect
8797 || h->root.type == bfd_link_hash_warning)
8798 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8799
8800 if (h->root.type != bfd_link_hash_defined
8801 && h->root.type != bfd_link_hash_defweak)
8802 /* This appears to be a reference to an undefined
8803 symbol. Just ignore it--it will be caught by the
8804 regular reloc processing. */
8805 return 0;
8806
8807 if (h->root.u.def.section->flags & SEC_MERGE)
8808 {
8809 sym_sec = h->root.u.def.section;
8810 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8811 (sym_sec)->sec_info, h->root.u.def.value);
8812 symval = symval + sym_sec->output_section->vma
8813 + sym_sec->output_offset;
8814 }
8815 else
8816 symval = (h->root.u.def.value
8817 + h->root.u.def.section->output_section->vma
8818 + h->root.u.def.section->output_offset);
8819 addend = irel->r_addend;
8820 }
8821
8822 foff = symval + addend;
8823
8824 return foff;
8825 }
8826
8827 static int
8828 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8829 asection *sec, Elf_Internal_Rela *rel)
8830 {
8831 bfd_byte *contents;
8832 unsigned short insn16;
8833
8834 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8835 return FALSE;
8836 contents = elf_section_data (sec)->this_hdr.contents;
8837 insn16 = bfd_getb16 (contents + rel->r_offset);
8838 if (insn16 == NDS32_NOP16)
8839 return TRUE;
8840 return FALSE;
8841 }
8842
8843 /* It checks whether the instruction could be converted to
8844 16-bit form and returns the converted one.
8845
8846 `internal_relocs' is supposed to be sorted. */
8847
8848 static int
8849 is_convert_32_to_16 (bfd *abfd, asection *sec,
8850 Elf_Internal_Rela *reloc,
8851 Elf_Internal_Rela *internal_relocs,
8852 Elf_Internal_Rela *irelend,
8853 uint16_t *insn16)
8854 {
8855 #define NORMAL_32_TO_16 (1 << 0)
8856 #define SPECIAL_32_TO_16 (1 << 1)
8857 bfd_byte *contents = NULL;
8858 bfd_signed_vma off;
8859 bfd_vma mem_addr;
8860 uint32_t insn = 0;
8861 Elf_Internal_Rela *pc_rel;
8862 Elf_Internal_Shdr *symtab_hdr;
8863 Elf_Internal_Sym *isymbuf = NULL;
8864 int convert_type;
8865 bfd_vma offset;
8866
8867 if (reloc->r_offset + 4 > sec->size)
8868 return FALSE;
8869
8870 offset = reloc->r_offset;
8871
8872 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
8873 return FALSE;
8874 insn = bfd_getb32 (contents + offset);
8875
8876 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8877 convert_type = NORMAL_32_TO_16;
8878 else if (special_convert_32_to_16 (insn, insn16, reloc))
8879 convert_type = SPECIAL_32_TO_16;
8880 else
8881 return FALSE;
8882
8883 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8884 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8885 return FALSE;
8886
8887 /* Find the first relocation of the same relocation-type,
8888 so we iteratie them forward. */
8889 pc_rel = reloc;
8890 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8891 pc_rel--;
8892
8893 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8894 {
8895 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8896 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8897 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8898 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8899 {
8900 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8901 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8902 || off == 0)
8903 return FALSE;
8904 break;
8905 }
8906 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8907 {
8908 /* movi => movi55 */
8909 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8910 symtab_hdr);
8911 /* mem_addr is unsigned, but the value should
8912 be between [-16, 15]. */
8913 if ((mem_addr + 0x10) >> 5)
8914 return FALSE;
8915 break;
8916 }
8917 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8918 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8919 {
8920 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8921 because it can be relaxed to addi for TLS_LE_ADD. */
8922 return FALSE;
8923 }
8924 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8925 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8926 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8927 && convert_type == SPECIAL_32_TO_16)
8928 {
8929 /* fp-as-gp
8930 We've selected a best fp-base for this access, so we can
8931 always resolve it anyway. Do nothing. */
8932 break;
8933 }
8934 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8935 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8936 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8937 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8938 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8939 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8940 {
8941 /* Prevent unresolved addi instruction translate
8942 to addi45 or addi333. */
8943 return FALSE;
8944 }
8945 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8946 {
8947 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8948 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8949 return FALSE;
8950 break;
8951 }
8952 }
8953
8954 return TRUE;
8955 }
8956
8957 static void
8958 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8959 Elf_Internal_Rela *reloc,
8960 Elf_Internal_Rela *internal_relocs,
8961 Elf_Internal_Rela *irelend,
8962 unsigned short insn16)
8963 {
8964 Elf_Internal_Rela *pc_rel;
8965 bfd_vma offset;
8966
8967 offset = reloc->r_offset;
8968 bfd_putb16 (insn16, contents + offset);
8969 /* Find the first relocation of the same relocation-type,
8970 so we iteratie them forward. */
8971 pc_rel = reloc;
8972 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8973 pc_rel--;
8974
8975 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8976 {
8977 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8978 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8979 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8980 {
8981 pc_rel->r_info =
8982 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8983 }
8984 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8985 pc_rel->r_info =
8986 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8987 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8988 pc_rel->r_info =
8989 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8990 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8991 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8992 pc_rel->r_info =
8993 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8994 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8995 pc_rel->r_info =
8996 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8997 }
8998 }
8999
9000 /* Find a relocation of type specified by `reloc_type'
9001 of the same r_offset with reloc.
9002 If not found, return irelend.
9003
9004 Assuming relocations are sorted by r_offset,
9005 we find the relocation from `reloc' backward untill relocs,
9006 or find it from `reloc' forward untill irelend. */
9007
9008 static Elf_Internal_Rela *
9009 find_relocs_at_address (Elf_Internal_Rela *reloc,
9010 Elf_Internal_Rela *relocs,
9011 Elf_Internal_Rela *irelend,
9012 enum elf_nds32_reloc_type reloc_type)
9013 {
9014 Elf_Internal_Rela *rel_t;
9015
9016 /* Find backward. */
9017 for (rel_t = reloc;
9018 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9019 rel_t--)
9020 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9021 return rel_t;
9022
9023 /* We didn't find it backward. Try find it forward. */
9024 for (rel_t = reloc;
9025 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9026 rel_t++)
9027 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9028 return rel_t;
9029
9030 return irelend;
9031 }
9032
9033 /* Find a relocation of specified type and offset.
9034 `reloc' is just a refence point to find a relocation at specified offset.
9035 If not found, return irelend.
9036
9037 Assuming relocations are sorted by r_offset,
9038 we find the relocation from `reloc' backward untill relocs,
9039 or find it from `reloc' forward untill irelend. */
9040
9041 static Elf_Internal_Rela *
9042 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9043 Elf_Internal_Rela *relocs,
9044 Elf_Internal_Rela *irelend,
9045 enum elf_nds32_reloc_type reloc_type,
9046 bfd_vma offset_p)
9047 {
9048 Elf_Internal_Rela *rel_t = NULL;
9049
9050 /* First, we try to find a relocation of offset `offset_p',
9051 and then we use find_relocs_at_address to find specific type. */
9052
9053 if (reloc->r_offset > offset_p)
9054 {
9055 /* Find backward. */
9056 for (rel_t = reloc;
9057 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9058 /* Do nothing. */;
9059 }
9060 else if (reloc->r_offset < offset_p)
9061 {
9062 /* Find forward. */
9063 for (rel_t = reloc;
9064 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9065 /* Do nothing. */;
9066 }
9067 else
9068 rel_t = reloc;
9069
9070 /* Not found? */
9071 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9072 return irelend;
9073
9074 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9075 }
9076
9077 typedef struct nds32_elf_blank nds32_elf_blank_t;
9078 struct nds32_elf_blank
9079 {
9080 /* Where the blank begins. */
9081 bfd_vma offset;
9082 /* The size of the blank. */
9083 bfd_vma size;
9084 /* The accumulative size before this blank. */
9085 bfd_vma total_size;
9086 nds32_elf_blank_t *next;
9087 nds32_elf_blank_t *prev;
9088 };
9089
9090 static nds32_elf_blank_t *blank_free_list = NULL;
9091
9092 static nds32_elf_blank_t *
9093 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9094 {
9095 nds32_elf_blank_t *blank_t;
9096
9097 if (blank_free_list)
9098 {
9099 blank_t = blank_free_list;
9100 blank_free_list = blank_free_list->next;
9101 }
9102 else
9103 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9104
9105 if (blank_t == NULL)
9106 return NULL;
9107
9108 blank_t->offset = offset_p;
9109 blank_t->size = size_p;
9110 blank_t->total_size = 0;
9111 blank_t->next = NULL;
9112 blank_t->prev = NULL;
9113
9114 return blank_t;
9115 }
9116
9117 static void
9118 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9119 {
9120 if (blank_free_list)
9121 {
9122 blank_free_list->prev = blank_p;
9123 blank_p->next = blank_free_list;
9124 }
9125 else
9126 blank_p->next = NULL;
9127
9128 blank_p->prev = NULL;
9129 blank_free_list = blank_p;
9130 }
9131
9132 static void
9133 clean_nds32_elf_blank (void)
9134 {
9135 nds32_elf_blank_t *blank_t;
9136
9137 while (blank_free_list)
9138 {
9139 blank_t = blank_free_list;
9140 blank_free_list = blank_free_list->next;
9141 free (blank_t);
9142 }
9143 }
9144
9145 static nds32_elf_blank_t *
9146 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9147 {
9148 nds32_elf_blank_t *blank_t;
9149
9150 if (!blank_p)
9151 return NULL;
9152 blank_t = blank_p;
9153
9154 while (blank_t && addr < blank_t->offset)
9155 blank_t = blank_t->prev;
9156 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9157 blank_t = blank_t->next;
9158
9159 return blank_t;
9160 }
9161
9162 static bfd_vma
9163 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9164 int overwrite)
9165 {
9166 nds32_elf_blank_t *blank_t;
9167
9168 blank_t = search_nds32_elf_blank (*blank_p, addr);
9169 if (!blank_t)
9170 return 0;
9171
9172 if (overwrite)
9173 *blank_p = blank_t;
9174
9175 if (addr < blank_t->offset + blank_t->size)
9176 return blank_t->total_size + (addr - blank_t->offset);
9177 else
9178 return blank_t->total_size + blank_t->size;
9179 }
9180
9181 static bfd_boolean
9182 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9183 {
9184 nds32_elf_blank_t *blank_t, *blank_t2;
9185
9186 if (!*blank_p)
9187 {
9188 *blank_p = create_nds32_elf_blank (addr, len);
9189 return *blank_p ? TRUE : FALSE;
9190 }
9191
9192 blank_t = search_nds32_elf_blank (*blank_p, addr);
9193
9194 if (blank_t == NULL)
9195 {
9196 blank_t = create_nds32_elf_blank (addr, len);
9197 if (!blank_t)
9198 return FALSE;
9199 while ((*blank_p)->prev != NULL)
9200 *blank_p = (*blank_p)->prev;
9201 blank_t->next = *blank_p;
9202 (*blank_p)->prev = blank_t;
9203 (*blank_p) = blank_t;
9204 return TRUE;
9205 }
9206
9207 if (addr < blank_t->offset + blank_t->size)
9208 {
9209 /* Extend the origin blank. */
9210 if (addr + len > blank_t->offset + blank_t->size)
9211 blank_t->size = addr + len - blank_t->offset;
9212 }
9213 else
9214 {
9215 blank_t2 = create_nds32_elf_blank (addr, len);
9216 if (!blank_t2)
9217 return FALSE;
9218 if (blank_t->next)
9219 {
9220 blank_t->next->prev = blank_t2;
9221 blank_t2->next = blank_t->next;
9222 }
9223 blank_t2->prev = blank_t;
9224 blank_t->next = blank_t2;
9225 *blank_p = blank_t2;
9226 }
9227
9228 return TRUE;
9229 }
9230
9231 static bfd_boolean
9232 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9233 bfd_vma len)
9234 {
9235 nds32_elf_blank_t *blank_t;
9236
9237 if (!insert_nds32_elf_blank (blank_p, addr, len))
9238 return FALSE;
9239
9240 blank_t = *blank_p;
9241
9242 if (!blank_t->prev)
9243 {
9244 blank_t->total_size = 0;
9245 blank_t = blank_t->next;
9246 }
9247
9248 while (blank_t)
9249 {
9250 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9251 blank_t = blank_t->next;
9252 }
9253
9254 return TRUE;
9255 }
9256
9257 static void
9258 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9259 {
9260 nds32_elf_blank_t *blank_t;
9261 bfd_vma total_size = 0;
9262
9263 if (!blank_p)
9264 return;
9265
9266 blank_t = blank_p;
9267 while (blank_t->prev)
9268 blank_t = blank_t->prev;
9269 while (blank_t)
9270 {
9271 blank_t->total_size = total_size;
9272 total_size += blank_t->size;
9273 blank_t = blank_t->next;
9274 }
9275 }
9276
9277 static bfd_boolean
9278 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9279 nds32_elf_blank_t *blank_p)
9280 {
9281 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
9282 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
9283 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9284 unsigned int sec_shndx; /* The section the be relaxed. */
9285 bfd_byte *contents; /* Contents data of iterating section. */
9286 Elf_Internal_Rela *internal_relocs;
9287 Elf_Internal_Rela *irel;
9288 Elf_Internal_Rela *irelend;
9289 struct elf_link_hash_entry **sym_hashes;
9290 struct elf_link_hash_entry **end_hashes;
9291 unsigned int symcount;
9292 asection *sect;
9293 nds32_elf_blank_t *blank_t;
9294 nds32_elf_blank_t *blank_t2;
9295 nds32_elf_blank_t *blank_head;
9296
9297 blank_head = blank_t = blank_p;
9298 while (blank_head->prev != NULL)
9299 blank_head = blank_head->prev;
9300 while (blank_t->next != NULL)
9301 blank_t = blank_t->next;
9302
9303 if (blank_t->offset + blank_t->size <= sec->size)
9304 {
9305 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9306 blank_t->next->prev = blank_t;
9307 }
9308 if (blank_head->offset > 0)
9309 {
9310 blank_head->prev = create_nds32_elf_blank (0, 0);
9311 blank_head->prev->next = blank_head;
9312 blank_head = blank_head->prev;
9313 }
9314
9315 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9316
9317 /* The deletion must stop at the next ALIGN reloc for an alignment
9318 power larger than the number of bytes we are deleting. */
9319
9320 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9321 if (!nds32_get_local_syms (abfd, sec, &isym))
9322 return FALSE;
9323
9324 if (isym == NULL)
9325 {
9326 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9327 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9328 symtab_hdr->contents = (bfd_byte *) isym;
9329 }
9330
9331 if (isym == NULL || symtab_hdr->sh_info == 0)
9332 return FALSE;
9333
9334 blank_t = blank_head;
9335 calc_nds32_blank_total (blank_head);
9336
9337 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9338 {
9339 /* Adjust all the relocs. */
9340
9341 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9342 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9343 TRUE /* keep_memory */);
9344 irelend = internal_relocs + sect->reloc_count;
9345
9346 blank_t = blank_head;
9347 blank_t2 = blank_head;
9348
9349 if (!(sect->flags & SEC_RELOC))
9350 continue;
9351
9352 nds32_get_section_contents (abfd, sect, &contents, TRUE);
9353
9354 for (irel = internal_relocs; irel < irelend; irel++)
9355 {
9356 bfd_vma raddr;
9357
9358 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9359 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9360 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9361 {
9362 unsigned long val = 0;
9363 unsigned long mask;
9364 long before, between;
9365 long offset = 0;
9366
9367 switch (ELF32_R_TYPE (irel->r_info))
9368 {
9369 case R_NDS32_DIFF8:
9370 offset = bfd_get_8 (abfd, contents + irel->r_offset);
9371 break;
9372 case R_NDS32_DIFF16:
9373 offset = bfd_get_16 (abfd, contents + irel->r_offset);
9374 break;
9375 case R_NDS32_DIFF32:
9376 val = bfd_get_32 (abfd, contents + irel->r_offset);
9377 /* Get the signed bit and mask for the high part. The
9378 gcc will alarm when right shift 32-bit since the
9379 type size of long may be 32-bit. */
9380 mask = 0 - (val >> 31);
9381 if (mask)
9382 offset = (val | (mask - 0xffffffff));
9383 else
9384 offset = val;
9385 break;
9386 default:
9387 BFD_ASSERT (0);
9388 }
9389
9390 /* DIFF value
9391 0 |encoded in location|
9392 |------------|-------------------|---------
9393 sym+off(addend)
9394 -- before ---| *****************
9395 --------------------- between ---|
9396
9397 We only care how much data are relax between DIFF,
9398 marked as ***. */
9399
9400 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9401 between = get_nds32_elf_blank_total (&blank_t,
9402 irel->r_addend + offset, 0);
9403 if (between == before)
9404 goto done_adjust_diff;
9405
9406 switch (ELF32_R_TYPE (irel->r_info))
9407 {
9408 case R_NDS32_DIFF8:
9409 bfd_put_8 (abfd, offset - (between - before),
9410 contents + irel->r_offset);
9411 break;
9412 case R_NDS32_DIFF16:
9413 bfd_put_16 (abfd, offset - (between - before),
9414 contents + irel->r_offset);
9415 break;
9416 case R_NDS32_DIFF32:
9417 bfd_put_32 (abfd, offset - (between - before),
9418 contents + irel->r_offset);
9419 break;
9420 }
9421 }
9422 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9423 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9424 {
9425 bfd_vma val = 0;
9426 unsigned int len = 0;
9427 unsigned long before, between;
9428 bfd_byte *endp, *p;
9429
9430 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9431 &len);
9432
9433 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9434 between = get_nds32_elf_blank_total (&blank_t,
9435 irel->r_addend + val, 0);
9436 if (between == before)
9437 goto done_adjust_diff;
9438
9439 p = contents + irel->r_offset;
9440 endp = p + len -1;
9441 memset (p, 0x80, len);
9442 *(endp) = 0;
9443 p = write_uleb128 (p, val - (between - before)) - 1;
9444 if (p < endp)
9445 *p |= 0x80;
9446 }
9447 done_adjust_diff:
9448
9449 if (sec == sect)
9450 {
9451 raddr = irel->r_offset;
9452 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9453 irel->r_offset, 1);
9454
9455 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9456 continue;
9457 if (blank_t2 && blank_t2->next
9458 && (blank_t2->offset > raddr
9459 || blank_t2->next->offset <= raddr))
9460 _bfd_error_handler
9461 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9462 abfd);
9463
9464 /* Mark reloc in deleted portion as NONE.
9465 For some relocs like R_NDS32_LABEL that doesn't modify the
9466 content in the section. R_NDS32_LABEL doesn't belong to the
9467 instruction in the section, so we should preserve it. */
9468 if (raddr >= blank_t2->offset
9469 && raddr < blank_t2->offset + blank_t2->size
9470 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9471 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9472 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9473 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9474 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9475 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9476 {
9477 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9478 R_NDS32_NONE);
9479 continue;
9480 }
9481 }
9482
9483 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9484 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9485 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9486 continue;
9487
9488 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9489 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9490 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9491 {
9492 if (irel->r_addend <= sec->size)
9493 irel->r_addend -=
9494 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9495 }
9496 }
9497 }
9498
9499 /* Adjust the local symbols defined in this section. */
9500 blank_t = blank_head;
9501 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9502 {
9503 if (isym->st_shndx == sec_shndx)
9504 {
9505 if (isym->st_value <= sec->size)
9506 {
9507 bfd_vma ahead;
9508 bfd_vma orig_addr = isym->st_value;
9509
9510 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9511 isym->st_value -= ahead;
9512
9513 /* Adjust function size. */
9514 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9515 && isym->st_size > 0)
9516 isym->st_size -=
9517 get_nds32_elf_blank_total
9518 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9519 }
9520 }
9521 }
9522
9523 /* Now adjust the global symbols defined in this section. */
9524 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9525 - symtab_hdr->sh_info);
9526 sym_hashes = elf_sym_hashes (abfd);
9527 end_hashes = sym_hashes + symcount;
9528 blank_t = blank_head;
9529 for (; sym_hashes < end_hashes; sym_hashes++)
9530 {
9531 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9532
9533 if ((sym_hash->root.type == bfd_link_hash_defined
9534 || sym_hash->root.type == bfd_link_hash_defweak)
9535 && sym_hash->root.u.def.section == sec)
9536 {
9537 if (sym_hash->root.u.def.value <= sec->size)
9538 {
9539 bfd_vma ahead;
9540 bfd_vma orig_addr = sym_hash->root.u.def.value;
9541
9542 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9543 sym_hash->root.u.def.value -= ahead;
9544
9545 /* Adjust function size. */
9546 if (sym_hash->type == STT_FUNC)
9547 sym_hash->size -=
9548 get_nds32_elf_blank_total
9549 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9550
9551 }
9552 }
9553 }
9554
9555 contents = elf_section_data (sec)->this_hdr.contents;
9556 blank_t = blank_head;
9557 while (blank_t->next)
9558 {
9559 /* Actually delete the bytes. */
9560
9561 /* If current blank is the last blank overlap with current section,
9562 go to finish process. */
9563 if (sec->size <= (blank_t->next->offset))
9564 break;
9565
9566 memmove (contents + blank_t->offset - blank_t->total_size,
9567 contents + blank_t->offset + blank_t->size,
9568 blank_t->next->offset - (blank_t->offset + blank_t->size));
9569
9570 blank_t = blank_t->next;
9571 }
9572
9573 if (sec->size > (blank_t->offset + blank_t->size))
9574 {
9575 /* There are remaining code between blank and section boundary.
9576 Move the remaining code to appropriate location. */
9577 memmove (contents + blank_t->offset - blank_t->total_size,
9578 contents + blank_t->offset + blank_t->size,
9579 sec->size - (blank_t->offset + blank_t->size));
9580 sec->size -= blank_t->total_size + blank_t->size;
9581 }
9582 else
9583 /* This blank is not entirely included in the section,
9584 reduce the section size by only part of the blank size. */
9585 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9586
9587 while (blank_head)
9588 {
9589 blank_t = blank_head;
9590 blank_head = blank_head->next;
9591 remove_nds32_elf_blank (blank_t);
9592 }
9593
9594 return TRUE;
9595 }
9596
9597 /* Get the contents of a section. */
9598
9599 static int
9600 nds32_get_section_contents (bfd *abfd, asection *sec,
9601 bfd_byte **contents_p, bfd_boolean cache)
9602 {
9603 /* Get the section contents. */
9604 if (elf_section_data (sec)->this_hdr.contents != NULL)
9605 *contents_p = elf_section_data (sec)->this_hdr.contents;
9606 else
9607 {
9608 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9609 return FALSE;
9610 if (cache)
9611 elf_section_data (sec)->this_hdr.contents = *contents_p;
9612 }
9613
9614 return TRUE;
9615 }
9616
9617 /* Get the contents of the internal symbol of abfd. */
9618
9619 static int
9620 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9621 Elf_Internal_Sym **isymbuf_p)
9622 {
9623 Elf_Internal_Shdr *symtab_hdr;
9624 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9625
9626 /* Read this BFD's local symbols if we haven't done so already. */
9627 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9628 {
9629 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9630 if (*isymbuf_p == NULL)
9631 {
9632 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9633 symtab_hdr->sh_info, 0,
9634 NULL, NULL, NULL);
9635 if (*isymbuf_p == NULL)
9636 return FALSE;
9637 }
9638 }
9639 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9640
9641 return TRUE;
9642 }
9643
9644 /* Range of small data. */
9645 static bfd_vma sdata_range[2][2];
9646 static bfd_vma const sdata_init_range[2] =
9647 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
9648
9649 static int
9650 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9651 bfd_byte *contents, bfd_vma addr)
9652 {
9653 unsigned long insn = bfd_getb32 (contents + addr);
9654
9655 if (insn & 0x80000000)
9656 return 2;
9657
9658 return 4;
9659 }
9660
9661 /* Set the gp relax range. We have to measure the safe range
9662 to do gp relaxation. */
9663
9664 static void
9665 relax_range_measurement (bfd *abfd)
9666 {
9667 asection *sec_f, *sec_b;
9668 /* For upper bound. */
9669 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9670 bfd_vma align;
9671 static int decide_relax_range = 0;
9672 int i;
9673 int range_number = ARRAY_SIZE (sdata_init_range);
9674
9675 if (decide_relax_range)
9676 return;
9677 decide_relax_range = 1;
9678
9679 if (sda_rela_sec == NULL)
9680 {
9681 /* Since there is no data sections, we assume the range is page size. */
9682 for (i = 0; i < range_number; i++)
9683 {
9684 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9685 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9686 }
9687 return;
9688 }
9689
9690 /* Get the biggest alignment power after the gp located section. */
9691 sec_f = sda_rela_sec->output_section;
9692 sec_b = sec_f->next;
9693 align = 0;
9694 while (sec_b != NULL)
9695 {
9696 if ((unsigned)(1 << sec_b->alignment_power) > align)
9697 align = (1 << sec_b->alignment_power);
9698 sec_b = sec_b->next;
9699 }
9700
9701 /* I guess we can not determine the section before
9702 gp located section, so we assume the align is max page size. */
9703 for (i = 0; i < range_number; i++)
9704 {
9705 sdata_range[i][1] = sdata_init_range[i] - align;
9706 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9707 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9708 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9709 }
9710 }
9711
9712 /* These are macros used to check flags encoded in r_addend.
9713 They are only used by nds32_elf_relax_section (). */
9714 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9715 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9716 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9717 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9718
9719 static const char * unrecognized_reloc_msg =
9720 /* xgettext:c-format */
9721 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9722
9723 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9724
9725 static bfd_boolean
9726 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9727 Elf_Internal_Rela *internal_relocs, int *insn_len,
9728 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9729 Elf_Internal_Shdr *symtab_hdr)
9730 {
9731 /* There are 3 variations for LONGCALL1
9732 case 4-4-2; 16-bit on, optimize off or optimize for space
9733 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9734 ori ta, ta, lo12(symbol) ; LO12S0
9735 jral5 ta ;
9736
9737 case 4-4-4; 16-bit off, optimize don't care
9738 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9739 ori ta, ta, lo12(symbol) ; LO12S0
9740 jral ta ;
9741
9742 case 4-4-4; 16-bit on, optimize for speed
9743 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9744 ori ta, ta, lo12(symbol) ; LO12S0
9745 jral ta ;
9746 Check code for -mlong-calls output. */
9747
9748 /* Get the reloc for the address from which the register is
9749 being loaded. This reloc will tell us which function is
9750 actually being called. */
9751
9752 bfd_vma laddr;
9753 int seq_len; /* Original length of instruction sequence. */
9754 uint32_t insn;
9755 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9756 bfd_signed_vma foff;
9757 uint16_t insn16;
9758
9759 irelend = internal_relocs + sec->reloc_count;
9760 seq_len = GET_SEQ_LEN (irel->r_addend);
9761 laddr = irel->r_offset;
9762 *insn_len = seq_len;
9763
9764 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9765 R_NDS32_HI20_RELA, laddr);
9766 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9767 R_NDS32_LO12S0_ORI_RELA,
9768 laddr + 4);
9769
9770 if (hi_irelfn == irelend || lo_irelfn == irelend)
9771 {
9772 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9773 (uint64_t) irel->r_offset);
9774 return FALSE;
9775 }
9776
9777 /* Get the value of the symbol referred to by the reloc. */
9778 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9779
9780 /* This condition only happened when symbol is undefined. */
9781 if (foff == 0
9782 || foff < -CONSERVATIVE_24BIT_S1
9783 || foff >= CONSERVATIVE_24BIT_S1)
9784 return FALSE;
9785
9786 /* Relax to: jal symbol; 25_PCREL. */
9787 /* For simplicity of coding, we are going to modify the section
9788 contents, the section relocs, and the BFD symbol table. We
9789 must tell the rest of the code not to free up this
9790 information. It would be possible to instead create a table
9791 of changes which have to be made, as is done in coff-mips.c;
9792 that would be more work, but would require less memory when
9793 the linker is run. */
9794
9795 /* Replace the long call with a jal. */
9796 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9797 R_NDS32_25_PCREL_RELA);
9798 irel->r_addend = hi_irelfn->r_addend;
9799
9800 /* We don't resolve this here but resolve it in relocate_section. */
9801 insn = INSN_JAL;
9802 bfd_putb32 (insn, contents + irel->r_offset);
9803
9804 hi_irelfn->r_info =
9805 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9806 lo_irelfn->r_info =
9807 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9808 *insn_len = 4;
9809
9810 if (seq_len & 0x2)
9811 {
9812 insn16 = NDS32_NOP16;
9813 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9814 lo_irelfn->r_info =
9815 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9816 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9817 *insn_len += 2;
9818 }
9819 return TRUE;
9820 }
9821
9822 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9823 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
9824
9825 static bfd_boolean
9826 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9827 Elf_Internal_Rela *internal_relocs, int *insn_len,
9828 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9829 Elf_Internal_Shdr *symtab_hdr)
9830 {
9831 /* bltz rt, .L1 ; LONGCALL2
9832 jal symbol ; 25_PCREL
9833 .L1: */
9834
9835 /* Get the reloc for the address from which the register is
9836 being loaded. This reloc will tell us which function is
9837 actually being called. */
9838
9839 bfd_vma laddr;
9840 uint32_t insn;
9841 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9842 bfd_signed_vma foff;
9843
9844 irelend = internal_relocs + sec->reloc_count;
9845 laddr = irel->r_offset;
9846 i1_irelfn =
9847 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9848 R_NDS32_25_PCREL_RELA, laddr + 4);
9849
9850 if (i1_irelfn == irelend)
9851 {
9852 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9853 (uint64_t) irel->r_offset);
9854 return FALSE;
9855 }
9856
9857 insn = bfd_getb32 (contents + laddr);
9858
9859 /* Get the value of the symbol referred to by the reloc. */
9860 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9861
9862 if (foff == 0
9863 || foff < -CONSERVATIVE_16BIT_S1
9864 || foff >= CONSERVATIVE_16BIT_S1)
9865 return FALSE;
9866
9867 /* Relax to bgezal rt, label ; 17_PCREL
9868 or bltzal rt, label ; 17_PCREL */
9869
9870 /* Convert to complimentary conditional call. */
9871 insn = CONVERT_CONDITION_CALL (insn);
9872
9873 /* For simplicity of coding, we are going to modify the section
9874 contents, the section relocs, and the BFD symbol table. We
9875 must tell the rest of the code not to free up this
9876 information. It would be possible to instead create a table
9877 of changes which have to be made, as is done in coff-mips.c;
9878 that would be more work, but would require less memory when
9879 the linker is run. */
9880
9881 /* Clean unnessary relocations. */
9882 i1_irelfn->r_info =
9883 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9884 cond_irelfn =
9885 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9886 R_NDS32_17_PCREL_RELA, laddr);
9887 if (cond_irelfn != irelend)
9888 cond_irelfn->r_info =
9889 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9890
9891 /* Replace the long call with a bgezal. */
9892 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9893 R_NDS32_17_PCREL_RELA);
9894 irel->r_addend = i1_irelfn->r_addend;
9895
9896 bfd_putb32 (insn, contents + irel->r_offset);
9897
9898 *insn_len = 4;
9899 return TRUE;
9900 }
9901
9902 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9903
9904 static bfd_boolean
9905 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9906 Elf_Internal_Rela *internal_relocs, int *insn_len,
9907 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9908 Elf_Internal_Shdr *symtab_hdr)
9909 {
9910 /* There are 3 variations for LONGCALL3
9911 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9912 bltz rt, $1 ; LONGCALL3
9913 sethi ta, hi20(symbol) ; HI20
9914 ori ta, ta, lo12(symbol) ; LO12S0
9915 jral5 ta ;
9916 $1
9917
9918 case 4-4-4-4; 16-bit off, optimize don't care
9919 bltz rt, $1 ; LONGCALL3
9920 sethi ta, hi20(symbol) ; HI20
9921 ori ta, ta, lo12(symbol) ; LO12S0
9922 jral ta ;
9923 $1
9924
9925 case 4-4-4-4; 16-bit on, optimize for speed
9926 bltz rt, $1 ; LONGCALL3
9927 sethi ta, hi20(symbol) ; HI20
9928 ori ta, ta, lo12(symbol) ; LO12S0
9929 jral ta ;
9930 $1 */
9931
9932 /* Get the reloc for the address from which the register is
9933 being loaded. This reloc will tell us which function is
9934 actually being called. */
9935
9936 bfd_vma laddr;
9937 int seq_len; /* Original length of instruction sequence. */
9938 uint32_t insn;
9939 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9940 bfd_signed_vma foff;
9941 uint16_t insn16;
9942
9943 irelend = internal_relocs + sec->reloc_count;
9944 seq_len = GET_SEQ_LEN (irel->r_addend);
9945 laddr = irel->r_offset;
9946 *insn_len = seq_len;
9947
9948 hi_irelfn =
9949 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9950 R_NDS32_HI20_RELA, laddr + 4);
9951 lo_irelfn =
9952 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9953 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9954
9955 if (hi_irelfn == irelend || lo_irelfn == irelend)
9956 {
9957 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9958 (uint64_t) irel->r_offset);
9959 return FALSE;
9960 }
9961
9962 /* Get the value of the symbol referred to by the reloc. */
9963 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9964
9965 if (foff == 0
9966 || foff < -CONSERVATIVE_24BIT_S1
9967 || foff >= CONSERVATIVE_24BIT_S1)
9968 return FALSE;
9969
9970 insn = bfd_getb32 (contents + laddr);
9971 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9972 {
9973 /* Relax to bgezal rt, label ; 17_PCREL
9974 or bltzal rt, label ; 17_PCREL */
9975
9976 /* Convert to complimentary conditional call. */
9977 insn = CONVERT_CONDITION_CALL (insn);
9978 bfd_putb32 (insn, contents + irel->r_offset);
9979
9980 *insn_len = 4;
9981 irel->r_info =
9982 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9983 hi_irelfn->r_info =
9984 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9985 lo_irelfn->r_info =
9986 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9987
9988 cond_irelfn =
9989 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9990 R_NDS32_17_PCREL_RELA, laddr);
9991 if (cond_irelfn != irelend)
9992 {
9993 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9994 R_NDS32_17_PCREL_RELA);
9995 cond_irelfn->r_addend = hi_irelfn->r_addend;
9996 }
9997
9998 if (seq_len & 0x2)
9999 {
10000 insn16 = NDS32_NOP16;
10001 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10002 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10003 R_NDS32_INSN16);
10004 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10005 insn_len += 2;
10006 }
10007 }
10008 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10009 {
10010 /* Relax to the following instruction sequence
10011 bltz rt, $1 ; LONGCALL2
10012 jal symbol ; 25_PCREL
10013 $1 */
10014 *insn_len = 8;
10015 insn = INSN_JAL;
10016 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10017
10018 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10019 R_NDS32_25_PCREL_RELA);
10020 irel->r_info =
10021 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10022
10023 lo_irelfn->r_info =
10024 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10025
10026 if (seq_len & 0x2)
10027 {
10028 insn16 = NDS32_NOP16;
10029 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10030 lo_irelfn->r_info =
10031 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10032 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10033 insn_len += 2;
10034 }
10035 }
10036 return TRUE;
10037 }
10038
10039 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
10040
10041 static bfd_boolean
10042 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10043 Elf_Internal_Rela *internal_relocs, int *insn_len,
10044 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10045 Elf_Internal_Shdr *symtab_hdr)
10046 {
10047 /* There are 3 variations for LONGJUMP1
10048 case 4-4-2; 16-bit bit on, optimize off or optimize for space
10049 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10050 ori ta, ta, lo12(symbol) ; LO12S0
10051 jr5 ta ;
10052
10053 case 4-4-4; 16-bit off, optimize don't care
10054 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10055 ori ta, ta, lo12(symbol) ; LO12S0
10056 jr ta ;
10057
10058 case 4-4-4; 16-bit on, optimize for speed
10059 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10060 ori ta, ta, lo12(symbol) ; LO12S0
10061 jr ta ; */
10062
10063 /* Get the reloc for the address from which the register is
10064 being loaded. This reloc will tell us which function is
10065 actually being called. */
10066
10067 bfd_vma laddr;
10068 int seq_len; /* Original length of instruction sequence. */
10069 int insn16_on; /* 16-bit on/off. */
10070 uint32_t insn;
10071 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10072 bfd_signed_vma foff;
10073 uint16_t insn16;
10074 unsigned long reloc;
10075
10076 irelend = internal_relocs + sec->reloc_count;
10077 seq_len = GET_SEQ_LEN (irel->r_addend);
10078 laddr = irel->r_offset;
10079 *insn_len = seq_len;
10080 insn16_on = IS_16BIT_ON (irel->r_addend);
10081
10082 hi_irelfn =
10083 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10084 R_NDS32_HI20_RELA, laddr);
10085 lo_irelfn =
10086 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10087 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10088 if (hi_irelfn == irelend || lo_irelfn == irelend)
10089 {
10090 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10091 (uint64_t) irel->r_offset);
10092 return FALSE;
10093 }
10094
10095 /* Get the value of the symbol referred to by the reloc. */
10096 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10097
10098 if (foff == 0
10099 || foff >= CONSERVATIVE_24BIT_S1
10100 || foff < -CONSERVATIVE_24BIT_S1)
10101 return FALSE;
10102
10103 if (insn16_on
10104 && foff >= -ACCURATE_8BIT_S1
10105 && foff < ACCURATE_8BIT_S1
10106 && (seq_len & 0x2))
10107 {
10108 /* j8 label */
10109 /* 16-bit on, but not optimized for speed. */
10110 reloc = R_NDS32_9_PCREL_RELA;
10111 insn16 = INSN_J8;
10112 bfd_putb16 (insn16, contents + irel->r_offset);
10113 *insn_len = 2;
10114 irel->r_info =
10115 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10116 }
10117 else
10118 {
10119 /* j label */
10120 reloc = R_NDS32_25_PCREL_RELA;
10121 insn = INSN_J;
10122 bfd_putb32 (insn, contents + irel->r_offset);
10123 *insn_len = 4;
10124 irel->r_info =
10125 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10126 irel->r_addend = 0;
10127 }
10128
10129 hi_irelfn->r_info =
10130 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10131 lo_irelfn->r_info =
10132 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10133
10134 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10135 {
10136 insn16 = NDS32_NOP16;
10137 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10138 lo_irelfn->r_info =
10139 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10140 R_NDS32_INSN16);
10141 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10142 *insn_len += 2;
10143 }
10144 return TRUE;
10145 }
10146
10147 /* Revert condition branch. This function does not check if the input
10148 instruction is condition branch or not. */
10149
10150 static void
10151 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10152 uint16_t *re_insn16, uint32_t *re_insn)
10153 {
10154 uint32_t comp_insn = 0;
10155 uint16_t comp_insn16 = 0;
10156
10157 if (insn)
10158 {
10159 if (N32_OP6 (insn) == N32_OP6_BR1)
10160 {
10161 /* beqs label. */
10162 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10163 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10164 {
10165 /* Insn can be contracted to 16-bit implied r5. */
10166 comp_insn16 =
10167 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10168 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10169 }
10170 }
10171 else if (N32_OP6 (insn) == N32_OP6_BR3)
10172 {
10173 /* bnec $ta, imm11, label. */
10174 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10175 }
10176 else
10177 {
10178 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10179 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10180 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10181 {
10182 if (N32_IS_RT3 (insn))
10183 {
10184 /* Insn can be contracted to 16-bit. */
10185 comp_insn16 =
10186 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10187 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10188 }
10189 else if (N32_RT5 (insn) == REG_R15)
10190 {
10191 /* Insn can be contracted to 16-bit. */
10192 comp_insn16 =
10193 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10194 }
10195 }
10196 }
10197 }
10198 else
10199 {
10200 switch ((insn16 & 0xf000) >> 12)
10201 {
10202 case 0xc:
10203 /* beqz38 or bnez38 */
10204 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10205 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10206 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10207 break;
10208
10209 case 0xd:
10210 /* beqs38 or bnes38 */
10211 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10212 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10213 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10214 | (REG_R5 << 15);
10215 break;
10216
10217 case 0xe:
10218 /* beqzS8 or bnezS8 */
10219 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10220 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10221 comp_insn |= REG_R15 << 20;
10222 break;
10223
10224 default:
10225 break;
10226 }
10227 }
10228 if (comp_insn && re_insn)
10229 *re_insn = comp_insn;
10230 if (comp_insn16 && re_insn16)
10231 *re_insn16 = comp_insn16;
10232 }
10233
10234 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
10235
10236 static bfd_boolean
10237 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10238 Elf_Internal_Rela *internal_relocs, int *insn_len,
10239 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10240 Elf_Internal_Shdr *symtab_hdr)
10241 {
10242 /* There are 3 variations for LONGJUMP2
10243 case 2-4; 1st insn convertible, 16-bit on,
10244 optimize off or optimize for space
10245 bnes38 rt, ra, $1 ; LONGJUMP2
10246 j label ; 25_PCREL
10247 $1:
10248
10249 case 4-4; 1st insn not convertible
10250 bne rt, ra, $1 ; LONGJUMP2
10251 j label ; 25_PCREL
10252 $1:
10253
10254 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10255 bne rt, ra, $1 ; LONGJUMP2
10256 j label ; 25_PCREL
10257 $1: */
10258
10259 /* Get the reloc for the address from which the register is
10260 being loaded. This reloc will tell us which function is
10261 actually being called. */
10262
10263 bfd_vma laddr;
10264 int seq_len; /* Original length of instruction sequence. */
10265 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10266 int first_size;
10267 unsigned int i;
10268 bfd_signed_vma foff;
10269 uint32_t insn, re_insn = 0;
10270 uint16_t insn16, re_insn16 = 0;
10271 unsigned long reloc, cond_reloc;
10272
10273 enum elf_nds32_reloc_type checked_types[] =
10274 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10275
10276 irelend = internal_relocs + sec->reloc_count;
10277 seq_len = GET_SEQ_LEN (irel->r_addend);
10278 laddr = irel->r_offset;
10279 *insn_len = seq_len;
10280 first_size = (seq_len == 6) ? 2 : 4;
10281
10282 i2_irelfn =
10283 find_relocs_at_address_addr (irel, internal_relocs,
10284 irelend, R_NDS32_25_PCREL_RELA,
10285 laddr + first_size);
10286
10287 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10288 {
10289 cond_irelfn =
10290 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10291 checked_types[i], laddr);
10292 if (cond_irelfn != irelend)
10293 break;
10294 }
10295
10296 if (i2_irelfn == irelend || cond_irelfn == irelend)
10297 {
10298 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10299 (uint64_t) irel->r_offset);
10300 return FALSE;
10301 }
10302
10303 /* Get the value of the symbol referred to by the reloc. */
10304 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10305 if (foff == 0
10306 || foff < -CONSERVATIVE_16BIT_S1
10307 || foff >= CONSERVATIVE_16BIT_S1)
10308 return FALSE;
10309
10310 /* Get the all corresponding instructions. */
10311 if (first_size == 4)
10312 {
10313 insn = bfd_getb32 (contents + laddr);
10314 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10315 }
10316 else
10317 {
10318 insn16 = bfd_getb16 (contents + laddr);
10319 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10320 }
10321
10322 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10323 && foff < ACCURATE_8BIT_S1 - first_size)
10324 {
10325 if (first_size == 4)
10326 {
10327 /* Don't convert it to 16-bit now, keep this as relaxable for
10328 ``label reloc; INSN16''. */
10329
10330 /* Save comp_insn32 to buffer. */
10331 bfd_putb32 (re_insn, contents + irel->r_offset);
10332 *insn_len = 4;
10333 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10334 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10335 cond_reloc = R_NDS32_INSN16;
10336 }
10337 else
10338 {
10339 bfd_putb16 (re_insn16, contents + irel->r_offset);
10340 *insn_len = 2;
10341 reloc = R_NDS32_9_PCREL_RELA;
10342 cond_reloc = R_NDS32_NONE;
10343 }
10344 }
10345 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10346 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10347 && foff < ACCURATE_14BIT_S1 - first_size))
10348 {
10349 /* beqs label ; 15_PCREL */
10350 bfd_putb32 (re_insn, contents + irel->r_offset);
10351 *insn_len = 4;
10352 reloc = R_NDS32_15_PCREL_RELA;
10353 cond_reloc = R_NDS32_NONE;
10354 }
10355 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10356 && foff >= -CONSERVATIVE_16BIT_S1
10357 && foff < CONSERVATIVE_16BIT_S1)
10358 {
10359 /* beqz label ; 17_PCREL */
10360 bfd_putb32 (re_insn, contents + irel->r_offset);
10361 *insn_len = 4;
10362 reloc = R_NDS32_17_PCREL_RELA;
10363 cond_reloc = R_NDS32_NONE;
10364 }
10365 else
10366 return FALSE;
10367
10368 /* Set all relocations. */
10369 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10370 irel->r_addend = i2_irelfn->r_addend;
10371
10372 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10373 cond_reloc);
10374 cond_irelfn->r_addend = 0;
10375
10376 if ((seq_len ^ *insn_len ) & 0x2)
10377 {
10378 insn16 = NDS32_NOP16;
10379 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10380 i2_irelfn->r_offset = 4;
10381 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10382 R_NDS32_INSN16);
10383 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10384 *insn_len += 2;
10385 }
10386 else
10387 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10388 R_NDS32_NONE);
10389 return TRUE;
10390 }
10391
10392 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
10393
10394 static bfd_boolean
10395 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10396 Elf_Internal_Rela *internal_relocs, int *insn_len,
10397 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10398 Elf_Internal_Shdr *symtab_hdr)
10399 {
10400 /* There are 5 variations for LONGJUMP3
10401 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10402 optimize off or optimize for space
10403 bnes38 rt, ra, $1 ; LONGJUMP3
10404 sethi ta, hi20(symbol) ; HI20
10405 ori ta, ta, lo12(symbol) ; LO12S0
10406 jr5 ta ;
10407 $1: ;
10408
10409 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10410 bnes38 rt, ra, $1 ; LONGJUMP3
10411 sethi ta, hi20(symbol) ; HI20
10412 ori ta, ta, lo12(symbol) ; LO12S0
10413 jr5 ta ;
10414 $1: ; LABEL
10415
10416 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10417 optimize off or optimize for space
10418 bne rt, ra, $1 ; LONGJUMP3
10419 sethi ta, hi20(symbol) ; HI20
10420 ori ta, ta, lo12(symbol) ; LO12S0
10421 jr5 ta ;
10422 $1: ;
10423
10424 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10425 16-bit off if no INSN16
10426 bne rt, ra, $1 ; LONGJUMP3
10427 sethi ta, hi20(symbol) ; HI20
10428 ori ta, ta, lo12(symbol) ; LO12S0
10429 jr ta ;
10430 $1: ;
10431
10432 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10433 16-bit off if no INSN16
10434 bne rt, ra, $1 ; LONGJUMP3
10435 sethi ta, hi20(symbol) ; HI20
10436 ori ta, ta, lo12(symbol) ; LO12S0
10437 jr ta ;
10438 $1: ; LABEL */
10439
10440 /* Get the reloc for the address from which the register is
10441 being loaded. This reloc will tell us which function is
10442 actually being called. */
10443 enum elf_nds32_reloc_type checked_types[] =
10444 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10445
10446 int reloc_off = 0, cond_removed = 0, convertible;
10447 bfd_vma laddr;
10448 int seq_len; /* Original length of instruction sequence. */
10449 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10450 int first_size;
10451 unsigned int i;
10452 bfd_signed_vma foff;
10453 uint32_t insn, re_insn = 0;
10454 uint16_t insn16, re_insn16 = 0;
10455 unsigned long reloc, cond_reloc;
10456
10457 irelend = internal_relocs + sec->reloc_count;
10458 seq_len = GET_SEQ_LEN (irel->r_addend);
10459 laddr = irel->r_offset;
10460 *insn_len = seq_len;
10461
10462 convertible = IS_1ST_CONVERT (irel->r_addend);
10463
10464 if (convertible)
10465 first_size = 2;
10466 else
10467 first_size = 4;
10468
10469 /* Get all needed relocations. */
10470 hi_irelfn =
10471 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10472 R_NDS32_HI20_RELA, laddr + first_size);
10473 lo_irelfn =
10474 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10475 R_NDS32_LO12S0_ORI_RELA,
10476 laddr + first_size + 4);
10477
10478 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10479 {
10480 cond_irelfn =
10481 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10482 checked_types[i], laddr);
10483 if (cond_irelfn != irelend)
10484 break;
10485 }
10486
10487 if (hi_irelfn == irelend
10488 || lo_irelfn == irelend
10489 || cond_irelfn == irelend)
10490 {
10491 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10492 (uint64_t) irel->r_offset);
10493 return FALSE;
10494 }
10495
10496 /* Get the value of the symbol referred to by the reloc. */
10497 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10498
10499 if (foff == 0
10500 || foff < -CONSERVATIVE_24BIT_S1
10501 || foff >= CONSERVATIVE_24BIT_S1)
10502 return FALSE;
10503
10504 /* Get the all corresponding instructions. */
10505 if (first_size == 4)
10506 {
10507 insn = bfd_getb32 (contents + laddr);
10508 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10509 }
10510 else
10511 {
10512 insn16 = bfd_getb16 (contents + laddr);
10513 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10514 }
10515
10516 /* For simplicity of coding, we are going to modify the section
10517 contents, the section relocs, and the BFD symbol table. We
10518 must tell the rest of the code not to free up this
10519 information. It would be possible to instead create a table
10520 of changes which have to be made, as is done in coff-mips.c;
10521 that would be more work, but would require less memory when
10522 the linker is run. */
10523
10524 if (re_insn16
10525 && foff >= -ACCURATE_8BIT_S1 - first_size
10526 && foff < ACCURATE_8BIT_S1 - first_size)
10527 {
10528 if (!(seq_len & 0x2))
10529 {
10530 /* Don't convert it to 16-bit now, keep this as relaxable
10531 for ``label reloc; INSN1a''6. */
10532 /* Save comp_insn32 to buffer. */
10533 bfd_putb32 (re_insn, contents + irel->r_offset);
10534 *insn_len = 4;
10535 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10536 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10537 cond_reloc = R_NDS32_INSN16;
10538 }
10539 else
10540 {
10541 /* Not optimize for speed; convert sequence to 16-bit. */
10542 /* Save comp_insn16 to buffer. */
10543 bfd_putb16 (re_insn16, contents + irel->r_offset);
10544 *insn_len = 2;
10545 reloc = R_NDS32_9_PCREL_RELA;
10546 cond_reloc = R_NDS32_NONE;
10547 }
10548 cond_removed = 1;
10549 }
10550 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10551 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10552 && foff < ACCURATE_14BIT_S1 - first_size))
10553 {
10554 /* beqs label ; 15_PCREL */
10555 bfd_putb32 (re_insn, contents + irel->r_offset);
10556 *insn_len = 4;
10557 reloc = R_NDS32_15_PCREL_RELA;
10558 cond_reloc = R_NDS32_NONE;
10559 cond_removed = 1;
10560 }
10561 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10562 && foff >= -CONSERVATIVE_16BIT_S1
10563 && foff < CONSERVATIVE_16BIT_S1)
10564 {
10565 /* beqz label ; 17_PCREL */
10566 bfd_putb32 (re_insn, contents + irel->r_offset);
10567 *insn_len = 4;
10568 reloc = R_NDS32_17_PCREL_RELA;
10569 cond_reloc = R_NDS32_NONE;
10570 cond_removed = 1;
10571 }
10572 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10573 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10574 {
10575 /* Relax to one of the following 3 variations
10576
10577 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10578 for space
10579 bnes38 rt, $1 ; LONGJUMP2
10580 j label ; 25_PCREL
10581 $1
10582
10583 case 4-4; 1st insn not convertible, others don't care
10584 bne rt, ra, $1 ; LONGJUMP2
10585 j label ; 25_PCREL
10586 $1
10587
10588 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10589 bne rt, ra, $1 ; LONGJUMP2
10590 j label ; 25_PCREL
10591 $1 */
10592
10593 /* Offset for first instruction. */
10594
10595 /* Use j label as second instruction. */
10596 *insn_len = 4 + first_size;
10597 insn = INSN_J;
10598 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10599 reloc = R_NDS32_LONGJUMP2;
10600 cond_reloc = R_NDS32_25_PLTREL;
10601 }
10602 else
10603 return FALSE;
10604
10605 if (cond_removed == 1)
10606 {
10607 /* Set all relocations. */
10608 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10609 irel->r_addend = hi_irelfn->r_addend;
10610
10611 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10612 cond_reloc);
10613 cond_irelfn->r_addend = 0;
10614 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10615 R_NDS32_NONE);
10616 }
10617 else
10618 {
10619 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10620 irel->r_addend = irel->r_addend;
10621 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10622 cond_reloc);
10623 }
10624
10625 if ((seq_len ^ *insn_len ) & 0x2)
10626 {
10627 insn16 = NDS32_NOP16;
10628 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10629 lo_irelfn->r_offset = *insn_len;
10630 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10631 R_NDS32_INSN16);
10632 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10633 *insn_len += 2;
10634 }
10635 else
10636 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10637 R_NDS32_NONE);
10638 return TRUE;
10639 }
10640
10641 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
10642
10643 static bfd_boolean
10644 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10645 Elf_Internal_Rela *internal_relocs, int *insn_len,
10646 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10647 Elf_Internal_Shdr *symtab_hdr)
10648 {
10649 /* The pattern for LONGCALL4. Support for function cse.
10650 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10651 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10652 jral ta ; PTR_RES/EMPTY/INSN16 */
10653
10654 bfd_vma laddr;
10655 uint32_t insn;
10656 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10657 Elf_Internal_Rela *irelend;
10658 bfd_signed_vma foff;
10659
10660 irelend = internal_relocs + sec->reloc_count;
10661 laddr = irel->r_offset;
10662
10663 /* Get the reloc for the address from which the register is
10664 being loaded. This reloc will tell us which function is
10665 actually being called. */
10666 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10667 R_NDS32_HI20_RELA, laddr);
10668
10669 if (hi_irel == irelend)
10670 {
10671 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10672 (uint64_t) irel->r_offset);
10673 return FALSE;
10674 }
10675
10676 /* Get the value of the symbol referred to by the reloc. */
10677 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10678
10679 /* This condition only happened when symbol is undefined. */
10680 if (foff == 0
10681 || foff < -CONSERVATIVE_24BIT_S1
10682 || foff >= CONSERVATIVE_24BIT_S1)
10683 return FALSE;
10684
10685 /* Relax to: jal symbol; 25_PCREL. */
10686 /* For simplicity of coding, we are going to modify the section
10687 contents, the section relocs, and the BFD symbol table. We
10688 must tell the rest of the code not to free up this
10689 information. It would be possible to instead create a table
10690 of changes which have to be made, as is done in coff-mips.c;
10691 that would be more work, but would require less memory when
10692 the linker is run. */
10693
10694 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10695 R_NDS32_PTR_RESOLVED, irel->r_addend);
10696 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10697 R_NDS32_EMPTY, irel->r_addend);
10698
10699 if (ptr_irel == irelend || em_irel == irelend)
10700 {
10701 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10702 (uint64_t) irel->r_offset);
10703 return FALSE;
10704 }
10705 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10706 insn = bfd_getb32 (contents + irel->r_addend);
10707 if (insn & 0x80000000)
10708 return FALSE;
10709
10710 /* Replace the long call with a jal. */
10711 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10712 R_NDS32_25_PCREL_RELA);
10713 ptr_irel->r_addend = 1;
10714
10715 /* We don't resolve this here but resolve it in relocate_section. */
10716 insn = INSN_JAL;
10717 bfd_putb32 (insn, contents + em_irel->r_offset);
10718
10719 irel->r_info =
10720 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10721
10722 /* If there is function cse, HI20 can not remove now. */
10723 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10724 R_NDS32_LONGCALL4, laddr);
10725 if (call_irel == irelend)
10726 {
10727 *insn_len = 0;
10728 hi_irel->r_info =
10729 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10730 }
10731
10732 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10733 R_NDS32_INSN16, irel->r_addend);
10734 if (insn_irel != irelend)
10735 insn_irel->r_info =
10736 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10737
10738 return TRUE;
10739 }
10740
10741 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
10742
10743 static bfd_boolean
10744 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10745 Elf_Internal_Rela *internal_relocs, int *insn_len,
10746 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10747 Elf_Internal_Shdr *symtab_hdr)
10748 {
10749 /* The pattern for LONGCALL5.
10750 bltz rt, .L1 ; LONGCALL5/17_PCREL
10751 jal symbol ; 25_PCREL
10752 .L1: */
10753
10754 bfd_vma laddr;
10755 uint32_t insn;
10756 Elf_Internal_Rela *cond_irel, *irelend;
10757 bfd_signed_vma foff;
10758
10759 irelend = internal_relocs + sec->reloc_count;
10760 laddr = irel->r_offset;
10761 insn = bfd_getb32 (contents + laddr);
10762
10763 /* Get the reloc for the address from which the register is
10764 being loaded. This reloc will tell us which function is
10765 actually being called. */
10766 cond_irel =
10767 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10768 R_NDS32_25_PCREL_RELA, irel->r_addend);
10769 if (cond_irel == irelend)
10770 {
10771 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10772 (uint64_t) irel->r_offset);
10773 return FALSE;
10774 }
10775
10776 /* Get the value of the symbol referred to by the reloc. */
10777 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10778
10779 if (foff == 0
10780 || foff < -CONSERVATIVE_16BIT_S1
10781 || foff >= CONSERVATIVE_16BIT_S1)
10782 return FALSE;
10783
10784 /* Relax to bgezal rt, label ; 17_PCREL
10785 or bltzal rt, label ; 17_PCREL. */
10786
10787 /* Convert to complimentary conditional call. */
10788 insn = CONVERT_CONDITION_CALL (insn);
10789
10790 /* For simplicity of coding, we are going to modify the section
10791 contents, the section relocs, and the BFD symbol table. We
10792 must tell the rest of the code not to free up this
10793 information. It would be possible to instead create a table
10794 of changes which have to be made, as is done in coff-mips.c;
10795 that would be more work, but would require less memory when
10796 the linker is run. */
10797
10798 /* Modify relocation and contents. */
10799 cond_irel->r_info =
10800 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10801
10802 /* Replace the long call with a bgezal. */
10803 bfd_putb32 (insn, contents + cond_irel->r_offset);
10804 *insn_len = 0;
10805
10806 /* Clean unnessary relocations. */
10807 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10808
10809 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10810 R_NDS32_17_PCREL_RELA, laddr);
10811 cond_irel->r_info =
10812 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10813
10814 return TRUE;
10815 }
10816
10817 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
10818
10819 static bfd_boolean
10820 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10821 Elf_Internal_Rela *internal_relocs, int *insn_len,
10822 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10823 Elf_Internal_Shdr *symtab_hdr)
10824 {
10825 /* The pattern for LONGCALL6.
10826 bltz rt, .L1 ; LONGCALL6/17_PCREL
10827 sethi ta, hi20(symbol) ; HI20/PTR
10828 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10829 jral ta ; PTR_RES/EMPTY/INSN16
10830 .L1 */
10831
10832 bfd_vma laddr;
10833 uint32_t insn;
10834 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10835 bfd_signed_vma foff;
10836
10837 irelend = internal_relocs + sec->reloc_count;
10838 laddr = irel->r_offset;
10839
10840 /* Get the reloc for the address from which the register is
10841 being loaded. This reloc will tell us which function is
10842 actually being called. */
10843 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10844 R_NDS32_EMPTY, irel->r_addend);
10845
10846 if (em_irel == irelend)
10847 {
10848 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10849 (uint64_t) irel->r_offset);
10850 return FALSE;
10851 }
10852
10853 /* Get the value of the symbol referred to by the reloc. */
10854 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10855
10856 if (foff == 0
10857 || foff < -CONSERVATIVE_24BIT_S1
10858 || foff >= CONSERVATIVE_24BIT_S1)
10859 return FALSE;
10860
10861 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10862 insn = bfd_getb32 (contents + irel->r_addend);
10863 if (insn & 0x80000000)
10864 return FALSE;
10865
10866 insn = bfd_getb32 (contents + laddr);
10867 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10868 {
10869 /* Relax to bgezal rt, label ; 17_PCREL
10870 or bltzal rt, label ; 17_PCREL. */
10871
10872 /* Convert to complimentary conditional call. */
10873 *insn_len = 0;
10874 insn = CONVERT_CONDITION_CALL (insn);
10875 bfd_putb32 (insn, contents + em_irel->r_offset);
10876
10877 em_irel->r_info =
10878 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10879
10880 /* Set resolved relocation. */
10881 cond_irel =
10882 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10883 R_NDS32_PTR_RESOLVED, irel->r_addend);
10884 if (cond_irel == irelend)
10885 {
10886 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10887 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10888 return FALSE;
10889 }
10890 cond_irel->r_addend = 1;
10891
10892 /* Clear relocations. */
10893
10894 irel->r_info =
10895 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10896
10897 cond_irel =
10898 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10899 R_NDS32_17_PCREL_RELA, laddr);
10900 if (cond_irel != irelend)
10901 cond_irel->r_info =
10902 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10903
10904 cond_irel =
10905 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10906 R_NDS32_INSN16, irel->r_addend);
10907 if (cond_irel != irelend)
10908 cond_irel->r_info =
10909 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10910
10911 }
10912 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10913 {
10914 /* Relax to the following instruction sequence
10915 bltz rt, .L1 ; LONGCALL2/17_PCREL
10916 jal symbol ; 25_PCREL/PTR_RES
10917 .L1 */
10918 *insn_len = 4;
10919 /* Convert instruction. */
10920 insn = INSN_JAL;
10921 bfd_putb32 (insn, contents + em_irel->r_offset);
10922
10923 /* Convert relocations. */
10924 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10925 R_NDS32_25_PCREL_RELA);
10926 irel->r_info =
10927 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10928
10929 /* Set resolved relocation. */
10930 cond_irel =
10931 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10932 R_NDS32_PTR_RESOLVED, irel->r_addend);
10933 if (cond_irel == irelend)
10934 {
10935 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10936 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10937 return FALSE;
10938 }
10939 cond_irel->r_addend = 1;
10940
10941 cond_irel =
10942 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10943 R_NDS32_INSN16, irel->r_addend);
10944 if (cond_irel != irelend)
10945 cond_irel->r_info =
10946 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10947 }
10948 return TRUE;
10949 }
10950
10951 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10952
10953 static bfd_boolean
10954 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10955 Elf_Internal_Rela *internal_relocs, int *insn_len,
10956 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10957 Elf_Internal_Shdr *symtab_hdr)
10958 {
10959 /* The pattern for LONGJUMP4.
10960 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10961 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10962 jr ta ; PTR_RES/INSN16/EMPTY */
10963
10964 bfd_vma laddr;
10965 int seq_len; /* Original length of instruction sequence. */
10966 uint32_t insn;
10967 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10968 bfd_signed_vma foff;
10969
10970 irelend = internal_relocs + sec->reloc_count;
10971 seq_len = GET_SEQ_LEN (irel->r_addend);
10972 laddr = irel->r_offset;
10973 *insn_len = seq_len;
10974
10975 /* Get the reloc for the address from which the register is
10976 being loaded. This reloc will tell us which function is
10977 actually being called. */
10978
10979 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10980 R_NDS32_HI20_RELA, laddr);
10981
10982 if (hi_irel == irelend)
10983 {
10984 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10985 (uint64_t) irel->r_offset);
10986 return FALSE;
10987 }
10988
10989 /* Get the value of the symbol referred to by the reloc. */
10990 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10991
10992 if (foff == 0
10993 || foff >= CONSERVATIVE_24BIT_S1
10994 || foff < -CONSERVATIVE_24BIT_S1)
10995 return FALSE;
10996
10997 /* Convert it to "j label", it may be converted to j8 in the final
10998 pass of relaxation. Therefore, we do not consider this currently. */
10999 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11000 R_NDS32_PTR_RESOLVED, irel->r_addend);
11001 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11002 R_NDS32_EMPTY, irel->r_addend);
11003
11004 if (ptr_irel == irelend || em_irel == irelend)
11005 {
11006 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
11007 (uint64_t) irel->r_offset);
11008 return FALSE;
11009 }
11010
11011 em_irel->r_info =
11012 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11013 ptr_irel->r_addend = 1;
11014
11015 /* Write instruction. */
11016 insn = INSN_J;
11017 bfd_putb32 (insn, contents + em_irel->r_offset);
11018
11019 /* Clear relocations. */
11020 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11021
11022 /* If there is function cse, HI20 can not remove now. */
11023 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11024 R_NDS32_LONGJUMP4, laddr);
11025 if (call_irel == irelend)
11026 {
11027 *insn_len = 0;
11028 hi_irel->r_info =
11029 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11030 }
11031
11032 return TRUE;
11033 }
11034
11035 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
11036
11037 static bfd_boolean
11038 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11039 Elf_Internal_Rela *internal_relocs, int *insn_len,
11040 int *seq_len, bfd_byte *contents,
11041 Elf_Internal_Sym *isymbuf,
11042 Elf_Internal_Shdr *symtab_hdr)
11043 {
11044 /* There are 2 variations for LONGJUMP5
11045 case 2-4; 1st insn convertible, 16-bit on.
11046 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11047 j label ; 25_PCREL/INSN16
11048 $1:
11049
11050 case 4-4; 1st insn not convertible
11051 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11052 j label ; 25_PCREL/INSN16
11053 .L1: */
11054
11055 bfd_vma laddr;
11056 Elf_Internal_Rela *cond_irel, *irelend;
11057 unsigned int i;
11058 bfd_signed_vma foff;
11059 uint32_t insn, re_insn = 0;
11060 uint16_t insn16, re_insn16 = 0;
11061 unsigned long reloc;
11062
11063 enum elf_nds32_reloc_type checked_types[] =
11064 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11065 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11066
11067 irelend = internal_relocs + sec->reloc_count;
11068 laddr = irel->r_offset;
11069
11070 /* Get the reloc for the address from which the register is
11071 being loaded. This reloc will tell us which function is
11072 actually being called. */
11073
11074 cond_irel =
11075 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11076 R_NDS32_25_PCREL_RELA, irel->r_addend);
11077 if (cond_irel == irelend)
11078 {
11079 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11080 (uint64_t) irel->r_offset);
11081 return FALSE;
11082 }
11083
11084 /* Get the value of the symbol referred to by the reloc. */
11085 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11086
11087 if (foff == 0
11088 || foff < -CONSERVATIVE_16BIT_S1
11089 || foff >= CONSERVATIVE_16BIT_S1)
11090 return FALSE;
11091
11092 /* Get the all corresponding instructions. */
11093 insn = bfd_getb32 (contents + laddr);
11094 /* Check instruction size. */
11095 if (insn & 0x80000000)
11096 {
11097 *seq_len = 0;
11098 insn16 = insn >> 16;
11099 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11100 }
11101 else
11102 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11103
11104 if (N32_OP6 (re_insn) == N32_OP6_BR1
11105 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11106 {
11107 /* beqs label ; 15_PCREL. */
11108 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11109 reloc = R_NDS32_15_PCREL_RELA;
11110 }
11111 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11112 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11113 {
11114 /* beqz label ; 17_PCREL. */
11115 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11116 reloc = R_NDS32_17_PCREL_RELA;
11117 }
11118 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11119 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11120 {
11121 /* beqc label ; 9_PCREL. */
11122 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11123 reloc = R_NDS32_WORD_9_PCREL_RELA;
11124 }
11125 else
11126 return FALSE;
11127
11128 /* Set all relocations. */
11129 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11130
11131 /* Clean relocations. */
11132 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11133 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11134 {
11135 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11136 checked_types[i], laddr);
11137 if (cond_irel != irelend)
11138 {
11139 if (*seq_len == 0
11140 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11141 {
11142 /* If the branch instruction is 2 byte, it cannot remove
11143 directly. Only convert it to nop16 and remove it after
11144 checking alignment issue. */
11145 insn16 = NDS32_NOP16;
11146 bfd_putb16 (insn16, contents + laddr);
11147 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11148 }
11149 else
11150 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11151 R_NDS32_NONE);
11152 }
11153 }
11154 *insn_len = 0;
11155
11156 return TRUE;
11157 }
11158
11159 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
11160
11161 static bfd_boolean
11162 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11163 Elf_Internal_Rela *internal_relocs, int *insn_len,
11164 int *seq_len, bfd_byte *contents,
11165 Elf_Internal_Sym *isymbuf,
11166 Elf_Internal_Shdr *symtab_hdr)
11167 {
11168 /* There are 5 variations for LONGJUMP6
11169 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11170 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11171 sethi ta, hi20(symbol) ; HI20/PTR
11172 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11173 jr ta ; PTR_RES/INSN16/EMPTY
11174 .L1:
11175
11176 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11177 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11178 sethi ta, hi20(symbol) ; HI20/PTR
11179 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11180 jr ta ; PTR_RES/INSN16/EMPTY
11181 .L1: */
11182
11183 enum elf_nds32_reloc_type checked_types[] =
11184 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11185 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11186
11187 int reloc_off = 0, cond_removed = 0;
11188 bfd_vma laddr;
11189 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11190 unsigned int i;
11191 bfd_signed_vma foff;
11192 uint32_t insn, re_insn = 0;
11193 uint16_t insn16, re_insn16 = 0;
11194 unsigned long reloc;
11195
11196 irelend = internal_relocs + sec->reloc_count;
11197 laddr = irel->r_offset;
11198
11199 /* Get the reloc for the address from which the register is
11200 being loaded. This reloc will tell us which function is
11201 actually being called. */
11202 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11203 R_NDS32_EMPTY, irel->r_addend);
11204
11205 if (em_irel == irelend)
11206 {
11207 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11208 (uint64_t) irel->r_offset);
11209 return FALSE;
11210 }
11211
11212 /* Get the value of the symbol referred to by the reloc. */
11213 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11214
11215 if (foff == 0
11216 || foff < -CONSERVATIVE_24BIT_S1
11217 || foff >= CONSERVATIVE_24BIT_S1)
11218 return FALSE;
11219
11220 insn = bfd_getb32 (contents + laddr);
11221 /* Check instruction size. */
11222 if (insn & 0x80000000)
11223 {
11224 *seq_len = 0;
11225 insn16 = insn >> 16;
11226 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11227 }
11228 else
11229 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11230
11231 /* For simplicity of coding, we are going to modify the section
11232 contents, the section relocs, and the BFD symbol table. We
11233 must tell the rest of the code not to free up this
11234 information. It would be possible to instead create a table
11235 of changes which have to be made, as is done in coff-mips.c;
11236 that would be more work, but would require less memory when
11237 the linker is run. */
11238
11239 if (N32_OP6 (re_insn) == N32_OP6_BR1
11240 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11241 {
11242 /* beqs label ; 15_PCREL. */
11243 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11244 reloc = R_NDS32_15_PCREL_RELA;
11245 cond_removed = 1;
11246 }
11247 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11248 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11249 {
11250 /* beqz label ; 17_PCREL. */
11251 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11252 reloc = R_NDS32_17_PCREL_RELA;
11253 cond_removed = 1;
11254 }
11255 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11256 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11257 {
11258 /* Relax to one of the following 2 variations
11259
11260 case 2-4; 1st insn convertible, 16-bit on.
11261 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11262 j label ; 25_PCREL/INSN16
11263 $1:
11264
11265 case 4-4; 1st insn not convertible
11266 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11267 j label ; 25_PCREL/INSN16
11268 .L1: */
11269
11270 /* Use j label as second instruction. */
11271 insn = INSN_J;
11272 reloc = R_NDS32_25_PCREL_RELA;
11273 bfd_putb32 (insn, contents + em_irel->r_offset);
11274 }
11275 else
11276 return FALSE;
11277
11278 /* Set all relocations. */
11279 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11280
11281 cond_irel =
11282 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11283 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11284 cond_irel->r_addend = 1;
11285
11286 /* Use INSN16 of first branch instruction to distinguish if keeping
11287 INSN16 of final instruction or not. */
11288 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11289 R_NDS32_INSN16, irel->r_offset);
11290 if (insn_irel == irelend)
11291 {
11292 /* Clean the final INSN16. */
11293 insn_irel =
11294 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11295 R_NDS32_INSN16, em_irel->r_offset);
11296 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11297 R_NDS32_NONE);
11298 }
11299
11300 if (cond_removed == 1)
11301 {
11302 *insn_len = 0;
11303
11304 /* Clear relocations. */
11305 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11306
11307 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11308 {
11309 cond_irel =
11310 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11311 checked_types[i], laddr);
11312 if (cond_irel != irelend)
11313 {
11314 if (*seq_len == 0
11315 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11316 {
11317 /* If the branch instruction is 2 byte, it cannot remove
11318 directly. Only convert it to nop16 and remove it after
11319 checking alignment issue. */
11320 insn16 = NDS32_NOP16;
11321 bfd_putb16 (insn16, contents + laddr);
11322 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11323 }
11324 else
11325 cond_irel->r_info =
11326 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11327 }
11328 }
11329 }
11330 else
11331 {
11332 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11333 R_NDS32_LONGJUMP5);
11334 }
11335
11336 return TRUE;
11337 }
11338
11339 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
11340
11341 static bfd_boolean
11342 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11343 Elf_Internal_Rela *internal_relocs, int *insn_len,
11344 int *seq_len, bfd_byte *contents,
11345 Elf_Internal_Sym *isymbuf,
11346 Elf_Internal_Shdr *symtab_hdr)
11347 {
11348 /* There are 2 variations for LONGJUMP5
11349 case 2-4; 1st insn convertible, 16-bit on.
11350 movi55 ta, imm11 ; LONGJUMP7/INSN16
11351 beq rt, ta, label ; 15_PCREL
11352
11353 case 4-4; 1st insn not convertible
11354 movi55 ta, imm11 ; LONGJUMP7/INSN16
11355 beq rt, ta, label ; 15_PCREL */
11356
11357 bfd_vma laddr;
11358 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
11359 bfd_signed_vma foff;
11360 uint32_t insn, re_insn = 0;
11361 uint16_t insn16;
11362 uint32_t imm11;
11363
11364 irelend = internal_relocs + sec->reloc_count;
11365 laddr = irel->r_offset;
11366
11367 /* Get the reloc for the address from which the register is
11368 being loaded. This reloc will tell us which function is
11369 actually being called. */
11370
11371 cond_irel =
11372 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11373 R_NDS32_15_PCREL_RELA, irel->r_addend);
11374 if (cond_irel == irelend)
11375 {
11376 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11377 (uint64_t) irel->r_offset);
11378 return FALSE;
11379 }
11380
11381 /* Get the value of the symbol referred to by the reloc. */
11382 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11383
11384 if (foff == 0
11385 || foff < -CONSERVATIVE_8BIT_S1
11386 || foff >= CONSERVATIVE_8BIT_S1)
11387 return FALSE;
11388
11389 /* Get the first instruction for its size. */
11390 insn = bfd_getb32 (contents + laddr);
11391 if (insn & 0x80000000)
11392 {
11393 *seq_len = 0;
11394 /* Get the immediate from movi55. */
11395 imm11 = N16_IMM5S (insn >> 16);
11396 }
11397 else
11398 {
11399 /* Get the immediate from movi. */
11400 imm11 = N32_IMM20S (insn);
11401 }
11402
11403 /* Get the branch instruction. */
11404 insn = bfd_getb32 (contents + irel->r_addend);
11405 /* Convert instruction to BR3. */
11406 if ((insn >> 14) & 0x1)
11407 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11408 else
11409 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11410
11411 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11412
11413 /* Set all relocations. */
11414 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11415 R_NDS32_WORD_9_PCREL_RELA);
11416
11417 /* Clean relocations. */
11418 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11419 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11420 R_NDS32_INSN16, irel->r_offset);
11421 if (insn_irel != irelend)
11422 {
11423 if (*seq_len == 0)
11424 {
11425 /* If the first insntruction is 16bit, convert it to nop16. */
11426 insn16 = NDS32_NOP16;
11427 bfd_putb16 (insn16, contents + laddr);
11428 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11429 }
11430 else
11431 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11432 R_NDS32_NONE);
11433 }
11434 *insn_len = 0;
11435
11436 return TRUE;
11437 }
11438
11439 /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11440 for each relax round. But the gp may changed dramatically and then cause
11441 the truncated to fit errors for the the converted gp instructions.
11442 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11443
11444 static bfd_boolean
11445 nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11446 Elf_Internal_Rela *irel, bfd_boolean *again,
11447 bfd_boolean init,
11448 struct elf_nds32_link_hash_table *table,
11449 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11450
11451 {
11452 int offset_to_gp;
11453 static bfd_boolean sec_pass = FALSE;
11454 static asection *first_sec = NULL, *sym_sec;
11455 /* Record the number of instructions which may be removed. */
11456 static int count = 0, record_count;
11457 Elf_Internal_Sym *isym;
11458 struct elf_link_hash_entry *h = NULL;
11459 int indx;
11460 unsigned long r_symndx;
11461 bfd *abfd = sec->owner;
11462 static bfd_vma record_sda = 0;
11463 int sda_offset = 0;
11464
11465 /* Force doing relaxation when hyper-relax is high. */
11466 if (table->hyper_relax == 2)
11467 return TRUE;
11468
11469 /* Do not relax the load/store patterns for the first
11470 relax round. */
11471 if (init)
11472 {
11473 if (!first_sec)
11474 first_sec = sec;
11475 else if (first_sec == sec)
11476 {
11477 record_count = count;
11478 count = 0;
11479 sec_pass = TRUE;
11480 }
11481
11482 if (!sec_pass)
11483 *again = TRUE;
11484
11485 return TRUE;
11486 }
11487
11488 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11489 DATA_SEGMENT_ALIGN size in the linker script may make it
11490 get even bigger. */
11491 if (record_sda == 0)
11492 record_sda = local_sda;
11493 else if (local_sda > record_sda)
11494 sda_offset = local_sda - record_sda;
11495
11496 /* Assume the instruction will be removed in the best case. */
11497 count++;
11498
11499 /* We record the offset to gp for each symbol, and then check
11500 if it is changed dramatically after relaxing.
11501 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11502 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11503 r_symndx = ELF32_R_SYM (irel->r_info);
11504 if (r_symndx >= symtab_hdr->sh_info)
11505 {
11506 /* Global symbols. */
11507 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11508 h = elf_sym_hashes (abfd)[indx];
11509 sym_sec = h->root.u.def.section;
11510 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11511 || bfd_is_abs_section (sym_sec))
11512 {
11513 /* Forbid doing relaxation when hyper-relax is low. */
11514 if (table->hyper_relax == 0)
11515 return FALSE;
11516
11517 offset_to_gp = *access_addr - local_sda;
11518 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11519 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11520 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11521 < abs (offset_to_gp) - sda_offset)
11522 {
11523 /* This may cause the error, so we reserve the
11524 safe enough size for relaxing. */
11525 if (*access_addr >= local_sda)
11526 *access_addr += (record_count * 4);
11527 else
11528 *access_addr -= (record_count * 4);
11529 }
11530 return sec_pass;
11531 }
11532 }
11533 else
11534 {
11535 /* Local symbols. */
11536 if (!elf32_nds32_allocate_local_sym_info (abfd))
11537 return FALSE;
11538 isym = isymbuf + r_symndx;
11539
11540 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11541 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11542 {
11543 /* Forbid doing relaxation when hyper-relax is low. */
11544 if (table->hyper_relax == 0)
11545 return FALSE;
11546
11547 offset_to_gp = *access_addr - local_sda;
11548 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11549 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11550 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11551 < abs (offset_to_gp) - sda_offset)
11552 {
11553 /* This may cause the error, so we reserve the
11554 safe enough size for relaxing. */
11555 if (*access_addr >= local_sda)
11556 *access_addr += (record_count * 4);
11557 else
11558 *access_addr -= (record_count * 4);
11559 }
11560 return sec_pass;
11561 }
11562 }
11563
11564 return TRUE;
11565 }
11566
11567 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11568
11569 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
11570
11571 static bfd_boolean
11572 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11573 asection *sec, Elf_Internal_Rela *irel,
11574 Elf_Internal_Rela *internal_relocs, int *insn_len,
11575 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11576 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11577 struct elf_nds32_link_hash_table *table)
11578 {
11579 int eliminate_sethi = 0, range_type;
11580 unsigned int i;
11581 bfd_vma local_sda, laddr;
11582 int seq_len; /* Original length of instruction sequence. */
11583 uint32_t insn;
11584 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11585 bfd_vma access_addr = 0;
11586 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11587 struct elf_link_hash_entry *h = NULL;
11588 int indx;
11589 enum elf_nds32_reloc_type checked_types[] =
11590 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11591 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11592 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11593 R_NDS32_TLS_LE_HI20
11594 };
11595
11596 irelend = internal_relocs + sec->reloc_count;
11597 seq_len = GET_SEQ_LEN (irel->r_addend);
11598 laddr = irel->r_offset;
11599 *insn_len = seq_len;
11600
11601 /* Get the high part relocation. */
11602 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11603 {
11604 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11605 checked_types[i], laddr);
11606 if (hi_irelfn != irelend)
11607 break;
11608 }
11609
11610 if (hi_irelfn == irelend)
11611 {
11612 /* Not R_NDS32_HI20_RELA. */
11613 if (i != 0)
11614 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11615 (uint64_t) irel->r_offset);
11616 return FALSE;
11617 }
11618
11619 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11620 nds32_elf_final_sda_base (sec->output_section->owner,
11621 link_info, &local_sda, FALSE);
11622
11623 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11624 {
11625 case R_NDS32_HI20_RELA:
11626 insn = bfd_getb32 (contents + laddr);
11627 access_addr =
11628 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11629
11630 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11631 {
11632 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11633 h = elf_sym_hashes (abfd)[indx];
11634 }
11635
11636 /* Try movi. */
11637 if (range_type == NDS32_LOADSTORE_IMM
11638 && access_addr < CONSERVATIVE_20BIT
11639 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11640 {
11641 eliminate_sethi = 1;
11642 break;
11643 }
11644
11645 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11646 {
11647 eliminate_sethi = 1;
11648 break;
11649 }
11650 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11651 NULL, FALSE, table, isymbuf, symtab_hdr))
11652 return FALSE;
11653
11654 if (!load_store_relax)
11655 return FALSE;
11656
11657 /* Case for set gp register. */
11658 if (N32_RT5 (insn) == REG_GP)
11659 return FALSE;
11660
11661 if (range_type == NDS32_LOADSTORE_FLOAT_S
11662 || range_type == NDS32_LOADSTORE_FLOAT_D)
11663 {
11664 range_l = sdata_range[0][0];
11665 range_h = sdata_range[0][1];
11666 }
11667 else
11668 {
11669 range_l = sdata_range[1][0];
11670 range_h = sdata_range[1][1];
11671 }
11672 break;
11673
11674 default:
11675 return FALSE;
11676 }
11677
11678 /* Delete sethi instruction. */
11679 if (eliminate_sethi == 1
11680 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11681 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11682 {
11683 hi_irelfn->r_info =
11684 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11685 irel->r_info =
11686 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11687 *insn_len = 0;
11688 return TRUE;
11689 }
11690
11691 return FALSE;
11692 }
11693
11694 /* Relax LO12 relocation for nds32_elf_relax_section. */
11695
11696 static void
11697 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11698 asection *sec, Elf_Internal_Rela *irel,
11699 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11700 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11701 struct elf_nds32_link_hash_table *table)
11702 {
11703 uint32_t insn;
11704 bfd_vma local_sda, laddr;
11705 unsigned long reloc;
11706 bfd_vma access_addr;
11707 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11708 Elf_Internal_Rela *irelfn = NULL, *irelend;
11709 struct elf_link_hash_entry *h = NULL;
11710 int indx;
11711
11712 /* For SDA base relative relaxation. */
11713 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11714 &local_sda, FALSE);
11715
11716 irelend = internal_relocs + sec->reloc_count;
11717 laddr = irel->r_offset;
11718 insn = bfd_getb32 (contents + laddr);
11719
11720 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11721 return;
11722
11723 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11724
11725 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11726 {
11727 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11728 h = elf_sym_hashes (abfd)[indx];
11729 }
11730
11731 /* Try movi. */
11732 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11733 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11734 {
11735 reloc = R_NDS32_20_RELA;
11736 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11737 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11738 bfd_putb32 (insn, contents + laddr);
11739 }
11740 else
11741 {
11742 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11743 {
11744 /* Fall through. */
11745 }
11746 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11747 FALSE, table, isymbuf, symtab_hdr))
11748 return;
11749
11750 range_l = sdata_range[1][0];
11751 range_h = sdata_range[1][1];
11752 switch (ELF32_R_TYPE (irel->r_info))
11753 {
11754 case R_NDS32_LO12S0_RELA:
11755 reloc = R_NDS32_SDA19S0_RELA;
11756 break;
11757 case R_NDS32_LO12S1_RELA:
11758 reloc = R_NDS32_SDA18S1_RELA;
11759 break;
11760 case R_NDS32_LO12S2_RELA:
11761 reloc = R_NDS32_SDA17S2_RELA;
11762 break;
11763 case R_NDS32_LO12S2_DP_RELA:
11764 range_l = sdata_range[0][0];
11765 range_h = sdata_range[0][1];
11766 reloc = R_NDS32_SDA12S2_DP_RELA;
11767 break;
11768 case R_NDS32_LO12S2_SP_RELA:
11769 range_l = sdata_range[0][0];
11770 range_h = sdata_range[0][1];
11771 reloc = R_NDS32_SDA12S2_SP_RELA;
11772 break;
11773 default:
11774 return;
11775 }
11776
11777 /* There are range_h and range_l because linker has to promise
11778 all sections move cross one page together. */
11779 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11780 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11781 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11782 {
11783 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11784 {
11785 /* Maybe we should add R_NDS32_INSN16 reloc type here
11786 or manually do some optimization. sethi can't be
11787 eliminated when updating $gp so the relative ori
11788 needs to be preserved. */
11789 return;
11790 }
11791 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11792 &insn))
11793 return;
11794 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11795 bfd_putb32 (insn, contents + laddr);
11796
11797 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11798 R_NDS32_INSN16);
11799 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11800 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11801 irelfn->r_info =
11802 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11803
11804 }
11805 }
11806 return;
11807 }
11808
11809 /* Relax PTR relocation for nds32_elf_relax_section. */
11810
11811 static bfd_boolean
11812 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11813 Elf_Internal_Rela *internal_relocs, int *insn_len,
11814 int *seq_len, bfd_byte *contents)
11815 {
11816 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11817
11818 irelend = internal_relocs + sec->reloc_count;
11819
11820 re_irel =
11821 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11822 R_NDS32_PTR_RESOLVED, irel->r_addend);
11823
11824 if (re_irel == irelend)
11825 {
11826 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11827 (uint64_t) irel->r_offset);
11828 return FALSE;
11829 }
11830
11831 if (re_irel->r_addend != 1)
11832 return FALSE;
11833
11834 /* Pointed target is relaxed and no longer needs this void *,
11835 change the type to NONE. */
11836 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11837
11838 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11839 not exist, it means only count 1 and remove it directly. */
11840 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11841 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11842 R_NDS32_PTR_COUNT);
11843 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11844 R_NDS32_PTR);
11845 if (count_irel != irelend)
11846 {
11847 if (--count_irel->r_addend > 0)
11848 return FALSE;
11849 }
11850
11851 if (ptr_irel != irelend)
11852 return FALSE;
11853
11854 /* If the PTR_COUNT is already 0, remove current instruction. */
11855 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11856 *insn_len = 0;
11857 return TRUE;
11858 }
11859
11860 /* Relax LWC relocation for nds32_elf_relax_section. */
11861
11862 static void
11863 nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11864 asection *sec, Elf_Internal_Rela *irel,
11865 Elf_Internal_Rela *internal_relocs,
11866 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11867 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11868 {
11869 /* Pattern:
11870 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11871 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11872 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11873 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11874 ... */
11875
11876 uint32_t insn;
11877 bfd_vma local_sda, laddr;
11878 unsigned long reloc;
11879 bfd_vma access_addr, flsi_offset;
11880 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11881 Elf_Internal_Rela *irelend, *re_irel;
11882 unsigned int opcode;
11883
11884 irelend = internal_relocs + sec->reloc_count;
11885 laddr = irel->r_offset;
11886 insn = bfd_getb32 (contents + laddr);
11887
11888 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11889 return;
11890
11891 /* Can not do relaxation for bi format. */
11892 if ((insn & 0x1000))
11893 return;
11894
11895 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11896 opcode = N32_OP6 (insn);
11897 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11898 reloc = R_NDS32_SDA12S2_SP_RELA;
11899 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11900 reloc = R_NDS32_SDA12S2_DP_RELA;
11901 else
11902 return;
11903
11904 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11905 R_NDS32_PTR_RESOLVED);
11906 if (re_irel == irelend)
11907 {
11908 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11909 (uint64_t) irel->r_offset);
11910 return;
11911 }
11912
11913 /* For SDA base relative relaxation. */
11914 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11915 &local_sda, FALSE);
11916 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11917 flsi_offset = (insn & 0xfff) << 2;
11918 access_addr += flsi_offset;
11919 range_l = sdata_range[0][0];
11920 range_h = sdata_range[0][1];
11921
11922 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11923 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11924 {
11925 /* Turn flsi instruction into sda access format. */
11926 insn = (insn & 0x7ff07000) | (REG_GP << 15);
11927
11928 /* Add relocation type to flsi. */
11929 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11930 irel->r_addend += flsi_offset;
11931 bfd_putb32 (insn, contents + re_irel->r_offset);
11932
11933 re_irel->r_addend |= 1;
11934 *again = TRUE;
11935 }
11936 }
11937
11938 static bfd_boolean
11939 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11940 Elf_Internal_Rela *internal_relocs,
11941 bfd_byte *contents,
11942 nds32_elf_blank_t **relax_blank_list,
11943 int optimize, int opt_size)
11944 {
11945 /* This code block is used to adjust 4-byte alignment by relax a pair
11946 of instruction a time.
11947
11948 It recognizes three types of relocations.
11949 1. R_NDS32_LABEL - a alignment.
11950 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11951 3. is_16bit_NOP () - remove a 16-bit instruction. */
11952
11953 /* TODO: It seems currently implementation only support 4-byte alignment.
11954 We should handle any-alignment. */
11955
11956 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11957 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11958 Elf_Internal_Rela rel_temp;
11959 Elf_Internal_Rela *irelend;
11960 bfd_vma address;
11961 uint16_t insn16;
11962
11963 /* Checking for branch relaxation relies on the relocations to
11964 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11965 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11966 sizeof (Elf_Internal_Rela), compar_reloc);
11967
11968 irelend = internal_relocs + sec->reloc_count;
11969
11970 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11971 /* FIXME: Can we generate the right order in assembler?
11972 So we don't have to swapping them here. */
11973
11974 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11975 label_rel < irelend; label_rel++)
11976 {
11977 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11978 continue;
11979
11980 /* Find the first reloc has the same offset with label_rel. */
11981 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11982 insn_rel++;
11983
11984 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11985 insn_rel++)
11986 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11987 address. */
11988 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11989 break;
11990
11991 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11992 && insn_rel < label_rel)
11993 {
11994 /* Swap the two reloc if the R_NDS32_INSN16 is
11995 before R_NDS32_LABEL. */
11996 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11997 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11998 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11999 }
12000 }
12001
12002 label_rel = NULL;
12003 insn_rel = NULL;
12004 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12005 or higher, remove other R_NDS32_LABEL with lower alignment.
12006 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12007 then the R_NDS32_LABEL sequence is broke. */
12008 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12009 {
12010 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12011 {
12012 if (label_rel == NULL)
12013 {
12014 if (tmp_rel->r_addend < 2)
12015 label_rel = tmp_rel;
12016 continue;
12017 }
12018 else if (tmp_rel->r_addend > 1)
12019 {
12020 /* Remove all LABEL relocation from label_rel to tmp_rel
12021 including relocations with same offset as tmp_rel. */
12022 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
12023 {
12024 if (tmp2_rel->r_offset == tmp_rel->r_offset)
12025 break;
12026
12027 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12028 && tmp2_rel->r_addend < 2)
12029 tmp2_rel->r_info =
12030 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12031 R_NDS32_NONE);
12032 }
12033 label_rel = NULL;
12034 }
12035 }
12036 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12037 {
12038 /* A new INSN16 which can be converted, so clear label_rel. */
12039 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12040 irelend, &insn16)
12041 || is_16bit_NOP (abfd, sec, tmp_rel))
12042 label_rel = NULL;
12043 }
12044 }
12045
12046 label_rel = NULL;
12047 insn_rel = NULL;
12048 /* Optimized for speed and nothing has not been relaxed.
12049 It's time to align labels.
12050 We may convert a 16-bit instruction right before a label to
12051 32-bit, in order to align the label if necessary
12052 all reloc entries has been sorted by r_offset. */
12053 for (irel = internal_relocs;
12054 irel < irelend && irel->r_offset < sec->size; irel++)
12055 {
12056 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12057 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12058 continue;
12059
12060 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12061 {
12062 /* A new INSN16 found, resize the old one. */
12063 if (is_convert_32_to_16
12064 (abfd, sec, irel, internal_relocs, irelend, &insn16)
12065 || is_16bit_NOP (abfd, sec, irel))
12066 {
12067 if (insn_rel)
12068 {
12069 /* Previous INSN16 reloc exists, reduce its
12070 size to 16-bit. */
12071 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12072 irelend, &insn16))
12073 {
12074 nds32_elf_write_16 (abfd, contents, insn_rel,
12075 internal_relocs, irelend, insn16);
12076
12077 if (!insert_nds32_elf_blank_recalc_total
12078 (relax_blank_list, insn_rel->r_offset + 2, 2))
12079 return FALSE;
12080 }
12081 else if (is_16bit_NOP (abfd, sec, insn_rel))
12082 {
12083 if (!insert_nds32_elf_blank_recalc_total
12084 (relax_blank_list, insn_rel->r_offset, 2))
12085 return FALSE;
12086 }
12087 insn_rel->r_info =
12088 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12089 }
12090 /* Save the new one for later use. */
12091 insn_rel = irel;
12092 }
12093 else
12094 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12095 R_NDS32_NONE);
12096 }
12097 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12098 {
12099 /* Search for label. */
12100 int force_relax = 0;
12101
12102 /* Label on 16-bit instruction or optimization
12103 needless, just reset this reloc. */
12104 insn16 = bfd_getb16 (contents + irel->r_offset);
12105 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12106 {
12107 irel->r_info =
12108 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12109 continue;
12110 }
12111
12112 address =
12113 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12114 irel->r_offset, 1);
12115
12116 if (!insn_rel)
12117 {
12118 /* Check if there is case which can not be aligned. */
12119 if (irel->r_addend == 2 && address & 0x2)
12120 return FALSE;
12121 continue;
12122 }
12123
12124 /* Try to align this label. */
12125
12126 if ((irel->r_addend & 0x1f) < 2)
12127 {
12128 /* Check if there is a INSN16 at the same address.
12129 Label_rel always seats before insn_rel after
12130 our sort. */
12131
12132 /* Search for INSN16 at LABEL location. If INSN16 is at
12133 same location and this LABEL alignment is lower than 2,
12134 the INSN16 can be converted to 2-byte. */
12135 for (tmp_rel = irel;
12136 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12137 tmp_rel++)
12138 {
12139 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12140 && (is_convert_32_to_16
12141 (abfd, sec, tmp_rel, internal_relocs,
12142 irelend, &insn16)
12143 || is_16bit_NOP (abfd, sec, tmp_rel)))
12144 {
12145 force_relax = 1;
12146 break;
12147 }
12148 }
12149 }
12150
12151 if (force_relax || irel->r_addend == 1 || address & 0x2)
12152 {
12153 /* Label not aligned. */
12154 /* Previous reloc exists, reduce its size to 16-bit. */
12155 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12156 internal_relocs, irelend, &insn16))
12157 {
12158 nds32_elf_write_16 (abfd, contents, insn_rel,
12159 internal_relocs, irelend, insn16);
12160
12161 if (!insert_nds32_elf_blank_recalc_total
12162 (relax_blank_list, insn_rel->r_offset + 2, 2))
12163 return FALSE;
12164 }
12165 else if (is_16bit_NOP (abfd, sec, insn_rel))
12166 {
12167 if (!insert_nds32_elf_blank_recalc_total
12168 (relax_blank_list, insn_rel->r_offset, 2))
12169 return FALSE;
12170 }
12171
12172 }
12173 /* INSN16 reloc is used. */
12174 insn_rel = NULL;
12175 }
12176 }
12177
12178 address =
12179 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12180 if (insn_rel && (address & 0x2 || opt_size))
12181 {
12182 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12183 irelend, &insn16))
12184 {
12185 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12186 irelend, insn16);
12187 if (!insert_nds32_elf_blank_recalc_total
12188 (relax_blank_list, insn_rel->r_offset + 2, 2))
12189 return FALSE;
12190 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12191 R_NDS32_NONE);
12192 }
12193 else if (is_16bit_NOP (abfd, sec, insn_rel))
12194 {
12195 if (!insert_nds32_elf_blank_recalc_total
12196 (relax_blank_list, insn_rel->r_offset, 2))
12197 return FALSE;
12198 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12199 R_NDS32_NONE);
12200 }
12201 }
12202 insn_rel = NULL;
12203 return TRUE;
12204 }
12205
12206 static bfd_boolean
12207 nds32_elf_relax_section (bfd *abfd, asection *sec,
12208 struct bfd_link_info *link_info, bfd_boolean *again)
12209 {
12210 nds32_elf_blank_t *relax_blank_list = NULL;
12211 Elf_Internal_Shdr *symtab_hdr;
12212 Elf_Internal_Rela *internal_relocs;
12213 Elf_Internal_Rela *irel;
12214 Elf_Internal_Rela *irelend;
12215 Elf_Internal_Sym *isymbuf = NULL;
12216 bfd_byte *contents = NULL;
12217 bfd_boolean result = TRUE;
12218 int optimize = 0;
12219 int opt_size = 0;
12220 uint32_t insn;
12221 uint16_t insn16;
12222
12223 /* Target dependnet option. */
12224 struct elf_nds32_link_hash_table *table;
12225 int load_store_relax;
12226
12227 relax_blank_list = NULL;
12228
12229 *again = FALSE;
12230
12231 /* Nothing to do for
12232 * relocatable link or
12233 * non-relocatable section or
12234 * non-code section or
12235 * empty content or
12236 * no reloc entry. */
12237 if (bfd_link_relocatable (link_info)
12238 || (sec->flags & SEC_RELOC) == 0
12239 || (sec->flags & SEC_EXCLUDE) != 0
12240 || (sec->flags & SEC_CODE) == 0
12241 || sec->size == 0
12242 || sec->reloc_count == 0)
12243 return TRUE;
12244
12245 /* 09.12.11 Workaround. */
12246 /* We have to adjust align for R_NDS32_LABEL if needed.
12247 The adjust approach only can fix 2-byte align once. */
12248 if (sec->alignment_power > 2)
12249 return TRUE;
12250
12251 /* Do TLS model conversion once at first. */
12252 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12253
12254 /* The optimization type to do. */
12255
12256 table = nds32_elf_hash_table (link_info);
12257
12258 /* Save the first section for abs symbol relaxation.
12259 This is used for checking gp relaxation in the
12260 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12261 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12262 table, NULL, NULL);
12263
12264 /* The begining of general relaxation. */
12265
12266 if (is_SDA_BASE_set == 0)
12267 {
12268 bfd_vma gp;
12269 is_SDA_BASE_set = 1;
12270 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12271 &gp, FALSE);
12272 relax_range_measurement (abfd);
12273 }
12274
12275 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12276 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12277 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12278 TRUE /* keep_memory */);
12279 if (internal_relocs == NULL)
12280 goto error_return;
12281
12282 irelend = internal_relocs + sec->reloc_count;
12283 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12284 irelend, R_NDS32_RELAX_ENTRY);
12285
12286 if (irel == irelend)
12287 return TRUE;
12288
12289 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12290 {
12291 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12292 return TRUE;
12293
12294 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12295 optimize = 1;
12296
12297 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12298 opt_size = 1;
12299 }
12300
12301 load_store_relax = table->load_store_relax;
12302
12303 /* Get symbol table and section content. */
12304 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12305 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12306 goto error_return;
12307
12308 /* Do relax loop only when finalize is not done.
12309 Take care of relaxable relocs except INSN16. */
12310 for (irel = internal_relocs; irel < irelend; irel++)
12311 {
12312 int seq_len; /* Original length of instruction sequence. */
12313 int insn_len = 0; /* Final length of instruction sequence. */
12314 bfd_boolean removed;
12315
12316 insn = 0;
12317 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12318 && (irel->r_addend & 0x1f) >= 2)
12319 optimize = 1;
12320
12321 /* Relocation Types
12322 R_NDS32_LONGCALL1 53
12323 R_NDS32_LONGCALL2 54
12324 R_NDS32_LONGCALL3 55
12325 R_NDS32_LONGJUMP1 56
12326 R_NDS32_LONGJUMP2 57
12327 R_NDS32_LONGJUMP3 58
12328 R_NDS32_LOADSTORE 59 */
12329 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12330 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12331 seq_len = GET_SEQ_LEN (irel->r_addend);
12332
12333 /* Relocation Types
12334 R_NDS32_LONGCALL4 107
12335 R_NDS32_LONGCALL5 108
12336 R_NDS32_LONGCALL6 109
12337 R_NDS32_LONGJUMP4 110
12338 R_NDS32_LONGJUMP5 111
12339 R_NDS32_LONGJUMP6 112
12340 R_NDS32_LONGJUMP7 113 */
12341 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12342 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12343 seq_len = 4;
12344
12345 /* Relocation Types
12346 R_NDS32_LO12S0_RELA 30
12347 R_NDS32_LO12S1_RELA 29
12348 R_NDS32_LO12S2_RELA 28
12349 R_NDS32_LO12S2_SP_RELA 71
12350 R_NDS32_LO12S2_DP_RELA 70
12351 R_NDS32_GOT_LO12 46
12352 R_NDS32_GOTOFF_LO12 50
12353 R_NDS32_PLTREL_LO12 65
12354 R_NDS32_PLT_GOTREL_LO12 67
12355 R_NDS32_17IFC_PCREL_RELA 96
12356 R_NDS32_GOT_SUFF 193
12357 R_NDS32_GOTOFF_SUFF 194
12358 R_NDS32_PLT_GOT_SUFF 195
12359 R_NDS32_MULCALL_SUFF 196
12360 R_NDS32_PTR 197 */
12361 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12362 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12363 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12364 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12365 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12366 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12367 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12368 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12369 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12370 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12371 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12372 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12373 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12374 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12375 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12376 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12377 seq_len = 0;
12378 else
12379 continue;
12380
12381 insn_len = seq_len;
12382 removed = FALSE;
12383
12384 switch (ELF32_R_TYPE (irel->r_info))
12385 {
12386 case R_NDS32_LONGCALL1:
12387 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12388 &insn_len, contents, isymbuf,
12389 symtab_hdr);
12390 break;
12391 case R_NDS32_LONGCALL2:
12392 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12393 &insn_len, contents, isymbuf,
12394 symtab_hdr);
12395 break;
12396 case R_NDS32_LONGCALL3:
12397 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12398 &insn_len, contents, isymbuf,
12399 symtab_hdr);
12400 break;
12401 case R_NDS32_LONGJUMP1:
12402 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12403 &insn_len, contents, isymbuf,
12404 symtab_hdr);
12405 break;
12406 case R_NDS32_LONGJUMP2:
12407 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12408 &insn_len, contents, isymbuf,
12409 symtab_hdr);
12410 break;
12411 case R_NDS32_LONGJUMP3:
12412 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12413 &insn_len, contents, isymbuf,
12414 symtab_hdr);
12415 break;
12416 case R_NDS32_LONGCALL4:
12417 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12418 &insn_len, contents, isymbuf,
12419 symtab_hdr);
12420 break;
12421 case R_NDS32_LONGCALL5:
12422 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12423 &insn_len, contents, isymbuf,
12424 symtab_hdr);
12425 break;
12426 case R_NDS32_LONGCALL6:
12427 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12428 &insn_len, contents, isymbuf,
12429 symtab_hdr);
12430 break;
12431 case R_NDS32_LONGJUMP4:
12432 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12433 &insn_len, contents, isymbuf,
12434 symtab_hdr);
12435 break;
12436 case R_NDS32_LONGJUMP5:
12437 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12438 &insn_len, &seq_len, contents,
12439 isymbuf, symtab_hdr);
12440 break;
12441 case R_NDS32_LONGJUMP6:
12442 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12443 &insn_len, &seq_len, contents,
12444 isymbuf, symtab_hdr);
12445 break;
12446 case R_NDS32_LONGJUMP7:
12447 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12448 &insn_len, &seq_len, contents,
12449 isymbuf, symtab_hdr);
12450 break;
12451 case R_NDS32_LOADSTORE:
12452 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12453 internal_relocs, &insn_len,
12454 contents, isymbuf, symtab_hdr,
12455 load_store_relax, table);
12456 break;
12457 case R_NDS32_LO12S0_RELA:
12458 case R_NDS32_LO12S1_RELA:
12459 case R_NDS32_LO12S2_RELA:
12460 case R_NDS32_LO12S2_DP_RELA:
12461 case R_NDS32_LO12S2_SP_RELA:
12462 /* Relax for low part. */
12463 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12464 contents, isymbuf, symtab_hdr, table);
12465
12466 /* It is impossible to delete blank, so just continue. */
12467 continue;
12468 case R_NDS32_PTR:
12469 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12470 &insn_len, &seq_len, contents);
12471 break;
12472 case R_NDS32_LSI:
12473 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12474 contents, isymbuf, symtab_hdr, again);
12475 continue;
12476 case R_NDS32_GOT_LO12:
12477 case R_NDS32_GOTOFF_LO12:
12478 case R_NDS32_PLTREL_LO12:
12479 case R_NDS32_PLT_GOTREL_LO12:
12480 case R_NDS32_GOTPC_LO12:
12481 case R_NDS32_TLS_LE_LO12:
12482 case R_NDS32_TLS_LE_ADD:
12483 case R_NDS32_TLS_LE_LS:
12484 case R_NDS32_PLT_GOT_SUFF:
12485 case R_NDS32_GOT_SUFF:
12486 case R_NDS32_GOTOFF_SUFF:
12487 continue;
12488 default:
12489 continue;
12490 }
12491
12492 if (removed && seq_len - insn_len > 0)
12493 {
12494 if (!insert_nds32_elf_blank
12495 (&relax_blank_list, irel->r_offset + insn_len,
12496 seq_len - insn_len))
12497 goto error_return;
12498 *again = TRUE;
12499 }
12500 }
12501
12502 calc_nds32_blank_total (relax_blank_list);
12503
12504 if (table->relax_fp_as_gp)
12505 {
12506 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12507 irelend, isymbuf))
12508 goto error_return;
12509
12510 if (!*again)
12511 {
12512 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12513 irelend))
12514 goto error_return;
12515 }
12516 }
12517
12518 if (!*again)
12519 {
12520 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12521 &relax_blank_list, optimize, opt_size))
12522 goto error_return;
12523 }
12524
12525 /* It doesn't matter optimize_for_space_no_align anymore.
12526 If object file is assembled with flag '-Os',
12527 the we don't adjust jump-destination on 4-byte boundary. */
12528
12529 if (relax_blank_list)
12530 {
12531 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12532 relax_blank_list = NULL;
12533 }
12534
12535 if (!*again)
12536 {
12537 /* Closing the section, so we don't relax it anymore. */
12538 bfd_vma sec_size_align;
12539 Elf_Internal_Rela *tmp_rel;
12540
12541 /* Pad to alignment boundary. Only handle current section alignment. */
12542 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12543 & ((-1U) << sec->alignment_power);
12544 if ((sec_size_align - sec->size) & 0x2)
12545 {
12546 insn16 = NDS32_NOP16;
12547 bfd_putb16 (insn16, contents + sec->size);
12548 sec->size += 2;
12549 }
12550
12551 while (sec_size_align != sec->size)
12552 {
12553 insn = NDS32_NOP32;
12554 bfd_putb32 (insn, contents + sec->size);
12555 sec->size += 4;
12556 }
12557
12558 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12559 irelend, R_NDS32_RELAX_ENTRY);
12560 if (tmp_rel != irelend)
12561 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12562
12563 clean_nds32_elf_blank ();
12564 }
12565
12566 finish:
12567 if (internal_relocs != NULL
12568 && elf_section_data (sec)->relocs != internal_relocs)
12569 free (internal_relocs);
12570
12571 if (contents != NULL
12572 && elf_section_data (sec)->this_hdr.contents != contents)
12573 free (contents);
12574
12575 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12576 free (isymbuf);
12577
12578 return result;
12579
12580 error_return:
12581 result = FALSE;
12582 goto finish;
12583 }
12584
12585 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12586 {
12587 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12588 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12589 {NULL, 0, 0, 0, 0}
12590 };
12591
12592 static bfd_boolean
12593 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12594 struct bfd_link_info *info,
12595 void *finfo ATTRIBUTE_UNUSED,
12596 bfd_boolean (*func) (void *, const char *,
12597 Elf_Internal_Sym *,
12598 asection *,
12599 struct elf_link_hash_entry *)
12600 ATTRIBUTE_UNUSED)
12601 {
12602 FILE *sym_ld_script = NULL;
12603 struct elf_nds32_link_hash_table *table;
12604
12605 table = nds32_elf_hash_table (info);
12606 sym_ld_script = table->sym_ld_script;
12607
12608 if (check_start_export_sym)
12609 fprintf (sym_ld_script, "}\n");
12610
12611 return TRUE;
12612 }
12613
12614 static enum elf_reloc_type_class
12615 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12616 const asection *rel_sec ATTRIBUTE_UNUSED,
12617 const Elf_Internal_Rela *rela)
12618 {
12619 switch ((int) ELF32_R_TYPE (rela->r_info))
12620 {
12621 case R_NDS32_RELATIVE:
12622 return reloc_class_relative;
12623 case R_NDS32_JMP_SLOT:
12624 return reloc_class_plt;
12625 case R_NDS32_COPY:
12626 return reloc_class_copy;
12627 default:
12628 return reloc_class_normal;
12629 }
12630 }
12631
12632 /* Put target dependent option into info hash table. */
12633 void
12634 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12635 int relax_fp_as_gp,
12636 int eliminate_gc_relocs,
12637 FILE * sym_ld_script,
12638 int hyper_relax,
12639 int tls_desc_trampoline,
12640 int load_store_relax)
12641 {
12642 struct elf_nds32_link_hash_table *table;
12643
12644 table = nds32_elf_hash_table (link_info);
12645 if (table == NULL)
12646 return;
12647
12648 table->relax_fp_as_gp = relax_fp_as_gp;
12649 table->eliminate_gc_relocs = eliminate_gc_relocs;
12650 table->sym_ld_script = sym_ld_script;
12651 table->hyper_relax = hyper_relax;
12652 table->tls_desc_trampoline = tls_desc_trampoline;
12653 table ->load_store_relax = load_store_relax;
12654 }
12655 \f
12656
12657 /* These functions and data-structures are used for fp-as-gp
12658 optimization. */
12659
12660 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12661 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12662 the read-only section and read-write section. */
12663 #define FAG_WINDOW (508 - 32)
12664
12665 /* An nds32_fag represent a gp-relative access.
12666 We find best fp-base by using a sliding window
12667 to find a base address which can cover most gp-access. */
12668 struct nds32_fag
12669 {
12670 struct nds32_fag *next; /* NULL-teminated linked list. */
12671 bfd_vma addr; /* The address of this fag. */
12672 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12673 It is used for applying FP7U2_FLAG. */
12674 int count; /* How many times this address is referred.
12675 There should be exactly `count' relocations
12676 in relas. */
12677 int relas_capcity; /* The buffer size of relas.
12678 We use an array instead of linked-list,
12679 and realloc is used to adjust buffer size. */
12680 };
12681
12682 static void
12683 nds32_fag_init (struct nds32_fag *head)
12684 {
12685 memset (head, 0, sizeof (struct nds32_fag));
12686 }
12687
12688 static void
12689 nds32_fag_verify (struct nds32_fag *head)
12690 {
12691 struct nds32_fag *iter;
12692 struct nds32_fag *prev;
12693
12694 prev = NULL;
12695 iter = head->next;
12696 while (iter)
12697 {
12698 if (prev && prev->addr >= iter->addr)
12699 puts ("Bug in fp-as-gp insertion.");
12700 prev = iter;
12701 iter = iter->next;
12702 }
12703 }
12704
12705 /* Insert a fag in ascending order.
12706 If a fag of the same address already exists,
12707 they are chained by relas array. */
12708
12709 static void
12710 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12711 Elf_Internal_Rela * rel)
12712 {
12713 struct nds32_fag *iter;
12714 struct nds32_fag *new_fag;
12715 const int INIT_RELAS_CAP = 4;
12716
12717 for (iter = head;
12718 iter->next && iter->next->addr <= addr;
12719 iter = iter->next)
12720 /* Find somewhere to insert. */ ;
12721
12722 /* `iter' will be equal to `head' if the list is empty. */
12723 if (iter != head && iter->addr == addr)
12724 {
12725 /* The address exists in the list.
12726 Insert `rel' into relocation list, relas. */
12727
12728 /* Check whether relas is big enough. */
12729 if (iter->count >= iter->relas_capcity)
12730 {
12731 iter->relas_capcity *= 2;
12732 iter->relas = bfd_realloc
12733 (iter->relas, iter->relas_capcity * sizeof (void *));
12734 }
12735 iter->relas[iter->count++] = rel;
12736 return;
12737 }
12738
12739 /* This is a new address. Create a fag node for it. */
12740 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12741 memset (new_fag, 0, sizeof (*new_fag));
12742 new_fag->addr = addr;
12743 new_fag->count = 1;
12744 new_fag->next = iter->next;
12745 new_fag->relas_capcity = INIT_RELAS_CAP;
12746 new_fag->relas = (Elf_Internal_Rela **)
12747 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12748 new_fag->relas[0] = rel;
12749 iter->next = new_fag;
12750
12751 nds32_fag_verify (head);
12752 }
12753
12754 static void
12755 nds32_fag_free_list (struct nds32_fag *head)
12756 {
12757 struct nds32_fag *iter;
12758
12759 iter = head->next;
12760 while (iter)
12761 {
12762 struct nds32_fag *tmp = iter;
12763 iter = iter->next;
12764 free (tmp->relas);
12765 tmp->relas = NULL;
12766 free (tmp);
12767 }
12768 }
12769
12770 /* Find the best fp-base address.
12771 The relocation associated with that address is returned,
12772 so we can track the symbol instead of a fixed address.
12773
12774 When relaxation, the address of an datum may change,
12775 because a text section is shrinked, so the data section
12776 moves forward. If the aligments of text and data section
12777 are different, their distance may change too.
12778 Therefore, tracking a fixed address is not appriate. */
12779
12780 static int
12781 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12782 {
12783 struct nds32_fag *base; /* First fag in the window. */
12784 struct nds32_fag *last; /* First fag outside the window. */
12785 int accu = 0; /* Usage accumulation. */
12786 struct nds32_fag *best; /* Best fag. */
12787 int baccu = 0; /* Best accumulation. */
12788
12789 /* Use first fag for initial, and find the last fag in the window.
12790
12791 In each iteration, we could simply subtract previous fag
12792 and accumulate following fags which are inside the window,
12793 untill we each the end. */
12794
12795 if (head->next == NULL)
12796 {
12797 *bestpp = NULL;
12798 return 0;
12799 }
12800
12801 /* Initialize base. */
12802 base = head->next;
12803 best = base;
12804 for (last = base;
12805 last && last->addr < base->addr + FAG_WINDOW;
12806 last = last->next)
12807 accu += last->count;
12808
12809 baccu = accu;
12810
12811 /* Record the best base in each iteration. */
12812 while (base->next)
12813 {
12814 accu -= base->count;
12815 base = base->next;
12816 /* Account fags in window. */
12817 for (/* Nothing. */;
12818 last && last->addr < base->addr + FAG_WINDOW;
12819 last = last->next)
12820 accu += last->count;
12821
12822 /* A better fp-base? */
12823 if (accu > baccu)
12824 {
12825 best = base;
12826 baccu = accu;
12827 }
12828 }
12829
12830 if (bestpp)
12831 *bestpp = best;
12832 return baccu;
12833 }
12834
12835 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12836 so we can convert it fo fp-relative access later.
12837 `best_fag' is the best fp-base. Only those inside the window
12838 of best_fag is applied the flag. */
12839
12840 static bfd_boolean
12841 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12842 asection *sec, struct nds32_fag *best_fag,
12843 Elf_Internal_Rela *internal_relocs,
12844 Elf_Internal_Rela *irelend)
12845 {
12846 struct nds32_fag *ifag;
12847 bfd_vma best_fpbase, gp;
12848 bfd *output_bfd;
12849
12850 output_bfd = sec->output_section->owner;
12851 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12852 best_fpbase = best_fag->addr;
12853
12854 if (best_fpbase > gp + sdata_range[1][1]
12855 || best_fpbase < gp - sdata_range[1][0])
12856 return FALSE;
12857
12858 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12859 so we know they can be converted to lwi37.fp. */
12860 for (ifag = best_fag;
12861 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12862 {
12863 int i;
12864
12865 for (i = 0; i < ifag->count; i++)
12866 {
12867 Elf_Internal_Rela *insn16_rel;
12868 Elf_Internal_Rela *fag_rel;
12869
12870 fag_rel = ifag->relas[i];
12871
12872 /* Only if this is within the WINDOWS, FP7U2_FLAG
12873 is applied. */
12874
12875 insn16_rel = find_relocs_at_address
12876 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12877
12878 if (insn16_rel != irelend)
12879 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12880 }
12881 }
12882 return TRUE;
12883 }
12884
12885 /* Reset INSN16 to clean fp as gp. */
12886
12887 static void
12888 nds32_fag_unmark_relax (struct nds32_fag *fag,
12889 Elf_Internal_Rela *internal_relocs,
12890 Elf_Internal_Rela *irelend)
12891 {
12892 struct nds32_fag *ifag;
12893 int i;
12894 Elf_Internal_Rela *insn16_rel;
12895 Elf_Internal_Rela *fag_rel;
12896
12897 for (ifag = fag; ifag; ifag = ifag->next)
12898 {
12899 for (i = 0; i < ifag->count; i++)
12900 {
12901 fag_rel = ifag->relas[i];
12902
12903 /* Restore the INSN16 relocation. */
12904 insn16_rel = find_relocs_at_address
12905 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12906
12907 if (insn16_rel != irelend)
12908 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12909 }
12910 }
12911 }
12912
12913 /* This is the main function of fp-as-gp optimization.
12914 It should be called by relax_section. */
12915
12916 static bfd_boolean
12917 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12918 bfd *abfd, asection *sec,
12919 Elf_Internal_Rela *internal_relocs,
12920 Elf_Internal_Rela *irelend,
12921 Elf_Internal_Sym *isymbuf)
12922 {
12923 Elf_Internal_Rela *begin_rel = NULL;
12924 Elf_Internal_Rela *irel;
12925 struct nds32_fag fag_head;
12926 Elf_Internal_Shdr *symtab_hdr;
12927 bfd_byte *contents;
12928 bfd_boolean ifc_inside = FALSE;
12929
12930 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12931
12932 /* Per-function fp-base selection.
12933 1. Create a list for all the gp-relative access.
12934 2. Base on those gp-relative address,
12935 find a fp-base which can cover most access.
12936 3. Use the fp-base for fp-as-gp relaxation.
12937
12938 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12939 we should
12940 1. delete the `la $fp, _FP_BASE_' instruction and
12941 2. not convert lwi.gp to lwi37.fp.
12942
12943 To delete the _FP_BASE_ instruction, we simply apply
12944 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12945
12946 To suppress the conversion, we simply NOT to apply
12947 R_NDS32_INSN16_FP7U2_FLAG flag. */
12948
12949 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12950
12951 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12952 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12953 return FALSE;
12954
12955 /* Check whether it is worth for fp-as-gp optimization,
12956 i.e., at least 3 gp-load.
12957
12958 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12959 apply this optimization. */
12960
12961 for (irel = internal_relocs; irel < irelend; irel++)
12962 {
12963 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12964 One we enter the begin of the region, we track all the LW/ST
12965 instructions, so when we leave the region, we try to find
12966 the best fp-base address for those LW/ST instructions. */
12967
12968 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12969 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12970 {
12971 /* Begin of the region. */
12972 if (begin_rel)
12973 /* xgettext:c-format */
12974 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12975
12976 begin_rel = irel;
12977 nds32_fag_init (&fag_head);
12978 ifc_inside = FALSE;
12979 }
12980 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12981 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12982 {
12983 int accu;
12984 struct nds32_fag *best_fag, *tmp_fag;
12985 int dist;
12986
12987 /* End of the region.
12988 Check whether it is worth to do fp-as-gp. */
12989
12990 if (begin_rel == NULL)
12991 {
12992 /* xgettext:c-format */
12993 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12994 abfd, sec);
12995 continue;
12996 }
12997
12998 accu = nds32_fag_find_base (&fag_head, &best_fag);
12999
13000 /* Clean FP7U2_FLAG because they may set ever. */
13001 tmp_fag = fag_head.next;
13002 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13003
13004 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
13005 if (accu < FAG_THRESHOLD
13006 || !nds32_fag_mark_relax (link_info, sec, best_fag,
13007 internal_relocs, irelend))
13008 {
13009 /* Not worth to do fp-as-gp. */
13010 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13011 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13012 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13013 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13014 nds32_fag_free_list (&fag_head);
13015 begin_rel = NULL;
13016 continue;
13017 }
13018
13019 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13020 so we use it to record the distance to the reloction of best
13021 fp-base. */
13022 dist = best_fag->relas[0] - begin_rel;
13023 BFD_ASSERT (dist > 0 && dist < 0xffffff);
13024 /* Use high 16 bits of addend to record the _FP_BASE_ matched
13025 relocation. And get the base value when relocating. */
13026 begin_rel->r_addend &= (0x1 << 16) - 1;
13027 begin_rel->r_addend |= dist << 16;
13028
13029 nds32_fag_free_list (&fag_head);
13030 begin_rel = NULL;
13031 }
13032
13033 if (begin_rel == NULL || ifc_inside)
13034 /* Skip if we are not in the region of fp-as-gp. */
13035 continue;
13036
13037 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13038 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13039 {
13040 bfd_vma addr;
13041 uint32_t insn;
13042
13043 /* A gp-relative access is found. Insert it to the fag-list. */
13044
13045 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
13046 insn = bfd_getb32 (contents + irel->r_offset);
13047 if (!N32_IS_RT3 (insn))
13048 continue;
13049
13050 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13051 nds32_fag_insert (&fag_head, addr, irel);
13052 }
13053 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13054 {
13055 begin_rel = NULL;
13056 }
13057 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13058 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13059 {
13060 /* Suppress fp as gp when encounter ifc. */
13061 ifc_inside = TRUE;
13062 }
13063 }
13064
13065 return TRUE;
13066 }
13067
13068 /* Remove unused `la $fp, _FD_BASE_' instruction. */
13069
13070 static bfd_boolean
13071 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13072 Elf_Internal_Rela *internal_relocs,
13073 Elf_Internal_Rela *irelend)
13074 {
13075 Elf_Internal_Rela *irel;
13076 Elf_Internal_Shdr *symtab_hdr;
13077 bfd_byte *contents = NULL;
13078 nds32_elf_blank_t *relax_blank_list = NULL;
13079 bfd_boolean result = TRUE;
13080 bfd_boolean unused_region = FALSE;
13081
13082 /*
13083 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13084 R_NDS32_17IFC_PCREL_RELA
13085 R_NDS32_10IFCU_PCREL_RELA. */
13086
13087 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13088 nds32_get_section_contents (abfd, sec, &contents, TRUE);
13089
13090 for (irel = internal_relocs; irel < irelend; irel++)
13091 {
13092 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13093 we marked to in previous pass.
13094 DO NOT scan relocations again, since we've alreadly decided it
13095 and set the flag. */
13096 const char *syname;
13097 int syndx;
13098 uint32_t insn;
13099
13100 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13101 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13102 unused_region = TRUE;
13103 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13104 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13105 unused_region = FALSE;
13106
13107 /* We're not in the region. */
13108 if (!unused_region)
13109 continue;
13110
13111 /* _FP_BASE_ must be a GLOBAL symbol. */
13112 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13113 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13114 continue;
13115
13116 /* The symbol name must be _FP_BASE_. */
13117 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13118 if (strcmp (syname, FP_BASE_NAME) != 0)
13119 continue;
13120
13121 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13122 {
13123 /* addi.gp $fp, -256 */
13124 insn = bfd_getb32 (contents + irel->r_offset);
13125 if (insn != INSN_ADDIGP_TO_FP)
13126 continue;
13127 }
13128 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13129 {
13130 /* addi $fp, $gp, -256 */
13131 insn = bfd_getb32 (contents + irel->r_offset);
13132 if (insn != INSN_ADDI_GP_TO_FP)
13133 continue;
13134 }
13135 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13136 {
13137 /* movi $fp, FP_BASE */
13138 insn = bfd_getb32 (contents + irel->r_offset);
13139 if (insn != INSN_MOVI_TO_FP)
13140 continue;
13141 }
13142 else
13143 continue;
13144
13145 /* We got here because a FP_BASE instruction is found. */
13146 if (!insert_nds32_elf_blank_recalc_total
13147 (&relax_blank_list, irel->r_offset, 4))
13148 goto error_return;
13149 }
13150
13151 finish:
13152 if (relax_blank_list)
13153 {
13154 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13155 relax_blank_list = NULL;
13156 }
13157 return result;
13158
13159 error_return:
13160 result = FALSE;
13161 goto finish;
13162 }
13163
13164 /* This is a version of bfd_generic_get_relocated_section_contents.
13165 We need this variety because relaxation will modify the dwarf
13166 infomation. When there is undefined symbol reference error mesage,
13167 linker need to dump line number where the symbol be used. However
13168 the address is be relaxed, it can not get the original dwarf contents.
13169 The variety only modify function call for reading in the section. */
13170
13171 static bfd_byte *
13172 nds32_elf_get_relocated_section_contents (bfd *abfd,
13173 struct bfd_link_info *link_info,
13174 struct bfd_link_order *link_order,
13175 bfd_byte *data,
13176 bfd_boolean relocatable,
13177 asymbol **symbols)
13178 {
13179 bfd *input_bfd = link_order->u.indirect.section->owner;
13180 asection *input_section = link_order->u.indirect.section;
13181 long reloc_size;
13182 arelent **reloc_vector;
13183 long reloc_count;
13184
13185 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13186 if (reloc_size < 0)
13187 return NULL;
13188
13189 /* Read in the section. */
13190 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
13191 return NULL;
13192
13193 if (reloc_size == 0)
13194 return data;
13195
13196 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13197 if (reloc_vector == NULL)
13198 return NULL;
13199
13200 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13201 reloc_vector, symbols);
13202 if (reloc_count < 0)
13203 goto error_return;
13204
13205 if (reloc_count > 0)
13206 {
13207 arelent **parent;
13208 for (parent = reloc_vector; *parent != NULL; parent++)
13209 {
13210 char *error_message = NULL;
13211 asymbol *symbol;
13212 bfd_reloc_status_type r;
13213
13214 symbol = *(*parent)->sym_ptr_ptr;
13215 if (symbol->section && discarded_section (symbol->section))
13216 {
13217 bfd_vma off;
13218 static reloc_howto_type none_howto
13219 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13220 "unused", FALSE, 0, 0, FALSE);
13221
13222 off = (*parent)->address * bfd_octets_per_byte (input_bfd);
13223 _bfd_clear_contents ((*parent)->howto, input_bfd,
13224 input_section, data, off);
13225 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13226 (*parent)->addend = 0;
13227 (*parent)->howto = &none_howto;
13228 r = bfd_reloc_ok;
13229 }
13230 else
13231 r = bfd_perform_relocation (input_bfd, *parent, data,
13232 input_section,
13233 relocatable ? abfd : NULL,
13234 &error_message);
13235
13236 if (relocatable)
13237 {
13238 asection *os = input_section->output_section;
13239
13240 /* A partial link, so keep the relocs. */
13241 os->orelocation[os->reloc_count] = *parent;
13242 os->reloc_count++;
13243 }
13244
13245 if (r != bfd_reloc_ok)
13246 {
13247 switch (r)
13248 {
13249 case bfd_reloc_undefined:
13250 (*link_info->callbacks->undefined_symbol)
13251 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13252 input_bfd, input_section, (*parent)->address, TRUE);
13253 break;
13254 case bfd_reloc_dangerous:
13255 BFD_ASSERT (error_message != NULL);
13256 (*link_info->callbacks->reloc_dangerous)
13257 (link_info, error_message,
13258 input_bfd, input_section, (*parent)->address);
13259 break;
13260 case bfd_reloc_overflow:
13261 (*link_info->callbacks->reloc_overflow)
13262 (link_info, NULL,
13263 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13264 (*parent)->howto->name, (*parent)->addend,
13265 input_bfd, input_section, (*parent)->address);
13266 break;
13267 case bfd_reloc_outofrange:
13268 /* PR ld/13730:
13269 This error can result when processing some partially
13270 complete binaries. Do not abort, but issue an error
13271 message instead. */
13272 link_info->callbacks->einfo
13273 /* xgettext:c-format */
13274 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13275 abfd, input_section, * parent);
13276 goto error_return;
13277
13278 default:
13279 abort ();
13280 break;
13281 }
13282 }
13283 }
13284 }
13285
13286 free (reloc_vector);
13287 return data;
13288
13289 error_return:
13290 free (reloc_vector);
13291 return NULL;
13292 }
13293
13294 /* Check target symbol. */
13295
13296 static bfd_boolean
13297 nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13298 {
13299 if (!sym || !sym->name || sym->name[0] != '$')
13300 return FALSE;
13301 return TRUE;
13302 }
13303
13304 /* nds32 find maybe function sym. Ignore target special symbol
13305 first, and then go the general function. */
13306
13307 static bfd_size_type
13308 nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13309 bfd_vma *code_off)
13310 {
13311 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13312 return 0;
13313
13314 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13315 }
13316 \f
13317
13318 /* Do TLS model conversion. */
13319
13320 typedef struct relax_group_list_t
13321 {
13322 Elf_Internal_Rela *relo;
13323 struct relax_group_list_t *next;
13324 struct relax_group_list_t *next_sibling;
13325 int id;
13326 } relax_group_list_t;
13327
13328 int
13329 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13330
13331 int
13332 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13333
13334 void
13335 dump_chain (relax_group_list_t *pHead);
13336
13337 int
13338 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13339 {
13340 relax_group_list_t *pNext = pHead;
13341
13342 /* Find place. */
13343 while (pNext->next)
13344 {
13345 if (pNext->next->id > (int) pElem->r_addend)
13346 break;
13347
13348 pNext = pNext->next;
13349 }
13350
13351 /* Insert node. */
13352 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13353 if (!pNew)
13354 return FALSE;
13355
13356 relax_group_list_t *tmp = pNext->next;
13357 pNext->next = pNew;
13358
13359 pNew->id = pElem->r_addend;
13360 pNew->relo = pElem;
13361 pNew->next = tmp;
13362 pNew->next_sibling = NULL;
13363
13364 return TRUE;
13365 }
13366
13367 int
13368 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13369 {
13370 relax_group_list_t *pNext = pNode;
13371
13372 /* Find place. */
13373 while (pNext->next_sibling)
13374 {
13375 pNext = pNext->next_sibling;
13376 }
13377
13378 /* Insert node. */
13379 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13380 if (!pNew)
13381 return FALSE;
13382
13383 relax_group_list_t *tmp = pNext->next_sibling;
13384 pNext->next_sibling = pNew;
13385
13386 pNew->id = -1;
13387 pNew->relo = pElem;
13388 pNew->next = NULL;
13389 pNew->next_sibling = tmp;
13390
13391 return TRUE;
13392 }
13393
13394 void
13395 dump_chain (relax_group_list_t *pHead)
13396 {
13397 relax_group_list_t *pNext = pHead->next;
13398 while (pNext)
13399 {
13400 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13401 relax_group_list_t *pNextSib = pNext->next_sibling;
13402 while (pNextSib)
13403 {
13404 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13405 pNextSib = pNextSib->next_sibling;
13406 }
13407 pNext = pNext->next;
13408 printf("\n");
13409 }
13410 }
13411
13412 /* Check R_NDS32_RELAX_GROUP of each section.
13413 There might be multiple sections in one object file. */
13414
13415 int
13416 elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13417 {
13418 elf32_nds32_relax_group_t *relax_group_ptr =
13419 elf32_nds32_relax_group_ptr (abfd);
13420
13421 int min_id = relax_group_ptr->min_id;
13422 int max_id = relax_group_ptr->max_id;
13423
13424 Elf_Internal_Rela *rel;
13425 Elf_Internal_Rela *relend;
13426 Elf_Internal_Rela *relocs;
13427 enum elf_nds32_reloc_type rtype;
13428
13429 do
13430 {
13431 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13432 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13433 TRUE /* keep_memory */);
13434 if (relocs == NULL)
13435 break;
13436
13437 /* Check R_NDS32_RELAX_GROUP. */
13438 relend = relocs + asec->reloc_count;
13439 for (rel = relocs; rel < relend; rel++)
13440 {
13441 int id;
13442 rtype = ELF32_R_TYPE (rel->r_info);
13443 if (rtype != R_NDS32_RELAX_GROUP)
13444 continue;
13445
13446 id = rel->r_addend;
13447 if (id < min_id)
13448 min_id = id;
13449 else if (id > max_id)
13450 max_id = id;
13451 }
13452 }
13453 while (FALSE);
13454
13455 if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs))
13456 free (relocs);
13457
13458 if ((min_id != relax_group_ptr->min_id)
13459 || (max_id != relax_group_ptr->max_id))
13460 {
13461 relax_group_ptr->count = max_id - min_id + 1;
13462 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13463 relax_group_ptr->min_id = min_id;
13464 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13465 relax_group_ptr->max_id = max_id;
13466 }
13467
13468 return relax_group_ptr->count;
13469 }
13470
13471 /* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13472 struct section_id_list_t *relax_group_section_id_list = NULL;
13473
13474 struct section_id_list_t *
13475 elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13476 {
13477 struct section_id_list_t *result = NULL;
13478 struct section_id_list_t *lst = *lst_ptr;
13479
13480 if (NULL == lst)
13481 {
13482 result = (struct section_id_list_t *) calloc
13483 (1, sizeof (struct section_id_list_t));
13484 BFD_ASSERT (result); /* Feed me. */
13485 result->id = id;
13486 *lst_ptr = result;
13487 }
13488 else
13489 {
13490 struct section_id_list_t *cur = lst;
13491 struct section_id_list_t *prv = NULL;
13492 struct section_id_list_t *sec = NULL;
13493
13494 while (cur)
13495 {
13496 if (cur->id < id)
13497 {
13498 prv = cur;
13499 cur = cur->next;
13500 continue;
13501 }
13502
13503 if (cur->id > id)
13504 {
13505 cur = NULL; /* To insert after prv. */
13506 sec = cur; /* In case prv == NULL. */
13507 }
13508
13509 break;
13510 }
13511
13512 if (NULL == cur)
13513 {
13514 /* Insert after prv. */
13515 result = (struct section_id_list_t *) calloc
13516 (1, sizeof (struct section_id_list_t));
13517 BFD_ASSERT (result); /* Feed me. */
13518 result->id = id;
13519 if (NULL != prv)
13520 {
13521 result->next = prv->next;
13522 prv->next = result;
13523 }
13524 else
13525 {
13526 *lst_ptr = result;
13527 result->next = sec;
13528 }
13529 }
13530 }
13531
13532 return result;
13533 }
13534
13535 int
13536 elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13537 {
13538 static int next_relax_group_bias = 0;
13539
13540 elf32_nds32_relax_group_t *relax_group_ptr =
13541 elf32_nds32_relax_group_ptr (abfd);
13542
13543 bfd_boolean result = TRUE;
13544 Elf_Internal_Rela *rel;
13545 Elf_Internal_Rela *relend;
13546 Elf_Internal_Rela *relocs = NULL;
13547 enum elf_nds32_reloc_type rtype;
13548 struct section_id_list_t *node = NULL;
13549 int count = 0;
13550
13551 do
13552 {
13553 if (0 == relax_group_ptr->count)
13554 break;
13555
13556 /* Check if this section has been handled. */
13557 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13558 if (NULL == node)
13559 break; /* Hit, the section id has handled. */
13560
13561 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13562 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13563 TRUE /* keep_memory */);
13564 if (relocs == NULL)
13565 {
13566 BFD_ASSERT (0); /* feed me */
13567 break;
13568 }
13569
13570 /* Allocate group id bias for this bfd! */
13571 if (0 == relax_group_ptr->init)
13572 {
13573 relax_group_ptr->bias = next_relax_group_bias;
13574 next_relax_group_bias += relax_group_ptr->count;
13575 relax_group_ptr->init = 1;
13576 }
13577
13578 /* Reorder relax group groups. */
13579 relend = relocs + asec->reloc_count;
13580 for (rel = relocs; rel < relend; rel++)
13581 {
13582 rtype = ELF32_R_TYPE(rel->r_info);
13583 if (rtype != R_NDS32_RELAX_GROUP)
13584 continue;
13585
13586 /* Change it. */
13587 rel->r_addend += relax_group_ptr->bias;
13588 /* Debugging count. */
13589 count++;
13590 }
13591 }
13592 while (FALSE);
13593
13594 if (relocs != NULL && elf_section_data (asec)->relocs != relocs)
13595 free (relocs);
13596
13597 return result;
13598 }
13599
13600 int
13601 nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13602 struct bfd_link_info *lnkinfo)
13603 {
13604 bfd_boolean result = TRUE;
13605 Elf_Internal_Rela *irel;
13606 Elf_Internal_Rela *irelend;
13607 Elf_Internal_Rela *internal_relocs;
13608 unsigned long r_symndx;
13609 enum elf_nds32_reloc_type r_type;
13610
13611 Elf_Internal_Sym *local_syms = NULL;
13612 bfd_byte *contents = NULL;
13613
13614 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13615
13616 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13617 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13618 sym_hashes = elf_sym_hashes (inbfd);
13619 sym_hashes_end =
13620 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13621 if (!elf_bad_symtab (inbfd))
13622 sym_hashes_end -= symtab_hdr->sh_info;
13623
13624 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13625 if (bfd_link_relocatable (lnkinfo))
13626 {
13627 elf32_nds32_unify_relax_group (inbfd, insec);
13628 return result;
13629 }
13630
13631 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13632 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13633 TRUE /* keep_memory */);
13634 if (internal_relocs == NULL)
13635 goto error_return;
13636
13637 irelend = internal_relocs + insec->reloc_count;
13638 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13639 irelend, R_NDS32_RELAX_ENTRY);
13640 if (irel == irelend)
13641 goto finish;
13642
13643 /* Chain/remove groups. */
13644 for (irel = internal_relocs; irel < irelend; irel++)
13645 {
13646 r_symndx = ELF32_R_SYM (irel->r_info);
13647 r_type = ELF32_R_TYPE (irel->r_info);
13648 if (r_type != R_NDS32_RELAX_GROUP)
13649 continue;
13650
13651 /* Remove it. */
13652 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13653 /* Chain it now. */
13654 if (!list_insert (&chain, irel))
13655 goto error_return;
13656 }
13657
13658 /* Collect group relocations. */
13659 /* Presume relocations are sorted. */
13660 relax_group_list_t *pNext = chain.next;
13661 while (pNext)
13662 {
13663 for (irel = internal_relocs; irel < irelend; irel++)
13664 {
13665 if (irel->r_offset == pNext->relo->r_offset)
13666 {
13667 /* Ignore Non-TLS relocation types. */
13668 r_type = ELF32_R_TYPE (irel->r_info);
13669 if ((R_NDS32_TLS_LE_HI20 > r_type)
13670 || (R_NDS32_RELAX_ENTRY == r_type))
13671 continue;
13672
13673 if (!list_insert_sibling (pNext, irel))
13674 goto error_return;
13675 }
13676 else if (irel->r_offset > pNext->relo->r_offset)
13677 {
13678 pNext = pNext->next;
13679 if (!pNext)
13680 break;
13681
13682 bfd_vma current_offset = pNext->relo->r_offset;
13683 if (irel->r_offset > current_offset)
13684 irel = internal_relocs; /* restart from head */
13685 else
13686 --irel; /* Check current irel again. */
13687 continue;
13688 }
13689 else
13690 {
13691 /* This shouldn't be reached. */
13692 }
13693 }
13694 if (pNext)
13695 pNext = pNext->next;
13696 }
13697
13698 #ifdef DUBUG_VERBOSE
13699 dump_chain(&chain);
13700 #endif
13701
13702 /* Get symbol table and section content. */
13703 if (incontents)
13704 contents = incontents;
13705 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13706 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13707 goto error_return;
13708
13709 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13710
13711 /* Convert TLS model each group if necessary. */
13712 pNext = chain.next;
13713
13714 int cur_grp_id = -1;
13715 int sethi_rt = -1;
13716 int add_rt = -1;
13717 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13718
13719 tls_type = org_tls_type = eff_tls_type = 0;
13720
13721 while (pNext)
13722 {
13723 relax_group_list_t *pNextSig = pNext->next_sibling;
13724 while (pNextSig)
13725 {
13726 struct elf_link_hash_entry *h = NULL;
13727
13728 irel = pNextSig->relo;
13729 r_symndx = ELF32_R_SYM(irel->r_info);
13730 r_type = ELF32_R_TYPE(irel->r_info);
13731
13732 if (pNext->id != cur_grp_id)
13733 {
13734 cur_grp_id = pNext->id;
13735 org_tls_type = get_tls_type (r_type, NULL);
13736 if (r_symndx >= symtab_hdr->sh_info)
13737 {
13738 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13739 while (h->root.type == bfd_link_hash_indirect
13740 || h->root.type == bfd_link_hash_warning)
13741 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13742 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13743 }
13744 else
13745 {
13746 tls_type = local_got_tls_type
13747 ? local_got_tls_type[r_symndx]
13748 : GOT_NORMAL;
13749 }
13750
13751 eff_tls_type = 1 << (fls (tls_type) - 1);
13752 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13753 }
13754
13755 if (eff_tls_type != org_tls_type)
13756 {
13757 switch (org_tls_type)
13758 {
13759 /* DESC to IEGP/IE/LE. */
13760 case GOT_TLS_DESC:
13761 switch (eff_tls_type)
13762 {
13763 case GOT_TLS_IE:
13764 switch (r_type)
13765 {
13766 case R_NDS32_TLS_DESC_HI20:
13767 irel->r_info = ELF32_R_INFO(r_symndx,
13768 R_NDS32_TLS_IE_HI20);
13769 break;
13770 case R_NDS32_TLS_DESC_LO12:
13771 irel->r_info = ELF32_R_INFO(r_symndx,
13772 R_NDS32_TLS_IE_LO12);
13773 break;
13774 case R_NDS32_TLS_DESC_ADD:
13775 {
13776 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13777 add_rt = N32_RT5 (insn);
13778 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13779 bfd_putb32 (insn, contents + irel->r_offset);
13780
13781 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13782 }
13783 break;
13784 case R_NDS32_TLS_DESC_FUNC:
13785 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13786 irel->r_info = ELF32_R_INFO(r_symndx,
13787 R_NDS32_RELAX_REMOVE);
13788 break;
13789 case R_NDS32_TLS_DESC_CALL:
13790 {
13791 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13792 REG_TP);
13793 bfd_putb32 (insn, contents + irel->r_offset);
13794
13795 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13796 }
13797 break;
13798 case R_NDS32_LOADSTORE:
13799 case R_NDS32_PTR:
13800 case R_NDS32_PTR_RESOLVED:
13801 case R_NDS32_NONE:
13802 case R_NDS32_LABEL:
13803 break;
13804 default:
13805 BFD_ASSERT(0);
13806 break;
13807 }
13808 break;
13809 case GOT_TLS_IEGP:
13810 switch (r_type)
13811 {
13812 case R_NDS32_TLS_DESC_HI20:
13813 irel->r_info = ELF32_R_INFO(r_symndx,
13814 R_NDS32_TLS_IEGP_HI20);
13815 break;
13816 case R_NDS32_TLS_DESC_LO12:
13817 irel->r_info = ELF32_R_INFO(r_symndx,
13818 R_NDS32_TLS_IEGP_LO12);
13819 break;
13820 case R_NDS32_TLS_DESC_ADD:
13821 {
13822 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13823 add_rt = N32_RT5 (insn);
13824 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13825 bfd_putb32 (insn, contents + irel->r_offset);
13826
13827 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13828 }
13829 break;
13830 case R_NDS32_TLS_DESC_FUNC:
13831 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13832 irel->r_info = ELF32_R_INFO(r_symndx,
13833 R_NDS32_RELAX_REMOVE);
13834 break;
13835 case R_NDS32_TLS_DESC_CALL:
13836 {
13837 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13838 REG_TP);
13839 bfd_putb32 (insn, contents + irel->r_offset);
13840
13841 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13842 }
13843 break;
13844 case R_NDS32_LOADSTORE:
13845 case R_NDS32_PTR:
13846 case R_NDS32_PTR_RESOLVED:
13847 case R_NDS32_NONE:
13848 case R_NDS32_LABEL:
13849 break;
13850 default:
13851 BFD_ASSERT(0);
13852 break;
13853 }
13854 break;
13855 case GOT_TLS_LE:
13856 switch (r_type)
13857 {
13858 case R_NDS32_TLS_DESC_HI20:
13859 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13860 break;
13861 case R_NDS32_TLS_DESC_LO12:
13862 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13863 break;
13864 case R_NDS32_TLS_DESC_ADD:
13865 {
13866 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13867
13868 add_rt = N32_RT5 (insn);
13869 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13870 bfd_putb32 (insn, contents + irel->r_offset);
13871
13872 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13873 }
13874 break;
13875 case R_NDS32_TLS_DESC_FUNC:
13876 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13877 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13878 break;
13879 case R_NDS32_TLS_DESC_CALL:
13880 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13881 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13882 break;
13883 case R_NDS32_LOADSTORE:
13884 case R_NDS32_PTR:
13885 case R_NDS32_PTR_RESOLVED:
13886 case R_NDS32_NONE:
13887 case R_NDS32_LABEL:
13888 break;
13889 default:
13890 BFD_ASSERT(0);
13891 break;
13892 }
13893 break;
13894 default:
13895 break;
13896 }
13897 break;
13898 /* IEGP to IE/LE. */
13899 case GOT_TLS_IEGP:
13900 switch (eff_tls_type)
13901 {
13902 case GOT_TLS_IE:
13903 switch (r_type)
13904 {
13905 case R_NDS32_TLS_IEGP_HI20:
13906 irel->r_info = ELF32_R_INFO(r_symndx,
13907 R_NDS32_TLS_IE_HI20);
13908 break;
13909 case R_NDS32_TLS_IEGP_LO12:
13910 irel->r_info = ELF32_R_INFO(r_symndx,
13911 R_NDS32_TLS_IE_LO12);
13912 break;
13913 case R_NDS32_PTR_RESOLVED:
13914 {
13915 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13916
13917 add_rt = N32_RT5 (insn);
13918 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13919 bfd_putb32 (insn, contents + irel->r_offset);
13920 }
13921 break;
13922 case R_NDS32_TLS_IEGP_LW:
13923 break;
13924 case R_NDS32_LOADSTORE:
13925 case R_NDS32_PTR:
13926 case R_NDS32_NONE:
13927 case R_NDS32_LABEL:
13928 break;
13929 default:
13930 BFD_ASSERT(0);
13931 break;
13932 }
13933 break;
13934 case GOT_TLS_LE:
13935 switch (r_type)
13936 {
13937 case R_NDS32_TLS_IEGP_HI20:
13938 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13939 break;
13940 case R_NDS32_TLS_IEGP_LO12:
13941 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13942 break;
13943 case R_NDS32_TLS_IEGP_LW:
13944 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13945 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13946 break;
13947 case R_NDS32_LOADSTORE:
13948 case R_NDS32_PTR:
13949 case R_NDS32_NONE:
13950 case R_NDS32_LABEL:
13951 case R_NDS32_PTR_RESOLVED:
13952 break;
13953 default:
13954 BFD_ASSERT(0);
13955 break;
13956 }
13957 break;
13958 default:
13959 break;
13960 }
13961 break;
13962 /* IE to LE. */
13963 case GOT_TLS_IE:
13964 switch (eff_tls_type)
13965 {
13966 case GOT_TLS_LE:
13967 switch (r_type)
13968 {
13969 case R_NDS32_TLS_IE_HI20:
13970 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13971 break;
13972 case R_NDS32_TLS_IE_LO12S2:
13973 {
13974 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13975
13976 add_rt = N32_RT5 (insn);
13977 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13978 bfd_putb32 (insn, contents + irel->r_offset);
13979
13980 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13981 }
13982 break;
13983 case R_NDS32_LOADSTORE:
13984 case R_NDS32_PTR:
13985 case R_NDS32_NONE:
13986 case R_NDS32_LABEL:
13987 break;
13988 default:
13989 BFD_ASSERT(0);
13990 break;
13991 }
13992 break;
13993 default:
13994 break;
13995 }
13996 break;
13997 default:
13998 break;
13999 }
14000 }
14001 pNextSig = pNextSig->next_sibling;
14002 }
14003
14004 #if 1
14005 pNext = pNext->next;
14006 #else
14007 while (pNext)
14008 {
14009 if (pNext->id != cur_grp_id)
14010 break;
14011 pNext = pNext->next;
14012 }
14013 #endif
14014 }
14015
14016 finish:
14017 if (incontents)
14018 contents = NULL;
14019
14020 if (internal_relocs != NULL
14021 && elf_section_data (insec)->relocs != internal_relocs)
14022 free (internal_relocs);
14023
14024 if (contents != NULL
14025 && elf_section_data (insec)->this_hdr.contents != contents)
14026 free (contents);
14027
14028 if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms)
14029 free (local_syms);
14030
14031 if (chain.next)
14032 {
14033 pNext = chain.next;
14034 relax_group_list_t *pDel;
14035 while (pNext)
14036 {
14037 pDel = pNext;
14038 pNext = pNext->next;
14039 free (pDel);
14040 }
14041 }
14042
14043 return result;
14044
14045 error_return:
14046 result = FALSE;
14047 goto finish;
14048 }
14049
14050 /* End TLS model conversion. */
14051
14052 #define ELF_ARCH bfd_arch_nds32
14053 #define ELF_MACHINE_CODE EM_NDS32
14054 #define ELF_MAXPAGESIZE 0x1000
14055 #define ELF_TARGET_ID NDS32_ELF_DATA
14056
14057 #define TARGET_BIG_SYM nds32_elf32_be_vec
14058 #define TARGET_BIG_NAME "elf32-nds32be"
14059 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
14060 #define TARGET_LITTLE_NAME "elf32-nds32le"
14061
14062 #define elf_info_to_howto nds32_info_to_howto
14063 #define elf_info_to_howto_rel nds32_info_to_howto_rel
14064
14065 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14066 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14067 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14068 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14069 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14070
14071 #define bfd_elf32_mkobject nds32_elf_mkobject
14072 #define elf_backend_action_discarded nds32_elf_action_discarded
14073 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14074 #define elf_backend_check_relocs nds32_elf_check_relocs
14075 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14076 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14077 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14078 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14079 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14080 #define elf_backend_relocate_section nds32_elf_relocate_section
14081 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
14082 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14083 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14084 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14085 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14086 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14087 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14088 #define elf_backend_object_p nds32_elf_object_p
14089 #define elf_backend_final_write_processing nds32_elf_final_write_processing
14090 #define elf_backend_special_sections nds32_elf_special_sections
14091 #define bfd_elf32_bfd_get_relocated_section_contents \
14092 nds32_elf_get_relocated_section_contents
14093 #define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14094 #define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
14095
14096 #define elf_backend_can_gc_sections 1
14097 #define elf_backend_can_refcount 1
14098 #define elf_backend_want_got_plt 1
14099 #define elf_backend_plt_readonly 1
14100 #define elf_backend_want_plt_sym 0
14101 #define elf_backend_got_header_size 12
14102 #define elf_backend_may_use_rel_p 1
14103 #define elf_backend_default_use_rela_p 1
14104 #define elf_backend_may_use_rela_p 1
14105 #define elf_backend_dtrel_excludes_plt 0
14106
14107 #include "elf32-target.h"
14108
14109 #undef ELF_MAXPAGESIZE
14110 #define ELF_MAXPAGESIZE 0x2000
14111
14112 #undef TARGET_BIG_SYM
14113 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
14114 #undef TARGET_BIG_NAME
14115 #define TARGET_BIG_NAME "elf32-nds32be-linux"
14116 #undef TARGET_LITTLE_SYM
14117 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
14118 #undef TARGET_LITTLE_NAME
14119 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
14120 #undef elf32_bed
14121 #define elf32_bed elf32_nds32_lin_bed
14122
14123 #include "elf32-target.h"
This page took 0.344195 seconds and 4 git commands to generate.