ELF: Copy dyn_relocs in _bfd_elf_link_hash_copy_indirect
[deliverable/binutils-gdb.git] / bfd / elf32-nds32.c
1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2020 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 **, bfd_boolean);
66 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67 Elf_Internal_Sym **);
68 static bfd_boolean 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 bfd_boolean 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 bfd_boolean
257 nds32_elf_mkobject (bfd *abfd)
258 {
259 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
260 NDS32_ELF_DATA);
261 }
262
263 /* Relocations used for relocation. */
264 /* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
265 initialize array nds32_elf_howto_table in any order. The benefit
266 is that we can add any new relocations with any numbers and don't
267 need to fill the gap by lots of EMPTY_HOWTO. */
268 #define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
269 [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
270
271 static reloc_howto_type nds32_elf_howto_table[] =
272 {
273 /* This reloc does nothing. */
274 HOWTO2 (R_NDS32_NONE, /* type */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 32, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_bitfield,/* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_NDS32_NONE", /* name */
283 FALSE, /* partial_inplace */
284 0, /* src_mask */
285 0, /* dst_mask */
286 FALSE), /* pcrel_offset */
287
288 /* A 16 bit absolute relocation. */
289 HOWTO2 (R_NDS32_16, /* type */
290 0, /* rightshift */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
292 16, /* bitsize */
293 FALSE, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_bitfield,/* complain_on_overflow */
296 nds32_elf_generic_reloc,/* special_function */
297 "R_NDS32_16", /* name */
298 FALSE, /* partial_inplace */
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
301 FALSE), /* pcrel_offset */
302
303 /* A 32 bit absolute relocation. */
304 HOWTO2 (R_NDS32_32, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 32, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield,/* complain_on_overflow */
311 nds32_elf_generic_reloc,/* special_function */
312 "R_NDS32_32", /* name */
313 FALSE, /* partial_inplace */
314 0xffffffff, /* src_mask */
315 0xffffffff, /* dst_mask */
316 FALSE), /* pcrel_offset */
317
318 /* A 20 bit address. */
319 HOWTO2 (R_NDS32_20, /* type */
320 0, /* rightshift */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
322 20, /* bitsize */
323 FALSE, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_unsigned,/* complain_on_overflow */
326 nds32_elf_generic_reloc,/* special_function */
327 "R_NDS32_20", /* name */
328 FALSE, /* partial_inplace */
329 0xfffff, /* src_mask */
330 0xfffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
332
333 /* An PC Relative 9-bit relocation, shifted by 2.
334 This reloc is complicated because relocations are relative to pc & -4.
335 i.e. branches in the right insn slot use the address of the left insn
336 slot for pc. */
337 /* It's not clear whether this should have partial_inplace set or not.
338 Branch relaxing in the assembler can store the addend in the insn,
339 and if bfd_install_relocation gets called the addend may get added
340 again. */
341 HOWTO2 (R_NDS32_9_PCREL, /* type */
342 1, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 8, /* bitsize */
345 TRUE, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_signed,/* complain_on_overflow */
348 nds32_elf_9_pcrel_reloc,/* special_function */
349 "R_NDS32_9_PCREL", /* name */
350 FALSE, /* partial_inplace */
351 0xff, /* src_mask */
352 0xff, /* dst_mask */
353 TRUE), /* pcrel_offset */
354
355 /* A relative 15 bit relocation, right shifted by 1. */
356 HOWTO2 (R_NDS32_15_PCREL, /* type */
357 1, /* rightshift */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
359 14, /* bitsize */
360 TRUE, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_signed,/* complain_on_overflow */
363 bfd_elf_generic_reloc, /* special_function */
364 "R_NDS32_15_PCREL", /* name */
365 FALSE, /* partial_inplace */
366 0x3fff, /* src_mask */
367 0x3fff, /* dst_mask */
368 TRUE), /* pcrel_offset */
369
370 /* A relative 17 bit relocation, right shifted by 1. */
371 HOWTO2 (R_NDS32_17_PCREL, /* type */
372 1, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 16, /* bitsize */
375 TRUE, /* pc_relative */
376 0, /* bitpos */
377 complain_overflow_signed,/* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 "R_NDS32_17_PCREL", /* name */
380 FALSE, /* partial_inplace */
381 0xffff, /* src_mask */
382 0xffff, /* dst_mask */
383 TRUE), /* pcrel_offset */
384
385 /* A relative 25 bit relocation, right shifted by 1. */
386 /* It's not clear whether this should have partial_inplace set or not.
387 Branch relaxing in the assembler can store the addend in the insn,
388 and if bfd_install_relocation gets called the addend may get added
389 again. */
390 HOWTO2 (R_NDS32_25_PCREL, /* type */
391 1, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 24, /* bitsize */
394 TRUE, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_signed,/* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 "R_NDS32_25_PCREL", /* name */
399 FALSE, /* partial_inplace */
400 0xffffff, /* src_mask */
401 0xffffff, /* dst_mask */
402 TRUE), /* pcrel_offset */
403
404 /* High 20 bits of address when lower 12 is or'd in. */
405 HOWTO2 (R_NDS32_HI20, /* type */
406 12, /* rightshift */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
408 20, /* bitsize */
409 FALSE, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_dont,/* complain_on_overflow */
412 nds32_elf_hi20_reloc, /* special_function */
413 "R_NDS32_HI20", /* name */
414 FALSE, /* partial_inplace */
415 0x000fffff, /* src_mask */
416 0x000fffff, /* dst_mask */
417 FALSE), /* pcrel_offset */
418
419 /* Lower 12 bits of address. */
420 HOWTO2 (R_NDS32_LO12S3, /* type */
421 3, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 9, /* bitsize */
424 FALSE, /* pc_relative */
425 0, /* bitpos */
426 complain_overflow_dont,/* complain_on_overflow */
427 nds32_elf_lo12_reloc, /* special_function */
428 "R_NDS32_LO12S3", /* name */
429 FALSE, /* partial_inplace */
430 0x000001ff, /* src_mask */
431 0x000001ff, /* dst_mask */
432 FALSE), /* pcrel_offset */
433
434 /* Lower 12 bits of address. */
435 HOWTO2 (R_NDS32_LO12S2, /* type */
436 2, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 10, /* bitsize */
439 FALSE, /* pc_relative */
440 0, /* bitpos */
441 complain_overflow_dont,/* complain_on_overflow */
442 nds32_elf_lo12_reloc, /* special_function */
443 "R_NDS32_LO12S2", /* name */
444 FALSE, /* partial_inplace */
445 0x000003ff, /* src_mask */
446 0x000003ff, /* dst_mask */
447 FALSE), /* pcrel_offset */
448
449 /* Lower 12 bits of address. */
450 HOWTO2 (R_NDS32_LO12S1, /* type */
451 1, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 11, /* bitsize */
454 FALSE, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_dont,/* complain_on_overflow */
457 nds32_elf_lo12_reloc, /* special_function */
458 "R_NDS32_LO12S1", /* name */
459 FALSE, /* partial_inplace */
460 0x000007ff, /* src_mask */
461 0x000007ff, /* dst_mask */
462 FALSE), /* pcrel_offset */
463
464 /* Lower 12 bits of address. */
465 HOWTO2 (R_NDS32_LO12S0, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 12, /* bitsize */
469 FALSE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_dont,/* complain_on_overflow */
472 nds32_elf_lo12_reloc, /* special_function */
473 "R_NDS32_LO12S0", /* name */
474 FALSE, /* partial_inplace */
475 0x00000fff, /* src_mask */
476 0x00000fff, /* dst_mask */
477 FALSE), /* pcrel_offset */
478
479 /* Small data area 15 bits offset. */
480 HOWTO2 (R_NDS32_SDA15S3, /* type */
481 3, /* rightshift */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
483 15, /* bitsize */
484 FALSE, /* pc_relative */
485 0, /* bitpos */
486 complain_overflow_signed,/* complain_on_overflow */
487 nds32_elf_sda15_reloc, /* special_function */
488 "R_NDS32_SDA15S3", /* name */
489 FALSE, /* partial_inplace */
490 0x00007fff, /* src_mask */
491 0x00007fff, /* dst_mask */
492 FALSE), /* pcrel_offset */
493
494 /* Small data area 15 bits offset. */
495 HOWTO2 (R_NDS32_SDA15S2, /* type */
496 2, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 15, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_signed,/* complain_on_overflow */
502 nds32_elf_sda15_reloc, /* special_function */
503 "R_NDS32_SDA15S2", /* name */
504 FALSE, /* partial_inplace */
505 0x00007fff, /* src_mask */
506 0x00007fff, /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 /* Small data area 15 bits offset. */
510 HOWTO2 (R_NDS32_SDA15S1, /* type */
511 1, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 15, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_signed,/* complain_on_overflow */
517 nds32_elf_sda15_reloc, /* special_function */
518 "R_NDS32_SDA15S1", /* name */
519 FALSE, /* partial_inplace */
520 0x00007fff, /* src_mask */
521 0x00007fff, /* dst_mask */
522 FALSE), /* pcrel_offset */
523
524 /* Small data area 15 bits offset. */
525 HOWTO2 (R_NDS32_SDA15S0, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 15, /* bitsize */
529 FALSE, /* pc_relative */
530 0, /* bitpos */
531 complain_overflow_signed,/* complain_on_overflow */
532 nds32_elf_sda15_reloc, /* special_function */
533 "R_NDS32_SDA15S0", /* name */
534 FALSE, /* partial_inplace */
535 0x00007fff, /* src_mask */
536 0x00007fff, /* dst_mask */
537 FALSE), /* pcrel_offset */
538
539 /* GNU extension to record C++ vtable hierarchy */
540 HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
541 0, /* rightshift */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
543 0, /* bitsize */
544 FALSE, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_dont,/* complain_on_overflow */
547 NULL, /* special_function */
548 "R_NDS32_GNU_VTINHERIT",/* name */
549 FALSE, /* partial_inplace */
550 0, /* src_mask */
551 0, /* dst_mask */
552 FALSE), /* pcrel_offset */
553
554 /* GNU extension to record C++ vtable member usage */
555 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 0, /* bitsize */
559 FALSE, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_dont,/* complain_on_overflow */
562 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
563 "R_NDS32_GNU_VTENTRY", /* name */
564 FALSE, /* partial_inplace */
565 0, /* src_mask */
566 0, /* dst_mask */
567 FALSE), /* pcrel_offset */
568
569 /* A 16 bit absolute relocation. */
570 HOWTO2 (R_NDS32_16_RELA, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 FALSE, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_bitfield,/* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_NDS32_16_RELA", /* name */
579 FALSE, /* partial_inplace */
580 0xffff, /* src_mask */
581 0xffff, /* dst_mask */
582 FALSE), /* pcrel_offset */
583
584 /* A 32 bit absolute relocation. */
585 HOWTO2 (R_NDS32_32_RELA, /* type */
586 0, /* rightshift */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
588 32, /* bitsize */
589 FALSE, /* pc_relative */
590 0, /* bitpos */
591 complain_overflow_bitfield,/* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_NDS32_32_RELA", /* name */
594 FALSE, /* partial_inplace */
595 0xffffffff, /* src_mask */
596 0xffffffff, /* dst_mask */
597 FALSE), /* pcrel_offset */
598
599 /* A 20 bit address. */
600 HOWTO2 (R_NDS32_20_RELA, /* type */
601 0, /* rightshift */
602 2, /* size (0 = byte, 1 = short, 2 = long) */
603 20, /* bitsize */
604 FALSE, /* pc_relative */
605 0, /* bitpos */
606 complain_overflow_signed,/* complain_on_overflow */
607 bfd_elf_generic_reloc, /* special_function */
608 "R_NDS32_20_RELA", /* name */
609 FALSE, /* partial_inplace */
610 0xfffff, /* src_mask */
611 0xfffff, /* dst_mask */
612 FALSE), /* pcrel_offset */
613
614 HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
615 1, /* rightshift */
616 1, /* size (0 = byte, 1 = short, 2 = long) */
617 8, /* bitsize */
618 TRUE, /* pc_relative */
619 0, /* bitpos */
620 complain_overflow_signed,/* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_NDS32_9_PCREL_RELA",/* name */
623 FALSE, /* partial_inplace */
624 0xff, /* src_mask */
625 0xff, /* dst_mask */
626 TRUE), /* pcrel_offset */
627
628 /* A relative 15 bit relocation, right shifted by 1. */
629 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
630 1, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 14, /* bitsize */
633 TRUE, /* pc_relative */
634 0, /* bitpos */
635 complain_overflow_signed,/* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_NDS32_15_PCREL_RELA",/* name */
638 FALSE, /* partial_inplace */
639 0x3fff, /* src_mask */
640 0x3fff, /* dst_mask */
641 TRUE), /* pcrel_offset */
642
643 /* A relative 17 bit relocation, right shifted by 1. */
644 HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
645 1, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 16, /* bitsize */
648 TRUE, /* pc_relative */
649 0, /* bitpos */
650 complain_overflow_signed,/* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_NDS32_17_PCREL_RELA",/* name */
653 FALSE, /* partial_inplace */
654 0xffff, /* src_mask */
655 0xffff, /* dst_mask */
656 TRUE), /* pcrel_offset */
657
658 /* A relative 25 bit relocation, right shifted by 2. */
659 HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
660 1, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 24, /* bitsize */
663 TRUE, /* pc_relative */
664 0, /* bitpos */
665 complain_overflow_signed,/* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_NDS32_25_PCREL_RELA",/* name */
668 FALSE, /* partial_inplace */
669 0xffffff, /* src_mask */
670 0xffffff, /* dst_mask */
671 TRUE), /* pcrel_offset */
672
673 /* High 20 bits of address when lower 16 is or'd in. */
674 HOWTO2 (R_NDS32_HI20_RELA, /* type */
675 12, /* rightshift */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
677 20, /* bitsize */
678 FALSE, /* pc_relative */
679 0, /* bitpos */
680 complain_overflow_dont,/* complain_on_overflow */
681 bfd_elf_generic_reloc, /* special_function */
682 "R_NDS32_HI20_RELA", /* name */
683 FALSE, /* partial_inplace */
684 0x000fffff, /* src_mask */
685 0x000fffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
687
688 /* Lower 12 bits of address. */
689 HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
690 3, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 9, /* bitsize */
693 FALSE, /* pc_relative */
694 0, /* bitpos */
695 complain_overflow_dont,/* complain_on_overflow */
696 bfd_elf_generic_reloc, /* special_function */
697 "R_NDS32_LO12S3_RELA", /* name */
698 FALSE, /* partial_inplace */
699 0x000001ff, /* src_mask */
700 0x000001ff, /* dst_mask */
701 FALSE), /* pcrel_offset */
702
703 /* Lower 12 bits of address. */
704 HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
705 2, /* rightshift */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
707 10, /* bitsize */
708 FALSE, /* pc_relative */
709 0, /* bitpos */
710 complain_overflow_dont,/* complain_on_overflow */
711 bfd_elf_generic_reloc, /* special_function */
712 "R_NDS32_LO12S2_RELA", /* name */
713 FALSE, /* partial_inplace */
714 0x000003ff, /* src_mask */
715 0x000003ff, /* dst_mask */
716 FALSE), /* pcrel_offset */
717
718 /* Lower 12 bits of address. */
719 HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
720 1, /* rightshift */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
722 11, /* bitsize */
723 FALSE, /* pc_relative */
724 0, /* bitpos */
725 complain_overflow_dont,/* complain_on_overflow */
726 bfd_elf_generic_reloc, /* special_function */
727 "R_NDS32_LO12S1_RELA", /* name */
728 FALSE, /* partial_inplace */
729 0x000007ff, /* src_mask */
730 0x000007ff, /* dst_mask */
731 FALSE), /* pcrel_offset */
732
733 /* Lower 12 bits of address. */
734 HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
735 0, /* rightshift */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
737 12, /* bitsize */
738 FALSE, /* pc_relative */
739 0, /* bitpos */
740 complain_overflow_dont,/* complain_on_overflow */
741 bfd_elf_generic_reloc, /* special_function */
742 "R_NDS32_LO12S0_RELA", /* name */
743 FALSE, /* partial_inplace */
744 0x00000fff, /* src_mask */
745 0x00000fff, /* dst_mask */
746 FALSE), /* pcrel_offset */
747
748 /* Small data area 15 bits offset. */
749 HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
750 3, /* rightshift */
751 2, /* size (0 = byte, 1 = short, 2 = long) */
752 15, /* bitsize */
753 FALSE, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_signed,/* complain_on_overflow */
756 bfd_elf_generic_reloc, /* special_function */
757 "R_NDS32_SDA15S3_RELA",/* name */
758 FALSE, /* partial_inplace */
759 0x00007fff, /* src_mask */
760 0x00007fff, /* dst_mask */
761 FALSE), /* pcrel_offset */
762
763 /* Small data area 15 bits offset. */
764 HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
765 2, /* rightshift */
766 2, /* size (0 = byte, 1 = short, 2 = long) */
767 15, /* bitsize */
768 FALSE, /* pc_relative */
769 0, /* bitpos */
770 complain_overflow_signed,/* complain_on_overflow */
771 bfd_elf_generic_reloc, /* special_function */
772 "R_NDS32_SDA15S2_RELA",/* name */
773 FALSE, /* partial_inplace */
774 0x00007fff, /* src_mask */
775 0x00007fff, /* dst_mask */
776 FALSE), /* pcrel_offset */
777
778 HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
779 1, /* rightshift */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
781 15, /* bitsize */
782 FALSE, /* pc_relative */
783 0, /* bitpos */
784 complain_overflow_signed,/* complain_on_overflow */
785 bfd_elf_generic_reloc, /* special_function */
786 "R_NDS32_SDA15S1_RELA",/* name */
787 FALSE, /* partial_inplace */
788 0x00007fff, /* src_mask */
789 0x00007fff, /* dst_mask */
790 FALSE), /* pcrel_offset */
791
792 HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
793 0, /* rightshift */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
795 15, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_signed,/* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_NDS32_SDA15S0_RELA",/* name */
801 FALSE, /* partial_inplace */
802 0x00007fff, /* src_mask */
803 0x00007fff, /* dst_mask */
804 FALSE), /* pcrel_offset */
805
806 /* GNU extension to record C++ vtable hierarchy */
807 HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
808 0, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 0, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont,/* complain_on_overflow */
814 NULL, /* special_function */
815 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0, /* dst_mask */
819 FALSE), /* pcrel_offset */
820
821 /* GNU extension to record C++ vtable member usage */
822 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
823 0, /* rightshift */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
825 0, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont,/* complain_on_overflow */
829 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
830 "R_NDS32_RELA_GNU_VTENTRY",/* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0, /* dst_mask */
834 FALSE), /* pcrel_offset */
835
836 /* Like R_NDS32_20, but referring to the GOT table entry for
837 the symbol. */
838 HOWTO2 (R_NDS32_GOT20, /* type */
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 20, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_signed,/* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_NDS32_GOT20", /* name */
847 FALSE, /* partial_inplace */
848 0xfffff, /* src_mask */
849 0xfffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
851
852 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
853 entry for the symbol. */
854 HOWTO2 (R_NDS32_25_PLTREL, /* type */
855 1, /* rightshift */
856 2, /* size (0 = byte, 1 = short, 2 = long) */
857 24, /* bitsize */
858 TRUE, /* pc_relative */
859 0, /* bitpos */
860 complain_overflow_signed,/* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_NDS32_25_PLTREL", /* name */
863 FALSE, /* partial_inplace */
864 0xffffff, /* src_mask */
865 0xffffff, /* dst_mask */
866 TRUE), /* pcrel_offset */
867
868 /* This is used only by the dynamic linker. The symbol should exist
869 both in the object being run and in some shared library. The
870 dynamic linker copies the data addressed by the symbol from the
871 shared library into the object, because the object being
872 run has to have the data at some particular address. */
873 HOWTO2 (R_NDS32_COPY, /* type */
874 0, /* rightshift */
875 2, /* size (0 = byte, 1 = short, 2 = long) */
876 32, /* bitsize */
877 FALSE, /* pc_relative */
878 0, /* bitpos */
879 complain_overflow_bitfield,/* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_NDS32_COPY", /* name */
882 FALSE, /* partial_inplace */
883 0xffffffff, /* src_mask */
884 0xffffffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
886
887 /* Like R_NDS32_20, but used when setting global offset table
888 entries. */
889 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
890 0, /* rightshift */
891 2, /* size (0 = byte, 1 = short, 2 = long) */
892 32, /* bitsize */
893 FALSE, /* pc_relative */
894 0, /* bitpos */
895 complain_overflow_bitfield,/* complain_on_overflow */
896 bfd_elf_generic_reloc, /* special_function */
897 "R_NDS32_GLOB_DAT", /* name */
898 FALSE, /* partial_inplace */
899 0xffffffff, /* src_mask */
900 0xffffffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
902
903 /* Marks a procedure linkage table entry for a symbol. */
904 HOWTO2 (R_NDS32_JMP_SLOT, /* type */
905 0, /* rightshift */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
907 32, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_bitfield,/* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_NDS32_JMP_SLOT", /* name */
913 FALSE, /* partial_inplace */
914 0xffffffff, /* src_mask */
915 0xffffffff, /* dst_mask */
916 FALSE), /* pcrel_offset */
917
918 /* Used only by the dynamic linker. When the object is run, this
919 longword is set to the load address of the object, plus the
920 addend. */
921 HOWTO2 (R_NDS32_RELATIVE, /* type */
922 0, /* rightshift */
923 2, /* size (0 = byte, 1 = short, 2 = long) */
924 32, /* bitsize */
925 FALSE, /* pc_relative */
926 0, /* bitpos */
927 complain_overflow_bitfield,/* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_NDS32_RELATIVE", /* name */
930 FALSE, /* partial_inplace */
931 0xffffffff, /* src_mask */
932 0xffffffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
934
935 HOWTO2 (R_NDS32_GOTOFF, /* type */
936 0, /* rightshift */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
938 20, /* bitsize */
939 FALSE, /* pc_relative */
940 0, /* bitpos */
941 complain_overflow_signed,/* complain_on_overflow */
942 bfd_elf_generic_reloc, /* special_function */
943 "R_NDS32_GOTOFF", /* name */
944 FALSE, /* partial_inplace */
945 0xfffff, /* src_mask */
946 0xfffff, /* dst_mask */
947 FALSE), /* pcrel_offset */
948
949 /* An PC Relative 20-bit relocation used when setting PIC offset
950 table register. */
951 HOWTO2 (R_NDS32_GOTPC20, /* type */
952 0, /* rightshift */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
954 20, /* bitsize */
955 TRUE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_signed,/* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_NDS32_GOTPC20", /* name */
960 FALSE, /* partial_inplace */
961 0xfffff, /* src_mask */
962 0xfffff, /* dst_mask */
963 TRUE), /* pcrel_offset */
964
965 /* Like R_NDS32_HI20, but referring to the GOT table entry for
966 the symbol. */
967 HOWTO2 (R_NDS32_GOT_HI20, /* type */
968 12, /* rightshift */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
970 20, /* bitsize */
971 FALSE, /* pc_relative */
972 0, /* bitpos */
973 complain_overflow_dont,/* complain_on_overflow */
974 bfd_elf_generic_reloc, /* special_function */
975 "R_NDS32_GOT_HI20", /* name */
976 FALSE, /* partial_inplace */
977 0x000fffff, /* src_mask */
978 0x000fffff, /* dst_mask */
979 FALSE), /* pcrel_offset */
980 HOWTO2 (R_NDS32_GOT_LO12, /* type */
981 0, /* rightshift */
982 2, /* size (0 = byte, 1 = short, 2 = long) */
983 12, /* bitsize */
984 FALSE, /* pc_relative */
985 0, /* bitpos */
986 complain_overflow_dont,/* complain_on_overflow */
987 bfd_elf_generic_reloc, /* special_function */
988 "R_NDS32_GOT_LO12", /* name */
989 FALSE, /* partial_inplace */
990 0x00000fff, /* src_mask */
991 0x00000fff, /* dst_mask */
992 FALSE), /* pcrel_offset */
993
994 /* An PC Relative relocation used when setting PIC offset table register.
995 Like R_NDS32_HI20, but referring to the GOT table entry for
996 the symbol. */
997 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
998 12, /* rightshift */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1000 20, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_dont,/* complain_on_overflow */
1004 bfd_elf_generic_reloc, /* special_function */
1005 "R_NDS32_GOTPC_HI20", /* name */
1006 FALSE, /* partial_inplace */
1007 0x000fffff, /* src_mask */
1008 0x000fffff, /* dst_mask */
1009 TRUE), /* pcrel_offset */
1010 HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
1011 0, /* rightshift */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1013 12, /* bitsize */
1014 FALSE, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_dont,/* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_NDS32_GOTPC_LO12", /* name */
1019 FALSE, /* partial_inplace */
1020 0x00000fff, /* src_mask */
1021 0x00000fff, /* dst_mask */
1022 TRUE), /* pcrel_offset */
1023
1024 HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
1025 12, /* rightshift */
1026 2, /* size (0 = byte, 1 = short, 2 = long) */
1027 20, /* bitsize */
1028 FALSE, /* pc_relative */
1029 0, /* bitpos */
1030 complain_overflow_dont,/* complain_on_overflow */
1031 bfd_elf_generic_reloc, /* special_function */
1032 "R_NDS32_GOTOFF_HI20", /* name */
1033 FALSE, /* partial_inplace */
1034 0x000fffff, /* src_mask */
1035 0x000fffff, /* dst_mask */
1036 FALSE), /* pcrel_offset */
1037 HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
1038 0, /* rightshift */
1039 2, /* size (0 = byte, 1 = short, 2 = long) */
1040 12, /* bitsize */
1041 FALSE, /* pc_relative */
1042 0, /* bitpos */
1043 complain_overflow_dont,/* complain_on_overflow */
1044 bfd_elf_generic_reloc, /* special_function */
1045 "R_NDS32_GOTOFF_LO12", /* name */
1046 FALSE, /* partial_inplace */
1047 0x00000fff, /* src_mask */
1048 0x00000fff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
1050
1051 /* Alignment hint for relaxable instruction. This is used with
1052 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1053 in order to make next label aligned on word boundary. */
1054 HOWTO2 (R_NDS32_INSN16, /* type */
1055 0, /* rightshift */
1056 2, /* size (0 = byte, 1 = short, 2 = long) */
1057 32, /* bitsize */
1058 FALSE, /* pc_relative */
1059 0, /* bitpos */
1060 complain_overflow_dont,/* complain_on_overflow */
1061 nds32_elf_ignore_reloc,/* special_function */
1062 "R_NDS32_INSN16", /* name */
1063 FALSE, /* partial_inplace */
1064 0x00000fff, /* src_mask */
1065 0x00000fff, /* dst_mask */
1066 FALSE), /* pcrel_offset */
1067
1068 /* Alignment hint for label. */
1069 HOWTO2 (R_NDS32_LABEL, /* type */
1070 0, /* rightshift */
1071 2, /* size (0 = byte, 1 = short, 2 = long) */
1072 32, /* bitsize */
1073 FALSE, /* pc_relative */
1074 0, /* bitpos */
1075 complain_overflow_dont,/* complain_on_overflow */
1076 nds32_elf_ignore_reloc,/* special_function */
1077 "R_NDS32_LABEL", /* name */
1078 FALSE, /* partial_inplace */
1079 0xffffffff, /* src_mask */
1080 0xffffffff, /* dst_mask */
1081 FALSE), /* pcrel_offset */
1082
1083 /* Relax hint for unconditional call sequence */
1084 HOWTO2 (R_NDS32_LONGCALL1, /* type */
1085 0, /* rightshift */
1086 2, /* size (0 = byte, 1 = short, 2 = long) */
1087 32, /* bitsize */
1088 FALSE, /* pc_relative */
1089 0, /* bitpos */
1090 complain_overflow_dont,/* complain_on_overflow */
1091 nds32_elf_ignore_reloc,/* special_function */
1092 "R_NDS32_LONGCALL1", /* name */
1093 FALSE, /* partial_inplace */
1094 0xffffffff, /* src_mask */
1095 0xffffffff, /* dst_mask */
1096 FALSE), /* pcrel_offset */
1097
1098 /* Relax hint for conditional call sequence. */
1099 HOWTO2 (R_NDS32_LONGCALL2, /* type */
1100 0, /* rightshift */
1101 2, /* size (0 = byte, 1 = short, 2 = long) */
1102 32, /* bitsize */
1103 FALSE, /* pc_relative */
1104 0, /* bitpos */
1105 complain_overflow_dont,/* complain_on_overflow */
1106 nds32_elf_ignore_reloc,/* special_function */
1107 "R_NDS32_LONGCALL2", /* name */
1108 FALSE, /* partial_inplace */
1109 0xffffffff, /* src_mask */
1110 0xffffffff, /* dst_mask */
1111 FALSE), /* pcrel_offset */
1112
1113 /* Relax hint for conditional call sequence. */
1114 HOWTO2 (R_NDS32_LONGCALL3, /* type */
1115 0, /* rightshift */
1116 2, /* size (0 = byte, 1 = short, 2 = long) */
1117 32, /* bitsize */
1118 FALSE, /* pc_relative */
1119 0, /* bitpos */
1120 complain_overflow_dont,/* complain_on_overflow */
1121 nds32_elf_ignore_reloc,/* special_function */
1122 "R_NDS32_LONGCALL3", /* name */
1123 FALSE, /* partial_inplace */
1124 0xffffffff, /* src_mask */
1125 0xffffffff, /* dst_mask */
1126 FALSE), /* pcrel_offset */
1127
1128 /* Relax hint for unconditional branch sequence. */
1129 HOWTO2 (R_NDS32_LONGJUMP1, /* type */
1130 0, /* rightshift */
1131 2, /* size (0 = byte, 1 = short, 2 = long) */
1132 32, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_dont,/* complain_on_overflow */
1136 nds32_elf_ignore_reloc,/* special_function */
1137 "R_NDS32_LONGJUMP1", /* name */
1138 FALSE, /* partial_inplace */
1139 0xffffffff, /* src_mask */
1140 0xffffffff, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1142
1143 /* Relax hint for conditional branch sequence. */
1144 HOWTO2 (R_NDS32_LONGJUMP2, /* type */
1145 0, /* rightshift */
1146 2, /* size (0 = byte, 1 = short, 2 = long) */
1147 32, /* bitsize */
1148 FALSE, /* pc_relative */
1149 0, /* bitpos */
1150 complain_overflow_dont,/* complain_on_overflow */
1151 nds32_elf_ignore_reloc,/* special_function */
1152 "R_NDS32_LONGJUMP2", /* name */
1153 FALSE, /* partial_inplace */
1154 0xffffffff, /* src_mask */
1155 0xffffffff, /* dst_mask */
1156 FALSE), /* pcrel_offset */
1157
1158 /* Relax hint for conditional branch sequence. */
1159 HOWTO2 (R_NDS32_LONGJUMP3, /* type */
1160 0, /* rightshift */
1161 2, /* size (0 = byte, 1 = short, 2 = long) */
1162 32, /* bitsize */
1163 FALSE, /* pc_relative */
1164 0, /* bitpos */
1165 complain_overflow_dont,/* complain_on_overflow */
1166 nds32_elf_ignore_reloc,/* special_function */
1167 "R_NDS32_LONGJUMP3", /* name */
1168 FALSE, /* partial_inplace */
1169 0xffffffff, /* src_mask */
1170 0xffffffff, /* dst_mask */
1171 FALSE), /* pcrel_offset */
1172
1173 /* Relax hint for load/store sequence. */
1174 HOWTO2 (R_NDS32_LOADSTORE, /* type */
1175 0, /* rightshift */
1176 2, /* size (0 = byte, 1 = short, 2 = long) */
1177 32, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_dont,/* complain_on_overflow */
1181 nds32_elf_ignore_reloc,/* special_function */
1182 "R_NDS32_LOADSTORE", /* name */
1183 FALSE, /* partial_inplace */
1184 0xffffffff, /* src_mask */
1185 0xffffffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1187
1188 /* Relax hint for load/store sequence. */
1189 HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
1190 0, /* rightshift */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 16, /* bitsize */
1193 FALSE, /* pc_relative */
1194 0, /* bitpos */
1195 complain_overflow_dont,/* complain_on_overflow */
1196 nds32_elf_ignore_reloc,/* special_function */
1197 "R_NDS32_9_FIXED_RELA",/* name */
1198 FALSE, /* partial_inplace */
1199 0x000000ff, /* src_mask */
1200 0x000000ff, /* dst_mask */
1201 FALSE), /* pcrel_offset */
1202
1203 /* Relax hint for load/store sequence. */
1204 HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
1205 0, /* rightshift */
1206 2, /* size (0 = byte, 1 = short, 2 = long) */
1207 32, /* bitsize */
1208 FALSE, /* pc_relative */
1209 0, /* bitpos */
1210 complain_overflow_dont,/* complain_on_overflow */
1211 nds32_elf_ignore_reloc,/* special_function */
1212 "R_NDS32_15_FIXED_RELA",/* name */
1213 FALSE, /* partial_inplace */
1214 0x00003fff, /* src_mask */
1215 0x00003fff, /* dst_mask */
1216 FALSE), /* pcrel_offset */
1217
1218 /* Relax hint for load/store sequence. */
1219 HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
1220 0, /* rightshift */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 32, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_dont,/* complain_on_overflow */
1226 nds32_elf_ignore_reloc,/* special_function */
1227 "R_NDS32_17_FIXED_RELA",/* name */
1228 FALSE, /* partial_inplace */
1229 0x0000ffff, /* src_mask */
1230 0x0000ffff, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1232
1233 /* Relax hint for load/store sequence. */
1234 HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
1235 0, /* rightshift */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 32, /* bitsize */
1238 FALSE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_dont,/* complain_on_overflow */
1241 nds32_elf_ignore_reloc,/* special_function */
1242 "R_NDS32_25_FIXED_RELA",/* name */
1243 FALSE, /* partial_inplace */
1244 0x00ffffff, /* src_mask */
1245 0x00ffffff, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1247
1248 /* High 20 bits of PLT symbol offset relative to PC. */
1249 HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
1250 12, /* rightshift */
1251 2, /* size (0 = byte, 1 = short, 2 = long) */
1252 20, /* bitsize */
1253 FALSE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_dont,/* complain_on_overflow */
1256 bfd_elf_generic_reloc, /* special_function */
1257 "R_NDS32_PLTREL_HI20", /* name */
1258 FALSE, /* partial_inplace */
1259 0x000fffff, /* src_mask */
1260 0x000fffff, /* dst_mask */
1261 FALSE), /* pcrel_offset */
1262
1263 /* Low 12 bits of PLT symbol offset relative to PC. */
1264 HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
1265 0, /* rightshift */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 12, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_dont,/* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 "R_NDS32_PLTREL_LO12", /* name */
1273 FALSE, /* partial_inplace */
1274 0x00000fff, /* src_mask */
1275 0x00000fff, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1277
1278 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1279 HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
1280 12, /* rightshift */
1281 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 20, /* bitsize */
1283 FALSE, /* pc_relative */
1284 0, /* bitpos */
1285 complain_overflow_dont,/* complain_on_overflow */
1286 bfd_elf_generic_reloc, /* special_function */
1287 "R_NDS32_PLT_GOTREL_HI20",/* name */
1288 FALSE, /* partial_inplace */
1289 0x000fffff, /* src_mask */
1290 0x000fffff, /* dst_mask */
1291 FALSE), /* pcrel_offset */
1292
1293 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1294 HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
1295 0, /* rightshift */
1296 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 12, /* bitsize */
1298 FALSE, /* pc_relative */
1299 0, /* bitpos */
1300 complain_overflow_dont,/* complain_on_overflow */
1301 bfd_elf_generic_reloc, /* special_function */
1302 "R_NDS32_PLT_GOTREL_LO12",/* name */
1303 FALSE, /* partial_inplace */
1304 0x00000fff, /* src_mask */
1305 0x00000fff, /* dst_mask */
1306 FALSE), /* pcrel_offset */
1307
1308 /* Small data area 12 bits offset. */
1309 HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
1310 2, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 12, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_signed,/* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_NDS32_SDA12S2_DP_RELA",/* name */
1318 FALSE, /* partial_inplace */
1319 0x00000fff, /* src_mask */
1320 0x00000fff, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1322
1323 /* Small data area 12 bits offset. */
1324 HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
1325 2, /* rightshift */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 12, /* bitsize */
1328 FALSE, /* pc_relative */
1329 0, /* bitpos */
1330 complain_overflow_signed,/* complain_on_overflow */
1331 bfd_elf_generic_reloc, /* special_function */
1332 "R_NDS32_SDA12S2_SP_RELA",/* name */
1333 FALSE, /* partial_inplace */
1334 0x00000fff, /* src_mask */
1335 0x00000fff, /* dst_mask */
1336 FALSE), /* pcrel_offset */
1337 /* Lower 12 bits of address. */
1338
1339 HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
1340 2, /* rightshift */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 10, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont,/* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
1347 "R_NDS32_LO12S2_DP_RELA",/* name */
1348 FALSE, /* partial_inplace */
1349 0x000003ff, /* src_mask */
1350 0x000003ff, /* dst_mask */
1351 FALSE), /* pcrel_offset */
1352
1353 /* Lower 12 bits of address. */
1354 HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
1355 2, /* rightshift */
1356 2, /* size (0 = byte, 1 = short, 2 = long) */
1357 10, /* bitsize */
1358 FALSE, /* pc_relative */
1359 0, /* bitpos */
1360 complain_overflow_dont,/* complain_on_overflow */
1361 bfd_elf_generic_reloc, /* special_function */
1362 "R_NDS32_LO12S2_SP_RELA",/* name */
1363 FALSE, /* partial_inplace */
1364 0x000003ff, /* src_mask */
1365 0x000003ff, /* dst_mask */
1366 FALSE), /* pcrel_offset */
1367 /* Lower 12 bits of address. Special identity for or case. */
1368 HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
1369 0, /* rightshift */
1370 2, /* size (0 = byte, 1 = short, 2 = long) */
1371 12, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_dont,/* complain_on_overflow */
1375 bfd_elf_generic_reloc, /* special_function */
1376 "R_NDS32_LO12S0_ORI_RELA",/* name */
1377 FALSE, /* partial_inplace */
1378 0x00000fff, /* src_mask */
1379 0x00000fff, /* dst_mask */
1380 FALSE), /* pcrel_offset */
1381 /* Small data area 19 bits offset. */
1382 HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
1383 3, /* rightshift */
1384 2, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_signed,/* complain_on_overflow */
1389 bfd_elf_generic_reloc, /* special_function */
1390 "R_NDS32_SDA16S3_RELA",/* name */
1391 FALSE, /* partial_inplace */
1392 0x0000ffff, /* src_mask */
1393 0x0000ffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1395
1396 /* Small data area 15 bits offset. */
1397 HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
1398 2, /* rightshift */
1399 2, /* size (0 = byte, 1 = short, 2 = long) */
1400 17, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_signed,/* complain_on_overflow */
1404 bfd_elf_generic_reloc, /* special_function */
1405 "R_NDS32_SDA17S2_RELA",/* name */
1406 FALSE, /* partial_inplace */
1407 0x0001ffff, /* src_mask */
1408 0x0001ffff, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1410
1411 HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
1412 1, /* rightshift */
1413 2, /* size (0 = byte, 1 = short, 2 = long) */
1414 18, /* bitsize */
1415 FALSE, /* pc_relative */
1416 0, /* bitpos */
1417 complain_overflow_signed,/* complain_on_overflow */
1418 bfd_elf_generic_reloc, /* special_function */
1419 "R_NDS32_SDA18S1_RELA",/* name */
1420 FALSE, /* partial_inplace */
1421 0x0003ffff, /* src_mask */
1422 0x0003ffff, /* dst_mask */
1423 FALSE), /* pcrel_offset */
1424
1425 HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
1426 0, /* rightshift */
1427 2, /* size (0 = byte, 1 = short, 2 = long) */
1428 19, /* bitsize */
1429 FALSE, /* pc_relative */
1430 0, /* bitpos */
1431 complain_overflow_signed,/* complain_on_overflow */
1432 bfd_elf_generic_reloc, /* special_function */
1433 "R_NDS32_SDA19S0_RELA",/* name */
1434 FALSE, /* partial_inplace */
1435 0x0007ffff, /* src_mask */
1436 0x0007ffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438 HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
1439 0, /* rightshift */
1440 0, /* size (0 = byte, 1 = short, 2 = long) */
1441 8, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont,/* complain_on_overflow */
1445 nds32_elf_ignore_reloc,/* special_function */
1446 "R_NDS32_DWARF2_OP1_RELA",/* name */
1447 FALSE, /* partial_inplace */
1448 0xff, /* src_mask */
1449 0xff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451 HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
1452 0, /* rightshift */
1453 1, /* size (0 = byte, 1 = short, 2 = long) */
1454 16, /* bitsize */
1455 FALSE, /* pc_relative */
1456 0, /* bitpos */
1457 complain_overflow_dont,/* complain_on_overflow */
1458 nds32_elf_ignore_reloc,/* special_function */
1459 "R_NDS32_DWARF2_OP2_RELA",/* name */
1460 FALSE, /* partial_inplace */
1461 0xffff, /* src_mask */
1462 0xffff, /* dst_mask */
1463 FALSE), /* pcrel_offset */
1464 HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
1465 0, /* rightshift */
1466 2, /* size (0 = byte, 1 = short, 2 = long) */
1467 32, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_dont,/* complain_on_overflow */
1471 nds32_elf_ignore_reloc,/* special_function */
1472 "R_NDS32_DWARF2_LEB_RELA",/* name */
1473 FALSE, /* partial_inplace */
1474 0xffffffff, /* src_mask */
1475 0xffffffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477 HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
1478 0, /* rightshift */
1479 1, /* size (0 = byte, 1 = short, 2 = long) */
1480 16, /* bitsize */
1481 FALSE, /* pc_relative */
1482 0, /* bitpos */
1483 complain_overflow_dont,/* complain_on_overflow */
1484 nds32_elf_ignore_reloc,/* special_function */
1485 "R_NDS32_UPDATE_TA_RELA",/* name */
1486 FALSE, /* partial_inplace */
1487 0xffff, /* src_mask */
1488 0xffff, /* dst_mask */
1489 FALSE), /* pcrel_offset */
1490 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1491 entry for the symbol. */
1492 HOWTO2 (R_NDS32_9_PLTREL, /* type */
1493 1, /* rightshift */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1495 8, /* bitsize */
1496 TRUE, /* pc_relative */
1497 0, /* bitpos */
1498 complain_overflow_signed,/* complain_on_overflow */
1499 bfd_elf_generic_reloc, /* special_function */
1500 "R_NDS32_9_PLTREL", /* name */
1501 FALSE, /* partial_inplace */
1502 0xff, /* src_mask */
1503 0xff, /* dst_mask */
1504 TRUE), /* pcrel_offset */
1505 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1506 HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
1507 0, /* rightshift */
1508 2, /* size (0 = byte, 1 = short, 2 = long) */
1509 20, /* bitsize */
1510 FALSE, /* pc_relative */
1511 0, /* bitpos */
1512 complain_overflow_dont,/* complain_on_overflow */
1513 bfd_elf_generic_reloc, /* special_function */
1514 "R_NDS32_PLT_GOTREL_LO20",/* name */
1515 FALSE, /* partial_inplace */
1516 0x000fffff, /* src_mask */
1517 0x000fffff, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1520 HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
1521 0, /* rightshift */
1522 2, /* size (0 = byte, 1 = short, 2 = long) */
1523 15, /* bitsize */
1524 FALSE, /* pc_relative */
1525 0, /* bitpos */
1526 complain_overflow_dont,/* complain_on_overflow */
1527 bfd_elf_generic_reloc, /* special_function */
1528 "R_NDS32_PLT_GOTREL_LO15",/* name */
1529 FALSE, /* partial_inplace */
1530 0x00007fff, /* src_mask */
1531 0x00007fff, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1533 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1534 HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
1535 0, /* rightshift */
1536 2, /* size (0 = byte, 1 = short, 2 = long) */
1537 19, /* bitsize */
1538 FALSE, /* pc_relative */
1539 0, /* bitpos */
1540 complain_overflow_dont,/* complain_on_overflow */
1541 bfd_elf_generic_reloc, /* special_function */
1542 "R_NDS32_PLT_GOTREL_LO19",/* name */
1543 FALSE, /* partial_inplace */
1544 0x0007ffff, /* src_mask */
1545 0x0007ffff, /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547 HOWTO2 (R_NDS32_GOT_LO15, /* type */
1548 0, /* rightshift */
1549 2, /* size (0 = byte, 1 = short, 2 = long) */
1550 15, /* bitsize */
1551 FALSE, /* pc_relative */
1552 0, /* bitpos */
1553 complain_overflow_dont,/* complain_on_overflow */
1554 bfd_elf_generic_reloc, /* special_function */
1555 "R_NDS32_GOT_LO15", /* name */
1556 FALSE, /* partial_inplace */
1557 0x00007fff, /* src_mask */
1558 0x00007fff, /* dst_mask */
1559 FALSE), /* pcrel_offset */
1560 HOWTO2 (R_NDS32_GOT_LO19, /* type */
1561 0, /* rightshift */
1562 2, /* size (0 = byte, 1 = short, 2 = long) */
1563 19, /* bitsize */
1564 FALSE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont,/* complain_on_overflow */
1567 bfd_elf_generic_reloc, /* special_function */
1568 "R_NDS32_GOT_LO19", /* name */
1569 FALSE, /* partial_inplace */
1570 0x0007ffff, /* src_mask */
1571 0x0007ffff, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1573 HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
1574 0, /* rightshift */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1576 15, /* bitsize */
1577 FALSE, /* pc_relative */
1578 0, /* bitpos */
1579 complain_overflow_dont,/* complain_on_overflow */
1580 bfd_elf_generic_reloc, /* special_function */
1581 "R_NDS32_GOTOFF_LO15", /* name */
1582 FALSE, /* partial_inplace */
1583 0x00007fff, /* src_mask */
1584 0x00007fff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586 HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
1587 0, /* rightshift */
1588 2, /* size (0 = byte, 1 = short, 2 = long) */
1589 19, /* bitsize */
1590 FALSE, /* pc_relative */
1591 0, /* bitpos */
1592 complain_overflow_dont,/* complain_on_overflow */
1593 bfd_elf_generic_reloc, /* special_function */
1594 "R_NDS32_GOTOFF_LO19", /* name */
1595 FALSE, /* partial_inplace */
1596 0x0007ffff, /* src_mask */
1597 0x0007ffff, /* dst_mask */
1598 FALSE), /* pcrel_offset */
1599 /* GOT 15 bits offset. */
1600 HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
1601 2, /* rightshift */
1602 2, /* size (0 = byte, 1 = short, 2 = long) */
1603 15, /* bitsize */
1604 FALSE, /* pc_relative */
1605 0, /* bitpos */
1606 complain_overflow_signed,/* complain_on_overflow */
1607 bfd_elf_generic_reloc, /* special_function */
1608 "R_NDS32_GOT15S2_RELA",/* name */
1609 FALSE, /* partial_inplace */
1610 0x00007fff, /* src_mask */
1611 0x00007fff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
1613 /* GOT 17 bits offset. */
1614 HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
1615 2, /* rightshift */
1616 2, /* size (0 = byte, 1 = short, 2 = long) */
1617 17, /* bitsize */
1618 FALSE, /* pc_relative */
1619 0, /* bitpos */
1620 complain_overflow_signed,/* complain_on_overflow */
1621 bfd_elf_generic_reloc, /* special_function */
1622 "R_NDS32_GOT17S2_RELA",/* name */
1623 FALSE, /* partial_inplace */
1624 0x0001ffff, /* src_mask */
1625 0x0001ffff, /* dst_mask */
1626 FALSE), /* pcrel_offset */
1627 /* A 5 bit address. */
1628 HOWTO2 (R_NDS32_5_RELA, /* type */
1629 0, /* rightshift */
1630 1, /* size (0 = byte, 1 = short, 2 = long) */
1631 5, /* bitsize */
1632 FALSE, /* pc_relative */
1633 0, /* bitpos */
1634 complain_overflow_signed,/* complain_on_overflow */
1635 bfd_elf_generic_reloc, /* special_function */
1636 "R_NDS32_5_RELA", /* name */
1637 FALSE, /* partial_inplace */
1638 0x1f, /* src_mask */
1639 0x1f, /* dst_mask */
1640 FALSE), /* pcrel_offset */
1641 HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
1642 1, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 9, /* bitsize */
1645 TRUE, /* pc_relative */
1646 0, /* bitpos */
1647 complain_overflow_unsigned,/* complain_on_overflow */
1648 bfd_elf_generic_reloc, /* special_function */
1649 "R_NDS32_10_UPCREL_RELA",/* name */
1650 FALSE, /* partial_inplace */
1651 0x1ff, /* src_mask */
1652 0x1ff, /* dst_mask */
1653 TRUE), /* pcrel_offset */
1654 HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
1655 2, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 7, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_unsigned,/* complain_on_overflow */
1661 bfd_elf_generic_reloc, /* special_function */
1662 "R_NDS32_SDA_FP7U2_RELA",/* name */
1663 FALSE, /* partial_inplace */
1664 0x0000007f, /* src_mask */
1665 0x0000007f, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667 HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
1668 1, /* rightshift */
1669 2, /* size (0 = byte, 1 = short, 2 = long) */
1670 8, /* bitsize */
1671 TRUE, /* pc_relative */
1672 0, /* bitpos */
1673 complain_overflow_signed,/* complain_on_overflow */
1674 bfd_elf_generic_reloc, /* special_function */
1675 "R_NDS32_WORD_9_PCREL_RELA",/* name */
1676 FALSE, /* partial_inplace */
1677 0xff, /* src_mask */
1678 0xff, /* dst_mask */
1679 TRUE), /* pcrel_offset */
1680 HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
1681 1, /* rightshift */
1682 2, /* size (0 = byte, 1 = short, 2 = long) */
1683 24, /* bitsize */
1684 FALSE, /* pc_relative */
1685 0, /* bitpos */
1686 complain_overflow_dont,/* complain_on_overflow */
1687 bfd_elf_generic_reloc, /* special_function */
1688 "R_NDS32_25_ABS_RELA", /* name */
1689 FALSE, /* partial_inplace */
1690 0xffffff, /* src_mask */
1691 0xffffff, /* dst_mask */
1692 FALSE), /* pcrel_offset */
1693
1694 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1695 HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
1696 1, /* rightshift */
1697 2, /* size (0 = byte, 1 = short, 2 = long) */
1698 16, /* bitsize */
1699 TRUE, /* pc_relative */
1700 0, /* bitpos */
1701 complain_overflow_signed,/* complain_on_overflow */
1702 bfd_elf_generic_reloc, /* special_function */
1703 "R_NDS32_17IFC_PCREL_RELA",/* name */
1704 FALSE, /* partial_inplace */
1705 0xffff, /* src_mask */
1706 0xffff, /* dst_mask */
1707 TRUE), /* pcrel_offset */
1708
1709 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1710 HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
1711 1, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 9, /* bitsize */
1714 TRUE, /* pc_relative */
1715 0, /* bitpos */
1716 complain_overflow_unsigned,/* complain_on_overflow */
1717 bfd_elf_generic_reloc, /* special_function */
1718 "R_NDS32_10IFCU_PCREL_RELA",/* name */
1719 FALSE, /* partial_inplace */
1720 0x1ff, /* src_mask */
1721 0x1ff, /* dst_mask */
1722 TRUE), /* pcrel_offset */
1723
1724 /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
1725 HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
1726 12, /* rightshift */
1727 2, /* size (0 = byte, 1 = short, 2 = long) */
1728 20, /* bitsize */
1729 FALSE, /* pc_relative */
1730 0, /* bitpos */
1731 complain_overflow_dont,/* complain_on_overflow */
1732 bfd_elf_generic_reloc, /* special_function */
1733 "R_NDS32_TLS_LE_HI20", /* name */
1734 FALSE, /* partial_inplace */
1735 0x000fffff, /* src_mask */
1736 0x000fffff, /* dst_mask */
1737 FALSE), /* pcrel_offset */
1738
1739 HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
1740 0, /* rightshift */
1741 2, /* size (0 = byte, 1 = short, 2 = long) */
1742 12, /* bitsize */
1743 FALSE, /* pc_relative */
1744 0, /* bitpos */
1745 complain_overflow_dont,/* complain_on_overflow */
1746 bfd_elf_generic_reloc, /* special_function */
1747 "R_NDS32_TLS_LE_LO12", /* name */
1748 FALSE, /* partial_inplace */
1749 0x00000fff, /* src_mask */
1750 0x00000fff, /* dst_mask */
1751 FALSE), /* pcrel_offset */
1752
1753 /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
1754 HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
1755 12, /* rightshift */
1756 2, /* size (0 = byte, 1 = short, 2 = long) */
1757 20, /* bitsize */
1758 FALSE, /* pc_relative */
1759 0, /* bitpos */
1760 complain_overflow_dont,/* complain_on_overflow */
1761 bfd_elf_generic_reloc, /* special_function */
1762 "R_NDS32_TLS_IE_HI20", /* name */
1763 FALSE, /* partial_inplace */
1764 0x000fffff, /* src_mask */
1765 0x000fffff, /* dst_mask */
1766 FALSE), /* pcrel_offset */
1767
1768 HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
1769 2, /* rightshift */
1770 2, /* size (0 = byte, 1 = short, 2 = long) */
1771 10, /* bitsize */
1772 FALSE, /* pc_relative */
1773 0, /* bitpos */
1774 complain_overflow_dont,/* complain_on_overflow */
1775 bfd_elf_generic_reloc, /* special_function */
1776 "R_NDS32_TLS_IE_LO12S2",/* name */
1777 FALSE, /* partial_inplace */
1778 0x000003ff, /* src_mask */
1779 0x000003ff, /* dst_mask */
1780 FALSE), /* pcrel_offset */
1781
1782 /* TLS LE TP offset relocation */
1783 HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
1784 0, /* rightshift */
1785 2, /* size (0 = byte, 1 = short, 2 = long) */
1786 32, /* bitsize */
1787 FALSE, /* pc_relative */
1788 0, /* bitpos */
1789 complain_overflow_bitfield,/* complain_on_overflow */
1790 bfd_elf_generic_reloc, /* special_function */
1791 "R_NDS32_TLS_TPOFF", /* name */
1792 FALSE, /* partial_inplace */
1793 0xffffffff, /* src_mask */
1794 0xffffffff, /* dst_mask */
1795 FALSE), /* pcrel_offset */
1796
1797 /* A 20 bit address. */
1798 HOWTO2 (R_NDS32_TLS_LE_20, /* type */
1799 0, /* rightshift */
1800 2, /* size (0 = byte, 1 = short, 2 = long) */
1801 20, /* bitsize */
1802 FALSE, /* pc_relative */
1803 0, /* bitpos */
1804 complain_overflow_signed,/* complain_on_overflow */
1805 bfd_elf_generic_reloc, /* special_function */
1806 "R_NDS32_TLS_LE_20", /* name */
1807 FALSE, /* partial_inplace */
1808 0xfffff, /* src_mask */
1809 0xfffff, /* dst_mask */
1810 FALSE), /* pcrel_offset */
1811
1812 HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
1813 0, /* rightshift */
1814 2, /* size (0 = byte, 1 = short, 2 = long) */
1815 15, /* bitsize */
1816 FALSE, /* pc_relative */
1817 0, /* bitpos */
1818 complain_overflow_signed,/* complain_on_overflow */
1819 bfd_elf_generic_reloc, /* special_function */
1820 "R_NDS32_TLS_LE_15S0", /* name */
1821 FALSE, /* partial_inplace */
1822 0x7fff, /* src_mask */
1823 0x7fff, /* dst_mask */
1824 FALSE), /* pcrel_offset */
1825 HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
1826 1, /* rightshift */
1827 2, /* size (0 = byte, 1 = short, 2 = long) */
1828 15, /* bitsize */
1829 FALSE, /* pc_relative */
1830 0, /* bitpos */
1831 complain_overflow_signed,/* complain_on_overflow */
1832 bfd_elf_generic_reloc, /* special_function */
1833 "R_NDS32_TLS_LE_15S1", /* name */
1834 FALSE, /* partial_inplace */
1835 0x7fff, /* src_mask */
1836 0x7fff, /* dst_mask */
1837 FALSE), /* pcrel_offset */
1838 HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
1839 2, /* rightshift */
1840 2, /* size (0 = byte, 1 = short, 2 = long) */
1841 15, /* bitsize */
1842 FALSE, /* pc_relative */
1843 0, /* bitpos */
1844 complain_overflow_signed,/* complain_on_overflow */
1845 bfd_elf_generic_reloc, /* special_function */
1846 "R_NDS32_TLS_LE_15S2", /* name */
1847 FALSE, /* partial_inplace */
1848 0x7fff, /* src_mask */
1849 0x7fff, /* dst_mask */
1850 FALSE), /* pcrel_offset */
1851
1852 /* Relax hint for unconditional call sequence */
1853 HOWTO2 (R_NDS32_LONGCALL4, /* type */
1854 0, /* rightshift */
1855 2, /* size (0 = byte, 1 = short, 2 = long) */
1856 32, /* bitsize */
1857 FALSE, /* pc_relative */
1858 0, /* bitpos */
1859 complain_overflow_dont,/* complain_on_overflow */
1860 nds32_elf_ignore_reloc,/* special_function */
1861 "R_NDS32_LONGCALL4", /* name */
1862 FALSE, /* partial_inplace */
1863 0xffffffff, /* src_mask */
1864 0xffffffff, /* dst_mask */
1865 FALSE), /* pcrel_offset */
1866
1867 /* Relax hint for conditional call sequence. */
1868 HOWTO2 (R_NDS32_LONGCALL5, /* type */
1869 0, /* rightshift */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1871 32, /* bitsize */
1872 FALSE, /* pc_relative */
1873 0, /* bitpos */
1874 complain_overflow_dont,/* complain_on_overflow */
1875 nds32_elf_ignore_reloc,/* special_function */
1876 "R_NDS32_LONGCALL5", /* name */
1877 FALSE, /* partial_inplace */
1878 0xffffffff, /* src_mask */
1879 0xffffffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1881
1882 /* Relax hint for conditional call sequence. */
1883 HOWTO2 (R_NDS32_LONGCALL6, /* type */
1884 0, /* rightshift */
1885 2, /* size (0 = byte, 1 = short, 2 = long) */
1886 32, /* bitsize */
1887 FALSE, /* pc_relative */
1888 0, /* bitpos */
1889 complain_overflow_dont,/* complain_on_overflow */
1890 nds32_elf_ignore_reloc,/* special_function */
1891 "R_NDS32_LONGCALL6", /* name */
1892 FALSE, /* partial_inplace */
1893 0xffffffff, /* src_mask */
1894 0xffffffff, /* dst_mask */
1895 FALSE), /* pcrel_offset */
1896
1897 /* Relax hint for unconditional branch sequence. */
1898 HOWTO2 (R_NDS32_LONGJUMP4, /* type */
1899 0, /* rightshift */
1900 2, /* size (0 = byte, 1 = short, 2 = long) */
1901 32, /* bitsize */
1902 FALSE, /* pc_relative */
1903 0, /* bitpos */
1904 complain_overflow_dont,/* complain_on_overflow */
1905 nds32_elf_ignore_reloc,/* special_function */
1906 "R_NDS32_LONGJUMP4", /* name */
1907 FALSE, /* partial_inplace */
1908 0xffffffff, /* src_mask */
1909 0xffffffff, /* dst_mask */
1910 FALSE), /* pcrel_offset */
1911
1912 /* Relax hint for conditional branch sequence. */
1913 HOWTO2 (R_NDS32_LONGJUMP5, /* type */
1914 0, /* rightshift */
1915 2, /* size (0 = byte, 1 = short, 2 = long) */
1916 32, /* bitsize */
1917 FALSE, /* pc_relative */
1918 0, /* bitpos */
1919 complain_overflow_dont,/* complain_on_overflow */
1920 nds32_elf_ignore_reloc,/* special_function */
1921 "R_NDS32_LONGJUMP5", /* name */
1922 FALSE, /* partial_inplace */
1923 0xffffffff, /* src_mask */
1924 0xffffffff, /* dst_mask */
1925 FALSE), /* pcrel_offset */
1926
1927 /* Relax hint for conditional branch sequence. */
1928 HOWTO2 (R_NDS32_LONGJUMP6, /* type */
1929 0, /* rightshift */
1930 2, /* size (0 = byte, 1 = short, 2 = long) */
1931 32, /* bitsize */
1932 FALSE, /* pc_relative */
1933 0, /* bitpos */
1934 complain_overflow_dont,/* complain_on_overflow */
1935 nds32_elf_ignore_reloc,/* special_function */
1936 "R_NDS32_LONGJUMP6", /* name */
1937 FALSE, /* partial_inplace */
1938 0xffffffff, /* src_mask */
1939 0xffffffff, /* dst_mask */
1940 FALSE), /* pcrel_offset */
1941
1942 /* Relax hint for conditional branch sequence. */
1943 HOWTO2 (R_NDS32_LONGJUMP7, /* type */
1944 0, /* rightshift */
1945 2, /* size (0 = byte, 1 = short, 2 = long) */
1946 32, /* bitsize */
1947 FALSE, /* pc_relative */
1948 0, /* bitpos */
1949 complain_overflow_dont,/* complain_on_overflow */
1950 nds32_elf_ignore_reloc,/* special_function */
1951 "R_NDS32_LONGJUMP7", /* name */
1952 FALSE, /* partial_inplace */
1953 0xffffffff, /* src_mask */
1954 0xffffffff, /* dst_mask */
1955 FALSE), /* pcrel_offset */
1956
1957 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1958 0, /* rightshift */
1959 2, /* size (0 = byte, 1 = short, 2 = long) */
1960 12, /* bitsize */
1961 FALSE, /* pc_relative */
1962 0, /* bitpos */
1963 complain_overflow_dont,/* complain_on_overflow */
1964 bfd_elf_generic_reloc, /* special_function */
1965 "R_NDS32_TLS_IE_LO12", /* name */
1966 FALSE, /* partial_inplace */
1967 0x00000fff, /* src_mask */
1968 0x00000fff, /* dst_mask */
1969 FALSE), /* pcrel_offset */
1970
1971 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1972 entry for the symbol. */
1973 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1974 12, /* rightshift */
1975 2, /* size (0 = byte, 1 = short, 2 = long) */
1976 20, /* bitsize */
1977 FALSE, /* pc_relative */
1978 0, /* bitpos */
1979 complain_overflow_dont,/* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_NDS32_TLS_IEGP_HI20",/* name */
1982 FALSE, /* partial_inplace */
1983 0x000fffff, /* src_mask */
1984 0x000fffff, /* dst_mask */
1985 FALSE), /* pcrel_offset */
1986
1987 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1988 0, /* rightshift */
1989 2, /* size (0 = byte, 1 = short, 2 = long) */
1990 12, /* bitsize */
1991 FALSE, /* pc_relative */
1992 0, /* bitpos */
1993 complain_overflow_dont,/* complain_on_overflow */
1994 bfd_elf_generic_reloc, /* special_function */
1995 "R_NDS32_TLS_IEGP_LO12",/* name */
1996 FALSE, /* partial_inplace */
1997 0x00000fff, /* src_mask */
1998 0x00000fff, /* dst_mask */
1999 FALSE), /* pcrel_offset */
2000
2001 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2002 2, /* rightshift */
2003 2, /* size (0 = byte, 1 = short, 2 = long) */
2004 10, /* bitsize */
2005 FALSE, /* pc_relative */
2006 0, /* bitpos */
2007 complain_overflow_dont,/* complain_on_overflow */
2008 bfd_elf_generic_reloc, /* special_function */
2009 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2010 FALSE, /* partial_inplace */
2011 0x000003ff, /* src_mask */
2012 0x000003ff, /* dst_mask */
2013 FALSE), /* pcrel_offset */
2014
2015 /* TLS description relocation */
2016 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2017 12, /* rightshift */
2018 2, /* size (0 = byte, 1 = short, 2 = long) */
2019 20, /* bitsize */
2020 FALSE, /* pc_relative */
2021 0, /* bitpos */
2022 complain_overflow_dont,/* complain_on_overflow */
2023 nds32_elf_hi20_reloc, /* special_function */
2024 "R_NDS32_TLS_DESC_HI20",/* name */
2025 FALSE, /* partial_inplace */
2026 0x000fffff, /* src_mask */
2027 0x000fffff, /* dst_mask */
2028 FALSE), /* pcrel_offset */
2029
2030 /* TLS GD/LD description offset high part. */
2031 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2032 12, /* rightshift */
2033 2, /* size (0 = byte, 1 = short, 2 = long) */
2034 20, /* bitsize */
2035 FALSE, /* pc_relative */
2036 0, /* bitpos */
2037 complain_overflow_dont,/* complain_on_overflow */
2038 nds32_elf_hi20_reloc, /* special_function */
2039 "R_NDS32_TLS_DESC_HI20",/* name */
2040 FALSE, /* partial_inplace */
2041 0x000fffff, /* src_mask */
2042 0x000fffff, /* dst_mask */
2043 FALSE), /* pcrel_offset */
2044
2045 /* TLS GD/LD description offset low part. */
2046 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2047 0, /* rightshift */
2048 2, /* size (0 = byte, 1 = short, 2 = long) */
2049 12, /* bitsize */
2050 FALSE, /* pc_relative */
2051 0, /* bitpos */
2052 complain_overflow_dont,/* complain_on_overflow */
2053 nds32_elf_lo12_reloc, /* special_function */
2054 "R_NDS32_TLS_DESC_LO12",/* name */
2055 FALSE, /* partial_inplace */
2056 0x00000fff, /* src_mask */
2057 0x00000fff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2059
2060 /* TLS GD/LD description offset set (movi). */
2061 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2062 0, /* rightshift */
2063 2, /* size (0 = byte, 1 = short, 2 = long) */
2064 20, /* bitsize */
2065 FALSE, /* pc_relative */
2066 0, /* bitpos */
2067 complain_overflow_signed,/* complain_on_overflow */
2068 bfd_elf_generic_reloc, /* special_function */
2069 "R_NDS32_TLS_DESC_20", /* name */
2070 FALSE, /* partial_inplace */
2071 0x000fffff, /* src_mask */
2072 0x000fffff, /* dst_mask */
2073 FALSE), /* pcrel_offset */
2074
2075 /* TLS GD/LD description offset set (lwi.gp). */
2076 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2077 2, /* rightshift */
2078 2, /* size (0 = byte, 1 = short, 2 = long) */
2079 17, /* bitsize */
2080 FALSE, /* pc_relative */
2081 0, /* bitpos */
2082 complain_overflow_signed,/* complain_on_overflow */
2083 bfd_elf_generic_reloc, /* special_function */
2084 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2085 FALSE, /* partial_inplace */
2086 0x0001ffff, /* src_mask */
2087 0x0001ffff, /* dst_mask */
2088 FALSE), /* pcrel_offset */
2089 };
2090
2091 /* Relocations used for relaxation. */
2092 #define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2093 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2094
2095 static reloc_howto_type nds32_elf_relax_howto_table[] = {
2096 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2097 0, /* rightshift */
2098 2, /* size (0 = byte, 1 = short, 2 = long) */
2099 32, /* bitsize */
2100 FALSE, /* pc_relative */
2101 0, /* bitpos */
2102 complain_overflow_dont,/* complain_on_overflow */
2103 nds32_elf_ignore_reloc,/* special_function */
2104 "R_NDS32_RELAX_ENTRY", /* name */
2105 FALSE, /* partial_inplace */
2106 0xffffffff, /* src_mask */
2107 0xffffffff, /* dst_mask */
2108 FALSE), /* pcrel_offset */
2109 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2110 0, /* rightshift */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2112 32, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_dont,/* complain_on_overflow */
2116 nds32_elf_ignore_reloc,/* special_function */
2117 "R_NDS32_GOT_SUFF", /* name */
2118 FALSE, /* partial_inplace */
2119 0xffffffff, /* src_mask */
2120 0xffffffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2123 0, /* rightshift */
2124 2, /* size (0 = byte, 1 = short, 2 = long) */
2125 32, /* bitsize */
2126 FALSE, /* pc_relative */
2127 0, /* bitpos */
2128 complain_overflow_bitfield,/* complain_on_overflow */
2129 nds32_elf_ignore_reloc,/* special_function */
2130 "R_NDS32_GOTOFF_SUFF", /* name */
2131 FALSE, /* partial_inplace */
2132 0xffffffff, /* src_mask */
2133 0xffffffff, /* dst_mask */
2134 FALSE), /* pcrel_offset */
2135 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2136 0, /* rightshift */
2137 2, /* size (0 = byte, 1 = short, 2 = long) */
2138 32, /* bitsize */
2139 FALSE, /* pc_relative */
2140 0, /* bitpos */
2141 complain_overflow_dont,/* complain_on_overflow */
2142 nds32_elf_ignore_reloc,/* special_function */
2143 "R_NDS32_PLT_GOT_SUFF",/* name */
2144 FALSE, /* partial_inplace */
2145 0xffffffff, /* src_mask */
2146 0xffffffff, /* dst_mask */
2147 FALSE), /* pcrel_offset */
2148 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2149 0, /* rightshift */
2150 2, /* size (0 = byte, 1 = short, 2 = long) */
2151 32, /* bitsize */
2152 FALSE, /* pc_relative */
2153 0, /* bitpos */
2154 complain_overflow_dont,/* complain_on_overflow */
2155 nds32_elf_ignore_reloc,/* special_function */
2156 "R_NDS32_MULCALL_SUFF",/* name */
2157 FALSE, /* partial_inplace */
2158 0xffffffff, /* src_mask */
2159 0xffffffff, /* dst_mask */
2160 FALSE), /* pcrel_offset */
2161 HOWTO3 (R_NDS32_PTR, /* type */
2162 0, /* rightshift */
2163 2, /* size (0 = byte, 1 = short, 2 = long) */
2164 32, /* bitsize */
2165 FALSE, /* pc_relative */
2166 0, /* bitpos */
2167 complain_overflow_dont,/* complain_on_overflow */
2168 nds32_elf_ignore_reloc,/* special_function */
2169 "R_NDS32_PTR", /* name */
2170 FALSE, /* partial_inplace */
2171 0xffffffff, /* src_mask */
2172 0xffffffff, /* dst_mask */
2173 FALSE), /* pcrel_offset */
2174 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2175 0, /* rightshift */
2176 2, /* size (0 = byte, 1 = short, 2 = long) */
2177 32, /* bitsize */
2178 FALSE, /* pc_relative */
2179 0, /* bitpos */
2180 complain_overflow_dont,/* complain_on_overflow */
2181 nds32_elf_ignore_reloc,/* special_function */
2182 "R_NDS32_PTR_COUNT", /* name */
2183 FALSE, /* partial_inplace */
2184 0xffffffff, /* src_mask */
2185 0xffffffff, /* dst_mask */
2186 FALSE), /* pcrel_offset */
2187 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2188 0, /* rightshift */
2189 2, /* size (0 = byte, 1 = short, 2 = long) */
2190 32, /* bitsize */
2191 FALSE, /* pc_relative */
2192 0, /* bitpos */
2193 complain_overflow_dont,/* complain_on_overflow */
2194 nds32_elf_ignore_reloc,/* special_function */
2195 "R_NDS32_PTR_RESOLVED",/* name */
2196 FALSE, /* partial_inplace */
2197 0xffffffff, /* src_mask */
2198 0xffffffff, /* dst_mask */
2199 FALSE), /* pcrel_offset */
2200 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2201 0, /* rightshift */
2202 2, /* size (0 = byte, 1 = short, 2 = long) */
2203 32, /* bitsize */
2204 FALSE, /* pc_relative */
2205 0, /* bitpos */
2206 complain_overflow_dont,/* complain_on_overflow */
2207 nds32_elf_ignore_reloc,/* special_function */
2208 "R_NDS32_PLTBLOCK", /* name */
2209 FALSE, /* partial_inplace */
2210 0xffffffff, /* src_mask */
2211 0xffffffff, /* dst_mask */
2212 FALSE), /* pcrel_offset */
2213 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2214 0, /* rightshift */
2215 2, /* size (0 = byte, 1 = short, 2 = long) */
2216 32, /* bitsize */
2217 FALSE, /* pc_relative */
2218 0, /* bitpos */
2219 complain_overflow_dont,/* complain_on_overflow */
2220 nds32_elf_ignore_reloc,/* special_function */
2221 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2222 FALSE, /* partial_inplace */
2223 0xffffffff, /* src_mask */
2224 0xffffffff, /* dst_mask */
2225 FALSE), /* pcrel_offset */
2226 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2227 0, /* rightshift */
2228 2, /* size (0 = byte, 1 = short, 2 = long) */
2229 32, /* bitsize */
2230 FALSE, /* pc_relative */
2231 0, /* bitpos */
2232 complain_overflow_dont,/* complain_on_overflow */
2233 nds32_elf_ignore_reloc,/* special_function */
2234 "R_NDS32_RELAX_REGION_END",/* name */
2235 FALSE, /* partial_inplace */
2236 0xffffffff, /* src_mask */
2237 0xffffffff, /* dst_mask */
2238 FALSE), /* pcrel_offset */
2239 HOWTO3 (R_NDS32_MINUEND, /* type */
2240 0, /* rightshift */
2241 2, /* size (0 = byte, 1 = short, 2 = long) */
2242 32, /* bitsize */
2243 FALSE, /* pc_relative */
2244 0, /* bitpos */
2245 complain_overflow_dont,/* complain_on_overflow */
2246 nds32_elf_ignore_reloc,/* special_function */
2247 "R_NDS32_MINUEND", /* name */
2248 FALSE, /* partial_inplace */
2249 0xffffffff, /* src_mask */
2250 0xffffffff, /* dst_mask */
2251 FALSE), /* pcrel_offset */
2252 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2253 0, /* rightshift */
2254 2, /* size (0 = byte, 1 = short, 2 = long) */
2255 32, /* bitsize */
2256 FALSE, /* pc_relative */
2257 0, /* bitpos */
2258 complain_overflow_dont,/* complain_on_overflow */
2259 nds32_elf_ignore_reloc,/* special_function */
2260 "R_NDS32_SUBTRAHEND", /* name */
2261 FALSE, /* partial_inplace */
2262 0xffffffff, /* src_mask */
2263 0xffffffff, /* dst_mask */
2264 FALSE), /* pcrel_offset */
2265 HOWTO3 (R_NDS32_DIFF8, /* type */
2266 0, /* rightshift */
2267 0, /* size (0 = byte, 1 = short, 2 = long) */
2268 8, /* bitsize */
2269 FALSE, /* pc_relative */
2270 0, /* bitpos */
2271 complain_overflow_dont,/* complain_on_overflow */
2272 nds32_elf_ignore_reloc,/* special_function */
2273 "R_NDS32_DIFF8", /* name */
2274 FALSE, /* partial_inplace */
2275 0x000000ff, /* src_mask */
2276 0x000000ff, /* dst_mask */
2277 FALSE), /* pcrel_offset */
2278 HOWTO3 (R_NDS32_DIFF16, /* type */
2279 0, /* rightshift */
2280 1, /* size (0 = byte, 1 = short, 2 = long) */
2281 16, /* bitsize */
2282 FALSE, /* pc_relative */
2283 0, /* bitpos */
2284 complain_overflow_dont,/* complain_on_overflow */
2285 nds32_elf_ignore_reloc,/* special_function */
2286 "R_NDS32_DIFF16", /* name */
2287 FALSE, /* partial_inplace */
2288 0x0000ffff, /* src_mask */
2289 0x0000ffff, /* dst_mask */
2290 FALSE), /* pcrel_offset */
2291 HOWTO3 (R_NDS32_DIFF32, /* type */
2292 0, /* rightshift */
2293 2, /* size (0 = byte, 1 = short, 2 = long) */
2294 32, /* bitsize */
2295 FALSE, /* pc_relative */
2296 0, /* bitpos */
2297 complain_overflow_dont,/* complain_on_overflow */
2298 nds32_elf_ignore_reloc,/* special_function */
2299 "R_NDS32_DIFF32", /* name */
2300 FALSE, /* partial_inplace */
2301 0xffffffff, /* src_mask */
2302 0xffffffff, /* dst_mask */
2303 FALSE), /* pcrel_offset */
2304 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2305 0, /* rightshift */
2306 0, /* size (0 = byte, 1 = short, 2 = long) */
2307 0, /* bitsize */
2308 FALSE, /* pc_relative */
2309 0, /* bitpos */
2310 complain_overflow_dont,/* complain_on_overflow */
2311 nds32_elf_ignore_reloc,/* special_function */
2312 "R_NDS32_DIFF_ULEB128",/* name */
2313 FALSE, /* partial_inplace */
2314 0xffffffff, /* src_mask */
2315 0xffffffff, /* dst_mask */
2316 FALSE), /* pcrel_offset */
2317 HOWTO3 (R_NDS32_DATA, /* type */
2318 0, /* rightshift */
2319 2, /* size (0 = byte, 1 = short, 2 = long) */
2320 32, /* bitsize */
2321 FALSE, /* pc_relative */
2322 0, /* bitpos */
2323 complain_overflow_dont,/* complain_on_overflow */
2324 nds32_elf_ignore_reloc,/* special_function */
2325 "R_NDS32_DATA", /* name */
2326 FALSE, /* partial_inplace */
2327 0xffffffff, /* src_mask */
2328 0xffffffff, /* dst_mask */
2329 FALSE), /* pcrel_offset */
2330 HOWTO3 (R_NDS32_TRAN, /* type */
2331 0, /* rightshift */
2332 2, /* size (0 = byte, 1 = short, 2 = long) */
2333 32, /* bitsize */
2334 FALSE, /* pc_relative */
2335 0, /* bitpos */
2336 complain_overflow_dont,/* complain_on_overflow */
2337 nds32_elf_ignore_reloc,/* special_function */
2338 "R_NDS32_TRAN", /* name */
2339 FALSE, /* partial_inplace */
2340 0xffffffff, /* src_mask */
2341 0xffffffff, /* dst_mask */
2342 FALSE), /* pcrel_offset */
2343 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2344 0, /* rightshift */
2345 2, /* size (0 = byte, 1 = short, 2 = long) */
2346 32, /* bitsize */
2347 FALSE, /* pc_relative */
2348 0, /* bitpos */
2349 complain_overflow_dont,/* complain_on_overflow */
2350 nds32_elf_ignore_reloc,/* special_function */
2351 "R_NDS32_TLS_LE_ADD", /* name */
2352 FALSE, /* partial_inplace */
2353 0xffffffff, /* src_mask */
2354 0xffffffff, /* dst_mask */
2355 FALSE), /* pcrel_offset */
2356 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2357 0, /* rightshift */
2358 2, /* size (0 = byte, 1 = short, 2 = long) */
2359 32, /* bitsize */
2360 FALSE, /* pc_relative */
2361 0, /* bitpos */
2362 complain_overflow_dont,/* complain_on_overflow */
2363 nds32_elf_ignore_reloc,/* special_function */
2364 "R_NDS32_TLS_LE_LS", /* name */
2365 FALSE, /* partial_inplace */
2366 0xffffffff, /* src_mask */
2367 0xffffffff, /* dst_mask */
2368 FALSE), /* pcrel_offset */
2369 HOWTO3 (R_NDS32_EMPTY, /* type */
2370 0, /* rightshift */
2371 2, /* size (0 = byte, 1 = short, 2 = long) */
2372 32, /* bitsize */
2373 FALSE, /* pc_relative */
2374 0, /* bitpos */
2375 complain_overflow_dont,/* complain_on_overflow */
2376 nds32_elf_ignore_reloc,/* special_function */
2377 "R_NDS32_EMPTY", /* name */
2378 FALSE, /* partial_inplace */
2379 0xffffffff, /* src_mask */
2380 0xffffffff, /* dst_mask */
2381 FALSE), /* pcrel_offset */
2382 /* TLS GD/LD description address base addition. */
2383 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2384 0, /* rightshift */
2385 2, /* size (0 = byte, 1 = short, 2 = long) */
2386 32, /* bitsize */
2387 FALSE, /* pc_relative */
2388 0, /* bitpos */
2389 complain_overflow_dont,/* complain_on_overflow */
2390 nds32_elf_ignore_reloc,/* special_function */
2391 "R_NDS32_TLS_DESC_ADD",/* name */
2392 FALSE, /* partial_inplace */
2393 0xffffffff, /* src_mask */
2394 0xffffffff, /* dst_mask */
2395 FALSE), /* pcrel_offset */
2396 /* TLS GD/LD description function load. */
2397 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2398 0, /* rightshift */
2399 2, /* size (0 = byte, 1 = short, 2 = long) */
2400 32, /* bitsize */
2401 FALSE, /* pc_relative */
2402 0, /* bitpos */
2403 complain_overflow_dont,/* complain_on_overflow */
2404 nds32_elf_ignore_reloc,/* special_function */
2405 "R_NDS32_TLS_DESC_FUNC",/* name */
2406 FALSE, /* partial_inplace */
2407 0xffffffff, /* src_mask */
2408 0xffffffff, /* dst_mask */
2409 FALSE), /* pcrel_offset */
2410 /* TLS DESC resolve function call. */
2411 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2412 0, /* rightshift */
2413 2, /* size (0 = byte, 1 = short, 2 = long) */
2414 32, /* bitsize */
2415 FALSE, /* pc_relative */
2416 0, /* bitpos */
2417 complain_overflow_dont,/* complain_on_overflow */
2418 nds32_elf_ignore_reloc,/* special_function */
2419 "R_NDS32_TLS_DESC_CALL",/* name */
2420 FALSE, /* partial_inplace */
2421 0xffffffff, /* src_mask */
2422 0xffffffff, /* dst_mask */
2423 FALSE), /* pcrel_offset */
2424 /* TLS DESC variable access. */
2425 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2426 0, /* rightshift */
2427 2, /* size (0 = byte, 1 = short, 2 = long) */
2428 32, /* bitsize */
2429 FALSE, /* pc_relative */
2430 0, /* bitpos */
2431 complain_overflow_dont,/* complain_on_overflow */
2432 nds32_elf_ignore_reloc,/* special_function */
2433 "R_NDS32_TLS_DESC_MEM",/* name */
2434 FALSE, /* partial_inplace */
2435 0xffffffff, /* src_mask */
2436 0xffffffff, /* dst_mask */
2437 FALSE), /* pcrel_offset */
2438 /* TLS GD/LD description mark (@tlsdec). */
2439 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2440 0, /* rightshift */
2441 2, /* size (0 = byte, 1 = short, 2 = long) */
2442 32, /* bitsize */
2443 FALSE, /* pc_relative */
2444 0, /* bitpos */
2445 complain_overflow_dont,/* complain_on_overflow */
2446 nds32_elf_ignore_reloc,/* special_function */
2447 "R_NDS32_REMOVE", /* name */
2448 FALSE, /* partial_inplace */
2449 0xffffffff, /* src_mask */
2450 0xffffffff, /* dst_mask */
2451 FALSE), /* pcrel_offset */
2452 /* TLS GD/LD description mark (@tlsdec). */
2453 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2454 0, /* rightshift */
2455 2, /* size (0 = byte, 1 = short, 2 = long) */
2456 32, /* bitsize */
2457 FALSE, /* pc_relative */
2458 0, /* bitpos */
2459 complain_overflow_dont,/* complain_on_overflow */
2460 nds32_elf_ignore_reloc,/* special_function */
2461 "R_NDS32_GROUP", /* name */
2462 FALSE, /* partial_inplace */
2463 0xffffffff, /* src_mask */
2464 0xffffffff, /* dst_mask */
2465 FALSE), /* pcrel_offset */
2466 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2467 0, /* rightshift */
2468 2, /* size (0 = byte, 1 = short, 2 = long) */
2469 32, /* bitsize */
2470 FALSE, /* pc_relative */
2471 0, /* bitpos */
2472 complain_overflow_dont,/* complain_on_overflow */
2473 nds32_elf_ignore_reloc,/* special_function */
2474 "R_NDS32_TLS_IEGP_LW", /* name */
2475 FALSE, /* partial_inplace */
2476 0xffffffff, /* src_mask */
2477 0xffffffff, /* dst_mask */
2478 FALSE), /* pcrel_offset */
2479 /* LA and FLSI relaxation. */
2480 HOWTO3 (R_NDS32_LSI, /* type */
2481 0, /* rightshift */
2482 2, /* size (0 = byte, 1 = short, 2 = long) */
2483 32, /* bitsize */
2484 FALSE, /* pc_relative */
2485 0, /* bitpos */
2486 complain_overflow_dont,/* complain_on_overflow */
2487 nds32_elf_ignore_reloc,/* special_function */
2488 "R_NDS32_LSI", /* name */
2489 FALSE, /* partial_inplace */
2490 0xffffffff, /* src_mask */
2491 0xffffffff, /* dst_mask */
2492 FALSE),
2493 };
2494
2495 static unsigned long dl_tlsdesc_lazy_trampoline[] =
2496 {
2497 0x46200000, /* sethi $r2,#0x0 */
2498 0x58210000, /* ori $r2,$r2,#0x0 */
2499 0x40217400, /* add $r2,$r2,$gp */
2500 0x04210000, /* lwi $r2,[$r2+#0x0] */
2501 0x46300000, /* sethi $r3,#0x0 */
2502 0x58318000, /* ori $r3,$r3,#0x0 */
2503 0x4031f400, /* add $r3,$r3,$gp */
2504 0x4a000800, /* jr $r2 */
2505 };
2506
2507 static void
2508 nds32_put_trampoline (void *contents, const unsigned long *template,
2509 unsigned count)
2510 {
2511 unsigned ix;
2512
2513 for (ix = 0; ix != count; ix++)
2514 {
2515 unsigned long insn = template[ix];
2516 bfd_putb32 (insn, (char *) contents + ix * 4);
2517 }
2518 }
2519
2520 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2521 This prototype is the same as qsort (). */
2522
2523 void
2524 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2525 int (*compar) (const void *lhs, const void *rhs))
2526 {
2527 char *ptr = (char *) base;
2528 int i, j;
2529 char tmp[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 bfd_boolean
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 bfd_boolean
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 bfd_boolean
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 bfd_boolean
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 bfd_boolean
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 alignemnt 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;
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 bfd_boolean 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->sdynbss = NULL;
3700 ret->srelbss = NULL;
3701 ret->sym_ld_script = NULL;
3702
3703 return &ret->root.root;
3704 }
3705
3706 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3707 shortcuts to them in our hash table. */
3708
3709 static bfd_boolean
3710 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3711 {
3712 struct elf_link_hash_table *ehtab;
3713
3714 if (!_bfd_elf_create_got_section (dynobj, info))
3715 return FALSE;
3716
3717 ehtab = elf_hash_table (info);
3718 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3719 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3720 if (!ehtab->sgot || !ehtab->sgotplt)
3721 abort ();
3722
3723 /* _bfd_elf_create_got_section will create it for us. */
3724 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3725 if (ehtab->srelgot == NULL
3726 || !bfd_set_section_flags (ehtab->srelgot,
3727 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3728 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3729 | SEC_READONLY))
3730 || !bfd_set_section_alignment (ehtab->srelgot, 2))
3731 return FALSE;
3732
3733 return TRUE;
3734 }
3735
3736 /* Create dynamic sections when linking against a dynamic object. */
3737
3738 static bfd_boolean
3739 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3740 {
3741 struct elf_link_hash_table *ehtab;
3742 struct elf_nds32_link_hash_table *htab;
3743 flagword flags, pltflags;
3744 register asection *s;
3745 const struct elf_backend_data *bed;
3746 int ptralign = 2; /* 32-bit */
3747 const char *secname;
3748 char *relname;
3749 flagword secflags;
3750 asection *sec;
3751
3752 bed = get_elf_backend_data (abfd);
3753 ehtab = elf_hash_table (info);
3754 htab = nds32_elf_hash_table (info);
3755
3756 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3757 .rel[a].bss sections. */
3758
3759 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3760 | SEC_LINKER_CREATED);
3761
3762 pltflags = flags;
3763 pltflags |= SEC_CODE;
3764 if (bed->plt_not_loaded)
3765 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3766 if (bed->plt_readonly)
3767 pltflags |= SEC_READONLY;
3768
3769 s = bfd_make_section (abfd, ".plt");
3770 ehtab->splt = s;
3771 if (s == NULL
3772 || !bfd_set_section_flags (s, pltflags)
3773 || !bfd_set_section_alignment (s, bed->plt_alignment))
3774 return FALSE;
3775
3776 if (bed->want_plt_sym)
3777 {
3778 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3779 .plt section. */
3780 struct bfd_link_hash_entry *bh = NULL;
3781 struct elf_link_hash_entry *h;
3782
3783 if (!(_bfd_generic_link_add_one_symbol
3784 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3785 (bfd_vma) 0, (const char *) NULL, FALSE,
3786 get_elf_backend_data (abfd)->collect, &bh)))
3787 return FALSE;
3788
3789 h = (struct elf_link_hash_entry *) bh;
3790 h->def_regular = 1;
3791 h->type = STT_OBJECT;
3792
3793 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3794 return FALSE;
3795 }
3796
3797 s = bfd_make_section (abfd,
3798 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3799 ehtab->srelplt = s;
3800 if (s == NULL
3801 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3802 || !bfd_set_section_alignment (s, ptralign))
3803 return FALSE;
3804
3805 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3806 return FALSE;
3807
3808 for (sec = abfd->sections; sec; sec = sec->next)
3809 {
3810 secflags = bfd_section_flags (sec);
3811 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3812 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3813 continue;
3814 secname = bfd_section_name (sec);
3815 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3816 strcpy (relname, ".rela");
3817 strcat (relname, secname);
3818 if (bfd_get_section_by_name (abfd, secname))
3819 continue;
3820 s = bfd_make_section (abfd, relname);
3821 if (s == NULL
3822 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3823 || !bfd_set_section_alignment (s, ptralign))
3824 return FALSE;
3825 }
3826
3827 if (bed->want_dynbss)
3828 {
3829 /* The .dynbss section is a place to put symbols which are defined
3830 by dynamic objects, are referenced by regular objects, and are
3831 not functions. We must allocate space for them in the process
3832 image and use a R_*_COPY reloc to tell the dynamic linker to
3833 initialize them at run time. The linker script puts the .dynbss
3834 section into the .bss section of the final image. */
3835 s = bfd_make_section (abfd, ".dynbss");
3836 htab->sdynbss = s;
3837 if (s == NULL
3838 || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3839 return FALSE;
3840 /* The .rel[a].bss section holds copy relocs. This section is not
3841 normally needed. We need to create it here, though, so that the
3842 linker will map it to an output section. We can't just create it
3843 only if we need it, because we will not know whether we need it
3844 until we have seen all the input files, and the first time the
3845 main linker code calls BFD after examining all the input files
3846 (size_dynamic_sections) the input sections have already been
3847 mapped to the output sections. If the section turns out not to
3848 be needed, we can discard it later. We will never need this
3849 section when generating a shared object, since they do not use
3850 copy relocs. */
3851 if (!bfd_link_pic (info))
3852 {
3853 s = bfd_make_section (abfd, (bed->default_use_rela_p
3854 ? ".rela.bss" : ".rel.bss"));
3855 htab->srelbss = s;
3856 if (s == NULL
3857 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3858 || !bfd_set_section_alignment (s, ptralign))
3859 return FALSE;
3860 }
3861 }
3862
3863 return TRUE;
3864 }
3865
3866 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3867 static void
3868 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3869 struct elf_link_hash_entry *dir,
3870 struct elf_link_hash_entry *ind)
3871 {
3872 struct elf_nds32_link_hash_entry *edir, *eind;
3873
3874 edir = (struct elf_nds32_link_hash_entry *) dir;
3875 eind = (struct elf_nds32_link_hash_entry *) ind;
3876
3877 if (ind->root.type == bfd_link_hash_indirect)
3878 {
3879 if (dir->got.refcount <= 0)
3880 {
3881 edir->tls_type = eind->tls_type;
3882 eind->tls_type = GOT_UNKNOWN;
3883 }
3884 }
3885
3886 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3887 }
3888 \f
3889 /* Adjust a symbol defined by a dynamic object and referenced by a
3890 regular object. The current definition is in some section of the
3891 dynamic object, but we're not including those sections. We have to
3892 change the definition to something the rest of the link can
3893 understand. */
3894
3895 static bfd_boolean
3896 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3897 struct elf_link_hash_entry *h)
3898 {
3899 struct elf_nds32_link_hash_table *htab;
3900 bfd *dynobj;
3901 asection *s;
3902 unsigned int power_of_two;
3903
3904 dynobj = elf_hash_table (info)->dynobj;
3905
3906 /* Make sure we know what is going on here. */
3907 BFD_ASSERT (dynobj != NULL
3908 && (h->needs_plt
3909 || h->is_weakalias
3910 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3911
3912
3913 /* If this is a function, put it in the procedure linkage table. We
3914 will fill in the contents of the procedure linkage table later,
3915 when we know the address of the .got section. */
3916 if (h->type == STT_FUNC || h->needs_plt)
3917 {
3918 if (!bfd_link_pic (info)
3919 && !h->def_dynamic
3920 && !h->ref_dynamic
3921 && h->root.type != bfd_link_hash_undefweak
3922 && h->root.type != bfd_link_hash_undefined)
3923 {
3924 /* This case can occur if we saw a PLT reloc in an input
3925 file, but the symbol was never referred to by a dynamic
3926 object. In such a case, we don't actually need to build
3927 a procedure linkage table, and we can just do a PCREL
3928 reloc instead. */
3929 h->plt.offset = (bfd_vma) - 1;
3930 h->needs_plt = 0;
3931 }
3932
3933 return TRUE;
3934 }
3935 else
3936 h->plt.offset = (bfd_vma) - 1;
3937
3938 /* If this is a weak symbol, and there is a real definition, the
3939 processor independent code will have arranged for us to see the
3940 real definition first, and we can just use the same value. */
3941 if (h->is_weakalias)
3942 {
3943 struct elf_link_hash_entry *def = weakdef (h);
3944 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3945 h->root.u.def.section = def->root.u.def.section;
3946 h->root.u.def.value = def->root.u.def.value;
3947 return TRUE;
3948 }
3949
3950 /* This is a reference to a symbol defined by a dynamic object which
3951 is not a function. */
3952
3953 /* If we are creating a shared library, we must presume that the
3954 only references to the symbol are via the global offset table.
3955 For such cases we need not do anything here; the relocations will
3956 be handled correctly by relocate_section. */
3957 if (bfd_link_pic (info))
3958 return TRUE;
3959
3960 /* If there are no references to this symbol that do not use the
3961 GOT, we don't need to generate a copy reloc. */
3962 if (!h->non_got_ref)
3963 return TRUE;
3964
3965 /* If -z nocopyreloc was given, we won't generate them either. */
3966 if (0 && info->nocopyreloc)
3967 {
3968 h->non_got_ref = 0;
3969 return TRUE;
3970 }
3971
3972 /* If we don't find any dynamic relocs in read-only sections, then
3973 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3974 if (!_bfd_elf_readonly_dynrelocs (h))
3975 {
3976 h->non_got_ref = 0;
3977 return TRUE;
3978 }
3979
3980 /* We must allocate the symbol in our .dynbss section, which will
3981 become part of the .bss section of the executable. There will be
3982 an entry for this symbol in the .dynsym section. The dynamic
3983 object will contain position independent code, so all references
3984 from the dynamic object to this symbol will go through the global
3985 offset table. The dynamic linker will use the .dynsym entry to
3986 determine the address it must put in the global offset table, so
3987 both the dynamic object and the regular object will refer to the
3988 same memory location for the variable. */
3989
3990 htab = nds32_elf_hash_table (info);
3991 s = htab->sdynbss;
3992 BFD_ASSERT (s != NULL);
3993
3994 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3995 to copy the initial value out of the dynamic object and into the
3996 runtime process image. We need to remember the offset into the
3997 .rela.bss section we are going to use. */
3998 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3999 {
4000 asection *srel;
4001
4002 srel = htab->srelbss;
4003 BFD_ASSERT (srel != NULL);
4004 srel->size += sizeof (Elf32_External_Rela);
4005 h->needs_copy = 1;
4006 }
4007
4008 /* We need to figure out the alignment required for this symbol. I
4009 have no idea how ELF linkers handle this. */
4010 power_of_two = bfd_log2 (h->size);
4011 if (power_of_two > 3)
4012 power_of_two = 3;
4013
4014 /* Apply the required alignment. */
4015 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4016 if (power_of_two > bfd_section_alignment (s))
4017 {
4018 if (!bfd_set_section_alignment (s, power_of_two))
4019 return FALSE;
4020 }
4021
4022 /* Define the symbol as being at this point in the section. */
4023 h->root.u.def.section = s;
4024 h->root.u.def.value = s->size;
4025
4026 /* Increment the section size to make room for the symbol. */
4027 s->size += h->size;
4028
4029 return TRUE;
4030 }
4031
4032 /* Allocate space in .plt, .got and associated reloc sections for
4033 dynamic relocs. */
4034
4035 static bfd_boolean
4036 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4037 {
4038 struct bfd_link_info *info;
4039 struct elf_link_hash_table *ehtab;
4040 struct elf_nds32_link_hash_table *htab;
4041 struct elf_dyn_relocs *p;
4042
4043 if (h->root.type == bfd_link_hash_indirect)
4044 return TRUE;
4045
4046 /* When warning symbols are created, they **replace** the "real"
4047 entry in the hash table, thus we never get to see the real
4048 symbol in a hash traversal. So look at it now. */
4049 if (h->root.type == bfd_link_hash_warning)
4050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4051
4052 info = (struct bfd_link_info *) inf;
4053 ehtab = elf_hash_table (info);
4054 htab = nds32_elf_hash_table (info);
4055 if (htab == NULL)
4056 return FALSE;
4057
4058 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4059 && h->plt.refcount > 0
4060 && !(bfd_link_pie (info) && h->def_regular))
4061 {
4062 /* Make sure this symbol is output as a dynamic symbol.
4063 Undefined weak syms won't yet be marked as dynamic. */
4064 if (h->dynindx == -1 && !h->forced_local)
4065 {
4066 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4067 return FALSE;
4068 }
4069
4070 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4071 {
4072 asection *s = ehtab->splt;
4073
4074 /* If this is the first .plt entry, make room for the special
4075 first entry. */
4076 if (s->size == 0)
4077 s->size += PLT_ENTRY_SIZE;
4078
4079 h->plt.offset = s->size;
4080
4081 /* If this symbol is not defined in a regular file, and we are
4082 not generating a shared library, then set the symbol to this
4083 location in the .plt. This is required to make function
4084 pointers compare as equal between the normal executable and
4085 the shared library. */
4086 if (!bfd_link_pic (info) && !h->def_regular)
4087 {
4088 h->root.u.def.section = s;
4089 h->root.u.def.value = h->plt.offset;
4090 }
4091
4092 /* Make room for this entry. */
4093 s->size += PLT_ENTRY_SIZE;
4094
4095 /* We also need to make an entry in the .got.plt section, which
4096 will be placed in the .got section by the linker script. */
4097 ehtab->sgotplt->size += 4;
4098
4099 /* We also need to make an entry in the .rel.plt section. */
4100 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4101 if (htab->tls_desc_trampoline)
4102 htab->next_tls_desc_index++;
4103 }
4104 else
4105 {
4106 h->plt.offset = (bfd_vma) - 1;
4107 h->needs_plt = 0;
4108 }
4109 }
4110 else
4111 {
4112 h->plt.offset = (bfd_vma) - 1;
4113 h->needs_plt = 0;
4114 }
4115
4116 if (h->got.refcount > 0)
4117 {
4118 asection *sgot;
4119 bfd_boolean dyn;
4120 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4121
4122 /* Make sure this symbol is output as a dynamic symbol.
4123 Undefined weak syms won't yet be marked as dynamic. */
4124 if (h->dynindx == -1 && !h->forced_local)
4125 {
4126 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127 return FALSE;
4128 }
4129
4130 sgot = elf_hash_table (info)->sgot;
4131 h->got.offset = sgot->size;
4132
4133 if (tls_type == GOT_UNKNOWN)
4134 abort ();
4135
4136 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4137 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4138 sgot->size += 4;
4139 else
4140 {
4141 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4142 if (tls_type & GOT_TLS_DESC)
4143 sgot->size += 8;
4144 }
4145
4146 dyn = htab->root.dynamic_sections_created;
4147
4148 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4149 {
4150 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4151 {
4152 /* TLS_DESC with trampoline needs a relocation slot
4153 within .rela.plt. */
4154 htab->num_tls_desc++;
4155 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4156 htab->tls_trampoline = -1;
4157 }
4158 else
4159 {
4160 /* other relocations, including TLS_DESC without trampoline, need
4161 a relocation slot within .rela.got. */
4162 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4163 }
4164 }
4165 }
4166 else
4167 h->got.offset = (bfd_vma)-1;
4168
4169 if (h->dyn_relocs == NULL)
4170 return TRUE;
4171
4172 /* In the shared -Bsymbolic case, discard space allocated for
4173 dynamic pc-relative relocs against symbols which turn out to be
4174 defined in regular objects. For the normal shared case, discard
4175 space for pc-relative relocs that have become local due to symbol
4176 visibility changes. */
4177
4178 if (bfd_link_pic (info))
4179 {
4180 if (h->def_regular && (h->forced_local || info->symbolic))
4181 {
4182 struct elf_dyn_relocs **pp;
4183
4184 for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4185 {
4186 p->count -= p->pc_count;
4187 p->pc_count = 0;
4188 if (p->count == 0)
4189 *pp = p->next;
4190 else
4191 pp = &p->next;
4192 }
4193 }
4194 }
4195 else
4196 {
4197 /* For the non-shared case, discard space for relocs against
4198 symbols which turn out to need copy relocs or are not dynamic. */
4199
4200 if (!h->non_got_ref
4201 && ((h->def_dynamic
4202 && !h->def_regular)
4203 || (htab->root.dynamic_sections_created
4204 && (h->root.type == bfd_link_hash_undefweak
4205 || h->root.type == bfd_link_hash_undefined))))
4206 {
4207 /* Make sure this symbol is output as a dynamic symbol.
4208 Undefined weak syms won't yet be marked as dynamic. */
4209 if (h->dynindx == -1 && !h->forced_local)
4210 {
4211 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4212 return FALSE;
4213 }
4214
4215 /* If that succeeded, we know we'll be keeping all the
4216 relocs. */
4217 if (h->dynindx != -1)
4218 goto keep;
4219 }
4220
4221 h->dyn_relocs = NULL;
4222
4223 keep:;
4224 }
4225
4226 /* Finally, allocate space. */
4227 for (p = h->dyn_relocs; p != NULL; p = p->next)
4228 {
4229 asection *sreloc = elf_section_data (p->sec)->sreloc;
4230 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4231 }
4232
4233 return TRUE;
4234 }
4235
4236 /* Add relocation REL to the end of relocation section SRELOC. */
4237
4238 static void
4239 elf32_nds32_add_dynreloc (bfd *output_bfd,
4240 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4241 asection *sreloc, Elf_Internal_Rela *rel)
4242 {
4243 bfd_byte *loc;
4244 if (sreloc == NULL)
4245 abort ();
4246
4247 loc = sreloc->contents;
4248 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4249 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4250 abort ();
4251
4252 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4253 }
4254
4255 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
4256 read-only sections. */
4257
4258 static bfd_boolean
4259 maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
4260 {
4261 asection *sec;
4262
4263 if (h->root.type == bfd_link_hash_indirect)
4264 return TRUE;
4265
4266 sec = _bfd_elf_readonly_dynrelocs (h);
4267 if (sec != NULL)
4268 {
4269 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
4270
4271 info->flags |= DF_TEXTREL;
4272 info->callbacks->minfo
4273 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
4274 sec->owner, h->root.root.string, sec);
4275
4276 /* Not an error, just cut short the traversal. */
4277 return FALSE;
4278 }
4279 return TRUE;
4280 }
4281
4282 /* Set the sizes of the dynamic sections. */
4283
4284 static bfd_boolean
4285 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4286 struct bfd_link_info *info)
4287 {
4288 struct elf_nds32_link_hash_table *htab;
4289 bfd *dynobj;
4290 asection *s;
4291 bfd_boolean relocs;
4292 bfd_boolean plt;
4293 bfd *ibfd;
4294
4295 htab = nds32_elf_hash_table (info);
4296 if (htab == NULL)
4297 return FALSE;
4298
4299 dynobj = elf_hash_table (info)->dynobj;
4300 BFD_ASSERT (dynobj != NULL);
4301
4302 if (elf_hash_table (info)->dynamic_sections_created)
4303 {
4304 /* Set the contents of the .interp section to the interpreter. */
4305 if (bfd_link_executable (info) && !info->nointerp)
4306 {
4307 s = bfd_get_section_by_name (dynobj, ".interp");
4308 BFD_ASSERT (s != NULL);
4309 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4310 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4311 }
4312 }
4313
4314 /* Set up .got offsets for local syms, and space for local dynamic
4315 relocs. */
4316 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4317 {
4318 bfd_signed_vma *local_got;
4319 bfd_signed_vma *end_local_got;
4320 bfd_size_type locsymcount;
4321 Elf_Internal_Shdr *symtab_hdr;
4322 asection *sgot;
4323 char *local_tls_type;
4324 unsigned long symndx;
4325 bfd_vma *local_tlsdesc_gotent;
4326
4327 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4328 continue;
4329
4330 for (s = ibfd->sections; s != NULL; s = s->next)
4331 {
4332 struct elf_dyn_relocs *p;
4333
4334 for (p = ((struct elf_dyn_relocs *)
4335 elf_section_data (s)->local_dynrel);
4336 p != NULL; p = p->next)
4337 {
4338 if (!bfd_is_abs_section (p->sec)
4339 && bfd_is_abs_section (p->sec->output_section))
4340 {
4341 /* Input section has been discarded, either because
4342 it is a copy of a linkonce section or due to
4343 linker script /DISCARD/, so we'll be discarding
4344 the relocs too. */
4345 }
4346 else if (p->count != 0)
4347 {
4348 asection *sreloc = elf_section_data (p->sec)->sreloc;
4349 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4350 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4351 info->flags |= DF_TEXTREL;
4352 }
4353 }
4354 }
4355
4356 local_got = elf_local_got_refcounts (ibfd);
4357 if (!local_got)
4358 continue;
4359
4360 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4361 locsymcount = symtab_hdr->sh_info;
4362 end_local_got = local_got + locsymcount;
4363 sgot = elf_hash_table (info)->sgot;
4364 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4365 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4366 for (symndx = 0; local_got < end_local_got;
4367 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4368 {
4369 if (*local_got > 0)
4370 {
4371 int num_of_got_entry_needed = 0;
4372 *local_got = sgot->size;
4373 *local_tlsdesc_gotent = sgot->size;
4374
4375 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4376 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4377 num_of_got_entry_needed = 1;
4378 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4379 else if (*local_tls_type & GOT_TLS_DESC)
4380 num_of_got_entry_needed = 2;
4381
4382 sgot->size += (num_of_got_entry_needed << 2);
4383
4384 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4385 others need a slot in .rela.got. */
4386 if (*local_tls_type == GOT_TLS_DESC)
4387 {
4388 if (bfd_link_pic (info))
4389 {
4390 if (htab->tls_desc_trampoline)
4391 {
4392 htab->num_tls_desc++;
4393 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4394 htab->tls_trampoline = -1;
4395 }
4396 else
4397 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4398 }
4399 else
4400 {
4401 /* TLS_DESC -> TLS_LE */
4402 }
4403 }
4404 else
4405 {
4406 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4407 }
4408 }
4409 else
4410 {
4411 *local_got = (bfd_vma) -1;
4412 *local_tlsdesc_gotent = (bfd_vma) -1;
4413 }
4414 }
4415 }
4416
4417 /* Allocate global sym .plt and .got entries, and space for global
4418 sym dynamic relocs. */
4419 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4420
4421 /* For every jump slot reserved in the sgotplt, reloc_count is
4422 incremented. However, when we reserve space for TLS descriptors,
4423 it's not incremented, so in order to compute the space reserved
4424 for them, it suffices to multiply the reloc count by the jump
4425 slot size. */
4426 if (htab->tls_desc_trampoline && htab->root.srelplt)
4427 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4428
4429 if (htab->tls_trampoline)
4430 {
4431 htab->tls_trampoline = htab->root.splt->size;
4432
4433 /* If we're not using lazy TLS relocations, don't generate the
4434 PLT and GOT entries they require. */
4435 if (!(info->flags & DF_BIND_NOW))
4436 {
4437 htab->dt_tlsdesc_got = htab->root.sgot->size;
4438 htab->root.sgot->size += 4;
4439
4440 htab->dt_tlsdesc_plt = htab->root.splt->size;
4441 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4442 }
4443 }
4444
4445 /* We now have determined the sizes of the various dynamic sections.
4446 Allocate memory for them. */
4447 /* The check_relocs and adjust_dynamic_symbol entry points have
4448 determined the sizes of the various dynamic sections. Allocate
4449 memory for them. */
4450 plt = FALSE;
4451 relocs = FALSE;
4452 for (s = dynobj->sections; s != NULL; s = s->next)
4453 {
4454 if ((s->flags & SEC_LINKER_CREATED) == 0)
4455 continue;
4456
4457 if (s == htab->root.splt)
4458 {
4459 /* Strip this section if we don't need it; see the
4460 comment below. */
4461 plt = s->size != 0;
4462 }
4463 else if (s == elf_hash_table (info)->sgot)
4464 {
4465 got_size += s->size;
4466 }
4467 else if (s == elf_hash_table (info)->sgotplt)
4468 {
4469 got_size += s->size;
4470 }
4471 else if (strncmp (bfd_section_name (s), ".rela", 5) == 0)
4472 {
4473 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4474 relocs = TRUE;
4475
4476 /* We use the reloc_count field as a counter if we need
4477 to copy relocs into the output file. */
4478 s->reloc_count = 0;
4479 }
4480 else
4481 {
4482 /* It's not one of our sections, so don't allocate space. */
4483 continue;
4484 }
4485
4486 if (s->size == 0)
4487 {
4488 /* If we don't need this section, strip it from the
4489 output file. This is mostly to handle .rela.bss and
4490 .rela.plt. We must create both sections in
4491 create_dynamic_sections, because they must be created
4492 before the linker maps input sections to output
4493 sections. The linker does that before
4494 adjust_dynamic_symbol is called, and it is that
4495 function which decides whether anything needs to go
4496 into these sections. */
4497 s->flags |= SEC_EXCLUDE;
4498 continue;
4499 }
4500
4501 /* Allocate memory for the section contents. We use bfd_zalloc
4502 here in case unused entries are not reclaimed before the
4503 section's contents are written out. This should not happen,
4504 but this way if it does, we get a R_NDS32_NONE reloc instead
4505 of garbage. */
4506 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4507 if (s->contents == NULL)
4508 return FALSE;
4509 }
4510
4511
4512 if (htab->root.dynamic_sections_created)
4513 {
4514 /* Add some entries to the .dynamic section. We fill in the
4515 values later, in nds32_elf_finish_dynamic_sections, but we
4516 must add the entries now so that we get the correct size for
4517 the .dynamic section. The DT_DEBUG entry is filled in by the
4518 dynamic linker and used by the debugger. */
4519 #define add_dynamic_entry(TAG, VAL) \
4520 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4521
4522 if (bfd_link_executable (info))
4523 {
4524 if (!add_dynamic_entry (DT_DEBUG, 0))
4525 return FALSE;
4526 }
4527
4528 if (elf_hash_table (info)->splt->size != 0)
4529 {
4530 if (!add_dynamic_entry (DT_PLTGOT, 0)
4531 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4532 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4533 || !add_dynamic_entry (DT_JMPREL, 0))
4534 return FALSE;
4535 }
4536
4537 if (htab->tls_desc_trampoline && plt)
4538 {
4539 if (htab->dt_tlsdesc_plt
4540 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4541 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4542 return FALSE;
4543 }
4544
4545 if (relocs)
4546 {
4547 if (!add_dynamic_entry (DT_RELA, 0)
4548 || !add_dynamic_entry (DT_RELASZ, 0)
4549 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4550 return FALSE;
4551
4552 /* If any dynamic relocs apply to a read-only section,
4553 then we need a DT_TEXTREL entry. */
4554 if ((info->flags & DF_TEXTREL) == 0)
4555 elf_link_hash_traverse (&htab->root, maybe_set_textrel,
4556 (void *) info);
4557
4558 if ((info->flags & DF_TEXTREL) != 0)
4559 {
4560 if (!add_dynamic_entry (DT_TEXTREL, 0))
4561 return FALSE;
4562 }
4563 }
4564 }
4565 #undef add_dynamic_entry
4566
4567 return TRUE;
4568 }
4569
4570 static bfd_reloc_status_type
4571 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4572 bfd_vma relocation, bfd_byte *location)
4573 {
4574 int size;
4575 bfd_vma x = 0;
4576 bfd_reloc_status_type flag;
4577 unsigned int rightshift = howto->rightshift;
4578 unsigned int bitpos = howto->bitpos;
4579
4580 /* If the size is negative, negate RELOCATION. This isn't very
4581 general. */
4582 if (howto->size < 0)
4583 relocation = -relocation;
4584
4585 /* Get the value we are going to relocate. */
4586 size = bfd_get_reloc_size (howto);
4587 switch (size)
4588 {
4589 default:
4590 abort ();
4591 break;
4592 case 0:
4593 return bfd_reloc_ok;
4594 case 2:
4595 x = bfd_getb16 (location);
4596 break;
4597 case 4:
4598 x = bfd_getb32 (location);
4599 break;
4600 }
4601
4602 /* Check for overflow. FIXME: We may drop bits during the addition
4603 which we don't check for. We must either check at every single
4604 operation, which would be tedious, or we must do the computations
4605 in a type larger than bfd_vma, which would be inefficient. */
4606 flag = bfd_reloc_ok;
4607 if (howto->complain_on_overflow != complain_overflow_dont)
4608 {
4609 bfd_vma addrmask, fieldmask, signmask, ss;
4610 bfd_vma a, b, sum;
4611
4612 /* Get the values to be added together. For signed and unsigned
4613 relocations, we assume that all values should be truncated to
4614 the size of an address. For bitfields, all the bits matter.
4615 See also bfd_check_overflow. */
4616 fieldmask = N_ONES (howto->bitsize);
4617 signmask = ~fieldmask;
4618 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4619 a = (relocation & addrmask) >> rightshift;
4620 b = (x & howto->src_mask & addrmask) >> bitpos;
4621
4622 switch (howto->complain_on_overflow)
4623 {
4624 case complain_overflow_signed:
4625 /* If any sign bits are set, all sign bits must be set.
4626 That is, A must be a valid negative address after
4627 shifting. */
4628 signmask = ~(fieldmask >> 1);
4629 /* Fall through. */
4630
4631 case complain_overflow_bitfield:
4632 /* Much like the signed check, but for a field one bit
4633 wider. We allow a bitfield to represent numbers in the
4634 range -2**n to 2**n-1, where n is the number of bits in the
4635 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4636 can't overflow, which is exactly what we want. */
4637 ss = a & signmask;
4638 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4639 flag = bfd_reloc_overflow;
4640
4641 /* We only need this next bit of code if the sign bit of B
4642 is below the sign bit of A. This would only happen if
4643 SRC_MASK had fewer bits than BITSIZE. Note that if
4644 SRC_MASK has more bits than BITSIZE, we can get into
4645 trouble; we would need to verify that B is in range, as
4646 we do for A above. */
4647 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4648 ss >>= bitpos;
4649
4650 /* Set all the bits above the sign bit. */
4651 b = (b ^ ss) - ss;
4652
4653 /* Now we can do the addition. */
4654 sum = a + b;
4655
4656 /* See if the result has the correct sign. Bits above the
4657 sign bit are junk now; ignore them. If the sum is
4658 positive, make sure we did not have all negative inputs;
4659 if the sum is negative, make sure we did not have all
4660 positive inputs. The test below looks only at the sign
4661 bits, and it really just
4662 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4663
4664 We mask with addrmask here to explicitly allow an address
4665 wrap-around. The Linux kernel relies on it, and it is
4666 the only way to write assembler code which can run when
4667 loaded at a location 0x80000000 away from the location at
4668 which it is linked. */
4669 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4670 flag = bfd_reloc_overflow;
4671
4672 break;
4673
4674 case complain_overflow_unsigned:
4675 /* Checking for an unsigned overflow is relatively easy:
4676 trim the addresses and add, and trim the result as well.
4677 Overflow is normally indicated when the result does not
4678 fit in the field. However, we also need to consider the
4679 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4680 input is 0x80000000, and bfd_vma is only 32 bits; then we
4681 will get sum == 0, but there is an overflow, since the
4682 inputs did not fit in the field. Instead of doing a
4683 separate test, we can check for this by or-ing in the
4684 operands when testing for the sum overflowing its final
4685 field. */
4686 sum = (a + b) & addrmask;
4687 if ((a | b | sum) & signmask)
4688 flag = bfd_reloc_overflow;
4689 break;
4690
4691 default:
4692 abort ();
4693 }
4694 }
4695
4696 /* Put RELOCATION in the right bits. */
4697 relocation >>= (bfd_vma) rightshift;
4698 relocation <<= (bfd_vma) bitpos;
4699
4700 /* Add RELOCATION to the right bits of X. */
4701 /* FIXME : 090616
4702 Because the relaxation may generate duplicate relocation at one address,
4703 an addition to immediate in the instruction may cause the relocation added
4704 several times.
4705 This bug should be fixed in assembler, but a check is also needed here. */
4706 if (howto->partial_inplace)
4707 x = ((x & ~howto->dst_mask)
4708 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4709 else
4710 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4711
4712
4713 /* Put the relocated value back in the object file. */
4714 switch (size)
4715 {
4716 default:
4717 case 0:
4718 case 1:
4719 case 8:
4720 abort ();
4721 break;
4722 case 2:
4723 bfd_putb16 (x, location);
4724 break;
4725 case 4:
4726 bfd_putb32 (x, location);
4727 break;
4728 }
4729
4730 return flag;
4731 }
4732
4733 static bfd_reloc_status_type
4734 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4735 asection *input_section, bfd_byte *contents,
4736 bfd_vma address, bfd_vma value, bfd_vma addend)
4737 {
4738 bfd_vma relocation;
4739
4740 /* Sanity check the address. */
4741 if (address > bfd_get_section_limit (input_bfd, input_section))
4742 return bfd_reloc_outofrange;
4743
4744 /* This function assumes that we are dealing with a basic relocation
4745 against a symbol. We want to compute the value of the symbol to
4746 relocate to. This is just VALUE, the value of the symbol, plus
4747 ADDEND, any addend associated with the reloc. */
4748 relocation = value + addend;
4749
4750 /* If the relocation is PC relative, we want to set RELOCATION to
4751 the distance between the symbol (currently in RELOCATION) and the
4752 location we are relocating. If pcrel_offset is FALSE we do not
4753 need to subtract out the offset of the location within the
4754 section (which is just ADDRESS). */
4755 if (howto->pc_relative)
4756 {
4757 relocation -= (input_section->output_section->vma
4758 + input_section->output_offset);
4759 if (howto->pcrel_offset)
4760 relocation -= address;
4761 }
4762
4763 return nds32_relocate_contents (howto, input_bfd, relocation,
4764 contents + address);
4765 }
4766
4767 static bfd_boolean
4768 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4769 const char *name,
4770 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4771 asection *input_sec,
4772 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4773 {
4774 const char *source;
4775 FILE *sym_ld_script = NULL;
4776 struct elf_nds32_link_hash_table *table;
4777
4778 table = nds32_elf_hash_table (info);
4779 sym_ld_script = table->sym_ld_script;
4780 if (!sym_ld_script)
4781 return TRUE;
4782
4783 if (!h || !name || *name == '\0')
4784 return TRUE;
4785
4786 if (input_sec->flags & SEC_EXCLUDE)
4787 return TRUE;
4788
4789 if (!check_start_export_sym)
4790 {
4791 fprintf (sym_ld_script, "SECTIONS\n{\n");
4792 check_start_export_sym = 1;
4793 }
4794
4795 if (h->root.type == bfd_link_hash_defined
4796 || h->root.type == bfd_link_hash_defweak)
4797 {
4798 if (!h->root.u.def.section->output_section)
4799 return TRUE;
4800
4801 if (bfd_is_const_section (input_sec))
4802 source = input_sec->name;
4803 else
4804 source = bfd_get_filename (input_sec->owner);
4805
4806 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4807 h->root.root.string,
4808 (long) (h->root.u.def.value
4809 + h->root.u.def.section->output_section->vma
4810 + h->root.u.def.section->output_offset), source);
4811 }
4812
4813 return TRUE;
4814 }
4815
4816 /* Relocate an NDS32/D ELF section.
4817 There is some attempt to make this function usable for many architectures,
4818 both for RELA and REL type relocs, if only to serve as a learning tool.
4819
4820 The RELOCATE_SECTION function is called by the new ELF backend linker
4821 to handle the relocations for a section.
4822
4823 The relocs are always passed as Rela structures; if the section
4824 actually uses Rel structures, the r_addend field will always be
4825 zero.
4826
4827 This function is responsible for adjust the section contents as
4828 necessary, and (if using Rela relocs and generating a
4829 relocatable output file) adjusting the reloc addend as
4830 necessary.
4831
4832 This function does not have to worry about setting the reloc
4833 address or the reloc symbol index.
4834
4835 LOCAL_SYMS is a pointer to the swapped in local symbols.
4836
4837 LOCAL_SECTIONS is an array giving the section in the input file
4838 corresponding to the st_shndx field of each local symbol.
4839
4840 The global hash table entry for the global symbols can be found
4841 via elf_sym_hashes (input_bfd).
4842
4843 When generating relocatable output, this function must handle
4844 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4845 going to be the section symbol corresponding to the output
4846 section, which means that the addend must be adjusted
4847 accordingly. */
4848
4849 /* Return the base VMA address which should be subtracted from real addresses
4850 when resolving @dtpoff relocation.
4851 This is PT_TLS segment p_vaddr. */
4852
4853 /* Return the relocation value for @tpoff relocation
4854 if STT_TLS virtual address is ADDRESS. */
4855
4856 /* Return the relocation value for @gottpoff relocation
4857 if STT_TLS virtual address is ADDRESS. */
4858
4859 static bfd_vma
4860 gottpoff (struct bfd_link_info *info, bfd_vma address)
4861 {
4862 bfd_vma tp_base;
4863 bfd_vma tp_offset;
4864
4865 /* If tls_sec is NULL, we should have signalled an error already. */
4866 if (elf_hash_table (info)->tls_sec == NULL)
4867 return 0;
4868
4869 tp_base = elf_hash_table (info)->tls_sec->vma;
4870 tp_offset = address - tp_base;
4871
4872 return tp_offset;
4873 }
4874
4875 static bfd_boolean
4876 patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4877 {
4878 /* TLS_GD/TLS_LD model #1
4879 46 00 00 00 sethi $r0,#0x0
4880 58 00 00 00 ori $r0,$r0,#0x0
4881 40 00 74 00 add $r0,$r0,$gp
4882 04 10 00 00 lwi $r1,[$r0+#0x0]
4883 4b e0 04 01 jral $lp,$r1 */
4884
4885 /* TLS_GD/TLS_LD model #2
4886 46 00 00 00 sethi $r0,#0x0
4887 58 00 00 00 ori $r0,$r0,#0x0
4888 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4889 40 00 74 00 add $r0,$r0,$gp
4890 4b e0 04 01 jral $lp,$r1 */
4891
4892 /* TLS_IE model (non-PIC)
4893 46 00 00 00 sethi $r0,#0x0
4894 04 00 00 00 lwi $r0,[$r0+#0x0]
4895 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4896
4897 /* TLS_IE model (PIC)
4898 46 00 00 00 sethi $r0,#0x0
4899 58 00 00 00 ori $r0,$r0,#0x0
4900 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4901 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4902
4903 /* TLS_GD_TO_IE model
4904 46 00 00 00 sethi $r0,#0x0
4905 58 00 00 00 ori $r0,$r0,#0x0
4906 40 00 74 00 add $r0,$rM,$gp
4907 04 00 00 01 lwi $r0,[$r0+#0x4]
4908 40 00 64 00 add $r0,$r0,$r25 */
4909
4910 bfd_boolean rz = FALSE;
4911
4912 typedef struct
4913 {
4914 uint32_t opcode;
4915 uint32_t mask;
4916 } pat_t;
4917
4918 uint32_t patch[3] =
4919 {
4920 0x40007400, /* add $r0,$rM,$gp */
4921 0x04000001, /* lwi $r0,[$r0+#0x4] */
4922 0x40006400, /* add $r0,$r0,$r25 */
4923 };
4924
4925 pat_t mode0[3] =
4926 {
4927 { 0x40000000, 0xfe0003ff },
4928 { 0x04000000, 0xfe000000 },
4929 { 0x4be00001, 0xffff83ff },
4930 };
4931
4932 pat_t mode1[3] =
4933 {
4934 { 0x38007402, 0xfe007fff },
4935 { 0x40007400, 0xfe007fff },
4936 { 0x4be00001, 0xffff83ff },
4937 };
4938
4939 unsigned char *p = contents + rel->r_offset;
4940
4941 uint32_t insn;
4942 uint32_t regidx = 0;
4943 insn = bfd_getb32 (p);
4944 if (INSN_SETHI == (0xfe0fffffu & insn))
4945 {
4946 regidx = 0x1f & (insn >> 20);
4947 p += 4;
4948 }
4949
4950 insn = bfd_getb32 (p);
4951 if (INSN_ORI == (0xfe007fffu & insn))
4952 {
4953 regidx = 0x1f & (insn >> 20);
4954 p += 4;
4955 }
4956
4957 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
4958 {
4959 /* already patched? */
4960 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4961 (patch[1] == bfd_getb32 (p + 4)))
4962 rz = TRUE;
4963 }
4964 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4965 {
4966 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4967 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4968 {
4969 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4970 bfd_putb32 (patch[1], p + 4);
4971 bfd_putb32 (patch[2], p + 8);
4972 rz = TRUE;
4973 }
4974 }
4975 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4976 {
4977 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4978 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
4979 {
4980 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4981 bfd_putb32 (patch[1], p + 4);
4982 bfd_putb32 (patch[2], p + 8);
4983 rz = TRUE;
4984 }
4985 }
4986
4987 if (!rz)
4988 {
4989 printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
4990 (int) rel->r_offset);
4991 BFD_ASSERT(0); /* Unsupported pattern. */
4992 }
4993
4994 return rz;
4995 }
4996
4997 static enum elf_nds32_tls_type
4998 get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
4999
5000 static unsigned int
5001 ones32 (register unsigned int x)
5002 {
5003 /* 32-bit recursive reduction using SWAR...
5004 but first step is mapping 2-bit values
5005 into sum of 2 1-bit values in sneaky way. */
5006 x -= ((x >> 1) & 0x55555555);
5007 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5008 x = (((x >> 4) + x) & 0x0f0f0f0f);
5009 x += (x >> 8);
5010 x += (x >> 16);
5011 return (x & 0x0000003f);
5012 }
5013
5014 #if !HAVE_FLS
5015 static unsigned int
5016 fls (register unsigned int x)
5017 {
5018 return ffs (x & (-x));
5019 }
5020 #endif /* !HAVE_FLS */
5021
5022 #define nds32_elf_local_tlsdesc_gotent(bfd) \
5023 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5024
5025 static bfd_boolean
5026 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
5027 struct bfd_link_info * info,
5028 bfd * input_bfd,
5029 asection * input_section,
5030 bfd_byte * contents,
5031 Elf_Internal_Rela * relocs,
5032 Elf_Internal_Sym * local_syms,
5033 asection ** local_sections)
5034 {
5035 Elf_Internal_Shdr *symtab_hdr;
5036 struct elf_link_hash_entry **sym_hashes;
5037 Elf_Internal_Rela *rel, *relend;
5038 bfd_boolean ret = TRUE; /* Assume success. */
5039 int align = 0;
5040 bfd_reloc_status_type r;
5041 const char *errmsg = NULL;
5042 bfd_vma gp;
5043 struct elf_link_hash_table *ehtab;
5044 struct elf_nds32_link_hash_table *htab;
5045 bfd *dynobj;
5046 bfd_vma *local_got_offsets;
5047 asection *sgot, *splt, *sreloc;
5048 bfd_vma high_address;
5049 struct elf_nds32_link_hash_table *table;
5050 int eliminate_gc_relocs;
5051 bfd_vma fpbase_addr;
5052
5053 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5054 sym_hashes = elf_sym_hashes (input_bfd);
5055 ehtab = elf_hash_table (info);
5056 htab = nds32_elf_hash_table (info);
5057 high_address = bfd_get_section_limit (input_bfd, input_section);
5058
5059 dynobj = htab->root.dynobj;
5060 local_got_offsets = elf_local_got_offsets (input_bfd);
5061
5062 sgot = ehtab->sgot;
5063 splt = ehtab->splt;
5064 sreloc = NULL;
5065
5066 rel = relocs;
5067 relend = relocs + input_section->reloc_count;
5068
5069 table = nds32_elf_hash_table (info);
5070 eliminate_gc_relocs = table->eliminate_gc_relocs;
5071
5072 /* By this time, we can adjust the value of _SDA_BASE_. */
5073 /* Explain _SDA_BASE_ */
5074 if ((!bfd_link_relocatable (info)))
5075 {
5076 is_SDA_BASE_set = 1;
5077 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5078 if (r != bfd_reloc_ok)
5079 return FALSE;
5080 }
5081
5082 /* Do TLS model conversion once at first. */
5083 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5084
5085 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5086 the fp value is set as gp, and it has be reverted for instruction
5087 setting fp. */
5088 fpbase_addr = elf_gp (output_bfd);
5089
5090 /* Deal with (dynamic) relocations. */
5091 for (rel = relocs; rel < relend; rel++)
5092 {
5093 enum elf_nds32_reloc_type r_type;
5094 reloc_howto_type *howto = NULL;
5095 unsigned long r_symndx;
5096 struct elf_link_hash_entry *h = NULL;
5097 Elf_Internal_Sym *sym = NULL;
5098 asection *sec;
5099 bfd_vma relocation;
5100 bfd_vma relocation_sym = 0xdeadbeef;
5101 Elf_Internal_Rela *lorel;
5102 bfd_vma off;
5103
5104 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5105 ensure it's zero (we use REL relocs, not RELA). Therefore this
5106 should be assigning zero to `addend', but for clarity we use
5107 `r_addend'. */
5108
5109 bfd_vma addend = rel->r_addend;
5110 bfd_vma offset = rel->r_offset;
5111
5112 r_type = ELF32_R_TYPE (rel->r_info);
5113 if (r_type >= R_NDS32_max)
5114 {
5115 /* xgettext:c-format */
5116 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5117 input_bfd, r_type);
5118 bfd_set_error (bfd_error_bad_value);
5119 ret = FALSE;
5120 continue;
5121 }
5122
5123 if (r_type == R_NDS32_GNU_VTENTRY
5124 || r_type == R_NDS32_GNU_VTINHERIT
5125 || r_type == R_NDS32_NONE
5126 || r_type == R_NDS32_RELA_GNU_VTENTRY
5127 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5128 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5129 || r_type == R_NDS32_DATA
5130 || r_type == R_NDS32_TRAN)
5131 continue;
5132
5133 /* If we enter the fp-as-gp region. Resolve the address
5134 of best fp-base. */
5135 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5136 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5137 {
5138 int dist;
5139
5140 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5141 dist = rel->r_addend >> 16;
5142 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5143 local_syms, symtab_hdr);
5144 }
5145 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5146 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5147 {
5148 fpbase_addr = elf_gp (output_bfd);
5149 }
5150
5151 /* Skip the relocations used for relaxation. */
5152 /* We have to update LONGCALL and LONGJUMP
5153 relocations when generating the relocatable files. */
5154 if (!bfd_link_relocatable (info)
5155 && (r_type >= R_NDS32_RELAX_ENTRY
5156 || (r_type >= R_NDS32_LONGCALL4
5157 && r_type <= R_NDS32_LONGJUMP7)))
5158 continue;
5159
5160 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5161 r_symndx = ELF32_R_SYM (rel->r_info);
5162
5163 /* This is a final link. */
5164 sym = NULL;
5165 sec = NULL;
5166 h = NULL;
5167
5168 if (r_symndx < symtab_hdr->sh_info)
5169 {
5170 /* Local symbol. */
5171 sym = local_syms + r_symndx;
5172 sec = local_sections[r_symndx];
5173
5174 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5175 addend = rel->r_addend;
5176
5177 /* keep symbol location for static TLS_IE GOT entry */
5178 relocation_sym = relocation;
5179 if (bfd_link_relocatable (info))
5180 {
5181 /* This is a relocatable link. We don't have to change
5182 anything, unless the reloc is against a section symbol,
5183 in which case we have to adjust according to where the
5184 section symbol winds up in the output section. */
5185 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5186 rel->r_addend += sec->output_offset + sym->st_value;
5187
5188 continue;
5189 }
5190 }
5191 else
5192 {
5193 /* External symbol. */
5194 if (bfd_link_relocatable (info))
5195 continue;
5196 bfd_boolean warned, ignored, unresolved_reloc;
5197 int symndx = r_symndx - symtab_hdr->sh_info;
5198
5199 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5200 r_symndx, symtab_hdr, sym_hashes, h, sec,
5201 relocation, unresolved_reloc, warned,
5202 ignored);
5203
5204 /* keep symbol location for static TLS_IE GOT entry */
5205 relocation_sym = relocation;
5206
5207 /* la $fp, _FP_BASE_ is per-function (region).
5208 Handle it specially. */
5209 switch ((int) r_type)
5210 {
5211 case R_NDS32_HI20_RELA:
5212 case R_NDS32_LO12S0_RELA:
5213 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5214 FP_BASE_NAME) == 0)
5215 {
5216 if (!bfd_link_pie (info))
5217 {
5218 _bfd_error_handler
5219 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5220 input_bfd);
5221 }
5222 relocation = fpbase_addr;
5223 }
5224 break;
5225 case R_NDS32_SDA19S0_RELA:
5226 case R_NDS32_SDA15S0_RELA:
5227 case R_NDS32_20_RELA:
5228 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5229 FP_BASE_NAME) == 0)
5230 {
5231 relocation = fpbase_addr;
5232 break;
5233 }
5234 }
5235 }
5236
5237 /* Sanity check the address. */
5238 if (offset > high_address)
5239 {
5240 r = bfd_reloc_outofrange;
5241 goto check_reloc;
5242 }
5243
5244 if (r_type >= R_NDS32_RELAX_ENTRY)
5245 continue;
5246
5247 switch ((int) r_type)
5248 {
5249 case R_NDS32_GOTOFF:
5250 /* Relocation is relative to the start of the global offset
5251 table (for ld24 rx, #uimm24), e.g. access at label+addend
5252
5253 ld24 rx. #label@GOTOFF + addend
5254 sub rx, r12. */
5255 case R_NDS32_GOTOFF_HI20:
5256 case R_NDS32_GOTOFF_LO12:
5257 case R_NDS32_GOTOFF_LO15:
5258 case R_NDS32_GOTOFF_LO19:
5259 BFD_ASSERT (sgot != NULL);
5260
5261 relocation -= elf_gp (output_bfd);
5262 break;
5263
5264 case R_NDS32_9_PLTREL:
5265 case R_NDS32_25_PLTREL:
5266 /* Relocation is to the entry for this symbol in the
5267 procedure linkage table. */
5268
5269 /* The native assembler will generate a 25_PLTREL reloc
5270 for a local symbol if you assemble a call from one
5271 section to another when using -K pic. */
5272 if (h == NULL)
5273 break;
5274
5275 if (h->forced_local)
5276 break;
5277
5278 /* We didn't make a PLT entry for this symbol. This
5279 happens when statically linking PIC code, or when
5280 using -Bsymbolic. */
5281 if (h->plt.offset == (bfd_vma) - 1)
5282 break;
5283
5284 relocation = (splt->output_section->vma
5285 + splt->output_offset + h->plt.offset);
5286 break;
5287
5288 case R_NDS32_PLT_GOTREL_HI20:
5289 case R_NDS32_PLT_GOTREL_LO12:
5290 case R_NDS32_PLT_GOTREL_LO15:
5291 case R_NDS32_PLT_GOTREL_LO19:
5292 case R_NDS32_PLT_GOTREL_LO20:
5293 if (h == NULL
5294 || h->forced_local
5295 || h->plt.offset == (bfd_vma) -1
5296 || (bfd_link_pie (info) && h->def_regular))
5297 {
5298 /* Maybe we should find better checking to optimize
5299 PIE PLT relocations. */
5300 /* We didn't make a PLT entry for this symbol. This
5301 happens when statically linking PIC code, or when
5302 using -Bsymbolic. */
5303 if (h)
5304 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
5305 relocation -= elf_gp (output_bfd);
5306 break;
5307 }
5308
5309 relocation = (splt->output_section->vma
5310 + splt->output_offset + h->plt.offset);
5311
5312 relocation -= elf_gp (output_bfd);
5313 break;
5314
5315 case R_NDS32_PLTREL_HI20:
5316 case R_NDS32_PLTREL_LO12:
5317
5318 /* Relocation is to the entry for this symbol in the
5319 procedure linkage table. */
5320
5321 /* The native assembler will generate a 25_PLTREL reloc
5322 for a local symbol if you assemble a call from one
5323 section to another when using -K pic. */
5324 if (h == NULL)
5325 break;
5326
5327 if (h->forced_local)
5328 break;
5329
5330 if (h->plt.offset == (bfd_vma) - 1)
5331 /* We didn't make a PLT entry for this symbol. This
5332 happens when statically linking PIC code, or when
5333 using -Bsymbolic. */
5334 break;
5335
5336 if (splt == NULL)
5337 break;
5338
5339 relocation = (splt->output_section->vma
5340 + splt->output_offset
5341 + h->plt.offset + 4)
5342 - (input_section->output_section->vma
5343 + input_section->output_offset
5344 + rel->r_offset);
5345
5346 break;
5347
5348 case R_NDS32_GOTPC20:
5349 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5350 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5351 relocation = elf_gp (output_bfd);
5352 break;
5353
5354 case R_NDS32_GOTPC_HI20:
5355 case R_NDS32_GOTPC_LO12:
5356 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5357 bl .+4
5358 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5359 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5360 or
5361 bl .+4
5362 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5363 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5364 relocation = elf_gp (output_bfd);
5365 relocation -= (input_section->output_section->vma
5366 + input_section->output_offset + rel->r_offset);
5367 break;
5368
5369 case R_NDS32_GOT20:
5370 /* Fall through. */
5371 case R_NDS32_GOT_HI20:
5372 case R_NDS32_GOT_LO12:
5373 case R_NDS32_GOT_LO15:
5374 case R_NDS32_GOT_LO19:
5375 /* Relocation is to the entry for this symbol in the global
5376 offset table. */
5377 BFD_ASSERT (sgot != NULL);
5378
5379 if (h != NULL)
5380 {
5381 /* External symbol */
5382 bfd_boolean dyn;
5383
5384 off = h->got.offset;
5385 BFD_ASSERT (off != (bfd_vma) - 1);
5386 dyn = htab->root.dynamic_sections_created;
5387 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5388 bfd_link_pic (info),
5389 h)
5390 || (bfd_link_pic (info)
5391 && (info->symbolic
5392 || h->dynindx == -1
5393 || h->forced_local) && h->def_regular))
5394 {
5395 /* This is actually a static link, or it is a
5396 -Bsymbolic link and the symbol is defined
5397 locally, or the symbol was forced to be local
5398 because of a version file. We must initialize
5399 this entry in the global offset table. Since the
5400 offset must always be a multiple of 4, we use the
5401 least significant bit to record whether we have
5402 initialized it already.
5403
5404 When doing a dynamic link, we create a .rela.got
5405 relocation entry to initialize the value. This
5406 is done in the finish_dynamic_symbol routine. */
5407 if ((off & 1) != 0) /* clear LSB */
5408 off &= ~1;
5409 else
5410 {
5411 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5412 h->got.offset |= 1;
5413 }
5414 }
5415 relocation = sgot->output_section->vma + sgot->output_offset + off
5416 - elf_gp (output_bfd);
5417 }
5418 else
5419 {
5420 /* Local symbol */
5421 bfd_byte *loc;
5422
5423 BFD_ASSERT (local_got_offsets != NULL
5424 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5425
5426 off = local_got_offsets[r_symndx];
5427
5428 /* The offset must always be a multiple of 4. We use
5429 the least significant bit to record whether we have
5430 already processed this entry. */
5431 if ((off & 1) != 0) /* clear LSB */
5432 off &= ~1;
5433 else
5434 {
5435 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5436
5437 if (bfd_link_pic (info))
5438 {
5439 asection *srelgot;
5440 Elf_Internal_Rela outrel;
5441
5442 /* We need to generate a R_NDS32_RELATIVE reloc
5443 for the dynamic linker. */
5444 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5445 BFD_ASSERT (srelgot != NULL);
5446
5447 outrel.r_offset = (elf_gp (output_bfd)
5448 + sgot->output_offset + off);
5449 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5450 outrel.r_addend = relocation;
5451 loc = srelgot->contents;
5452 loc +=
5453 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5454 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5455 ++srelgot->reloc_count;
5456 }
5457 local_got_offsets[r_symndx] |= 1;
5458 }
5459 relocation = sgot->output_section->vma + sgot->output_offset + off
5460 - elf_gp (output_bfd);
5461 }
5462
5463 break;
5464
5465 case R_NDS32_16_RELA:
5466 case R_NDS32_20_RELA:
5467 case R_NDS32_5_RELA:
5468 case R_NDS32_32_RELA:
5469 case R_NDS32_9_PCREL_RELA:
5470 case R_NDS32_WORD_9_PCREL_RELA:
5471 case R_NDS32_10_UPCREL_RELA:
5472 case R_NDS32_15_PCREL_RELA:
5473 case R_NDS32_17_PCREL_RELA:
5474 case R_NDS32_25_PCREL_RELA:
5475 case R_NDS32_HI20_RELA:
5476 case R_NDS32_LO12S3_RELA:
5477 case R_NDS32_LO12S2_RELA:
5478 case R_NDS32_LO12S2_DP_RELA:
5479 case R_NDS32_LO12S2_SP_RELA:
5480 case R_NDS32_LO12S1_RELA:
5481 case R_NDS32_LO12S0_RELA:
5482 case R_NDS32_LO12S0_ORI_RELA:
5483 if (bfd_link_pic (info) && r_symndx != 0
5484 && (input_section->flags & SEC_ALLOC) != 0
5485 && (eliminate_gc_relocs == 0
5486 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5487 && ((r_type != R_NDS32_9_PCREL_RELA
5488 && r_type != R_NDS32_WORD_9_PCREL_RELA
5489 && r_type != R_NDS32_10_UPCREL_RELA
5490 && r_type != R_NDS32_15_PCREL_RELA
5491 && r_type != R_NDS32_17_PCREL_RELA
5492 && r_type != R_NDS32_25_PCREL_RELA
5493 && !(r_type == R_NDS32_32_RELA
5494 && strcmp (input_section->name, ".eh_frame") == 0))
5495 || (h != NULL && h->dynindx != -1
5496 && (!info->symbolic || !h->def_regular))))
5497 {
5498 Elf_Internal_Rela outrel;
5499 bfd_boolean skip, relocate;
5500 bfd_byte *loc;
5501
5502 /* When generating a shared object, these relocations
5503 are copied into the output file to be resolved at run
5504 time. */
5505
5506 if (sreloc == NULL)
5507 {
5508 const char *name;
5509
5510 name = bfd_elf_string_from_elf_section
5511 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5512 elf_section_data (input_section)->rela.hdr->sh_name);
5513 if (name == NULL)
5514 return FALSE;
5515
5516 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5517 && strcmp (bfd_section_name (input_section),
5518 name + 5) == 0);
5519
5520 sreloc = bfd_get_section_by_name (dynobj, name);
5521 BFD_ASSERT (sreloc != NULL);
5522 }
5523
5524 skip = FALSE;
5525 relocate = FALSE;
5526
5527 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5528 info,
5529 input_section,
5530 rel->r_offset);
5531 if (outrel.r_offset == (bfd_vma) - 1)
5532 skip = TRUE;
5533 else if (outrel.r_offset == (bfd_vma) - 2)
5534 skip = TRUE, relocate = TRUE;
5535 outrel.r_offset += (input_section->output_section->vma
5536 + input_section->output_offset);
5537
5538 if (skip)
5539 memset (&outrel, 0, sizeof outrel);
5540 else if (r_type == R_NDS32_17_PCREL_RELA
5541 || r_type == R_NDS32_15_PCREL_RELA
5542 || r_type == R_NDS32_25_PCREL_RELA)
5543 {
5544 BFD_ASSERT (h != NULL && h->dynindx != -1);
5545 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5546 outrel.r_addend = rel->r_addend;
5547 }
5548 else
5549 {
5550 /* h->dynindx may be -1 if this symbol was marked to
5551 become local. */
5552 if (h == NULL
5553 || ((info->symbolic || h->dynindx == -1)
5554 && h->def_regular)
5555 || (bfd_link_pie (info) && h->def_regular))
5556 {
5557 relocate = TRUE;
5558 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5559 outrel.r_addend = relocation + rel->r_addend;
5560
5561 if (h)
5562 {
5563 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5564
5565 BFD_ASSERT (sgot != NULL);
5566 /* If we did not allocate got entry for the symbol,
5567 we can not fill the nonexistent got entry. */
5568 if (h->got.offset != (bfd_vma) -1
5569 && (h->got.offset & 1) == 0)
5570 {
5571 bfd_put_32 (output_bfd, outrel.r_addend,
5572 sgot->contents + h->got.offset);
5573 }
5574 }
5575 }
5576 else
5577 {
5578 if (h->dynindx == -1)
5579 {
5580 _bfd_error_handler
5581 (_("%pB: relocation %s against `%s' can not be used when "
5582 "making a shared object; recompile with -fPIC"),
5583 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5584 bfd_set_error (bfd_error_bad_value);
5585 return FALSE;
5586 }
5587
5588 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5589 outrel.r_addend = rel->r_addend;
5590 }
5591 }
5592
5593 loc = sreloc->contents;
5594 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5595 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5596 ++sreloc->reloc_count;
5597
5598 /* If this reloc is against an external symbol, we do
5599 not want to fiddle with the addend. Otherwise, we
5600 need to include the symbol value so that it becomes
5601 an addend for the dynamic reloc. */
5602 if (!relocate)
5603 continue;
5604 }
5605 break;
5606
5607 case R_NDS32_25_ABS_RELA:
5608 if (bfd_link_pic (info))
5609 {
5610 _bfd_error_handler
5611 (_("%pB: warning: %s unsupported in shared mode"),
5612 input_bfd, "R_NDS32_25_ABS_RELA");
5613 return FALSE;
5614 }
5615 break;
5616
5617 case R_NDS32_9_PCREL:
5618 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5619 contents, offset,
5620 sec, relocation, addend);
5621 goto check_reloc;
5622
5623 case R_NDS32_HI20:
5624 /* We allow an arbitrary number of HI20 relocs before the
5625 LO12 reloc. This permits gcc to emit the HI and LO relocs
5626 itself. */
5627 for (lorel = rel + 1;
5628 (lorel < relend
5629 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5630 continue;
5631 if (lorel < relend
5632 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5633 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5634 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5635 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5636 {
5637 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5638 contents, relocation + addend);
5639 r = bfd_reloc_ok;
5640 }
5641 else
5642 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5643 contents, offset, relocation,
5644 addend);
5645 goto check_reloc;
5646
5647 case R_NDS32_GOT17S2_RELA:
5648 case R_NDS32_GOT15S2_RELA:
5649 BFD_ASSERT (sgot != NULL);
5650
5651 if (h != NULL)
5652 {
5653 bfd_boolean dyn;
5654
5655 off = h->got.offset;
5656 BFD_ASSERT (off != (bfd_vma) - 1);
5657
5658 dyn = htab->root.dynamic_sections_created;
5659 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5660 (dyn, bfd_link_pic (info), h)
5661 || (bfd_link_pic (info)
5662 && (info->symbolic
5663 || h->dynindx == -1
5664 || h->forced_local)
5665 && h->def_regular))
5666 {
5667 /* This is actually a static link, or it is a
5668 -Bsymbolic link and the symbol is defined
5669 locally, or the symbol was forced to be local
5670 because of a version file. We must initialize
5671 this entry in the global offset table. Since the
5672 offset must always be a multiple of 4, we use the
5673 least significant bit to record whether we have
5674 initialized it already.
5675
5676 When doing a dynamic link, we create a .rela.got
5677 relocation entry to initialize the value. This
5678 is done in the finish_dynamic_symbol routine. */
5679 if ((off & 1) != 0)
5680 off &= ~1;
5681 else
5682 {
5683 bfd_put_32 (output_bfd, relocation,
5684 sgot->contents + off);
5685 h->got.offset |= 1;
5686 }
5687 }
5688 }
5689 else
5690 {
5691 bfd_byte *loc;
5692
5693 BFD_ASSERT (local_got_offsets != NULL
5694 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5695
5696 off = local_got_offsets[r_symndx];
5697
5698 /* The offset must always be a multiple of 4. We use
5699 the least significant bit to record whether we have
5700 already processed this entry. */
5701 if ((off & 1) != 0)
5702 off &= ~1;
5703 else
5704 {
5705 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5706
5707 if (bfd_link_pic (info))
5708 {
5709 asection *srelgot;
5710 Elf_Internal_Rela outrel;
5711
5712 /* We need to generate a R_NDS32_RELATIVE reloc
5713 for the dynamic linker. */
5714 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5715 BFD_ASSERT (srelgot != NULL);
5716
5717 outrel.r_offset = (elf_gp (output_bfd)
5718 + sgot->output_offset + off);
5719 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5720 outrel.r_addend = relocation;
5721 loc = srelgot->contents;
5722 loc +=
5723 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5724 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5725 ++srelgot->reloc_count;
5726 }
5727 local_got_offsets[r_symndx] |= 1;
5728 }
5729 }
5730 relocation = sgot->output_section->vma + sgot->output_offset + off
5731 - elf_gp (output_bfd);
5732
5733 if (relocation & align)
5734 {
5735 /* Incorrect alignment. */
5736 _bfd_error_handler
5737 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5738 ret = FALSE;
5739 r = bfd_reloc_dangerous;
5740 goto check_reloc;
5741 }
5742 break;
5743
5744 case R_NDS32_SDA16S3_RELA:
5745 case R_NDS32_SDA15S3_RELA:
5746 case R_NDS32_SDA15S3:
5747 align = 0x7;
5748 goto handle_sda;
5749
5750 case R_NDS32_SDA17S2_RELA:
5751 case R_NDS32_SDA15S2_RELA:
5752 case R_NDS32_SDA12S2_SP_RELA:
5753 case R_NDS32_SDA12S2_DP_RELA:
5754 case R_NDS32_SDA15S2:
5755 case R_NDS32_SDA_FP7U2_RELA:
5756 align = 0x3;
5757 goto handle_sda;
5758
5759 case R_NDS32_SDA18S1_RELA:
5760 case R_NDS32_SDA15S1_RELA:
5761 case R_NDS32_SDA15S1:
5762 align = 0x1;
5763 goto handle_sda;
5764
5765 case R_NDS32_SDA19S0_RELA:
5766 case R_NDS32_SDA15S0_RELA:
5767 case R_NDS32_SDA15S0:
5768 align = 0x0;
5769 handle_sda:
5770 BFD_ASSERT (sec != NULL);
5771
5772 /* If the symbol is in the abs section, the out_bfd will be null.
5773 This happens when the relocation has a symbol@GOTOFF. */
5774 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5775 if (r != bfd_reloc_ok)
5776 {
5777 _bfd_error_handler
5778 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5779 ret = FALSE;
5780 goto check_reloc;
5781 }
5782
5783 /* At this point `relocation' contains the object's
5784 address. */
5785 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5786 {
5787 relocation -= fpbase_addr;
5788 }
5789 else
5790 relocation -= gp;
5791 /* Now it contains the offset from _SDA_BASE_. */
5792
5793 /* Make sure alignment is correct. */
5794
5795 if (relocation & align)
5796 {
5797 /* Incorrect alignment. */
5798 _bfd_error_handler
5799 /* xgettext:c-format */
5800 (_("%pB(%pA): warning: unaligned small data access"
5801 " of type %d"),
5802 input_bfd, input_section, r_type);
5803 ret = FALSE;
5804 goto check_reloc;
5805 }
5806 break;
5807
5808 case R_NDS32_17IFC_PCREL_RELA:
5809 case R_NDS32_10IFCU_PCREL_RELA:
5810 /* Do nothing. */
5811 break;
5812
5813 case R_NDS32_TLS_LE_HI20:
5814 case R_NDS32_TLS_LE_LO12:
5815 case R_NDS32_TLS_LE_20:
5816 case R_NDS32_TLS_LE_15S0:
5817 case R_NDS32_TLS_LE_15S1:
5818 case R_NDS32_TLS_LE_15S2:
5819 /* We do not have garbage collection for got entries.
5820 Therefore, IE to LE may have one empty entry, and DESC to
5821 LE may have two. */
5822 if (elf_hash_table (info)->tls_sec != NULL)
5823 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5824 break;
5825
5826 case R_NDS32_TLS_IE_HI20:
5827 case R_NDS32_TLS_IE_LO12S2:
5828 case R_NDS32_TLS_DESC_HI20:
5829 case R_NDS32_TLS_DESC_LO12:
5830 case R_NDS32_TLS_IE_LO12:
5831 case R_NDS32_TLS_IEGP_HI20:
5832 case R_NDS32_TLS_IEGP_LO12:
5833 case R_NDS32_TLS_IEGP_LO12S2:
5834 {
5835 /* Relocation is to the entry for this symbol in the global
5836 offset table. */
5837 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5838 asection *srelgot;
5839 Elf_Internal_Rela outrel;
5840 bfd_byte *loc;
5841 int indx = 0;
5842
5843 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5844
5845 BFD_ASSERT (sgot != NULL);
5846 if (h != NULL)
5847 {
5848 bfd_boolean dyn;
5849
5850 off = h->got.offset;
5851 BFD_ASSERT (off != (bfd_vma) -1);
5852 dyn = htab->root.dynamic_sections_created;
5853 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5854 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5855 && (!bfd_link_pic (info)
5856 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5857 indx = h->dynindx;
5858 }
5859 else
5860 {
5861 BFD_ASSERT (local_got_offsets != NULL
5862 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5863 off = local_got_offsets[r_symndx];
5864 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5865 }
5866
5867 relocation = sgot->output_section->vma + sgot->output_offset + off;
5868
5869 if (1 < ones32 (tls_type))
5870 {
5871 eff_tls_type = 1 << (fls (tls_type) - 1);
5872 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5873
5874 /* TLS model X -> LE is not implement yet!
5875 workaround here! */
5876 if (eff_tls_type == GOT_TLS_LE)
5877 {
5878 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5879 }
5880 }
5881
5882 /* The offset must always be a multiple of 4. We use
5883 the least significant bit to record whether we have
5884 already processed this entry. */
5885 bfd_boolean need_relocs = FALSE;
5886 srelgot = ehtab->srelgot;
5887 if ((bfd_link_pic (info) || indx != 0)
5888 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5889 || h->root.type != bfd_link_hash_undefweak))
5890 {
5891 need_relocs = TRUE;
5892 BFD_ASSERT (srelgot != NULL);
5893 }
5894
5895 if (off & 1)
5896 {
5897 off &= ~1;
5898 relocation &= ~1;
5899
5900 if (eff_tls_type & GOT_TLS_DESC)
5901 {
5902 relocation -= elf_gp (output_bfd);
5903 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5904 {
5905 /* TLS model shall be converted. */
5906 BFD_ASSERT(0);
5907 }
5908 }
5909 else if (eff_tls_type & GOT_TLS_IEGP)
5910 {
5911 relocation -= elf_gp (output_bfd);
5912 }
5913 }
5914 else
5915 {
5916 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5917 {
5918 /* TLS model workaround shall be applied. */
5919 BFD_ASSERT(0);
5920 }
5921 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5922 {
5923 if (eff_tls_type & GOT_TLS_IEGP)
5924 relocation -= elf_gp(output_bfd);
5925
5926 if (need_relocs)
5927 {
5928 if (indx == 0)
5929 outrel.r_addend = gottpoff (info, relocation_sym);
5930 else
5931 outrel.r_addend = 0;
5932 outrel.r_offset = (sgot->output_section->vma
5933 + sgot->output_offset + off);
5934 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5935
5936 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5937 &outrel);
5938 }
5939 else
5940 {
5941 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5942 sgot->contents + off);
5943 }
5944 }
5945 else if (eff_tls_type & GOT_TLS_DESC)
5946 {
5947 relocation -= elf_gp (output_bfd);
5948 if (need_relocs)
5949 {
5950 if (indx == 0)
5951 outrel.r_addend = gottpoff (info, relocation_sym);
5952 else
5953 outrel.r_addend = 0;
5954 outrel.r_offset = (sgot->output_section->vma
5955 + sgot->output_offset + off);
5956 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5957
5958 if (htab->tls_desc_trampoline)
5959 {
5960 asection *srelplt;
5961 srelplt = ehtab->srelplt;
5962 loc = srelplt->contents;
5963 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5964 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5965 <= srelplt->contents + srelplt->size);
5966
5967 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5968 }
5969 else
5970 {
5971 loc = srelgot->contents;
5972 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5973 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5974 ++srelgot->reloc_count;
5975 }
5976 }
5977 else
5978 {
5979 /* feed me! */
5980 bfd_put_32 (output_bfd, 0xdeadbeef,
5981 sgot->contents + off);
5982 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5983 sgot->contents + off + 4);
5984 patch_tls_desc_to_ie (contents, rel, input_bfd);
5985 BFD_ASSERT(0);
5986 }
5987 }
5988 else
5989 {
5990 /* TLS model workaround shall be applied. */
5991 BFD_ASSERT(0);
5992 }
5993
5994 if (h != NULL)
5995 h->got.offset |= 1;
5996 else
5997 local_got_offsets[r_symndx] |= 1;
5998 }
5999 }
6000 break;
6001 /* DON'T fall through. */
6002
6003 default:
6004 /* OLD_NDS32_RELOC. */
6005
6006 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6007 contents, offset, relocation, addend);
6008 goto check_reloc;
6009 }
6010
6011 switch ((int) r_type)
6012 {
6013 case R_NDS32_20_RELA:
6014 case R_NDS32_5_RELA:
6015 case R_NDS32_9_PCREL_RELA:
6016 case R_NDS32_WORD_9_PCREL_RELA:
6017 case R_NDS32_10_UPCREL_RELA:
6018 case R_NDS32_15_PCREL_RELA:
6019 case R_NDS32_17_PCREL_RELA:
6020 case R_NDS32_25_PCREL_RELA:
6021 case R_NDS32_25_ABS_RELA:
6022 case R_NDS32_HI20_RELA:
6023 case R_NDS32_LO12S3_RELA:
6024 case R_NDS32_LO12S2_RELA:
6025 case R_NDS32_LO12S2_DP_RELA:
6026 case R_NDS32_LO12S2_SP_RELA:
6027 case R_NDS32_LO12S1_RELA:
6028 case R_NDS32_LO12S0_RELA:
6029 case R_NDS32_LO12S0_ORI_RELA:
6030 case R_NDS32_SDA16S3_RELA:
6031 case R_NDS32_SDA17S2_RELA:
6032 case R_NDS32_SDA18S1_RELA:
6033 case R_NDS32_SDA19S0_RELA:
6034 case R_NDS32_SDA15S3_RELA:
6035 case R_NDS32_SDA15S2_RELA:
6036 case R_NDS32_SDA12S2_DP_RELA:
6037 case R_NDS32_SDA12S2_SP_RELA:
6038 case R_NDS32_SDA15S1_RELA:
6039 case R_NDS32_SDA15S0_RELA:
6040 case R_NDS32_SDA_FP7U2_RELA:
6041 case R_NDS32_9_PLTREL:
6042 case R_NDS32_25_PLTREL:
6043 case R_NDS32_GOT20:
6044 case R_NDS32_GOT_HI20:
6045 case R_NDS32_GOT_LO12:
6046 case R_NDS32_GOT_LO15:
6047 case R_NDS32_GOT_LO19:
6048 case R_NDS32_GOT15S2_RELA:
6049 case R_NDS32_GOT17S2_RELA:
6050 case R_NDS32_GOTPC20:
6051 case R_NDS32_GOTPC_HI20:
6052 case R_NDS32_GOTPC_LO12:
6053 case R_NDS32_GOTOFF:
6054 case R_NDS32_GOTOFF_HI20:
6055 case R_NDS32_GOTOFF_LO12:
6056 case R_NDS32_GOTOFF_LO15:
6057 case R_NDS32_GOTOFF_LO19:
6058 case R_NDS32_PLTREL_HI20:
6059 case R_NDS32_PLTREL_LO12:
6060 case R_NDS32_PLT_GOTREL_HI20:
6061 case R_NDS32_PLT_GOTREL_LO12:
6062 case R_NDS32_PLT_GOTREL_LO15:
6063 case R_NDS32_PLT_GOTREL_LO19:
6064 case R_NDS32_PLT_GOTREL_LO20:
6065 case R_NDS32_17IFC_PCREL_RELA:
6066 case R_NDS32_10IFCU_PCREL_RELA:
6067 case R_NDS32_TLS_LE_HI20:
6068 case R_NDS32_TLS_LE_LO12:
6069 case R_NDS32_TLS_IE_HI20:
6070 case R_NDS32_TLS_IE_LO12S2:
6071 case R_NDS32_TLS_LE_20:
6072 case R_NDS32_TLS_LE_15S0:
6073 case R_NDS32_TLS_LE_15S1:
6074 case R_NDS32_TLS_LE_15S2:
6075 case R_NDS32_TLS_DESC_HI20:
6076 case R_NDS32_TLS_DESC_LO12:
6077 case R_NDS32_TLS_IE_LO12:
6078 case R_NDS32_TLS_IEGP_HI20:
6079 case R_NDS32_TLS_IEGP_LO12:
6080 case R_NDS32_TLS_IEGP_LO12S2:
6081 /* Instruction related relocs must handle endian properly. */
6082 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
6083 r = nds32_elf_final_link_relocate (howto, input_bfd,
6084 input_section, contents,
6085 rel->r_offset, relocation,
6086 rel->r_addend);
6087 break;
6088
6089 default:
6090 /* All other relocs can use default handler. */
6091 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6092 contents, rel->r_offset,
6093 relocation, rel->r_addend);
6094 break;
6095 }
6096
6097 check_reloc:
6098
6099 if (r != bfd_reloc_ok)
6100 {
6101 /* FIXME: This should be generic enough to go in a utility. */
6102 const char *name;
6103
6104 if (h != NULL)
6105 name = h->root.root.string;
6106 else
6107 {
6108 name = bfd_elf_string_from_elf_section
6109 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6110 if (name == NULL || *name == '\0')
6111 name = bfd_section_name (sec);
6112 }
6113
6114 if (errmsg != NULL)
6115 goto common_error;
6116
6117 switch (r)
6118 {
6119 case bfd_reloc_overflow:
6120 (*info->callbacks->reloc_overflow)
6121 (info, (h ? &h->root : NULL), name, howto->name,
6122 (bfd_vma) 0, input_bfd, input_section, offset);
6123 break;
6124
6125 case bfd_reloc_undefined:
6126 (*info->callbacks->undefined_symbol)
6127 (info, name, input_bfd, input_section, offset, TRUE);
6128 break;
6129
6130 case bfd_reloc_outofrange:
6131 errmsg = _("internal error: out of range error");
6132 goto common_error;
6133
6134 case bfd_reloc_notsupported:
6135 errmsg = _("internal error: unsupported relocation error");
6136 goto common_error;
6137
6138 case bfd_reloc_dangerous:
6139 errmsg = _("internal error: dangerous error");
6140 goto common_error;
6141
6142 default:
6143 errmsg = _("internal error: unknown error");
6144 /* Fall through. */
6145
6146 common_error:
6147 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6148 input_section, offset);
6149 break;
6150 }
6151 }
6152 }
6153
6154 /* Resotre header size to avoid overflow load. */
6155 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6156 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6157
6158 return ret;
6159 }
6160
6161 /* Finish up dynamic symbol handling. We set the contents of various
6162 dynamic sections here. */
6163
6164 static bfd_boolean
6165 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6166 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6167 {
6168 struct elf_link_hash_table *ehtab;
6169 struct elf_nds32_link_hash_entry *hent;
6170 bfd_byte *loc;
6171
6172 ehtab = elf_hash_table (info);
6173 hent = (struct elf_nds32_link_hash_entry *) h;
6174
6175 if (h->plt.offset != (bfd_vma) - 1)
6176 {
6177 asection *splt;
6178 asection *sgot;
6179 asection *srela;
6180
6181 bfd_vma plt_index;
6182 bfd_vma got_offset;
6183 bfd_vma local_plt_offset;
6184 Elf_Internal_Rela rela;
6185
6186 /* This symbol has an entry in the procedure linkage table. Set
6187 it up. */
6188
6189 BFD_ASSERT (h->dynindx != -1);
6190
6191 splt = ehtab->splt;
6192 sgot = ehtab->sgotplt;
6193 srela = ehtab->srelplt;
6194 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6195
6196 /* Get the index in the procedure linkage table which
6197 corresponds to this symbol. This is the index of this symbol
6198 in all the symbols for which we are making plt entries. The
6199 first entry in the procedure linkage table is reserved. */
6200 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6201
6202 /* Get the offset into the .got table of the entry that
6203 corresponds to this function. Each .got entry is 4 bytes.
6204 The first three are reserved. */
6205 got_offset = (plt_index + 3) * 4;
6206
6207 /* Fill in the entry in the procedure linkage table. */
6208 if (!bfd_link_pic (info))
6209 {
6210 unsigned long insn;
6211
6212 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6213 + sgot->output_offset + got_offset) >> 12)
6214 & 0xfffff);
6215 bfd_putb32 (insn, splt->contents + h->plt.offset);
6216
6217 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6218 + sgot->output_offset + got_offset) & 0x0fff)
6219 >> 2);
6220 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6221
6222 insn = PLT_ENTRY_WORD2;
6223 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6224
6225 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6226 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6227
6228 insn = PLT_ENTRY_WORD4
6229 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6230 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6231 local_plt_offset = 12;
6232 }
6233 else
6234 {
6235 /* sda_base must be set at this time. */
6236 unsigned long insn;
6237 long offset;
6238
6239 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6240 - elf_gp (output_bfd);
6241 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6242 bfd_putb32 (insn, splt->contents + h->plt.offset);
6243
6244 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6245 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6246
6247 insn = PLT_PIC_ENTRY_WORD2;
6248 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6249
6250 insn = PLT_PIC_ENTRY_WORD3;
6251 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6252
6253 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6254 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6255
6256 insn = PLT_PIC_ENTRY_WORD5
6257 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6258 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6259
6260 local_plt_offset = 16;
6261 }
6262
6263 /* Fill in the entry in the global offset table,
6264 so it will fall through to the next instruction for the first time. */
6265 bfd_put_32 (output_bfd,
6266 (splt->output_section->vma + splt->output_offset
6267 + h->plt.offset + local_plt_offset),
6268 sgot->contents + got_offset);
6269
6270 /* Fill in the entry in the .rela.plt section. */
6271 rela.r_offset = (sgot->output_section->vma
6272 + sgot->output_offset + got_offset);
6273 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6274 rela.r_addend = 0;
6275 loc = srela->contents;
6276 loc += plt_index * sizeof (Elf32_External_Rela);
6277 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6278
6279 if (!h->def_regular)
6280 {
6281 /* Mark the symbol as undefined, rather than as defined in
6282 the .plt section. Leave the value alone. */
6283 sym->st_shndx = SHN_UNDEF;
6284 if (!h->ref_regular_nonweak)
6285 sym->st_value = 0;
6286 }
6287 }
6288
6289 if (h->got.offset != (bfd_vma) - 1
6290 && hent->tls_type == GOT_NORMAL)
6291 {
6292 asection *sgot;
6293 asection *srelagot;
6294 Elf_Internal_Rela rela;
6295
6296 /* This symbol has an entry in the global offset table.
6297 Set it up. */
6298
6299 sgot = ehtab->sgot;
6300 srelagot = ehtab->srelgot;
6301 BFD_ASSERT (sgot != NULL && srelagot != NULL);
6302
6303 rela.r_offset = (sgot->output_section->vma
6304 + sgot->output_offset + (h->got.offset & ~1));
6305
6306 /* If this is a -Bsymbolic link, and the symbol is defined
6307 locally, we just want to emit a RELATIVE reloc. Likewise if
6308 the symbol was forced to be local because of a version file.
6309 The entry in the global offset table will already have been
6310 initialized in the relocate_section function. */
6311 if ((bfd_link_pic (info)
6312 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6313 && h->def_regular)
6314 || (bfd_link_pie (info) && h->def_regular))
6315 {
6316 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6317 rela.r_addend = (h->root.u.def.value
6318 + h->root.u.def.section->output_section->vma
6319 + h->root.u.def.section->output_offset);
6320
6321 if ((h->got.offset & 1) == 0)
6322 {
6323 bfd_put_32 (output_bfd, rela.r_addend,
6324 sgot->contents + h->got.offset);
6325 }
6326 }
6327 else
6328 {
6329 BFD_ASSERT ((h->got.offset & 1) == 0);
6330 bfd_put_32 (output_bfd, (bfd_vma) 0,
6331 sgot->contents + h->got.offset);
6332 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6333 rela.r_addend = 0;
6334 }
6335
6336 loc = srelagot->contents;
6337 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6338 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6339 ++srelagot->reloc_count;
6340 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6341 }
6342
6343 if (h->needs_copy)
6344 {
6345 asection *s;
6346 Elf_Internal_Rela rela;
6347
6348 /* This symbols needs a copy reloc. Set it up. */
6349
6350 BFD_ASSERT (h->dynindx != -1
6351 && (h->root.type == bfd_link_hash_defined
6352 || h->root.type == bfd_link_hash_defweak));
6353
6354 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6355 BFD_ASSERT (s != NULL);
6356
6357 rela.r_offset = (h->root.u.def.value
6358 + h->root.u.def.section->output_section->vma
6359 + h->root.u.def.section->output_offset);
6360 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6361 rela.r_addend = 0;
6362 loc = s->contents;
6363 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6364 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6365 ++s->reloc_count;
6366 }
6367
6368 /* Mark some specially defined symbols as absolute. */
6369 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6370 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6371 sym->st_shndx = SHN_ABS;
6372
6373 return TRUE;
6374 }
6375
6376
6377 /* Finish up the dynamic sections. */
6378
6379 static bfd_boolean
6380 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6381 {
6382 bfd *dynobj;
6383 asection *sdyn;
6384 asection *sgotplt;
6385 struct elf_link_hash_table *ehtab;
6386 struct elf_nds32_link_hash_table *htab;
6387
6388 ehtab = elf_hash_table (info);
6389 htab = nds32_elf_hash_table (info);
6390 if (htab == NULL)
6391 return FALSE;
6392
6393 dynobj = elf_hash_table (info)->dynobj;
6394
6395 sgotplt = ehtab->sgotplt;
6396 /* A broken linker script might have discarded the dynamic sections.
6397 Catch this here so that we do not seg-fault later on. */
6398 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6399 return FALSE;
6400 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6401
6402 if (elf_hash_table (info)->dynamic_sections_created)
6403 {
6404 asection *splt;
6405 Elf32_External_Dyn *dyncon, *dynconend;
6406
6407 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6408
6409 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6410 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6411
6412 for (; dyncon < dynconend; dyncon++)
6413 {
6414 Elf_Internal_Dyn dyn;
6415 asection *s;
6416
6417 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6418
6419 switch (dyn.d_tag)
6420 {
6421 default:
6422 break;
6423
6424 case DT_PLTGOT:
6425 /* name = ".got"; */
6426 s = ehtab->sgot->output_section;
6427 goto get_vma;
6428 case DT_JMPREL:
6429 s = ehtab->srelplt->output_section;
6430 get_vma:
6431 BFD_ASSERT (s != NULL);
6432 dyn.d_un.d_ptr = s->vma;
6433 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6434 break;
6435
6436 case DT_PLTRELSZ:
6437 s = ehtab->srelplt->output_section;
6438 BFD_ASSERT (s != NULL);
6439 dyn.d_un.d_val = s->size;
6440 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6441 break;
6442
6443 case DT_RELASZ:
6444 /* My reading of the SVR4 ABI indicates that the
6445 procedure linkage table relocs (DT_JMPREL) should be
6446 included in the overall relocs (DT_RELA). This is
6447 what Solaris does. However, UnixWare can not handle
6448 that case. Therefore, we override the DT_RELASZ entry
6449 here to make it not include the JMPREL relocs. Since
6450 the linker script arranges for .rela.plt to follow all
6451 other relocation sections, we don't have to worry
6452 about changing the DT_RELA entry. */
6453 if (ehtab->srelplt != NULL)
6454 {
6455 s = ehtab->srelplt->output_section;
6456 dyn.d_un.d_val -= s->size;
6457 }
6458 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6459 break;
6460
6461 case DT_TLSDESC_PLT:
6462 s = htab->root.splt;
6463 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6464 + htab->dt_tlsdesc_plt);
6465 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6466 break;
6467
6468 case DT_TLSDESC_GOT:
6469 s = htab->root.sgot;
6470 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6471 + htab->dt_tlsdesc_got);
6472 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6473 break;
6474 }
6475 }
6476
6477 /* Fill in the first entry in the procedure linkage table. */
6478 splt = ehtab->splt;
6479 if (splt && splt->size > 0)
6480 {
6481 if (bfd_link_pic (info))
6482 {
6483 unsigned long insn;
6484 long offset;
6485
6486 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6487 - elf_gp (output_bfd);
6488 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6489 bfd_putb32 (insn, splt->contents);
6490
6491 /* here has a typo? */
6492 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6493 bfd_putb32 (insn, splt->contents + 4);
6494
6495 insn = PLT0_PIC_ENTRY_WORD2;
6496 bfd_putb32 (insn, splt->contents + 8);
6497
6498 insn = PLT0_PIC_ENTRY_WORD3;
6499 bfd_putb32 (insn, splt->contents + 12);
6500
6501 insn = PLT0_PIC_ENTRY_WORD4;
6502 bfd_putb32 (insn, splt->contents + 16);
6503
6504 insn = PLT0_PIC_ENTRY_WORD5;
6505 bfd_putb32 (insn, splt->contents + 20);
6506 }
6507 else
6508 {
6509 unsigned long insn;
6510 unsigned long addr;
6511
6512 /* addr = .got + 4 */
6513 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6514 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6515 bfd_putb32 (insn, splt->contents);
6516
6517 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6518 bfd_putb32 (insn, splt->contents + 4);
6519
6520 insn = PLT0_ENTRY_WORD2;
6521 bfd_putb32 (insn, splt->contents + 8);
6522
6523 insn = PLT0_ENTRY_WORD3;
6524 bfd_putb32 (insn, splt->contents + 12);
6525
6526 insn = PLT0_ENTRY_WORD4;
6527 bfd_putb32 (insn, splt->contents + 16);
6528 }
6529
6530 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6531 PLT_ENTRY_SIZE;
6532 }
6533
6534 if (htab->dt_tlsdesc_plt)
6535 {
6536 /* Calculate addresses. */
6537 asection *sgot = sgot = ehtab->sgot;
6538 bfd_vma pltgot = sgotplt->output_section->vma
6539 + sgotplt->output_offset;
6540 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6541 + htab->dt_tlsdesc_got;
6542
6543 /* Get GP offset. */
6544 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6545 tlsdesc_got -= elf_gp (output_bfd);
6546
6547 /* Do relocation. */
6548 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6549 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6550 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6551 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6552
6553 /* Insert .plt. */
6554 nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6555 dl_tlsdesc_lazy_trampoline,
6556 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6557 }
6558 }
6559
6560 /* Fill in the first three entries in the global offset table. */
6561 if (sgotplt && sgotplt->size > 0)
6562 {
6563 if (sdyn == NULL)
6564 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6565 else
6566 bfd_put_32 (output_bfd,
6567 sdyn->output_section->vma + sdyn->output_offset,
6568 sgotplt->contents);
6569 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6570 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6571
6572 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6573 }
6574
6575 return TRUE;
6576 }
6577 \f
6578
6579 /* Set the right machine number. */
6580
6581 static bfd_boolean
6582 nds32_elf_object_p (bfd *abfd)
6583 {
6584 static unsigned int cur_arch = 0;
6585
6586 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6587 {
6588 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6589 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6590 }
6591
6592 switch (cur_arch)
6593 {
6594 default:
6595 case E_N1_ARCH:
6596 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6597 break;
6598 case E_N1H_ARCH:
6599 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6600 break;
6601 case E_NDS_ARCH_STAR_V2_0:
6602 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6603 break;
6604 case E_NDS_ARCH_STAR_V3_0:
6605 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6606 break;
6607 case E_NDS_ARCH_STAR_V3_M:
6608 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6609 break;
6610 }
6611
6612 return TRUE;
6613 }
6614
6615 /* Store the machine number in the flags field. */
6616
6617 static bfd_boolean
6618 nds32_elf_final_write_processing (bfd *abfd)
6619 {
6620 unsigned long val;
6621 static unsigned int cur_mach = 0;
6622
6623 if (bfd_mach_n1 != bfd_get_mach (abfd))
6624 {
6625 cur_mach = bfd_get_mach (abfd);
6626 }
6627
6628 switch (cur_mach)
6629 {
6630 case bfd_mach_n1:
6631 /* Only happen when object is empty, since the case is abandon. */
6632 val = E_N1_ARCH;
6633 val |= E_NDS_ABI_AABI;
6634 val |= E_NDS32_ELF_VER_1_4;
6635 break;
6636 case bfd_mach_n1h:
6637 val = E_N1H_ARCH;
6638 break;
6639 case bfd_mach_n1h_v2:
6640 val = E_NDS_ARCH_STAR_V2_0;
6641 break;
6642 case bfd_mach_n1h_v3:
6643 val = E_NDS_ARCH_STAR_V3_0;
6644 break;
6645 case bfd_mach_n1h_v3m:
6646 val = E_NDS_ARCH_STAR_V3_M;
6647 break;
6648 default:
6649 val = 0;
6650 break;
6651 }
6652
6653 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6654 elf_elfheader (abfd)->e_flags |= val;
6655 return _bfd_elf_final_write_processing (abfd);
6656 }
6657
6658 /* Function to keep NDS32 specific file flags. */
6659
6660 static bfd_boolean
6661 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6662 {
6663 BFD_ASSERT (!elf_flags_init (abfd)
6664 || elf_elfheader (abfd)->e_flags == flags);
6665
6666 elf_elfheader (abfd)->e_flags = flags;
6667 elf_flags_init (abfd) = TRUE;
6668 return TRUE;
6669 }
6670
6671 static unsigned int
6672 convert_e_flags (unsigned int e_flags, unsigned int arch)
6673 {
6674 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6675 {
6676 /* From 0.9 to 1.0. */
6677 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6678
6679 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6680 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6681 if (arch == E_NDS_ARCH_STAR_V1_0)
6682 {
6683 /* Done. */
6684 return e_flags;
6685 }
6686 }
6687
6688 /* From 1.0 to 2.0. */
6689 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6690
6691 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6692 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6693
6694 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6695 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6696 return e_flags;
6697 }
6698
6699 static bfd_boolean
6700 nds32_check_vec_size (bfd *ibfd)
6701 {
6702 static unsigned int nds32_vec_size = 0;
6703
6704 asection *sec_t = NULL;
6705 bfd_byte *contents = NULL;
6706
6707 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6708
6709 if (sec_t && sec_t->size >= 4)
6710 {
6711 /* Get vec_size in file. */
6712 unsigned int flag_t;
6713
6714 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
6715 flag_t = bfd_get_32 (ibfd, contents);
6716
6717 /* The value could only be 4 or 16. */
6718
6719 if (!nds32_vec_size)
6720 /* Set if not set yet. */
6721 nds32_vec_size = (flag_t & 0x3);
6722 else if (nds32_vec_size != (flag_t & 0x3))
6723 {
6724 _bfd_error_handler
6725 /* xgettext:c-format */
6726 (_("%pB: ISR vector size mismatch"
6727 " with previous modules, previous %u-byte, current %u-byte"),
6728 ibfd,
6729 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6730 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6731 return FALSE;
6732 }
6733 else
6734 /* Only keep the first vec_size section. */
6735 sec_t->flags |= SEC_EXCLUDE;
6736 }
6737
6738 return TRUE;
6739 }
6740
6741 /* Merge backend specific data from an object file to the output
6742 object file when linking. */
6743
6744 static bfd_boolean
6745 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6746 {
6747 bfd *obfd = info->output_bfd;
6748 flagword out_flags;
6749 flagword in_flags;
6750 flagword out_16regs;
6751 flagword in_no_mac;
6752 flagword out_no_mac;
6753 flagword in_16regs;
6754 flagword out_version;
6755 flagword in_version;
6756 flagword out_fpu_config;
6757 flagword in_fpu_config;
6758
6759 /* FIXME: What should be checked when linking shared libraries? */
6760 if ((ibfd->flags & DYNAMIC) != 0)
6761 return TRUE;
6762
6763 /* TODO: Revise to use object-attributes instead. */
6764 if (!nds32_check_vec_size (ibfd))
6765 return FALSE;
6766
6767 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6768 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6769 return TRUE;
6770
6771 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6772 {
6773 _bfd_error_handler
6774 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6775
6776 bfd_set_error (bfd_error_bad_value);
6777 return FALSE;
6778 }
6779
6780 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6781 treat as generic one without checking and merging. */
6782 if (elf_elfheader (ibfd)->e_flags)
6783 {
6784 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6785 if (in_version == E_NDS32_ELF_VER_1_2)
6786 {
6787 _bfd_error_handler
6788 (_("%pB: warning: older version of object file encountered, "
6789 "please recompile with current tool chain"), ibfd);
6790 }
6791
6792 /* We may need to merge V1 and V2 arch object files to V2. */
6793 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6794 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6795 {
6796 /* Need to convert version. */
6797 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6798 == E_NDS_ARCH_STAR_RESERVED)
6799 {
6800 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6801 }
6802 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6803 == E_NDS_ARCH_STAR_V3_M
6804 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6805 == E_NDS_ARCH_STAR_V3_0)
6806 {
6807 elf_elfheader (ibfd)->e_flags =
6808 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6809 | E_NDS_ARCH_STAR_V3_0;
6810 }
6811 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6812 == E_NDS_ARCH_STAR_V0_9
6813 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6814 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6815 {
6816 elf_elfheader (obfd)->e_flags =
6817 convert_e_flags (elf_elfheader (obfd)->e_flags,
6818 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6819 }
6820 else
6821 {
6822 elf_elfheader (ibfd)->e_flags =
6823 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6824 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6825 }
6826 }
6827
6828 /* Extract some flags. */
6829 in_flags = elf_elfheader (ibfd)->e_flags
6830 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6831 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6832
6833 /* The following flags need special treatment. */
6834 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6835 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6836 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6837
6838 /* Extract some flags. */
6839 out_flags = elf_elfheader (obfd)->e_flags
6840 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6841 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6842
6843 /* The following flags need special treatment. */
6844 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6845 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6846 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6847 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6848 if (!elf_flags_init (obfd))
6849 {
6850 /* If the input is the default architecture then do not
6851 bother setting the flags for the output architecture,
6852 instead allow future merges to do this. If no future
6853 merges ever set these flags then they will retain their
6854 unitialised values, which surprise surprise, correspond
6855 to the default values. */
6856 if (bfd_get_arch_info (ibfd)->the_default)
6857 return TRUE;
6858
6859 elf_flags_init (obfd) = TRUE;
6860 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6861
6862 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6863 && bfd_get_arch_info (obfd)->the_default)
6864 {
6865 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6866 bfd_get_mach (ibfd));
6867 }
6868
6869 return TRUE;
6870 }
6871
6872 /* Check flag compatibility. */
6873 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6874 {
6875 _bfd_error_handler
6876 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6877 bfd_set_error (bfd_error_bad_value);
6878 return FALSE;
6879 }
6880
6881 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6882 {
6883 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6884 {
6885 _bfd_error_handler
6886 (_("%pB: error: instruction set mismatch with previous modules"),
6887 ibfd);
6888
6889 bfd_set_error (bfd_error_bad_value);
6890 return FALSE;
6891 }
6892 }
6893
6894 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6895 and perf ext1 and DIV are mergerd to perf ext1. */
6896 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6897 {
6898 elf_elfheader (obfd)->e_flags =
6899 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6900 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6901 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6902 ? E_NDS32_HAS_EXT_INST : 0)
6903 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6904 ? E_NDS32_HAS_EXT_INST : 0)
6905 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6906 | ((in_version > out_version) ? out_version : in_version);
6907 }
6908 else
6909 {
6910 if (in_version != out_version)
6911 _bfd_error_handler
6912 /* xgettext:c-format */
6913 (_("%pB: warning: incompatible elf-versions %s and %s"),
6914 ibfd, nds32_elfver_strtab[out_version],
6915 nds32_elfver_strtab[in_version]);
6916
6917 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6918 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6919 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6920 | (in_version > out_version ? out_version : in_version);
6921 }
6922 }
6923
6924 return TRUE;
6925 }
6926
6927 /* Display the flags field. */
6928
6929 static bfd_boolean
6930 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6931 {
6932 FILE *file = (FILE *) ptr;
6933
6934 BFD_ASSERT (abfd != NULL && ptr != NULL);
6935
6936 _bfd_elf_print_private_bfd_data (abfd, ptr);
6937
6938 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6939
6940 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6941 {
6942 default:
6943 case E_N1_ARCH:
6944 fprintf (file, _(": n1 instructions"));
6945 break;
6946 case E_N1H_ARCH:
6947 fprintf (file, _(": n1h instructions"));
6948 break;
6949 }
6950
6951 fputc ('\n', file);
6952
6953 return TRUE;
6954 }
6955
6956 static unsigned int
6957 nds32_elf_action_discarded (asection *sec)
6958 {
6959
6960 if (strncmp
6961 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6962 return 0;
6963
6964 return _bfd_elf_default_action_discarded (sec);
6965 }
6966
6967 static asection *
6968 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6969 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6970 Elf_Internal_Sym *sym)
6971 {
6972 if (h != NULL)
6973 switch (ELF32_R_TYPE (rel->r_info))
6974 {
6975 case R_NDS32_GNU_VTINHERIT:
6976 case R_NDS32_GNU_VTENTRY:
6977 case R_NDS32_RELA_GNU_VTINHERIT:
6978 case R_NDS32_RELA_GNU_VTENTRY:
6979 return NULL;
6980 }
6981
6982 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6983 }
6984
6985 static enum elf_nds32_tls_type
6986 get_tls_type (enum elf_nds32_reloc_type r_type,
6987 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6988 {
6989 enum elf_nds32_tls_type tls_type;
6990
6991 switch (r_type)
6992 {
6993 case R_NDS32_TLS_LE_HI20:
6994 case R_NDS32_TLS_LE_LO12:
6995 tls_type = GOT_TLS_LE;
6996 break;
6997 case R_NDS32_TLS_IE_HI20:
6998 case R_NDS32_TLS_IE_LO12S2:
6999 case R_NDS32_TLS_IE_LO12:
7000 tls_type = GOT_TLS_IE;
7001 break;
7002 case R_NDS32_TLS_IEGP_HI20:
7003 case R_NDS32_TLS_IEGP_LO12:
7004 case R_NDS32_TLS_IEGP_LO12S2:
7005 tls_type = GOT_TLS_IEGP;
7006 break;
7007 case R_NDS32_TLS_DESC_HI20:
7008 case R_NDS32_TLS_DESC_LO12:
7009 case R_NDS32_TLS_DESC_ADD:
7010 case R_NDS32_TLS_DESC_FUNC:
7011 case R_NDS32_TLS_DESC_CALL:
7012 tls_type = GOT_TLS_DESC;
7013 break;
7014 default:
7015 tls_type = GOT_NORMAL;
7016 break;
7017 }
7018
7019 return tls_type;
7020 }
7021
7022 /* Ensure that we have allocated bookkeeping structures for ABFD's local
7023 symbols. */
7024
7025 static bfd_boolean
7026 elf32_nds32_allocate_local_sym_info (bfd *abfd)
7027 {
7028 if (elf_local_got_refcounts (abfd) == NULL)
7029 {
7030 bfd_size_type num_syms;
7031 bfd_size_type size;
7032 char *data;
7033
7034 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7035 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7036 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7037 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7038 + sizeof (bfd_vma) + sizeof (int)
7039 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7040 data = bfd_zalloc (abfd, size);
7041 if (data == NULL)
7042 return FALSE;
7043
7044 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7045 data += num_syms * sizeof (bfd_signed_vma);
7046
7047 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7048 data += num_syms * sizeof (char);
7049
7050 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7051 data += num_syms * sizeof (bfd_vma);
7052
7053 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7054 data += num_syms * sizeof (int);
7055 }
7056
7057 return TRUE;
7058 }
7059
7060 /* Look through the relocs for a section during the first phase.
7061 Since we don't do .gots or .plts, we just need to consider the
7062 virtual table relocs for gc. */
7063
7064 static bfd_boolean
7065 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7066 asection *sec, const Elf_Internal_Rela *relocs)
7067 {
7068 Elf_Internal_Shdr *symtab_hdr;
7069 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7070 const Elf_Internal_Rela *rel;
7071 const Elf_Internal_Rela *rel_end;
7072 struct elf_link_hash_table *ehtab;
7073 struct elf_nds32_link_hash_table *htab;
7074 bfd *dynobj;
7075 asection *sreloc = NULL;
7076
7077 /* No need for relocation if relocatable already. */
7078 if (bfd_link_relocatable (info))
7079 {
7080 elf32_nds32_check_relax_group (abfd, sec);
7081 return TRUE;
7082 }
7083
7084 /* Don't do anything special with non-loaded, non-alloced sections.
7085 In particular, any relocs in such sections should not affect GOT
7086 and PLT reference counting (ie. we don't allow them to create GOT
7087 or PLT entries), there's no possibility or desire to optimize TLS
7088 relocs, and there's not much point in propagating relocs to shared
7089 libs that the dynamic linker won't relocate. */
7090 if ((sec->flags & SEC_ALLOC) == 0)
7091 return TRUE;
7092
7093 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7094 sym_hashes = elf_sym_hashes (abfd);
7095 sym_hashes_end =
7096 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7097 if (!elf_bad_symtab (abfd))
7098 sym_hashes_end -= symtab_hdr->sh_info;
7099
7100 ehtab = elf_hash_table (info);
7101 htab = nds32_elf_hash_table (info);
7102 dynobj = htab->root.dynobj;
7103
7104 rel_end = relocs + sec->reloc_count;
7105 for (rel = relocs; rel < rel_end; rel++)
7106 {
7107 enum elf_nds32_reloc_type r_type;
7108 struct elf_link_hash_entry *h;
7109 unsigned long r_symndx;
7110 enum elf_nds32_tls_type tls_type, old_tls_type;
7111
7112 r_symndx = ELF32_R_SYM (rel->r_info);
7113 r_type = ELF32_R_TYPE (rel->r_info);
7114 if (r_symndx < symtab_hdr->sh_info)
7115 h = NULL;
7116 else
7117 {
7118 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7119 while (h->root.type == bfd_link_hash_indirect
7120 || h->root.type == bfd_link_hash_warning)
7121 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7122 }
7123
7124 /* Create .got section if necessary.
7125 Some relocs require a global offset table. We create
7126 got section here, since these relocation need a got section
7127 and if it is not created yet. */
7128 if (ehtab->sgot == NULL)
7129 {
7130 switch (r_type)
7131 {
7132 case R_NDS32_GOT_HI20:
7133 case R_NDS32_GOT_LO12:
7134 case R_NDS32_GOT_LO15:
7135 case R_NDS32_GOT_LO19:
7136 case R_NDS32_GOT17S2_RELA:
7137 case R_NDS32_GOT15S2_RELA:
7138 case R_NDS32_GOTOFF:
7139 case R_NDS32_GOTOFF_HI20:
7140 case R_NDS32_GOTOFF_LO12:
7141 case R_NDS32_GOTOFF_LO15:
7142 case R_NDS32_GOTOFF_LO19:
7143 case R_NDS32_GOTPC20:
7144 case R_NDS32_GOTPC_HI20:
7145 case R_NDS32_GOTPC_LO12:
7146 case R_NDS32_GOT20:
7147 case R_NDS32_TLS_IE_HI20:
7148 case R_NDS32_TLS_IE_LO12:
7149 case R_NDS32_TLS_IE_LO12S2:
7150 case R_NDS32_TLS_IEGP_HI20:
7151 case R_NDS32_TLS_IEGP_LO12:
7152 case R_NDS32_TLS_IEGP_LO12S2:
7153 case R_NDS32_TLS_DESC_HI20:
7154 case R_NDS32_TLS_DESC_LO12:
7155 if (dynobj == NULL)
7156 htab->root.dynobj = dynobj = abfd;
7157 if (!create_got_section (dynobj, info))
7158 return FALSE;
7159 break;
7160
7161 default:
7162 break;
7163 }
7164 }
7165
7166 /* Check relocation type. */
7167 switch ((int) r_type)
7168 {
7169 case R_NDS32_GOT_HI20:
7170 case R_NDS32_GOT_LO12:
7171 case R_NDS32_GOT_LO15:
7172 case R_NDS32_GOT_LO19:
7173 case R_NDS32_GOT20:
7174 case R_NDS32_TLS_LE_HI20:
7175 case R_NDS32_TLS_LE_LO12:
7176 case R_NDS32_TLS_IE_HI20:
7177 case R_NDS32_TLS_IE_LO12:
7178 case R_NDS32_TLS_IE_LO12S2:
7179 case R_NDS32_TLS_IEGP_HI20:
7180 case R_NDS32_TLS_IEGP_LO12:
7181 case R_NDS32_TLS_IEGP_LO12S2:
7182 case R_NDS32_TLS_DESC_HI20:
7183 case R_NDS32_TLS_DESC_LO12:
7184 tls_type = get_tls_type (r_type, h);
7185 if (h)
7186 {
7187 if (tls_type != GOT_TLS_LE)
7188 h->got.refcount += 1;
7189 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7190 }
7191 else
7192 {
7193 /* This is a global offset table entry for a local symbol. */
7194 if (!elf32_nds32_allocate_local_sym_info (abfd))
7195 return FALSE;
7196
7197 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7198 if (tls_type != GOT_TLS_LE)
7199 elf_local_got_refcounts (abfd)[r_symndx] += 1;
7200 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7201 }
7202
7203 /* We would already have issued an error message if there
7204 is a TLS/non-TLS mismatch, based on the symbol
7205 type. So just combine any TLS types needed. */
7206 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7207 && tls_type != GOT_NORMAL)
7208 tls_type |= old_tls_type;
7209
7210 /* DESC to IE/IEGP if link to executable. */
7211 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7212 && (bfd_link_executable (info)))
7213 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7214
7215 if (old_tls_type != tls_type)
7216 {
7217 if (h != NULL)
7218 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7219 else
7220 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7221 }
7222 break;
7223 case R_NDS32_9_PLTREL:
7224 case R_NDS32_25_PLTREL:
7225 case R_NDS32_PLTREL_HI20:
7226 case R_NDS32_PLTREL_LO12:
7227 case R_NDS32_PLT_GOTREL_HI20:
7228 case R_NDS32_PLT_GOTREL_LO12:
7229 case R_NDS32_PLT_GOTREL_LO15:
7230 case R_NDS32_PLT_GOTREL_LO19:
7231 case R_NDS32_PLT_GOTREL_LO20:
7232
7233 /* This symbol requires a procedure linkage table entry. We
7234 actually build the entry in adjust_dynamic_symbol,
7235 because this might be a case of linking PIC code without
7236 linking in any dynamic objects, in which case we don't
7237 need to generate a procedure linkage table after all. */
7238
7239 /* If this is a local symbol, we resolve it directly without
7240 creating a procedure linkage table entry. */
7241 if (h == NULL)
7242 continue;
7243
7244 if (h->forced_local
7245 || (bfd_link_pie (info) && h->def_regular))
7246 break;
7247
7248 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7249 h->needs_plt = 1;
7250 h->plt.refcount += 1;
7251 break;
7252
7253 case R_NDS32_16_RELA:
7254 case R_NDS32_20_RELA:
7255 case R_NDS32_5_RELA:
7256 case R_NDS32_32_RELA:
7257 case R_NDS32_HI20_RELA:
7258 case R_NDS32_LO12S3_RELA:
7259 case R_NDS32_LO12S2_RELA:
7260 case R_NDS32_LO12S2_DP_RELA:
7261 case R_NDS32_LO12S2_SP_RELA:
7262 case R_NDS32_LO12S1_RELA:
7263 case R_NDS32_LO12S0_RELA:
7264 case R_NDS32_LO12S0_ORI_RELA:
7265 case R_NDS32_SDA16S3_RELA:
7266 case R_NDS32_SDA17S2_RELA:
7267 case R_NDS32_SDA18S1_RELA:
7268 case R_NDS32_SDA19S0_RELA:
7269 case R_NDS32_SDA15S3_RELA:
7270 case R_NDS32_SDA15S2_RELA:
7271 case R_NDS32_SDA12S2_DP_RELA:
7272 case R_NDS32_SDA12S2_SP_RELA:
7273 case R_NDS32_SDA15S1_RELA:
7274 case R_NDS32_SDA15S0_RELA:
7275 case R_NDS32_SDA_FP7U2_RELA:
7276 case R_NDS32_15_PCREL_RELA:
7277 case R_NDS32_17_PCREL_RELA:
7278 case R_NDS32_25_PCREL_RELA:
7279
7280 if (h != NULL && !bfd_link_pic (info))
7281 {
7282 h->non_got_ref = 1;
7283 h->plt.refcount += 1;
7284 }
7285
7286 /* If we are creating a shared library, and this is a reloc against
7287 a global symbol, or a non PC relative reloc against a local
7288 symbol, then we need to copy the reloc into the shared library.
7289 However, if we are linking with -Bsymbolic, we do not need to
7290 copy a reloc against a global symbol which is defined in an
7291 object we are including in the link (i.e., DEF_REGULAR is set).
7292 At this point we have not seen all the input files, so it is
7293 possible that DEF_REGULAR is not set now but will be set later
7294 (it is never cleared). We account for that possibility below by
7295 storing information in the dyn_relocs field of the hash table
7296 entry. A similar situation occurs when creating shared libraries
7297 and symbol visibility changes render the symbol local.
7298
7299 If on the other hand, we are creating an executable, we may need
7300 to keep relocations for symbols satisfied by a dynamic library
7301 if we manage to avoid copy relocs for the symbol. */
7302 if ((bfd_link_pic (info)
7303 && (sec->flags & SEC_ALLOC) != 0
7304 && ((r_type != R_NDS32_25_PCREL_RELA
7305 && r_type != R_NDS32_15_PCREL_RELA
7306 && r_type != R_NDS32_17_PCREL_RELA
7307 && !(r_type == R_NDS32_32_RELA
7308 && strcmp (sec->name, ".eh_frame") == 0))
7309 || (h != NULL
7310 && (!info->symbolic
7311 || h->root.type == bfd_link_hash_defweak
7312 || !h->def_regular))))
7313 || (!bfd_link_pic (info)
7314 && (sec->flags & SEC_ALLOC) != 0
7315 && h != NULL
7316 && (h->root.type == bfd_link_hash_defweak
7317 || !h->def_regular)))
7318 {
7319 struct elf_dyn_relocs *p;
7320 struct elf_dyn_relocs **head;
7321
7322 if (dynobj == NULL)
7323 htab->root.dynobj = dynobj = abfd;
7324
7325 /* When creating a shared object, we must copy these
7326 relocs into the output file. We create a reloc
7327 section in dynobj and make room for the reloc. */
7328 if (sreloc == NULL)
7329 {
7330 const char *name;
7331
7332 name = bfd_elf_string_from_elf_section
7333 (abfd, elf_elfheader (abfd)->e_shstrndx,
7334 elf_section_data (sec)->rela.hdr->sh_name);
7335 if (name == NULL)
7336 return FALSE;
7337
7338 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
7339 && strcmp (bfd_section_name (sec),
7340 name + 5) == 0);
7341
7342 sreloc = bfd_get_section_by_name (dynobj, name);
7343 if (sreloc == NULL)
7344 {
7345 flagword flags;
7346
7347 sreloc = bfd_make_section (dynobj, name);
7348 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7349 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7350 if ((sec->flags & SEC_ALLOC) != 0)
7351 flags |= SEC_ALLOC | SEC_LOAD;
7352 if (sreloc == NULL
7353 || !bfd_set_section_flags (sreloc, flags)
7354 || !bfd_set_section_alignment (sreloc, 2))
7355 return FALSE;
7356
7357 elf_section_type (sreloc) = SHT_RELA;
7358 }
7359 elf_section_data (sec)->sreloc = sreloc;
7360 }
7361
7362 /* If this is a global symbol, we count the number of
7363 relocations we need for this symbol. */
7364 if (h != NULL)
7365 head = &h->dyn_relocs;
7366 else
7367 {
7368 asection *s;
7369 void *vpp;
7370
7371 Elf_Internal_Sym *isym;
7372 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7373 if (isym == NULL)
7374 return FALSE;
7375
7376 /* Track dynamic relocs needed for local syms too. */
7377 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7378 if (s == NULL)
7379 return FALSE;
7380
7381 vpp = &elf_section_data (s)->local_dynrel;
7382 head = (struct elf_dyn_relocs **) vpp;
7383 }
7384
7385 p = *head;
7386 if (p == NULL || p->sec != sec)
7387 {
7388 size_t amt = sizeof (*p);
7389 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7390 if (p == NULL)
7391 return FALSE;
7392 p->next = *head;
7393 *head = p;
7394 p->sec = sec;
7395 p->count = 0;
7396 p->pc_count = 0;
7397 }
7398
7399 p->count += 1;
7400
7401 /* Since eh_frame is readonly, R_NDS32_32_RELA
7402 reloc for eh_frame will cause shared library has
7403 TEXTREL entry in the dynamic section. This lead glibc
7404 testsuites to failure (bug-13092) and cause kernel fail
7405 (bug-11819). I think the best solution is to replace
7406 absolute reloc with pc relative reloc in the eh_frame.
7407 To do that, we need to support the following issues:
7408
7409 === For GCC ===
7410 * gcc/config/nds32/nds32.h: Define
7411 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7412 and DW_EH_PE_sdata4 into DWARF exception header when
7413 option have '-fpic'.
7414
7415 === For binutils ===
7416 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7417 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7418 may break our nds DIFF mechanism, therefore, we
7419 must disable all linker relaxations to ensure
7420 correctness.
7421 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7422 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7423 do the necessary modification.
7424
7425 Unfortunately, it still have some problems for nds32
7426 to support pc relative reloc in the eh_frame. So I use
7427 another solution to fix this issue.
7428
7429 However, I find that ld always emit TEXTREL marker for
7430 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7431 correspond to R_NDS32_32_RELA for .eh_frame section.
7432 It means that we always reserve redundant entries of rel.dyn
7433 for these relocs which actually do nothing in dynamic linker.
7434
7435 Therefore, we regard these relocs as pc relative relocs
7436 here and increase the pc_count. */
7437 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7438 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7439 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7440 || (r_type == R_NDS32_32_RELA
7441 && strcmp (sec->name, ".eh_frame") == 0))
7442 p->pc_count += 1;
7443 }
7444 break;
7445
7446 /* This relocation describes the C++ object vtable hierarchy.
7447 Reconstruct it for later use during GC. */
7448 case R_NDS32_RELA_GNU_VTINHERIT:
7449 case R_NDS32_GNU_VTINHERIT:
7450 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7451 return FALSE;
7452 break;
7453
7454 /* This relocation describes which C++ vtable entries are actually
7455 used. Record for later use during GC. */
7456 case R_NDS32_GNU_VTENTRY:
7457 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7458 return FALSE;
7459 break;
7460 case R_NDS32_RELA_GNU_VTENTRY:
7461 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7462 return FALSE;
7463 break;
7464 }
7465 }
7466
7467 return TRUE;
7468 }
7469
7470 /* Write VAL in uleb128 format to P, returning a pointer to the
7471 following byte.
7472 This code is copied from elf-attr.c. */
7473
7474 static bfd_byte *
7475 write_uleb128 (bfd_byte *p, unsigned int val)
7476 {
7477 bfd_byte c;
7478 do
7479 {
7480 c = val & 0x7f;
7481 val >>= 7;
7482 if (val)
7483 c |= 0x80;
7484 *(p++) = c;
7485 }
7486 while (val);
7487 return p;
7488 }
7489
7490 static bfd_signed_vma
7491 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7492 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7493 {
7494 bfd_signed_vma foff;
7495 bfd_vma symval, addend;
7496 asection *sym_sec;
7497
7498 /* Get the value of the symbol referred to by the reloc. */
7499 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7500 {
7501 Elf_Internal_Sym *isym;
7502
7503 /* A local symbol. */
7504 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7505
7506 if (isym->st_shndx == SHN_UNDEF)
7507 sym_sec = bfd_und_section_ptr;
7508 else if (isym->st_shndx == SHN_ABS)
7509 sym_sec = bfd_abs_section_ptr;
7510 else if (isym->st_shndx == SHN_COMMON)
7511 sym_sec = bfd_com_section_ptr;
7512 else
7513 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7514 symval = isym->st_value + sym_sec->output_section->vma
7515 + sym_sec->output_offset;
7516 }
7517 else
7518 {
7519 unsigned long indx;
7520 struct elf_link_hash_entry *h;
7521
7522 /* An external symbol. */
7523 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7524 h = elf_sym_hashes (abfd)[indx];
7525 BFD_ASSERT (h != NULL);
7526
7527 if (h->root.type != bfd_link_hash_defined
7528 && h->root.type != bfd_link_hash_defweak)
7529 /* This appears to be a reference to an undefined
7530 symbol. Just ignore it--it will be caught by the
7531 regular reloc processing. */
7532 return 0;
7533
7534 if (h->root.u.def.section->flags & SEC_MERGE)
7535 {
7536 sym_sec = h->root.u.def.section;
7537 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7538 elf_section_data (sym_sec)->sec_info,
7539 h->root.u.def.value);
7540 symval = symval + sym_sec->output_section->vma
7541 + sym_sec->output_offset;
7542 }
7543 else
7544 symval = (h->root.u.def.value
7545 + h->root.u.def.section->output_section->vma
7546 + h->root.u.def.section->output_offset);
7547 }
7548
7549 addend = irel->r_addend;
7550
7551 foff = (symval + addend
7552 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7553 return foff;
7554 }
7555 \f
7556
7557 /* Convert a 32-bit instruction to 16-bit one.
7558 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7559 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7560 type of INSN16. Return 1 if successful. */
7561
7562 static int
7563 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7564 int *pinsn_type)
7565 {
7566 uint16_t insn16 = 0;
7567 int insn_type = 0;
7568 unsigned long mach = bfd_get_mach (abfd);
7569
7570 if (N32_SH5 (insn) != 0)
7571 return 0;
7572
7573 switch (N32_SUB5 (insn))
7574 {
7575 case N32_ALU1_ADD_SLLI:
7576 case N32_ALU1_ADD_SRLI:
7577 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7578 {
7579 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7580 N32_RB5 (insn));
7581 insn_type = NDS32_INSN_ADD333;
7582 }
7583 else if (N32_IS_RT4 (insn))
7584 {
7585 if (N32_RT5 (insn) == N32_RA5 (insn))
7586 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7587 else if (N32_RT5 (insn) == N32_RB5 (insn))
7588 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7589 insn_type = NDS32_INSN_ADD45;
7590 }
7591 break;
7592
7593 case N32_ALU1_SUB_SLLI:
7594 case N32_ALU1_SUB_SRLI:
7595 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7596 {
7597 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7598 N32_RB5 (insn));
7599 insn_type = NDS32_INSN_SUB333;
7600 }
7601 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7602 {
7603 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7604 insn_type = NDS32_INSN_SUB45;
7605 }
7606 break;
7607
7608 case N32_ALU1_AND_SLLI:
7609 case N32_ALU1_AND_SRLI:
7610 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7611 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7612 && N32_IS_RB3 (insn))
7613 {
7614 if (N32_RT5 (insn) == N32_RA5 (insn))
7615 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7616 else if (N32_RT5 (insn) == N32_RB5 (insn))
7617 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7618 if (insn16)
7619 insn_type = NDS32_INSN_AND33;
7620 }
7621 break;
7622
7623 case N32_ALU1_XOR_SLLI:
7624 case N32_ALU1_XOR_SRLI:
7625 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7626 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7627 && N32_IS_RB3 (insn))
7628 {
7629 if (N32_RT5 (insn) == N32_RA5 (insn))
7630 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7631 else if (N32_RT5 (insn) == N32_RB5 (insn))
7632 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7633 if (insn16)
7634 insn_type = NDS32_INSN_XOR33;
7635 }
7636 break;
7637
7638 case N32_ALU1_OR_SLLI:
7639 case N32_ALU1_OR_SRLI:
7640 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7641 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7642 && N32_IS_RB3 (insn))
7643 {
7644 if (N32_RT5 (insn) == N32_RA5 (insn))
7645 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7646 else if (N32_RT5 (insn) == N32_RB5 (insn))
7647 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7648 if (insn16)
7649 insn_type = NDS32_INSN_OR33;
7650 }
7651 break;
7652 case N32_ALU1_NOR:
7653 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7654 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7655 && N32_RA5 (insn) == N32_RB5 (insn))
7656 {
7657 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7658 insn_type = NDS32_INSN_NOT33;
7659 }
7660 break;
7661 case N32_ALU1_SRAI:
7662 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7663 {
7664 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7665 insn_type = NDS32_INSN_SRAI45;
7666 }
7667 break;
7668
7669 case N32_ALU1_SRLI:
7670 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7671 {
7672 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7673 insn_type = NDS32_INSN_SRLI45;
7674 }
7675 break;
7676
7677 case N32_ALU1_SLLI:
7678 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7679 {
7680 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7681 N32_UB5 (insn));
7682 insn_type = NDS32_INSN_SLLI333;
7683 }
7684 break;
7685
7686 case N32_ALU1_ZEH:
7687 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7688 {
7689 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7690 insn_type = NDS32_INSN_ZEH33;
7691 }
7692 break;
7693
7694 case N32_ALU1_SEB:
7695 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7696 {
7697 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7698 insn_type = NDS32_INSN_SEB33;
7699 }
7700 break;
7701
7702 case N32_ALU1_SEH:
7703 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7704 {
7705 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7706 insn_type = NDS32_INSN_SEH33;
7707 }
7708 break;
7709
7710 case N32_ALU1_SLT:
7711 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7712 {
7713 /* Implicit r15. */
7714 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7715 insn_type = NDS32_INSN_SLT45;
7716 }
7717 break;
7718
7719 case N32_ALU1_SLTS:
7720 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7721 {
7722 /* Implicit r15. */
7723 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7724 insn_type = NDS32_INSN_SLTS45;
7725 }
7726 break;
7727 }
7728
7729 if ((insn16 & 0x8000) == 0)
7730 return 0;
7731
7732 if (pinsn16)
7733 *pinsn16 = insn16;
7734 if (pinsn_type)
7735 *pinsn_type = insn_type;
7736 return 1;
7737 }
7738
7739 static int
7740 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7741 int *pinsn_type)
7742 {
7743 uint16_t insn16 = 0;
7744 int insn_type;
7745 unsigned long mach = bfd_get_mach (abfd);
7746
7747 /* TODO: bset, bclr, btgl, btst. */
7748 if (__GF (insn, 6, 4) != 0)
7749 return 0;
7750
7751 switch (N32_IMMU (insn, 6))
7752 {
7753 case N32_ALU2_MUL:
7754 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7755 && N32_IS_RB3 (insn))
7756 {
7757 if (N32_RT5 (insn) == N32_RA5 (insn))
7758 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7759 else if (N32_RT5 (insn) == N32_RB5 (insn))
7760 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7761 if (insn16)
7762 insn_type = NDS32_INSN_MUL33;
7763 }
7764 }
7765
7766 if ((insn16 & 0x8000) == 0)
7767 return 0;
7768
7769 if (pinsn16)
7770 *pinsn16 = insn16;
7771 if (pinsn_type)
7772 *pinsn_type = insn_type;
7773 return 1;
7774 }
7775
7776 int
7777 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7778 int *pinsn_type)
7779 {
7780 int op6;
7781 uint16_t insn16 = 0;
7782 int insn_type = 0;
7783 unsigned long mach = bfd_get_mach (abfd);
7784
7785 /* Decode 32-bit instruction. */
7786 if (insn & 0x80000000)
7787 {
7788 /* Not 32-bit insn. */
7789 return 0;
7790 }
7791
7792 op6 = N32_OP6 (insn);
7793
7794 /* Convert it to 16-bit instruction. */
7795 switch (op6)
7796 {
7797 case N32_OP6_MOVI:
7798 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7799 {
7800 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7801 insn_type = NDS32_INSN_MOVI55;
7802 }
7803 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7804 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7805 {
7806 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7807 N32_IMM20S (insn) - 16);
7808 insn_type = NDS32_INSN_MOVPI45;
7809 }
7810 break;
7811
7812 case N32_OP6_ADDI:
7813 if (N32_IMM15S (insn) == 0)
7814 {
7815 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7816 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7817 if (mach <= MACH_V2
7818 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7819 {
7820 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7821 insn_type = NDS32_INSN_MOV55;
7822 }
7823 }
7824 else if (N32_IMM15S (insn) > 0)
7825 {
7826 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7827 {
7828 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7829 N32_IMM15S (insn));
7830 insn_type = NDS32_INSN_ADDI333;
7831 }
7832 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7833 && N32_IMM15S (insn) < 32)
7834 {
7835 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7836 insn_type = NDS32_INSN_ADDI45;
7837 }
7838 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7839 && N32_RT5 (insn) == N32_RA5 (insn)
7840 && N32_IMM15S (insn) < 512)
7841 {
7842 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7843 insn_type = NDS32_INSN_ADDI10_SP;
7844 }
7845 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7846 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7847 && (N32_IMM15S (insn) % 4 == 0))
7848 {
7849 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7850 N32_IMM15S (insn) >> 2);
7851 insn_type = NDS32_INSN_ADDRI36_SP;
7852 }
7853 }
7854 else
7855 {
7856 /* Less than 0. */
7857 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7858 {
7859 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7860 0 - N32_IMM15S (insn));
7861 insn_type = NDS32_INSN_SUBI333;
7862 }
7863 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7864 && N32_IMM15S (insn) > -32)
7865 {
7866 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7867 0 - N32_IMM15S (insn));
7868 insn_type = NDS32_INSN_SUBI45;
7869 }
7870 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7871 && N32_RT5 (insn) == N32_RA5 (insn)
7872 && N32_IMM15S (insn) >= -512)
7873 {
7874 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7875 insn_type = NDS32_INSN_ADDI10_SP;
7876 }
7877 }
7878 break;
7879
7880 case N32_OP6_ORI:
7881 if (N32_IMM15S (insn) == 0)
7882 {
7883 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7884 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7885 if (mach <= MACH_V2
7886 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7887 {
7888 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7889 insn_type = NDS32_INSN_MOV55;
7890 }
7891 }
7892 break;
7893
7894 case N32_OP6_SUBRI:
7895 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7896 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7897 {
7898 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7899 insn_type = NDS32_INSN_NEG33;
7900 }
7901 break;
7902
7903 case N32_OP6_ANDI:
7904 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7905 {
7906 if (N32_IMM15U (insn) == 1)
7907 {
7908 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7909 insn_type = NDS32_INSN_XLSB33;
7910 }
7911 else if (N32_IMM15U (insn) == 0x7ff)
7912 {
7913 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7914 insn_type = NDS32_INSN_X11B33;
7915 }
7916 else if (N32_IMM15U (insn) == 0xff)
7917 {
7918 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7919 insn_type = NDS32_INSN_ZEB33;
7920 }
7921 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7922 && N32_IMM15U (insn) < 256)
7923 {
7924 int imm15u = N32_IMM15U (insn);
7925
7926 if (__builtin_popcount (imm15u) == 1)
7927 {
7928 /* BMSKI33 */
7929 int imm3u = __builtin_ctz (imm15u);
7930
7931 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7932 insn_type = NDS32_INSN_BMSKI33;
7933 }
7934 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7935 {
7936 /* FEXTI33 */
7937 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7938
7939 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7940 insn_type = NDS32_INSN_FEXTI33;
7941 }
7942 }
7943 }
7944 break;
7945
7946 case N32_OP6_SLTI:
7947 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7948 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7949 {
7950 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7951 insn_type = NDS32_INSN_SLTI45;
7952 }
7953 break;
7954
7955 case N32_OP6_SLTSI:
7956 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7957 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7958 {
7959 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7960 insn_type = NDS32_INSN_SLTSI45;
7961 }
7962 break;
7963
7964 case N32_OP6_LWI:
7965 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7966 {
7967 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7968 insn_type = NDS32_INSN_LWI450;
7969 }
7970 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7971 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7972 {
7973 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7974 N32_IMM15S (insn));
7975 insn_type = NDS32_INSN_LWI333;
7976 }
7977 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7978 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7979 {
7980 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7981 insn_type = NDS32_INSN_LWI37;
7982 }
7983 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7984 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7985 {
7986 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7987 insn_type = NDS32_INSN_LWI37_SP;
7988 }
7989 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7990 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7991 {
7992 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7993 N32_IMM15S (insn) + 32);
7994 insn_type = NDS32_INSN_LWI45_FE;
7995 }
7996 break;
7997
7998 case N32_OP6_SWI:
7999 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8000 {
8001 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8002 insn_type = NDS32_INSN_SWI450;
8003 }
8004 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8005 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8006 {
8007 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8008 N32_IMM15S (insn));
8009 insn_type = NDS32_INSN_SWI333;
8010 }
8011 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8012 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8013 {
8014 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8015 insn_type = NDS32_INSN_SWI37;
8016 }
8017 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8018 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8019 {
8020 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8021 insn_type = NDS32_INSN_SWI37_SP;
8022 }
8023 break;
8024
8025 case N32_OP6_LWI_BI:
8026 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8027 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8028 {
8029 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8030 N32_IMM15S (insn));
8031 insn_type = NDS32_INSN_LWI333_BI;
8032 }
8033 break;
8034
8035 case N32_OP6_SWI_BI:
8036 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8037 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8038 {
8039 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8040 N32_IMM15S (insn));
8041 insn_type = NDS32_INSN_SWI333_BI;
8042 }
8043 break;
8044
8045 case N32_OP6_LHI:
8046 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8047 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8048 {
8049 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8050 N32_IMM15S (insn));
8051 insn_type = NDS32_INSN_LHI333;
8052 }
8053 break;
8054
8055 case N32_OP6_SHI:
8056 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8057 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8058 {
8059 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8060 N32_IMM15S (insn));
8061 insn_type = NDS32_INSN_SHI333;
8062 }
8063 break;
8064
8065 case N32_OP6_LBI:
8066 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8067 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8068 {
8069 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8070 N32_IMM15S (insn));
8071 insn_type = NDS32_INSN_LBI333;
8072 }
8073 break;
8074
8075 case N32_OP6_SBI:
8076 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8077 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8078 {
8079 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8080 N32_IMM15S (insn));
8081 insn_type = NDS32_INSN_SBI333;
8082 }
8083 break;
8084
8085 case N32_OP6_ALU1:
8086 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8087
8088 case N32_OP6_ALU2:
8089 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8090
8091 case N32_OP6_BR1:
8092 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8093 goto done;
8094
8095 if ((insn & N32_BIT (14)) == 0)
8096 {
8097 /* N32_BR1_BEQ */
8098 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8099 && N32_RT5 (insn) != REG_R5)
8100 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8101 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8102 && N32_RA5 (insn) != REG_R5)
8103 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8104 insn_type = NDS32_INSN_BEQS38;
8105 break;
8106 }
8107 else
8108 {
8109 /* N32_BR1_BNE */
8110 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8111 && N32_RT5 (insn) != REG_R5)
8112 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8113 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8114 && N32_RA5 (insn) != REG_R5)
8115 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8116 insn_type = NDS32_INSN_BNES38;
8117 break;
8118 }
8119 break;
8120
8121 case N32_OP6_BR2:
8122 switch (N32_BR2_SUB (insn))
8123 {
8124 case N32_BR2_BEQZ:
8125 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8126 {
8127 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8128 insn_type = NDS32_INSN_BEQZ38;
8129 }
8130 else if (N32_RT5 (insn) == REG_R15
8131 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8132 {
8133 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8134 insn_type = NDS32_INSN_BEQZS8;
8135 }
8136 break;
8137
8138 case N32_BR2_BNEZ:
8139 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8140 {
8141 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8142 insn_type = NDS32_INSN_BNEZ38;
8143 }
8144 else if (N32_RT5 (insn) == REG_R15
8145 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8146 {
8147 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8148 insn_type = NDS32_INSN_BNEZS8;
8149 }
8150 break;
8151
8152 case N32_BR2_SOP0:
8153 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8154 {
8155 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8156 insn_type = NDS32_INSN_IFCALL9;
8157 }
8158 break;
8159 }
8160 break;
8161
8162 case N32_OP6_JI:
8163 if ((insn & N32_BIT (24)) == 0)
8164 {
8165 /* N32_JI_J */
8166 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8167 {
8168 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8169 insn_type = NDS32_INSN_J8;
8170 }
8171 }
8172 break;
8173
8174 case N32_OP6_JREG:
8175 if (__GF (insn, 8, 2) != 0)
8176 goto done;
8177
8178 switch (N32_IMMU (insn, 5))
8179 {
8180 case N32_JREG_JR:
8181 if (N32_JREG_HINT (insn) == 0)
8182 {
8183 /* jr */
8184 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8185 insn_type = NDS32_INSN_JR5;
8186 }
8187 else if (N32_JREG_HINT (insn) == 1)
8188 {
8189 /* ret */
8190 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8191 insn_type = NDS32_INSN_RET5;
8192 }
8193 else if (N32_JREG_HINT (insn) == 3)
8194 {
8195 /* ifret = mov55 $sp, $sp */
8196 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8197 insn_type = NDS32_INSN_IFRET;
8198 }
8199 break;
8200
8201 case N32_JREG_JRAL:
8202 /* It's convertible when return rt5 is $lp and address
8203 translation is kept. */
8204 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8205 {
8206 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8207 insn_type = NDS32_INSN_JRAL5;
8208 }
8209 break;
8210 }
8211 break;
8212
8213 case N32_OP6_MISC:
8214 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8215 {
8216 /* For v3, swid above 31 are used for ex9.it. */
8217 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8218 insn_type = NDS32_INSN_BREAK16;
8219 }
8220 break;
8221
8222 default:
8223 /* This instruction has no 16-bit variant. */
8224 goto done;
8225 }
8226
8227 done:
8228 /* Bit-15 of insn16 should be set for a valid instruction. */
8229 if ((insn16 & 0x8000) == 0)
8230 return 0;
8231
8232 if (pinsn16)
8233 *pinsn16 = insn16;
8234 if (pinsn_type)
8235 *pinsn_type = insn_type;
8236 return 1;
8237 }
8238
8239 static int
8240 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8241 Elf_Internal_Rela *reloc)
8242 {
8243 uint16_t insn16 = 0;
8244
8245 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8246 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8247 return 0;
8248
8249 if (!N32_IS_RT3 (insn))
8250 return 0;
8251
8252 switch (N32_OP6 (insn))
8253 {
8254 case N32_OP6_LWI:
8255 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8256 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8257 break;
8258 case N32_OP6_SWI:
8259 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8260 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8261 break;
8262 case N32_OP6_HWGP:
8263 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8264 break;
8265
8266 if (__GF (insn, 17, 3) == 6)
8267 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8268 else if (__GF (insn, 17, 3) == 7)
8269 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8270 break;
8271 }
8272
8273 if ((insn16 & 0x8000) == 0)
8274 return 0;
8275
8276 *pinsn16 = insn16;
8277 return 1;
8278 }
8279
8280 /* Convert a 16-bit instruction to 32-bit one.
8281 INSN16 it the input and PINSN it the point to output.
8282 Return non-zero on successful. Otherwise 0 is returned. */
8283
8284 int
8285 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8286 {
8287 uint32_t insn = 0xffffffff;
8288 unsigned long mach = bfd_get_mach (abfd);
8289
8290 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8291
8292 switch (__GF (insn16, 9, 6))
8293 {
8294 case 0x4: /* add45 */
8295 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8296 N16_RA5 (insn16));
8297 goto done;
8298 case 0x5: /* sub45 */
8299 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8300 N16_RA5 (insn16));
8301 goto done;
8302 case 0x6: /* addi45 */
8303 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8304 N16_IMM5U (insn16));
8305 goto done;
8306 case 0x7: /* subi45 */
8307 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8308 -N16_IMM5U (insn16));
8309 goto done;
8310 case 0x8: /* srai45 */
8311 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8312 N16_IMM5U (insn16));
8313 goto done;
8314 case 0x9: /* srli45 */
8315 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8316 N16_IMM5U (insn16));
8317 goto done;
8318 case 0xa: /* slli333 */
8319 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8320 N16_IMM3U (insn16));
8321 goto done;
8322 case 0xc: /* add333 */
8323 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8324 N16_RB3 (insn16));
8325 goto done;
8326 case 0xd: /* sub333 */
8327 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8328 N16_RB3 (insn16));
8329 goto done;
8330 case 0xe: /* addi333 */
8331 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8332 N16_IMM3U (insn16));
8333 goto done;
8334 case 0xf: /* subi333 */
8335 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8336 -N16_IMM3U (insn16));
8337 goto done;
8338 case 0x10: /* lwi333 */
8339 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8340 N16_IMM3U (insn16));
8341 goto done;
8342 case 0x12: /* lhi333 */
8343 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8344 N16_IMM3U (insn16));
8345 goto done;
8346 case 0x13: /* lbi333 */
8347 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8348 N16_IMM3U (insn16));
8349 goto done;
8350 case 0x11: /* lwi333.bi */
8351 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8352 N16_IMM3U (insn16));
8353 goto done;
8354 case 0x14: /* swi333 */
8355 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8356 N16_IMM3U (insn16));
8357 goto done;
8358 case 0x16: /* shi333 */
8359 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8360 N16_IMM3U (insn16));
8361 goto done;
8362 case 0x17: /* sbi333 */
8363 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8364 N16_IMM3U (insn16));
8365 goto done;
8366 case 0x15: /* swi333.bi */
8367 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8368 N16_IMM3U (insn16));
8369 goto done;
8370 case 0x18: /* addri36.sp */
8371 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8372 N16_IMM6U (insn16) << 2);
8373 goto done;
8374 case 0x19: /* lwi45.fe */
8375 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8376 (N16_IMM5U (insn16) - 32));
8377 goto done;
8378 case 0x1a: /* lwi450 */
8379 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8380 goto done;
8381 case 0x1b: /* swi450 */
8382 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8383 goto done;
8384
8385 /* These are r15 implied instructions. */
8386 case 0x30: /* slts45 */
8387 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8388 goto done;
8389 case 0x31: /* slt45 */
8390 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8391 goto done;
8392 case 0x32: /* sltsi45 */
8393 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8394 goto done;
8395 case 0x33: /* slti45 */
8396 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8397 goto done;
8398 case 0x34: /* beqzs8, bnezs8 */
8399 if (insn16 & N32_BIT (8))
8400 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8401 else
8402 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8403 goto done;
8404
8405 case 0x35: /* break16, ex9.it */
8406 /* Only consider range of v3 break16. */
8407 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8408 goto done;
8409
8410 case 0x3c: /* ifcall9 */
8411 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8412 goto done;
8413 case 0x3d: /* movpi45 */
8414 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8415 goto done;
8416
8417 case 0x3f: /* MISC33 */
8418 switch (insn16 & 0x7)
8419 {
8420 case 2: /* neg33 */
8421 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8422 break;
8423 case 3: /* not33 */
8424 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8425 N16_RA3 (insn16));
8426 break;
8427 case 4: /* mul33 */
8428 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8429 N16_RA3 (insn16));
8430 break;
8431 case 5: /* xor33 */
8432 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8433 N16_RA3 (insn16));
8434 break;
8435 case 6: /* and33 */
8436 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8437 N16_RA3 (insn16));
8438 break;
8439 case 7: /* or33 */
8440 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8441 N16_RA3 (insn16));
8442 break;
8443 }
8444 goto done;
8445
8446 case 0xb:
8447 switch (insn16 & 0x7)
8448 {
8449 case 0: /* zeb33 */
8450 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8451 break;
8452 case 1: /* zeh33 */
8453 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8454 break;
8455 case 2: /* seb33 */
8456 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8457 break;
8458 case 3: /* seh33 */
8459 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8460 break;
8461 case 4: /* xlsb33 */
8462 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8463 break;
8464 case 5: /* x11b33 */
8465 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8466 break;
8467 case 6: /* bmski33 */
8468 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8469 1 << __GF (insn16, 3, 3));
8470 break;
8471 case 7: /* fexti33 */
8472 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8473 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8474 break;
8475 }
8476 goto done;
8477 }
8478
8479 switch (__GF (insn16, 10, 5))
8480 {
8481 case 0x0: /* mov55 or ifret16 */
8482 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8483 && N16_RT5 (insn16) == N16_RA5 (insn16))
8484 insn = N32_JREG (JR, 0, 0, 0, 3);
8485 else
8486 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8487 goto done;
8488 case 0x1: /* movi55 */
8489 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8490 goto done;
8491 case 0x1b: /* addi10s (V2) */
8492 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8493 goto done;
8494 }
8495
8496 switch (__GF (insn16, 11, 4))
8497 {
8498 case 0x7: /* lwi37.fp/swi37.fp */
8499 if (insn16 & N32_BIT (7)) /* swi37.fp */
8500 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8501 else /* lwi37.fp */
8502 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8503 goto done;
8504 case 0x8: /* beqz38 */
8505 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8506 goto done;
8507 case 0x9: /* bnez38 */
8508 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8509 goto done;
8510 case 0xa: /* beqs38/j8, implied r5 */
8511 if (N16_RT38 (insn16) == 5)
8512 insn = N32_JI (J, N16_IMM8S (insn16));
8513 else
8514 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8515 goto done;
8516 case 0xb: /* bnes38 and others. */
8517 if (N16_RT38 (insn16) == 5)
8518 {
8519 switch (__GF (insn16, 5, 3))
8520 {
8521 case 0: /* jr5 */
8522 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8523 break;
8524 case 4: /* ret5 */
8525 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8526 break;
8527 case 1: /* jral5 */
8528 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8529 break;
8530 case 2: /* ex9.it imm5 */
8531 /* ex9.it had no 32-bit variantl. */
8532 break;
8533 case 5: /* add5.pc */
8534 /* add5.pc had no 32-bit variantl. */
8535 break;
8536 }
8537 }
8538 else /* bnes38 */
8539 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8540 goto done;
8541 case 0xe: /* lwi37/swi37 */
8542 if (insn16 & (1 << 7)) /* swi37.sp */
8543 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8544 else /* lwi37.sp */
8545 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8546 goto done;
8547 }
8548
8549 done:
8550 if (insn & 0x80000000)
8551 return 0;
8552
8553 if (pinsn)
8554 *pinsn = insn;
8555 return 1;
8556 }
8557 \f
8558
8559 static bfd_boolean
8560 is_sda_access_insn (unsigned long insn)
8561 {
8562 switch (N32_OP6 (insn))
8563 {
8564 case N32_OP6_LWI:
8565 case N32_OP6_LHI:
8566 case N32_OP6_LHSI:
8567 case N32_OP6_LBI:
8568 case N32_OP6_LBSI:
8569 case N32_OP6_SWI:
8570 case N32_OP6_SHI:
8571 case N32_OP6_SBI:
8572 case N32_OP6_LWC:
8573 case N32_OP6_LDC:
8574 case N32_OP6_SWC:
8575 case N32_OP6_SDC:
8576 return TRUE;
8577 default:
8578 ;
8579 }
8580 return FALSE;
8581 }
8582
8583 static unsigned long
8584 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8585 {
8586 uint32_t oinsn = 0;
8587
8588 switch (type)
8589 {
8590 case R_NDS32_GOT_LO12:
8591 case R_NDS32_GOTOFF_LO12:
8592 case R_NDS32_PLTREL_LO12:
8593 case R_NDS32_PLT_GOTREL_LO12:
8594 case R_NDS32_LO12S0_RELA:
8595 switch (N32_OP6 (insn))
8596 {
8597 case N32_OP6_LBI:
8598 /* lbi.gp */
8599 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8600 break;
8601 case N32_OP6_LBSI:
8602 /* lbsi.gp */
8603 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8604 break;
8605 case N32_OP6_SBI:
8606 /* sbi.gp */
8607 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8608 break;
8609 case N32_OP6_ORI:
8610 /* addi.gp */
8611 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8612 break;
8613 }
8614 break;
8615
8616 case R_NDS32_LO12S1_RELA:
8617 switch (N32_OP6 (insn))
8618 {
8619 case N32_OP6_LHI:
8620 /* lhi.gp */
8621 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8622 break;
8623 case N32_OP6_LHSI:
8624 /* lhsi.gp */
8625 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8626 break;
8627 case N32_OP6_SHI:
8628 /* shi.gp */
8629 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8630 break;
8631 }
8632 break;
8633
8634 case R_NDS32_LO12S2_RELA:
8635 switch (N32_OP6 (insn))
8636 {
8637 case N32_OP6_LWI:
8638 /* lwi.gp */
8639 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8640 break;
8641 case N32_OP6_SWI:
8642 /* swi.gp */
8643 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8644 break;
8645 }
8646 break;
8647
8648 case R_NDS32_LO12S2_DP_RELA:
8649 case R_NDS32_LO12S2_SP_RELA:
8650 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8651 break;
8652 }
8653
8654 if (oinsn)
8655 *pinsn = oinsn;
8656
8657 return oinsn != 0;
8658 }
8659
8660 /* Linker hasn't found the correct merge section for non-section symbol
8661 in relax time, this work is left to the function elf_link_input_bfd().
8662 So for non-section symbol, _bfd_merged_section_offset is also needed
8663 to find the correct symbol address. */
8664
8665 static bfd_vma
8666 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8667 asection **psec, Elf_Internal_Rela *rel)
8668 {
8669 asection *sec = *psec;
8670 bfd_vma relocation;
8671
8672 relocation = (sec->output_section->vma
8673 + sec->output_offset + sym->st_value);
8674 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8675 {
8676 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8677 rel->r_addend =
8678 _bfd_merged_section_offset (abfd, psec,
8679 elf_section_data (sec)->sec_info,
8680 sym->st_value + rel->r_addend);
8681 else
8682 rel->r_addend =
8683 _bfd_merged_section_offset (abfd, psec,
8684 elf_section_data (sec)->sec_info,
8685 sym->st_value) + rel->r_addend;
8686
8687 if (sec != *psec)
8688 {
8689 /* If we have changed the section, and our original section is
8690 marked with SEC_EXCLUDE, it means that the original
8691 SEC_MERGE section has been completely subsumed in some
8692 other SEC_MERGE section. In this case, we need to leave
8693 some info around for --emit-relocs. */
8694 if ((sec->flags & SEC_EXCLUDE) != 0)
8695 sec->kept_section = *psec;
8696 sec = *psec;
8697 }
8698 rel->r_addend -= relocation;
8699 rel->r_addend += sec->output_section->vma + sec->output_offset;
8700 }
8701 return relocation;
8702 }
8703
8704 static bfd_vma
8705 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8706 Elf_Internal_Sym *isymbuf,
8707 Elf_Internal_Shdr *symtab_hdr)
8708 {
8709 bfd_signed_vma foff;
8710 bfd_vma symval, addend;
8711 Elf_Internal_Rela irel_fn;
8712 Elf_Internal_Sym *isym;
8713 asection *sym_sec;
8714
8715 /* Get the value of the symbol referred to by the reloc. */
8716 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8717 {
8718 /* A local symbol. */
8719 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8720
8721 if (isym->st_shndx == SHN_UNDEF)
8722 sym_sec = bfd_und_section_ptr;
8723 else if (isym->st_shndx == SHN_ABS)
8724 sym_sec = bfd_abs_section_ptr;
8725 else if (isym->st_shndx == SHN_COMMON)
8726 sym_sec = bfd_com_section_ptr;
8727 else
8728 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8729 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8730 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8731 addend = irel_fn.r_addend;
8732 }
8733 else
8734 {
8735 unsigned long indx;
8736 struct elf_link_hash_entry *h;
8737
8738 /* An external symbol. */
8739 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8740 h = elf_sym_hashes (abfd)[indx];
8741 BFD_ASSERT (h != NULL);
8742
8743 while (h->root.type == bfd_link_hash_indirect
8744 || h->root.type == bfd_link_hash_warning)
8745 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8746
8747 if (h->root.type != bfd_link_hash_defined
8748 && h->root.type != bfd_link_hash_defweak)
8749 /* This appears to be a reference to an undefined
8750 symbol. Just ignore it--it will be caught by the
8751 regular reloc processing. */
8752 return 0;
8753
8754 if (h->root.u.def.section->flags & SEC_MERGE)
8755 {
8756 sym_sec = h->root.u.def.section;
8757 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8758 (sym_sec)->sec_info, h->root.u.def.value);
8759 symval = symval + sym_sec->output_section->vma
8760 + sym_sec->output_offset;
8761 }
8762 else
8763 symval = (h->root.u.def.value
8764 + h->root.u.def.section->output_section->vma
8765 + h->root.u.def.section->output_offset);
8766 addend = irel->r_addend;
8767 }
8768
8769 foff = symval + addend;
8770
8771 return foff;
8772 }
8773
8774 static int
8775 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8776 asection *sec, Elf_Internal_Rela *rel)
8777 {
8778 bfd_byte *contents;
8779 unsigned short insn16;
8780
8781 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8782 return FALSE;
8783 contents = elf_section_data (sec)->this_hdr.contents;
8784 insn16 = bfd_getb16 (contents + rel->r_offset);
8785 if (insn16 == NDS32_NOP16)
8786 return TRUE;
8787 return FALSE;
8788 }
8789
8790 /* It checks whether the instruction could be converted to
8791 16-bit form and returns the converted one.
8792
8793 `internal_relocs' is supposed to be sorted. */
8794
8795 static int
8796 is_convert_32_to_16 (bfd *abfd, asection *sec,
8797 Elf_Internal_Rela *reloc,
8798 Elf_Internal_Rela *internal_relocs,
8799 Elf_Internal_Rela *irelend,
8800 uint16_t *insn16)
8801 {
8802 #define NORMAL_32_TO_16 (1 << 0)
8803 #define SPECIAL_32_TO_16 (1 << 1)
8804 bfd_byte *contents = NULL;
8805 bfd_signed_vma off;
8806 bfd_vma mem_addr;
8807 uint32_t insn = 0;
8808 Elf_Internal_Rela *pc_rel;
8809 Elf_Internal_Shdr *symtab_hdr;
8810 Elf_Internal_Sym *isymbuf = NULL;
8811 int convert_type;
8812 bfd_vma offset;
8813
8814 if (reloc->r_offset + 4 > sec->size)
8815 return FALSE;
8816
8817 offset = reloc->r_offset;
8818
8819 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
8820 return FALSE;
8821 insn = bfd_getb32 (contents + offset);
8822
8823 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8824 convert_type = NORMAL_32_TO_16;
8825 else if (special_convert_32_to_16 (insn, insn16, reloc))
8826 convert_type = SPECIAL_32_TO_16;
8827 else
8828 return FALSE;
8829
8830 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8831 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8832 return FALSE;
8833
8834 /* Find the first relocation of the same relocation-type,
8835 so we iteratie them forward. */
8836 pc_rel = reloc;
8837 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8838 pc_rel--;
8839
8840 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8841 {
8842 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8843 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8844 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8845 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8846 {
8847 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8848 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8849 || off == 0)
8850 return FALSE;
8851 break;
8852 }
8853 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8854 {
8855 /* movi => movi55 */
8856 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8857 symtab_hdr);
8858 /* mem_addr is unsigned, but the value should
8859 be between [-16, 15]. */
8860 if ((mem_addr + 0x10) >> 5)
8861 return FALSE;
8862 break;
8863 }
8864 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8865 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8866 {
8867 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8868 because it can be relaxed to addi for TLS_LE_ADD. */
8869 return FALSE;
8870 }
8871 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8872 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8873 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8874 && convert_type == SPECIAL_32_TO_16)
8875 {
8876 /* fp-as-gp
8877 We've selected a best fp-base for this access, so we can
8878 always resolve it anyway. Do nothing. */
8879 break;
8880 }
8881 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8882 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8883 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8884 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8885 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8886 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8887 {
8888 /* Prevent unresolved addi instruction translate
8889 to addi45 or addi333. */
8890 return FALSE;
8891 }
8892 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8893 {
8894 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8895 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8896 return FALSE;
8897 break;
8898 }
8899 }
8900
8901 return TRUE;
8902 }
8903
8904 static void
8905 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8906 Elf_Internal_Rela *reloc,
8907 Elf_Internal_Rela *internal_relocs,
8908 Elf_Internal_Rela *irelend,
8909 unsigned short insn16)
8910 {
8911 Elf_Internal_Rela *pc_rel;
8912 bfd_vma offset;
8913
8914 offset = reloc->r_offset;
8915 bfd_putb16 (insn16, contents + offset);
8916 /* Find the first relocation of the same relocation-type,
8917 so we iteratie them forward. */
8918 pc_rel = reloc;
8919 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8920 pc_rel--;
8921
8922 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8923 {
8924 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8925 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8926 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8927 {
8928 pc_rel->r_info =
8929 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8930 }
8931 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8932 pc_rel->r_info =
8933 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8934 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8935 pc_rel->r_info =
8936 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8937 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8938 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8939 pc_rel->r_info =
8940 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8941 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8942 pc_rel->r_info =
8943 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8944 }
8945 }
8946
8947 /* Find a relocation of type specified by `reloc_type'
8948 of the same r_offset with reloc.
8949 If not found, return irelend.
8950
8951 Assuming relocations are sorted by r_offset,
8952 we find the relocation from `reloc' backward untill relocs,
8953 or find it from `reloc' forward untill irelend. */
8954
8955 static Elf_Internal_Rela *
8956 find_relocs_at_address (Elf_Internal_Rela *reloc,
8957 Elf_Internal_Rela *relocs,
8958 Elf_Internal_Rela *irelend,
8959 enum elf_nds32_reloc_type reloc_type)
8960 {
8961 Elf_Internal_Rela *rel_t;
8962
8963 /* Find backward. */
8964 for (rel_t = reloc;
8965 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8966 rel_t--)
8967 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8968 return rel_t;
8969
8970 /* We didn't find it backward. Try find it forward. */
8971 for (rel_t = reloc;
8972 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8973 rel_t++)
8974 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8975 return rel_t;
8976
8977 return irelend;
8978 }
8979
8980 /* Find a relocation of specified type and offset.
8981 `reloc' is just a refence point to find a relocation at specified offset.
8982 If not found, return irelend.
8983
8984 Assuming relocations are sorted by r_offset,
8985 we find the relocation from `reloc' backward untill relocs,
8986 or find it from `reloc' forward untill irelend. */
8987
8988 static Elf_Internal_Rela *
8989 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8990 Elf_Internal_Rela *relocs,
8991 Elf_Internal_Rela *irelend,
8992 enum elf_nds32_reloc_type reloc_type,
8993 bfd_vma offset_p)
8994 {
8995 Elf_Internal_Rela *rel_t = NULL;
8996
8997 /* First, we try to find a relocation of offset `offset_p',
8998 and then we use find_relocs_at_address to find specific type. */
8999
9000 if (reloc->r_offset > offset_p)
9001 {
9002 /* Find backward. */
9003 for (rel_t = reloc;
9004 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9005 /* Do nothing. */;
9006 }
9007 else if (reloc->r_offset < offset_p)
9008 {
9009 /* Find forward. */
9010 for (rel_t = reloc;
9011 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9012 /* Do nothing. */;
9013 }
9014 else
9015 rel_t = reloc;
9016
9017 /* Not found? */
9018 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9019 return irelend;
9020
9021 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9022 }
9023
9024 typedef struct nds32_elf_blank nds32_elf_blank_t;
9025 struct nds32_elf_blank
9026 {
9027 /* Where the blank begins. */
9028 bfd_vma offset;
9029 /* The size of the blank. */
9030 bfd_vma size;
9031 /* The accumulative size before this blank. */
9032 bfd_vma total_size;
9033 nds32_elf_blank_t *next;
9034 nds32_elf_blank_t *prev;
9035 };
9036
9037 static nds32_elf_blank_t *blank_free_list = NULL;
9038
9039 static nds32_elf_blank_t *
9040 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9041 {
9042 nds32_elf_blank_t *blank_t;
9043
9044 if (blank_free_list)
9045 {
9046 blank_t = blank_free_list;
9047 blank_free_list = blank_free_list->next;
9048 }
9049 else
9050 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9051
9052 if (blank_t == NULL)
9053 return NULL;
9054
9055 blank_t->offset = offset_p;
9056 blank_t->size = size_p;
9057 blank_t->total_size = 0;
9058 blank_t->next = NULL;
9059 blank_t->prev = NULL;
9060
9061 return blank_t;
9062 }
9063
9064 static void
9065 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9066 {
9067 if (blank_free_list)
9068 {
9069 blank_free_list->prev = blank_p;
9070 blank_p->next = blank_free_list;
9071 }
9072 else
9073 blank_p->next = NULL;
9074
9075 blank_p->prev = NULL;
9076 blank_free_list = blank_p;
9077 }
9078
9079 static void
9080 clean_nds32_elf_blank (void)
9081 {
9082 nds32_elf_blank_t *blank_t;
9083
9084 while (blank_free_list)
9085 {
9086 blank_t = blank_free_list;
9087 blank_free_list = blank_free_list->next;
9088 free (blank_t);
9089 }
9090 }
9091
9092 static nds32_elf_blank_t *
9093 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9094 {
9095 nds32_elf_blank_t *blank_t;
9096
9097 if (!blank_p)
9098 return NULL;
9099 blank_t = blank_p;
9100
9101 while (blank_t && addr < blank_t->offset)
9102 blank_t = blank_t->prev;
9103 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9104 blank_t = blank_t->next;
9105
9106 return blank_t;
9107 }
9108
9109 static bfd_vma
9110 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9111 int overwrite)
9112 {
9113 nds32_elf_blank_t *blank_t;
9114
9115 blank_t = search_nds32_elf_blank (*blank_p, addr);
9116 if (!blank_t)
9117 return 0;
9118
9119 if (overwrite)
9120 *blank_p = blank_t;
9121
9122 if (addr < blank_t->offset + blank_t->size)
9123 return blank_t->total_size + (addr - blank_t->offset);
9124 else
9125 return blank_t->total_size + blank_t->size;
9126 }
9127
9128 static bfd_boolean
9129 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9130 {
9131 nds32_elf_blank_t *blank_t, *blank_t2;
9132
9133 if (!*blank_p)
9134 {
9135 *blank_p = create_nds32_elf_blank (addr, len);
9136 return *blank_p ? TRUE : FALSE;
9137 }
9138
9139 blank_t = search_nds32_elf_blank (*blank_p, addr);
9140
9141 if (blank_t == NULL)
9142 {
9143 blank_t = create_nds32_elf_blank (addr, len);
9144 if (!blank_t)
9145 return FALSE;
9146 while ((*blank_p)->prev != NULL)
9147 *blank_p = (*blank_p)->prev;
9148 blank_t->next = *blank_p;
9149 (*blank_p)->prev = blank_t;
9150 (*blank_p) = blank_t;
9151 return TRUE;
9152 }
9153
9154 if (addr < blank_t->offset + blank_t->size)
9155 {
9156 /* Extend the origin blank. */
9157 if (addr + len > blank_t->offset + blank_t->size)
9158 blank_t->size = addr + len - blank_t->offset;
9159 }
9160 else
9161 {
9162 blank_t2 = create_nds32_elf_blank (addr, len);
9163 if (!blank_t2)
9164 return FALSE;
9165 if (blank_t->next)
9166 {
9167 blank_t->next->prev = blank_t2;
9168 blank_t2->next = blank_t->next;
9169 }
9170 blank_t2->prev = blank_t;
9171 blank_t->next = blank_t2;
9172 *blank_p = blank_t2;
9173 }
9174
9175 return TRUE;
9176 }
9177
9178 static bfd_boolean
9179 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9180 bfd_vma len)
9181 {
9182 nds32_elf_blank_t *blank_t;
9183
9184 if (!insert_nds32_elf_blank (blank_p, addr, len))
9185 return FALSE;
9186
9187 blank_t = *blank_p;
9188
9189 if (!blank_t->prev)
9190 {
9191 blank_t->total_size = 0;
9192 blank_t = blank_t->next;
9193 }
9194
9195 while (blank_t)
9196 {
9197 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9198 blank_t = blank_t->next;
9199 }
9200
9201 return TRUE;
9202 }
9203
9204 static void
9205 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9206 {
9207 nds32_elf_blank_t *blank_t;
9208 bfd_vma total_size = 0;
9209
9210 if (!blank_p)
9211 return;
9212
9213 blank_t = blank_p;
9214 while (blank_t->prev)
9215 blank_t = blank_t->prev;
9216 while (blank_t)
9217 {
9218 blank_t->total_size = total_size;
9219 total_size += blank_t->size;
9220 blank_t = blank_t->next;
9221 }
9222 }
9223
9224 static bfd_boolean
9225 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9226 nds32_elf_blank_t *blank_p)
9227 {
9228 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
9229 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
9230 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9231 unsigned int sec_shndx; /* The section the be relaxed. */
9232 bfd_byte *contents; /* Contents data of iterating section. */
9233 Elf_Internal_Rela *internal_relocs;
9234 Elf_Internal_Rela *irel;
9235 Elf_Internal_Rela *irelend;
9236 struct elf_link_hash_entry **sym_hashes;
9237 struct elf_link_hash_entry **end_hashes;
9238 unsigned int symcount;
9239 asection *sect;
9240 nds32_elf_blank_t *blank_t;
9241 nds32_elf_blank_t *blank_t2;
9242 nds32_elf_blank_t *blank_head;
9243
9244 blank_head = blank_t = blank_p;
9245 while (blank_head->prev != NULL)
9246 blank_head = blank_head->prev;
9247 while (blank_t->next != NULL)
9248 blank_t = blank_t->next;
9249
9250 if (blank_t->offset + blank_t->size <= sec->size)
9251 {
9252 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9253 blank_t->next->prev = blank_t;
9254 }
9255 if (blank_head->offset > 0)
9256 {
9257 blank_head->prev = create_nds32_elf_blank (0, 0);
9258 blank_head->prev->next = blank_head;
9259 blank_head = blank_head->prev;
9260 }
9261
9262 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9263
9264 /* The deletion must stop at the next ALIGN reloc for an alignment
9265 power larger than the number of bytes we are deleting. */
9266
9267 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9268 if (!nds32_get_local_syms (abfd, sec, &isym))
9269 return FALSE;
9270
9271 if (isym == NULL)
9272 {
9273 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9274 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9275 symtab_hdr->contents = (bfd_byte *) isym;
9276 }
9277
9278 if (isym == NULL || symtab_hdr->sh_info == 0)
9279 return FALSE;
9280
9281 blank_t = blank_head;
9282 calc_nds32_blank_total (blank_head);
9283
9284 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9285 {
9286 /* Adjust all the relocs. */
9287
9288 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9289 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9290 TRUE /* keep_memory */);
9291 irelend = internal_relocs + sect->reloc_count;
9292
9293 blank_t = blank_head;
9294 blank_t2 = blank_head;
9295
9296 if (!(sect->flags & SEC_RELOC))
9297 continue;
9298
9299 nds32_get_section_contents (abfd, sect, &contents, TRUE);
9300
9301 for (irel = internal_relocs; irel < irelend; irel++)
9302 {
9303 bfd_vma raddr;
9304
9305 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9306 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9307 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9308 {
9309 unsigned long val = 0;
9310 unsigned long mask;
9311 long before, between;
9312 long offset = 0;
9313
9314 switch (ELF32_R_TYPE (irel->r_info))
9315 {
9316 case R_NDS32_DIFF8:
9317 offset = bfd_get_8 (abfd, contents + irel->r_offset);
9318 break;
9319 case R_NDS32_DIFF16:
9320 offset = bfd_get_16 (abfd, contents + irel->r_offset);
9321 break;
9322 case R_NDS32_DIFF32:
9323 val = bfd_get_32 (abfd, contents + irel->r_offset);
9324 /* Get the signed bit and mask for the high part. The
9325 gcc will alarm when right shift 32-bit since the
9326 type size of long may be 32-bit. */
9327 mask = 0 - (val >> 31);
9328 if (mask)
9329 offset = (val | (mask - 0xffffffff));
9330 else
9331 offset = val;
9332 break;
9333 default:
9334 BFD_ASSERT (0);
9335 }
9336
9337 /* DIFF value
9338 0 |encoded in location|
9339 |------------|-------------------|---------
9340 sym+off(addend)
9341 -- before ---| *****************
9342 --------------------- between ---|
9343
9344 We only care how much data are relax between DIFF,
9345 marked as ***. */
9346
9347 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9348 between = get_nds32_elf_blank_total (&blank_t,
9349 irel->r_addend + offset, 0);
9350 if (between == before)
9351 goto done_adjust_diff;
9352
9353 switch (ELF32_R_TYPE (irel->r_info))
9354 {
9355 case R_NDS32_DIFF8:
9356 bfd_put_8 (abfd, offset - (between - before),
9357 contents + irel->r_offset);
9358 break;
9359 case R_NDS32_DIFF16:
9360 bfd_put_16 (abfd, offset - (between - before),
9361 contents + irel->r_offset);
9362 break;
9363 case R_NDS32_DIFF32:
9364 bfd_put_32 (abfd, offset - (between - before),
9365 contents + irel->r_offset);
9366 break;
9367 }
9368 }
9369 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9370 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9371 {
9372 bfd_vma val = 0;
9373 unsigned int len = 0;
9374 unsigned long before, between;
9375 bfd_byte *endp, *p;
9376
9377 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9378 &len);
9379
9380 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9381 between = get_nds32_elf_blank_total (&blank_t,
9382 irel->r_addend + val, 0);
9383 if (between == before)
9384 goto done_adjust_diff;
9385
9386 p = contents + irel->r_offset;
9387 endp = p + len -1;
9388 memset (p, 0x80, len);
9389 *(endp) = 0;
9390 p = write_uleb128 (p, val - (between - before)) - 1;
9391 if (p < endp)
9392 *p |= 0x80;
9393 }
9394 done_adjust_diff:
9395
9396 if (sec == sect)
9397 {
9398 raddr = irel->r_offset;
9399 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9400 irel->r_offset, 1);
9401
9402 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9403 continue;
9404 if (blank_t2 && blank_t2->next
9405 && (blank_t2->offset > raddr
9406 || blank_t2->next->offset <= raddr))
9407 _bfd_error_handler
9408 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9409 abfd);
9410
9411 /* Mark reloc in deleted portion as NONE.
9412 For some relocs like R_NDS32_LABEL that doesn't modify the
9413 content in the section. R_NDS32_LABEL doesn't belong to the
9414 instruction in the section, so we should preserve it. */
9415 if (raddr >= blank_t2->offset
9416 && raddr < blank_t2->offset + blank_t2->size
9417 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9418 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9419 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9420 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9421 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9422 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9423 {
9424 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9425 R_NDS32_NONE);
9426 continue;
9427 }
9428 }
9429
9430 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9431 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9432 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9433 continue;
9434
9435 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9436 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9437 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9438 {
9439 if (irel->r_addend <= sec->size)
9440 irel->r_addend -=
9441 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9442 }
9443 }
9444 }
9445
9446 /* Adjust the local symbols defined in this section. */
9447 blank_t = blank_head;
9448 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9449 {
9450 if (isym->st_shndx == sec_shndx)
9451 {
9452 if (isym->st_value <= sec->size)
9453 {
9454 bfd_vma ahead;
9455 bfd_vma orig_addr = isym->st_value;
9456
9457 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9458 isym->st_value -= ahead;
9459
9460 /* Adjust function size. */
9461 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9462 && isym->st_size > 0)
9463 isym->st_size -=
9464 get_nds32_elf_blank_total
9465 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9466 }
9467 }
9468 }
9469
9470 /* Now adjust the global symbols defined in this section. */
9471 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9472 - symtab_hdr->sh_info);
9473 sym_hashes = elf_sym_hashes (abfd);
9474 end_hashes = sym_hashes + symcount;
9475 blank_t = blank_head;
9476 for (; sym_hashes < end_hashes; sym_hashes++)
9477 {
9478 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9479
9480 if ((sym_hash->root.type == bfd_link_hash_defined
9481 || sym_hash->root.type == bfd_link_hash_defweak)
9482 && sym_hash->root.u.def.section == sec)
9483 {
9484 if (sym_hash->root.u.def.value <= sec->size)
9485 {
9486 bfd_vma ahead;
9487 bfd_vma orig_addr = sym_hash->root.u.def.value;
9488
9489 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9490 sym_hash->root.u.def.value -= ahead;
9491
9492 /* Adjust function size. */
9493 if (sym_hash->type == STT_FUNC)
9494 sym_hash->size -=
9495 get_nds32_elf_blank_total
9496 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9497
9498 }
9499 }
9500 }
9501
9502 contents = elf_section_data (sec)->this_hdr.contents;
9503 blank_t = blank_head;
9504 while (blank_t->next)
9505 {
9506 /* Actually delete the bytes. */
9507
9508 /* If current blank is the last blank overlap with current section,
9509 go to finish process. */
9510 if (sec->size <= (blank_t->next->offset))
9511 break;
9512
9513 memmove (contents + blank_t->offset - blank_t->total_size,
9514 contents + blank_t->offset + blank_t->size,
9515 blank_t->next->offset - (blank_t->offset + blank_t->size));
9516
9517 blank_t = blank_t->next;
9518 }
9519
9520 if (sec->size > (blank_t->offset + blank_t->size))
9521 {
9522 /* There are remaining code between blank and section boundary.
9523 Move the remaining code to appropriate location. */
9524 memmove (contents + blank_t->offset - blank_t->total_size,
9525 contents + blank_t->offset + blank_t->size,
9526 sec->size - (blank_t->offset + blank_t->size));
9527 sec->size -= blank_t->total_size + blank_t->size;
9528 }
9529 else
9530 /* This blank is not entirely included in the section,
9531 reduce the section size by only part of the blank size. */
9532 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9533
9534 while (blank_head)
9535 {
9536 blank_t = blank_head;
9537 blank_head = blank_head->next;
9538 remove_nds32_elf_blank (blank_t);
9539 }
9540
9541 return TRUE;
9542 }
9543
9544 /* Get the contents of a section. */
9545
9546 static int
9547 nds32_get_section_contents (bfd *abfd, asection *sec,
9548 bfd_byte **contents_p, bfd_boolean cache)
9549 {
9550 /* Get the section contents. */
9551 if (elf_section_data (sec)->this_hdr.contents != NULL)
9552 *contents_p = elf_section_data (sec)->this_hdr.contents;
9553 else
9554 {
9555 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9556 return FALSE;
9557 if (cache)
9558 elf_section_data (sec)->this_hdr.contents = *contents_p;
9559 }
9560
9561 return TRUE;
9562 }
9563
9564 /* Get the contents of the internal symbol of abfd. */
9565
9566 static int
9567 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9568 Elf_Internal_Sym **isymbuf_p)
9569 {
9570 Elf_Internal_Shdr *symtab_hdr;
9571 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9572
9573 /* Read this BFD's local symbols if we haven't done so already. */
9574 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9575 {
9576 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9577 if (*isymbuf_p == NULL)
9578 {
9579 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9580 symtab_hdr->sh_info, 0,
9581 NULL, NULL, NULL);
9582 if (*isymbuf_p == NULL)
9583 return FALSE;
9584 }
9585 }
9586 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9587
9588 return TRUE;
9589 }
9590
9591 /* Range of small data. */
9592 static bfd_vma sdata_range[2][2];
9593 static bfd_vma const sdata_init_range[2] =
9594 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
9595
9596 static int
9597 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9598 bfd_byte *contents, bfd_vma addr)
9599 {
9600 unsigned long insn = bfd_getb32 (contents + addr);
9601
9602 if (insn & 0x80000000)
9603 return 2;
9604
9605 return 4;
9606 }
9607
9608 /* Set the gp relax range. We have to measure the safe range
9609 to do gp relaxation. */
9610
9611 static void
9612 relax_range_measurement (bfd *abfd)
9613 {
9614 asection *sec_f, *sec_b;
9615 /* For upper bound. */
9616 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9617 bfd_vma align;
9618 static int decide_relax_range = 0;
9619 int i;
9620 int range_number = ARRAY_SIZE (sdata_init_range);
9621
9622 if (decide_relax_range)
9623 return;
9624 decide_relax_range = 1;
9625
9626 if (sda_rela_sec == NULL)
9627 {
9628 /* Since there is no data sections, we assume the range is page size. */
9629 for (i = 0; i < range_number; i++)
9630 {
9631 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9632 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9633 }
9634 return;
9635 }
9636
9637 /* Get the biggest alignment power after the gp located section. */
9638 sec_f = sda_rela_sec->output_section;
9639 sec_b = sec_f->next;
9640 align = 0;
9641 while (sec_b != NULL)
9642 {
9643 if ((unsigned)(1 << sec_b->alignment_power) > align)
9644 align = (1 << sec_b->alignment_power);
9645 sec_b = sec_b->next;
9646 }
9647
9648 /* I guess we can not determine the section before
9649 gp located section, so we assume the align is max page size. */
9650 for (i = 0; i < range_number; i++)
9651 {
9652 sdata_range[i][1] = sdata_init_range[i] - align;
9653 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9654 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9655 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9656 }
9657 }
9658
9659 /* These are macros used to check flags encoded in r_addend.
9660 They are only used by nds32_elf_relax_section (). */
9661 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9662 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9663 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9664 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9665
9666 static const char * unrecognized_reloc_msg =
9667 /* xgettext:c-format */
9668 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9669
9670 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9671
9672 static bfd_boolean
9673 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9674 Elf_Internal_Rela *internal_relocs, int *insn_len,
9675 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9676 Elf_Internal_Shdr *symtab_hdr)
9677 {
9678 /* There are 3 variations for LONGCALL1
9679 case 4-4-2; 16-bit on, optimize off or optimize for space
9680 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9681 ori ta, ta, lo12(symbol) ; LO12S0
9682 jral5 ta ;
9683
9684 case 4-4-4; 16-bit off, optimize don't care
9685 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9686 ori ta, ta, lo12(symbol) ; LO12S0
9687 jral ta ;
9688
9689 case 4-4-4; 16-bit on, optimize for speed
9690 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9691 ori ta, ta, lo12(symbol) ; LO12S0
9692 jral ta ;
9693 Check code for -mlong-calls output. */
9694
9695 /* Get the reloc for the address from which the register is
9696 being loaded. This reloc will tell us which function is
9697 actually being called. */
9698
9699 bfd_vma laddr;
9700 int seq_len; /* Original length of instruction sequence. */
9701 uint32_t insn;
9702 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9703 bfd_signed_vma foff;
9704 uint16_t insn16;
9705
9706 irelend = internal_relocs + sec->reloc_count;
9707 seq_len = GET_SEQ_LEN (irel->r_addend);
9708 laddr = irel->r_offset;
9709 *insn_len = seq_len;
9710
9711 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9712 R_NDS32_HI20_RELA, laddr);
9713 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9714 R_NDS32_LO12S0_ORI_RELA,
9715 laddr + 4);
9716
9717 if (hi_irelfn == irelend || lo_irelfn == irelend)
9718 {
9719 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9720 (uint64_t) irel->r_offset);
9721 return FALSE;
9722 }
9723
9724 /* Get the value of the symbol referred to by the reloc. */
9725 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9726
9727 /* This condition only happened when symbol is undefined. */
9728 if (foff == 0
9729 || foff < -CONSERVATIVE_24BIT_S1
9730 || foff >= CONSERVATIVE_24BIT_S1)
9731 return FALSE;
9732
9733 /* Relax to: jal symbol; 25_PCREL. */
9734 /* For simplicity of coding, we are going to modify the section
9735 contents, the section relocs, and the BFD symbol table. We
9736 must tell the rest of the code not to free up this
9737 information. It would be possible to instead create a table
9738 of changes which have to be made, as is done in coff-mips.c;
9739 that would be more work, but would require less memory when
9740 the linker is run. */
9741
9742 /* Replace the long call with a jal. */
9743 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9744 R_NDS32_25_PCREL_RELA);
9745 irel->r_addend = hi_irelfn->r_addend;
9746
9747 /* We don't resolve this here but resolve it in relocate_section. */
9748 insn = INSN_JAL;
9749 bfd_putb32 (insn, contents + irel->r_offset);
9750
9751 hi_irelfn->r_info =
9752 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9753 lo_irelfn->r_info =
9754 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9755 *insn_len = 4;
9756
9757 if (seq_len & 0x2)
9758 {
9759 insn16 = NDS32_NOP16;
9760 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9761 lo_irelfn->r_info =
9762 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9763 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9764 *insn_len += 2;
9765 }
9766 return TRUE;
9767 }
9768
9769 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9770 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
9771
9772 static bfd_boolean
9773 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9774 Elf_Internal_Rela *internal_relocs, int *insn_len,
9775 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9776 Elf_Internal_Shdr *symtab_hdr)
9777 {
9778 /* bltz rt, .L1 ; LONGCALL2
9779 jal symbol ; 25_PCREL
9780 .L1: */
9781
9782 /* Get the reloc for the address from which the register is
9783 being loaded. This reloc will tell us which function is
9784 actually being called. */
9785
9786 bfd_vma laddr;
9787 uint32_t insn;
9788 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9789 bfd_signed_vma foff;
9790
9791 irelend = internal_relocs + sec->reloc_count;
9792 laddr = irel->r_offset;
9793 i1_irelfn =
9794 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9795 R_NDS32_25_PCREL_RELA, laddr + 4);
9796
9797 if (i1_irelfn == irelend)
9798 {
9799 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9800 (uint64_t) irel->r_offset);
9801 return FALSE;
9802 }
9803
9804 insn = bfd_getb32 (contents + laddr);
9805
9806 /* Get the value of the symbol referred to by the reloc. */
9807 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9808
9809 if (foff == 0
9810 || foff < -CONSERVATIVE_16BIT_S1
9811 || foff >= CONSERVATIVE_16BIT_S1)
9812 return FALSE;
9813
9814 /* Relax to bgezal rt, label ; 17_PCREL
9815 or bltzal rt, label ; 17_PCREL */
9816
9817 /* Convert to complimentary conditional call. */
9818 insn = CONVERT_CONDITION_CALL (insn);
9819
9820 /* For simplicity of coding, we are going to modify the section
9821 contents, the section relocs, and the BFD symbol table. We
9822 must tell the rest of the code not to free up this
9823 information. It would be possible to instead create a table
9824 of changes which have to be made, as is done in coff-mips.c;
9825 that would be more work, but would require less memory when
9826 the linker is run. */
9827
9828 /* Clean unnessary relocations. */
9829 i1_irelfn->r_info =
9830 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9831 cond_irelfn =
9832 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9833 R_NDS32_17_PCREL_RELA, laddr);
9834 if (cond_irelfn != irelend)
9835 cond_irelfn->r_info =
9836 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9837
9838 /* Replace the long call with a bgezal. */
9839 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9840 R_NDS32_17_PCREL_RELA);
9841 irel->r_addend = i1_irelfn->r_addend;
9842
9843 bfd_putb32 (insn, contents + irel->r_offset);
9844
9845 *insn_len = 4;
9846 return TRUE;
9847 }
9848
9849 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9850
9851 static bfd_boolean
9852 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9853 Elf_Internal_Rela *internal_relocs, int *insn_len,
9854 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9855 Elf_Internal_Shdr *symtab_hdr)
9856 {
9857 /* There are 3 variations for LONGCALL3
9858 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9859 bltz rt, $1 ; LONGCALL3
9860 sethi ta, hi20(symbol) ; HI20
9861 ori ta, ta, lo12(symbol) ; LO12S0
9862 jral5 ta ;
9863 $1
9864
9865 case 4-4-4-4; 16-bit off, optimize don't care
9866 bltz rt, $1 ; LONGCALL3
9867 sethi ta, hi20(symbol) ; HI20
9868 ori ta, ta, lo12(symbol) ; LO12S0
9869 jral ta ;
9870 $1
9871
9872 case 4-4-4-4; 16-bit on, optimize for speed
9873 bltz rt, $1 ; LONGCALL3
9874 sethi ta, hi20(symbol) ; HI20
9875 ori ta, ta, lo12(symbol) ; LO12S0
9876 jral ta ;
9877 $1 */
9878
9879 /* Get the reloc for the address from which the register is
9880 being loaded. This reloc will tell us which function is
9881 actually being called. */
9882
9883 bfd_vma laddr;
9884 int seq_len; /* Original length of instruction sequence. */
9885 uint32_t insn;
9886 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9887 bfd_signed_vma foff;
9888 uint16_t insn16;
9889
9890 irelend = internal_relocs + sec->reloc_count;
9891 seq_len = GET_SEQ_LEN (irel->r_addend);
9892 laddr = irel->r_offset;
9893 *insn_len = seq_len;
9894
9895 hi_irelfn =
9896 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9897 R_NDS32_HI20_RELA, laddr + 4);
9898 lo_irelfn =
9899 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9900 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9901
9902 if (hi_irelfn == irelend || lo_irelfn == irelend)
9903 {
9904 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9905 (uint64_t) irel->r_offset);
9906 return FALSE;
9907 }
9908
9909 /* Get the value of the symbol referred to by the reloc. */
9910 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9911
9912 if (foff == 0
9913 || foff < -CONSERVATIVE_24BIT_S1
9914 || foff >= CONSERVATIVE_24BIT_S1)
9915 return FALSE;
9916
9917 insn = bfd_getb32 (contents + laddr);
9918 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9919 {
9920 /* Relax to bgezal rt, label ; 17_PCREL
9921 or bltzal rt, label ; 17_PCREL */
9922
9923 /* Convert to complimentary conditional call. */
9924 insn = CONVERT_CONDITION_CALL (insn);
9925 bfd_putb32 (insn, contents + irel->r_offset);
9926
9927 *insn_len = 4;
9928 irel->r_info =
9929 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9930 hi_irelfn->r_info =
9931 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9932 lo_irelfn->r_info =
9933 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9934
9935 cond_irelfn =
9936 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9937 R_NDS32_17_PCREL_RELA, laddr);
9938 if (cond_irelfn != irelend)
9939 {
9940 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9941 R_NDS32_17_PCREL_RELA);
9942 cond_irelfn->r_addend = hi_irelfn->r_addend;
9943 }
9944
9945 if (seq_len & 0x2)
9946 {
9947 insn16 = NDS32_NOP16;
9948 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9949 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9950 R_NDS32_INSN16);
9951 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9952 insn_len += 2;
9953 }
9954 }
9955 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9956 {
9957 /* Relax to the following instruction sequence
9958 bltz rt, $1 ; LONGCALL2
9959 jal symbol ; 25_PCREL
9960 $1 */
9961 *insn_len = 8;
9962 insn = INSN_JAL;
9963 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9964
9965 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9966 R_NDS32_25_PCREL_RELA);
9967 irel->r_info =
9968 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9969
9970 lo_irelfn->r_info =
9971 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9972
9973 if (seq_len & 0x2)
9974 {
9975 insn16 = NDS32_NOP16;
9976 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9977 lo_irelfn->r_info =
9978 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9979 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9980 insn_len += 2;
9981 }
9982 }
9983 return TRUE;
9984 }
9985
9986 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
9987
9988 static bfd_boolean
9989 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9990 Elf_Internal_Rela *internal_relocs, int *insn_len,
9991 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9992 Elf_Internal_Shdr *symtab_hdr)
9993 {
9994 /* There are 3 variations for LONGJUMP1
9995 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9996 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9997 ori ta, ta, lo12(symbol) ; LO12S0
9998 jr5 ta ;
9999
10000 case 4-4-4; 16-bit off, optimize don't care
10001 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10002 ori ta, ta, lo12(symbol) ; LO12S0
10003 jr ta ;
10004
10005 case 4-4-4; 16-bit on, optimize for speed
10006 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10007 ori ta, ta, lo12(symbol) ; LO12S0
10008 jr ta ; */
10009
10010 /* Get the reloc for the address from which the register is
10011 being loaded. This reloc will tell us which function is
10012 actually being called. */
10013
10014 bfd_vma laddr;
10015 int seq_len; /* Original length of instruction sequence. */
10016 int insn16_on; /* 16-bit on/off. */
10017 uint32_t insn;
10018 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10019 bfd_signed_vma foff;
10020 uint16_t insn16;
10021 unsigned long reloc;
10022
10023 irelend = internal_relocs + sec->reloc_count;
10024 seq_len = GET_SEQ_LEN (irel->r_addend);
10025 laddr = irel->r_offset;
10026 *insn_len = seq_len;
10027 insn16_on = IS_16BIT_ON (irel->r_addend);
10028
10029 hi_irelfn =
10030 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10031 R_NDS32_HI20_RELA, laddr);
10032 lo_irelfn =
10033 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10034 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10035 if (hi_irelfn == irelend || lo_irelfn == irelend)
10036 {
10037 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10038 (uint64_t) irel->r_offset);
10039 return FALSE;
10040 }
10041
10042 /* Get the value of the symbol referred to by the reloc. */
10043 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10044
10045 if (foff == 0
10046 || foff >= CONSERVATIVE_24BIT_S1
10047 || foff < -CONSERVATIVE_24BIT_S1)
10048 return FALSE;
10049
10050 if (insn16_on
10051 && foff >= -ACCURATE_8BIT_S1
10052 && foff < ACCURATE_8BIT_S1
10053 && (seq_len & 0x2))
10054 {
10055 /* j8 label */
10056 /* 16-bit on, but not optimized for speed. */
10057 reloc = R_NDS32_9_PCREL_RELA;
10058 insn16 = INSN_J8;
10059 bfd_putb16 (insn16, contents + irel->r_offset);
10060 *insn_len = 2;
10061 irel->r_info =
10062 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10063 }
10064 else
10065 {
10066 /* j label */
10067 reloc = R_NDS32_25_PCREL_RELA;
10068 insn = INSN_J;
10069 bfd_putb32 (insn, contents + irel->r_offset);
10070 *insn_len = 4;
10071 irel->r_info =
10072 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10073 irel->r_addend = 0;
10074 }
10075
10076 hi_irelfn->r_info =
10077 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10078 lo_irelfn->r_info =
10079 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10080
10081 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10082 {
10083 insn16 = NDS32_NOP16;
10084 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10085 lo_irelfn->r_info =
10086 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10087 R_NDS32_INSN16);
10088 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10089 *insn_len += 2;
10090 }
10091 return TRUE;
10092 }
10093
10094 /* Revert condition branch. This function does not check if the input
10095 instruction is condition branch or not. */
10096
10097 static void
10098 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10099 uint16_t *re_insn16, uint32_t *re_insn)
10100 {
10101 uint32_t comp_insn = 0;
10102 uint16_t comp_insn16 = 0;
10103
10104 if (insn)
10105 {
10106 if (N32_OP6 (insn) == N32_OP6_BR1)
10107 {
10108 /* beqs label. */
10109 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10110 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10111 {
10112 /* Insn can be contracted to 16-bit implied r5. */
10113 comp_insn16 =
10114 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10115 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10116 }
10117 }
10118 else if (N32_OP6 (insn) == N32_OP6_BR3)
10119 {
10120 /* bnec $ta, imm11, label. */
10121 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10122 }
10123 else
10124 {
10125 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10126 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10127 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10128 {
10129 if (N32_IS_RT3 (insn))
10130 {
10131 /* Insn can be contracted to 16-bit. */
10132 comp_insn16 =
10133 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10134 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10135 }
10136 else if (N32_RT5 (insn) == REG_R15)
10137 {
10138 /* Insn can be contracted to 16-bit. */
10139 comp_insn16 =
10140 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10141 }
10142 }
10143 }
10144 }
10145 else
10146 {
10147 switch ((insn16 & 0xf000) >> 12)
10148 {
10149 case 0xc:
10150 /* beqz38 or bnez38 */
10151 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10152 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10153 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10154 break;
10155
10156 case 0xd:
10157 /* beqs38 or bnes38 */
10158 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10159 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10160 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10161 | (REG_R5 << 15);
10162 break;
10163
10164 case 0xe:
10165 /* beqzS8 or bnezS8 */
10166 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10167 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10168 comp_insn |= REG_R15 << 20;
10169 break;
10170
10171 default:
10172 break;
10173 }
10174 }
10175 if (comp_insn && re_insn)
10176 *re_insn = comp_insn;
10177 if (comp_insn16 && re_insn16)
10178 *re_insn16 = comp_insn16;
10179 }
10180
10181 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
10182
10183 static bfd_boolean
10184 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10185 Elf_Internal_Rela *internal_relocs, int *insn_len,
10186 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10187 Elf_Internal_Shdr *symtab_hdr)
10188 {
10189 /* There are 3 variations for LONGJUMP2
10190 case 2-4; 1st insn convertible, 16-bit on,
10191 optimize off or optimize for space
10192 bnes38 rt, ra, $1 ; LONGJUMP2
10193 j label ; 25_PCREL
10194 $1:
10195
10196 case 4-4; 1st insn not convertible
10197 bne rt, ra, $1 ; LONGJUMP2
10198 j label ; 25_PCREL
10199 $1:
10200
10201 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10202 bne rt, ra, $1 ; LONGJUMP2
10203 j label ; 25_PCREL
10204 $1: */
10205
10206 /* Get the reloc for the address from which the register is
10207 being loaded. This reloc will tell us which function is
10208 actually being called. */
10209
10210 bfd_vma laddr;
10211 int seq_len; /* Original length of instruction sequence. */
10212 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10213 int first_size;
10214 unsigned int i;
10215 bfd_signed_vma foff;
10216 uint32_t insn, re_insn = 0;
10217 uint16_t insn16, re_insn16 = 0;
10218 unsigned long reloc, cond_reloc;
10219
10220 enum elf_nds32_reloc_type checked_types[] =
10221 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10222
10223 irelend = internal_relocs + sec->reloc_count;
10224 seq_len = GET_SEQ_LEN (irel->r_addend);
10225 laddr = irel->r_offset;
10226 *insn_len = seq_len;
10227 first_size = (seq_len == 6) ? 2 : 4;
10228
10229 i2_irelfn =
10230 find_relocs_at_address_addr (irel, internal_relocs,
10231 irelend, R_NDS32_25_PCREL_RELA,
10232 laddr + first_size);
10233
10234 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10235 {
10236 cond_irelfn =
10237 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10238 checked_types[i], laddr);
10239 if (cond_irelfn != irelend)
10240 break;
10241 }
10242
10243 if (i2_irelfn == irelend || cond_irelfn == irelend)
10244 {
10245 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10246 (uint64_t) irel->r_offset);
10247 return FALSE;
10248 }
10249
10250 /* Get the value of the symbol referred to by the reloc. */
10251 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10252 if (foff == 0
10253 || foff < -CONSERVATIVE_16BIT_S1
10254 || foff >= CONSERVATIVE_16BIT_S1)
10255 return FALSE;
10256
10257 /* Get the all corresponding instructions. */
10258 if (first_size == 4)
10259 {
10260 insn = bfd_getb32 (contents + laddr);
10261 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10262 }
10263 else
10264 {
10265 insn16 = bfd_getb16 (contents + laddr);
10266 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10267 }
10268
10269 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10270 && foff < ACCURATE_8BIT_S1 - first_size)
10271 {
10272 if (first_size == 4)
10273 {
10274 /* Don't convert it to 16-bit now, keep this as relaxable for
10275 ``label reloc; INSN16''. */
10276
10277 /* Save comp_insn32 to buffer. */
10278 bfd_putb32 (re_insn, contents + irel->r_offset);
10279 *insn_len = 4;
10280 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10281 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10282 cond_reloc = R_NDS32_INSN16;
10283 }
10284 else
10285 {
10286 bfd_putb16 (re_insn16, contents + irel->r_offset);
10287 *insn_len = 2;
10288 reloc = R_NDS32_9_PCREL_RELA;
10289 cond_reloc = R_NDS32_NONE;
10290 }
10291 }
10292 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10293 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10294 && foff < ACCURATE_14BIT_S1 - first_size))
10295 {
10296 /* beqs label ; 15_PCREL */
10297 bfd_putb32 (re_insn, contents + irel->r_offset);
10298 *insn_len = 4;
10299 reloc = R_NDS32_15_PCREL_RELA;
10300 cond_reloc = R_NDS32_NONE;
10301 }
10302 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10303 && foff >= -CONSERVATIVE_16BIT_S1
10304 && foff < CONSERVATIVE_16BIT_S1)
10305 {
10306 /* beqz label ; 17_PCREL */
10307 bfd_putb32 (re_insn, contents + irel->r_offset);
10308 *insn_len = 4;
10309 reloc = R_NDS32_17_PCREL_RELA;
10310 cond_reloc = R_NDS32_NONE;
10311 }
10312 else
10313 return FALSE;
10314
10315 /* Set all relocations. */
10316 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10317 irel->r_addend = i2_irelfn->r_addend;
10318
10319 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10320 cond_reloc);
10321 cond_irelfn->r_addend = 0;
10322
10323 if ((seq_len ^ *insn_len ) & 0x2)
10324 {
10325 insn16 = NDS32_NOP16;
10326 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10327 i2_irelfn->r_offset = 4;
10328 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10329 R_NDS32_INSN16);
10330 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10331 *insn_len += 2;
10332 }
10333 else
10334 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10335 R_NDS32_NONE);
10336 return TRUE;
10337 }
10338
10339 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
10340
10341 static bfd_boolean
10342 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10343 Elf_Internal_Rela *internal_relocs, int *insn_len,
10344 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10345 Elf_Internal_Shdr *symtab_hdr)
10346 {
10347 /* There are 5 variations for LONGJUMP3
10348 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10349 optimize off or optimize for space
10350 bnes38 rt, ra, $1 ; LONGJUMP3
10351 sethi ta, hi20(symbol) ; HI20
10352 ori ta, ta, lo12(symbol) ; LO12S0
10353 jr5 ta ;
10354 $1: ;
10355
10356 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10357 bnes38 rt, ra, $1 ; LONGJUMP3
10358 sethi ta, hi20(symbol) ; HI20
10359 ori ta, ta, lo12(symbol) ; LO12S0
10360 jr5 ta ;
10361 $1: ; LABEL
10362
10363 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10364 optimize off or optimize for space
10365 bne rt, ra, $1 ; LONGJUMP3
10366 sethi ta, hi20(symbol) ; HI20
10367 ori ta, ta, lo12(symbol) ; LO12S0
10368 jr5 ta ;
10369 $1: ;
10370
10371 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10372 16-bit off if no INSN16
10373 bne rt, ra, $1 ; LONGJUMP3
10374 sethi ta, hi20(symbol) ; HI20
10375 ori ta, ta, lo12(symbol) ; LO12S0
10376 jr ta ;
10377 $1: ;
10378
10379 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10380 16-bit off if no INSN16
10381 bne rt, ra, $1 ; LONGJUMP3
10382 sethi ta, hi20(symbol) ; HI20
10383 ori ta, ta, lo12(symbol) ; LO12S0
10384 jr ta ;
10385 $1: ; LABEL */
10386
10387 /* Get the reloc for the address from which the register is
10388 being loaded. This reloc will tell us which function is
10389 actually being called. */
10390 enum elf_nds32_reloc_type checked_types[] =
10391 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10392
10393 int reloc_off = 0, cond_removed = 0, convertible;
10394 bfd_vma laddr;
10395 int seq_len; /* Original length of instruction sequence. */
10396 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10397 int first_size;
10398 unsigned int i;
10399 bfd_signed_vma foff;
10400 uint32_t insn, re_insn = 0;
10401 uint16_t insn16, re_insn16 = 0;
10402 unsigned long reloc, cond_reloc;
10403
10404 irelend = internal_relocs + sec->reloc_count;
10405 seq_len = GET_SEQ_LEN (irel->r_addend);
10406 laddr = irel->r_offset;
10407 *insn_len = seq_len;
10408
10409 convertible = IS_1ST_CONVERT (irel->r_addend);
10410
10411 if (convertible)
10412 first_size = 2;
10413 else
10414 first_size = 4;
10415
10416 /* Get all needed relocations. */
10417 hi_irelfn =
10418 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10419 R_NDS32_HI20_RELA, laddr + first_size);
10420 lo_irelfn =
10421 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10422 R_NDS32_LO12S0_ORI_RELA,
10423 laddr + first_size + 4);
10424
10425 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10426 {
10427 cond_irelfn =
10428 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10429 checked_types[i], laddr);
10430 if (cond_irelfn != irelend)
10431 break;
10432 }
10433
10434 if (hi_irelfn == irelend
10435 || lo_irelfn == irelend
10436 || cond_irelfn == irelend)
10437 {
10438 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10439 (uint64_t) irel->r_offset);
10440 return FALSE;
10441 }
10442
10443 /* Get the value of the symbol referred to by the reloc. */
10444 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10445
10446 if (foff == 0
10447 || foff < -CONSERVATIVE_24BIT_S1
10448 || foff >= CONSERVATIVE_24BIT_S1)
10449 return FALSE;
10450
10451 /* Get the all corresponding instructions. */
10452 if (first_size == 4)
10453 {
10454 insn = bfd_getb32 (contents + laddr);
10455 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10456 }
10457 else
10458 {
10459 insn16 = bfd_getb16 (contents + laddr);
10460 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10461 }
10462
10463 /* For simplicity of coding, we are going to modify the section
10464 contents, the section relocs, and the BFD symbol table. We
10465 must tell the rest of the code not to free up this
10466 information. It would be possible to instead create a table
10467 of changes which have to be made, as is done in coff-mips.c;
10468 that would be more work, but would require less memory when
10469 the linker is run. */
10470
10471 if (re_insn16
10472 && foff >= -ACCURATE_8BIT_S1 - first_size
10473 && foff < ACCURATE_8BIT_S1 - first_size)
10474 {
10475 if (!(seq_len & 0x2))
10476 {
10477 /* Don't convert it to 16-bit now, keep this as relaxable
10478 for ``label reloc; INSN1a''6. */
10479 /* Save comp_insn32 to buffer. */
10480 bfd_putb32 (re_insn, contents + irel->r_offset);
10481 *insn_len = 4;
10482 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10483 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10484 cond_reloc = R_NDS32_INSN16;
10485 }
10486 else
10487 {
10488 /* Not optimize for speed; convert sequence to 16-bit. */
10489 /* Save comp_insn16 to buffer. */
10490 bfd_putb16 (re_insn16, contents + irel->r_offset);
10491 *insn_len = 2;
10492 reloc = R_NDS32_9_PCREL_RELA;
10493 cond_reloc = R_NDS32_NONE;
10494 }
10495 cond_removed = 1;
10496 }
10497 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10498 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10499 && foff < ACCURATE_14BIT_S1 - first_size))
10500 {
10501 /* beqs label ; 15_PCREL */
10502 bfd_putb32 (re_insn, contents + irel->r_offset);
10503 *insn_len = 4;
10504 reloc = R_NDS32_15_PCREL_RELA;
10505 cond_reloc = R_NDS32_NONE;
10506 cond_removed = 1;
10507 }
10508 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10509 && foff >= -CONSERVATIVE_16BIT_S1
10510 && foff < CONSERVATIVE_16BIT_S1)
10511 {
10512 /* beqz label ; 17_PCREL */
10513 bfd_putb32 (re_insn, contents + irel->r_offset);
10514 *insn_len = 4;
10515 reloc = R_NDS32_17_PCREL_RELA;
10516 cond_reloc = R_NDS32_NONE;
10517 cond_removed = 1;
10518 }
10519 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10520 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10521 {
10522 /* Relax to one of the following 3 variations
10523
10524 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10525 for space
10526 bnes38 rt, $1 ; LONGJUMP2
10527 j label ; 25_PCREL
10528 $1
10529
10530 case 4-4; 1st insn not convertible, others don't care
10531 bne rt, ra, $1 ; LONGJUMP2
10532 j label ; 25_PCREL
10533 $1
10534
10535 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10536 bne rt, ra, $1 ; LONGJUMP2
10537 j label ; 25_PCREL
10538 $1 */
10539
10540 /* Offset for first instruction. */
10541
10542 /* Use j label as second instruction. */
10543 *insn_len = 4 + first_size;
10544 insn = INSN_J;
10545 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10546 reloc = R_NDS32_LONGJUMP2;
10547 cond_reloc = R_NDS32_25_PLTREL;
10548 }
10549 else
10550 return FALSE;
10551
10552 if (cond_removed == 1)
10553 {
10554 /* Set all relocations. */
10555 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10556 irel->r_addend = hi_irelfn->r_addend;
10557
10558 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10559 cond_reloc);
10560 cond_irelfn->r_addend = 0;
10561 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10562 R_NDS32_NONE);
10563 }
10564 else
10565 {
10566 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10567 irel->r_addend = irel->r_addend;
10568 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10569 cond_reloc);
10570 }
10571
10572 if ((seq_len ^ *insn_len ) & 0x2)
10573 {
10574 insn16 = NDS32_NOP16;
10575 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10576 lo_irelfn->r_offset = *insn_len;
10577 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10578 R_NDS32_INSN16);
10579 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10580 *insn_len += 2;
10581 }
10582 else
10583 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10584 R_NDS32_NONE);
10585 return TRUE;
10586 }
10587
10588 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
10589
10590 static bfd_boolean
10591 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10592 Elf_Internal_Rela *internal_relocs, int *insn_len,
10593 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10594 Elf_Internal_Shdr *symtab_hdr)
10595 {
10596 /* The pattern for LONGCALL4. Support for function cse.
10597 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10598 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10599 jral ta ; PTR_RES/EMPTY/INSN16 */
10600
10601 bfd_vma laddr;
10602 uint32_t insn;
10603 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10604 Elf_Internal_Rela *irelend;
10605 bfd_signed_vma foff;
10606
10607 irelend = internal_relocs + sec->reloc_count;
10608 laddr = irel->r_offset;
10609
10610 /* Get the reloc for the address from which the register is
10611 being loaded. This reloc will tell us which function is
10612 actually being called. */
10613 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10614 R_NDS32_HI20_RELA, laddr);
10615
10616 if (hi_irel == irelend)
10617 {
10618 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10619 (uint64_t) irel->r_offset);
10620 return FALSE;
10621 }
10622
10623 /* Get the value of the symbol referred to by the reloc. */
10624 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10625
10626 /* This condition only happened when symbol is undefined. */
10627 if (foff == 0
10628 || foff < -CONSERVATIVE_24BIT_S1
10629 || foff >= CONSERVATIVE_24BIT_S1)
10630 return FALSE;
10631
10632 /* Relax to: jal symbol; 25_PCREL. */
10633 /* For simplicity of coding, we are going to modify the section
10634 contents, the section relocs, and the BFD symbol table. We
10635 must tell the rest of the code not to free up this
10636 information. It would be possible to instead create a table
10637 of changes which have to be made, as is done in coff-mips.c;
10638 that would be more work, but would require less memory when
10639 the linker is run. */
10640
10641 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10642 R_NDS32_PTR_RESOLVED, irel->r_addend);
10643 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10644 R_NDS32_EMPTY, irel->r_addend);
10645
10646 if (ptr_irel == irelend || em_irel == irelend)
10647 {
10648 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10649 (uint64_t) irel->r_offset);
10650 return FALSE;
10651 }
10652 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10653 insn = bfd_getb32 (contents + irel->r_addend);
10654 if (insn & 0x80000000)
10655 return FALSE;
10656
10657 /* Replace the long call with a jal. */
10658 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10659 R_NDS32_25_PCREL_RELA);
10660 ptr_irel->r_addend = 1;
10661
10662 /* We don't resolve this here but resolve it in relocate_section. */
10663 insn = INSN_JAL;
10664 bfd_putb32 (insn, contents + em_irel->r_offset);
10665
10666 irel->r_info =
10667 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10668
10669 /* If there is function cse, HI20 can not remove now. */
10670 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10671 R_NDS32_LONGCALL4, laddr);
10672 if (call_irel == irelend)
10673 {
10674 *insn_len = 0;
10675 hi_irel->r_info =
10676 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10677 }
10678
10679 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10680 R_NDS32_INSN16, irel->r_addend);
10681 if (insn_irel != irelend)
10682 insn_irel->r_info =
10683 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10684
10685 return TRUE;
10686 }
10687
10688 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
10689
10690 static bfd_boolean
10691 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10692 Elf_Internal_Rela *internal_relocs, int *insn_len,
10693 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10694 Elf_Internal_Shdr *symtab_hdr)
10695 {
10696 /* The pattern for LONGCALL5.
10697 bltz rt, .L1 ; LONGCALL5/17_PCREL
10698 jal symbol ; 25_PCREL
10699 .L1: */
10700
10701 bfd_vma laddr;
10702 uint32_t insn;
10703 Elf_Internal_Rela *cond_irel, *irelend;
10704 bfd_signed_vma foff;
10705
10706 irelend = internal_relocs + sec->reloc_count;
10707 laddr = irel->r_offset;
10708 insn = bfd_getb32 (contents + laddr);
10709
10710 /* Get the reloc for the address from which the register is
10711 being loaded. This reloc will tell us which function is
10712 actually being called. */
10713 cond_irel =
10714 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10715 R_NDS32_25_PCREL_RELA, irel->r_addend);
10716 if (cond_irel == irelend)
10717 {
10718 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10719 (uint64_t) irel->r_offset);
10720 return FALSE;
10721 }
10722
10723 /* Get the value of the symbol referred to by the reloc. */
10724 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10725
10726 if (foff == 0
10727 || foff < -CONSERVATIVE_16BIT_S1
10728 || foff >= CONSERVATIVE_16BIT_S1)
10729 return FALSE;
10730
10731 /* Relax to bgezal rt, label ; 17_PCREL
10732 or bltzal rt, label ; 17_PCREL. */
10733
10734 /* Convert to complimentary conditional call. */
10735 insn = CONVERT_CONDITION_CALL (insn);
10736
10737 /* For simplicity of coding, we are going to modify the section
10738 contents, the section relocs, and the BFD symbol table. We
10739 must tell the rest of the code not to free up this
10740 information. It would be possible to instead create a table
10741 of changes which have to be made, as is done in coff-mips.c;
10742 that would be more work, but would require less memory when
10743 the linker is run. */
10744
10745 /* Modify relocation and contents. */
10746 cond_irel->r_info =
10747 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10748
10749 /* Replace the long call with a bgezal. */
10750 bfd_putb32 (insn, contents + cond_irel->r_offset);
10751 *insn_len = 0;
10752
10753 /* Clean unnessary relocations. */
10754 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10755
10756 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10757 R_NDS32_17_PCREL_RELA, laddr);
10758 cond_irel->r_info =
10759 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10760
10761 return TRUE;
10762 }
10763
10764 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
10765
10766 static bfd_boolean
10767 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10768 Elf_Internal_Rela *internal_relocs, int *insn_len,
10769 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10770 Elf_Internal_Shdr *symtab_hdr)
10771 {
10772 /* The pattern for LONGCALL6.
10773 bltz rt, .L1 ; LONGCALL6/17_PCREL
10774 sethi ta, hi20(symbol) ; HI20/PTR
10775 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10776 jral ta ; PTR_RES/EMPTY/INSN16
10777 .L1 */
10778
10779 bfd_vma laddr;
10780 uint32_t insn;
10781 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10782 bfd_signed_vma foff;
10783
10784 irelend = internal_relocs + sec->reloc_count;
10785 laddr = irel->r_offset;
10786
10787 /* Get the reloc for the address from which the register is
10788 being loaded. This reloc will tell us which function is
10789 actually being called. */
10790 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10791 R_NDS32_EMPTY, irel->r_addend);
10792
10793 if (em_irel == irelend)
10794 {
10795 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10796 (uint64_t) irel->r_offset);
10797 return FALSE;
10798 }
10799
10800 /* Get the value of the symbol referred to by the reloc. */
10801 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10802
10803 if (foff == 0
10804 || foff < -CONSERVATIVE_24BIT_S1
10805 || foff >= CONSERVATIVE_24BIT_S1)
10806 return FALSE;
10807
10808 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10809 insn = bfd_getb32 (contents + irel->r_addend);
10810 if (insn & 0x80000000)
10811 return FALSE;
10812
10813 insn = bfd_getb32 (contents + laddr);
10814 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10815 {
10816 /* Relax to bgezal rt, label ; 17_PCREL
10817 or bltzal rt, label ; 17_PCREL. */
10818
10819 /* Convert to complimentary conditional call. */
10820 *insn_len = 0;
10821 insn = CONVERT_CONDITION_CALL (insn);
10822 bfd_putb32 (insn, contents + em_irel->r_offset);
10823
10824 em_irel->r_info =
10825 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10826
10827 /* Set resolved relocation. */
10828 cond_irel =
10829 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10830 R_NDS32_PTR_RESOLVED, irel->r_addend);
10831 if (cond_irel == irelend)
10832 {
10833 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10834 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10835 return FALSE;
10836 }
10837 cond_irel->r_addend = 1;
10838
10839 /* Clear relocations. */
10840
10841 irel->r_info =
10842 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10843
10844 cond_irel =
10845 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10846 R_NDS32_17_PCREL_RELA, laddr);
10847 if (cond_irel != irelend)
10848 cond_irel->r_info =
10849 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10850
10851 cond_irel =
10852 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10853 R_NDS32_INSN16, irel->r_addend);
10854 if (cond_irel != irelend)
10855 cond_irel->r_info =
10856 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10857
10858 }
10859 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10860 {
10861 /* Relax to the following instruction sequence
10862 bltz rt, .L1 ; LONGCALL2/17_PCREL
10863 jal symbol ; 25_PCREL/PTR_RES
10864 .L1 */
10865 *insn_len = 4;
10866 /* Convert instruction. */
10867 insn = INSN_JAL;
10868 bfd_putb32 (insn, contents + em_irel->r_offset);
10869
10870 /* Convert relocations. */
10871 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10872 R_NDS32_25_PCREL_RELA);
10873 irel->r_info =
10874 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10875
10876 /* Set resolved relocation. */
10877 cond_irel =
10878 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10879 R_NDS32_PTR_RESOLVED, irel->r_addend);
10880 if (cond_irel == irelend)
10881 {
10882 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10883 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10884 return FALSE;
10885 }
10886 cond_irel->r_addend = 1;
10887
10888 cond_irel =
10889 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10890 R_NDS32_INSN16, irel->r_addend);
10891 if (cond_irel != irelend)
10892 cond_irel->r_info =
10893 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10894 }
10895 return TRUE;
10896 }
10897
10898 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10899
10900 static bfd_boolean
10901 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10902 Elf_Internal_Rela *internal_relocs, int *insn_len,
10903 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10904 Elf_Internal_Shdr *symtab_hdr)
10905 {
10906 /* The pattern for LONGJUMP4.
10907 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10908 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10909 jr ta ; PTR_RES/INSN16/EMPTY */
10910
10911 bfd_vma laddr;
10912 int seq_len; /* Original length of instruction sequence. */
10913 uint32_t insn;
10914 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10915 bfd_signed_vma foff;
10916
10917 irelend = internal_relocs + sec->reloc_count;
10918 seq_len = GET_SEQ_LEN (irel->r_addend);
10919 laddr = irel->r_offset;
10920 *insn_len = seq_len;
10921
10922 /* Get the reloc for the address from which the register is
10923 being loaded. This reloc will tell us which function is
10924 actually being called. */
10925
10926 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10927 R_NDS32_HI20_RELA, laddr);
10928
10929 if (hi_irel == irelend)
10930 {
10931 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10932 (uint64_t) irel->r_offset);
10933 return FALSE;
10934 }
10935
10936 /* Get the value of the symbol referred to by the reloc. */
10937 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10938
10939 if (foff == 0
10940 || foff >= CONSERVATIVE_24BIT_S1
10941 || foff < -CONSERVATIVE_24BIT_S1)
10942 return FALSE;
10943
10944 /* Convert it to "j label", it may be converted to j8 in the final
10945 pass of relaxation. Therefore, we do not consider this currently. */
10946 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10947 R_NDS32_PTR_RESOLVED, irel->r_addend);
10948 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10949 R_NDS32_EMPTY, irel->r_addend);
10950
10951 if (ptr_irel == irelend || em_irel == irelend)
10952 {
10953 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10954 (uint64_t) irel->r_offset);
10955 return FALSE;
10956 }
10957
10958 em_irel->r_info =
10959 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10960 ptr_irel->r_addend = 1;
10961
10962 /* Write instruction. */
10963 insn = INSN_J;
10964 bfd_putb32 (insn, contents + em_irel->r_offset);
10965
10966 /* Clear relocations. */
10967 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10968
10969 /* If there is function cse, HI20 can not remove now. */
10970 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10971 R_NDS32_LONGJUMP4, laddr);
10972 if (call_irel == irelend)
10973 {
10974 *insn_len = 0;
10975 hi_irel->r_info =
10976 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10977 }
10978
10979 return TRUE;
10980 }
10981
10982 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
10983
10984 static bfd_boolean
10985 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10986 Elf_Internal_Rela *internal_relocs, int *insn_len,
10987 int *seq_len, bfd_byte *contents,
10988 Elf_Internal_Sym *isymbuf,
10989 Elf_Internal_Shdr *symtab_hdr)
10990 {
10991 /* There are 2 variations for LONGJUMP5
10992 case 2-4; 1st insn convertible, 16-bit on.
10993 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10994 j label ; 25_PCREL/INSN16
10995 $1:
10996
10997 case 4-4; 1st insn not convertible
10998 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10999 j label ; 25_PCREL/INSN16
11000 .L1: */
11001
11002 bfd_vma laddr;
11003 Elf_Internal_Rela *cond_irel, *irelend;
11004 unsigned int i;
11005 bfd_signed_vma foff;
11006 uint32_t insn, re_insn = 0;
11007 uint16_t insn16, re_insn16 = 0;
11008 unsigned long reloc;
11009
11010 enum elf_nds32_reloc_type checked_types[] =
11011 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11012 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11013
11014 irelend = internal_relocs + sec->reloc_count;
11015 laddr = irel->r_offset;
11016
11017 /* Get the reloc for the address from which the register is
11018 being loaded. This reloc will tell us which function is
11019 actually being called. */
11020
11021 cond_irel =
11022 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11023 R_NDS32_25_PCREL_RELA, irel->r_addend);
11024 if (cond_irel == irelend)
11025 {
11026 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11027 (uint64_t) irel->r_offset);
11028 return FALSE;
11029 }
11030
11031 /* Get the value of the symbol referred to by the reloc. */
11032 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11033
11034 if (foff == 0
11035 || foff < -CONSERVATIVE_16BIT_S1
11036 || foff >= CONSERVATIVE_16BIT_S1)
11037 return FALSE;
11038
11039 /* Get the all corresponding instructions. */
11040 insn = bfd_getb32 (contents + laddr);
11041 /* Check instruction size. */
11042 if (insn & 0x80000000)
11043 {
11044 *seq_len = 0;
11045 insn16 = insn >> 16;
11046 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11047 }
11048 else
11049 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11050
11051 if (N32_OP6 (re_insn) == N32_OP6_BR1
11052 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11053 {
11054 /* beqs label ; 15_PCREL. */
11055 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11056 reloc = R_NDS32_15_PCREL_RELA;
11057 }
11058 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11059 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11060 {
11061 /* beqz label ; 17_PCREL. */
11062 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11063 reloc = R_NDS32_17_PCREL_RELA;
11064 }
11065 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11066 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11067 {
11068 /* beqc label ; 9_PCREL. */
11069 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11070 reloc = R_NDS32_WORD_9_PCREL_RELA;
11071 }
11072 else
11073 return FALSE;
11074
11075 /* Set all relocations. */
11076 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11077
11078 /* Clean relocations. */
11079 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11080 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11081 {
11082 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11083 checked_types[i], laddr);
11084 if (cond_irel != irelend)
11085 {
11086 if (*seq_len == 0
11087 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11088 {
11089 /* If the branch instruction is 2 byte, it cannot remove
11090 directly. Only convert it to nop16 and remove it after
11091 checking alignment issue. */
11092 insn16 = NDS32_NOP16;
11093 bfd_putb16 (insn16, contents + laddr);
11094 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11095 }
11096 else
11097 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11098 R_NDS32_NONE);
11099 }
11100 }
11101 *insn_len = 0;
11102
11103 return TRUE;
11104 }
11105
11106 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
11107
11108 static bfd_boolean
11109 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11110 Elf_Internal_Rela *internal_relocs, int *insn_len,
11111 int *seq_len, bfd_byte *contents,
11112 Elf_Internal_Sym *isymbuf,
11113 Elf_Internal_Shdr *symtab_hdr)
11114 {
11115 /* There are 5 variations for LONGJUMP6
11116 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11117 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11118 sethi ta, hi20(symbol) ; HI20/PTR
11119 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11120 jr ta ; PTR_RES/INSN16/EMPTY
11121 .L1:
11122
11123 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11124 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11125 sethi ta, hi20(symbol) ; HI20/PTR
11126 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11127 jr ta ; PTR_RES/INSN16/EMPTY
11128 .L1: */
11129
11130 enum elf_nds32_reloc_type checked_types[] =
11131 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11132 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11133
11134 int reloc_off = 0, cond_removed = 0;
11135 bfd_vma laddr;
11136 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11137 unsigned int i;
11138 bfd_signed_vma foff;
11139 uint32_t insn, re_insn = 0;
11140 uint16_t insn16, re_insn16 = 0;
11141 unsigned long reloc;
11142
11143 irelend = internal_relocs + sec->reloc_count;
11144 laddr = irel->r_offset;
11145
11146 /* Get the reloc for the address from which the register is
11147 being loaded. This reloc will tell us which function is
11148 actually being called. */
11149 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11150 R_NDS32_EMPTY, irel->r_addend);
11151
11152 if (em_irel == irelend)
11153 {
11154 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11155 (uint64_t) irel->r_offset);
11156 return FALSE;
11157 }
11158
11159 /* Get the value of the symbol referred to by the reloc. */
11160 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11161
11162 if (foff == 0
11163 || foff < -CONSERVATIVE_24BIT_S1
11164 || foff >= CONSERVATIVE_24BIT_S1)
11165 return FALSE;
11166
11167 insn = bfd_getb32 (contents + laddr);
11168 /* Check instruction size. */
11169 if (insn & 0x80000000)
11170 {
11171 *seq_len = 0;
11172 insn16 = insn >> 16;
11173 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11174 }
11175 else
11176 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11177
11178 /* For simplicity of coding, we are going to modify the section
11179 contents, the section relocs, and the BFD symbol table. We
11180 must tell the rest of the code not to free up this
11181 information. It would be possible to instead create a table
11182 of changes which have to be made, as is done in coff-mips.c;
11183 that would be more work, but would require less memory when
11184 the linker is run. */
11185
11186 if (N32_OP6 (re_insn) == N32_OP6_BR1
11187 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11188 {
11189 /* beqs label ; 15_PCREL. */
11190 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11191 reloc = R_NDS32_15_PCREL_RELA;
11192 cond_removed = 1;
11193 }
11194 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11195 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11196 {
11197 /* beqz label ; 17_PCREL. */
11198 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11199 reloc = R_NDS32_17_PCREL_RELA;
11200 cond_removed = 1;
11201 }
11202 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11203 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11204 {
11205 /* Relax to one of the following 2 variations
11206
11207 case 2-4; 1st insn convertible, 16-bit on.
11208 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11209 j label ; 25_PCREL/INSN16
11210 $1:
11211
11212 case 4-4; 1st insn not convertible
11213 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11214 j label ; 25_PCREL/INSN16
11215 .L1: */
11216
11217 /* Use j label as second instruction. */
11218 insn = INSN_J;
11219 reloc = R_NDS32_25_PCREL_RELA;
11220 bfd_putb32 (insn, contents + em_irel->r_offset);
11221 }
11222 else
11223 return FALSE;
11224
11225 /* Set all relocations. */
11226 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11227
11228 cond_irel =
11229 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11230 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11231 cond_irel->r_addend = 1;
11232
11233 /* Use INSN16 of first branch instruction to distinguish if keeping
11234 INSN16 of final instruction or not. */
11235 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11236 R_NDS32_INSN16, irel->r_offset);
11237 if (insn_irel == irelend)
11238 {
11239 /* Clean the final INSN16. */
11240 insn_irel =
11241 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11242 R_NDS32_INSN16, em_irel->r_offset);
11243 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11244 R_NDS32_NONE);
11245 }
11246
11247 if (cond_removed == 1)
11248 {
11249 *insn_len = 0;
11250
11251 /* Clear relocations. */
11252 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11253
11254 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11255 {
11256 cond_irel =
11257 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11258 checked_types[i], laddr);
11259 if (cond_irel != irelend)
11260 {
11261 if (*seq_len == 0
11262 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11263 {
11264 /* If the branch instruction is 2 byte, it cannot remove
11265 directly. Only convert it to nop16 and remove it after
11266 checking alignment issue. */
11267 insn16 = NDS32_NOP16;
11268 bfd_putb16 (insn16, contents + laddr);
11269 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11270 }
11271 else
11272 cond_irel->r_info =
11273 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11274 }
11275 }
11276 }
11277 else
11278 {
11279 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11280 R_NDS32_LONGJUMP5);
11281 }
11282
11283 return TRUE;
11284 }
11285
11286 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
11287
11288 static bfd_boolean
11289 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11290 Elf_Internal_Rela *internal_relocs, int *insn_len,
11291 int *seq_len, bfd_byte *contents,
11292 Elf_Internal_Sym *isymbuf,
11293 Elf_Internal_Shdr *symtab_hdr)
11294 {
11295 /* There are 2 variations for LONGJUMP5
11296 case 2-4; 1st insn convertible, 16-bit on.
11297 movi55 ta, imm11 ; LONGJUMP7/INSN16
11298 beq rt, ta, label ; 15_PCREL
11299
11300 case 4-4; 1st insn not convertible
11301 movi55 ta, imm11 ; LONGJUMP7/INSN16
11302 beq rt, ta, label ; 15_PCREL */
11303
11304 bfd_vma laddr;
11305 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
11306 bfd_signed_vma foff;
11307 uint32_t insn, re_insn = 0;
11308 uint16_t insn16;
11309 uint32_t imm11;
11310
11311 irelend = internal_relocs + sec->reloc_count;
11312 laddr = irel->r_offset;
11313
11314 /* Get the reloc for the address from which the register is
11315 being loaded. This reloc will tell us which function is
11316 actually being called. */
11317
11318 cond_irel =
11319 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11320 R_NDS32_15_PCREL_RELA, irel->r_addend);
11321 if (cond_irel == irelend)
11322 {
11323 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11324 (uint64_t) irel->r_offset);
11325 return FALSE;
11326 }
11327
11328 /* Get the value of the symbol referred to by the reloc. */
11329 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11330
11331 if (foff == 0
11332 || foff < -CONSERVATIVE_8BIT_S1
11333 || foff >= CONSERVATIVE_8BIT_S1)
11334 return FALSE;
11335
11336 /* Get the first instruction for its size. */
11337 insn = bfd_getb32 (contents + laddr);
11338 if (insn & 0x80000000)
11339 {
11340 *seq_len = 0;
11341 /* Get the immediate from movi55. */
11342 imm11 = N16_IMM5S (insn >> 16);
11343 }
11344 else
11345 {
11346 /* Get the immediate from movi. */
11347 imm11 = N32_IMM20S (insn);
11348 }
11349
11350 /* Get the branch instruction. */
11351 insn = bfd_getb32 (contents + irel->r_addend);
11352 /* Convert instruction to BR3. */
11353 if ((insn >> 14) & 0x1)
11354 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11355 else
11356 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11357
11358 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11359
11360 /* Set all relocations. */
11361 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11362 R_NDS32_WORD_9_PCREL_RELA);
11363
11364 /* Clean relocations. */
11365 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11366 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11367 R_NDS32_INSN16, irel->r_offset);
11368 if (insn_irel != irelend)
11369 {
11370 if (*seq_len == 0)
11371 {
11372 /* If the first insntruction is 16bit, convert it to nop16. */
11373 insn16 = NDS32_NOP16;
11374 bfd_putb16 (insn16, contents + laddr);
11375 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11376 }
11377 else
11378 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11379 R_NDS32_NONE);
11380 }
11381 *insn_len = 0;
11382
11383 return TRUE;
11384 }
11385
11386 /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11387 for each relax round. But the gp may changed dramatically and then cause
11388 the truncated to fit errors for the the converted gp instructions.
11389 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11390
11391 static bfd_boolean
11392 nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11393 Elf_Internal_Rela *irel, bfd_boolean *again,
11394 bfd_boolean init,
11395 struct elf_nds32_link_hash_table *table,
11396 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11397
11398 {
11399 int offset_to_gp;
11400 static bfd_boolean sec_pass = FALSE;
11401 static asection *first_sec = NULL, *sym_sec;
11402 /* Record the number of instructions which may be removed. */
11403 static int count = 0, record_count;
11404 Elf_Internal_Sym *isym;
11405 struct elf_link_hash_entry *h = NULL;
11406 int indx;
11407 unsigned long r_symndx;
11408 bfd *abfd = sec->owner;
11409 static bfd_vma record_sda = 0;
11410 int sda_offset = 0;
11411
11412 /* Force doing relaxation when hyper-relax is high. */
11413 if (table->hyper_relax == 2)
11414 return TRUE;
11415
11416 /* Do not relax the load/store patterns for the first
11417 relax round. */
11418 if (init)
11419 {
11420 if (!first_sec)
11421 first_sec = sec;
11422 else if (first_sec == sec)
11423 {
11424 record_count = count;
11425 count = 0;
11426 sec_pass = TRUE;
11427 }
11428
11429 if (!sec_pass)
11430 *again = TRUE;
11431
11432 return TRUE;
11433 }
11434
11435 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11436 DATA_SEGMENT_ALIGN size in the linker script may make it
11437 get even bigger. */
11438 if (record_sda == 0)
11439 record_sda = local_sda;
11440 else if (local_sda > record_sda)
11441 sda_offset = local_sda - record_sda;
11442
11443 /* Assume the instruction will be removed in the best case. */
11444 count++;
11445
11446 /* We record the offset to gp for each symbol, and then check
11447 if it is changed dramatically after relaxing.
11448 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11449 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11450 r_symndx = ELF32_R_SYM (irel->r_info);
11451 if (r_symndx >= symtab_hdr->sh_info)
11452 {
11453 /* Global symbols. */
11454 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11455 h = elf_sym_hashes (abfd)[indx];
11456 sym_sec = h->root.u.def.section;
11457 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11458 || bfd_is_abs_section (sym_sec))
11459 {
11460 /* Forbid doing relaxation when hyper-relax is low. */
11461 if (table->hyper_relax == 0)
11462 return FALSE;
11463
11464 offset_to_gp = *access_addr - local_sda;
11465 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11466 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11467 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11468 < abs (offset_to_gp) - sda_offset)
11469 {
11470 /* This may cause the error, so we reserve the
11471 safe enough size for relaxing. */
11472 if (*access_addr >= local_sda)
11473 *access_addr += (record_count * 4);
11474 else
11475 *access_addr -= (record_count * 4);
11476 }
11477 return sec_pass;
11478 }
11479 }
11480 else
11481 {
11482 /* Local symbols. */
11483 if (!elf32_nds32_allocate_local_sym_info (abfd))
11484 return FALSE;
11485 isym = isymbuf + r_symndx;
11486
11487 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11488 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11489 {
11490 /* Forbid doing relaxation when hyper-relax is low. */
11491 if (table->hyper_relax == 0)
11492 return FALSE;
11493
11494 offset_to_gp = *access_addr - local_sda;
11495 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11496 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11497 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11498 < abs (offset_to_gp) - sda_offset)
11499 {
11500 /* This may cause the error, so we reserve the
11501 safe enough size for relaxing. */
11502 if (*access_addr >= local_sda)
11503 *access_addr += (record_count * 4);
11504 else
11505 *access_addr -= (record_count * 4);
11506 }
11507 return sec_pass;
11508 }
11509 }
11510
11511 return TRUE;
11512 }
11513
11514 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11515
11516 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
11517
11518 static bfd_boolean
11519 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11520 asection *sec, Elf_Internal_Rela *irel,
11521 Elf_Internal_Rela *internal_relocs, int *insn_len,
11522 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11523 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11524 struct elf_nds32_link_hash_table *table)
11525 {
11526 int eliminate_sethi = 0, range_type;
11527 unsigned int i;
11528 bfd_vma local_sda, laddr;
11529 int seq_len; /* Original length of instruction sequence. */
11530 uint32_t insn;
11531 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11532 bfd_vma access_addr = 0;
11533 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11534 struct elf_link_hash_entry *h = NULL;
11535 int indx;
11536 enum elf_nds32_reloc_type checked_types[] =
11537 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11538 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11539 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11540 R_NDS32_TLS_LE_HI20
11541 };
11542
11543 irelend = internal_relocs + sec->reloc_count;
11544 seq_len = GET_SEQ_LEN (irel->r_addend);
11545 laddr = irel->r_offset;
11546 *insn_len = seq_len;
11547
11548 /* Get the high part relocation. */
11549 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11550 {
11551 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11552 checked_types[i], laddr);
11553 if (hi_irelfn != irelend)
11554 break;
11555 }
11556
11557 if (hi_irelfn == irelend)
11558 {
11559 /* Not R_NDS32_HI20_RELA. */
11560 if (i != 0)
11561 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11562 (uint64_t) irel->r_offset);
11563 return FALSE;
11564 }
11565
11566 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11567 nds32_elf_final_sda_base (sec->output_section->owner,
11568 link_info, &local_sda, FALSE);
11569
11570 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11571 {
11572 case R_NDS32_HI20_RELA:
11573 insn = bfd_getb32 (contents + laddr);
11574 access_addr =
11575 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11576
11577 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11578 {
11579 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11580 h = elf_sym_hashes (abfd)[indx];
11581 }
11582
11583 /* Try movi. */
11584 if (range_type == NDS32_LOADSTORE_IMM
11585 && access_addr < CONSERVATIVE_20BIT
11586 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11587 {
11588 eliminate_sethi = 1;
11589 break;
11590 }
11591
11592 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11593 {
11594 eliminate_sethi = 1;
11595 break;
11596 }
11597 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11598 NULL, FALSE, table, isymbuf, symtab_hdr))
11599 return FALSE;
11600
11601 if (!load_store_relax)
11602 return FALSE;
11603
11604 /* Case for set gp register. */
11605 if (N32_RT5 (insn) == REG_GP)
11606 return FALSE;
11607
11608 if (range_type == NDS32_LOADSTORE_FLOAT_S
11609 || range_type == NDS32_LOADSTORE_FLOAT_D)
11610 {
11611 range_l = sdata_range[0][0];
11612 range_h = sdata_range[0][1];
11613 }
11614 else
11615 {
11616 range_l = sdata_range[1][0];
11617 range_h = sdata_range[1][1];
11618 }
11619 break;
11620
11621 default:
11622 return FALSE;
11623 }
11624
11625 /* Delete sethi instruction. */
11626 if (eliminate_sethi == 1
11627 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11628 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11629 {
11630 hi_irelfn->r_info =
11631 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11632 irel->r_info =
11633 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11634 *insn_len = 0;
11635 return TRUE;
11636 }
11637
11638 return FALSE;
11639 }
11640
11641 /* Relax LO12 relocation for nds32_elf_relax_section. */
11642
11643 static void
11644 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11645 asection *sec, Elf_Internal_Rela *irel,
11646 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11647 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11648 struct elf_nds32_link_hash_table *table)
11649 {
11650 uint32_t insn;
11651 bfd_vma local_sda, laddr;
11652 unsigned long reloc;
11653 bfd_vma access_addr;
11654 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11655 Elf_Internal_Rela *irelfn = NULL, *irelend;
11656 struct elf_link_hash_entry *h = NULL;
11657 int indx;
11658
11659 /* For SDA base relative relaxation. */
11660 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11661 &local_sda, FALSE);
11662
11663 irelend = internal_relocs + sec->reloc_count;
11664 laddr = irel->r_offset;
11665 insn = bfd_getb32 (contents + laddr);
11666
11667 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11668 return;
11669
11670 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11671
11672 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11673 {
11674 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11675 h = elf_sym_hashes (abfd)[indx];
11676 }
11677
11678 /* Try movi. */
11679 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11680 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11681 {
11682 reloc = R_NDS32_20_RELA;
11683 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11684 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11685 bfd_putb32 (insn, contents + laddr);
11686 }
11687 else
11688 {
11689 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11690 {
11691 /* Fall through. */
11692 }
11693 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11694 FALSE, table, isymbuf, symtab_hdr))
11695 return;
11696
11697 range_l = sdata_range[1][0];
11698 range_h = sdata_range[1][1];
11699 switch (ELF32_R_TYPE (irel->r_info))
11700 {
11701 case R_NDS32_LO12S0_RELA:
11702 reloc = R_NDS32_SDA19S0_RELA;
11703 break;
11704 case R_NDS32_LO12S1_RELA:
11705 reloc = R_NDS32_SDA18S1_RELA;
11706 break;
11707 case R_NDS32_LO12S2_RELA:
11708 reloc = R_NDS32_SDA17S2_RELA;
11709 break;
11710 case R_NDS32_LO12S2_DP_RELA:
11711 range_l = sdata_range[0][0];
11712 range_h = sdata_range[0][1];
11713 reloc = R_NDS32_SDA12S2_DP_RELA;
11714 break;
11715 case R_NDS32_LO12S2_SP_RELA:
11716 range_l = sdata_range[0][0];
11717 range_h = sdata_range[0][1];
11718 reloc = R_NDS32_SDA12S2_SP_RELA;
11719 break;
11720 default:
11721 return;
11722 }
11723
11724 /* There are range_h and range_l because linker has to promise
11725 all sections move cross one page together. */
11726 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11727 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11728 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11729 {
11730 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11731 {
11732 /* Maybe we should add R_NDS32_INSN16 reloc type here
11733 or manually do some optimization. sethi can't be
11734 eliminated when updating $gp so the relative ori
11735 needs to be preserved. */
11736 return;
11737 }
11738 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11739 &insn))
11740 return;
11741 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11742 bfd_putb32 (insn, contents + laddr);
11743
11744 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11745 R_NDS32_INSN16);
11746 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11747 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11748 irelfn->r_info =
11749 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11750
11751 }
11752 }
11753 return;
11754 }
11755
11756 /* Relax PTR relocation for nds32_elf_relax_section. */
11757
11758 static bfd_boolean
11759 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11760 Elf_Internal_Rela *internal_relocs, int *insn_len,
11761 int *seq_len, bfd_byte *contents)
11762 {
11763 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11764
11765 irelend = internal_relocs + sec->reloc_count;
11766
11767 re_irel =
11768 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11769 R_NDS32_PTR_RESOLVED, irel->r_addend);
11770
11771 if (re_irel == irelend)
11772 {
11773 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11774 (uint64_t) irel->r_offset);
11775 return FALSE;
11776 }
11777
11778 if (re_irel->r_addend != 1)
11779 return FALSE;
11780
11781 /* Pointed target is relaxed and no longer needs this void *,
11782 change the type to NONE. */
11783 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11784
11785 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11786 not exist, it means only count 1 and remove it directly. */
11787 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11788 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11789 R_NDS32_PTR_COUNT);
11790 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11791 R_NDS32_PTR);
11792 if (count_irel != irelend)
11793 {
11794 if (--count_irel->r_addend > 0)
11795 return FALSE;
11796 }
11797
11798 if (ptr_irel != irelend)
11799 return FALSE;
11800
11801 /* If the PTR_COUNT is already 0, remove current instruction. */
11802 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11803 *insn_len = 0;
11804 return TRUE;
11805 }
11806
11807 /* Relax LWC relocation for nds32_elf_relax_section. */
11808
11809 static void
11810 nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11811 asection *sec, Elf_Internal_Rela *irel,
11812 Elf_Internal_Rela *internal_relocs,
11813 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11814 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11815 {
11816 /* Pattern:
11817 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11818 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11819 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11820 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11821 ... */
11822
11823 uint32_t insn;
11824 bfd_vma local_sda, laddr;
11825 unsigned long reloc;
11826 bfd_vma access_addr, flsi_offset;
11827 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11828 Elf_Internal_Rela *irelend, *re_irel;
11829 unsigned int opcode;
11830
11831 irelend = internal_relocs + sec->reloc_count;
11832 laddr = irel->r_offset;
11833 insn = bfd_getb32 (contents + laddr);
11834
11835 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11836 return;
11837
11838 /* Can not do relaxation for bi format. */
11839 if ((insn & 0x1000))
11840 return;
11841
11842 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11843 opcode = N32_OP6 (insn);
11844 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11845 reloc = R_NDS32_SDA12S2_SP_RELA;
11846 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11847 reloc = R_NDS32_SDA12S2_DP_RELA;
11848 else
11849 return;
11850
11851 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11852 R_NDS32_PTR_RESOLVED);
11853 if (re_irel == irelend)
11854 {
11855 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11856 (uint64_t) irel->r_offset);
11857 return;
11858 }
11859
11860 /* For SDA base relative relaxation. */
11861 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11862 &local_sda, FALSE);
11863 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11864 flsi_offset = (insn & 0xfff) << 2;
11865 access_addr += flsi_offset;
11866 range_l = sdata_range[0][0];
11867 range_h = sdata_range[0][1];
11868
11869 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11870 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11871 {
11872 /* Turn flsi instruction into sda access format. */
11873 insn = (insn & 0x7ff07000) | (REG_GP << 15);
11874
11875 /* Add relocation type to flsi. */
11876 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11877 irel->r_addend += flsi_offset;
11878 bfd_putb32 (insn, contents + re_irel->r_offset);
11879
11880 re_irel->r_addend |= 1;
11881 *again = TRUE;
11882 }
11883 }
11884
11885 static bfd_boolean
11886 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11887 Elf_Internal_Rela *internal_relocs,
11888 bfd_byte *contents,
11889 nds32_elf_blank_t **relax_blank_list,
11890 int optimize, int opt_size)
11891 {
11892 /* This code block is used to adjust 4-byte alignment by relax a pair
11893 of instruction a time.
11894
11895 It recognizes three types of relocations.
11896 1. R_NDS32_LABEL - a alignment.
11897 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11898 3. is_16bit_NOP () - remove a 16-bit instruction. */
11899
11900 /* TODO: It seems currently implementation only support 4-byte alignment.
11901 We should handle any-alignment. */
11902
11903 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11904 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11905 Elf_Internal_Rela rel_temp;
11906 Elf_Internal_Rela *irelend;
11907 bfd_vma address;
11908 uint16_t insn16;
11909
11910 /* Checking for branch relaxation relies on the relocations to
11911 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11912 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11913 sizeof (Elf_Internal_Rela), compar_reloc);
11914
11915 irelend = internal_relocs + sec->reloc_count;
11916
11917 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11918 /* FIXME: Can we generate the right order in assembler?
11919 So we don't have to swapping them here. */
11920
11921 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11922 label_rel < irelend; label_rel++)
11923 {
11924 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11925 continue;
11926
11927 /* Find the first reloc has the same offset with label_rel. */
11928 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11929 insn_rel++;
11930
11931 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11932 insn_rel++)
11933 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11934 address. */
11935 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11936 break;
11937
11938 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11939 && insn_rel < label_rel)
11940 {
11941 /* Swap the two reloc if the R_NDS32_INSN16 is
11942 before R_NDS32_LABEL. */
11943 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11944 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11945 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11946 }
11947 }
11948
11949 label_rel = NULL;
11950 insn_rel = NULL;
11951 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11952 or higher, remove other R_NDS32_LABEL with lower alignment.
11953 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11954 then the R_NDS32_LABEL sequence is broke. */
11955 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11956 {
11957 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11958 {
11959 if (label_rel == NULL)
11960 {
11961 if (tmp_rel->r_addend < 2)
11962 label_rel = tmp_rel;
11963 continue;
11964 }
11965 else if (tmp_rel->r_addend > 1)
11966 {
11967 /* Remove all LABEL relocation from label_rel to tmp_rel
11968 including relocations with same offset as tmp_rel. */
11969 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
11970 {
11971 if (tmp2_rel->r_offset == tmp_rel->r_offset)
11972 break;
11973
11974 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11975 && tmp2_rel->r_addend < 2)
11976 tmp2_rel->r_info =
11977 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11978 R_NDS32_NONE);
11979 }
11980 label_rel = NULL;
11981 }
11982 }
11983 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11984 {
11985 /* A new INSN16 which can be converted, so clear label_rel. */
11986 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11987 irelend, &insn16)
11988 || is_16bit_NOP (abfd, sec, tmp_rel))
11989 label_rel = NULL;
11990 }
11991 }
11992
11993 label_rel = NULL;
11994 insn_rel = NULL;
11995 /* Optimized for speed and nothing has not been relaxed.
11996 It's time to align labels.
11997 We may convert a 16-bit instruction right before a label to
11998 32-bit, in order to align the label if necessary
11999 all reloc entries has been sorted by r_offset. */
12000 for (irel = internal_relocs;
12001 irel < irelend && irel->r_offset < sec->size; irel++)
12002 {
12003 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12004 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12005 continue;
12006
12007 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12008 {
12009 /* A new INSN16 found, resize the old one. */
12010 if (is_convert_32_to_16
12011 (abfd, sec, irel, internal_relocs, irelend, &insn16)
12012 || is_16bit_NOP (abfd, sec, irel))
12013 {
12014 if (insn_rel)
12015 {
12016 /* Previous INSN16 reloc exists, reduce its
12017 size to 16-bit. */
12018 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12019 irelend, &insn16))
12020 {
12021 nds32_elf_write_16 (abfd, contents, insn_rel,
12022 internal_relocs, irelend, insn16);
12023
12024 if (!insert_nds32_elf_blank_recalc_total
12025 (relax_blank_list, insn_rel->r_offset + 2, 2))
12026 return FALSE;
12027 }
12028 else if (is_16bit_NOP (abfd, sec, insn_rel))
12029 {
12030 if (!insert_nds32_elf_blank_recalc_total
12031 (relax_blank_list, insn_rel->r_offset, 2))
12032 return FALSE;
12033 }
12034 insn_rel->r_info =
12035 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12036 }
12037 /* Save the new one for later use. */
12038 insn_rel = irel;
12039 }
12040 else
12041 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12042 R_NDS32_NONE);
12043 }
12044 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12045 {
12046 /* Search for label. */
12047 int force_relax = 0;
12048
12049 /* Label on 16-bit instruction or optimization
12050 needless, just reset this reloc. */
12051 insn16 = bfd_getb16 (contents + irel->r_offset);
12052 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12053 {
12054 irel->r_info =
12055 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12056 continue;
12057 }
12058
12059 address =
12060 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12061 irel->r_offset, 1);
12062
12063 if (!insn_rel)
12064 {
12065 /* Check if there is case which can not be aligned. */
12066 if (irel->r_addend == 2 && address & 0x2)
12067 return FALSE;
12068 continue;
12069 }
12070
12071 /* Try to align this label. */
12072
12073 if ((irel->r_addend & 0x1f) < 2)
12074 {
12075 /* Check if there is a INSN16 at the same address.
12076 Label_rel always seats before insn_rel after
12077 our sort. */
12078
12079 /* Search for INSN16 at LABEL location. If INSN16 is at
12080 same location and this LABEL alignment is lower than 2,
12081 the INSN16 can be converted to 2-byte. */
12082 for (tmp_rel = irel;
12083 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12084 tmp_rel++)
12085 {
12086 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12087 && (is_convert_32_to_16
12088 (abfd, sec, tmp_rel, internal_relocs,
12089 irelend, &insn16)
12090 || is_16bit_NOP (abfd, sec, tmp_rel)))
12091 {
12092 force_relax = 1;
12093 break;
12094 }
12095 }
12096 }
12097
12098 if (force_relax || irel->r_addend == 1 || address & 0x2)
12099 {
12100 /* Label not aligned. */
12101 /* Previous reloc exists, reduce its size to 16-bit. */
12102 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12103 internal_relocs, irelend, &insn16))
12104 {
12105 nds32_elf_write_16 (abfd, contents, insn_rel,
12106 internal_relocs, irelend, insn16);
12107
12108 if (!insert_nds32_elf_blank_recalc_total
12109 (relax_blank_list, insn_rel->r_offset + 2, 2))
12110 return FALSE;
12111 }
12112 else if (is_16bit_NOP (abfd, sec, insn_rel))
12113 {
12114 if (!insert_nds32_elf_blank_recalc_total
12115 (relax_blank_list, insn_rel->r_offset, 2))
12116 return FALSE;
12117 }
12118
12119 }
12120 /* INSN16 reloc is used. */
12121 insn_rel = NULL;
12122 }
12123 }
12124
12125 address =
12126 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12127 if (insn_rel && (address & 0x2 || opt_size))
12128 {
12129 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12130 irelend, &insn16))
12131 {
12132 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12133 irelend, insn16);
12134 if (!insert_nds32_elf_blank_recalc_total
12135 (relax_blank_list, insn_rel->r_offset + 2, 2))
12136 return FALSE;
12137 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12138 R_NDS32_NONE);
12139 }
12140 else if (is_16bit_NOP (abfd, sec, insn_rel))
12141 {
12142 if (!insert_nds32_elf_blank_recalc_total
12143 (relax_blank_list, insn_rel->r_offset, 2))
12144 return FALSE;
12145 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12146 R_NDS32_NONE);
12147 }
12148 }
12149 insn_rel = NULL;
12150 return TRUE;
12151 }
12152
12153 static bfd_boolean
12154 nds32_elf_relax_section (bfd *abfd, asection *sec,
12155 struct bfd_link_info *link_info, bfd_boolean *again)
12156 {
12157 nds32_elf_blank_t *relax_blank_list = NULL;
12158 Elf_Internal_Shdr *symtab_hdr;
12159 Elf_Internal_Rela *internal_relocs;
12160 Elf_Internal_Rela *irel;
12161 Elf_Internal_Rela *irelend;
12162 Elf_Internal_Sym *isymbuf = NULL;
12163 bfd_byte *contents = NULL;
12164 bfd_boolean result = TRUE;
12165 int optimize = 0;
12166 int opt_size = 0;
12167 uint32_t insn;
12168 uint16_t insn16;
12169
12170 /* Target dependnet option. */
12171 struct elf_nds32_link_hash_table *table;
12172 int load_store_relax;
12173
12174 relax_blank_list = NULL;
12175
12176 *again = FALSE;
12177
12178 /* Nothing to do for
12179 * relocatable link or
12180 * non-relocatable section or
12181 * non-code section or
12182 * empty content or
12183 * no reloc entry. */
12184 if (bfd_link_relocatable (link_info)
12185 || (sec->flags & SEC_RELOC) == 0
12186 || (sec->flags & SEC_EXCLUDE) != 0
12187 || (sec->flags & SEC_CODE) == 0
12188 || sec->size == 0
12189 || sec->reloc_count == 0)
12190 return TRUE;
12191
12192 /* 09.12.11 Workaround. */
12193 /* We have to adjust align for R_NDS32_LABEL if needed.
12194 The adjust approach only can fix 2-byte align once. */
12195 if (sec->alignment_power > 2)
12196 return TRUE;
12197
12198 /* Do TLS model conversion once at first. */
12199 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12200
12201 /* The optimization type to do. */
12202
12203 table = nds32_elf_hash_table (link_info);
12204
12205 /* Save the first section for abs symbol relaxation.
12206 This is used for checking gp relaxation in the
12207 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12208 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12209 table, NULL, NULL);
12210
12211 /* The begining of general relaxation. */
12212
12213 if (is_SDA_BASE_set == 0)
12214 {
12215 bfd_vma gp;
12216 is_SDA_BASE_set = 1;
12217 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12218 &gp, FALSE);
12219 relax_range_measurement (abfd);
12220 }
12221
12222 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12223 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12224 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12225 TRUE /* keep_memory */);
12226 if (internal_relocs == NULL)
12227 goto error_return;
12228
12229 irelend = internal_relocs + sec->reloc_count;
12230 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12231 irelend, R_NDS32_RELAX_ENTRY);
12232
12233 if (irel == irelend)
12234 return TRUE;
12235
12236 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12237 {
12238 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12239 return TRUE;
12240
12241 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12242 optimize = 1;
12243
12244 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12245 opt_size = 1;
12246 }
12247
12248 load_store_relax = table->load_store_relax;
12249
12250 /* Get symbol table and section content. */
12251 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12252 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12253 goto error_return;
12254
12255 /* Do relax loop only when finalize is not done.
12256 Take care of relaxable relocs except INSN16. */
12257 for (irel = internal_relocs; irel < irelend; irel++)
12258 {
12259 int seq_len; /* Original length of instruction sequence. */
12260 int insn_len = 0; /* Final length of instruction sequence. */
12261 bfd_boolean removed;
12262
12263 insn = 0;
12264 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12265 && (irel->r_addend & 0x1f) >= 2)
12266 optimize = 1;
12267
12268 /* Relocation Types
12269 R_NDS32_LONGCALL1 53
12270 R_NDS32_LONGCALL2 54
12271 R_NDS32_LONGCALL3 55
12272 R_NDS32_LONGJUMP1 56
12273 R_NDS32_LONGJUMP2 57
12274 R_NDS32_LONGJUMP3 58
12275 R_NDS32_LOADSTORE 59 */
12276 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12277 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12278 seq_len = GET_SEQ_LEN (irel->r_addend);
12279
12280 /* Relocation Types
12281 R_NDS32_LONGCALL4 107
12282 R_NDS32_LONGCALL5 108
12283 R_NDS32_LONGCALL6 109
12284 R_NDS32_LONGJUMP4 110
12285 R_NDS32_LONGJUMP5 111
12286 R_NDS32_LONGJUMP6 112
12287 R_NDS32_LONGJUMP7 113 */
12288 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12289 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12290 seq_len = 4;
12291
12292 /* Relocation Types
12293 R_NDS32_LO12S0_RELA 30
12294 R_NDS32_LO12S1_RELA 29
12295 R_NDS32_LO12S2_RELA 28
12296 R_NDS32_LO12S2_SP_RELA 71
12297 R_NDS32_LO12S2_DP_RELA 70
12298 R_NDS32_GOT_LO12 46
12299 R_NDS32_GOTOFF_LO12 50
12300 R_NDS32_PLTREL_LO12 65
12301 R_NDS32_PLT_GOTREL_LO12 67
12302 R_NDS32_17IFC_PCREL_RELA 96
12303 R_NDS32_GOT_SUFF 193
12304 R_NDS32_GOTOFF_SUFF 194
12305 R_NDS32_PLT_GOT_SUFF 195
12306 R_NDS32_MULCALL_SUFF 196
12307 R_NDS32_PTR 197 */
12308 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12309 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12310 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12311 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12312 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12313 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12314 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12315 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12316 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12317 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12318 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12319 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12320 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12321 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12322 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12323 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12324 seq_len = 0;
12325 else
12326 continue;
12327
12328 insn_len = seq_len;
12329 removed = FALSE;
12330
12331 switch (ELF32_R_TYPE (irel->r_info))
12332 {
12333 case R_NDS32_LONGCALL1:
12334 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12335 &insn_len, contents, isymbuf,
12336 symtab_hdr);
12337 break;
12338 case R_NDS32_LONGCALL2:
12339 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12340 &insn_len, contents, isymbuf,
12341 symtab_hdr);
12342 break;
12343 case R_NDS32_LONGCALL3:
12344 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12345 &insn_len, contents, isymbuf,
12346 symtab_hdr);
12347 break;
12348 case R_NDS32_LONGJUMP1:
12349 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12350 &insn_len, contents, isymbuf,
12351 symtab_hdr);
12352 break;
12353 case R_NDS32_LONGJUMP2:
12354 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12355 &insn_len, contents, isymbuf,
12356 symtab_hdr);
12357 break;
12358 case R_NDS32_LONGJUMP3:
12359 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12360 &insn_len, contents, isymbuf,
12361 symtab_hdr);
12362 break;
12363 case R_NDS32_LONGCALL4:
12364 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12365 &insn_len, contents, isymbuf,
12366 symtab_hdr);
12367 break;
12368 case R_NDS32_LONGCALL5:
12369 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12370 &insn_len, contents, isymbuf,
12371 symtab_hdr);
12372 break;
12373 case R_NDS32_LONGCALL6:
12374 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12375 &insn_len, contents, isymbuf,
12376 symtab_hdr);
12377 break;
12378 case R_NDS32_LONGJUMP4:
12379 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12380 &insn_len, contents, isymbuf,
12381 symtab_hdr);
12382 break;
12383 case R_NDS32_LONGJUMP5:
12384 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12385 &insn_len, &seq_len, contents,
12386 isymbuf, symtab_hdr);
12387 break;
12388 case R_NDS32_LONGJUMP6:
12389 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12390 &insn_len, &seq_len, contents,
12391 isymbuf, symtab_hdr);
12392 break;
12393 case R_NDS32_LONGJUMP7:
12394 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12395 &insn_len, &seq_len, contents,
12396 isymbuf, symtab_hdr);
12397 break;
12398 case R_NDS32_LOADSTORE:
12399 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12400 internal_relocs, &insn_len,
12401 contents, isymbuf, symtab_hdr,
12402 load_store_relax, table);
12403 break;
12404 case R_NDS32_LO12S0_RELA:
12405 case R_NDS32_LO12S1_RELA:
12406 case R_NDS32_LO12S2_RELA:
12407 case R_NDS32_LO12S2_DP_RELA:
12408 case R_NDS32_LO12S2_SP_RELA:
12409 /* Relax for low part. */
12410 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12411 contents, isymbuf, symtab_hdr, table);
12412
12413 /* It is impossible to delete blank, so just continue. */
12414 continue;
12415 case R_NDS32_PTR:
12416 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12417 &insn_len, &seq_len, contents);
12418 break;
12419 case R_NDS32_LSI:
12420 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12421 contents, isymbuf, symtab_hdr, again);
12422 continue;
12423 case R_NDS32_GOT_LO12:
12424 case R_NDS32_GOTOFF_LO12:
12425 case R_NDS32_PLTREL_LO12:
12426 case R_NDS32_PLT_GOTREL_LO12:
12427 case R_NDS32_GOTPC_LO12:
12428 case R_NDS32_TLS_LE_LO12:
12429 case R_NDS32_TLS_LE_ADD:
12430 case R_NDS32_TLS_LE_LS:
12431 case R_NDS32_PLT_GOT_SUFF:
12432 case R_NDS32_GOT_SUFF:
12433 case R_NDS32_GOTOFF_SUFF:
12434 continue;
12435 default:
12436 continue;
12437 }
12438
12439 if (removed && seq_len - insn_len > 0)
12440 {
12441 if (!insert_nds32_elf_blank
12442 (&relax_blank_list, irel->r_offset + insn_len,
12443 seq_len - insn_len))
12444 goto error_return;
12445 *again = TRUE;
12446 }
12447 }
12448
12449 calc_nds32_blank_total (relax_blank_list);
12450
12451 if (table->relax_fp_as_gp)
12452 {
12453 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12454 irelend, isymbuf))
12455 goto error_return;
12456
12457 if (!*again)
12458 {
12459 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12460 irelend))
12461 goto error_return;
12462 }
12463 }
12464
12465 if (!*again)
12466 {
12467 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12468 &relax_blank_list, optimize, opt_size))
12469 goto error_return;
12470 }
12471
12472 /* It doesn't matter optimize_for_space_no_align anymore.
12473 If object file is assembled with flag '-Os',
12474 the we don't adjust jump-destination on 4-byte boundary. */
12475
12476 if (relax_blank_list)
12477 {
12478 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12479 relax_blank_list = NULL;
12480 }
12481
12482 if (!*again)
12483 {
12484 /* Closing the section, so we don't relax it anymore. */
12485 bfd_vma sec_size_align;
12486 Elf_Internal_Rela *tmp_rel;
12487
12488 /* Pad to alignment boundary. Only handle current section alignment. */
12489 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12490 & ((-1U) << sec->alignment_power);
12491 if ((sec_size_align - sec->size) & 0x2)
12492 {
12493 insn16 = NDS32_NOP16;
12494 bfd_putb16 (insn16, contents + sec->size);
12495 sec->size += 2;
12496 }
12497
12498 while (sec_size_align != sec->size)
12499 {
12500 insn = NDS32_NOP32;
12501 bfd_putb32 (insn, contents + sec->size);
12502 sec->size += 4;
12503 }
12504
12505 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12506 irelend, R_NDS32_RELAX_ENTRY);
12507 if (tmp_rel != irelend)
12508 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12509
12510 clean_nds32_elf_blank ();
12511 }
12512
12513 finish:
12514 if (elf_section_data (sec)->relocs != internal_relocs)
12515 free (internal_relocs);
12516
12517 if (elf_section_data (sec)->this_hdr.contents != contents)
12518 free (contents);
12519
12520 if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12521 free (isymbuf);
12522
12523 return result;
12524
12525 error_return:
12526 result = FALSE;
12527 goto finish;
12528 }
12529
12530 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12531 {
12532 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12533 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12534 {NULL, 0, 0, 0, 0}
12535 };
12536
12537 static bfd_boolean
12538 nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12539 {
12540 const char *name = hdr->bfd_section->name;
12541
12542 if (strncmp (name, ".sbss", 5) == 0
12543 || strncmp (name, ".sdata", 6) == 0)
12544 hdr->bfd_section->flags |= SEC_SMALL_DATA;
12545
12546 return TRUE;
12547 }
12548
12549 static bfd_boolean
12550 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12551 struct bfd_link_info *info,
12552 void *finfo ATTRIBUTE_UNUSED,
12553 bfd_boolean (*func) (void *, const char *,
12554 Elf_Internal_Sym *,
12555 asection *,
12556 struct elf_link_hash_entry *)
12557 ATTRIBUTE_UNUSED)
12558 {
12559 FILE *sym_ld_script = NULL;
12560 struct elf_nds32_link_hash_table *table;
12561
12562 table = nds32_elf_hash_table (info);
12563 sym_ld_script = table->sym_ld_script;
12564
12565 if (check_start_export_sym)
12566 fprintf (sym_ld_script, "}\n");
12567
12568 return TRUE;
12569 }
12570
12571 static enum elf_reloc_type_class
12572 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12573 const asection *rel_sec ATTRIBUTE_UNUSED,
12574 const Elf_Internal_Rela *rela)
12575 {
12576 switch ((int) ELF32_R_TYPE (rela->r_info))
12577 {
12578 case R_NDS32_RELATIVE:
12579 return reloc_class_relative;
12580 case R_NDS32_JMP_SLOT:
12581 return reloc_class_plt;
12582 case R_NDS32_COPY:
12583 return reloc_class_copy;
12584 default:
12585 return reloc_class_normal;
12586 }
12587 }
12588
12589 /* Put target dependent option into info hash table. */
12590 void
12591 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12592 int relax_fp_as_gp,
12593 int eliminate_gc_relocs,
12594 FILE * sym_ld_script,
12595 int hyper_relax,
12596 int tls_desc_trampoline,
12597 int load_store_relax)
12598 {
12599 struct elf_nds32_link_hash_table *table;
12600
12601 table = nds32_elf_hash_table (link_info);
12602 if (table == NULL)
12603 return;
12604
12605 table->relax_fp_as_gp = relax_fp_as_gp;
12606 table->eliminate_gc_relocs = eliminate_gc_relocs;
12607 table->sym_ld_script = sym_ld_script;
12608 table->hyper_relax = hyper_relax;
12609 table->tls_desc_trampoline = tls_desc_trampoline;
12610 table ->load_store_relax = load_store_relax;
12611 }
12612 \f
12613
12614 /* These functions and data-structures are used for fp-as-gp
12615 optimization. */
12616
12617 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12618 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12619 the read-only section and read-write section. */
12620 #define FAG_WINDOW (508 - 32)
12621
12622 /* An nds32_fag represent a gp-relative access.
12623 We find best fp-base by using a sliding window
12624 to find a base address which can cover most gp-access. */
12625 struct nds32_fag
12626 {
12627 struct nds32_fag *next; /* NULL-teminated linked list. */
12628 bfd_vma addr; /* The address of this fag. */
12629 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12630 It is used for applying FP7U2_FLAG. */
12631 int count; /* How many times this address is referred.
12632 There should be exactly `count' relocations
12633 in relas. */
12634 int relas_capcity; /* The buffer size of relas.
12635 We use an array instead of linked-list,
12636 and realloc is used to adjust buffer size. */
12637 };
12638
12639 static void
12640 nds32_fag_init (struct nds32_fag *head)
12641 {
12642 memset (head, 0, sizeof (struct nds32_fag));
12643 }
12644
12645 static void
12646 nds32_fag_verify (struct nds32_fag *head)
12647 {
12648 struct nds32_fag *iter;
12649 struct nds32_fag *prev;
12650
12651 prev = NULL;
12652 iter = head->next;
12653 while (iter)
12654 {
12655 if (prev && prev->addr >= iter->addr)
12656 puts ("Bug in fp-as-gp insertion.");
12657 prev = iter;
12658 iter = iter->next;
12659 }
12660 }
12661
12662 /* Insert a fag in ascending order.
12663 If a fag of the same address already exists,
12664 they are chained by relas array. */
12665
12666 static void
12667 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12668 Elf_Internal_Rela * rel)
12669 {
12670 struct nds32_fag *iter;
12671 struct nds32_fag *new_fag;
12672 const int INIT_RELAS_CAP = 4;
12673
12674 for (iter = head;
12675 iter->next && iter->next->addr <= addr;
12676 iter = iter->next)
12677 /* Find somewhere to insert. */ ;
12678
12679 /* `iter' will be equal to `head' if the list is empty. */
12680 if (iter != head && iter->addr == addr)
12681 {
12682 /* The address exists in the list.
12683 Insert `rel' into relocation list, relas. */
12684
12685 /* Check whether relas is big enough. */
12686 if (iter->count >= iter->relas_capcity)
12687 {
12688 iter->relas_capcity *= 2;
12689 iter->relas = bfd_realloc
12690 (iter->relas, iter->relas_capcity * sizeof (void *));
12691 }
12692 iter->relas[iter->count++] = rel;
12693 return;
12694 }
12695
12696 /* This is a new address. Create a fag node for it. */
12697 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12698 memset (new_fag, 0, sizeof (*new_fag));
12699 new_fag->addr = addr;
12700 new_fag->count = 1;
12701 new_fag->next = iter->next;
12702 new_fag->relas_capcity = INIT_RELAS_CAP;
12703 new_fag->relas = (Elf_Internal_Rela **)
12704 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12705 new_fag->relas[0] = rel;
12706 iter->next = new_fag;
12707
12708 nds32_fag_verify (head);
12709 }
12710
12711 static void
12712 nds32_fag_free_list (struct nds32_fag *head)
12713 {
12714 struct nds32_fag *iter;
12715
12716 iter = head->next;
12717 while (iter)
12718 {
12719 struct nds32_fag *tmp = iter;
12720 iter = iter->next;
12721 free (tmp->relas);
12722 tmp->relas = NULL;
12723 free (tmp);
12724 }
12725 }
12726
12727 /* Find the best fp-base address.
12728 The relocation associated with that address is returned,
12729 so we can track the symbol instead of a fixed address.
12730
12731 When relaxation, the address of an datum may change,
12732 because a text section is shrinked, so the data section
12733 moves forward. If the aligments of text and data section
12734 are different, their distance may change too.
12735 Therefore, tracking a fixed address is not appriate. */
12736
12737 static int
12738 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12739 {
12740 struct nds32_fag *base; /* First fag in the window. */
12741 struct nds32_fag *last; /* First fag outside the window. */
12742 int accu = 0; /* Usage accumulation. */
12743 struct nds32_fag *best; /* Best fag. */
12744 int baccu = 0; /* Best accumulation. */
12745
12746 /* Use first fag for initial, and find the last fag in the window.
12747
12748 In each iteration, we could simply subtract previous fag
12749 and accumulate following fags which are inside the window,
12750 untill we each the end. */
12751
12752 if (head->next == NULL)
12753 {
12754 *bestpp = NULL;
12755 return 0;
12756 }
12757
12758 /* Initialize base. */
12759 base = head->next;
12760 best = base;
12761 for (last = base;
12762 last && last->addr < base->addr + FAG_WINDOW;
12763 last = last->next)
12764 accu += last->count;
12765
12766 baccu = accu;
12767
12768 /* Record the best base in each iteration. */
12769 while (base->next)
12770 {
12771 accu -= base->count;
12772 base = base->next;
12773 /* Account fags in window. */
12774 for (/* Nothing. */;
12775 last && last->addr < base->addr + FAG_WINDOW;
12776 last = last->next)
12777 accu += last->count;
12778
12779 /* A better fp-base? */
12780 if (accu > baccu)
12781 {
12782 best = base;
12783 baccu = accu;
12784 }
12785 }
12786
12787 if (bestpp)
12788 *bestpp = best;
12789 return baccu;
12790 }
12791
12792 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12793 so we can convert it fo fp-relative access later.
12794 `best_fag' is the best fp-base. Only those inside the window
12795 of best_fag is applied the flag. */
12796
12797 static bfd_boolean
12798 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12799 asection *sec, struct nds32_fag *best_fag,
12800 Elf_Internal_Rela *internal_relocs,
12801 Elf_Internal_Rela *irelend)
12802 {
12803 struct nds32_fag *ifag;
12804 bfd_vma best_fpbase, gp;
12805 bfd *output_bfd;
12806
12807 output_bfd = sec->output_section->owner;
12808 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12809 best_fpbase = best_fag->addr;
12810
12811 if (best_fpbase > gp + sdata_range[1][1]
12812 || best_fpbase < gp - sdata_range[1][0])
12813 return FALSE;
12814
12815 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12816 so we know they can be converted to lwi37.fp. */
12817 for (ifag = best_fag;
12818 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12819 {
12820 int i;
12821
12822 for (i = 0; i < ifag->count; i++)
12823 {
12824 Elf_Internal_Rela *insn16_rel;
12825 Elf_Internal_Rela *fag_rel;
12826
12827 fag_rel = ifag->relas[i];
12828
12829 /* Only if this is within the WINDOWS, FP7U2_FLAG
12830 is applied. */
12831
12832 insn16_rel = find_relocs_at_address
12833 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12834
12835 if (insn16_rel != irelend)
12836 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12837 }
12838 }
12839 return TRUE;
12840 }
12841
12842 /* Reset INSN16 to clean fp as gp. */
12843
12844 static void
12845 nds32_fag_unmark_relax (struct nds32_fag *fag,
12846 Elf_Internal_Rela *internal_relocs,
12847 Elf_Internal_Rela *irelend)
12848 {
12849 struct nds32_fag *ifag;
12850 int i;
12851 Elf_Internal_Rela *insn16_rel;
12852 Elf_Internal_Rela *fag_rel;
12853
12854 for (ifag = fag; ifag; ifag = ifag->next)
12855 {
12856 for (i = 0; i < ifag->count; i++)
12857 {
12858 fag_rel = ifag->relas[i];
12859
12860 /* Restore the INSN16 relocation. */
12861 insn16_rel = find_relocs_at_address
12862 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12863
12864 if (insn16_rel != irelend)
12865 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12866 }
12867 }
12868 }
12869
12870 /* This is the main function of fp-as-gp optimization.
12871 It should be called by relax_section. */
12872
12873 static bfd_boolean
12874 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12875 bfd *abfd, asection *sec,
12876 Elf_Internal_Rela *internal_relocs,
12877 Elf_Internal_Rela *irelend,
12878 Elf_Internal_Sym *isymbuf)
12879 {
12880 Elf_Internal_Rela *begin_rel = NULL;
12881 Elf_Internal_Rela *irel;
12882 struct nds32_fag fag_head;
12883 Elf_Internal_Shdr *symtab_hdr;
12884 bfd_byte *contents;
12885 bfd_boolean ifc_inside = FALSE;
12886
12887 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12888
12889 /* Per-function fp-base selection.
12890 1. Create a list for all the gp-relative access.
12891 2. Base on those gp-relative address,
12892 find a fp-base which can cover most access.
12893 3. Use the fp-base for fp-as-gp relaxation.
12894
12895 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12896 we should
12897 1. delete the `la $fp, _FP_BASE_' instruction and
12898 2. not convert lwi.gp to lwi37.fp.
12899
12900 To delete the _FP_BASE_ instruction, we simply apply
12901 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12902
12903 To suppress the conversion, we simply NOT to apply
12904 R_NDS32_INSN16_FP7U2_FLAG flag. */
12905
12906 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12907
12908 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12909 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12910 return FALSE;
12911
12912 /* Check whether it is worth for fp-as-gp optimization,
12913 i.e., at least 3 gp-load.
12914
12915 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12916 apply this optimization. */
12917
12918 for (irel = internal_relocs; irel < irelend; irel++)
12919 {
12920 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12921 One we enter the begin of the region, we track all the LW/ST
12922 instructions, so when we leave the region, we try to find
12923 the best fp-base address for those LW/ST instructions. */
12924
12925 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12926 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12927 {
12928 /* Begin of the region. */
12929 if (begin_rel)
12930 /* xgettext:c-format */
12931 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12932
12933 begin_rel = irel;
12934 nds32_fag_init (&fag_head);
12935 ifc_inside = FALSE;
12936 }
12937 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12938 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12939 {
12940 int accu;
12941 struct nds32_fag *best_fag, *tmp_fag;
12942 int dist;
12943
12944 /* End of the region.
12945 Check whether it is worth to do fp-as-gp. */
12946
12947 if (begin_rel == NULL)
12948 {
12949 /* xgettext:c-format */
12950 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12951 abfd, sec);
12952 continue;
12953 }
12954
12955 accu = nds32_fag_find_base (&fag_head, &best_fag);
12956
12957 /* Clean FP7U2_FLAG because they may set ever. */
12958 tmp_fag = fag_head.next;
12959 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12960
12961 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12962 if (accu < FAG_THRESHOLD
12963 || !nds32_fag_mark_relax (link_info, sec, best_fag,
12964 internal_relocs, irelend))
12965 {
12966 /* Not worth to do fp-as-gp. */
12967 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12968 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12969 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12970 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12971 nds32_fag_free_list (&fag_head);
12972 begin_rel = NULL;
12973 continue;
12974 }
12975
12976 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12977 so we use it to record the distance to the reloction of best
12978 fp-base. */
12979 dist = best_fag->relas[0] - begin_rel;
12980 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12981 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12982 relocation. And get the base value when relocating. */
12983 begin_rel->r_addend &= (0x1 << 16) - 1;
12984 begin_rel->r_addend |= dist << 16;
12985
12986 nds32_fag_free_list (&fag_head);
12987 begin_rel = NULL;
12988 }
12989
12990 if (begin_rel == NULL || ifc_inside)
12991 /* Skip if we are not in the region of fp-as-gp. */
12992 continue;
12993
12994 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12995 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12996 {
12997 bfd_vma addr;
12998 uint32_t insn;
12999
13000 /* A gp-relative access is found. Insert it to the fag-list. */
13001
13002 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
13003 insn = bfd_getb32 (contents + irel->r_offset);
13004 if (!N32_IS_RT3 (insn))
13005 continue;
13006
13007 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13008 nds32_fag_insert (&fag_head, addr, irel);
13009 }
13010 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13011 {
13012 begin_rel = NULL;
13013 }
13014 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13015 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13016 {
13017 /* Suppress fp as gp when encounter ifc. */
13018 ifc_inside = TRUE;
13019 }
13020 }
13021
13022 return TRUE;
13023 }
13024
13025 /* Remove unused `la $fp, _FD_BASE_' instruction. */
13026
13027 static bfd_boolean
13028 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13029 Elf_Internal_Rela *internal_relocs,
13030 Elf_Internal_Rela *irelend)
13031 {
13032 Elf_Internal_Rela *irel;
13033 Elf_Internal_Shdr *symtab_hdr;
13034 bfd_byte *contents = NULL;
13035 nds32_elf_blank_t *relax_blank_list = NULL;
13036 bfd_boolean result = TRUE;
13037 bfd_boolean unused_region = FALSE;
13038
13039 /*
13040 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13041 R_NDS32_17IFC_PCREL_RELA
13042 R_NDS32_10IFCU_PCREL_RELA. */
13043
13044 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13045 nds32_get_section_contents (abfd, sec, &contents, TRUE);
13046
13047 for (irel = internal_relocs; irel < irelend; irel++)
13048 {
13049 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13050 we marked to in previous pass.
13051 DO NOT scan relocations again, since we've alreadly decided it
13052 and set the flag. */
13053 const char *syname;
13054 int syndx;
13055 uint32_t insn;
13056
13057 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13058 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13059 unused_region = TRUE;
13060 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13061 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13062 unused_region = FALSE;
13063
13064 /* We're not in the region. */
13065 if (!unused_region)
13066 continue;
13067
13068 /* _FP_BASE_ must be a GLOBAL symbol. */
13069 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13070 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13071 continue;
13072
13073 /* The symbol name must be _FP_BASE_. */
13074 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13075 if (strcmp (syname, FP_BASE_NAME) != 0)
13076 continue;
13077
13078 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13079 {
13080 /* addi.gp $fp, -256 */
13081 insn = bfd_getb32 (contents + irel->r_offset);
13082 if (insn != INSN_ADDIGP_TO_FP)
13083 continue;
13084 }
13085 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13086 {
13087 /* addi $fp, $gp, -256 */
13088 insn = bfd_getb32 (contents + irel->r_offset);
13089 if (insn != INSN_ADDI_GP_TO_FP)
13090 continue;
13091 }
13092 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13093 {
13094 /* movi $fp, FP_BASE */
13095 insn = bfd_getb32 (contents + irel->r_offset);
13096 if (insn != INSN_MOVI_TO_FP)
13097 continue;
13098 }
13099 else
13100 continue;
13101
13102 /* We got here because a FP_BASE instruction is found. */
13103 if (!insert_nds32_elf_blank_recalc_total
13104 (&relax_blank_list, irel->r_offset, 4))
13105 goto error_return;
13106 }
13107
13108 finish:
13109 if (relax_blank_list)
13110 {
13111 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13112 relax_blank_list = NULL;
13113 }
13114 return result;
13115
13116 error_return:
13117 result = FALSE;
13118 goto finish;
13119 }
13120
13121 /* This is a version of bfd_generic_get_relocated_section_contents.
13122 We need this variety because relaxation will modify the dwarf
13123 infomation. When there is undefined symbol reference error mesage,
13124 linker need to dump line number where the symbol be used. However
13125 the address is be relaxed, it can not get the original dwarf contents.
13126 The variety only modify function call for reading in the section. */
13127
13128 static bfd_byte *
13129 nds32_elf_get_relocated_section_contents (bfd *abfd,
13130 struct bfd_link_info *link_info,
13131 struct bfd_link_order *link_order,
13132 bfd_byte *data,
13133 bfd_boolean relocatable,
13134 asymbol **symbols)
13135 {
13136 bfd *input_bfd = link_order->u.indirect.section->owner;
13137 asection *input_section = link_order->u.indirect.section;
13138 long reloc_size;
13139 arelent **reloc_vector;
13140 long reloc_count;
13141
13142 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13143 if (reloc_size < 0)
13144 return NULL;
13145
13146 /* Read in the section. */
13147 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
13148 return NULL;
13149
13150 if (reloc_size == 0)
13151 return data;
13152
13153 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13154 if (reloc_vector == NULL)
13155 return NULL;
13156
13157 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13158 reloc_vector, symbols);
13159 if (reloc_count < 0)
13160 goto error_return;
13161
13162 if (reloc_count > 0)
13163 {
13164 arelent **parent;
13165 for (parent = reloc_vector; *parent != NULL; parent++)
13166 {
13167 char *error_message = NULL;
13168 asymbol *symbol;
13169 bfd_reloc_status_type r;
13170
13171 symbol = *(*parent)->sym_ptr_ptr;
13172 if (symbol->section && discarded_section (symbol->section))
13173 {
13174 bfd_vma off;
13175 static reloc_howto_type none_howto
13176 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13177 "unused", FALSE, 0, 0, FALSE);
13178
13179 off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13180 input_section);
13181 _bfd_clear_contents ((*parent)->howto, input_bfd,
13182 input_section, data, off);
13183 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13184 (*parent)->addend = 0;
13185 (*parent)->howto = &none_howto;
13186 r = bfd_reloc_ok;
13187 }
13188 else
13189 r = bfd_perform_relocation (input_bfd, *parent, data,
13190 input_section,
13191 relocatable ? abfd : NULL,
13192 &error_message);
13193
13194 if (relocatable)
13195 {
13196 asection *os = input_section->output_section;
13197
13198 /* A partial link, so keep the relocs. */
13199 os->orelocation[os->reloc_count] = *parent;
13200 os->reloc_count++;
13201 }
13202
13203 if (r != bfd_reloc_ok)
13204 {
13205 switch (r)
13206 {
13207 case bfd_reloc_undefined:
13208 (*link_info->callbacks->undefined_symbol)
13209 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13210 input_bfd, input_section, (*parent)->address, TRUE);
13211 break;
13212 case bfd_reloc_dangerous:
13213 BFD_ASSERT (error_message != NULL);
13214 (*link_info->callbacks->reloc_dangerous)
13215 (link_info, error_message,
13216 input_bfd, input_section, (*parent)->address);
13217 break;
13218 case bfd_reloc_overflow:
13219 (*link_info->callbacks->reloc_overflow)
13220 (link_info, NULL,
13221 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13222 (*parent)->howto->name, (*parent)->addend,
13223 input_bfd, input_section, (*parent)->address);
13224 break;
13225 case bfd_reloc_outofrange:
13226 /* PR ld/13730:
13227 This error can result when processing some partially
13228 complete binaries. Do not abort, but issue an error
13229 message instead. */
13230 link_info->callbacks->einfo
13231 /* xgettext:c-format */
13232 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13233 abfd, input_section, * parent);
13234 goto error_return;
13235
13236 default:
13237 abort ();
13238 break;
13239 }
13240 }
13241 }
13242 }
13243
13244 free (reloc_vector);
13245 return data;
13246
13247 error_return:
13248 free (reloc_vector);
13249 return NULL;
13250 }
13251
13252 /* Check target symbol. */
13253
13254 static bfd_boolean
13255 nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13256 {
13257 if (!sym || !sym->name || sym->name[0] != '$')
13258 return FALSE;
13259 return TRUE;
13260 }
13261
13262 /* nds32 find maybe function sym. Ignore target special symbol
13263 first, and then go the general function. */
13264
13265 static bfd_size_type
13266 nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13267 bfd_vma *code_off)
13268 {
13269 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13270 return 0;
13271
13272 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13273 }
13274 \f
13275
13276 /* Do TLS model conversion. */
13277
13278 typedef struct relax_group_list_t
13279 {
13280 Elf_Internal_Rela *relo;
13281 struct relax_group_list_t *next;
13282 struct relax_group_list_t *next_sibling;
13283 int id;
13284 } relax_group_list_t;
13285
13286 int
13287 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13288
13289 int
13290 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13291
13292 void
13293 dump_chain (relax_group_list_t *pHead);
13294
13295 int
13296 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13297 {
13298 relax_group_list_t *pNext = pHead;
13299
13300 /* Find place. */
13301 while (pNext->next)
13302 {
13303 if (pNext->next->id > (int) pElem->r_addend)
13304 break;
13305
13306 pNext = pNext->next;
13307 }
13308
13309 /* Insert node. */
13310 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13311 if (!pNew)
13312 return FALSE;
13313
13314 relax_group_list_t *tmp = pNext->next;
13315 pNext->next = pNew;
13316
13317 pNew->id = pElem->r_addend;
13318 pNew->relo = pElem;
13319 pNew->next = tmp;
13320 pNew->next_sibling = NULL;
13321
13322 return TRUE;
13323 }
13324
13325 int
13326 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13327 {
13328 relax_group_list_t *pNext = pNode;
13329
13330 /* Find place. */
13331 while (pNext->next_sibling)
13332 {
13333 pNext = pNext->next_sibling;
13334 }
13335
13336 /* Insert node. */
13337 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13338 if (!pNew)
13339 return FALSE;
13340
13341 relax_group_list_t *tmp = pNext->next_sibling;
13342 pNext->next_sibling = pNew;
13343
13344 pNew->id = -1;
13345 pNew->relo = pElem;
13346 pNew->next = NULL;
13347 pNew->next_sibling = tmp;
13348
13349 return TRUE;
13350 }
13351
13352 void
13353 dump_chain (relax_group_list_t *pHead)
13354 {
13355 relax_group_list_t *pNext = pHead->next;
13356 while (pNext)
13357 {
13358 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13359 relax_group_list_t *pNextSib = pNext->next_sibling;
13360 while (pNextSib)
13361 {
13362 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13363 pNextSib = pNextSib->next_sibling;
13364 }
13365 pNext = pNext->next;
13366 printf("\n");
13367 }
13368 }
13369
13370 /* Check R_NDS32_RELAX_GROUP of each section.
13371 There might be multiple sections in one object file. */
13372
13373 int
13374 elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13375 {
13376 elf32_nds32_relax_group_t *relax_group_ptr =
13377 elf32_nds32_relax_group_ptr (abfd);
13378
13379 int min_id = relax_group_ptr->min_id;
13380 int max_id = relax_group_ptr->max_id;
13381
13382 Elf_Internal_Rela *rel;
13383 Elf_Internal_Rela *relend;
13384 Elf_Internal_Rela *relocs;
13385 enum elf_nds32_reloc_type rtype;
13386
13387 do
13388 {
13389 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13390 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13391 TRUE /* keep_memory */);
13392 if (relocs == NULL)
13393 break;
13394
13395 /* Check R_NDS32_RELAX_GROUP. */
13396 relend = relocs + asec->reloc_count;
13397 for (rel = relocs; rel < relend; rel++)
13398 {
13399 int id;
13400 rtype = ELF32_R_TYPE (rel->r_info);
13401 if (rtype != R_NDS32_RELAX_GROUP)
13402 continue;
13403
13404 id = rel->r_addend;
13405 if (id < min_id)
13406 min_id = id;
13407 else if (id > max_id)
13408 max_id = id;
13409 }
13410 }
13411 while (FALSE);
13412
13413 if (elf_section_data (asec)->relocs != relocs)
13414 free (relocs);
13415
13416 if ((min_id != relax_group_ptr->min_id)
13417 || (max_id != relax_group_ptr->max_id))
13418 {
13419 relax_group_ptr->count = max_id - min_id + 1;
13420 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13421 relax_group_ptr->min_id = min_id;
13422 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13423 relax_group_ptr->max_id = max_id;
13424 }
13425
13426 return relax_group_ptr->count;
13427 }
13428
13429 /* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13430 struct section_id_list_t *relax_group_section_id_list = NULL;
13431
13432 struct section_id_list_t *
13433 elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13434 {
13435 struct section_id_list_t *result = NULL;
13436 struct section_id_list_t *lst = *lst_ptr;
13437
13438 if (NULL == lst)
13439 {
13440 result = (struct section_id_list_t *) calloc
13441 (1, sizeof (struct section_id_list_t));
13442 BFD_ASSERT (result); /* Feed me. */
13443 result->id = id;
13444 *lst_ptr = result;
13445 }
13446 else
13447 {
13448 struct section_id_list_t *cur = lst;
13449 struct section_id_list_t *prv = NULL;
13450 struct section_id_list_t *sec = NULL;
13451
13452 while (cur)
13453 {
13454 if (cur->id < id)
13455 {
13456 prv = cur;
13457 cur = cur->next;
13458 continue;
13459 }
13460
13461 if (cur->id > id)
13462 {
13463 cur = NULL; /* To insert after prv. */
13464 sec = cur; /* In case prv == NULL. */
13465 }
13466
13467 break;
13468 }
13469
13470 if (NULL == cur)
13471 {
13472 /* Insert after prv. */
13473 result = (struct section_id_list_t *) calloc
13474 (1, sizeof (struct section_id_list_t));
13475 BFD_ASSERT (result); /* Feed me. */
13476 result->id = id;
13477 if (NULL != prv)
13478 {
13479 result->next = prv->next;
13480 prv->next = result;
13481 }
13482 else
13483 {
13484 *lst_ptr = result;
13485 result->next = sec;
13486 }
13487 }
13488 }
13489
13490 return result;
13491 }
13492
13493 int
13494 elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13495 {
13496 static int next_relax_group_bias = 0;
13497
13498 elf32_nds32_relax_group_t *relax_group_ptr =
13499 elf32_nds32_relax_group_ptr (abfd);
13500
13501 bfd_boolean result = TRUE;
13502 Elf_Internal_Rela *rel;
13503 Elf_Internal_Rela *relend;
13504 Elf_Internal_Rela *relocs = NULL;
13505 enum elf_nds32_reloc_type rtype;
13506 struct section_id_list_t *node = NULL;
13507 int count = 0;
13508
13509 do
13510 {
13511 if (0 == relax_group_ptr->count)
13512 break;
13513
13514 /* Check if this section has been handled. */
13515 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13516 if (NULL == node)
13517 break; /* Hit, the section id has handled. */
13518
13519 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13520 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13521 TRUE /* keep_memory */);
13522 if (relocs == NULL)
13523 {
13524 BFD_ASSERT (0); /* feed me */
13525 break;
13526 }
13527
13528 /* Allocate group id bias for this bfd! */
13529 if (0 == relax_group_ptr->init)
13530 {
13531 relax_group_ptr->bias = next_relax_group_bias;
13532 next_relax_group_bias += relax_group_ptr->count;
13533 relax_group_ptr->init = 1;
13534 }
13535
13536 /* Reorder relax group groups. */
13537 relend = relocs + asec->reloc_count;
13538 for (rel = relocs; rel < relend; rel++)
13539 {
13540 rtype = ELF32_R_TYPE(rel->r_info);
13541 if (rtype != R_NDS32_RELAX_GROUP)
13542 continue;
13543
13544 /* Change it. */
13545 rel->r_addend += relax_group_ptr->bias;
13546 /* Debugging count. */
13547 count++;
13548 }
13549 }
13550 while (FALSE);
13551
13552 if (elf_section_data (asec)->relocs != relocs)
13553 free (relocs);
13554
13555 return result;
13556 }
13557
13558 int
13559 nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13560 struct bfd_link_info *lnkinfo)
13561 {
13562 bfd_boolean result = TRUE;
13563 Elf_Internal_Rela *irel;
13564 Elf_Internal_Rela *irelend;
13565 Elf_Internal_Rela *internal_relocs;
13566 unsigned long r_symndx;
13567 enum elf_nds32_reloc_type r_type;
13568
13569 Elf_Internal_Sym *local_syms = NULL;
13570 bfd_byte *contents = NULL;
13571
13572 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13573
13574 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13575 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13576 sym_hashes = elf_sym_hashes (inbfd);
13577 sym_hashes_end =
13578 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13579 if (!elf_bad_symtab (inbfd))
13580 sym_hashes_end -= symtab_hdr->sh_info;
13581
13582 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13583 if (bfd_link_relocatable (lnkinfo))
13584 {
13585 elf32_nds32_unify_relax_group (inbfd, insec);
13586 return result;
13587 }
13588
13589 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13590 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13591 TRUE /* keep_memory */);
13592 if (internal_relocs == NULL)
13593 goto error_return;
13594
13595 irelend = internal_relocs + insec->reloc_count;
13596 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13597 irelend, R_NDS32_RELAX_ENTRY);
13598 if (irel == irelend)
13599 goto finish;
13600
13601 /* Chain/remove groups. */
13602 for (irel = internal_relocs; irel < irelend; irel++)
13603 {
13604 r_symndx = ELF32_R_SYM (irel->r_info);
13605 r_type = ELF32_R_TYPE (irel->r_info);
13606 if (r_type != R_NDS32_RELAX_GROUP)
13607 continue;
13608
13609 /* Remove it. */
13610 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13611 /* Chain it now. */
13612 if (!list_insert (&chain, irel))
13613 goto error_return;
13614 }
13615
13616 /* Collect group relocations. */
13617 /* Presume relocations are sorted. */
13618 relax_group_list_t *pNext = chain.next;
13619 while (pNext)
13620 {
13621 for (irel = internal_relocs; irel < irelend; irel++)
13622 {
13623 if (irel->r_offset == pNext->relo->r_offset)
13624 {
13625 /* Ignore Non-TLS relocation types. */
13626 r_type = ELF32_R_TYPE (irel->r_info);
13627 if ((R_NDS32_TLS_LE_HI20 > r_type)
13628 || (R_NDS32_RELAX_ENTRY == r_type))
13629 continue;
13630
13631 if (!list_insert_sibling (pNext, irel))
13632 goto error_return;
13633 }
13634 else if (irel->r_offset > pNext->relo->r_offset)
13635 {
13636 pNext = pNext->next;
13637 if (!pNext)
13638 break;
13639
13640 bfd_vma current_offset = pNext->relo->r_offset;
13641 if (irel->r_offset > current_offset)
13642 irel = internal_relocs; /* restart from head */
13643 else
13644 --irel; /* Check current irel again. */
13645 continue;
13646 }
13647 else
13648 {
13649 /* This shouldn't be reached. */
13650 }
13651 }
13652 if (pNext)
13653 pNext = pNext->next;
13654 }
13655
13656 #ifdef DUBUG_VERBOSE
13657 dump_chain(&chain);
13658 #endif
13659
13660 /* Get symbol table and section content. */
13661 if (incontents)
13662 contents = incontents;
13663 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13664 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13665 goto error_return;
13666
13667 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13668
13669 /* Convert TLS model each group if necessary. */
13670 pNext = chain.next;
13671
13672 int cur_grp_id = -1;
13673 int sethi_rt = -1;
13674 int add_rt = -1;
13675 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13676
13677 tls_type = org_tls_type = eff_tls_type = 0;
13678
13679 while (pNext)
13680 {
13681 relax_group_list_t *pNextSig = pNext->next_sibling;
13682 while (pNextSig)
13683 {
13684 struct elf_link_hash_entry *h = NULL;
13685
13686 irel = pNextSig->relo;
13687 r_symndx = ELF32_R_SYM(irel->r_info);
13688 r_type = ELF32_R_TYPE(irel->r_info);
13689
13690 if (pNext->id != cur_grp_id)
13691 {
13692 cur_grp_id = pNext->id;
13693 org_tls_type = get_tls_type (r_type, NULL);
13694 if (r_symndx >= symtab_hdr->sh_info)
13695 {
13696 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13697 while (h->root.type == bfd_link_hash_indirect
13698 || h->root.type == bfd_link_hash_warning)
13699 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13700 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13701 }
13702 else
13703 {
13704 tls_type = local_got_tls_type
13705 ? local_got_tls_type[r_symndx]
13706 : GOT_NORMAL;
13707 }
13708
13709 eff_tls_type = 1 << (fls (tls_type) - 1);
13710 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13711 }
13712
13713 if (eff_tls_type != org_tls_type)
13714 {
13715 switch (org_tls_type)
13716 {
13717 /* DESC to IEGP/IE/LE. */
13718 case GOT_TLS_DESC:
13719 switch (eff_tls_type)
13720 {
13721 case GOT_TLS_IE:
13722 switch (r_type)
13723 {
13724 case R_NDS32_TLS_DESC_HI20:
13725 irel->r_info = ELF32_R_INFO(r_symndx,
13726 R_NDS32_TLS_IE_HI20);
13727 break;
13728 case R_NDS32_TLS_DESC_LO12:
13729 irel->r_info = ELF32_R_INFO(r_symndx,
13730 R_NDS32_TLS_IE_LO12);
13731 break;
13732 case R_NDS32_TLS_DESC_ADD:
13733 {
13734 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13735 add_rt = N32_RT5 (insn);
13736 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13737 bfd_putb32 (insn, contents + irel->r_offset);
13738
13739 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13740 }
13741 break;
13742 case R_NDS32_TLS_DESC_FUNC:
13743 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13744 irel->r_info = ELF32_R_INFO(r_symndx,
13745 R_NDS32_RELAX_REMOVE);
13746 break;
13747 case R_NDS32_TLS_DESC_CALL:
13748 {
13749 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13750 REG_TP);
13751 bfd_putb32 (insn, contents + irel->r_offset);
13752
13753 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13754 }
13755 break;
13756 case R_NDS32_LOADSTORE:
13757 case R_NDS32_PTR:
13758 case R_NDS32_PTR_RESOLVED:
13759 case R_NDS32_NONE:
13760 case R_NDS32_LABEL:
13761 break;
13762 default:
13763 BFD_ASSERT(0);
13764 break;
13765 }
13766 break;
13767 case GOT_TLS_IEGP:
13768 switch (r_type)
13769 {
13770 case R_NDS32_TLS_DESC_HI20:
13771 irel->r_info = ELF32_R_INFO(r_symndx,
13772 R_NDS32_TLS_IEGP_HI20);
13773 break;
13774 case R_NDS32_TLS_DESC_LO12:
13775 irel->r_info = ELF32_R_INFO(r_symndx,
13776 R_NDS32_TLS_IEGP_LO12);
13777 break;
13778 case R_NDS32_TLS_DESC_ADD:
13779 {
13780 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13781 add_rt = N32_RT5 (insn);
13782 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13783 bfd_putb32 (insn, contents + irel->r_offset);
13784
13785 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13786 }
13787 break;
13788 case R_NDS32_TLS_DESC_FUNC:
13789 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13790 irel->r_info = ELF32_R_INFO(r_symndx,
13791 R_NDS32_RELAX_REMOVE);
13792 break;
13793 case R_NDS32_TLS_DESC_CALL:
13794 {
13795 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13796 REG_TP);
13797 bfd_putb32 (insn, contents + irel->r_offset);
13798
13799 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13800 }
13801 break;
13802 case R_NDS32_LOADSTORE:
13803 case R_NDS32_PTR:
13804 case R_NDS32_PTR_RESOLVED:
13805 case R_NDS32_NONE:
13806 case R_NDS32_LABEL:
13807 break;
13808 default:
13809 BFD_ASSERT(0);
13810 break;
13811 }
13812 break;
13813 case GOT_TLS_LE:
13814 switch (r_type)
13815 {
13816 case R_NDS32_TLS_DESC_HI20:
13817 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13818 break;
13819 case R_NDS32_TLS_DESC_LO12:
13820 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13821 break;
13822 case R_NDS32_TLS_DESC_ADD:
13823 {
13824 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13825
13826 add_rt = N32_RT5 (insn);
13827 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13828 bfd_putb32 (insn, contents + irel->r_offset);
13829
13830 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13831 }
13832 break;
13833 case R_NDS32_TLS_DESC_FUNC:
13834 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13835 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13836 break;
13837 case R_NDS32_TLS_DESC_CALL:
13838 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13839 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13840 break;
13841 case R_NDS32_LOADSTORE:
13842 case R_NDS32_PTR:
13843 case R_NDS32_PTR_RESOLVED:
13844 case R_NDS32_NONE:
13845 case R_NDS32_LABEL:
13846 break;
13847 default:
13848 BFD_ASSERT(0);
13849 break;
13850 }
13851 break;
13852 default:
13853 break;
13854 }
13855 break;
13856 /* IEGP to IE/LE. */
13857 case GOT_TLS_IEGP:
13858 switch (eff_tls_type)
13859 {
13860 case GOT_TLS_IE:
13861 switch (r_type)
13862 {
13863 case R_NDS32_TLS_IEGP_HI20:
13864 irel->r_info = ELF32_R_INFO(r_symndx,
13865 R_NDS32_TLS_IE_HI20);
13866 break;
13867 case R_NDS32_TLS_IEGP_LO12:
13868 irel->r_info = ELF32_R_INFO(r_symndx,
13869 R_NDS32_TLS_IE_LO12);
13870 break;
13871 case R_NDS32_PTR_RESOLVED:
13872 {
13873 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13874
13875 add_rt = N32_RT5 (insn);
13876 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13877 bfd_putb32 (insn, contents + irel->r_offset);
13878 }
13879 break;
13880 case R_NDS32_TLS_IEGP_LW:
13881 break;
13882 case R_NDS32_LOADSTORE:
13883 case R_NDS32_PTR:
13884 case R_NDS32_NONE:
13885 case R_NDS32_LABEL:
13886 break;
13887 default:
13888 BFD_ASSERT(0);
13889 break;
13890 }
13891 break;
13892 case GOT_TLS_LE:
13893 switch (r_type)
13894 {
13895 case R_NDS32_TLS_IEGP_HI20:
13896 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13897 break;
13898 case R_NDS32_TLS_IEGP_LO12:
13899 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13900 break;
13901 case R_NDS32_TLS_IEGP_LW:
13902 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13903 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13904 break;
13905 case R_NDS32_LOADSTORE:
13906 case R_NDS32_PTR:
13907 case R_NDS32_NONE:
13908 case R_NDS32_LABEL:
13909 case R_NDS32_PTR_RESOLVED:
13910 break;
13911 default:
13912 BFD_ASSERT(0);
13913 break;
13914 }
13915 break;
13916 default:
13917 break;
13918 }
13919 break;
13920 /* IE to LE. */
13921 case GOT_TLS_IE:
13922 switch (eff_tls_type)
13923 {
13924 case GOT_TLS_LE:
13925 switch (r_type)
13926 {
13927 case R_NDS32_TLS_IE_HI20:
13928 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13929 break;
13930 case R_NDS32_TLS_IE_LO12S2:
13931 {
13932 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13933
13934 add_rt = N32_RT5 (insn);
13935 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13936 bfd_putb32 (insn, contents + irel->r_offset);
13937
13938 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13939 }
13940 break;
13941 case R_NDS32_LOADSTORE:
13942 case R_NDS32_PTR:
13943 case R_NDS32_NONE:
13944 case R_NDS32_LABEL:
13945 break;
13946 default:
13947 BFD_ASSERT(0);
13948 break;
13949 }
13950 break;
13951 default:
13952 break;
13953 }
13954 break;
13955 default:
13956 break;
13957 }
13958 }
13959 pNextSig = pNextSig->next_sibling;
13960 }
13961
13962 #if 1
13963 pNext = pNext->next;
13964 #else
13965 while (pNext)
13966 {
13967 if (pNext->id != cur_grp_id)
13968 break;
13969 pNext = pNext->next;
13970 }
13971 #endif
13972 }
13973
13974 finish:
13975 if (incontents)
13976 contents = NULL;
13977
13978 if (elf_section_data (insec)->relocs != internal_relocs)
13979 free (internal_relocs);
13980
13981 if (elf_section_data (insec)->this_hdr.contents != contents)
13982 free (contents);
13983
13984 if (symtab_hdr->contents != (bfd_byte *) local_syms)
13985 free (local_syms);
13986
13987 if (chain.next)
13988 {
13989 pNext = chain.next;
13990 relax_group_list_t *pDel;
13991 while (pNext)
13992 {
13993 pDel = pNext;
13994 pNext = pNext->next;
13995 free (pDel);
13996 }
13997 }
13998
13999 return result;
14000
14001 error_return:
14002 result = FALSE;
14003 goto finish;
14004 }
14005
14006 /* End TLS model conversion. */
14007
14008 #define ELF_ARCH bfd_arch_nds32
14009 #define ELF_MACHINE_CODE EM_NDS32
14010 #define ELF_MAXPAGESIZE 0x1000
14011 #define ELF_TARGET_ID NDS32_ELF_DATA
14012
14013 #define TARGET_BIG_SYM nds32_elf32_be_vec
14014 #define TARGET_BIG_NAME "elf32-nds32be"
14015 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
14016 #define TARGET_LITTLE_NAME "elf32-nds32le"
14017
14018 #define elf_info_to_howto nds32_info_to_howto
14019 #define elf_info_to_howto_rel nds32_info_to_howto_rel
14020
14021 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14022 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14023 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14024 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14025 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14026
14027 #define bfd_elf32_mkobject nds32_elf_mkobject
14028 #define elf_backend_action_discarded nds32_elf_action_discarded
14029 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14030 #define elf_backend_check_relocs nds32_elf_check_relocs
14031 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14032 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14033 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14034 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14035 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14036 #define elf_backend_relocate_section nds32_elf_relocate_section
14037 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
14038 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14039 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14040 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14041 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14042 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14043 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14044 #define elf_backend_object_p nds32_elf_object_p
14045 #define elf_backend_final_write_processing nds32_elf_final_write_processing
14046 #define elf_backend_special_sections nds32_elf_special_sections
14047 #define elf_backend_section_flags nds32_elf_section_flags
14048 #define bfd_elf32_bfd_get_relocated_section_contents \
14049 nds32_elf_get_relocated_section_contents
14050 #define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14051 #define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
14052
14053 #define elf_backend_can_gc_sections 1
14054 #define elf_backend_can_refcount 1
14055 #define elf_backend_want_got_plt 1
14056 #define elf_backend_plt_readonly 1
14057 #define elf_backend_want_plt_sym 0
14058 #define elf_backend_got_header_size 12
14059 #define elf_backend_may_use_rel_p 1
14060 #define elf_backend_default_use_rela_p 1
14061 #define elf_backend_may_use_rela_p 1
14062 #define elf_backend_dtrel_excludes_plt 0
14063
14064 #include "elf32-target.h"
14065
14066 #undef ELF_MAXPAGESIZE
14067 #define ELF_MAXPAGESIZE 0x2000
14068
14069 #undef TARGET_BIG_SYM
14070 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
14071 #undef TARGET_BIG_NAME
14072 #define TARGET_BIG_NAME "elf32-nds32be-linux"
14073 #undef TARGET_LITTLE_SYM
14074 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
14075 #undef TARGET_LITTLE_NAME
14076 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
14077 #undef elf32_bed
14078 #define elf32_bed elf32_nds32_lin_bed
14079
14080 #include "elf32-target.h"
This page took 0.378744 seconds and 4 git commands to generate.