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