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