ELF: Don't check relocations in non-loaded, non-alloced sections
[deliverable/binutils-gdb.git] / bfd / elf32-nds32.c
1 /* NDS32-specific support for 32-bit ELF.
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
3 Contributed by Andes Technology Corporation.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "libiberty.h"
29 #include "elf/nds32.h"
30 #include "opcode/nds32.h"
31 #include "elf32-nds32.h"
32 #include "opcode/cgen.h"
33 #include "../opcodes/nds32-opc.h"
34
35 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
36 #define OCTETS_PER_BYTE(ABFD, SEC) 1
37
38 /* Relocation HOWTO functions. */
39 static bfd_reloc_status_type nds32_elf_ignore_reloc
40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type nds32_elf_hi20_reloc
44 (bfd *, arelent *, asymbol *, void *,
45 asection *, bfd *, char **);
46 static bfd_reloc_status_type nds32_elf_lo12_reloc
47 (bfd *, arelent *, asymbol *, void *,
48 asection *, bfd *, char **);
49 static bfd_reloc_status_type nds32_elf_generic_reloc
50 (bfd *, arelent *, asymbol *, void *,
51 asection *, bfd *, char **);
52 static bfd_reloc_status_type nds32_elf_sda15_reloc
53 (bfd *, arelent *, asymbol *, void *,
54 asection *, bfd *, char **);
55
56 /* Helper functions for HOWTO. */
57 static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
58 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
59 asection *, bfd_vma, bfd_vma);
60
61 /* Nds32 helper functions. */
62 static bfd_vma calculate_memory_address
63 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
64 static int nds32_get_section_contents (bfd *, asection *,
65 bfd_byte **, bfd_boolean);
66 static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67 Elf_Internal_Sym **);
68 static bfd_boolean nds32_relax_fp_as_gp
69 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
70 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
71 Elf_Internal_Sym *isymbuf);
72 static bfd_boolean nds32_fag_remove_unused_fpbase
73 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74 Elf_Internal_Rela *irelend);
75
76 enum
77 {
78 MACH_V1 = bfd_mach_n1h,
79 MACH_V2 = bfd_mach_n1h_v2,
80 MACH_V3 = bfd_mach_n1h_v3,
81 MACH_V3M = bfd_mach_n1h_v3m
82 };
83
84 #define MIN(a, b) ((a) > (b) ? (b) : (a))
85 #define MAX(a, b) ((a) > (b) ? (a) : (b))
86
87 /* The name of the dynamic interpreter. This is put in the .interp
88 section. */
89 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
90
91 #define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92 && (flags) & SEC_LOAD \
93 && (flags) & SEC_READONLY)
94
95 /* The nop opcode we use. */
96 #define NDS32_NOP32 0x40000009
97 #define NDS32_NOP16 0x9200
98
99 /* The size in bytes of an entry in the procedure linkage table. */
100 #define PLT_ENTRY_SIZE 24
101 #define PLT_HEADER_SIZE 24
102
103 /* The first entry in a procedure linkage table are reserved,
104 and the initial contents are unimportant (we zero them out).
105 Subsequent entries look like this. */
106 #define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
107 #define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
108 #define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
109 #define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
110 #define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
111
112 /* $ta is change to $r15 (from $r25). */
113 #define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
114 #define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
115 #define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
116 #define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
117 #define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
118 #define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
119
120 #define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
121 #define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
122 #define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
123 #define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
124 #define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
125
126 #define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
127 #define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
128 #define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
129 #define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
130 #define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
131 #define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
132
133 /* These are macros used to get the relocation accurate value. */
134 #define ACCURATE_8BIT_S1 (0x100)
135 #define ACCURATE_U9BIT_S1 (0x400)
136 #define ACCURATE_12BIT_S1 (0x2000)
137 #define ACCURATE_14BIT_S1 (0x4000)
138 #define ACCURATE_19BIT (0x40000)
139
140 /* These are macros used to get the relocation conservative value. */
141 #define CONSERVATIVE_8BIT_S1 (0x100 - 4)
142 #define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143 #define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144 #define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
145 /* These must be more conservative because the address may be in
146 different segment. */
147 #define CONSERVATIVE_15BIT (0x4000 - 0x1000)
148 #define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
149 #define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
150 #define CONSERVATIVE_19BIT (0x40000 - 0x1000)
151 #define CONSERVATIVE_20BIT (0x80000 - 0x1000)
152
153 /* Size of small data/bss sections, used to calculate SDA_BASE. */
154 static long got_size = 0;
155 static int is_SDA_BASE_set = 0;
156
157 /* Convert ELF-VER in eflags to string for debugging purpose. */
158 static const char *const nds32_elfver_strtab[] =
159 {
160 "ELF-1.2",
161 "ELF-1.3",
162 "ELF-1.4",
163 };
164
165 /* The nds32 linker needs to keep track of the number of relocs that it
166 decides to copy in check_relocs for each symbol. This is so that
167 it can discard PC relative relocs if it doesn't need them when
168 linking with -Bsymbolic. We store the information in a field
169 extending the regular ELF linker hash table. */
170
171 /* This structure keeps track of the number of PC relative relocs we
172 have copied for a given symbol. */
173
174 struct elf_nds32_pcrel_relocs_copied
175 {
176 /* Next section. */
177 struct elf_nds32_pcrel_relocs_copied *next;
178 /* A section in dynobj. */
179 asection *section;
180 /* Number of relocs copied in this section. */
181 bfd_size_type count;
182 };
183
184 enum elf_nds32_tls_type
185 {
186 GOT_UNKNOWN = (0),
187 GOT_NORMAL = (1 << 0),
188 GOT_TLS_LE = (1 << 1),
189 GOT_TLS_IE = (1 << 2),
190 GOT_TLS_IEGP = (1 << 3),
191 GOT_TLS_LD = (1 << 4),
192 GOT_TLS_GD = (1 << 5),
193 GOT_TLS_DESC = (1 << 6),
194 };
195
196 /* Nds32 ELF linker hash entry. */
197
198 struct elf_nds32_link_hash_entry
199 {
200 struct elf_link_hash_entry root;
201
202 /* For checking relocation type. */
203 enum elf_nds32_tls_type tls_type;
204
205 int offset_to_gp;
206 };
207
208 /* Get the nds32 ELF linker hash table from a link_info structure. */
209
210 #define FP_BASE_NAME "_FP_BASE_"
211 static int check_start_export_sym = 0;
212
213 /* The offset for executable tls relaxation. */
214 #define TP_OFFSET 0x0
215
216 typedef struct
217 {
218 int min_id;
219 int max_id;
220 int count;
221 int bias;
222 int init;
223 } elf32_nds32_relax_group_t;
224
225 struct elf_nds32_obj_tdata
226 {
227 struct elf_obj_tdata root;
228
229 /* tls_type for each local got entry. */
230 char *local_got_tls_type;
231
232 /* GOTPLT entries for TLS descriptors. */
233 bfd_vma *local_tlsdesc_gotent;
234
235 /* for R_NDS32_RELAX_GROUP handling. */
236 elf32_nds32_relax_group_t relax_group;
237
238 unsigned int hdr_size;
239 int* offset_to_gp;
240 };
241
242 #define elf_nds32_tdata(bfd) \
243 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
244
245 #define elf32_nds32_local_got_tls_type(bfd) \
246 (elf_nds32_tdata (bfd)->local_got_tls_type)
247
248 #define elf32_nds32_local_gp_offset(bfd) \
249 (elf_nds32_tdata (bfd)->offset_to_gp)
250
251 #define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
252
253 #define elf32_nds32_relax_group_ptr(bfd) \
254 &(elf_nds32_tdata (bfd)->relax_group)
255
256 static bfd_boolean
257 nds32_elf_mkobject (bfd *abfd)
258 {
259 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
260 NDS32_ELF_DATA);
261 }
262
263 /* Relocations used for relocation. */
264 /* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
265 initialize array nds32_elf_howto_table in any order. The benefit
266 is that we can add any new relocations with any numbers and don't
267 need to fill the gap by lots of EMPTY_HOWTO. */
268 #define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
269 [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
270
271 static reloc_howto_type nds32_elf_howto_table[] =
272 {
273 /* This reloc does nothing. */
274 HOWTO2 (R_NDS32_NONE, /* type */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 32, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_bitfield,/* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_NDS32_NONE", /* name */
283 FALSE, /* partial_inplace */
284 0, /* src_mask */
285 0, /* dst_mask */
286 FALSE), /* pcrel_offset */
287
288 /* A 16 bit absolute relocation. */
289 HOWTO2 (R_NDS32_16, /* type */
290 0, /* rightshift */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
292 16, /* bitsize */
293 FALSE, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_bitfield,/* complain_on_overflow */
296 nds32_elf_generic_reloc,/* special_function */
297 "R_NDS32_16", /* name */
298 FALSE, /* partial_inplace */
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
301 FALSE), /* pcrel_offset */
302
303 /* A 32 bit absolute relocation. */
304 HOWTO2 (R_NDS32_32, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 32, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield,/* complain_on_overflow */
311 nds32_elf_generic_reloc,/* special_function */
312 "R_NDS32_32", /* name */
313 FALSE, /* partial_inplace */
314 0xffffffff, /* src_mask */
315 0xffffffff, /* dst_mask */
316 FALSE), /* pcrel_offset */
317
318 /* A 20 bit address. */
319 HOWTO2 (R_NDS32_20, /* type */
320 0, /* rightshift */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
322 20, /* bitsize */
323 FALSE, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_unsigned,/* complain_on_overflow */
326 nds32_elf_generic_reloc,/* special_function */
327 "R_NDS32_20", /* name */
328 FALSE, /* partial_inplace */
329 0xfffff, /* src_mask */
330 0xfffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
332
333 /* An PC Relative 9-bit relocation, shifted by 2.
334 This reloc is complicated because relocations are relative to pc & -4.
335 i.e. branches in the right insn slot use the address of the left insn
336 slot for pc. */
337 /* It's not clear whether this should have partial_inplace set or not.
338 Branch relaxing in the assembler can store the addend in the insn,
339 and if bfd_install_relocation gets called the addend may get added
340 again. */
341 HOWTO2 (R_NDS32_9_PCREL, /* type */
342 1, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 8, /* bitsize */
345 TRUE, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_signed,/* complain_on_overflow */
348 nds32_elf_9_pcrel_reloc,/* special_function */
349 "R_NDS32_9_PCREL", /* name */
350 FALSE, /* partial_inplace */
351 0xff, /* src_mask */
352 0xff, /* dst_mask */
353 TRUE), /* pcrel_offset */
354
355 /* A relative 15 bit relocation, right shifted by 1. */
356 HOWTO2 (R_NDS32_15_PCREL, /* type */
357 1, /* rightshift */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
359 14, /* bitsize */
360 TRUE, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_signed,/* complain_on_overflow */
363 bfd_elf_generic_reloc, /* special_function */
364 "R_NDS32_15_PCREL", /* name */
365 FALSE, /* partial_inplace */
366 0x3fff, /* src_mask */
367 0x3fff, /* dst_mask */
368 TRUE), /* pcrel_offset */
369
370 /* A relative 17 bit relocation, right shifted by 1. */
371 HOWTO2 (R_NDS32_17_PCREL, /* type */
372 1, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 16, /* bitsize */
375 TRUE, /* pc_relative */
376 0, /* bitpos */
377 complain_overflow_signed,/* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 "R_NDS32_17_PCREL", /* name */
380 FALSE, /* partial_inplace */
381 0xffff, /* src_mask */
382 0xffff, /* dst_mask */
383 TRUE), /* pcrel_offset */
384
385 /* A relative 25 bit relocation, right shifted by 1. */
386 /* It's not clear whether this should have partial_inplace set or not.
387 Branch relaxing in the assembler can store the addend in the insn,
388 and if bfd_install_relocation gets called the addend may get added
389 again. */
390 HOWTO2 (R_NDS32_25_PCREL, /* type */
391 1, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 24, /* bitsize */
394 TRUE, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_signed,/* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 "R_NDS32_25_PCREL", /* name */
399 FALSE, /* partial_inplace */
400 0xffffff, /* src_mask */
401 0xffffff, /* dst_mask */
402 TRUE), /* pcrel_offset */
403
404 /* High 20 bits of address when lower 12 is or'd in. */
405 HOWTO2 (R_NDS32_HI20, /* type */
406 12, /* rightshift */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
408 20, /* bitsize */
409 FALSE, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_dont,/* complain_on_overflow */
412 nds32_elf_hi20_reloc, /* special_function */
413 "R_NDS32_HI20", /* name */
414 FALSE, /* partial_inplace */
415 0x000fffff, /* src_mask */
416 0x000fffff, /* dst_mask */
417 FALSE), /* pcrel_offset */
418
419 /* Lower 12 bits of address. */
420 HOWTO2 (R_NDS32_LO12S3, /* type */
421 3, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 9, /* bitsize */
424 FALSE, /* pc_relative */
425 0, /* bitpos */
426 complain_overflow_dont,/* complain_on_overflow */
427 nds32_elf_lo12_reloc, /* special_function */
428 "R_NDS32_LO12S3", /* name */
429 FALSE, /* partial_inplace */
430 0x000001ff, /* src_mask */
431 0x000001ff, /* dst_mask */
432 FALSE), /* pcrel_offset */
433
434 /* Lower 12 bits of address. */
435 HOWTO2 (R_NDS32_LO12S2, /* type */
436 2, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 10, /* bitsize */
439 FALSE, /* pc_relative */
440 0, /* bitpos */
441 complain_overflow_dont,/* complain_on_overflow */
442 nds32_elf_lo12_reloc, /* special_function */
443 "R_NDS32_LO12S2", /* name */
444 FALSE, /* partial_inplace */
445 0x000003ff, /* src_mask */
446 0x000003ff, /* dst_mask */
447 FALSE), /* pcrel_offset */
448
449 /* Lower 12 bits of address. */
450 HOWTO2 (R_NDS32_LO12S1, /* type */
451 1, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 11, /* bitsize */
454 FALSE, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_dont,/* complain_on_overflow */
457 nds32_elf_lo12_reloc, /* special_function */
458 "R_NDS32_LO12S1", /* name */
459 FALSE, /* partial_inplace */
460 0x000007ff, /* src_mask */
461 0x000007ff, /* dst_mask */
462 FALSE), /* pcrel_offset */
463
464 /* Lower 12 bits of address. */
465 HOWTO2 (R_NDS32_LO12S0, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 12, /* bitsize */
469 FALSE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_dont,/* complain_on_overflow */
472 nds32_elf_lo12_reloc, /* special_function */
473 "R_NDS32_LO12S0", /* name */
474 FALSE, /* partial_inplace */
475 0x00000fff, /* src_mask */
476 0x00000fff, /* dst_mask */
477 FALSE), /* pcrel_offset */
478
479 /* Small data area 15 bits offset. */
480 HOWTO2 (R_NDS32_SDA15S3, /* type */
481 3, /* rightshift */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
483 15, /* bitsize */
484 FALSE, /* pc_relative */
485 0, /* bitpos */
486 complain_overflow_signed,/* complain_on_overflow */
487 nds32_elf_sda15_reloc, /* special_function */
488 "R_NDS32_SDA15S3", /* name */
489 FALSE, /* partial_inplace */
490 0x00007fff, /* src_mask */
491 0x00007fff, /* dst_mask */
492 FALSE), /* pcrel_offset */
493
494 /* Small data area 15 bits offset. */
495 HOWTO2 (R_NDS32_SDA15S2, /* type */
496 2, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 15, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_signed,/* complain_on_overflow */
502 nds32_elf_sda15_reloc, /* special_function */
503 "R_NDS32_SDA15S2", /* name */
504 FALSE, /* partial_inplace */
505 0x00007fff, /* src_mask */
506 0x00007fff, /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 /* Small data area 15 bits offset. */
510 HOWTO2 (R_NDS32_SDA15S1, /* type */
511 1, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 15, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_signed,/* complain_on_overflow */
517 nds32_elf_sda15_reloc, /* special_function */
518 "R_NDS32_SDA15S1", /* name */
519 FALSE, /* partial_inplace */
520 0x00007fff, /* src_mask */
521 0x00007fff, /* dst_mask */
522 FALSE), /* pcrel_offset */
523
524 /* Small data area 15 bits offset. */
525 HOWTO2 (R_NDS32_SDA15S0, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 15, /* bitsize */
529 FALSE, /* pc_relative */
530 0, /* bitpos */
531 complain_overflow_signed,/* complain_on_overflow */
532 nds32_elf_sda15_reloc, /* special_function */
533 "R_NDS32_SDA15S0", /* name */
534 FALSE, /* partial_inplace */
535 0x00007fff, /* src_mask */
536 0x00007fff, /* dst_mask */
537 FALSE), /* pcrel_offset */
538
539 /* GNU extension to record C++ vtable hierarchy */
540 HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
541 0, /* rightshift */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
543 0, /* bitsize */
544 FALSE, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_dont,/* complain_on_overflow */
547 NULL, /* special_function */
548 "R_NDS32_GNU_VTINHERIT",/* name */
549 FALSE, /* partial_inplace */
550 0, /* src_mask */
551 0, /* dst_mask */
552 FALSE), /* pcrel_offset */
553
554 /* GNU extension to record C++ vtable member usage */
555 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 0, /* bitsize */
559 FALSE, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_dont,/* complain_on_overflow */
562 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
563 "R_NDS32_GNU_VTENTRY", /* name */
564 FALSE, /* partial_inplace */
565 0, /* src_mask */
566 0, /* dst_mask */
567 FALSE), /* pcrel_offset */
568
569 /* A 16 bit absolute relocation. */
570 HOWTO2 (R_NDS32_16_RELA, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 FALSE, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_bitfield,/* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_NDS32_16_RELA", /* name */
579 FALSE, /* partial_inplace */
580 0xffff, /* src_mask */
581 0xffff, /* dst_mask */
582 FALSE), /* pcrel_offset */
583
584 /* A 32 bit absolute relocation. */
585 HOWTO2 (R_NDS32_32_RELA, /* type */
586 0, /* rightshift */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
588 32, /* bitsize */
589 FALSE, /* pc_relative */
590 0, /* bitpos */
591 complain_overflow_bitfield,/* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_NDS32_32_RELA", /* name */
594 FALSE, /* partial_inplace */
595 0xffffffff, /* src_mask */
596 0xffffffff, /* dst_mask */
597 FALSE), /* pcrel_offset */
598
599 /* A 20 bit address. */
600 HOWTO2 (R_NDS32_20_RELA, /* type */
601 0, /* rightshift */
602 2, /* size (0 = byte, 1 = short, 2 = long) */
603 20, /* bitsize */
604 FALSE, /* pc_relative */
605 0, /* bitpos */
606 complain_overflow_signed,/* complain_on_overflow */
607 bfd_elf_generic_reloc, /* special_function */
608 "R_NDS32_20_RELA", /* name */
609 FALSE, /* partial_inplace */
610 0xfffff, /* src_mask */
611 0xfffff, /* dst_mask */
612 FALSE), /* pcrel_offset */
613
614 HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
615 1, /* rightshift */
616 1, /* size (0 = byte, 1 = short, 2 = long) */
617 8, /* bitsize */
618 TRUE, /* pc_relative */
619 0, /* bitpos */
620 complain_overflow_signed,/* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_NDS32_9_PCREL_RELA",/* name */
623 FALSE, /* partial_inplace */
624 0xff, /* src_mask */
625 0xff, /* dst_mask */
626 TRUE), /* pcrel_offset */
627
628 /* A relative 15 bit relocation, right shifted by 1. */
629 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
630 1, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 14, /* bitsize */
633 TRUE, /* pc_relative */
634 0, /* bitpos */
635 complain_overflow_signed,/* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_NDS32_15_PCREL_RELA",/* name */
638 FALSE, /* partial_inplace */
639 0x3fff, /* src_mask */
640 0x3fff, /* dst_mask */
641 TRUE), /* pcrel_offset */
642
643 /* A relative 17 bit relocation, right shifted by 1. */
644 HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
645 1, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 16, /* bitsize */
648 TRUE, /* pc_relative */
649 0, /* bitpos */
650 complain_overflow_signed,/* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_NDS32_17_PCREL_RELA",/* name */
653 FALSE, /* partial_inplace */
654 0xffff, /* src_mask */
655 0xffff, /* dst_mask */
656 TRUE), /* pcrel_offset */
657
658 /* A relative 25 bit relocation, right shifted by 2. */
659 HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
660 1, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 24, /* bitsize */
663 TRUE, /* pc_relative */
664 0, /* bitpos */
665 complain_overflow_signed,/* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_NDS32_25_PCREL_RELA",/* name */
668 FALSE, /* partial_inplace */
669 0xffffff, /* src_mask */
670 0xffffff, /* dst_mask */
671 TRUE), /* pcrel_offset */
672
673 /* High 20 bits of address when lower 16 is or'd in. */
674 HOWTO2 (R_NDS32_HI20_RELA, /* type */
675 12, /* rightshift */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
677 20, /* bitsize */
678 FALSE, /* pc_relative */
679 0, /* bitpos */
680 complain_overflow_dont,/* complain_on_overflow */
681 bfd_elf_generic_reloc, /* special_function */
682 "R_NDS32_HI20_RELA", /* name */
683 FALSE, /* partial_inplace */
684 0x000fffff, /* src_mask */
685 0x000fffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
687
688 /* Lower 12 bits of address. */
689 HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
690 3, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 9, /* bitsize */
693 FALSE, /* pc_relative */
694 0, /* bitpos */
695 complain_overflow_dont,/* complain_on_overflow */
696 bfd_elf_generic_reloc, /* special_function */
697 "R_NDS32_LO12S3_RELA", /* name */
698 FALSE, /* partial_inplace */
699 0x000001ff, /* src_mask */
700 0x000001ff, /* dst_mask */
701 FALSE), /* pcrel_offset */
702
703 /* Lower 12 bits of address. */
704 HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
705 2, /* rightshift */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
707 10, /* bitsize */
708 FALSE, /* pc_relative */
709 0, /* bitpos */
710 complain_overflow_dont,/* complain_on_overflow */
711 bfd_elf_generic_reloc, /* special_function */
712 "R_NDS32_LO12S2_RELA", /* name */
713 FALSE, /* partial_inplace */
714 0x000003ff, /* src_mask */
715 0x000003ff, /* dst_mask */
716 FALSE), /* pcrel_offset */
717
718 /* Lower 12 bits of address. */
719 HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
720 1, /* rightshift */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
722 11, /* bitsize */
723 FALSE, /* pc_relative */
724 0, /* bitpos */
725 complain_overflow_dont,/* complain_on_overflow */
726 bfd_elf_generic_reloc, /* special_function */
727 "R_NDS32_LO12S1_RELA", /* name */
728 FALSE, /* partial_inplace */
729 0x000007ff, /* src_mask */
730 0x000007ff, /* dst_mask */
731 FALSE), /* pcrel_offset */
732
733 /* Lower 12 bits of address. */
734 HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
735 0, /* rightshift */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
737 12, /* bitsize */
738 FALSE, /* pc_relative */
739 0, /* bitpos */
740 complain_overflow_dont,/* complain_on_overflow */
741 bfd_elf_generic_reloc, /* special_function */
742 "R_NDS32_LO12S0_RELA", /* name */
743 FALSE, /* partial_inplace */
744 0x00000fff, /* src_mask */
745 0x00000fff, /* dst_mask */
746 FALSE), /* pcrel_offset */
747
748 /* Small data area 15 bits offset. */
749 HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
750 3, /* rightshift */
751 2, /* size (0 = byte, 1 = short, 2 = long) */
752 15, /* bitsize */
753 FALSE, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_signed,/* complain_on_overflow */
756 bfd_elf_generic_reloc, /* special_function */
757 "R_NDS32_SDA15S3_RELA",/* name */
758 FALSE, /* partial_inplace */
759 0x00007fff, /* src_mask */
760 0x00007fff, /* dst_mask */
761 FALSE), /* pcrel_offset */
762
763 /* Small data area 15 bits offset. */
764 HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
765 2, /* rightshift */
766 2, /* size (0 = byte, 1 = short, 2 = long) */
767 15, /* bitsize */
768 FALSE, /* pc_relative */
769 0, /* bitpos */
770 complain_overflow_signed,/* complain_on_overflow */
771 bfd_elf_generic_reloc, /* special_function */
772 "R_NDS32_SDA15S2_RELA",/* name */
773 FALSE, /* partial_inplace */
774 0x00007fff, /* src_mask */
775 0x00007fff, /* dst_mask */
776 FALSE), /* pcrel_offset */
777
778 HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
779 1, /* rightshift */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
781 15, /* bitsize */
782 FALSE, /* pc_relative */
783 0, /* bitpos */
784 complain_overflow_signed,/* complain_on_overflow */
785 bfd_elf_generic_reloc, /* special_function */
786 "R_NDS32_SDA15S1_RELA",/* name */
787 FALSE, /* partial_inplace */
788 0x00007fff, /* src_mask */
789 0x00007fff, /* dst_mask */
790 FALSE), /* pcrel_offset */
791
792 HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
793 0, /* rightshift */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
795 15, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_signed,/* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_NDS32_SDA15S0_RELA",/* name */
801 FALSE, /* partial_inplace */
802 0x00007fff, /* src_mask */
803 0x00007fff, /* dst_mask */
804 FALSE), /* pcrel_offset */
805
806 /* GNU extension to record C++ vtable hierarchy */
807 HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
808 0, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 0, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont,/* complain_on_overflow */
814 NULL, /* special_function */
815 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0, /* dst_mask */
819 FALSE), /* pcrel_offset */
820
821 /* GNU extension to record C++ vtable member usage */
822 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
823 0, /* rightshift */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
825 0, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont,/* complain_on_overflow */
829 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
830 "R_NDS32_RELA_GNU_VTENTRY",/* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0, /* dst_mask */
834 FALSE), /* pcrel_offset */
835
836 /* Like R_NDS32_20, but referring to the GOT table entry for
837 the symbol. */
838 HOWTO2 (R_NDS32_GOT20, /* type */
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 20, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_signed,/* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_NDS32_GOT20", /* name */
847 FALSE, /* partial_inplace */
848 0xfffff, /* src_mask */
849 0xfffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
851
852 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
853 entry for the symbol. */
854 HOWTO2 (R_NDS32_25_PLTREL, /* type */
855 1, /* rightshift */
856 2, /* size (0 = byte, 1 = short, 2 = long) */
857 24, /* bitsize */
858 TRUE, /* pc_relative */
859 0, /* bitpos */
860 complain_overflow_signed,/* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_NDS32_25_PLTREL", /* name */
863 FALSE, /* partial_inplace */
864 0xffffff, /* src_mask */
865 0xffffff, /* dst_mask */
866 TRUE), /* pcrel_offset */
867
868 /* This is used only by the dynamic linker. The symbol should exist
869 both in the object being run and in some shared library. The
870 dynamic linker copies the data addressed by the symbol from the
871 shared library into the object, because the object being
872 run has to have the data at some particular address. */
873 HOWTO2 (R_NDS32_COPY, /* type */
874 0, /* rightshift */
875 2, /* size (0 = byte, 1 = short, 2 = long) */
876 32, /* bitsize */
877 FALSE, /* pc_relative */
878 0, /* bitpos */
879 complain_overflow_bitfield,/* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_NDS32_COPY", /* name */
882 FALSE, /* partial_inplace */
883 0xffffffff, /* src_mask */
884 0xffffffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
886
887 /* Like R_NDS32_20, but used when setting global offset table
888 entries. */
889 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
890 0, /* rightshift */
891 2, /* size (0 = byte, 1 = short, 2 = long) */
892 32, /* bitsize */
893 FALSE, /* pc_relative */
894 0, /* bitpos */
895 complain_overflow_bitfield,/* complain_on_overflow */
896 bfd_elf_generic_reloc, /* special_function */
897 "R_NDS32_GLOB_DAT", /* name */
898 FALSE, /* partial_inplace */
899 0xffffffff, /* src_mask */
900 0xffffffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
902
903 /* Marks a procedure linkage table entry for a symbol. */
904 HOWTO2 (R_NDS32_JMP_SLOT, /* type */
905 0, /* rightshift */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
907 32, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_bitfield,/* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_NDS32_JMP_SLOT", /* name */
913 FALSE, /* partial_inplace */
914 0xffffffff, /* src_mask */
915 0xffffffff, /* dst_mask */
916 FALSE), /* pcrel_offset */
917
918 /* Used only by the dynamic linker. When the object is run, this
919 longword is set to the load address of the object, plus the
920 addend. */
921 HOWTO2 (R_NDS32_RELATIVE, /* type */
922 0, /* rightshift */
923 2, /* size (0 = byte, 1 = short, 2 = long) */
924 32, /* bitsize */
925 FALSE, /* pc_relative */
926 0, /* bitpos */
927 complain_overflow_bitfield,/* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_NDS32_RELATIVE", /* name */
930 FALSE, /* partial_inplace */
931 0xffffffff, /* src_mask */
932 0xffffffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
934
935 HOWTO2 (R_NDS32_GOTOFF, /* type */
936 0, /* rightshift */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
938 20, /* bitsize */
939 FALSE, /* pc_relative */
940 0, /* bitpos */
941 complain_overflow_signed,/* complain_on_overflow */
942 bfd_elf_generic_reloc, /* special_function */
943 "R_NDS32_GOTOFF", /* name */
944 FALSE, /* partial_inplace */
945 0xfffff, /* src_mask */
946 0xfffff, /* dst_mask */
947 FALSE), /* pcrel_offset */
948
949 /* An PC Relative 20-bit relocation used when setting PIC offset
950 table register. */
951 HOWTO2 (R_NDS32_GOTPC20, /* type */
952 0, /* rightshift */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
954 20, /* bitsize */
955 TRUE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_signed,/* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_NDS32_GOTPC20", /* name */
960 FALSE, /* partial_inplace */
961 0xfffff, /* src_mask */
962 0xfffff, /* dst_mask */
963 TRUE), /* pcrel_offset */
964
965 /* Like R_NDS32_HI20, but referring to the GOT table entry for
966 the symbol. */
967 HOWTO2 (R_NDS32_GOT_HI20, /* type */
968 12, /* rightshift */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
970 20, /* bitsize */
971 FALSE, /* pc_relative */
972 0, /* bitpos */
973 complain_overflow_dont,/* complain_on_overflow */
974 bfd_elf_generic_reloc, /* special_function */
975 "R_NDS32_GOT_HI20", /* name */
976 FALSE, /* partial_inplace */
977 0x000fffff, /* src_mask */
978 0x000fffff, /* dst_mask */
979 FALSE), /* pcrel_offset */
980 HOWTO2 (R_NDS32_GOT_LO12, /* type */
981 0, /* rightshift */
982 2, /* size (0 = byte, 1 = short, 2 = long) */
983 12, /* bitsize */
984 FALSE, /* pc_relative */
985 0, /* bitpos */
986 complain_overflow_dont,/* complain_on_overflow */
987 bfd_elf_generic_reloc, /* special_function */
988 "R_NDS32_GOT_LO12", /* name */
989 FALSE, /* partial_inplace */
990 0x00000fff, /* src_mask */
991 0x00000fff, /* dst_mask */
992 FALSE), /* pcrel_offset */
993
994 /* An PC Relative relocation used when setting PIC offset table register.
995 Like R_NDS32_HI20, but referring to the GOT table entry for
996 the symbol. */
997 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
998 12, /* rightshift */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1000 20, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_dont,/* complain_on_overflow */
1004 bfd_elf_generic_reloc, /* special_function */
1005 "R_NDS32_GOTPC_HI20", /* name */
1006 FALSE, /* partial_inplace */
1007 0x000fffff, /* src_mask */
1008 0x000fffff, /* dst_mask */
1009 TRUE), /* pcrel_offset */
1010 HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
1011 0, /* rightshift */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1013 12, /* bitsize */
1014 FALSE, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_dont,/* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_NDS32_GOTPC_LO12", /* name */
1019 FALSE, /* partial_inplace */
1020 0x00000fff, /* src_mask */
1021 0x00000fff, /* dst_mask */
1022 TRUE), /* pcrel_offset */
1023
1024 HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
1025 12, /* rightshift */
1026 2, /* size (0 = byte, 1 = short, 2 = long) */
1027 20, /* bitsize */
1028 FALSE, /* pc_relative */
1029 0, /* bitpos */
1030 complain_overflow_dont,/* complain_on_overflow */
1031 bfd_elf_generic_reloc, /* special_function */
1032 "R_NDS32_GOTOFF_HI20", /* name */
1033 FALSE, /* partial_inplace */
1034 0x000fffff, /* src_mask */
1035 0x000fffff, /* dst_mask */
1036 FALSE), /* pcrel_offset */
1037 HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
1038 0, /* rightshift */
1039 2, /* size (0 = byte, 1 = short, 2 = long) */
1040 12, /* bitsize */
1041 FALSE, /* pc_relative */
1042 0, /* bitpos */
1043 complain_overflow_dont,/* complain_on_overflow */
1044 bfd_elf_generic_reloc, /* special_function */
1045 "R_NDS32_GOTOFF_LO12", /* name */
1046 FALSE, /* partial_inplace */
1047 0x00000fff, /* src_mask */
1048 0x00000fff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
1050
1051 /* Alignment hint for relaxable instruction. This is used with
1052 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1053 in order to make next label aligned on word boundary. */
1054 HOWTO2 (R_NDS32_INSN16, /* type */
1055 0, /* rightshift */
1056 2, /* size (0 = byte, 1 = short, 2 = long) */
1057 32, /* bitsize */
1058 FALSE, /* pc_relative */
1059 0, /* bitpos */
1060 complain_overflow_dont,/* complain_on_overflow */
1061 nds32_elf_ignore_reloc,/* special_function */
1062 "R_NDS32_INSN16", /* name */
1063 FALSE, /* partial_inplace */
1064 0x00000fff, /* src_mask */
1065 0x00000fff, /* dst_mask */
1066 FALSE), /* pcrel_offset */
1067
1068 /* Alignment hint for label. */
1069 HOWTO2 (R_NDS32_LABEL, /* type */
1070 0, /* rightshift */
1071 2, /* size (0 = byte, 1 = short, 2 = long) */
1072 32, /* bitsize */
1073 FALSE, /* pc_relative */
1074 0, /* bitpos */
1075 complain_overflow_dont,/* complain_on_overflow */
1076 nds32_elf_ignore_reloc,/* special_function */
1077 "R_NDS32_LABEL", /* name */
1078 FALSE, /* partial_inplace */
1079 0xffffffff, /* src_mask */
1080 0xffffffff, /* dst_mask */
1081 FALSE), /* pcrel_offset */
1082
1083 /* Relax hint for unconditional call sequence */
1084 HOWTO2 (R_NDS32_LONGCALL1, /* type */
1085 0, /* rightshift */
1086 2, /* size (0 = byte, 1 = short, 2 = long) */
1087 32, /* bitsize */
1088 FALSE, /* pc_relative */
1089 0, /* bitpos */
1090 complain_overflow_dont,/* complain_on_overflow */
1091 nds32_elf_ignore_reloc,/* special_function */
1092 "R_NDS32_LONGCALL1", /* name */
1093 FALSE, /* partial_inplace */
1094 0xffffffff, /* src_mask */
1095 0xffffffff, /* dst_mask */
1096 FALSE), /* pcrel_offset */
1097
1098 /* Relax hint for conditional call sequence. */
1099 HOWTO2 (R_NDS32_LONGCALL2, /* type */
1100 0, /* rightshift */
1101 2, /* size (0 = byte, 1 = short, 2 = long) */
1102 32, /* bitsize */
1103 FALSE, /* pc_relative */
1104 0, /* bitpos */
1105 complain_overflow_dont,/* complain_on_overflow */
1106 nds32_elf_ignore_reloc,/* special_function */
1107 "R_NDS32_LONGCALL2", /* name */
1108 FALSE, /* partial_inplace */
1109 0xffffffff, /* src_mask */
1110 0xffffffff, /* dst_mask */
1111 FALSE), /* pcrel_offset */
1112
1113 /* Relax hint for conditional call sequence. */
1114 HOWTO2 (R_NDS32_LONGCALL3, /* type */
1115 0, /* rightshift */
1116 2, /* size (0 = byte, 1 = short, 2 = long) */
1117 32, /* bitsize */
1118 FALSE, /* pc_relative */
1119 0, /* bitpos */
1120 complain_overflow_dont,/* complain_on_overflow */
1121 nds32_elf_ignore_reloc,/* special_function */
1122 "R_NDS32_LONGCALL3", /* name */
1123 FALSE, /* partial_inplace */
1124 0xffffffff, /* src_mask */
1125 0xffffffff, /* dst_mask */
1126 FALSE), /* pcrel_offset */
1127
1128 /* Relax hint for unconditional branch sequence. */
1129 HOWTO2 (R_NDS32_LONGJUMP1, /* type */
1130 0, /* rightshift */
1131 2, /* size (0 = byte, 1 = short, 2 = long) */
1132 32, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_dont,/* complain_on_overflow */
1136 nds32_elf_ignore_reloc,/* special_function */
1137 "R_NDS32_LONGJUMP1", /* name */
1138 FALSE, /* partial_inplace */
1139 0xffffffff, /* src_mask */
1140 0xffffffff, /* dst_mask */
1141 FALSE), /* pcrel_offset */
1142
1143 /* Relax hint for conditional branch sequence. */
1144 HOWTO2 (R_NDS32_LONGJUMP2, /* type */
1145 0, /* rightshift */
1146 2, /* size (0 = byte, 1 = short, 2 = long) */
1147 32, /* bitsize */
1148 FALSE, /* pc_relative */
1149 0, /* bitpos */
1150 complain_overflow_dont,/* complain_on_overflow */
1151 nds32_elf_ignore_reloc,/* special_function */
1152 "R_NDS32_LONGJUMP2", /* name */
1153 FALSE, /* partial_inplace */
1154 0xffffffff, /* src_mask */
1155 0xffffffff, /* dst_mask */
1156 FALSE), /* pcrel_offset */
1157
1158 /* Relax hint for conditional branch sequence. */
1159 HOWTO2 (R_NDS32_LONGJUMP3, /* type */
1160 0, /* rightshift */
1161 2, /* size (0 = byte, 1 = short, 2 = long) */
1162 32, /* bitsize */
1163 FALSE, /* pc_relative */
1164 0, /* bitpos */
1165 complain_overflow_dont,/* complain_on_overflow */
1166 nds32_elf_ignore_reloc,/* special_function */
1167 "R_NDS32_LONGJUMP3", /* name */
1168 FALSE, /* partial_inplace */
1169 0xffffffff, /* src_mask */
1170 0xffffffff, /* dst_mask */
1171 FALSE), /* pcrel_offset */
1172
1173 /* Relax hint for load/store sequence. */
1174 HOWTO2 (R_NDS32_LOADSTORE, /* type */
1175 0, /* rightshift */
1176 2, /* size (0 = byte, 1 = short, 2 = long) */
1177 32, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_dont,/* complain_on_overflow */
1181 nds32_elf_ignore_reloc,/* special_function */
1182 "R_NDS32_LOADSTORE", /* name */
1183 FALSE, /* partial_inplace */
1184 0xffffffff, /* src_mask */
1185 0xffffffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
1187
1188 /* Relax hint for load/store sequence. */
1189 HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
1190 0, /* rightshift */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 16, /* bitsize */
1193 FALSE, /* pc_relative */
1194 0, /* bitpos */
1195 complain_overflow_dont,/* complain_on_overflow */
1196 nds32_elf_ignore_reloc,/* special_function */
1197 "R_NDS32_9_FIXED_RELA",/* name */
1198 FALSE, /* partial_inplace */
1199 0x000000ff, /* src_mask */
1200 0x000000ff, /* dst_mask */
1201 FALSE), /* pcrel_offset */
1202
1203 /* Relax hint for load/store sequence. */
1204 HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
1205 0, /* rightshift */
1206 2, /* size (0 = byte, 1 = short, 2 = long) */
1207 32, /* bitsize */
1208 FALSE, /* pc_relative */
1209 0, /* bitpos */
1210 complain_overflow_dont,/* complain_on_overflow */
1211 nds32_elf_ignore_reloc,/* special_function */
1212 "R_NDS32_15_FIXED_RELA",/* name */
1213 FALSE, /* partial_inplace */
1214 0x00003fff, /* src_mask */
1215 0x00003fff, /* dst_mask */
1216 FALSE), /* pcrel_offset */
1217
1218 /* Relax hint for load/store sequence. */
1219 HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
1220 0, /* rightshift */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 32, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_dont,/* complain_on_overflow */
1226 nds32_elf_ignore_reloc,/* special_function */
1227 "R_NDS32_17_FIXED_RELA",/* name */
1228 FALSE, /* partial_inplace */
1229 0x0000ffff, /* src_mask */
1230 0x0000ffff, /* dst_mask */
1231 FALSE), /* pcrel_offset */
1232
1233 /* Relax hint for load/store sequence. */
1234 HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
1235 0, /* rightshift */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 32, /* bitsize */
1238 FALSE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_dont,/* complain_on_overflow */
1241 nds32_elf_ignore_reloc,/* special_function */
1242 "R_NDS32_25_FIXED_RELA",/* name */
1243 FALSE, /* partial_inplace */
1244 0x00ffffff, /* src_mask */
1245 0x00ffffff, /* dst_mask */
1246 FALSE), /* pcrel_offset */
1247
1248 /* High 20 bits of PLT symbol offset relative to PC. */
1249 HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
1250 12, /* rightshift */
1251 2, /* size (0 = byte, 1 = short, 2 = long) */
1252 20, /* bitsize */
1253 FALSE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_dont,/* complain_on_overflow */
1256 bfd_elf_generic_reloc, /* special_function */
1257 "R_NDS32_PLTREL_HI20", /* name */
1258 FALSE, /* partial_inplace */
1259 0x000fffff, /* src_mask */
1260 0x000fffff, /* dst_mask */
1261 FALSE), /* pcrel_offset */
1262
1263 /* Low 12 bits of PLT symbol offset relative to PC. */
1264 HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
1265 0, /* rightshift */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 12, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_dont,/* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 "R_NDS32_PLTREL_LO12", /* name */
1273 FALSE, /* partial_inplace */
1274 0x00000fff, /* src_mask */
1275 0x00000fff, /* dst_mask */
1276 FALSE), /* pcrel_offset */
1277
1278 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
1279 HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
1280 12, /* rightshift */
1281 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 20, /* bitsize */
1283 FALSE, /* pc_relative */
1284 0, /* bitpos */
1285 complain_overflow_dont,/* complain_on_overflow */
1286 bfd_elf_generic_reloc, /* special_function */
1287 "R_NDS32_PLT_GOTREL_HI20",/* name */
1288 FALSE, /* partial_inplace */
1289 0x000fffff, /* src_mask */
1290 0x000fffff, /* dst_mask */
1291 FALSE), /* pcrel_offset */
1292
1293 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
1294 HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
1295 0, /* rightshift */
1296 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 12, /* bitsize */
1298 FALSE, /* pc_relative */
1299 0, /* bitpos */
1300 complain_overflow_dont,/* complain_on_overflow */
1301 bfd_elf_generic_reloc, /* special_function */
1302 "R_NDS32_PLT_GOTREL_LO12",/* name */
1303 FALSE, /* partial_inplace */
1304 0x00000fff, /* src_mask */
1305 0x00000fff, /* dst_mask */
1306 FALSE), /* pcrel_offset */
1307
1308 /* Small data area 12 bits offset. */
1309 HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
1310 2, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 12, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_signed,/* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_NDS32_SDA12S2_DP_RELA",/* name */
1318 FALSE, /* partial_inplace */
1319 0x00000fff, /* src_mask */
1320 0x00000fff, /* dst_mask */
1321 FALSE), /* pcrel_offset */
1322
1323 /* Small data area 12 bits offset. */
1324 HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
1325 2, /* rightshift */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 12, /* bitsize */
1328 FALSE, /* pc_relative */
1329 0, /* bitpos */
1330 complain_overflow_signed,/* complain_on_overflow */
1331 bfd_elf_generic_reloc, /* special_function */
1332 "R_NDS32_SDA12S2_SP_RELA",/* name */
1333 FALSE, /* partial_inplace */
1334 0x00000fff, /* src_mask */
1335 0x00000fff, /* dst_mask */
1336 FALSE), /* pcrel_offset */
1337 /* Lower 12 bits of address. */
1338
1339 HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
1340 2, /* rightshift */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 10, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont,/* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
1347 "R_NDS32_LO12S2_DP_RELA",/* name */
1348 FALSE, /* partial_inplace */
1349 0x000003ff, /* src_mask */
1350 0x000003ff, /* dst_mask */
1351 FALSE), /* pcrel_offset */
1352
1353 /* Lower 12 bits of address. */
1354 HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
1355 2, /* rightshift */
1356 2, /* size (0 = byte, 1 = short, 2 = long) */
1357 10, /* bitsize */
1358 FALSE, /* pc_relative */
1359 0, /* bitpos */
1360 complain_overflow_dont,/* complain_on_overflow */
1361 bfd_elf_generic_reloc, /* special_function */
1362 "R_NDS32_LO12S2_SP_RELA",/* name */
1363 FALSE, /* partial_inplace */
1364 0x000003ff, /* src_mask */
1365 0x000003ff, /* dst_mask */
1366 FALSE), /* pcrel_offset */
1367 /* Lower 12 bits of address. Special identity for or case. */
1368 HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
1369 0, /* rightshift */
1370 2, /* size (0 = byte, 1 = short, 2 = long) */
1371 12, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_dont,/* complain_on_overflow */
1375 bfd_elf_generic_reloc, /* special_function */
1376 "R_NDS32_LO12S0_ORI_RELA",/* name */
1377 FALSE, /* partial_inplace */
1378 0x00000fff, /* src_mask */
1379 0x00000fff, /* dst_mask */
1380 FALSE), /* pcrel_offset */
1381 /* Small data area 19 bits offset. */
1382 HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
1383 3, /* rightshift */
1384 2, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_signed,/* complain_on_overflow */
1389 bfd_elf_generic_reloc, /* special_function */
1390 "R_NDS32_SDA16S3_RELA",/* name */
1391 FALSE, /* partial_inplace */
1392 0x0000ffff, /* src_mask */
1393 0x0000ffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1395
1396 /* Small data area 15 bits offset. */
1397 HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
1398 2, /* rightshift */
1399 2, /* size (0 = byte, 1 = short, 2 = long) */
1400 17, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_signed,/* complain_on_overflow */
1404 bfd_elf_generic_reloc, /* special_function */
1405 "R_NDS32_SDA17S2_RELA",/* name */
1406 FALSE, /* partial_inplace */
1407 0x0001ffff, /* src_mask */
1408 0x0001ffff, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1410
1411 HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
1412 1, /* rightshift */
1413 2, /* size (0 = byte, 1 = short, 2 = long) */
1414 18, /* bitsize */
1415 FALSE, /* pc_relative */
1416 0, /* bitpos */
1417 complain_overflow_signed,/* complain_on_overflow */
1418 bfd_elf_generic_reloc, /* special_function */
1419 "R_NDS32_SDA18S1_RELA",/* name */
1420 FALSE, /* partial_inplace */
1421 0x0003ffff, /* src_mask */
1422 0x0003ffff, /* dst_mask */
1423 FALSE), /* pcrel_offset */
1424
1425 HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
1426 0, /* rightshift */
1427 2, /* size (0 = byte, 1 = short, 2 = long) */
1428 19, /* bitsize */
1429 FALSE, /* pc_relative */
1430 0, /* bitpos */
1431 complain_overflow_signed,/* complain_on_overflow */
1432 bfd_elf_generic_reloc, /* special_function */
1433 "R_NDS32_SDA19S0_RELA",/* name */
1434 FALSE, /* partial_inplace */
1435 0x0007ffff, /* src_mask */
1436 0x0007ffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438 HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
1439 0, /* rightshift */
1440 0, /* size (0 = byte, 1 = short, 2 = long) */
1441 8, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont,/* complain_on_overflow */
1445 nds32_elf_ignore_reloc,/* special_function */
1446 "R_NDS32_DWARF2_OP1_RELA",/* name */
1447 FALSE, /* partial_inplace */
1448 0xff, /* src_mask */
1449 0xff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451 HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
1452 0, /* rightshift */
1453 1, /* size (0 = byte, 1 = short, 2 = long) */
1454 16, /* bitsize */
1455 FALSE, /* pc_relative */
1456 0, /* bitpos */
1457 complain_overflow_dont,/* complain_on_overflow */
1458 nds32_elf_ignore_reloc,/* special_function */
1459 "R_NDS32_DWARF2_OP2_RELA",/* name */
1460 FALSE, /* partial_inplace */
1461 0xffff, /* src_mask */
1462 0xffff, /* dst_mask */
1463 FALSE), /* pcrel_offset */
1464 HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
1465 0, /* rightshift */
1466 2, /* size (0 = byte, 1 = short, 2 = long) */
1467 32, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_dont,/* complain_on_overflow */
1471 nds32_elf_ignore_reloc,/* special_function */
1472 "R_NDS32_DWARF2_LEB_RELA",/* name */
1473 FALSE, /* partial_inplace */
1474 0xffffffff, /* src_mask */
1475 0xffffffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477 HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
1478 0, /* rightshift */
1479 1, /* size (0 = byte, 1 = short, 2 = long) */
1480 16, /* bitsize */
1481 FALSE, /* pc_relative */
1482 0, /* bitpos */
1483 complain_overflow_dont,/* complain_on_overflow */
1484 nds32_elf_ignore_reloc,/* special_function */
1485 "R_NDS32_UPDATE_TA_RELA",/* name */
1486 FALSE, /* partial_inplace */
1487 0xffff, /* src_mask */
1488 0xffff, /* dst_mask */
1489 FALSE), /* pcrel_offset */
1490 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1491 entry for the symbol. */
1492 HOWTO2 (R_NDS32_9_PLTREL, /* type */
1493 1, /* rightshift */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1495 8, /* bitsize */
1496 TRUE, /* pc_relative */
1497 0, /* bitpos */
1498 complain_overflow_signed,/* complain_on_overflow */
1499 bfd_elf_generic_reloc, /* special_function */
1500 "R_NDS32_9_PLTREL", /* name */
1501 FALSE, /* partial_inplace */
1502 0xff, /* src_mask */
1503 0xff, /* dst_mask */
1504 TRUE), /* pcrel_offset */
1505 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
1506 HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
1507 0, /* rightshift */
1508 2, /* size (0 = byte, 1 = short, 2 = long) */
1509 20, /* bitsize */
1510 FALSE, /* pc_relative */
1511 0, /* bitpos */
1512 complain_overflow_dont,/* complain_on_overflow */
1513 bfd_elf_generic_reloc, /* special_function */
1514 "R_NDS32_PLT_GOTREL_LO20",/* name */
1515 FALSE, /* partial_inplace */
1516 0x000fffff, /* src_mask */
1517 0x000fffff, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1520 HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
1521 0, /* rightshift */
1522 2, /* size (0 = byte, 1 = short, 2 = long) */
1523 15, /* bitsize */
1524 FALSE, /* pc_relative */
1525 0, /* bitpos */
1526 complain_overflow_dont,/* complain_on_overflow */
1527 bfd_elf_generic_reloc, /* special_function */
1528 "R_NDS32_PLT_GOTREL_LO15",/* name */
1529 FALSE, /* partial_inplace */
1530 0x00007fff, /* src_mask */
1531 0x00007fff, /* dst_mask */
1532 FALSE), /* pcrel_offset */
1533 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
1534 HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
1535 0, /* rightshift */
1536 2, /* size (0 = byte, 1 = short, 2 = long) */
1537 19, /* bitsize */
1538 FALSE, /* pc_relative */
1539 0, /* bitpos */
1540 complain_overflow_dont,/* complain_on_overflow */
1541 bfd_elf_generic_reloc, /* special_function */
1542 "R_NDS32_PLT_GOTREL_LO19",/* name */
1543 FALSE, /* partial_inplace */
1544 0x0007ffff, /* src_mask */
1545 0x0007ffff, /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547 HOWTO2 (R_NDS32_GOT_LO15, /* type */
1548 0, /* rightshift */
1549 2, /* size (0 = byte, 1 = short, 2 = long) */
1550 15, /* bitsize */
1551 FALSE, /* pc_relative */
1552 0, /* bitpos */
1553 complain_overflow_dont,/* complain_on_overflow */
1554 bfd_elf_generic_reloc, /* special_function */
1555 "R_NDS32_GOT_LO15", /* name */
1556 FALSE, /* partial_inplace */
1557 0x00007fff, /* src_mask */
1558 0x00007fff, /* dst_mask */
1559 FALSE), /* pcrel_offset */
1560 HOWTO2 (R_NDS32_GOT_LO19, /* type */
1561 0, /* rightshift */
1562 2, /* size (0 = byte, 1 = short, 2 = long) */
1563 19, /* bitsize */
1564 FALSE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont,/* complain_on_overflow */
1567 bfd_elf_generic_reloc, /* special_function */
1568 "R_NDS32_GOT_LO19", /* name */
1569 FALSE, /* partial_inplace */
1570 0x0007ffff, /* src_mask */
1571 0x0007ffff, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1573 HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
1574 0, /* rightshift */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1576 15, /* bitsize */
1577 FALSE, /* pc_relative */
1578 0, /* bitpos */
1579 complain_overflow_dont,/* complain_on_overflow */
1580 bfd_elf_generic_reloc, /* special_function */
1581 "R_NDS32_GOTOFF_LO15", /* name */
1582 FALSE, /* partial_inplace */
1583 0x00007fff, /* src_mask */
1584 0x00007fff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586 HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
1587 0, /* rightshift */
1588 2, /* size (0 = byte, 1 = short, 2 = long) */
1589 19, /* bitsize */
1590 FALSE, /* pc_relative */
1591 0, /* bitpos */
1592 complain_overflow_dont,/* complain_on_overflow */
1593 bfd_elf_generic_reloc, /* special_function */
1594 "R_NDS32_GOTOFF_LO19", /* name */
1595 FALSE, /* partial_inplace */
1596 0x0007ffff, /* src_mask */
1597 0x0007ffff, /* dst_mask */
1598 FALSE), /* pcrel_offset */
1599 /* GOT 15 bits offset. */
1600 HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
1601 2, /* rightshift */
1602 2, /* size (0 = byte, 1 = short, 2 = long) */
1603 15, /* bitsize */
1604 FALSE, /* pc_relative */
1605 0, /* bitpos */
1606 complain_overflow_signed,/* complain_on_overflow */
1607 bfd_elf_generic_reloc, /* special_function */
1608 "R_NDS32_GOT15S2_RELA",/* name */
1609 FALSE, /* partial_inplace */
1610 0x00007fff, /* src_mask */
1611 0x00007fff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
1613 /* GOT 17 bits offset. */
1614 HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
1615 2, /* rightshift */
1616 2, /* size (0 = byte, 1 = short, 2 = long) */
1617 17, /* bitsize */
1618 FALSE, /* pc_relative */
1619 0, /* bitpos */
1620 complain_overflow_signed,/* complain_on_overflow */
1621 bfd_elf_generic_reloc, /* special_function */
1622 "R_NDS32_GOT17S2_RELA",/* name */
1623 FALSE, /* partial_inplace */
1624 0x0001ffff, /* src_mask */
1625 0x0001ffff, /* dst_mask */
1626 FALSE), /* pcrel_offset */
1627 /* A 5 bit address. */
1628 HOWTO2 (R_NDS32_5_RELA, /* type */
1629 0, /* rightshift */
1630 1, /* size (0 = byte, 1 = short, 2 = long) */
1631 5, /* bitsize */
1632 FALSE, /* pc_relative */
1633 0, /* bitpos */
1634 complain_overflow_signed,/* complain_on_overflow */
1635 bfd_elf_generic_reloc, /* special_function */
1636 "R_NDS32_5_RELA", /* name */
1637 FALSE, /* partial_inplace */
1638 0x1f, /* src_mask */
1639 0x1f, /* dst_mask */
1640 FALSE), /* pcrel_offset */
1641 HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
1642 1, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 9, /* bitsize */
1645 TRUE, /* pc_relative */
1646 0, /* bitpos */
1647 complain_overflow_unsigned,/* complain_on_overflow */
1648 bfd_elf_generic_reloc, /* special_function */
1649 "R_NDS32_10_UPCREL_RELA",/* name */
1650 FALSE, /* partial_inplace */
1651 0x1ff, /* src_mask */
1652 0x1ff, /* dst_mask */
1653 TRUE), /* pcrel_offset */
1654 HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
1655 2, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 7, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_unsigned,/* complain_on_overflow */
1661 bfd_elf_generic_reloc, /* special_function */
1662 "R_NDS32_SDA_FP7U2_RELA",/* name */
1663 FALSE, /* partial_inplace */
1664 0x0000007f, /* src_mask */
1665 0x0000007f, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667 HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
1668 1, /* rightshift */
1669 2, /* size (0 = byte, 1 = short, 2 = long) */
1670 8, /* bitsize */
1671 TRUE, /* pc_relative */
1672 0, /* bitpos */
1673 complain_overflow_signed,/* complain_on_overflow */
1674 bfd_elf_generic_reloc, /* special_function */
1675 "R_NDS32_WORD_9_PCREL_RELA",/* name */
1676 FALSE, /* partial_inplace */
1677 0xff, /* src_mask */
1678 0xff, /* dst_mask */
1679 TRUE), /* pcrel_offset */
1680 HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
1681 1, /* rightshift */
1682 2, /* size (0 = byte, 1 = short, 2 = long) */
1683 24, /* bitsize */
1684 FALSE, /* pc_relative */
1685 0, /* bitpos */
1686 complain_overflow_dont,/* complain_on_overflow */
1687 bfd_elf_generic_reloc, /* special_function */
1688 "R_NDS32_25_ABS_RELA", /* name */
1689 FALSE, /* partial_inplace */
1690 0xffffff, /* src_mask */
1691 0xffffff, /* dst_mask */
1692 FALSE), /* pcrel_offset */
1693
1694 /* A relative 17 bit relocation for ifc, right shifted by 1. */
1695 HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
1696 1, /* rightshift */
1697 2, /* size (0 = byte, 1 = short, 2 = long) */
1698 16, /* bitsize */
1699 TRUE, /* pc_relative */
1700 0, /* bitpos */
1701 complain_overflow_signed,/* complain_on_overflow */
1702 bfd_elf_generic_reloc, /* special_function */
1703 "R_NDS32_17IFC_PCREL_RELA",/* name */
1704 FALSE, /* partial_inplace */
1705 0xffff, /* src_mask */
1706 0xffff, /* dst_mask */
1707 TRUE), /* pcrel_offset */
1708
1709 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
1710 HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
1711 1, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 9, /* bitsize */
1714 TRUE, /* pc_relative */
1715 0, /* bitpos */
1716 complain_overflow_unsigned,/* complain_on_overflow */
1717 bfd_elf_generic_reloc, /* special_function */
1718 "R_NDS32_10IFCU_PCREL_RELA",/* name */
1719 FALSE, /* partial_inplace */
1720 0x1ff, /* src_mask */
1721 0x1ff, /* dst_mask */
1722 TRUE), /* pcrel_offset */
1723
1724 /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
1725 HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
1726 12, /* rightshift */
1727 2, /* size (0 = byte, 1 = short, 2 = long) */
1728 20, /* bitsize */
1729 FALSE, /* pc_relative */
1730 0, /* bitpos */
1731 complain_overflow_dont,/* complain_on_overflow */
1732 bfd_elf_generic_reloc, /* special_function */
1733 "R_NDS32_TLS_LE_HI20", /* name */
1734 FALSE, /* partial_inplace */
1735 0x000fffff, /* src_mask */
1736 0x000fffff, /* dst_mask */
1737 FALSE), /* pcrel_offset */
1738
1739 HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
1740 0, /* rightshift */
1741 2, /* size (0 = byte, 1 = short, 2 = long) */
1742 12, /* bitsize */
1743 FALSE, /* pc_relative */
1744 0, /* bitpos */
1745 complain_overflow_dont,/* complain_on_overflow */
1746 bfd_elf_generic_reloc, /* special_function */
1747 "R_NDS32_TLS_LE_LO12", /* name */
1748 FALSE, /* partial_inplace */
1749 0x00000fff, /* src_mask */
1750 0x00000fff, /* dst_mask */
1751 FALSE), /* pcrel_offset */
1752
1753 /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
1754 HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
1755 12, /* rightshift */
1756 2, /* size (0 = byte, 1 = short, 2 = long) */
1757 20, /* bitsize */
1758 FALSE, /* pc_relative */
1759 0, /* bitpos */
1760 complain_overflow_dont,/* complain_on_overflow */
1761 bfd_elf_generic_reloc, /* special_function */
1762 "R_NDS32_TLS_IE_HI20", /* name */
1763 FALSE, /* partial_inplace */
1764 0x000fffff, /* src_mask */
1765 0x000fffff, /* dst_mask */
1766 FALSE), /* pcrel_offset */
1767
1768 HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
1769 2, /* rightshift */
1770 2, /* size (0 = byte, 1 = short, 2 = long) */
1771 10, /* bitsize */
1772 FALSE, /* pc_relative */
1773 0, /* bitpos */
1774 complain_overflow_dont,/* complain_on_overflow */
1775 bfd_elf_generic_reloc, /* special_function */
1776 "R_NDS32_TLS_IE_LO12S2",/* name */
1777 FALSE, /* partial_inplace */
1778 0x000003ff, /* src_mask */
1779 0x000003ff, /* dst_mask */
1780 FALSE), /* pcrel_offset */
1781
1782 /* TLS LE TP offset relocation */
1783 HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
1784 0, /* rightshift */
1785 2, /* size (0 = byte, 1 = short, 2 = long) */
1786 32, /* bitsize */
1787 FALSE, /* pc_relative */
1788 0, /* bitpos */
1789 complain_overflow_bitfield,/* complain_on_overflow */
1790 bfd_elf_generic_reloc, /* special_function */
1791 "R_NDS32_TLS_TPOFF", /* name */
1792 FALSE, /* partial_inplace */
1793 0xffffffff, /* src_mask */
1794 0xffffffff, /* dst_mask */
1795 FALSE), /* pcrel_offset */
1796
1797 /* A 20 bit address. */
1798 HOWTO2 (R_NDS32_TLS_LE_20, /* type */
1799 0, /* rightshift */
1800 2, /* size (0 = byte, 1 = short, 2 = long) */
1801 20, /* bitsize */
1802 FALSE, /* pc_relative */
1803 0, /* bitpos */
1804 complain_overflow_signed,/* complain_on_overflow */
1805 bfd_elf_generic_reloc, /* special_function */
1806 "R_NDS32_TLS_LE_20", /* name */
1807 FALSE, /* partial_inplace */
1808 0xfffff, /* src_mask */
1809 0xfffff, /* dst_mask */
1810 FALSE), /* pcrel_offset */
1811
1812 HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
1813 0, /* rightshift */
1814 2, /* size (0 = byte, 1 = short, 2 = long) */
1815 15, /* bitsize */
1816 FALSE, /* pc_relative */
1817 0, /* bitpos */
1818 complain_overflow_signed,/* complain_on_overflow */
1819 bfd_elf_generic_reloc, /* special_function */
1820 "R_NDS32_TLS_LE_15S0", /* name */
1821 FALSE, /* partial_inplace */
1822 0x7fff, /* src_mask */
1823 0x7fff, /* dst_mask */
1824 FALSE), /* pcrel_offset */
1825 HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
1826 1, /* rightshift */
1827 2, /* size (0 = byte, 1 = short, 2 = long) */
1828 15, /* bitsize */
1829 FALSE, /* pc_relative */
1830 0, /* bitpos */
1831 complain_overflow_signed,/* complain_on_overflow */
1832 bfd_elf_generic_reloc, /* special_function */
1833 "R_NDS32_TLS_LE_15S1", /* name */
1834 FALSE, /* partial_inplace */
1835 0x7fff, /* src_mask */
1836 0x7fff, /* dst_mask */
1837 FALSE), /* pcrel_offset */
1838 HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
1839 2, /* rightshift */
1840 2, /* size (0 = byte, 1 = short, 2 = long) */
1841 15, /* bitsize */
1842 FALSE, /* pc_relative */
1843 0, /* bitpos */
1844 complain_overflow_signed,/* complain_on_overflow */
1845 bfd_elf_generic_reloc, /* special_function */
1846 "R_NDS32_TLS_LE_15S2", /* name */
1847 FALSE, /* partial_inplace */
1848 0x7fff, /* src_mask */
1849 0x7fff, /* dst_mask */
1850 FALSE), /* pcrel_offset */
1851
1852 /* Relax hint for unconditional call sequence */
1853 HOWTO2 (R_NDS32_LONGCALL4, /* type */
1854 0, /* rightshift */
1855 2, /* size (0 = byte, 1 = short, 2 = long) */
1856 32, /* bitsize */
1857 FALSE, /* pc_relative */
1858 0, /* bitpos */
1859 complain_overflow_dont,/* complain_on_overflow */
1860 nds32_elf_ignore_reloc,/* special_function */
1861 "R_NDS32_LONGCALL4", /* name */
1862 FALSE, /* partial_inplace */
1863 0xffffffff, /* src_mask */
1864 0xffffffff, /* dst_mask */
1865 FALSE), /* pcrel_offset */
1866
1867 /* Relax hint for conditional call sequence. */
1868 HOWTO2 (R_NDS32_LONGCALL5, /* type */
1869 0, /* rightshift */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1871 32, /* bitsize */
1872 FALSE, /* pc_relative */
1873 0, /* bitpos */
1874 complain_overflow_dont,/* complain_on_overflow */
1875 nds32_elf_ignore_reloc,/* special_function */
1876 "R_NDS32_LONGCALL5", /* name */
1877 FALSE, /* partial_inplace */
1878 0xffffffff, /* src_mask */
1879 0xffffffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1881
1882 /* Relax hint for conditional call sequence. */
1883 HOWTO2 (R_NDS32_LONGCALL6, /* type */
1884 0, /* rightshift */
1885 2, /* size (0 = byte, 1 = short, 2 = long) */
1886 32, /* bitsize */
1887 FALSE, /* pc_relative */
1888 0, /* bitpos */
1889 complain_overflow_dont,/* complain_on_overflow */
1890 nds32_elf_ignore_reloc,/* special_function */
1891 "R_NDS32_LONGCALL6", /* name */
1892 FALSE, /* partial_inplace */
1893 0xffffffff, /* src_mask */
1894 0xffffffff, /* dst_mask */
1895 FALSE), /* pcrel_offset */
1896
1897 /* Relax hint for unconditional branch sequence. */
1898 HOWTO2 (R_NDS32_LONGJUMP4, /* type */
1899 0, /* rightshift */
1900 2, /* size (0 = byte, 1 = short, 2 = long) */
1901 32, /* bitsize */
1902 FALSE, /* pc_relative */
1903 0, /* bitpos */
1904 complain_overflow_dont,/* complain_on_overflow */
1905 nds32_elf_ignore_reloc,/* special_function */
1906 "R_NDS32_LONGJUMP4", /* name */
1907 FALSE, /* partial_inplace */
1908 0xffffffff, /* src_mask */
1909 0xffffffff, /* dst_mask */
1910 FALSE), /* pcrel_offset */
1911
1912 /* Relax hint for conditional branch sequence. */
1913 HOWTO2 (R_NDS32_LONGJUMP5, /* type */
1914 0, /* rightshift */
1915 2, /* size (0 = byte, 1 = short, 2 = long) */
1916 32, /* bitsize */
1917 FALSE, /* pc_relative */
1918 0, /* bitpos */
1919 complain_overflow_dont,/* complain_on_overflow */
1920 nds32_elf_ignore_reloc,/* special_function */
1921 "R_NDS32_LONGJUMP5", /* name */
1922 FALSE, /* partial_inplace */
1923 0xffffffff, /* src_mask */
1924 0xffffffff, /* dst_mask */
1925 FALSE), /* pcrel_offset */
1926
1927 /* Relax hint for conditional branch sequence. */
1928 HOWTO2 (R_NDS32_LONGJUMP6, /* type */
1929 0, /* rightshift */
1930 2, /* size (0 = byte, 1 = short, 2 = long) */
1931 32, /* bitsize */
1932 FALSE, /* pc_relative */
1933 0, /* bitpos */
1934 complain_overflow_dont,/* complain_on_overflow */
1935 nds32_elf_ignore_reloc,/* special_function */
1936 "R_NDS32_LONGJUMP6", /* name */
1937 FALSE, /* partial_inplace */
1938 0xffffffff, /* src_mask */
1939 0xffffffff, /* dst_mask */
1940 FALSE), /* pcrel_offset */
1941
1942 /* Relax hint for conditional branch sequence. */
1943 HOWTO2 (R_NDS32_LONGJUMP7, /* type */
1944 0, /* rightshift */
1945 2, /* size (0 = byte, 1 = short, 2 = long) */
1946 32, /* bitsize */
1947 FALSE, /* pc_relative */
1948 0, /* bitpos */
1949 complain_overflow_dont,/* complain_on_overflow */
1950 nds32_elf_ignore_reloc,/* special_function */
1951 "R_NDS32_LONGJUMP7", /* name */
1952 FALSE, /* partial_inplace */
1953 0xffffffff, /* src_mask */
1954 0xffffffff, /* dst_mask */
1955 FALSE), /* pcrel_offset */
1956
1957 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1958 0, /* rightshift */
1959 2, /* size (0 = byte, 1 = short, 2 = long) */
1960 12, /* bitsize */
1961 FALSE, /* pc_relative */
1962 0, /* bitpos */
1963 complain_overflow_dont,/* complain_on_overflow */
1964 bfd_elf_generic_reloc, /* special_function */
1965 "R_NDS32_TLS_IE_LO12", /* name */
1966 FALSE, /* partial_inplace */
1967 0x00000fff, /* src_mask */
1968 0x00000fff, /* dst_mask */
1969 FALSE), /* pcrel_offset */
1970
1971 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1972 entry for the symbol. */
1973 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1974 12, /* rightshift */
1975 2, /* size (0 = byte, 1 = short, 2 = long) */
1976 20, /* bitsize */
1977 FALSE, /* pc_relative */
1978 0, /* bitpos */
1979 complain_overflow_dont,/* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_NDS32_TLS_IEGP_HI20",/* name */
1982 FALSE, /* partial_inplace */
1983 0x000fffff, /* src_mask */
1984 0x000fffff, /* dst_mask */
1985 FALSE), /* pcrel_offset */
1986
1987 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1988 0, /* rightshift */
1989 2, /* size (0 = byte, 1 = short, 2 = long) */
1990 12, /* bitsize */
1991 FALSE, /* pc_relative */
1992 0, /* bitpos */
1993 complain_overflow_dont,/* complain_on_overflow */
1994 bfd_elf_generic_reloc, /* special_function */
1995 "R_NDS32_TLS_IEGP_LO12",/* name */
1996 FALSE, /* partial_inplace */
1997 0x00000fff, /* src_mask */
1998 0x00000fff, /* dst_mask */
1999 FALSE), /* pcrel_offset */
2000
2001 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2002 2, /* rightshift */
2003 2, /* size (0 = byte, 1 = short, 2 = long) */
2004 10, /* bitsize */
2005 FALSE, /* pc_relative */
2006 0, /* bitpos */
2007 complain_overflow_dont,/* complain_on_overflow */
2008 bfd_elf_generic_reloc, /* special_function */
2009 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2010 FALSE, /* partial_inplace */
2011 0x000003ff, /* src_mask */
2012 0x000003ff, /* dst_mask */
2013 FALSE), /* pcrel_offset */
2014
2015 /* TLS description relocation */
2016 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2017 12, /* rightshift */
2018 2, /* size (0 = byte, 1 = short, 2 = long) */
2019 20, /* bitsize */
2020 FALSE, /* pc_relative */
2021 0, /* bitpos */
2022 complain_overflow_dont,/* complain_on_overflow */
2023 nds32_elf_hi20_reloc, /* special_function */
2024 "R_NDS32_TLS_DESC_HI20",/* name */
2025 FALSE, /* partial_inplace */
2026 0x000fffff, /* src_mask */
2027 0x000fffff, /* dst_mask */
2028 FALSE), /* pcrel_offset */
2029
2030 /* TLS GD/LD description offset high part. */
2031 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2032 12, /* rightshift */
2033 2, /* size (0 = byte, 1 = short, 2 = long) */
2034 20, /* bitsize */
2035 FALSE, /* pc_relative */
2036 0, /* bitpos */
2037 complain_overflow_dont,/* complain_on_overflow */
2038 nds32_elf_hi20_reloc, /* special_function */
2039 "R_NDS32_TLS_DESC_HI20",/* name */
2040 FALSE, /* partial_inplace */
2041 0x000fffff, /* src_mask */
2042 0x000fffff, /* dst_mask */
2043 FALSE), /* pcrel_offset */
2044
2045 /* TLS GD/LD description offset low part. */
2046 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2047 0, /* rightshift */
2048 2, /* size (0 = byte, 1 = short, 2 = long) */
2049 12, /* bitsize */
2050 FALSE, /* pc_relative */
2051 0, /* bitpos */
2052 complain_overflow_dont,/* complain_on_overflow */
2053 nds32_elf_lo12_reloc, /* special_function */
2054 "R_NDS32_TLS_DESC_LO12",/* name */
2055 FALSE, /* partial_inplace */
2056 0x00000fff, /* src_mask */
2057 0x00000fff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2059
2060 /* TLS GD/LD description offset set (movi). */
2061 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2062 0, /* rightshift */
2063 2, /* size (0 = byte, 1 = short, 2 = long) */
2064 20, /* bitsize */
2065 FALSE, /* pc_relative */
2066 0, /* bitpos */
2067 complain_overflow_signed,/* complain_on_overflow */
2068 bfd_elf_generic_reloc, /* special_function */
2069 "R_NDS32_TLS_DESC_20", /* name */
2070 FALSE, /* partial_inplace */
2071 0x000fffff, /* src_mask */
2072 0x000fffff, /* dst_mask */
2073 FALSE), /* pcrel_offset */
2074
2075 /* TLS GD/LD description offset set (lwi.gp). */
2076 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2077 2, /* rightshift */
2078 2, /* size (0 = byte, 1 = short, 2 = long) */
2079 17, /* bitsize */
2080 FALSE, /* pc_relative */
2081 0, /* bitpos */
2082 complain_overflow_signed,/* complain_on_overflow */
2083 bfd_elf_generic_reloc, /* special_function */
2084 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2085 FALSE, /* partial_inplace */
2086 0x0001ffff, /* src_mask */
2087 0x0001ffff, /* dst_mask */
2088 FALSE), /* pcrel_offset */
2089 };
2090
2091 /* Relocations used for relaxation. */
2092 #define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2093 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2094
2095 static reloc_howto_type nds32_elf_relax_howto_table[] = {
2096 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2097 0, /* rightshift */
2098 2, /* size (0 = byte, 1 = short, 2 = long) */
2099 32, /* bitsize */
2100 FALSE, /* pc_relative */
2101 0, /* bitpos */
2102 complain_overflow_dont,/* complain_on_overflow */
2103 nds32_elf_ignore_reloc,/* special_function */
2104 "R_NDS32_RELAX_ENTRY", /* name */
2105 FALSE, /* partial_inplace */
2106 0xffffffff, /* src_mask */
2107 0xffffffff, /* dst_mask */
2108 FALSE), /* pcrel_offset */
2109 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2110 0, /* rightshift */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2112 32, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_dont,/* complain_on_overflow */
2116 nds32_elf_ignore_reloc,/* special_function */
2117 "R_NDS32_GOT_SUFF", /* name */
2118 FALSE, /* partial_inplace */
2119 0xffffffff, /* src_mask */
2120 0xffffffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2123 0, /* rightshift */
2124 2, /* size (0 = byte, 1 = short, 2 = long) */
2125 32, /* bitsize */
2126 FALSE, /* pc_relative */
2127 0, /* bitpos */
2128 complain_overflow_bitfield,/* complain_on_overflow */
2129 nds32_elf_ignore_reloc,/* special_function */
2130 "R_NDS32_GOTOFF_SUFF", /* name */
2131 FALSE, /* partial_inplace */
2132 0xffffffff, /* src_mask */
2133 0xffffffff, /* dst_mask */
2134 FALSE), /* pcrel_offset */
2135 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2136 0, /* rightshift */
2137 2, /* size (0 = byte, 1 = short, 2 = long) */
2138 32, /* bitsize */
2139 FALSE, /* pc_relative */
2140 0, /* bitpos */
2141 complain_overflow_dont,/* complain_on_overflow */
2142 nds32_elf_ignore_reloc,/* special_function */
2143 "R_NDS32_PLT_GOT_SUFF",/* name */
2144 FALSE, /* partial_inplace */
2145 0xffffffff, /* src_mask */
2146 0xffffffff, /* dst_mask */
2147 FALSE), /* pcrel_offset */
2148 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2149 0, /* rightshift */
2150 2, /* size (0 = byte, 1 = short, 2 = long) */
2151 32, /* bitsize */
2152 FALSE, /* pc_relative */
2153 0, /* bitpos */
2154 complain_overflow_dont,/* complain_on_overflow */
2155 nds32_elf_ignore_reloc,/* special_function */
2156 "R_NDS32_MULCALL_SUFF",/* name */
2157 FALSE, /* partial_inplace */
2158 0xffffffff, /* src_mask */
2159 0xffffffff, /* dst_mask */
2160 FALSE), /* pcrel_offset */
2161 HOWTO3 (R_NDS32_PTR, /* type */
2162 0, /* rightshift */
2163 2, /* size (0 = byte, 1 = short, 2 = long) */
2164 32, /* bitsize */
2165 FALSE, /* pc_relative */
2166 0, /* bitpos */
2167 complain_overflow_dont,/* complain_on_overflow */
2168 nds32_elf_ignore_reloc,/* special_function */
2169 "R_NDS32_PTR", /* name */
2170 FALSE, /* partial_inplace */
2171 0xffffffff, /* src_mask */
2172 0xffffffff, /* dst_mask */
2173 FALSE), /* pcrel_offset */
2174 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2175 0, /* rightshift */
2176 2, /* size (0 = byte, 1 = short, 2 = long) */
2177 32, /* bitsize */
2178 FALSE, /* pc_relative */
2179 0, /* bitpos */
2180 complain_overflow_dont,/* complain_on_overflow */
2181 nds32_elf_ignore_reloc,/* special_function */
2182 "R_NDS32_PTR_COUNT", /* name */
2183 FALSE, /* partial_inplace */
2184 0xffffffff, /* src_mask */
2185 0xffffffff, /* dst_mask */
2186 FALSE), /* pcrel_offset */
2187 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2188 0, /* rightshift */
2189 2, /* size (0 = byte, 1 = short, 2 = long) */
2190 32, /* bitsize */
2191 FALSE, /* pc_relative */
2192 0, /* bitpos */
2193 complain_overflow_dont,/* complain_on_overflow */
2194 nds32_elf_ignore_reloc,/* special_function */
2195 "R_NDS32_PTR_RESOLVED",/* name */
2196 FALSE, /* partial_inplace */
2197 0xffffffff, /* src_mask */
2198 0xffffffff, /* dst_mask */
2199 FALSE), /* pcrel_offset */
2200 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2201 0, /* rightshift */
2202 2, /* size (0 = byte, 1 = short, 2 = long) */
2203 32, /* bitsize */
2204 FALSE, /* pc_relative */
2205 0, /* bitpos */
2206 complain_overflow_dont,/* complain_on_overflow */
2207 nds32_elf_ignore_reloc,/* special_function */
2208 "R_NDS32_PLTBLOCK", /* name */
2209 FALSE, /* partial_inplace */
2210 0xffffffff, /* src_mask */
2211 0xffffffff, /* dst_mask */
2212 FALSE), /* pcrel_offset */
2213 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2214 0, /* rightshift */
2215 2, /* size (0 = byte, 1 = short, 2 = long) */
2216 32, /* bitsize */
2217 FALSE, /* pc_relative */
2218 0, /* bitpos */
2219 complain_overflow_dont,/* complain_on_overflow */
2220 nds32_elf_ignore_reloc,/* special_function */
2221 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2222 FALSE, /* partial_inplace */
2223 0xffffffff, /* src_mask */
2224 0xffffffff, /* dst_mask */
2225 FALSE), /* pcrel_offset */
2226 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2227 0, /* rightshift */
2228 2, /* size (0 = byte, 1 = short, 2 = long) */
2229 32, /* bitsize */
2230 FALSE, /* pc_relative */
2231 0, /* bitpos */
2232 complain_overflow_dont,/* complain_on_overflow */
2233 nds32_elf_ignore_reloc,/* special_function */
2234 "R_NDS32_RELAX_REGION_END",/* name */
2235 FALSE, /* partial_inplace */
2236 0xffffffff, /* src_mask */
2237 0xffffffff, /* dst_mask */
2238 FALSE), /* pcrel_offset */
2239 HOWTO3 (R_NDS32_MINUEND, /* type */
2240 0, /* rightshift */
2241 2, /* size (0 = byte, 1 = short, 2 = long) */
2242 32, /* bitsize */
2243 FALSE, /* pc_relative */
2244 0, /* bitpos */
2245 complain_overflow_dont,/* complain_on_overflow */
2246 nds32_elf_ignore_reloc,/* special_function */
2247 "R_NDS32_MINUEND", /* name */
2248 FALSE, /* partial_inplace */
2249 0xffffffff, /* src_mask */
2250 0xffffffff, /* dst_mask */
2251 FALSE), /* pcrel_offset */
2252 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2253 0, /* rightshift */
2254 2, /* size (0 = byte, 1 = short, 2 = long) */
2255 32, /* bitsize */
2256 FALSE, /* pc_relative */
2257 0, /* bitpos */
2258 complain_overflow_dont,/* complain_on_overflow */
2259 nds32_elf_ignore_reloc,/* special_function */
2260 "R_NDS32_SUBTRAHEND", /* name */
2261 FALSE, /* partial_inplace */
2262 0xffffffff, /* src_mask */
2263 0xffffffff, /* dst_mask */
2264 FALSE), /* pcrel_offset */
2265 HOWTO3 (R_NDS32_DIFF8, /* type */
2266 0, /* rightshift */
2267 0, /* size (0 = byte, 1 = short, 2 = long) */
2268 8, /* bitsize */
2269 FALSE, /* pc_relative */
2270 0, /* bitpos */
2271 complain_overflow_dont,/* complain_on_overflow */
2272 nds32_elf_ignore_reloc,/* special_function */
2273 "R_NDS32_DIFF8", /* name */
2274 FALSE, /* partial_inplace */
2275 0x000000ff, /* src_mask */
2276 0x000000ff, /* dst_mask */
2277 FALSE), /* pcrel_offset */
2278 HOWTO3 (R_NDS32_DIFF16, /* type */
2279 0, /* rightshift */
2280 1, /* size (0 = byte, 1 = short, 2 = long) */
2281 16, /* bitsize */
2282 FALSE, /* pc_relative */
2283 0, /* bitpos */
2284 complain_overflow_dont,/* complain_on_overflow */
2285 nds32_elf_ignore_reloc,/* special_function */
2286 "R_NDS32_DIFF16", /* name */
2287 FALSE, /* partial_inplace */
2288 0x0000ffff, /* src_mask */
2289 0x0000ffff, /* dst_mask */
2290 FALSE), /* pcrel_offset */
2291 HOWTO3 (R_NDS32_DIFF32, /* type */
2292 0, /* rightshift */
2293 2, /* size (0 = byte, 1 = short, 2 = long) */
2294 32, /* bitsize */
2295 FALSE, /* pc_relative */
2296 0, /* bitpos */
2297 complain_overflow_dont,/* complain_on_overflow */
2298 nds32_elf_ignore_reloc,/* special_function */
2299 "R_NDS32_DIFF32", /* name */
2300 FALSE, /* partial_inplace */
2301 0xffffffff, /* src_mask */
2302 0xffffffff, /* dst_mask */
2303 FALSE), /* pcrel_offset */
2304 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2305 0, /* rightshift */
2306 0, /* size (0 = byte, 1 = short, 2 = long) */
2307 0, /* bitsize */
2308 FALSE, /* pc_relative */
2309 0, /* bitpos */
2310 complain_overflow_dont,/* complain_on_overflow */
2311 nds32_elf_ignore_reloc,/* special_function */
2312 "R_NDS32_DIFF_ULEB128",/* name */
2313 FALSE, /* partial_inplace */
2314 0xffffffff, /* src_mask */
2315 0xffffffff, /* dst_mask */
2316 FALSE), /* pcrel_offset */
2317 HOWTO3 (R_NDS32_DATA, /* type */
2318 0, /* rightshift */
2319 2, /* size (0 = byte, 1 = short, 2 = long) */
2320 32, /* bitsize */
2321 FALSE, /* pc_relative */
2322 0, /* bitpos */
2323 complain_overflow_dont,/* complain_on_overflow */
2324 nds32_elf_ignore_reloc,/* special_function */
2325 "R_NDS32_DATA", /* name */
2326 FALSE, /* partial_inplace */
2327 0xffffffff, /* src_mask */
2328 0xffffffff, /* dst_mask */
2329 FALSE), /* pcrel_offset */
2330 HOWTO3 (R_NDS32_TRAN, /* type */
2331 0, /* rightshift */
2332 2, /* size (0 = byte, 1 = short, 2 = long) */
2333 32, /* bitsize */
2334 FALSE, /* pc_relative */
2335 0, /* bitpos */
2336 complain_overflow_dont,/* complain_on_overflow */
2337 nds32_elf_ignore_reloc,/* special_function */
2338 "R_NDS32_TRAN", /* name */
2339 FALSE, /* partial_inplace */
2340 0xffffffff, /* src_mask */
2341 0xffffffff, /* dst_mask */
2342 FALSE), /* pcrel_offset */
2343 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2344 0, /* rightshift */
2345 2, /* size (0 = byte, 1 = short, 2 = long) */
2346 32, /* bitsize */
2347 FALSE, /* pc_relative */
2348 0, /* bitpos */
2349 complain_overflow_dont,/* complain_on_overflow */
2350 nds32_elf_ignore_reloc,/* special_function */
2351 "R_NDS32_TLS_LE_ADD", /* name */
2352 FALSE, /* partial_inplace */
2353 0xffffffff, /* src_mask */
2354 0xffffffff, /* dst_mask */
2355 FALSE), /* pcrel_offset */
2356 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2357 0, /* rightshift */
2358 2, /* size (0 = byte, 1 = short, 2 = long) */
2359 32, /* bitsize */
2360 FALSE, /* pc_relative */
2361 0, /* bitpos */
2362 complain_overflow_dont,/* complain_on_overflow */
2363 nds32_elf_ignore_reloc,/* special_function */
2364 "R_NDS32_TLS_LE_LS", /* name */
2365 FALSE, /* partial_inplace */
2366 0xffffffff, /* src_mask */
2367 0xffffffff, /* dst_mask */
2368 FALSE), /* pcrel_offset */
2369 HOWTO3 (R_NDS32_EMPTY, /* type */
2370 0, /* rightshift */
2371 2, /* size (0 = byte, 1 = short, 2 = long) */
2372 32, /* bitsize */
2373 FALSE, /* pc_relative */
2374 0, /* bitpos */
2375 complain_overflow_dont,/* complain_on_overflow */
2376 nds32_elf_ignore_reloc,/* special_function */
2377 "R_NDS32_EMPTY", /* name */
2378 FALSE, /* partial_inplace */
2379 0xffffffff, /* src_mask */
2380 0xffffffff, /* dst_mask */
2381 FALSE), /* pcrel_offset */
2382 /* TLS GD/LD description address base addition. */
2383 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2384 0, /* rightshift */
2385 2, /* size (0 = byte, 1 = short, 2 = long) */
2386 32, /* bitsize */
2387 FALSE, /* pc_relative */
2388 0, /* bitpos */
2389 complain_overflow_dont,/* complain_on_overflow */
2390 nds32_elf_ignore_reloc,/* special_function */
2391 "R_NDS32_TLS_DESC_ADD",/* name */
2392 FALSE, /* partial_inplace */
2393 0xffffffff, /* src_mask */
2394 0xffffffff, /* dst_mask */
2395 FALSE), /* pcrel_offset */
2396 /* TLS GD/LD description function load. */
2397 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2398 0, /* rightshift */
2399 2, /* size (0 = byte, 1 = short, 2 = long) */
2400 32, /* bitsize */
2401 FALSE, /* pc_relative */
2402 0, /* bitpos */
2403 complain_overflow_dont,/* complain_on_overflow */
2404 nds32_elf_ignore_reloc,/* special_function */
2405 "R_NDS32_TLS_DESC_FUNC",/* name */
2406 FALSE, /* partial_inplace */
2407 0xffffffff, /* src_mask */
2408 0xffffffff, /* dst_mask */
2409 FALSE), /* pcrel_offset */
2410 /* TLS DESC resolve function call. */
2411 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2412 0, /* rightshift */
2413 2, /* size (0 = byte, 1 = short, 2 = long) */
2414 32, /* bitsize */
2415 FALSE, /* pc_relative */
2416 0, /* bitpos */
2417 complain_overflow_dont,/* complain_on_overflow */
2418 nds32_elf_ignore_reloc,/* special_function */
2419 "R_NDS32_TLS_DESC_CALL",/* name */
2420 FALSE, /* partial_inplace */
2421 0xffffffff, /* src_mask */
2422 0xffffffff, /* dst_mask */
2423 FALSE), /* pcrel_offset */
2424 /* TLS DESC variable access. */
2425 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2426 0, /* rightshift */
2427 2, /* size (0 = byte, 1 = short, 2 = long) */
2428 32, /* bitsize */
2429 FALSE, /* pc_relative */
2430 0, /* bitpos */
2431 complain_overflow_dont,/* complain_on_overflow */
2432 nds32_elf_ignore_reloc,/* special_function */
2433 "R_NDS32_TLS_DESC_MEM",/* name */
2434 FALSE, /* partial_inplace */
2435 0xffffffff, /* src_mask */
2436 0xffffffff, /* dst_mask */
2437 FALSE), /* pcrel_offset */
2438 /* TLS GD/LD description mark (@tlsdec). */
2439 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2440 0, /* rightshift */
2441 2, /* size (0 = byte, 1 = short, 2 = long) */
2442 32, /* bitsize */
2443 FALSE, /* pc_relative */
2444 0, /* bitpos */
2445 complain_overflow_dont,/* complain_on_overflow */
2446 nds32_elf_ignore_reloc,/* special_function */
2447 "R_NDS32_REMOVE", /* name */
2448 FALSE, /* partial_inplace */
2449 0xffffffff, /* src_mask */
2450 0xffffffff, /* dst_mask */
2451 FALSE), /* pcrel_offset */
2452 /* TLS GD/LD description mark (@tlsdec). */
2453 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2454 0, /* rightshift */
2455 2, /* size (0 = byte, 1 = short, 2 = long) */
2456 32, /* bitsize */
2457 FALSE, /* pc_relative */
2458 0, /* bitpos */
2459 complain_overflow_dont,/* complain_on_overflow */
2460 nds32_elf_ignore_reloc,/* special_function */
2461 "R_NDS32_GROUP", /* name */
2462 FALSE, /* partial_inplace */
2463 0xffffffff, /* src_mask */
2464 0xffffffff, /* dst_mask */
2465 FALSE), /* pcrel_offset */
2466 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2467 0, /* rightshift */
2468 2, /* size (0 = byte, 1 = short, 2 = long) */
2469 32, /* bitsize */
2470 FALSE, /* pc_relative */
2471 0, /* bitpos */
2472 complain_overflow_dont,/* complain_on_overflow */
2473 nds32_elf_ignore_reloc,/* special_function */
2474 "R_NDS32_TLS_IEGP_LW", /* name */
2475 FALSE, /* partial_inplace */
2476 0xffffffff, /* src_mask */
2477 0xffffffff, /* dst_mask */
2478 FALSE), /* pcrel_offset */
2479 /* LA and FLSI relaxation. */
2480 HOWTO3 (R_NDS32_LSI, /* type */
2481 0, /* rightshift */
2482 2, /* size (0 = byte, 1 = short, 2 = long) */
2483 32, /* bitsize */
2484 FALSE, /* pc_relative */
2485 0, /* bitpos */
2486 complain_overflow_dont,/* complain_on_overflow */
2487 nds32_elf_ignore_reloc,/* special_function */
2488 "R_NDS32_LSI", /* name */
2489 FALSE, /* partial_inplace */
2490 0xffffffff, /* src_mask */
2491 0xffffffff, /* dst_mask */
2492 FALSE),
2493 };
2494
2495 static unsigned long dl_tlsdesc_lazy_trampoline[] =
2496 {
2497 0x46200000, /* sethi $r2,#0x0 */
2498 0x58210000, /* ori $r2,$r2,#0x0 */
2499 0x40217400, /* add $r2,$r2,$gp */
2500 0x04210000, /* lwi $r2,[$r2+#0x0] */
2501 0x46300000, /* sethi $r3,#0x0 */
2502 0x58318000, /* ori $r3,$r3,#0x0 */
2503 0x4031f400, /* add $r3,$r3,$gp */
2504 0x4a000800, /* jr $r2 */
2505 };
2506
2507 static void
2508 nds32_put_trampoline (void *contents, const unsigned long *template,
2509 unsigned count)
2510 {
2511 unsigned ix;
2512
2513 for (ix = 0; ix != count; ix++)
2514 {
2515 unsigned long insn = template[ix];
2516 bfd_putb32 (insn, (char *) contents + ix * 4);
2517 }
2518 }
2519
2520 /* nds32_insertion_sort sorts an array with nmemb elements of size size.
2521 This prototype is the same as qsort (). */
2522
2523 void
2524 nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2525 int (*compar) (const void *lhs, const void *rhs))
2526 {
2527 char *ptr = (char *) base;
2528 int i, j;
2529 char tmp[sizeof (Elf_Internal_Rela)];
2530
2531 BFD_ASSERT (size <= sizeof (tmp));
2532
2533 /* If i is less than j, i is inserted before j.
2534
2535 |---- j ----- i --------------|
2536 \ / \ /
2537 sorted unsorted
2538 */
2539
2540 for (i = 1; i < (int) nmemb; i++)
2541 {
2542 for (j = (i - 1); j >= 0; j--)
2543 if (compar (ptr + i * size, ptr + j * size) >= 0)
2544 break;
2545
2546 j++;
2547
2548 if (i == j)
2549 continue; /* i is in order. */
2550
2551 memcpy (tmp, ptr + i * size, size);
2552 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2553 memcpy (ptr + j * size, tmp, size);
2554 }
2555 }
2556
2557 /* Sort relocation by r_offset.
2558
2559 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2560 algorithm. Relocations at the same r_offset must keep their order.
2561 For example, RELAX_ENTRY must be the very first relocation entry.
2562
2563 Currently, this function implements insertion-sort.
2564
2565 FIXME: If we already sort them in assembler, why bother sort them
2566 here again? */
2567
2568 static int
2569 compar_reloc (const void *lhs, const void *rhs)
2570 {
2571 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2572 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2573
2574 if (l->r_offset > r->r_offset)
2575 return 1;
2576 else if (l->r_offset == r->r_offset)
2577 return 0;
2578 else
2579 return -1;
2580 }
2581
2582 /* Functions listed below are only used for old relocs.
2583 nds32_elf_9_pcrel_reloc
2584 nds32_elf_do_9_pcrel_reloc
2585 nds32_elf_hi20_reloc
2586 nds32_elf_relocate_hi20
2587 nds32_elf_lo12_reloc
2588 nds32_elf_sda15_reloc
2589 nds32_elf_generic_reloc. */
2590
2591 /* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2592
2593 static bfd_reloc_status_type
2594 nds32_elf_9_pcrel_reloc (bfd * abfd,
2595 arelent * reloc_entry,
2596 asymbol * symbol,
2597 void * data,
2598 asection * input_section,
2599 bfd * output_bfd,
2600 char ** error_message ATTRIBUTE_UNUSED)
2601 {
2602 /* This part is from bfd_elf_generic_reloc. */
2603 if (output_bfd != (bfd *) NULL
2604 && (symbol->flags & BSF_SECTION_SYM) == 0
2605 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2606 {
2607 reloc_entry->address += input_section->output_offset;
2608 return bfd_reloc_ok;
2609 }
2610
2611 if (output_bfd != NULL)
2612 {
2613 /* FIXME: See bfd_perform_relocation. Is this right? */
2614 return bfd_reloc_continue;
2615 }
2616
2617 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2618 input_section,
2619 data, reloc_entry->address,
2620 symbol->section,
2621 (symbol->value
2622 + symbol->section->output_section->vma
2623 + symbol->section->output_offset),
2624 reloc_entry->addend);
2625 }
2626
2627 /* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2628 #define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2629
2630 static bfd_reloc_status_type
2631 nds32_elf_do_9_pcrel_reloc (bfd * abfd,
2632 reloc_howto_type * howto,
2633 asection * input_section,
2634 bfd_byte * data,
2635 bfd_vma offset,
2636 asection * symbol_section ATTRIBUTE_UNUSED,
2637 bfd_vma symbol_value,
2638 bfd_vma addend)
2639 {
2640 bfd_signed_vma relocation;
2641 unsigned short x;
2642 bfd_reloc_status_type status;
2643
2644 /* Sanity check the address (offset in section). */
2645 if (offset > bfd_get_section_limit (abfd, input_section))
2646 return bfd_reloc_outofrange;
2647
2648 relocation = symbol_value + addend;
2649 /* Make it pc relative. */
2650 relocation -= (input_section->output_section->vma
2651 + input_section->output_offset);
2652 /* These jumps mask off the lower two bits of the current address
2653 before doing pcrel calculations. */
2654 relocation -= (offset & -(bfd_vma) 2);
2655
2656 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2657 status = bfd_reloc_overflow;
2658 else
2659 status = bfd_reloc_ok;
2660
2661 x = bfd_getb16 (data + offset);
2662
2663 relocation >>= howto->rightshift;
2664 relocation <<= howto->bitpos;
2665 x = (x & ~howto->dst_mask)
2666 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2667
2668 bfd_putb16 ((bfd_vma) x, data + offset);
2669
2670 return status;
2671 }
2672
2673 /* Handle the R_NDS32_HI20_[SU]LO relocs.
2674 HI20_SLO is for the add3 and load/store with displacement instructions.
2675 HI20 is for the or3 instruction.
2676 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2677 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2678 we must add one to the high 16 bytes (which will get subtracted off when
2679 the low 16 bits are added).
2680 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2681 because there is a carry from the LO12 to the HI20. Here we just save
2682 the information we need; we do the actual relocation when we see the LO12.
2683 This code is copied from the elf32-mips.c. We also support an arbitrary
2684 number of HI20 relocs to be associated with a single LO12 reloc. The
2685 assembler sorts the relocs to ensure each HI20 immediately precedes its
2686 LO12. However if there are multiple copies, the assembler may not find
2687 the real LO12 so it picks the first one it finds. */
2688
2689 struct nds32_hi20
2690 {
2691 struct nds32_hi20 *next;
2692 bfd_byte *addr;
2693 bfd_vma addend;
2694 };
2695
2696 static struct nds32_hi20 *nds32_hi20_list;
2697
2698 static bfd_reloc_status_type
2699 nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2700 arelent *reloc_entry,
2701 asymbol *symbol,
2702 void *data,
2703 asection *input_section,
2704 bfd *output_bfd,
2705 char **error_message ATTRIBUTE_UNUSED)
2706 {
2707 bfd_reloc_status_type ret;
2708 bfd_vma relocation;
2709 struct nds32_hi20 *n;
2710
2711 /* This part is from bfd_elf_generic_reloc.
2712 If we're relocating, and this an external symbol, we don't want
2713 to change anything. */
2714 if (output_bfd != (bfd *) NULL
2715 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2716 {
2717 reloc_entry->address += input_section->output_offset;
2718 return bfd_reloc_ok;
2719 }
2720
2721 /* Sanity check the address (offset in section). */
2722 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2723 return bfd_reloc_outofrange;
2724
2725 ret = bfd_reloc_ok;
2726 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2727 ret = bfd_reloc_undefined;
2728
2729 if (bfd_is_com_section (symbol->section))
2730 relocation = 0;
2731 else
2732 relocation = symbol->value;
2733
2734 relocation += symbol->section->output_section->vma;
2735 relocation += symbol->section->output_offset;
2736 relocation += reloc_entry->addend;
2737
2738 /* Save the information, and let LO12 do the actual relocation. */
2739 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2740 if (n == NULL)
2741 return bfd_reloc_outofrange;
2742
2743 n->addr = (bfd_byte *) data + reloc_entry->address;
2744 n->addend = relocation;
2745 n->next = nds32_hi20_list;
2746 nds32_hi20_list = n;
2747
2748 if (output_bfd != (bfd *) NULL)
2749 reloc_entry->address += input_section->output_offset;
2750
2751 return ret;
2752 }
2753
2754 /* Handle an NDS32 ELF HI20 reloc. */
2755
2756 static void
2757 nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2758 int type ATTRIBUTE_UNUSED,
2759 Elf_Internal_Rela *relhi,
2760 Elf_Internal_Rela *rello,
2761 bfd_byte *contents,
2762 bfd_vma addend)
2763 {
2764 unsigned long insn;
2765 bfd_vma addlo;
2766
2767 insn = bfd_getb32 (contents + relhi->r_offset);
2768
2769 addlo = bfd_getb32 (contents + rello->r_offset);
2770 addlo &= 0xfff;
2771
2772 addend += ((insn & 0xfffff) << 20) + addlo;
2773
2774 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2775 bfd_putb32 (insn, contents + relhi->r_offset);
2776 }
2777
2778 /* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2779 inplace relocation; this function exists in order to do the
2780 R_NDS32_HI20_[SU]LO relocation described above. */
2781
2782 static bfd_reloc_status_type
2783 nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2784 void *data, asection *input_section, bfd *output_bfd,
2785 char **error_message)
2786 {
2787 /* This part is from bfd_elf_generic_reloc.
2788 If we're relocating, and this an external symbol, we don't want
2789 to change anything. */
2790 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2791 && reloc_entry->addend == 0)
2792 {
2793 reloc_entry->address += input_section->output_offset;
2794 return bfd_reloc_ok;
2795 }
2796
2797 if (nds32_hi20_list != NULL)
2798 {
2799 struct nds32_hi20 *l;
2800
2801 l = nds32_hi20_list;
2802 while (l != NULL)
2803 {
2804 unsigned long insn;
2805 unsigned long val;
2806 unsigned long vallo;
2807 struct nds32_hi20 *next;
2808
2809 /* Do the HI20 relocation. Note that we actually don't need
2810 to know anything about the LO12 itself, except where to
2811 find the low 12 bits of the addend needed by the LO12. */
2812 insn = bfd_getb32 (l->addr);
2813 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2814 vallo &= 0xfff;
2815 switch (reloc_entry->howto->type)
2816 {
2817 case R_NDS32_LO12S3:
2818 vallo <<= 3;
2819 break;
2820
2821 case R_NDS32_LO12S2:
2822 vallo <<= 2;
2823 break;
2824
2825 case R_NDS32_LO12S1:
2826 vallo <<= 1;
2827 break;
2828
2829 case R_NDS32_LO12S0:
2830 vallo <<= 0;
2831 break;
2832 }
2833
2834 val = ((insn & 0xfffff) << 12) + vallo;
2835 val += l->addend;
2836
2837 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2838 bfd_putb32 ((bfd_vma) insn, l->addr);
2839
2840 next = l->next;
2841 free (l);
2842 l = next;
2843 }
2844
2845 nds32_hi20_list = NULL;
2846 }
2847
2848 /* Now do the LO12 reloc in the usual way.
2849 ??? It would be nice to call bfd_elf_generic_reloc here,
2850 but we have partial_inplace set. bfd_elf_generic_reloc will
2851 pass the handling back to bfd_install_relocation which will install
2852 a section relative addend which is wrong. */
2853 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2854 input_section, output_bfd, error_message);
2855 }
2856
2857 /* Do generic partial_inplace relocation.
2858 This is a local replacement for bfd_elf_generic_reloc. */
2859
2860 static bfd_reloc_status_type
2861 nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2862 asymbol *symbol, void *data, asection *input_section,
2863 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2864 {
2865 bfd_reloc_status_type ret;
2866 bfd_vma relocation;
2867 bfd_byte *inplace_address;
2868
2869 /* This part is from bfd_elf_generic_reloc.
2870 If we're relocating, and this an external symbol, we don't want
2871 to change anything. */
2872 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2873 && reloc_entry->addend == 0)
2874 {
2875 reloc_entry->address += input_section->output_offset;
2876 return bfd_reloc_ok;
2877 }
2878
2879 /* Now do the reloc in the usual way.
2880 ??? It would be nice to call bfd_elf_generic_reloc here,
2881 but we have partial_inplace set. bfd_elf_generic_reloc will
2882 pass the handling back to bfd_install_relocation which will install
2883 a section relative addend which is wrong. */
2884
2885 /* Sanity check the address (offset in section). */
2886 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2887 return bfd_reloc_outofrange;
2888
2889 ret = bfd_reloc_ok;
2890 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2891 ret = bfd_reloc_undefined;
2892
2893 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2894 relocation = 0;
2895 else
2896 relocation = symbol->value;
2897
2898 /* Only do this for a final link. */
2899 if (output_bfd == (bfd *) NULL)
2900 {
2901 relocation += symbol->section->output_section->vma;
2902 relocation += symbol->section->output_offset;
2903 }
2904
2905 relocation += reloc_entry->addend;
2906 switch (reloc_entry->howto->type)
2907 {
2908 case R_NDS32_LO12S3:
2909 relocation >>= 3;
2910 break;
2911
2912 case R_NDS32_LO12S2:
2913 relocation >>= 2;
2914 break;
2915
2916 case R_NDS32_LO12S1:
2917 relocation >>= 1;
2918 break;
2919
2920 case R_NDS32_LO12S0:
2921 default:
2922 relocation >>= 0;
2923 break;
2924 }
2925
2926 inplace_address = (bfd_byte *) data + reloc_entry->address;
2927
2928 #define DOIT(x) \
2929 x = ((x & ~reloc_entry->howto->dst_mask) | \
2930 (((x & reloc_entry->howto->src_mask) + relocation) & \
2931 reloc_entry->howto->dst_mask))
2932
2933 switch (reloc_entry->howto->size)
2934 {
2935 case 1:
2936 {
2937 short x = bfd_getb16 (inplace_address);
2938
2939 DOIT (x);
2940 bfd_putb16 ((bfd_vma) x, inplace_address);
2941 }
2942 break;
2943 case 2:
2944 {
2945 unsigned long x = bfd_getb32 (inplace_address);
2946
2947 DOIT (x);
2948 bfd_putb32 ((bfd_vma) x, inplace_address);
2949 }
2950 break;
2951 default:
2952 BFD_ASSERT (0);
2953 }
2954
2955 if (output_bfd != (bfd *) NULL)
2956 reloc_entry->address += input_section->output_offset;
2957
2958 return ret;
2959 }
2960
2961 /* Handle the R_NDS32_SDA15 reloc.
2962 This reloc is used to compute the address of objects in the small data area
2963 and to perform loads and stores from that area.
2964 The lower 15 bits are sign extended and added to the register specified
2965 in the instruction, which is assumed to point to _SDA_BASE_.
2966
2967 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2968 the access size, this must be taken care of. */
2969
2970 static bfd_reloc_status_type
2971 nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2972 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2973 asection *input_section, bfd *output_bfd,
2974 char **error_message ATTRIBUTE_UNUSED)
2975 {
2976 /* This part is from bfd_elf_generic_reloc. */
2977 if (output_bfd != (bfd *) NULL
2978 && (symbol->flags & BSF_SECTION_SYM) == 0
2979 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2980 {
2981 reloc_entry->address += input_section->output_offset;
2982 return bfd_reloc_ok;
2983 }
2984
2985 if (output_bfd != NULL)
2986 {
2987 /* FIXME: See bfd_perform_relocation. Is this right? */
2988 return bfd_reloc_continue;
2989 }
2990
2991 /* FIXME: not sure what to do here yet. But then again, the linker
2992 may never call us. */
2993 abort ();
2994 }
2995
2996 /* nds32_elf_ignore_reloc is the special function for
2997 relocation types which don't need to be relocated
2998 like relaxation relocation types.
2999 This function simply return bfd_reloc_ok when it is
3000 invoked. */
3001
3002 static bfd_reloc_status_type
3003 nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3004 asymbol *symbol ATTRIBUTE_UNUSED,
3005 void *data ATTRIBUTE_UNUSED, asection *input_section,
3006 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3007 {
3008 if (output_bfd != NULL)
3009 reloc_entry->address += input_section->output_offset;
3010
3011 return bfd_reloc_ok;
3012 }
3013 \f
3014
3015 /* Map BFD reloc types to NDS32 ELF reloc types. */
3016
3017 struct nds32_reloc_map_entry
3018 {
3019 bfd_reloc_code_real_type bfd_reloc_val;
3020 unsigned char elf_reloc_val;
3021 };
3022
3023 static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3024 {
3025 {BFD_RELOC_NONE, R_NDS32_NONE},
3026 {BFD_RELOC_16, R_NDS32_16_RELA},
3027 {BFD_RELOC_32, R_NDS32_32_RELA},
3028 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3029 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3030
3031 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3032 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3033 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3034 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3035 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3036 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3037 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3038 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3039 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3040 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3041 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3042 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3043 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3044 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3045 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3046 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3047 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3048 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3049 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3050 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3051 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3052 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3053 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3054 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3055 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3056 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3057 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3058 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3059 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3060 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3061 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3062 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3063 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3064 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3065 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3066 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3067 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3068 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3069 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3070 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3071 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3072 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3073 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3074 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3075 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3076 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3077 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3078 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3079 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3080 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3081 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3082 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3083 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3084 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3085 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3086 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3087 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3088 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3089 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3090 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3091 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3092 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3093 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3094 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3095 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3096 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3097 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3098 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3099 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3100 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3101 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3102 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3103 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3104 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3105 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3106 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3107 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3108 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3109 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3110 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3111 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3112 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3113 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3114 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3115 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3116 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3117 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3118 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3119 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3120 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3121 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3122 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3123 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3124 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3125 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3126 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3127 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3128 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3129 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3130 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3131 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3132 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3133 /* Not sure. */
3134 {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3135 /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
3136 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3137 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3138 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3139 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3140 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3141 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3142 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3143 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3144 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3145 {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3146 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3147 {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3148 {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3149 {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3150 {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3151 {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3152 {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3153 {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3154 {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3155 {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3156 {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3157 {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3158 {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3159 {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3160 {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3161 {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3162 {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3163 };
3164
3165 /* Patch tag. */
3166
3167 static reloc_howto_type *
3168 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3169 const char *r_name)
3170 {
3171 unsigned int i;
3172
3173 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3174 if (nds32_elf_howto_table[i].name != NULL
3175 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3176 return &nds32_elf_howto_table[i];
3177
3178 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3179 if (nds32_elf_relax_howto_table[i].name != NULL
3180 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3181 return &nds32_elf_relax_howto_table[i];
3182
3183 return NULL;
3184 }
3185
3186 static reloc_howto_type *
3187 bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3188 {
3189 if (code < R_NDS32_RELAX_ENTRY)
3190 {
3191 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3192 return &nds32_elf_howto_table[code];
3193 }
3194 else
3195 {
3196 if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3197 >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3198 {
3199 int i = code;
3200 i += 1;
3201 }
3202
3203 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3204 < ARRAY_SIZE (nds32_elf_relax_howto_table));
3205 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3206 }
3207 }
3208
3209 static reloc_howto_type *
3210 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3211 bfd_reloc_code_real_type code)
3212 {
3213 unsigned int i;
3214
3215 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3216 {
3217 if (nds32_reloc_map[i].bfd_reloc_val == code)
3218 return bfd_elf32_bfd_reloc_type_table_lookup
3219 (nds32_reloc_map[i].elf_reloc_val);
3220 }
3221
3222 return NULL;
3223 }
3224
3225 /* Set the howto pointer for an NDS32 ELF reloc. */
3226
3227 static bfd_boolean
3228 nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3229 Elf_Internal_Rela *dst)
3230 {
3231 enum elf_nds32_reloc_type r_type;
3232
3233 r_type = ELF32_R_TYPE (dst->r_info);
3234 if (r_type > R_NDS32_GNU_VTENTRY)
3235 {
3236 /* xgettext:c-format */
3237 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3238 abfd, r_type);
3239 bfd_set_error (bfd_error_bad_value);
3240 return FALSE;
3241 }
3242
3243 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
3244 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3245 return TRUE;
3246 }
3247
3248 static bfd_boolean
3249 nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3250 Elf_Internal_Rela *dst)
3251 {
3252 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3253
3254 if ((r_type == R_NDS32_NONE)
3255 || ((r_type > R_NDS32_GNU_VTENTRY)
3256 && (r_type < R_NDS32_max)))
3257 {
3258 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3259 return TRUE;
3260 }
3261
3262 /* xgettext:c-format */
3263 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3264 bfd_set_error (bfd_error_bad_value);
3265 return FALSE;
3266 }
3267
3268 /* Support for core dump NOTE sections.
3269 Reference to include/linux/elfcore.h in Linux. */
3270
3271 static bfd_boolean
3272 nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3273 {
3274 int offset;
3275 size_t size;
3276
3277 switch (note->descsz)
3278 {
3279 case 0x114:
3280 /* Linux/NDS32 32-bit, ABI1. */
3281
3282 /* pr_cursig */
3283 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3284
3285 /* pr_pid */
3286 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3287
3288 /* pr_reg */
3289 offset = 72;
3290 size = 200;
3291 break;
3292
3293 case 0xfc:
3294 /* Linux/NDS32 32-bit. */
3295
3296 /* pr_cursig */
3297 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3298
3299 /* pr_pid */
3300 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3301
3302 /* pr_reg */
3303 offset = 72;
3304 size = 176;
3305 break;
3306
3307 default:
3308 return FALSE;
3309 }
3310
3311 /* Make a ".reg" section. */
3312 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3313 size, note->descpos + offset);
3314 }
3315
3316 static bfd_boolean
3317 nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3318 {
3319 switch (note->descsz)
3320 {
3321 case 124:
3322 /* Linux/NDS32. */
3323
3324 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3325 elf_tdata (abfd)->core->program =
3326 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3327 elf_tdata (abfd)->core->command =
3328 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3329 break;
3330
3331 default:
3332 return FALSE;
3333 }
3334
3335 /* Note that for some reason, a spurious space is tacked
3336 onto the end of the args in some (at least one anyway)
3337 implementations, so strip it off if it exists. */
3338 {
3339 char *command = elf_tdata (abfd)->core->command;
3340 int n = strlen (command);
3341
3342 if (0 < n && command[n - 1] == ' ')
3343 command[n - 1] = '\0';
3344 }
3345
3346 return TRUE;
3347 }
3348
3349 /* Hook called by the linker routine which adds symbols from an object
3350 file. We must handle the special NDS32 section numbers here.
3351 We also keep watching for whether we need to create the sdata special
3352 linker sections. */
3353
3354 static bfd_boolean
3355 nds32_elf_add_symbol_hook (bfd *abfd,
3356 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3357 Elf_Internal_Sym *sym,
3358 const char **namep ATTRIBUTE_UNUSED,
3359 flagword *flagsp ATTRIBUTE_UNUSED,
3360 asection **secp, bfd_vma *valp)
3361 {
3362 switch (sym->st_shndx)
3363 {
3364 case SHN_COMMON:
3365 /* Common symbols less than the GP size are automatically
3366 treated as SHN_MIPS_SCOMMON symbols. */
3367 if (sym->st_size > elf_gp_size (abfd)
3368 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3369 break;
3370
3371 /* st_value is the alignemnt constraint.
3372 That might be its actual size if it is an array or structure. */
3373 switch (sym->st_value)
3374 {
3375 case 1:
3376 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3377 break;
3378 case 2:
3379 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3380 break;
3381 case 4:
3382 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3383 break;
3384 case 8:
3385 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3386 break;
3387 default:
3388 return TRUE;
3389 }
3390
3391 (*secp)->flags |= SEC_IS_COMMON;
3392 *valp = sym->st_size;
3393 break;
3394 }
3395
3396 return TRUE;
3397 }
3398
3399 /* This function can figure out the best location for a base register to access
3400 data relative to this base register
3401 INPUT:
3402 sda_d0: size of first DOUBLE WORD data section
3403 sda_w0: size of first WORD data section
3404 sda_h0: size of first HALF WORD data section
3405 sda_b : size of BYTE data section
3406 sda_hi: size of second HALF WORD data section
3407 sda_w1: size of second WORD data section
3408 sda_d1: size of second DOUBLE WORD data section
3409 OUTPUT:
3410 offset (always positive) from the beginning of sda_d0 if OK
3411 a negative error value if fail
3412 NOTE:
3413 these 7 sections have to be located back to back if exist
3414 a pass in 0 value for non-existing section */
3415
3416 /* Due to the interpretation of simm15 field of load/store depending on
3417 data accessing size, the organization of base register relative data shall
3418 like the following figure
3419 -------------------------------------------
3420 | DOUBLE WORD sized data (range +/- 128K)
3421 -------------------------------------------
3422 | WORD sized data (range +/- 64K)
3423 -------------------------------------------
3424 | HALF WORD sized data (range +/- 32K)
3425 -------------------------------------------
3426 | BYTE sized data (range +/- 16K)
3427 -------------------------------------------
3428 | HALF WORD sized data (range +/- 32K)
3429 -------------------------------------------
3430 | WORD sized data (range +/- 64K)
3431 -------------------------------------------
3432 | DOUBLE WORD sized data (range +/- 128K)
3433 -------------------------------------------
3434 Its base register shall be set to access these data freely. */
3435
3436 /* We have to figure out the SDA_BASE value, so that we can adjust the
3437 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3438 BFD. If we can't find it, we're stuck. We cache it in the ELF
3439 target data. We don't need to adjust the symbol value for an
3440 external symbol if we are producing relocatable output. */
3441
3442 static asection *sda_rela_sec = NULL;
3443
3444 #define SDA_SECTION_NUM 10
3445
3446 static bfd_reloc_status_type
3447 nds32_elf_final_sda_base (bfd * output_bfd,
3448 struct bfd_link_info * info,
3449 bfd_vma * psb,
3450 bfd_boolean add_symbol)
3451 {
3452 int relax_fp_as_gp;
3453 struct elf_nds32_link_hash_table *table;
3454 struct bfd_link_hash_entry *h, *h2;
3455 long unsigned int total = 0;
3456 asection *first = NULL, *final = NULL, *temp;
3457 bfd_vma sda_base = 0;
3458
3459 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
3460 if (!h || (h->type != bfd_link_hash_defined
3461 && h->type != bfd_link_hash_defweak))
3462 {
3463 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3464 4 byte-aligned. Therefore, it has to set the first section ".data"
3465 4 byte-aligned. */
3466 static const char sec_name[SDA_SECTION_NUM][10] =
3467 {
3468 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3469 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3470 };
3471 size_t i = 0;
3472
3473 if (output_bfd->sections == NULL)
3474 {
3475 *psb = elf_gp (output_bfd);
3476 return bfd_reloc_ok;
3477 }
3478
3479 /* Get the first and final section. */
3480 while (i < ARRAY_SIZE (sec_name))
3481 {
3482 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3483 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3484 first = temp;
3485 if (temp && (temp->size != 0 || temp->rawsize != 0))
3486 final = temp;
3487
3488 /* Summarize the sections in order to check if joining .bss. */
3489 if (temp && temp->size != 0)
3490 total += temp->size;
3491 else if (temp && temp->rawsize != 0)
3492 total += temp->rawsize;
3493
3494 i++;
3495 }
3496
3497 /* Check .bss size. */
3498 temp = bfd_get_section_by_name (output_bfd, ".bss");
3499 if (temp)
3500 {
3501 if (temp->size != 0)
3502 total += temp->size;
3503 else if (temp->rawsize != 0)
3504 total += temp->rawsize;
3505
3506 if (total < 0x80000)
3507 {
3508 if (!first && (temp->size != 0 || temp->rawsize != 0))
3509 first = temp;
3510 if ((temp->size != 0 || temp->rawsize != 0))
3511 final = temp;
3512 }
3513 }
3514
3515 if (first && final)
3516 {
3517 /* The middle of data region. */
3518 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3519
3520 /* Find the section sda_base located. */
3521 i = 0;
3522 while (i < ARRAY_SIZE (sec_name))
3523 {
3524 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3525 if (final && (final->size != 0 || final->rawsize != 0)
3526 && sda_base >= final->vma)
3527 {
3528 first = final;
3529 i++;
3530 }
3531 else
3532 break;
3533 }
3534 }
3535 else
3536 {
3537 /* If there is not any default data section in output bfd, try to find
3538 the first data section. If no data section be found, just simplily
3539 choose the first output section. */
3540 temp = output_bfd->sections;
3541 while (temp)
3542 {
3543 if (temp->flags & SEC_ALLOC
3544 && (((temp->flags & SEC_DATA)
3545 && ((temp->flags & SEC_READONLY) == 0))
3546 || (temp->flags & SEC_LOAD) == 0)
3547 && (temp->size != 0 || temp->rawsize != 0))
3548 {
3549 if (!first)
3550 first = temp;
3551 final = temp;
3552 }
3553 temp = temp->next;
3554 }
3555
3556 /* There is no data or bss section. */
3557 if (!first || (first->size == 0 && first->rawsize == 0))
3558 {
3559 first = output_bfd->sections;
3560 while (first && first->size == 0 && first->rawsize == 0)
3561 first = first->next;
3562 }
3563
3564 /* There is no concrete section. */
3565 if (!first)
3566 {
3567 *psb = elf_gp (output_bfd);
3568 return bfd_reloc_ok;
3569 }
3570
3571 if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3572 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3573 else
3574 sda_base = first->vma + 0x2000;
3575 }
3576
3577 sda_base -= first->vma;
3578 sda_base = sda_base & (~7);
3579
3580 if (!_bfd_generic_link_add_one_symbol
3581 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3582 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3583 get_elf_backend_data (output_bfd)->collect, &h))
3584 return FALSE;
3585
3586 sda_rela_sec = first;
3587 }
3588
3589 /* Set _FP_BASE_ to _SDA_BASE_. */
3590 table = nds32_elf_hash_table (info);
3591 relax_fp_as_gp = table->relax_fp_as_gp;
3592 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3593 /* _SDA_BASE_ is difined in linker script. */
3594 if (!first)
3595 {
3596 first = h->u.def.section;
3597 sda_base = h->u.def.value;
3598 }
3599
3600 if (relax_fp_as_gp && h2
3601 && (h2->type == bfd_link_hash_undefweak
3602 || h2->type == bfd_link_hash_undefined))
3603 {
3604 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3605 And set FP equal to SDA_BASE to do relaxation for
3606 la $fp, _FP_BASE_. */
3607 if (!_bfd_generic_link_add_one_symbol
3608 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3609 first, sda_base, (const char *) NULL,
3610 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3611 return FALSE;
3612 }
3613
3614 if (add_symbol)
3615 {
3616 if (h)
3617 {
3618 /* Now set gp. */
3619 elf_gp (output_bfd) = (h->u.def.value
3620 + h->u.def.section->output_section->vma
3621 + h->u.def.section->output_offset);
3622 }
3623 else
3624 {
3625 _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3626 return bfd_reloc_dangerous;
3627 }
3628 }
3629
3630 *psb = h->u.def.value
3631 + h->u.def.section->output_section->vma
3632 + h->u.def.section->output_offset;
3633 return bfd_reloc_ok;
3634 }
3635 \f
3636
3637 /* Return size of a PLT entry. */
3638 #define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3639
3640 /* Create an entry in an nds32 ELF linker hash table. */
3641
3642 static struct bfd_hash_entry *
3643 nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3644 struct bfd_hash_table *table,
3645 const char *string)
3646 {
3647 struct elf_nds32_link_hash_entry *ret;
3648
3649 ret = (struct elf_nds32_link_hash_entry *) entry;
3650
3651 /* Allocate the structure if it has not already been allocated by a
3652 subclass. */
3653 if (ret == NULL)
3654 ret = (struct elf_nds32_link_hash_entry *)
3655 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3656
3657 if (ret == NULL)
3658 return (struct bfd_hash_entry *) ret;
3659
3660 /* Call the allocation method of the superclass. */
3661 ret = (struct elf_nds32_link_hash_entry *)
3662 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3663
3664 if (ret != NULL)
3665 {
3666 struct elf_nds32_link_hash_entry *eh;
3667
3668 eh = (struct elf_nds32_link_hash_entry *) ret;
3669 eh->tls_type = GOT_UNKNOWN;
3670 eh->offset_to_gp = 0;
3671 }
3672
3673 return (struct bfd_hash_entry *) ret;
3674 }
3675
3676 /* Create an nds32 ELF linker hash table. */
3677
3678 static struct bfd_link_hash_table *
3679 nds32_elf_link_hash_table_create (bfd *abfd)
3680 {
3681 struct elf_nds32_link_hash_table *ret;
3682
3683 size_t amt = sizeof (struct elf_nds32_link_hash_table);
3684
3685 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3686 if (ret == NULL)
3687 return NULL;
3688
3689 /* Patch tag. */
3690 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3691 nds32_elf_link_hash_newfunc,
3692 sizeof (struct elf_nds32_link_hash_entry),
3693 NDS32_ELF_DATA))
3694 {
3695 free (ret);
3696 return NULL;
3697 }
3698
3699 ret->sdynbss = NULL;
3700 ret->srelbss = NULL;
3701 ret->sym_ld_script = NULL;
3702
3703 return &ret->root.root;
3704 }
3705
3706 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3707 shortcuts to them in our hash table. */
3708
3709 static bfd_boolean
3710 create_got_section (bfd *dynobj, struct bfd_link_info *info)
3711 {
3712 struct elf_link_hash_table *ehtab;
3713
3714 if (!_bfd_elf_create_got_section (dynobj, info))
3715 return FALSE;
3716
3717 ehtab = elf_hash_table (info);
3718 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3719 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3720 if (!ehtab->sgot || !ehtab->sgotplt)
3721 abort ();
3722
3723 /* _bfd_elf_create_got_section will create it for us. */
3724 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3725 if (ehtab->srelgot == NULL
3726 || !bfd_set_section_flags (ehtab->srelgot,
3727 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3728 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3729 | SEC_READONLY))
3730 || !bfd_set_section_alignment (ehtab->srelgot, 2))
3731 return FALSE;
3732
3733 return TRUE;
3734 }
3735
3736 /* Create dynamic sections when linking against a dynamic object. */
3737
3738 static bfd_boolean
3739 nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3740 {
3741 struct elf_link_hash_table *ehtab;
3742 struct elf_nds32_link_hash_table *htab;
3743 flagword flags, pltflags;
3744 register asection *s;
3745 const struct elf_backend_data *bed;
3746 int ptralign = 2; /* 32-bit */
3747 const char *secname;
3748 char *relname;
3749 flagword secflags;
3750 asection *sec;
3751
3752 bed = get_elf_backend_data (abfd);
3753 ehtab = elf_hash_table (info);
3754 htab = nds32_elf_hash_table (info);
3755
3756 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3757 .rel[a].bss sections. */
3758
3759 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3760 | SEC_LINKER_CREATED);
3761
3762 pltflags = flags;
3763 pltflags |= SEC_CODE;
3764 if (bed->plt_not_loaded)
3765 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3766 if (bed->plt_readonly)
3767 pltflags |= SEC_READONLY;
3768
3769 s = bfd_make_section (abfd, ".plt");
3770 ehtab->splt = s;
3771 if (s == NULL
3772 || !bfd_set_section_flags (s, pltflags)
3773 || !bfd_set_section_alignment (s, bed->plt_alignment))
3774 return FALSE;
3775
3776 if (bed->want_plt_sym)
3777 {
3778 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3779 .plt section. */
3780 struct bfd_link_hash_entry *bh = NULL;
3781 struct elf_link_hash_entry *h;
3782
3783 if (!(_bfd_generic_link_add_one_symbol
3784 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3785 (bfd_vma) 0, (const char *) NULL, FALSE,
3786 get_elf_backend_data (abfd)->collect, &bh)))
3787 return FALSE;
3788
3789 h = (struct elf_link_hash_entry *) bh;
3790 h->def_regular = 1;
3791 h->type = STT_OBJECT;
3792
3793 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3794 return FALSE;
3795 }
3796
3797 s = bfd_make_section (abfd,
3798 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3799 ehtab->srelplt = s;
3800 if (s == NULL
3801 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3802 || !bfd_set_section_alignment (s, ptralign))
3803 return FALSE;
3804
3805 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3806 return FALSE;
3807
3808 for (sec = abfd->sections; sec; sec = sec->next)
3809 {
3810 secflags = bfd_section_flags (sec);
3811 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3812 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3813 continue;
3814 secname = bfd_section_name (sec);
3815 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3816 strcpy (relname, ".rela");
3817 strcat (relname, secname);
3818 if (bfd_get_section_by_name (abfd, secname))
3819 continue;
3820 s = bfd_make_section (abfd, relname);
3821 if (s == NULL
3822 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3823 || !bfd_set_section_alignment (s, ptralign))
3824 return FALSE;
3825 }
3826
3827 if (bed->want_dynbss)
3828 {
3829 /* The .dynbss section is a place to put symbols which are defined
3830 by dynamic objects, are referenced by regular objects, and are
3831 not functions. We must allocate space for them in the process
3832 image and use a R_*_COPY reloc to tell the dynamic linker to
3833 initialize them at run time. The linker script puts the .dynbss
3834 section into the .bss section of the final image. */
3835 s = bfd_make_section (abfd, ".dynbss");
3836 htab->sdynbss = s;
3837 if (s == NULL
3838 || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3839 return FALSE;
3840 /* The .rel[a].bss section holds copy relocs. This section is not
3841 normally needed. We need to create it here, though, so that the
3842 linker will map it to an output section. We can't just create it
3843 only if we need it, because we will not know whether we need it
3844 until we have seen all the input files, and the first time the
3845 main linker code calls BFD after examining all the input files
3846 (size_dynamic_sections) the input sections have already been
3847 mapped to the output sections. If the section turns out not to
3848 be needed, we can discard it later. We will never need this
3849 section when generating a shared object, since they do not use
3850 copy relocs. */
3851 if (!bfd_link_pic (info))
3852 {
3853 s = bfd_make_section (abfd, (bed->default_use_rela_p
3854 ? ".rela.bss" : ".rel.bss"));
3855 htab->srelbss = s;
3856 if (s == NULL
3857 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3858 || !bfd_set_section_alignment (s, ptralign))
3859 return FALSE;
3860 }
3861 }
3862
3863 return TRUE;
3864 }
3865
3866 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3867 static void
3868 nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3869 struct elf_link_hash_entry *dir,
3870 struct elf_link_hash_entry *ind)
3871 {
3872 struct elf_nds32_link_hash_entry *edir, *eind;
3873
3874 edir = (struct elf_nds32_link_hash_entry *) dir;
3875 eind = (struct elf_nds32_link_hash_entry *) ind;
3876
3877 if (ind->root.type == bfd_link_hash_indirect)
3878 {
3879 if (dir->got.refcount <= 0)
3880 {
3881 edir->tls_type = eind->tls_type;
3882 eind->tls_type = GOT_UNKNOWN;
3883 }
3884 }
3885
3886 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3887 }
3888 \f
3889 /* Adjust a symbol defined by a dynamic object and referenced by a
3890 regular object. The current definition is in some section of the
3891 dynamic object, but we're not including those sections. We have to
3892 change the definition to something the rest of the link can
3893 understand. */
3894
3895 static bfd_boolean
3896 nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3897 struct elf_link_hash_entry *h)
3898 {
3899 struct elf_nds32_link_hash_table *htab;
3900 bfd *dynobj;
3901 asection *s;
3902 unsigned int power_of_two;
3903
3904 dynobj = elf_hash_table (info)->dynobj;
3905
3906 /* Make sure we know what is going on here. */
3907 BFD_ASSERT (dynobj != NULL
3908 && (h->needs_plt
3909 || h->is_weakalias
3910 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3911
3912
3913 /* If this is a function, put it in the procedure linkage table. We
3914 will fill in the contents of the procedure linkage table later,
3915 when we know the address of the .got section. */
3916 if (h->type == STT_FUNC || h->needs_plt)
3917 {
3918 if (!bfd_link_pic (info)
3919 && !h->def_dynamic
3920 && !h->ref_dynamic
3921 && h->root.type != bfd_link_hash_undefweak
3922 && h->root.type != bfd_link_hash_undefined)
3923 {
3924 /* This case can occur if we saw a PLT reloc in an input
3925 file, but the symbol was never referred to by a dynamic
3926 object. In such a case, we don't actually need to build
3927 a procedure linkage table, and we can just do a PCREL
3928 reloc instead. */
3929 h->plt.offset = (bfd_vma) - 1;
3930 h->needs_plt = 0;
3931 }
3932
3933 return TRUE;
3934 }
3935 else
3936 h->plt.offset = (bfd_vma) - 1;
3937
3938 /* If this is a weak symbol, and there is a real definition, the
3939 processor independent code will have arranged for us to see the
3940 real definition first, and we can just use the same value. */
3941 if (h->is_weakalias)
3942 {
3943 struct elf_link_hash_entry *def = weakdef (h);
3944 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3945 h->root.u.def.section = def->root.u.def.section;
3946 h->root.u.def.value = def->root.u.def.value;
3947 return TRUE;
3948 }
3949
3950 /* This is a reference to a symbol defined by a dynamic object which
3951 is not a function. */
3952
3953 /* If we are creating a shared library, we must presume that the
3954 only references to the symbol are via the global offset table.
3955 For such cases we need not do anything here; the relocations will
3956 be handled correctly by relocate_section. */
3957 if (bfd_link_pic (info))
3958 return TRUE;
3959
3960 /* If there are no references to this symbol that do not use the
3961 GOT, we don't need to generate a copy reloc. */
3962 if (!h->non_got_ref)
3963 return TRUE;
3964
3965 /* If -z nocopyreloc was given, we won't generate them either. */
3966 if (0 && info->nocopyreloc)
3967 {
3968 h->non_got_ref = 0;
3969 return TRUE;
3970 }
3971
3972 /* If we don't find any dynamic relocs in read-only sections, then
3973 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
3974 if (!_bfd_elf_readonly_dynrelocs (h))
3975 {
3976 h->non_got_ref = 0;
3977 return TRUE;
3978 }
3979
3980 /* We must allocate the symbol in our .dynbss section, which will
3981 become part of the .bss section of the executable. There will be
3982 an entry for this symbol in the .dynsym section. The dynamic
3983 object will contain position independent code, so all references
3984 from the dynamic object to this symbol will go through the global
3985 offset table. The dynamic linker will use the .dynsym entry to
3986 determine the address it must put in the global offset table, so
3987 both the dynamic object and the regular object will refer to the
3988 same memory location for the variable. */
3989
3990 htab = nds32_elf_hash_table (info);
3991 s = htab->sdynbss;
3992 BFD_ASSERT (s != NULL);
3993
3994 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3995 to copy the initial value out of the dynamic object and into the
3996 runtime process image. We need to remember the offset into the
3997 .rela.bss section we are going to use. */
3998 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3999 {
4000 asection *srel;
4001
4002 srel = htab->srelbss;
4003 BFD_ASSERT (srel != NULL);
4004 srel->size += sizeof (Elf32_External_Rela);
4005 h->needs_copy = 1;
4006 }
4007
4008 /* We need to figure out the alignment required for this symbol. I
4009 have no idea how ELF linkers handle this. */
4010 power_of_two = bfd_log2 (h->size);
4011 if (power_of_two > 3)
4012 power_of_two = 3;
4013
4014 /* Apply the required alignment. */
4015 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4016 if (power_of_two > bfd_section_alignment (s))
4017 {
4018 if (!bfd_set_section_alignment (s, power_of_two))
4019 return FALSE;
4020 }
4021
4022 /* Define the symbol as being at this point in the section. */
4023 h->root.u.def.section = s;
4024 h->root.u.def.value = s->size;
4025
4026 /* Increment the section size to make room for the symbol. */
4027 s->size += h->size;
4028
4029 return TRUE;
4030 }
4031
4032 /* Allocate space in .plt, .got and associated reloc sections for
4033 dynamic relocs. */
4034
4035 static bfd_boolean
4036 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4037 {
4038 struct bfd_link_info *info;
4039 struct elf_link_hash_table *ehtab;
4040 struct elf_nds32_link_hash_table *htab;
4041 struct elf_dyn_relocs *p;
4042
4043 if (h->root.type == bfd_link_hash_indirect)
4044 return TRUE;
4045
4046 /* When warning symbols are created, they **replace** the "real"
4047 entry in the hash table, thus we never get to see the real
4048 symbol in a hash traversal. So look at it now. */
4049 if (h->root.type == bfd_link_hash_warning)
4050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4051
4052 info = (struct bfd_link_info *) inf;
4053 ehtab = elf_hash_table (info);
4054 htab = nds32_elf_hash_table (info);
4055 if (htab == NULL)
4056 return FALSE;
4057
4058 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4059 && h->plt.refcount > 0
4060 && !(bfd_link_pie (info) && h->def_regular))
4061 {
4062 /* Make sure this symbol is output as a dynamic symbol.
4063 Undefined weak syms won't yet be marked as dynamic. */
4064 if (h->dynindx == -1 && !h->forced_local)
4065 {
4066 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4067 return FALSE;
4068 }
4069
4070 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4071 {
4072 asection *s = ehtab->splt;
4073
4074 /* If this is the first .plt entry, make room for the special
4075 first entry. */
4076 if (s->size == 0)
4077 s->size += PLT_ENTRY_SIZE;
4078
4079 h->plt.offset = s->size;
4080
4081 /* If this symbol is not defined in a regular file, and we are
4082 not generating a shared library, then set the symbol to this
4083 location in the .plt. This is required to make function
4084 pointers compare as equal between the normal executable and
4085 the shared library. */
4086 if (!bfd_link_pic (info) && !h->def_regular)
4087 {
4088 h->root.u.def.section = s;
4089 h->root.u.def.value = h->plt.offset;
4090 }
4091
4092 /* Make room for this entry. */
4093 s->size += PLT_ENTRY_SIZE;
4094
4095 /* We also need to make an entry in the .got.plt section, which
4096 will be placed in the .got section by the linker script. */
4097 ehtab->sgotplt->size += 4;
4098
4099 /* We also need to make an entry in the .rel.plt section. */
4100 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4101 if (htab->tls_desc_trampoline)
4102 htab->next_tls_desc_index++;
4103 }
4104 else
4105 {
4106 h->plt.offset = (bfd_vma) - 1;
4107 h->needs_plt = 0;
4108 }
4109 }
4110 else
4111 {
4112 h->plt.offset = (bfd_vma) - 1;
4113 h->needs_plt = 0;
4114 }
4115
4116 if (h->got.refcount > 0)
4117 {
4118 asection *sgot;
4119 bfd_boolean dyn;
4120 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4121
4122 /* Make sure this symbol is output as a dynamic symbol.
4123 Undefined weak syms won't yet be marked as dynamic. */
4124 if (h->dynindx == -1 && !h->forced_local)
4125 {
4126 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127 return FALSE;
4128 }
4129
4130 sgot = elf_hash_table (info)->sgot;
4131 h->got.offset = sgot->size;
4132
4133 if (tls_type == GOT_UNKNOWN)
4134 abort ();
4135
4136 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4137 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4138 sgot->size += 4;
4139 else
4140 {
4141 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4142 if (tls_type & GOT_TLS_DESC)
4143 sgot->size += 8;
4144 }
4145
4146 dyn = htab->root.dynamic_sections_created;
4147
4148 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4149 {
4150 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4151 {
4152 /* TLS_DESC with trampoline needs a relocation slot
4153 within .rela.plt. */
4154 htab->num_tls_desc++;
4155 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4156 htab->tls_trampoline = -1;
4157 }
4158 else
4159 {
4160 /* other relocations, including TLS_DESC without trampoline, need
4161 a relocation slot within .rela.got. */
4162 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4163 }
4164 }
4165 }
4166 else
4167 h->got.offset = (bfd_vma)-1;
4168
4169 if (h->dyn_relocs == NULL)
4170 return TRUE;
4171
4172 /* In the shared -Bsymbolic case, discard space allocated for
4173 dynamic pc-relative relocs against symbols which turn out to be
4174 defined in regular objects. For the normal shared case, discard
4175 space for pc-relative relocs that have become local due to symbol
4176 visibility changes. */
4177
4178 if (bfd_link_pic (info))
4179 {
4180 if (h->def_regular && (h->forced_local || info->symbolic))
4181 {
4182 struct elf_dyn_relocs **pp;
4183
4184 for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4185 {
4186 p->count -= p->pc_count;
4187 p->pc_count = 0;
4188 if (p->count == 0)
4189 *pp = p->next;
4190 else
4191 pp = &p->next;
4192 }
4193 }
4194 }
4195 else
4196 {
4197 /* For the non-shared case, discard space for relocs against
4198 symbols which turn out to need copy relocs or are not dynamic. */
4199
4200 if (!h->non_got_ref
4201 && ((h->def_dynamic
4202 && !h->def_regular)
4203 || (htab->root.dynamic_sections_created
4204 && (h->root.type == bfd_link_hash_undefweak
4205 || h->root.type == bfd_link_hash_undefined))))
4206 {
4207 /* Make sure this symbol is output as a dynamic symbol.
4208 Undefined weak syms won't yet be marked as dynamic. */
4209 if (h->dynindx == -1 && !h->forced_local)
4210 {
4211 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4212 return FALSE;
4213 }
4214
4215 /* If that succeeded, we know we'll be keeping all the
4216 relocs. */
4217 if (h->dynindx != -1)
4218 goto keep;
4219 }
4220
4221 h->dyn_relocs = NULL;
4222
4223 keep:;
4224 }
4225
4226 /* Finally, allocate space. */
4227 for (p = h->dyn_relocs; p != NULL; p = p->next)
4228 {
4229 asection *sreloc = elf_section_data (p->sec)->sreloc;
4230 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4231 }
4232
4233 return TRUE;
4234 }
4235
4236 /* Add relocation REL to the end of relocation section SRELOC. */
4237
4238 static void
4239 elf32_nds32_add_dynreloc (bfd *output_bfd,
4240 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4241 asection *sreloc, Elf_Internal_Rela *rel)
4242 {
4243 bfd_byte *loc;
4244 if (sreloc == NULL)
4245 abort ();
4246
4247 loc = sreloc->contents;
4248 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4249 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4250 abort ();
4251
4252 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4253 }
4254
4255 /* Set the sizes of the dynamic sections. */
4256
4257 static bfd_boolean
4258 nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4259 struct bfd_link_info *info)
4260 {
4261 struct elf_nds32_link_hash_table *htab;
4262 bfd *dynobj;
4263 asection *s;
4264 bfd_boolean relocs;
4265 bfd_boolean plt;
4266 bfd *ibfd;
4267
4268 htab = nds32_elf_hash_table (info);
4269 if (htab == NULL)
4270 return FALSE;
4271
4272 dynobj = elf_hash_table (info)->dynobj;
4273 BFD_ASSERT (dynobj != NULL);
4274
4275 if (elf_hash_table (info)->dynamic_sections_created)
4276 {
4277 /* Set the contents of the .interp section to the interpreter. */
4278 if (bfd_link_executable (info) && !info->nointerp)
4279 {
4280 s = bfd_get_section_by_name (dynobj, ".interp");
4281 BFD_ASSERT (s != NULL);
4282 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4283 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4284 }
4285 }
4286
4287 /* Set up .got offsets for local syms, and space for local dynamic
4288 relocs. */
4289 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4290 {
4291 bfd_signed_vma *local_got;
4292 bfd_signed_vma *end_local_got;
4293 bfd_size_type locsymcount;
4294 Elf_Internal_Shdr *symtab_hdr;
4295 asection *sgot;
4296 char *local_tls_type;
4297 unsigned long symndx;
4298 bfd_vma *local_tlsdesc_gotent;
4299
4300 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4301 continue;
4302
4303 for (s = ibfd->sections; s != NULL; s = s->next)
4304 {
4305 struct elf_dyn_relocs *p;
4306
4307 for (p = ((struct elf_dyn_relocs *)
4308 elf_section_data (s)->local_dynrel);
4309 p != NULL; p = p->next)
4310 {
4311 if (!bfd_is_abs_section (p->sec)
4312 && bfd_is_abs_section (p->sec->output_section))
4313 {
4314 /* Input section has been discarded, either because
4315 it is a copy of a linkonce section or due to
4316 linker script /DISCARD/, so we'll be discarding
4317 the relocs too. */
4318 }
4319 else if (p->count != 0)
4320 {
4321 asection *sreloc = elf_section_data (p->sec)->sreloc;
4322 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4323 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4324 info->flags |= DF_TEXTREL;
4325 }
4326 }
4327 }
4328
4329 local_got = elf_local_got_refcounts (ibfd);
4330 if (!local_got)
4331 continue;
4332
4333 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4334 locsymcount = symtab_hdr->sh_info;
4335 end_local_got = local_got + locsymcount;
4336 sgot = elf_hash_table (info)->sgot;
4337 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4338 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4339 for (symndx = 0; local_got < end_local_got;
4340 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4341 {
4342 if (*local_got > 0)
4343 {
4344 int num_of_got_entry_needed = 0;
4345 *local_got = sgot->size;
4346 *local_tlsdesc_gotent = sgot->size;
4347
4348 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4349 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4350 num_of_got_entry_needed = 1;
4351 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4352 else if (*local_tls_type & GOT_TLS_DESC)
4353 num_of_got_entry_needed = 2;
4354
4355 sgot->size += (num_of_got_entry_needed << 2);
4356
4357 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4358 others need a slot in .rela.got. */
4359 if (*local_tls_type == GOT_TLS_DESC)
4360 {
4361 if (bfd_link_pic (info))
4362 {
4363 if (htab->tls_desc_trampoline)
4364 {
4365 htab->num_tls_desc++;
4366 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4367 htab->tls_trampoline = -1;
4368 }
4369 else
4370 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4371 }
4372 else
4373 {
4374 /* TLS_DESC -> TLS_LE */
4375 }
4376 }
4377 else
4378 {
4379 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4380 }
4381 }
4382 else
4383 {
4384 *local_got = (bfd_vma) -1;
4385 *local_tlsdesc_gotent = (bfd_vma) -1;
4386 }
4387 }
4388 }
4389
4390 /* Allocate global sym .plt and .got entries, and space for global
4391 sym dynamic relocs. */
4392 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4393
4394 /* For every jump slot reserved in the sgotplt, reloc_count is
4395 incremented. However, when we reserve space for TLS descriptors,
4396 it's not incremented, so in order to compute the space reserved
4397 for them, it suffices to multiply the reloc count by the jump
4398 slot size. */
4399 if (htab->tls_desc_trampoline && htab->root.srelplt)
4400 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4401
4402 if (htab->tls_trampoline)
4403 {
4404 htab->tls_trampoline = htab->root.splt->size;
4405
4406 /* If we're not using lazy TLS relocations, don't generate the
4407 PLT and GOT entries they require. */
4408 if (!(info->flags & DF_BIND_NOW))
4409 {
4410 htab->dt_tlsdesc_got = htab->root.sgot->size;
4411 htab->root.sgot->size += 4;
4412
4413 htab->dt_tlsdesc_plt = htab->root.splt->size;
4414 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4415 }
4416 }
4417
4418 /* We now have determined the sizes of the various dynamic sections.
4419 Allocate memory for them. */
4420 /* The check_relocs and adjust_dynamic_symbol entry points have
4421 determined the sizes of the various dynamic sections. Allocate
4422 memory for them. */
4423 plt = FALSE;
4424 relocs = FALSE;
4425 for (s = dynobj->sections; s != NULL; s = s->next)
4426 {
4427 if ((s->flags & SEC_LINKER_CREATED) == 0)
4428 continue;
4429
4430 if (s == htab->root.splt)
4431 {
4432 /* Strip this section if we don't need it; see the
4433 comment below. */
4434 plt = s->size != 0;
4435 }
4436 else if (s == elf_hash_table (info)->sgot)
4437 {
4438 got_size += s->size;
4439 }
4440 else if (s == elf_hash_table (info)->sgotplt)
4441 {
4442 got_size += s->size;
4443 }
4444 else if (strncmp (bfd_section_name (s), ".rela", 5) == 0)
4445 {
4446 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4447 relocs = TRUE;
4448
4449 /* We use the reloc_count field as a counter if we need
4450 to copy relocs into the output file. */
4451 s->reloc_count = 0;
4452 }
4453 else
4454 {
4455 /* It's not one of our sections, so don't allocate space. */
4456 continue;
4457 }
4458
4459 if (s->size == 0)
4460 {
4461 /* If we don't need this section, strip it from the
4462 output file. This is mostly to handle .rela.bss and
4463 .rela.plt. We must create both sections in
4464 create_dynamic_sections, because they must be created
4465 before the linker maps input sections to output
4466 sections. The linker does that before
4467 adjust_dynamic_symbol is called, and it is that
4468 function which decides whether anything needs to go
4469 into these sections. */
4470 s->flags |= SEC_EXCLUDE;
4471 continue;
4472 }
4473
4474 /* Allocate memory for the section contents. We use bfd_zalloc
4475 here in case unused entries are not reclaimed before the
4476 section's contents are written out. This should not happen,
4477 but this way if it does, we get a R_NDS32_NONE reloc instead
4478 of garbage. */
4479 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4480 if (s->contents == NULL)
4481 return FALSE;
4482 }
4483
4484
4485 if (htab->root.dynamic_sections_created)
4486 {
4487 /* Add some entries to the .dynamic section. We fill in the
4488 values later, in nds32_elf_finish_dynamic_sections, but we
4489 must add the entries now so that we get the correct size for
4490 the .dynamic section. The DT_DEBUG entry is filled in by the
4491 dynamic linker and used by the debugger. */
4492 #define add_dynamic_entry(TAG, VAL) \
4493 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4494
4495 if (bfd_link_executable (info))
4496 {
4497 if (!add_dynamic_entry (DT_DEBUG, 0))
4498 return FALSE;
4499 }
4500
4501 if (elf_hash_table (info)->splt->size != 0)
4502 {
4503 if (!add_dynamic_entry (DT_PLTGOT, 0)
4504 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4505 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4506 || !add_dynamic_entry (DT_JMPREL, 0))
4507 return FALSE;
4508 }
4509
4510 if (htab->tls_desc_trampoline && plt)
4511 {
4512 if (htab->dt_tlsdesc_plt
4513 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4514 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4515 return FALSE;
4516 }
4517
4518 if (relocs)
4519 {
4520 if (!add_dynamic_entry (DT_RELA, 0)
4521 || !add_dynamic_entry (DT_RELASZ, 0)
4522 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4523 return FALSE;
4524
4525 /* If any dynamic relocs apply to a read-only section,
4526 then we need a DT_TEXTREL entry. */
4527 if ((info->flags & DF_TEXTREL) == 0)
4528 elf_link_hash_traverse (&htab->root,
4529 _bfd_elf_maybe_set_textrel,
4530 (void *) info);
4531
4532 if ((info->flags & DF_TEXTREL) != 0)
4533 {
4534 if (!add_dynamic_entry (DT_TEXTREL, 0))
4535 return FALSE;
4536 }
4537 }
4538 }
4539 #undef add_dynamic_entry
4540
4541 return TRUE;
4542 }
4543
4544 static bfd_reloc_status_type
4545 nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4546 bfd_vma relocation, bfd_byte *location)
4547 {
4548 int size;
4549 bfd_vma x = 0;
4550 bfd_reloc_status_type flag;
4551 unsigned int rightshift = howto->rightshift;
4552 unsigned int bitpos = howto->bitpos;
4553
4554 /* If the size is negative, negate RELOCATION. This isn't very
4555 general. */
4556 if (howto->size < 0)
4557 relocation = -relocation;
4558
4559 /* Get the value we are going to relocate. */
4560 size = bfd_get_reloc_size (howto);
4561 switch (size)
4562 {
4563 default:
4564 abort ();
4565 break;
4566 case 0:
4567 return bfd_reloc_ok;
4568 case 2:
4569 x = bfd_getb16 (location);
4570 break;
4571 case 4:
4572 x = bfd_getb32 (location);
4573 break;
4574 }
4575
4576 /* Check for overflow. FIXME: We may drop bits during the addition
4577 which we don't check for. We must either check at every single
4578 operation, which would be tedious, or we must do the computations
4579 in a type larger than bfd_vma, which would be inefficient. */
4580 flag = bfd_reloc_ok;
4581 if (howto->complain_on_overflow != complain_overflow_dont)
4582 {
4583 bfd_vma addrmask, fieldmask, signmask, ss;
4584 bfd_vma a, b, sum;
4585
4586 /* Get the values to be added together. For signed and unsigned
4587 relocations, we assume that all values should be truncated to
4588 the size of an address. For bitfields, all the bits matter.
4589 See also bfd_check_overflow. */
4590 fieldmask = N_ONES (howto->bitsize);
4591 signmask = ~fieldmask;
4592 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4593 a = (relocation & addrmask) >> rightshift;
4594 b = (x & howto->src_mask & addrmask) >> bitpos;
4595
4596 switch (howto->complain_on_overflow)
4597 {
4598 case complain_overflow_signed:
4599 /* If any sign bits are set, all sign bits must be set.
4600 That is, A must be a valid negative address after
4601 shifting. */
4602 signmask = ~(fieldmask >> 1);
4603 /* Fall through. */
4604
4605 case complain_overflow_bitfield:
4606 /* Much like the signed check, but for a field one bit
4607 wider. We allow a bitfield to represent numbers in the
4608 range -2**n to 2**n-1, where n is the number of bits in the
4609 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4610 can't overflow, which is exactly what we want. */
4611 ss = a & signmask;
4612 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4613 flag = bfd_reloc_overflow;
4614
4615 /* We only need this next bit of code if the sign bit of B
4616 is below the sign bit of A. This would only happen if
4617 SRC_MASK had fewer bits than BITSIZE. Note that if
4618 SRC_MASK has more bits than BITSIZE, we can get into
4619 trouble; we would need to verify that B is in range, as
4620 we do for A above. */
4621 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4622 ss >>= bitpos;
4623
4624 /* Set all the bits above the sign bit. */
4625 b = (b ^ ss) - ss;
4626
4627 /* Now we can do the addition. */
4628 sum = a + b;
4629
4630 /* See if the result has the correct sign. Bits above the
4631 sign bit are junk now; ignore them. If the sum is
4632 positive, make sure we did not have all negative inputs;
4633 if the sum is negative, make sure we did not have all
4634 positive inputs. The test below looks only at the sign
4635 bits, and it really just
4636 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4637
4638 We mask with addrmask here to explicitly allow an address
4639 wrap-around. The Linux kernel relies on it, and it is
4640 the only way to write assembler code which can run when
4641 loaded at a location 0x80000000 away from the location at
4642 which it is linked. */
4643 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4644 flag = bfd_reloc_overflow;
4645
4646 break;
4647
4648 case complain_overflow_unsigned:
4649 /* Checking for an unsigned overflow is relatively easy:
4650 trim the addresses and add, and trim the result as well.
4651 Overflow is normally indicated when the result does not
4652 fit in the field. However, we also need to consider the
4653 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4654 input is 0x80000000, and bfd_vma is only 32 bits; then we
4655 will get sum == 0, but there is an overflow, since the
4656 inputs did not fit in the field. Instead of doing a
4657 separate test, we can check for this by or-ing in the
4658 operands when testing for the sum overflowing its final
4659 field. */
4660 sum = (a + b) & addrmask;
4661 if ((a | b | sum) & signmask)
4662 flag = bfd_reloc_overflow;
4663 break;
4664
4665 default:
4666 abort ();
4667 }
4668 }
4669
4670 /* Put RELOCATION in the right bits. */
4671 relocation >>= (bfd_vma) rightshift;
4672 relocation <<= (bfd_vma) bitpos;
4673
4674 /* Add RELOCATION to the right bits of X. */
4675 /* FIXME : 090616
4676 Because the relaxation may generate duplicate relocation at one address,
4677 an addition to immediate in the instruction may cause the relocation added
4678 several times.
4679 This bug should be fixed in assembler, but a check is also needed here. */
4680 if (howto->partial_inplace)
4681 x = ((x & ~howto->dst_mask)
4682 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4683 else
4684 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4685
4686
4687 /* Put the relocated value back in the object file. */
4688 switch (size)
4689 {
4690 default:
4691 case 0:
4692 case 1:
4693 case 8:
4694 abort ();
4695 break;
4696 case 2:
4697 bfd_putb16 (x, location);
4698 break;
4699 case 4:
4700 bfd_putb32 (x, location);
4701 break;
4702 }
4703
4704 return flag;
4705 }
4706
4707 static bfd_reloc_status_type
4708 nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4709 asection *input_section, bfd_byte *contents,
4710 bfd_vma address, bfd_vma value, bfd_vma addend)
4711 {
4712 bfd_vma relocation;
4713
4714 /* Sanity check the address. */
4715 if (address > bfd_get_section_limit (input_bfd, input_section))
4716 return bfd_reloc_outofrange;
4717
4718 /* This function assumes that we are dealing with a basic relocation
4719 against a symbol. We want to compute the value of the symbol to
4720 relocate to. This is just VALUE, the value of the symbol, plus
4721 ADDEND, any addend associated with the reloc. */
4722 relocation = value + addend;
4723
4724 /* If the relocation is PC relative, we want to set RELOCATION to
4725 the distance between the symbol (currently in RELOCATION) and the
4726 location we are relocating. If pcrel_offset is FALSE we do not
4727 need to subtract out the offset of the location within the
4728 section (which is just ADDRESS). */
4729 if (howto->pc_relative)
4730 {
4731 relocation -= (input_section->output_section->vma
4732 + input_section->output_offset);
4733 if (howto->pcrel_offset)
4734 relocation -= address;
4735 }
4736
4737 return nds32_relocate_contents (howto, input_bfd, relocation,
4738 contents + address);
4739 }
4740
4741 static bfd_boolean
4742 nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4743 const char *name,
4744 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4745 asection *input_sec,
4746 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4747 {
4748 const char *source;
4749 FILE *sym_ld_script = NULL;
4750 struct elf_nds32_link_hash_table *table;
4751
4752 table = nds32_elf_hash_table (info);
4753 sym_ld_script = table->sym_ld_script;
4754 if (!sym_ld_script)
4755 return TRUE;
4756
4757 if (!h || !name || *name == '\0')
4758 return TRUE;
4759
4760 if (input_sec->flags & SEC_EXCLUDE)
4761 return TRUE;
4762
4763 if (!check_start_export_sym)
4764 {
4765 fprintf (sym_ld_script, "SECTIONS\n{\n");
4766 check_start_export_sym = 1;
4767 }
4768
4769 if (h->root.type == bfd_link_hash_defined
4770 || h->root.type == bfd_link_hash_defweak)
4771 {
4772 if (!h->root.u.def.section->output_section)
4773 return TRUE;
4774
4775 if (bfd_is_const_section (input_sec))
4776 source = input_sec->name;
4777 else
4778 source = bfd_get_filename (input_sec->owner);
4779
4780 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4781 h->root.root.string,
4782 (long) (h->root.u.def.value
4783 + h->root.u.def.section->output_section->vma
4784 + h->root.u.def.section->output_offset), source);
4785 }
4786
4787 return TRUE;
4788 }
4789
4790 /* Relocate an NDS32/D ELF section.
4791 There is some attempt to make this function usable for many architectures,
4792 both for RELA and REL type relocs, if only to serve as a learning tool.
4793
4794 The RELOCATE_SECTION function is called by the new ELF backend linker
4795 to handle the relocations for a section.
4796
4797 The relocs are always passed as Rela structures; if the section
4798 actually uses Rel structures, the r_addend field will always be
4799 zero.
4800
4801 This function is responsible for adjust the section contents as
4802 necessary, and (if using Rela relocs and generating a
4803 relocatable output file) adjusting the reloc addend as
4804 necessary.
4805
4806 This function does not have to worry about setting the reloc
4807 address or the reloc symbol index.
4808
4809 LOCAL_SYMS is a pointer to the swapped in local symbols.
4810
4811 LOCAL_SECTIONS is an array giving the section in the input file
4812 corresponding to the st_shndx field of each local symbol.
4813
4814 The global hash table entry for the global symbols can be found
4815 via elf_sym_hashes (input_bfd).
4816
4817 When generating relocatable output, this function must handle
4818 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4819 going to be the section symbol corresponding to the output
4820 section, which means that the addend must be adjusted
4821 accordingly. */
4822
4823 /* Return the base VMA address which should be subtracted from real addresses
4824 when resolving @dtpoff relocation.
4825 This is PT_TLS segment p_vaddr. */
4826
4827 /* Return the relocation value for @tpoff relocation
4828 if STT_TLS virtual address is ADDRESS. */
4829
4830 /* Return the relocation value for @gottpoff relocation
4831 if STT_TLS virtual address is ADDRESS. */
4832
4833 static bfd_vma
4834 gottpoff (struct bfd_link_info *info, bfd_vma address)
4835 {
4836 bfd_vma tp_base;
4837 bfd_vma tp_offset;
4838
4839 /* If tls_sec is NULL, we should have signalled an error already. */
4840 if (elf_hash_table (info)->tls_sec == NULL)
4841 return 0;
4842
4843 tp_base = elf_hash_table (info)->tls_sec->vma;
4844 tp_offset = address - tp_base;
4845
4846 return tp_offset;
4847 }
4848
4849 static bfd_boolean
4850 patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4851 {
4852 /* TLS_GD/TLS_LD model #1
4853 46 00 00 00 sethi $r0,#0x0
4854 58 00 00 00 ori $r0,$r0,#0x0
4855 40 00 74 00 add $r0,$r0,$gp
4856 04 10 00 00 lwi $r1,[$r0+#0x0]
4857 4b e0 04 01 jral $lp,$r1 */
4858
4859 /* TLS_GD/TLS_LD model #2
4860 46 00 00 00 sethi $r0,#0x0
4861 58 00 00 00 ori $r0,$r0,#0x0
4862 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4863 40 00 74 00 add $r0,$r0,$gp
4864 4b e0 04 01 jral $lp,$r1 */
4865
4866 /* TLS_IE model (non-PIC)
4867 46 00 00 00 sethi $r0,#0x0
4868 04 00 00 00 lwi $r0,[$r0+#0x0]
4869 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4870
4871 /* TLS_IE model (PIC)
4872 46 00 00 00 sethi $r0,#0x0
4873 58 00 00 00 ori $r0,$r0,#0x0
4874 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4875 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4876
4877 /* TLS_GD_TO_IE model
4878 46 00 00 00 sethi $r0,#0x0
4879 58 00 00 00 ori $r0,$r0,#0x0
4880 40 00 74 00 add $r0,$rM,$gp
4881 04 00 00 01 lwi $r0,[$r0+#0x4]
4882 40 00 64 00 add $r0,$r0,$r25 */
4883
4884 bfd_boolean rz = FALSE;
4885
4886 typedef struct
4887 {
4888 uint32_t opcode;
4889 uint32_t mask;
4890 } pat_t;
4891
4892 uint32_t patch[3] =
4893 {
4894 0x40007400, /* add $r0,$rM,$gp */
4895 0x04000001, /* lwi $r0,[$r0+#0x4] */
4896 0x40006400, /* add $r0,$r0,$r25 */
4897 };
4898
4899 pat_t mode0[3] =
4900 {
4901 { 0x40000000, 0xfe0003ff },
4902 { 0x04000000, 0xfe000000 },
4903 { 0x4be00001, 0xffff83ff },
4904 };
4905
4906 pat_t mode1[3] =
4907 {
4908 { 0x38007402, 0xfe007fff },
4909 { 0x40007400, 0xfe007fff },
4910 { 0x4be00001, 0xffff83ff },
4911 };
4912
4913 unsigned char *p = contents + rel->r_offset;
4914
4915 uint32_t insn;
4916 uint32_t regidx = 0;
4917 insn = bfd_getb32 (p);
4918 if (INSN_SETHI == (0xfe0fffffu & insn))
4919 {
4920 regidx = 0x1f & (insn >> 20);
4921 p += 4;
4922 }
4923
4924 insn = bfd_getb32 (p);
4925 if (INSN_ORI == (0xfe007fffu & insn))
4926 {
4927 regidx = 0x1f & (insn >> 20);
4928 p += 4;
4929 }
4930
4931 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
4932 {
4933 /* already patched? */
4934 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4935 (patch[1] == bfd_getb32 (p + 4)))
4936 rz = TRUE;
4937 }
4938 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4939 {
4940 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4941 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4942 {
4943 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4944 bfd_putb32 (patch[1], p + 4);
4945 bfd_putb32 (patch[2], p + 8);
4946 rz = TRUE;
4947 }
4948 }
4949 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4950 {
4951 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4952 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
4953 {
4954 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4955 bfd_putb32 (patch[1], p + 4);
4956 bfd_putb32 (patch[2], p + 8);
4957 rz = TRUE;
4958 }
4959 }
4960
4961 if (!rz)
4962 {
4963 printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
4964 (int) rel->r_offset);
4965 BFD_ASSERT(0); /* Unsupported pattern. */
4966 }
4967
4968 return rz;
4969 }
4970
4971 static enum elf_nds32_tls_type
4972 get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
4973
4974 static unsigned int
4975 ones32 (register unsigned int x)
4976 {
4977 /* 32-bit recursive reduction using SWAR...
4978 but first step is mapping 2-bit values
4979 into sum of 2 1-bit values in sneaky way. */
4980 x -= ((x >> 1) & 0x55555555);
4981 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
4982 x = (((x >> 4) + x) & 0x0f0f0f0f);
4983 x += (x >> 8);
4984 x += (x >> 16);
4985 return (x & 0x0000003f);
4986 }
4987
4988 #if !HAVE_FLS
4989 static unsigned int
4990 fls (register unsigned int x)
4991 {
4992 return ffs (x & (-x));
4993 }
4994 #endif /* !HAVE_FLS */
4995
4996 #define nds32_elf_local_tlsdesc_gotent(bfd) \
4997 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
4998
4999 static bfd_boolean
5000 nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
5001 struct bfd_link_info * info,
5002 bfd * input_bfd,
5003 asection * input_section,
5004 bfd_byte * contents,
5005 Elf_Internal_Rela * relocs,
5006 Elf_Internal_Sym * local_syms,
5007 asection ** local_sections)
5008 {
5009 Elf_Internal_Shdr *symtab_hdr;
5010 struct elf_link_hash_entry **sym_hashes;
5011 Elf_Internal_Rela *rel, *relend;
5012 bfd_boolean ret = TRUE; /* Assume success. */
5013 int align = 0;
5014 bfd_reloc_status_type r;
5015 const char *errmsg = NULL;
5016 bfd_vma gp;
5017 struct elf_link_hash_table *ehtab;
5018 struct elf_nds32_link_hash_table *htab;
5019 bfd *dynobj;
5020 bfd_vma *local_got_offsets;
5021 asection *sgot, *splt, *sreloc;
5022 bfd_vma high_address;
5023 struct elf_nds32_link_hash_table *table;
5024 int eliminate_gc_relocs;
5025 bfd_vma fpbase_addr;
5026
5027 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5028 sym_hashes = elf_sym_hashes (input_bfd);
5029 ehtab = elf_hash_table (info);
5030 htab = nds32_elf_hash_table (info);
5031 high_address = bfd_get_section_limit (input_bfd, input_section);
5032
5033 dynobj = htab->root.dynobj;
5034 local_got_offsets = elf_local_got_offsets (input_bfd);
5035
5036 sgot = ehtab->sgot;
5037 splt = ehtab->splt;
5038 sreloc = NULL;
5039
5040 rel = relocs;
5041 relend = relocs + input_section->reloc_count;
5042
5043 table = nds32_elf_hash_table (info);
5044 eliminate_gc_relocs = table->eliminate_gc_relocs;
5045
5046 /* By this time, we can adjust the value of _SDA_BASE_. */
5047 /* Explain _SDA_BASE_ */
5048 if ((!bfd_link_relocatable (info)))
5049 {
5050 is_SDA_BASE_set = 1;
5051 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5052 if (r != bfd_reloc_ok)
5053 return FALSE;
5054 }
5055
5056 /* Do TLS model conversion once at first. */
5057 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5058
5059 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5060 the fp value is set as gp, and it has be reverted for instruction
5061 setting fp. */
5062 fpbase_addr = elf_gp (output_bfd);
5063
5064 /* Deal with (dynamic) relocations. */
5065 for (rel = relocs; rel < relend; rel++)
5066 {
5067 enum elf_nds32_reloc_type r_type;
5068 reloc_howto_type *howto = NULL;
5069 unsigned long r_symndx;
5070 struct elf_link_hash_entry *h = NULL;
5071 Elf_Internal_Sym *sym = NULL;
5072 asection *sec;
5073 bfd_vma relocation;
5074 bfd_vma relocation_sym = 0xdeadbeef;
5075 Elf_Internal_Rela *lorel;
5076 bfd_vma off;
5077
5078 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5079 ensure it's zero (we use REL relocs, not RELA). Therefore this
5080 should be assigning zero to `addend', but for clarity we use
5081 `r_addend'. */
5082
5083 bfd_vma addend = rel->r_addend;
5084 bfd_vma offset = rel->r_offset;
5085
5086 r_type = ELF32_R_TYPE (rel->r_info);
5087 if (r_type >= R_NDS32_max)
5088 {
5089 /* xgettext:c-format */
5090 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5091 input_bfd, r_type);
5092 bfd_set_error (bfd_error_bad_value);
5093 ret = FALSE;
5094 continue;
5095 }
5096
5097 if (r_type == R_NDS32_GNU_VTENTRY
5098 || r_type == R_NDS32_GNU_VTINHERIT
5099 || r_type == R_NDS32_NONE
5100 || r_type == R_NDS32_RELA_GNU_VTENTRY
5101 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5102 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5103 || r_type == R_NDS32_DATA
5104 || r_type == R_NDS32_TRAN)
5105 continue;
5106
5107 /* If we enter the fp-as-gp region. Resolve the address
5108 of best fp-base. */
5109 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5110 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5111 {
5112 int dist;
5113
5114 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5115 dist = rel->r_addend >> 16;
5116 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5117 local_syms, symtab_hdr);
5118 }
5119 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5120 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5121 {
5122 fpbase_addr = elf_gp (output_bfd);
5123 }
5124
5125 /* Skip the relocations used for relaxation. */
5126 /* We have to update LONGCALL and LONGJUMP
5127 relocations when generating the relocatable files. */
5128 if (!bfd_link_relocatable (info)
5129 && (r_type >= R_NDS32_RELAX_ENTRY
5130 || (r_type >= R_NDS32_LONGCALL4
5131 && r_type <= R_NDS32_LONGJUMP7)))
5132 continue;
5133
5134 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5135 r_symndx = ELF32_R_SYM (rel->r_info);
5136
5137 /* This is a final link. */
5138 sym = NULL;
5139 sec = NULL;
5140 h = NULL;
5141
5142 if (r_symndx < symtab_hdr->sh_info)
5143 {
5144 /* Local symbol. */
5145 sym = local_syms + r_symndx;
5146 sec = local_sections[r_symndx];
5147
5148 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5149 addend = rel->r_addend;
5150
5151 /* keep symbol location for static TLS_IE GOT entry */
5152 relocation_sym = relocation;
5153 if (bfd_link_relocatable (info))
5154 {
5155 /* This is a relocatable link. We don't have to change
5156 anything, unless the reloc is against a section symbol,
5157 in which case we have to adjust according to where the
5158 section symbol winds up in the output section. */
5159 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5160 rel->r_addend += sec->output_offset + sym->st_value;
5161
5162 continue;
5163 }
5164 }
5165 else
5166 {
5167 /* External symbol. */
5168 if (bfd_link_relocatable (info))
5169 continue;
5170 bfd_boolean warned, ignored, unresolved_reloc;
5171 int symndx = r_symndx - symtab_hdr->sh_info;
5172
5173 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5174 r_symndx, symtab_hdr, sym_hashes, h, sec,
5175 relocation, unresolved_reloc, warned,
5176 ignored);
5177
5178 /* keep symbol location for static TLS_IE GOT entry */
5179 relocation_sym = relocation;
5180
5181 /* la $fp, _FP_BASE_ is per-function (region).
5182 Handle it specially. */
5183 switch ((int) r_type)
5184 {
5185 case R_NDS32_HI20_RELA:
5186 case R_NDS32_LO12S0_RELA:
5187 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5188 FP_BASE_NAME) == 0)
5189 {
5190 if (!bfd_link_pie (info))
5191 {
5192 _bfd_error_handler
5193 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5194 input_bfd);
5195 }
5196 relocation = fpbase_addr;
5197 }
5198 break;
5199 case R_NDS32_SDA19S0_RELA:
5200 case R_NDS32_SDA15S0_RELA:
5201 case R_NDS32_20_RELA:
5202 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5203 FP_BASE_NAME) == 0)
5204 {
5205 relocation = fpbase_addr;
5206 break;
5207 }
5208 }
5209 }
5210
5211 /* Sanity check the address. */
5212 if (offset > high_address)
5213 {
5214 r = bfd_reloc_outofrange;
5215 goto check_reloc;
5216 }
5217
5218 if (r_type >= R_NDS32_RELAX_ENTRY)
5219 continue;
5220
5221 switch ((int) r_type)
5222 {
5223 case R_NDS32_GOTOFF:
5224 /* Relocation is relative to the start of the global offset
5225 table (for ld24 rx, #uimm24), e.g. access at label+addend
5226
5227 ld24 rx. #label@GOTOFF + addend
5228 sub rx, r12. */
5229 case R_NDS32_GOTOFF_HI20:
5230 case R_NDS32_GOTOFF_LO12:
5231 case R_NDS32_GOTOFF_LO15:
5232 case R_NDS32_GOTOFF_LO19:
5233 BFD_ASSERT (sgot != NULL);
5234
5235 relocation -= elf_gp (output_bfd);
5236 break;
5237
5238 case R_NDS32_9_PLTREL:
5239 case R_NDS32_25_PLTREL:
5240 /* Relocation is to the entry for this symbol in the
5241 procedure linkage table. */
5242
5243 /* The native assembler will generate a 25_PLTREL reloc
5244 for a local symbol if you assemble a call from one
5245 section to another when using -K pic. */
5246 if (h == NULL)
5247 break;
5248
5249 if (h->forced_local)
5250 break;
5251
5252 /* We didn't make a PLT entry for this symbol. This
5253 happens when statically linking PIC code, or when
5254 using -Bsymbolic. */
5255 if (h->plt.offset == (bfd_vma) - 1)
5256 break;
5257
5258 relocation = (splt->output_section->vma
5259 + splt->output_offset + h->plt.offset);
5260 break;
5261
5262 case R_NDS32_PLT_GOTREL_HI20:
5263 case R_NDS32_PLT_GOTREL_LO12:
5264 case R_NDS32_PLT_GOTREL_LO15:
5265 case R_NDS32_PLT_GOTREL_LO19:
5266 case R_NDS32_PLT_GOTREL_LO20:
5267 if (h == NULL
5268 || h->forced_local
5269 || h->plt.offset == (bfd_vma) -1
5270 || (bfd_link_pie (info) && h->def_regular))
5271 {
5272 /* Maybe we should find better checking to optimize
5273 PIE PLT relocations. */
5274 /* We didn't make a PLT entry for this symbol. This
5275 happens when statically linking PIC code, or when
5276 using -Bsymbolic. */
5277 if (h)
5278 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
5279 relocation -= elf_gp (output_bfd);
5280 break;
5281 }
5282
5283 relocation = (splt->output_section->vma
5284 + splt->output_offset + h->plt.offset);
5285
5286 relocation -= elf_gp (output_bfd);
5287 break;
5288
5289 case R_NDS32_PLTREL_HI20:
5290 case R_NDS32_PLTREL_LO12:
5291
5292 /* Relocation is to the entry for this symbol in the
5293 procedure linkage table. */
5294
5295 /* The native assembler will generate a 25_PLTREL reloc
5296 for a local symbol if you assemble a call from one
5297 section to another when using -K pic. */
5298 if (h == NULL)
5299 break;
5300
5301 if (h->forced_local)
5302 break;
5303
5304 if (h->plt.offset == (bfd_vma) - 1)
5305 /* We didn't make a PLT entry for this symbol. This
5306 happens when statically linking PIC code, or when
5307 using -Bsymbolic. */
5308 break;
5309
5310 if (splt == NULL)
5311 break;
5312
5313 relocation = (splt->output_section->vma
5314 + splt->output_offset
5315 + h->plt.offset + 4)
5316 - (input_section->output_section->vma
5317 + input_section->output_offset
5318 + rel->r_offset);
5319
5320 break;
5321
5322 case R_NDS32_GOTPC20:
5323 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5324 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5325 relocation = elf_gp (output_bfd);
5326 break;
5327
5328 case R_NDS32_GOTPC_HI20:
5329 case R_NDS32_GOTPC_LO12:
5330 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5331 bl .+4
5332 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5333 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5334 or
5335 bl .+4
5336 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5337 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5338 relocation = elf_gp (output_bfd);
5339 relocation -= (input_section->output_section->vma
5340 + input_section->output_offset + rel->r_offset);
5341 break;
5342
5343 case R_NDS32_GOT20:
5344 /* Fall through. */
5345 case R_NDS32_GOT_HI20:
5346 case R_NDS32_GOT_LO12:
5347 case R_NDS32_GOT_LO15:
5348 case R_NDS32_GOT_LO19:
5349 /* Relocation is to the entry for this symbol in the global
5350 offset table. */
5351 BFD_ASSERT (sgot != NULL);
5352
5353 if (h != NULL)
5354 {
5355 /* External symbol */
5356 bfd_boolean dyn;
5357
5358 off = h->got.offset;
5359 BFD_ASSERT (off != (bfd_vma) - 1);
5360 dyn = htab->root.dynamic_sections_created;
5361 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5362 bfd_link_pic (info),
5363 h)
5364 || (bfd_link_pic (info)
5365 && (info->symbolic
5366 || h->dynindx == -1
5367 || h->forced_local) && h->def_regular))
5368 {
5369 /* This is actually a static link, or it is a
5370 -Bsymbolic link and the symbol is defined
5371 locally, or the symbol was forced to be local
5372 because of a version file. We must initialize
5373 this entry in the global offset table. Since the
5374 offset must always be a multiple of 4, we use the
5375 least significant bit to record whether we have
5376 initialized it already.
5377
5378 When doing a dynamic link, we create a .rela.got
5379 relocation entry to initialize the value. This
5380 is done in the finish_dynamic_symbol routine. */
5381 if ((off & 1) != 0) /* clear LSB */
5382 off &= ~1;
5383 else
5384 {
5385 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5386 h->got.offset |= 1;
5387 }
5388 }
5389 relocation = sgot->output_section->vma + sgot->output_offset + off
5390 - elf_gp (output_bfd);
5391 }
5392 else
5393 {
5394 /* Local symbol */
5395 bfd_byte *loc;
5396
5397 BFD_ASSERT (local_got_offsets != NULL
5398 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5399
5400 off = local_got_offsets[r_symndx];
5401
5402 /* The offset must always be a multiple of 4. We use
5403 the least significant bit to record whether we have
5404 already processed this entry. */
5405 if ((off & 1) != 0) /* clear LSB */
5406 off &= ~1;
5407 else
5408 {
5409 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5410
5411 if (bfd_link_pic (info))
5412 {
5413 asection *srelgot;
5414 Elf_Internal_Rela outrel;
5415
5416 /* We need to generate a R_NDS32_RELATIVE reloc
5417 for the dynamic linker. */
5418 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5419 BFD_ASSERT (srelgot != NULL);
5420
5421 outrel.r_offset = (elf_gp (output_bfd)
5422 + sgot->output_offset + off);
5423 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5424 outrel.r_addend = relocation;
5425 loc = srelgot->contents;
5426 loc +=
5427 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5428 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5429 ++srelgot->reloc_count;
5430 }
5431 local_got_offsets[r_symndx] |= 1;
5432 }
5433 relocation = sgot->output_section->vma + sgot->output_offset + off
5434 - elf_gp (output_bfd);
5435 }
5436
5437 break;
5438
5439 case R_NDS32_16_RELA:
5440 case R_NDS32_20_RELA:
5441 case R_NDS32_5_RELA:
5442 case R_NDS32_32_RELA:
5443 case R_NDS32_9_PCREL_RELA:
5444 case R_NDS32_WORD_9_PCREL_RELA:
5445 case R_NDS32_10_UPCREL_RELA:
5446 case R_NDS32_15_PCREL_RELA:
5447 case R_NDS32_17_PCREL_RELA:
5448 case R_NDS32_25_PCREL_RELA:
5449 case R_NDS32_HI20_RELA:
5450 case R_NDS32_LO12S3_RELA:
5451 case R_NDS32_LO12S2_RELA:
5452 case R_NDS32_LO12S2_DP_RELA:
5453 case R_NDS32_LO12S2_SP_RELA:
5454 case R_NDS32_LO12S1_RELA:
5455 case R_NDS32_LO12S0_RELA:
5456 case R_NDS32_LO12S0_ORI_RELA:
5457 if (bfd_link_pic (info) && r_symndx != 0
5458 && (input_section->flags & SEC_ALLOC) != 0
5459 && (eliminate_gc_relocs == 0
5460 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5461 && ((r_type != R_NDS32_9_PCREL_RELA
5462 && r_type != R_NDS32_WORD_9_PCREL_RELA
5463 && r_type != R_NDS32_10_UPCREL_RELA
5464 && r_type != R_NDS32_15_PCREL_RELA
5465 && r_type != R_NDS32_17_PCREL_RELA
5466 && r_type != R_NDS32_25_PCREL_RELA
5467 && !(r_type == R_NDS32_32_RELA
5468 && strcmp (input_section->name, ".eh_frame") == 0))
5469 || (h != NULL && h->dynindx != -1
5470 && (!info->symbolic || !h->def_regular))))
5471 {
5472 Elf_Internal_Rela outrel;
5473 bfd_boolean skip, relocate;
5474 bfd_byte *loc;
5475
5476 /* When generating a shared object, these relocations
5477 are copied into the output file to be resolved at run
5478 time. */
5479
5480 if (sreloc == NULL)
5481 {
5482 const char *name;
5483
5484 name = bfd_elf_string_from_elf_section
5485 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5486 elf_section_data (input_section)->rela.hdr->sh_name);
5487 if (name == NULL)
5488 return FALSE;
5489
5490 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5491 && strcmp (bfd_section_name (input_section),
5492 name + 5) == 0);
5493
5494 sreloc = bfd_get_section_by_name (dynobj, name);
5495 BFD_ASSERT (sreloc != NULL);
5496 }
5497
5498 skip = FALSE;
5499 relocate = FALSE;
5500
5501 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5502 info,
5503 input_section,
5504 rel->r_offset);
5505 if (outrel.r_offset == (bfd_vma) - 1)
5506 skip = TRUE;
5507 else if (outrel.r_offset == (bfd_vma) - 2)
5508 skip = TRUE, relocate = TRUE;
5509 outrel.r_offset += (input_section->output_section->vma
5510 + input_section->output_offset);
5511
5512 if (skip)
5513 memset (&outrel, 0, sizeof outrel);
5514 else if (r_type == R_NDS32_17_PCREL_RELA
5515 || r_type == R_NDS32_15_PCREL_RELA
5516 || r_type == R_NDS32_25_PCREL_RELA)
5517 {
5518 BFD_ASSERT (h != NULL && h->dynindx != -1);
5519 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5520 outrel.r_addend = rel->r_addend;
5521 }
5522 else
5523 {
5524 /* h->dynindx may be -1 if this symbol was marked to
5525 become local. */
5526 if (h == NULL
5527 || ((info->symbolic || h->dynindx == -1)
5528 && h->def_regular)
5529 || (bfd_link_pie (info) && h->def_regular))
5530 {
5531 relocate = TRUE;
5532 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5533 outrel.r_addend = relocation + rel->r_addend;
5534
5535 if (h)
5536 {
5537 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5538
5539 BFD_ASSERT (sgot != NULL);
5540 /* If we did not allocate got entry for the symbol,
5541 we can not fill the nonexistent got entry. */
5542 if (h->got.offset != (bfd_vma) -1
5543 && (h->got.offset & 1) == 0)
5544 {
5545 bfd_put_32 (output_bfd, outrel.r_addend,
5546 sgot->contents + h->got.offset);
5547 }
5548 }
5549 }
5550 else
5551 {
5552 if (h->dynindx == -1)
5553 {
5554 _bfd_error_handler
5555 (_("%pB: relocation %s against `%s' can not be used when "
5556 "making a shared object; recompile with -fPIC"),
5557 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5558 bfd_set_error (bfd_error_bad_value);
5559 return FALSE;
5560 }
5561
5562 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5563 outrel.r_addend = rel->r_addend;
5564 }
5565 }
5566
5567 loc = sreloc->contents;
5568 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5569 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5570 ++sreloc->reloc_count;
5571
5572 /* If this reloc is against an external symbol, we do
5573 not want to fiddle with the addend. Otherwise, we
5574 need to include the symbol value so that it becomes
5575 an addend for the dynamic reloc. */
5576 if (!relocate)
5577 continue;
5578 }
5579 break;
5580
5581 case R_NDS32_25_ABS_RELA:
5582 if (bfd_link_pic (info))
5583 {
5584 _bfd_error_handler
5585 (_("%pB: warning: %s unsupported in shared mode"),
5586 input_bfd, "R_NDS32_25_ABS_RELA");
5587 return FALSE;
5588 }
5589 break;
5590
5591 case R_NDS32_9_PCREL:
5592 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5593 contents, offset,
5594 sec, relocation, addend);
5595 goto check_reloc;
5596
5597 case R_NDS32_HI20:
5598 /* We allow an arbitrary number of HI20 relocs before the
5599 LO12 reloc. This permits gcc to emit the HI and LO relocs
5600 itself. */
5601 for (lorel = rel + 1;
5602 (lorel < relend
5603 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5604 continue;
5605 if (lorel < relend
5606 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5607 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5608 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5609 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5610 {
5611 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5612 contents, relocation + addend);
5613 r = bfd_reloc_ok;
5614 }
5615 else
5616 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5617 contents, offset, relocation,
5618 addend);
5619 goto check_reloc;
5620
5621 case R_NDS32_GOT17S2_RELA:
5622 case R_NDS32_GOT15S2_RELA:
5623 BFD_ASSERT (sgot != NULL);
5624
5625 if (h != NULL)
5626 {
5627 bfd_boolean dyn;
5628
5629 off = h->got.offset;
5630 BFD_ASSERT (off != (bfd_vma) - 1);
5631
5632 dyn = htab->root.dynamic_sections_created;
5633 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5634 (dyn, bfd_link_pic (info), h)
5635 || (bfd_link_pic (info)
5636 && (info->symbolic
5637 || h->dynindx == -1
5638 || h->forced_local)
5639 && h->def_regular))
5640 {
5641 /* This is actually a static link, or it is a
5642 -Bsymbolic link and the symbol is defined
5643 locally, or the symbol was forced to be local
5644 because of a version file. We must initialize
5645 this entry in the global offset table. Since the
5646 offset must always be a multiple of 4, we use the
5647 least significant bit to record whether we have
5648 initialized it already.
5649
5650 When doing a dynamic link, we create a .rela.got
5651 relocation entry to initialize the value. This
5652 is done in the finish_dynamic_symbol routine. */
5653 if ((off & 1) != 0)
5654 off &= ~1;
5655 else
5656 {
5657 bfd_put_32 (output_bfd, relocation,
5658 sgot->contents + off);
5659 h->got.offset |= 1;
5660 }
5661 }
5662 }
5663 else
5664 {
5665 bfd_byte *loc;
5666
5667 BFD_ASSERT (local_got_offsets != NULL
5668 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5669
5670 off = local_got_offsets[r_symndx];
5671
5672 /* The offset must always be a multiple of 4. We use
5673 the least significant bit to record whether we have
5674 already processed this entry. */
5675 if ((off & 1) != 0)
5676 off &= ~1;
5677 else
5678 {
5679 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5680
5681 if (bfd_link_pic (info))
5682 {
5683 asection *srelgot;
5684 Elf_Internal_Rela outrel;
5685
5686 /* We need to generate a R_NDS32_RELATIVE reloc
5687 for the dynamic linker. */
5688 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5689 BFD_ASSERT (srelgot != NULL);
5690
5691 outrel.r_offset = (elf_gp (output_bfd)
5692 + sgot->output_offset + off);
5693 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5694 outrel.r_addend = relocation;
5695 loc = srelgot->contents;
5696 loc +=
5697 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5698 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5699 ++srelgot->reloc_count;
5700 }
5701 local_got_offsets[r_symndx] |= 1;
5702 }
5703 }
5704 relocation = sgot->output_section->vma + sgot->output_offset + off
5705 - elf_gp (output_bfd);
5706
5707 if (relocation & align)
5708 {
5709 /* Incorrect alignment. */
5710 _bfd_error_handler
5711 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5712 ret = FALSE;
5713 r = bfd_reloc_dangerous;
5714 goto check_reloc;
5715 }
5716 break;
5717
5718 case R_NDS32_SDA16S3_RELA:
5719 case R_NDS32_SDA15S3_RELA:
5720 case R_NDS32_SDA15S3:
5721 align = 0x7;
5722 goto handle_sda;
5723
5724 case R_NDS32_SDA17S2_RELA:
5725 case R_NDS32_SDA15S2_RELA:
5726 case R_NDS32_SDA12S2_SP_RELA:
5727 case R_NDS32_SDA12S2_DP_RELA:
5728 case R_NDS32_SDA15S2:
5729 case R_NDS32_SDA_FP7U2_RELA:
5730 align = 0x3;
5731 goto handle_sda;
5732
5733 case R_NDS32_SDA18S1_RELA:
5734 case R_NDS32_SDA15S1_RELA:
5735 case R_NDS32_SDA15S1:
5736 align = 0x1;
5737 goto handle_sda;
5738
5739 case R_NDS32_SDA19S0_RELA:
5740 case R_NDS32_SDA15S0_RELA:
5741 case R_NDS32_SDA15S0:
5742 align = 0x0;
5743 handle_sda:
5744 BFD_ASSERT (sec != NULL);
5745
5746 /* If the symbol is in the abs section, the out_bfd will be null.
5747 This happens when the relocation has a symbol@GOTOFF. */
5748 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5749 if (r != bfd_reloc_ok)
5750 {
5751 _bfd_error_handler
5752 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5753 ret = FALSE;
5754 goto check_reloc;
5755 }
5756
5757 /* At this point `relocation' contains the object's
5758 address. */
5759 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5760 {
5761 relocation -= fpbase_addr;
5762 }
5763 else
5764 relocation -= gp;
5765 /* Now it contains the offset from _SDA_BASE_. */
5766
5767 /* Make sure alignment is correct. */
5768
5769 if (relocation & align)
5770 {
5771 /* Incorrect alignment. */
5772 _bfd_error_handler
5773 /* xgettext:c-format */
5774 (_("%pB(%pA): warning: unaligned small data access"
5775 " of type %d"),
5776 input_bfd, input_section, r_type);
5777 ret = FALSE;
5778 goto check_reloc;
5779 }
5780 break;
5781
5782 case R_NDS32_17IFC_PCREL_RELA:
5783 case R_NDS32_10IFCU_PCREL_RELA:
5784 /* Do nothing. */
5785 break;
5786
5787 case R_NDS32_TLS_LE_HI20:
5788 case R_NDS32_TLS_LE_LO12:
5789 case R_NDS32_TLS_LE_20:
5790 case R_NDS32_TLS_LE_15S0:
5791 case R_NDS32_TLS_LE_15S1:
5792 case R_NDS32_TLS_LE_15S2:
5793 /* We do not have garbage collection for got entries.
5794 Therefore, IE to LE may have one empty entry, and DESC to
5795 LE may have two. */
5796 if (elf_hash_table (info)->tls_sec != NULL)
5797 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5798 break;
5799
5800 case R_NDS32_TLS_IE_HI20:
5801 case R_NDS32_TLS_IE_LO12S2:
5802 case R_NDS32_TLS_DESC_HI20:
5803 case R_NDS32_TLS_DESC_LO12:
5804 case R_NDS32_TLS_IE_LO12:
5805 case R_NDS32_TLS_IEGP_HI20:
5806 case R_NDS32_TLS_IEGP_LO12:
5807 case R_NDS32_TLS_IEGP_LO12S2:
5808 {
5809 /* Relocation is to the entry for this symbol in the global
5810 offset table. */
5811 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5812 asection *srelgot;
5813 Elf_Internal_Rela outrel;
5814 bfd_byte *loc;
5815 int indx = 0;
5816
5817 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5818
5819 BFD_ASSERT (sgot != NULL);
5820 if (h != NULL)
5821 {
5822 bfd_boolean dyn;
5823
5824 off = h->got.offset;
5825 BFD_ASSERT (off != (bfd_vma) -1);
5826 dyn = htab->root.dynamic_sections_created;
5827 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5828 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5829 && (!bfd_link_pic (info)
5830 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5831 indx = h->dynindx;
5832 }
5833 else
5834 {
5835 BFD_ASSERT (local_got_offsets != NULL
5836 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5837 off = local_got_offsets[r_symndx];
5838 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5839 }
5840
5841 relocation = sgot->output_section->vma + sgot->output_offset + off;
5842
5843 if (1 < ones32 (tls_type))
5844 {
5845 eff_tls_type = 1 << (fls (tls_type) - 1);
5846 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5847
5848 /* TLS model X -> LE is not implement yet!
5849 workaround here! */
5850 if (eff_tls_type == GOT_TLS_LE)
5851 {
5852 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5853 }
5854 }
5855
5856 /* The offset must always be a multiple of 4. We use
5857 the least significant bit to record whether we have
5858 already processed this entry. */
5859 bfd_boolean need_relocs = FALSE;
5860 srelgot = ehtab->srelgot;
5861 if ((bfd_link_pic (info) || indx != 0)
5862 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5863 || h->root.type != bfd_link_hash_undefweak))
5864 {
5865 need_relocs = TRUE;
5866 BFD_ASSERT (srelgot != NULL);
5867 }
5868
5869 if (off & 1)
5870 {
5871 off &= ~1;
5872 relocation &= ~1;
5873
5874 if (eff_tls_type & GOT_TLS_DESC)
5875 {
5876 relocation -= elf_gp (output_bfd);
5877 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5878 {
5879 /* TLS model shall be converted. */
5880 BFD_ASSERT(0);
5881 }
5882 }
5883 else if (eff_tls_type & GOT_TLS_IEGP)
5884 {
5885 relocation -= elf_gp (output_bfd);
5886 }
5887 }
5888 else
5889 {
5890 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5891 {
5892 /* TLS model workaround shall be applied. */
5893 BFD_ASSERT(0);
5894 }
5895 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5896 {
5897 if (eff_tls_type & GOT_TLS_IEGP)
5898 relocation -= elf_gp(output_bfd);
5899
5900 if (need_relocs)
5901 {
5902 if (indx == 0)
5903 outrel.r_addend = gottpoff (info, relocation_sym);
5904 else
5905 outrel.r_addend = 0;
5906 outrel.r_offset = (sgot->output_section->vma
5907 + sgot->output_offset + off);
5908 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5909
5910 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5911 &outrel);
5912 }
5913 else
5914 {
5915 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5916 sgot->contents + off);
5917 }
5918 }
5919 else if (eff_tls_type & GOT_TLS_DESC)
5920 {
5921 relocation -= elf_gp (output_bfd);
5922 if (need_relocs)
5923 {
5924 if (indx == 0)
5925 outrel.r_addend = gottpoff (info, relocation_sym);
5926 else
5927 outrel.r_addend = 0;
5928 outrel.r_offset = (sgot->output_section->vma
5929 + sgot->output_offset + off);
5930 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5931
5932 if (htab->tls_desc_trampoline)
5933 {
5934 asection *srelplt;
5935 srelplt = ehtab->srelplt;
5936 loc = srelplt->contents;
5937 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5938 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5939 <= srelplt->contents + srelplt->size);
5940
5941 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5942 }
5943 else
5944 {
5945 loc = srelgot->contents;
5946 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5947 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5948 ++srelgot->reloc_count;
5949 }
5950 }
5951 else
5952 {
5953 /* feed me! */
5954 bfd_put_32 (output_bfd, 0xdeadbeef,
5955 sgot->contents + off);
5956 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5957 sgot->contents + off + 4);
5958 patch_tls_desc_to_ie (contents, rel, input_bfd);
5959 BFD_ASSERT(0);
5960 }
5961 }
5962 else
5963 {
5964 /* TLS model workaround shall be applied. */
5965 BFD_ASSERT(0);
5966 }
5967
5968 if (h != NULL)
5969 h->got.offset |= 1;
5970 else
5971 local_got_offsets[r_symndx] |= 1;
5972 }
5973 }
5974 break;
5975 /* DON'T fall through. */
5976
5977 default:
5978 /* OLD_NDS32_RELOC. */
5979
5980 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5981 contents, offset, relocation, addend);
5982 goto check_reloc;
5983 }
5984
5985 switch ((int) r_type)
5986 {
5987 case R_NDS32_20_RELA:
5988 case R_NDS32_5_RELA:
5989 case R_NDS32_9_PCREL_RELA:
5990 case R_NDS32_WORD_9_PCREL_RELA:
5991 case R_NDS32_10_UPCREL_RELA:
5992 case R_NDS32_15_PCREL_RELA:
5993 case R_NDS32_17_PCREL_RELA:
5994 case R_NDS32_25_PCREL_RELA:
5995 case R_NDS32_25_ABS_RELA:
5996 case R_NDS32_HI20_RELA:
5997 case R_NDS32_LO12S3_RELA:
5998 case R_NDS32_LO12S2_RELA:
5999 case R_NDS32_LO12S2_DP_RELA:
6000 case R_NDS32_LO12S2_SP_RELA:
6001 case R_NDS32_LO12S1_RELA:
6002 case R_NDS32_LO12S0_RELA:
6003 case R_NDS32_LO12S0_ORI_RELA:
6004 case R_NDS32_SDA16S3_RELA:
6005 case R_NDS32_SDA17S2_RELA:
6006 case R_NDS32_SDA18S1_RELA:
6007 case R_NDS32_SDA19S0_RELA:
6008 case R_NDS32_SDA15S3_RELA:
6009 case R_NDS32_SDA15S2_RELA:
6010 case R_NDS32_SDA12S2_DP_RELA:
6011 case R_NDS32_SDA12S2_SP_RELA:
6012 case R_NDS32_SDA15S1_RELA:
6013 case R_NDS32_SDA15S0_RELA:
6014 case R_NDS32_SDA_FP7U2_RELA:
6015 case R_NDS32_9_PLTREL:
6016 case R_NDS32_25_PLTREL:
6017 case R_NDS32_GOT20:
6018 case R_NDS32_GOT_HI20:
6019 case R_NDS32_GOT_LO12:
6020 case R_NDS32_GOT_LO15:
6021 case R_NDS32_GOT_LO19:
6022 case R_NDS32_GOT15S2_RELA:
6023 case R_NDS32_GOT17S2_RELA:
6024 case R_NDS32_GOTPC20:
6025 case R_NDS32_GOTPC_HI20:
6026 case R_NDS32_GOTPC_LO12:
6027 case R_NDS32_GOTOFF:
6028 case R_NDS32_GOTOFF_HI20:
6029 case R_NDS32_GOTOFF_LO12:
6030 case R_NDS32_GOTOFF_LO15:
6031 case R_NDS32_GOTOFF_LO19:
6032 case R_NDS32_PLTREL_HI20:
6033 case R_NDS32_PLTREL_LO12:
6034 case R_NDS32_PLT_GOTREL_HI20:
6035 case R_NDS32_PLT_GOTREL_LO12:
6036 case R_NDS32_PLT_GOTREL_LO15:
6037 case R_NDS32_PLT_GOTREL_LO19:
6038 case R_NDS32_PLT_GOTREL_LO20:
6039 case R_NDS32_17IFC_PCREL_RELA:
6040 case R_NDS32_10IFCU_PCREL_RELA:
6041 case R_NDS32_TLS_LE_HI20:
6042 case R_NDS32_TLS_LE_LO12:
6043 case R_NDS32_TLS_IE_HI20:
6044 case R_NDS32_TLS_IE_LO12S2:
6045 case R_NDS32_TLS_LE_20:
6046 case R_NDS32_TLS_LE_15S0:
6047 case R_NDS32_TLS_LE_15S1:
6048 case R_NDS32_TLS_LE_15S2:
6049 case R_NDS32_TLS_DESC_HI20:
6050 case R_NDS32_TLS_DESC_LO12:
6051 case R_NDS32_TLS_IE_LO12:
6052 case R_NDS32_TLS_IEGP_HI20:
6053 case R_NDS32_TLS_IEGP_LO12:
6054 case R_NDS32_TLS_IEGP_LO12S2:
6055 /* Instruction related relocs must handle endian properly. */
6056 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
6057 r = nds32_elf_final_link_relocate (howto, input_bfd,
6058 input_section, contents,
6059 rel->r_offset, relocation,
6060 rel->r_addend);
6061 break;
6062
6063 default:
6064 /* All other relocs can use default handler. */
6065 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6066 contents, rel->r_offset,
6067 relocation, rel->r_addend);
6068 break;
6069 }
6070
6071 check_reloc:
6072
6073 if (r != bfd_reloc_ok)
6074 {
6075 /* FIXME: This should be generic enough to go in a utility. */
6076 const char *name;
6077
6078 if (h != NULL)
6079 name = h->root.root.string;
6080 else
6081 {
6082 name = bfd_elf_string_from_elf_section
6083 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6084 if (name == NULL || *name == '\0')
6085 name = bfd_section_name (sec);
6086 }
6087
6088 if (errmsg != NULL)
6089 goto common_error;
6090
6091 switch (r)
6092 {
6093 case bfd_reloc_overflow:
6094 (*info->callbacks->reloc_overflow)
6095 (info, (h ? &h->root : NULL), name, howto->name,
6096 (bfd_vma) 0, input_bfd, input_section, offset);
6097 break;
6098
6099 case bfd_reloc_undefined:
6100 (*info->callbacks->undefined_symbol)
6101 (info, name, input_bfd, input_section, offset, TRUE);
6102 break;
6103
6104 case bfd_reloc_outofrange:
6105 errmsg = _("internal error: out of range error");
6106 goto common_error;
6107
6108 case bfd_reloc_notsupported:
6109 errmsg = _("internal error: unsupported relocation error");
6110 goto common_error;
6111
6112 case bfd_reloc_dangerous:
6113 errmsg = _("internal error: dangerous error");
6114 goto common_error;
6115
6116 default:
6117 errmsg = _("internal error: unknown error");
6118 /* Fall through. */
6119
6120 common_error:
6121 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6122 input_section, offset);
6123 break;
6124 }
6125 }
6126 }
6127
6128 /* Resotre header size to avoid overflow load. */
6129 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6130 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6131
6132 return ret;
6133 }
6134
6135 /* Finish up dynamic symbol handling. We set the contents of various
6136 dynamic sections here. */
6137
6138 static bfd_boolean
6139 nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6140 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6141 {
6142 struct elf_link_hash_table *ehtab;
6143 struct elf_nds32_link_hash_entry *hent;
6144 bfd_byte *loc;
6145
6146 ehtab = elf_hash_table (info);
6147 hent = (struct elf_nds32_link_hash_entry *) h;
6148
6149 if (h->plt.offset != (bfd_vma) - 1)
6150 {
6151 asection *splt;
6152 asection *sgot;
6153 asection *srela;
6154
6155 bfd_vma plt_index;
6156 bfd_vma got_offset;
6157 bfd_vma local_plt_offset;
6158 Elf_Internal_Rela rela;
6159
6160 /* This symbol has an entry in the procedure linkage table. Set
6161 it up. */
6162
6163 BFD_ASSERT (h->dynindx != -1);
6164
6165 splt = ehtab->splt;
6166 sgot = ehtab->sgotplt;
6167 srela = ehtab->srelplt;
6168 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6169
6170 /* Get the index in the procedure linkage table which
6171 corresponds to this symbol. This is the index of this symbol
6172 in all the symbols for which we are making plt entries. The
6173 first entry in the procedure linkage table is reserved. */
6174 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6175
6176 /* Get the offset into the .got table of the entry that
6177 corresponds to this function. Each .got entry is 4 bytes.
6178 The first three are reserved. */
6179 got_offset = (plt_index + 3) * 4;
6180
6181 /* Fill in the entry in the procedure linkage table. */
6182 if (!bfd_link_pic (info))
6183 {
6184 unsigned long insn;
6185
6186 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6187 + sgot->output_offset + got_offset) >> 12)
6188 & 0xfffff);
6189 bfd_putb32 (insn, splt->contents + h->plt.offset);
6190
6191 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6192 + sgot->output_offset + got_offset) & 0x0fff)
6193 >> 2);
6194 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6195
6196 insn = PLT_ENTRY_WORD2;
6197 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6198
6199 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6200 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6201
6202 insn = PLT_ENTRY_WORD4
6203 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6204 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6205 local_plt_offset = 12;
6206 }
6207 else
6208 {
6209 /* sda_base must be set at this time. */
6210 unsigned long insn;
6211 long offset;
6212
6213 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6214 - elf_gp (output_bfd);
6215 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6216 bfd_putb32 (insn, splt->contents + h->plt.offset);
6217
6218 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6219 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6220
6221 insn = PLT_PIC_ENTRY_WORD2;
6222 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6223
6224 insn = PLT_PIC_ENTRY_WORD3;
6225 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6226
6227 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6228 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6229
6230 insn = PLT_PIC_ENTRY_WORD5
6231 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6232 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6233
6234 local_plt_offset = 16;
6235 }
6236
6237 /* Fill in the entry in the global offset table,
6238 so it will fall through to the next instruction for the first time. */
6239 bfd_put_32 (output_bfd,
6240 (splt->output_section->vma + splt->output_offset
6241 + h->plt.offset + local_plt_offset),
6242 sgot->contents + got_offset);
6243
6244 /* Fill in the entry in the .rela.plt section. */
6245 rela.r_offset = (sgot->output_section->vma
6246 + sgot->output_offset + got_offset);
6247 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6248 rela.r_addend = 0;
6249 loc = srela->contents;
6250 loc += plt_index * sizeof (Elf32_External_Rela);
6251 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6252
6253 if (!h->def_regular)
6254 {
6255 /* Mark the symbol as undefined, rather than as defined in
6256 the .plt section. Leave the value alone. */
6257 sym->st_shndx = SHN_UNDEF;
6258 if (!h->ref_regular_nonweak)
6259 sym->st_value = 0;
6260 }
6261 }
6262
6263 if (h->got.offset != (bfd_vma) - 1
6264 && hent->tls_type == GOT_NORMAL)
6265 {
6266 asection *sgot;
6267 asection *srelagot;
6268 Elf_Internal_Rela rela;
6269
6270 /* This symbol has an entry in the global offset table.
6271 Set it up. */
6272
6273 sgot = ehtab->sgot;
6274 srelagot = ehtab->srelgot;
6275 BFD_ASSERT (sgot != NULL && srelagot != NULL);
6276
6277 rela.r_offset = (sgot->output_section->vma
6278 + sgot->output_offset + (h->got.offset & ~1));
6279
6280 /* If this is a -Bsymbolic link, and the symbol is defined
6281 locally, we just want to emit a RELATIVE reloc. Likewise if
6282 the symbol was forced to be local because of a version file.
6283 The entry in the global offset table will already have been
6284 initialized in the relocate_section function. */
6285 if ((bfd_link_pic (info)
6286 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6287 && h->def_regular)
6288 || (bfd_link_pie (info) && h->def_regular))
6289 {
6290 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6291 rela.r_addend = (h->root.u.def.value
6292 + h->root.u.def.section->output_section->vma
6293 + h->root.u.def.section->output_offset);
6294
6295 if ((h->got.offset & 1) == 0)
6296 {
6297 bfd_put_32 (output_bfd, rela.r_addend,
6298 sgot->contents + h->got.offset);
6299 }
6300 }
6301 else
6302 {
6303 BFD_ASSERT ((h->got.offset & 1) == 0);
6304 bfd_put_32 (output_bfd, (bfd_vma) 0,
6305 sgot->contents + h->got.offset);
6306 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6307 rela.r_addend = 0;
6308 }
6309
6310 loc = srelagot->contents;
6311 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6312 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6313 ++srelagot->reloc_count;
6314 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6315 }
6316
6317 if (h->needs_copy)
6318 {
6319 asection *s;
6320 Elf_Internal_Rela rela;
6321
6322 /* This symbols needs a copy reloc. Set it up. */
6323
6324 BFD_ASSERT (h->dynindx != -1
6325 && (h->root.type == bfd_link_hash_defined
6326 || h->root.type == bfd_link_hash_defweak));
6327
6328 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6329 BFD_ASSERT (s != NULL);
6330
6331 rela.r_offset = (h->root.u.def.value
6332 + h->root.u.def.section->output_section->vma
6333 + h->root.u.def.section->output_offset);
6334 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6335 rela.r_addend = 0;
6336 loc = s->contents;
6337 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6338 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6339 ++s->reloc_count;
6340 }
6341
6342 /* Mark some specially defined symbols as absolute. */
6343 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6344 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6345 sym->st_shndx = SHN_ABS;
6346
6347 return TRUE;
6348 }
6349
6350
6351 /* Finish up the dynamic sections. */
6352
6353 static bfd_boolean
6354 nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6355 {
6356 bfd *dynobj;
6357 asection *sdyn;
6358 asection *sgotplt;
6359 struct elf_link_hash_table *ehtab;
6360 struct elf_nds32_link_hash_table *htab;
6361
6362 ehtab = elf_hash_table (info);
6363 htab = nds32_elf_hash_table (info);
6364 if (htab == NULL)
6365 return FALSE;
6366
6367 dynobj = elf_hash_table (info)->dynobj;
6368
6369 sgotplt = ehtab->sgotplt;
6370 /* A broken linker script might have discarded the dynamic sections.
6371 Catch this here so that we do not seg-fault later on. */
6372 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6373 return FALSE;
6374 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6375
6376 if (elf_hash_table (info)->dynamic_sections_created)
6377 {
6378 asection *splt;
6379 Elf32_External_Dyn *dyncon, *dynconend;
6380
6381 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6382
6383 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6384 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6385
6386 for (; dyncon < dynconend; dyncon++)
6387 {
6388 Elf_Internal_Dyn dyn;
6389 asection *s;
6390
6391 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6392
6393 switch (dyn.d_tag)
6394 {
6395 default:
6396 break;
6397
6398 case DT_PLTGOT:
6399 /* name = ".got"; */
6400 s = ehtab->sgot->output_section;
6401 goto get_vma;
6402 case DT_JMPREL:
6403 s = ehtab->srelplt->output_section;
6404 get_vma:
6405 BFD_ASSERT (s != NULL);
6406 dyn.d_un.d_ptr = s->vma;
6407 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6408 break;
6409
6410 case DT_PLTRELSZ:
6411 s = ehtab->srelplt->output_section;
6412 BFD_ASSERT (s != NULL);
6413 dyn.d_un.d_val = s->size;
6414 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6415 break;
6416
6417 case DT_RELASZ:
6418 /* My reading of the SVR4 ABI indicates that the
6419 procedure linkage table relocs (DT_JMPREL) should be
6420 included in the overall relocs (DT_RELA). This is
6421 what Solaris does. However, UnixWare can not handle
6422 that case. Therefore, we override the DT_RELASZ entry
6423 here to make it not include the JMPREL relocs. Since
6424 the linker script arranges for .rela.plt to follow all
6425 other relocation sections, we don't have to worry
6426 about changing the DT_RELA entry. */
6427 if (ehtab->srelplt != NULL)
6428 {
6429 s = ehtab->srelplt->output_section;
6430 dyn.d_un.d_val -= s->size;
6431 }
6432 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6433 break;
6434
6435 case DT_TLSDESC_PLT:
6436 s = htab->root.splt;
6437 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6438 + htab->dt_tlsdesc_plt);
6439 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6440 break;
6441
6442 case DT_TLSDESC_GOT:
6443 s = htab->root.sgot;
6444 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6445 + htab->dt_tlsdesc_got);
6446 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6447 break;
6448 }
6449 }
6450
6451 /* Fill in the first entry in the procedure linkage table. */
6452 splt = ehtab->splt;
6453 if (splt && splt->size > 0)
6454 {
6455 if (bfd_link_pic (info))
6456 {
6457 unsigned long insn;
6458 long offset;
6459
6460 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6461 - elf_gp (output_bfd);
6462 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6463 bfd_putb32 (insn, splt->contents);
6464
6465 /* here has a typo? */
6466 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6467 bfd_putb32 (insn, splt->contents + 4);
6468
6469 insn = PLT0_PIC_ENTRY_WORD2;
6470 bfd_putb32 (insn, splt->contents + 8);
6471
6472 insn = PLT0_PIC_ENTRY_WORD3;
6473 bfd_putb32 (insn, splt->contents + 12);
6474
6475 insn = PLT0_PIC_ENTRY_WORD4;
6476 bfd_putb32 (insn, splt->contents + 16);
6477
6478 insn = PLT0_PIC_ENTRY_WORD5;
6479 bfd_putb32 (insn, splt->contents + 20);
6480 }
6481 else
6482 {
6483 unsigned long insn;
6484 unsigned long addr;
6485
6486 /* addr = .got + 4 */
6487 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6488 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6489 bfd_putb32 (insn, splt->contents);
6490
6491 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6492 bfd_putb32 (insn, splt->contents + 4);
6493
6494 insn = PLT0_ENTRY_WORD2;
6495 bfd_putb32 (insn, splt->contents + 8);
6496
6497 insn = PLT0_ENTRY_WORD3;
6498 bfd_putb32 (insn, splt->contents + 12);
6499
6500 insn = PLT0_ENTRY_WORD4;
6501 bfd_putb32 (insn, splt->contents + 16);
6502 }
6503
6504 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6505 PLT_ENTRY_SIZE;
6506 }
6507
6508 if (htab->dt_tlsdesc_plt)
6509 {
6510 /* Calculate addresses. */
6511 asection *sgot = sgot = ehtab->sgot;
6512 bfd_vma pltgot = sgotplt->output_section->vma
6513 + sgotplt->output_offset;
6514 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6515 + htab->dt_tlsdesc_got;
6516
6517 /* Get GP offset. */
6518 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6519 tlsdesc_got -= elf_gp (output_bfd);
6520
6521 /* Do relocation. */
6522 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6523 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6524 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6525 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6526
6527 /* Insert .plt. */
6528 nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6529 dl_tlsdesc_lazy_trampoline,
6530 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6531 }
6532 }
6533
6534 /* Fill in the first three entries in the global offset table. */
6535 if (sgotplt && sgotplt->size > 0)
6536 {
6537 if (sdyn == NULL)
6538 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6539 else
6540 bfd_put_32 (output_bfd,
6541 sdyn->output_section->vma + sdyn->output_offset,
6542 sgotplt->contents);
6543 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6544 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6545
6546 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6547 }
6548
6549 return TRUE;
6550 }
6551 \f
6552
6553 /* Set the right machine number. */
6554
6555 static bfd_boolean
6556 nds32_elf_object_p (bfd *abfd)
6557 {
6558 static unsigned int cur_arch = 0;
6559
6560 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6561 {
6562 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6563 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6564 }
6565
6566 switch (cur_arch)
6567 {
6568 default:
6569 case E_N1_ARCH:
6570 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6571 break;
6572 case E_N1H_ARCH:
6573 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6574 break;
6575 case E_NDS_ARCH_STAR_V2_0:
6576 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6577 break;
6578 case E_NDS_ARCH_STAR_V3_0:
6579 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6580 break;
6581 case E_NDS_ARCH_STAR_V3_M:
6582 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6583 break;
6584 }
6585
6586 return TRUE;
6587 }
6588
6589 /* Store the machine number in the flags field. */
6590
6591 static bfd_boolean
6592 nds32_elf_final_write_processing (bfd *abfd)
6593 {
6594 unsigned long val;
6595 static unsigned int cur_mach = 0;
6596
6597 if (bfd_mach_n1 != bfd_get_mach (abfd))
6598 {
6599 cur_mach = bfd_get_mach (abfd);
6600 }
6601
6602 switch (cur_mach)
6603 {
6604 case bfd_mach_n1:
6605 /* Only happen when object is empty, since the case is abandon. */
6606 val = E_N1_ARCH;
6607 val |= E_NDS_ABI_AABI;
6608 val |= E_NDS32_ELF_VER_1_4;
6609 break;
6610 case bfd_mach_n1h:
6611 val = E_N1H_ARCH;
6612 break;
6613 case bfd_mach_n1h_v2:
6614 val = E_NDS_ARCH_STAR_V2_0;
6615 break;
6616 case bfd_mach_n1h_v3:
6617 val = E_NDS_ARCH_STAR_V3_0;
6618 break;
6619 case bfd_mach_n1h_v3m:
6620 val = E_NDS_ARCH_STAR_V3_M;
6621 break;
6622 default:
6623 val = 0;
6624 break;
6625 }
6626
6627 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6628 elf_elfheader (abfd)->e_flags |= val;
6629 return _bfd_elf_final_write_processing (abfd);
6630 }
6631
6632 /* Function to keep NDS32 specific file flags. */
6633
6634 static bfd_boolean
6635 nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6636 {
6637 BFD_ASSERT (!elf_flags_init (abfd)
6638 || elf_elfheader (abfd)->e_flags == flags);
6639
6640 elf_elfheader (abfd)->e_flags = flags;
6641 elf_flags_init (abfd) = TRUE;
6642 return TRUE;
6643 }
6644
6645 static unsigned int
6646 convert_e_flags (unsigned int e_flags, unsigned int arch)
6647 {
6648 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6649 {
6650 /* From 0.9 to 1.0. */
6651 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6652
6653 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6654 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6655 if (arch == E_NDS_ARCH_STAR_V1_0)
6656 {
6657 /* Done. */
6658 return e_flags;
6659 }
6660 }
6661
6662 /* From 1.0 to 2.0. */
6663 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6664
6665 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6666 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6667
6668 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6669 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6670 return e_flags;
6671 }
6672
6673 static bfd_boolean
6674 nds32_check_vec_size (bfd *ibfd)
6675 {
6676 static unsigned int nds32_vec_size = 0;
6677
6678 asection *sec_t = NULL;
6679 bfd_byte *contents = NULL;
6680
6681 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6682
6683 if (sec_t && sec_t->size >= 4)
6684 {
6685 /* Get vec_size in file. */
6686 unsigned int flag_t;
6687
6688 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
6689 flag_t = bfd_get_32 (ibfd, contents);
6690
6691 /* The value could only be 4 or 16. */
6692
6693 if (!nds32_vec_size)
6694 /* Set if not set yet. */
6695 nds32_vec_size = (flag_t & 0x3);
6696 else if (nds32_vec_size != (flag_t & 0x3))
6697 {
6698 _bfd_error_handler
6699 /* xgettext:c-format */
6700 (_("%pB: ISR vector size mismatch"
6701 " with previous modules, previous %u-byte, current %u-byte"),
6702 ibfd,
6703 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6704 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6705 return FALSE;
6706 }
6707 else
6708 /* Only keep the first vec_size section. */
6709 sec_t->flags |= SEC_EXCLUDE;
6710 }
6711
6712 return TRUE;
6713 }
6714
6715 /* Merge backend specific data from an object file to the output
6716 object file when linking. */
6717
6718 static bfd_boolean
6719 nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6720 {
6721 bfd *obfd = info->output_bfd;
6722 flagword out_flags;
6723 flagword in_flags;
6724 flagword out_16regs;
6725 flagword in_no_mac;
6726 flagword out_no_mac;
6727 flagword in_16regs;
6728 flagword out_version;
6729 flagword in_version;
6730 flagword out_fpu_config;
6731 flagword in_fpu_config;
6732
6733 /* FIXME: What should be checked when linking shared libraries? */
6734 if ((ibfd->flags & DYNAMIC) != 0)
6735 return TRUE;
6736
6737 /* TODO: Revise to use object-attributes instead. */
6738 if (!nds32_check_vec_size (ibfd))
6739 return FALSE;
6740
6741 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6742 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6743 return TRUE;
6744
6745 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6746 {
6747 _bfd_error_handler
6748 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6749
6750 bfd_set_error (bfd_error_bad_value);
6751 return FALSE;
6752 }
6753
6754 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6755 treat as generic one without checking and merging. */
6756 if (elf_elfheader (ibfd)->e_flags)
6757 {
6758 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6759 if (in_version == E_NDS32_ELF_VER_1_2)
6760 {
6761 _bfd_error_handler
6762 (_("%pB: warning: older version of object file encountered, "
6763 "please recompile with current tool chain"), ibfd);
6764 }
6765
6766 /* We may need to merge V1 and V2 arch object files to V2. */
6767 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6768 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6769 {
6770 /* Need to convert version. */
6771 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6772 == E_NDS_ARCH_STAR_RESERVED)
6773 {
6774 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6775 }
6776 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6777 == E_NDS_ARCH_STAR_V3_M
6778 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6779 == E_NDS_ARCH_STAR_V3_0)
6780 {
6781 elf_elfheader (ibfd)->e_flags =
6782 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6783 | E_NDS_ARCH_STAR_V3_0;
6784 }
6785 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6786 == E_NDS_ARCH_STAR_V0_9
6787 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6788 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6789 {
6790 elf_elfheader (obfd)->e_flags =
6791 convert_e_flags (elf_elfheader (obfd)->e_flags,
6792 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6793 }
6794 else
6795 {
6796 elf_elfheader (ibfd)->e_flags =
6797 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6798 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6799 }
6800 }
6801
6802 /* Extract some flags. */
6803 in_flags = elf_elfheader (ibfd)->e_flags
6804 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6805 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6806
6807 /* The following flags need special treatment. */
6808 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6809 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6810 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6811
6812 /* Extract some flags. */
6813 out_flags = elf_elfheader (obfd)->e_flags
6814 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6815 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6816
6817 /* The following flags need special treatment. */
6818 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6819 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6820 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6821 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6822 if (!elf_flags_init (obfd))
6823 {
6824 /* If the input is the default architecture then do not
6825 bother setting the flags for the output architecture,
6826 instead allow future merges to do this. If no future
6827 merges ever set these flags then they will retain their
6828 unitialised values, which surprise surprise, correspond
6829 to the default values. */
6830 if (bfd_get_arch_info (ibfd)->the_default)
6831 return TRUE;
6832
6833 elf_flags_init (obfd) = TRUE;
6834 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6835
6836 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6837 && bfd_get_arch_info (obfd)->the_default)
6838 {
6839 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6840 bfd_get_mach (ibfd));
6841 }
6842
6843 return TRUE;
6844 }
6845
6846 /* Check flag compatibility. */
6847 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6848 {
6849 _bfd_error_handler
6850 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6851 bfd_set_error (bfd_error_bad_value);
6852 return FALSE;
6853 }
6854
6855 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6856 {
6857 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6858 {
6859 _bfd_error_handler
6860 (_("%pB: error: instruction set mismatch with previous modules"),
6861 ibfd);
6862
6863 bfd_set_error (bfd_error_bad_value);
6864 return FALSE;
6865 }
6866 }
6867
6868 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6869 and perf ext1 and DIV are mergerd to perf ext1. */
6870 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6871 {
6872 elf_elfheader (obfd)->e_flags =
6873 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6874 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6875 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6876 ? E_NDS32_HAS_EXT_INST : 0)
6877 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6878 ? E_NDS32_HAS_EXT_INST : 0)
6879 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6880 | ((in_version > out_version) ? out_version : in_version);
6881 }
6882 else
6883 {
6884 if (in_version != out_version)
6885 _bfd_error_handler
6886 /* xgettext:c-format */
6887 (_("%pB: warning: incompatible elf-versions %s and %s"),
6888 ibfd, nds32_elfver_strtab[out_version],
6889 nds32_elfver_strtab[in_version]);
6890
6891 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6892 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6893 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6894 | (in_version > out_version ? out_version : in_version);
6895 }
6896 }
6897
6898 return TRUE;
6899 }
6900
6901 /* Display the flags field. */
6902
6903 static bfd_boolean
6904 nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6905 {
6906 FILE *file = (FILE *) ptr;
6907
6908 BFD_ASSERT (abfd != NULL && ptr != NULL);
6909
6910 _bfd_elf_print_private_bfd_data (abfd, ptr);
6911
6912 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6913
6914 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6915 {
6916 default:
6917 case E_N1_ARCH:
6918 fprintf (file, _(": n1 instructions"));
6919 break;
6920 case E_N1H_ARCH:
6921 fprintf (file, _(": n1h instructions"));
6922 break;
6923 }
6924
6925 fputc ('\n', file);
6926
6927 return TRUE;
6928 }
6929
6930 static unsigned int
6931 nds32_elf_action_discarded (asection *sec)
6932 {
6933
6934 if (strncmp
6935 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6936 return 0;
6937
6938 return _bfd_elf_default_action_discarded (sec);
6939 }
6940
6941 static asection *
6942 nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6943 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6944 Elf_Internal_Sym *sym)
6945 {
6946 if (h != NULL)
6947 switch (ELF32_R_TYPE (rel->r_info))
6948 {
6949 case R_NDS32_GNU_VTINHERIT:
6950 case R_NDS32_GNU_VTENTRY:
6951 case R_NDS32_RELA_GNU_VTINHERIT:
6952 case R_NDS32_RELA_GNU_VTENTRY:
6953 return NULL;
6954 }
6955
6956 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6957 }
6958
6959 static enum elf_nds32_tls_type
6960 get_tls_type (enum elf_nds32_reloc_type r_type,
6961 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6962 {
6963 enum elf_nds32_tls_type tls_type;
6964
6965 switch (r_type)
6966 {
6967 case R_NDS32_TLS_LE_HI20:
6968 case R_NDS32_TLS_LE_LO12:
6969 tls_type = GOT_TLS_LE;
6970 break;
6971 case R_NDS32_TLS_IE_HI20:
6972 case R_NDS32_TLS_IE_LO12S2:
6973 case R_NDS32_TLS_IE_LO12:
6974 tls_type = GOT_TLS_IE;
6975 break;
6976 case R_NDS32_TLS_IEGP_HI20:
6977 case R_NDS32_TLS_IEGP_LO12:
6978 case R_NDS32_TLS_IEGP_LO12S2:
6979 tls_type = GOT_TLS_IEGP;
6980 break;
6981 case R_NDS32_TLS_DESC_HI20:
6982 case R_NDS32_TLS_DESC_LO12:
6983 case R_NDS32_TLS_DESC_ADD:
6984 case R_NDS32_TLS_DESC_FUNC:
6985 case R_NDS32_TLS_DESC_CALL:
6986 tls_type = GOT_TLS_DESC;
6987 break;
6988 default:
6989 tls_type = GOT_NORMAL;
6990 break;
6991 }
6992
6993 return tls_type;
6994 }
6995
6996 /* Ensure that we have allocated bookkeeping structures for ABFD's local
6997 symbols. */
6998
6999 static bfd_boolean
7000 elf32_nds32_allocate_local_sym_info (bfd *abfd)
7001 {
7002 if (elf_local_got_refcounts (abfd) == NULL)
7003 {
7004 bfd_size_type num_syms;
7005 bfd_size_type size;
7006 char *data;
7007
7008 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7009 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7010 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7011 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7012 + sizeof (bfd_vma) + sizeof (int)
7013 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7014 data = bfd_zalloc (abfd, size);
7015 if (data == NULL)
7016 return FALSE;
7017
7018 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7019 data += num_syms * sizeof (bfd_signed_vma);
7020
7021 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7022 data += num_syms * sizeof (char);
7023
7024 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7025 data += num_syms * sizeof (bfd_vma);
7026
7027 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7028 data += num_syms * sizeof (int);
7029 }
7030
7031 return TRUE;
7032 }
7033
7034 /* Look through the relocs for a section during the first phase.
7035 Since we don't do .gots or .plts, we just need to consider the
7036 virtual table relocs for gc. */
7037
7038 static bfd_boolean
7039 nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7040 asection *sec, const Elf_Internal_Rela *relocs)
7041 {
7042 Elf_Internal_Shdr *symtab_hdr;
7043 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7044 const Elf_Internal_Rela *rel;
7045 const Elf_Internal_Rela *rel_end;
7046 struct elf_link_hash_table *ehtab;
7047 struct elf_nds32_link_hash_table *htab;
7048 bfd *dynobj;
7049 asection *sreloc = NULL;
7050
7051 /* No need for relocation if relocatable already. */
7052 if (bfd_link_relocatable (info))
7053 {
7054 elf32_nds32_check_relax_group (abfd, sec);
7055 return TRUE;
7056 }
7057
7058 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7059 sym_hashes = elf_sym_hashes (abfd);
7060 sym_hashes_end =
7061 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7062 if (!elf_bad_symtab (abfd))
7063 sym_hashes_end -= symtab_hdr->sh_info;
7064
7065 ehtab = elf_hash_table (info);
7066 htab = nds32_elf_hash_table (info);
7067 dynobj = htab->root.dynobj;
7068
7069 rel_end = relocs + sec->reloc_count;
7070 for (rel = relocs; rel < rel_end; rel++)
7071 {
7072 enum elf_nds32_reloc_type r_type;
7073 struct elf_link_hash_entry *h;
7074 unsigned long r_symndx;
7075 enum elf_nds32_tls_type tls_type, old_tls_type;
7076
7077 r_symndx = ELF32_R_SYM (rel->r_info);
7078 r_type = ELF32_R_TYPE (rel->r_info);
7079 if (r_symndx < symtab_hdr->sh_info)
7080 h = NULL;
7081 else
7082 {
7083 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7084 while (h->root.type == bfd_link_hash_indirect
7085 || h->root.type == bfd_link_hash_warning)
7086 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7087 }
7088
7089 /* Create .got section if necessary.
7090 Some relocs require a global offset table. We create
7091 got section here, since these relocation need a got section
7092 and if it is not created yet. */
7093 if (ehtab->sgot == NULL)
7094 {
7095 switch (r_type)
7096 {
7097 case R_NDS32_GOT_HI20:
7098 case R_NDS32_GOT_LO12:
7099 case R_NDS32_GOT_LO15:
7100 case R_NDS32_GOT_LO19:
7101 case R_NDS32_GOT17S2_RELA:
7102 case R_NDS32_GOT15S2_RELA:
7103 case R_NDS32_GOTOFF:
7104 case R_NDS32_GOTOFF_HI20:
7105 case R_NDS32_GOTOFF_LO12:
7106 case R_NDS32_GOTOFF_LO15:
7107 case R_NDS32_GOTOFF_LO19:
7108 case R_NDS32_GOTPC20:
7109 case R_NDS32_GOTPC_HI20:
7110 case R_NDS32_GOTPC_LO12:
7111 case R_NDS32_GOT20:
7112 case R_NDS32_TLS_IE_HI20:
7113 case R_NDS32_TLS_IE_LO12:
7114 case R_NDS32_TLS_IE_LO12S2:
7115 case R_NDS32_TLS_IEGP_HI20:
7116 case R_NDS32_TLS_IEGP_LO12:
7117 case R_NDS32_TLS_IEGP_LO12S2:
7118 case R_NDS32_TLS_DESC_HI20:
7119 case R_NDS32_TLS_DESC_LO12:
7120 if (dynobj == NULL)
7121 htab->root.dynobj = dynobj = abfd;
7122 if (!create_got_section (dynobj, info))
7123 return FALSE;
7124 break;
7125
7126 default:
7127 break;
7128 }
7129 }
7130
7131 /* Check relocation type. */
7132 switch ((int) r_type)
7133 {
7134 case R_NDS32_GOT_HI20:
7135 case R_NDS32_GOT_LO12:
7136 case R_NDS32_GOT_LO15:
7137 case R_NDS32_GOT_LO19:
7138 case R_NDS32_GOT20:
7139 case R_NDS32_TLS_LE_HI20:
7140 case R_NDS32_TLS_LE_LO12:
7141 case R_NDS32_TLS_IE_HI20:
7142 case R_NDS32_TLS_IE_LO12:
7143 case R_NDS32_TLS_IE_LO12S2:
7144 case R_NDS32_TLS_IEGP_HI20:
7145 case R_NDS32_TLS_IEGP_LO12:
7146 case R_NDS32_TLS_IEGP_LO12S2:
7147 case R_NDS32_TLS_DESC_HI20:
7148 case R_NDS32_TLS_DESC_LO12:
7149 tls_type = get_tls_type (r_type, h);
7150 if (h)
7151 {
7152 if (tls_type != GOT_TLS_LE)
7153 h->got.refcount += 1;
7154 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7155 }
7156 else
7157 {
7158 /* This is a global offset table entry for a local symbol. */
7159 if (!elf32_nds32_allocate_local_sym_info (abfd))
7160 return FALSE;
7161
7162 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7163 if (tls_type != GOT_TLS_LE)
7164 elf_local_got_refcounts (abfd)[r_symndx] += 1;
7165 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7166 }
7167
7168 /* We would already have issued an error message if there
7169 is a TLS/non-TLS mismatch, based on the symbol
7170 type. So just combine any TLS types needed. */
7171 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7172 && tls_type != GOT_NORMAL)
7173 tls_type |= old_tls_type;
7174
7175 /* DESC to IE/IEGP if link to executable. */
7176 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7177 && (bfd_link_executable (info)))
7178 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7179
7180 if (old_tls_type != tls_type)
7181 {
7182 if (h != NULL)
7183 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7184 else
7185 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7186 }
7187 break;
7188 case R_NDS32_9_PLTREL:
7189 case R_NDS32_25_PLTREL:
7190 case R_NDS32_PLTREL_HI20:
7191 case R_NDS32_PLTREL_LO12:
7192 case R_NDS32_PLT_GOTREL_HI20:
7193 case R_NDS32_PLT_GOTREL_LO12:
7194 case R_NDS32_PLT_GOTREL_LO15:
7195 case R_NDS32_PLT_GOTREL_LO19:
7196 case R_NDS32_PLT_GOTREL_LO20:
7197
7198 /* This symbol requires a procedure linkage table entry. We
7199 actually build the entry in adjust_dynamic_symbol,
7200 because this might be a case of linking PIC code without
7201 linking in any dynamic objects, in which case we don't
7202 need to generate a procedure linkage table after all. */
7203
7204 /* If this is a local symbol, we resolve it directly without
7205 creating a procedure linkage table entry. */
7206 if (h == NULL)
7207 continue;
7208
7209 if (h->forced_local
7210 || (bfd_link_pie (info) && h->def_regular))
7211 break;
7212
7213 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7214 h->needs_plt = 1;
7215 h->plt.refcount += 1;
7216 break;
7217
7218 case R_NDS32_16_RELA:
7219 case R_NDS32_20_RELA:
7220 case R_NDS32_5_RELA:
7221 case R_NDS32_32_RELA:
7222 case R_NDS32_HI20_RELA:
7223 case R_NDS32_LO12S3_RELA:
7224 case R_NDS32_LO12S2_RELA:
7225 case R_NDS32_LO12S2_DP_RELA:
7226 case R_NDS32_LO12S2_SP_RELA:
7227 case R_NDS32_LO12S1_RELA:
7228 case R_NDS32_LO12S0_RELA:
7229 case R_NDS32_LO12S0_ORI_RELA:
7230 case R_NDS32_SDA16S3_RELA:
7231 case R_NDS32_SDA17S2_RELA:
7232 case R_NDS32_SDA18S1_RELA:
7233 case R_NDS32_SDA19S0_RELA:
7234 case R_NDS32_SDA15S3_RELA:
7235 case R_NDS32_SDA15S2_RELA:
7236 case R_NDS32_SDA12S2_DP_RELA:
7237 case R_NDS32_SDA12S2_SP_RELA:
7238 case R_NDS32_SDA15S1_RELA:
7239 case R_NDS32_SDA15S0_RELA:
7240 case R_NDS32_SDA_FP7U2_RELA:
7241 case R_NDS32_15_PCREL_RELA:
7242 case R_NDS32_17_PCREL_RELA:
7243 case R_NDS32_25_PCREL_RELA:
7244
7245 if (h != NULL && !bfd_link_pic (info))
7246 {
7247 h->non_got_ref = 1;
7248 h->plt.refcount += 1;
7249 }
7250
7251 /* If we are creating a shared library, and this is a reloc against
7252 a global symbol, or a non PC relative reloc against a local
7253 symbol, then we need to copy the reloc into the shared library.
7254 However, if we are linking with -Bsymbolic, we do not need to
7255 copy a reloc against a global symbol which is defined in an
7256 object we are including in the link (i.e., DEF_REGULAR is set).
7257 At this point we have not seen all the input files, so it is
7258 possible that DEF_REGULAR is not set now but will be set later
7259 (it is never cleared). We account for that possibility below by
7260 storing information in the dyn_relocs field of the hash table
7261 entry. A similar situation occurs when creating shared libraries
7262 and symbol visibility changes render the symbol local.
7263
7264 If on the other hand, we are creating an executable, we may need
7265 to keep relocations for symbols satisfied by a dynamic library
7266 if we manage to avoid copy relocs for the symbol. */
7267 if ((bfd_link_pic (info)
7268 && (sec->flags & SEC_ALLOC) != 0
7269 && ((r_type != R_NDS32_25_PCREL_RELA
7270 && r_type != R_NDS32_15_PCREL_RELA
7271 && r_type != R_NDS32_17_PCREL_RELA
7272 && !(r_type == R_NDS32_32_RELA
7273 && strcmp (sec->name, ".eh_frame") == 0))
7274 || (h != NULL
7275 && (!info->symbolic
7276 || h->root.type == bfd_link_hash_defweak
7277 || !h->def_regular))))
7278 || (!bfd_link_pic (info)
7279 && (sec->flags & SEC_ALLOC) != 0
7280 && h != NULL
7281 && (h->root.type == bfd_link_hash_defweak
7282 || !h->def_regular)))
7283 {
7284 struct elf_dyn_relocs *p;
7285 struct elf_dyn_relocs **head;
7286
7287 if (dynobj == NULL)
7288 htab->root.dynobj = dynobj = abfd;
7289
7290 /* When creating a shared object, we must copy these
7291 relocs into the output file. We create a reloc
7292 section in dynobj and make room for the reloc. */
7293 if (sreloc == NULL)
7294 {
7295 const char *name;
7296
7297 name = bfd_elf_string_from_elf_section
7298 (abfd, elf_elfheader (abfd)->e_shstrndx,
7299 elf_section_data (sec)->rela.hdr->sh_name);
7300 if (name == NULL)
7301 return FALSE;
7302
7303 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
7304 && strcmp (bfd_section_name (sec),
7305 name + 5) == 0);
7306
7307 sreloc = bfd_get_section_by_name (dynobj, name);
7308 if (sreloc == NULL)
7309 {
7310 flagword flags;
7311
7312 sreloc = bfd_make_section (dynobj, name);
7313 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7314 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7315 if ((sec->flags & SEC_ALLOC) != 0)
7316 flags |= SEC_ALLOC | SEC_LOAD;
7317 if (sreloc == NULL
7318 || !bfd_set_section_flags (sreloc, flags)
7319 || !bfd_set_section_alignment (sreloc, 2))
7320 return FALSE;
7321
7322 elf_section_type (sreloc) = SHT_RELA;
7323 }
7324 elf_section_data (sec)->sreloc = sreloc;
7325 }
7326
7327 /* If this is a global symbol, we count the number of
7328 relocations we need for this symbol. */
7329 if (h != NULL)
7330 head = &h->dyn_relocs;
7331 else
7332 {
7333 asection *s;
7334 void *vpp;
7335
7336 Elf_Internal_Sym *isym;
7337 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7338 if (isym == NULL)
7339 return FALSE;
7340
7341 /* Track dynamic relocs needed for local syms too. */
7342 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7343 if (s == NULL)
7344 return FALSE;
7345
7346 vpp = &elf_section_data (s)->local_dynrel;
7347 head = (struct elf_dyn_relocs **) vpp;
7348 }
7349
7350 p = *head;
7351 if (p == NULL || p->sec != sec)
7352 {
7353 size_t amt = sizeof (*p);
7354 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7355 if (p == NULL)
7356 return FALSE;
7357 p->next = *head;
7358 *head = p;
7359 p->sec = sec;
7360 p->count = 0;
7361 p->pc_count = 0;
7362 }
7363
7364 p->count += 1;
7365
7366 /* Since eh_frame is readonly, R_NDS32_32_RELA
7367 reloc for eh_frame will cause shared library has
7368 TEXTREL entry in the dynamic section. This lead glibc
7369 testsuites to failure (bug-13092) and cause kernel fail
7370 (bug-11819). I think the best solution is to replace
7371 absolute reloc with pc relative reloc in the eh_frame.
7372 To do that, we need to support the following issues:
7373
7374 === For GCC ===
7375 * gcc/config/nds32/nds32.h: Define
7376 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7377 and DW_EH_PE_sdata4 into DWARF exception header when
7378 option have '-fpic'.
7379
7380 === For binutils ===
7381 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7382 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7383 may break our nds DIFF mechanism, therefore, we
7384 must disable all linker relaxations to ensure
7385 correctness.
7386 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7387 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7388 do the necessary modification.
7389
7390 Unfortunately, it still have some problems for nds32
7391 to support pc relative reloc in the eh_frame. So I use
7392 another solution to fix this issue.
7393
7394 However, I find that ld always emit TEXTREL marker for
7395 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7396 correspond to R_NDS32_32_RELA for .eh_frame section.
7397 It means that we always reserve redundant entries of rel.dyn
7398 for these relocs which actually do nothing in dynamic linker.
7399
7400 Therefore, we regard these relocs as pc relative relocs
7401 here and increase the pc_count. */
7402 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7403 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7404 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7405 || (r_type == R_NDS32_32_RELA
7406 && strcmp (sec->name, ".eh_frame") == 0))
7407 p->pc_count += 1;
7408 }
7409 break;
7410
7411 /* This relocation describes the C++ object vtable hierarchy.
7412 Reconstruct it for later use during GC. */
7413 case R_NDS32_RELA_GNU_VTINHERIT:
7414 case R_NDS32_GNU_VTINHERIT:
7415 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7416 return FALSE;
7417 break;
7418
7419 /* This relocation describes which C++ vtable entries are actually
7420 used. Record for later use during GC. */
7421 case R_NDS32_GNU_VTENTRY:
7422 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7423 return FALSE;
7424 break;
7425 case R_NDS32_RELA_GNU_VTENTRY:
7426 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7427 return FALSE;
7428 break;
7429 }
7430 }
7431
7432 return TRUE;
7433 }
7434
7435 /* Write VAL in uleb128 format to P, returning a pointer to the
7436 following byte.
7437 This code is copied from elf-attr.c. */
7438
7439 static bfd_byte *
7440 write_uleb128 (bfd_byte *p, unsigned int val)
7441 {
7442 bfd_byte c;
7443 do
7444 {
7445 c = val & 0x7f;
7446 val >>= 7;
7447 if (val)
7448 c |= 0x80;
7449 *(p++) = c;
7450 }
7451 while (val);
7452 return p;
7453 }
7454
7455 static bfd_signed_vma
7456 calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7457 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7458 {
7459 bfd_signed_vma foff;
7460 bfd_vma symval, addend;
7461 asection *sym_sec;
7462
7463 /* Get the value of the symbol referred to by the reloc. */
7464 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7465 {
7466 Elf_Internal_Sym *isym;
7467
7468 /* A local symbol. */
7469 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7470
7471 if (isym->st_shndx == SHN_UNDEF)
7472 sym_sec = bfd_und_section_ptr;
7473 else if (isym->st_shndx == SHN_ABS)
7474 sym_sec = bfd_abs_section_ptr;
7475 else if (isym->st_shndx == SHN_COMMON)
7476 sym_sec = bfd_com_section_ptr;
7477 else
7478 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7479 symval = isym->st_value + sym_sec->output_section->vma
7480 + sym_sec->output_offset;
7481 }
7482 else
7483 {
7484 unsigned long indx;
7485 struct elf_link_hash_entry *h;
7486
7487 /* An external symbol. */
7488 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7489 h = elf_sym_hashes (abfd)[indx];
7490 BFD_ASSERT (h != NULL);
7491
7492 if (h->root.type != bfd_link_hash_defined
7493 && h->root.type != bfd_link_hash_defweak)
7494 /* This appears to be a reference to an undefined
7495 symbol. Just ignore it--it will be caught by the
7496 regular reloc processing. */
7497 return 0;
7498
7499 if (h->root.u.def.section->flags & SEC_MERGE)
7500 {
7501 sym_sec = h->root.u.def.section;
7502 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7503 elf_section_data (sym_sec)->sec_info,
7504 h->root.u.def.value);
7505 symval = symval + sym_sec->output_section->vma
7506 + sym_sec->output_offset;
7507 }
7508 else
7509 symval = (h->root.u.def.value
7510 + h->root.u.def.section->output_section->vma
7511 + h->root.u.def.section->output_offset);
7512 }
7513
7514 addend = irel->r_addend;
7515
7516 foff = (symval + addend
7517 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7518 return foff;
7519 }
7520 \f
7521
7522 /* Convert a 32-bit instruction to 16-bit one.
7523 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7524 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7525 type of INSN16. Return 1 if successful. */
7526
7527 static int
7528 nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7529 int *pinsn_type)
7530 {
7531 uint16_t insn16 = 0;
7532 int insn_type = 0;
7533 unsigned long mach = bfd_get_mach (abfd);
7534
7535 if (N32_SH5 (insn) != 0)
7536 return 0;
7537
7538 switch (N32_SUB5 (insn))
7539 {
7540 case N32_ALU1_ADD_SLLI:
7541 case N32_ALU1_ADD_SRLI:
7542 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7543 {
7544 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7545 N32_RB5 (insn));
7546 insn_type = NDS32_INSN_ADD333;
7547 }
7548 else if (N32_IS_RT4 (insn))
7549 {
7550 if (N32_RT5 (insn) == N32_RA5 (insn))
7551 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7552 else if (N32_RT5 (insn) == N32_RB5 (insn))
7553 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7554 insn_type = NDS32_INSN_ADD45;
7555 }
7556 break;
7557
7558 case N32_ALU1_SUB_SLLI:
7559 case N32_ALU1_SUB_SRLI:
7560 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7561 {
7562 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7563 N32_RB5 (insn));
7564 insn_type = NDS32_INSN_SUB333;
7565 }
7566 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7567 {
7568 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7569 insn_type = NDS32_INSN_SUB45;
7570 }
7571 break;
7572
7573 case N32_ALU1_AND_SLLI:
7574 case N32_ALU1_AND_SRLI:
7575 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7576 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7577 && N32_IS_RB3 (insn))
7578 {
7579 if (N32_RT5 (insn) == N32_RA5 (insn))
7580 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7581 else if (N32_RT5 (insn) == N32_RB5 (insn))
7582 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7583 if (insn16)
7584 insn_type = NDS32_INSN_AND33;
7585 }
7586 break;
7587
7588 case N32_ALU1_XOR_SLLI:
7589 case N32_ALU1_XOR_SRLI:
7590 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7591 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7592 && N32_IS_RB3 (insn))
7593 {
7594 if (N32_RT5 (insn) == N32_RA5 (insn))
7595 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7596 else if (N32_RT5 (insn) == N32_RB5 (insn))
7597 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7598 if (insn16)
7599 insn_type = NDS32_INSN_XOR33;
7600 }
7601 break;
7602
7603 case N32_ALU1_OR_SLLI:
7604 case N32_ALU1_OR_SRLI:
7605 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7606 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7607 && N32_IS_RB3 (insn))
7608 {
7609 if (N32_RT5 (insn) == N32_RA5 (insn))
7610 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7611 else if (N32_RT5 (insn) == N32_RB5 (insn))
7612 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7613 if (insn16)
7614 insn_type = NDS32_INSN_OR33;
7615 }
7616 break;
7617 case N32_ALU1_NOR:
7618 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7619 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7620 && N32_RA5 (insn) == N32_RB5 (insn))
7621 {
7622 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7623 insn_type = NDS32_INSN_NOT33;
7624 }
7625 break;
7626 case N32_ALU1_SRAI:
7627 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7628 {
7629 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7630 insn_type = NDS32_INSN_SRAI45;
7631 }
7632 break;
7633
7634 case N32_ALU1_SRLI:
7635 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7636 {
7637 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7638 insn_type = NDS32_INSN_SRLI45;
7639 }
7640 break;
7641
7642 case N32_ALU1_SLLI:
7643 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7644 {
7645 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7646 N32_UB5 (insn));
7647 insn_type = NDS32_INSN_SLLI333;
7648 }
7649 break;
7650
7651 case N32_ALU1_ZEH:
7652 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7653 {
7654 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7655 insn_type = NDS32_INSN_ZEH33;
7656 }
7657 break;
7658
7659 case N32_ALU1_SEB:
7660 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7661 {
7662 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7663 insn_type = NDS32_INSN_SEB33;
7664 }
7665 break;
7666
7667 case N32_ALU1_SEH:
7668 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7669 {
7670 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7671 insn_type = NDS32_INSN_SEH33;
7672 }
7673 break;
7674
7675 case N32_ALU1_SLT:
7676 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7677 {
7678 /* Implicit r15. */
7679 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7680 insn_type = NDS32_INSN_SLT45;
7681 }
7682 break;
7683
7684 case N32_ALU1_SLTS:
7685 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7686 {
7687 /* Implicit r15. */
7688 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7689 insn_type = NDS32_INSN_SLTS45;
7690 }
7691 break;
7692 }
7693
7694 if ((insn16 & 0x8000) == 0)
7695 return 0;
7696
7697 if (pinsn16)
7698 *pinsn16 = insn16;
7699 if (pinsn_type)
7700 *pinsn_type = insn_type;
7701 return 1;
7702 }
7703
7704 static int
7705 nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7706 int *pinsn_type)
7707 {
7708 uint16_t insn16 = 0;
7709 int insn_type;
7710 unsigned long mach = bfd_get_mach (abfd);
7711
7712 /* TODO: bset, bclr, btgl, btst. */
7713 if (__GF (insn, 6, 4) != 0)
7714 return 0;
7715
7716 switch (N32_IMMU (insn, 6))
7717 {
7718 case N32_ALU2_MUL:
7719 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7720 && N32_IS_RB3 (insn))
7721 {
7722 if (N32_RT5 (insn) == N32_RA5 (insn))
7723 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7724 else if (N32_RT5 (insn) == N32_RB5 (insn))
7725 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7726 if (insn16)
7727 insn_type = NDS32_INSN_MUL33;
7728 }
7729 }
7730
7731 if ((insn16 & 0x8000) == 0)
7732 return 0;
7733
7734 if (pinsn16)
7735 *pinsn16 = insn16;
7736 if (pinsn_type)
7737 *pinsn_type = insn_type;
7738 return 1;
7739 }
7740
7741 int
7742 nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7743 int *pinsn_type)
7744 {
7745 int op6;
7746 uint16_t insn16 = 0;
7747 int insn_type = 0;
7748 unsigned long mach = bfd_get_mach (abfd);
7749
7750 /* Decode 32-bit instruction. */
7751 if (insn & 0x80000000)
7752 {
7753 /* Not 32-bit insn. */
7754 return 0;
7755 }
7756
7757 op6 = N32_OP6 (insn);
7758
7759 /* Convert it to 16-bit instruction. */
7760 switch (op6)
7761 {
7762 case N32_OP6_MOVI:
7763 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7764 {
7765 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7766 insn_type = NDS32_INSN_MOVI55;
7767 }
7768 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7769 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7770 {
7771 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7772 N32_IMM20S (insn) - 16);
7773 insn_type = NDS32_INSN_MOVPI45;
7774 }
7775 break;
7776
7777 case N32_OP6_ADDI:
7778 if (N32_IMM15S (insn) == 0)
7779 {
7780 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7781 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7782 if (mach <= MACH_V2
7783 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7784 {
7785 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7786 insn_type = NDS32_INSN_MOV55;
7787 }
7788 }
7789 else if (N32_IMM15S (insn) > 0)
7790 {
7791 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7792 {
7793 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7794 N32_IMM15S (insn));
7795 insn_type = NDS32_INSN_ADDI333;
7796 }
7797 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7798 && N32_IMM15S (insn) < 32)
7799 {
7800 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7801 insn_type = NDS32_INSN_ADDI45;
7802 }
7803 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7804 && N32_RT5 (insn) == N32_RA5 (insn)
7805 && N32_IMM15S (insn) < 512)
7806 {
7807 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7808 insn_type = NDS32_INSN_ADDI10_SP;
7809 }
7810 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7811 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7812 && (N32_IMM15S (insn) % 4 == 0))
7813 {
7814 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7815 N32_IMM15S (insn) >> 2);
7816 insn_type = NDS32_INSN_ADDRI36_SP;
7817 }
7818 }
7819 else
7820 {
7821 /* Less than 0. */
7822 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7823 {
7824 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7825 0 - N32_IMM15S (insn));
7826 insn_type = NDS32_INSN_SUBI333;
7827 }
7828 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7829 && N32_IMM15S (insn) > -32)
7830 {
7831 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7832 0 - N32_IMM15S (insn));
7833 insn_type = NDS32_INSN_SUBI45;
7834 }
7835 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7836 && N32_RT5 (insn) == N32_RA5 (insn)
7837 && N32_IMM15S (insn) >= -512)
7838 {
7839 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7840 insn_type = NDS32_INSN_ADDI10_SP;
7841 }
7842 }
7843 break;
7844
7845 case N32_OP6_ORI:
7846 if (N32_IMM15S (insn) == 0)
7847 {
7848 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7849 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7850 if (mach <= MACH_V2
7851 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7852 {
7853 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7854 insn_type = NDS32_INSN_MOV55;
7855 }
7856 }
7857 break;
7858
7859 case N32_OP6_SUBRI:
7860 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7861 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7862 {
7863 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7864 insn_type = NDS32_INSN_NEG33;
7865 }
7866 break;
7867
7868 case N32_OP6_ANDI:
7869 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7870 {
7871 if (N32_IMM15U (insn) == 1)
7872 {
7873 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7874 insn_type = NDS32_INSN_XLSB33;
7875 }
7876 else if (N32_IMM15U (insn) == 0x7ff)
7877 {
7878 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7879 insn_type = NDS32_INSN_X11B33;
7880 }
7881 else if (N32_IMM15U (insn) == 0xff)
7882 {
7883 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7884 insn_type = NDS32_INSN_ZEB33;
7885 }
7886 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7887 && N32_IMM15U (insn) < 256)
7888 {
7889 int imm15u = N32_IMM15U (insn);
7890
7891 if (__builtin_popcount (imm15u) == 1)
7892 {
7893 /* BMSKI33 */
7894 int imm3u = __builtin_ctz (imm15u);
7895
7896 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7897 insn_type = NDS32_INSN_BMSKI33;
7898 }
7899 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7900 {
7901 /* FEXTI33 */
7902 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7903
7904 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7905 insn_type = NDS32_INSN_FEXTI33;
7906 }
7907 }
7908 }
7909 break;
7910
7911 case N32_OP6_SLTI:
7912 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7913 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7914 {
7915 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7916 insn_type = NDS32_INSN_SLTI45;
7917 }
7918 break;
7919
7920 case N32_OP6_SLTSI:
7921 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7922 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7923 {
7924 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7925 insn_type = NDS32_INSN_SLTSI45;
7926 }
7927 break;
7928
7929 case N32_OP6_LWI:
7930 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7931 {
7932 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7933 insn_type = NDS32_INSN_LWI450;
7934 }
7935 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7936 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7937 {
7938 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7939 N32_IMM15S (insn));
7940 insn_type = NDS32_INSN_LWI333;
7941 }
7942 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7943 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7944 {
7945 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7946 insn_type = NDS32_INSN_LWI37;
7947 }
7948 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7949 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7950 {
7951 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7952 insn_type = NDS32_INSN_LWI37_SP;
7953 }
7954 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7955 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7956 {
7957 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7958 N32_IMM15S (insn) + 32);
7959 insn_type = NDS32_INSN_LWI45_FE;
7960 }
7961 break;
7962
7963 case N32_OP6_SWI:
7964 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7965 {
7966 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7967 insn_type = NDS32_INSN_SWI450;
7968 }
7969 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7970 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7971 {
7972 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7973 N32_IMM15S (insn));
7974 insn_type = NDS32_INSN_SWI333;
7975 }
7976 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7977 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7978 {
7979 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7980 insn_type = NDS32_INSN_SWI37;
7981 }
7982 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7983 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7984 {
7985 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7986 insn_type = NDS32_INSN_SWI37_SP;
7987 }
7988 break;
7989
7990 case N32_OP6_LWI_BI:
7991 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7992 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7993 {
7994 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7995 N32_IMM15S (insn));
7996 insn_type = NDS32_INSN_LWI333_BI;
7997 }
7998 break;
7999
8000 case N32_OP6_SWI_BI:
8001 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8002 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8003 {
8004 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8005 N32_IMM15S (insn));
8006 insn_type = NDS32_INSN_SWI333_BI;
8007 }
8008 break;
8009
8010 case N32_OP6_LHI:
8011 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8012 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8013 {
8014 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8015 N32_IMM15S (insn));
8016 insn_type = NDS32_INSN_LHI333;
8017 }
8018 break;
8019
8020 case N32_OP6_SHI:
8021 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8022 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8023 {
8024 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8025 N32_IMM15S (insn));
8026 insn_type = NDS32_INSN_SHI333;
8027 }
8028 break;
8029
8030 case N32_OP6_LBI:
8031 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8032 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8033 {
8034 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8035 N32_IMM15S (insn));
8036 insn_type = NDS32_INSN_LBI333;
8037 }
8038 break;
8039
8040 case N32_OP6_SBI:
8041 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8042 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8043 {
8044 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8045 N32_IMM15S (insn));
8046 insn_type = NDS32_INSN_SBI333;
8047 }
8048 break;
8049
8050 case N32_OP6_ALU1:
8051 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8052
8053 case N32_OP6_ALU2:
8054 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8055
8056 case N32_OP6_BR1:
8057 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8058 goto done;
8059
8060 if ((insn & N32_BIT (14)) == 0)
8061 {
8062 /* N32_BR1_BEQ */
8063 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8064 && N32_RT5 (insn) != REG_R5)
8065 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8066 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8067 && N32_RA5 (insn) != REG_R5)
8068 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8069 insn_type = NDS32_INSN_BEQS38;
8070 break;
8071 }
8072 else
8073 {
8074 /* N32_BR1_BNE */
8075 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8076 && N32_RT5 (insn) != REG_R5)
8077 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8078 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8079 && N32_RA5 (insn) != REG_R5)
8080 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8081 insn_type = NDS32_INSN_BNES38;
8082 break;
8083 }
8084 break;
8085
8086 case N32_OP6_BR2:
8087 switch (N32_BR2_SUB (insn))
8088 {
8089 case N32_BR2_BEQZ:
8090 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8091 {
8092 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8093 insn_type = NDS32_INSN_BEQZ38;
8094 }
8095 else if (N32_RT5 (insn) == REG_R15
8096 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8097 {
8098 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8099 insn_type = NDS32_INSN_BEQZS8;
8100 }
8101 break;
8102
8103 case N32_BR2_BNEZ:
8104 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8105 {
8106 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8107 insn_type = NDS32_INSN_BNEZ38;
8108 }
8109 else if (N32_RT5 (insn) == REG_R15
8110 && IS_WITHIN_S (N32_IMM16S (insn), 8))
8111 {
8112 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8113 insn_type = NDS32_INSN_BNEZS8;
8114 }
8115 break;
8116
8117 case N32_BR2_SOP0:
8118 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8119 {
8120 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8121 insn_type = NDS32_INSN_IFCALL9;
8122 }
8123 break;
8124 }
8125 break;
8126
8127 case N32_OP6_JI:
8128 if ((insn & N32_BIT (24)) == 0)
8129 {
8130 /* N32_JI_J */
8131 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8132 {
8133 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8134 insn_type = NDS32_INSN_J8;
8135 }
8136 }
8137 break;
8138
8139 case N32_OP6_JREG:
8140 if (__GF (insn, 8, 2) != 0)
8141 goto done;
8142
8143 switch (N32_IMMU (insn, 5))
8144 {
8145 case N32_JREG_JR:
8146 if (N32_JREG_HINT (insn) == 0)
8147 {
8148 /* jr */
8149 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8150 insn_type = NDS32_INSN_JR5;
8151 }
8152 else if (N32_JREG_HINT (insn) == 1)
8153 {
8154 /* ret */
8155 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8156 insn_type = NDS32_INSN_RET5;
8157 }
8158 else if (N32_JREG_HINT (insn) == 3)
8159 {
8160 /* ifret = mov55 $sp, $sp */
8161 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8162 insn_type = NDS32_INSN_IFRET;
8163 }
8164 break;
8165
8166 case N32_JREG_JRAL:
8167 /* It's convertible when return rt5 is $lp and address
8168 translation is kept. */
8169 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8170 {
8171 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8172 insn_type = NDS32_INSN_JRAL5;
8173 }
8174 break;
8175 }
8176 break;
8177
8178 case N32_OP6_MISC:
8179 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8180 {
8181 /* For v3, swid above 31 are used for ex9.it. */
8182 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8183 insn_type = NDS32_INSN_BREAK16;
8184 }
8185 break;
8186
8187 default:
8188 /* This instruction has no 16-bit variant. */
8189 goto done;
8190 }
8191
8192 done:
8193 /* Bit-15 of insn16 should be set for a valid instruction. */
8194 if ((insn16 & 0x8000) == 0)
8195 return 0;
8196
8197 if (pinsn16)
8198 *pinsn16 = insn16;
8199 if (pinsn_type)
8200 *pinsn_type = insn_type;
8201 return 1;
8202 }
8203
8204 static int
8205 special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8206 Elf_Internal_Rela *reloc)
8207 {
8208 uint16_t insn16 = 0;
8209
8210 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8211 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8212 return 0;
8213
8214 if (!N32_IS_RT3 (insn))
8215 return 0;
8216
8217 switch (N32_OP6 (insn))
8218 {
8219 case N32_OP6_LWI:
8220 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8221 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8222 break;
8223 case N32_OP6_SWI:
8224 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8225 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8226 break;
8227 case N32_OP6_HWGP:
8228 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8229 break;
8230
8231 if (__GF (insn, 17, 3) == 6)
8232 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8233 else if (__GF (insn, 17, 3) == 7)
8234 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8235 break;
8236 }
8237
8238 if ((insn16 & 0x8000) == 0)
8239 return 0;
8240
8241 *pinsn16 = insn16;
8242 return 1;
8243 }
8244
8245 /* Convert a 16-bit instruction to 32-bit one.
8246 INSN16 it the input and PINSN it the point to output.
8247 Return non-zero on successful. Otherwise 0 is returned. */
8248
8249 int
8250 nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8251 {
8252 uint32_t insn = 0xffffffff;
8253 unsigned long mach = bfd_get_mach (abfd);
8254
8255 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8256
8257 switch (__GF (insn16, 9, 6))
8258 {
8259 case 0x4: /* add45 */
8260 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8261 N16_RA5 (insn16));
8262 goto done;
8263 case 0x5: /* sub45 */
8264 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8265 N16_RA5 (insn16));
8266 goto done;
8267 case 0x6: /* addi45 */
8268 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8269 N16_IMM5U (insn16));
8270 goto done;
8271 case 0x7: /* subi45 */
8272 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8273 -N16_IMM5U (insn16));
8274 goto done;
8275 case 0x8: /* srai45 */
8276 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8277 N16_IMM5U (insn16));
8278 goto done;
8279 case 0x9: /* srli45 */
8280 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8281 N16_IMM5U (insn16));
8282 goto done;
8283 case 0xa: /* slli333 */
8284 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8285 N16_IMM3U (insn16));
8286 goto done;
8287 case 0xc: /* add333 */
8288 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8289 N16_RB3 (insn16));
8290 goto done;
8291 case 0xd: /* sub333 */
8292 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8293 N16_RB3 (insn16));
8294 goto done;
8295 case 0xe: /* addi333 */
8296 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8297 N16_IMM3U (insn16));
8298 goto done;
8299 case 0xf: /* subi333 */
8300 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8301 -N16_IMM3U (insn16));
8302 goto done;
8303 case 0x10: /* lwi333 */
8304 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8305 N16_IMM3U (insn16));
8306 goto done;
8307 case 0x12: /* lhi333 */
8308 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8309 N16_IMM3U (insn16));
8310 goto done;
8311 case 0x13: /* lbi333 */
8312 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8313 N16_IMM3U (insn16));
8314 goto done;
8315 case 0x11: /* lwi333.bi */
8316 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8317 N16_IMM3U (insn16));
8318 goto done;
8319 case 0x14: /* swi333 */
8320 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8321 N16_IMM3U (insn16));
8322 goto done;
8323 case 0x16: /* shi333 */
8324 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8325 N16_IMM3U (insn16));
8326 goto done;
8327 case 0x17: /* sbi333 */
8328 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8329 N16_IMM3U (insn16));
8330 goto done;
8331 case 0x15: /* swi333.bi */
8332 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8333 N16_IMM3U (insn16));
8334 goto done;
8335 case 0x18: /* addri36.sp */
8336 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8337 N16_IMM6U (insn16) << 2);
8338 goto done;
8339 case 0x19: /* lwi45.fe */
8340 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8341 (N16_IMM5U (insn16) - 32));
8342 goto done;
8343 case 0x1a: /* lwi450 */
8344 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8345 goto done;
8346 case 0x1b: /* swi450 */
8347 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8348 goto done;
8349
8350 /* These are r15 implied instructions. */
8351 case 0x30: /* slts45 */
8352 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8353 goto done;
8354 case 0x31: /* slt45 */
8355 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8356 goto done;
8357 case 0x32: /* sltsi45 */
8358 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8359 goto done;
8360 case 0x33: /* slti45 */
8361 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8362 goto done;
8363 case 0x34: /* beqzs8, bnezs8 */
8364 if (insn16 & N32_BIT (8))
8365 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8366 else
8367 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8368 goto done;
8369
8370 case 0x35: /* break16, ex9.it */
8371 /* Only consider range of v3 break16. */
8372 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8373 goto done;
8374
8375 case 0x3c: /* ifcall9 */
8376 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8377 goto done;
8378 case 0x3d: /* movpi45 */
8379 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8380 goto done;
8381
8382 case 0x3f: /* MISC33 */
8383 switch (insn16 & 0x7)
8384 {
8385 case 2: /* neg33 */
8386 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8387 break;
8388 case 3: /* not33 */
8389 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8390 N16_RA3 (insn16));
8391 break;
8392 case 4: /* mul33 */
8393 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8394 N16_RA3 (insn16));
8395 break;
8396 case 5: /* xor33 */
8397 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8398 N16_RA3 (insn16));
8399 break;
8400 case 6: /* and33 */
8401 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8402 N16_RA3 (insn16));
8403 break;
8404 case 7: /* or33 */
8405 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8406 N16_RA3 (insn16));
8407 break;
8408 }
8409 goto done;
8410
8411 case 0xb:
8412 switch (insn16 & 0x7)
8413 {
8414 case 0: /* zeb33 */
8415 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8416 break;
8417 case 1: /* zeh33 */
8418 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8419 break;
8420 case 2: /* seb33 */
8421 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8422 break;
8423 case 3: /* seh33 */
8424 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8425 break;
8426 case 4: /* xlsb33 */
8427 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8428 break;
8429 case 5: /* x11b33 */
8430 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8431 break;
8432 case 6: /* bmski33 */
8433 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8434 1 << __GF (insn16, 3, 3));
8435 break;
8436 case 7: /* fexti33 */
8437 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8438 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8439 break;
8440 }
8441 goto done;
8442 }
8443
8444 switch (__GF (insn16, 10, 5))
8445 {
8446 case 0x0: /* mov55 or ifret16 */
8447 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8448 && N16_RT5 (insn16) == N16_RA5 (insn16))
8449 insn = N32_JREG (JR, 0, 0, 0, 3);
8450 else
8451 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8452 goto done;
8453 case 0x1: /* movi55 */
8454 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8455 goto done;
8456 case 0x1b: /* addi10s (V2) */
8457 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8458 goto done;
8459 }
8460
8461 switch (__GF (insn16, 11, 4))
8462 {
8463 case 0x7: /* lwi37.fp/swi37.fp */
8464 if (insn16 & N32_BIT (7)) /* swi37.fp */
8465 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8466 else /* lwi37.fp */
8467 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8468 goto done;
8469 case 0x8: /* beqz38 */
8470 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8471 goto done;
8472 case 0x9: /* bnez38 */
8473 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8474 goto done;
8475 case 0xa: /* beqs38/j8, implied r5 */
8476 if (N16_RT38 (insn16) == 5)
8477 insn = N32_JI (J, N16_IMM8S (insn16));
8478 else
8479 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8480 goto done;
8481 case 0xb: /* bnes38 and others. */
8482 if (N16_RT38 (insn16) == 5)
8483 {
8484 switch (__GF (insn16, 5, 3))
8485 {
8486 case 0: /* jr5 */
8487 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8488 break;
8489 case 4: /* ret5 */
8490 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8491 break;
8492 case 1: /* jral5 */
8493 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8494 break;
8495 case 2: /* ex9.it imm5 */
8496 /* ex9.it had no 32-bit variantl. */
8497 break;
8498 case 5: /* add5.pc */
8499 /* add5.pc had no 32-bit variantl. */
8500 break;
8501 }
8502 }
8503 else /* bnes38 */
8504 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8505 goto done;
8506 case 0xe: /* lwi37/swi37 */
8507 if (insn16 & (1 << 7)) /* swi37.sp */
8508 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8509 else /* lwi37.sp */
8510 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8511 goto done;
8512 }
8513
8514 done:
8515 if (insn & 0x80000000)
8516 return 0;
8517
8518 if (pinsn)
8519 *pinsn = insn;
8520 return 1;
8521 }
8522 \f
8523
8524 static bfd_boolean
8525 is_sda_access_insn (unsigned long insn)
8526 {
8527 switch (N32_OP6 (insn))
8528 {
8529 case N32_OP6_LWI:
8530 case N32_OP6_LHI:
8531 case N32_OP6_LHSI:
8532 case N32_OP6_LBI:
8533 case N32_OP6_LBSI:
8534 case N32_OP6_SWI:
8535 case N32_OP6_SHI:
8536 case N32_OP6_SBI:
8537 case N32_OP6_LWC:
8538 case N32_OP6_LDC:
8539 case N32_OP6_SWC:
8540 case N32_OP6_SDC:
8541 return TRUE;
8542 default:
8543 ;
8544 }
8545 return FALSE;
8546 }
8547
8548 static unsigned long
8549 turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8550 {
8551 uint32_t oinsn = 0;
8552
8553 switch (type)
8554 {
8555 case R_NDS32_GOT_LO12:
8556 case R_NDS32_GOTOFF_LO12:
8557 case R_NDS32_PLTREL_LO12:
8558 case R_NDS32_PLT_GOTREL_LO12:
8559 case R_NDS32_LO12S0_RELA:
8560 switch (N32_OP6 (insn))
8561 {
8562 case N32_OP6_LBI:
8563 /* lbi.gp */
8564 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8565 break;
8566 case N32_OP6_LBSI:
8567 /* lbsi.gp */
8568 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8569 break;
8570 case N32_OP6_SBI:
8571 /* sbi.gp */
8572 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8573 break;
8574 case N32_OP6_ORI:
8575 /* addi.gp */
8576 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8577 break;
8578 }
8579 break;
8580
8581 case R_NDS32_LO12S1_RELA:
8582 switch (N32_OP6 (insn))
8583 {
8584 case N32_OP6_LHI:
8585 /* lhi.gp */
8586 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8587 break;
8588 case N32_OP6_LHSI:
8589 /* lhsi.gp */
8590 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8591 break;
8592 case N32_OP6_SHI:
8593 /* shi.gp */
8594 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8595 break;
8596 }
8597 break;
8598
8599 case R_NDS32_LO12S2_RELA:
8600 switch (N32_OP6 (insn))
8601 {
8602 case N32_OP6_LWI:
8603 /* lwi.gp */
8604 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8605 break;
8606 case N32_OP6_SWI:
8607 /* swi.gp */
8608 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8609 break;
8610 }
8611 break;
8612
8613 case R_NDS32_LO12S2_DP_RELA:
8614 case R_NDS32_LO12S2_SP_RELA:
8615 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8616 break;
8617 }
8618
8619 if (oinsn)
8620 *pinsn = oinsn;
8621
8622 return oinsn != 0;
8623 }
8624
8625 /* Linker hasn't found the correct merge section for non-section symbol
8626 in relax time, this work is left to the function elf_link_input_bfd().
8627 So for non-section symbol, _bfd_merged_section_offset is also needed
8628 to find the correct symbol address. */
8629
8630 static bfd_vma
8631 nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8632 asection **psec, Elf_Internal_Rela *rel)
8633 {
8634 asection *sec = *psec;
8635 bfd_vma relocation;
8636
8637 relocation = (sec->output_section->vma
8638 + sec->output_offset + sym->st_value);
8639 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8640 {
8641 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8642 rel->r_addend =
8643 _bfd_merged_section_offset (abfd, psec,
8644 elf_section_data (sec)->sec_info,
8645 sym->st_value + rel->r_addend);
8646 else
8647 rel->r_addend =
8648 _bfd_merged_section_offset (abfd, psec,
8649 elf_section_data (sec)->sec_info,
8650 sym->st_value) + rel->r_addend;
8651
8652 if (sec != *psec)
8653 {
8654 /* If we have changed the section, and our original section is
8655 marked with SEC_EXCLUDE, it means that the original
8656 SEC_MERGE section has been completely subsumed in some
8657 other SEC_MERGE section. In this case, we need to leave
8658 some info around for --emit-relocs. */
8659 if ((sec->flags & SEC_EXCLUDE) != 0)
8660 sec->kept_section = *psec;
8661 sec = *psec;
8662 }
8663 rel->r_addend -= relocation;
8664 rel->r_addend += sec->output_section->vma + sec->output_offset;
8665 }
8666 return relocation;
8667 }
8668
8669 static bfd_vma
8670 calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8671 Elf_Internal_Sym *isymbuf,
8672 Elf_Internal_Shdr *symtab_hdr)
8673 {
8674 bfd_signed_vma foff;
8675 bfd_vma symval, addend;
8676 Elf_Internal_Rela irel_fn;
8677 Elf_Internal_Sym *isym;
8678 asection *sym_sec;
8679
8680 /* Get the value of the symbol referred to by the reloc. */
8681 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8682 {
8683 /* A local symbol. */
8684 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8685
8686 if (isym->st_shndx == SHN_UNDEF)
8687 sym_sec = bfd_und_section_ptr;
8688 else if (isym->st_shndx == SHN_ABS)
8689 sym_sec = bfd_abs_section_ptr;
8690 else if (isym->st_shndx == SHN_COMMON)
8691 sym_sec = bfd_com_section_ptr;
8692 else
8693 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8694 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8695 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8696 addend = irel_fn.r_addend;
8697 }
8698 else
8699 {
8700 unsigned long indx;
8701 struct elf_link_hash_entry *h;
8702
8703 /* An external symbol. */
8704 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8705 h = elf_sym_hashes (abfd)[indx];
8706 BFD_ASSERT (h != NULL);
8707
8708 while (h->root.type == bfd_link_hash_indirect
8709 || h->root.type == bfd_link_hash_warning)
8710 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8711
8712 if (h->root.type != bfd_link_hash_defined
8713 && h->root.type != bfd_link_hash_defweak)
8714 /* This appears to be a reference to an undefined
8715 symbol. Just ignore it--it will be caught by the
8716 regular reloc processing. */
8717 return 0;
8718
8719 if (h->root.u.def.section->flags & SEC_MERGE)
8720 {
8721 sym_sec = h->root.u.def.section;
8722 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8723 (sym_sec)->sec_info, h->root.u.def.value);
8724 symval = symval + sym_sec->output_section->vma
8725 + sym_sec->output_offset;
8726 }
8727 else
8728 symval = (h->root.u.def.value
8729 + h->root.u.def.section->output_section->vma
8730 + h->root.u.def.section->output_offset);
8731 addend = irel->r_addend;
8732 }
8733
8734 foff = symval + addend;
8735
8736 return foff;
8737 }
8738
8739 static int
8740 is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8741 asection *sec, Elf_Internal_Rela *rel)
8742 {
8743 bfd_byte *contents;
8744 unsigned short insn16;
8745
8746 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8747 return FALSE;
8748 contents = elf_section_data (sec)->this_hdr.contents;
8749 insn16 = bfd_getb16 (contents + rel->r_offset);
8750 if (insn16 == NDS32_NOP16)
8751 return TRUE;
8752 return FALSE;
8753 }
8754
8755 /* It checks whether the instruction could be converted to
8756 16-bit form and returns the converted one.
8757
8758 `internal_relocs' is supposed to be sorted. */
8759
8760 static int
8761 is_convert_32_to_16 (bfd *abfd, asection *sec,
8762 Elf_Internal_Rela *reloc,
8763 Elf_Internal_Rela *internal_relocs,
8764 Elf_Internal_Rela *irelend,
8765 uint16_t *insn16)
8766 {
8767 #define NORMAL_32_TO_16 (1 << 0)
8768 #define SPECIAL_32_TO_16 (1 << 1)
8769 bfd_byte *contents = NULL;
8770 bfd_signed_vma off;
8771 bfd_vma mem_addr;
8772 uint32_t insn = 0;
8773 Elf_Internal_Rela *pc_rel;
8774 Elf_Internal_Shdr *symtab_hdr;
8775 Elf_Internal_Sym *isymbuf = NULL;
8776 int convert_type;
8777 bfd_vma offset;
8778
8779 if (reloc->r_offset + 4 > sec->size)
8780 return FALSE;
8781
8782 offset = reloc->r_offset;
8783
8784 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
8785 return FALSE;
8786 insn = bfd_getb32 (contents + offset);
8787
8788 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8789 convert_type = NORMAL_32_TO_16;
8790 else if (special_convert_32_to_16 (insn, insn16, reloc))
8791 convert_type = SPECIAL_32_TO_16;
8792 else
8793 return FALSE;
8794
8795 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8796 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8797 return FALSE;
8798
8799 /* Find the first relocation of the same relocation-type,
8800 so we iteratie them forward. */
8801 pc_rel = reloc;
8802 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8803 pc_rel--;
8804
8805 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8806 {
8807 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8808 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8809 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8810 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8811 {
8812 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8813 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8814 || off == 0)
8815 return FALSE;
8816 break;
8817 }
8818 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8819 {
8820 /* movi => movi55 */
8821 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8822 symtab_hdr);
8823 /* mem_addr is unsigned, but the value should
8824 be between [-16, 15]. */
8825 if ((mem_addr + 0x10) >> 5)
8826 return FALSE;
8827 break;
8828 }
8829 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8830 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8831 {
8832 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8833 because it can be relaxed to addi for TLS_LE_ADD. */
8834 return FALSE;
8835 }
8836 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8837 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8838 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8839 && convert_type == SPECIAL_32_TO_16)
8840 {
8841 /* fp-as-gp
8842 We've selected a best fp-base for this access, so we can
8843 always resolve it anyway. Do nothing. */
8844 break;
8845 }
8846 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8847 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8848 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8849 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8850 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8851 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8852 {
8853 /* Prevent unresolved addi instruction translate
8854 to addi45 or addi333. */
8855 return FALSE;
8856 }
8857 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8858 {
8859 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8860 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8861 return FALSE;
8862 break;
8863 }
8864 }
8865
8866 return TRUE;
8867 }
8868
8869 static void
8870 nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8871 Elf_Internal_Rela *reloc,
8872 Elf_Internal_Rela *internal_relocs,
8873 Elf_Internal_Rela *irelend,
8874 unsigned short insn16)
8875 {
8876 Elf_Internal_Rela *pc_rel;
8877 bfd_vma offset;
8878
8879 offset = reloc->r_offset;
8880 bfd_putb16 (insn16, contents + offset);
8881 /* Find the first relocation of the same relocation-type,
8882 so we iteratie them forward. */
8883 pc_rel = reloc;
8884 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8885 pc_rel--;
8886
8887 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8888 {
8889 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8890 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8891 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8892 {
8893 pc_rel->r_info =
8894 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8895 }
8896 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8897 pc_rel->r_info =
8898 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8899 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8900 pc_rel->r_info =
8901 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8902 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8903 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8904 pc_rel->r_info =
8905 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8906 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8907 pc_rel->r_info =
8908 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8909 }
8910 }
8911
8912 /* Find a relocation of type specified by `reloc_type'
8913 of the same r_offset with reloc.
8914 If not found, return irelend.
8915
8916 Assuming relocations are sorted by r_offset,
8917 we find the relocation from `reloc' backward untill relocs,
8918 or find it from `reloc' forward untill irelend. */
8919
8920 static Elf_Internal_Rela *
8921 find_relocs_at_address (Elf_Internal_Rela *reloc,
8922 Elf_Internal_Rela *relocs,
8923 Elf_Internal_Rela *irelend,
8924 enum elf_nds32_reloc_type reloc_type)
8925 {
8926 Elf_Internal_Rela *rel_t;
8927
8928 /* Find backward. */
8929 for (rel_t = reloc;
8930 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8931 rel_t--)
8932 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8933 return rel_t;
8934
8935 /* We didn't find it backward. Try find it forward. */
8936 for (rel_t = reloc;
8937 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8938 rel_t++)
8939 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8940 return rel_t;
8941
8942 return irelend;
8943 }
8944
8945 /* Find a relocation of specified type and offset.
8946 `reloc' is just a refence point to find a relocation at specified offset.
8947 If not found, return irelend.
8948
8949 Assuming relocations are sorted by r_offset,
8950 we find the relocation from `reloc' backward untill relocs,
8951 or find it from `reloc' forward untill irelend. */
8952
8953 static Elf_Internal_Rela *
8954 find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8955 Elf_Internal_Rela *relocs,
8956 Elf_Internal_Rela *irelend,
8957 enum elf_nds32_reloc_type reloc_type,
8958 bfd_vma offset_p)
8959 {
8960 Elf_Internal_Rela *rel_t = NULL;
8961
8962 /* First, we try to find a relocation of offset `offset_p',
8963 and then we use find_relocs_at_address to find specific type. */
8964
8965 if (reloc->r_offset > offset_p)
8966 {
8967 /* Find backward. */
8968 for (rel_t = reloc;
8969 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8970 /* Do nothing. */;
8971 }
8972 else if (reloc->r_offset < offset_p)
8973 {
8974 /* Find forward. */
8975 for (rel_t = reloc;
8976 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8977 /* Do nothing. */;
8978 }
8979 else
8980 rel_t = reloc;
8981
8982 /* Not found? */
8983 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8984 return irelend;
8985
8986 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8987 }
8988
8989 typedef struct nds32_elf_blank nds32_elf_blank_t;
8990 struct nds32_elf_blank
8991 {
8992 /* Where the blank begins. */
8993 bfd_vma offset;
8994 /* The size of the blank. */
8995 bfd_vma size;
8996 /* The accumulative size before this blank. */
8997 bfd_vma total_size;
8998 nds32_elf_blank_t *next;
8999 nds32_elf_blank_t *prev;
9000 };
9001
9002 static nds32_elf_blank_t *blank_free_list = NULL;
9003
9004 static nds32_elf_blank_t *
9005 create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9006 {
9007 nds32_elf_blank_t *blank_t;
9008
9009 if (blank_free_list)
9010 {
9011 blank_t = blank_free_list;
9012 blank_free_list = blank_free_list->next;
9013 }
9014 else
9015 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9016
9017 if (blank_t == NULL)
9018 return NULL;
9019
9020 blank_t->offset = offset_p;
9021 blank_t->size = size_p;
9022 blank_t->total_size = 0;
9023 blank_t->next = NULL;
9024 blank_t->prev = NULL;
9025
9026 return blank_t;
9027 }
9028
9029 static void
9030 remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9031 {
9032 if (blank_free_list)
9033 {
9034 blank_free_list->prev = blank_p;
9035 blank_p->next = blank_free_list;
9036 }
9037 else
9038 blank_p->next = NULL;
9039
9040 blank_p->prev = NULL;
9041 blank_free_list = blank_p;
9042 }
9043
9044 static void
9045 clean_nds32_elf_blank (void)
9046 {
9047 nds32_elf_blank_t *blank_t;
9048
9049 while (blank_free_list)
9050 {
9051 blank_t = blank_free_list;
9052 blank_free_list = blank_free_list->next;
9053 free (blank_t);
9054 }
9055 }
9056
9057 static nds32_elf_blank_t *
9058 search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9059 {
9060 nds32_elf_blank_t *blank_t;
9061
9062 if (!blank_p)
9063 return NULL;
9064 blank_t = blank_p;
9065
9066 while (blank_t && addr < blank_t->offset)
9067 blank_t = blank_t->prev;
9068 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9069 blank_t = blank_t->next;
9070
9071 return blank_t;
9072 }
9073
9074 static bfd_vma
9075 get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9076 int overwrite)
9077 {
9078 nds32_elf_blank_t *blank_t;
9079
9080 blank_t = search_nds32_elf_blank (*blank_p, addr);
9081 if (!blank_t)
9082 return 0;
9083
9084 if (overwrite)
9085 *blank_p = blank_t;
9086
9087 if (addr < blank_t->offset + blank_t->size)
9088 return blank_t->total_size + (addr - blank_t->offset);
9089 else
9090 return blank_t->total_size + blank_t->size;
9091 }
9092
9093 static bfd_boolean
9094 insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9095 {
9096 nds32_elf_blank_t *blank_t, *blank_t2;
9097
9098 if (!*blank_p)
9099 {
9100 *blank_p = create_nds32_elf_blank (addr, len);
9101 return *blank_p ? TRUE : FALSE;
9102 }
9103
9104 blank_t = search_nds32_elf_blank (*blank_p, addr);
9105
9106 if (blank_t == NULL)
9107 {
9108 blank_t = create_nds32_elf_blank (addr, len);
9109 if (!blank_t)
9110 return FALSE;
9111 while ((*blank_p)->prev != NULL)
9112 *blank_p = (*blank_p)->prev;
9113 blank_t->next = *blank_p;
9114 (*blank_p)->prev = blank_t;
9115 (*blank_p) = blank_t;
9116 return TRUE;
9117 }
9118
9119 if (addr < blank_t->offset + blank_t->size)
9120 {
9121 /* Extend the origin blank. */
9122 if (addr + len > blank_t->offset + blank_t->size)
9123 blank_t->size = addr + len - blank_t->offset;
9124 }
9125 else
9126 {
9127 blank_t2 = create_nds32_elf_blank (addr, len);
9128 if (!blank_t2)
9129 return FALSE;
9130 if (blank_t->next)
9131 {
9132 blank_t->next->prev = blank_t2;
9133 blank_t2->next = blank_t->next;
9134 }
9135 blank_t2->prev = blank_t;
9136 blank_t->next = blank_t2;
9137 *blank_p = blank_t2;
9138 }
9139
9140 return TRUE;
9141 }
9142
9143 static bfd_boolean
9144 insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9145 bfd_vma len)
9146 {
9147 nds32_elf_blank_t *blank_t;
9148
9149 if (!insert_nds32_elf_blank (blank_p, addr, len))
9150 return FALSE;
9151
9152 blank_t = *blank_p;
9153
9154 if (!blank_t->prev)
9155 {
9156 blank_t->total_size = 0;
9157 blank_t = blank_t->next;
9158 }
9159
9160 while (blank_t)
9161 {
9162 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9163 blank_t = blank_t->next;
9164 }
9165
9166 return TRUE;
9167 }
9168
9169 static void
9170 calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9171 {
9172 nds32_elf_blank_t *blank_t;
9173 bfd_vma total_size = 0;
9174
9175 if (!blank_p)
9176 return;
9177
9178 blank_t = blank_p;
9179 while (blank_t->prev)
9180 blank_t = blank_t->prev;
9181 while (blank_t)
9182 {
9183 blank_t->total_size = total_size;
9184 total_size += blank_t->size;
9185 blank_t = blank_t->next;
9186 }
9187 }
9188
9189 static bfd_boolean
9190 nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9191 nds32_elf_blank_t *blank_p)
9192 {
9193 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
9194 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
9195 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9196 unsigned int sec_shndx; /* The section the be relaxed. */
9197 bfd_byte *contents; /* Contents data of iterating section. */
9198 Elf_Internal_Rela *internal_relocs;
9199 Elf_Internal_Rela *irel;
9200 Elf_Internal_Rela *irelend;
9201 struct elf_link_hash_entry **sym_hashes;
9202 struct elf_link_hash_entry **end_hashes;
9203 unsigned int symcount;
9204 asection *sect;
9205 nds32_elf_blank_t *blank_t;
9206 nds32_elf_blank_t *blank_t2;
9207 nds32_elf_blank_t *blank_head;
9208
9209 blank_head = blank_t = blank_p;
9210 while (blank_head->prev != NULL)
9211 blank_head = blank_head->prev;
9212 while (blank_t->next != NULL)
9213 blank_t = blank_t->next;
9214
9215 if (blank_t->offset + blank_t->size <= sec->size)
9216 {
9217 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9218 blank_t->next->prev = blank_t;
9219 }
9220 if (blank_head->offset > 0)
9221 {
9222 blank_head->prev = create_nds32_elf_blank (0, 0);
9223 blank_head->prev->next = blank_head;
9224 blank_head = blank_head->prev;
9225 }
9226
9227 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9228
9229 /* The deletion must stop at the next ALIGN reloc for an alignment
9230 power larger than the number of bytes we are deleting. */
9231
9232 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9233 if (!nds32_get_local_syms (abfd, sec, &isym))
9234 return FALSE;
9235
9236 if (isym == NULL)
9237 {
9238 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9239 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9240 symtab_hdr->contents = (bfd_byte *) isym;
9241 }
9242
9243 if (isym == NULL || symtab_hdr->sh_info == 0)
9244 return FALSE;
9245
9246 blank_t = blank_head;
9247 calc_nds32_blank_total (blank_head);
9248
9249 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9250 {
9251 /* Adjust all the relocs. */
9252
9253 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9254 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9255 TRUE /* keep_memory */);
9256 irelend = internal_relocs + sect->reloc_count;
9257
9258 blank_t = blank_head;
9259 blank_t2 = blank_head;
9260
9261 if (!(sect->flags & SEC_RELOC))
9262 continue;
9263
9264 nds32_get_section_contents (abfd, sect, &contents, TRUE);
9265
9266 for (irel = internal_relocs; irel < irelend; irel++)
9267 {
9268 bfd_vma raddr;
9269
9270 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9271 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9272 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9273 {
9274 unsigned long val = 0;
9275 unsigned long mask;
9276 long before, between;
9277 long offset = 0;
9278
9279 switch (ELF32_R_TYPE (irel->r_info))
9280 {
9281 case R_NDS32_DIFF8:
9282 offset = bfd_get_8 (abfd, contents + irel->r_offset);
9283 break;
9284 case R_NDS32_DIFF16:
9285 offset = bfd_get_16 (abfd, contents + irel->r_offset);
9286 break;
9287 case R_NDS32_DIFF32:
9288 val = bfd_get_32 (abfd, contents + irel->r_offset);
9289 /* Get the signed bit and mask for the high part. The
9290 gcc will alarm when right shift 32-bit since the
9291 type size of long may be 32-bit. */
9292 mask = 0 - (val >> 31);
9293 if (mask)
9294 offset = (val | (mask - 0xffffffff));
9295 else
9296 offset = val;
9297 break;
9298 default:
9299 BFD_ASSERT (0);
9300 }
9301
9302 /* DIFF value
9303 0 |encoded in location|
9304 |------------|-------------------|---------
9305 sym+off(addend)
9306 -- before ---| *****************
9307 --------------------- between ---|
9308
9309 We only care how much data are relax between DIFF,
9310 marked as ***. */
9311
9312 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9313 between = get_nds32_elf_blank_total (&blank_t,
9314 irel->r_addend + offset, 0);
9315 if (between == before)
9316 goto done_adjust_diff;
9317
9318 switch (ELF32_R_TYPE (irel->r_info))
9319 {
9320 case R_NDS32_DIFF8:
9321 bfd_put_8 (abfd, offset - (between - before),
9322 contents + irel->r_offset);
9323 break;
9324 case R_NDS32_DIFF16:
9325 bfd_put_16 (abfd, offset - (between - before),
9326 contents + irel->r_offset);
9327 break;
9328 case R_NDS32_DIFF32:
9329 bfd_put_32 (abfd, offset - (between - before),
9330 contents + irel->r_offset);
9331 break;
9332 }
9333 }
9334 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9335 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9336 {
9337 bfd_vma val = 0;
9338 unsigned int len = 0;
9339 unsigned long before, between;
9340 bfd_byte *endp, *p;
9341
9342 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9343 &len);
9344
9345 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9346 between = get_nds32_elf_blank_total (&blank_t,
9347 irel->r_addend + val, 0);
9348 if (between == before)
9349 goto done_adjust_diff;
9350
9351 p = contents + irel->r_offset;
9352 endp = p + len -1;
9353 memset (p, 0x80, len);
9354 *(endp) = 0;
9355 p = write_uleb128 (p, val - (between - before)) - 1;
9356 if (p < endp)
9357 *p |= 0x80;
9358 }
9359 done_adjust_diff:
9360
9361 if (sec == sect)
9362 {
9363 raddr = irel->r_offset;
9364 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9365 irel->r_offset, 1);
9366
9367 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9368 continue;
9369 if (blank_t2 && blank_t2->next
9370 && (blank_t2->offset > raddr
9371 || blank_t2->next->offset <= raddr))
9372 _bfd_error_handler
9373 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9374 abfd);
9375
9376 /* Mark reloc in deleted portion as NONE.
9377 For some relocs like R_NDS32_LABEL that doesn't modify the
9378 content in the section. R_NDS32_LABEL doesn't belong to the
9379 instruction in the section, so we should preserve it. */
9380 if (raddr >= blank_t2->offset
9381 && raddr < blank_t2->offset + blank_t2->size
9382 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9383 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9384 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9385 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9386 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9387 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9388 {
9389 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9390 R_NDS32_NONE);
9391 continue;
9392 }
9393 }
9394
9395 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9396 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9397 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9398 continue;
9399
9400 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9401 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9402 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9403 {
9404 if (irel->r_addend <= sec->size)
9405 irel->r_addend -=
9406 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9407 }
9408 }
9409 }
9410
9411 /* Adjust the local symbols defined in this section. */
9412 blank_t = blank_head;
9413 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9414 {
9415 if (isym->st_shndx == sec_shndx)
9416 {
9417 if (isym->st_value <= sec->size)
9418 {
9419 bfd_vma ahead;
9420 bfd_vma orig_addr = isym->st_value;
9421
9422 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9423 isym->st_value -= ahead;
9424
9425 /* Adjust function size. */
9426 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9427 && isym->st_size > 0)
9428 isym->st_size -=
9429 get_nds32_elf_blank_total
9430 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9431 }
9432 }
9433 }
9434
9435 /* Now adjust the global symbols defined in this section. */
9436 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9437 - symtab_hdr->sh_info);
9438 sym_hashes = elf_sym_hashes (abfd);
9439 end_hashes = sym_hashes + symcount;
9440 blank_t = blank_head;
9441 for (; sym_hashes < end_hashes; sym_hashes++)
9442 {
9443 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9444
9445 if ((sym_hash->root.type == bfd_link_hash_defined
9446 || sym_hash->root.type == bfd_link_hash_defweak)
9447 && sym_hash->root.u.def.section == sec)
9448 {
9449 if (sym_hash->root.u.def.value <= sec->size)
9450 {
9451 bfd_vma ahead;
9452 bfd_vma orig_addr = sym_hash->root.u.def.value;
9453
9454 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9455 sym_hash->root.u.def.value -= ahead;
9456
9457 /* Adjust function size. */
9458 if (sym_hash->type == STT_FUNC)
9459 sym_hash->size -=
9460 get_nds32_elf_blank_total
9461 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9462
9463 }
9464 }
9465 }
9466
9467 contents = elf_section_data (sec)->this_hdr.contents;
9468 blank_t = blank_head;
9469 while (blank_t->next)
9470 {
9471 /* Actually delete the bytes. */
9472
9473 /* If current blank is the last blank overlap with current section,
9474 go to finish process. */
9475 if (sec->size <= (blank_t->next->offset))
9476 break;
9477
9478 memmove (contents + blank_t->offset - blank_t->total_size,
9479 contents + blank_t->offset + blank_t->size,
9480 blank_t->next->offset - (blank_t->offset + blank_t->size));
9481
9482 blank_t = blank_t->next;
9483 }
9484
9485 if (sec->size > (blank_t->offset + blank_t->size))
9486 {
9487 /* There are remaining code between blank and section boundary.
9488 Move the remaining code to appropriate location. */
9489 memmove (contents + blank_t->offset - blank_t->total_size,
9490 contents + blank_t->offset + blank_t->size,
9491 sec->size - (blank_t->offset + blank_t->size));
9492 sec->size -= blank_t->total_size + blank_t->size;
9493 }
9494 else
9495 /* This blank is not entirely included in the section,
9496 reduce the section size by only part of the blank size. */
9497 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9498
9499 while (blank_head)
9500 {
9501 blank_t = blank_head;
9502 blank_head = blank_head->next;
9503 remove_nds32_elf_blank (blank_t);
9504 }
9505
9506 return TRUE;
9507 }
9508
9509 /* Get the contents of a section. */
9510
9511 static int
9512 nds32_get_section_contents (bfd *abfd, asection *sec,
9513 bfd_byte **contents_p, bfd_boolean cache)
9514 {
9515 /* Get the section contents. */
9516 if (elf_section_data (sec)->this_hdr.contents != NULL)
9517 *contents_p = elf_section_data (sec)->this_hdr.contents;
9518 else
9519 {
9520 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9521 return FALSE;
9522 if (cache)
9523 elf_section_data (sec)->this_hdr.contents = *contents_p;
9524 }
9525
9526 return TRUE;
9527 }
9528
9529 /* Get the contents of the internal symbol of abfd. */
9530
9531 static int
9532 nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9533 Elf_Internal_Sym **isymbuf_p)
9534 {
9535 Elf_Internal_Shdr *symtab_hdr;
9536 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9537
9538 /* Read this BFD's local symbols if we haven't done so already. */
9539 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9540 {
9541 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9542 if (*isymbuf_p == NULL)
9543 {
9544 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9545 symtab_hdr->sh_info, 0,
9546 NULL, NULL, NULL);
9547 if (*isymbuf_p == NULL)
9548 return FALSE;
9549 }
9550 }
9551 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9552
9553 return TRUE;
9554 }
9555
9556 /* Range of small data. */
9557 static bfd_vma sdata_range[2][2];
9558 static bfd_vma const sdata_init_range[2] =
9559 { ACCURATE_12BIT_S1, ACCURATE_19BIT };
9560
9561 static int
9562 nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9563 bfd_byte *contents, bfd_vma addr)
9564 {
9565 unsigned long insn = bfd_getb32 (contents + addr);
9566
9567 if (insn & 0x80000000)
9568 return 2;
9569
9570 return 4;
9571 }
9572
9573 /* Set the gp relax range. We have to measure the safe range
9574 to do gp relaxation. */
9575
9576 static void
9577 relax_range_measurement (bfd *abfd)
9578 {
9579 asection *sec_f, *sec_b;
9580 /* For upper bound. */
9581 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9582 bfd_vma align;
9583 static int decide_relax_range = 0;
9584 int i;
9585 int range_number = ARRAY_SIZE (sdata_init_range);
9586
9587 if (decide_relax_range)
9588 return;
9589 decide_relax_range = 1;
9590
9591 if (sda_rela_sec == NULL)
9592 {
9593 /* Since there is no data sections, we assume the range is page size. */
9594 for (i = 0; i < range_number; i++)
9595 {
9596 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9597 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9598 }
9599 return;
9600 }
9601
9602 /* Get the biggest alignment power after the gp located section. */
9603 sec_f = sda_rela_sec->output_section;
9604 sec_b = sec_f->next;
9605 align = 0;
9606 while (sec_b != NULL)
9607 {
9608 if ((unsigned)(1 << sec_b->alignment_power) > align)
9609 align = (1 << sec_b->alignment_power);
9610 sec_b = sec_b->next;
9611 }
9612
9613 /* I guess we can not determine the section before
9614 gp located section, so we assume the align is max page size. */
9615 for (i = 0; i < range_number; i++)
9616 {
9617 sdata_range[i][1] = sdata_init_range[i] - align;
9618 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9619 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9620 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9621 }
9622 }
9623
9624 /* These are macros used to check flags encoded in r_addend.
9625 They are only used by nds32_elf_relax_section (). */
9626 #define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9627 #define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9628 #define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9629 #define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9630
9631 static const char * unrecognized_reloc_msg =
9632 /* xgettext:c-format */
9633 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9634
9635 /* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9636
9637 static bfd_boolean
9638 nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9639 Elf_Internal_Rela *internal_relocs, int *insn_len,
9640 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9641 Elf_Internal_Shdr *symtab_hdr)
9642 {
9643 /* There are 3 variations for LONGCALL1
9644 case 4-4-2; 16-bit on, optimize off or optimize for space
9645 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9646 ori ta, ta, lo12(symbol) ; LO12S0
9647 jral5 ta ;
9648
9649 case 4-4-4; 16-bit off, optimize don't care
9650 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9651 ori ta, ta, lo12(symbol) ; LO12S0
9652 jral ta ;
9653
9654 case 4-4-4; 16-bit on, optimize for speed
9655 sethi ta, hi20(symbol) ; LONGCALL1/HI20
9656 ori ta, ta, lo12(symbol) ; LO12S0
9657 jral ta ;
9658 Check code for -mlong-calls output. */
9659
9660 /* Get the reloc for the address from which the register is
9661 being loaded. This reloc will tell us which function is
9662 actually being called. */
9663
9664 bfd_vma laddr;
9665 int seq_len; /* Original length of instruction sequence. */
9666 uint32_t insn;
9667 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9668 bfd_signed_vma foff;
9669 uint16_t insn16;
9670
9671 irelend = internal_relocs + sec->reloc_count;
9672 seq_len = GET_SEQ_LEN (irel->r_addend);
9673 laddr = irel->r_offset;
9674 *insn_len = seq_len;
9675
9676 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9677 R_NDS32_HI20_RELA, laddr);
9678 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9679 R_NDS32_LO12S0_ORI_RELA,
9680 laddr + 4);
9681
9682 if (hi_irelfn == irelend || lo_irelfn == irelend)
9683 {
9684 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9685 (uint64_t) irel->r_offset);
9686 return FALSE;
9687 }
9688
9689 /* Get the value of the symbol referred to by the reloc. */
9690 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9691
9692 /* This condition only happened when symbol is undefined. */
9693 if (foff == 0
9694 || foff < -CONSERVATIVE_24BIT_S1
9695 || foff >= CONSERVATIVE_24BIT_S1)
9696 return FALSE;
9697
9698 /* Relax to: jal symbol; 25_PCREL. */
9699 /* For simplicity of coding, we are going to modify the section
9700 contents, the section relocs, and the BFD symbol table. We
9701 must tell the rest of the code not to free up this
9702 information. It would be possible to instead create a table
9703 of changes which have to be made, as is done in coff-mips.c;
9704 that would be more work, but would require less memory when
9705 the linker is run. */
9706
9707 /* Replace the long call with a jal. */
9708 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9709 R_NDS32_25_PCREL_RELA);
9710 irel->r_addend = hi_irelfn->r_addend;
9711
9712 /* We don't resolve this here but resolve it in relocate_section. */
9713 insn = INSN_JAL;
9714 bfd_putb32 (insn, contents + irel->r_offset);
9715
9716 hi_irelfn->r_info =
9717 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9718 lo_irelfn->r_info =
9719 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9720 *insn_len = 4;
9721
9722 if (seq_len & 0x2)
9723 {
9724 insn16 = NDS32_NOP16;
9725 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9726 lo_irelfn->r_info =
9727 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9728 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9729 *insn_len += 2;
9730 }
9731 return TRUE;
9732 }
9733
9734 #define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9735 /* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
9736
9737 static bfd_boolean
9738 nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9739 Elf_Internal_Rela *internal_relocs, int *insn_len,
9740 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9741 Elf_Internal_Shdr *symtab_hdr)
9742 {
9743 /* bltz rt, .L1 ; LONGCALL2
9744 jal symbol ; 25_PCREL
9745 .L1: */
9746
9747 /* Get the reloc for the address from which the register is
9748 being loaded. This reloc will tell us which function is
9749 actually being called. */
9750
9751 bfd_vma laddr;
9752 uint32_t insn;
9753 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9754 bfd_signed_vma foff;
9755
9756 irelend = internal_relocs + sec->reloc_count;
9757 laddr = irel->r_offset;
9758 i1_irelfn =
9759 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9760 R_NDS32_25_PCREL_RELA, laddr + 4);
9761
9762 if (i1_irelfn == irelend)
9763 {
9764 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9765 (uint64_t) irel->r_offset);
9766 return FALSE;
9767 }
9768
9769 insn = bfd_getb32 (contents + laddr);
9770
9771 /* Get the value of the symbol referred to by the reloc. */
9772 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9773
9774 if (foff == 0
9775 || foff < -CONSERVATIVE_16BIT_S1
9776 || foff >= CONSERVATIVE_16BIT_S1)
9777 return FALSE;
9778
9779 /* Relax to bgezal rt, label ; 17_PCREL
9780 or bltzal rt, label ; 17_PCREL */
9781
9782 /* Convert to complimentary conditional call. */
9783 insn = CONVERT_CONDITION_CALL (insn);
9784
9785 /* For simplicity of coding, we are going to modify the section
9786 contents, the section relocs, and the BFD symbol table. We
9787 must tell the rest of the code not to free up this
9788 information. It would be possible to instead create a table
9789 of changes which have to be made, as is done in coff-mips.c;
9790 that would be more work, but would require less memory when
9791 the linker is run. */
9792
9793 /* Clean unnessary relocations. */
9794 i1_irelfn->r_info =
9795 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9796 cond_irelfn =
9797 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9798 R_NDS32_17_PCREL_RELA, laddr);
9799 if (cond_irelfn != irelend)
9800 cond_irelfn->r_info =
9801 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9802
9803 /* Replace the long call with a bgezal. */
9804 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9805 R_NDS32_17_PCREL_RELA);
9806 irel->r_addend = i1_irelfn->r_addend;
9807
9808 bfd_putb32 (insn, contents + irel->r_offset);
9809
9810 *insn_len = 4;
9811 return TRUE;
9812 }
9813
9814 /* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9815
9816 static bfd_boolean
9817 nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9818 Elf_Internal_Rela *internal_relocs, int *insn_len,
9819 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9820 Elf_Internal_Shdr *symtab_hdr)
9821 {
9822 /* There are 3 variations for LONGCALL3
9823 case 4-4-4-2; 16-bit on, optimize off or optimize for space
9824 bltz rt, $1 ; LONGCALL3
9825 sethi ta, hi20(symbol) ; HI20
9826 ori ta, ta, lo12(symbol) ; LO12S0
9827 jral5 ta ;
9828 $1
9829
9830 case 4-4-4-4; 16-bit off, optimize don't care
9831 bltz rt, $1 ; LONGCALL3
9832 sethi ta, hi20(symbol) ; HI20
9833 ori ta, ta, lo12(symbol) ; LO12S0
9834 jral ta ;
9835 $1
9836
9837 case 4-4-4-4; 16-bit on, optimize for speed
9838 bltz rt, $1 ; LONGCALL3
9839 sethi ta, hi20(symbol) ; HI20
9840 ori ta, ta, lo12(symbol) ; LO12S0
9841 jral ta ;
9842 $1 */
9843
9844 /* Get the reloc for the address from which the register is
9845 being loaded. This reloc will tell us which function is
9846 actually being called. */
9847
9848 bfd_vma laddr;
9849 int seq_len; /* Original length of instruction sequence. */
9850 uint32_t insn;
9851 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9852 bfd_signed_vma foff;
9853 uint16_t insn16;
9854
9855 irelend = internal_relocs + sec->reloc_count;
9856 seq_len = GET_SEQ_LEN (irel->r_addend);
9857 laddr = irel->r_offset;
9858 *insn_len = seq_len;
9859
9860 hi_irelfn =
9861 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9862 R_NDS32_HI20_RELA, laddr + 4);
9863 lo_irelfn =
9864 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9865 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9866
9867 if (hi_irelfn == irelend || lo_irelfn == irelend)
9868 {
9869 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9870 (uint64_t) irel->r_offset);
9871 return FALSE;
9872 }
9873
9874 /* Get the value of the symbol referred to by the reloc. */
9875 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9876
9877 if (foff == 0
9878 || foff < -CONSERVATIVE_24BIT_S1
9879 || foff >= CONSERVATIVE_24BIT_S1)
9880 return FALSE;
9881
9882 insn = bfd_getb32 (contents + laddr);
9883 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9884 {
9885 /* Relax to bgezal rt, label ; 17_PCREL
9886 or bltzal rt, label ; 17_PCREL */
9887
9888 /* Convert to complimentary conditional call. */
9889 insn = CONVERT_CONDITION_CALL (insn);
9890 bfd_putb32 (insn, contents + irel->r_offset);
9891
9892 *insn_len = 4;
9893 irel->r_info =
9894 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9895 hi_irelfn->r_info =
9896 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9897 lo_irelfn->r_info =
9898 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9899
9900 cond_irelfn =
9901 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9902 R_NDS32_17_PCREL_RELA, laddr);
9903 if (cond_irelfn != irelend)
9904 {
9905 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9906 R_NDS32_17_PCREL_RELA);
9907 cond_irelfn->r_addend = hi_irelfn->r_addend;
9908 }
9909
9910 if (seq_len & 0x2)
9911 {
9912 insn16 = NDS32_NOP16;
9913 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9914 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9915 R_NDS32_INSN16);
9916 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9917 insn_len += 2;
9918 }
9919 }
9920 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9921 {
9922 /* Relax to the following instruction sequence
9923 bltz rt, $1 ; LONGCALL2
9924 jal symbol ; 25_PCREL
9925 $1 */
9926 *insn_len = 8;
9927 insn = INSN_JAL;
9928 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9929
9930 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9931 R_NDS32_25_PCREL_RELA);
9932 irel->r_info =
9933 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9934
9935 lo_irelfn->r_info =
9936 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9937
9938 if (seq_len & 0x2)
9939 {
9940 insn16 = NDS32_NOP16;
9941 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9942 lo_irelfn->r_info =
9943 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9944 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9945 insn_len += 2;
9946 }
9947 }
9948 return TRUE;
9949 }
9950
9951 /* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
9952
9953 static bfd_boolean
9954 nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9955 Elf_Internal_Rela *internal_relocs, int *insn_len,
9956 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9957 Elf_Internal_Shdr *symtab_hdr)
9958 {
9959 /* There are 3 variations for LONGJUMP1
9960 case 4-4-2; 16-bit bit on, optimize off or optimize for space
9961 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9962 ori ta, ta, lo12(symbol) ; LO12S0
9963 jr5 ta ;
9964
9965 case 4-4-4; 16-bit off, optimize don't care
9966 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9967 ori ta, ta, lo12(symbol) ; LO12S0
9968 jr ta ;
9969
9970 case 4-4-4; 16-bit on, optimize for speed
9971 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9972 ori ta, ta, lo12(symbol) ; LO12S0
9973 jr ta ; */
9974
9975 /* Get the reloc for the address from which the register is
9976 being loaded. This reloc will tell us which function is
9977 actually being called. */
9978
9979 bfd_vma laddr;
9980 int seq_len; /* Original length of instruction sequence. */
9981 int insn16_on; /* 16-bit on/off. */
9982 uint32_t insn;
9983 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9984 bfd_signed_vma foff;
9985 uint16_t insn16;
9986 unsigned long reloc;
9987
9988 irelend = internal_relocs + sec->reloc_count;
9989 seq_len = GET_SEQ_LEN (irel->r_addend);
9990 laddr = irel->r_offset;
9991 *insn_len = seq_len;
9992 insn16_on = IS_16BIT_ON (irel->r_addend);
9993
9994 hi_irelfn =
9995 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9996 R_NDS32_HI20_RELA, laddr);
9997 lo_irelfn =
9998 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9999 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10000 if (hi_irelfn == irelend || lo_irelfn == irelend)
10001 {
10002 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10003 (uint64_t) irel->r_offset);
10004 return FALSE;
10005 }
10006
10007 /* Get the value of the symbol referred to by the reloc. */
10008 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10009
10010 if (foff == 0
10011 || foff >= CONSERVATIVE_24BIT_S1
10012 || foff < -CONSERVATIVE_24BIT_S1)
10013 return FALSE;
10014
10015 if (insn16_on
10016 && foff >= -ACCURATE_8BIT_S1
10017 && foff < ACCURATE_8BIT_S1
10018 && (seq_len & 0x2))
10019 {
10020 /* j8 label */
10021 /* 16-bit on, but not optimized for speed. */
10022 reloc = R_NDS32_9_PCREL_RELA;
10023 insn16 = INSN_J8;
10024 bfd_putb16 (insn16, contents + irel->r_offset);
10025 *insn_len = 2;
10026 irel->r_info =
10027 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10028 }
10029 else
10030 {
10031 /* j label */
10032 reloc = R_NDS32_25_PCREL_RELA;
10033 insn = INSN_J;
10034 bfd_putb32 (insn, contents + irel->r_offset);
10035 *insn_len = 4;
10036 irel->r_info =
10037 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10038 irel->r_addend = 0;
10039 }
10040
10041 hi_irelfn->r_info =
10042 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10043 lo_irelfn->r_info =
10044 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10045
10046 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10047 {
10048 insn16 = NDS32_NOP16;
10049 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10050 lo_irelfn->r_info =
10051 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10052 R_NDS32_INSN16);
10053 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10054 *insn_len += 2;
10055 }
10056 return TRUE;
10057 }
10058
10059 /* Revert condition branch. This function does not check if the input
10060 instruction is condition branch or not. */
10061
10062 static void
10063 nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10064 uint16_t *re_insn16, uint32_t *re_insn)
10065 {
10066 uint32_t comp_insn = 0;
10067 uint16_t comp_insn16 = 0;
10068
10069 if (insn)
10070 {
10071 if (N32_OP6 (insn) == N32_OP6_BR1)
10072 {
10073 /* beqs label. */
10074 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10075 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10076 {
10077 /* Insn can be contracted to 16-bit implied r5. */
10078 comp_insn16 =
10079 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10080 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10081 }
10082 }
10083 else if (N32_OP6 (insn) == N32_OP6_BR3)
10084 {
10085 /* bnec $ta, imm11, label. */
10086 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10087 }
10088 else
10089 {
10090 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10091 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10092 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10093 {
10094 if (N32_IS_RT3 (insn))
10095 {
10096 /* Insn can be contracted to 16-bit. */
10097 comp_insn16 =
10098 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10099 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10100 }
10101 else if (N32_RT5 (insn) == REG_R15)
10102 {
10103 /* Insn can be contracted to 16-bit. */
10104 comp_insn16 =
10105 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10106 }
10107 }
10108 }
10109 }
10110 else
10111 {
10112 switch ((insn16 & 0xf000) >> 12)
10113 {
10114 case 0xc:
10115 /* beqz38 or bnez38 */
10116 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10117 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10118 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10119 break;
10120
10121 case 0xd:
10122 /* beqs38 or bnes38 */
10123 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10124 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10125 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10126 | (REG_R5 << 15);
10127 break;
10128
10129 case 0xe:
10130 /* beqzS8 or bnezS8 */
10131 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10132 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10133 comp_insn |= REG_R15 << 20;
10134 break;
10135
10136 default:
10137 break;
10138 }
10139 }
10140 if (comp_insn && re_insn)
10141 *re_insn = comp_insn;
10142 if (comp_insn16 && re_insn16)
10143 *re_insn16 = comp_insn16;
10144 }
10145
10146 /* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
10147
10148 static bfd_boolean
10149 nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10150 Elf_Internal_Rela *internal_relocs, int *insn_len,
10151 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10152 Elf_Internal_Shdr *symtab_hdr)
10153 {
10154 /* There are 3 variations for LONGJUMP2
10155 case 2-4; 1st insn convertible, 16-bit on,
10156 optimize off or optimize for space
10157 bnes38 rt, ra, $1 ; LONGJUMP2
10158 j label ; 25_PCREL
10159 $1:
10160
10161 case 4-4; 1st insn not convertible
10162 bne rt, ra, $1 ; LONGJUMP2
10163 j label ; 25_PCREL
10164 $1:
10165
10166 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10167 bne rt, ra, $1 ; LONGJUMP2
10168 j label ; 25_PCREL
10169 $1: */
10170
10171 /* Get the reloc for the address from which the register is
10172 being loaded. This reloc will tell us which function is
10173 actually being called. */
10174
10175 bfd_vma laddr;
10176 int seq_len; /* Original length of instruction sequence. */
10177 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10178 int first_size;
10179 unsigned int i;
10180 bfd_signed_vma foff;
10181 uint32_t insn, re_insn = 0;
10182 uint16_t insn16, re_insn16 = 0;
10183 unsigned long reloc, cond_reloc;
10184
10185 enum elf_nds32_reloc_type checked_types[] =
10186 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10187
10188 irelend = internal_relocs + sec->reloc_count;
10189 seq_len = GET_SEQ_LEN (irel->r_addend);
10190 laddr = irel->r_offset;
10191 *insn_len = seq_len;
10192 first_size = (seq_len == 6) ? 2 : 4;
10193
10194 i2_irelfn =
10195 find_relocs_at_address_addr (irel, internal_relocs,
10196 irelend, R_NDS32_25_PCREL_RELA,
10197 laddr + first_size);
10198
10199 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10200 {
10201 cond_irelfn =
10202 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10203 checked_types[i], laddr);
10204 if (cond_irelfn != irelend)
10205 break;
10206 }
10207
10208 if (i2_irelfn == irelend || cond_irelfn == irelend)
10209 {
10210 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10211 (uint64_t) irel->r_offset);
10212 return FALSE;
10213 }
10214
10215 /* Get the value of the symbol referred to by the reloc. */
10216 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10217 if (foff == 0
10218 || foff < -CONSERVATIVE_16BIT_S1
10219 || foff >= CONSERVATIVE_16BIT_S1)
10220 return FALSE;
10221
10222 /* Get the all corresponding instructions. */
10223 if (first_size == 4)
10224 {
10225 insn = bfd_getb32 (contents + laddr);
10226 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10227 }
10228 else
10229 {
10230 insn16 = bfd_getb16 (contents + laddr);
10231 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10232 }
10233
10234 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10235 && foff < ACCURATE_8BIT_S1 - first_size)
10236 {
10237 if (first_size == 4)
10238 {
10239 /* Don't convert it to 16-bit now, keep this as relaxable for
10240 ``label reloc; INSN16''. */
10241
10242 /* Save comp_insn32 to buffer. */
10243 bfd_putb32 (re_insn, contents + irel->r_offset);
10244 *insn_len = 4;
10245 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10246 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10247 cond_reloc = R_NDS32_INSN16;
10248 }
10249 else
10250 {
10251 bfd_putb16 (re_insn16, contents + irel->r_offset);
10252 *insn_len = 2;
10253 reloc = R_NDS32_9_PCREL_RELA;
10254 cond_reloc = R_NDS32_NONE;
10255 }
10256 }
10257 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10258 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10259 && foff < ACCURATE_14BIT_S1 - first_size))
10260 {
10261 /* beqs label ; 15_PCREL */
10262 bfd_putb32 (re_insn, contents + irel->r_offset);
10263 *insn_len = 4;
10264 reloc = R_NDS32_15_PCREL_RELA;
10265 cond_reloc = R_NDS32_NONE;
10266 }
10267 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10268 && foff >= -CONSERVATIVE_16BIT_S1
10269 && foff < CONSERVATIVE_16BIT_S1)
10270 {
10271 /* beqz label ; 17_PCREL */
10272 bfd_putb32 (re_insn, contents + irel->r_offset);
10273 *insn_len = 4;
10274 reloc = R_NDS32_17_PCREL_RELA;
10275 cond_reloc = R_NDS32_NONE;
10276 }
10277 else
10278 return FALSE;
10279
10280 /* Set all relocations. */
10281 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10282 irel->r_addend = i2_irelfn->r_addend;
10283
10284 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10285 cond_reloc);
10286 cond_irelfn->r_addend = 0;
10287
10288 if ((seq_len ^ *insn_len ) & 0x2)
10289 {
10290 insn16 = NDS32_NOP16;
10291 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10292 i2_irelfn->r_offset = 4;
10293 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10294 R_NDS32_INSN16);
10295 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10296 *insn_len += 2;
10297 }
10298 else
10299 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10300 R_NDS32_NONE);
10301 return TRUE;
10302 }
10303
10304 /* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
10305
10306 static bfd_boolean
10307 nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10308 Elf_Internal_Rela *internal_relocs, int *insn_len,
10309 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10310 Elf_Internal_Shdr *symtab_hdr)
10311 {
10312 /* There are 5 variations for LONGJUMP3
10313 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10314 optimize off or optimize for space
10315 bnes38 rt, ra, $1 ; LONGJUMP3
10316 sethi ta, hi20(symbol) ; HI20
10317 ori ta, ta, lo12(symbol) ; LO12S0
10318 jr5 ta ;
10319 $1: ;
10320
10321 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10322 bnes38 rt, ra, $1 ; LONGJUMP3
10323 sethi ta, hi20(symbol) ; HI20
10324 ori ta, ta, lo12(symbol) ; LO12S0
10325 jr5 ta ;
10326 $1: ; LABEL
10327
10328 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10329 optimize off or optimize for space
10330 bne rt, ra, $1 ; LONGJUMP3
10331 sethi ta, hi20(symbol) ; HI20
10332 ori ta, ta, lo12(symbol) ; LO12S0
10333 jr5 ta ;
10334 $1: ;
10335
10336 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10337 16-bit off if no INSN16
10338 bne rt, ra, $1 ; LONGJUMP3
10339 sethi ta, hi20(symbol) ; HI20
10340 ori ta, ta, lo12(symbol) ; LO12S0
10341 jr ta ;
10342 $1: ;
10343
10344 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10345 16-bit off if no INSN16
10346 bne rt, ra, $1 ; LONGJUMP3
10347 sethi ta, hi20(symbol) ; HI20
10348 ori ta, ta, lo12(symbol) ; LO12S0
10349 jr ta ;
10350 $1: ; LABEL */
10351
10352 /* Get the reloc for the address from which the register is
10353 being loaded. This reloc will tell us which function is
10354 actually being called. */
10355 enum elf_nds32_reloc_type checked_types[] =
10356 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10357
10358 int reloc_off = 0, cond_removed = 0, convertible;
10359 bfd_vma laddr;
10360 int seq_len; /* Original length of instruction sequence. */
10361 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10362 int first_size;
10363 unsigned int i;
10364 bfd_signed_vma foff;
10365 uint32_t insn, re_insn = 0;
10366 uint16_t insn16, re_insn16 = 0;
10367 unsigned long reloc, cond_reloc;
10368
10369 irelend = internal_relocs + sec->reloc_count;
10370 seq_len = GET_SEQ_LEN (irel->r_addend);
10371 laddr = irel->r_offset;
10372 *insn_len = seq_len;
10373
10374 convertible = IS_1ST_CONVERT (irel->r_addend);
10375
10376 if (convertible)
10377 first_size = 2;
10378 else
10379 first_size = 4;
10380
10381 /* Get all needed relocations. */
10382 hi_irelfn =
10383 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10384 R_NDS32_HI20_RELA, laddr + first_size);
10385 lo_irelfn =
10386 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10387 R_NDS32_LO12S0_ORI_RELA,
10388 laddr + first_size + 4);
10389
10390 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10391 {
10392 cond_irelfn =
10393 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10394 checked_types[i], laddr);
10395 if (cond_irelfn != irelend)
10396 break;
10397 }
10398
10399 if (hi_irelfn == irelend
10400 || lo_irelfn == irelend
10401 || cond_irelfn == irelend)
10402 {
10403 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10404 (uint64_t) irel->r_offset);
10405 return FALSE;
10406 }
10407
10408 /* Get the value of the symbol referred to by the reloc. */
10409 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10410
10411 if (foff == 0
10412 || foff < -CONSERVATIVE_24BIT_S1
10413 || foff >= CONSERVATIVE_24BIT_S1)
10414 return FALSE;
10415
10416 /* Get the all corresponding instructions. */
10417 if (first_size == 4)
10418 {
10419 insn = bfd_getb32 (contents + laddr);
10420 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10421 }
10422 else
10423 {
10424 insn16 = bfd_getb16 (contents + laddr);
10425 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10426 }
10427
10428 /* For simplicity of coding, we are going to modify the section
10429 contents, the section relocs, and the BFD symbol table. We
10430 must tell the rest of the code not to free up this
10431 information. It would be possible to instead create a table
10432 of changes which have to be made, as is done in coff-mips.c;
10433 that would be more work, but would require less memory when
10434 the linker is run. */
10435
10436 if (re_insn16
10437 && foff >= -ACCURATE_8BIT_S1 - first_size
10438 && foff < ACCURATE_8BIT_S1 - first_size)
10439 {
10440 if (!(seq_len & 0x2))
10441 {
10442 /* Don't convert it to 16-bit now, keep this as relaxable
10443 for ``label reloc; INSN1a''6. */
10444 /* Save comp_insn32 to buffer. */
10445 bfd_putb32 (re_insn, contents + irel->r_offset);
10446 *insn_len = 4;
10447 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10448 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10449 cond_reloc = R_NDS32_INSN16;
10450 }
10451 else
10452 {
10453 /* Not optimize for speed; convert sequence to 16-bit. */
10454 /* Save comp_insn16 to buffer. */
10455 bfd_putb16 (re_insn16, contents + irel->r_offset);
10456 *insn_len = 2;
10457 reloc = R_NDS32_9_PCREL_RELA;
10458 cond_reloc = R_NDS32_NONE;
10459 }
10460 cond_removed = 1;
10461 }
10462 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10463 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10464 && foff < ACCURATE_14BIT_S1 - first_size))
10465 {
10466 /* beqs label ; 15_PCREL */
10467 bfd_putb32 (re_insn, contents + irel->r_offset);
10468 *insn_len = 4;
10469 reloc = R_NDS32_15_PCREL_RELA;
10470 cond_reloc = R_NDS32_NONE;
10471 cond_removed = 1;
10472 }
10473 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10474 && foff >= -CONSERVATIVE_16BIT_S1
10475 && foff < CONSERVATIVE_16BIT_S1)
10476 {
10477 /* beqz label ; 17_PCREL */
10478 bfd_putb32 (re_insn, contents + irel->r_offset);
10479 *insn_len = 4;
10480 reloc = R_NDS32_17_PCREL_RELA;
10481 cond_reloc = R_NDS32_NONE;
10482 cond_removed = 1;
10483 }
10484 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10485 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10486 {
10487 /* Relax to one of the following 3 variations
10488
10489 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10490 for space
10491 bnes38 rt, $1 ; LONGJUMP2
10492 j label ; 25_PCREL
10493 $1
10494
10495 case 4-4; 1st insn not convertible, others don't care
10496 bne rt, ra, $1 ; LONGJUMP2
10497 j label ; 25_PCREL
10498 $1
10499
10500 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10501 bne rt, ra, $1 ; LONGJUMP2
10502 j label ; 25_PCREL
10503 $1 */
10504
10505 /* Offset for first instruction. */
10506
10507 /* Use j label as second instruction. */
10508 *insn_len = 4 + first_size;
10509 insn = INSN_J;
10510 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10511 reloc = R_NDS32_LONGJUMP2;
10512 cond_reloc = R_NDS32_25_PLTREL;
10513 }
10514 else
10515 return FALSE;
10516
10517 if (cond_removed == 1)
10518 {
10519 /* Set all relocations. */
10520 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10521 irel->r_addend = hi_irelfn->r_addend;
10522
10523 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10524 cond_reloc);
10525 cond_irelfn->r_addend = 0;
10526 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10527 R_NDS32_NONE);
10528 }
10529 else
10530 {
10531 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10532 irel->r_addend = irel->r_addend;
10533 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10534 cond_reloc);
10535 }
10536
10537 if ((seq_len ^ *insn_len ) & 0x2)
10538 {
10539 insn16 = NDS32_NOP16;
10540 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10541 lo_irelfn->r_offset = *insn_len;
10542 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10543 R_NDS32_INSN16);
10544 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10545 *insn_len += 2;
10546 }
10547 else
10548 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10549 R_NDS32_NONE);
10550 return TRUE;
10551 }
10552
10553 /* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
10554
10555 static bfd_boolean
10556 nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10557 Elf_Internal_Rela *internal_relocs, int *insn_len,
10558 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10559 Elf_Internal_Shdr *symtab_hdr)
10560 {
10561 /* The pattern for LONGCALL4. Support for function cse.
10562 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10563 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10564 jral ta ; PTR_RES/EMPTY/INSN16 */
10565
10566 bfd_vma laddr;
10567 uint32_t insn;
10568 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10569 Elf_Internal_Rela *irelend;
10570 bfd_signed_vma foff;
10571
10572 irelend = internal_relocs + sec->reloc_count;
10573 laddr = irel->r_offset;
10574
10575 /* Get the reloc for the address from which the register is
10576 being loaded. This reloc will tell us which function is
10577 actually being called. */
10578 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10579 R_NDS32_HI20_RELA, laddr);
10580
10581 if (hi_irel == irelend)
10582 {
10583 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10584 (uint64_t) irel->r_offset);
10585 return FALSE;
10586 }
10587
10588 /* Get the value of the symbol referred to by the reloc. */
10589 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10590
10591 /* This condition only happened when symbol is undefined. */
10592 if (foff == 0
10593 || foff < -CONSERVATIVE_24BIT_S1
10594 || foff >= CONSERVATIVE_24BIT_S1)
10595 return FALSE;
10596
10597 /* Relax to: jal symbol; 25_PCREL. */
10598 /* For simplicity of coding, we are going to modify the section
10599 contents, the section relocs, and the BFD symbol table. We
10600 must tell the rest of the code not to free up this
10601 information. It would be possible to instead create a table
10602 of changes which have to be made, as is done in coff-mips.c;
10603 that would be more work, but would require less memory when
10604 the linker is run. */
10605
10606 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10607 R_NDS32_PTR_RESOLVED, irel->r_addend);
10608 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10609 R_NDS32_EMPTY, irel->r_addend);
10610
10611 if (ptr_irel == irelend || em_irel == irelend)
10612 {
10613 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10614 (uint64_t) irel->r_offset);
10615 return FALSE;
10616 }
10617 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10618 insn = bfd_getb32 (contents + irel->r_addend);
10619 if (insn & 0x80000000)
10620 return FALSE;
10621
10622 /* Replace the long call with a jal. */
10623 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10624 R_NDS32_25_PCREL_RELA);
10625 ptr_irel->r_addend = 1;
10626
10627 /* We don't resolve this here but resolve it in relocate_section. */
10628 insn = INSN_JAL;
10629 bfd_putb32 (insn, contents + em_irel->r_offset);
10630
10631 irel->r_info =
10632 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10633
10634 /* If there is function cse, HI20 can not remove now. */
10635 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10636 R_NDS32_LONGCALL4, laddr);
10637 if (call_irel == irelend)
10638 {
10639 *insn_len = 0;
10640 hi_irel->r_info =
10641 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10642 }
10643
10644 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10645 R_NDS32_INSN16, irel->r_addend);
10646 if (insn_irel != irelend)
10647 insn_irel->r_info =
10648 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10649
10650 return TRUE;
10651 }
10652
10653 /* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
10654
10655 static bfd_boolean
10656 nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10657 Elf_Internal_Rela *internal_relocs, int *insn_len,
10658 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10659 Elf_Internal_Shdr *symtab_hdr)
10660 {
10661 /* The pattern for LONGCALL5.
10662 bltz rt, .L1 ; LONGCALL5/17_PCREL
10663 jal symbol ; 25_PCREL
10664 .L1: */
10665
10666 bfd_vma laddr;
10667 uint32_t insn;
10668 Elf_Internal_Rela *cond_irel, *irelend;
10669 bfd_signed_vma foff;
10670
10671 irelend = internal_relocs + sec->reloc_count;
10672 laddr = irel->r_offset;
10673 insn = bfd_getb32 (contents + laddr);
10674
10675 /* Get the reloc for the address from which the register is
10676 being loaded. This reloc will tell us which function is
10677 actually being called. */
10678 cond_irel =
10679 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10680 R_NDS32_25_PCREL_RELA, irel->r_addend);
10681 if (cond_irel == irelend)
10682 {
10683 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10684 (uint64_t) irel->r_offset);
10685 return FALSE;
10686 }
10687
10688 /* Get the value of the symbol referred to by the reloc. */
10689 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10690
10691 if (foff == 0
10692 || foff < -CONSERVATIVE_16BIT_S1
10693 || foff >= CONSERVATIVE_16BIT_S1)
10694 return FALSE;
10695
10696 /* Relax to bgezal rt, label ; 17_PCREL
10697 or bltzal rt, label ; 17_PCREL. */
10698
10699 /* Convert to complimentary conditional call. */
10700 insn = CONVERT_CONDITION_CALL (insn);
10701
10702 /* For simplicity of coding, we are going to modify the section
10703 contents, the section relocs, and the BFD symbol table. We
10704 must tell the rest of the code not to free up this
10705 information. It would be possible to instead create a table
10706 of changes which have to be made, as is done in coff-mips.c;
10707 that would be more work, but would require less memory when
10708 the linker is run. */
10709
10710 /* Modify relocation and contents. */
10711 cond_irel->r_info =
10712 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10713
10714 /* Replace the long call with a bgezal. */
10715 bfd_putb32 (insn, contents + cond_irel->r_offset);
10716 *insn_len = 0;
10717
10718 /* Clean unnessary relocations. */
10719 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10720
10721 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10722 R_NDS32_17_PCREL_RELA, laddr);
10723 cond_irel->r_info =
10724 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10725
10726 return TRUE;
10727 }
10728
10729 /* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
10730
10731 static bfd_boolean
10732 nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10733 Elf_Internal_Rela *internal_relocs, int *insn_len,
10734 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10735 Elf_Internal_Shdr *symtab_hdr)
10736 {
10737 /* The pattern for LONGCALL6.
10738 bltz rt, .L1 ; LONGCALL6/17_PCREL
10739 sethi ta, hi20(symbol) ; HI20/PTR
10740 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10741 jral ta ; PTR_RES/EMPTY/INSN16
10742 .L1 */
10743
10744 bfd_vma laddr;
10745 uint32_t insn;
10746 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10747 bfd_signed_vma foff;
10748
10749 irelend = internal_relocs + sec->reloc_count;
10750 laddr = irel->r_offset;
10751
10752 /* Get the reloc for the address from which the register is
10753 being loaded. This reloc will tell us which function is
10754 actually being called. */
10755 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10756 R_NDS32_EMPTY, irel->r_addend);
10757
10758 if (em_irel == irelend)
10759 {
10760 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10761 (uint64_t) irel->r_offset);
10762 return FALSE;
10763 }
10764
10765 /* Get the value of the symbol referred to by the reloc. */
10766 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10767
10768 if (foff == 0
10769 || foff < -CONSERVATIVE_24BIT_S1
10770 || foff >= CONSERVATIVE_24BIT_S1)
10771 return FALSE;
10772
10773 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10774 insn = bfd_getb32 (contents + irel->r_addend);
10775 if (insn & 0x80000000)
10776 return FALSE;
10777
10778 insn = bfd_getb32 (contents + laddr);
10779 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10780 {
10781 /* Relax to bgezal rt, label ; 17_PCREL
10782 or bltzal rt, label ; 17_PCREL. */
10783
10784 /* Convert to complimentary conditional call. */
10785 *insn_len = 0;
10786 insn = CONVERT_CONDITION_CALL (insn);
10787 bfd_putb32 (insn, contents + em_irel->r_offset);
10788
10789 em_irel->r_info =
10790 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10791
10792 /* Set resolved relocation. */
10793 cond_irel =
10794 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10795 R_NDS32_PTR_RESOLVED, irel->r_addend);
10796 if (cond_irel == irelend)
10797 {
10798 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10799 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10800 return FALSE;
10801 }
10802 cond_irel->r_addend = 1;
10803
10804 /* Clear relocations. */
10805
10806 irel->r_info =
10807 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10808
10809 cond_irel =
10810 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10811 R_NDS32_17_PCREL_RELA, laddr);
10812 if (cond_irel != irelend)
10813 cond_irel->r_info =
10814 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10815
10816 cond_irel =
10817 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10818 R_NDS32_INSN16, irel->r_addend);
10819 if (cond_irel != irelend)
10820 cond_irel->r_info =
10821 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10822
10823 }
10824 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10825 {
10826 /* Relax to the following instruction sequence
10827 bltz rt, .L1 ; LONGCALL2/17_PCREL
10828 jal symbol ; 25_PCREL/PTR_RES
10829 .L1 */
10830 *insn_len = 4;
10831 /* Convert instruction. */
10832 insn = INSN_JAL;
10833 bfd_putb32 (insn, contents + em_irel->r_offset);
10834
10835 /* Convert relocations. */
10836 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10837 R_NDS32_25_PCREL_RELA);
10838 irel->r_info =
10839 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10840
10841 /* Set resolved relocation. */
10842 cond_irel =
10843 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10844 R_NDS32_PTR_RESOLVED, irel->r_addend);
10845 if (cond_irel == irelend)
10846 {
10847 _bfd_error_handler (unrecognized_reloc_msg, abfd,
10848 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10849 return FALSE;
10850 }
10851 cond_irel->r_addend = 1;
10852
10853 cond_irel =
10854 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10855 R_NDS32_INSN16, irel->r_addend);
10856 if (cond_irel != irelend)
10857 cond_irel->r_info =
10858 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10859 }
10860 return TRUE;
10861 }
10862
10863 /* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
10864
10865 static bfd_boolean
10866 nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10867 Elf_Internal_Rela *internal_relocs, int *insn_len,
10868 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10869 Elf_Internal_Shdr *symtab_hdr)
10870 {
10871 /* The pattern for LONGJUMP4.
10872 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10873 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10874 jr ta ; PTR_RES/INSN16/EMPTY */
10875
10876 bfd_vma laddr;
10877 int seq_len; /* Original length of instruction sequence. */
10878 uint32_t insn;
10879 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10880 bfd_signed_vma foff;
10881
10882 irelend = internal_relocs + sec->reloc_count;
10883 seq_len = GET_SEQ_LEN (irel->r_addend);
10884 laddr = irel->r_offset;
10885 *insn_len = seq_len;
10886
10887 /* Get the reloc for the address from which the register is
10888 being loaded. This reloc will tell us which function is
10889 actually being called. */
10890
10891 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10892 R_NDS32_HI20_RELA, laddr);
10893
10894 if (hi_irel == irelend)
10895 {
10896 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10897 (uint64_t) irel->r_offset);
10898 return FALSE;
10899 }
10900
10901 /* Get the value of the symbol referred to by the reloc. */
10902 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10903
10904 if (foff == 0
10905 || foff >= CONSERVATIVE_24BIT_S1
10906 || foff < -CONSERVATIVE_24BIT_S1)
10907 return FALSE;
10908
10909 /* Convert it to "j label", it may be converted to j8 in the final
10910 pass of relaxation. Therefore, we do not consider this currently. */
10911 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10912 R_NDS32_PTR_RESOLVED, irel->r_addend);
10913 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10914 R_NDS32_EMPTY, irel->r_addend);
10915
10916 if (ptr_irel == irelend || em_irel == irelend)
10917 {
10918 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10919 (uint64_t) irel->r_offset);
10920 return FALSE;
10921 }
10922
10923 em_irel->r_info =
10924 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10925 ptr_irel->r_addend = 1;
10926
10927 /* Write instruction. */
10928 insn = INSN_J;
10929 bfd_putb32 (insn, contents + em_irel->r_offset);
10930
10931 /* Clear relocations. */
10932 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10933
10934 /* If there is function cse, HI20 can not remove now. */
10935 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10936 R_NDS32_LONGJUMP4, laddr);
10937 if (call_irel == irelend)
10938 {
10939 *insn_len = 0;
10940 hi_irel->r_info =
10941 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10942 }
10943
10944 return TRUE;
10945 }
10946
10947 /* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
10948
10949 static bfd_boolean
10950 nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10951 Elf_Internal_Rela *internal_relocs, int *insn_len,
10952 int *seq_len, bfd_byte *contents,
10953 Elf_Internal_Sym *isymbuf,
10954 Elf_Internal_Shdr *symtab_hdr)
10955 {
10956 /* There are 2 variations for LONGJUMP5
10957 case 2-4; 1st insn convertible, 16-bit on.
10958 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10959 j label ; 25_PCREL/INSN16
10960 $1:
10961
10962 case 4-4; 1st insn not convertible
10963 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10964 j label ; 25_PCREL/INSN16
10965 .L1: */
10966
10967 bfd_vma laddr;
10968 Elf_Internal_Rela *cond_irel, *irelend;
10969 unsigned int i;
10970 bfd_signed_vma foff;
10971 uint32_t insn, re_insn = 0;
10972 uint16_t insn16, re_insn16 = 0;
10973 unsigned long reloc;
10974
10975 enum elf_nds32_reloc_type checked_types[] =
10976 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10977 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10978
10979 irelend = internal_relocs + sec->reloc_count;
10980 laddr = irel->r_offset;
10981
10982 /* Get the reloc for the address from which the register is
10983 being loaded. This reloc will tell us which function is
10984 actually being called. */
10985
10986 cond_irel =
10987 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10988 R_NDS32_25_PCREL_RELA, irel->r_addend);
10989 if (cond_irel == irelend)
10990 {
10991 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10992 (uint64_t) irel->r_offset);
10993 return FALSE;
10994 }
10995
10996 /* Get the value of the symbol referred to by the reloc. */
10997 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10998
10999 if (foff == 0
11000 || foff < -CONSERVATIVE_16BIT_S1
11001 || foff >= CONSERVATIVE_16BIT_S1)
11002 return FALSE;
11003
11004 /* Get the all corresponding instructions. */
11005 insn = bfd_getb32 (contents + laddr);
11006 /* Check instruction size. */
11007 if (insn & 0x80000000)
11008 {
11009 *seq_len = 0;
11010 insn16 = insn >> 16;
11011 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11012 }
11013 else
11014 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11015
11016 if (N32_OP6 (re_insn) == N32_OP6_BR1
11017 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11018 {
11019 /* beqs label ; 15_PCREL. */
11020 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11021 reloc = R_NDS32_15_PCREL_RELA;
11022 }
11023 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11024 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11025 {
11026 /* beqz label ; 17_PCREL. */
11027 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11028 reloc = R_NDS32_17_PCREL_RELA;
11029 }
11030 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11031 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11032 {
11033 /* beqc label ; 9_PCREL. */
11034 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11035 reloc = R_NDS32_WORD_9_PCREL_RELA;
11036 }
11037 else
11038 return FALSE;
11039
11040 /* Set all relocations. */
11041 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11042
11043 /* Clean relocations. */
11044 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11045 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11046 {
11047 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11048 checked_types[i], laddr);
11049 if (cond_irel != irelend)
11050 {
11051 if (*seq_len == 0
11052 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11053 {
11054 /* If the branch instruction is 2 byte, it cannot remove
11055 directly. Only convert it to nop16 and remove it after
11056 checking alignment issue. */
11057 insn16 = NDS32_NOP16;
11058 bfd_putb16 (insn16, contents + laddr);
11059 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11060 }
11061 else
11062 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11063 R_NDS32_NONE);
11064 }
11065 }
11066 *insn_len = 0;
11067
11068 return TRUE;
11069 }
11070
11071 /* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
11072
11073 static bfd_boolean
11074 nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11075 Elf_Internal_Rela *internal_relocs, int *insn_len,
11076 int *seq_len, bfd_byte *contents,
11077 Elf_Internal_Sym *isymbuf,
11078 Elf_Internal_Shdr *symtab_hdr)
11079 {
11080 /* There are 5 variations for LONGJUMP6
11081 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11082 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11083 sethi ta, hi20(symbol) ; HI20/PTR
11084 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11085 jr ta ; PTR_RES/INSN16/EMPTY
11086 .L1:
11087
11088 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11089 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11090 sethi ta, hi20(symbol) ; HI20/PTR
11091 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11092 jr ta ; PTR_RES/INSN16/EMPTY
11093 .L1: */
11094
11095 enum elf_nds32_reloc_type checked_types[] =
11096 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11097 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11098
11099 int reloc_off = 0, cond_removed = 0;
11100 bfd_vma laddr;
11101 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11102 unsigned int i;
11103 bfd_signed_vma foff;
11104 uint32_t insn, re_insn = 0;
11105 uint16_t insn16, re_insn16 = 0;
11106 unsigned long reloc;
11107
11108 irelend = internal_relocs + sec->reloc_count;
11109 laddr = irel->r_offset;
11110
11111 /* Get the reloc for the address from which the register is
11112 being loaded. This reloc will tell us which function is
11113 actually being called. */
11114 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11115 R_NDS32_EMPTY, irel->r_addend);
11116
11117 if (em_irel == irelend)
11118 {
11119 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11120 (uint64_t) irel->r_offset);
11121 return FALSE;
11122 }
11123
11124 /* Get the value of the symbol referred to by the reloc. */
11125 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11126
11127 if (foff == 0
11128 || foff < -CONSERVATIVE_24BIT_S1
11129 || foff >= CONSERVATIVE_24BIT_S1)
11130 return FALSE;
11131
11132 insn = bfd_getb32 (contents + laddr);
11133 /* Check instruction size. */
11134 if (insn & 0x80000000)
11135 {
11136 *seq_len = 0;
11137 insn16 = insn >> 16;
11138 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11139 }
11140 else
11141 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11142
11143 /* For simplicity of coding, we are going to modify the section
11144 contents, the section relocs, and the BFD symbol table. We
11145 must tell the rest of the code not to free up this
11146 information. It would be possible to instead create a table
11147 of changes which have to be made, as is done in coff-mips.c;
11148 that would be more work, but would require less memory when
11149 the linker is run. */
11150
11151 if (N32_OP6 (re_insn) == N32_OP6_BR1
11152 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11153 {
11154 /* beqs label ; 15_PCREL. */
11155 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11156 reloc = R_NDS32_15_PCREL_RELA;
11157 cond_removed = 1;
11158 }
11159 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11160 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11161 {
11162 /* beqz label ; 17_PCREL. */
11163 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11164 reloc = R_NDS32_17_PCREL_RELA;
11165 cond_removed = 1;
11166 }
11167 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11168 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11169 {
11170 /* Relax to one of the following 2 variations
11171
11172 case 2-4; 1st insn convertible, 16-bit on.
11173 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11174 j label ; 25_PCREL/INSN16
11175 $1:
11176
11177 case 4-4; 1st insn not convertible
11178 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11179 j label ; 25_PCREL/INSN16
11180 .L1: */
11181
11182 /* Use j label as second instruction. */
11183 insn = INSN_J;
11184 reloc = R_NDS32_25_PCREL_RELA;
11185 bfd_putb32 (insn, contents + em_irel->r_offset);
11186 }
11187 else
11188 return FALSE;
11189
11190 /* Set all relocations. */
11191 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11192
11193 cond_irel =
11194 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11195 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11196 cond_irel->r_addend = 1;
11197
11198 /* Use INSN16 of first branch instruction to distinguish if keeping
11199 INSN16 of final instruction or not. */
11200 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11201 R_NDS32_INSN16, irel->r_offset);
11202 if (insn_irel == irelend)
11203 {
11204 /* Clean the final INSN16. */
11205 insn_irel =
11206 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11207 R_NDS32_INSN16, em_irel->r_offset);
11208 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11209 R_NDS32_NONE);
11210 }
11211
11212 if (cond_removed == 1)
11213 {
11214 *insn_len = 0;
11215
11216 /* Clear relocations. */
11217 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11218
11219 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11220 {
11221 cond_irel =
11222 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11223 checked_types[i], laddr);
11224 if (cond_irel != irelend)
11225 {
11226 if (*seq_len == 0
11227 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11228 {
11229 /* If the branch instruction is 2 byte, it cannot remove
11230 directly. Only convert it to nop16 and remove it after
11231 checking alignment issue. */
11232 insn16 = NDS32_NOP16;
11233 bfd_putb16 (insn16, contents + laddr);
11234 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11235 }
11236 else
11237 cond_irel->r_info =
11238 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11239 }
11240 }
11241 }
11242 else
11243 {
11244 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11245 R_NDS32_LONGJUMP5);
11246 }
11247
11248 return TRUE;
11249 }
11250
11251 /* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
11252
11253 static bfd_boolean
11254 nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11255 Elf_Internal_Rela *internal_relocs, int *insn_len,
11256 int *seq_len, bfd_byte *contents,
11257 Elf_Internal_Sym *isymbuf,
11258 Elf_Internal_Shdr *symtab_hdr)
11259 {
11260 /* There are 2 variations for LONGJUMP5
11261 case 2-4; 1st insn convertible, 16-bit on.
11262 movi55 ta, imm11 ; LONGJUMP7/INSN16
11263 beq rt, ta, label ; 15_PCREL
11264
11265 case 4-4; 1st insn not convertible
11266 movi55 ta, imm11 ; LONGJUMP7/INSN16
11267 beq rt, ta, label ; 15_PCREL */
11268
11269 bfd_vma laddr;
11270 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
11271 bfd_signed_vma foff;
11272 uint32_t insn, re_insn = 0;
11273 uint16_t insn16;
11274 uint32_t imm11;
11275
11276 irelend = internal_relocs + sec->reloc_count;
11277 laddr = irel->r_offset;
11278
11279 /* Get the reloc for the address from which the register is
11280 being loaded. This reloc will tell us which function is
11281 actually being called. */
11282
11283 cond_irel =
11284 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11285 R_NDS32_15_PCREL_RELA, irel->r_addend);
11286 if (cond_irel == irelend)
11287 {
11288 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11289 (uint64_t) irel->r_offset);
11290 return FALSE;
11291 }
11292
11293 /* Get the value of the symbol referred to by the reloc. */
11294 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11295
11296 if (foff == 0
11297 || foff < -CONSERVATIVE_8BIT_S1
11298 || foff >= CONSERVATIVE_8BIT_S1)
11299 return FALSE;
11300
11301 /* Get the first instruction for its size. */
11302 insn = bfd_getb32 (contents + laddr);
11303 if (insn & 0x80000000)
11304 {
11305 *seq_len = 0;
11306 /* Get the immediate from movi55. */
11307 imm11 = N16_IMM5S (insn >> 16);
11308 }
11309 else
11310 {
11311 /* Get the immediate from movi. */
11312 imm11 = N32_IMM20S (insn);
11313 }
11314
11315 /* Get the branch instruction. */
11316 insn = bfd_getb32 (contents + irel->r_addend);
11317 /* Convert instruction to BR3. */
11318 if ((insn >> 14) & 0x1)
11319 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11320 else
11321 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11322
11323 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11324
11325 /* Set all relocations. */
11326 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11327 R_NDS32_WORD_9_PCREL_RELA);
11328
11329 /* Clean relocations. */
11330 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11331 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11332 R_NDS32_INSN16, irel->r_offset);
11333 if (insn_irel != irelend)
11334 {
11335 if (*seq_len == 0)
11336 {
11337 /* If the first insntruction is 16bit, convert it to nop16. */
11338 insn16 = NDS32_NOP16;
11339 bfd_putb16 (insn16, contents + laddr);
11340 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11341 }
11342 else
11343 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11344 R_NDS32_NONE);
11345 }
11346 *insn_len = 0;
11347
11348 return TRUE;
11349 }
11350
11351 /* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11352 for each relax round. But the gp may changed dramatically and then cause
11353 the truncated to fit errors for the the converted gp instructions.
11354 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11355
11356 static bfd_boolean
11357 nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11358 Elf_Internal_Rela *irel, bfd_boolean *again,
11359 bfd_boolean init,
11360 struct elf_nds32_link_hash_table *table,
11361 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11362
11363 {
11364 int offset_to_gp;
11365 static bfd_boolean sec_pass = FALSE;
11366 static asection *first_sec = NULL, *sym_sec;
11367 /* Record the number of instructions which may be removed. */
11368 static int count = 0, record_count;
11369 Elf_Internal_Sym *isym;
11370 struct elf_link_hash_entry *h = NULL;
11371 int indx;
11372 unsigned long r_symndx;
11373 bfd *abfd = sec->owner;
11374 static bfd_vma record_sda = 0;
11375 int sda_offset = 0;
11376
11377 /* Force doing relaxation when hyper-relax is high. */
11378 if (table->hyper_relax == 2)
11379 return TRUE;
11380
11381 /* Do not relax the load/store patterns for the first
11382 relax round. */
11383 if (init)
11384 {
11385 if (!first_sec)
11386 first_sec = sec;
11387 else if (first_sec == sec)
11388 {
11389 record_count = count;
11390 count = 0;
11391 sec_pass = TRUE;
11392 }
11393
11394 if (!sec_pass)
11395 *again = TRUE;
11396
11397 return TRUE;
11398 }
11399
11400 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11401 DATA_SEGMENT_ALIGN size in the linker script may make it
11402 get even bigger. */
11403 if (record_sda == 0)
11404 record_sda = local_sda;
11405 else if (local_sda > record_sda)
11406 sda_offset = local_sda - record_sda;
11407
11408 /* Assume the instruction will be removed in the best case. */
11409 count++;
11410
11411 /* We record the offset to gp for each symbol, and then check
11412 if it is changed dramatically after relaxing.
11413 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11414 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11415 r_symndx = ELF32_R_SYM (irel->r_info);
11416 if (r_symndx >= symtab_hdr->sh_info)
11417 {
11418 /* Global symbols. */
11419 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11420 h = elf_sym_hashes (abfd)[indx];
11421 sym_sec = h->root.u.def.section;
11422 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11423 || bfd_is_abs_section (sym_sec))
11424 {
11425 /* Forbid doing relaxation when hyper-relax is low. */
11426 if (table->hyper_relax == 0)
11427 return FALSE;
11428
11429 offset_to_gp = *access_addr - local_sda;
11430 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11431 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11432 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11433 < abs (offset_to_gp) - sda_offset)
11434 {
11435 /* This may cause the error, so we reserve the
11436 safe enough size for relaxing. */
11437 if (*access_addr >= local_sda)
11438 *access_addr += (record_count * 4);
11439 else
11440 *access_addr -= (record_count * 4);
11441 }
11442 return sec_pass;
11443 }
11444 }
11445 else
11446 {
11447 /* Local symbols. */
11448 if (!elf32_nds32_allocate_local_sym_info (abfd))
11449 return FALSE;
11450 isym = isymbuf + r_symndx;
11451
11452 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11453 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11454 {
11455 /* Forbid doing relaxation when hyper-relax is low. */
11456 if (table->hyper_relax == 0)
11457 return FALSE;
11458
11459 offset_to_gp = *access_addr - local_sda;
11460 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11461 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11462 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11463 < abs (offset_to_gp) - sda_offset)
11464 {
11465 /* This may cause the error, so we reserve the
11466 safe enough size for relaxing. */
11467 if (*access_addr >= local_sda)
11468 *access_addr += (record_count * 4);
11469 else
11470 *access_addr -= (record_count * 4);
11471 }
11472 return sec_pass;
11473 }
11474 }
11475
11476 return TRUE;
11477 }
11478
11479 #define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11480
11481 /* Relax LOADSTORE relocation for nds32_elf_relax_section. */
11482
11483 static bfd_boolean
11484 nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11485 asection *sec, Elf_Internal_Rela *irel,
11486 Elf_Internal_Rela *internal_relocs, int *insn_len,
11487 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11488 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11489 struct elf_nds32_link_hash_table *table)
11490 {
11491 int eliminate_sethi = 0, range_type;
11492 unsigned int i;
11493 bfd_vma local_sda, laddr;
11494 int seq_len; /* Original length of instruction sequence. */
11495 uint32_t insn;
11496 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11497 bfd_vma access_addr = 0;
11498 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11499 struct elf_link_hash_entry *h = NULL;
11500 int indx;
11501 enum elf_nds32_reloc_type checked_types[] =
11502 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11503 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11504 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11505 R_NDS32_TLS_LE_HI20
11506 };
11507
11508 irelend = internal_relocs + sec->reloc_count;
11509 seq_len = GET_SEQ_LEN (irel->r_addend);
11510 laddr = irel->r_offset;
11511 *insn_len = seq_len;
11512
11513 /* Get the high part relocation. */
11514 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11515 {
11516 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11517 checked_types[i], laddr);
11518 if (hi_irelfn != irelend)
11519 break;
11520 }
11521
11522 if (hi_irelfn == irelend)
11523 {
11524 /* Not R_NDS32_HI20_RELA. */
11525 if (i != 0)
11526 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11527 (uint64_t) irel->r_offset);
11528 return FALSE;
11529 }
11530
11531 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11532 nds32_elf_final_sda_base (sec->output_section->owner,
11533 link_info, &local_sda, FALSE);
11534
11535 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11536 {
11537 case R_NDS32_HI20_RELA:
11538 insn = bfd_getb32 (contents + laddr);
11539 access_addr =
11540 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11541
11542 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11543 {
11544 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11545 h = elf_sym_hashes (abfd)[indx];
11546 }
11547
11548 /* Try movi. */
11549 if (range_type == NDS32_LOADSTORE_IMM
11550 && access_addr < CONSERVATIVE_20BIT
11551 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11552 {
11553 eliminate_sethi = 1;
11554 break;
11555 }
11556
11557 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11558 {
11559 eliminate_sethi = 1;
11560 break;
11561 }
11562 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11563 NULL, FALSE, table, isymbuf, symtab_hdr))
11564 return FALSE;
11565
11566 if (!load_store_relax)
11567 return FALSE;
11568
11569 /* Case for set gp register. */
11570 if (N32_RT5 (insn) == REG_GP)
11571 return FALSE;
11572
11573 if (range_type == NDS32_LOADSTORE_FLOAT_S
11574 || range_type == NDS32_LOADSTORE_FLOAT_D)
11575 {
11576 range_l = sdata_range[0][0];
11577 range_h = sdata_range[0][1];
11578 }
11579 else
11580 {
11581 range_l = sdata_range[1][0];
11582 range_h = sdata_range[1][1];
11583 }
11584 break;
11585
11586 default:
11587 return FALSE;
11588 }
11589
11590 /* Delete sethi instruction. */
11591 if (eliminate_sethi == 1
11592 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11593 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11594 {
11595 hi_irelfn->r_info =
11596 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11597 irel->r_info =
11598 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11599 *insn_len = 0;
11600 return TRUE;
11601 }
11602
11603 return FALSE;
11604 }
11605
11606 /* Relax LO12 relocation for nds32_elf_relax_section. */
11607
11608 static void
11609 nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11610 asection *sec, Elf_Internal_Rela *irel,
11611 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11612 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11613 struct elf_nds32_link_hash_table *table)
11614 {
11615 uint32_t insn;
11616 bfd_vma local_sda, laddr;
11617 unsigned long reloc;
11618 bfd_vma access_addr;
11619 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11620 Elf_Internal_Rela *irelfn = NULL, *irelend;
11621 struct elf_link_hash_entry *h = NULL;
11622 int indx;
11623
11624 /* For SDA base relative relaxation. */
11625 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11626 &local_sda, FALSE);
11627
11628 irelend = internal_relocs + sec->reloc_count;
11629 laddr = irel->r_offset;
11630 insn = bfd_getb32 (contents + laddr);
11631
11632 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11633 return;
11634
11635 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11636
11637 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11638 {
11639 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11640 h = elf_sym_hashes (abfd)[indx];
11641 }
11642
11643 /* Try movi. */
11644 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11645 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11646 {
11647 reloc = R_NDS32_20_RELA;
11648 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11649 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11650 bfd_putb32 (insn, contents + laddr);
11651 }
11652 else
11653 {
11654 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11655 {
11656 /* Fall through. */
11657 }
11658 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11659 FALSE, table, isymbuf, symtab_hdr))
11660 return;
11661
11662 range_l = sdata_range[1][0];
11663 range_h = sdata_range[1][1];
11664 switch (ELF32_R_TYPE (irel->r_info))
11665 {
11666 case R_NDS32_LO12S0_RELA:
11667 reloc = R_NDS32_SDA19S0_RELA;
11668 break;
11669 case R_NDS32_LO12S1_RELA:
11670 reloc = R_NDS32_SDA18S1_RELA;
11671 break;
11672 case R_NDS32_LO12S2_RELA:
11673 reloc = R_NDS32_SDA17S2_RELA;
11674 break;
11675 case R_NDS32_LO12S2_DP_RELA:
11676 range_l = sdata_range[0][0];
11677 range_h = sdata_range[0][1];
11678 reloc = R_NDS32_SDA12S2_DP_RELA;
11679 break;
11680 case R_NDS32_LO12S2_SP_RELA:
11681 range_l = sdata_range[0][0];
11682 range_h = sdata_range[0][1];
11683 reloc = R_NDS32_SDA12S2_SP_RELA;
11684 break;
11685 default:
11686 return;
11687 }
11688
11689 /* There are range_h and range_l because linker has to promise
11690 all sections move cross one page together. */
11691 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11692 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11693 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11694 {
11695 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11696 {
11697 /* Maybe we should add R_NDS32_INSN16 reloc type here
11698 or manually do some optimization. sethi can't be
11699 eliminated when updating $gp so the relative ori
11700 needs to be preserved. */
11701 return;
11702 }
11703 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11704 &insn))
11705 return;
11706 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11707 bfd_putb32 (insn, contents + laddr);
11708
11709 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11710 R_NDS32_INSN16);
11711 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11712 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11713 irelfn->r_info =
11714 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11715
11716 }
11717 }
11718 return;
11719 }
11720
11721 /* Relax PTR relocation for nds32_elf_relax_section. */
11722
11723 static bfd_boolean
11724 nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11725 Elf_Internal_Rela *internal_relocs, int *insn_len,
11726 int *seq_len, bfd_byte *contents)
11727 {
11728 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11729
11730 irelend = internal_relocs + sec->reloc_count;
11731
11732 re_irel =
11733 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11734 R_NDS32_PTR_RESOLVED, irel->r_addend);
11735
11736 if (re_irel == irelend)
11737 {
11738 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11739 (uint64_t) irel->r_offset);
11740 return FALSE;
11741 }
11742
11743 if (re_irel->r_addend != 1)
11744 return FALSE;
11745
11746 /* Pointed target is relaxed and no longer needs this void *,
11747 change the type to NONE. */
11748 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11749
11750 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11751 not exist, it means only count 1 and remove it directly. */
11752 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11753 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11754 R_NDS32_PTR_COUNT);
11755 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11756 R_NDS32_PTR);
11757 if (count_irel != irelend)
11758 {
11759 if (--count_irel->r_addend > 0)
11760 return FALSE;
11761 }
11762
11763 if (ptr_irel != irelend)
11764 return FALSE;
11765
11766 /* If the PTR_COUNT is already 0, remove current instruction. */
11767 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11768 *insn_len = 0;
11769 return TRUE;
11770 }
11771
11772 /* Relax LWC relocation for nds32_elf_relax_section. */
11773
11774 static void
11775 nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11776 asection *sec, Elf_Internal_Rela *irel,
11777 Elf_Internal_Rela *internal_relocs,
11778 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11779 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
11780 {
11781 /* Pattern:
11782 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11783 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11784 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11785 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11786 ... */
11787
11788 uint32_t insn;
11789 bfd_vma local_sda, laddr;
11790 unsigned long reloc;
11791 bfd_vma access_addr, flsi_offset;
11792 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11793 Elf_Internal_Rela *irelend, *re_irel;
11794 unsigned int opcode;
11795
11796 irelend = internal_relocs + sec->reloc_count;
11797 laddr = irel->r_offset;
11798 insn = bfd_getb32 (contents + laddr);
11799
11800 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11801 return;
11802
11803 /* Can not do relaxation for bi format. */
11804 if ((insn & 0x1000))
11805 return;
11806
11807 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11808 opcode = N32_OP6 (insn);
11809 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11810 reloc = R_NDS32_SDA12S2_SP_RELA;
11811 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11812 reloc = R_NDS32_SDA12S2_DP_RELA;
11813 else
11814 return;
11815
11816 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11817 R_NDS32_PTR_RESOLVED);
11818 if (re_irel == irelend)
11819 {
11820 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11821 (uint64_t) irel->r_offset);
11822 return;
11823 }
11824
11825 /* For SDA base relative relaxation. */
11826 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11827 &local_sda, FALSE);
11828 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11829 flsi_offset = (insn & 0xfff) << 2;
11830 access_addr += flsi_offset;
11831 range_l = sdata_range[0][0];
11832 range_h = sdata_range[0][1];
11833
11834 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11835 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11836 {
11837 /* Turn flsi instruction into sda access format. */
11838 insn = (insn & 0x7ff07000) | (REG_GP << 15);
11839
11840 /* Add relocation type to flsi. */
11841 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11842 irel->r_addend += flsi_offset;
11843 bfd_putb32 (insn, contents + re_irel->r_offset);
11844
11845 re_irel->r_addend |= 1;
11846 *again = TRUE;
11847 }
11848 }
11849
11850 static bfd_boolean
11851 nds32_relax_adjust_label (bfd *abfd, asection *sec,
11852 Elf_Internal_Rela *internal_relocs,
11853 bfd_byte *contents,
11854 nds32_elf_blank_t **relax_blank_list,
11855 int optimize, int opt_size)
11856 {
11857 /* This code block is used to adjust 4-byte alignment by relax a pair
11858 of instruction a time.
11859
11860 It recognizes three types of relocations.
11861 1. R_NDS32_LABEL - a alignment.
11862 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11863 3. is_16bit_NOP () - remove a 16-bit instruction. */
11864
11865 /* TODO: It seems currently implementation only support 4-byte alignment.
11866 We should handle any-alignment. */
11867
11868 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11869 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11870 Elf_Internal_Rela rel_temp;
11871 Elf_Internal_Rela *irelend;
11872 bfd_vma address;
11873 uint16_t insn16;
11874
11875 /* Checking for branch relaxation relies on the relocations to
11876 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11877 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11878 sizeof (Elf_Internal_Rela), compar_reloc);
11879
11880 irelend = internal_relocs + sec->reloc_count;
11881
11882 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11883 /* FIXME: Can we generate the right order in assembler?
11884 So we don't have to swapping them here. */
11885
11886 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11887 label_rel < irelend; label_rel++)
11888 {
11889 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11890 continue;
11891
11892 /* Find the first reloc has the same offset with label_rel. */
11893 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11894 insn_rel++;
11895
11896 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11897 insn_rel++)
11898 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11899 address. */
11900 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11901 break;
11902
11903 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11904 && insn_rel < label_rel)
11905 {
11906 /* Swap the two reloc if the R_NDS32_INSN16 is
11907 before R_NDS32_LABEL. */
11908 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11909 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11910 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11911 }
11912 }
11913
11914 label_rel = NULL;
11915 insn_rel = NULL;
11916 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11917 or higher, remove other R_NDS32_LABEL with lower alignment.
11918 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11919 then the R_NDS32_LABEL sequence is broke. */
11920 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11921 {
11922 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11923 {
11924 if (label_rel == NULL)
11925 {
11926 if (tmp_rel->r_addend < 2)
11927 label_rel = tmp_rel;
11928 continue;
11929 }
11930 else if (tmp_rel->r_addend > 1)
11931 {
11932 /* Remove all LABEL relocation from label_rel to tmp_rel
11933 including relocations with same offset as tmp_rel. */
11934 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
11935 {
11936 if (tmp2_rel->r_offset == tmp_rel->r_offset)
11937 break;
11938
11939 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11940 && tmp2_rel->r_addend < 2)
11941 tmp2_rel->r_info =
11942 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11943 R_NDS32_NONE);
11944 }
11945 label_rel = NULL;
11946 }
11947 }
11948 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11949 {
11950 /* A new INSN16 which can be converted, so clear label_rel. */
11951 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11952 irelend, &insn16)
11953 || is_16bit_NOP (abfd, sec, tmp_rel))
11954 label_rel = NULL;
11955 }
11956 }
11957
11958 label_rel = NULL;
11959 insn_rel = NULL;
11960 /* Optimized for speed and nothing has not been relaxed.
11961 It's time to align labels.
11962 We may convert a 16-bit instruction right before a label to
11963 32-bit, in order to align the label if necessary
11964 all reloc entries has been sorted by r_offset. */
11965 for (irel = internal_relocs;
11966 irel < irelend && irel->r_offset < sec->size; irel++)
11967 {
11968 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11969 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11970 continue;
11971
11972 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11973 {
11974 /* A new INSN16 found, resize the old one. */
11975 if (is_convert_32_to_16
11976 (abfd, sec, irel, internal_relocs, irelend, &insn16)
11977 || is_16bit_NOP (abfd, sec, irel))
11978 {
11979 if (insn_rel)
11980 {
11981 /* Previous INSN16 reloc exists, reduce its
11982 size to 16-bit. */
11983 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11984 irelend, &insn16))
11985 {
11986 nds32_elf_write_16 (abfd, contents, insn_rel,
11987 internal_relocs, irelend, insn16);
11988
11989 if (!insert_nds32_elf_blank_recalc_total
11990 (relax_blank_list, insn_rel->r_offset + 2, 2))
11991 return FALSE;
11992 }
11993 else if (is_16bit_NOP (abfd, sec, insn_rel))
11994 {
11995 if (!insert_nds32_elf_blank_recalc_total
11996 (relax_blank_list, insn_rel->r_offset, 2))
11997 return FALSE;
11998 }
11999 insn_rel->r_info =
12000 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12001 }
12002 /* Save the new one for later use. */
12003 insn_rel = irel;
12004 }
12005 else
12006 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12007 R_NDS32_NONE);
12008 }
12009 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12010 {
12011 /* Search for label. */
12012 int force_relax = 0;
12013
12014 /* Label on 16-bit instruction or optimization
12015 needless, just reset this reloc. */
12016 insn16 = bfd_getb16 (contents + irel->r_offset);
12017 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12018 {
12019 irel->r_info =
12020 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12021 continue;
12022 }
12023
12024 address =
12025 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12026 irel->r_offset, 1);
12027
12028 if (!insn_rel)
12029 {
12030 /* Check if there is case which can not be aligned. */
12031 if (irel->r_addend == 2 && address & 0x2)
12032 return FALSE;
12033 continue;
12034 }
12035
12036 /* Try to align this label. */
12037
12038 if ((irel->r_addend & 0x1f) < 2)
12039 {
12040 /* Check if there is a INSN16 at the same address.
12041 Label_rel always seats before insn_rel after
12042 our sort. */
12043
12044 /* Search for INSN16 at LABEL location. If INSN16 is at
12045 same location and this LABEL alignment is lower than 2,
12046 the INSN16 can be converted to 2-byte. */
12047 for (tmp_rel = irel;
12048 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12049 tmp_rel++)
12050 {
12051 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12052 && (is_convert_32_to_16
12053 (abfd, sec, tmp_rel, internal_relocs,
12054 irelend, &insn16)
12055 || is_16bit_NOP (abfd, sec, tmp_rel)))
12056 {
12057 force_relax = 1;
12058 break;
12059 }
12060 }
12061 }
12062
12063 if (force_relax || irel->r_addend == 1 || address & 0x2)
12064 {
12065 /* Label not aligned. */
12066 /* Previous reloc exists, reduce its size to 16-bit. */
12067 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12068 internal_relocs, irelend, &insn16))
12069 {
12070 nds32_elf_write_16 (abfd, contents, insn_rel,
12071 internal_relocs, irelend, insn16);
12072
12073 if (!insert_nds32_elf_blank_recalc_total
12074 (relax_blank_list, insn_rel->r_offset + 2, 2))
12075 return FALSE;
12076 }
12077 else if (is_16bit_NOP (abfd, sec, insn_rel))
12078 {
12079 if (!insert_nds32_elf_blank_recalc_total
12080 (relax_blank_list, insn_rel->r_offset, 2))
12081 return FALSE;
12082 }
12083
12084 }
12085 /* INSN16 reloc is used. */
12086 insn_rel = NULL;
12087 }
12088 }
12089
12090 address =
12091 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12092 if (insn_rel && (address & 0x2 || opt_size))
12093 {
12094 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12095 irelend, &insn16))
12096 {
12097 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12098 irelend, insn16);
12099 if (!insert_nds32_elf_blank_recalc_total
12100 (relax_blank_list, insn_rel->r_offset + 2, 2))
12101 return FALSE;
12102 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12103 R_NDS32_NONE);
12104 }
12105 else if (is_16bit_NOP (abfd, sec, insn_rel))
12106 {
12107 if (!insert_nds32_elf_blank_recalc_total
12108 (relax_blank_list, insn_rel->r_offset, 2))
12109 return FALSE;
12110 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12111 R_NDS32_NONE);
12112 }
12113 }
12114 insn_rel = NULL;
12115 return TRUE;
12116 }
12117
12118 static bfd_boolean
12119 nds32_elf_relax_section (bfd *abfd, asection *sec,
12120 struct bfd_link_info *link_info, bfd_boolean *again)
12121 {
12122 nds32_elf_blank_t *relax_blank_list = NULL;
12123 Elf_Internal_Shdr *symtab_hdr;
12124 Elf_Internal_Rela *internal_relocs;
12125 Elf_Internal_Rela *irel;
12126 Elf_Internal_Rela *irelend;
12127 Elf_Internal_Sym *isymbuf = NULL;
12128 bfd_byte *contents = NULL;
12129 bfd_boolean result = TRUE;
12130 int optimize = 0;
12131 int opt_size = 0;
12132 uint32_t insn;
12133 uint16_t insn16;
12134
12135 /* Target dependnet option. */
12136 struct elf_nds32_link_hash_table *table;
12137 int load_store_relax;
12138
12139 relax_blank_list = NULL;
12140
12141 *again = FALSE;
12142
12143 /* Nothing to do for
12144 * relocatable link or
12145 * non-relocatable section or
12146 * non-code section or
12147 * empty content or
12148 * no reloc entry. */
12149 if (bfd_link_relocatable (link_info)
12150 || (sec->flags & SEC_RELOC) == 0
12151 || (sec->flags & SEC_EXCLUDE) != 0
12152 || (sec->flags & SEC_CODE) == 0
12153 || sec->size == 0
12154 || sec->reloc_count == 0)
12155 return TRUE;
12156
12157 /* 09.12.11 Workaround. */
12158 /* We have to adjust align for R_NDS32_LABEL if needed.
12159 The adjust approach only can fix 2-byte align once. */
12160 if (sec->alignment_power > 2)
12161 return TRUE;
12162
12163 /* Do TLS model conversion once at first. */
12164 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12165
12166 /* The optimization type to do. */
12167
12168 table = nds32_elf_hash_table (link_info);
12169
12170 /* Save the first section for abs symbol relaxation.
12171 This is used for checking gp relaxation in the
12172 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12173 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12174 table, NULL, NULL);
12175
12176 /* The begining of general relaxation. */
12177
12178 if (is_SDA_BASE_set == 0)
12179 {
12180 bfd_vma gp;
12181 is_SDA_BASE_set = 1;
12182 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12183 &gp, FALSE);
12184 relax_range_measurement (abfd);
12185 }
12186
12187 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12188 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12189 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12190 TRUE /* keep_memory */);
12191 if (internal_relocs == NULL)
12192 goto error_return;
12193
12194 irelend = internal_relocs + sec->reloc_count;
12195 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12196 irelend, R_NDS32_RELAX_ENTRY);
12197
12198 if (irel == irelend)
12199 return TRUE;
12200
12201 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12202 {
12203 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12204 return TRUE;
12205
12206 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12207 optimize = 1;
12208
12209 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12210 opt_size = 1;
12211 }
12212
12213 load_store_relax = table->load_store_relax;
12214
12215 /* Get symbol table and section content. */
12216 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12217 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12218 goto error_return;
12219
12220 /* Do relax loop only when finalize is not done.
12221 Take care of relaxable relocs except INSN16. */
12222 for (irel = internal_relocs; irel < irelend; irel++)
12223 {
12224 int seq_len; /* Original length of instruction sequence. */
12225 int insn_len = 0; /* Final length of instruction sequence. */
12226 bfd_boolean removed;
12227
12228 insn = 0;
12229 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12230 && (irel->r_addend & 0x1f) >= 2)
12231 optimize = 1;
12232
12233 /* Relocation Types
12234 R_NDS32_LONGCALL1 53
12235 R_NDS32_LONGCALL2 54
12236 R_NDS32_LONGCALL3 55
12237 R_NDS32_LONGJUMP1 56
12238 R_NDS32_LONGJUMP2 57
12239 R_NDS32_LONGJUMP3 58
12240 R_NDS32_LOADSTORE 59 */
12241 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12242 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12243 seq_len = GET_SEQ_LEN (irel->r_addend);
12244
12245 /* Relocation Types
12246 R_NDS32_LONGCALL4 107
12247 R_NDS32_LONGCALL5 108
12248 R_NDS32_LONGCALL6 109
12249 R_NDS32_LONGJUMP4 110
12250 R_NDS32_LONGJUMP5 111
12251 R_NDS32_LONGJUMP6 112
12252 R_NDS32_LONGJUMP7 113 */
12253 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12254 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12255 seq_len = 4;
12256
12257 /* Relocation Types
12258 R_NDS32_LO12S0_RELA 30
12259 R_NDS32_LO12S1_RELA 29
12260 R_NDS32_LO12S2_RELA 28
12261 R_NDS32_LO12S2_SP_RELA 71
12262 R_NDS32_LO12S2_DP_RELA 70
12263 R_NDS32_GOT_LO12 46
12264 R_NDS32_GOTOFF_LO12 50
12265 R_NDS32_PLTREL_LO12 65
12266 R_NDS32_PLT_GOTREL_LO12 67
12267 R_NDS32_17IFC_PCREL_RELA 96
12268 R_NDS32_GOT_SUFF 193
12269 R_NDS32_GOTOFF_SUFF 194
12270 R_NDS32_PLT_GOT_SUFF 195
12271 R_NDS32_MULCALL_SUFF 196
12272 R_NDS32_PTR 197 */
12273 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12274 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12275 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12276 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12277 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12278 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12279 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12280 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12281 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12282 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12283 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12284 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12285 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12286 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12287 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12288 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12289 seq_len = 0;
12290 else
12291 continue;
12292
12293 insn_len = seq_len;
12294 removed = FALSE;
12295
12296 switch (ELF32_R_TYPE (irel->r_info))
12297 {
12298 case R_NDS32_LONGCALL1:
12299 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12300 &insn_len, contents, isymbuf,
12301 symtab_hdr);
12302 break;
12303 case R_NDS32_LONGCALL2:
12304 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12305 &insn_len, contents, isymbuf,
12306 symtab_hdr);
12307 break;
12308 case R_NDS32_LONGCALL3:
12309 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12310 &insn_len, contents, isymbuf,
12311 symtab_hdr);
12312 break;
12313 case R_NDS32_LONGJUMP1:
12314 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12315 &insn_len, contents, isymbuf,
12316 symtab_hdr);
12317 break;
12318 case R_NDS32_LONGJUMP2:
12319 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12320 &insn_len, contents, isymbuf,
12321 symtab_hdr);
12322 break;
12323 case R_NDS32_LONGJUMP3:
12324 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12325 &insn_len, contents, isymbuf,
12326 symtab_hdr);
12327 break;
12328 case R_NDS32_LONGCALL4:
12329 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12330 &insn_len, contents, isymbuf,
12331 symtab_hdr);
12332 break;
12333 case R_NDS32_LONGCALL5:
12334 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12335 &insn_len, contents, isymbuf,
12336 symtab_hdr);
12337 break;
12338 case R_NDS32_LONGCALL6:
12339 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12340 &insn_len, contents, isymbuf,
12341 symtab_hdr);
12342 break;
12343 case R_NDS32_LONGJUMP4:
12344 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12345 &insn_len, contents, isymbuf,
12346 symtab_hdr);
12347 break;
12348 case R_NDS32_LONGJUMP5:
12349 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12350 &insn_len, &seq_len, contents,
12351 isymbuf, symtab_hdr);
12352 break;
12353 case R_NDS32_LONGJUMP6:
12354 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12355 &insn_len, &seq_len, contents,
12356 isymbuf, symtab_hdr);
12357 break;
12358 case R_NDS32_LONGJUMP7:
12359 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12360 &insn_len, &seq_len, contents,
12361 isymbuf, symtab_hdr);
12362 break;
12363 case R_NDS32_LOADSTORE:
12364 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12365 internal_relocs, &insn_len,
12366 contents, isymbuf, symtab_hdr,
12367 load_store_relax, table);
12368 break;
12369 case R_NDS32_LO12S0_RELA:
12370 case R_NDS32_LO12S1_RELA:
12371 case R_NDS32_LO12S2_RELA:
12372 case R_NDS32_LO12S2_DP_RELA:
12373 case R_NDS32_LO12S2_SP_RELA:
12374 /* Relax for low part. */
12375 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12376 contents, isymbuf, symtab_hdr, table);
12377
12378 /* It is impossible to delete blank, so just continue. */
12379 continue;
12380 case R_NDS32_PTR:
12381 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12382 &insn_len, &seq_len, contents);
12383 break;
12384 case R_NDS32_LSI:
12385 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12386 contents, isymbuf, symtab_hdr, again);
12387 continue;
12388 case R_NDS32_GOT_LO12:
12389 case R_NDS32_GOTOFF_LO12:
12390 case R_NDS32_PLTREL_LO12:
12391 case R_NDS32_PLT_GOTREL_LO12:
12392 case R_NDS32_GOTPC_LO12:
12393 case R_NDS32_TLS_LE_LO12:
12394 case R_NDS32_TLS_LE_ADD:
12395 case R_NDS32_TLS_LE_LS:
12396 case R_NDS32_PLT_GOT_SUFF:
12397 case R_NDS32_GOT_SUFF:
12398 case R_NDS32_GOTOFF_SUFF:
12399 continue;
12400 default:
12401 continue;
12402 }
12403
12404 if (removed && seq_len - insn_len > 0)
12405 {
12406 if (!insert_nds32_elf_blank
12407 (&relax_blank_list, irel->r_offset + insn_len,
12408 seq_len - insn_len))
12409 goto error_return;
12410 *again = TRUE;
12411 }
12412 }
12413
12414 calc_nds32_blank_total (relax_blank_list);
12415
12416 if (table->relax_fp_as_gp)
12417 {
12418 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12419 irelend, isymbuf))
12420 goto error_return;
12421
12422 if (!*again)
12423 {
12424 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12425 irelend))
12426 goto error_return;
12427 }
12428 }
12429
12430 if (!*again)
12431 {
12432 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12433 &relax_blank_list, optimize, opt_size))
12434 goto error_return;
12435 }
12436
12437 /* It doesn't matter optimize_for_space_no_align anymore.
12438 If object file is assembled with flag '-Os',
12439 the we don't adjust jump-destination on 4-byte boundary. */
12440
12441 if (relax_blank_list)
12442 {
12443 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12444 relax_blank_list = NULL;
12445 }
12446
12447 if (!*again)
12448 {
12449 /* Closing the section, so we don't relax it anymore. */
12450 bfd_vma sec_size_align;
12451 Elf_Internal_Rela *tmp_rel;
12452
12453 /* Pad to alignment boundary. Only handle current section alignment. */
12454 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12455 & ((-1U) << sec->alignment_power);
12456 if ((sec_size_align - sec->size) & 0x2)
12457 {
12458 insn16 = NDS32_NOP16;
12459 bfd_putb16 (insn16, contents + sec->size);
12460 sec->size += 2;
12461 }
12462
12463 while (sec_size_align != sec->size)
12464 {
12465 insn = NDS32_NOP32;
12466 bfd_putb32 (insn, contents + sec->size);
12467 sec->size += 4;
12468 }
12469
12470 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12471 irelend, R_NDS32_RELAX_ENTRY);
12472 if (tmp_rel != irelend)
12473 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12474
12475 clean_nds32_elf_blank ();
12476 }
12477
12478 finish:
12479 if (elf_section_data (sec)->relocs != internal_relocs)
12480 free (internal_relocs);
12481
12482 if (elf_section_data (sec)->this_hdr.contents != contents)
12483 free (contents);
12484
12485 if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12486 free (isymbuf);
12487
12488 return result;
12489
12490 error_return:
12491 result = FALSE;
12492 goto finish;
12493 }
12494
12495 static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12496 {
12497 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12498 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12499 {NULL, 0, 0, 0, 0}
12500 };
12501
12502 static bfd_boolean
12503 nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12504 {
12505 const char *name = hdr->bfd_section->name;
12506
12507 if (strncmp (name, ".sbss", 5) == 0
12508 || strncmp (name, ".sdata", 6) == 0)
12509 hdr->bfd_section->flags |= SEC_SMALL_DATA;
12510
12511 return TRUE;
12512 }
12513
12514 static bfd_boolean
12515 nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12516 struct bfd_link_info *info,
12517 void *finfo ATTRIBUTE_UNUSED,
12518 bfd_boolean (*func) (void *, const char *,
12519 Elf_Internal_Sym *,
12520 asection *,
12521 struct elf_link_hash_entry *)
12522 ATTRIBUTE_UNUSED)
12523 {
12524 FILE *sym_ld_script = NULL;
12525 struct elf_nds32_link_hash_table *table;
12526
12527 table = nds32_elf_hash_table (info);
12528 sym_ld_script = table->sym_ld_script;
12529
12530 if (check_start_export_sym)
12531 fprintf (sym_ld_script, "}\n");
12532
12533 return TRUE;
12534 }
12535
12536 static enum elf_reloc_type_class
12537 nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12538 const asection *rel_sec ATTRIBUTE_UNUSED,
12539 const Elf_Internal_Rela *rela)
12540 {
12541 switch ((int) ELF32_R_TYPE (rela->r_info))
12542 {
12543 case R_NDS32_RELATIVE:
12544 return reloc_class_relative;
12545 case R_NDS32_JMP_SLOT:
12546 return reloc_class_plt;
12547 case R_NDS32_COPY:
12548 return reloc_class_copy;
12549 default:
12550 return reloc_class_normal;
12551 }
12552 }
12553
12554 /* Put target dependent option into info hash table. */
12555 void
12556 bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12557 int relax_fp_as_gp,
12558 int eliminate_gc_relocs,
12559 FILE * sym_ld_script,
12560 int hyper_relax,
12561 int tls_desc_trampoline,
12562 int load_store_relax)
12563 {
12564 struct elf_nds32_link_hash_table *table;
12565
12566 table = nds32_elf_hash_table (link_info);
12567 if (table == NULL)
12568 return;
12569
12570 table->relax_fp_as_gp = relax_fp_as_gp;
12571 table->eliminate_gc_relocs = eliminate_gc_relocs;
12572 table->sym_ld_script = sym_ld_script;
12573 table->hyper_relax = hyper_relax;
12574 table->tls_desc_trampoline = tls_desc_trampoline;
12575 table ->load_store_relax = load_store_relax;
12576 }
12577 \f
12578
12579 /* These functions and data-structures are used for fp-as-gp
12580 optimization. */
12581
12582 #define FAG_THRESHOLD 3 /* At least 3 gp-access. */
12583 /* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12584 the read-only section and read-write section. */
12585 #define FAG_WINDOW (508 - 32)
12586
12587 /* An nds32_fag represent a gp-relative access.
12588 We find best fp-base by using a sliding window
12589 to find a base address which can cover most gp-access. */
12590 struct nds32_fag
12591 {
12592 struct nds32_fag *next; /* NULL-teminated linked list. */
12593 bfd_vma addr; /* The address of this fag. */
12594 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12595 It is used for applying FP7U2_FLAG. */
12596 int count; /* How many times this address is referred.
12597 There should be exactly `count' relocations
12598 in relas. */
12599 int relas_capcity; /* The buffer size of relas.
12600 We use an array instead of linked-list,
12601 and realloc is used to adjust buffer size. */
12602 };
12603
12604 static void
12605 nds32_fag_init (struct nds32_fag *head)
12606 {
12607 memset (head, 0, sizeof (struct nds32_fag));
12608 }
12609
12610 static void
12611 nds32_fag_verify (struct nds32_fag *head)
12612 {
12613 struct nds32_fag *iter;
12614 struct nds32_fag *prev;
12615
12616 prev = NULL;
12617 iter = head->next;
12618 while (iter)
12619 {
12620 if (prev && prev->addr >= iter->addr)
12621 puts ("Bug in fp-as-gp insertion.");
12622 prev = iter;
12623 iter = iter->next;
12624 }
12625 }
12626
12627 /* Insert a fag in ascending order.
12628 If a fag of the same address already exists,
12629 they are chained by relas array. */
12630
12631 static void
12632 nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12633 Elf_Internal_Rela * rel)
12634 {
12635 struct nds32_fag *iter;
12636 struct nds32_fag *new_fag;
12637 const int INIT_RELAS_CAP = 4;
12638
12639 for (iter = head;
12640 iter->next && iter->next->addr <= addr;
12641 iter = iter->next)
12642 /* Find somewhere to insert. */ ;
12643
12644 /* `iter' will be equal to `head' if the list is empty. */
12645 if (iter != head && iter->addr == addr)
12646 {
12647 /* The address exists in the list.
12648 Insert `rel' into relocation list, relas. */
12649
12650 /* Check whether relas is big enough. */
12651 if (iter->count >= iter->relas_capcity)
12652 {
12653 iter->relas_capcity *= 2;
12654 iter->relas = bfd_realloc
12655 (iter->relas, iter->relas_capcity * sizeof (void *));
12656 }
12657 iter->relas[iter->count++] = rel;
12658 return;
12659 }
12660
12661 /* This is a new address. Create a fag node for it. */
12662 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12663 memset (new_fag, 0, sizeof (*new_fag));
12664 new_fag->addr = addr;
12665 new_fag->count = 1;
12666 new_fag->next = iter->next;
12667 new_fag->relas_capcity = INIT_RELAS_CAP;
12668 new_fag->relas = (Elf_Internal_Rela **)
12669 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12670 new_fag->relas[0] = rel;
12671 iter->next = new_fag;
12672
12673 nds32_fag_verify (head);
12674 }
12675
12676 static void
12677 nds32_fag_free_list (struct nds32_fag *head)
12678 {
12679 struct nds32_fag *iter;
12680
12681 iter = head->next;
12682 while (iter)
12683 {
12684 struct nds32_fag *tmp = iter;
12685 iter = iter->next;
12686 free (tmp->relas);
12687 tmp->relas = NULL;
12688 free (tmp);
12689 }
12690 }
12691
12692 /* Find the best fp-base address.
12693 The relocation associated with that address is returned,
12694 so we can track the symbol instead of a fixed address.
12695
12696 When relaxation, the address of an datum may change,
12697 because a text section is shrinked, so the data section
12698 moves forward. If the aligments of text and data section
12699 are different, their distance may change too.
12700 Therefore, tracking a fixed address is not appriate. */
12701
12702 static int
12703 nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12704 {
12705 struct nds32_fag *base; /* First fag in the window. */
12706 struct nds32_fag *last; /* First fag outside the window. */
12707 int accu = 0; /* Usage accumulation. */
12708 struct nds32_fag *best; /* Best fag. */
12709 int baccu = 0; /* Best accumulation. */
12710
12711 /* Use first fag for initial, and find the last fag in the window.
12712
12713 In each iteration, we could simply subtract previous fag
12714 and accumulate following fags which are inside the window,
12715 untill we each the end. */
12716
12717 if (head->next == NULL)
12718 {
12719 *bestpp = NULL;
12720 return 0;
12721 }
12722
12723 /* Initialize base. */
12724 base = head->next;
12725 best = base;
12726 for (last = base;
12727 last && last->addr < base->addr + FAG_WINDOW;
12728 last = last->next)
12729 accu += last->count;
12730
12731 baccu = accu;
12732
12733 /* Record the best base in each iteration. */
12734 while (base->next)
12735 {
12736 accu -= base->count;
12737 base = base->next;
12738 /* Account fags in window. */
12739 for (/* Nothing. */;
12740 last && last->addr < base->addr + FAG_WINDOW;
12741 last = last->next)
12742 accu += last->count;
12743
12744 /* A better fp-base? */
12745 if (accu > baccu)
12746 {
12747 best = base;
12748 baccu = accu;
12749 }
12750 }
12751
12752 if (bestpp)
12753 *bestpp = best;
12754 return baccu;
12755 }
12756
12757 /* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12758 so we can convert it fo fp-relative access later.
12759 `best_fag' is the best fp-base. Only those inside the window
12760 of best_fag is applied the flag. */
12761
12762 static bfd_boolean
12763 nds32_fag_mark_relax (struct bfd_link_info *link_info,
12764 asection *sec, struct nds32_fag *best_fag,
12765 Elf_Internal_Rela *internal_relocs,
12766 Elf_Internal_Rela *irelend)
12767 {
12768 struct nds32_fag *ifag;
12769 bfd_vma best_fpbase, gp;
12770 bfd *output_bfd;
12771
12772 output_bfd = sec->output_section->owner;
12773 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12774 best_fpbase = best_fag->addr;
12775
12776 if (best_fpbase > gp + sdata_range[1][1]
12777 || best_fpbase < gp - sdata_range[1][0])
12778 return FALSE;
12779
12780 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12781 so we know they can be converted to lwi37.fp. */
12782 for (ifag = best_fag;
12783 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12784 {
12785 int i;
12786
12787 for (i = 0; i < ifag->count; i++)
12788 {
12789 Elf_Internal_Rela *insn16_rel;
12790 Elf_Internal_Rela *fag_rel;
12791
12792 fag_rel = ifag->relas[i];
12793
12794 /* Only if this is within the WINDOWS, FP7U2_FLAG
12795 is applied. */
12796
12797 insn16_rel = find_relocs_at_address
12798 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12799
12800 if (insn16_rel != irelend)
12801 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12802 }
12803 }
12804 return TRUE;
12805 }
12806
12807 /* Reset INSN16 to clean fp as gp. */
12808
12809 static void
12810 nds32_fag_unmark_relax (struct nds32_fag *fag,
12811 Elf_Internal_Rela *internal_relocs,
12812 Elf_Internal_Rela *irelend)
12813 {
12814 struct nds32_fag *ifag;
12815 int i;
12816 Elf_Internal_Rela *insn16_rel;
12817 Elf_Internal_Rela *fag_rel;
12818
12819 for (ifag = fag; ifag; ifag = ifag->next)
12820 {
12821 for (i = 0; i < ifag->count; i++)
12822 {
12823 fag_rel = ifag->relas[i];
12824
12825 /* Restore the INSN16 relocation. */
12826 insn16_rel = find_relocs_at_address
12827 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12828
12829 if (insn16_rel != irelend)
12830 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12831 }
12832 }
12833 }
12834
12835 /* This is the main function of fp-as-gp optimization.
12836 It should be called by relax_section. */
12837
12838 static bfd_boolean
12839 nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12840 bfd *abfd, asection *sec,
12841 Elf_Internal_Rela *internal_relocs,
12842 Elf_Internal_Rela *irelend,
12843 Elf_Internal_Sym *isymbuf)
12844 {
12845 Elf_Internal_Rela *begin_rel = NULL;
12846 Elf_Internal_Rela *irel;
12847 struct nds32_fag fag_head;
12848 Elf_Internal_Shdr *symtab_hdr;
12849 bfd_byte *contents;
12850 bfd_boolean ifc_inside = FALSE;
12851
12852 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12853
12854 /* Per-function fp-base selection.
12855 1. Create a list for all the gp-relative access.
12856 2. Base on those gp-relative address,
12857 find a fp-base which can cover most access.
12858 3. Use the fp-base for fp-as-gp relaxation.
12859
12860 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12861 we should
12862 1. delete the `la $fp, _FP_BASE_' instruction and
12863 2. not convert lwi.gp to lwi37.fp.
12864
12865 To delete the _FP_BASE_ instruction, we simply apply
12866 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12867
12868 To suppress the conversion, we simply NOT to apply
12869 R_NDS32_INSN16_FP7U2_FLAG flag. */
12870
12871 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12872
12873 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
12874 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12875 return FALSE;
12876
12877 /* Check whether it is worth for fp-as-gp optimization,
12878 i.e., at least 3 gp-load.
12879
12880 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12881 apply this optimization. */
12882
12883 for (irel = internal_relocs; irel < irelend; irel++)
12884 {
12885 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12886 One we enter the begin of the region, we track all the LW/ST
12887 instructions, so when we leave the region, we try to find
12888 the best fp-base address for those LW/ST instructions. */
12889
12890 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12891 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12892 {
12893 /* Begin of the region. */
12894 if (begin_rel)
12895 /* xgettext:c-format */
12896 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12897
12898 begin_rel = irel;
12899 nds32_fag_init (&fag_head);
12900 ifc_inside = FALSE;
12901 }
12902 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12903 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12904 {
12905 int accu;
12906 struct nds32_fag *best_fag, *tmp_fag;
12907 int dist;
12908
12909 /* End of the region.
12910 Check whether it is worth to do fp-as-gp. */
12911
12912 if (begin_rel == NULL)
12913 {
12914 /* xgettext:c-format */
12915 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12916 abfd, sec);
12917 continue;
12918 }
12919
12920 accu = nds32_fag_find_base (&fag_head, &best_fag);
12921
12922 /* Clean FP7U2_FLAG because they may set ever. */
12923 tmp_fag = fag_head.next;
12924 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12925
12926 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12927 if (accu < FAG_THRESHOLD
12928 || !nds32_fag_mark_relax (link_info, sec, best_fag,
12929 internal_relocs, irelend))
12930 {
12931 /* Not worth to do fp-as-gp. */
12932 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12933 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12934 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12935 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12936 nds32_fag_free_list (&fag_head);
12937 begin_rel = NULL;
12938 continue;
12939 }
12940
12941 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12942 so we use it to record the distance to the reloction of best
12943 fp-base. */
12944 dist = best_fag->relas[0] - begin_rel;
12945 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12946 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12947 relocation. And get the base value when relocating. */
12948 begin_rel->r_addend &= (0x1 << 16) - 1;
12949 begin_rel->r_addend |= dist << 16;
12950
12951 nds32_fag_free_list (&fag_head);
12952 begin_rel = NULL;
12953 }
12954
12955 if (begin_rel == NULL || ifc_inside)
12956 /* Skip if we are not in the region of fp-as-gp. */
12957 continue;
12958
12959 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12960 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12961 {
12962 bfd_vma addr;
12963 uint32_t insn;
12964
12965 /* A gp-relative access is found. Insert it to the fag-list. */
12966
12967 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
12968 insn = bfd_getb32 (contents + irel->r_offset);
12969 if (!N32_IS_RT3 (insn))
12970 continue;
12971
12972 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12973 nds32_fag_insert (&fag_head, addr, irel);
12974 }
12975 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12976 {
12977 begin_rel = NULL;
12978 }
12979 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12980 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12981 {
12982 /* Suppress fp as gp when encounter ifc. */
12983 ifc_inside = TRUE;
12984 }
12985 }
12986
12987 return TRUE;
12988 }
12989
12990 /* Remove unused `la $fp, _FD_BASE_' instruction. */
12991
12992 static bfd_boolean
12993 nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12994 Elf_Internal_Rela *internal_relocs,
12995 Elf_Internal_Rela *irelend)
12996 {
12997 Elf_Internal_Rela *irel;
12998 Elf_Internal_Shdr *symtab_hdr;
12999 bfd_byte *contents = NULL;
13000 nds32_elf_blank_t *relax_blank_list = NULL;
13001 bfd_boolean result = TRUE;
13002 bfd_boolean unused_region = FALSE;
13003
13004 /*
13005 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13006 R_NDS32_17IFC_PCREL_RELA
13007 R_NDS32_10IFCU_PCREL_RELA. */
13008
13009 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13010 nds32_get_section_contents (abfd, sec, &contents, TRUE);
13011
13012 for (irel = internal_relocs; irel < irelend; irel++)
13013 {
13014 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13015 we marked to in previous pass.
13016 DO NOT scan relocations again, since we've alreadly decided it
13017 and set the flag. */
13018 const char *syname;
13019 int syndx;
13020 uint32_t insn;
13021
13022 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13023 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13024 unused_region = TRUE;
13025 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13026 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13027 unused_region = FALSE;
13028
13029 /* We're not in the region. */
13030 if (!unused_region)
13031 continue;
13032
13033 /* _FP_BASE_ must be a GLOBAL symbol. */
13034 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13035 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13036 continue;
13037
13038 /* The symbol name must be _FP_BASE_. */
13039 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13040 if (strcmp (syname, FP_BASE_NAME) != 0)
13041 continue;
13042
13043 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13044 {
13045 /* addi.gp $fp, -256 */
13046 insn = bfd_getb32 (contents + irel->r_offset);
13047 if (insn != INSN_ADDIGP_TO_FP)
13048 continue;
13049 }
13050 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13051 {
13052 /* addi $fp, $gp, -256 */
13053 insn = bfd_getb32 (contents + irel->r_offset);
13054 if (insn != INSN_ADDI_GP_TO_FP)
13055 continue;
13056 }
13057 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13058 {
13059 /* movi $fp, FP_BASE */
13060 insn = bfd_getb32 (contents + irel->r_offset);
13061 if (insn != INSN_MOVI_TO_FP)
13062 continue;
13063 }
13064 else
13065 continue;
13066
13067 /* We got here because a FP_BASE instruction is found. */
13068 if (!insert_nds32_elf_blank_recalc_total
13069 (&relax_blank_list, irel->r_offset, 4))
13070 goto error_return;
13071 }
13072
13073 finish:
13074 if (relax_blank_list)
13075 {
13076 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13077 relax_blank_list = NULL;
13078 }
13079 return result;
13080
13081 error_return:
13082 result = FALSE;
13083 goto finish;
13084 }
13085
13086 /* This is a version of bfd_generic_get_relocated_section_contents.
13087 We need this variety because relaxation will modify the dwarf
13088 infomation. When there is undefined symbol reference error mesage,
13089 linker need to dump line number where the symbol be used. However
13090 the address is be relaxed, it can not get the original dwarf contents.
13091 The variety only modify function call for reading in the section. */
13092
13093 static bfd_byte *
13094 nds32_elf_get_relocated_section_contents (bfd *abfd,
13095 struct bfd_link_info *link_info,
13096 struct bfd_link_order *link_order,
13097 bfd_byte *data,
13098 bfd_boolean relocatable,
13099 asymbol **symbols)
13100 {
13101 bfd *input_bfd = link_order->u.indirect.section->owner;
13102 asection *input_section = link_order->u.indirect.section;
13103 long reloc_size;
13104 arelent **reloc_vector;
13105 long reloc_count;
13106
13107 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13108 if (reloc_size < 0)
13109 return NULL;
13110
13111 /* Read in the section. */
13112 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
13113 return NULL;
13114
13115 if (reloc_size == 0)
13116 return data;
13117
13118 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13119 if (reloc_vector == NULL)
13120 return NULL;
13121
13122 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13123 reloc_vector, symbols);
13124 if (reloc_count < 0)
13125 goto error_return;
13126
13127 if (reloc_count > 0)
13128 {
13129 arelent **parent;
13130 for (parent = reloc_vector; *parent != NULL; parent++)
13131 {
13132 char *error_message = NULL;
13133 asymbol *symbol;
13134 bfd_reloc_status_type r;
13135
13136 symbol = *(*parent)->sym_ptr_ptr;
13137 if (symbol->section && discarded_section (symbol->section))
13138 {
13139 bfd_vma off;
13140 static reloc_howto_type none_howto
13141 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13142 "unused", FALSE, 0, 0, FALSE);
13143
13144 off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13145 input_section);
13146 _bfd_clear_contents ((*parent)->howto, input_bfd,
13147 input_section, data, off);
13148 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13149 (*parent)->addend = 0;
13150 (*parent)->howto = &none_howto;
13151 r = bfd_reloc_ok;
13152 }
13153 else
13154 r = bfd_perform_relocation (input_bfd, *parent, data,
13155 input_section,
13156 relocatable ? abfd : NULL,
13157 &error_message);
13158
13159 if (relocatable)
13160 {
13161 asection *os = input_section->output_section;
13162
13163 /* A partial link, so keep the relocs. */
13164 os->orelocation[os->reloc_count] = *parent;
13165 os->reloc_count++;
13166 }
13167
13168 if (r != bfd_reloc_ok)
13169 {
13170 switch (r)
13171 {
13172 case bfd_reloc_undefined:
13173 (*link_info->callbacks->undefined_symbol)
13174 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13175 input_bfd, input_section, (*parent)->address, TRUE);
13176 break;
13177 case bfd_reloc_dangerous:
13178 BFD_ASSERT (error_message != NULL);
13179 (*link_info->callbacks->reloc_dangerous)
13180 (link_info, error_message,
13181 input_bfd, input_section, (*parent)->address);
13182 break;
13183 case bfd_reloc_overflow:
13184 (*link_info->callbacks->reloc_overflow)
13185 (link_info, NULL,
13186 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13187 (*parent)->howto->name, (*parent)->addend,
13188 input_bfd, input_section, (*parent)->address);
13189 break;
13190 case bfd_reloc_outofrange:
13191 /* PR ld/13730:
13192 This error can result when processing some partially
13193 complete binaries. Do not abort, but issue an error
13194 message instead. */
13195 link_info->callbacks->einfo
13196 /* xgettext:c-format */
13197 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13198 abfd, input_section, * parent);
13199 goto error_return;
13200
13201 default:
13202 abort ();
13203 break;
13204 }
13205 }
13206 }
13207 }
13208
13209 free (reloc_vector);
13210 return data;
13211
13212 error_return:
13213 free (reloc_vector);
13214 return NULL;
13215 }
13216
13217 /* Check target symbol. */
13218
13219 static bfd_boolean
13220 nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13221 {
13222 if (!sym || !sym->name || sym->name[0] != '$')
13223 return FALSE;
13224 return TRUE;
13225 }
13226
13227 /* nds32 find maybe function sym. Ignore target special symbol
13228 first, and then go the general function. */
13229
13230 static bfd_size_type
13231 nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13232 bfd_vma *code_off)
13233 {
13234 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13235 return 0;
13236
13237 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13238 }
13239 \f
13240
13241 /* Do TLS model conversion. */
13242
13243 typedef struct relax_group_list_t
13244 {
13245 Elf_Internal_Rela *relo;
13246 struct relax_group_list_t *next;
13247 struct relax_group_list_t *next_sibling;
13248 int id;
13249 } relax_group_list_t;
13250
13251 int
13252 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13253
13254 int
13255 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13256
13257 void
13258 dump_chain (relax_group_list_t *pHead);
13259
13260 int
13261 list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13262 {
13263 relax_group_list_t *pNext = pHead;
13264
13265 /* Find place. */
13266 while (pNext->next)
13267 {
13268 if (pNext->next->id > (int) pElem->r_addend)
13269 break;
13270
13271 pNext = pNext->next;
13272 }
13273
13274 /* Insert node. */
13275 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13276 if (!pNew)
13277 return FALSE;
13278
13279 relax_group_list_t *tmp = pNext->next;
13280 pNext->next = pNew;
13281
13282 pNew->id = pElem->r_addend;
13283 pNew->relo = pElem;
13284 pNew->next = tmp;
13285 pNew->next_sibling = NULL;
13286
13287 return TRUE;
13288 }
13289
13290 int
13291 list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13292 {
13293 relax_group_list_t *pNext = pNode;
13294
13295 /* Find place. */
13296 while (pNext->next_sibling)
13297 {
13298 pNext = pNext->next_sibling;
13299 }
13300
13301 /* Insert node. */
13302 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13303 if (!pNew)
13304 return FALSE;
13305
13306 relax_group_list_t *tmp = pNext->next_sibling;
13307 pNext->next_sibling = pNew;
13308
13309 pNew->id = -1;
13310 pNew->relo = pElem;
13311 pNew->next = NULL;
13312 pNew->next_sibling = tmp;
13313
13314 return TRUE;
13315 }
13316
13317 void
13318 dump_chain (relax_group_list_t *pHead)
13319 {
13320 relax_group_list_t *pNext = pHead->next;
13321 while (pNext)
13322 {
13323 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13324 relax_group_list_t *pNextSib = pNext->next_sibling;
13325 while (pNextSib)
13326 {
13327 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13328 pNextSib = pNextSib->next_sibling;
13329 }
13330 pNext = pNext->next;
13331 printf("\n");
13332 }
13333 }
13334
13335 /* Check R_NDS32_RELAX_GROUP of each section.
13336 There might be multiple sections in one object file. */
13337
13338 int
13339 elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13340 {
13341 elf32_nds32_relax_group_t *relax_group_ptr =
13342 elf32_nds32_relax_group_ptr (abfd);
13343
13344 int min_id = relax_group_ptr->min_id;
13345 int max_id = relax_group_ptr->max_id;
13346
13347 Elf_Internal_Rela *rel;
13348 Elf_Internal_Rela *relend;
13349 Elf_Internal_Rela *relocs;
13350 enum elf_nds32_reloc_type rtype;
13351
13352 do
13353 {
13354 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13355 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13356 TRUE /* keep_memory */);
13357 if (relocs == NULL)
13358 break;
13359
13360 /* Check R_NDS32_RELAX_GROUP. */
13361 relend = relocs + asec->reloc_count;
13362 for (rel = relocs; rel < relend; rel++)
13363 {
13364 int id;
13365 rtype = ELF32_R_TYPE (rel->r_info);
13366 if (rtype != R_NDS32_RELAX_GROUP)
13367 continue;
13368
13369 id = rel->r_addend;
13370 if (id < min_id)
13371 min_id = id;
13372 else if (id > max_id)
13373 max_id = id;
13374 }
13375 }
13376 while (FALSE);
13377
13378 if (elf_section_data (asec)->relocs != relocs)
13379 free (relocs);
13380
13381 if ((min_id != relax_group_ptr->min_id)
13382 || (max_id != relax_group_ptr->max_id))
13383 {
13384 relax_group_ptr->count = max_id - min_id + 1;
13385 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13386 relax_group_ptr->min_id = min_id;
13387 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13388 relax_group_ptr->max_id = max_id;
13389 }
13390
13391 return relax_group_ptr->count;
13392 }
13393
13394 /* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13395 struct section_id_list_t *relax_group_section_id_list = NULL;
13396
13397 struct section_id_list_t *
13398 elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13399 {
13400 struct section_id_list_t *result = NULL;
13401 struct section_id_list_t *lst = *lst_ptr;
13402
13403 if (NULL == lst)
13404 {
13405 result = (struct section_id_list_t *) calloc
13406 (1, sizeof (struct section_id_list_t));
13407 BFD_ASSERT (result); /* Feed me. */
13408 result->id = id;
13409 *lst_ptr = result;
13410 }
13411 else
13412 {
13413 struct section_id_list_t *cur = lst;
13414 struct section_id_list_t *prv = NULL;
13415 struct section_id_list_t *sec = NULL;
13416
13417 while (cur)
13418 {
13419 if (cur->id < id)
13420 {
13421 prv = cur;
13422 cur = cur->next;
13423 continue;
13424 }
13425
13426 if (cur->id > id)
13427 {
13428 cur = NULL; /* To insert after prv. */
13429 sec = cur; /* In case prv == NULL. */
13430 }
13431
13432 break;
13433 }
13434
13435 if (NULL == cur)
13436 {
13437 /* Insert after prv. */
13438 result = (struct section_id_list_t *) calloc
13439 (1, sizeof (struct section_id_list_t));
13440 BFD_ASSERT (result); /* Feed me. */
13441 result->id = id;
13442 if (NULL != prv)
13443 {
13444 result->next = prv->next;
13445 prv->next = result;
13446 }
13447 else
13448 {
13449 *lst_ptr = result;
13450 result->next = sec;
13451 }
13452 }
13453 }
13454
13455 return result;
13456 }
13457
13458 int
13459 elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13460 {
13461 static int next_relax_group_bias = 0;
13462
13463 elf32_nds32_relax_group_t *relax_group_ptr =
13464 elf32_nds32_relax_group_ptr (abfd);
13465
13466 bfd_boolean result = TRUE;
13467 Elf_Internal_Rela *rel;
13468 Elf_Internal_Rela *relend;
13469 Elf_Internal_Rela *relocs = NULL;
13470 enum elf_nds32_reloc_type rtype;
13471 struct section_id_list_t *node = NULL;
13472 int count = 0;
13473
13474 do
13475 {
13476 if (0 == relax_group_ptr->count)
13477 break;
13478
13479 /* Check if this section has been handled. */
13480 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13481 if (NULL == node)
13482 break; /* Hit, the section id has handled. */
13483
13484 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13485 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13486 TRUE /* keep_memory */);
13487 if (relocs == NULL)
13488 {
13489 BFD_ASSERT (0); /* feed me */
13490 break;
13491 }
13492
13493 /* Allocate group id bias for this bfd! */
13494 if (0 == relax_group_ptr->init)
13495 {
13496 relax_group_ptr->bias = next_relax_group_bias;
13497 next_relax_group_bias += relax_group_ptr->count;
13498 relax_group_ptr->init = 1;
13499 }
13500
13501 /* Reorder relax group groups. */
13502 relend = relocs + asec->reloc_count;
13503 for (rel = relocs; rel < relend; rel++)
13504 {
13505 rtype = ELF32_R_TYPE(rel->r_info);
13506 if (rtype != R_NDS32_RELAX_GROUP)
13507 continue;
13508
13509 /* Change it. */
13510 rel->r_addend += relax_group_ptr->bias;
13511 /* Debugging count. */
13512 count++;
13513 }
13514 }
13515 while (FALSE);
13516
13517 if (elf_section_data (asec)->relocs != relocs)
13518 free (relocs);
13519
13520 return result;
13521 }
13522
13523 int
13524 nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13525 struct bfd_link_info *lnkinfo)
13526 {
13527 bfd_boolean result = TRUE;
13528 Elf_Internal_Rela *irel;
13529 Elf_Internal_Rela *irelend;
13530 Elf_Internal_Rela *internal_relocs;
13531 unsigned long r_symndx;
13532 enum elf_nds32_reloc_type r_type;
13533
13534 Elf_Internal_Sym *local_syms = NULL;
13535 bfd_byte *contents = NULL;
13536
13537 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13538
13539 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13540 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13541 sym_hashes = elf_sym_hashes (inbfd);
13542 sym_hashes_end =
13543 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13544 if (!elf_bad_symtab (inbfd))
13545 sym_hashes_end -= symtab_hdr->sh_info;
13546
13547 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13548 if (bfd_link_relocatable (lnkinfo))
13549 {
13550 elf32_nds32_unify_relax_group (inbfd, insec);
13551 return result;
13552 }
13553
13554 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13555 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13556 TRUE /* keep_memory */);
13557 if (internal_relocs == NULL)
13558 goto error_return;
13559
13560 irelend = internal_relocs + insec->reloc_count;
13561 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13562 irelend, R_NDS32_RELAX_ENTRY);
13563 if (irel == irelend)
13564 goto finish;
13565
13566 /* Chain/remove groups. */
13567 for (irel = internal_relocs; irel < irelend; irel++)
13568 {
13569 r_symndx = ELF32_R_SYM (irel->r_info);
13570 r_type = ELF32_R_TYPE (irel->r_info);
13571 if (r_type != R_NDS32_RELAX_GROUP)
13572 continue;
13573
13574 /* Remove it. */
13575 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13576 /* Chain it now. */
13577 if (!list_insert (&chain, irel))
13578 goto error_return;
13579 }
13580
13581 /* Collect group relocations. */
13582 /* Presume relocations are sorted. */
13583 relax_group_list_t *pNext = chain.next;
13584 while (pNext)
13585 {
13586 for (irel = internal_relocs; irel < irelend; irel++)
13587 {
13588 if (irel->r_offset == pNext->relo->r_offset)
13589 {
13590 /* Ignore Non-TLS relocation types. */
13591 r_type = ELF32_R_TYPE (irel->r_info);
13592 if ((R_NDS32_TLS_LE_HI20 > r_type)
13593 || (R_NDS32_RELAX_ENTRY == r_type))
13594 continue;
13595
13596 if (!list_insert_sibling (pNext, irel))
13597 goto error_return;
13598 }
13599 else if (irel->r_offset > pNext->relo->r_offset)
13600 {
13601 pNext = pNext->next;
13602 if (!pNext)
13603 break;
13604
13605 bfd_vma current_offset = pNext->relo->r_offset;
13606 if (irel->r_offset > current_offset)
13607 irel = internal_relocs; /* restart from head */
13608 else
13609 --irel; /* Check current irel again. */
13610 continue;
13611 }
13612 else
13613 {
13614 /* This shouldn't be reached. */
13615 }
13616 }
13617 if (pNext)
13618 pNext = pNext->next;
13619 }
13620
13621 #ifdef DUBUG_VERBOSE
13622 dump_chain(&chain);
13623 #endif
13624
13625 /* Get symbol table and section content. */
13626 if (incontents)
13627 contents = incontents;
13628 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13629 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13630 goto error_return;
13631
13632 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13633
13634 /* Convert TLS model each group if necessary. */
13635 pNext = chain.next;
13636
13637 int cur_grp_id = -1;
13638 int sethi_rt = -1;
13639 int add_rt = -1;
13640 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13641
13642 tls_type = org_tls_type = eff_tls_type = 0;
13643
13644 while (pNext)
13645 {
13646 relax_group_list_t *pNextSig = pNext->next_sibling;
13647 while (pNextSig)
13648 {
13649 struct elf_link_hash_entry *h = NULL;
13650
13651 irel = pNextSig->relo;
13652 r_symndx = ELF32_R_SYM(irel->r_info);
13653 r_type = ELF32_R_TYPE(irel->r_info);
13654
13655 if (pNext->id != cur_grp_id)
13656 {
13657 cur_grp_id = pNext->id;
13658 org_tls_type = get_tls_type (r_type, NULL);
13659 if (r_symndx >= symtab_hdr->sh_info)
13660 {
13661 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13662 while (h->root.type == bfd_link_hash_indirect
13663 || h->root.type == bfd_link_hash_warning)
13664 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13665 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13666 }
13667 else
13668 {
13669 tls_type = local_got_tls_type
13670 ? local_got_tls_type[r_symndx]
13671 : GOT_NORMAL;
13672 }
13673
13674 eff_tls_type = 1 << (fls (tls_type) - 1);
13675 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13676 }
13677
13678 if (eff_tls_type != org_tls_type)
13679 {
13680 switch (org_tls_type)
13681 {
13682 /* DESC to IEGP/IE/LE. */
13683 case GOT_TLS_DESC:
13684 switch (eff_tls_type)
13685 {
13686 case GOT_TLS_IE:
13687 switch (r_type)
13688 {
13689 case R_NDS32_TLS_DESC_HI20:
13690 irel->r_info = ELF32_R_INFO(r_symndx,
13691 R_NDS32_TLS_IE_HI20);
13692 break;
13693 case R_NDS32_TLS_DESC_LO12:
13694 irel->r_info = ELF32_R_INFO(r_symndx,
13695 R_NDS32_TLS_IE_LO12);
13696 break;
13697 case R_NDS32_TLS_DESC_ADD:
13698 {
13699 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13700 add_rt = N32_RT5 (insn);
13701 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13702 bfd_putb32 (insn, contents + irel->r_offset);
13703
13704 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13705 }
13706 break;
13707 case R_NDS32_TLS_DESC_FUNC:
13708 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13709 irel->r_info = ELF32_R_INFO(r_symndx,
13710 R_NDS32_RELAX_REMOVE);
13711 break;
13712 case R_NDS32_TLS_DESC_CALL:
13713 {
13714 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13715 REG_TP);
13716 bfd_putb32 (insn, contents + irel->r_offset);
13717
13718 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13719 }
13720 break;
13721 case R_NDS32_LOADSTORE:
13722 case R_NDS32_PTR:
13723 case R_NDS32_PTR_RESOLVED:
13724 case R_NDS32_NONE:
13725 case R_NDS32_LABEL:
13726 break;
13727 default:
13728 BFD_ASSERT(0);
13729 break;
13730 }
13731 break;
13732 case GOT_TLS_IEGP:
13733 switch (r_type)
13734 {
13735 case R_NDS32_TLS_DESC_HI20:
13736 irel->r_info = ELF32_R_INFO(r_symndx,
13737 R_NDS32_TLS_IEGP_HI20);
13738 break;
13739 case R_NDS32_TLS_DESC_LO12:
13740 irel->r_info = ELF32_R_INFO(r_symndx,
13741 R_NDS32_TLS_IEGP_LO12);
13742 break;
13743 case R_NDS32_TLS_DESC_ADD:
13744 {
13745 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13746 add_rt = N32_RT5 (insn);
13747 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13748 bfd_putb32 (insn, contents + irel->r_offset);
13749
13750 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13751 }
13752 break;
13753 case R_NDS32_TLS_DESC_FUNC:
13754 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13755 irel->r_info = ELF32_R_INFO(r_symndx,
13756 R_NDS32_RELAX_REMOVE);
13757 break;
13758 case R_NDS32_TLS_DESC_CALL:
13759 {
13760 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13761 REG_TP);
13762 bfd_putb32 (insn, contents + irel->r_offset);
13763
13764 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13765 }
13766 break;
13767 case R_NDS32_LOADSTORE:
13768 case R_NDS32_PTR:
13769 case R_NDS32_PTR_RESOLVED:
13770 case R_NDS32_NONE:
13771 case R_NDS32_LABEL:
13772 break;
13773 default:
13774 BFD_ASSERT(0);
13775 break;
13776 }
13777 break;
13778 case GOT_TLS_LE:
13779 switch (r_type)
13780 {
13781 case R_NDS32_TLS_DESC_HI20:
13782 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13783 break;
13784 case R_NDS32_TLS_DESC_LO12:
13785 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13786 break;
13787 case R_NDS32_TLS_DESC_ADD:
13788 {
13789 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13790
13791 add_rt = N32_RT5 (insn);
13792 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13793 bfd_putb32 (insn, contents + irel->r_offset);
13794
13795 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13796 }
13797 break;
13798 case R_NDS32_TLS_DESC_FUNC:
13799 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13800 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13801 break;
13802 case R_NDS32_TLS_DESC_CALL:
13803 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13804 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13805 break;
13806 case R_NDS32_LOADSTORE:
13807 case R_NDS32_PTR:
13808 case R_NDS32_PTR_RESOLVED:
13809 case R_NDS32_NONE:
13810 case R_NDS32_LABEL:
13811 break;
13812 default:
13813 BFD_ASSERT(0);
13814 break;
13815 }
13816 break;
13817 default:
13818 break;
13819 }
13820 break;
13821 /* IEGP to IE/LE. */
13822 case GOT_TLS_IEGP:
13823 switch (eff_tls_type)
13824 {
13825 case GOT_TLS_IE:
13826 switch (r_type)
13827 {
13828 case R_NDS32_TLS_IEGP_HI20:
13829 irel->r_info = ELF32_R_INFO(r_symndx,
13830 R_NDS32_TLS_IE_HI20);
13831 break;
13832 case R_NDS32_TLS_IEGP_LO12:
13833 irel->r_info = ELF32_R_INFO(r_symndx,
13834 R_NDS32_TLS_IE_LO12);
13835 break;
13836 case R_NDS32_PTR_RESOLVED:
13837 {
13838 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13839
13840 add_rt = N32_RT5 (insn);
13841 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13842 bfd_putb32 (insn, contents + irel->r_offset);
13843 }
13844 break;
13845 case R_NDS32_TLS_IEGP_LW:
13846 break;
13847 case R_NDS32_LOADSTORE:
13848 case R_NDS32_PTR:
13849 case R_NDS32_NONE:
13850 case R_NDS32_LABEL:
13851 break;
13852 default:
13853 BFD_ASSERT(0);
13854 break;
13855 }
13856 break;
13857 case GOT_TLS_LE:
13858 switch (r_type)
13859 {
13860 case R_NDS32_TLS_IEGP_HI20:
13861 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13862 break;
13863 case R_NDS32_TLS_IEGP_LO12:
13864 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13865 break;
13866 case R_NDS32_TLS_IEGP_LW:
13867 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13868 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13869 break;
13870 case R_NDS32_LOADSTORE:
13871 case R_NDS32_PTR:
13872 case R_NDS32_NONE:
13873 case R_NDS32_LABEL:
13874 case R_NDS32_PTR_RESOLVED:
13875 break;
13876 default:
13877 BFD_ASSERT(0);
13878 break;
13879 }
13880 break;
13881 default:
13882 break;
13883 }
13884 break;
13885 /* IE to LE. */
13886 case GOT_TLS_IE:
13887 switch (eff_tls_type)
13888 {
13889 case GOT_TLS_LE:
13890 switch (r_type)
13891 {
13892 case R_NDS32_TLS_IE_HI20:
13893 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13894 break;
13895 case R_NDS32_TLS_IE_LO12S2:
13896 {
13897 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13898
13899 add_rt = N32_RT5 (insn);
13900 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13901 bfd_putb32 (insn, contents + irel->r_offset);
13902
13903 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13904 }
13905 break;
13906 case R_NDS32_LOADSTORE:
13907 case R_NDS32_PTR:
13908 case R_NDS32_NONE:
13909 case R_NDS32_LABEL:
13910 break;
13911 default:
13912 BFD_ASSERT(0);
13913 break;
13914 }
13915 break;
13916 default:
13917 break;
13918 }
13919 break;
13920 default:
13921 break;
13922 }
13923 }
13924 pNextSig = pNextSig->next_sibling;
13925 }
13926
13927 #if 1
13928 pNext = pNext->next;
13929 #else
13930 while (pNext)
13931 {
13932 if (pNext->id != cur_grp_id)
13933 break;
13934 pNext = pNext->next;
13935 }
13936 #endif
13937 }
13938
13939 finish:
13940 if (incontents)
13941 contents = NULL;
13942
13943 if (elf_section_data (insec)->relocs != internal_relocs)
13944 free (internal_relocs);
13945
13946 if (elf_section_data (insec)->this_hdr.contents != contents)
13947 free (contents);
13948
13949 if (symtab_hdr->contents != (bfd_byte *) local_syms)
13950 free (local_syms);
13951
13952 if (chain.next)
13953 {
13954 pNext = chain.next;
13955 relax_group_list_t *pDel;
13956 while (pNext)
13957 {
13958 pDel = pNext;
13959 pNext = pNext->next;
13960 free (pDel);
13961 }
13962 }
13963
13964 return result;
13965
13966 error_return:
13967 result = FALSE;
13968 goto finish;
13969 }
13970
13971 /* End TLS model conversion. */
13972
13973 #define ELF_ARCH bfd_arch_nds32
13974 #define ELF_MACHINE_CODE EM_NDS32
13975 #define ELF_MAXPAGESIZE 0x1000
13976 #define ELF_TARGET_ID NDS32_ELF_DATA
13977
13978 #define TARGET_BIG_SYM nds32_elf32_be_vec
13979 #define TARGET_BIG_NAME "elf32-nds32be"
13980 #define TARGET_LITTLE_SYM nds32_elf32_le_vec
13981 #define TARGET_LITTLE_NAME "elf32-nds32le"
13982
13983 #define elf_info_to_howto nds32_info_to_howto
13984 #define elf_info_to_howto_rel nds32_info_to_howto_rel
13985
13986 #define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
13987 #define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
13988 #define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
13989 #define bfd_elf32_bfd_relax_section nds32_elf_relax_section
13990 #define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
13991
13992 #define bfd_elf32_mkobject nds32_elf_mkobject
13993 #define elf_backend_action_discarded nds32_elf_action_discarded
13994 #define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
13995 #define elf_backend_check_relocs nds32_elf_check_relocs
13996 #define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
13997 #define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
13998 #define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
13999 #define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14000 #define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14001 #define elf_backend_relocate_section nds32_elf_relocate_section
14002 #define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
14003 #define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14004 #define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14005 #define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14006 #define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14007 #define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14008 #define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14009 #define elf_backend_object_p nds32_elf_object_p
14010 #define elf_backend_final_write_processing nds32_elf_final_write_processing
14011 #define elf_backend_special_sections nds32_elf_special_sections
14012 #define elf_backend_section_flags nds32_elf_section_flags
14013 #define bfd_elf32_bfd_get_relocated_section_contents \
14014 nds32_elf_get_relocated_section_contents
14015 #define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14016 #define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
14017
14018 #define elf_backend_can_gc_sections 1
14019 #define elf_backend_can_refcount 1
14020 #define elf_backend_want_got_plt 1
14021 #define elf_backend_plt_readonly 1
14022 #define elf_backend_want_plt_sym 0
14023 #define elf_backend_got_header_size 12
14024 #define elf_backend_may_use_rel_p 1
14025 #define elf_backend_default_use_rela_p 1
14026 #define elf_backend_may_use_rela_p 1
14027 #define elf_backend_dtrel_excludes_plt 0
14028
14029 #include "elf32-target.h"
14030
14031 #undef ELF_MAXPAGESIZE
14032 #define ELF_MAXPAGESIZE 0x2000
14033
14034 #undef TARGET_BIG_SYM
14035 #define TARGET_BIG_SYM nds32_elf32_linux_be_vec
14036 #undef TARGET_BIG_NAME
14037 #define TARGET_BIG_NAME "elf32-nds32be-linux"
14038 #undef TARGET_LITTLE_SYM
14039 #define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
14040 #undef TARGET_LITTLE_NAME
14041 #define TARGET_LITTLE_NAME "elf32-nds32le-linux"
14042 #undef elf32_bed
14043 #define elf32_bed elf32_nds32_lin_bed
14044
14045 #include "elf32-target.h"
This page took 0.294526 seconds and 5 git commands to generate.