Change regcache list to be an hash map
[deliverable/binutils-gdb.git] / bfd / elf32-nds32.c
CommitLineData
35c08157 1/* NDS32-specific support for 32-bit ELF.
82704155 2 Copyright (C) 2012-2019 Free Software Foundation, Inc.
35c08157
KLC
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
1c8f6a4d 20 02110-1301, USA. */
35c08157
KLC
21
22
23#include "sysdep.h"
24#include "bfd.h"
35c08157
KLC
25#include "bfdlink.h"
26#include "libbfd.h"
27#include "elf-bfd.h"
28#include "libiberty.h"
35c08157
KLC
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
bb294208
AM
35/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
36#define OCTETS_PER_BYTE(ABFD, SEC) 1
37
35c08157
KLC
38/* Relocation HOWTO functions. */
39static bfd_reloc_status_type nds32_elf_ignore_reloc
40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43static bfd_reloc_status_type nds32_elf_hi20_reloc
44 (bfd *, arelent *, asymbol *, void *,
45 asection *, bfd *, char **);
46static bfd_reloc_status_type nds32_elf_lo12_reloc
47 (bfd *, arelent *, asymbol *, void *,
48 asection *, bfd *, char **);
49static bfd_reloc_status_type nds32_elf_generic_reloc
50 (bfd *, arelent *, asymbol *, void *,
51 asection *, bfd *, char **);
52static bfd_reloc_status_type nds32_elf_sda15_reloc
53 (bfd *, arelent *, asymbol *, void *,
54 asection *, bfd *, char **);
55
56/* Helper functions for HOWTO. */
57static 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);
35c08157
KLC
60
61/* Nds32 helper functions. */
35c08157 62static bfd_vma calculate_memory_address
fbaf61ad 63 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
0c4bd9d9
KLC
64static int nds32_get_section_contents (bfd *, asection *,
65 bfd_byte **, bfd_boolean);
35c08157
KLC
66static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67 Elf_Internal_Sym **);
35c08157
KLC
68static 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);
72static bfd_boolean nds32_fag_remove_unused_fpbase
73 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74 Elf_Internal_Rela *irelend);
75
76enum
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
fbaf61ad
NC
91#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92 && (flags) & SEC_LOAD \
93 && (flags) & SEC_READONLY)
94
35c08157
KLC
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. */
07d6d2b8
AM
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 */
35c08157
KLC
111
112/* $ta is change to $r15 (from $r25). */
113#define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
07d6d2b8
AM
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. */
35c08157
KLC
125
126#define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
07d6d2b8
AM
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 */
35c08157 132
1c8f6a4d
KLC
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
35c08157
KLC
153/* Size of small data/bss sections, used to calculate SDA_BASE. */
154static long got_size = 0;
155static int is_SDA_BASE_set = 0;
35c08157 156
35c08157
KLC
157/* Convert ELF-VER in eflags to string for debugging purpose. */
158static 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
174struct 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
fbaf61ad
NC
184enum 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
35c08157
KLC
196/* Nds32 ELF linker hash entry. */
197
198struct elf_nds32_link_hash_entry
199{
200 struct elf_link_hash_entry root;
201
202 /* Track dynamic relocs copied for this symbol. */
3bf083ed 203 struct elf_dyn_relocs *dyn_relocs;
1c8f6a4d
KLC
204
205 /* For checking relocation type. */
fbaf61ad
NC
206 enum elf_nds32_tls_type tls_type;
207
208 int offset_to_gp;
35c08157
KLC
209};
210
211/* Get the nds32 ELF linker hash table from a link_info structure. */
212
213#define FP_BASE_NAME "_FP_BASE_"
214static int check_start_export_sym = 0;
35c08157 215
1c8f6a4d
KLC
216/* The offset for executable tls relaxation. */
217#define TP_OFFSET 0x0
218
fbaf61ad
NC
219typedef struct
220{
221 int min_id;
222 int max_id;
223 int count;
224 int bias;
225 int init;
226} elf32_nds32_relax_group_t;
227
1c8f6a4d
KLC
228struct elf_nds32_obj_tdata
229{
230 struct elf_obj_tdata root;
231
232 /* tls_type for each local got entry. */
233 char *local_got_tls_type;
fbaf61ad
NC
234
235 /* GOTPLT entries for TLS descriptors. */
236 bfd_vma *local_tlsdesc_gotent;
237
238 /* for R_NDS32_RELAX_GROUP handling. */
239 elf32_nds32_relax_group_t relax_group;
240
241 unsigned int hdr_size;
242 int* offset_to_gp;
1c8f6a4d
KLC
243};
244
245#define elf_nds32_tdata(bfd) \
246 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
247
248#define elf32_nds32_local_got_tls_type(bfd) \
249 (elf_nds32_tdata (bfd)->local_got_tls_type)
250
fbaf61ad
NC
251#define elf32_nds32_local_gp_offset(bfd) \
252 (elf_nds32_tdata (bfd)->offset_to_gp)
253
1c8f6a4d
KLC
254#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
255
fbaf61ad
NC
256#define elf32_nds32_relax_group_ptr(bfd) \
257 &(elf_nds32_tdata (bfd)->relax_group)
258
1c8f6a4d
KLC
259static bfd_boolean
260nds32_elf_mkobject (bfd *abfd)
261{
262 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
263 NDS32_ELF_DATA);
264}
265
35c08157 266/* Relocations used for relocation. */
fbaf61ad
NC
267/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
268 initialize array nds32_elf_howto_table in any order. The benefit
269 is that we can add any new relocations with any numbers and don't
270 need to fill the gap by lots of EMPTY_HOWTO. */
271#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
272 [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
273
35c08157
KLC
274static reloc_howto_type nds32_elf_howto_table[] =
275{
276 /* This reloc does nothing. */
fbaf61ad
NC
277 HOWTO2 (R_NDS32_NONE, /* type */
278 0, /* rightshift */
279 2, /* size (0 = byte, 1 = short, 2 = long) */
280 32, /* bitsize */
281 FALSE, /* pc_relative */
282 0, /* bitpos */
283 complain_overflow_bitfield,/* complain_on_overflow */
284 bfd_elf_generic_reloc, /* special_function */
285 "R_NDS32_NONE", /* name */
286 FALSE, /* partial_inplace */
287 0, /* src_mask */
288 0, /* dst_mask */
289 FALSE), /* pcrel_offset */
35c08157
KLC
290
291 /* A 16 bit absolute relocation. */
fbaf61ad
NC
292 HOWTO2 (R_NDS32_16, /* type */
293 0, /* rightshift */
294 1, /* size (0 = byte, 1 = short, 2 = long) */
295 16, /* bitsize */
296 FALSE, /* pc_relative */
297 0, /* bitpos */
298 complain_overflow_bitfield,/* complain_on_overflow */
299 nds32_elf_generic_reloc,/* special_function */
300 "R_NDS32_16", /* name */
301 FALSE, /* partial_inplace */
302 0xffff, /* src_mask */
303 0xffff, /* dst_mask */
304 FALSE), /* pcrel_offset */
35c08157
KLC
305
306 /* A 32 bit absolute relocation. */
fbaf61ad
NC
307 HOWTO2 (R_NDS32_32, /* type */
308 0, /* rightshift */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
310 32, /* bitsize */
311 FALSE, /* pc_relative */
312 0, /* bitpos */
313 complain_overflow_bitfield,/* complain_on_overflow */
314 nds32_elf_generic_reloc,/* special_function */
315 "R_NDS32_32", /* name */
316 FALSE, /* partial_inplace */
317 0xffffffff, /* src_mask */
318 0xffffffff, /* dst_mask */
319 FALSE), /* pcrel_offset */
35c08157
KLC
320
321 /* A 20 bit address. */
fbaf61ad
NC
322 HOWTO2 (R_NDS32_20, /* type */
323 0, /* rightshift */
324 2, /* size (0 = byte, 1 = short, 2 = long) */
325 20, /* bitsize */
326 FALSE, /* pc_relative */
327 0, /* bitpos */
328 complain_overflow_unsigned,/* complain_on_overflow */
329 nds32_elf_generic_reloc,/* special_function */
330 "R_NDS32_20", /* name */
331 FALSE, /* partial_inplace */
332 0xfffff, /* src_mask */
333 0xfffff, /* dst_mask */
334 FALSE), /* pcrel_offset */
35c08157
KLC
335
336 /* An PC Relative 9-bit relocation, shifted by 2.
337 This reloc is complicated because relocations are relative to pc & -4.
338 i.e. branches in the right insn slot use the address of the left insn
339 slot for pc. */
fbaf61ad 340 /* It's not clear whether this should have partial_inplace set or not.
35c08157
KLC
341 Branch relaxing in the assembler can store the addend in the insn,
342 and if bfd_install_relocation gets called the addend may get added
343 again. */
fbaf61ad
NC
344 HOWTO2 (R_NDS32_9_PCREL, /* type */
345 1, /* rightshift */
346 1, /* size (0 = byte, 1 = short, 2 = long) */
347 8, /* bitsize */
348 TRUE, /* pc_relative */
349 0, /* bitpos */
350 complain_overflow_signed,/* complain_on_overflow */
351 nds32_elf_9_pcrel_reloc,/* special_function */
352 "R_NDS32_9_PCREL", /* name */
353 FALSE, /* partial_inplace */
354 0xff, /* src_mask */
355 0xff, /* dst_mask */
356 TRUE), /* pcrel_offset */
35c08157
KLC
357
358 /* A relative 15 bit relocation, right shifted by 1. */
fbaf61ad
NC
359 HOWTO2 (R_NDS32_15_PCREL, /* type */
360 1, /* rightshift */
361 2, /* size (0 = byte, 1 = short, 2 = long) */
362 14, /* bitsize */
363 TRUE, /* pc_relative */
364 0, /* bitpos */
365 complain_overflow_signed,/* complain_on_overflow */
366 bfd_elf_generic_reloc, /* special_function */
367 "R_NDS32_15_PCREL", /* name */
368 FALSE, /* partial_inplace */
369 0x3fff, /* src_mask */
370 0x3fff, /* dst_mask */
371 TRUE), /* pcrel_offset */
35c08157
KLC
372
373 /* A relative 17 bit relocation, right shifted by 1. */
fbaf61ad
NC
374 HOWTO2 (R_NDS32_17_PCREL, /* type */
375 1, /* rightshift */
376 2, /* size (0 = byte, 1 = short, 2 = long) */
377 16, /* bitsize */
378 TRUE, /* pc_relative */
379 0, /* bitpos */
380 complain_overflow_signed,/* complain_on_overflow */
381 bfd_elf_generic_reloc, /* special_function */
382 "R_NDS32_17_PCREL", /* name */
383 FALSE, /* partial_inplace */
384 0xffff, /* src_mask */
385 0xffff, /* dst_mask */
386 TRUE), /* pcrel_offset */
35c08157
KLC
387
388 /* A relative 25 bit relocation, right shifted by 1. */
fbaf61ad 389 /* It's not clear whether this should have partial_inplace set or not.
35c08157
KLC
390 Branch relaxing in the assembler can store the addend in the insn,
391 and if bfd_install_relocation gets called the addend may get added
392 again. */
fbaf61ad
NC
393 HOWTO2 (R_NDS32_25_PCREL, /* type */
394 1, /* rightshift */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
396 24, /* bitsize */
397 TRUE, /* pc_relative */
398 0, /* bitpos */
399 complain_overflow_signed,/* complain_on_overflow */
400 bfd_elf_generic_reloc, /* special_function */
401 "R_NDS32_25_PCREL", /* name */
402 FALSE, /* partial_inplace */
403 0xffffff, /* src_mask */
404 0xffffff, /* dst_mask */
405 TRUE), /* pcrel_offset */
35c08157
KLC
406
407 /* High 20 bits of address when lower 12 is or'd in. */
fbaf61ad
NC
408 HOWTO2 (R_NDS32_HI20, /* type */
409 12, /* rightshift */
410 2, /* size (0 = byte, 1 = short, 2 = long) */
411 20, /* bitsize */
412 FALSE, /* pc_relative */
413 0, /* bitpos */
414 complain_overflow_dont,/* complain_on_overflow */
415 nds32_elf_hi20_reloc, /* special_function */
416 "R_NDS32_HI20", /* name */
417 FALSE, /* partial_inplace */
418 0x000fffff, /* src_mask */
419 0x000fffff, /* dst_mask */
420 FALSE), /* pcrel_offset */
35c08157
KLC
421
422 /* Lower 12 bits of address. */
fbaf61ad
NC
423 HOWTO2 (R_NDS32_LO12S3, /* type */
424 3, /* rightshift */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
426 9, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_dont,/* complain_on_overflow */
430 nds32_elf_lo12_reloc, /* special_function */
431 "R_NDS32_LO12S3", /* name */
432 FALSE, /* partial_inplace */
433 0x000001ff, /* src_mask */
434 0x000001ff, /* dst_mask */
435 FALSE), /* pcrel_offset */
35c08157
KLC
436
437 /* Lower 12 bits of address. */
fbaf61ad
NC
438 HOWTO2 (R_NDS32_LO12S2, /* type */
439 2, /* rightshift */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
441 10, /* bitsize */
442 FALSE, /* pc_relative */
443 0, /* bitpos */
444 complain_overflow_dont,/* complain_on_overflow */
445 nds32_elf_lo12_reloc, /* special_function */
446 "R_NDS32_LO12S2", /* name */
447 FALSE, /* partial_inplace */
448 0x000003ff, /* src_mask */
449 0x000003ff, /* dst_mask */
450 FALSE), /* pcrel_offset */
35c08157
KLC
451
452 /* Lower 12 bits of address. */
fbaf61ad
NC
453 HOWTO2 (R_NDS32_LO12S1, /* type */
454 1, /* rightshift */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
456 11, /* bitsize */
457 FALSE, /* pc_relative */
458 0, /* bitpos */
459 complain_overflow_dont,/* complain_on_overflow */
460 nds32_elf_lo12_reloc, /* special_function */
461 "R_NDS32_LO12S1", /* name */
462 FALSE, /* partial_inplace */
463 0x000007ff, /* src_mask */
464 0x000007ff, /* dst_mask */
465 FALSE), /* pcrel_offset */
35c08157
KLC
466
467 /* Lower 12 bits of address. */
fbaf61ad
NC
468 HOWTO2 (R_NDS32_LO12S0, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 12, /* bitsize */
472 FALSE, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_dont,/* complain_on_overflow */
475 nds32_elf_lo12_reloc, /* special_function */
476 "R_NDS32_LO12S0", /* name */
477 FALSE, /* partial_inplace */
478 0x00000fff, /* src_mask */
479 0x00000fff, /* dst_mask */
480 FALSE), /* pcrel_offset */
35c08157
KLC
481
482 /* Small data area 15 bits offset. */
fbaf61ad
NC
483 HOWTO2 (R_NDS32_SDA15S3, /* type */
484 3, /* rightshift */
485 2, /* size (0 = byte, 1 = short, 2 = long) */
486 15, /* bitsize */
487 FALSE, /* pc_relative */
488 0, /* bitpos */
489 complain_overflow_signed,/* complain_on_overflow */
490 nds32_elf_sda15_reloc, /* special_function */
491 "R_NDS32_SDA15S3", /* name */
492 FALSE, /* partial_inplace */
493 0x00007fff, /* src_mask */
494 0x00007fff, /* dst_mask */
495 FALSE), /* pcrel_offset */
35c08157
KLC
496
497 /* Small data area 15 bits offset. */
fbaf61ad
NC
498 HOWTO2 (R_NDS32_SDA15S2, /* type */
499 2, /* rightshift */
500 2, /* size (0 = byte, 1 = short, 2 = long) */
501 15, /* bitsize */
502 FALSE, /* pc_relative */
503 0, /* bitpos */
504 complain_overflow_signed,/* complain_on_overflow */
505 nds32_elf_sda15_reloc, /* special_function */
506 "R_NDS32_SDA15S2", /* name */
507 FALSE, /* partial_inplace */
508 0x00007fff, /* src_mask */
509 0x00007fff, /* dst_mask */
510 FALSE), /* pcrel_offset */
35c08157
KLC
511
512 /* Small data area 15 bits offset. */
fbaf61ad
NC
513 HOWTO2 (R_NDS32_SDA15S1, /* type */
514 1, /* rightshift */
515 2, /* size (0 = byte, 1 = short, 2 = long) */
516 15, /* bitsize */
517 FALSE, /* pc_relative */
518 0, /* bitpos */
519 complain_overflow_signed,/* complain_on_overflow */
520 nds32_elf_sda15_reloc, /* special_function */
521 "R_NDS32_SDA15S1", /* name */
522 FALSE, /* partial_inplace */
523 0x00007fff, /* src_mask */
524 0x00007fff, /* dst_mask */
525 FALSE), /* pcrel_offset */
35c08157
KLC
526
527 /* Small data area 15 bits offset. */
fbaf61ad
NC
528 HOWTO2 (R_NDS32_SDA15S0, /* type */
529 0, /* rightshift */
530 2, /* size (0 = byte, 1 = short, 2 = long) */
531 15, /* bitsize */
532 FALSE, /* pc_relative */
533 0, /* bitpos */
534 complain_overflow_signed,/* complain_on_overflow */
535 nds32_elf_sda15_reloc, /* special_function */
536 "R_NDS32_SDA15S0", /* name */
537 FALSE, /* partial_inplace */
538 0x00007fff, /* src_mask */
539 0x00007fff, /* dst_mask */
540 FALSE), /* pcrel_offset */
541
542 /* GNU extension to record C++ vtable hierarchy */
543 HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
544 0, /* rightshift */
545 2, /* size (0 = byte, 1 = short, 2 = long) */
546 0, /* bitsize */
547 FALSE, /* pc_relative */
548 0, /* bitpos */
549 complain_overflow_dont,/* complain_on_overflow */
550 NULL, /* special_function */
551 "R_NDS32_GNU_VTINHERIT",/* name */
552 FALSE, /* partial_inplace */
553 0, /* src_mask */
554 0, /* dst_mask */
555 FALSE), /* pcrel_offset */
556
557 /* GNU extension to record C++ vtable member usage */
558 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
559 0, /* rightshift */
560 2, /* size (0 = byte, 1 = short, 2 = long) */
561 0, /* bitsize */
562 FALSE, /* pc_relative */
563 0, /* bitpos */
564 complain_overflow_dont,/* complain_on_overflow */
565 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
566 "R_NDS32_GNU_VTENTRY", /* name */
567 FALSE, /* partial_inplace */
568 0, /* src_mask */
569 0, /* dst_mask */
570 FALSE), /* pcrel_offset */
35c08157
KLC
571
572 /* A 16 bit absolute relocation. */
fbaf61ad
NC
573 HOWTO2 (R_NDS32_16_RELA, /* type */
574 0, /* rightshift */
575 1, /* size (0 = byte, 1 = short, 2 = long) */
576 16, /* bitsize */
577 FALSE, /* pc_relative */
578 0, /* bitpos */
579 complain_overflow_bitfield,/* complain_on_overflow */
580 bfd_elf_generic_reloc, /* special_function */
581 "R_NDS32_16_RELA", /* name */
582 FALSE, /* partial_inplace */
583 0xffff, /* src_mask */
584 0xffff, /* dst_mask */
585 FALSE), /* pcrel_offset */
35c08157
KLC
586
587 /* A 32 bit absolute relocation. */
fbaf61ad
NC
588 HOWTO2 (R_NDS32_32_RELA, /* type */
589 0, /* rightshift */
590 2, /* size (0 = byte, 1 = short, 2 = long) */
591 32, /* bitsize */
592 FALSE, /* pc_relative */
593 0, /* bitpos */
594 complain_overflow_bitfield,/* complain_on_overflow */
595 bfd_elf_generic_reloc, /* special_function */
596 "R_NDS32_32_RELA", /* name */
597 FALSE, /* partial_inplace */
598 0xffffffff, /* src_mask */
599 0xffffffff, /* dst_mask */
600 FALSE), /* pcrel_offset */
35c08157
KLC
601
602 /* A 20 bit address. */
fbaf61ad
NC
603 HOWTO2 (R_NDS32_20_RELA, /* type */
604 0, /* rightshift */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
606 20, /* bitsize */
607 FALSE, /* pc_relative */
608 0, /* bitpos */
609 complain_overflow_signed,/* complain_on_overflow */
610 bfd_elf_generic_reloc, /* special_function */
611 "R_NDS32_20_RELA", /* name */
612 FALSE, /* partial_inplace */
613 0xfffff, /* src_mask */
614 0xfffff, /* dst_mask */
615 FALSE), /* pcrel_offset */
616
617 HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
618 1, /* rightshift */
619 1, /* size (0 = byte, 1 = short, 2 = long) */
620 8, /* bitsize */
621 TRUE, /* pc_relative */
622 0, /* bitpos */
623 complain_overflow_signed,/* complain_on_overflow */
624 bfd_elf_generic_reloc, /* special_function */
625 "R_NDS32_9_PCREL_RELA",/* name */
626 FALSE, /* partial_inplace */
627 0xff, /* src_mask */
628 0xff, /* dst_mask */
629 TRUE), /* pcrel_offset */
35c08157
KLC
630
631 /* A relative 15 bit relocation, right shifted by 1. */
fbaf61ad
NC
632 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
633 1, /* rightshift */
634 2, /* size (0 = byte, 1 = short, 2 = long) */
635 14, /* bitsize */
636 TRUE, /* pc_relative */
637 0, /* bitpos */
638 complain_overflow_signed,/* complain_on_overflow */
639 bfd_elf_generic_reloc, /* special_function */
640 "R_NDS32_15_PCREL_RELA",/* name */
641 FALSE, /* partial_inplace */
642 0x3fff, /* src_mask */
643 0x3fff, /* dst_mask */
644 TRUE), /* pcrel_offset */
35c08157
KLC
645
646 /* A relative 17 bit relocation, right shifted by 1. */
fbaf61ad
NC
647 HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
648 1, /* rightshift */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
650 16, /* bitsize */
651 TRUE, /* pc_relative */
652 0, /* bitpos */
653 complain_overflow_signed,/* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_NDS32_17_PCREL_RELA",/* name */
656 FALSE, /* partial_inplace */
657 0xffff, /* src_mask */
658 0xffff, /* dst_mask */
659 TRUE), /* pcrel_offset */
35c08157
KLC
660
661 /* A relative 25 bit relocation, right shifted by 2. */
fbaf61ad
NC
662 HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
663 1, /* rightshift */
664 2, /* size (0 = byte, 1 = short, 2 = long) */
665 24, /* bitsize */
666 TRUE, /* pc_relative */
667 0, /* bitpos */
668 complain_overflow_signed,/* complain_on_overflow */
669 bfd_elf_generic_reloc, /* special_function */
670 "R_NDS32_25_PCREL_RELA",/* name */
671 FALSE, /* partial_inplace */
672 0xffffff, /* src_mask */
673 0xffffff, /* dst_mask */
674 TRUE), /* pcrel_offset */
35c08157
KLC
675
676 /* High 20 bits of address when lower 16 is or'd in. */
fbaf61ad
NC
677 HOWTO2 (R_NDS32_HI20_RELA, /* type */
678 12, /* rightshift */
679 2, /* size (0 = byte, 1 = short, 2 = long) */
680 20, /* bitsize */
681 FALSE, /* pc_relative */
682 0, /* bitpos */
683 complain_overflow_dont,/* complain_on_overflow */
684 bfd_elf_generic_reloc, /* special_function */
685 "R_NDS32_HI20_RELA", /* name */
686 FALSE, /* partial_inplace */
687 0x000fffff, /* src_mask */
688 0x000fffff, /* dst_mask */
689 FALSE), /* pcrel_offset */
35c08157
KLC
690
691 /* Lower 12 bits of address. */
fbaf61ad
NC
692 HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
693 3, /* rightshift */
694 2, /* size (0 = byte, 1 = short, 2 = long) */
695 9, /* bitsize */
696 FALSE, /* pc_relative */
697 0, /* bitpos */
698 complain_overflow_dont,/* complain_on_overflow */
699 bfd_elf_generic_reloc, /* special_function */
700 "R_NDS32_LO12S3_RELA", /* name */
701 FALSE, /* partial_inplace */
702 0x000001ff, /* src_mask */
703 0x000001ff, /* dst_mask */
704 FALSE), /* pcrel_offset */
35c08157
KLC
705
706 /* Lower 12 bits of address. */
fbaf61ad
NC
707 HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
708 2, /* rightshift */
709 2, /* size (0 = byte, 1 = short, 2 = long) */
710 10, /* bitsize */
711 FALSE, /* pc_relative */
712 0, /* bitpos */
713 complain_overflow_dont,/* complain_on_overflow */
714 bfd_elf_generic_reloc, /* special_function */
715 "R_NDS32_LO12S2_RELA", /* name */
716 FALSE, /* partial_inplace */
717 0x000003ff, /* src_mask */
718 0x000003ff, /* dst_mask */
719 FALSE), /* pcrel_offset */
35c08157
KLC
720
721 /* Lower 12 bits of address. */
fbaf61ad
NC
722 HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
723 1, /* rightshift */
724 2, /* size (0 = byte, 1 = short, 2 = long) */
725 11, /* bitsize */
726 FALSE, /* pc_relative */
727 0, /* bitpos */
728 complain_overflow_dont,/* complain_on_overflow */
729 bfd_elf_generic_reloc, /* special_function */
730 "R_NDS32_LO12S1_RELA", /* name */
731 FALSE, /* partial_inplace */
732 0x000007ff, /* src_mask */
733 0x000007ff, /* dst_mask */
734 FALSE), /* pcrel_offset */
35c08157
KLC
735
736 /* Lower 12 bits of address. */
fbaf61ad
NC
737 HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
738 0, /* rightshift */
739 2, /* size (0 = byte, 1 = short, 2 = long) */
740 12, /* bitsize */
741 FALSE, /* pc_relative */
742 0, /* bitpos */
743 complain_overflow_dont,/* complain_on_overflow */
744 bfd_elf_generic_reloc, /* special_function */
745 "R_NDS32_LO12S0_RELA", /* name */
746 FALSE, /* partial_inplace */
747 0x00000fff, /* src_mask */
748 0x00000fff, /* dst_mask */
749 FALSE), /* pcrel_offset */
35c08157
KLC
750
751 /* Small data area 15 bits offset. */
fbaf61ad
NC
752 HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
753 3, /* rightshift */
754 2, /* size (0 = byte, 1 = short, 2 = long) */
755 15, /* bitsize */
756 FALSE, /* pc_relative */
757 0, /* bitpos */
758 complain_overflow_signed,/* complain_on_overflow */
759 bfd_elf_generic_reloc, /* special_function */
760 "R_NDS32_SDA15S3_RELA",/* name */
761 FALSE, /* partial_inplace */
762 0x00007fff, /* src_mask */
763 0x00007fff, /* dst_mask */
764 FALSE), /* pcrel_offset */
35c08157
KLC
765
766 /* Small data area 15 bits offset. */
fbaf61ad
NC
767 HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
768 2, /* rightshift */
769 2, /* size (0 = byte, 1 = short, 2 = long) */
770 15, /* bitsize */
771 FALSE, /* pc_relative */
772 0, /* bitpos */
773 complain_overflow_signed,/* complain_on_overflow */
774 bfd_elf_generic_reloc, /* special_function */
775 "R_NDS32_SDA15S2_RELA",/* name */
776 FALSE, /* partial_inplace */
777 0x00007fff, /* src_mask */
778 0x00007fff, /* dst_mask */
779 FALSE), /* pcrel_offset */
780
781 HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
782 1, /* rightshift */
783 2, /* size (0 = byte, 1 = short, 2 = long) */
784 15, /* bitsize */
785 FALSE, /* pc_relative */
786 0, /* bitpos */
787 complain_overflow_signed,/* complain_on_overflow */
788 bfd_elf_generic_reloc, /* special_function */
789 "R_NDS32_SDA15S1_RELA",/* name */
790 FALSE, /* partial_inplace */
791 0x00007fff, /* src_mask */
792 0x00007fff, /* dst_mask */
793 FALSE), /* pcrel_offset */
794
795 HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
796 0, /* rightshift */
797 2, /* size (0 = byte, 1 = short, 2 = long) */
798 15, /* bitsize */
799 FALSE, /* pc_relative */
800 0, /* bitpos */
801 complain_overflow_signed,/* complain_on_overflow */
802 bfd_elf_generic_reloc, /* special_function */
803 "R_NDS32_SDA15S0_RELA",/* name */
804 FALSE, /* partial_inplace */
805 0x00007fff, /* src_mask */
806 0x00007fff, /* dst_mask */
807 FALSE), /* pcrel_offset */
808
809 /* GNU extension to record C++ vtable hierarchy */
810 HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
811 0, /* rightshift */
812 2, /* size (0 = byte, 1 = short, 2 = long) */
813 0, /* bitsize */
814 FALSE, /* pc_relative */
815 0, /* bitpos */
816 complain_overflow_dont,/* complain_on_overflow */
817 NULL, /* special_function */
818 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
819 FALSE, /* partial_inplace */
820 0, /* src_mask */
821 0, /* dst_mask */
822 FALSE), /* pcrel_offset */
823
824 /* GNU extension to record C++ vtable member usage */
825 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
826 0, /* rightshift */
827 2, /* size (0 = byte, 1 = short, 2 = long) */
828 0, /* bitsize */
829 FALSE, /* pc_relative */
830 0, /* bitpos */
831 complain_overflow_dont,/* complain_on_overflow */
832 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
833 "R_NDS32_RELA_GNU_VTENTRY",/* name */
834 FALSE, /* partial_inplace */
835 0, /* src_mask */
836 0, /* dst_mask */
837 FALSE), /* pcrel_offset */
35c08157
KLC
838
839 /* Like R_NDS32_20, but referring to the GOT table entry for
840 the symbol. */
fbaf61ad
NC
841 HOWTO2 (R_NDS32_GOT20, /* type */
842 0, /* rightshift */
843 2, /* size (0 = byte, 1 = short, 2 = long) */
844 20, /* bitsize */
845 FALSE, /* pc_relative */
846 0, /* bitpos */
847 complain_overflow_signed,/* complain_on_overflow */
848 bfd_elf_generic_reloc, /* special_function */
849 "R_NDS32_GOT20", /* name */
850 FALSE, /* partial_inplace */
851 0xfffff, /* src_mask */
852 0xfffff, /* dst_mask */
853 FALSE), /* pcrel_offset */
35c08157
KLC
854
855 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
856 entry for the symbol. */
fbaf61ad
NC
857 HOWTO2 (R_NDS32_25_PLTREL, /* type */
858 1, /* rightshift */
859 2, /* size (0 = byte, 1 = short, 2 = long) */
860 24, /* bitsize */
861 TRUE, /* pc_relative */
862 0, /* bitpos */
863 complain_overflow_signed,/* complain_on_overflow */
864 bfd_elf_generic_reloc, /* special_function */
865 "R_NDS32_25_PLTREL", /* name */
866 FALSE, /* partial_inplace */
867 0xffffff, /* src_mask */
868 0xffffff, /* dst_mask */
869 TRUE), /* pcrel_offset */
35c08157
KLC
870
871 /* This is used only by the dynamic linker. The symbol should exist
872 both in the object being run and in some shared library. The
873 dynamic linker copies the data addressed by the symbol from the
874 shared library into the object, because the object being
875 run has to have the data at some particular address. */
fbaf61ad
NC
876 HOWTO2 (R_NDS32_COPY, /* type */
877 0, /* rightshift */
878 2, /* size (0 = byte, 1 = short, 2 = long) */
879 32, /* bitsize */
880 FALSE, /* pc_relative */
881 0, /* bitpos */
882 complain_overflow_bitfield,/* complain_on_overflow */
883 bfd_elf_generic_reloc, /* special_function */
884 "R_NDS32_COPY", /* name */
885 FALSE, /* partial_inplace */
886 0xffffffff, /* src_mask */
887 0xffffffff, /* dst_mask */
888 FALSE), /* pcrel_offset */
35c08157
KLC
889
890 /* Like R_NDS32_20, but used when setting global offset table
891 entries. */
fbaf61ad
NC
892 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
893 0, /* rightshift */
894 2, /* size (0 = byte, 1 = short, 2 = long) */
895 32, /* bitsize */
896 FALSE, /* pc_relative */
897 0, /* bitpos */
898 complain_overflow_bitfield,/* complain_on_overflow */
899 bfd_elf_generic_reloc, /* special_function */
900 "R_NDS32_GLOB_DAT", /* name */
901 FALSE, /* partial_inplace */
902 0xffffffff, /* src_mask */
903 0xffffffff, /* dst_mask */
904 FALSE), /* pcrel_offset */
35c08157
KLC
905
906 /* Marks a procedure linkage table entry for a symbol. */
fbaf61ad
NC
907 HOWTO2 (R_NDS32_JMP_SLOT, /* type */
908 0, /* rightshift */
909 2, /* size (0 = byte, 1 = short, 2 = long) */
910 32, /* bitsize */
911 FALSE, /* pc_relative */
912 0, /* bitpos */
913 complain_overflow_bitfield,/* complain_on_overflow */
914 bfd_elf_generic_reloc, /* special_function */
915 "R_NDS32_JMP_SLOT", /* name */
916 FALSE, /* partial_inplace */
917 0xffffffff, /* src_mask */
918 0xffffffff, /* dst_mask */
919 FALSE), /* pcrel_offset */
35c08157
KLC
920
921 /* Used only by the dynamic linker. When the object is run, this
922 longword is set to the load address of the object, plus the
923 addend. */
fbaf61ad
NC
924 HOWTO2 (R_NDS32_RELATIVE, /* type */
925 0, /* rightshift */
926 2, /* size (0 = byte, 1 = short, 2 = long) */
927 32, /* bitsize */
928 FALSE, /* pc_relative */
929 0, /* bitpos */
930 complain_overflow_bitfield,/* complain_on_overflow */
931 bfd_elf_generic_reloc, /* special_function */
932 "R_NDS32_RELATIVE", /* name */
933 FALSE, /* partial_inplace */
934 0xffffffff, /* src_mask */
935 0xffffffff, /* dst_mask */
936 FALSE), /* pcrel_offset */
937
938 HOWTO2 (R_NDS32_GOTOFF, /* type */
939 0, /* rightshift */
940 2, /* size (0 = byte, 1 = short, 2 = long) */
941 20, /* bitsize */
942 FALSE, /* pc_relative */
943 0, /* bitpos */
944 complain_overflow_signed,/* complain_on_overflow */
945 bfd_elf_generic_reloc, /* special_function */
946 "R_NDS32_GOTOFF", /* name */
947 FALSE, /* partial_inplace */
948 0xfffff, /* src_mask */
949 0xfffff, /* dst_mask */
950 FALSE), /* pcrel_offset */
35c08157
KLC
951
952 /* An PC Relative 20-bit relocation used when setting PIC offset
953 table register. */
fbaf61ad
NC
954 HOWTO2 (R_NDS32_GOTPC20, /* type */
955 0, /* rightshift */
956 2, /* size (0 = byte, 1 = short, 2 = long) */
957 20, /* bitsize */
958 TRUE, /* pc_relative */
959 0, /* bitpos */
960 complain_overflow_signed,/* complain_on_overflow */
961 bfd_elf_generic_reloc, /* special_function */
962 "R_NDS32_GOTPC20", /* name */
963 FALSE, /* partial_inplace */
964 0xfffff, /* src_mask */
965 0xfffff, /* dst_mask */
966 TRUE), /* pcrel_offset */
35c08157
KLC
967
968 /* Like R_NDS32_HI20, but referring to the GOT table entry for
969 the symbol. */
fbaf61ad
NC
970 HOWTO2 (R_NDS32_GOT_HI20, /* type */
971 12, /* rightshift */
972 2, /* size (0 = byte, 1 = short, 2 = long) */
973 20, /* bitsize */
974 FALSE, /* pc_relative */
975 0, /* bitpos */
976 complain_overflow_dont,/* complain_on_overflow */
977 bfd_elf_generic_reloc, /* special_function */
978 "R_NDS32_GOT_HI20", /* name */
979 FALSE, /* partial_inplace */
980 0x000fffff, /* src_mask */
981 0x000fffff, /* dst_mask */
982 FALSE), /* pcrel_offset */
983 HOWTO2 (R_NDS32_GOT_LO12, /* type */
984 0, /* rightshift */
985 2, /* size (0 = byte, 1 = short, 2 = long) */
986 12, /* bitsize */
987 FALSE, /* pc_relative */
988 0, /* bitpos */
989 complain_overflow_dont,/* complain_on_overflow */
990 bfd_elf_generic_reloc, /* special_function */
991 "R_NDS32_GOT_LO12", /* name */
992 FALSE, /* partial_inplace */
993 0x00000fff, /* src_mask */
994 0x00000fff, /* dst_mask */
995 FALSE), /* pcrel_offset */
35c08157
KLC
996
997 /* An PC Relative relocation used when setting PIC offset table register.
998 Like R_NDS32_HI20, but referring to the GOT table entry for
999 the symbol. */
fbaf61ad
NC
1000 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
1001 12, /* rightshift */
1002 2, /* size (0 = byte, 1 = short, 2 = long) */
1003 20, /* bitsize */
1004 FALSE, /* pc_relative */
1005 0, /* bitpos */
1006 complain_overflow_dont,/* complain_on_overflow */
1007 bfd_elf_generic_reloc, /* special_function */
1008 "R_NDS32_GOTPC_HI20", /* name */
1009 FALSE, /* partial_inplace */
1010 0x000fffff, /* src_mask */
1011 0x000fffff, /* dst_mask */
1012 TRUE), /* pcrel_offset */
1013 HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
1014 0, /* rightshift */
1015 2, /* size (0 = byte, 1 = short, 2 = long) */
1016 12, /* bitsize */
1017 FALSE, /* pc_relative */
1018 0, /* bitpos */
1019 complain_overflow_dont,/* complain_on_overflow */
1020 bfd_elf_generic_reloc, /* special_function */
1021 "R_NDS32_GOTPC_LO12", /* name */
1022 FALSE, /* partial_inplace */
1023 0x00000fff, /* src_mask */
1024 0x00000fff, /* dst_mask */
1025 TRUE), /* pcrel_offset */
1026
1027 HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
1028 12, /* rightshift */
1029 2, /* size (0 = byte, 1 = short, 2 = long) */
1030 20, /* bitsize */
1031 FALSE, /* pc_relative */
1032 0, /* bitpos */
1033 complain_overflow_dont,/* complain_on_overflow */
1034 bfd_elf_generic_reloc, /* special_function */
1035 "R_NDS32_GOTOFF_HI20", /* name */
1036 FALSE, /* partial_inplace */
1037 0x000fffff, /* src_mask */
1038 0x000fffff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1040 HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
1041 0, /* rightshift */
1042 2, /* size (0 = byte, 1 = short, 2 = long) */
1043 12, /* bitsize */
1044 FALSE, /* pc_relative */
1045 0, /* bitpos */
1046 complain_overflow_dont,/* complain_on_overflow */
1047 bfd_elf_generic_reloc, /* special_function */
1048 "R_NDS32_GOTOFF_LO12", /* name */
1049 FALSE, /* partial_inplace */
1050 0x00000fff, /* src_mask */
1051 0x00000fff, /* dst_mask */
1052 FALSE), /* pcrel_offset */
35c08157
KLC
1053
1054 /* Alignment hint for relaxable instruction. This is used with
1055 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1056 in order to make next label aligned on word boundary. */
fbaf61ad
NC
1057 HOWTO2 (R_NDS32_INSN16, /* type */
1058 0, /* rightshift */
1059 2, /* size (0 = byte, 1 = short, 2 = long) */
1060 32, /* bitsize */
1061 FALSE, /* pc_relative */
1062 0, /* bitpos */
1063 complain_overflow_dont,/* complain_on_overflow */
1064 nds32_elf_ignore_reloc,/* special_function */
1065 "R_NDS32_INSN16", /* name */
1066 FALSE, /* partial_inplace */
1067 0x00000fff, /* src_mask */
1068 0x00000fff, /* dst_mask */
1069 FALSE), /* pcrel_offset */
35c08157
KLC
1070
1071 /* Alignment hint for label. */
fbaf61ad
NC
1072 HOWTO2 (R_NDS32_LABEL, /* type */
1073 0, /* rightshift */
1074 2, /* size (0 = byte, 1 = short, 2 = long) */
1075 32, /* bitsize */
1076 FALSE, /* pc_relative */
1077 0, /* bitpos */
1078 complain_overflow_dont,/* complain_on_overflow */
1079 nds32_elf_ignore_reloc,/* special_function */
1080 "R_NDS32_LABEL", /* name */
1081 FALSE, /* partial_inplace */
1082 0xffffffff, /* src_mask */
1083 0xffffffff, /* dst_mask */
1084 FALSE), /* pcrel_offset */
35c08157
KLC
1085
1086 /* Relax hint for unconditional call sequence */
fbaf61ad
NC
1087 HOWTO2 (R_NDS32_LONGCALL1, /* type */
1088 0, /* rightshift */
1089 2, /* size (0 = byte, 1 = short, 2 = long) */
1090 32, /* bitsize */
1091 FALSE, /* pc_relative */
1092 0, /* bitpos */
1093 complain_overflow_dont,/* complain_on_overflow */
1094 nds32_elf_ignore_reloc,/* special_function */
1095 "R_NDS32_LONGCALL1", /* name */
1096 FALSE, /* partial_inplace */
1097 0xffffffff, /* src_mask */
1098 0xffffffff, /* dst_mask */
1099 FALSE), /* pcrel_offset */
35c08157
KLC
1100
1101 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1102 HOWTO2 (R_NDS32_LONGCALL2, /* type */
1103 0, /* rightshift */
1104 2, /* size (0 = byte, 1 = short, 2 = long) */
1105 32, /* bitsize */
1106 FALSE, /* pc_relative */
1107 0, /* bitpos */
1108 complain_overflow_dont,/* complain_on_overflow */
1109 nds32_elf_ignore_reloc,/* special_function */
1110 "R_NDS32_LONGCALL2", /* name */
1111 FALSE, /* partial_inplace */
1112 0xffffffff, /* src_mask */
1113 0xffffffff, /* dst_mask */
1114 FALSE), /* pcrel_offset */
35c08157
KLC
1115
1116 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1117 HOWTO2 (R_NDS32_LONGCALL3, /* type */
1118 0, /* rightshift */
1119 2, /* size (0 = byte, 1 = short, 2 = long) */
1120 32, /* bitsize */
1121 FALSE, /* pc_relative */
1122 0, /* bitpos */
1123 complain_overflow_dont,/* complain_on_overflow */
1124 nds32_elf_ignore_reloc,/* special_function */
1125 "R_NDS32_LONGCALL3", /* name */
1126 FALSE, /* partial_inplace */
1127 0xffffffff, /* src_mask */
1128 0xffffffff, /* dst_mask */
1129 FALSE), /* pcrel_offset */
35c08157
KLC
1130
1131 /* Relax hint for unconditional branch sequence. */
fbaf61ad
NC
1132 HOWTO2 (R_NDS32_LONGJUMP1, /* type */
1133 0, /* rightshift */
1134 2, /* size (0 = byte, 1 = short, 2 = long) */
1135 32, /* bitsize */
1136 FALSE, /* pc_relative */
1137 0, /* bitpos */
1138 complain_overflow_dont,/* complain_on_overflow */
1139 nds32_elf_ignore_reloc,/* special_function */
1140 "R_NDS32_LONGJUMP1", /* name */
1141 FALSE, /* partial_inplace */
1142 0xffffffff, /* src_mask */
1143 0xffffffff, /* dst_mask */
1144 FALSE), /* pcrel_offset */
35c08157
KLC
1145
1146 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1147 HOWTO2 (R_NDS32_LONGJUMP2, /* type */
1148 0, /* rightshift */
1149 2, /* size (0 = byte, 1 = short, 2 = long) */
1150 32, /* bitsize */
1151 FALSE, /* pc_relative */
1152 0, /* bitpos */
1153 complain_overflow_dont,/* complain_on_overflow */
1154 nds32_elf_ignore_reloc,/* special_function */
1155 "R_NDS32_LONGJUMP2", /* name */
1156 FALSE, /* partial_inplace */
1157 0xffffffff, /* src_mask */
1158 0xffffffff, /* dst_mask */
1159 FALSE), /* pcrel_offset */
35c08157
KLC
1160
1161 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1162 HOWTO2 (R_NDS32_LONGJUMP3, /* type */
1163 0, /* rightshift */
1164 2, /* size (0 = byte, 1 = short, 2 = long) */
1165 32, /* bitsize */
1166 FALSE, /* pc_relative */
1167 0, /* bitpos */
1168 complain_overflow_dont,/* complain_on_overflow */
1169 nds32_elf_ignore_reloc,/* special_function */
1170 "R_NDS32_LONGJUMP3", /* name */
1171 FALSE, /* partial_inplace */
1172 0xffffffff, /* src_mask */
1173 0xffffffff, /* dst_mask */
1174 FALSE), /* pcrel_offset */
35c08157
KLC
1175
1176 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1177 HOWTO2 (R_NDS32_LOADSTORE, /* type */
1178 0, /* rightshift */
1179 2, /* size (0 = byte, 1 = short, 2 = long) */
1180 32, /* bitsize */
1181 FALSE, /* pc_relative */
1182 0, /* bitpos */
1183 complain_overflow_dont,/* complain_on_overflow */
1184 nds32_elf_ignore_reloc,/* special_function */
1185 "R_NDS32_LOADSTORE", /* name */
1186 FALSE, /* partial_inplace */
1187 0xffffffff, /* src_mask */
1188 0xffffffff, /* dst_mask */
1189 FALSE), /* pcrel_offset */
35c08157
KLC
1190
1191 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1192 HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
1193 0, /* rightshift */
1194 1, /* size (0 = byte, 1 = short, 2 = long) */
1195 16, /* bitsize */
1196 FALSE, /* pc_relative */
1197 0, /* bitpos */
1198 complain_overflow_dont,/* complain_on_overflow */
1199 nds32_elf_ignore_reloc,/* special_function */
1200 "R_NDS32_9_FIXED_RELA",/* name */
1201 FALSE, /* partial_inplace */
1202 0x000000ff, /* src_mask */
1203 0x000000ff, /* dst_mask */
1204 FALSE), /* pcrel_offset */
35c08157
KLC
1205
1206 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1207 HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
1208 0, /* rightshift */
1209 2, /* size (0 = byte, 1 = short, 2 = long) */
1210 32, /* bitsize */
1211 FALSE, /* pc_relative */
1212 0, /* bitpos */
1213 complain_overflow_dont,/* complain_on_overflow */
1214 nds32_elf_ignore_reloc,/* special_function */
1215 "R_NDS32_15_FIXED_RELA",/* name */
1216 FALSE, /* partial_inplace */
1217 0x00003fff, /* src_mask */
1218 0x00003fff, /* dst_mask */
1219 FALSE), /* pcrel_offset */
35c08157
KLC
1220
1221 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1222 HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
1223 0, /* rightshift */
1224 2, /* size (0 = byte, 1 = short, 2 = long) */
1225 32, /* bitsize */
1226 FALSE, /* pc_relative */
1227 0, /* bitpos */
1228 complain_overflow_dont,/* complain_on_overflow */
1229 nds32_elf_ignore_reloc,/* special_function */
1230 "R_NDS32_17_FIXED_RELA",/* name */
1231 FALSE, /* partial_inplace */
1232 0x0000ffff, /* src_mask */
1233 0x0000ffff, /* dst_mask */
1234 FALSE), /* pcrel_offset */
35c08157
KLC
1235
1236 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1237 HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
1238 0, /* rightshift */
1239 2, /* size (0 = byte, 1 = short, 2 = long) */
1240 32, /* bitsize */
1241 FALSE, /* pc_relative */
1242 0, /* bitpos */
1243 complain_overflow_dont,/* complain_on_overflow */
1244 nds32_elf_ignore_reloc,/* special_function */
1245 "R_NDS32_25_FIXED_RELA",/* name */
1246 FALSE, /* partial_inplace */
1247 0x00ffffff, /* src_mask */
1248 0x00ffffff, /* dst_mask */
1249 FALSE), /* pcrel_offset */
35c08157
KLC
1250
1251 /* High 20 bits of PLT symbol offset relative to PC. */
fbaf61ad
NC
1252 HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
1253 12, /* rightshift */
1254 2, /* size (0 = byte, 1 = short, 2 = long) */
1255 20, /* bitsize */
1256 FALSE, /* pc_relative */
1257 0, /* bitpos */
1258 complain_overflow_dont,/* complain_on_overflow */
1259 bfd_elf_generic_reloc, /* special_function */
1260 "R_NDS32_PLTREL_HI20", /* name */
1261 FALSE, /* partial_inplace */
1262 0x000fffff, /* src_mask */
1263 0x000fffff, /* dst_mask */
1264 FALSE), /* pcrel_offset */
35c08157
KLC
1265
1266 /* Low 12 bits of PLT symbol offset relative to PC. */
fbaf61ad
NC
1267 HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
1268 0, /* rightshift */
1269 2, /* size (0 = byte, 1 = short, 2 = long) */
1270 12, /* bitsize */
1271 FALSE, /* pc_relative */
1272 0, /* bitpos */
1273 complain_overflow_dont,/* complain_on_overflow */
1274 bfd_elf_generic_reloc, /* special_function */
1275 "R_NDS32_PLTREL_LO12", /* name */
1276 FALSE, /* partial_inplace */
1277 0x00000fff, /* src_mask */
1278 0x00000fff, /* dst_mask */
1279 FALSE), /* pcrel_offset */
35c08157
KLC
1280
1281 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1282 HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
1283 12, /* rightshift */
1284 2, /* size (0 = byte, 1 = short, 2 = long) */
1285 20, /* bitsize */
1286 FALSE, /* pc_relative */
1287 0, /* bitpos */
1288 complain_overflow_dont,/* complain_on_overflow */
1289 bfd_elf_generic_reloc, /* special_function */
1290 "R_NDS32_PLT_GOTREL_HI20",/* name */
1291 FALSE, /* partial_inplace */
1292 0x000fffff, /* src_mask */
1293 0x000fffff, /* dst_mask */
1294 FALSE), /* pcrel_offset */
35c08157
KLC
1295
1296 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1297 HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
1298 0, /* rightshift */
1299 2, /* size (0 = byte, 1 = short, 2 = long) */
1300 12, /* bitsize */
1301 FALSE, /* pc_relative */
1302 0, /* bitpos */
1303 complain_overflow_dont,/* complain_on_overflow */
1304 bfd_elf_generic_reloc, /* special_function */
1305 "R_NDS32_PLT_GOTREL_LO12",/* name */
1306 FALSE, /* partial_inplace */
1307 0x00000fff, /* src_mask */
1308 0x00000fff, /* dst_mask */
1309 FALSE), /* pcrel_offset */
35c08157
KLC
1310
1311 /* Small data area 12 bits offset. */
fbaf61ad
NC
1312 HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
1313 2, /* rightshift */
1314 2, /* size (0 = byte, 1 = short, 2 = long) */
1315 12, /* bitsize */
1316 FALSE, /* pc_relative */
1317 0, /* bitpos */
1318 complain_overflow_signed,/* complain_on_overflow */
1319 bfd_elf_generic_reloc, /* special_function */
1320 "R_NDS32_SDA12S2_DP_RELA",/* name */
1321 FALSE, /* partial_inplace */
1322 0x00000fff, /* src_mask */
1323 0x00000fff, /* dst_mask */
1324 FALSE), /* pcrel_offset */
35c08157
KLC
1325
1326 /* Small data area 12 bits offset. */
fbaf61ad
NC
1327 HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
1328 2, /* rightshift */
1329 2, /* size (0 = byte, 1 = short, 2 = long) */
1330 12, /* bitsize */
1331 FALSE, /* pc_relative */
1332 0, /* bitpos */
1333 complain_overflow_signed,/* complain_on_overflow */
1334 bfd_elf_generic_reloc, /* special_function */
1335 "R_NDS32_SDA12S2_SP_RELA",/* name */
1336 FALSE, /* partial_inplace */
1337 0x00000fff, /* src_mask */
1338 0x00000fff, /* dst_mask */
1339 FALSE), /* pcrel_offset */
35c08157
KLC
1340 /* Lower 12 bits of address. */
1341
fbaf61ad
NC
1342 HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
1343 2, /* rightshift */
1344 2, /* size (0 = byte, 1 = short, 2 = long) */
1345 10, /* bitsize */
1346 FALSE, /* pc_relative */
1347 0, /* bitpos */
1348 complain_overflow_dont,/* complain_on_overflow */
1349 bfd_elf_generic_reloc, /* special_function */
1350 "R_NDS32_LO12S2_DP_RELA",/* name */
1351 FALSE, /* partial_inplace */
1352 0x000003ff, /* src_mask */
1353 0x000003ff, /* dst_mask */
1354 FALSE), /* pcrel_offset */
35c08157
KLC
1355
1356 /* Lower 12 bits of address. */
fbaf61ad
NC
1357 HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
1358 2, /* rightshift */
1359 2, /* size (0 = byte, 1 = short, 2 = long) */
1360 10, /* bitsize */
1361 FALSE, /* pc_relative */
1362 0, /* bitpos */
1363 complain_overflow_dont,/* complain_on_overflow */
1364 bfd_elf_generic_reloc, /* special_function */
1365 "R_NDS32_LO12S2_SP_RELA",/* name */
1366 FALSE, /* partial_inplace */
1367 0x000003ff, /* src_mask */
1368 0x000003ff, /* dst_mask */
1369 FALSE), /* pcrel_offset */
35c08157 1370 /* Lower 12 bits of address. Special identity for or case. */
fbaf61ad
NC
1371 HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
1372 0, /* rightshift */
1373 2, /* size (0 = byte, 1 = short, 2 = long) */
1374 12, /* bitsize */
1375 FALSE, /* pc_relative */
1376 0, /* bitpos */
1377 complain_overflow_dont,/* complain_on_overflow */
1378 bfd_elf_generic_reloc, /* special_function */
1379 "R_NDS32_LO12S0_ORI_RELA",/* name */
1380 FALSE, /* partial_inplace */
1381 0x00000fff, /* src_mask */
1382 0x00000fff, /* dst_mask */
1383 FALSE), /* pcrel_offset */
35c08157 1384 /* Small data area 19 bits offset. */
fbaf61ad
NC
1385 HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
1386 3, /* rightshift */
1387 2, /* size (0 = byte, 1 = short, 2 = long) */
1388 16, /* bitsize */
1389 FALSE, /* pc_relative */
1390 0, /* bitpos */
1391 complain_overflow_signed,/* complain_on_overflow */
1392 bfd_elf_generic_reloc, /* special_function */
1393 "R_NDS32_SDA16S3_RELA",/* name */
1394 FALSE, /* partial_inplace */
1395 0x0000ffff, /* src_mask */
1396 0x0000ffff, /* dst_mask */
1397 FALSE), /* pcrel_offset */
35c08157
KLC
1398
1399 /* Small data area 15 bits offset. */
fbaf61ad
NC
1400 HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
1401 2, /* rightshift */
1402 2, /* size (0 = byte, 1 = short, 2 = long) */
1403 17, /* bitsize */
1404 FALSE, /* pc_relative */
1405 0, /* bitpos */
1406 complain_overflow_signed,/* complain_on_overflow */
1407 bfd_elf_generic_reloc, /* special_function */
1408 "R_NDS32_SDA17S2_RELA",/* name */
1409 FALSE, /* partial_inplace */
1410 0x0001ffff, /* src_mask */
1411 0x0001ffff, /* dst_mask */
1412 FALSE), /* pcrel_offset */
1413
1414 HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
1415 1, /* rightshift */
1416 2, /* size (0 = byte, 1 = short, 2 = long) */
1417 18, /* bitsize */
1418 FALSE, /* pc_relative */
1419 0, /* bitpos */
1420 complain_overflow_signed,/* complain_on_overflow */
1421 bfd_elf_generic_reloc, /* special_function */
1422 "R_NDS32_SDA18S1_RELA",/* name */
1423 FALSE, /* partial_inplace */
1424 0x0003ffff, /* src_mask */
1425 0x0003ffff, /* dst_mask */
1426 FALSE), /* pcrel_offset */
1427
1428 HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
1429 0, /* rightshift */
1430 2, /* size (0 = byte, 1 = short, 2 = long) */
1431 19, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
1434 complain_overflow_signed,/* complain_on_overflow */
1435 bfd_elf_generic_reloc, /* special_function */
1436 "R_NDS32_SDA19S0_RELA",/* name */
1437 FALSE, /* partial_inplace */
1438 0x0007ffff, /* src_mask */
1439 0x0007ffff, /* dst_mask */
1440 FALSE), /* pcrel_offset */
1441 HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
1442 0, /* rightshift */
1443 0, /* size (0 = byte, 1 = short, 2 = long) */
1444 8, /* bitsize */
1445 FALSE, /* pc_relative */
1446 0, /* bitpos */
1447 complain_overflow_dont,/* complain_on_overflow */
1448 nds32_elf_ignore_reloc,/* special_function */
1449 "R_NDS32_DWARF2_OP1_RELA",/* name */
1450 FALSE, /* partial_inplace */
1451 0xff, /* src_mask */
1452 0xff, /* dst_mask */
1453 FALSE), /* pcrel_offset */
1454 HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
1455 0, /* rightshift */
1456 1, /* size (0 = byte, 1 = short, 2 = long) */
1457 16, /* bitsize */
1458 FALSE, /* pc_relative */
1459 0, /* bitpos */
1460 complain_overflow_dont,/* complain_on_overflow */
1461 nds32_elf_ignore_reloc,/* special_function */
1462 "R_NDS32_DWARF2_OP2_RELA",/* name */
1463 FALSE, /* partial_inplace */
1464 0xffff, /* src_mask */
1465 0xffff, /* dst_mask */
1466 FALSE), /* pcrel_offset */
1467 HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
1468 0, /* rightshift */
1469 2, /* size (0 = byte, 1 = short, 2 = long) */
1470 32, /* bitsize */
1471 FALSE, /* pc_relative */
1472 0, /* bitpos */
1473 complain_overflow_dont,/* complain_on_overflow */
1474 nds32_elf_ignore_reloc,/* special_function */
1475 "R_NDS32_DWARF2_LEB_RELA",/* name */
1476 FALSE, /* partial_inplace */
1477 0xffffffff, /* src_mask */
1478 0xffffffff, /* dst_mask */
1479 FALSE), /* pcrel_offset */
1480 HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
1481 0, /* rightshift */
1482 1, /* size (0 = byte, 1 = short, 2 = long) */
1483 16, /* bitsize */
1484 FALSE, /* pc_relative */
1485 0, /* bitpos */
1486 complain_overflow_dont,/* complain_on_overflow */
1487 nds32_elf_ignore_reloc,/* special_function */
1488 "R_NDS32_UPDATE_TA_RELA",/* name */
1489 FALSE, /* partial_inplace */
1490 0xffff, /* src_mask */
1491 0xffff, /* dst_mask */
1492 FALSE), /* pcrel_offset */
35c08157
KLC
1493 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1494 entry for the symbol. */
fbaf61ad
NC
1495 HOWTO2 (R_NDS32_9_PLTREL, /* type */
1496 1, /* rightshift */
1497 1, /* size (0 = byte, 1 = short, 2 = long) */
1498 8, /* bitsize */
1499 TRUE, /* pc_relative */
1500 0, /* bitpos */
1501 complain_overflow_signed,/* complain_on_overflow */
1502 bfd_elf_generic_reloc, /* special_function */
1503 "R_NDS32_9_PLTREL", /* name */
1504 FALSE, /* partial_inplace */
1505 0xff, /* src_mask */
1506 0xff, /* dst_mask */
1507 TRUE), /* pcrel_offset */
35c08157 1508 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1509 HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
1510 0, /* rightshift */
1511 2, /* size (0 = byte, 1 = short, 2 = long) */
1512 20, /* bitsize */
1513 FALSE, /* pc_relative */
1514 0, /* bitpos */
1515 complain_overflow_dont,/* complain_on_overflow */
1516 bfd_elf_generic_reloc, /* special_function */
1517 "R_NDS32_PLT_GOTREL_LO20",/* name */
1518 FALSE, /* partial_inplace */
1519 0x000fffff, /* src_mask */
1520 0x000fffff, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1522 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1523 HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
1524 0, /* rightshift */
1525 2, /* size (0 = byte, 1 = short, 2 = long) */
1526 15, /* bitsize */
1527 FALSE, /* pc_relative */
1528 0, /* bitpos */
1529 complain_overflow_dont,/* complain_on_overflow */
1530 bfd_elf_generic_reloc, /* special_function */
1531 "R_NDS32_PLT_GOTREL_LO15",/* name */
1532 FALSE, /* partial_inplace */
1533 0x00007fff, /* src_mask */
1534 0x00007fff, /* dst_mask */
1535 FALSE), /* pcrel_offset */
35c08157 1536 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1537 HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
1538 0, /* rightshift */
1539 2, /* size (0 = byte, 1 = short, 2 = long) */
1540 19, /* bitsize */
1541 FALSE, /* pc_relative */
1542 0, /* bitpos */
1543 complain_overflow_dont,/* complain_on_overflow */
1544 bfd_elf_generic_reloc, /* special_function */
1545 "R_NDS32_PLT_GOTREL_LO19",/* name */
1546 FALSE, /* partial_inplace */
1547 0x0007ffff, /* src_mask */
1548 0x0007ffff, /* dst_mask */
1549 FALSE), /* pcrel_offset */
1550 HOWTO2 (R_NDS32_GOT_LO15, /* type */
1551 0, /* rightshift */
1552 2, /* size (0 = byte, 1 = short, 2 = long) */
1553 15, /* bitsize */
1554 FALSE, /* pc_relative */
1555 0, /* bitpos */
1556 complain_overflow_dont,/* complain_on_overflow */
1557 bfd_elf_generic_reloc, /* special_function */
1558 "R_NDS32_GOT_LO15", /* name */
1559 FALSE, /* partial_inplace */
1560 0x00007fff, /* src_mask */
1561 0x00007fff, /* dst_mask */
1562 FALSE), /* pcrel_offset */
1563 HOWTO2 (R_NDS32_GOT_LO19, /* type */
1564 0, /* rightshift */
1565 2, /* size (0 = byte, 1 = short, 2 = long) */
1566 19, /* bitsize */
1567 FALSE, /* pc_relative */
1568 0, /* bitpos */
1569 complain_overflow_dont,/* complain_on_overflow */
1570 bfd_elf_generic_reloc, /* special_function */
1571 "R_NDS32_GOT_LO19", /* name */
1572 FALSE, /* partial_inplace */
1573 0x0007ffff, /* src_mask */
1574 0x0007ffff, /* dst_mask */
1575 FALSE), /* pcrel_offset */
1576 HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
1577 0, /* rightshift */
1578 2, /* size (0 = byte, 1 = short, 2 = long) */
1579 15, /* bitsize */
1580 FALSE, /* pc_relative */
1581 0, /* bitpos */
1582 complain_overflow_dont,/* complain_on_overflow */
1583 bfd_elf_generic_reloc, /* special_function */
1584 "R_NDS32_GOTOFF_LO15", /* name */
1585 FALSE, /* partial_inplace */
1586 0x00007fff, /* src_mask */
1587 0x00007fff, /* dst_mask */
1588 FALSE), /* pcrel_offset */
1589 HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
1590 0, /* rightshift */
1591 2, /* size (0 = byte, 1 = short, 2 = long) */
1592 19, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont,/* complain_on_overflow */
1596 bfd_elf_generic_reloc, /* special_function */
1597 "R_NDS32_GOTOFF_LO19", /* name */
1598 FALSE, /* partial_inplace */
1599 0x0007ffff, /* src_mask */
1600 0x0007ffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
35c08157 1602 /* GOT 15 bits offset. */
fbaf61ad
NC
1603 HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
1604 2, /* rightshift */
1605 2, /* size (0 = byte, 1 = short, 2 = long) */
1606 15, /* bitsize */
1607 FALSE, /* pc_relative */
1608 0, /* bitpos */
1609 complain_overflow_signed,/* complain_on_overflow */
1610 bfd_elf_generic_reloc, /* special_function */
1611 "R_NDS32_GOT15S2_RELA",/* name */
1612 FALSE, /* partial_inplace */
1613 0x00007fff, /* src_mask */
1614 0x00007fff, /* dst_mask */
1615 FALSE), /* pcrel_offset */
35c08157 1616 /* GOT 17 bits offset. */
fbaf61ad
NC
1617 HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
1618 2, /* rightshift */
1619 2, /* size (0 = byte, 1 = short, 2 = long) */
1620 17, /* bitsize */
1621 FALSE, /* pc_relative */
1622 0, /* bitpos */
1623 complain_overflow_signed,/* complain_on_overflow */
1624 bfd_elf_generic_reloc, /* special_function */
1625 "R_NDS32_GOT17S2_RELA",/* name */
1626 FALSE, /* partial_inplace */
1627 0x0001ffff, /* src_mask */
1628 0x0001ffff, /* dst_mask */
1629 FALSE), /* pcrel_offset */
35c08157 1630 /* A 5 bit address. */
fbaf61ad
NC
1631 HOWTO2 (R_NDS32_5_RELA, /* type */
1632 0, /* rightshift */
1633 1, /* size (0 = byte, 1 = short, 2 = long) */
1634 5, /* bitsize */
1635 FALSE, /* pc_relative */
1636 0, /* bitpos */
1637 complain_overflow_signed,/* complain_on_overflow */
1638 bfd_elf_generic_reloc, /* special_function */
1639 "R_NDS32_5_RELA", /* name */
1640 FALSE, /* partial_inplace */
1641 0x1f, /* src_mask */
1642 0x1f, /* dst_mask */
1643 FALSE), /* pcrel_offset */
1644 HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
1645 1, /* rightshift */
1646 1, /* size (0 = byte, 1 = short, 2 = long) */
1647 9, /* bitsize */
1648 TRUE, /* pc_relative */
1649 0, /* bitpos */
1650 complain_overflow_unsigned,/* complain_on_overflow */
1651 bfd_elf_generic_reloc, /* special_function */
1652 "R_NDS32_10_UPCREL_RELA",/* name */
1653 FALSE, /* partial_inplace */
1654 0x1ff, /* src_mask */
1655 0x1ff, /* dst_mask */
1656 TRUE), /* pcrel_offset */
1657 HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
1658 2, /* rightshift */
1659 1, /* size (0 = byte, 1 = short, 2 = long) */
1660 7, /* bitsize */
1661 FALSE, /* pc_relative */
1662 0, /* bitpos */
1663 complain_overflow_unsigned,/* complain_on_overflow */
1664 bfd_elf_generic_reloc, /* special_function */
1665 "R_NDS32_SDA_FP7U2_RELA",/* name */
1666 FALSE, /* partial_inplace */
1667 0x0000007f, /* src_mask */
1668 0x0000007f, /* dst_mask */
1669 FALSE), /* pcrel_offset */
1670 HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
1671 1, /* rightshift */
1672 2, /* size (0 = byte, 1 = short, 2 = long) */
1673 8, /* bitsize */
1674 TRUE, /* pc_relative */
1675 0, /* bitpos */
1676 complain_overflow_signed,/* complain_on_overflow */
1677 bfd_elf_generic_reloc, /* special_function */
1678 "R_NDS32_WORD_9_PCREL_RELA",/* name */
1679 FALSE, /* partial_inplace */
1680 0xff, /* src_mask */
1681 0xff, /* dst_mask */
1682 TRUE), /* pcrel_offset */
1683 HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
1684 1, /* rightshift */
1685 2, /* size (0 = byte, 1 = short, 2 = long) */
1686 24, /* bitsize */
1687 FALSE, /* pc_relative */
1688 0, /* bitpos */
1689 complain_overflow_dont,/* complain_on_overflow */
1690 bfd_elf_generic_reloc, /* special_function */
1691 "R_NDS32_25_ABS_RELA", /* name */
1692 FALSE, /* partial_inplace */
1693 0xffffff, /* src_mask */
1694 0xffffff, /* dst_mask */
1695 FALSE), /* pcrel_offset */
35c08157
KLC
1696
1697 /* A relative 17 bit relocation for ifc, right shifted by 1. */
fbaf61ad
NC
1698 HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
1699 1, /* rightshift */
1700 2, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
1702 TRUE, /* pc_relative */
1703 0, /* bitpos */
1704 complain_overflow_signed,/* complain_on_overflow */
1705 bfd_elf_generic_reloc, /* special_function */
1706 "R_NDS32_17IFC_PCREL_RELA",/* name */
1707 FALSE, /* partial_inplace */
1708 0xffff, /* src_mask */
1709 0xffff, /* dst_mask */
1710 TRUE), /* pcrel_offset */
35c08157
KLC
1711
1712 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
fbaf61ad
NC
1713 HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
1714 1, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 9, /* bitsize */
1717 TRUE, /* pc_relative */
1718 0, /* bitpos */
1719 complain_overflow_unsigned,/* complain_on_overflow */
1720 bfd_elf_generic_reloc, /* special_function */
1721 "R_NDS32_10IFCU_PCREL_RELA",/* name */
1722 FALSE, /* partial_inplace */
1723 0x1ff, /* src_mask */
1724 0x1ff, /* dst_mask */
1725 TRUE), /* pcrel_offset */
1726
1727 /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
1728 HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
1729 12, /* rightshift */
1730 2, /* size (0 = byte, 1 = short, 2 = long) */
1731 20, /* bitsize */
1732 FALSE, /* pc_relative */
1733 0, /* bitpos */
1734 complain_overflow_dont,/* complain_on_overflow */
1735 bfd_elf_generic_reloc, /* special_function */
1736 "R_NDS32_TLS_LE_HI20", /* name */
1737 FALSE, /* partial_inplace */
1738 0x000fffff, /* src_mask */
1739 0x000fffff, /* dst_mask */
1740 FALSE), /* pcrel_offset */
1741
1742 HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
1743 0, /* rightshift */
1744 2, /* size (0 = byte, 1 = short, 2 = long) */
1745 12, /* bitsize */
1746 FALSE, /* pc_relative */
1747 0, /* bitpos */
1748 complain_overflow_dont,/* complain_on_overflow */
1749 bfd_elf_generic_reloc, /* special_function */
1750 "R_NDS32_TLS_LE_LO12", /* name */
1751 FALSE, /* partial_inplace */
1752 0x00000fff, /* src_mask */
1753 0x00000fff, /* dst_mask */
1754 FALSE), /* pcrel_offset */
1755
1756 /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
1757 HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
1758 12, /* rightshift */
1759 2, /* size (0 = byte, 1 = short, 2 = long) */
1760 20, /* bitsize */
1761 FALSE, /* pc_relative */
1762 0, /* bitpos */
1763 complain_overflow_dont,/* complain_on_overflow */
1764 bfd_elf_generic_reloc, /* special_function */
1765 "R_NDS32_TLS_IE_HI20", /* name */
1766 FALSE, /* partial_inplace */
1767 0x000fffff, /* src_mask */
1768 0x000fffff, /* dst_mask */
1769 FALSE), /* pcrel_offset */
1770
1771 HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
1772 2, /* rightshift */
1773 2, /* size (0 = byte, 1 = short, 2 = long) */
1774 10, /* bitsize */
1775 FALSE, /* pc_relative */
1776 0, /* bitpos */
1777 complain_overflow_dont,/* complain_on_overflow */
1778 bfd_elf_generic_reloc, /* special_function */
1779 "R_NDS32_TLS_IE_LO12S2",/* name */
1780 FALSE, /* partial_inplace */
1781 0x000003ff, /* src_mask */
1782 0x000003ff, /* dst_mask */
1783 FALSE), /* pcrel_offset */
1784
1785 /* TLS LE TP offset relocation */
1786 HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
1787 0, /* rightshift */
1788 2, /* size (0 = byte, 1 = short, 2 = long) */
1789 32, /* bitsize */
1790 FALSE, /* pc_relative */
1791 0, /* bitpos */
1792 complain_overflow_bitfield,/* complain_on_overflow */
1793 bfd_elf_generic_reloc, /* special_function */
1794 "R_NDS32_TLS_TPOFF", /* name */
1795 FALSE, /* partial_inplace */
1796 0xffffffff, /* src_mask */
1797 0xffffffff, /* dst_mask */
1798 FALSE), /* pcrel_offset */
1799
1c8f6a4d 1800 /* A 20 bit address. */
fbaf61ad
NC
1801 HOWTO2 (R_NDS32_TLS_LE_20, /* type */
1802 0, /* rightshift */
1803 2, /* size (0 = byte, 1 = short, 2 = long) */
1804 20, /* bitsize */
1805 FALSE, /* pc_relative */
1806 0, /* bitpos */
1807 complain_overflow_signed,/* complain_on_overflow */
1808 bfd_elf_generic_reloc, /* special_function */
1809 "R_NDS32_TLS_LE_20", /* name */
1810 FALSE, /* partial_inplace */
1811 0xfffff, /* src_mask */
1812 0xfffff, /* dst_mask */
1813 FALSE), /* pcrel_offset */
1814
1815 HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
1816 0, /* rightshift */
1817 2, /* size (0 = byte, 1 = short, 2 = long) */
1818 15, /* bitsize */
1819 FALSE, /* pc_relative */
1820 0, /* bitpos */
1821 complain_overflow_signed,/* complain_on_overflow */
1822 bfd_elf_generic_reloc, /* special_function */
1823 "R_NDS32_TLS_LE_15S0", /* name */
1824 FALSE, /* partial_inplace */
1825 0x7fff, /* src_mask */
1826 0x7fff, /* dst_mask */
1827 FALSE), /* pcrel_offset */
1828 HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
1829 1, /* rightshift */
1830 2, /* size (0 = byte, 1 = short, 2 = long) */
1831 15, /* bitsize */
1832 FALSE, /* pc_relative */
1833 0, /* bitpos */
1834 complain_overflow_signed,/* complain_on_overflow */
1835 bfd_elf_generic_reloc, /* special_function */
1836 "R_NDS32_TLS_LE_15S1", /* name */
1837 FALSE, /* partial_inplace */
1838 0x7fff, /* src_mask */
1839 0x7fff, /* dst_mask */
1840 FALSE), /* pcrel_offset */
1841 HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
1842 2, /* rightshift */
1843 2, /* size (0 = byte, 1 = short, 2 = long) */
1844 15, /* bitsize */
1845 FALSE, /* pc_relative */
1846 0, /* bitpos */
1847 complain_overflow_signed,/* complain_on_overflow */
1848 bfd_elf_generic_reloc, /* special_function */
1849 "R_NDS32_TLS_LE_15S2", /* name */
1850 FALSE, /* partial_inplace */
1851 0x7fff, /* src_mask */
1852 0x7fff, /* dst_mask */
1853 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1854
1855 /* Relax hint for unconditional call sequence */
fbaf61ad
NC
1856 HOWTO2 (R_NDS32_LONGCALL4, /* type */
1857 0, /* rightshift */
1858 2, /* size (0 = byte, 1 = short, 2 = long) */
1859 32, /* bitsize */
1860 FALSE, /* pc_relative */
1861 0, /* bitpos */
1862 complain_overflow_dont,/* complain_on_overflow */
1863 nds32_elf_ignore_reloc,/* special_function */
1864 "R_NDS32_LONGCALL4", /* name */
1865 FALSE, /* partial_inplace */
1866 0xffffffff, /* src_mask */
1867 0xffffffff, /* dst_mask */
1868 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1869
1870 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1871 HOWTO2 (R_NDS32_LONGCALL5, /* type */
1872 0, /* rightshift */
1873 2, /* size (0 = byte, 1 = short, 2 = long) */
1874 32, /* bitsize */
1875 FALSE, /* pc_relative */
1876 0, /* bitpos */
1877 complain_overflow_dont,/* complain_on_overflow */
1878 nds32_elf_ignore_reloc,/* special_function */
1879 "R_NDS32_LONGCALL5", /* name */
1880 FALSE, /* partial_inplace */
1881 0xffffffff, /* src_mask */
1882 0xffffffff, /* dst_mask */
1883 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1884
1885 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1886 HOWTO2 (R_NDS32_LONGCALL6, /* type */
1887 0, /* rightshift */
1888 2, /* size (0 = byte, 1 = short, 2 = long) */
1889 32, /* bitsize */
1890 FALSE, /* pc_relative */
1891 0, /* bitpos */
1892 complain_overflow_dont,/* complain_on_overflow */
1893 nds32_elf_ignore_reloc,/* special_function */
1894 "R_NDS32_LONGCALL6", /* name */
1895 FALSE, /* partial_inplace */
1896 0xffffffff, /* src_mask */
1897 0xffffffff, /* dst_mask */
1898 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1899
1900 /* Relax hint for unconditional branch sequence. */
fbaf61ad
NC
1901 HOWTO2 (R_NDS32_LONGJUMP4, /* type */
1902 0, /* rightshift */
1903 2, /* size (0 = byte, 1 = short, 2 = long) */
1904 32, /* bitsize */
1905 FALSE, /* pc_relative */
1906 0, /* bitpos */
1907 complain_overflow_dont,/* complain_on_overflow */
1908 nds32_elf_ignore_reloc,/* special_function */
1909 "R_NDS32_LONGJUMP4", /* name */
1910 FALSE, /* partial_inplace */
1911 0xffffffff, /* src_mask */
1912 0xffffffff, /* dst_mask */
1913 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1914
1915 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1916 HOWTO2 (R_NDS32_LONGJUMP5, /* type */
1917 0, /* rightshift */
1918 2, /* size (0 = byte, 1 = short, 2 = long) */
1919 32, /* bitsize */
1920 FALSE, /* pc_relative */
1921 0, /* bitpos */
1922 complain_overflow_dont,/* complain_on_overflow */
1923 nds32_elf_ignore_reloc,/* special_function */
1924 "R_NDS32_LONGJUMP5", /* name */
1925 FALSE, /* partial_inplace */
1926 0xffffffff, /* src_mask */
1927 0xffffffff, /* dst_mask */
1928 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1929
1930 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1931 HOWTO2 (R_NDS32_LONGJUMP6, /* type */
1932 0, /* rightshift */
1933 2, /* size (0 = byte, 1 = short, 2 = long) */
1934 32, /* bitsize */
1935 FALSE, /* pc_relative */
1936 0, /* bitpos */
1937 complain_overflow_dont,/* complain_on_overflow */
1938 nds32_elf_ignore_reloc,/* special_function */
1939 "R_NDS32_LONGJUMP6", /* name */
1940 FALSE, /* partial_inplace */
1941 0xffffffff, /* src_mask */
1942 0xffffffff, /* dst_mask */
1943 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1944
1945 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1946 HOWTO2 (R_NDS32_LONGJUMP7, /* type */
1947 0, /* rightshift */
1948 2, /* size (0 = byte, 1 = short, 2 = long) */
1949 32, /* bitsize */
1950 FALSE, /* pc_relative */
1951 0, /* bitpos */
1952 complain_overflow_dont,/* complain_on_overflow */
1953 nds32_elf_ignore_reloc,/* special_function */
1954 "R_NDS32_LONGJUMP7", /* name */
1955 FALSE, /* partial_inplace */
1956 0xffffffff, /* src_mask */
1957 0xffffffff, /* dst_mask */
1958 FALSE), /* pcrel_offset */
1959
1960 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1961 0, /* rightshift */
1962 2, /* size (0 = byte, 1 = short, 2 = long) */
1963 12, /* bitsize */
1964 FALSE, /* pc_relative */
1965 0, /* bitpos */
1966 complain_overflow_dont,/* complain_on_overflow */
1967 bfd_elf_generic_reloc, /* special_function */
1968 "R_NDS32_TLS_IE_LO12", /* name */
1969 FALSE, /* partial_inplace */
1970 0x00000fff, /* src_mask */
1971 0x00000fff, /* dst_mask */
1972 FALSE), /* pcrel_offset */
1973
1974 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1975 entry for the symbol. */
1976 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1977 12, /* rightshift */
1978 2, /* size (0 = byte, 1 = short, 2 = long) */
1979 20, /* bitsize */
1980 FALSE, /* pc_relative */
1981 0, /* bitpos */
1982 complain_overflow_dont,/* complain_on_overflow */
1983 bfd_elf_generic_reloc, /* special_function */
1984 "R_NDS32_TLS_IEGP_HI20",/* name */
1985 FALSE, /* partial_inplace */
1986 0x000fffff, /* src_mask */
1987 0x000fffff, /* dst_mask */
1988 FALSE), /* pcrel_offset */
1989
1990 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1991 0, /* rightshift */
1992 2, /* size (0 = byte, 1 = short, 2 = long) */
1993 12, /* bitsize */
1994 FALSE, /* pc_relative */
1995 0, /* bitpos */
1996 complain_overflow_dont,/* complain_on_overflow */
1997 bfd_elf_generic_reloc, /* special_function */
1998 "R_NDS32_TLS_IEGP_LO12",/* name */
1999 FALSE, /* partial_inplace */
2000 0x00000fff, /* src_mask */
2001 0x00000fff, /* dst_mask */
2002 FALSE), /* pcrel_offset */
2003
2004 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2005 2, /* rightshift */
2006 2, /* size (0 = byte, 1 = short, 2 = long) */
2007 10, /* bitsize */
2008 FALSE, /* pc_relative */
2009 0, /* bitpos */
2010 complain_overflow_dont,/* complain_on_overflow */
2011 bfd_elf_generic_reloc, /* special_function */
2012 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2013 FALSE, /* partial_inplace */
2014 0x000003ff, /* src_mask */
2015 0x000003ff, /* dst_mask */
2016 FALSE), /* pcrel_offset */
2017
2018 /* TLS description relocation */
2019 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2020 12, /* rightshift */
2021 2, /* size (0 = byte, 1 = short, 2 = long) */
2022 20, /* bitsize */
2023 FALSE, /* pc_relative */
2024 0, /* bitpos */
2025 complain_overflow_dont,/* complain_on_overflow */
2026 nds32_elf_hi20_reloc, /* special_function */
2027 "R_NDS32_TLS_DESC_HI20",/* name */
2028 FALSE, /* partial_inplace */
2029 0x000fffff, /* src_mask */
2030 0x000fffff, /* dst_mask */
2031 FALSE), /* pcrel_offset */
2032
2033 /* TLS GD/LD description offset high part. */
2034 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2035 12, /* rightshift */
2036 2, /* size (0 = byte, 1 = short, 2 = long) */
2037 20, /* bitsize */
2038 FALSE, /* pc_relative */
2039 0, /* bitpos */
2040 complain_overflow_dont,/* complain_on_overflow */
2041 nds32_elf_hi20_reloc, /* special_function */
2042 "R_NDS32_TLS_DESC_HI20",/* name */
2043 FALSE, /* partial_inplace */
2044 0x000fffff, /* src_mask */
2045 0x000fffff, /* dst_mask */
2046 FALSE), /* pcrel_offset */
2047
2048 /* TLS GD/LD description offset low part. */
2049 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2050 0, /* rightshift */
2051 2, /* size (0 = byte, 1 = short, 2 = long) */
2052 12, /* bitsize */
2053 FALSE, /* pc_relative */
2054 0, /* bitpos */
2055 complain_overflow_dont,/* complain_on_overflow */
2056 nds32_elf_lo12_reloc, /* special_function */
2057 "R_NDS32_TLS_DESC_LO12",/* name */
2058 FALSE, /* partial_inplace */
2059 0x00000fff, /* src_mask */
2060 0x00000fff, /* dst_mask */
2061 FALSE), /* pcrel_offset */
2062
2063 /* TLS GD/LD description offset set (movi). */
2064 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2065 0, /* rightshift */
2066 2, /* size (0 = byte, 1 = short, 2 = long) */
2067 20, /* bitsize */
2068 FALSE, /* pc_relative */
2069 0, /* bitpos */
2070 complain_overflow_signed,/* complain_on_overflow */
2071 bfd_elf_generic_reloc, /* special_function */
2072 "R_NDS32_TLS_DESC_20", /* name */
2073 FALSE, /* partial_inplace */
2074 0x000fffff, /* src_mask */
2075 0x000fffff, /* dst_mask */
2076 FALSE), /* pcrel_offset */
2077
2078 /* TLS GD/LD description offset set (lwi.gp). */
2079 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2080 2, /* rightshift */
2081 2, /* size (0 = byte, 1 = short, 2 = long) */
2082 17, /* bitsize */
2083 FALSE, /* pc_relative */
2084 0, /* bitpos */
2085 complain_overflow_signed,/* complain_on_overflow */
2086 bfd_elf_generic_reloc, /* special_function */
2087 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2088 FALSE, /* partial_inplace */
2089 0x0001ffff, /* src_mask */
2090 0x0001ffff, /* dst_mask */
2091 FALSE), /* pcrel_offset */
35c08157
KLC
2092};
2093
2094/* Relocations used for relaxation. */
fbaf61ad
NC
2095#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2096 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2097
2098static reloc_howto_type nds32_elf_relax_howto_table[] = {
2099 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2100 0, /* rightshift */
2101 2, /* size (0 = byte, 1 = short, 2 = long) */
2102 32, /* bitsize */
2103 FALSE, /* pc_relative */
2104 0, /* bitpos */
2105 complain_overflow_dont,/* complain_on_overflow */
2106 nds32_elf_ignore_reloc,/* special_function */
2107 "R_NDS32_RELAX_ENTRY", /* name */
2108 FALSE, /* partial_inplace */
2109 0xffffffff, /* src_mask */
2110 0xffffffff, /* dst_mask */
2111 FALSE), /* pcrel_offset */
2112 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2113 0, /* rightshift */
2114 2, /* size (0 = byte, 1 = short, 2 = long) */
2115 32, /* bitsize */
2116 FALSE, /* pc_relative */
2117 0, /* bitpos */
2118 complain_overflow_dont,/* complain_on_overflow */
2119 nds32_elf_ignore_reloc,/* special_function */
2120 "R_NDS32_GOT_SUFF", /* name */
2121 FALSE, /* partial_inplace */
2122 0xffffffff, /* src_mask */
2123 0xffffffff, /* dst_mask */
2124 FALSE), /* pcrel_offset */
2125 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2126 0, /* rightshift */
2127 2, /* size (0 = byte, 1 = short, 2 = long) */
2128 32, /* bitsize */
2129 FALSE, /* pc_relative */
2130 0, /* bitpos */
2131 complain_overflow_bitfield,/* complain_on_overflow */
2132 nds32_elf_ignore_reloc,/* special_function */
2133 "R_NDS32_GOTOFF_SUFF", /* name */
2134 FALSE, /* partial_inplace */
2135 0xffffffff, /* src_mask */
2136 0xffffffff, /* dst_mask */
2137 FALSE), /* pcrel_offset */
2138 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2139 0, /* rightshift */
2140 2, /* size (0 = byte, 1 = short, 2 = long) */
2141 32, /* bitsize */
2142 FALSE, /* pc_relative */
2143 0, /* bitpos */
2144 complain_overflow_dont,/* complain_on_overflow */
2145 nds32_elf_ignore_reloc,/* special_function */
2146 "R_NDS32_PLT_GOT_SUFF",/* name */
2147 FALSE, /* partial_inplace */
2148 0xffffffff, /* src_mask */
2149 0xffffffff, /* dst_mask */
2150 FALSE), /* pcrel_offset */
2151 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2152 0, /* rightshift */
2153 2, /* size (0 = byte, 1 = short, 2 = long) */
2154 32, /* bitsize */
2155 FALSE, /* pc_relative */
2156 0, /* bitpos */
2157 complain_overflow_dont,/* complain_on_overflow */
2158 nds32_elf_ignore_reloc,/* special_function */
2159 "R_NDS32_MULCALL_SUFF",/* name */
2160 FALSE, /* partial_inplace */
2161 0xffffffff, /* src_mask */
2162 0xffffffff, /* dst_mask */
2163 FALSE), /* pcrel_offset */
2164 HOWTO3 (R_NDS32_PTR, /* type */
2165 0, /* rightshift */
2166 2, /* size (0 = byte, 1 = short, 2 = long) */
2167 32, /* bitsize */
2168 FALSE, /* pc_relative */
2169 0, /* bitpos */
2170 complain_overflow_dont,/* complain_on_overflow */
2171 nds32_elf_ignore_reloc,/* special_function */
2172 "R_NDS32_PTR", /* name */
2173 FALSE, /* partial_inplace */
2174 0xffffffff, /* src_mask */
2175 0xffffffff, /* dst_mask */
2176 FALSE), /* pcrel_offset */
2177 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2178 0, /* rightshift */
2179 2, /* size (0 = byte, 1 = short, 2 = long) */
2180 32, /* bitsize */
2181 FALSE, /* pc_relative */
2182 0, /* bitpos */
2183 complain_overflow_dont,/* complain_on_overflow */
2184 nds32_elf_ignore_reloc,/* special_function */
2185 "R_NDS32_PTR_COUNT", /* name */
2186 FALSE, /* partial_inplace */
2187 0xffffffff, /* src_mask */
2188 0xffffffff, /* dst_mask */
2189 FALSE), /* pcrel_offset */
2190 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2191 0, /* rightshift */
2192 2, /* size (0 = byte, 1 = short, 2 = long) */
2193 32, /* bitsize */
2194 FALSE, /* pc_relative */
2195 0, /* bitpos */
2196 complain_overflow_dont,/* complain_on_overflow */
2197 nds32_elf_ignore_reloc,/* special_function */
2198 "R_NDS32_PTR_RESOLVED",/* name */
2199 FALSE, /* partial_inplace */
2200 0xffffffff, /* src_mask */
2201 0xffffffff, /* dst_mask */
2202 FALSE), /* pcrel_offset */
2203 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2204 0, /* rightshift */
2205 2, /* size (0 = byte, 1 = short, 2 = long) */
2206 32, /* bitsize */
2207 FALSE, /* pc_relative */
2208 0, /* bitpos */
2209 complain_overflow_dont,/* complain_on_overflow */
2210 nds32_elf_ignore_reloc,/* special_function */
2211 "R_NDS32_PLTBLOCK", /* name */
2212 FALSE, /* partial_inplace */
2213 0xffffffff, /* src_mask */
2214 0xffffffff, /* dst_mask */
2215 FALSE), /* pcrel_offset */
2216 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2217 0, /* rightshift */
2218 2, /* size (0 = byte, 1 = short, 2 = long) */
2219 32, /* bitsize */
2220 FALSE, /* pc_relative */
2221 0, /* bitpos */
2222 complain_overflow_dont,/* complain_on_overflow */
2223 nds32_elf_ignore_reloc,/* special_function */
2224 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2225 FALSE, /* partial_inplace */
2226 0xffffffff, /* src_mask */
2227 0xffffffff, /* dst_mask */
2228 FALSE), /* pcrel_offset */
2229 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2230 0, /* rightshift */
2231 2, /* size (0 = byte, 1 = short, 2 = long) */
2232 32, /* bitsize */
2233 FALSE, /* pc_relative */
2234 0, /* bitpos */
2235 complain_overflow_dont,/* complain_on_overflow */
2236 nds32_elf_ignore_reloc,/* special_function */
2237 "R_NDS32_RELAX_REGION_END",/* name */
2238 FALSE, /* partial_inplace */
2239 0xffffffff, /* src_mask */
2240 0xffffffff, /* dst_mask */
2241 FALSE), /* pcrel_offset */
2242 HOWTO3 (R_NDS32_MINUEND, /* type */
2243 0, /* rightshift */
2244 2, /* size (0 = byte, 1 = short, 2 = long) */
2245 32, /* bitsize */
2246 FALSE, /* pc_relative */
2247 0, /* bitpos */
2248 complain_overflow_dont,/* complain_on_overflow */
2249 nds32_elf_ignore_reloc,/* special_function */
2250 "R_NDS32_MINUEND", /* name */
2251 FALSE, /* partial_inplace */
2252 0xffffffff, /* src_mask */
2253 0xffffffff, /* dst_mask */
2254 FALSE), /* pcrel_offset */
2255 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2256 0, /* rightshift */
2257 2, /* size (0 = byte, 1 = short, 2 = long) */
2258 32, /* bitsize */
2259 FALSE, /* pc_relative */
2260 0, /* bitpos */
2261 complain_overflow_dont,/* complain_on_overflow */
2262 nds32_elf_ignore_reloc,/* special_function */
2263 "R_NDS32_SUBTRAHEND", /* name */
2264 FALSE, /* partial_inplace */
2265 0xffffffff, /* src_mask */
2266 0xffffffff, /* dst_mask */
2267 FALSE), /* pcrel_offset */
2268 HOWTO3 (R_NDS32_DIFF8, /* type */
2269 0, /* rightshift */
2270 0, /* size (0 = byte, 1 = short, 2 = long) */
2271 8, /* bitsize */
2272 FALSE, /* pc_relative */
2273 0, /* bitpos */
2274 complain_overflow_dont,/* complain_on_overflow */
2275 nds32_elf_ignore_reloc,/* special_function */
2276 "R_NDS32_DIFF8", /* name */
2277 FALSE, /* partial_inplace */
2278 0x000000ff, /* src_mask */
2279 0x000000ff, /* dst_mask */
2280 FALSE), /* pcrel_offset */
2281 HOWTO3 (R_NDS32_DIFF16, /* type */
2282 0, /* rightshift */
2283 1, /* size (0 = byte, 1 = short, 2 = long) */
2284 16, /* bitsize */
2285 FALSE, /* pc_relative */
2286 0, /* bitpos */
2287 complain_overflow_dont,/* complain_on_overflow */
2288 nds32_elf_ignore_reloc,/* special_function */
2289 "R_NDS32_DIFF16", /* name */
2290 FALSE, /* partial_inplace */
2291 0x0000ffff, /* src_mask */
2292 0x0000ffff, /* dst_mask */
2293 FALSE), /* pcrel_offset */
2294 HOWTO3 (R_NDS32_DIFF32, /* type */
2295 0, /* rightshift */
2296 2, /* size (0 = byte, 1 = short, 2 = long) */
2297 32, /* bitsize */
2298 FALSE, /* pc_relative */
2299 0, /* bitpos */
2300 complain_overflow_dont,/* complain_on_overflow */
2301 nds32_elf_ignore_reloc,/* special_function */
2302 "R_NDS32_DIFF32", /* name */
2303 FALSE, /* partial_inplace */
2304 0xffffffff, /* src_mask */
2305 0xffffffff, /* dst_mask */
2306 FALSE), /* pcrel_offset */
2307 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2308 0, /* rightshift */
2309 0, /* size (0 = byte, 1 = short, 2 = long) */
2310 0, /* bitsize */
2311 FALSE, /* pc_relative */
2312 0, /* bitpos */
2313 complain_overflow_dont,/* complain_on_overflow */
2314 nds32_elf_ignore_reloc,/* special_function */
2315 "R_NDS32_DIFF_ULEB128",/* name */
2316 FALSE, /* partial_inplace */
2317 0xffffffff, /* src_mask */
2318 0xffffffff, /* dst_mask */
2319 FALSE), /* pcrel_offset */
2320 HOWTO3 (R_NDS32_DATA, /* type */
2321 0, /* rightshift */
2322 2, /* size (0 = byte, 1 = short, 2 = long) */
2323 32, /* bitsize */
2324 FALSE, /* pc_relative */
2325 0, /* bitpos */
2326 complain_overflow_dont,/* complain_on_overflow */
2327 nds32_elf_ignore_reloc,/* special_function */
2328 "R_NDS32_DATA", /* name */
2329 FALSE, /* partial_inplace */
2330 0xffffffff, /* src_mask */
2331 0xffffffff, /* dst_mask */
2332 FALSE), /* pcrel_offset */
2333 HOWTO3 (R_NDS32_TRAN, /* type */
2334 0, /* rightshift */
2335 2, /* size (0 = byte, 1 = short, 2 = long) */
2336 32, /* bitsize */
2337 FALSE, /* pc_relative */
2338 0, /* bitpos */
2339 complain_overflow_dont,/* complain_on_overflow */
2340 nds32_elf_ignore_reloc,/* special_function */
2341 "R_NDS32_TRAN", /* name */
2342 FALSE, /* partial_inplace */
2343 0xffffffff, /* src_mask */
2344 0xffffffff, /* dst_mask */
2345 FALSE), /* pcrel_offset */
2346 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2347 0, /* rightshift */
2348 2, /* size (0 = byte, 1 = short, 2 = long) */
2349 32, /* bitsize */
2350 FALSE, /* pc_relative */
2351 0, /* bitpos */
2352 complain_overflow_dont,/* complain_on_overflow */
2353 nds32_elf_ignore_reloc,/* special_function */
2354 "R_NDS32_TLS_LE_ADD", /* name */
2355 FALSE, /* partial_inplace */
2356 0xffffffff, /* src_mask */
2357 0xffffffff, /* dst_mask */
2358 FALSE), /* pcrel_offset */
2359 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2360 0, /* rightshift */
2361 2, /* size (0 = byte, 1 = short, 2 = long) */
2362 32, /* bitsize */
2363 FALSE, /* pc_relative */
2364 0, /* bitpos */
2365 complain_overflow_dont,/* complain_on_overflow */
2366 nds32_elf_ignore_reloc,/* special_function */
2367 "R_NDS32_TLS_LE_LS", /* name */
2368 FALSE, /* partial_inplace */
2369 0xffffffff, /* src_mask */
2370 0xffffffff, /* dst_mask */
2371 FALSE), /* pcrel_offset */
2372 HOWTO3 (R_NDS32_EMPTY, /* type */
2373 0, /* rightshift */
2374 2, /* size (0 = byte, 1 = short, 2 = long) */
2375 32, /* bitsize */
2376 FALSE, /* pc_relative */
2377 0, /* bitpos */
2378 complain_overflow_dont,/* complain_on_overflow */
2379 nds32_elf_ignore_reloc,/* special_function */
2380 "R_NDS32_EMPTY", /* name */
2381 FALSE, /* partial_inplace */
2382 0xffffffff, /* src_mask */
2383 0xffffffff, /* dst_mask */
2384 FALSE), /* pcrel_offset */
2385 /* TLS GD/LD description address base addition. */
2386 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2387 0, /* rightshift */
2388 2, /* size (0 = byte, 1 = short, 2 = long) */
2389 32, /* bitsize */
2390 FALSE, /* pc_relative */
2391 0, /* bitpos */
2392 complain_overflow_dont,/* complain_on_overflow */
2393 nds32_elf_ignore_reloc,/* special_function */
2394 "R_NDS32_TLS_DESC_ADD",/* name */
2395 FALSE, /* partial_inplace */
2396 0xffffffff, /* src_mask */
2397 0xffffffff, /* dst_mask */
2398 FALSE), /* pcrel_offset */
2399 /* TLS GD/LD description function load. */
2400 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2401 0, /* rightshift */
2402 2, /* size (0 = byte, 1 = short, 2 = long) */
2403 32, /* bitsize */
2404 FALSE, /* pc_relative */
2405 0, /* bitpos */
2406 complain_overflow_dont,/* complain_on_overflow */
2407 nds32_elf_ignore_reloc,/* special_function */
2408 "R_NDS32_TLS_DESC_FUNC",/* name */
2409 FALSE, /* partial_inplace */
2410 0xffffffff, /* src_mask */
2411 0xffffffff, /* dst_mask */
2412 FALSE), /* pcrel_offset */
2413 /* TLS DESC resolve function call. */
2414 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2415 0, /* rightshift */
2416 2, /* size (0 = byte, 1 = short, 2 = long) */
2417 32, /* bitsize */
2418 FALSE, /* pc_relative */
2419 0, /* bitpos */
2420 complain_overflow_dont,/* complain_on_overflow */
2421 nds32_elf_ignore_reloc,/* special_function */
2422 "R_NDS32_TLS_DESC_CALL",/* name */
2423 FALSE, /* partial_inplace */
2424 0xffffffff, /* src_mask */
2425 0xffffffff, /* dst_mask */
2426 FALSE), /* pcrel_offset */
2427 /* TLS DESC variable access. */
2428 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2429 0, /* rightshift */
2430 2, /* size (0 = byte, 1 = short, 2 = long) */
2431 32, /* bitsize */
2432 FALSE, /* pc_relative */
2433 0, /* bitpos */
2434 complain_overflow_dont,/* complain_on_overflow */
2435 nds32_elf_ignore_reloc,/* special_function */
2436 "R_NDS32_TLS_DESC_MEM",/* name */
2437 FALSE, /* partial_inplace */
2438 0xffffffff, /* src_mask */
2439 0xffffffff, /* dst_mask */
2440 FALSE), /* pcrel_offset */
2441 /* TLS GD/LD description mark (@tlsdec). */
2442 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2443 0, /* rightshift */
2444 2, /* size (0 = byte, 1 = short, 2 = long) */
2445 32, /* bitsize */
2446 FALSE, /* pc_relative */
2447 0, /* bitpos */
2448 complain_overflow_dont,/* complain_on_overflow */
2449 nds32_elf_ignore_reloc,/* special_function */
2450 "R_NDS32_REMOVE", /* name */
2451 FALSE, /* partial_inplace */
2452 0xffffffff, /* src_mask */
2453 0xffffffff, /* dst_mask */
2454 FALSE), /* pcrel_offset */
2455 /* TLS GD/LD description mark (@tlsdec). */
2456 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2457 0, /* rightshift */
2458 2, /* size (0 = byte, 1 = short, 2 = long) */
2459 32, /* bitsize */
2460 FALSE, /* pc_relative */
2461 0, /* bitpos */
2462 complain_overflow_dont,/* complain_on_overflow */
2463 nds32_elf_ignore_reloc,/* special_function */
2464 "R_NDS32_GROUP", /* name */
2465 FALSE, /* partial_inplace */
2466 0xffffffff, /* src_mask */
2467 0xffffffff, /* dst_mask */
2468 FALSE), /* pcrel_offset */
2469 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2470 0, /* rightshift */
2471 2, /* size (0 = byte, 1 = short, 2 = long) */
2472 32, /* bitsize */
2473 FALSE, /* pc_relative */
2474 0, /* bitpos */
2475 complain_overflow_dont,/* complain_on_overflow */
2476 nds32_elf_ignore_reloc,/* special_function */
2477 "R_NDS32_TLS_IEGP_LW", /* name */
2478 FALSE, /* partial_inplace */
2479 0xffffffff, /* src_mask */
2480 0xffffffff, /* dst_mask */
2481 FALSE), /* pcrel_offset */
2482 /* LA and FLSI relaxation. */
2483 HOWTO3 (R_NDS32_LSI, /* type */
2484 0, /* rightshift */
2485 2, /* size (0 = byte, 1 = short, 2 = long) */
2486 32, /* bitsize */
2487 FALSE, /* pc_relative */
2488 0, /* bitpos */
2489 complain_overflow_dont,/* complain_on_overflow */
2490 nds32_elf_ignore_reloc,/* special_function */
2491 "R_NDS32_LSI", /* name */
2492 FALSE, /* partial_inplace */
2493 0xffffffff, /* src_mask */
2494 0xffffffff, /* dst_mask */
2495 FALSE),
35c08157
KLC
2496};
2497
fbaf61ad
NC
2498static unsigned long dl_tlsdesc_lazy_trampoline[] =
2499{
2500 0x46200000, /* sethi $r2,#0x0 */
2501 0x58210000, /* ori $r2,$r2,#0x0 */
2502 0x40217400, /* add $r2,$r2,$gp */
2503 0x04210000, /* lwi $r2,[$r2+#0x0] */
2504 0x46300000, /* sethi $r3,#0x0 */
2505 0x58318000, /* ori $r3,$r3,#0x0 */
2506 0x4031f400, /* add $r3,$r3,$gp */
2507 0x4a000800, /* jr $r2 */
2508};
2509
2510static void
2511nds32_put_trampoline (void *contents, const unsigned long *template,
2512 unsigned count)
2513{
2514 unsigned ix;
2515
2516 for (ix = 0; ix != count; ix++)
2517 {
2518 unsigned long insn = template[ix];
2519 bfd_putb32 (insn, (char *) contents + ix * 4);
2520 }
2521}
2522
35c08157
KLC
2523/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2524 This prototype is the same as qsort (). */
2525
2526void
2527nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2528 int (*compar) (const void *lhs, const void *rhs))
2529{
2530 char *ptr = (char *) base;
1c8f6a4d 2531 int i, j;
9a733151
AM
2532 char tmp[sizeof (Elf_Internal_Rela)];
2533
2534 BFD_ASSERT (size <= sizeof (tmp));
35c08157
KLC
2535
2536 /* If i is less than j, i is inserted before j.
2537
2538 |---- j ----- i --------------|
2539 \ / \ /
2540 sorted unsorted
2541 */
2542
1c8f6a4d 2543 for (i = 1; i < (int) nmemb; i++)
35c08157 2544 {
1c8f6a4d
KLC
2545 for (j = (i - 1); j >= 0; j--)
2546 if (compar (ptr + i * size, ptr + j * size) >= 0)
35c08157
KLC
2547 break;
2548
1c8f6a4d
KLC
2549 j++;
2550
35c08157 2551 if (i == j)
1c8f6a4d 2552 continue; /* i is in order. */
35c08157
KLC
2553
2554 memcpy (tmp, ptr + i * size, size);
2555 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2556 memcpy (ptr + j * size, tmp, size);
2557 }
2558}
2559
2560/* Sort relocation by r_offset.
2561
2562 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2563 algorithm. Relocations at the same r_offset must keep their order.
2564 For example, RELAX_ENTRY must be the very first relocation entry.
2565
2566 Currently, this function implements insertion-sort.
2567
2568 FIXME: If we already sort them in assembler, why bother sort them
2569 here again? */
2570
2571static int
2572compar_reloc (const void *lhs, const void *rhs)
2573{
2574 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2575 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2576
2577 if (l->r_offset > r->r_offset)
2578 return 1;
2579 else if (l->r_offset == r->r_offset)
2580 return 0;
2581 else
2582 return -1;
2583}
2584
2585/* Functions listed below are only used for old relocs.
fbaf61ad
NC
2586 nds32_elf_9_pcrel_reloc
2587 nds32_elf_do_9_pcrel_reloc
2588 nds32_elf_hi20_reloc
2589 nds32_elf_relocate_hi20
2590 nds32_elf_lo12_reloc
2591 nds32_elf_sda15_reloc
2592 nds32_elf_generic_reloc. */
35c08157
KLC
2593
2594/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2595
2596static bfd_reloc_status_type
fbaf61ad
NC
2597nds32_elf_9_pcrel_reloc (bfd * abfd,
2598 arelent * reloc_entry,
2599 asymbol * symbol,
2600 void * data,
2601 asection * input_section,
2602 bfd * output_bfd,
2603 char ** error_message ATTRIBUTE_UNUSED)
35c08157
KLC
2604{
2605 /* This part is from bfd_elf_generic_reloc. */
2606 if (output_bfd != (bfd *) NULL
2607 && (symbol->flags & BSF_SECTION_SYM) == 0
2608 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2609 {
2610 reloc_entry->address += input_section->output_offset;
2611 return bfd_reloc_ok;
2612 }
2613
2614 if (output_bfd != NULL)
2615 {
2616 /* FIXME: See bfd_perform_relocation. Is this right? */
2617 return bfd_reloc_continue;
2618 }
2619
2620 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2621 input_section,
2622 data, reloc_entry->address,
2623 symbol->section,
2624 (symbol->value
2625 + symbol->section->output_section->vma
2626 + symbol->section->output_offset),
2627 reloc_entry->addend);
2628}
2629
2630/* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2631#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2632
2633static bfd_reloc_status_type
fbaf61ad
NC
2634nds32_elf_do_9_pcrel_reloc (bfd * abfd,
2635 reloc_howto_type * howto,
2636 asection * input_section,
2637 bfd_byte * data,
2638 bfd_vma offset,
2639 asection * symbol_section ATTRIBUTE_UNUSED,
2640 bfd_vma symbol_value,
2641 bfd_vma addend)
35c08157
KLC
2642{
2643 bfd_signed_vma relocation;
2644 unsigned short x;
2645 bfd_reloc_status_type status;
2646
2647 /* Sanity check the address (offset in section). */
2648 if (offset > bfd_get_section_limit (abfd, input_section))
2649 return bfd_reloc_outofrange;
2650
2651 relocation = symbol_value + addend;
2652 /* Make it pc relative. */
2653 relocation -= (input_section->output_section->vma
2654 + input_section->output_offset);
2655 /* These jumps mask off the lower two bits of the current address
2656 before doing pcrel calculations. */
2657 relocation -= (offset & -(bfd_vma) 2);
2658
1c8f6a4d 2659 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
35c08157
KLC
2660 status = bfd_reloc_overflow;
2661 else
2662 status = bfd_reloc_ok;
2663
2664 x = bfd_getb16 (data + offset);
2665
2666 relocation >>= howto->rightshift;
2667 relocation <<= howto->bitpos;
2668 x = (x & ~howto->dst_mask)
2669 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2670
2671 bfd_putb16 ((bfd_vma) x, data + offset);
2672
2673 return status;
2674}
2675
2676/* Handle the R_NDS32_HI20_[SU]LO relocs.
2677 HI20_SLO is for the add3 and load/store with displacement instructions.
2678 HI20 is for the or3 instruction.
2679 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2680 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2681 we must add one to the high 16 bytes (which will get subtracted off when
2682 the low 16 bits are added).
2683 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2684 because there is a carry from the LO12 to the HI20. Here we just save
2685 the information we need; we do the actual relocation when we see the LO12.
2686 This code is copied from the elf32-mips.c. We also support an arbitrary
2687 number of HI20 relocs to be associated with a single LO12 reloc. The
2688 assembler sorts the relocs to ensure each HI20 immediately precedes its
2689 LO12. However if there are multiple copies, the assembler may not find
2690 the real LO12 so it picks the first one it finds. */
2691
2692struct nds32_hi20
2693{
2694 struct nds32_hi20 *next;
2695 bfd_byte *addr;
2696 bfd_vma addend;
2697};
2698
2699static struct nds32_hi20 *nds32_hi20_list;
2700
2701static bfd_reloc_status_type
fbaf61ad
NC
2702nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2703 arelent *reloc_entry,
2704 asymbol *symbol,
2705 void *data,
2706 asection *input_section,
2707 bfd *output_bfd,
2708 char **error_message ATTRIBUTE_UNUSED)
35c08157
KLC
2709{
2710 bfd_reloc_status_type ret;
2711 bfd_vma relocation;
2712 struct nds32_hi20 *n;
2713
2714 /* This part is from bfd_elf_generic_reloc.
2715 If we're relocating, and this an external symbol, we don't want
2716 to change anything. */
2717 if (output_bfd != (bfd *) NULL
2718 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2719 {
2720 reloc_entry->address += input_section->output_offset;
2721 return bfd_reloc_ok;
2722 }
2723
2724 /* Sanity check the address (offset in section). */
2725 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2726 return bfd_reloc_outofrange;
2727
2728 ret = bfd_reloc_ok;
2729 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2730 ret = bfd_reloc_undefined;
2731
2732 if (bfd_is_com_section (symbol->section))
2733 relocation = 0;
2734 else
2735 relocation = symbol->value;
2736
2737 relocation += symbol->section->output_section->vma;
2738 relocation += symbol->section->output_offset;
2739 relocation += reloc_entry->addend;
2740
2741 /* Save the information, and let LO12 do the actual relocation. */
2742 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2743 if (n == NULL)
2744 return bfd_reloc_outofrange;
2745
2746 n->addr = (bfd_byte *) data + reloc_entry->address;
2747 n->addend = relocation;
2748 n->next = nds32_hi20_list;
2749 nds32_hi20_list = n;
2750
2751 if (output_bfd != (bfd *) NULL)
2752 reloc_entry->address += input_section->output_offset;
2753
2754 return ret;
2755}
2756
2757/* Handle an NDS32 ELF HI20 reloc. */
2758
2759static void
2760nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
fbaf61ad
NC
2761 int type ATTRIBUTE_UNUSED,
2762 Elf_Internal_Rela *relhi,
2763 Elf_Internal_Rela *rello,
2764 bfd_byte *contents,
35c08157
KLC
2765 bfd_vma addend)
2766{
2767 unsigned long insn;
2768 bfd_vma addlo;
2769
2770 insn = bfd_getb32 (contents + relhi->r_offset);
2771
2772 addlo = bfd_getb32 (contents + rello->r_offset);
2773 addlo &= 0xfff;
2774
2775 addend += ((insn & 0xfffff) << 20) + addlo;
2776
2777 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2778 bfd_putb32 (insn, contents + relhi->r_offset);
2779}
2780
2781/* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2782 inplace relocation; this function exists in order to do the
2783 R_NDS32_HI20_[SU]LO relocation described above. */
2784
2785static bfd_reloc_status_type
2786nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2787 void *data, asection *input_section, bfd *output_bfd,
2788 char **error_message)
2789{
2790 /* This part is from bfd_elf_generic_reloc.
2791 If we're relocating, and this an external symbol, we don't want
2792 to change anything. */
2793 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2794 && reloc_entry->addend == 0)
2795 {
2796 reloc_entry->address += input_section->output_offset;
2797 return bfd_reloc_ok;
2798 }
2799
2800 if (nds32_hi20_list != NULL)
2801 {
2802 struct nds32_hi20 *l;
2803
2804 l = nds32_hi20_list;
2805 while (l != NULL)
2806 {
2807 unsigned long insn;
2808 unsigned long val;
2809 unsigned long vallo;
2810 struct nds32_hi20 *next;
2811
2812 /* Do the HI20 relocation. Note that we actually don't need
2813 to know anything about the LO12 itself, except where to
2814 find the low 12 bits of the addend needed by the LO12. */
2815 insn = bfd_getb32 (l->addr);
2816 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2817 vallo &= 0xfff;
2818 switch (reloc_entry->howto->type)
2819 {
2820 case R_NDS32_LO12S3:
2821 vallo <<= 3;
2822 break;
2823
2824 case R_NDS32_LO12S2:
2825 vallo <<= 2;
2826 break;
2827
2828 case R_NDS32_LO12S1:
2829 vallo <<= 1;
2830 break;
2831
2832 case R_NDS32_LO12S0:
2833 vallo <<= 0;
2834 break;
2835 }
2836
2837 val = ((insn & 0xfffff) << 12) + vallo;
2838 val += l->addend;
2839
2840 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2841 bfd_putb32 ((bfd_vma) insn, l->addr);
2842
2843 next = l->next;
2844 free (l);
2845 l = next;
2846 }
2847
2848 nds32_hi20_list = NULL;
2849 }
2850
2851 /* Now do the LO12 reloc in the usual way.
2852 ??? It would be nice to call bfd_elf_generic_reloc here,
2853 but we have partial_inplace set. bfd_elf_generic_reloc will
2854 pass the handling back to bfd_install_relocation which will install
2855 a section relative addend which is wrong. */
2856 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2857 input_section, output_bfd, error_message);
2858}
2859
2860/* Do generic partial_inplace relocation.
2861 This is a local replacement for bfd_elf_generic_reloc. */
2862
2863static bfd_reloc_status_type
2864nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2865 asymbol *symbol, void *data, asection *input_section,
2866 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2867{
2868 bfd_reloc_status_type ret;
2869 bfd_vma relocation;
2870 bfd_byte *inplace_address;
2871
2872 /* This part is from bfd_elf_generic_reloc.
2873 If we're relocating, and this an external symbol, we don't want
2874 to change anything. */
2875 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2876 && reloc_entry->addend == 0)
2877 {
2878 reloc_entry->address += input_section->output_offset;
2879 return bfd_reloc_ok;
2880 }
2881
2882 /* Now do the reloc in the usual way.
2883 ??? It would be nice to call bfd_elf_generic_reloc here,
2884 but we have partial_inplace set. bfd_elf_generic_reloc will
2885 pass the handling back to bfd_install_relocation which will install
2886 a section relative addend which is wrong. */
2887
2888 /* Sanity check the address (offset in section). */
2889 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2890 return bfd_reloc_outofrange;
2891
2892 ret = bfd_reloc_ok;
2893 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2894 ret = bfd_reloc_undefined;
2895
2896 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2897 relocation = 0;
2898 else
2899 relocation = symbol->value;
2900
2901 /* Only do this for a final link. */
2902 if (output_bfd == (bfd *) NULL)
2903 {
2904 relocation += symbol->section->output_section->vma;
2905 relocation += symbol->section->output_offset;
2906 }
2907
2908 relocation += reloc_entry->addend;
2909 switch (reloc_entry->howto->type)
2910 {
2911 case R_NDS32_LO12S3:
2912 relocation >>= 3;
2913 break;
2914
2915 case R_NDS32_LO12S2:
2916 relocation >>= 2;
2917 break;
2918
2919 case R_NDS32_LO12S1:
2920 relocation >>= 1;
2921 break;
2922
2923 case R_NDS32_LO12S0:
2924 default:
2925 relocation >>= 0;
2926 break;
2927 }
2928
2929 inplace_address = (bfd_byte *) data + reloc_entry->address;
2930
2931#define DOIT(x) \
2932 x = ((x & ~reloc_entry->howto->dst_mask) | \
2933 (((x & reloc_entry->howto->src_mask) + relocation) & \
2934 reloc_entry->howto->dst_mask))
2935
2936 switch (reloc_entry->howto->size)
2937 {
2938 case 1:
2939 {
2940 short x = bfd_getb16 (inplace_address);
2941
2942 DOIT (x);
2943 bfd_putb16 ((bfd_vma) x, inplace_address);
2944 }
2945 break;
2946 case 2:
2947 {
2948 unsigned long x = bfd_getb32 (inplace_address);
2949
2950 DOIT (x);
2951 bfd_putb32 ((bfd_vma) x, inplace_address);
2952 }
2953 break;
2954 default:
2955 BFD_ASSERT (0);
2956 }
2957
2958 if (output_bfd != (bfd *) NULL)
2959 reloc_entry->address += input_section->output_offset;
2960
2961 return ret;
2962}
2963
2964/* Handle the R_NDS32_SDA15 reloc.
2965 This reloc is used to compute the address of objects in the small data area
2966 and to perform loads and stores from that area.
2967 The lower 15 bits are sign extended and added to the register specified
2968 in the instruction, which is assumed to point to _SDA_BASE_.
2969
2970 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2971 the access size, this must be taken care of. */
2972
2973static bfd_reloc_status_type
2974nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2975 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2976 asection *input_section, bfd *output_bfd,
2977 char **error_message ATTRIBUTE_UNUSED)
2978{
2979 /* This part is from bfd_elf_generic_reloc. */
2980 if (output_bfd != (bfd *) NULL
2981 && (symbol->flags & BSF_SECTION_SYM) == 0
2982 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2983 {
2984 reloc_entry->address += input_section->output_offset;
2985 return bfd_reloc_ok;
2986 }
2987
2988 if (output_bfd != NULL)
2989 {
2990 /* FIXME: See bfd_perform_relocation. Is this right? */
2991 return bfd_reloc_continue;
2992 }
2993
2994 /* FIXME: not sure what to do here yet. But then again, the linker
2995 may never call us. */
2996 abort ();
2997}
2998
2999/* nds32_elf_ignore_reloc is the special function for
3000 relocation types which don't need to be relocated
3001 like relaxation relocation types.
3002 This function simply return bfd_reloc_ok when it is
3003 invoked. */
3004
3005static bfd_reloc_status_type
3006nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3007 asymbol *symbol ATTRIBUTE_UNUSED,
3008 void *data ATTRIBUTE_UNUSED, asection *input_section,
3009 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3010{
3011 if (output_bfd != NULL)
3012 reloc_entry->address += input_section->output_offset;
3013
3014 return bfd_reloc_ok;
3015}
3016\f
3017
3018/* Map BFD reloc types to NDS32 ELF reloc types. */
3019
3020struct nds32_reloc_map_entry
3021{
3022 bfd_reloc_code_real_type bfd_reloc_val;
3023 unsigned char elf_reloc_val;
3024};
3025
3026static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3027{
3028 {BFD_RELOC_NONE, R_NDS32_NONE},
3029 {BFD_RELOC_16, R_NDS32_16_RELA},
3030 {BFD_RELOC_32, R_NDS32_32_RELA},
fbaf61ad
NC
3031 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3032 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3033
35c08157 3034 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
35c08157
KLC
3035 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3036 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3037 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3038 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3039 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
35c08157
KLC
3040 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3041 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3042 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3043 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3044 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3045 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3046 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3047 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3048 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3049 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
fbaf61ad
NC
3050 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3051 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3052 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3053 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
35c08157
KLC
3054 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3055 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3056 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3057 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3058 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3059 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3060 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3061 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
fbaf61ad
NC
3062 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3063 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
35c08157
KLC
3064 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3065 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3066 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
35c08157
KLC
3067 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3068 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
35c08157
KLC
3069 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3070 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3071 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3072 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3073 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3074 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3075 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3076 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3077 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3078 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3079 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3080 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3081 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
fbaf61ad
NC
3082 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3083 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3084 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3085 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3086 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3087 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3088 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
35c08157
KLC
3089 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3090 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3091 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3092 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
35c08157
KLC
3093 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3094 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3095 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3096 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
35c08157
KLC
3097 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3098 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3099 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3100 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
fbaf61ad
NC
3101 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3102 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3103 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3104 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3105 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3106 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3107 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
35c08157
KLC
3108 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3109 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
fbaf61ad
NC
3110 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3111 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3112 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3113 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3114 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3115 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3116 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3117 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
35c08157
KLC
3118 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3119 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
35c08157 3120 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
35c08157
KLC
3121 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3122 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3123 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3124 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3125 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
35c08157
KLC
3126 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3127 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3128 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3129 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
fbaf61ad 3130 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
35c08157
KLC
3131 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3132 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3133 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3134 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3135 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
fbaf61ad
NC
3136 /* Not sure. */
3137 {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3138 /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
1c8f6a4d
KLC
3139 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3140 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
1c8f6a4d
KLC
3141 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3142 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3143 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3144 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
fbaf61ad
NC
3145 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3146 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3147 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3148 {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3149 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3150 {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3151 {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3152 {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3153 {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3154 {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3155 {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3156 {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3157 {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3158 {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3159 {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3160 {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3161 {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3162 {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3163 {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3164 {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3165 {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
35c08157
KLC
3166};
3167
3168/* Patch tag. */
3169
3170static reloc_howto_type *
3171bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3172 const char *r_name)
3173{
3174 unsigned int i;
3175
3176 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3177 if (nds32_elf_howto_table[i].name != NULL
3178 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3179 return &nds32_elf_howto_table[i];
3180
3181 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3182 if (nds32_elf_relax_howto_table[i].name != NULL
3183 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3184 return &nds32_elf_relax_howto_table[i];
3185
3186 return NULL;
3187}
3188
3189static reloc_howto_type *
3190bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3191{
3192 if (code < R_NDS32_RELAX_ENTRY)
3193 {
3194 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3195 return &nds32_elf_howto_table[code];
3196 }
3197 else
3198 {
fbaf61ad
NC
3199 if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3200 >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3201 {
3202 int i = code;
3203 i += 1;
3204 }
3205
35c08157
KLC
3206 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3207 < ARRAY_SIZE (nds32_elf_relax_howto_table));
3208 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3209 }
3210}
3211
3212static reloc_howto_type *
3213bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3214 bfd_reloc_code_real_type code)
3215{
3216 unsigned int i;
3217
3218 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3219 {
3220 if (nds32_reloc_map[i].bfd_reloc_val == code)
3221 return bfd_elf32_bfd_reloc_type_table_lookup
fbaf61ad 3222 (nds32_reloc_map[i].elf_reloc_val);
35c08157
KLC
3223 }
3224
3225 return NULL;
3226}
3227
3228/* Set the howto pointer for an NDS32 ELF reloc. */
3229
f3185997
NC
3230static bfd_boolean
3231nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
35c08157
KLC
3232 Elf_Internal_Rela *dst)
3233{
3234 enum elf_nds32_reloc_type r_type;
3235
3236 r_type = ELF32_R_TYPE (dst->r_info);
5860e3f8
NC
3237 if (r_type > R_NDS32_GNU_VTENTRY)
3238 {
695344c0 3239 /* xgettext:c-format */
0aa13fee
AM
3240 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3241 abfd, r_type);
f3185997
NC
3242 bfd_set_error (bfd_error_bad_value);
3243 return FALSE;
5860e3f8 3244 }
fbaf61ad
NC
3245
3246 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
35c08157 3247 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
f3185997 3248 return TRUE;
35c08157
KLC
3249}
3250
f3185997 3251static bfd_boolean
35c08157
KLC
3252nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3253 Elf_Internal_Rela *dst)
3254{
f3185997
NC
3255 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3256
3257 if ((r_type == R_NDS32_NONE)
3258 || ((r_type > R_NDS32_GNU_VTENTRY)
3259 && (r_type < R_NDS32_max)))
3260 {
3261 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3262 return TRUE;
3263 }
3264
3265 /* xgettext:c-format */
3266 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3267 bfd_set_error (bfd_error_bad_value);
fbaf61ad 3268 return FALSE;
35c08157
KLC
3269}
3270
3271/* Support for core dump NOTE sections.
3272 Reference to include/linux/elfcore.h in Linux. */
3273
3274static bfd_boolean
3275nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3276{
3277 int offset;
3278 size_t size;
3279
3280 switch (note->descsz)
3281 {
3282 case 0x114:
fbaf61ad 3283 /* Linux/NDS32 32-bit, ABI1. */
35c08157
KLC
3284
3285 /* pr_cursig */
3286 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3287
3288 /* pr_pid */
3289 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3290
3291 /* pr_reg */
3292 offset = 72;
3293 size = 200;
3294 break;
3295
3296 case 0xfc:
fbaf61ad 3297 /* Linux/NDS32 32-bit. */
35c08157
KLC
3298
3299 /* pr_cursig */
3300 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3301
3302 /* pr_pid */
3303 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3304
3305 /* pr_reg */
3306 offset = 72;
3307 size = 176;
3308 break;
3309
3310 default:
3311 return FALSE;
3312 }
3313
3314 /* Make a ".reg" section. */
3315 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3316 size, note->descpos + offset);
3317}
3318
3319static bfd_boolean
3320nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3321{
3322 switch (note->descsz)
3323 {
3324 case 124:
fbaf61ad 3325 /* Linux/NDS32. */
35c08157
KLC
3326
3327 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3328 elf_tdata (abfd)->core->program =
3329 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3330 elf_tdata (abfd)->core->command =
3331 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2b804145 3332 break;
35c08157
KLC
3333
3334 default:
3335 return FALSE;
3336 }
3337
3338 /* Note that for some reason, a spurious space is tacked
3339 onto the end of the args in some (at least one anyway)
3340 implementations, so strip it off if it exists. */
3341 {
3342 char *command = elf_tdata (abfd)->core->command;
3343 int n = strlen (command);
3344
3345 if (0 < n && command[n - 1] == ' ')
3346 command[n - 1] = '\0';
3347 }
3348
3349 return TRUE;
3350}
3351
3352/* Hook called by the linker routine which adds symbols from an object
3353 file. We must handle the special NDS32 section numbers here.
3354 We also keep watching for whether we need to create the sdata special
3355 linker sections. */
3356
3357static bfd_boolean
3358nds32_elf_add_symbol_hook (bfd *abfd,
3359 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3360 Elf_Internal_Sym *sym,
3361 const char **namep ATTRIBUTE_UNUSED,
3362 flagword *flagsp ATTRIBUTE_UNUSED,
3363 asection **secp, bfd_vma *valp)
3364{
3365 switch (sym->st_shndx)
3366 {
3367 case SHN_COMMON:
3368 /* Common symbols less than the GP size are automatically
3369 treated as SHN_MIPS_SCOMMON symbols. */
3370 if (sym->st_size > elf_gp_size (abfd)
3371 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3372 break;
3373
3374 /* st_value is the alignemnt constraint.
3375 That might be its actual size if it is an array or structure. */
3376 switch (sym->st_value)
3377 {
3378 case 1:
3379 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3380 break;
3381 case 2:
3382 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3383 break;
3384 case 4:
3385 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3386 break;
3387 case 8:
3388 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3389 break;
3390 default:
3391 return TRUE;
3392 }
3393
3394 (*secp)->flags |= SEC_IS_COMMON;
3395 *valp = sym->st_size;
3396 break;
3397 }
3398
3399 return TRUE;
3400}
3401
35c08157
KLC
3402/* This function can figure out the best location for a base register to access
3403 data relative to this base register
3404 INPUT:
3405 sda_d0: size of first DOUBLE WORD data section
3406 sda_w0: size of first WORD data section
3407 sda_h0: size of first HALF WORD data section
3408 sda_b : size of BYTE data section
3409 sda_hi: size of second HALF WORD data section
3410 sda_w1: size of second WORD data section
3411 sda_d1: size of second DOUBLE WORD data section
3412 OUTPUT:
3413 offset (always positive) from the beginning of sda_d0 if OK
3414 a negative error value if fail
3415 NOTE:
3416 these 7 sections have to be located back to back if exist
3417 a pass in 0 value for non-existing section */
3418
3419/* Due to the interpretation of simm15 field of load/store depending on
3420 data accessing size, the organization of base register relative data shall
3421 like the following figure
3422 -------------------------------------------
3423 | DOUBLE WORD sized data (range +/- 128K)
3424 -------------------------------------------
3425 | WORD sized data (range +/- 64K)
3426 -------------------------------------------
3427 | HALF WORD sized data (range +/- 32K)
3428 -------------------------------------------
3429 | BYTE sized data (range +/- 16K)
3430 -------------------------------------------
3431 | HALF WORD sized data (range +/- 32K)
3432 -------------------------------------------
3433 | WORD sized data (range +/- 64K)
3434 -------------------------------------------
3435 | DOUBLE WORD sized data (range +/- 128K)
3436 -------------------------------------------
3437 Its base register shall be set to access these data freely. */
3438
3439/* We have to figure out the SDA_BASE value, so that we can adjust the
3440 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3441 BFD. If we can't find it, we're stuck. We cache it in the ELF
3442 target data. We don't need to adjust the symbol value for an
3443 external symbol if we are producing relocatable output. */
3444
3445static asection *sda_rela_sec = NULL;
3446
1c8f6a4d 3447#define SDA_SECTION_NUM 10
35c08157
KLC
3448
3449static bfd_reloc_status_type
fbaf61ad
NC
3450nds32_elf_final_sda_base (bfd * output_bfd,
3451 struct bfd_link_info * info,
3452 bfd_vma * psb,
3453 bfd_boolean add_symbol)
35c08157
KLC
3454{
3455 int relax_fp_as_gp;
3456 struct elf_nds32_link_hash_table *table;
3457 struct bfd_link_hash_entry *h, *h2;
1c8f6a4d 3458 long unsigned int total = 0;
fbaf61ad
NC
3459 asection *first = NULL, *final = NULL, *temp;
3460 bfd_vma sda_base = 0;
35c08157
KLC
3461
3462 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
fbaf61ad
NC
3463 if (!h || (h->type != bfd_link_hash_defined
3464 && h->type != bfd_link_hash_defweak))
35c08157 3465 {
35c08157
KLC
3466 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3467 4 byte-aligned. Therefore, it has to set the first section ".data"
3468 4 byte-aligned. */
3469 static const char sec_name[SDA_SECTION_NUM][10] =
3470 {
3471 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
1c8f6a4d 3472 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
35c08157
KLC
3473 };
3474 size_t i = 0;
3475
3476 if (output_bfd->sections == NULL)
3477 {
3478 *psb = elf_gp (output_bfd);
3479 return bfd_reloc_ok;
3480 }
3481
3482 /* Get the first and final section. */
fbaf61ad 3483 while (i < ARRAY_SIZE (sec_name))
35c08157
KLC
3484 {
3485 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3486 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3487 first = temp;
3488 if (temp && (temp->size != 0 || temp->rawsize != 0))
3489 final = temp;
1c8f6a4d
KLC
3490
3491 /* Summarize the sections in order to check if joining .bss. */
3492 if (temp && temp->size != 0)
3493 total += temp->size;
3494 else if (temp && temp->rawsize != 0)
3495 total += temp->rawsize;
3496
35c08157
KLC
3497 i++;
3498 }
3499
1c8f6a4d
KLC
3500 /* Check .bss size. */
3501 temp = bfd_get_section_by_name (output_bfd, ".bss");
3502 if (temp)
3503 {
3504 if (temp->size != 0)
3505 total += temp->size;
3506 else if (temp->rawsize != 0)
3507 total += temp->rawsize;
3508
3509 if (total < 0x80000)
3510 {
3511 if (!first && (temp->size != 0 || temp->rawsize != 0))
3512 first = temp;
3513 if ((temp->size != 0 || temp->rawsize != 0))
3514 final = temp;
3515 }
3516 }
3517
35c08157
KLC
3518 if (first && final)
3519 {
3520 /* The middle of data region. */
1c8f6a4d 3521 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
35c08157
KLC
3522
3523 /* Find the section sda_base located. */
3524 i = 0;
fbaf61ad 3525 while (i < ARRAY_SIZE (sec_name))
35c08157
KLC
3526 {
3527 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3528 if (final && (final->size != 0 || final->rawsize != 0)
3529 && sda_base >= final->vma)
3530 {
3531 first = final;
3532 i++;
3533 }
3534 else
3535 break;
3536 }
3537 }
3538 else
3539 {
fbaf61ad
NC
3540 /* If there is not any default data section in output bfd, try to find
3541 the first data section. If no data section be found, just simplily
3542 choose the first output section. */
3543 temp = output_bfd->sections;
3544 while (temp)
3545 {
3546 if (temp->flags & SEC_ALLOC
3547 && (((temp->flags & SEC_DATA)
3548 && ((temp->flags & SEC_READONLY) == 0))
3549 || (temp->flags & SEC_LOAD) == 0)
3550 && (temp->size != 0 || temp->rawsize != 0))
3551 {
3552 if (!first)
3553 first = temp;
3554 final = temp;
3555 }
3556 temp = temp->next;
3557 }
3558
3559 /* There is no data or bss section. */
3560 if (!first || (first->size == 0 && first->rawsize == 0))
3561 {
3562 first = output_bfd->sections;
3563 while (first && first->size == 0 && first->rawsize == 0)
3564 first = first->next;
3565 }
3566
3567 /* There is no concrete section. */
35c08157
KLC
3568 if (!first)
3569 {
3570 *psb = elf_gp (output_bfd);
3571 return bfd_reloc_ok;
3572 }
fbaf61ad
NC
3573
3574 if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3575 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3576 else
3577 sda_base = first->vma + 0x2000;
35c08157
KLC
3578 }
3579
3580 sda_base -= first->vma;
3581 sda_base = sda_base & (~7);
3582
3583 if (!_bfd_generic_link_add_one_symbol
fbaf61ad
NC
3584 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3585 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3586 get_elf_backend_data (output_bfd)->collect, &h))
35c08157
KLC
3587 return FALSE;
3588
3589 sda_rela_sec = first;
fbaf61ad 3590 }
35c08157 3591
fbaf61ad
NC
3592 /* Set _FP_BASE_ to _SDA_BASE_. */
3593 table = nds32_elf_hash_table (info);
3594 relax_fp_as_gp = table->relax_fp_as_gp;
3595 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3596 /* _SDA_BASE_ is difined in linker script. */
3597 if (!first)
3598 {
3599 first = h->u.def.section;
3600 sda_base = h->u.def.value;
3601 }
3602
3603 if (relax_fp_as_gp && h2
3604 && (h2->type == bfd_link_hash_undefweak
3605 || h2->type == bfd_link_hash_undefined))
3606 {
3607 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3608 And set FP equal to SDA_BASE to do relaxation for
3609 la $fp, _FP_BASE_. */
3610 if (!_bfd_generic_link_add_one_symbol
3611 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3612 first, sda_base, (const char *) NULL,
3613 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3614 return FALSE;
35c08157
KLC
3615 }
3616
535b785f 3617 if (add_symbol)
35c08157
KLC
3618 {
3619 if (h)
3620 {
3621 /* Now set gp. */
3622 elf_gp (output_bfd) = (h->u.def.value
3623 + h->u.def.section->output_section->vma
3624 + h->u.def.section->output_offset);
3625 }
3626 else
3627 {
38f14ab8 3628 _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
35c08157
KLC
3629 return bfd_reloc_dangerous;
3630 }
3631 }
3632
fbaf61ad
NC
3633 *psb = h->u.def.value
3634 + h->u.def.section->output_section->vma
3635 + h->u.def.section->output_offset;
35c08157
KLC
3636 return bfd_reloc_ok;
3637}
3638\f
3639
3640/* Return size of a PLT entry. */
3641#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3642
35c08157
KLC
3643/* Create an entry in an nds32 ELF linker hash table. */
3644
3645static struct bfd_hash_entry *
3646nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3647 struct bfd_hash_table *table,
3648 const char *string)
3649{
3650 struct elf_nds32_link_hash_entry *ret;
3651
3652 ret = (struct elf_nds32_link_hash_entry *) entry;
3653
3654 /* Allocate the structure if it has not already been allocated by a
3655 subclass. */
3656 if (ret == NULL)
3657 ret = (struct elf_nds32_link_hash_entry *)
3658 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3659
3660 if (ret == NULL)
3661 return (struct bfd_hash_entry *) ret;
3662
3663 /* Call the allocation method of the superclass. */
3664 ret = (struct elf_nds32_link_hash_entry *)
3665 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3666
3667 if (ret != NULL)
3668 {
3669 struct elf_nds32_link_hash_entry *eh;
3670
3671 eh = (struct elf_nds32_link_hash_entry *) ret;
3672 eh->dyn_relocs = NULL;
1c8f6a4d 3673 eh->tls_type = GOT_UNKNOWN;
fbaf61ad 3674 eh->offset_to_gp = 0;
35c08157
KLC
3675 }
3676
3677 return (struct bfd_hash_entry *) ret;
3678}
3679
3680/* Create an nds32 ELF linker hash table. */
3681
3682static struct bfd_link_hash_table *
3683nds32_elf_link_hash_table_create (bfd *abfd)
3684{
3685 struct elf_nds32_link_hash_table *ret;
3686
3687 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3688
3689 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3690 if (ret == NULL)
3691 return NULL;
3692
fbaf61ad 3693 /* Patch tag. */
35c08157
KLC
3694 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3695 nds32_elf_link_hash_newfunc,
3696 sizeof (struct elf_nds32_link_hash_entry),
3697 NDS32_ELF_DATA))
3698 {
3699 free (ret);
3700 return NULL;
3701 }
3702
fbaf61ad
NC
3703 ret->sdynbss = NULL;
3704 ret->srelbss = NULL;
3705 ret->sym_ld_script = NULL;
3706
35c08157
KLC
3707 return &ret->root.root;
3708}
3709
fbaf61ad
NC
3710/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3711 shortcuts to them in our hash table. */
3712
3713static bfd_boolean
3714create_got_section (bfd *dynobj, struct bfd_link_info *info)
3715{
3716 struct elf_link_hash_table *ehtab;
3717
3718 if (!_bfd_elf_create_got_section (dynobj, info))
3719 return FALSE;
3720
3721 ehtab = elf_hash_table (info);
3722 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3723 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3724 if (!ehtab->sgot || !ehtab->sgotplt)
3725 abort ();
3726
3727 /* _bfd_elf_create_got_section will create it for us. */
3728 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3729 if (ehtab->srelgot == NULL
fd361982 3730 || !bfd_set_section_flags (ehtab->srelgot,
fbaf61ad
NC
3731 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3732 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3733 | SEC_READONLY))
fd361982 3734 || !bfd_set_section_alignment (ehtab->srelgot, 2))
fbaf61ad
NC
3735 return FALSE;
3736
3737 return TRUE;
3738}
3739
35c08157
KLC
3740/* Create dynamic sections when linking against a dynamic object. */
3741
3742static bfd_boolean
3743nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3744{
fbaf61ad 3745 struct elf_link_hash_table *ehtab;
35c08157
KLC
3746 struct elf_nds32_link_hash_table *htab;
3747 flagword flags, pltflags;
3748 register asection *s;
3749 const struct elf_backend_data *bed;
3750 int ptralign = 2; /* 32-bit */
fbaf61ad
NC
3751 const char *secname;
3752 char *relname;
3753 flagword secflags;
3754 asection *sec;
35c08157
KLC
3755
3756 bed = get_elf_backend_data (abfd);
fbaf61ad 3757 ehtab = elf_hash_table (info);
35c08157
KLC
3758 htab = nds32_elf_hash_table (info);
3759
3760 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3761 .rel[a].bss sections. */
3762
3763 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3764 | SEC_LINKER_CREATED);
3765
3766 pltflags = flags;
3767 pltflags |= SEC_CODE;
3768 if (bed->plt_not_loaded)
3769 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3770 if (bed->plt_readonly)
3771 pltflags |= SEC_READONLY;
3772
3773 s = bfd_make_section (abfd, ".plt");
fbaf61ad 3774 ehtab->splt = s;
35c08157 3775 if (s == NULL
fd361982
AM
3776 || !bfd_set_section_flags (s, pltflags)
3777 || !bfd_set_section_alignment (s, bed->plt_alignment))
35c08157
KLC
3778 return FALSE;
3779
3780 if (bed->want_plt_sym)
3781 {
3782 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3783 .plt section. */
3784 struct bfd_link_hash_entry *bh = NULL;
3785 struct elf_link_hash_entry *h;
3786
3787 if (!(_bfd_generic_link_add_one_symbol
3788 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3789 (bfd_vma) 0, (const char *) NULL, FALSE,
3790 get_elf_backend_data (abfd)->collect, &bh)))
3791 return FALSE;
3792
3793 h = (struct elf_link_hash_entry *) bh;
3794 h->def_regular = 1;
3795 h->type = STT_OBJECT;
3796
0e1862bb 3797 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
35c08157
KLC
3798 return FALSE;
3799 }
3800
3801 s = bfd_make_section (abfd,
3802 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
fbaf61ad 3803 ehtab->srelplt = s;
35c08157 3804 if (s == NULL
fd361982
AM
3805 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3806 || !bfd_set_section_alignment (s, ptralign))
35c08157
KLC
3807 return FALSE;
3808
fbaf61ad 3809 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
35c08157
KLC
3810 return FALSE;
3811
fbaf61ad
NC
3812 for (sec = abfd->sections; sec; sec = sec->next)
3813 {
fd361982 3814 secflags = bfd_section_flags (sec);
fbaf61ad
NC
3815 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3816 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3817 continue;
fd361982 3818 secname = bfd_section_name (sec);
fbaf61ad
NC
3819 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3820 strcpy (relname, ".rela");
3821 strcat (relname, secname);
3822 if (bfd_get_section_by_name (abfd, secname))
3823 continue;
3824 s = bfd_make_section (abfd, relname);
3825 if (s == NULL
fd361982
AM
3826 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3827 || !bfd_set_section_alignment (s, ptralign))
fbaf61ad
NC
3828 return FALSE;
3829 }
35c08157
KLC
3830
3831 if (bed->want_dynbss)
3832 {
3833 /* The .dynbss section is a place to put symbols which are defined
3834 by dynamic objects, are referenced by regular objects, and are
3835 not functions. We must allocate space for them in the process
3836 image and use a R_*_COPY reloc to tell the dynamic linker to
3837 initialize them at run time. The linker script puts the .dynbss
3838 section into the .bss section of the final image. */
3839 s = bfd_make_section (abfd, ".dynbss");
3840 htab->sdynbss = s;
3841 if (s == NULL
fd361982 3842 || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
35c08157
KLC
3843 return FALSE;
3844 /* The .rel[a].bss section holds copy relocs. This section is not
3845 normally needed. We need to create it here, though, so that the
3846 linker will map it to an output section. We can't just create it
3847 only if we need it, because we will not know whether we need it
3848 until we have seen all the input files, and the first time the
3849 main linker code calls BFD after examining all the input files
3850 (size_dynamic_sections) the input sections have already been
3851 mapped to the output sections. If the section turns out not to
3852 be needed, we can discard it later. We will never need this
3853 section when generating a shared object, since they do not use
3854 copy relocs. */
0e1862bb 3855 if (!bfd_link_pic (info))
35c08157
KLC
3856 {
3857 s = bfd_make_section (abfd, (bed->default_use_rela_p
3858 ? ".rela.bss" : ".rel.bss"));
3859 htab->srelbss = s;
3860 if (s == NULL
fd361982
AM
3861 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3862 || !bfd_set_section_alignment (s, ptralign))
35c08157
KLC
3863 return FALSE;
3864 }
3865 }
3866
3867 return TRUE;
3868}
3869
3870/* Copy the extra info we tack onto an elf_link_hash_entry. */
3871static void
3872nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3873 struct elf_link_hash_entry *dir,
3874 struct elf_link_hash_entry *ind)
3875{
3876 struct elf_nds32_link_hash_entry *edir, *eind;
3877
3878 edir = (struct elf_nds32_link_hash_entry *) dir;
3879 eind = (struct elf_nds32_link_hash_entry *) ind;
3880
3881 if (eind->dyn_relocs != NULL)
3882 {
3883 if (edir->dyn_relocs != NULL)
3884 {
3bf083ed
AM
3885 struct elf_dyn_relocs **pp;
3886 struct elf_dyn_relocs *p;
35c08157
KLC
3887
3888 if (ind->root.type == bfd_link_hash_indirect)
3889 abort ();
3890
3891 /* Add reloc counts against the weak sym to the strong sym
3892 list. Merge any entries against the same section. */
3893 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3894 {
3bf083ed 3895 struct elf_dyn_relocs *q;
35c08157
KLC
3896
3897 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3898 if (q->sec == p->sec)
3899 {
3900 q->pc_count += p->pc_count;
3901 q->count += p->count;
3902 *pp = p->next;
3903 break;
3904 }
3905 if (q == NULL)
3906 pp = &p->next;
3907 }
3908 *pp = edir->dyn_relocs;
3909 }
3910
3911 edir->dyn_relocs = eind->dyn_relocs;
3912 eind->dyn_relocs = NULL;
3913 }
3914
fbaf61ad
NC
3915 if (ind->root.type == bfd_link_hash_indirect)
3916 {
3917 if (dir->got.refcount <= 0)
3918 {
3919 edir->tls_type = eind->tls_type;
3920 eind->tls_type = GOT_UNKNOWN;
3921 }
3922 }
3923
35c08157
KLC
3924 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3925}
3926\f
fbaf61ad 3927
63c1f59d
AM
3928/* Find dynamic relocs for H that apply to read-only sections. */
3929
3930static asection *
3931readonly_dynrelocs (struct elf_link_hash_entry *h)
3932{
3bf083ed 3933 struct elf_dyn_relocs *p;
63c1f59d
AM
3934
3935 for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3936 {
3937 asection *s = p->sec->output_section;
3938
3939 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3940 return p->sec;
3941 }
3942 return NULL;
3943}
35c08157
KLC
3944
3945/* Adjust a symbol defined by a dynamic object and referenced by a
3946 regular object. The current definition is in some section of the
3947 dynamic object, but we're not including those sections. We have to
3948 change the definition to something the rest of the link can
3949 understand. */
3950
3951static bfd_boolean
3952nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3953 struct elf_link_hash_entry *h)
3954{
3955 struct elf_nds32_link_hash_table *htab;
35c08157
KLC
3956 bfd *dynobj;
3957 asection *s;
3958 unsigned int power_of_two;
3959
3960 dynobj = elf_hash_table (info)->dynobj;
3961
3962 /* Make sure we know what is going on here. */
3963 BFD_ASSERT (dynobj != NULL
3964 && (h->needs_plt
60d67dc8 3965 || h->is_weakalias
35c08157
KLC
3966 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3967
3968
3969 /* If this is a function, put it in the procedure linkage table. We
3970 will fill in the contents of the procedure linkage table later,
3971 when we know the address of the .got section. */
3972 if (h->type == STT_FUNC || h->needs_plt)
3973 {
0e1862bb 3974 if (!bfd_link_pic (info)
35c08157
KLC
3975 && !h->def_dynamic
3976 && !h->ref_dynamic
3977 && h->root.type != bfd_link_hash_undefweak
3978 && h->root.type != bfd_link_hash_undefined)
3979 {
3980 /* This case can occur if we saw a PLT reloc in an input
3981 file, but the symbol was never referred to by a dynamic
3982 object. In such a case, we don't actually need to build
3983 a procedure linkage table, and we can just do a PCREL
3984 reloc instead. */
3985 h->plt.offset = (bfd_vma) - 1;
3986 h->needs_plt = 0;
3987 }
3988
3989 return TRUE;
3990 }
3991 else
3992 h->plt.offset = (bfd_vma) - 1;
3993
3994 /* If this is a weak symbol, and there is a real definition, the
3995 processor independent code will have arranged for us to see the
3996 real definition first, and we can just use the same value. */
60d67dc8 3997 if (h->is_weakalias)
35c08157 3998 {
60d67dc8
AM
3999 struct elf_link_hash_entry *def = weakdef (h);
4000 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4001 h->root.u.def.section = def->root.u.def.section;
4002 h->root.u.def.value = def->root.u.def.value;
35c08157
KLC
4003 return TRUE;
4004 }
4005
4006 /* This is a reference to a symbol defined by a dynamic object which
4007 is not a function. */
4008
4009 /* If we are creating a shared library, we must presume that the
4010 only references to the symbol are via the global offset table.
4011 For such cases we need not do anything here; the relocations will
4012 be handled correctly by relocate_section. */
0e1862bb 4013 if (bfd_link_pic (info))
35c08157
KLC
4014 return TRUE;
4015
4016 /* If there are no references to this symbol that do not use the
4017 GOT, we don't need to generate a copy reloc. */
4018 if (!h->non_got_ref)
4019 return TRUE;
4020
4021 /* If -z nocopyreloc was given, we won't generate them either. */
3bf083ed 4022 if (0 && info->nocopyreloc)
35c08157
KLC
4023 {
4024 h->non_got_ref = 0;
4025 return TRUE;
4026 }
4027
3bf083ed
AM
4028 /* If we don't find any dynamic relocs in read-only sections, then
4029 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
fbaf61ad 4030 if (!readonly_dynrelocs (h))
35c08157
KLC
4031 {
4032 h->non_got_ref = 0;
4033 return TRUE;
4034 }
4035
4036 /* We must allocate the symbol in our .dynbss section, which will
4037 become part of the .bss section of the executable. There will be
4038 an entry for this symbol in the .dynsym section. The dynamic
4039 object will contain position independent code, so all references
4040 from the dynamic object to this symbol will go through the global
4041 offset table. The dynamic linker will use the .dynsym entry to
4042 determine the address it must put in the global offset table, so
4043 both the dynamic object and the regular object will refer to the
4044 same memory location for the variable. */
4045
4046 htab = nds32_elf_hash_table (info);
4047 s = htab->sdynbss;
4048 BFD_ASSERT (s != NULL);
4049
4050 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4051 to copy the initial value out of the dynamic object and into the
4052 runtime process image. We need to remember the offset into the
4053 .rela.bss section we are going to use. */
4054 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4055 {
4056 asection *srel;
4057
4058 srel = htab->srelbss;
4059 BFD_ASSERT (srel != NULL);
4060 srel->size += sizeof (Elf32_External_Rela);
4061 h->needs_copy = 1;
4062 }
4063
4064 /* We need to figure out the alignment required for this symbol. I
4065 have no idea how ELF linkers handle this. */
4066 power_of_two = bfd_log2 (h->size);
4067 if (power_of_two > 3)
4068 power_of_two = 3;
4069
4070 /* Apply the required alignment. */
4071 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
fd361982 4072 if (power_of_two > bfd_section_alignment (s))
35c08157 4073 {
fd361982 4074 if (!bfd_set_section_alignment (s, power_of_two))
35c08157
KLC
4075 return FALSE;
4076 }
4077
4078 /* Define the symbol as being at this point in the section. */
4079 h->root.u.def.section = s;
4080 h->root.u.def.value = s->size;
4081
4082 /* Increment the section size to make room for the symbol. */
4083 s->size += h->size;
4084
4085 return TRUE;
4086}
4087
4088/* Allocate space in .plt, .got and associated reloc sections for
4089 dynamic relocs. */
4090
4091static bfd_boolean
4092allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4093{
4094 struct bfd_link_info *info;
fbaf61ad 4095 struct elf_link_hash_table *ehtab;
35c08157
KLC
4096 struct elf_nds32_link_hash_table *htab;
4097 struct elf_nds32_link_hash_entry *eh;
3bf083ed 4098 struct elf_dyn_relocs *p;
35c08157
KLC
4099
4100 if (h->root.type == bfd_link_hash_indirect)
4101 return TRUE;
4102
fbaf61ad
NC
4103 /* When warning symbols are created, they **replace** the "real"
4104 entry in the hash table, thus we never get to see the real
4105 symbol in a hash traversal. So look at it now. */
35c08157 4106 if (h->root.type == bfd_link_hash_warning)
35c08157
KLC
4107 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4108
fbaf61ad
NC
4109 eh = (struct elf_nds32_link_hash_entry *) h;
4110
35c08157 4111 info = (struct bfd_link_info *) inf;
fbaf61ad 4112 ehtab = elf_hash_table (info);
35c08157 4113 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4114 if (htab == NULL)
4115 return FALSE;
35c08157
KLC
4116
4117 eh = (struct elf_nds32_link_hash_entry *) h;
4118
fbaf61ad
NC
4119 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4120 && h->plt.refcount > 0
4121 && !(bfd_link_pie (info) && h->def_regular))
35c08157
KLC
4122 {
4123 /* Make sure this symbol is output as a dynamic symbol.
4124 Undefined weak syms won't yet be marked as dynamic. */
4125 if (h->dynindx == -1 && !h->forced_local)
4126 {
4127 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4128 return FALSE;
4129 }
4130
0e1862bb 4131 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
35c08157 4132 {
fbaf61ad 4133 asection *s = ehtab->splt;
35c08157
KLC
4134
4135 /* If this is the first .plt entry, make room for the special
4136 first entry. */
4137 if (s->size == 0)
4138 s->size += PLT_ENTRY_SIZE;
4139
4140 h->plt.offset = s->size;
4141
4142 /* If this symbol is not defined in a regular file, and we are
4143 not generating a shared library, then set the symbol to this
4144 location in the .plt. This is required to make function
4145 pointers compare as equal between the normal executable and
4146 the shared library. */
0e1862bb 4147 if (!bfd_link_pic (info) && !h->def_regular)
35c08157
KLC
4148 {
4149 h->root.u.def.section = s;
4150 h->root.u.def.value = h->plt.offset;
4151 }
4152
4153 /* Make room for this entry. */
4154 s->size += PLT_ENTRY_SIZE;
4155
4156 /* We also need to make an entry in the .got.plt section, which
4157 will be placed in the .got section by the linker script. */
fbaf61ad 4158 ehtab->sgotplt->size += 4;
35c08157
KLC
4159
4160 /* We also need to make an entry in the .rel.plt section. */
fbaf61ad
NC
4161 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4162 if (htab->tls_desc_trampoline)
4163 htab->next_tls_desc_index++;
35c08157
KLC
4164 }
4165 else
4166 {
4167 h->plt.offset = (bfd_vma) - 1;
4168 h->needs_plt = 0;
4169 }
4170 }
4171 else
4172 {
4173 h->plt.offset = (bfd_vma) - 1;
4174 h->needs_plt = 0;
4175 }
4176
4177 if (h->got.refcount > 0)
4178 {
fbaf61ad 4179 asection *sgot;
35c08157 4180 bfd_boolean dyn;
1c8f6a4d 4181 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
35c08157
KLC
4182
4183 /* Make sure this symbol is output as a dynamic symbol.
4184 Undefined weak syms won't yet be marked as dynamic. */
4185 if (h->dynindx == -1 && !h->forced_local)
4186 {
4187 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4188 return FALSE;
4189 }
4190
fbaf61ad
NC
4191 sgot = elf_hash_table (info)->sgot;
4192 h->got.offset = sgot->size;
1c8f6a4d
KLC
4193
4194 if (tls_type == GOT_UNKNOWN)
4195 abort ();
fbaf61ad
NC
4196
4197 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4198 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4199 sgot->size += 4;
4200 else
4201 {
4202 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4203 if (tls_type & GOT_TLS_DESC)
4204 sgot->size += 8;
4205 }
1c8f6a4d 4206
35c08157 4207 dyn = htab->root.dynamic_sections_created;
fbaf61ad 4208
0e1862bb 4209 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
fbaf61ad
NC
4210 {
4211 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4212 {
4213 /* TLS_DESC with trampoline needs a relocation slot
4214 within .rela.plt. */
4215 htab->num_tls_desc++;
4216 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4217 htab->tls_trampoline = -1;
4218 }
4219 else
4220 {
4221 /* other relocations, including TLS_DESC without trampoline, need
4222 a relocation slot within .rela.got. */
4223 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4224 }
4225 }
35c08157
KLC
4226 }
4227 else
fbaf61ad 4228 h->got.offset = (bfd_vma)-1;
35c08157
KLC
4229
4230 if (eh->dyn_relocs == NULL)
4231 return TRUE;
4232
4233 /* In the shared -Bsymbolic case, discard space allocated for
4234 dynamic pc-relative relocs against symbols which turn out to be
4235 defined in regular objects. For the normal shared case, discard
4236 space for pc-relative relocs that have become local due to symbol
4237 visibility changes. */
4238
0e1862bb 4239 if (bfd_link_pic (info))
35c08157
KLC
4240 {
4241 if (h->def_regular && (h->forced_local || info->symbolic))
4242 {
3bf083ed 4243 struct elf_dyn_relocs **pp;
35c08157
KLC
4244
4245 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
4246 {
4247 p->count -= p->pc_count;
4248 p->pc_count = 0;
4249 if (p->count == 0)
4250 *pp = p->next;
4251 else
4252 pp = &p->next;
4253 }
4254 }
4255 }
4256 else
4257 {
4258 /* For the non-shared case, discard space for relocs against
4259 symbols which turn out to need copy relocs or are not dynamic. */
4260
4261 if (!h->non_got_ref
4262 && ((h->def_dynamic
4263 && !h->def_regular)
4264 || (htab->root.dynamic_sections_created
4265 && (h->root.type == bfd_link_hash_undefweak
4266 || h->root.type == bfd_link_hash_undefined))))
4267 {
4268 /* Make sure this symbol is output as a dynamic symbol.
4269 Undefined weak syms won't yet be marked as dynamic. */
4270 if (h->dynindx == -1 && !h->forced_local)
4271 {
4272 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4273 return FALSE;
4274 }
4275
4276 /* If that succeeded, we know we'll be keeping all the
4277 relocs. */
4278 if (h->dynindx != -1)
4279 goto keep;
4280 }
4281
4282 eh->dyn_relocs = NULL;
4283
fbaf61ad 4284keep:;
35c08157
KLC
4285 }
4286
4287 /* Finally, allocate space. */
4288 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4289 {
4290 asection *sreloc = elf_section_data (p->sec)->sreloc;
4291 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4292 }
4293
4294 return TRUE;
4295}
4296
fbaf61ad
NC
4297/* Add relocation REL to the end of relocation section SRELOC. */
4298
4299static void
4300elf32_nds32_add_dynreloc (bfd *output_bfd,
4301 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4302 asection *sreloc, Elf_Internal_Rela *rel)
4303{
4304 bfd_byte *loc;
4305 if (sreloc == NULL)
4306 abort ();
4307
4308 loc = sreloc->contents;
4309 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4310 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4311 abort ();
4312
4313 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4314}
4315
63c1f59d
AM
4316/* Set DF_TEXTREL if we find any dynamic relocs that apply to
4317 read-only sections. */
35c08157
KLC
4318
4319static bfd_boolean
63c1f59d 4320maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
35c08157 4321{
63c1f59d 4322 asection *sec;
35c08157 4323
63c1f59d
AM
4324 if (h->root.type == bfd_link_hash_indirect)
4325 return TRUE;
35c08157 4326
63c1f59d
AM
4327 sec = readonly_dynrelocs (h);
4328 if (sec != NULL)
35c08157 4329 {
63c1f59d 4330 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
35c08157 4331
63c1f59d
AM
4332 info->flags |= DF_TEXTREL;
4333 info->callbacks->minfo
c1c8c1ef 4334 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 4335 sec->owner, h->root.root.string, sec);
35c08157 4336
63c1f59d
AM
4337 /* Not an error, just cut short the traversal. */
4338 return FALSE;
35c08157
KLC
4339 }
4340 return TRUE;
4341}
4342
4343/* Set the sizes of the dynamic sections. */
4344
4345static bfd_boolean
4346nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4347 struct bfd_link_info *info)
4348{
4349 struct elf_nds32_link_hash_table *htab;
4350 bfd *dynobj;
4351 asection *s;
4352 bfd_boolean relocs;
fbaf61ad 4353 bfd_boolean plt;
35c08157
KLC
4354 bfd *ibfd;
4355
4356 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4357 if (htab == NULL)
4358 return FALSE;
4359
4360 dynobj = elf_hash_table (info)->dynobj;
35c08157
KLC
4361 BFD_ASSERT (dynobj != NULL);
4362
fbaf61ad 4363 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
4364 {
4365 /* Set the contents of the .interp section to the interpreter. */
f5233a16 4366 if (bfd_link_executable (info) && !info->nointerp)
35c08157
KLC
4367 {
4368 s = bfd_get_section_by_name (dynobj, ".interp");
4369 BFD_ASSERT (s != NULL);
4370 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4371 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4372 }
4373 }
4374
4375 /* Set up .got offsets for local syms, and space for local dynamic
4376 relocs. */
c72f2fb2 4377 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
35c08157
KLC
4378 {
4379 bfd_signed_vma *local_got;
4380 bfd_signed_vma *end_local_got;
4381 bfd_size_type locsymcount;
4382 Elf_Internal_Shdr *symtab_hdr;
fbaf61ad
NC
4383 asection *sgot;
4384 char *local_tls_type;
4385 unsigned long symndx;
4386 bfd_vma *local_tlsdesc_gotent;
35c08157
KLC
4387
4388 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4389 continue;
4390
4391 for (s = ibfd->sections; s != NULL; s = s->next)
4392 {
3bf083ed 4393 struct elf_dyn_relocs *p;
35c08157 4394
3bf083ed 4395 for (p = ((struct elf_dyn_relocs *)
35c08157
KLC
4396 elf_section_data (s)->local_dynrel);
4397 p != NULL; p = p->next)
4398 {
4399 if (!bfd_is_abs_section (p->sec)
4400 && bfd_is_abs_section (p->sec->output_section))
4401 {
4402 /* Input section has been discarded, either because
4403 it is a copy of a linkonce section or due to
4404 linker script /DISCARD/, so we'll be discarding
4405 the relocs too. */
4406 }
4407 else if (p->count != 0)
4408 {
fbaf61ad
NC
4409 asection *sreloc = elf_section_data (p->sec)->sreloc;
4410 sreloc->size += p->count * sizeof (Elf32_External_Rela);
35c08157
KLC
4411 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4412 info->flags |= DF_TEXTREL;
4413 }
4414 }
4415 }
4416
4417 local_got = elf_local_got_refcounts (ibfd);
4418 if (!local_got)
4419 continue;
4420
4421 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4422 locsymcount = symtab_hdr->sh_info;
4423 end_local_got = local_got + locsymcount;
fbaf61ad
NC
4424 sgot = elf_hash_table (info)->sgot;
4425 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4426 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4427 for (symndx = 0; local_got < end_local_got;
4428 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
35c08157
KLC
4429 {
4430 if (*local_got > 0)
4431 {
fbaf61ad
NC
4432 int num_of_got_entry_needed = 0;
4433 *local_got = sgot->size;
4434 *local_tlsdesc_gotent = sgot->size;
4435
4436 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4437 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4438 num_of_got_entry_needed = 1;
4439 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4440 else if (*local_tls_type & GOT_TLS_DESC)
4441 num_of_got_entry_needed = 2;
4442
4443 sgot->size += (num_of_got_entry_needed << 2);
4444
4445 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4446 others need a slot in .rela.got. */
4447 if (*local_tls_type == GOT_TLS_DESC)
4448 {
4449 if (bfd_link_pic (info))
4450 {
4451 if (htab->tls_desc_trampoline)
4452 {
4453 htab->num_tls_desc++;
4454 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4455 htab->tls_trampoline = -1;
4456 }
4457 else
4458 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4459 }
4460 else
4461 {
4462 /* TLS_DESC -> TLS_LE */
4463 }
4464 }
4465 else
4466 {
4467 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4468 }
35c08157
KLC
4469 }
4470 else
fbaf61ad
NC
4471 {
4472 *local_got = (bfd_vma) -1;
4473 *local_tlsdesc_gotent = (bfd_vma) -1;
4474 }
35c08157
KLC
4475 }
4476 }
4477
4478 /* Allocate global sym .plt and .got entries, and space for global
4479 sym dynamic relocs. */
4480 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4481
fbaf61ad
NC
4482 /* For every jump slot reserved in the sgotplt, reloc_count is
4483 incremented. However, when we reserve space for TLS descriptors,
4484 it's not incremented, so in order to compute the space reserved
4485 for them, it suffices to multiply the reloc count by the jump
4486 slot size. */
4487 if (htab->tls_desc_trampoline && htab->root.srelplt)
4488 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4489
4490 if (htab->tls_trampoline)
4491 {
4492 htab->tls_trampoline = htab->root.splt->size;
4493
4494 /* If we're not using lazy TLS relocations, don't generate the
4495 PLT and GOT entries they require. */
4496 if (!(info->flags & DF_BIND_NOW))
4497 {
4498 htab->dt_tlsdesc_got = htab->root.sgot->size;
4499 htab->root.sgot->size += 4;
4500
4501 htab->dt_tlsdesc_plt = htab->root.splt->size;
4502 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4503 }
4504 }
4505
35c08157
KLC
4506 /* We now have determined the sizes of the various dynamic sections.
4507 Allocate memory for them. */
fbaf61ad
NC
4508 /* The check_relocs and adjust_dynamic_symbol entry points have
4509 determined the sizes of the various dynamic sections. Allocate
4510 memory for them. */
4511 plt = FALSE;
35c08157
KLC
4512 relocs = FALSE;
4513 for (s = dynobj->sections; s != NULL; s = s->next)
4514 {
4515 if ((s->flags & SEC_LINKER_CREATED) == 0)
4516 continue;
4517
ce558b89 4518 if (s == htab->root.splt)
35c08157
KLC
4519 {
4520 /* Strip this section if we don't need it; see the
4521 comment below. */
fbaf61ad 4522 plt = s->size != 0;
35c08157 4523 }
fbaf61ad 4524 else if (s == elf_hash_table (info)->sgot)
35c08157
KLC
4525 {
4526 got_size += s->size;
4527 }
fbaf61ad 4528 else if (s == elf_hash_table (info)->sgotplt)
35c08157
KLC
4529 {
4530 got_size += s->size;
4531 }
fd361982 4532 else if (strncmp (bfd_section_name (s), ".rela", 5) == 0)
35c08157 4533 {
fbaf61ad 4534 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
35c08157
KLC
4535 relocs = TRUE;
4536
4537 /* We use the reloc_count field as a counter if we need
4538 to copy relocs into the output file. */
4539 s->reloc_count = 0;
4540 }
4541 else
4542 {
4543 /* It's not one of our sections, so don't allocate space. */
4544 continue;
4545 }
4546
4547 if (s->size == 0)
4548 {
4549 /* If we don't need this section, strip it from the
4550 output file. This is mostly to handle .rela.bss and
4551 .rela.plt. We must create both sections in
4552 create_dynamic_sections, because they must be created
4553 before the linker maps input sections to output
4554 sections. The linker does that before
4555 adjust_dynamic_symbol is called, and it is that
4556 function which decides whether anything needs to go
4557 into these sections. */
4558 s->flags |= SEC_EXCLUDE;
4559 continue;
4560 }
4561
4562 /* Allocate memory for the section contents. We use bfd_zalloc
4563 here in case unused entries are not reclaimed before the
4564 section's contents are written out. This should not happen,
4565 but this way if it does, we get a R_NDS32_NONE reloc instead
4566 of garbage. */
4567 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4568 if (s->contents == NULL)
4569 return FALSE;
4570 }
4571
4572
4573 if (htab->root.dynamic_sections_created)
4574 {
4575 /* Add some entries to the .dynamic section. We fill in the
4576 values later, in nds32_elf_finish_dynamic_sections, but we
4577 must add the entries now so that we get the correct size for
4578 the .dynamic section. The DT_DEBUG entry is filled in by the
4579 dynamic linker and used by the debugger. */
4580#define add_dynamic_entry(TAG, VAL) \
4581 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4582
fbaf61ad 4583 if (bfd_link_executable (info))
35c08157
KLC
4584 {
4585 if (!add_dynamic_entry (DT_DEBUG, 0))
4586 return FALSE;
4587 }
4588
fbaf61ad 4589 if (elf_hash_table (info)->splt->size != 0)
35c08157
KLC
4590 {
4591 if (!add_dynamic_entry (DT_PLTGOT, 0)
4592 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4593 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4594 || !add_dynamic_entry (DT_JMPREL, 0))
4595 return FALSE;
4596 }
4597
fbaf61ad
NC
4598 if (htab->tls_desc_trampoline && plt)
4599 {
4600 if (htab->dt_tlsdesc_plt
4601 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4602 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4603 return FALSE;
4604 }
4605
35c08157
KLC
4606 if (relocs)
4607 {
4608 if (!add_dynamic_entry (DT_RELA, 0)
4609 || !add_dynamic_entry (DT_RELASZ, 0)
4610 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4611 return FALSE;
4612
4613 /* If any dynamic relocs apply to a read-only section,
4614 then we need a DT_TEXTREL entry. */
4615 if ((info->flags & DF_TEXTREL) == 0)
63c1f59d 4616 elf_link_hash_traverse (&htab->root, maybe_set_textrel,
35c08157
KLC
4617 (void *) info);
4618
4619 if ((info->flags & DF_TEXTREL) != 0)
4620 {
4621 if (!add_dynamic_entry (DT_TEXTREL, 0))
4622 return FALSE;
4623 }
4624 }
4625 }
4626#undef add_dynamic_entry
4627
4628 return TRUE;
4629}
4630
4631static bfd_reloc_status_type
4632nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4633 bfd_vma relocation, bfd_byte *location)
4634{
4635 int size;
4636 bfd_vma x = 0;
4637 bfd_reloc_status_type flag;
4638 unsigned int rightshift = howto->rightshift;
4639 unsigned int bitpos = howto->bitpos;
4640
4641 /* If the size is negative, negate RELOCATION. This isn't very
4642 general. */
4643 if (howto->size < 0)
4644 relocation = -relocation;
4645
4646 /* Get the value we are going to relocate. */
4647 size = bfd_get_reloc_size (howto);
4648 switch (size)
4649 {
4650 default:
35c08157
KLC
4651 abort ();
4652 break;
6346d5ca
AM
4653 case 0:
4654 return bfd_reloc_ok;
35c08157
KLC
4655 case 2:
4656 x = bfd_getb16 (location);
4657 break;
4658 case 4:
4659 x = bfd_getb32 (location);
4660 break;
4661 }
4662
4663 /* Check for overflow. FIXME: We may drop bits during the addition
4664 which we don't check for. We must either check at every single
4665 operation, which would be tedious, or we must do the computations
4666 in a type larger than bfd_vma, which would be inefficient. */
4667 flag = bfd_reloc_ok;
4668 if (howto->complain_on_overflow != complain_overflow_dont)
4669 {
4670 bfd_vma addrmask, fieldmask, signmask, ss;
4671 bfd_vma a, b, sum;
4672
4673 /* Get the values to be added together. For signed and unsigned
4674 relocations, we assume that all values should be truncated to
4675 the size of an address. For bitfields, all the bits matter.
4676 See also bfd_check_overflow. */
4677 fieldmask = N_ONES (howto->bitsize);
4678 signmask = ~fieldmask;
4679 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4680 a = (relocation & addrmask) >> rightshift;
4681 b = (x & howto->src_mask & addrmask) >> bitpos;
4682
4683 switch (howto->complain_on_overflow)
4684 {
4685 case complain_overflow_signed:
4686 /* If any sign bits are set, all sign bits must be set.
4687 That is, A must be a valid negative address after
4688 shifting. */
4689 signmask = ~(fieldmask >> 1);
4690 /* Fall through. */
4691
4692 case complain_overflow_bitfield:
4693 /* Much like the signed check, but for a field one bit
4694 wider. We allow a bitfield to represent numbers in the
4695 range -2**n to 2**n-1, where n is the number of bits in the
4696 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4697 can't overflow, which is exactly what we want. */
4698 ss = a & signmask;
4699 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4700 flag = bfd_reloc_overflow;
4701
4702 /* We only need this next bit of code if the sign bit of B
4703 is below the sign bit of A. This would only happen if
4704 SRC_MASK had fewer bits than BITSIZE. Note that if
4705 SRC_MASK has more bits than BITSIZE, we can get into
4706 trouble; we would need to verify that B is in range, as
4707 we do for A above. */
4708 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4709 ss >>= bitpos;
4710
4711 /* Set all the bits above the sign bit. */
4712 b = (b ^ ss) - ss;
4713
4714 /* Now we can do the addition. */
4715 sum = a + b;
4716
4717 /* See if the result has the correct sign. Bits above the
4718 sign bit are junk now; ignore them. If the sum is
4719 positive, make sure we did not have all negative inputs;
4720 if the sum is negative, make sure we did not have all
4721 positive inputs. The test below looks only at the sign
4722 bits, and it really just
4723 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4724
4725 We mask with addrmask here to explicitly allow an address
4726 wrap-around. The Linux kernel relies on it, and it is
4727 the only way to write assembler code which can run when
4728 loaded at a location 0x80000000 away from the location at
4729 which it is linked. */
4730 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4731 flag = bfd_reloc_overflow;
4732
4733 break;
4734
4735 case complain_overflow_unsigned:
4736 /* Checking for an unsigned overflow is relatively easy:
4737 trim the addresses and add, and trim the result as well.
4738 Overflow is normally indicated when the result does not
4739 fit in the field. However, we also need to consider the
4740 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4741 input is 0x80000000, and bfd_vma is only 32 bits; then we
4742 will get sum == 0, but there is an overflow, since the
4743 inputs did not fit in the field. Instead of doing a
4744 separate test, we can check for this by or-ing in the
4745 operands when testing for the sum overflowing its final
4746 field. */
4747 sum = (a + b) & addrmask;
4748 if ((a | b | sum) & signmask)
4749 flag = bfd_reloc_overflow;
4750 break;
4751
4752 default:
4753 abort ();
4754 }
4755 }
4756
4757 /* Put RELOCATION in the right bits. */
4758 relocation >>= (bfd_vma) rightshift;
4759 relocation <<= (bfd_vma) bitpos;
4760
4761 /* Add RELOCATION to the right bits of X. */
4762 /* FIXME : 090616
4763 Because the relaxation may generate duplicate relocation at one address,
4764 an addition to immediate in the instruction may cause the relocation added
4765 several times.
4766 This bug should be fixed in assembler, but a check is also needed here. */
4767 if (howto->partial_inplace)
4768 x = ((x & ~howto->dst_mask)
4769 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4770 else
4771 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4772
4773
4774 /* Put the relocated value back in the object file. */
4775 switch (size)
4776 {
4777 default:
4778 case 0:
4779 case 1:
4780 case 8:
4781 abort ();
4782 break;
4783 case 2:
4784 bfd_putb16 (x, location);
4785 break;
4786 case 4:
4787 bfd_putb32 (x, location);
4788 break;
4789 }
4790
4791 return flag;
4792}
4793
4794static bfd_reloc_status_type
4795nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4796 asection *input_section, bfd_byte *contents,
4797 bfd_vma address, bfd_vma value, bfd_vma addend)
4798{
4799 bfd_vma relocation;
4800
4801 /* Sanity check the address. */
4802 if (address > bfd_get_section_limit (input_bfd, input_section))
4803 return bfd_reloc_outofrange;
4804
4805 /* This function assumes that we are dealing with a basic relocation
4806 against a symbol. We want to compute the value of the symbol to
4807 relocate to. This is just VALUE, the value of the symbol, plus
4808 ADDEND, any addend associated with the reloc. */
4809 relocation = value + addend;
4810
4811 /* If the relocation is PC relative, we want to set RELOCATION to
4812 the distance between the symbol (currently in RELOCATION) and the
c2bf1eec 4813 location we are relocating. If pcrel_offset is FALSE we do not
35c08157
KLC
4814 need to subtract out the offset of the location within the
4815 section (which is just ADDRESS). */
4816 if (howto->pc_relative)
4817 {
4818 relocation -= (input_section->output_section->vma
4819 + input_section->output_offset);
4820 if (howto->pcrel_offset)
4821 relocation -= address;
4822 }
4823
4824 return nds32_relocate_contents (howto, input_bfd, relocation,
4825 contents + address);
4826}
4827
4828static bfd_boolean
4829nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4830 const char *name,
4831 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4832 asection *input_sec,
4833 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4834{
4835 const char *source;
4836 FILE *sym_ld_script = NULL;
4837 struct elf_nds32_link_hash_table *table;
4838
4839 table = nds32_elf_hash_table (info);
4840 sym_ld_script = table->sym_ld_script;
4841 if (!sym_ld_script)
4842 return TRUE;
4843
4844 if (!h || !name || *name == '\0')
4845 return TRUE;
4846
4847 if (input_sec->flags & SEC_EXCLUDE)
4848 return TRUE;
4849
4850 if (!check_start_export_sym)
4851 {
4852 fprintf (sym_ld_script, "SECTIONS\n{\n");
4853 check_start_export_sym = 1;
4854 }
4855
4856 if (h->root.type == bfd_link_hash_defined
4857 || h->root.type == bfd_link_hash_defweak)
4858 {
4859 if (!h->root.u.def.section->output_section)
4860 return TRUE;
4861
4862 if (bfd_is_const_section (input_sec))
4863 source = input_sec->name;
4864 else
4865 source = input_sec->owner->filename;
4866
4867 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4868 h->root.root.string,
4869 (long) (h->root.u.def.value
4870 + h->root.u.def.section->output_section->vma
4871 + h->root.u.def.section->output_offset), source);
4872 }
4873
4874 return TRUE;
4875}
4876
4877/* Relocate an NDS32/D ELF section.
4878 There is some attempt to make this function usable for many architectures,
4879 both for RELA and REL type relocs, if only to serve as a learning tool.
4880
4881 The RELOCATE_SECTION function is called by the new ELF backend linker
4882 to handle the relocations for a section.
4883
4884 The relocs are always passed as Rela structures; if the section
4885 actually uses Rel structures, the r_addend field will always be
4886 zero.
4887
4888 This function is responsible for adjust the section contents as
4889 necessary, and (if using Rela relocs and generating a
4890 relocatable output file) adjusting the reloc addend as
4891 necessary.
4892
4893 This function does not have to worry about setting the reloc
4894 address or the reloc symbol index.
4895
4896 LOCAL_SYMS is a pointer to the swapped in local symbols.
4897
4898 LOCAL_SECTIONS is an array giving the section in the input file
4899 corresponding to the st_shndx field of each local symbol.
4900
4901 The global hash table entry for the global symbols can be found
4902 via elf_sym_hashes (input_bfd).
4903
4904 When generating relocatable output, this function must handle
4905 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4906 going to be the section symbol corresponding to the output
4907 section, which means that the addend must be adjusted
4908 accordingly. */
4909
fbaf61ad
NC
4910/* Return the base VMA address which should be subtracted from real addresses
4911 when resolving @dtpoff relocation.
4912 This is PT_TLS segment p_vaddr. */
4913
4914/* Return the relocation value for @tpoff relocation
4915 if STT_TLS virtual address is ADDRESS. */
4916
4917/* Return the relocation value for @gottpoff relocation
4918 if STT_TLS virtual address is ADDRESS. */
4919
1c8f6a4d 4920static bfd_vma
fbaf61ad 4921gottpoff (struct bfd_link_info *info, bfd_vma address)
1c8f6a4d 4922{
fbaf61ad
NC
4923 bfd_vma tp_base;
4924 bfd_vma tp_offset;
4925
1c8f6a4d
KLC
4926 /* If tls_sec is NULL, we should have signalled an error already. */
4927 if (elf_hash_table (info)->tls_sec == NULL)
4928 return 0;
fbaf61ad
NC
4929
4930 tp_base = elf_hash_table (info)->tls_sec->vma;
4931 tp_offset = address - tp_base;
4932
4933 return tp_offset;
1c8f6a4d
KLC
4934}
4935
35c08157 4936static bfd_boolean
fbaf61ad
NC
4937patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4938{
4939 /* TLS_GD/TLS_LD model #1
4940 46 00 00 00 sethi $r0,#0x0
4941 58 00 00 00 ori $r0,$r0,#0x0
4942 40 00 74 00 add $r0,$r0,$gp
4943 04 10 00 00 lwi $r1,[$r0+#0x0]
4944 4b e0 04 01 jral $lp,$r1 */
4945
4946 /* TLS_GD/TLS_LD model #2
4947 46 00 00 00 sethi $r0,#0x0
4948 58 00 00 00 ori $r0,$r0,#0x0
4949 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4950 40 00 74 00 add $r0,$r0,$gp
4951 4b e0 04 01 jral $lp,$r1 */
4952
4953 /* TLS_IE model (non-PIC)
4954 46 00 00 00 sethi $r0,#0x0
4955 04 00 00 00 lwi $r0,[$r0+#0x0]
4956 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4957
4958 /* TLS_IE model (PIC)
4959 46 00 00 00 sethi $r0,#0x0
4960 58 00 00 00 ori $r0,$r0,#0x0
4961 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4962 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4963
4964 /* TLS_GD_TO_IE model
4965 46 00 00 00 sethi $r0,#0x0
4966 58 00 00 00 ori $r0,$r0,#0x0
4967 40 00 74 00 add $r0,$rM,$gp
4968 04 00 00 01 lwi $r0,[$r0+#0x4]
4969 40 00 64 00 add $r0,$r0,$r25 */
4970
4971 bfd_boolean rz = FALSE;
4972
4973 typedef struct
4974 {
4975 uint32_t opcode;
4976 uint32_t mask;
4977 } pat_t;
4978
4979 uint32_t patch[3] =
4980 {
4981 0x40007400, /* add $r0,$rM,$gp */
4982 0x04000001, /* lwi $r0,[$r0+#0x4] */
4983 0x40006400, /* add $r0,$r0,$r25 */
4984 };
4985
4986 pat_t mode0[3] =
4987 {
4988 { 0x40000000, 0xfe0003ff },
4989 { 0x04000000, 0xfe000000 },
4990 { 0x4be00001, 0xffff83ff },
4991 };
4992
4993 pat_t mode1[3] =
4994 {
4995 { 0x38007402, 0xfe007fff },
4996 { 0x40007400, 0xfe007fff },
4997 { 0x4be00001, 0xffff83ff },
4998 };
4999
5000 unsigned char *p = contents + rel->r_offset;
5001
5002 uint32_t insn;
5003 uint32_t regidx = 0;
5004 insn = bfd_getb32 (p);
5005 if (INSN_SETHI == (0xfe0fffffu & insn))
5006 {
5007 regidx = 0x1f & (insn >> 20);
5008 p += 4;
5009 }
5010
5011 insn = bfd_getb32 (p);
5012 if (INSN_ORI == (0xfe007fffu & insn))
5013 {
5014 regidx = 0x1f & (insn >> 20);
5015 p += 4;
5016 }
5017
5018 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
5019 {
5020 /* already patched? */
5021 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
5022 (patch[1] == bfd_getb32 (p + 4)))
5023 rz = TRUE;
5024 }
5025 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
5026 {
5027 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
5028 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
5029 {
5030 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5031 bfd_putb32 (patch[1], p + 4);
5032 bfd_putb32 (patch[2], p + 8);
5033 rz = TRUE;
5034 }
5035 }
5036 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
5037 {
5038 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
5039 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5040 {
5041 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5042 bfd_putb32 (patch[1], p + 4);
5043 bfd_putb32 (patch[2], p + 8);
5044 rz = TRUE;
5045 }
5046 }
5047
5048 if (!rz)
5049 {
5050 printf ("%s: %s @ 0x%08x\n", __func__, ibfd->filename,
5051 (int) rel->r_offset);
5052 BFD_ASSERT(0); /* Unsupported pattern. */
5053 }
5054
5055 return rz;
5056}
5057
5058static enum elf_nds32_tls_type
5059get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5060
5061static unsigned int
5062ones32 (register unsigned int x)
5063{
5064 /* 32-bit recursive reduction using SWAR...
5065 but first step is mapping 2-bit values
5066 into sum of 2 1-bit values in sneaky way. */
5067 x -= ((x >> 1) & 0x55555555);
5068 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5069 x = (((x >> 4) + x) & 0x0f0f0f0f);
5070 x += (x >> 8);
5071 x += (x >> 16);
5072 return (x & 0x0000003f);
5073}
5074
a767a1c4 5075#if !HAVE_FLS
fbaf61ad
NC
5076static unsigned int
5077fls (register unsigned int x)
5078{
5079 return ffs (x & (-x));
5080}
a767a1c4 5081#endif /* !HAVE_FLS */
fbaf61ad
NC
5082
5083#define nds32_elf_local_tlsdesc_gotent(bfd) \
5084 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5085
5086static bfd_boolean
5087nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
5088 struct bfd_link_info * info,
5089 bfd * input_bfd,
5090 asection * input_section,
5091 bfd_byte * contents,
5092 Elf_Internal_Rela * relocs,
5093 Elf_Internal_Sym * local_syms,
5094 asection ** local_sections)
5095{
5096 Elf_Internal_Shdr *symtab_hdr;
5097 struct elf_link_hash_entry **sym_hashes;
5098 Elf_Internal_Rela *rel, *relend;
5099 bfd_boolean ret = TRUE; /* Assume success. */
5100 int align = 0;
5101 bfd_reloc_status_type r;
5102 const char *errmsg = NULL;
5103 bfd_vma gp;
5104 struct elf_link_hash_table *ehtab;
5105 struct elf_nds32_link_hash_table *htab;
5106 bfd *dynobj;
5107 bfd_vma *local_got_offsets;
5108 asection *sgot, *splt, *sreloc;
5109 bfd_vma high_address;
5110 struct elf_nds32_link_hash_table *table;
5111 int eliminate_gc_relocs;
35c08157
KLC
5112 bfd_vma fpbase_addr;
5113
5114 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5115 sym_hashes = elf_sym_hashes (input_bfd);
fbaf61ad 5116 ehtab = elf_hash_table (info);
35c08157
KLC
5117 htab = nds32_elf_hash_table (info);
5118 high_address = bfd_get_section_limit (input_bfd, input_section);
5119
5120 dynobj = htab->root.dynobj;
5121 local_got_offsets = elf_local_got_offsets (input_bfd);
5122
fbaf61ad
NC
5123 sgot = ehtab->sgot;
5124 splt = ehtab->splt;
35c08157
KLC
5125 sreloc = NULL;
5126
5127 rel = relocs;
5128 relend = relocs + input_section->reloc_count;
5129
5130 table = nds32_elf_hash_table (info);
5131 eliminate_gc_relocs = table->eliminate_gc_relocs;
fbaf61ad 5132
35c08157 5133 /* By this time, we can adjust the value of _SDA_BASE_. */
fbaf61ad 5134 /* Explain _SDA_BASE_ */
0e1862bb 5135 if ((!bfd_link_relocatable (info)))
35c08157
KLC
5136 {
5137 is_SDA_BASE_set = 1;
5138 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5139 if (r != bfd_reloc_ok)
5140 return FALSE;
5141 }
5142
fbaf61ad
NC
5143 /* Do TLS model conversion once at first. */
5144 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5145
35c08157
KLC
5146 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5147 the fp value is set as gp, and it has be reverted for instruction
5148 setting fp. */
5149 fpbase_addr = elf_gp (output_bfd);
5150
fbaf61ad 5151 /* Deal with (dynamic) relocations. */
35c08157
KLC
5152 for (rel = relocs; rel < relend; rel++)
5153 {
5154 enum elf_nds32_reloc_type r_type;
5155 reloc_howto_type *howto = NULL;
5156 unsigned long r_symndx;
5157 struct elf_link_hash_entry *h = NULL;
5158 Elf_Internal_Sym *sym = NULL;
5159 asection *sec;
5160 bfd_vma relocation;
fbaf61ad
NC
5161 bfd_vma relocation_sym = 0xdeadbeef;
5162 Elf_Internal_Rela *lorel;
5163 bfd_vma off;
35c08157
KLC
5164
5165 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5166 ensure it's zero (we use REL relocs, not RELA). Therefore this
5167 should be assigning zero to `addend', but for clarity we use
5168 `r_addend'. */
5169
5170 bfd_vma addend = rel->r_addend;
5171 bfd_vma offset = rel->r_offset;
5172
5173 r_type = ELF32_R_TYPE (rel->r_info);
5174 if (r_type >= R_NDS32_max)
5175 {
695344c0 5176 /* xgettext:c-format */
0aa13fee 5177 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4eca0228 5178 input_bfd, r_type);
35c08157
KLC
5179 bfd_set_error (bfd_error_bad_value);
5180 ret = FALSE;
5181 continue;
5182 }
5183
5184 if (r_type == R_NDS32_GNU_VTENTRY
5185 || r_type == R_NDS32_GNU_VTINHERIT
5186 || r_type == R_NDS32_NONE
5187 || r_type == R_NDS32_RELA_GNU_VTENTRY
5188 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5189 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5190 || r_type == R_NDS32_DATA
fbaf61ad 5191 || r_type == R_NDS32_TRAN)
35c08157
KLC
5192 continue;
5193
0c4bd9d9
KLC
5194 /* If we enter the fp-as-gp region. Resolve the address
5195 of best fp-base. */
35c08157
KLC
5196 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5197 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5198 {
5199 int dist;
5200
5201 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5202 dist = rel->r_addend >> 16;
5203 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5204 local_syms, symtab_hdr);
5205 }
5206 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5207 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5208 {
5209 fpbase_addr = elf_gp (output_bfd);
5210 }
5211
fbaf61ad
NC
5212 /* Skip the relocations used for relaxation. */
5213 /* We have to update LONGCALL and LONGJUMP
5214 relocations when generating the relocatable files. */
5215 if (!bfd_link_relocatable (info)
5216 && (r_type >= R_NDS32_RELAX_ENTRY
5217 || (r_type >= R_NDS32_LONGCALL4
5218 && r_type <= R_NDS32_LONGJUMP7)))
35c08157
KLC
5219 continue;
5220
5221 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5222 r_symndx = ELF32_R_SYM (rel->r_info);
5223
5224 /* This is a final link. */
5225 sym = NULL;
5226 sec = NULL;
5227 h = NULL;
5228
5229 if (r_symndx < symtab_hdr->sh_info)
5230 {
5231 /* Local symbol. */
5232 sym = local_syms + r_symndx;
5233 sec = local_sections[r_symndx];
5234
5235 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5236 addend = rel->r_addend;
fbaf61ad
NC
5237
5238 /* keep symbol location for static TLS_IE GOT entry */
5239 relocation_sym = relocation;
5240 if (bfd_link_relocatable (info))
5241 {
5242 /* This is a relocatable link. We don't have to change
5243 anything, unless the reloc is against a section symbol,
5244 in which case we have to adjust according to where the
5245 section symbol winds up in the output section. */
5246 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5247 rel->r_addend += sec->output_offset + sym->st_value;
5248
5249 continue;
5250 }
35c08157
KLC
5251 }
5252 else
5253 {
5254 /* External symbol. */
fbaf61ad
NC
5255 if (bfd_link_relocatable (info))
5256 continue;
35c08157
KLC
5257 bfd_boolean warned, ignored, unresolved_reloc;
5258 int symndx = r_symndx - symtab_hdr->sh_info;
5259
5260 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5261 r_symndx, symtab_hdr, sym_hashes, h, sec,
5262 relocation, unresolved_reloc, warned,
5263 ignored);
5264
fbaf61ad
NC
5265 /* keep symbol location for static TLS_IE GOT entry */
5266 relocation_sym = relocation;
5267
35c08157
KLC
5268 /* la $fp, _FP_BASE_ is per-function (region).
5269 Handle it specially. */
5270 switch ((int) r_type)
5271 {
fbaf61ad
NC
5272 case R_NDS32_HI20_RELA:
5273 case R_NDS32_LO12S0_RELA:
5274 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5275 FP_BASE_NAME) == 0)
5276 {
5277 if (!bfd_link_pie (info))
5278 {
5279 _bfd_error_handler
5280 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5281 input_bfd);
5282 }
5283 relocation = fpbase_addr;
5284 }
5285 break;
35c08157
KLC
5286 case R_NDS32_SDA19S0_RELA:
5287 case R_NDS32_SDA15S0_RELA:
5288 case R_NDS32_20_RELA:
5289 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5290 FP_BASE_NAME) == 0)
5291 {
5292 relocation = fpbase_addr;
5293 break;
5294 }
5295 }
35c08157
KLC
5296 }
5297
5298 /* Sanity check the address. */
5299 if (offset > high_address)
5300 {
5301 r = bfd_reloc_outofrange;
5302 goto check_reloc;
5303 }
5304
fbaf61ad 5305 if (r_type >= R_NDS32_RELAX_ENTRY)
35c08157
KLC
5306 continue;
5307
5308 switch ((int) r_type)
5309 {
5310 case R_NDS32_GOTOFF:
5311 /* Relocation is relative to the start of the global offset
5312 table (for ld24 rx, #uimm24), e.g. access at label+addend
5313
5314 ld24 rx. #label@GOTOFF + addend
5315 sub rx, r12. */
5316 case R_NDS32_GOTOFF_HI20:
5317 case R_NDS32_GOTOFF_LO12:
5318 case R_NDS32_GOTOFF_LO15:
5319 case R_NDS32_GOTOFF_LO19:
5320 BFD_ASSERT (sgot != NULL);
5321
5322 relocation -= elf_gp (output_bfd);
5323 break;
5324
5325 case R_NDS32_9_PLTREL:
5326 case R_NDS32_25_PLTREL:
5327 /* Relocation is to the entry for this symbol in the
5328 procedure linkage table. */
5329
5330 /* The native assembler will generate a 25_PLTREL reloc
5331 for a local symbol if you assemble a call from one
5332 section to another when using -K pic. */
5333 if (h == NULL)
5334 break;
5335
5336 if (h->forced_local)
5337 break;
5338
5339 /* We didn't make a PLT entry for this symbol. This
5340 happens when statically linking PIC code, or when
5341 using -Bsymbolic. */
5342 if (h->plt.offset == (bfd_vma) - 1)
5343 break;
5344
5345 relocation = (splt->output_section->vma
5346 + splt->output_offset + h->plt.offset);
5347 break;
5348
5349 case R_NDS32_PLT_GOTREL_HI20:
5350 case R_NDS32_PLT_GOTREL_LO12:
5351 case R_NDS32_PLT_GOTREL_LO15:
5352 case R_NDS32_PLT_GOTREL_LO19:
5353 case R_NDS32_PLT_GOTREL_LO20:
fbaf61ad
NC
5354 if (h == NULL
5355 || h->forced_local
5356 || h->plt.offset == (bfd_vma) -1
5357 || (bfd_link_pie (info) && h->def_regular))
35c08157 5358 {
fbaf61ad
NC
5359 /* Maybe we should find better checking to optimize
5360 PIE PLT relocations. */
35c08157
KLC
5361 /* We didn't make a PLT entry for this symbol. This
5362 happens when statically linking PIC code, or when
5363 using -Bsymbolic. */
fbaf61ad
NC
5364 if (h)
5365 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
35c08157
KLC
5366 relocation -= elf_gp (output_bfd);
5367 break;
5368 }
5369
5370 relocation = (splt->output_section->vma
5371 + splt->output_offset + h->plt.offset);
5372
5373 relocation -= elf_gp (output_bfd);
5374 break;
5375
5376 case R_NDS32_PLTREL_HI20:
5377 case R_NDS32_PLTREL_LO12:
5378
5379 /* Relocation is to the entry for this symbol in the
5380 procedure linkage table. */
5381
5382 /* The native assembler will generate a 25_PLTREL reloc
5383 for a local symbol if you assemble a call from one
5384 section to another when using -K pic. */
5385 if (h == NULL)
5386 break;
5387
5388 if (h->forced_local)
5389 break;
5390
5391 if (h->plt.offset == (bfd_vma) - 1)
5392 /* We didn't make a PLT entry for this symbol. This
5393 happens when statically linking PIC code, or when
5394 using -Bsymbolic. */
5395 break;
5396
5397 if (splt == NULL)
5398 break;
5399
5400 relocation = (splt->output_section->vma
5401 + splt->output_offset
5402 + h->plt.offset + 4)
5403 - (input_section->output_section->vma
5404 + input_section->output_offset
5405 + rel->r_offset);
5406
5407 break;
5408
5409 case R_NDS32_GOTPC20:
5410 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5411 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5412 relocation = elf_gp (output_bfd);
5413 break;
5414
5415 case R_NDS32_GOTPC_HI20:
5416 case R_NDS32_GOTPC_LO12:
fbaf61ad
NC
5417 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5418 bl .+4
5419 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5420 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5421 or
5422 bl .+4
5423 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5424 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5425 relocation = elf_gp (output_bfd);
5426 relocation -= (input_section->output_section->vma
5427 + input_section->output_offset + rel->r_offset);
5428 break;
35c08157
KLC
5429
5430 case R_NDS32_GOT20:
5431 /* Fall through. */
5432 case R_NDS32_GOT_HI20:
5433 case R_NDS32_GOT_LO12:
5434 case R_NDS32_GOT_LO15:
5435 case R_NDS32_GOT_LO19:
5436 /* Relocation is to the entry for this symbol in the global
5437 offset table. */
5438 BFD_ASSERT (sgot != NULL);
5439
5440 if (h != NULL)
5441 {
fbaf61ad 5442 /* External symbol */
35c08157 5443 bfd_boolean dyn;
35c08157
KLC
5444
5445 off = h->got.offset;
5446 BFD_ASSERT (off != (bfd_vma) - 1);
5447 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
5448 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5449 bfd_link_pic (info),
5450 h)
5451 || (bfd_link_pic (info)
35c08157
KLC
5452 && (info->symbolic
5453 || h->dynindx == -1
5454 || h->forced_local) && h->def_regular))
5455 {
5456 /* This is actually a static link, or it is a
5457 -Bsymbolic link and the symbol is defined
5458 locally, or the symbol was forced to be local
5459 because of a version file. We must initialize
5460 this entry in the global offset table. Since the
5461 offset must always be a multiple of 4, we use the
5462 least significant bit to record whether we have
5463 initialized it already.
5464
5465 When doing a dynamic link, we create a .rela.got
5466 relocation entry to initialize the value. This
5467 is done in the finish_dynamic_symbol routine. */
fbaf61ad 5468 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5469 off &= ~1;
5470 else
5471 {
5472 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5473 h->got.offset |= 1;
5474 }
5475 }
5476 relocation = sgot->output_section->vma + sgot->output_offset + off
5477 - elf_gp (output_bfd);
5478 }
5479 else
5480 {
fbaf61ad 5481 /* Local symbol */
35c08157
KLC
5482 bfd_byte *loc;
5483
5484 BFD_ASSERT (local_got_offsets != NULL
5485 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5486
5487 off = local_got_offsets[r_symndx];
5488
5489 /* The offset must always be a multiple of 4. We use
5490 the least significant bit to record whether we have
5491 already processed this entry. */
fbaf61ad 5492 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5493 off &= ~1;
5494 else
5495 {
5496 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5497
0e1862bb 5498 if (bfd_link_pic (info))
35c08157
KLC
5499 {
5500 asection *srelgot;
5501 Elf_Internal_Rela outrel;
5502
5503 /* We need to generate a R_NDS32_RELATIVE reloc
5504 for the dynamic linker. */
fbaf61ad 5505 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
35c08157
KLC
5506 BFD_ASSERT (srelgot != NULL);
5507
5508 outrel.r_offset = (elf_gp (output_bfd)
5509 + sgot->output_offset + off);
5510 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5511 outrel.r_addend = relocation;
5512 loc = srelgot->contents;
5513 loc +=
5514 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5515 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5516 ++srelgot->reloc_count;
5517 }
5518 local_got_offsets[r_symndx] |= 1;
5519 }
5520 relocation = sgot->output_section->vma + sgot->output_offset + off
5521 - elf_gp (output_bfd);
5522 }
5523
5524 break;
5525
5526 case R_NDS32_16_RELA:
5527 case R_NDS32_20_RELA:
5528 case R_NDS32_5_RELA:
5529 case R_NDS32_32_RELA:
5530 case R_NDS32_9_PCREL_RELA:
5531 case R_NDS32_WORD_9_PCREL_RELA:
5532 case R_NDS32_10_UPCREL_RELA:
5533 case R_NDS32_15_PCREL_RELA:
5534 case R_NDS32_17_PCREL_RELA:
5535 case R_NDS32_25_PCREL_RELA:
5536 case R_NDS32_HI20_RELA:
5537 case R_NDS32_LO12S3_RELA:
5538 case R_NDS32_LO12S2_RELA:
5539 case R_NDS32_LO12S2_DP_RELA:
5540 case R_NDS32_LO12S2_SP_RELA:
5541 case R_NDS32_LO12S1_RELA:
5542 case R_NDS32_LO12S0_RELA:
5543 case R_NDS32_LO12S0_ORI_RELA:
0e1862bb 5544 if (bfd_link_pic (info) && r_symndx != 0
35c08157
KLC
5545 && (input_section->flags & SEC_ALLOC) != 0
5546 && (eliminate_gc_relocs == 0
5547 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5548 && ((r_type != R_NDS32_9_PCREL_RELA
5549 && r_type != R_NDS32_WORD_9_PCREL_RELA
5550 && r_type != R_NDS32_10_UPCREL_RELA
5551 && r_type != R_NDS32_15_PCREL_RELA
5552 && r_type != R_NDS32_17_PCREL_RELA
5553 && r_type != R_NDS32_25_PCREL_RELA
5554 && !(r_type == R_NDS32_32_RELA
5555 && strcmp (input_section->name, ".eh_frame") == 0))
5556 || (h != NULL && h->dynindx != -1
5557 && (!info->symbolic || !h->def_regular))))
5558 {
5559 Elf_Internal_Rela outrel;
5560 bfd_boolean skip, relocate;
5561 bfd_byte *loc;
5562
5563 /* When generating a shared object, these relocations
5564 are copied into the output file to be resolved at run
5565 time. */
5566
5567 if (sreloc == NULL)
5568 {
5569 const char *name;
5570
5571 name = bfd_elf_string_from_elf_section
5572 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5573 elf_section_data (input_section)->rela.hdr->sh_name);
5574 if (name == NULL)
5575 return FALSE;
5576
5577 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
fd361982 5578 && strcmp (bfd_section_name (input_section),
35c08157
KLC
5579 name + 5) == 0);
5580
5581 sreloc = bfd_get_section_by_name (dynobj, name);
5582 BFD_ASSERT (sreloc != NULL);
5583 }
5584
5585 skip = FALSE;
5586 relocate = FALSE;
5587
5588 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5589 info,
5590 input_section,
5591 rel->r_offset);
5592 if (outrel.r_offset == (bfd_vma) - 1)
5593 skip = TRUE;
5594 else if (outrel.r_offset == (bfd_vma) - 2)
5595 skip = TRUE, relocate = TRUE;
5596 outrel.r_offset += (input_section->output_section->vma
5597 + input_section->output_offset);
5598
5599 if (skip)
5600 memset (&outrel, 0, sizeof outrel);
5601 else if (r_type == R_NDS32_17_PCREL_RELA
5602 || r_type == R_NDS32_15_PCREL_RELA
5603 || r_type == R_NDS32_25_PCREL_RELA)
5604 {
5605 BFD_ASSERT (h != NULL && h->dynindx != -1);
5606 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5607 outrel.r_addend = rel->r_addend;
5608 }
5609 else
5610 {
5611 /* h->dynindx may be -1 if this symbol was marked to
5612 become local. */
5613 if (h == NULL
5614 || ((info->symbolic || h->dynindx == -1)
fbaf61ad
NC
5615 && h->def_regular)
5616 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
5617 {
5618 relocate = TRUE;
5619 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5620 outrel.r_addend = relocation + rel->r_addend;
fbaf61ad
NC
5621
5622 if (h)
5623 {
5624 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5625
5626 BFD_ASSERT (sgot != NULL);
5627 /* If we did not allocate got entry for the symbol,
5628 we can not fill the nonexistent got entry. */
5629 if (h->got.offset != (bfd_vma) -1
5630 && (h->got.offset & 1) == 0)
5631 {
5632 bfd_put_32 (output_bfd, outrel.r_addend,
5633 sgot->contents + h->got.offset);
5634 }
5635 }
35c08157
KLC
5636 }
5637 else
5638 {
fbaf61ad
NC
5639 if (h->dynindx == -1)
5640 {
5641 _bfd_error_handler
acef8081 5642 (_("%pB: relocation %s against `%s' can not be used when "
fbaf61ad
NC
5643 "making a shared object; recompile with -fPIC"),
5644 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5645 bfd_set_error (bfd_error_bad_value);
5646 return FALSE;
5647 }
5648
35c08157
KLC
5649 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5650 outrel.r_addend = rel->r_addend;
5651 }
5652 }
5653
5654 loc = sreloc->contents;
5655 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5656 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5657 ++sreloc->reloc_count;
5658
5659 /* If this reloc is against an external symbol, we do
5660 not want to fiddle with the addend. Otherwise, we
5661 need to include the symbol value so that it becomes
5662 an addend for the dynamic reloc. */
5663 if (!relocate)
5664 continue;
5665 }
5666 break;
5667
5668 case R_NDS32_25_ABS_RELA:
0e1862bb 5669 if (bfd_link_pic (info))
35c08157 5670 {
4eca0228 5671 _bfd_error_handler
38f14ab8
AM
5672 (_("%pB: warning: %s unsupported in shared mode"),
5673 input_bfd, "R_NDS32_25_ABS_RELA");
35c08157
KLC
5674 return FALSE;
5675 }
5676 break;
5677
5678 case R_NDS32_9_PCREL:
5679 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5680 contents, offset,
5681 sec, relocation, addend);
5682 goto check_reloc;
5683
5684 case R_NDS32_HI20:
fbaf61ad
NC
5685 /* We allow an arbitrary number of HI20 relocs before the
5686 LO12 reloc. This permits gcc to emit the HI and LO relocs
5687 itself. */
5688 for (lorel = rel + 1;
5689 (lorel < relend
5690 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5691 continue;
5692 if (lorel < relend
5693 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5694 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5695 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5696 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
35c08157 5697 {
fbaf61ad
NC
5698 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5699 contents, relocation + addend);
5700 r = bfd_reloc_ok;
35c08157 5701 }
fbaf61ad
NC
5702 else
5703 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5704 contents, offset, relocation,
5705 addend);
35c08157
KLC
5706 goto check_reloc;
5707
5708 case R_NDS32_GOT17S2_RELA:
5709 case R_NDS32_GOT15S2_RELA:
fbaf61ad
NC
5710 BFD_ASSERT (sgot != NULL);
5711
5712 if (h != NULL)
35c08157 5713 {
fbaf61ad 5714 bfd_boolean dyn;
35c08157 5715
fbaf61ad
NC
5716 off = h->got.offset;
5717 BFD_ASSERT (off != (bfd_vma) - 1);
35c08157 5718
fbaf61ad
NC
5719 dyn = htab->root.dynamic_sections_created;
5720 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5721 (dyn, bfd_link_pic (info), h)
5722 || (bfd_link_pic (info)
5723 && (info->symbolic
5724 || h->dynindx == -1
5725 || h->forced_local)
5726 && h->def_regular))
35c08157 5727 {
fbaf61ad
NC
5728 /* This is actually a static link, or it is a
5729 -Bsymbolic link and the symbol is defined
5730 locally, or the symbol was forced to be local
5731 because of a version file. We must initialize
5732 this entry in the global offset table. Since the
5733 offset must always be a multiple of 4, we use the
5734 least significant bit to record whether we have
5735 initialized it already.
5736
5737 When doing a dynamic link, we create a .rela.got
5738 relocation entry to initialize the value. This
5739 is done in the finish_dynamic_symbol routine. */
5740 if ((off & 1) != 0)
5741 off &= ~1;
5742 else
35c08157 5743 {
fbaf61ad
NC
5744 bfd_put_32 (output_bfd, relocation,
5745 sgot->contents + off);
5746 h->got.offset |= 1;
35c08157
KLC
5747 }
5748 }
fbaf61ad
NC
5749 }
5750 else
5751 {
5752 bfd_byte *loc;
35c08157 5753
fbaf61ad
NC
5754 BFD_ASSERT (local_got_offsets != NULL
5755 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
35c08157 5756
fbaf61ad 5757 off = local_got_offsets[r_symndx];
35c08157 5758
fbaf61ad
NC
5759 /* The offset must always be a multiple of 4. We use
5760 the least significant bit to record whether we have
5761 already processed this entry. */
5762 if ((off & 1) != 0)
5763 off &= ~1;
5764 else
5765 {
5766 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5767
5768 if (bfd_link_pic (info))
35c08157 5769 {
fbaf61ad
NC
5770 asection *srelgot;
5771 Elf_Internal_Rela outrel;
35c08157 5772
fbaf61ad
NC
5773 /* We need to generate a R_NDS32_RELATIVE reloc
5774 for the dynamic linker. */
5775 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5776 BFD_ASSERT (srelgot != NULL);
5777
5778 outrel.r_offset = (elf_gp (output_bfd)
5779 + sgot->output_offset + off);
5780 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5781 outrel.r_addend = relocation;
5782 loc = srelgot->contents;
5783 loc +=
5784 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5785 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5786 ++srelgot->reloc_count;
35c08157 5787 }
fbaf61ad 5788 local_got_offsets[r_symndx] |= 1;
35c08157 5789 }
35c08157 5790 }
fbaf61ad
NC
5791 relocation = sgot->output_section->vma + sgot->output_offset + off
5792 - elf_gp (output_bfd);
5793
35c08157
KLC
5794 if (relocation & align)
5795 {
5796 /* Incorrect alignment. */
4eca0228 5797 _bfd_error_handler
38f14ab8 5798 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
35c08157
KLC
5799 ret = FALSE;
5800 r = bfd_reloc_dangerous;
5801 goto check_reloc;
5802 }
5803 break;
5804
5805 case R_NDS32_SDA16S3_RELA:
5806 case R_NDS32_SDA15S3_RELA:
5807 case R_NDS32_SDA15S3:
5808 align = 0x7;
5809 goto handle_sda;
5810
5811 case R_NDS32_SDA17S2_RELA:
5812 case R_NDS32_SDA15S2_RELA:
5813 case R_NDS32_SDA12S2_SP_RELA:
5814 case R_NDS32_SDA12S2_DP_RELA:
5815 case R_NDS32_SDA15S2:
5816 case R_NDS32_SDA_FP7U2_RELA:
5817 align = 0x3;
5818 goto handle_sda;
5819
5820 case R_NDS32_SDA18S1_RELA:
5821 case R_NDS32_SDA15S1_RELA:
5822 case R_NDS32_SDA15S1:
5823 align = 0x1;
5824 goto handle_sda;
5825
5826 case R_NDS32_SDA19S0_RELA:
5827 case R_NDS32_SDA15S0_RELA:
5828 case R_NDS32_SDA15S0:
fbaf61ad 5829 align = 0x0;
35c08157 5830handle_sda:
fbaf61ad 5831 BFD_ASSERT (sec != NULL);
35c08157 5832
fbaf61ad
NC
5833 /* If the symbol is in the abs section, the out_bfd will be null.
5834 This happens when the relocation has a symbol@GOTOFF. */
5835 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5836 if (r != bfd_reloc_ok)
5837 {
5838 _bfd_error_handler
5839 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5840 ret = FALSE;
5841 goto check_reloc;
5842 }
35c08157 5843
fbaf61ad
NC
5844 /* At this point `relocation' contains the object's
5845 address. */
5846 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5847 {
5848 relocation -= fpbase_addr;
5849 }
5850 else
5851 relocation -= gp;
5852 /* Now it contains the offset from _SDA_BASE_. */
35c08157 5853
fbaf61ad 5854 /* Make sure alignment is correct. */
35c08157 5855
fbaf61ad
NC
5856 if (relocation & align)
5857 {
5858 /* Incorrect alignment. */
5859 _bfd_error_handler
5860 /* xgettext:c-format */
5861 (_("%pB(%pA): warning: unaligned small data access"
5862 " of type %d"),
5863 input_bfd, input_section, r_type);
5864 ret = FALSE;
5865 goto check_reloc;
35c08157 5866 }
35c08157 5867 break;
fbaf61ad 5868
35c08157
KLC
5869 case R_NDS32_17IFC_PCREL_RELA:
5870 case R_NDS32_10IFCU_PCREL_RELA:
fbaf61ad 5871 /* Do nothing. */
35c08157
KLC
5872 break;
5873
1c8f6a4d
KLC
5874 case R_NDS32_TLS_LE_HI20:
5875 case R_NDS32_TLS_LE_LO12:
5876 case R_NDS32_TLS_LE_20:
5877 case R_NDS32_TLS_LE_15S0:
5878 case R_NDS32_TLS_LE_15S1:
5879 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
5880 /* We do not have garbage collection for got entries.
5881 Therefore, IE to LE may have one empty entry, and DESC to
5882 LE may have two. */
1c8f6a4d
KLC
5883 if (elf_hash_table (info)->tls_sec != NULL)
5884 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5885 break;
fbaf61ad 5886
1c8f6a4d
KLC
5887 case R_NDS32_TLS_IE_HI20:
5888 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
5889 case R_NDS32_TLS_DESC_HI20:
5890 case R_NDS32_TLS_DESC_LO12:
5891 case R_NDS32_TLS_IE_LO12:
5892 case R_NDS32_TLS_IEGP_HI20:
5893 case R_NDS32_TLS_IEGP_LO12:
5894 case R_NDS32_TLS_IEGP_LO12S2:
1c8f6a4d
KLC
5895 {
5896 /* Relocation is to the entry for this symbol in the global
5897 offset table. */
fbaf61ad 5898 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
1c8f6a4d
KLC
5899 asection *srelgot;
5900 Elf_Internal_Rela outrel;
1c8f6a4d
KLC
5901 bfd_byte *loc;
5902 int indx = 0;
5903
fbaf61ad
NC
5904 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5905
1c8f6a4d
KLC
5906 BFD_ASSERT (sgot != NULL);
5907 if (h != NULL)
5908 {
5909 bfd_boolean dyn;
5910
5911 off = h->got.offset;
fbaf61ad 5912 BFD_ASSERT (off != (bfd_vma) -1);
1c8f6a4d
KLC
5913 dyn = htab->root.dynamic_sections_created;
5914 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
0e1862bb
L
5915 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5916 && (!bfd_link_pic (info)
1c8f6a4d
KLC
5917 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5918 indx = h->dynindx;
5919 }
5920 else
5921 {
1c8f6a4d
KLC
5922 BFD_ASSERT (local_got_offsets != NULL
5923 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
1c8f6a4d 5924 off = local_got_offsets[r_symndx];
1c8f6a4d
KLC
5925 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5926 }
fbaf61ad 5927
1c8f6a4d
KLC
5928 relocation = sgot->output_section->vma + sgot->output_offset + off;
5929
fbaf61ad
NC
5930 if (1 < ones32 (tls_type))
5931 {
5932 eff_tls_type = 1 << (fls (tls_type) - 1);
5933 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5934
5935 /* TLS model X -> LE is not implement yet!
5936 workaround here! */
5937 if (eff_tls_type == GOT_TLS_LE)
5938 {
5939 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5940 }
5941 }
1c8f6a4d
KLC
5942
5943 /* The offset must always be a multiple of 4. We use
5944 the least significant bit to record whether we have
5945 already processed this entry. */
fbaf61ad
NC
5946 bfd_boolean need_relocs = FALSE;
5947 srelgot = ehtab->srelgot;
5948 if ((bfd_link_pic (info) || indx != 0)
5949 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5950 || h->root.type != bfd_link_hash_undefweak))
5951 {
5952 need_relocs = TRUE;
5953 BFD_ASSERT (srelgot != NULL);
5954 }
5955
5956 if (off & 1)
5957 {
5958 off &= ~1;
5959 relocation &= ~1;
5960
5961 if (eff_tls_type & GOT_TLS_DESC)
5962 {
5963 relocation -= elf_gp (output_bfd);
5964 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5965 {
5966 /* TLS model shall be converted. */
5967 BFD_ASSERT(0);
5968 }
5969 }
5970 else if (eff_tls_type & GOT_TLS_IEGP)
5971 {
5972 relocation -= elf_gp (output_bfd);
5973 }
5974 }
1c8f6a4d
KLC
5975 else
5976 {
fbaf61ad 5977 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
1c8f6a4d 5978 {
fbaf61ad
NC
5979 /* TLS model workaround shall be applied. */
5980 BFD_ASSERT(0);
1c8f6a4d 5981 }
fbaf61ad 5982 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
1c8f6a4d 5983 {
fbaf61ad
NC
5984 if (eff_tls_type & GOT_TLS_IEGP)
5985 relocation -= elf_gp(output_bfd);
5986
1c8f6a4d
KLC
5987 if (need_relocs)
5988 {
fbaf61ad
NC
5989 if (indx == 0)
5990 outrel.r_addend = gottpoff (info, relocation_sym);
1c8f6a4d
KLC
5991 else
5992 outrel.r_addend = 0;
5993 outrel.r_offset = (sgot->output_section->vma
fbaf61ad
NC
5994 + sgot->output_offset + off);
5995 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5996
5997 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5998 &outrel);
1c8f6a4d
KLC
5999 }
6000 else
fbaf61ad
NC
6001 {
6002 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6003 sgot->contents + off);
6004 }
1c8f6a4d 6005 }
fbaf61ad
NC
6006 else if (eff_tls_type & GOT_TLS_DESC)
6007 {
6008 relocation -= elf_gp (output_bfd);
6009 if (need_relocs)
6010 {
6011 if (indx == 0)
6012 outrel.r_addend = gottpoff (info, relocation_sym);
6013 else
6014 outrel.r_addend = 0;
6015 outrel.r_offset = (sgot->output_section->vma
6016 + sgot->output_offset + off);
6017 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
6018
6019 if (htab->tls_desc_trampoline)
6020 {
6021 asection *srelplt;
6022 srelplt = ehtab->srelplt;
6023 loc = srelplt->contents;
6024 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
6025 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
6026 <= srelplt->contents + srelplt->size);
6027
6028 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6029 }
6030 else
6031 {
6032 loc = srelgot->contents;
6033 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
6034 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6035 ++srelgot->reloc_count;
6036 }
6037 }
6038 else
6039 {
6040 /* feed me! */
6041 bfd_put_32 (output_bfd, 0xdeadbeef,
6042 sgot->contents + off);
6043 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6044 sgot->contents + off + 4);
6045 patch_tls_desc_to_ie (contents, rel, input_bfd);
6046 BFD_ASSERT(0);
6047 }
6048 }
6049 else
6050 {
6051 /* TLS model workaround shall be applied. */
6052 BFD_ASSERT(0);
6053 }
6054
6055 if (h != NULL)
6056 h->got.offset |= 1;
6057 else
6058 local_got_offsets[r_symndx] |= 1;
6059 }
6060 }
6061 break;
6062 /* DON'T fall through. */
6063
6064 default:
6065 /* OLD_NDS32_RELOC. */
6066
6067 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6068 contents, offset, relocation, addend);
6069 goto check_reloc;
6070 }
6071
6072 switch ((int) r_type)
6073 {
35c08157
KLC
6074 case R_NDS32_20_RELA:
6075 case R_NDS32_5_RELA:
6076 case R_NDS32_9_PCREL_RELA:
6077 case R_NDS32_WORD_9_PCREL_RELA:
6078 case R_NDS32_10_UPCREL_RELA:
6079 case R_NDS32_15_PCREL_RELA:
6080 case R_NDS32_17_PCREL_RELA:
6081 case R_NDS32_25_PCREL_RELA:
6082 case R_NDS32_25_ABS_RELA:
6083 case R_NDS32_HI20_RELA:
6084 case R_NDS32_LO12S3_RELA:
6085 case R_NDS32_LO12S2_RELA:
6086 case R_NDS32_LO12S2_DP_RELA:
6087 case R_NDS32_LO12S2_SP_RELA:
6088 case R_NDS32_LO12S1_RELA:
6089 case R_NDS32_LO12S0_RELA:
6090 case R_NDS32_LO12S0_ORI_RELA:
6091 case R_NDS32_SDA16S3_RELA:
6092 case R_NDS32_SDA17S2_RELA:
6093 case R_NDS32_SDA18S1_RELA:
6094 case R_NDS32_SDA19S0_RELA:
6095 case R_NDS32_SDA15S3_RELA:
6096 case R_NDS32_SDA15S2_RELA:
6097 case R_NDS32_SDA12S2_DP_RELA:
6098 case R_NDS32_SDA12S2_SP_RELA:
6099 case R_NDS32_SDA15S1_RELA:
6100 case R_NDS32_SDA15S0_RELA:
6101 case R_NDS32_SDA_FP7U2_RELA:
6102 case R_NDS32_9_PLTREL:
6103 case R_NDS32_25_PLTREL:
6104 case R_NDS32_GOT20:
6105 case R_NDS32_GOT_HI20:
6106 case R_NDS32_GOT_LO12:
6107 case R_NDS32_GOT_LO15:
6108 case R_NDS32_GOT_LO19:
6109 case R_NDS32_GOT15S2_RELA:
6110 case R_NDS32_GOT17S2_RELA:
6111 case R_NDS32_GOTPC20:
6112 case R_NDS32_GOTPC_HI20:
6113 case R_NDS32_GOTPC_LO12:
6114 case R_NDS32_GOTOFF:
6115 case R_NDS32_GOTOFF_HI20:
6116 case R_NDS32_GOTOFF_LO12:
6117 case R_NDS32_GOTOFF_LO15:
6118 case R_NDS32_GOTOFF_LO19:
6119 case R_NDS32_PLTREL_HI20:
6120 case R_NDS32_PLTREL_LO12:
6121 case R_NDS32_PLT_GOTREL_HI20:
6122 case R_NDS32_PLT_GOTREL_LO12:
6123 case R_NDS32_PLT_GOTREL_LO15:
6124 case R_NDS32_PLT_GOTREL_LO19:
6125 case R_NDS32_PLT_GOTREL_LO20:
6126 case R_NDS32_17IFC_PCREL_RELA:
6127 case R_NDS32_10IFCU_PCREL_RELA:
1c8f6a4d
KLC
6128 case R_NDS32_TLS_LE_HI20:
6129 case R_NDS32_TLS_LE_LO12:
6130 case R_NDS32_TLS_IE_HI20:
6131 case R_NDS32_TLS_IE_LO12S2:
6132 case R_NDS32_TLS_LE_20:
6133 case R_NDS32_TLS_LE_15S0:
6134 case R_NDS32_TLS_LE_15S1:
6135 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
6136 case R_NDS32_TLS_DESC_HI20:
6137 case R_NDS32_TLS_DESC_LO12:
6138 case R_NDS32_TLS_IE_LO12:
6139 case R_NDS32_TLS_IEGP_HI20:
6140 case R_NDS32_TLS_IEGP_LO12:
6141 case R_NDS32_TLS_IEGP_LO12S2:
35c08157 6142 /* Instruction related relocs must handle endian properly. */
1c8f6a4d 6143 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
35c08157
KLC
6144 r = nds32_elf_final_link_relocate (howto, input_bfd,
6145 input_section, contents,
6146 rel->r_offset, relocation,
6147 rel->r_addend);
6148 break;
6149
6150 default:
6151 /* All other relocs can use default handler. */
6152 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6153 contents, rel->r_offset,
6154 relocation, rel->r_addend);
6155 break;
6156 }
6157
6158check_reloc:
6159
6160 if (r != bfd_reloc_ok)
6161 {
6162 /* FIXME: This should be generic enough to go in a utility. */
6163 const char *name;
6164
6165 if (h != NULL)
6166 name = h->root.root.string;
6167 else
6168 {
6169 name = bfd_elf_string_from_elf_section
6170 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6171 if (name == NULL || *name == '\0')
fd361982 6172 name = bfd_section_name (sec);
35c08157
KLC
6173 }
6174
6175 if (errmsg != NULL)
6176 goto common_error;
6177
6178 switch (r)
6179 {
6180 case bfd_reloc_overflow:
1a72702b
AM
6181 (*info->callbacks->reloc_overflow)
6182 (info, (h ? &h->root : NULL), name, howto->name,
6183 (bfd_vma) 0, input_bfd, input_section, offset);
35c08157
KLC
6184 break;
6185
6186 case bfd_reloc_undefined:
1a72702b
AM
6187 (*info->callbacks->undefined_symbol)
6188 (info, name, input_bfd, input_section, offset, TRUE);
35c08157
KLC
6189 break;
6190
6191 case bfd_reloc_outofrange:
6192 errmsg = _("internal error: out of range error");
6193 goto common_error;
6194
6195 case bfd_reloc_notsupported:
6196 errmsg = _("internal error: unsupported relocation error");
6197 goto common_error;
6198
6199 case bfd_reloc_dangerous:
6200 errmsg = _("internal error: dangerous error");
6201 goto common_error;
6202
6203 default:
6204 errmsg = _("internal error: unknown error");
6205 /* Fall through. */
6206
fbaf61ad 6207common_error:
1a72702b
AM
6208 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6209 input_section, offset);
35c08157
KLC
6210 break;
6211 }
6212 }
6213 }
6214
fbaf61ad
NC
6215 /* Resotre header size to avoid overflow load. */
6216 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6217 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6218
35c08157
KLC
6219 return ret;
6220}
6221
6222/* Finish up dynamic symbol handling. We set the contents of various
6223 dynamic sections here. */
6224
6225static bfd_boolean
6226nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6227 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6228{
fbaf61ad
NC
6229 struct elf_link_hash_table *ehtab;
6230 struct elf_nds32_link_hash_entry *hent;
35c08157
KLC
6231 bfd_byte *loc;
6232
fbaf61ad
NC
6233 ehtab = elf_hash_table (info);
6234 hent = (struct elf_nds32_link_hash_entry *) h;
35c08157
KLC
6235
6236 if (h->plt.offset != (bfd_vma) - 1)
6237 {
6238 asection *splt;
6239 asection *sgot;
6240 asection *srela;
6241
6242 bfd_vma plt_index;
6243 bfd_vma got_offset;
6244 bfd_vma local_plt_offset;
6245 Elf_Internal_Rela rela;
6246
6247 /* This symbol has an entry in the procedure linkage table. Set
6248 it up. */
6249
6250 BFD_ASSERT (h->dynindx != -1);
6251
fbaf61ad
NC
6252 splt = ehtab->splt;
6253 sgot = ehtab->sgotplt;
6254 srela = ehtab->srelplt;
35c08157
KLC
6255 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6256
6257 /* Get the index in the procedure linkage table which
6258 corresponds to this symbol. This is the index of this symbol
6259 in all the symbols for which we are making plt entries. The
6260 first entry in the procedure linkage table is reserved. */
6261 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6262
6263 /* Get the offset into the .got table of the entry that
6264 corresponds to this function. Each .got entry is 4 bytes.
6265 The first three are reserved. */
6266 got_offset = (plt_index + 3) * 4;
6267
6268 /* Fill in the entry in the procedure linkage table. */
0e1862bb 6269 if (!bfd_link_pic (info))
35c08157
KLC
6270 {
6271 unsigned long insn;
6272
6273 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6274 + sgot->output_offset + got_offset) >> 12)
6275 & 0xfffff);
6276 bfd_putb32 (insn, splt->contents + h->plt.offset);
6277
6278 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6279 + sgot->output_offset + got_offset) & 0x0fff)
6280 >> 2);
6281 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6282
6283 insn = PLT_ENTRY_WORD2;
6284 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6285
6286 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6287 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6288
6289 insn = PLT_ENTRY_WORD4
6290 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6291 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6292 local_plt_offset = 12;
6293 }
6294 else
6295 {
6296 /* sda_base must be set at this time. */
6297 unsigned long insn;
6298 long offset;
6299
35c08157
KLC
6300 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6301 - elf_gp (output_bfd);
6302 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6303 bfd_putb32 (insn, splt->contents + h->plt.offset);
6304
6305 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6306 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6307
6308 insn = PLT_PIC_ENTRY_WORD2;
6309 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6310
6311 insn = PLT_PIC_ENTRY_WORD3;
6312 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6313
6314 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6315 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6316
6317 insn = PLT_PIC_ENTRY_WORD5
6318 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6319 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6320
6321 local_plt_offset = 16;
6322 }
6323
6324 /* Fill in the entry in the global offset table,
6325 so it will fall through to the next instruction for the first time. */
6326 bfd_put_32 (output_bfd,
6327 (splt->output_section->vma + splt->output_offset
6328 + h->plt.offset + local_plt_offset),
6329 sgot->contents + got_offset);
6330
6331 /* Fill in the entry in the .rela.plt section. */
6332 rela.r_offset = (sgot->output_section->vma
6333 + sgot->output_offset + got_offset);
6334 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6335 rela.r_addend = 0;
6336 loc = srela->contents;
6337 loc += plt_index * sizeof (Elf32_External_Rela);
6338 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6339
6340 if (!h->def_regular)
6341 {
6342 /* Mark the symbol as undefined, rather than as defined in
6343 the .plt section. Leave the value alone. */
6344 sym->st_shndx = SHN_UNDEF;
6345 if (!h->ref_regular_nonweak)
6346 sym->st_value = 0;
6347 }
6348 }
6349
fbaf61ad
NC
6350 if (h->got.offset != (bfd_vma) - 1
6351 && hent->tls_type == GOT_NORMAL)
35c08157
KLC
6352 {
6353 asection *sgot;
fbaf61ad 6354 asection *srelagot;
35c08157
KLC
6355 Elf_Internal_Rela rela;
6356
6357 /* This symbol has an entry in the global offset table.
6358 Set it up. */
6359
fbaf61ad
NC
6360 sgot = ehtab->sgot;
6361 srelagot = ehtab->srelgot;
6362 BFD_ASSERT (sgot != NULL && srelagot != NULL);
35c08157
KLC
6363
6364 rela.r_offset = (sgot->output_section->vma
6365 + sgot->output_offset + (h->got.offset & ~1));
6366
6367 /* If this is a -Bsymbolic link, and the symbol is defined
6368 locally, we just want to emit a RELATIVE reloc. Likewise if
6369 the symbol was forced to be local because of a version file.
6370 The entry in the global offset table will already have been
6371 initialized in the relocate_section function. */
fbaf61ad
NC
6372 if ((bfd_link_pic (info)
6373 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6374 && h->def_regular)
6375 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
6376 {
6377 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6378 rela.r_addend = (h->root.u.def.value
6379 + h->root.u.def.section->output_section->vma
6380 + h->root.u.def.section->output_offset);
fbaf61ad
NC
6381
6382 if ((h->got.offset & 1) == 0)
6383 {
6384 bfd_put_32 (output_bfd, rela.r_addend,
6385 sgot->contents + h->got.offset);
6386 }
35c08157
KLC
6387 }
6388 else
6389 {
6390 BFD_ASSERT ((h->got.offset & 1) == 0);
6391 bfd_put_32 (output_bfd, (bfd_vma) 0,
6392 sgot->contents + h->got.offset);
6393 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6394 rela.r_addend = 0;
6395 }
6396
fbaf61ad
NC
6397 loc = srelagot->contents;
6398 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
35c08157 6399 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
fbaf61ad
NC
6400 ++srelagot->reloc_count;
6401 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
35c08157
KLC
6402 }
6403
6404 if (h->needs_copy)
6405 {
6406 asection *s;
6407 Elf_Internal_Rela rela;
6408
6409 /* This symbols needs a copy reloc. Set it up. */
6410
6411 BFD_ASSERT (h->dynindx != -1
6412 && (h->root.type == bfd_link_hash_defined
6413 || h->root.type == bfd_link_hash_defweak));
6414
6415 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6416 BFD_ASSERT (s != NULL);
6417
6418 rela.r_offset = (h->root.u.def.value
6419 + h->root.u.def.section->output_section->vma
6420 + h->root.u.def.section->output_offset);
6421 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6422 rela.r_addend = 0;
6423 loc = s->contents;
6424 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6425 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6426 ++s->reloc_count;
6427 }
6428
6429 /* Mark some specially defined symbols as absolute. */
6430 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6431 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6432 sym->st_shndx = SHN_ABS;
6433
6434 return TRUE;
6435}
6436
6437
6438/* Finish up the dynamic sections. */
6439
6440static bfd_boolean
6441nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6442{
35c08157
KLC
6443 bfd *dynobj;
6444 asection *sdyn;
fbaf61ad
NC
6445 asection *sgotplt;
6446 struct elf_link_hash_table *ehtab;
6447 struct elf_nds32_link_hash_table *htab;
35c08157 6448
fbaf61ad 6449 ehtab = elf_hash_table (info);
35c08157 6450 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
6451 if (htab == NULL)
6452 return FALSE;
6453
6454 dynobj = elf_hash_table (info)->dynobj;
35c08157 6455
fbaf61ad
NC
6456 sgotplt = ehtab->sgotplt;
6457 /* A broken linker script might have discarded the dynamic sections.
6458 Catch this here so that we do not seg-fault later on. */
6459 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6460 return FALSE;
35c08157
KLC
6461 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6462
fbaf61ad 6463 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
6464 {
6465 asection *splt;
6466 Elf32_External_Dyn *dyncon, *dynconend;
6467
fbaf61ad 6468 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
35c08157
KLC
6469
6470 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6471 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6472
6473 for (; dyncon < dynconend; dyncon++)
6474 {
6475 Elf_Internal_Dyn dyn;
6476 asection *s;
6477
6478 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6479
6480 switch (dyn.d_tag)
6481 {
6482 default:
6483 break;
6484
6485 case DT_PLTGOT:
fbaf61ad
NC
6486 /* name = ".got"; */
6487 s = ehtab->sgot->output_section;
35c08157
KLC
6488 goto get_vma;
6489 case DT_JMPREL:
fbaf61ad
NC
6490 s = ehtab->srelplt->output_section;
6491get_vma:
6492 BFD_ASSERT (s != NULL);
6493 dyn.d_un.d_ptr = s->vma;
35c08157
KLC
6494 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6495 break;
6496
6497 case DT_PLTRELSZ:
fbaf61ad
NC
6498 s = ehtab->srelplt->output_section;
6499 BFD_ASSERT (s != NULL);
35c08157
KLC
6500 dyn.d_un.d_val = s->size;
6501 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6502 break;
fbaf61ad
NC
6503
6504 case DT_RELASZ:
6505 /* My reading of the SVR4 ABI indicates that the
6506 procedure linkage table relocs (DT_JMPREL) should be
6507 included in the overall relocs (DT_RELA). This is
6508 what Solaris does. However, UnixWare can not handle
6509 that case. Therefore, we override the DT_RELASZ entry
6510 here to make it not include the JMPREL relocs. Since
6511 the linker script arranges for .rela.plt to follow all
6512 other relocation sections, we don't have to worry
6513 about changing the DT_RELA entry. */
6514 if (ehtab->srelplt != NULL)
6515 {
6516 s = ehtab->srelplt->output_section;
6517 dyn.d_un.d_val -= s->size;
6518 }
6519 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6520 break;
6521
6522 case DT_TLSDESC_PLT:
6523 s = htab->root.splt;
6524 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6525 + htab->dt_tlsdesc_plt);
6526 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6527 break;
6528
6529 case DT_TLSDESC_GOT:
6530 s = htab->root.sgot;
6531 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6532 + htab->dt_tlsdesc_got);
6533 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6534 break;
35c08157
KLC
6535 }
6536 }
6537
6538 /* Fill in the first entry in the procedure linkage table. */
fbaf61ad 6539 splt = ehtab->splt;
35c08157
KLC
6540 if (splt && splt->size > 0)
6541 {
0e1862bb 6542 if (bfd_link_pic (info))
35c08157
KLC
6543 {
6544 unsigned long insn;
6545 long offset;
6546
fbaf61ad
NC
6547 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6548 - elf_gp (output_bfd);
35c08157
KLC
6549 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6550 bfd_putb32 (insn, splt->contents);
6551
35c08157
KLC
6552 /* here has a typo? */
6553 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6554 bfd_putb32 (insn, splt->contents + 4);
6555
6556 insn = PLT0_PIC_ENTRY_WORD2;
6557 bfd_putb32 (insn, splt->contents + 8);
6558
6559 insn = PLT0_PIC_ENTRY_WORD3;
6560 bfd_putb32 (insn, splt->contents + 12);
6561
6562 insn = PLT0_PIC_ENTRY_WORD4;
6563 bfd_putb32 (insn, splt->contents + 16);
6564
6565 insn = PLT0_PIC_ENTRY_WORD5;
6566 bfd_putb32 (insn, splt->contents + 20);
6567 }
6568 else
6569 {
6570 unsigned long insn;
6571 unsigned long addr;
6572
6573 /* addr = .got + 4 */
fbaf61ad 6574 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
35c08157
KLC
6575 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6576 bfd_putb32 (insn, splt->contents);
6577
6578 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6579 bfd_putb32 (insn, splt->contents + 4);
6580
6581 insn = PLT0_ENTRY_WORD2;
6582 bfd_putb32 (insn, splt->contents + 8);
6583
6584 insn = PLT0_ENTRY_WORD3;
6585 bfd_putb32 (insn, splt->contents + 12);
6586
6587 insn = PLT0_ENTRY_WORD4;
6588 bfd_putb32 (insn, splt->contents + 16);
6589 }
6590
6591 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6592 PLT_ENTRY_SIZE;
6593 }
fbaf61ad
NC
6594
6595 if (htab->dt_tlsdesc_plt)
6596 {
6597 /* Calculate addresses. */
6598 asection *sgot = sgot = ehtab->sgot;
6599 bfd_vma pltgot = sgotplt->output_section->vma
6600 + sgotplt->output_offset;
6601 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6602 + htab->dt_tlsdesc_got;
6603
6604 /* Get GP offset. */
6605 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6606 tlsdesc_got -= elf_gp (output_bfd);
6607
6608 /* Do relocation. */
6609 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6610 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6611 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6612 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6613
6614 /* Insert .plt. */
6615 nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6616 dl_tlsdesc_lazy_trampoline,
6617 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6618 }
35c08157
KLC
6619 }
6620
6621 /* Fill in the first three entries in the global offset table. */
fbaf61ad 6622 if (sgotplt && sgotplt->size > 0)
35c08157
KLC
6623 {
6624 if (sdyn == NULL)
fbaf61ad 6625 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
35c08157
KLC
6626 else
6627 bfd_put_32 (output_bfd,
6628 sdyn->output_section->vma + sdyn->output_offset,
fbaf61ad
NC
6629 sgotplt->contents);
6630 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6631 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
35c08157 6632
fbaf61ad 6633 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
35c08157
KLC
6634 }
6635
6636 return TRUE;
6637}
6638\f
6639
6640/* Set the right machine number. */
6641
6642static bfd_boolean
6643nds32_elf_object_p (bfd *abfd)
6644{
6645 static unsigned int cur_arch = 0;
6646
6647 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6648 {
6649 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6650 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6651 }
6652
6653 switch (cur_arch)
6654 {
6655 default:
6656 case E_N1_ARCH:
6657 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6658 break;
6659 case E_N1H_ARCH:
6660 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6661 break;
6662 case E_NDS_ARCH_STAR_V2_0:
6663 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6664 break;
6665 case E_NDS_ARCH_STAR_V3_0:
6666 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6667 break;
6668 case E_NDS_ARCH_STAR_V3_M:
6669 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6670 break;
6671 }
6672
6673 return TRUE;
6674}
6675
6676/* Store the machine number in the flags field. */
6677
cc364be6
AM
6678static bfd_boolean
6679nds32_elf_final_write_processing (bfd *abfd)
35c08157
KLC
6680{
6681 unsigned long val;
6682 static unsigned int cur_mach = 0;
6683
6684 if (bfd_mach_n1 != bfd_get_mach (abfd))
6685 {
6686 cur_mach = bfd_get_mach (abfd);
6687 }
6688
6689 switch (cur_mach)
6690 {
6691 case bfd_mach_n1:
6692 /* Only happen when object is empty, since the case is abandon. */
6693 val = E_N1_ARCH;
6694 val |= E_NDS_ABI_AABI;
6695 val |= E_NDS32_ELF_VER_1_4;
6696 break;
6697 case bfd_mach_n1h:
6698 val = E_N1H_ARCH;
6699 break;
6700 case bfd_mach_n1h_v2:
6701 val = E_NDS_ARCH_STAR_V2_0;
6702 break;
6703 case bfd_mach_n1h_v3:
6704 val = E_NDS_ARCH_STAR_V3_0;
6705 break;
6706 case bfd_mach_n1h_v3m:
6707 val = E_NDS_ARCH_STAR_V3_M;
6708 break;
6709 default:
6710 val = 0;
6711 break;
6712 }
6713
6714 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6715 elf_elfheader (abfd)->e_flags |= val;
cc364be6 6716 return _bfd_elf_final_write_processing (abfd);
35c08157
KLC
6717}
6718
6719/* Function to keep NDS32 specific file flags. */
6720
6721static bfd_boolean
6722nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6723{
6724 BFD_ASSERT (!elf_flags_init (abfd)
6725 || elf_elfheader (abfd)->e_flags == flags);
6726
6727 elf_elfheader (abfd)->e_flags = flags;
6728 elf_flags_init (abfd) = TRUE;
6729 return TRUE;
6730}
6731
6732static unsigned int
6733convert_e_flags (unsigned int e_flags, unsigned int arch)
6734{
6735 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6736 {
6737 /* From 0.9 to 1.0. */
6738 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6739
6740 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6741 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6742 if (arch == E_NDS_ARCH_STAR_V1_0)
6743 {
6744 /* Done. */
6745 return e_flags;
6746 }
6747 }
6748
6749 /* From 1.0 to 2.0. */
6750 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6751
6752 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6753 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6754
6755 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6756 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6757 return e_flags;
6758}
6759
6760static bfd_boolean
6761nds32_check_vec_size (bfd *ibfd)
6762{
6763 static unsigned int nds32_vec_size = 0;
6764
6765 asection *sec_t = NULL;
6766 bfd_byte *contents = NULL;
6767
6768 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6769
6770 if (sec_t && sec_t->size >= 4)
6771 {
6772 /* Get vec_size in file. */
6773 unsigned int flag_t;
6774
0c4bd9d9 6775 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
35c08157
KLC
6776 flag_t = bfd_get_32 (ibfd, contents);
6777
6778 /* The value could only be 4 or 16. */
6779
6780 if (!nds32_vec_size)
6781 /* Set if not set yet. */
6782 nds32_vec_size = (flag_t & 0x3);
6783 else if (nds32_vec_size != (flag_t & 0x3))
6784 {
4eca0228 6785 _bfd_error_handler
695344c0 6786 /* xgettext:c-format */
871b3ab2 6787 (_("%pB: ISR vector size mismatch"
4eca0228
AM
6788 " with previous modules, previous %u-byte, current %u-byte"),
6789 ibfd,
6790 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6791 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
35c08157
KLC
6792 return FALSE;
6793 }
6794 else
6795 /* Only keep the first vec_size section. */
6796 sec_t->flags |= SEC_EXCLUDE;
6797 }
6798
6799 return TRUE;
6800}
6801
6802/* Merge backend specific data from an object file to the output
6803 object file when linking. */
6804
6805static bfd_boolean
50e03d47 6806nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
35c08157 6807{
50e03d47 6808 bfd *obfd = info->output_bfd;
35c08157
KLC
6809 flagword out_flags;
6810 flagword in_flags;
6811 flagword out_16regs;
6812 flagword in_no_mac;
6813 flagword out_no_mac;
6814 flagword in_16regs;
6815 flagword out_version;
6816 flagword in_version;
6817 flagword out_fpu_config;
6818 flagword in_fpu_config;
6819
6820 /* TODO: Revise to use object-attributes instead. */
6821 if (!nds32_check_vec_size (ibfd))
6822 return FALSE;
6823
6824 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6825 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6826 return TRUE;
6827
6828 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6829 {
4eca0228 6830 _bfd_error_handler
38f14ab8 6831 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
35c08157
KLC
6832
6833 bfd_set_error (bfd_error_bad_value);
6834 return FALSE;
6835 }
6836
fbaf61ad
NC
6837 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6838 treat as generic one without checking and merging. */
6839 if (elf_elfheader (ibfd)->e_flags)
35c08157 6840 {
fbaf61ad
NC
6841 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6842 if (in_version == E_NDS32_ELF_VER_1_2)
35c08157 6843 {
fbaf61ad
NC
6844 _bfd_error_handler
6845 (_("%pB: warning: older version of object file encountered, "
6846 "please recompile with current tool chain"), ibfd);
35c08157 6847 }
35c08157 6848
fbaf61ad
NC
6849 /* We may need to merge V1 and V2 arch object files to V2. */
6850 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6851 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
35c08157 6852 {
fbaf61ad
NC
6853 /* Need to convert version. */
6854 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6855 == E_NDS_ARCH_STAR_RESERVED)
6856 {
6857 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6858 }
6859 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6860 == E_NDS_ARCH_STAR_V3_M
6861 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6862 == E_NDS_ARCH_STAR_V3_0)
6863 {
6864 elf_elfheader (ibfd)->e_flags =
6865 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6866 | E_NDS_ARCH_STAR_V3_0;
6867 }
6868 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6869 == E_NDS_ARCH_STAR_V0_9
6870 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6871 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6872 {
6873 elf_elfheader (obfd)->e_flags =
6874 convert_e_flags (elf_elfheader (obfd)->e_flags,
6875 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6876 }
6877 else
6878 {
6879 elf_elfheader (ibfd)->e_flags =
6880 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6881 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6882 }
35c08157
KLC
6883 }
6884
fbaf61ad
NC
6885 /* Extract some flags. */
6886 in_flags = elf_elfheader (ibfd)->e_flags
6887 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6888 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6889
6890 /* The following flags need special treatment. */
6891 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6892 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6893 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6894
6895 /* Extract some flags. */
6896 out_flags = elf_elfheader (obfd)->e_flags
6897 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6898 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6899
6900 /* The following flags need special treatment. */
6901 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6902 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6903 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6904 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6905 if (!elf_flags_init (obfd))
6906 {
6907 /* If the input is the default architecture then do not
6908 bother setting the flags for the output architecture,
6909 instead allow future merges to do this. If no future
6910 merges ever set these flags then they will retain their
6911 unitialised values, which surprise surprise, correspond
6912 to the default values. */
6913 if (bfd_get_arch_info (ibfd)->the_default)
6914 return TRUE;
6915
6916 elf_flags_init (obfd) = TRUE;
6917 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
35c08157 6918
fbaf61ad
NC
6919 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6920 && bfd_get_arch_info (obfd)->the_default)
6921 {
6922 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6923 bfd_get_mach (ibfd));
6924 }
35c08157 6925
fbaf61ad
NC
6926 return TRUE;
6927 }
35c08157 6928
fbaf61ad
NC
6929 /* Check flag compatibility. */
6930 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
35c08157 6931 {
4eca0228 6932 _bfd_error_handler
fbaf61ad 6933 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
35c08157
KLC
6934 bfd_set_error (bfd_error_bad_value);
6935 return FALSE;
6936 }
35c08157 6937
fbaf61ad
NC
6938 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6939 {
6940 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6941 {
6942 _bfd_error_handler
6943 (_("%pB: error: instruction set mismatch with previous modules"),
6944 ibfd);
6945
6946 bfd_set_error (bfd_error_bad_value);
6947 return FALSE;
6948 }
6949 }
6950
6951 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6952 and perf ext1 and DIV are mergerd to perf ext1. */
6953 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6954 {
6955 elf_elfheader (obfd)->e_flags =
6956 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6957 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6958 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6959 ? E_NDS32_HAS_EXT_INST : 0)
6960 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6961 ? E_NDS32_HAS_EXT_INST : 0)
6962 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6963 | ((in_version > out_version) ? out_version : in_version);
6964 }
6965 else
6966 {
6967 if (in_version != out_version)
6968 _bfd_error_handler
6969 /* xgettext:c-format */
6970 (_("%pB: warning: incompatible elf-versions %s and %s"),
6971 ibfd, nds32_elfver_strtab[out_version],
6972 nds32_elfver_strtab[in_version]);
35c08157 6973
fbaf61ad
NC
6974 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6975 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6976 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6977 | (in_version > out_version ? out_version : in_version);
6978 }
35c08157
KLC
6979 }
6980
6981 return TRUE;
6982}
6983
6984/* Display the flags field. */
6985
6986static bfd_boolean
6987nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6988{
6989 FILE *file = (FILE *) ptr;
6990
6991 BFD_ASSERT (abfd != NULL && ptr != NULL);
6992
6993 _bfd_elf_print_private_bfd_data (abfd, ptr);
6994
6995 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6996
6997 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6998 {
6999 default:
7000 case E_N1_ARCH:
7001 fprintf (file, _(": n1 instructions"));
7002 break;
7003 case E_N1H_ARCH:
7004 fprintf (file, _(": n1h instructions"));
7005 break;
7006 }
7007
7008 fputc ('\n', file);
7009
7010 return TRUE;
7011}
7012
7013static unsigned int
7014nds32_elf_action_discarded (asection *sec)
7015{
7016
7017 if (strncmp
7018 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7019 return 0;
7020
7021 return _bfd_elf_default_action_discarded (sec);
7022}
7023
7024static asection *
7025nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7026 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7027 Elf_Internal_Sym *sym)
7028{
7029 if (h != NULL)
7030 switch (ELF32_R_TYPE (rel->r_info))
7031 {
7032 case R_NDS32_GNU_VTINHERIT:
7033 case R_NDS32_GNU_VTENTRY:
7034 case R_NDS32_RELA_GNU_VTINHERIT:
7035 case R_NDS32_RELA_GNU_VTENTRY:
7036 return NULL;
7037 }
7038
7039 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7040}
7041
fbaf61ad
NC
7042static enum elf_nds32_tls_type
7043get_tls_type (enum elf_nds32_reloc_type r_type,
7044 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7045{
7046 enum elf_nds32_tls_type tls_type;
7047
7048 switch (r_type)
7049 {
7050 case R_NDS32_TLS_LE_HI20:
7051 case R_NDS32_TLS_LE_LO12:
7052 tls_type = GOT_TLS_LE;
7053 break;
7054 case R_NDS32_TLS_IE_HI20:
7055 case R_NDS32_TLS_IE_LO12S2:
7056 case R_NDS32_TLS_IE_LO12:
7057 tls_type = GOT_TLS_IE;
7058 break;
7059 case R_NDS32_TLS_IEGP_HI20:
7060 case R_NDS32_TLS_IEGP_LO12:
7061 case R_NDS32_TLS_IEGP_LO12S2:
7062 tls_type = GOT_TLS_IEGP;
7063 break;
7064 case R_NDS32_TLS_DESC_HI20:
7065 case R_NDS32_TLS_DESC_LO12:
7066 case R_NDS32_TLS_DESC_ADD:
7067 case R_NDS32_TLS_DESC_FUNC:
7068 case R_NDS32_TLS_DESC_CALL:
7069 tls_type = GOT_TLS_DESC;
7070 break;
7071 default:
7072 tls_type = GOT_NORMAL;
7073 break;
7074 }
7075
7076 return tls_type;
7077}
7078
7079/* Ensure that we have allocated bookkeeping structures for ABFD's local
7080 symbols. */
7081
7082static bfd_boolean
7083elf32_nds32_allocate_local_sym_info (bfd *abfd)
7084{
7085 if (elf_local_got_refcounts (abfd) == NULL)
7086 {
7087 bfd_size_type num_syms;
7088 bfd_size_type size;
7089 char *data;
7090
7091 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7092 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7093 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7094 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7095 + sizeof (bfd_vma) + sizeof (int)
7096 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7097 data = bfd_zalloc (abfd, size);
7098 if (data == NULL)
7099 return FALSE;
7100
7101 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7102 data += num_syms * sizeof (bfd_signed_vma);
7103
7104 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7105 data += num_syms * sizeof (char);
7106
7107 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7108 data += num_syms * sizeof (bfd_vma);
7109
7110 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7111 data += num_syms * sizeof (int);
7112 }
7113
7114 return TRUE;
7115}
7116
35c08157
KLC
7117/* Look through the relocs for a section during the first phase.
7118 Since we don't do .gots or .plts, we just need to consider the
7119 virtual table relocs for gc. */
7120
7121static bfd_boolean
7122nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7123 asection *sec, const Elf_Internal_Rela *relocs)
7124{
7125 Elf_Internal_Shdr *symtab_hdr;
7126 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7127 const Elf_Internal_Rela *rel;
7128 const Elf_Internal_Rela *rel_end;
fbaf61ad 7129 struct elf_link_hash_table *ehtab;
35c08157
KLC
7130 struct elf_nds32_link_hash_table *htab;
7131 bfd *dynobj;
7132 asection *sreloc = NULL;
7133
fbaf61ad 7134 /* No need for relocation if relocatable already. */
0e1862bb 7135 if (bfd_link_relocatable (info))
fbaf61ad
NC
7136 {
7137 elf32_nds32_check_relax_group (abfd, sec);
7138 return TRUE;
7139 }
35c08157 7140
65281396
AM
7141 /* Don't do anything special with non-loaded, non-alloced sections.
7142 In particular, any relocs in such sections should not affect GOT
7143 and PLT reference counting (ie. we don't allow them to create GOT
7144 or PLT entries), there's no possibility or desire to optimize TLS
7145 relocs, and there's not much point in propagating relocs to shared
7146 libs that the dynamic linker won't relocate. */
7147 if ((sec->flags & SEC_ALLOC) == 0)
7148 return TRUE;
7149
35c08157
KLC
7150 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7151 sym_hashes = elf_sym_hashes (abfd);
7152 sym_hashes_end =
7153 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7154 if (!elf_bad_symtab (abfd))
7155 sym_hashes_end -= symtab_hdr->sh_info;
7156
fbaf61ad 7157 ehtab = elf_hash_table (info);
35c08157
KLC
7158 htab = nds32_elf_hash_table (info);
7159 dynobj = htab->root.dynobj;
7160
7161 rel_end = relocs + sec->reloc_count;
7162 for (rel = relocs; rel < rel_end; rel++)
7163 {
7164 enum elf_nds32_reloc_type r_type;
7165 struct elf_link_hash_entry *h;
7166 unsigned long r_symndx;
fbaf61ad 7167 enum elf_nds32_tls_type tls_type, old_tls_type;
35c08157
KLC
7168
7169 r_symndx = ELF32_R_SYM (rel->r_info);
7170 r_type = ELF32_R_TYPE (rel->r_info);
7171 if (r_symndx < symtab_hdr->sh_info)
7172 h = NULL;
7173 else
7174 {
7175 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7176 while (h->root.type == bfd_link_hash_indirect
7177 || h->root.type == bfd_link_hash_warning)
7178 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7179 }
7180
fbaf61ad
NC
7181 /* Create .got section if necessary.
7182 Some relocs require a global offset table. We create
7183 got section here, since these relocation need a got section
7184 and if it is not created yet. */
7185 if (ehtab->sgot == NULL)
35c08157
KLC
7186 {
7187 switch (r_type)
7188 {
7189 case R_NDS32_GOT_HI20:
7190 case R_NDS32_GOT_LO12:
7191 case R_NDS32_GOT_LO15:
7192 case R_NDS32_GOT_LO19:
7193 case R_NDS32_GOT17S2_RELA:
7194 case R_NDS32_GOT15S2_RELA:
7195 case R_NDS32_GOTOFF:
7196 case R_NDS32_GOTOFF_HI20:
7197 case R_NDS32_GOTOFF_LO12:
7198 case R_NDS32_GOTOFF_LO15:
7199 case R_NDS32_GOTOFF_LO19:
7200 case R_NDS32_GOTPC20:
7201 case R_NDS32_GOTPC_HI20:
7202 case R_NDS32_GOTPC_LO12:
7203 case R_NDS32_GOT20:
1c8f6a4d 7204 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7205 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7206 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7207 case R_NDS32_TLS_IEGP_HI20:
7208 case R_NDS32_TLS_IEGP_LO12:
7209 case R_NDS32_TLS_IEGP_LO12S2:
7210 case R_NDS32_TLS_DESC_HI20:
7211 case R_NDS32_TLS_DESC_LO12:
35c08157
KLC
7212 if (dynobj == NULL)
7213 htab->root.dynobj = dynobj = abfd;
fbaf61ad 7214 if (!create_got_section (dynobj, info))
35c08157
KLC
7215 return FALSE;
7216 break;
7217
7218 default:
7219 break;
7220 }
7221 }
7222
fbaf61ad 7223 /* Check relocation type. */
35c08157
KLC
7224 switch ((int) r_type)
7225 {
7226 case R_NDS32_GOT_HI20:
7227 case R_NDS32_GOT_LO12:
7228 case R_NDS32_GOT_LO15:
7229 case R_NDS32_GOT_LO19:
7230 case R_NDS32_GOT20:
fbaf61ad
NC
7231 case R_NDS32_TLS_LE_HI20:
7232 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 7233 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7234 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7235 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7236 case R_NDS32_TLS_IEGP_HI20:
7237 case R_NDS32_TLS_IEGP_LO12:
7238 case R_NDS32_TLS_IEGP_LO12S2:
7239 case R_NDS32_TLS_DESC_HI20:
7240 case R_NDS32_TLS_DESC_LO12:
7241 tls_type = get_tls_type (r_type, h);
7242 if (h)
1c8f6a4d 7243 {
fbaf61ad
NC
7244 if (tls_type != GOT_TLS_LE)
7245 h->got.refcount += 1;
1c8f6a4d 7246 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
1c8f6a4d 7247 }
35c08157
KLC
7248 else
7249 {
fbaf61ad
NC
7250 /* This is a global offset table entry for a local symbol. */
7251 if (!elf32_nds32_allocate_local_sym_info (abfd))
7252 return FALSE;
35c08157 7253
fbaf61ad
NC
7254 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7255 if (tls_type != GOT_TLS_LE)
7256 elf_local_got_refcounts (abfd)[r_symndx] += 1;
1c8f6a4d 7257 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
35c08157 7258 }
35c08157 7259
fbaf61ad 7260 /* We would already have issued an error message if there
1c8f6a4d
KLC
7261 is a TLS/non-TLS mismatch, based on the symbol
7262 type. So just combine any TLS types needed. */
7263 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7264 && tls_type != GOT_NORMAL)
7265 tls_type |= old_tls_type;
7266
fbaf61ad
NC
7267 /* DESC to IE/IEGP if link to executable. */
7268 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7269 && (bfd_link_executable (info)))
7270 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7271
1c8f6a4d
KLC
7272 if (old_tls_type != tls_type)
7273 {
7274 if (h != NULL)
7275 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7276 else
7277 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7278 }
7279 break;
7280 case R_NDS32_9_PLTREL:
35c08157
KLC
7281 case R_NDS32_25_PLTREL:
7282 case R_NDS32_PLTREL_HI20:
7283 case R_NDS32_PLTREL_LO12:
7284 case R_NDS32_PLT_GOTREL_HI20:
7285 case R_NDS32_PLT_GOTREL_LO12:
7286 case R_NDS32_PLT_GOTREL_LO15:
7287 case R_NDS32_PLT_GOTREL_LO19:
7288 case R_NDS32_PLT_GOTREL_LO20:
7289
7290 /* This symbol requires a procedure linkage table entry. We
7291 actually build the entry in adjust_dynamic_symbol,
7292 because this might be a case of linking PIC code without
7293 linking in any dynamic objects, in which case we don't
7294 need to generate a procedure linkage table after all. */
7295
7296 /* If this is a local symbol, we resolve it directly without
7297 creating a procedure linkage table entry. */
7298 if (h == NULL)
7299 continue;
7300
fbaf61ad
NC
7301 if (h->forced_local
7302 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
7303 break;
7304
1c8f6a4d 7305 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
35c08157
KLC
7306 h->needs_plt = 1;
7307 h->plt.refcount += 1;
7308 break;
7309
7310 case R_NDS32_16_RELA:
7311 case R_NDS32_20_RELA:
7312 case R_NDS32_5_RELA:
7313 case R_NDS32_32_RELA:
7314 case R_NDS32_HI20_RELA:
7315 case R_NDS32_LO12S3_RELA:
7316 case R_NDS32_LO12S2_RELA:
7317 case R_NDS32_LO12S2_DP_RELA:
7318 case R_NDS32_LO12S2_SP_RELA:
7319 case R_NDS32_LO12S1_RELA:
7320 case R_NDS32_LO12S0_RELA:
7321 case R_NDS32_LO12S0_ORI_RELA:
7322 case R_NDS32_SDA16S3_RELA:
7323 case R_NDS32_SDA17S2_RELA:
7324 case R_NDS32_SDA18S1_RELA:
7325 case R_NDS32_SDA19S0_RELA:
7326 case R_NDS32_SDA15S3_RELA:
7327 case R_NDS32_SDA15S2_RELA:
7328 case R_NDS32_SDA12S2_DP_RELA:
7329 case R_NDS32_SDA12S2_SP_RELA:
7330 case R_NDS32_SDA15S1_RELA:
7331 case R_NDS32_SDA15S0_RELA:
7332 case R_NDS32_SDA_FP7U2_RELA:
7333 case R_NDS32_15_PCREL_RELA:
7334 case R_NDS32_17_PCREL_RELA:
7335 case R_NDS32_25_PCREL_RELA:
7336
0e1862bb 7337 if (h != NULL && !bfd_link_pic (info))
35c08157
KLC
7338 {
7339 h->non_got_ref = 1;
7340 h->plt.refcount += 1;
7341 }
7342
7343 /* If we are creating a shared library, and this is a reloc against
7344 a global symbol, or a non PC relative reloc against a local
7345 symbol, then we need to copy the reloc into the shared library.
7346 However, if we are linking with -Bsymbolic, we do not need to
7347 copy a reloc against a global symbol which is defined in an
7348 object we are including in the link (i.e., DEF_REGULAR is set).
7349 At this point we have not seen all the input files, so it is
7350 possible that DEF_REGULAR is not set now but will be set later
7351 (it is never cleared). We account for that possibility below by
7352 storing information in the dyn_relocs field of the hash table
7353 entry. A similar situation occurs when creating shared libraries
7354 and symbol visibility changes render the symbol local.
7355
7356 If on the other hand, we are creating an executable, we may need
7357 to keep relocations for symbols satisfied by a dynamic library
7358 if we manage to avoid copy relocs for the symbol. */
0e1862bb 7359 if ((bfd_link_pic (info)
35c08157
KLC
7360 && (sec->flags & SEC_ALLOC) != 0
7361 && ((r_type != R_NDS32_25_PCREL_RELA
7362 && r_type != R_NDS32_15_PCREL_RELA
7363 && r_type != R_NDS32_17_PCREL_RELA
7364 && !(r_type == R_NDS32_32_RELA
7365 && strcmp (sec->name, ".eh_frame") == 0))
7366 || (h != NULL
7367 && (!info->symbolic
7368 || h->root.type == bfd_link_hash_defweak
7369 || !h->def_regular))))
0e1862bb 7370 || (!bfd_link_pic (info)
35c08157
KLC
7371 && (sec->flags & SEC_ALLOC) != 0
7372 && h != NULL
7373 && (h->root.type == bfd_link_hash_defweak
7374 || !h->def_regular)))
7375 {
3bf083ed
AM
7376 struct elf_dyn_relocs *p;
7377 struct elf_dyn_relocs **head;
35c08157
KLC
7378
7379 if (dynobj == NULL)
7380 htab->root.dynobj = dynobj = abfd;
7381
7382 /* When creating a shared object, we must copy these
7383 relocs into the output file. We create a reloc
7384 section in dynobj and make room for the reloc. */
7385 if (sreloc == NULL)
7386 {
7387 const char *name;
7388
7389 name = bfd_elf_string_from_elf_section
7390 (abfd, elf_elfheader (abfd)->e_shstrndx,
7391 elf_section_data (sec)->rela.hdr->sh_name);
7392 if (name == NULL)
7393 return FALSE;
7394
7395 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
fd361982 7396 && strcmp (bfd_section_name (sec),
35c08157
KLC
7397 name + 5) == 0);
7398
7399 sreloc = bfd_get_section_by_name (dynobj, name);
7400 if (sreloc == NULL)
7401 {
7402 flagword flags;
7403
7404 sreloc = bfd_make_section (dynobj, name);
7405 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7406 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7407 if ((sec->flags & SEC_ALLOC) != 0)
7408 flags |= SEC_ALLOC | SEC_LOAD;
7409 if (sreloc == NULL
fd361982
AM
7410 || !bfd_set_section_flags (sreloc, flags)
7411 || !bfd_set_section_alignment (sreloc, 2))
35c08157
KLC
7412 return FALSE;
7413
7414 elf_section_type (sreloc) = SHT_RELA;
7415 }
7416 elf_section_data (sec)->sreloc = sreloc;
7417 }
7418
7419 /* If this is a global symbol, we count the number of
7420 relocations we need for this symbol. */
7421 if (h != NULL)
7422 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
7423 else
7424 {
7425 asection *s;
61034b0b 7426 void *vpp;
35c08157
KLC
7427
7428 Elf_Internal_Sym *isym;
7429 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7430 if (isym == NULL)
7431 return FALSE;
7432
7433 /* Track dynamic relocs needed for local syms too. */
7434 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7435 if (s == NULL)
7436 return FALSE;
7437
61034b0b 7438 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 7439 head = (struct elf_dyn_relocs **) vpp;
35c08157
KLC
7440 }
7441
7442 p = *head;
7443 if (p == NULL || p->sec != sec)
7444 {
7445 bfd_size_type amt = sizeof (*p);
3bf083ed 7446 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
35c08157
KLC
7447 if (p == NULL)
7448 return FALSE;
7449 p->next = *head;
7450 *head = p;
7451 p->sec = sec;
7452 p->count = 0;
7453 p->pc_count = 0;
7454 }
7455
7456 p->count += 1;
fbaf61ad
NC
7457
7458 /* Since eh_frame is readonly, R_NDS32_32_RELA
7459 reloc for eh_frame will cause shared library has
7460 TEXTREL entry in the dynamic section. This lead glibc
7461 testsuites to failure (bug-13092) and cause kernel fail
7462 (bug-11819). I think the best solution is to replace
7463 absolute reloc with pc relative reloc in the eh_frame.
7464 To do that, we need to support the following issues:
7465
7466 === For GCC ===
7467 * gcc/config/nds32/nds32.h: Define
7468 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7469 and DW_EH_PE_sdata4 into DWARF exception header when
7470 option have '-fpic'.
7471
7472 === For binutils ===
7473 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7474 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7475 may break our nds DIFF mechanism, therefore, we
7476 must disable all linker relaxations to ensure
7477 correctness.
7478 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7479 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7480 do the necessary modification.
7481
7482 Unfortunately, it still have some problems for nds32
7483 to support pc relative reloc in the eh_frame. So I use
7484 another solution to fix this issue.
7485
7486 However, I find that ld always emit TEXTREL marker for
7487 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7488 correspond to R_NDS32_32_RELA for .eh_frame section.
7489 It means that we always reserve redundant entries of rel.dyn
7490 for these relocs which actually do nothing in dynamic linker.
7491
7492 Therefore, we regard these relocs as pc relative relocs
7493 here and increase the pc_count. */
35c08157
KLC
7494 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7495 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
fbaf61ad
NC
7496 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7497 || (r_type == R_NDS32_32_RELA
7498 && strcmp (sec->name, ".eh_frame") == 0))
35c08157
KLC
7499 p->pc_count += 1;
7500 }
7501 break;
7502
7503 /* This relocation describes the C++ object vtable hierarchy.
7504 Reconstruct it for later use during GC. */
7505 case R_NDS32_RELA_GNU_VTINHERIT:
7506 case R_NDS32_GNU_VTINHERIT:
7507 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7508 return FALSE;
7509 break;
7510
7511 /* This relocation describes which C++ vtable entries are actually
7512 used. Record for later use during GC. */
7513 case R_NDS32_GNU_VTENTRY:
7514 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7515 return FALSE;
7516 break;
7517 case R_NDS32_RELA_GNU_VTENTRY:
7518 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7519 return FALSE;
7520 break;
7521 }
7522 }
7523
7524 return TRUE;
7525}
7526
7527/* Write VAL in uleb128 format to P, returning a pointer to the
7528 following byte.
7529 This code is copied from elf-attr.c. */
7530
7531static bfd_byte *
7532write_uleb128 (bfd_byte *p, unsigned int val)
7533{
7534 bfd_byte c;
7535 do
7536 {
7537 c = val & 0x7f;
7538 val >>= 7;
7539 if (val)
7540 c |= 0x80;
7541 *(p++) = c;
7542 }
7543 while (val);
7544 return p;
7545}
7546
7547static bfd_signed_vma
7548calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
fbaf61ad 7549 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
35c08157
KLC
7550{
7551 bfd_signed_vma foff;
7552 bfd_vma symval, addend;
7553 asection *sym_sec;
7554
7555 /* Get the value of the symbol referred to by the reloc. */
7556 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7557 {
7558 Elf_Internal_Sym *isym;
7559
7560 /* A local symbol. */
7561 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7562
7563 if (isym->st_shndx == SHN_UNDEF)
7564 sym_sec = bfd_und_section_ptr;
7565 else if (isym->st_shndx == SHN_ABS)
7566 sym_sec = bfd_abs_section_ptr;
7567 else if (isym->st_shndx == SHN_COMMON)
7568 sym_sec = bfd_com_section_ptr;
7569 else
7570 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7571 symval = isym->st_value + sym_sec->output_section->vma
7572 + sym_sec->output_offset;
7573 }
7574 else
7575 {
7576 unsigned long indx;
7577 struct elf_link_hash_entry *h;
35c08157
KLC
7578
7579 /* An external symbol. */
7580 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7581 h = elf_sym_hashes (abfd)[indx];
7582 BFD_ASSERT (h != NULL);
7583
7584 if (h->root.type != bfd_link_hash_defined
7585 && h->root.type != bfd_link_hash_defweak)
7586 /* This appears to be a reference to an undefined
7587 symbol. Just ignore it--it will be caught by the
7588 regular reloc processing. */
7589 return 0;
35c08157
KLC
7590
7591 if (h->root.u.def.section->flags & SEC_MERGE)
7592 {
7593 sym_sec = h->root.u.def.section;
7594 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7595 elf_section_data (sym_sec)->sec_info,
7596 h->root.u.def.value);
7597 symval = symval + sym_sec->output_section->vma
7598 + sym_sec->output_offset;
7599 }
7600 else
7601 symval = (h->root.u.def.value
7602 + h->root.u.def.section->output_section->vma
7603 + h->root.u.def.section->output_offset);
7604 }
7605
7606 addend = irel->r_addend;
7607
7608 foff = (symval + addend
7609 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7610 return foff;
7611}
35c08157 7612\f
fbaf61ad 7613
35c08157
KLC
7614/* Convert a 32-bit instruction to 16-bit one.
7615 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7616 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7617 type of INSN16. Return 1 if successful. */
7618
7619static int
7620nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7621 int *pinsn_type)
7622{
7623 uint16_t insn16 = 0;
6cae483a 7624 int insn_type = 0;
35c08157
KLC
7625 unsigned long mach = bfd_get_mach (abfd);
7626
7627 if (N32_SH5 (insn) != 0)
7628 return 0;
7629
7630 switch (N32_SUB5 (insn))
7631 {
7632 case N32_ALU1_ADD_SLLI:
7633 case N32_ALU1_ADD_SRLI:
7634 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7635 {
7636 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7637 N32_RB5 (insn));
7638 insn_type = NDS32_INSN_ADD333;
7639 }
7640 else if (N32_IS_RT4 (insn))
7641 {
7642 if (N32_RT5 (insn) == N32_RA5 (insn))
7643 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7644 else if (N32_RT5 (insn) == N32_RB5 (insn))
7645 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7646 insn_type = NDS32_INSN_ADD45;
7647 }
7648 break;
7649
7650 case N32_ALU1_SUB_SLLI:
7651 case N32_ALU1_SUB_SRLI:
7652 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7653 {
7654 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7655 N32_RB5 (insn));
7656 insn_type = NDS32_INSN_SUB333;
7657 }
7658 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7659 {
7660 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7661 insn_type = NDS32_INSN_SUB45;
7662 }
7663 break;
7664
7665 case N32_ALU1_AND_SLLI:
7666 case N32_ALU1_AND_SRLI:
7667 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7668 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7669 && N32_IS_RB3 (insn))
7670 {
7671 if (N32_RT5 (insn) == N32_RA5 (insn))
7672 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7673 else if (N32_RT5 (insn) == N32_RB5 (insn))
7674 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7675 if (insn16)
7676 insn_type = NDS32_INSN_AND33;
7677 }
7678 break;
7679
7680 case N32_ALU1_XOR_SLLI:
7681 case N32_ALU1_XOR_SRLI:
7682 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7683 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7684 && N32_IS_RB3 (insn))
7685 {
7686 if (N32_RT5 (insn) == N32_RA5 (insn))
7687 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7688 else if (N32_RT5 (insn) == N32_RB5 (insn))
7689 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7690 if (insn16)
7691 insn_type = NDS32_INSN_XOR33;
7692 }
7693 break;
7694
7695 case N32_ALU1_OR_SLLI:
7696 case N32_ALU1_OR_SRLI:
7697 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7698 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7699 && N32_IS_RB3 (insn))
7700 {
7701 if (N32_RT5 (insn) == N32_RA5 (insn))
7702 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7703 else if (N32_RT5 (insn) == N32_RB5 (insn))
7704 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7705 if (insn16)
7706 insn_type = NDS32_INSN_OR33;
7707 }
7708 break;
7709 case N32_ALU1_NOR:
7710 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7711 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7712 && N32_RA5 (insn) == N32_RB5 (insn))
7713 {
7714 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7715 insn_type = NDS32_INSN_NOT33;
7716 }
7717 break;
7718 case N32_ALU1_SRAI:
7719 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7720 {
7721 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7722 insn_type = NDS32_INSN_SRAI45;
7723 }
7724 break;
7725
7726 case N32_ALU1_SRLI:
7727 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7728 {
7729 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7730 insn_type = NDS32_INSN_SRLI45;
7731 }
7732 break;
7733
7734 case N32_ALU1_SLLI:
7735 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7736 {
7737 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7738 N32_UB5 (insn));
7739 insn_type = NDS32_INSN_SLLI333;
7740 }
7741 break;
7742
7743 case N32_ALU1_ZEH:
7744 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7745 {
7746 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7747 insn_type = NDS32_INSN_ZEH33;
7748 }
7749 break;
7750
7751 case N32_ALU1_SEB:
7752 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7753 {
7754 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7755 insn_type = NDS32_INSN_SEB33;
7756 }
7757 break;
7758
7759 case N32_ALU1_SEH:
7760 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7761 {
7762 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7763 insn_type = NDS32_INSN_SEH33;
7764 }
7765 break;
7766
7767 case N32_ALU1_SLT:
7768 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7769 {
7770 /* Implicit r15. */
7771 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7772 insn_type = NDS32_INSN_SLT45;
7773 }
7774 break;
7775
7776 case N32_ALU1_SLTS:
7777 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7778 {
7779 /* Implicit r15. */
7780 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7781 insn_type = NDS32_INSN_SLTS45;
7782 }
7783 break;
7784 }
7785
7786 if ((insn16 & 0x8000) == 0)
7787 return 0;
7788
7789 if (pinsn16)
7790 *pinsn16 = insn16;
7791 if (pinsn_type)
7792 *pinsn_type = insn_type;
7793 return 1;
7794}
7795
7796static int
7797nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7798 int *pinsn_type)
7799{
7800 uint16_t insn16 = 0;
7801 int insn_type;
7802 unsigned long mach = bfd_get_mach (abfd);
7803
7804 /* TODO: bset, bclr, btgl, btst. */
7805 if (__GF (insn, 6, 4) != 0)
7806 return 0;
7807
7808 switch (N32_IMMU (insn, 6))
7809 {
7810 case N32_ALU2_MUL:
7811 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7812 && N32_IS_RB3 (insn))
7813 {
7814 if (N32_RT5 (insn) == N32_RA5 (insn))
7815 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7816 else if (N32_RT5 (insn) == N32_RB5 (insn))
7817 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7818 if (insn16)
7819 insn_type = NDS32_INSN_MUL33;
7820 }
7821 }
7822
7823 if ((insn16 & 0x8000) == 0)
7824 return 0;
7825
7826 if (pinsn16)
7827 *pinsn16 = insn16;
7828 if (pinsn_type)
7829 *pinsn_type = insn_type;
7830 return 1;
7831}
7832
7833int
7834nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7835 int *pinsn_type)
7836{
7837 int op6;
7838 uint16_t insn16 = 0;
1624c9ca 7839 int insn_type = 0;
35c08157
KLC
7840 unsigned long mach = bfd_get_mach (abfd);
7841
7842 /* Decode 32-bit instruction. */
7843 if (insn & 0x80000000)
7844 {
7845 /* Not 32-bit insn. */
7846 return 0;
7847 }
7848
7849 op6 = N32_OP6 (insn);
7850
7851 /* Convert it to 16-bit instruction. */
7852 switch (op6)
7853 {
7854 case N32_OP6_MOVI:
7855 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7856 {
7857 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7858 insn_type = NDS32_INSN_MOVI55;
7859 }
7860 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7861 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7862 {
7863 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7864 N32_IMM20S (insn) - 16);
7865 insn_type = NDS32_INSN_MOVPI45;
7866 }
7867 break;
7868
7869 case N32_OP6_ADDI:
7870 if (N32_IMM15S (insn) == 0)
7871 {
7872 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7873 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7874 if (mach <= MACH_V2
7875 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7876 {
7877 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7878 insn_type = NDS32_INSN_MOV55;
7879 }
7880 }
7881 else if (N32_IMM15S (insn) > 0)
7882 {
7883 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7884 {
7885 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7886 N32_IMM15S (insn));
7887 insn_type = NDS32_INSN_ADDI333;
7888 }
7889 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7890 && N32_IMM15S (insn) < 32)
7891 {
7892 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7893 insn_type = NDS32_INSN_ADDI45;
7894 }
7895 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7896 && N32_RT5 (insn) == N32_RA5 (insn)
7897 && N32_IMM15S (insn) < 512)
7898 {
7899 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7900 insn_type = NDS32_INSN_ADDI10_SP;
7901 }
7902 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7903 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7904 && (N32_IMM15S (insn) % 4 == 0))
7905 {
7906 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7907 N32_IMM15S (insn) >> 2);
7908 insn_type = NDS32_INSN_ADDRI36_SP;
7909 }
7910 }
7911 else
7912 {
7913 /* Less than 0. */
7914 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7915 {
7916 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7917 0 - N32_IMM15S (insn));
7918 insn_type = NDS32_INSN_SUBI333;
7919 }
7920 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7921 && N32_IMM15S (insn) > -32)
7922 {
1c8f6a4d
KLC
7923 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7924 0 - N32_IMM15S (insn));
35c08157
KLC
7925 insn_type = NDS32_INSN_SUBI45;
7926 }
7927 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7928 && N32_RT5 (insn) == N32_RA5 (insn)
7929 && N32_IMM15S (insn) >= -512)
7930 {
7931 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7932 insn_type = NDS32_INSN_ADDI10_SP;
7933 }
7934 }
7935 break;
7936
7937 case N32_OP6_ORI:
7938 if (N32_IMM15S (insn) == 0)
7939 {
7940 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7941 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7942 if (mach <= MACH_V2
7943 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7944 {
7945 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7946 insn_type = NDS32_INSN_MOV55;
7947 }
7948 }
7949 break;
7950
7951 case N32_OP6_SUBRI:
7952 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7953 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7954 {
7955 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7956 insn_type = NDS32_INSN_NEG33;
7957 }
7958 break;
7959
7960 case N32_OP6_ANDI:
7961 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7962 {
7963 if (N32_IMM15U (insn) == 1)
7964 {
7965 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7966 insn_type = NDS32_INSN_XLSB33;
7967 }
7968 else if (N32_IMM15U (insn) == 0x7ff)
7969 {
7970 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7971 insn_type = NDS32_INSN_X11B33;
7972 }
7973 else if (N32_IMM15U (insn) == 0xff)
7974 {
7975 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7976 insn_type = NDS32_INSN_ZEB33;
7977 }
7978 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7979 && N32_IMM15U (insn) < 256)
7980 {
7981 int imm15u = N32_IMM15U (insn);
7982
7983 if (__builtin_popcount (imm15u) == 1)
7984 {
7985 /* BMSKI33 */
7986 int imm3u = __builtin_ctz (imm15u);
7987
7988 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7989 insn_type = NDS32_INSN_BMSKI33;
7990 }
7991 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7992 {
7993 /* FEXTI33 */
7994 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7995
7996 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7997 insn_type = NDS32_INSN_FEXTI33;
7998 }
7999 }
8000 }
8001 break;
8002
8003 case N32_OP6_SLTI:
8004 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8005 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8006 {
8007 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8008 insn_type = NDS32_INSN_SLTI45;
8009 }
8010 break;
8011
8012 case N32_OP6_SLTSI:
8013 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8014 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8015 {
8016 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8017 insn_type = NDS32_INSN_SLTSI45;
8018 }
8019 break;
8020
8021 case N32_OP6_LWI:
8022 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8023 {
8024 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8025 insn_type = NDS32_INSN_LWI450;
8026 }
8027 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8028 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8029 {
8030 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8031 N32_IMM15S (insn));
8032 insn_type = NDS32_INSN_LWI333;
8033 }
8034 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8035 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8036 {
8037 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8038 insn_type = NDS32_INSN_LWI37;
8039 }
8040 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8041 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8042 {
8043 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8044 insn_type = NDS32_INSN_LWI37_SP;
8045 }
8046 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8047 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8048 {
1c8f6a4d
KLC
8049 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8050 N32_IMM15S (insn) + 32);
35c08157
KLC
8051 insn_type = NDS32_INSN_LWI45_FE;
8052 }
8053 break;
8054
8055 case N32_OP6_SWI:
8056 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8057 {
8058 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8059 insn_type = NDS32_INSN_SWI450;
8060 }
8061 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8062 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8063 {
1c8f6a4d
KLC
8064 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8065 N32_IMM15S (insn));
35c08157
KLC
8066 insn_type = NDS32_INSN_SWI333;
8067 }
8068 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8069 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8070 {
8071 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8072 insn_type = NDS32_INSN_SWI37;
8073 }
8074 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8075 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8076 {
8077 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8078 insn_type = NDS32_INSN_SWI37_SP;
8079 }
8080 break;
8081
8082 case N32_OP6_LWI_BI:
8083 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8084 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8085 {
8086 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8087 N32_IMM15S (insn));
8088 insn_type = NDS32_INSN_LWI333_BI;
8089 }
8090 break;
8091
8092 case N32_OP6_SWI_BI:
8093 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8094 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8095 {
8096 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8097 N32_IMM15S (insn));
8098 insn_type = NDS32_INSN_SWI333_BI;
8099 }
8100 break;
8101
8102 case N32_OP6_LHI:
8103 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8104 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8105 {
8106 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8107 N32_IMM15S (insn));
8108 insn_type = NDS32_INSN_LHI333;
8109 }
8110 break;
8111
8112 case N32_OP6_SHI:
8113 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8114 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8115 {
8116 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8117 N32_IMM15S (insn));
8118 insn_type = NDS32_INSN_SHI333;
8119 }
8120 break;
8121
8122 case N32_OP6_LBI:
8123 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8124 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8125 {
8126 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8127 N32_IMM15S (insn));
8128 insn_type = NDS32_INSN_LBI333;
8129 }
8130 break;
8131
8132 case N32_OP6_SBI:
8133 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8134 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8135 {
8136 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8137 N32_IMM15S (insn));
8138 insn_type = NDS32_INSN_SBI333;
8139 }
8140 break;
8141
8142 case N32_OP6_ALU1:
8143 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8144
8145 case N32_OP6_ALU2:
8146 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8147
8148 case N32_OP6_BR1:
8149 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8150 goto done;
8151
4ec521f2 8152 if ((insn & N32_BIT (14)) == 0)
35c08157
KLC
8153 {
8154 /* N32_BR1_BEQ */
8155 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8156 && N32_RT5 (insn) != REG_R5)
8157 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8158 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8159 && N32_RA5 (insn) != REG_R5)
8160 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8161 insn_type = NDS32_INSN_BEQS38;
8162 break;
8163 }
8164 else
8165 {
8166 /* N32_BR1_BNE */
8167 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8168 && N32_RT5 (insn) != REG_R5)
8169 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8170 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8171 && N32_RA5 (insn) != REG_R5)
8172 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8173 insn_type = NDS32_INSN_BNES38;
8174 break;
8175 }
8176 break;
8177
8178 case N32_OP6_BR2:
8179 switch (N32_BR2_SUB (insn))
8180 {
8181 case N32_BR2_BEQZ:
8182 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8183 {
8184 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8185 insn_type = NDS32_INSN_BEQZ38;
8186 }
1c8f6a4d
KLC
8187 else if (N32_RT5 (insn) == REG_R15
8188 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8189 {
8190 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8191 insn_type = NDS32_INSN_BEQZS8;
8192 }
8193 break;
8194
8195 case N32_BR2_BNEZ:
8196 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8197 {
8198 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8199 insn_type = NDS32_INSN_BNEZ38;
8200 }
1c8f6a4d
KLC
8201 else if (N32_RT5 (insn) == REG_R15
8202 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8203 {
8204 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8205 insn_type = NDS32_INSN_BNEZS8;
8206 }
8207 break;
8208
fbaf61ad
NC
8209 case N32_BR2_SOP0:
8210 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
35c08157
KLC
8211 {
8212 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8213 insn_type = NDS32_INSN_IFCALL9;
8214 }
8215 break;
8216 }
8217 break;
8218
8219 case N32_OP6_JI:
4ec521f2 8220 if ((insn & N32_BIT (24)) == 0)
35c08157
KLC
8221 {
8222 /* N32_JI_J */
8223 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8224 {
8225 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8226 insn_type = NDS32_INSN_J8;
8227 }
8228 }
8229 break;
8230
8231 case N32_OP6_JREG:
8232 if (__GF (insn, 8, 2) != 0)
8233 goto done;
8234
8235 switch (N32_IMMU (insn, 5))
8236 {
8237 case N32_JREG_JR:
8238 if (N32_JREG_HINT (insn) == 0)
8239 {
8240 /* jr */
8241 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8242 insn_type = NDS32_INSN_JR5;
8243 }
8244 else if (N32_JREG_HINT (insn) == 1)
8245 {
8246 /* ret */
8247 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8248 insn_type = NDS32_INSN_RET5;
8249 }
8250 else if (N32_JREG_HINT (insn) == 3)
8251 {
8252 /* ifret = mov55 $sp, $sp */
8253 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8254 insn_type = NDS32_INSN_IFRET;
8255 }
8256 break;
8257
8258 case N32_JREG_JRAL:
8259 /* It's convertible when return rt5 is $lp and address
8260 translation is kept. */
8261 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8262 {
8263 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8264 insn_type = NDS32_INSN_JRAL5;
8265 }
8266 break;
8267 }
8268 break;
8269
8270 case N32_OP6_MISC:
8271 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8272 {
8273 /* For v3, swid above 31 are used for ex9.it. */
8274 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8275 insn_type = NDS32_INSN_BREAK16;
8276 }
8277 break;
8278
8279 default:
8280 /* This instruction has no 16-bit variant. */
8281 goto done;
8282 }
8283
8284done:
8285 /* Bit-15 of insn16 should be set for a valid instruction. */
8286 if ((insn16 & 0x8000) == 0)
8287 return 0;
8288
8289 if (pinsn16)
8290 *pinsn16 = insn16;
8291 if (pinsn_type)
8292 *pinsn_type = insn_type;
8293 return 1;
8294}
8295
8296static int
8297special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8298 Elf_Internal_Rela *reloc)
8299{
8300 uint16_t insn16 = 0;
8301
8302 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8303 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8304 return 0;
8305
8306 if (!N32_IS_RT3 (insn))
8307 return 0;
8308
8309 switch (N32_OP6 (insn))
8310 {
8311 case N32_OP6_LWI:
8312 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8313 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8314 break;
8315 case N32_OP6_SWI:
8316 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8317 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8318 break;
8319 case N32_OP6_HWGP:
8320 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8321 break;
8322
8323 if (__GF (insn, 17, 3) == 6)
8324 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8325 else if (__GF (insn, 17, 3) == 7)
8326 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8327 break;
8328 }
8329
8330 if ((insn16 & 0x8000) == 0)
8331 return 0;
8332
8333 *pinsn16 = insn16;
8334 return 1;
8335}
8336
8337/* Convert a 16-bit instruction to 32-bit one.
8338 INSN16 it the input and PINSN it the point to output.
8339 Return non-zero on successful. Otherwise 0 is returned. */
8340
8341int
8342nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8343{
8344 uint32_t insn = 0xffffffff;
8345 unsigned long mach = bfd_get_mach (abfd);
8346
8347 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8348
8349 switch (__GF (insn16, 9, 6))
8350 {
8351 case 0x4: /* add45 */
1c8f6a4d
KLC
8352 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8353 N16_RA5 (insn16));
35c08157
KLC
8354 goto done;
8355 case 0x5: /* sub45 */
1c8f6a4d
KLC
8356 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8357 N16_RA5 (insn16));
35c08157
KLC
8358 goto done;
8359 case 0x6: /* addi45 */
1c8f6a4d
KLC
8360 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8361 N16_IMM5U (insn16));
35c08157
KLC
8362 goto done;
8363 case 0x7: /* subi45 */
1c8f6a4d
KLC
8364 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8365 -N16_IMM5U (insn16));
35c08157
KLC
8366 goto done;
8367 case 0x8: /* srai45 */
1c8f6a4d
KLC
8368 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8369 N16_IMM5U (insn16));
35c08157
KLC
8370 goto done;
8371 case 0x9: /* srli45 */
1c8f6a4d
KLC
8372 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8373 N16_IMM5U (insn16));
35c08157 8374 goto done;
35c08157 8375 case 0xa: /* slli333 */
1c8f6a4d
KLC
8376 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8377 N16_IMM3U (insn16));
35c08157
KLC
8378 goto done;
8379 case 0xc: /* add333 */
1c8f6a4d
KLC
8380 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8381 N16_RB3 (insn16));
35c08157
KLC
8382 goto done;
8383 case 0xd: /* sub333 */
1c8f6a4d
KLC
8384 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8385 N16_RB3 (insn16));
35c08157
KLC
8386 goto done;
8387 case 0xe: /* addi333 */
1c8f6a4d
KLC
8388 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8389 N16_IMM3U (insn16));
35c08157
KLC
8390 goto done;
8391 case 0xf: /* subi333 */
1c8f6a4d
KLC
8392 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8393 -N16_IMM3U (insn16));
35c08157 8394 goto done;
35c08157 8395 case 0x10: /* lwi333 */
1c8f6a4d
KLC
8396 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8397 N16_IMM3U (insn16));
35c08157
KLC
8398 goto done;
8399 case 0x12: /* lhi333 */
1c8f6a4d
KLC
8400 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8401 N16_IMM3U (insn16));
35c08157
KLC
8402 goto done;
8403 case 0x13: /* lbi333 */
1c8f6a4d
KLC
8404 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8405 N16_IMM3U (insn16));
35c08157
KLC
8406 goto done;
8407 case 0x11: /* lwi333.bi */
1c8f6a4d
KLC
8408 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8409 N16_IMM3U (insn16));
35c08157
KLC
8410 goto done;
8411 case 0x14: /* swi333 */
1c8f6a4d
KLC
8412 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8413 N16_IMM3U (insn16));
35c08157
KLC
8414 goto done;
8415 case 0x16: /* shi333 */
1c8f6a4d
KLC
8416 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8417 N16_IMM3U (insn16));
35c08157
KLC
8418 goto done;
8419 case 0x17: /* sbi333 */
1c8f6a4d
KLC
8420 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8421 N16_IMM3U (insn16));
35c08157
KLC
8422 goto done;
8423 case 0x15: /* swi333.bi */
1c8f6a4d
KLC
8424 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8425 N16_IMM3U (insn16));
35c08157 8426 goto done;
35c08157 8427 case 0x18: /* addri36.sp */
1c8f6a4d
KLC
8428 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8429 N16_IMM6U (insn16) << 2);
35c08157 8430 goto done;
35c08157 8431 case 0x19: /* lwi45.fe */
1c8f6a4d
KLC
8432 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8433 (N16_IMM5U (insn16) - 32));
35c08157
KLC
8434 goto done;
8435 case 0x1a: /* lwi450 */
8436 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8437 goto done;
8438 case 0x1b: /* swi450 */
8439 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8440 goto done;
8441
1c8f6a4d 8442 /* These are r15 implied instructions. */
35c08157
KLC
8443 case 0x30: /* slts45 */
8444 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8445 goto done;
8446 case 0x31: /* slt45 */
8447 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8448 goto done;
8449 case 0x32: /* sltsi45 */
8450 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8451 goto done;
8452 case 0x33: /* slti45 */
8453 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8454 goto done;
8455 case 0x34: /* beqzs8, bnezs8 */
4ec521f2 8456 if (insn16 & N32_BIT (8))
35c08157
KLC
8457 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8458 else
8459 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8460 goto done;
8461
8462 case 0x35: /* break16, ex9.it */
8463 /* Only consider range of v3 break16. */
8464 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8465 goto done;
8466
8467 case 0x3c: /* ifcall9 */
fbaf61ad 8468 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
35c08157
KLC
8469 goto done;
8470 case 0x3d: /* movpi45 */
8471 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8472 goto done;
8473
8474 case 0x3f: /* MISC33 */
1c8f6a4d 8475 switch (insn16 & 0x7)
35c08157
KLC
8476 {
8477 case 2: /* neg33 */
8478 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8479 break;
8480 case 3: /* not33 */
1c8f6a4d
KLC
8481 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8482 N16_RA3 (insn16));
35c08157
KLC
8483 break;
8484 case 4: /* mul33 */
1c8f6a4d
KLC
8485 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8486 N16_RA3 (insn16));
35c08157
KLC
8487 break;
8488 case 5: /* xor33 */
1c8f6a4d
KLC
8489 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8490 N16_RA3 (insn16));
35c08157
KLC
8491 break;
8492 case 6: /* and33 */
1c8f6a4d
KLC
8493 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8494 N16_RA3 (insn16));
35c08157
KLC
8495 break;
8496 case 7: /* or33 */
1c8f6a4d
KLC
8497 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8498 N16_RA3 (insn16));
35c08157
KLC
8499 break;
8500 }
8501 goto done;
8502
1c8f6a4d 8503 case 0xb:
35c08157
KLC
8504 switch (insn16 & 0x7)
8505 {
8506 case 0: /* zeb33 */
8507 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8508 break;
8509 case 1: /* zeh33 */
8510 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8511 break;
8512 case 2: /* seb33 */
8513 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8514 break;
8515 case 3: /* seh33 */
8516 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8517 break;
8518 case 4: /* xlsb33 */
8519 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8520 break;
8521 case 5: /* x11b33 */
8522 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8523 break;
8524 case 6: /* bmski33 */
8525 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8526 1 << __GF (insn16, 3, 3));
35c08157
KLC
8527 break;
8528 case 7: /* fexti33 */
8529 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8530 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
35c08157
KLC
8531 break;
8532 }
8533 goto done;
8534 }
8535
8536 switch (__GF (insn16, 10, 5))
8537 {
8538 case 0x0: /* mov55 or ifret16 */
8539 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8540 && N16_RT5 (insn16) == N16_RA5 (insn16))
1c8f6a4d 8541 insn = N32_JREG (JR, 0, 0, 0, 3);
35c08157 8542 else
1c8f6a4d 8543 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
35c08157
KLC
8544 goto done;
8545 case 0x1: /* movi55 */
8546 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8547 goto done;
8548 case 0x1b: /* addi10s (V2) */
8549 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8550 goto done;
8551 }
8552
8553 switch (__GF (insn16, 11, 4))
8554 {
8555 case 0x7: /* lwi37.fp/swi37.fp */
4ec521f2 8556 if (insn16 & N32_BIT (7)) /* swi37.fp */
35c08157
KLC
8557 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8558 else /* lwi37.fp */
8559 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8560 goto done;
8561 case 0x8: /* beqz38 */
8562 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8563 goto done;
8564 case 0x9: /* bnez38 */
8565 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8566 goto done;
8567 case 0xa: /* beqs38/j8, implied r5 */
8568 if (N16_RT38 (insn16) == 5)
8569 insn = N32_JI (J, N16_IMM8S (insn16));
8570 else
8571 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8572 goto done;
fbaf61ad 8573 case 0xb: /* bnes38 and others. */
35c08157
KLC
8574 if (N16_RT38 (insn16) == 5)
8575 {
8576 switch (__GF (insn16, 5, 3))
8577 {
8578 case 0: /* jr5 */
8579 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8580 break;
8581 case 4: /* ret5 */
8582 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8583 break;
8584 case 1: /* jral5 */
8585 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8586 break;
8587 case 2: /* ex9.it imm5 */
8588 /* ex9.it had no 32-bit variantl. */
8589 break;
8590 case 5: /* add5.pc */
8591 /* add5.pc had no 32-bit variantl. */
8592 break;
8593 }
8594 }
8595 else /* bnes38 */
8596 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8597 goto done;
8598 case 0xe: /* lwi37/swi37 */
8599 if (insn16 & (1 << 7)) /* swi37.sp */
8600 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8601 else /* lwi37.sp */
8602 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8603 goto done;
8604 }
8605
8606done:
8607 if (insn & 0x80000000)
8608 return 0;
8609
8610 if (pinsn)
8611 *pinsn = insn;
8612 return 1;
8613}
8614\f
fbaf61ad 8615
35c08157
KLC
8616static bfd_boolean
8617is_sda_access_insn (unsigned long insn)
8618{
8619 switch (N32_OP6 (insn))
8620 {
8621 case N32_OP6_LWI:
8622 case N32_OP6_LHI:
8623 case N32_OP6_LHSI:
8624 case N32_OP6_LBI:
8625 case N32_OP6_LBSI:
8626 case N32_OP6_SWI:
8627 case N32_OP6_SHI:
8628 case N32_OP6_SBI:
8629 case N32_OP6_LWC:
8630 case N32_OP6_LDC:
8631 case N32_OP6_SWC:
8632 case N32_OP6_SDC:
8633 return TRUE;
8634 default:
8635 ;
8636 }
8637 return FALSE;
8638}
8639
8640static unsigned long
8641turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8642{
8643 uint32_t oinsn = 0;
8644
8645 switch (type)
8646 {
8647 case R_NDS32_GOT_LO12:
8648 case R_NDS32_GOTOFF_LO12:
8649 case R_NDS32_PLTREL_LO12:
8650 case R_NDS32_PLT_GOTREL_LO12:
8651 case R_NDS32_LO12S0_RELA:
8652 switch (N32_OP6 (insn))
8653 {
8654 case N32_OP6_LBI:
8655 /* lbi.gp */
8656 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8657 break;
8658 case N32_OP6_LBSI:
8659 /* lbsi.gp */
4ec521f2 8660 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8661 break;
8662 case N32_OP6_SBI:
8663 /* sbi.gp */
8664 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8665 break;
8666 case N32_OP6_ORI:
8667 /* addi.gp */
4ec521f2 8668 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8669 break;
8670 }
8671 break;
8672
8673 case R_NDS32_LO12S1_RELA:
8674 switch (N32_OP6 (insn))
8675 {
8676 case N32_OP6_LHI:
8677 /* lhi.gp */
8678 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8679 break;
8680 case N32_OP6_LHSI:
8681 /* lhsi.gp */
4ec521f2 8682 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
35c08157
KLC
8683 break;
8684 case N32_OP6_SHI:
8685 /* shi.gp */
4ec521f2 8686 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8687 break;
8688 }
8689 break;
8690
8691 case R_NDS32_LO12S2_RELA:
8692 switch (N32_OP6 (insn))
8693 {
8694 case N32_OP6_LWI:
8695 /* lwi.gp */
8696 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8697 break;
8698 case N32_OP6_SWI:
8699 /* swi.gp */
8700 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8701 break;
8702 }
8703 break;
8704
8705 case R_NDS32_LO12S2_DP_RELA:
8706 case R_NDS32_LO12S2_SP_RELA:
8707 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8708 break;
8709 }
8710
8711 if (oinsn)
8712 *pinsn = oinsn;
8713
8714 return oinsn != 0;
8715}
8716
8717/* Linker hasn't found the correct merge section for non-section symbol
8718 in relax time, this work is left to the function elf_link_input_bfd().
8719 So for non-section symbol, _bfd_merged_section_offset is also needed
8720 to find the correct symbol address. */
8721
8722static bfd_vma
8723nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8724 asection **psec, Elf_Internal_Rela *rel)
8725{
8726 asection *sec = *psec;
8727 bfd_vma relocation;
8728
8729 relocation = (sec->output_section->vma
8730 + sec->output_offset + sym->st_value);
8731 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8732 {
8733 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8734 rel->r_addend =
8735 _bfd_merged_section_offset (abfd, psec,
8736 elf_section_data (sec)->sec_info,
8737 sym->st_value + rel->r_addend);
8738 else
8739 rel->r_addend =
8740 _bfd_merged_section_offset (abfd, psec,
8741 elf_section_data (sec)->sec_info,
8742 sym->st_value) + rel->r_addend;
8743
8744 if (sec != *psec)
8745 {
8746 /* If we have changed the section, and our original section is
8747 marked with SEC_EXCLUDE, it means that the original
8748 SEC_MERGE section has been completely subsumed in some
8749 other SEC_MERGE section. In this case, we need to leave
8750 some info around for --emit-relocs. */
8751 if ((sec->flags & SEC_EXCLUDE) != 0)
8752 sec->kept_section = *psec;
8753 sec = *psec;
8754 }
8755 rel->r_addend -= relocation;
8756 rel->r_addend += sec->output_section->vma + sec->output_offset;
8757 }
8758 return relocation;
8759}
8760
8761static bfd_vma
8762calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8763 Elf_Internal_Sym *isymbuf,
8764 Elf_Internal_Shdr *symtab_hdr)
8765{
8766 bfd_signed_vma foff;
8767 bfd_vma symval, addend;
8768 Elf_Internal_Rela irel_fn;
8769 Elf_Internal_Sym *isym;
8770 asection *sym_sec;
8771
8772 /* Get the value of the symbol referred to by the reloc. */
8773 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8774 {
8775 /* A local symbol. */
8776 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8777
8778 if (isym->st_shndx == SHN_UNDEF)
8779 sym_sec = bfd_und_section_ptr;
8780 else if (isym->st_shndx == SHN_ABS)
8781 sym_sec = bfd_abs_section_ptr;
8782 else if (isym->st_shndx == SHN_COMMON)
8783 sym_sec = bfd_com_section_ptr;
8784 else
8785 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8786 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8787 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8788 addend = irel_fn.r_addend;
8789 }
8790 else
8791 {
8792 unsigned long indx;
8793 struct elf_link_hash_entry *h;
8794
8795 /* An external symbol. */
8796 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8797 h = elf_sym_hashes (abfd)[indx];
8798 BFD_ASSERT (h != NULL);
8799
8800 while (h->root.type == bfd_link_hash_indirect
8801 || h->root.type == bfd_link_hash_warning)
8802 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8803
8804 if (h->root.type != bfd_link_hash_defined
8805 && h->root.type != bfd_link_hash_defweak)
8806 /* This appears to be a reference to an undefined
8807 symbol. Just ignore it--it will be caught by the
8808 regular reloc processing. */
8809 return 0;
8810
8811 if (h->root.u.def.section->flags & SEC_MERGE)
8812 {
8813 sym_sec = h->root.u.def.section;
8814 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8815 (sym_sec)->sec_info, h->root.u.def.value);
8816 symval = symval + sym_sec->output_section->vma
8817 + sym_sec->output_offset;
8818 }
8819 else
8820 symval = (h->root.u.def.value
8821 + h->root.u.def.section->output_section->vma
8822 + h->root.u.def.section->output_offset);
8823 addend = irel->r_addend;
8824 }
8825
8826 foff = symval + addend;
8827
8828 return foff;
8829}
8830
35c08157
KLC
8831static int
8832is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8833 asection *sec, Elf_Internal_Rela *rel)
8834{
8835 bfd_byte *contents;
8836 unsigned short insn16;
8837
8838 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8839 return FALSE;
8840 contents = elf_section_data (sec)->this_hdr.contents;
8841 insn16 = bfd_getb16 (contents + rel->r_offset);
8842 if (insn16 == NDS32_NOP16)
8843 return TRUE;
8844 return FALSE;
8845}
8846
8847/* It checks whether the instruction could be converted to
8848 16-bit form and returns the converted one.
8849
8850 `internal_relocs' is supposed to be sorted. */
8851
8852static int
8853is_convert_32_to_16 (bfd *abfd, asection *sec,
8854 Elf_Internal_Rela *reloc,
8855 Elf_Internal_Rela *internal_relocs,
8856 Elf_Internal_Rela *irelend,
8857 uint16_t *insn16)
8858{
8859#define NORMAL_32_TO_16 (1 << 0)
8860#define SPECIAL_32_TO_16 (1 << 1)
8861 bfd_byte *contents = NULL;
8862 bfd_signed_vma off;
8863 bfd_vma mem_addr;
8864 uint32_t insn = 0;
8865 Elf_Internal_Rela *pc_rel;
35c08157
KLC
8866 Elf_Internal_Shdr *symtab_hdr;
8867 Elf_Internal_Sym *isymbuf = NULL;
8868 int convert_type;
8869 bfd_vma offset;
8870
8871 if (reloc->r_offset + 4 > sec->size)
8872 return FALSE;
8873
8874 offset = reloc->r_offset;
8875
0c4bd9d9 8876 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
35c08157
KLC
8877 return FALSE;
8878 insn = bfd_getb32 (contents + offset);
8879
8880 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8881 convert_type = NORMAL_32_TO_16;
8882 else if (special_convert_32_to_16 (insn, insn16, reloc))
8883 convert_type = SPECIAL_32_TO_16;
8884 else
8885 return FALSE;
8886
8887 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8888 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8889 return FALSE;
8890
8891 /* Find the first relocation of the same relocation-type,
8892 so we iteratie them forward. */
8893 pc_rel = reloc;
1c8f6a4d 8894 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
35c08157
KLC
8895 pc_rel--;
8896
8897 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8898 {
8899 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8900 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8901 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8902 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8903 {
fbaf61ad 8904 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8905 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8906 || off == 0)
35c08157
KLC
8907 return FALSE;
8908 break;
8909 }
8910 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8911 {
8912 /* movi => movi55 */
1c8f6a4d
KLC
8913 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8914 symtab_hdr);
8915 /* mem_addr is unsigned, but the value should
8916 be between [-16, 15]. */
35c08157
KLC
8917 if ((mem_addr + 0x10) >> 5)
8918 return FALSE;
8919 break;
8920 }
1c8f6a4d
KLC
8921 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8922 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8923 {
8924 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8925 because it can be relaxed to addi for TLS_LE_ADD. */
8926 return FALSE;
8927 }
35c08157
KLC
8928 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8929 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8930 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8931 && convert_type == SPECIAL_32_TO_16)
8932 {
8933 /* fp-as-gp
8934 We've selected a best fp-base for this access, so we can
8935 always resolve it anyway. Do nothing. */
8936 break;
8937 }
8938 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8939 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8940 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8941 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8942 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8943 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8944 {
1c8f6a4d
KLC
8945 /* Prevent unresolved addi instruction translate
8946 to addi45 or addi333. */
35c08157
KLC
8947 return FALSE;
8948 }
1c8f6a4d
KLC
8949 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8950 {
fbaf61ad 8951 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8952 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8953 return FALSE;
8954 break;
8955 }
35c08157
KLC
8956 }
8957
8958 return TRUE;
8959}
8960
8961static void
8962nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8963 Elf_Internal_Rela *reloc,
8964 Elf_Internal_Rela *internal_relocs,
8965 Elf_Internal_Rela *irelend,
8966 unsigned short insn16)
8967{
8968 Elf_Internal_Rela *pc_rel;
8969 bfd_vma offset;
8970
8971 offset = reloc->r_offset;
8972 bfd_putb16 (insn16, contents + offset);
8973 /* Find the first relocation of the same relocation-type,
8974 so we iteratie them forward. */
8975 pc_rel = reloc;
8976 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8977 pc_rel--;
8978
8979 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8980 {
8981 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8982 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8983 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8984 {
8985 pc_rel->r_info =
8986 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8987 }
8988 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8989 pc_rel->r_info =
8990 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8991 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8992 pc_rel->r_info =
8993 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8994 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8995 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8996 pc_rel->r_info =
8997 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
1c8f6a4d
KLC
8998 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8999 pc_rel->r_info =
9000 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
35c08157
KLC
9001 }
9002}
9003
9004/* Find a relocation of type specified by `reloc_type'
9005 of the same r_offset with reloc.
9006 If not found, return irelend.
9007
9008 Assuming relocations are sorted by r_offset,
9009 we find the relocation from `reloc' backward untill relocs,
9010 or find it from `reloc' forward untill irelend. */
9011
9012static Elf_Internal_Rela *
9013find_relocs_at_address (Elf_Internal_Rela *reloc,
9014 Elf_Internal_Rela *relocs,
9015 Elf_Internal_Rela *irelend,
9016 enum elf_nds32_reloc_type reloc_type)
9017{
9018 Elf_Internal_Rela *rel_t;
9019
9020 /* Find backward. */
9021 for (rel_t = reloc;
9022 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9023 rel_t--)
9024 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9025 return rel_t;
9026
1c8f6a4d 9027 /* We didn't find it backward. Try find it forward. */
35c08157
KLC
9028 for (rel_t = reloc;
9029 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9030 rel_t++)
9031 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9032 return rel_t;
9033
9034 return irelend;
9035}
9036
9037/* Find a relocation of specified type and offset.
9038 `reloc' is just a refence point to find a relocation at specified offset.
9039 If not found, return irelend.
9040
9041 Assuming relocations are sorted by r_offset,
9042 we find the relocation from `reloc' backward untill relocs,
9043 or find it from `reloc' forward untill irelend. */
9044
9045static Elf_Internal_Rela *
9046find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9047 Elf_Internal_Rela *relocs,
9048 Elf_Internal_Rela *irelend,
6cae483a 9049 enum elf_nds32_reloc_type reloc_type,
35c08157
KLC
9050 bfd_vma offset_p)
9051{
9052 Elf_Internal_Rela *rel_t = NULL;
9053
9054 /* First, we try to find a relocation of offset `offset_p',
9055 and then we use find_relocs_at_address to find specific type. */
9056
9057 if (reloc->r_offset > offset_p)
9058 {
9059 /* Find backward. */
9060 for (rel_t = reloc;
9061 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9062 /* Do nothing. */;
9063 }
9064 else if (reloc->r_offset < offset_p)
9065 {
9066 /* Find forward. */
9067 for (rel_t = reloc;
9068 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9069 /* Do nothing. */;
9070 }
9071 else
9072 rel_t = reloc;
9073
9074 /* Not found? */
9075 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9076 return irelend;
9077
9078 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9079}
9080
fbaf61ad
NC
9081typedef struct nds32_elf_blank nds32_elf_blank_t;
9082struct nds32_elf_blank
35c08157 9083{
fbaf61ad
NC
9084 /* Where the blank begins. */
9085 bfd_vma offset;
9086 /* The size of the blank. */
9087 bfd_vma size;
9088 /* The accumulative size before this blank. */
9089 bfd_vma total_size;
9090 nds32_elf_blank_t *next;
9091 nds32_elf_blank_t *prev;
9092};
35c08157
KLC
9093
9094static nds32_elf_blank_t *blank_free_list = NULL;
9095
9096static nds32_elf_blank_t *
9097create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9098{
9099 nds32_elf_blank_t *blank_t;
9100
9101 if (blank_free_list)
9102 {
9103 blank_t = blank_free_list;
9104 blank_free_list = blank_free_list->next;
9105 }
9106 else
9107 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9108
9109 if (blank_t == NULL)
9110 return NULL;
9111
9112 blank_t->offset = offset_p;
9113 blank_t->size = size_p;
9114 blank_t->total_size = 0;
9115 blank_t->next = NULL;
9116 blank_t->prev = NULL;
9117
9118 return blank_t;
9119}
9120
9121static void
9122remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9123{
9124 if (blank_free_list)
9125 {
9126 blank_free_list->prev = blank_p;
9127 blank_p->next = blank_free_list;
9128 }
9129 else
9130 blank_p->next = NULL;
9131
9132 blank_p->prev = NULL;
9133 blank_free_list = blank_p;
9134}
9135
9136static void
9137clean_nds32_elf_blank (void)
9138{
9139 nds32_elf_blank_t *blank_t;
9140
9141 while (blank_free_list)
9142 {
9143 blank_t = blank_free_list;
9144 blank_free_list = blank_free_list->next;
9145 free (blank_t);
9146 }
9147}
9148
9149static nds32_elf_blank_t *
9150search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9151{
9152 nds32_elf_blank_t *blank_t;
9153
9154 if (!blank_p)
9155 return NULL;
9156 blank_t = blank_p;
9157
9158 while (blank_t && addr < blank_t->offset)
9159 blank_t = blank_t->prev;
9160 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9161 blank_t = blank_t->next;
9162
9163 return blank_t;
9164}
9165
9166static bfd_vma
9167get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9168 int overwrite)
9169{
9170 nds32_elf_blank_t *blank_t;
9171
9172 blank_t = search_nds32_elf_blank (*blank_p, addr);
9173 if (!blank_t)
9174 return 0;
9175
9176 if (overwrite)
9177 *blank_p = blank_t;
9178
9179 if (addr < blank_t->offset + blank_t->size)
9180 return blank_t->total_size + (addr - blank_t->offset);
9181 else
9182 return blank_t->total_size + blank_t->size;
9183}
9184
9185static bfd_boolean
9186insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9187{
9188 nds32_elf_blank_t *blank_t, *blank_t2;
9189
9190 if (!*blank_p)
9191 {
9192 *blank_p = create_nds32_elf_blank (addr, len);
9193 return *blank_p ? TRUE : FALSE;
9194 }
9195
9196 blank_t = search_nds32_elf_blank (*blank_p, addr);
9197
9198 if (blank_t == NULL)
9199 {
9200 blank_t = create_nds32_elf_blank (addr, len);
9201 if (!blank_t)
9202 return FALSE;
9203 while ((*blank_p)->prev != NULL)
9204 *blank_p = (*blank_p)->prev;
9205 blank_t->next = *blank_p;
9206 (*blank_p)->prev = blank_t;
9207 (*blank_p) = blank_t;
9208 return TRUE;
9209 }
9210
9211 if (addr < blank_t->offset + blank_t->size)
9212 {
fbaf61ad
NC
9213 /* Extend the origin blank. */
9214 if (addr + len > blank_t->offset + blank_t->size)
9215 blank_t->size = addr + len - blank_t->offset;
35c08157
KLC
9216 }
9217 else
9218 {
9219 blank_t2 = create_nds32_elf_blank (addr, len);
9220 if (!blank_t2)
9221 return FALSE;
9222 if (blank_t->next)
9223 {
9224 blank_t->next->prev = blank_t2;
9225 blank_t2->next = blank_t->next;
9226 }
9227 blank_t2->prev = blank_t;
9228 blank_t->next = blank_t2;
9229 *blank_p = blank_t2;
9230 }
9231
9232 return TRUE;
9233}
9234
9235static bfd_boolean
9236insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9237 bfd_vma len)
9238{
9239 nds32_elf_blank_t *blank_t;
9240
9241 if (!insert_nds32_elf_blank (blank_p, addr, len))
9242 return FALSE;
9243
9244 blank_t = *blank_p;
9245
9246 if (!blank_t->prev)
9247 {
9248 blank_t->total_size = 0;
9249 blank_t = blank_t->next;
9250 }
9251
9252 while (blank_t)
9253 {
9254 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9255 blank_t = blank_t->next;
9256 }
9257
9258 return TRUE;
9259}
9260
9261static void
9262calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9263{
9264 nds32_elf_blank_t *blank_t;
9265 bfd_vma total_size = 0;
9266
9267 if (!blank_p)
9268 return;
9269
9270 blank_t = blank_p;
9271 while (blank_t->prev)
9272 blank_t = blank_t->prev;
9273 while (blank_t)
9274 {
9275 blank_t->total_size = total_size;
9276 total_size += blank_t->size;
9277 blank_t = blank_t->next;
9278 }
9279}
9280
9281static bfd_boolean
9282nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9283 nds32_elf_blank_t *blank_p)
9284{
9285 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
1c8f6a4d 9286 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
35c08157
KLC
9287 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9288 unsigned int sec_shndx; /* The section the be relaxed. */
9289 bfd_byte *contents; /* Contents data of iterating section. */
9290 Elf_Internal_Rela *internal_relocs;
9291 Elf_Internal_Rela *irel;
9292 Elf_Internal_Rela *irelend;
9293 struct elf_link_hash_entry **sym_hashes;
9294 struct elf_link_hash_entry **end_hashes;
9295 unsigned int symcount;
9296 asection *sect;
9297 nds32_elf_blank_t *blank_t;
9298 nds32_elf_blank_t *blank_t2;
9299 nds32_elf_blank_t *blank_head;
9300
9301 blank_head = blank_t = blank_p;
9302 while (blank_head->prev != NULL)
9303 blank_head = blank_head->prev;
9304 while (blank_t->next != NULL)
9305 blank_t = blank_t->next;
9306
9307 if (blank_t->offset + blank_t->size <= sec->size)
9308 {
9309 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9310 blank_t->next->prev = blank_t;
9311 }
9312 if (blank_head->offset > 0)
9313 {
9314 blank_head->prev = create_nds32_elf_blank (0, 0);
9315 blank_head->prev->next = blank_head;
9316 blank_head = blank_head->prev;
9317 }
9318
9319 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9320
9321 /* The deletion must stop at the next ALIGN reloc for an alignment
9322 power larger than the number of bytes we are deleting. */
9323
9324 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9325 if (!nds32_get_local_syms (abfd, sec, &isym))
9326 return FALSE;
9327
9328 if (isym == NULL)
9329 {
9330 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9331 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9332 symtab_hdr->contents = (bfd_byte *) isym;
9333 }
9334
9335 if (isym == NULL || symtab_hdr->sh_info == 0)
9336 return FALSE;
9337
9338 blank_t = blank_head;
9339 calc_nds32_blank_total (blank_head);
9340
9341 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9342 {
9343 /* Adjust all the relocs. */
9344
9345 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9346 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9347 TRUE /* keep_memory */);
9348 irelend = internal_relocs + sect->reloc_count;
9349
9350 blank_t = blank_head;
9351 blank_t2 = blank_head;
9352
9353 if (!(sect->flags & SEC_RELOC))
9354 continue;
9355
0c4bd9d9 9356 nds32_get_section_contents (abfd, sect, &contents, TRUE);
35c08157
KLC
9357
9358 for (irel = internal_relocs; irel < irelend; irel++)
9359 {
9360 bfd_vma raddr;
9361
9362 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9363 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9364 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9365 {
9366 unsigned long val = 0;
1c8f6a4d
KLC
9367 unsigned long mask;
9368 long before, between;
6cae483a 9369 long offset = 0;
35c08157
KLC
9370
9371 switch (ELF32_R_TYPE (irel->r_info))
9372 {
9373 case R_NDS32_DIFF8:
1c8f6a4d 9374 offset = bfd_get_8 (abfd, contents + irel->r_offset);
35c08157
KLC
9375 break;
9376 case R_NDS32_DIFF16:
1c8f6a4d 9377 offset = bfd_get_16 (abfd, contents + irel->r_offset);
35c08157
KLC
9378 break;
9379 case R_NDS32_DIFF32:
9380 val = bfd_get_32 (abfd, contents + irel->r_offset);
1c8f6a4d
KLC
9381 /* Get the signed bit and mask for the high part. The
9382 gcc will alarm when right shift 32-bit since the
9383 type size of long may be 32-bit. */
9384 mask = 0 - (val >> 31);
9385 if (mask)
9386 offset = (val | (mask - 0xffffffff));
9387 else
9388 offset = val;
35c08157
KLC
9389 break;
9390 default:
9391 BFD_ASSERT (0);
9392 }
9393
9394 /* DIFF value
9395 0 |encoded in location|
9396 |------------|-------------------|---------
9397 sym+off(addend)
9398 -- before ---| *****************
9399 --------------------- between ---|
9400
1c8f6a4d
KLC
9401 We only care how much data are relax between DIFF,
9402 marked as ***. */
35c08157
KLC
9403
9404 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9405 between = get_nds32_elf_blank_total (&blank_t,
9406 irel->r_addend + offset, 0);
35c08157
KLC
9407 if (between == before)
9408 goto done_adjust_diff;
9409
9410 switch (ELF32_R_TYPE (irel->r_info))
9411 {
9412 case R_NDS32_DIFF8:
1c8f6a4d
KLC
9413 bfd_put_8 (abfd, offset - (between - before),
9414 contents + irel->r_offset);
35c08157
KLC
9415 break;
9416 case R_NDS32_DIFF16:
1c8f6a4d
KLC
9417 bfd_put_16 (abfd, offset - (between - before),
9418 contents + irel->r_offset);
35c08157
KLC
9419 break;
9420 case R_NDS32_DIFF32:
1c8f6a4d
KLC
9421 bfd_put_32 (abfd, offset - (between - before),
9422 contents + irel->r_offset);
35c08157
KLC
9423 break;
9424 }
9425 }
9426 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9427 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9428 {
9429 bfd_vma val = 0;
9430 unsigned int len = 0;
9431 unsigned long before, between;
9432 bfd_byte *endp, *p;
9433
4265548c
PA
9434 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9435 &len);
35c08157
KLC
9436
9437 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9438 between = get_nds32_elf_blank_total (&blank_t,
9439 irel->r_addend + val, 0);
35c08157
KLC
9440 if (between == before)
9441 goto done_adjust_diff;
9442
9443 p = contents + irel->r_offset;
9444 endp = p + len -1;
9445 memset (p, 0x80, len);
9446 *(endp) = 0;
9447 p = write_uleb128 (p, val - (between - before)) - 1;
9448 if (p < endp)
9449 *p |= 0x80;
9450 }
9451done_adjust_diff:
9452
9453 if (sec == sect)
9454 {
9455 raddr = irel->r_offset;
1c8f6a4d
KLC
9456 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9457 irel->r_offset, 1);
35c08157
KLC
9458
9459 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9460 continue;
9461 if (blank_t2 && blank_t2->next
1c8f6a4d
KLC
9462 && (blank_t2->offset > raddr
9463 || blank_t2->next->offset <= raddr))
4eca0228 9464 _bfd_error_handler
38f14ab8
AM
9465 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9466 abfd);
35c08157
KLC
9467
9468 /* Mark reloc in deleted portion as NONE.
9469 For some relocs like R_NDS32_LABEL that doesn't modify the
9470 content in the section. R_NDS32_LABEL doesn't belong to the
9471 instruction in the section, so we should preserve it. */
9472 if (raddr >= blank_t2->offset
9473 && raddr < blank_t2->offset + blank_t2->size
9474 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9475 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9476 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9477 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9478 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9479 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9480 {
9481 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9482 R_NDS32_NONE);
9483 continue;
9484 }
9485 }
9486
9487 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9488 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9489 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9490 continue;
9491
9492 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9493 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9494 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9495 {
9496 if (irel->r_addend <= sec->size)
9497 irel->r_addend -=
9498 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9499 }
9500 }
9501 }
9502
9503 /* Adjust the local symbols defined in this section. */
9504 blank_t = blank_head;
9505 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9506 {
9507 if (isym->st_shndx == sec_shndx)
9508 {
9509 if (isym->st_value <= sec->size)
9510 {
9511 bfd_vma ahead;
9512 bfd_vma orig_addr = isym->st_value;
9513
9514 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9515 isym->st_value -= ahead;
9516
9517 /* Adjust function size. */
1c8f6a4d
KLC
9518 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9519 && isym->st_size > 0)
9520 isym->st_size -=
9521 get_nds32_elf_blank_total
9522 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
35c08157
KLC
9523 }
9524 }
9525 }
9526
9527 /* Now adjust the global symbols defined in this section. */
9528 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9529 - symtab_hdr->sh_info);
9530 sym_hashes = elf_sym_hashes (abfd);
9531 end_hashes = sym_hashes + symcount;
9532 blank_t = blank_head;
9533 for (; sym_hashes < end_hashes; sym_hashes++)
9534 {
9535 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9536
9537 if ((sym_hash->root.type == bfd_link_hash_defined
9538 || sym_hash->root.type == bfd_link_hash_defweak)
9539 && sym_hash->root.u.def.section == sec)
9540 {
9541 if (sym_hash->root.u.def.value <= sec->size)
9542 {
9543 bfd_vma ahead;
9544 bfd_vma orig_addr = sym_hash->root.u.def.value;
9545
9546 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9547 sym_hash->root.u.def.value -= ahead;
9548
9549 /* Adjust function size. */
9550 if (sym_hash->type == STT_FUNC)
1c8f6a4d
KLC
9551 sym_hash->size -=
9552 get_nds32_elf_blank_total
9553 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
35c08157
KLC
9554
9555 }
9556 }
9557 }
9558
9559 contents = elf_section_data (sec)->this_hdr.contents;
9560 blank_t = blank_head;
9561 while (blank_t->next)
9562 {
9563 /* Actually delete the bytes. */
9564
9565 /* If current blank is the last blank overlap with current section,
9566 go to finish process. */
9567 if (sec->size <= (blank_t->next->offset))
9568 break;
9569
9570 memmove (contents + blank_t->offset - blank_t->total_size,
9571 contents + blank_t->offset + blank_t->size,
9572 blank_t->next->offset - (blank_t->offset + blank_t->size));
9573
9574 blank_t = blank_t->next;
9575 }
9576
9577 if (sec->size > (blank_t->offset + blank_t->size))
9578 {
9579 /* There are remaining code between blank and section boundary.
9580 Move the remaining code to appropriate location. */
9581 memmove (contents + blank_t->offset - blank_t->total_size,
9582 contents + blank_t->offset + blank_t->size,
9583 sec->size - (blank_t->offset + blank_t->size));
9584 sec->size -= blank_t->total_size + blank_t->size;
9585 }
9586 else
9587 /* This blank is not entirely included in the section,
9588 reduce the section size by only part of the blank size. */
9589 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9590
9591 while (blank_head)
9592 {
9593 blank_t = blank_head;
9594 blank_head = blank_head->next;
9595 remove_nds32_elf_blank (blank_t);
9596 }
9597
9598 return TRUE;
9599}
9600
9601/* Get the contents of a section. */
9602
9603static int
0c4bd9d9
KLC
9604nds32_get_section_contents (bfd *abfd, asection *sec,
9605 bfd_byte **contents_p, bfd_boolean cache)
35c08157
KLC
9606{
9607 /* Get the section contents. */
9608 if (elf_section_data (sec)->this_hdr.contents != NULL)
9609 *contents_p = elf_section_data (sec)->this_hdr.contents;
9610 else
9611 {
9612 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9613 return FALSE;
0c4bd9d9
KLC
9614 if (cache)
9615 elf_section_data (sec)->this_hdr.contents = *contents_p;
35c08157
KLC
9616 }
9617
9618 return TRUE;
9619}
9620
9621/* Get the contents of the internal symbol of abfd. */
9622
9623static int
9624nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9625 Elf_Internal_Sym **isymbuf_p)
9626{
9627 Elf_Internal_Shdr *symtab_hdr;
9628 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9629
9630 /* Read this BFD's local symbols if we haven't done so already. */
9631 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9632 {
9633 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9634 if (*isymbuf_p == NULL)
9635 {
9636 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9637 symtab_hdr->sh_info, 0,
9638 NULL, NULL, NULL);
9639 if (*isymbuf_p == NULL)
9640 return FALSE;
9641 }
9642 }
9643 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9644
9645 return TRUE;
9646}
9647
9648/* Range of small data. */
1c8f6a4d
KLC
9649static bfd_vma sdata_range[2][2];
9650static bfd_vma const sdata_init_range[2] =
9651{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
35c08157
KLC
9652
9653static int
9654nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9655 bfd_byte *contents, bfd_vma addr)
9656{
9657 unsigned long insn = bfd_getb32 (contents + addr);
9658
9659 if (insn & 0x80000000)
9660 return 2;
9661
9662 return 4;
9663}
9664
9665/* Set the gp relax range. We have to measure the safe range
9666 to do gp relaxation. */
9667
9668static void
9669relax_range_measurement (bfd *abfd)
9670{
9671 asection *sec_f, *sec_b;
9672 /* For upper bound. */
9673 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9674 bfd_vma align;
35c08157
KLC
9675 static int decide_relax_range = 0;
9676 int i;
fbaf61ad 9677 int range_number = ARRAY_SIZE (sdata_init_range);
35c08157
KLC
9678
9679 if (decide_relax_range)
9680 return;
9681 decide_relax_range = 1;
9682
9683 if (sda_rela_sec == NULL)
9684 {
9685 /* Since there is no data sections, we assume the range is page size. */
1c8f6a4d 9686 for (i = 0; i < range_number; i++)
35c08157
KLC
9687 {
9688 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9689 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9690 }
9691 return;
9692 }
9693
9694 /* Get the biggest alignment power after the gp located section. */
9695 sec_f = sda_rela_sec->output_section;
9696 sec_b = sec_f->next;
9697 align = 0;
9698 while (sec_b != NULL)
9699 {
9700 if ((unsigned)(1 << sec_b->alignment_power) > align)
9701 align = (1 << sec_b->alignment_power);
9702 sec_b = sec_b->next;
9703 }
9704
9705 /* I guess we can not determine the section before
9706 gp located section, so we assume the align is max page size. */
1c8f6a4d 9707 for (i = 0; i < range_number; i++)
35c08157 9708 {
1c8f6a4d 9709 sdata_range[i][1] = sdata_init_range[i] - align;
35c08157 9710 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
1c8f6a4d 9711 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
35c08157
KLC
9712 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9713 }
9714}
9715
9716/* These are macros used to check flags encoded in r_addend.
9717 They are only used by nds32_elf_relax_section (). */
9718#define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9719#define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9720#define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9721#define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9722
695344c0
NC
9723static const char * unrecognized_reloc_msg =
9724 /* xgettext:c-format */
2dcf00ce 9725 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
695344c0 9726
1c8f6a4d
KLC
9727/* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9728
35c08157 9729static bfd_boolean
1c8f6a4d
KLC
9730nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9731 Elf_Internal_Rela *internal_relocs, int *insn_len,
9732 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9733 Elf_Internal_Shdr *symtab_hdr)
9734{
9735 /* There are 3 variations for LONGCALL1
9736 case 4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8 9737 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9738 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9739 jral5 ta ;
1c8f6a4d
KLC
9740
9741 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8 9742 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9743 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9744 jral ta ;
1c8f6a4d
KLC
9745
9746 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8 9747 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9748 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9749 jral ta ;
1c8f6a4d
KLC
9750 Check code for -mlong-calls output. */
9751
9752 /* Get the reloc for the address from which the register is
9753 being loaded. This reloc will tell us which function is
9754 actually being called. */
9755
9756 bfd_vma laddr;
9757 int seq_len; /* Original length of instruction sequence. */
35c08157 9758 uint32_t insn;
1c8f6a4d 9759 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d 9760 bfd_signed_vma foff;
35c08157 9761 uint16_t insn16;
35c08157 9762
1c8f6a4d
KLC
9763 irelend = internal_relocs + sec->reloc_count;
9764 seq_len = GET_SEQ_LEN (irel->r_addend);
9765 laddr = irel->r_offset;
9766 *insn_len = seq_len;
35c08157 9767
1c8f6a4d
KLC
9768 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9769 R_NDS32_HI20_RELA, laddr);
9770 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9771 R_NDS32_LO12S0_ORI_RELA,
9772 laddr + 4);
35c08157 9773
1c8f6a4d 9774 if (hi_irelfn == irelend || lo_irelfn == irelend)
35c08157 9775 {
695344c0 9776 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
2dcf00ce 9777 (uint64_t) irel->r_offset);
1c8f6a4d 9778 return FALSE;
35c08157
KLC
9779 }
9780
1c8f6a4d 9781 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9782 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9783
1c8f6a4d 9784 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
9785 if (foff == 0
9786 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9787 || foff >= CONSERVATIVE_24BIT_S1)
9788 return FALSE;
35c08157 9789
fbaf61ad 9790 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
9791 /* For simplicity of coding, we are going to modify the section
9792 contents, the section relocs, and the BFD symbol table. We
9793 must tell the rest of the code not to free up this
9794 information. It would be possible to instead create a table
9795 of changes which have to be made, as is done in coff-mips.c;
9796 that would be more work, but would require less memory when
9797 the linker is run. */
9798
9799 /* Replace the long call with a jal. */
9800 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9801 R_NDS32_25_PCREL_RELA);
9802 irel->r_addend = hi_irelfn->r_addend;
9803
9804 /* We don't resolve this here but resolve it in relocate_section. */
9805 insn = INSN_JAL;
9806 bfd_putb32 (insn, contents + irel->r_offset);
9807
9808 hi_irelfn->r_info =
9809 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9810 lo_irelfn->r_info =
9811 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9812 *insn_len = 4;
9813
9814 if (seq_len & 0x2)
9815 {
9816 insn16 = NDS32_NOP16;
9817 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9818 lo_irelfn->r_info =
9819 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9820 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9821 *insn_len += 2;
9822 }
9823 return TRUE;
9824}
35c08157 9825
1c8f6a4d
KLC
9826#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9827/* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
35c08157 9828
1c8f6a4d
KLC
9829static bfd_boolean
9830nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9831 Elf_Internal_Rela *internal_relocs, int *insn_len,
9832 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9833 Elf_Internal_Shdr *symtab_hdr)
9834{
9835 /* bltz rt, .L1 ; LONGCALL2
9836 jal symbol ; 25_PCREL
9837 .L1: */
35c08157 9838
1c8f6a4d
KLC
9839 /* Get the reloc for the address from which the register is
9840 being loaded. This reloc will tell us which function is
9841 actually being called. */
35c08157 9842
1c8f6a4d
KLC
9843 bfd_vma laddr;
9844 uint32_t insn;
9845 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
1c8f6a4d 9846 bfd_signed_vma foff;
35c08157 9847
1c8f6a4d
KLC
9848 irelend = internal_relocs + sec->reloc_count;
9849 laddr = irel->r_offset;
9850 i1_irelfn =
9851 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9852 R_NDS32_25_PCREL_RELA, laddr + 4);
9853
9854 if (i1_irelfn == irelend)
35c08157 9855 {
695344c0 9856 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
2dcf00ce 9857 (uint64_t) irel->r_offset);
1c8f6a4d 9858 return FALSE;
35c08157
KLC
9859 }
9860
1c8f6a4d 9861 insn = bfd_getb32 (contents + laddr);
35c08157 9862
1c8f6a4d 9863 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9864 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
35c08157 9865
fbaf61ad
NC
9866 if (foff == 0
9867 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
9868 || foff >= CONSERVATIVE_16BIT_S1)
9869 return FALSE;
35c08157 9870
1c8f6a4d
KLC
9871 /* Relax to bgezal rt, label ; 17_PCREL
9872 or bltzal rt, label ; 17_PCREL */
9873
9874 /* Convert to complimentary conditional call. */
9875 insn = CONVERT_CONDITION_CALL (insn);
9876
9877 /* For simplicity of coding, we are going to modify the section
9878 contents, the section relocs, and the BFD symbol table. We
9879 must tell the rest of the code not to free up this
9880 information. It would be possible to instead create a table
9881 of changes which have to be made, as is done in coff-mips.c;
9882 that would be more work, but would require less memory when
9883 the linker is run. */
9884
9885 /* Clean unnessary relocations. */
9886 i1_irelfn->r_info =
9887 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9888 cond_irelfn =
9889 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9890 R_NDS32_17_PCREL_RELA, laddr);
9891 if (cond_irelfn != irelend)
9892 cond_irelfn->r_info =
9893 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9894
9895 /* Replace the long call with a bgezal. */
9896 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9897 R_NDS32_17_PCREL_RELA);
9898 irel->r_addend = i1_irelfn->r_addend;
9899
9900 bfd_putb32 (insn, contents + irel->r_offset);
9901
9902 *insn_len = 4;
9903 return TRUE;
9904}
35c08157 9905
1c8f6a4d
KLC
9906/* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9907
9908static bfd_boolean
9909nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9910 Elf_Internal_Rela *internal_relocs, int *insn_len,
9911 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9912 Elf_Internal_Shdr *symtab_hdr)
9913{
9914 /* There are 3 variations for LONGCALL3
9915 case 4-4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8
AM
9916 bltz rt, $1 ; LONGCALL3
9917 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9918 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9919 jral5 ta ;
1c8f6a4d
KLC
9920 $1
9921
9922 case 4-4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
9923 bltz rt, $1 ; LONGCALL3
9924 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9925 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9926 jral ta ;
1c8f6a4d
KLC
9927 $1
9928
9929 case 4-4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
9930 bltz rt, $1 ; LONGCALL3
9931 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9932 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9933 jral ta ;
1c8f6a4d
KLC
9934 $1 */
9935
9936 /* Get the reloc for the address from which the register is
9937 being loaded. This reloc will tell us which function is
9938 actually being called. */
9939
9940 bfd_vma laddr;
9941 int seq_len; /* Original length of instruction sequence. */
9942 uint32_t insn;
9943 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
1c8f6a4d
KLC
9944 bfd_signed_vma foff;
9945 uint16_t insn16;
9946
9947 irelend = internal_relocs + sec->reloc_count;
9948 seq_len = GET_SEQ_LEN (irel->r_addend);
9949 laddr = irel->r_offset;
9950 *insn_len = seq_len;
9951
9952 hi_irelfn =
9953 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9954 R_NDS32_HI20_RELA, laddr + 4);
9955 lo_irelfn =
9956 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9957 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9958
9959 if (hi_irelfn == irelend || lo_irelfn == irelend)
9960 {
695344c0 9961 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
2dcf00ce 9962 (uint64_t) irel->r_offset);
1c8f6a4d 9963 return FALSE;
35c08157
KLC
9964 }
9965
1c8f6a4d 9966 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9967 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9968
fbaf61ad
NC
9969 if (foff == 0
9970 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9971 || foff >= CONSERVATIVE_24BIT_S1)
9972 return FALSE;
35c08157 9973
1c8f6a4d
KLC
9974 insn = bfd_getb32 (contents + laddr);
9975 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
35c08157 9976 {
1c8f6a4d
KLC
9977 /* Relax to bgezal rt, label ; 17_PCREL
9978 or bltzal rt, label ; 17_PCREL */
35c08157 9979
1c8f6a4d
KLC
9980 /* Convert to complimentary conditional call. */
9981 insn = CONVERT_CONDITION_CALL (insn);
9982 bfd_putb32 (insn, contents + irel->r_offset);
35c08157 9983
1c8f6a4d
KLC
9984 *insn_len = 4;
9985 irel->r_info =
9986 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9987 hi_irelfn->r_info =
9988 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9989 lo_irelfn->r_info =
9990 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9991
9992 cond_irelfn =
9993 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9994 R_NDS32_17_PCREL_RELA, laddr);
9995 if (cond_irelfn != irelend)
35c08157 9996 {
1c8f6a4d
KLC
9997 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9998 R_NDS32_17_PCREL_RELA);
9999 cond_irelfn->r_addend = hi_irelfn->r_addend;
35c08157 10000 }
1c8f6a4d
KLC
10001
10002 if (seq_len & 0x2)
35c08157 10003 {
1c8f6a4d
KLC
10004 insn16 = NDS32_NOP16;
10005 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10006 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10007 R_NDS32_INSN16);
10008 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10009 insn_len += 2;
35c08157 10010 }
1c8f6a4d
KLC
10011 }
10012 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10013 {
10014 /* Relax to the following instruction sequence
10015 bltz rt, $1 ; LONGCALL2
10016 jal symbol ; 25_PCREL
10017 $1 */
10018 *insn_len = 8;
10019 insn = INSN_JAL;
10020 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
35c08157 10021
1c8f6a4d
KLC
10022 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10023 R_NDS32_25_PCREL_RELA);
10024 irel->r_info =
10025 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10026
10027 lo_irelfn->r_info =
10028 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10029
1c8f6a4d 10030 if (seq_len & 0x2)
35c08157 10031 {
1c8f6a4d
KLC
10032 insn16 = NDS32_NOP16;
10033 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10034 lo_irelfn->r_info =
10035 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10036 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10037 insn_len += 2;
10038 }
10039 }
10040 return TRUE;
10041}
35c08157 10042
1c8f6a4d 10043/* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
35c08157 10044
1c8f6a4d
KLC
10045static bfd_boolean
10046nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10047 Elf_Internal_Rela *internal_relocs, int *insn_len,
10048 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10049 Elf_Internal_Shdr *symtab_hdr)
10050{
10051 /* There are 3 variations for LONGJUMP1
10052 case 4-4-2; 16-bit bit on, optimize off or optimize for space
07d6d2b8
AM
10053 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10054 ori ta, ta, lo12(symbol) ; LO12S0
10055 jr5 ta ;
1c8f6a4d
KLC
10056
10057 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
10058 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10059 ori ta, ta, lo12(symbol) ; LO12S0
10060 jr ta ;
1c8f6a4d
KLC
10061
10062 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
10063 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10064 ori ta, ta, lo12(symbol) ; LO12S0
10065 jr ta ; */
1c8f6a4d
KLC
10066
10067 /* Get the reloc for the address from which the register is
10068 being loaded. This reloc will tell us which function is
10069 actually being called. */
10070
10071 bfd_vma laddr;
10072 int seq_len; /* Original length of instruction sequence. */
10073 int insn16_on; /* 16-bit on/off. */
10074 uint32_t insn;
10075 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d
KLC
10076 bfd_signed_vma foff;
10077 uint16_t insn16;
10078 unsigned long reloc;
35c08157 10079
1c8f6a4d
KLC
10080 irelend = internal_relocs + sec->reloc_count;
10081 seq_len = GET_SEQ_LEN (irel->r_addend);
10082 laddr = irel->r_offset;
10083 *insn_len = seq_len;
10084 insn16_on = IS_16BIT_ON (irel->r_addend);
10085
10086 hi_irelfn =
10087 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10088 R_NDS32_HI20_RELA, laddr);
10089 lo_irelfn =
10090 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10091 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10092 if (hi_irelfn == irelend || lo_irelfn == irelend)
10093 {
695344c0 10094 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
2dcf00ce 10095 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10096 return FALSE;
10097 }
35c08157 10098
1c8f6a4d 10099 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10100 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10101
fbaf61ad
NC
10102 if (foff == 0
10103 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10104 || foff < -CONSERVATIVE_24BIT_S1)
10105 return FALSE;
35c08157 10106
fbaf61ad
NC
10107 if (insn16_on
10108 && foff >= -ACCURATE_8BIT_S1
10109 && foff < ACCURATE_8BIT_S1
10110 && (seq_len & 0x2))
1c8f6a4d
KLC
10111 {
10112 /* j8 label */
10113 /* 16-bit on, but not optimized for speed. */
10114 reloc = R_NDS32_9_PCREL_RELA;
10115 insn16 = INSN_J8;
10116 bfd_putb16 (insn16, contents + irel->r_offset);
10117 *insn_len = 2;
10118 irel->r_info =
10119 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10120 }
10121 else
10122 {
10123 /* j label */
10124 reloc = R_NDS32_25_PCREL_RELA;
10125 insn = INSN_J;
10126 bfd_putb32 (insn, contents + irel->r_offset);
10127 *insn_len = 4;
10128 irel->r_info =
10129 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10130 irel->r_addend = 0;
10131 }
35c08157 10132
1c8f6a4d
KLC
10133 hi_irelfn->r_info =
10134 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10135 lo_irelfn->r_info =
10136 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10137
1c8f6a4d
KLC
10138 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10139 {
10140 insn16 = NDS32_NOP16;
10141 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10142 lo_irelfn->r_info =
10143 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10144 R_NDS32_INSN16);
10145 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10146 *insn_len += 2;
10147 }
10148 return TRUE;
10149}
10150
10151/* Revert condition branch. This function does not check if the input
10152 instruction is condition branch or not. */
10153
10154static void
10155nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10156 uint16_t *re_insn16, uint32_t *re_insn)
10157{
10158 uint32_t comp_insn = 0;
10159 uint16_t comp_insn16 = 0;
10160
10161 if (insn)
10162 {
10163 if (N32_OP6 (insn) == N32_OP6_BR1)
10164 {
10165 /* beqs label. */
10166 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10167 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
35c08157 10168 {
1c8f6a4d
KLC
10169 /* Insn can be contracted to 16-bit implied r5. */
10170 comp_insn16 =
10171 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10172 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
35c08157 10173 }
1c8f6a4d
KLC
10174 }
10175 else if (N32_OP6 (insn) == N32_OP6_BR3)
10176 {
10177 /* bnec $ta, imm11, label. */
10178 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10179 }
10180 else
10181 {
10182 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10183 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10184 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
35c08157 10185 {
1c8f6a4d 10186 if (N32_IS_RT3 (insn))
35c08157 10187 {
1c8f6a4d
KLC
10188 /* Insn can be contracted to 16-bit. */
10189 comp_insn16 =
10190 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10191 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10192 }
10193 else if (N32_RT5 (insn) == REG_R15)
10194 {
10195 /* Insn can be contracted to 16-bit. */
10196 comp_insn16 =
10197 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
35c08157 10198 }
35c08157
KLC
10199 }
10200 }
1c8f6a4d
KLC
10201 }
10202 else
10203 {
10204 switch ((insn16 & 0xf000) >> 12)
35c08157 10205 {
1c8f6a4d
KLC
10206 case 0xc:
10207 /* beqz38 or bnez38 */
10208 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10209 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10210 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10211 break;
35c08157 10212
1c8f6a4d
KLC
10213 case 0xd:
10214 /* beqs38 or bnes38 */
10215 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10216 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10217 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10218 | (REG_R5 << 15);
10219 break;
35c08157 10220
1c8f6a4d
KLC
10221 case 0xe:
10222 /* beqzS8 or bnezS8 */
10223 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10224 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10225 comp_insn |= REG_R15 << 20;
10226 break;
35c08157 10227
1c8f6a4d
KLC
10228 default:
10229 break;
10230 }
10231 }
10232 if (comp_insn && re_insn)
10233 *re_insn = comp_insn;
10234 if (comp_insn16 && re_insn16)
10235 *re_insn16 = comp_insn16;
10236}
35c08157 10237
1c8f6a4d 10238/* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
35c08157 10239
1c8f6a4d
KLC
10240static bfd_boolean
10241nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10242 Elf_Internal_Rela *internal_relocs, int *insn_len,
10243 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10244 Elf_Internal_Shdr *symtab_hdr)
10245{
10246 /* There are 3 variations for LONGJUMP2
10247 case 2-4; 1st insn convertible, 16-bit on,
10248 optimize off or optimize for space
10249 bnes38 rt, ra, $1 ; LONGJUMP2
10250 j label ; 25_PCREL
10251 $1:
10252
10253 case 4-4; 1st insn not convertible
10254 bne rt, ra, $1 ; LONGJUMP2
10255 j label ; 25_PCREL
10256 $1:
10257
10258 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10259 bne rt, ra, $1 ; LONGJUMP2
10260 j label ; 25_PCREL
10261 $1: */
10262
10263 /* Get the reloc for the address from which the register is
10264 being loaded. This reloc will tell us which function is
10265 actually being called. */
10266
10267 bfd_vma laddr;
10268 int seq_len; /* Original length of instruction sequence. */
10269 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10270 int first_size;
1c8f6a4d
KLC
10271 unsigned int i;
10272 bfd_signed_vma foff;
10273 uint32_t insn, re_insn = 0;
10274 uint16_t insn16, re_insn16 = 0;
10275 unsigned long reloc, cond_reloc;
35c08157 10276
1c8f6a4d
KLC
10277 enum elf_nds32_reloc_type checked_types[] =
10278 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
35c08157 10279
1c8f6a4d
KLC
10280 irelend = internal_relocs + sec->reloc_count;
10281 seq_len = GET_SEQ_LEN (irel->r_addend);
10282 laddr = irel->r_offset;
10283 *insn_len = seq_len;
10284 first_size = (seq_len == 6) ? 2 : 4;
10285
10286 i2_irelfn =
10287 find_relocs_at_address_addr (irel, internal_relocs,
10288 irelend, R_NDS32_25_PCREL_RELA,
10289 laddr + first_size);
10290
fbaf61ad 10291 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10292 {
10293 cond_irelfn =
10294 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10295 checked_types[i], laddr);
10296 if (cond_irelfn != irelend)
10297 break;
10298 }
35c08157 10299
1c8f6a4d
KLC
10300 if (i2_irelfn == irelend || cond_irelfn == irelend)
10301 {
695344c0 10302 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
2dcf00ce 10303 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10304 return FALSE;
10305 }
35c08157 10306
1c8f6a4d 10307 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad
NC
10308 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10309 if (foff == 0
10310 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10311 || foff >= CONSERVATIVE_16BIT_S1)
10312 return FALSE;
35c08157 10313
1c8f6a4d
KLC
10314 /* Get the all corresponding instructions. */
10315 if (first_size == 4)
10316 {
10317 insn = bfd_getb32 (contents + laddr);
10318 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10319 }
10320 else
10321 {
10322 insn16 = bfd_getb16 (contents + laddr);
10323 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10324 }
35c08157 10325
1c8f6a4d
KLC
10326 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10327 && foff < ACCURATE_8BIT_S1 - first_size)
10328 {
10329 if (first_size == 4)
10330 {
10331 /* Don't convert it to 16-bit now, keep this as relaxable for
10332 ``label reloc; INSN16''. */
35c08157 10333
1c8f6a4d
KLC
10334 /* Save comp_insn32 to buffer. */
10335 bfd_putb32 (re_insn, contents + irel->r_offset);
10336 *insn_len = 4;
10337 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10338 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10339 cond_reloc = R_NDS32_INSN16;
10340 }
10341 else
10342 {
10343 bfd_putb16 (re_insn16, contents + irel->r_offset);
10344 *insn_len = 2;
10345 reloc = R_NDS32_9_PCREL_RELA;
10346 cond_reloc = R_NDS32_NONE;
10347 }
10348 }
10349 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10350 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10351 && foff < ACCURATE_14BIT_S1 - first_size))
10352 {
10353 /* beqs label ; 15_PCREL */
10354 bfd_putb32 (re_insn, contents + irel->r_offset);
10355 *insn_len = 4;
10356 reloc = R_NDS32_15_PCREL_RELA;
10357 cond_reloc = R_NDS32_NONE;
10358 }
10359 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10360 && foff >= -CONSERVATIVE_16BIT_S1
10361 && foff < CONSERVATIVE_16BIT_S1)
10362 {
10363 /* beqz label ; 17_PCREL */
10364 bfd_putb32 (re_insn, contents + irel->r_offset);
10365 *insn_len = 4;
10366 reloc = R_NDS32_17_PCREL_RELA;
10367 cond_reloc = R_NDS32_NONE;
10368 }
10369 else
10370 return FALSE;
35c08157 10371
1c8f6a4d
KLC
10372 /* Set all relocations. */
10373 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10374 irel->r_addend = i2_irelfn->r_addend;
35c08157 10375
1c8f6a4d
KLC
10376 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10377 cond_reloc);
10378 cond_irelfn->r_addend = 0;
35c08157 10379
1c8f6a4d
KLC
10380 if ((seq_len ^ *insn_len ) & 0x2)
10381 {
10382 insn16 = NDS32_NOP16;
10383 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10384 i2_irelfn->r_offset = 4;
10385 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10386 R_NDS32_INSN16);
10387 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10388 *insn_len += 2;
10389 }
10390 else
10391 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10392 R_NDS32_NONE);
10393 return TRUE;
10394}
35c08157 10395
1c8f6a4d 10396/* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
35c08157 10397
1c8f6a4d
KLC
10398static bfd_boolean
10399nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10400 Elf_Internal_Rela *internal_relocs, int *insn_len,
10401 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10402 Elf_Internal_Shdr *symtab_hdr)
10403{
10404 /* There are 5 variations for LONGJUMP3
10405 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10406 optimize off or optimize for space
07d6d2b8
AM
10407 bnes38 rt, ra, $1 ; LONGJUMP3
10408 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10409 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10410 jr5 ta ;
10411 $1: ;
1c8f6a4d
KLC
10412
10413 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
07d6d2b8
AM
10414 bnes38 rt, ra, $1 ; LONGJUMP3
10415 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10416 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10417 jr5 ta ;
10418 $1: ; LABEL
1c8f6a4d
KLC
10419
10420 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10421 optimize off or optimize for space
07d6d2b8
AM
10422 bne rt, ra, $1 ; LONGJUMP3
10423 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10424 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10425 jr5 ta ;
10426 $1: ;
1c8f6a4d
KLC
10427
10428 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10429 16-bit off if no INSN16
07d6d2b8
AM
10430 bne rt, ra, $1 ; LONGJUMP3
10431 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10432 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10433 jr ta ;
10434 $1: ;
1c8f6a4d
KLC
10435
10436 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10437 16-bit off if no INSN16
07d6d2b8
AM
10438 bne rt, ra, $1 ; LONGJUMP3
10439 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10440 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10441 jr ta ;
10442 $1: ; LABEL */
1c8f6a4d
KLC
10443
10444 /* Get the reloc for the address from which the register is
10445 being loaded. This reloc will tell us which function is
10446 actually being called. */
10447 enum elf_nds32_reloc_type checked_types[] =
10448 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10449
10450 int reloc_off = 0, cond_removed = 0, convertible;
10451 bfd_vma laddr;
10452 int seq_len; /* Original length of instruction sequence. */
10453 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10454 int first_size;
1c8f6a4d
KLC
10455 unsigned int i;
10456 bfd_signed_vma foff;
10457 uint32_t insn, re_insn = 0;
10458 uint16_t insn16, re_insn16 = 0;
10459 unsigned long reloc, cond_reloc;
35c08157 10460
1c8f6a4d
KLC
10461 irelend = internal_relocs + sec->reloc_count;
10462 seq_len = GET_SEQ_LEN (irel->r_addend);
10463 laddr = irel->r_offset;
10464 *insn_len = seq_len;
35c08157 10465
1c8f6a4d 10466 convertible = IS_1ST_CONVERT (irel->r_addend);
35c08157 10467
1c8f6a4d
KLC
10468 if (convertible)
10469 first_size = 2;
10470 else
10471 first_size = 4;
10472
10473 /* Get all needed relocations. */
10474 hi_irelfn =
10475 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10476 R_NDS32_HI20_RELA, laddr + first_size);
10477 lo_irelfn =
10478 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10479 R_NDS32_LO12S0_ORI_RELA,
10480 laddr + first_size + 4);
10481
fbaf61ad 10482 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10483 {
10484 cond_irelfn =
10485 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10486 checked_types[i], laddr);
10487 if (cond_irelfn != irelend)
10488 break;
10489 }
35c08157 10490
fbaf61ad
NC
10491 if (hi_irelfn == irelend
10492 || lo_irelfn == irelend
10493 || cond_irelfn == irelend)
1c8f6a4d 10494 {
695344c0 10495 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
2dcf00ce 10496 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10497 return FALSE;
10498 }
35c08157 10499
1c8f6a4d 10500 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10501 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10502
fbaf61ad
NC
10503 if (foff == 0
10504 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10505 || foff >= CONSERVATIVE_24BIT_S1)
10506 return FALSE;
35c08157 10507
1c8f6a4d
KLC
10508 /* Get the all corresponding instructions. */
10509 if (first_size == 4)
10510 {
10511 insn = bfd_getb32 (contents + laddr);
10512 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10513 }
10514 else
10515 {
10516 insn16 = bfd_getb16 (contents + laddr);
10517 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10518 }
35c08157 10519
1c8f6a4d
KLC
10520 /* For simplicity of coding, we are going to modify the section
10521 contents, the section relocs, and the BFD symbol table. We
10522 must tell the rest of the code not to free up this
10523 information. It would be possible to instead create a table
10524 of changes which have to be made, as is done in coff-mips.c;
10525 that would be more work, but would require less memory when
10526 the linker is run. */
35c08157 10527
fbaf61ad
NC
10528 if (re_insn16
10529 && foff >= -ACCURATE_8BIT_S1 - first_size
1c8f6a4d
KLC
10530 && foff < ACCURATE_8BIT_S1 - first_size)
10531 {
10532 if (!(seq_len & 0x2))
10533 {
10534 /* Don't convert it to 16-bit now, keep this as relaxable
10535 for ``label reloc; INSN1a''6. */
10536 /* Save comp_insn32 to buffer. */
10537 bfd_putb32 (re_insn, contents + irel->r_offset);
10538 *insn_len = 4;
10539 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10540 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10541 cond_reloc = R_NDS32_INSN16;
10542 }
10543 else
10544 {
10545 /* Not optimize for speed; convert sequence to 16-bit. */
10546 /* Save comp_insn16 to buffer. */
10547 bfd_putb16 (re_insn16, contents + irel->r_offset);
10548 *insn_len = 2;
10549 reloc = R_NDS32_9_PCREL_RELA;
10550 cond_reloc = R_NDS32_NONE;
10551 }
10552 cond_removed = 1;
10553 }
10554 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10555 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10556 && foff < ACCURATE_14BIT_S1 - first_size))
10557 {
10558 /* beqs label ; 15_PCREL */
10559 bfd_putb32 (re_insn, contents + irel->r_offset);
10560 *insn_len = 4;
10561 reloc = R_NDS32_15_PCREL_RELA;
10562 cond_reloc = R_NDS32_NONE;
10563 cond_removed = 1;
10564 }
10565 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10566 && foff >= -CONSERVATIVE_16BIT_S1
10567 && foff < CONSERVATIVE_16BIT_S1)
10568 {
10569 /* beqz label ; 17_PCREL */
10570 bfd_putb32 (re_insn, contents + irel->r_offset);
10571 *insn_len = 4;
10572 reloc = R_NDS32_17_PCREL_RELA;
10573 cond_reloc = R_NDS32_NONE;
10574 cond_removed = 1;
10575 }
10576 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10577 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10578 {
10579 /* Relax to one of the following 3 variations
10580
10581 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10582 for space
10583 bnes38 rt, $1 ; LONGJUMP2
10584 j label ; 25_PCREL
10585 $1
10586
10587 case 4-4; 1st insn not convertible, others don't care
10588 bne rt, ra, $1 ; LONGJUMP2
10589 j label ; 25_PCREL
10590 $1
10591
10592 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10593 bne rt, ra, $1 ; LONGJUMP2
10594 j label ; 25_PCREL
10595 $1 */
10596
10597 /* Offset for first instruction. */
10598
10599 /* Use j label as second instruction. */
10600 *insn_len = 4 + first_size;
10601 insn = INSN_J;
10602 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10603 reloc = R_NDS32_LONGJUMP2;
10604 cond_reloc = R_NDS32_25_PLTREL;
10605 }
10606 else
10607 return FALSE;
35c08157 10608
1c8f6a4d
KLC
10609 if (cond_removed == 1)
10610 {
10611 /* Set all relocations. */
10612 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10613 irel->r_addend = hi_irelfn->r_addend;
10614
10615 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10616 cond_reloc);
10617 cond_irelfn->r_addend = 0;
10618 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10619 R_NDS32_NONE);
10620 }
10621 else
10622 {
10623 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
fbaf61ad 10624 irel->r_addend = irel->r_addend;
1c8f6a4d
KLC
10625 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10626 cond_reloc);
10627 }
35c08157 10628
1c8f6a4d
KLC
10629 if ((seq_len ^ *insn_len ) & 0x2)
10630 {
10631 insn16 = NDS32_NOP16;
10632 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10633 lo_irelfn->r_offset = *insn_len;
10634 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10635 R_NDS32_INSN16);
10636 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10637 *insn_len += 2;
10638 }
10639 else
10640 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10641 R_NDS32_NONE);
10642 return TRUE;
10643}
35c08157 10644
1c8f6a4d 10645/* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
35c08157 10646
1c8f6a4d
KLC
10647static bfd_boolean
10648nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10649 Elf_Internal_Rela *internal_relocs, int *insn_len,
10650 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10651 Elf_Internal_Shdr *symtab_hdr)
10652{
10653 /* The pattern for LONGCALL4. Support for function cse.
10654 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10655 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10656 jral ta ; PTR_RES/EMPTY/INSN16 */
35c08157 10657
1c8f6a4d
KLC
10658 bfd_vma laddr;
10659 uint32_t insn;
10660 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10661 Elf_Internal_Rela *irelend;
1c8f6a4d 10662 bfd_signed_vma foff;
35c08157 10663
1c8f6a4d
KLC
10664 irelend = internal_relocs + sec->reloc_count;
10665 laddr = irel->r_offset;
35c08157 10666
1c8f6a4d
KLC
10667 /* Get the reloc for the address from which the register is
10668 being loaded. This reloc will tell us which function is
10669 actually being called. */
10670 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10671 R_NDS32_HI20_RELA, laddr);
35c08157 10672
1c8f6a4d
KLC
10673 if (hi_irel == irelend)
10674 {
695344c0 10675 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10676 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10677 return FALSE;
10678 }
35c08157 10679
1c8f6a4d 10680 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10681 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10682
1c8f6a4d 10683 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
10684 if (foff == 0
10685 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10686 || foff >= CONSERVATIVE_24BIT_S1)
10687 return FALSE;
35c08157 10688
fbaf61ad 10689 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
10690 /* For simplicity of coding, we are going to modify the section
10691 contents, the section relocs, and the BFD symbol table. We
10692 must tell the rest of the code not to free up this
10693 information. It would be possible to instead create a table
10694 of changes which have to be made, as is done in coff-mips.c;
10695 that would be more work, but would require less memory when
10696 the linker is run. */
35c08157 10697
1c8f6a4d
KLC
10698 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10699 R_NDS32_PTR_RESOLVED, irel->r_addend);
10700 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10701 R_NDS32_EMPTY, irel->r_addend);
35c08157 10702
1c8f6a4d
KLC
10703 if (ptr_irel == irelend || em_irel == irelend)
10704 {
695344c0 10705 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10706 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10707 return FALSE;
10708 }
10709 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10710 insn = bfd_getb32 (contents + irel->r_addend);
10711 if (insn & 0x80000000)
10712 return FALSE;
35c08157 10713
1c8f6a4d
KLC
10714 /* Replace the long call with a jal. */
10715 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10716 R_NDS32_25_PCREL_RELA);
10717 ptr_irel->r_addend = 1;
35c08157 10718
1c8f6a4d
KLC
10719 /* We don't resolve this here but resolve it in relocate_section. */
10720 insn = INSN_JAL;
10721 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10722
1c8f6a4d
KLC
10723 irel->r_info =
10724 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10725
1c8f6a4d
KLC
10726 /* If there is function cse, HI20 can not remove now. */
10727 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10728 R_NDS32_LONGCALL4, laddr);
10729 if (call_irel == irelend)
10730 {
10731 *insn_len = 0;
10732 hi_irel->r_info =
10733 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10734 }
35c08157 10735
1c8f6a4d
KLC
10736 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10737 R_NDS32_INSN16, irel->r_addend);
10738 if (insn_irel != irelend)
10739 insn_irel->r_info =
10740 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10741
1c8f6a4d
KLC
10742 return TRUE;
10743}
35c08157 10744
1c8f6a4d 10745/* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
35c08157 10746
1c8f6a4d
KLC
10747static bfd_boolean
10748nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10749 Elf_Internal_Rela *internal_relocs, int *insn_len,
10750 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10751 Elf_Internal_Shdr *symtab_hdr)
10752{
10753 /* The pattern for LONGCALL5.
10754 bltz rt, .L1 ; LONGCALL5/17_PCREL
10755 jal symbol ; 25_PCREL
10756 .L1: */
35c08157 10757
1c8f6a4d
KLC
10758 bfd_vma laddr;
10759 uint32_t insn;
10760 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d 10761 bfd_signed_vma foff;
35c08157 10762
1c8f6a4d
KLC
10763 irelend = internal_relocs + sec->reloc_count;
10764 laddr = irel->r_offset;
10765 insn = bfd_getb32 (contents + laddr);
35c08157 10766
1c8f6a4d
KLC
10767 /* Get the reloc for the address from which the register is
10768 being loaded. This reloc will tell us which function is
10769 actually being called. */
10770 cond_irel =
10771 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10772 R_NDS32_25_PCREL_RELA, irel->r_addend);
10773 if (cond_irel == irelend)
10774 {
695344c0 10775 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
2dcf00ce 10776 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10777 return FALSE;
10778 }
35c08157 10779
1c8f6a4d 10780 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10781 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 10782
fbaf61ad
NC
10783 if (foff == 0
10784 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10785 || foff >= CONSERVATIVE_16BIT_S1)
10786 return FALSE;
35c08157 10787
1c8f6a4d 10788 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10789 or bltzal rt, label ; 17_PCREL. */
35c08157 10790
1c8f6a4d
KLC
10791 /* Convert to complimentary conditional call. */
10792 insn = CONVERT_CONDITION_CALL (insn);
35c08157 10793
1c8f6a4d
KLC
10794 /* For simplicity of coding, we are going to modify the section
10795 contents, the section relocs, and the BFD symbol table. We
10796 must tell the rest of the code not to free up this
10797 information. It would be possible to instead create a table
10798 of changes which have to be made, as is done in coff-mips.c;
10799 that would be more work, but would require less memory when
10800 the linker is run. */
35c08157 10801
1c8f6a4d
KLC
10802 /* Modify relocation and contents. */
10803 cond_irel->r_info =
10804 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
35c08157 10805
1c8f6a4d
KLC
10806 /* Replace the long call with a bgezal. */
10807 bfd_putb32 (insn, contents + cond_irel->r_offset);
10808 *insn_len = 0;
35c08157 10809
1c8f6a4d
KLC
10810 /* Clean unnessary relocations. */
10811 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10812
1c8f6a4d
KLC
10813 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10814 R_NDS32_17_PCREL_RELA, laddr);
10815 cond_irel->r_info =
10816 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10817
1c8f6a4d
KLC
10818 return TRUE;
10819}
35c08157 10820
1c8f6a4d 10821/* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
35c08157 10822
1c8f6a4d
KLC
10823static bfd_boolean
10824nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10825 Elf_Internal_Rela *internal_relocs, int *insn_len,
10826 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10827 Elf_Internal_Shdr *symtab_hdr)
10828{
10829 /* The pattern for LONGCALL6.
10830 bltz rt, .L1 ; LONGCALL6/17_PCREL
10831 sethi ta, hi20(symbol) ; HI20/PTR
10832 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10833 jral ta ; PTR_RES/EMPTY/INSN16
10834 .L1 */
10835
10836 bfd_vma laddr;
10837 uint32_t insn;
10838 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
1c8f6a4d 10839 bfd_signed_vma foff;
35c08157 10840
1c8f6a4d
KLC
10841 irelend = internal_relocs + sec->reloc_count;
10842 laddr = irel->r_offset;
35c08157 10843
1c8f6a4d
KLC
10844 /* Get the reloc for the address from which the register is
10845 being loaded. This reloc will tell us which function is
10846 actually being called. */
10847 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10848 R_NDS32_EMPTY, irel->r_addend);
35c08157 10849
1c8f6a4d
KLC
10850 if (em_irel == irelend)
10851 {
695344c0 10852 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
2dcf00ce 10853 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10854 return FALSE;
10855 }
35c08157 10856
1c8f6a4d 10857 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10858 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 10859
fbaf61ad
NC
10860 if (foff == 0
10861 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10862 || foff >= CONSERVATIVE_24BIT_S1)
10863 return FALSE;
35c08157 10864
1c8f6a4d
KLC
10865 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10866 insn = bfd_getb32 (contents + irel->r_addend);
10867 if (insn & 0x80000000)
10868 return FALSE;
35c08157 10869
1c8f6a4d
KLC
10870 insn = bfd_getb32 (contents + laddr);
10871 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10872 {
10873 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10874 or bltzal rt, label ; 17_PCREL. */
35c08157 10875
1c8f6a4d
KLC
10876 /* Convert to complimentary conditional call. */
10877 *insn_len = 0;
10878 insn = CONVERT_CONDITION_CALL (insn);
10879 bfd_putb32 (insn, contents + em_irel->r_offset);
10880
10881 em_irel->r_info =
10882 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10883
10884 /* Set resolved relocation. */
10885 cond_irel =
10886 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10887 R_NDS32_PTR_RESOLVED, irel->r_addend);
10888 if (cond_irel == irelend)
35c08157 10889 {
695344c0 10890 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10891 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10892 return FALSE;
10893 }
10894 cond_irel->r_addend = 1;
35c08157 10895
1c8f6a4d 10896 /* Clear relocations. */
35c08157 10897
1c8f6a4d
KLC
10898 irel->r_info =
10899 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10900
1c8f6a4d
KLC
10901 cond_irel =
10902 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10903 R_NDS32_17_PCREL_RELA, laddr);
10904 if (cond_irel != irelend)
10905 cond_irel->r_info =
10906 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10907
1c8f6a4d
KLC
10908 cond_irel =
10909 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10910 R_NDS32_INSN16, irel->r_addend);
10911 if (cond_irel != irelend)
10912 cond_irel->r_info =
10913 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10914
1c8f6a4d
KLC
10915 }
10916 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10917 {
10918 /* Relax to the following instruction sequence
10919 bltz rt, .L1 ; LONGCALL2/17_PCREL
10920 jal symbol ; 25_PCREL/PTR_RES
10921 .L1 */
10922 *insn_len = 4;
10923 /* Convert instruction. */
10924 insn = INSN_JAL;
10925 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10926
1c8f6a4d
KLC
10927 /* Convert relocations. */
10928 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10929 R_NDS32_25_PCREL_RELA);
10930 irel->r_info =
10931 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
35c08157 10932
1c8f6a4d
KLC
10933 /* Set resolved relocation. */
10934 cond_irel =
10935 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10936 R_NDS32_PTR_RESOLVED, irel->r_addend);
10937 if (cond_irel == irelend)
10938 {
695344c0 10939 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10940 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10941 return FALSE;
10942 }
10943 cond_irel->r_addend = 1;
35c08157 10944
1c8f6a4d
KLC
10945 cond_irel =
10946 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10947 R_NDS32_INSN16, irel->r_addend);
10948 if (cond_irel != irelend)
10949 cond_irel->r_info =
10950 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10951 }
10952 return TRUE;
10953}
35c08157 10954
1c8f6a4d 10955/* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
35c08157 10956
1c8f6a4d
KLC
10957static bfd_boolean
10958nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10959 Elf_Internal_Rela *internal_relocs, int *insn_len,
10960 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10961 Elf_Internal_Shdr *symtab_hdr)
10962{
10963 /* The pattern for LONGJUMP4.
10964 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10965 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10966 jr ta ; PTR_RES/INSN16/EMPTY */
10967
10968 bfd_vma laddr;
10969 int seq_len; /* Original length of instruction sequence. */
10970 uint32_t insn;
10971 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
1c8f6a4d 10972 bfd_signed_vma foff;
35c08157 10973
1c8f6a4d
KLC
10974 irelend = internal_relocs + sec->reloc_count;
10975 seq_len = GET_SEQ_LEN (irel->r_addend);
10976 laddr = irel->r_offset;
10977 *insn_len = seq_len;
35c08157 10978
1c8f6a4d
KLC
10979 /* Get the reloc for the address from which the register is
10980 being loaded. This reloc will tell us which function is
10981 actually being called. */
35c08157 10982
1c8f6a4d
KLC
10983 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10984 R_NDS32_HI20_RELA, laddr);
35c08157 10985
1c8f6a4d
KLC
10986 if (hi_irel == irelend)
10987 {
695344c0 10988 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 10989 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10990 return FALSE;
10991 }
35c08157 10992
1c8f6a4d 10993 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10994 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10995
fbaf61ad
NC
10996 if (foff == 0
10997 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10998 || foff < -CONSERVATIVE_24BIT_S1)
10999 return FALSE;
35c08157 11000
1c8f6a4d
KLC
11001 /* Convert it to "j label", it may be converted to j8 in the final
11002 pass of relaxation. Therefore, we do not consider this currently. */
11003 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11004 R_NDS32_PTR_RESOLVED, irel->r_addend);
11005 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11006 R_NDS32_EMPTY, irel->r_addend);
35c08157 11007
1c8f6a4d
KLC
11008 if (ptr_irel == irelend || em_irel == irelend)
11009 {
695344c0 11010 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 11011 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11012 return FALSE;
11013 }
35c08157 11014
1c8f6a4d
KLC
11015 em_irel->r_info =
11016 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11017 ptr_irel->r_addend = 1;
35c08157 11018
1c8f6a4d
KLC
11019 /* Write instruction. */
11020 insn = INSN_J;
11021 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 11022
1c8f6a4d
KLC
11023 /* Clear relocations. */
11024 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11025
1c8f6a4d
KLC
11026 /* If there is function cse, HI20 can not remove now. */
11027 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11028 R_NDS32_LONGJUMP4, laddr);
11029 if (call_irel == irelend)
11030 {
11031 *insn_len = 0;
11032 hi_irel->r_info =
11033 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11034 }
35c08157 11035
1c8f6a4d
KLC
11036 return TRUE;
11037}
35c08157 11038
1c8f6a4d 11039/* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
35c08157 11040
1c8f6a4d
KLC
11041static bfd_boolean
11042nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11043 Elf_Internal_Rela *internal_relocs, int *insn_len,
11044 int *seq_len, bfd_byte *contents,
11045 Elf_Internal_Sym *isymbuf,
11046 Elf_Internal_Shdr *symtab_hdr)
11047{
11048 /* There are 2 variations for LONGJUMP5
11049 case 2-4; 1st insn convertible, 16-bit on.
11050 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11051 j label ; 25_PCREL/INSN16
11052 $1:
11053
11054 case 4-4; 1st insn not convertible
11055 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11056 j label ; 25_PCREL/INSN16
11057 .L1: */
11058
11059 bfd_vma laddr;
11060 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d
KLC
11061 unsigned int i;
11062 bfd_signed_vma foff;
11063 uint32_t insn, re_insn = 0;
11064 uint16_t insn16, re_insn16 = 0;
11065 unsigned long reloc;
35c08157 11066
1c8f6a4d
KLC
11067 enum elf_nds32_reloc_type checked_types[] =
11068 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11069 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
35c08157 11070
1c8f6a4d
KLC
11071 irelend = internal_relocs + sec->reloc_count;
11072 laddr = irel->r_offset;
35c08157 11073
1c8f6a4d
KLC
11074 /* Get the reloc for the address from which the register is
11075 being loaded. This reloc will tell us which function is
11076 actually being called. */
35c08157 11077
1c8f6a4d
KLC
11078 cond_irel =
11079 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11080 R_NDS32_25_PCREL_RELA, irel->r_addend);
11081 if (cond_irel == irelend)
11082 {
695344c0 11083 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
2dcf00ce 11084 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11085 return FALSE;
11086 }
35c08157 11087
1c8f6a4d 11088 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11089 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11090
fbaf61ad
NC
11091 if (foff == 0
11092 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
11093 || foff >= CONSERVATIVE_16BIT_S1)
11094 return FALSE;
35c08157 11095
1c8f6a4d
KLC
11096 /* Get the all corresponding instructions. */
11097 insn = bfd_getb32 (contents + laddr);
11098 /* Check instruction size. */
11099 if (insn & 0x80000000)
11100 {
11101 *seq_len = 0;
11102 insn16 = insn >> 16;
11103 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11104 }
11105 else
11106 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11107
1c8f6a4d
KLC
11108 if (N32_OP6 (re_insn) == N32_OP6_BR1
11109 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11110 {
11111 /* beqs label ; 15_PCREL. */
11112 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11113 reloc = R_NDS32_15_PCREL_RELA;
11114 }
11115 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11116 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11117 {
11118 /* beqz label ; 17_PCREL. */
11119 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11120 reloc = R_NDS32_17_PCREL_RELA;
11121 }
11122 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11123 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11124 {
11125 /* beqc label ; 9_PCREL. */
11126 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11127 reloc = R_NDS32_WORD_9_PCREL_RELA;
11128 }
11129 else
11130 return FALSE;
35c08157 11131
1c8f6a4d
KLC
11132 /* Set all relocations. */
11133 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
35c08157 11134
1c8f6a4d
KLC
11135 /* Clean relocations. */
11136 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
fbaf61ad 11137 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11138 {
11139 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11140 checked_types[i], laddr);
11141 if (cond_irel != irelend)
11142 {
11143 if (*seq_len == 0
11144 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
35c08157 11145 {
1c8f6a4d
KLC
11146 /* If the branch instruction is 2 byte, it cannot remove
11147 directly. Only convert it to nop16 and remove it after
11148 checking alignment issue. */
11149 insn16 = NDS32_NOP16;
11150 bfd_putb16 (insn16, contents + laddr);
11151 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157
KLC
11152 }
11153 else
1c8f6a4d
KLC
11154 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11155 R_NDS32_NONE);
35c08157 11156 }
1c8f6a4d
KLC
11157 }
11158 *insn_len = 0;
35c08157 11159
1c8f6a4d
KLC
11160 return TRUE;
11161}
35c08157 11162
1c8f6a4d 11163/* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
35c08157 11164
1c8f6a4d
KLC
11165static bfd_boolean
11166nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11167 Elf_Internal_Rela *internal_relocs, int *insn_len,
11168 int *seq_len, bfd_byte *contents,
11169 Elf_Internal_Sym *isymbuf,
11170 Elf_Internal_Shdr *symtab_hdr)
11171{
11172 /* There are 5 variations for LONGJUMP6
11173 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11174 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11175 sethi ta, hi20(symbol) ; HI20/PTR
11176 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11177 jr ta ; PTR_RES/INSN16/EMPTY
11178 .L1:
11179
11180 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11181 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11182 sethi ta, hi20(symbol) ; HI20/PTR
11183 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11184 jr ta ; PTR_RES/INSN16/EMPTY
11185 .L1: */
11186
11187 enum elf_nds32_reloc_type checked_types[] =
11188 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11189 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11190
11191 int reloc_off = 0, cond_removed = 0;
11192 bfd_vma laddr;
11193 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11194 unsigned int i;
11195 bfd_signed_vma foff;
11196 uint32_t insn, re_insn = 0;
11197 uint16_t insn16, re_insn16 = 0;
11198 unsigned long reloc;
35c08157 11199
1c8f6a4d
KLC
11200 irelend = internal_relocs + sec->reloc_count;
11201 laddr = irel->r_offset;
35c08157 11202
1c8f6a4d
KLC
11203 /* Get the reloc for the address from which the register is
11204 being loaded. This reloc will tell us which function is
11205 actually being called. */
11206 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11207 R_NDS32_EMPTY, irel->r_addend);
35c08157 11208
1c8f6a4d
KLC
11209 if (em_irel == irelend)
11210 {
695344c0 11211 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
2dcf00ce 11212 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11213 return FALSE;
11214 }
35c08157 11215
1c8f6a4d 11216 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11217 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 11218
fbaf61ad
NC
11219 if (foff == 0
11220 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
11221 || foff >= CONSERVATIVE_24BIT_S1)
11222 return FALSE;
35c08157 11223
1c8f6a4d
KLC
11224 insn = bfd_getb32 (contents + laddr);
11225 /* Check instruction size. */
11226 if (insn & 0x80000000)
11227 {
11228 *seq_len = 0;
11229 insn16 = insn >> 16;
11230 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11231 }
11232 else
11233 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11234
1c8f6a4d
KLC
11235 /* For simplicity of coding, we are going to modify the section
11236 contents, the section relocs, and the BFD symbol table. We
11237 must tell the rest of the code not to free up this
11238 information. It would be possible to instead create a table
11239 of changes which have to be made, as is done in coff-mips.c;
11240 that would be more work, but would require less memory when
11241 the linker is run. */
35c08157 11242
1c8f6a4d
KLC
11243 if (N32_OP6 (re_insn) == N32_OP6_BR1
11244 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11245 {
fbaf61ad 11246 /* beqs label ; 15_PCREL. */
1c8f6a4d
KLC
11247 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11248 reloc = R_NDS32_15_PCREL_RELA;
11249 cond_removed = 1;
11250 }
11251 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11252 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11253 {
fbaf61ad 11254 /* beqz label ; 17_PCREL. */
1c8f6a4d
KLC
11255 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11256 reloc = R_NDS32_17_PCREL_RELA;
11257 cond_removed = 1;
11258 }
11259 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11260 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11261 {
11262 /* Relax to one of the following 2 variations
35c08157 11263
1c8f6a4d
KLC
11264 case 2-4; 1st insn convertible, 16-bit on.
11265 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11266 j label ; 25_PCREL/INSN16
11267 $1:
35c08157 11268
1c8f6a4d
KLC
11269 case 4-4; 1st insn not convertible
11270 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11271 j label ; 25_PCREL/INSN16
11272 .L1: */
35c08157 11273
1c8f6a4d
KLC
11274 /* Use j label as second instruction. */
11275 insn = INSN_J;
11276 reloc = R_NDS32_25_PCREL_RELA;
11277 bfd_putb32 (insn, contents + em_irel->r_offset);
11278 }
11279 else
11280 return FALSE;
35c08157 11281
1c8f6a4d
KLC
11282 /* Set all relocations. */
11283 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
35c08157 11284
1c8f6a4d
KLC
11285 cond_irel =
11286 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11287 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11288 cond_irel->r_addend = 1;
35c08157 11289
1c8f6a4d
KLC
11290 /* Use INSN16 of first branch instruction to distinguish if keeping
11291 INSN16 of final instruction or not. */
11292 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11293 R_NDS32_INSN16, irel->r_offset);
11294 if (insn_irel == irelend)
11295 {
11296 /* Clean the final INSN16. */
11297 insn_irel =
11298 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11299 R_NDS32_INSN16, em_irel->r_offset);
11300 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11301 R_NDS32_NONE);
11302 }
11303
11304 if (cond_removed == 1)
11305 {
11306 *insn_len = 0;
11307
11308 /* Clear relocations. */
11309 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11310
fbaf61ad 11311 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11312 {
11313 cond_irel =
11314 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11315 checked_types[i], laddr);
11316 if (cond_irel != irelend)
35c08157 11317 {
1c8f6a4d
KLC
11318 if (*seq_len == 0
11319 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11320 {
11321 /* If the branch instruction is 2 byte, it cannot remove
11322 directly. Only convert it to nop16 and remove it after
11323 checking alignment issue. */
11324 insn16 = NDS32_NOP16;
11325 bfd_putb16 (insn16, contents + laddr);
11326 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11327 }
11328 else
11329 cond_irel->r_info =
11330 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 11331 }
35c08157 11332 }
1c8f6a4d
KLC
11333 }
11334 else
11335 {
11336 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11337 R_NDS32_LONGJUMP5);
11338 }
35c08157 11339
1c8f6a4d
KLC
11340 return TRUE;
11341}
35c08157 11342
1c8f6a4d 11343/* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
35c08157 11344
1c8f6a4d
KLC
11345static bfd_boolean
11346nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11347 Elf_Internal_Rela *internal_relocs, int *insn_len,
11348 int *seq_len, bfd_byte *contents,
11349 Elf_Internal_Sym *isymbuf,
11350 Elf_Internal_Shdr *symtab_hdr)
11351{
11352 /* There are 2 variations for LONGJUMP5
11353 case 2-4; 1st insn convertible, 16-bit on.
11354 movi55 ta, imm11 ; LONGJUMP7/INSN16
11355 beq rt, ta, label ; 15_PCREL
11356
11357 case 4-4; 1st insn not convertible
11358 movi55 ta, imm11 ; LONGJUMP7/INSN16
11359 beq rt, ta, label ; 15_PCREL */
11360
11361 bfd_vma laddr;
11362 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11363 bfd_signed_vma foff;
11364 uint32_t insn, re_insn = 0;
11365 uint16_t insn16;
11366 uint32_t imm11;
35c08157 11367
1c8f6a4d
KLC
11368 irelend = internal_relocs + sec->reloc_count;
11369 laddr = irel->r_offset;
35c08157 11370
1c8f6a4d
KLC
11371 /* Get the reloc for the address from which the register is
11372 being loaded. This reloc will tell us which function is
11373 actually being called. */
35c08157 11374
1c8f6a4d
KLC
11375 cond_irel =
11376 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11377 R_NDS32_15_PCREL_RELA, irel->r_addend);
11378 if (cond_irel == irelend)
11379 {
695344c0 11380 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
2dcf00ce 11381 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11382 return FALSE;
11383 }
35c08157 11384
1c8f6a4d 11385 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11386 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11387
fbaf61ad
NC
11388 if (foff == 0
11389 || foff < -CONSERVATIVE_8BIT_S1
1c8f6a4d
KLC
11390 || foff >= CONSERVATIVE_8BIT_S1)
11391 return FALSE;
35c08157 11392
1c8f6a4d
KLC
11393 /* Get the first instruction for its size. */
11394 insn = bfd_getb32 (contents + laddr);
11395 if (insn & 0x80000000)
11396 {
11397 *seq_len = 0;
11398 /* Get the immediate from movi55. */
11399 imm11 = N16_IMM5S (insn >> 16);
11400 }
11401 else
11402 {
11403 /* Get the immediate from movi. */
11404 imm11 = N32_IMM20S (insn);
35c08157
KLC
11405 }
11406
1c8f6a4d
KLC
11407 /* Get the branch instruction. */
11408 insn = bfd_getb32 (contents + irel->r_addend);
11409 /* Convert instruction to BR3. */
11410 if ((insn >> 14) & 0x1)
11411 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11412 else
11413 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
35c08157 11414
1c8f6a4d 11415 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
35c08157 11416
1c8f6a4d
KLC
11417 /* Set all relocations. */
11418 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11419 R_NDS32_WORD_9_PCREL_RELA);
11420
11421 /* Clean relocations. */
11422 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11423 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11424 R_NDS32_INSN16, irel->r_offset);
11425 if (insn_irel != irelend)
11426 {
11427 if (*seq_len == 0)
35c08157 11428 {
1c8f6a4d
KLC
11429 /* If the first insntruction is 16bit, convert it to nop16. */
11430 insn16 = NDS32_NOP16;
11431 bfd_putb16 (insn16, contents + laddr);
11432 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157 11433 }
1c8f6a4d
KLC
11434 else
11435 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11436 R_NDS32_NONE);
35c08157 11437 }
1c8f6a4d 11438 *insn_len = 0;
35c08157 11439
1c8f6a4d
KLC
11440 return TRUE;
11441}
35c08157 11442
fbaf61ad
NC
11443/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11444 for each relax round. But the gp may changed dramatically and then cause
11445 the truncated to fit errors for the the converted gp instructions.
11446 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11447
11448static bfd_boolean
11449nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11450 Elf_Internal_Rela *irel, bfd_boolean *again,
11451 bfd_boolean init,
11452 struct elf_nds32_link_hash_table *table,
11453 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11454
11455{
11456 int offset_to_gp;
11457 static bfd_boolean sec_pass = FALSE;
11458 static asection *first_sec = NULL, *sym_sec;
11459 /* Record the number of instructions which may be removed. */
11460 static int count = 0, record_count;
11461 Elf_Internal_Sym *isym;
11462 struct elf_link_hash_entry *h = NULL;
11463 int indx;
11464 unsigned long r_symndx;
11465 bfd *abfd = sec->owner;
11466 static bfd_vma record_sda = 0;
11467 int sda_offset = 0;
11468
11469 /* Force doing relaxation when hyper-relax is high. */
11470 if (table->hyper_relax == 2)
11471 return TRUE;
11472
11473 /* Do not relax the load/store patterns for the first
11474 relax round. */
11475 if (init)
11476 {
11477 if (!first_sec)
11478 first_sec = sec;
11479 else if (first_sec == sec)
11480 {
11481 record_count = count;
11482 count = 0;
11483 sec_pass = TRUE;
11484 }
11485
11486 if (!sec_pass)
11487 *again = TRUE;
11488
11489 return TRUE;
11490 }
11491
11492 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11493 DATA_SEGMENT_ALIGN size in the linker script may make it
11494 get even bigger. */
11495 if (record_sda == 0)
11496 record_sda = local_sda;
11497 else if (local_sda > record_sda)
11498 sda_offset = local_sda - record_sda;
11499
11500 /* Assume the instruction will be removed in the best case. */
11501 count++;
11502
11503 /* We record the offset to gp for each symbol, and then check
11504 if it is changed dramatically after relaxing.
11505 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11506 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11507 r_symndx = ELF32_R_SYM (irel->r_info);
11508 if (r_symndx >= symtab_hdr->sh_info)
11509 {
11510 /* Global symbols. */
11511 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11512 h = elf_sym_hashes (abfd)[indx];
11513 sym_sec = h->root.u.def.section;
11514 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11515 || bfd_is_abs_section (sym_sec))
11516 {
11517 /* Forbid doing relaxation when hyper-relax is low. */
11518 if (table->hyper_relax == 0)
11519 return FALSE;
11520
11521 offset_to_gp = *access_addr - local_sda;
11522 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11523 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11524 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11525 < abs (offset_to_gp) - sda_offset)
11526 {
11527 /* This may cause the error, so we reserve the
11528 safe enough size for relaxing. */
11529 if (*access_addr >= local_sda)
11530 *access_addr += (record_count * 4);
11531 else
11532 *access_addr -= (record_count * 4);
11533 }
11534 return sec_pass;
11535 }
11536 }
11537 else
11538 {
11539 /* Local symbols. */
11540 if (!elf32_nds32_allocate_local_sym_info (abfd))
11541 return FALSE;
11542 isym = isymbuf + r_symndx;
11543
11544 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11545 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11546 {
11547 /* Forbid doing relaxation when hyper-relax is low. */
11548 if (table->hyper_relax == 0)
11549 return FALSE;
11550
11551 offset_to_gp = *access_addr - local_sda;
11552 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11553 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11554 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11555 < abs (offset_to_gp) - sda_offset)
11556 {
11557 /* This may cause the error, so we reserve the
11558 safe enough size for relaxing. */
11559 if (*access_addr >= local_sda)
11560 *access_addr += (record_count * 4);
11561 else
11562 *access_addr -= (record_count * 4);
11563 }
11564 return sec_pass;
11565 }
11566 }
11567
11568 return TRUE;
11569}
11570
1c8f6a4d 11571#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
35c08157 11572
1c8f6a4d 11573/* Relax LOADSTORE relocation for nds32_elf_relax_section. */
35c08157 11574
1c8f6a4d
KLC
11575static bfd_boolean
11576nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11577 asection *sec, Elf_Internal_Rela *irel,
11578 Elf_Internal_Rela *internal_relocs, int *insn_len,
11579 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
fbaf61ad
NC
11580 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11581 struct elf_nds32_link_hash_table *table)
1c8f6a4d 11582{
6cae483a
AM
11583 int eliminate_sethi = 0, range_type;
11584 unsigned int i;
1c8f6a4d
KLC
11585 bfd_vma local_sda, laddr;
11586 int seq_len; /* Original length of instruction sequence. */
11587 uint32_t insn;
11588 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11589 bfd_vma access_addr = 0;
11590 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
fbaf61ad
NC
11591 struct elf_link_hash_entry *h = NULL;
11592 int indx;
1c8f6a4d
KLC
11593 enum elf_nds32_reloc_type checked_types[] =
11594 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11595 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11596 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11597 R_NDS32_TLS_LE_HI20
11598 };
35c08157 11599
1c8f6a4d
KLC
11600 irelend = internal_relocs + sec->reloc_count;
11601 seq_len = GET_SEQ_LEN (irel->r_addend);
11602 laddr = irel->r_offset;
11603 *insn_len = seq_len;
35c08157 11604
1c8f6a4d 11605 /* Get the high part relocation. */
6cae483a 11606 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11607 {
11608 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11609 checked_types[i], laddr);
11610 if (hi_irelfn != irelend)
11611 break;
11612 }
35c08157 11613
1c8f6a4d
KLC
11614 if (hi_irelfn == irelend)
11615 {
fbaf61ad
NC
11616 /* Not R_NDS32_HI20_RELA. */
11617 if (i != 0)
11618 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11619 (uint64_t) irel->r_offset);
11620 return FALSE;
1c8f6a4d 11621 }
35c08157 11622
1c8f6a4d
KLC
11623 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11624 nds32_elf_final_sda_base (sec->output_section->owner,
11625 link_info, &local_sda, FALSE);
35c08157 11626
1c8f6a4d
KLC
11627 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11628 {
11629 case R_NDS32_HI20_RELA:
11630 insn = bfd_getb32 (contents + laddr);
11631 access_addr =
11632 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11633
fbaf61ad 11634 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
1c8f6a4d 11635 {
fbaf61ad
NC
11636 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11637 h = elf_sym_hashes (abfd)[indx];
11638 }
35c08157 11639
fbaf61ad
NC
11640 /* Try movi. */
11641 if (range_type == NDS32_LOADSTORE_IMM
11642 && access_addr < CONSERVATIVE_20BIT
11643 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11644 {
11645 eliminate_sethi = 1;
11646 break;
11647 }
1c8f6a4d 11648
fbaf61ad
NC
11649 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11650 {
11651 eliminate_sethi = 1;
11652 break;
1c8f6a4d 11653 }
fbaf61ad
NC
11654 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11655 NULL, FALSE, table, isymbuf, symtab_hdr))
11656 return FALSE;
1c8f6a4d
KLC
11657
11658 if (!load_store_relax)
11659 return FALSE;
11660
11661 /* Case for set gp register. */
11662 if (N32_RT5 (insn) == REG_GP)
fbaf61ad 11663 return FALSE;
1c8f6a4d
KLC
11664
11665 if (range_type == NDS32_LOADSTORE_FLOAT_S
6cef73f9 11666 || range_type == NDS32_LOADSTORE_FLOAT_D)
1c8f6a4d
KLC
11667 {
11668 range_l = sdata_range[0][0];
11669 range_h = sdata_range[0][1];
11670 }
11671 else
11672 {
11673 range_l = sdata_range[1][0];
11674 range_h = sdata_range[1][1];
11675 }
11676 break;
11677
1c8f6a4d
KLC
11678 default:
11679 return FALSE;
11680 }
11681
11682 /* Delete sethi instruction. */
11683 if (eliminate_sethi == 1
11684 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11685 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11686 {
11687 hi_irelfn->r_info =
11688 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11689 irel->r_info =
11690 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11691 *insn_len = 0;
fbaf61ad 11692 return TRUE;
1c8f6a4d 11693 }
fbaf61ad
NC
11694
11695 return FALSE;
1c8f6a4d
KLC
11696}
11697
11698/* Relax LO12 relocation for nds32_elf_relax_section. */
11699
11700static void
11701nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11702 asection *sec, Elf_Internal_Rela *irel,
11703 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
fbaf61ad
NC
11704 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11705 struct elf_nds32_link_hash_table *table)
1c8f6a4d
KLC
11706{
11707 uint32_t insn;
11708 bfd_vma local_sda, laddr;
11709 unsigned long reloc;
11710 bfd_vma access_addr;
11711 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11712 Elf_Internal_Rela *irelfn = NULL, *irelend;
11713 struct elf_link_hash_entry *h = NULL;
11714 int indx;
11715
11716 /* For SDA base relative relaxation. */
11717 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11718 &local_sda, FALSE);
11719
11720 irelend = internal_relocs + sec->reloc_count;
11721 laddr = irel->r_offset;
11722 insn = bfd_getb32 (contents + laddr);
11723
11724 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11725 return;
11726
11727 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11728
11729 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11730 {
11731 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11732 h = elf_sym_hashes (abfd)[indx];
11733 }
11734
fbaf61ad 11735 /* Try movi. */
1c8f6a4d
KLC
11736 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11737 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11738 {
11739 reloc = R_NDS32_20_RELA;
11740 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11741 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11742 bfd_putb32 (insn, contents + laddr);
11743 }
1c8f6a4d
KLC
11744 else
11745 {
fbaf61ad
NC
11746 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11747 {
11748 /* Fall through. */
11749 }
11750 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11751 FALSE, table, isymbuf, symtab_hdr))
11752 return;
11753
1c8f6a4d
KLC
11754 range_l = sdata_range[1][0];
11755 range_h = sdata_range[1][1];
11756 switch (ELF32_R_TYPE (irel->r_info))
11757 {
11758 case R_NDS32_LO12S0_RELA:
11759 reloc = R_NDS32_SDA19S0_RELA;
11760 break;
11761 case R_NDS32_LO12S1_RELA:
11762 reloc = R_NDS32_SDA18S1_RELA;
11763 break;
11764 case R_NDS32_LO12S2_RELA:
11765 reloc = R_NDS32_SDA17S2_RELA;
11766 break;
11767 case R_NDS32_LO12S2_DP_RELA:
11768 range_l = sdata_range[0][0];
11769 range_h = sdata_range[0][1];
11770 reloc = R_NDS32_SDA12S2_DP_RELA;
11771 break;
11772 case R_NDS32_LO12S2_SP_RELA:
11773 range_l = sdata_range[0][0];
11774 range_h = sdata_range[0][1];
11775 reloc = R_NDS32_SDA12S2_SP_RELA;
11776 break;
11777 default:
11778 return;
11779 }
11780
11781 /* There are range_h and range_l because linker has to promise
11782 all sections move cross one page together. */
11783 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
fbaf61ad
NC
11784 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11785 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
1c8f6a4d
KLC
11786 {
11787 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
35c08157 11788 {
1c8f6a4d
KLC
11789 /* Maybe we should add R_NDS32_INSN16 reloc type here
11790 or manually do some optimization. sethi can't be
11791 eliminated when updating $gp so the relative ori
11792 needs to be preserved. */
11793 return;
35c08157 11794 }
1c8f6a4d
KLC
11795 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11796 &insn))
11797 return;
11798 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11799 bfd_putb32 (insn, contents + laddr);
11800
11801 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11802 R_NDS32_INSN16);
11803 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11804 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11805 irelfn->r_info =
11806 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11807
35c08157 11808 }
1c8f6a4d
KLC
11809 }
11810 return;
11811}
35c08157 11812
1c8f6a4d 11813/* Relax PTR relocation for nds32_elf_relax_section. */
35c08157 11814
1c8f6a4d
KLC
11815static bfd_boolean
11816nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11817 Elf_Internal_Rela *internal_relocs, int *insn_len,
11818 int *seq_len, bfd_byte *contents)
11819{
11820 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
35c08157 11821
1c8f6a4d 11822 irelend = internal_relocs + sec->reloc_count;
35c08157 11823
1c8f6a4d
KLC
11824 re_irel =
11825 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11826 R_NDS32_PTR_RESOLVED, irel->r_addend);
35c08157 11827
1c8f6a4d
KLC
11828 if (re_irel == irelend)
11829 {
695344c0 11830 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
2dcf00ce 11831 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11832 return FALSE;
11833 }
35c08157 11834
1c8f6a4d
KLC
11835 if (re_irel->r_addend != 1)
11836 return FALSE;
35c08157 11837
1c8f6a4d
KLC
11838 /* Pointed target is relaxed and no longer needs this void *,
11839 change the type to NONE. */
11840 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11841
1c8f6a4d
KLC
11842 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11843 not exist, it means only count 1 and remove it directly. */
11844 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11845 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11846 R_NDS32_PTR_COUNT);
11847 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11848 R_NDS32_PTR);
11849 if (count_irel != irelend)
11850 {
11851 if (--count_irel->r_addend > 0)
11852 return FALSE;
11853 }
11854
11855 if (ptr_irel != irelend)
11856 return FALSE;
11857
11858 /* If the PTR_COUNT is already 0, remove current instruction. */
11859 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11860 *insn_len = 0;
11861 return TRUE;
11862}
11863
fbaf61ad 11864/* Relax LWC relocation for nds32_elf_relax_section. */
1c8f6a4d
KLC
11865
11866static void
fbaf61ad
NC
11867nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11868 asection *sec, Elf_Internal_Rela *irel,
11869 Elf_Internal_Rela *internal_relocs,
11870 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11871 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
1c8f6a4d 11872{
fbaf61ad
NC
11873 /* Pattern:
11874 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11875 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11876 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11877 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11878 ... */
11879
1c8f6a4d 11880 uint32_t insn;
1c8f6a4d 11881 bfd_vma local_sda, laddr;
fbaf61ad
NC
11882 unsigned long reloc;
11883 bfd_vma access_addr, flsi_offset;
11884 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11885 Elf_Internal_Rela *irelend, *re_irel;
11886 unsigned int opcode;
1c8f6a4d
KLC
11887
11888 irelend = internal_relocs + sec->reloc_count;
11889 laddr = irel->r_offset;
11890 insn = bfd_getb32 (contents + laddr);
11891
fbaf61ad 11892 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
1c8f6a4d
KLC
11893 return;
11894
fbaf61ad
NC
11895 /* Can not do relaxation for bi format. */
11896 if ((insn & 0x1000))
1c8f6a4d
KLC
11897 return;
11898
fbaf61ad
NC
11899 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11900 opcode = N32_OP6 (insn);
11901 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11902 reloc = R_NDS32_SDA12S2_SP_RELA;
11903 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11904 reloc = R_NDS32_SDA12S2_DP_RELA;
1c8f6a4d
KLC
11905 else
11906 return;
11907
fbaf61ad
NC
11908 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11909 R_NDS32_PTR_RESOLVED);
11910 if (re_irel == irelend)
1c8f6a4d 11911 {
fbaf61ad
NC
11912 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11913 (uint64_t) irel->r_offset);
11914 return;
1c8f6a4d 11915 }
1c8f6a4d 11916
fbaf61ad 11917 /* For SDA base relative relaxation. */
1c8f6a4d
KLC
11918 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11919 &local_sda, FALSE);
fbaf61ad
NC
11920 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11921 flsi_offset = (insn & 0xfff) << 2;
11922 access_addr += flsi_offset;
11923 range_l = sdata_range[0][0];
11924 range_h = sdata_range[0][1];
1c8f6a4d 11925
fbaf61ad
NC
11926 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11927 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
1c8f6a4d 11928 {
fbaf61ad
NC
11929 /* Turn flsi instruction into sda access format. */
11930 insn = (insn & 0x7ff07000) | (REG_GP << 15);
1c8f6a4d 11931
fbaf61ad
NC
11932 /* Add relocation type to flsi. */
11933 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11934 irel->r_addend += flsi_offset;
11935 bfd_putb32 (insn, contents + re_irel->r_offset);
1c8f6a4d 11936
fbaf61ad 11937 re_irel->r_addend |= 1;
1c8f6a4d
KLC
11938 *again = TRUE;
11939 }
1c8f6a4d
KLC
11940}
11941
11942static bfd_boolean
11943nds32_relax_adjust_label (bfd *abfd, asection *sec,
11944 Elf_Internal_Rela *internal_relocs,
11945 bfd_byte *contents,
11946 nds32_elf_blank_t **relax_blank_list,
11947 int optimize, int opt_size)
11948{
11949 /* This code block is used to adjust 4-byte alignment by relax a pair
11950 of instruction a time.
11951
11952 It recognizes three types of relocations.
de194d85 11953 1. R_NDS32_LABEL - a alignment.
1c8f6a4d
KLC
11954 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11955 3. is_16bit_NOP () - remove a 16-bit instruction. */
11956
de194d85
YC
11957 /* TODO: It seems currently implementation only support 4-byte alignment.
11958 We should handle any-alignment. */
1c8f6a4d
KLC
11959
11960 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11961 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11962 Elf_Internal_Rela rel_temp;
11963 Elf_Internal_Rela *irelend;
11964 bfd_vma address;
11965 uint16_t insn16;
11966
11967 /* Checking for branch relaxation relies on the relocations to
11968 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11969 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11970 sizeof (Elf_Internal_Rela), compar_reloc);
11971
11972 irelend = internal_relocs + sec->reloc_count;
11973
11974 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11975 /* FIXME: Can we generate the right order in assembler?
11976 So we don't have to swapping them here. */
11977
11978 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11979 label_rel < irelend; label_rel++)
11980 {
11981 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11982 continue;
11983
11984 /* Find the first reloc has the same offset with label_rel. */
11985 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11986 insn_rel++;
11987
11988 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11989 insn_rel++)
11990 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11991 address. */
11992 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11993 break;
11994
11995 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11996 && insn_rel < label_rel)
11997 {
11998 /* Swap the two reloc if the R_NDS32_INSN16 is
11999 before R_NDS32_LABEL. */
12000 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
12001 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
12002 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
12003 }
12004 }
12005
12006 label_rel = NULL;
12007 insn_rel = NULL;
12008 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12009 or higher, remove other R_NDS32_LABEL with lower alignment.
12010 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12011 then the R_NDS32_LABEL sequence is broke. */
12012 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12013 {
12014 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12015 {
12016 if (label_rel == NULL)
12017 {
12018 if (tmp_rel->r_addend < 2)
12019 label_rel = tmp_rel;
12020 continue;
12021 }
12022 else if (tmp_rel->r_addend > 1)
12023 {
12024 /* Remove all LABEL relocation from label_rel to tmp_rel
12025 including relocations with same offset as tmp_rel. */
fbaf61ad 12026 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
1c8f6a4d 12027 {
fbaf61ad
NC
12028 if (tmp2_rel->r_offset == tmp_rel->r_offset)
12029 break;
12030
1c8f6a4d
KLC
12031 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12032 && tmp2_rel->r_addend < 2)
12033 tmp2_rel->r_info =
12034 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12035 R_NDS32_NONE);
12036 }
12037 label_rel = NULL;
12038 }
12039 }
12040 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12041 {
12042 /* A new INSN16 which can be converted, so clear label_rel. */
12043 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12044 irelend, &insn16)
12045 || is_16bit_NOP (abfd, sec, tmp_rel))
12046 label_rel = NULL;
12047 }
12048 }
12049
12050 label_rel = NULL;
12051 insn_rel = NULL;
12052 /* Optimized for speed and nothing has not been relaxed.
12053 It's time to align labels.
12054 We may convert a 16-bit instruction right before a label to
12055 32-bit, in order to align the label if necessary
12056 all reloc entries has been sorted by r_offset. */
fbaf61ad
NC
12057 for (irel = internal_relocs;
12058 irel < irelend && irel->r_offset < sec->size; irel++)
1c8f6a4d
KLC
12059 {
12060 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12061 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12062 continue;
12063
12064 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12065 {
12066 /* A new INSN16 found, resize the old one. */
12067 if (is_convert_32_to_16
12068 (abfd, sec, irel, internal_relocs, irelend, &insn16)
12069 || is_16bit_NOP (abfd, sec, irel))
12070 {
12071 if (insn_rel)
12072 {
12073 /* Previous INSN16 reloc exists, reduce its
12074 size to 16-bit. */
12075 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12076 irelend, &insn16))
35c08157 12077 {
1c8f6a4d
KLC
12078 nds32_elf_write_16 (abfd, contents, insn_rel,
12079 internal_relocs, irelend, insn16);
12080
12081 if (!insert_nds32_elf_blank_recalc_total
12082 (relax_blank_list, insn_rel->r_offset + 2, 2))
12083 return FALSE;
12084 }
12085 else if (is_16bit_NOP (abfd, sec, insn_rel))
12086 {
12087 if (!insert_nds32_elf_blank_recalc_total
12088 (relax_blank_list, insn_rel->r_offset, 2))
12089 return FALSE;
12090 }
12091 insn_rel->r_info =
12092 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12093 }
12094 /* Save the new one for later use. */
12095 insn_rel = irel;
12096 }
12097 else
12098 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12099 R_NDS32_NONE);
12100 }
12101 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12102 {
12103 /* Search for label. */
12104 int force_relax = 0;
12105
12106 /* Label on 16-bit instruction or optimization
12107 needless, just reset this reloc. */
12108 insn16 = bfd_getb16 (contents + irel->r_offset);
12109 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12110 {
12111 irel->r_info =
12112 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12113 continue;
12114 }
12115
12116 address =
12117 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12118 irel->r_offset, 1);
12119
12120 if (!insn_rel)
12121 {
12122 /* Check if there is case which can not be aligned. */
12123 if (irel->r_addend == 2 && address & 0x2)
12124 return FALSE;
12125 continue;
12126 }
12127
12128 /* Try to align this label. */
12129
12130 if ((irel->r_addend & 0x1f) < 2)
12131 {
12132 /* Check if there is a INSN16 at the same address.
12133 Label_rel always seats before insn_rel after
12134 our sort. */
12135
12136 /* Search for INSN16 at LABEL location. If INSN16 is at
12137 same location and this LABEL alignment is lower than 2,
12138 the INSN16 can be converted to 2-byte. */
12139 for (tmp_rel = irel;
12140 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12141 tmp_rel++)
12142 {
12143 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12144 && (is_convert_32_to_16
12145 (abfd, sec, tmp_rel, internal_relocs,
12146 irelend, &insn16)
12147 || is_16bit_NOP (abfd, sec, tmp_rel)))
12148 {
12149 force_relax = 1;
12150 break;
12151 }
12152 }
12153 }
12154
12155 if (force_relax || irel->r_addend == 1 || address & 0x2)
12156 {
12157 /* Label not aligned. */
12158 /* Previous reloc exists, reduce its size to 16-bit. */
12159 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12160 internal_relocs, irelend, &insn16))
12161 {
12162 nds32_elf_write_16 (abfd, contents, insn_rel,
12163 internal_relocs, irelend, insn16);
12164
12165 if (!insert_nds32_elf_blank_recalc_total
12166 (relax_blank_list, insn_rel->r_offset + 2, 2))
12167 return FALSE;
12168 }
12169 else if (is_16bit_NOP (abfd, sec, insn_rel))
12170 {
12171 if (!insert_nds32_elf_blank_recalc_total
12172 (relax_blank_list, insn_rel->r_offset, 2))
12173 return FALSE;
12174 }
12175
12176 }
12177 /* INSN16 reloc is used. */
12178 insn_rel = NULL;
12179 }
12180 }
12181
12182 address =
12183 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12184 if (insn_rel && (address & 0x2 || opt_size))
12185 {
12186 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12187 irelend, &insn16))
12188 {
12189 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12190 irelend, insn16);
12191 if (!insert_nds32_elf_blank_recalc_total
12192 (relax_blank_list, insn_rel->r_offset + 2, 2))
12193 return FALSE;
12194 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12195 R_NDS32_NONE);
12196 }
12197 else if (is_16bit_NOP (abfd, sec, insn_rel))
12198 {
12199 if (!insert_nds32_elf_blank_recalc_total
12200 (relax_blank_list, insn_rel->r_offset, 2))
12201 return FALSE;
12202 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12203 R_NDS32_NONE);
12204 }
12205 }
12206 insn_rel = NULL;
12207 return TRUE;
12208}
12209
1c8f6a4d
KLC
12210static bfd_boolean
12211nds32_elf_relax_section (bfd *abfd, asection *sec,
12212 struct bfd_link_info *link_info, bfd_boolean *again)
12213{
12214 nds32_elf_blank_t *relax_blank_list = NULL;
12215 Elf_Internal_Shdr *symtab_hdr;
12216 Elf_Internal_Rela *internal_relocs;
12217 Elf_Internal_Rela *irel;
12218 Elf_Internal_Rela *irelend;
12219 Elf_Internal_Sym *isymbuf = NULL;
12220 bfd_byte *contents = NULL;
12221 bfd_boolean result = TRUE;
12222 int optimize = 0;
12223 int opt_size = 0;
12224 uint32_t insn;
12225 uint16_t insn16;
12226
12227 /* Target dependnet option. */
12228 struct elf_nds32_link_hash_table *table;
12229 int load_store_relax;
1c8f6a4d
KLC
12230
12231 relax_blank_list = NULL;
12232
12233 *again = FALSE;
12234
12235 /* Nothing to do for
12236 * relocatable link or
12237 * non-relocatable section or
12238 * non-code section or
12239 * empty content or
12240 * no reloc entry. */
0e1862bb 12241 if (bfd_link_relocatable (link_info)
1c8f6a4d 12242 || (sec->flags & SEC_RELOC) == 0
cd28e7aa 12243 || (sec->flags & SEC_EXCLUDE) != 0
1c8f6a4d 12244 || (sec->flags & SEC_CODE) == 0
fbaf61ad
NC
12245 || sec->size == 0
12246 || sec->reloc_count == 0)
1c8f6a4d
KLC
12247 return TRUE;
12248
12249 /* 09.12.11 Workaround. */
12250 /* We have to adjust align for R_NDS32_LABEL if needed.
12251 The adjust approach only can fix 2-byte align once. */
12252 if (sec->alignment_power > 2)
12253 return TRUE;
12254
fbaf61ad
NC
12255 /* Do TLS model conversion once at first. */
12256 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12257
1c8f6a4d
KLC
12258 /* The optimization type to do. */
12259
12260 table = nds32_elf_hash_table (link_info);
1c8f6a4d 12261
fbaf61ad
NC
12262 /* Save the first section for abs symbol relaxation.
12263 This is used for checking gp relaxation in the
12264 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12265 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12266 table, NULL, NULL);
12267
1c8f6a4d
KLC
12268 /* The begining of general relaxation. */
12269
12270 if (is_SDA_BASE_set == 0)
12271 {
12272 bfd_vma gp;
12273 is_SDA_BASE_set = 1;
12274 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12275 &gp, FALSE);
12276 relax_range_measurement (abfd);
12277 }
12278
1c8f6a4d
KLC
12279 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12280 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12281 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12282 TRUE /* keep_memory */);
12283 if (internal_relocs == NULL)
12284 goto error_return;
12285
12286 irelend = internal_relocs + sec->reloc_count;
12287 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12288 irelend, R_NDS32_RELAX_ENTRY);
12289
12290 if (irel == irelend)
12291 return TRUE;
12292
12293 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12294 {
12295 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
f9671640 12296 return TRUE;
1c8f6a4d
KLC
12297
12298 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12299 optimize = 1;
12300
12301 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12302 opt_size = 1;
12303 }
12304
12305 load_store_relax = table->load_store_relax;
12306
12307 /* Get symbol table and section content. */
0c4bd9d9 12308 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
1c8f6a4d
KLC
12309 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12310 goto error_return;
12311
12312 /* Do relax loop only when finalize is not done.
12313 Take care of relaxable relocs except INSN16. */
12314 for (irel = internal_relocs; irel < irelend; irel++)
12315 {
12316 int seq_len; /* Original length of instruction sequence. */
12317 int insn_len = 0; /* Final length of instruction sequence. */
12318 bfd_boolean removed;
12319
12320 insn = 0;
12321 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12322 && (irel->r_addend & 0x1f) >= 2)
12323 optimize = 1;
12324
12325 /* Relocation Types
12326 R_NDS32_LONGCALL1 53
12327 R_NDS32_LONGCALL2 54
12328 R_NDS32_LONGCALL3 55
12329 R_NDS32_LONGJUMP1 56
12330 R_NDS32_LONGJUMP2 57
12331 R_NDS32_LONGJUMP3 58
12332 R_NDS32_LOADSTORE 59 */
12333 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12334 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12335 seq_len = GET_SEQ_LEN (irel->r_addend);
12336
12337 /* Relocation Types
12338 R_NDS32_LONGCALL4 107
12339 R_NDS32_LONGCALL5 108
12340 R_NDS32_LONGCALL6 109
12341 R_NDS32_LONGJUMP4 110
12342 R_NDS32_LONGJUMP5 111
12343 R_NDS32_LONGJUMP6 112
12344 R_NDS32_LONGJUMP7 113 */
12345 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12346 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12347 seq_len = 4;
12348
12349 /* Relocation Types
12350 R_NDS32_LO12S0_RELA 30
12351 R_NDS32_LO12S1_RELA 29
12352 R_NDS32_LO12S2_RELA 28
12353 R_NDS32_LO12S2_SP_RELA 71
12354 R_NDS32_LO12S2_DP_RELA 70
12355 R_NDS32_GOT_LO12 46
12356 R_NDS32_GOTOFF_LO12 50
12357 R_NDS32_PLTREL_LO12 65
12358 R_NDS32_PLT_GOTREL_LO12 67
12359 R_NDS32_17IFC_PCREL_RELA 96
12360 R_NDS32_GOT_SUFF 193
12361 R_NDS32_GOTOFF_SUFF 194
12362 R_NDS32_PLT_GOT_SUFF 195
12363 R_NDS32_MULCALL_SUFF 196
12364 R_NDS32_PTR 197 */
12365 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12366 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12367 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12368 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12369 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12370 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12371 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12372 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12373 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12374 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12375 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12376 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12377 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12378 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
fbaf61ad
NC
12379 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12380 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
1c8f6a4d
KLC
12381 seq_len = 0;
12382 else
12383 continue;
12384
12385 insn_len = seq_len;
12386 removed = FALSE;
12387
12388 switch (ELF32_R_TYPE (irel->r_info))
12389 {
12390 case R_NDS32_LONGCALL1:
12391 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12392 &insn_len, contents, isymbuf,
12393 symtab_hdr);
12394 break;
12395 case R_NDS32_LONGCALL2:
12396 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12397 &insn_len, contents, isymbuf,
12398 symtab_hdr);
12399 break;
12400 case R_NDS32_LONGCALL3:
12401 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12402 &insn_len, contents, isymbuf,
12403 symtab_hdr);
12404 break;
12405 case R_NDS32_LONGJUMP1:
12406 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12407 &insn_len, contents, isymbuf,
12408 symtab_hdr);
12409 break;
12410 case R_NDS32_LONGJUMP2:
12411 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12412 &insn_len, contents, isymbuf,
12413 symtab_hdr);
12414 break;
12415 case R_NDS32_LONGJUMP3:
12416 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12417 &insn_len, contents, isymbuf,
12418 symtab_hdr);
12419 break;
12420 case R_NDS32_LONGCALL4:
12421 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12422 &insn_len, contents, isymbuf,
12423 symtab_hdr);
12424 break;
12425 case R_NDS32_LONGCALL5:
12426 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12427 &insn_len, contents, isymbuf,
12428 symtab_hdr);
12429 break;
12430 case R_NDS32_LONGCALL6:
12431 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12432 &insn_len, contents, isymbuf,
12433 symtab_hdr);
12434 break;
12435 case R_NDS32_LONGJUMP4:
12436 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12437 &insn_len, contents, isymbuf,
12438 symtab_hdr);
12439 break;
12440 case R_NDS32_LONGJUMP5:
12441 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12442 &insn_len, &seq_len, contents,
12443 isymbuf, symtab_hdr);
12444 break;
12445 case R_NDS32_LONGJUMP6:
12446 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12447 &insn_len, &seq_len, contents,
12448 isymbuf, symtab_hdr);
12449 break;
12450 case R_NDS32_LONGJUMP7:
12451 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12452 &insn_len, &seq_len, contents,
12453 isymbuf, symtab_hdr);
12454 break;
12455 case R_NDS32_LOADSTORE:
12456 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12457 internal_relocs, &insn_len,
12458 contents, isymbuf, symtab_hdr,
fbaf61ad 12459 load_store_relax, table);
1c8f6a4d
KLC
12460 break;
12461 case R_NDS32_LO12S0_RELA:
12462 case R_NDS32_LO12S1_RELA:
fbaf61ad 12463 case R_NDS32_LO12S2_RELA:
1c8f6a4d
KLC
12464 case R_NDS32_LO12S2_DP_RELA:
12465 case R_NDS32_LO12S2_SP_RELA:
1c8f6a4d
KLC
12466 /* Relax for low part. */
12467 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
fbaf61ad 12468 contents, isymbuf, symtab_hdr, table);
1c8f6a4d
KLC
12469
12470 /* It is impossible to delete blank, so just continue. */
12471 continue;
fbaf61ad
NC
12472 case R_NDS32_PTR:
12473 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12474 &insn_len, &seq_len, contents);
12475 break;
12476 case R_NDS32_LSI:
12477 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12478 contents, isymbuf, symtab_hdr, again);
12479 continue;
1c8f6a4d
KLC
12480 case R_NDS32_GOT_LO12:
12481 case R_NDS32_GOTOFF_LO12:
12482 case R_NDS32_PLTREL_LO12:
12483 case R_NDS32_PLT_GOTREL_LO12:
12484 case R_NDS32_GOTPC_LO12:
1c8f6a4d 12485 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 12486 case R_NDS32_TLS_LE_ADD:
1c8f6a4d 12487 case R_NDS32_TLS_LE_LS:
1c8f6a4d 12488 case R_NDS32_PLT_GOT_SUFF:
1c8f6a4d 12489 case R_NDS32_GOT_SUFF:
1c8f6a4d 12490 case R_NDS32_GOTOFF_SUFF:
1c8f6a4d
KLC
12491 continue;
12492 default:
12493 continue;
1c8f6a4d 12494 }
fbaf61ad 12495
1c8f6a4d
KLC
12496 if (removed && seq_len - insn_len > 0)
12497 {
12498 if (!insert_nds32_elf_blank
12499 (&relax_blank_list, irel->r_offset + insn_len,
12500 seq_len - insn_len))
12501 goto error_return;
12502 *again = TRUE;
35c08157 12503 }
1c8f6a4d
KLC
12504 }
12505
12506 calc_nds32_blank_total (relax_blank_list);
12507
12508 if (table->relax_fp_as_gp)
12509 {
12510 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12511 irelend, isymbuf))
12512 goto error_return;
35c08157 12513
535b785f 12514 if (!*again)
35c08157 12515 {
1c8f6a4d
KLC
12516 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12517 irelend))
12518 goto error_return;
35c08157
KLC
12519 }
12520 }
1c8f6a4d 12521
535b785f 12522 if (!*again)
1c8f6a4d
KLC
12523 {
12524 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12525 &relax_blank_list, optimize, opt_size))
12526 goto error_return;
12527 }
12528
12529 /* It doesn't matter optimize_for_space_no_align anymore.
35c08157
KLC
12530 If object file is assembled with flag '-Os',
12531 the we don't adjust jump-destination on 4-byte boundary. */
12532
12533 if (relax_blank_list)
12534 {
12535 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12536 relax_blank_list = NULL;
12537 }
12538
535b785f 12539 if (!*again)
35c08157
KLC
12540 {
12541 /* Closing the section, so we don't relax it anymore. */
12542 bfd_vma sec_size_align;
12543 Elf_Internal_Rela *tmp_rel;
12544
12545 /* Pad to alignment boundary. Only handle current section alignment. */
609332f1
NC
12546 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12547 & ((-1U) << sec->alignment_power);
35c08157
KLC
12548 if ((sec_size_align - sec->size) & 0x2)
12549 {
12550 insn16 = NDS32_NOP16;
12551 bfd_putb16 (insn16, contents + sec->size);
12552 sec->size += 2;
12553 }
12554
12555 while (sec_size_align != sec->size)
12556 {
12557 insn = NDS32_NOP32;
12558 bfd_putb32 (insn, contents + sec->size);
12559 sec->size += 4;
12560 }
12561
1c8f6a4d
KLC
12562 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12563 irelend, R_NDS32_RELAX_ENTRY);
35c08157
KLC
12564 if (tmp_rel != irelend)
12565 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12566
12567 clean_nds32_elf_blank ();
12568 }
12569
12570finish:
12571 if (internal_relocs != NULL
12572 && elf_section_data (sec)->relocs != internal_relocs)
12573 free (internal_relocs);
12574
12575 if (contents != NULL
12576 && elf_section_data (sec)->this_hdr.contents != contents)
12577 free (contents);
12578
12579 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12580 free (isymbuf);
12581
12582 return result;
12583
12584error_return:
12585 result = FALSE;
12586 goto finish;
12587}
12588
12589static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12590{
12591 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12592 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12593 {NULL, 0, 0, 0, 0}
12594};
12595
12596static bfd_boolean
12597nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12598 struct bfd_link_info *info,
12599 void *finfo ATTRIBUTE_UNUSED,
12600 bfd_boolean (*func) (void *, const char *,
12601 Elf_Internal_Sym *,
12602 asection *,
12603 struct elf_link_hash_entry *)
12604 ATTRIBUTE_UNUSED)
12605{
12606 FILE *sym_ld_script = NULL;
12607 struct elf_nds32_link_hash_table *table;
12608
12609 table = nds32_elf_hash_table (info);
12610 sym_ld_script = table->sym_ld_script;
12611
12612 if (check_start_export_sym)
12613 fprintf (sym_ld_script, "}\n");
12614
12615 return TRUE;
12616}
12617
12618static enum elf_reloc_type_class
12619nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12620 const asection *rel_sec ATTRIBUTE_UNUSED,
12621 const Elf_Internal_Rela *rela)
12622{
12623 switch ((int) ELF32_R_TYPE (rela->r_info))
12624 {
12625 case R_NDS32_RELATIVE:
12626 return reloc_class_relative;
12627 case R_NDS32_JMP_SLOT:
12628 return reloc_class_plt;
12629 case R_NDS32_COPY:
12630 return reloc_class_copy;
12631 default:
12632 return reloc_class_normal;
12633 }
12634}
12635
12636/* Put target dependent option into info hash table. */
12637void
12638bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12639 int relax_fp_as_gp,
12640 int eliminate_gc_relocs,
fbaf61ad
NC
12641 FILE * sym_ld_script,
12642 int hyper_relax,
12643 int tls_desc_trampoline,
12644 int load_store_relax)
35c08157
KLC
12645{
12646 struct elf_nds32_link_hash_table *table;
12647
12648 table = nds32_elf_hash_table (link_info);
12649 if (table == NULL)
12650 return;
12651
12652 table->relax_fp_as_gp = relax_fp_as_gp;
12653 table->eliminate_gc_relocs = eliminate_gc_relocs;
12654 table->sym_ld_script = sym_ld_script;
fbaf61ad
NC
12655 table->hyper_relax = hyper_relax;
12656 table->tls_desc_trampoline = tls_desc_trampoline;
35c08157 12657 table ->load_store_relax = load_store_relax;
35c08157
KLC
12658}
12659\f
fbaf61ad 12660
35c08157
KLC
12661/* These functions and data-structures are used for fp-as-gp
12662 optimization. */
12663
12664#define FAG_THRESHOLD 3 /* At least 3 gp-access. */
1c8f6a4d
KLC
12665/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12666 the read-only section and read-write section. */
12667#define FAG_WINDOW (508 - 32)
35c08157
KLC
12668
12669/* An nds32_fag represent a gp-relative access.
12670 We find best fp-base by using a sliding window
12671 to find a base address which can cover most gp-access. */
12672struct nds32_fag
12673{
12674 struct nds32_fag *next; /* NULL-teminated linked list. */
12675 bfd_vma addr; /* The address of this fag. */
12676 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12677 It is used for applying FP7U2_FLAG. */
12678 int count; /* How many times this address is referred.
12679 There should be exactly `count' relocations
12680 in relas. */
12681 int relas_capcity; /* The buffer size of relas.
12682 We use an array instead of linked-list,
12683 and realloc is used to adjust buffer size. */
12684};
12685
12686static void
12687nds32_fag_init (struct nds32_fag *head)
12688{
12689 memset (head, 0, sizeof (struct nds32_fag));
12690}
12691
12692static void
12693nds32_fag_verify (struct nds32_fag *head)
12694{
12695 struct nds32_fag *iter;
12696 struct nds32_fag *prev;
12697
12698 prev = NULL;
12699 iter = head->next;
12700 while (iter)
12701 {
12702 if (prev && prev->addr >= iter->addr)
12703 puts ("Bug in fp-as-gp insertion.");
12704 prev = iter;
12705 iter = iter->next;
12706 }
12707}
12708
12709/* Insert a fag in ascending order.
12710 If a fag of the same address already exists,
12711 they are chained by relas array. */
12712
12713static void
12714nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12715 Elf_Internal_Rela * rel)
12716{
12717 struct nds32_fag *iter;
12718 struct nds32_fag *new_fag;
12719 const int INIT_RELAS_CAP = 4;
12720
12721 for (iter = head;
12722 iter->next && iter->next->addr <= addr;
12723 iter = iter->next)
12724 /* Find somewhere to insert. */ ;
12725
12726 /* `iter' will be equal to `head' if the list is empty. */
12727 if (iter != head && iter->addr == addr)
12728 {
12729 /* The address exists in the list.
12730 Insert `rel' into relocation list, relas. */
12731
12732 /* Check whether relas is big enough. */
12733 if (iter->count >= iter->relas_capcity)
12734 {
12735 iter->relas_capcity *= 2;
12736 iter->relas = bfd_realloc
12737 (iter->relas, iter->relas_capcity * sizeof (void *));
12738 }
12739 iter->relas[iter->count++] = rel;
12740 return;
12741 }
12742
12743 /* This is a new address. Create a fag node for it. */
12744 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12745 memset (new_fag, 0, sizeof (*new_fag));
12746 new_fag->addr = addr;
12747 new_fag->count = 1;
12748 new_fag->next = iter->next;
12749 new_fag->relas_capcity = INIT_RELAS_CAP;
12750 new_fag->relas = (Elf_Internal_Rela **)
12751 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12752 new_fag->relas[0] = rel;
12753 iter->next = new_fag;
12754
12755 nds32_fag_verify (head);
12756}
12757
12758static void
12759nds32_fag_free_list (struct nds32_fag *head)
12760{
12761 struct nds32_fag *iter;
12762
12763 iter = head->next;
12764 while (iter)
12765 {
12766 struct nds32_fag *tmp = iter;
12767 iter = iter->next;
12768 free (tmp->relas);
12769 tmp->relas = NULL;
12770 free (tmp);
12771 }
12772}
12773
35c08157
KLC
12774/* Find the best fp-base address.
12775 The relocation associated with that address is returned,
12776 so we can track the symbol instead of a fixed address.
12777
12778 When relaxation, the address of an datum may change,
12779 because a text section is shrinked, so the data section
1c8f6a4d 12780 moves forward. If the aligments of text and data section
35c08157
KLC
12781 are different, their distance may change too.
12782 Therefore, tracking a fixed address is not appriate. */
12783
12784static int
12785nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12786{
12787 struct nds32_fag *base; /* First fag in the window. */
12788 struct nds32_fag *last; /* First fag outside the window. */
12789 int accu = 0; /* Usage accumulation. */
12790 struct nds32_fag *best; /* Best fag. */
12791 int baccu = 0; /* Best accumulation. */
12792
12793 /* Use first fag for initial, and find the last fag in the window.
12794
12795 In each iteration, we could simply subtract previous fag
12796 and accumulate following fags which are inside the window,
12797 untill we each the end. */
12798
1c8f6a4d
KLC
12799 if (head->next == NULL)
12800 {
12801 *bestpp = NULL;
12802 return 0;
12803 }
35c08157
KLC
12804
12805 /* Initialize base. */
12806 base = head->next;
12807 best = base;
12808 for (last = base;
12809 last && last->addr < base->addr + FAG_WINDOW;
12810 last = last->next)
12811 accu += last->count;
12812
12813 baccu = accu;
12814
12815 /* Record the best base in each iteration. */
12816 while (base->next)
1c8f6a4d
KLC
12817 {
12818 accu -= base->count;
12819 base = base->next;
12820 /* Account fags in window. */
12821 for (/* Nothing. */;
12822 last && last->addr < base->addr + FAG_WINDOW;
12823 last = last->next)
12824 accu += last->count;
12825
12826 /* A better fp-base? */
12827 if (accu > baccu)
12828 {
12829 best = base;
12830 baccu = accu;
12831 }
12832 }
35c08157
KLC
12833
12834 if (bestpp)
12835 *bestpp = best;
12836 return baccu;
12837}
12838
12839/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12840 so we can convert it fo fp-relative access later.
12841 `best_fag' is the best fp-base. Only those inside the window
12842 of best_fag is applied the flag. */
12843
12844static bfd_boolean
12845nds32_fag_mark_relax (struct bfd_link_info *link_info,
fbaf61ad 12846 asection *sec, struct nds32_fag *best_fag,
35c08157
KLC
12847 Elf_Internal_Rela *internal_relocs,
12848 Elf_Internal_Rela *irelend)
12849{
12850 struct nds32_fag *ifag;
12851 bfd_vma best_fpbase, gp;
12852 bfd *output_bfd;
12853
fbaf61ad 12854 output_bfd = sec->output_section->owner;
35c08157
KLC
12855 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12856 best_fpbase = best_fag->addr;
12857
1c8f6a4d
KLC
12858 if (best_fpbase > gp + sdata_range[1][1]
12859 || best_fpbase < gp - sdata_range[1][0])
35c08157
KLC
12860 return FALSE;
12861
12862 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12863 so we know they can be converted to lwi37.fp. */
12864 for (ifag = best_fag;
12865 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12866 {
12867 int i;
12868
12869 for (i = 0; i < ifag->count; i++)
12870 {
12871 Elf_Internal_Rela *insn16_rel;
12872 Elf_Internal_Rela *fag_rel;
12873
12874 fag_rel = ifag->relas[i];
12875
12876 /* Only if this is within the WINDOWS, FP7U2_FLAG
12877 is applied. */
12878
12879 insn16_rel = find_relocs_at_address
12880 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12881
12882 if (insn16_rel != irelend)
12883 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12884 }
12885 }
12886 return TRUE;
12887}
12888
1c8f6a4d
KLC
12889/* Reset INSN16 to clean fp as gp. */
12890
12891static void
12892nds32_fag_unmark_relax (struct nds32_fag *fag,
12893 Elf_Internal_Rela *internal_relocs,
12894 Elf_Internal_Rela *irelend)
12895{
12896 struct nds32_fag *ifag;
12897 int i;
12898 Elf_Internal_Rela *insn16_rel;
12899 Elf_Internal_Rela *fag_rel;
12900
12901 for (ifag = fag; ifag; ifag = ifag->next)
12902 {
12903 for (i = 0; i < ifag->count; i++)
12904 {
12905 fag_rel = ifag->relas[i];
12906
12907 /* Restore the INSN16 relocation. */
12908 insn16_rel = find_relocs_at_address
12909 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12910
12911 if (insn16_rel != irelend)
12912 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12913 }
12914 }
12915}
12916
35c08157
KLC
12917/* This is the main function of fp-as-gp optimization.
12918 It should be called by relax_section. */
12919
12920static bfd_boolean
12921nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12922 bfd *abfd, asection *sec,
12923 Elf_Internal_Rela *internal_relocs,
12924 Elf_Internal_Rela *irelend,
12925 Elf_Internal_Sym *isymbuf)
12926{
12927 Elf_Internal_Rela *begin_rel = NULL;
12928 Elf_Internal_Rela *irel;
12929 struct nds32_fag fag_head;
12930 Elf_Internal_Shdr *symtab_hdr;
12931 bfd_byte *contents;
1c8f6a4d 12932 bfd_boolean ifc_inside = FALSE;
35c08157
KLC
12933
12934 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12935
12936 /* Per-function fp-base selection.
12937 1. Create a list for all the gp-relative access.
12938 2. Base on those gp-relative address,
12939 find a fp-base which can cover most access.
12940 3. Use the fp-base for fp-as-gp relaxation.
12941
12942 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12943 we should
12944 1. delete the `la $fp, _FP_BASE_' instruction and
12945 2. not convert lwi.gp to lwi37.fp.
12946
12947 To delete the _FP_BASE_ instruction, we simply apply
12948 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12949
12950 To suppress the conversion, we simply NOT to apply
12951 R_NDS32_INSN16_FP7U2_FLAG flag. */
12952
12953 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12954
0c4bd9d9 12955 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
35c08157
KLC
12956 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12957 return FALSE;
12958
12959 /* Check whether it is worth for fp-as-gp optimization,
12960 i.e., at least 3 gp-load.
12961
12962 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12963 apply this optimization. */
12964
12965 for (irel = internal_relocs; irel < irelend; irel++)
12966 {
12967 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12968 One we enter the begin of the region, we track all the LW/ST
12969 instructions, so when we leave the region, we try to find
12970 the best fp-base address for those LW/ST instructions. */
12971
12972 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12973 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12974 {
12975 /* Begin of the region. */
12976 if (begin_rel)
695344c0 12977 /* xgettext:c-format */
38f14ab8 12978 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
35c08157
KLC
12979
12980 begin_rel = irel;
12981 nds32_fag_init (&fag_head);
1c8f6a4d 12982 ifc_inside = FALSE;
35c08157
KLC
12983 }
12984 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12985 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12986 {
12987 int accu;
1c8f6a4d 12988 struct nds32_fag *best_fag, *tmp_fag;
35c08157
KLC
12989 int dist;
12990
12991 /* End of the region.
12992 Check whether it is worth to do fp-as-gp. */
12993
12994 if (begin_rel == NULL)
12995 {
695344c0 12996 /* xgettext:c-format */
38f14ab8
AM
12997 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12998 abfd, sec);
35c08157
KLC
12999 continue;
13000 }
13001
13002 accu = nds32_fag_find_base (&fag_head, &best_fag);
13003
1c8f6a4d
KLC
13004 /* Clean FP7U2_FLAG because they may set ever. */
13005 tmp_fag = fag_head.next;
13006 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13007
35c08157
KLC
13008 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
13009 if (accu < FAG_THRESHOLD
fbaf61ad 13010 || !nds32_fag_mark_relax (link_info, sec, best_fag,
35c08157
KLC
13011 internal_relocs, irelend))
13012 {
13013 /* Not worth to do fp-as-gp. */
13014 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13015 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13016 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13017 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13018 nds32_fag_free_list (&fag_head);
13019 begin_rel = NULL;
13020 continue;
13021 }
13022
13023 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13024 so we use it to record the distance to the reloction of best
13025 fp-base. */
13026 dist = best_fag->relas[0] - begin_rel;
13027 BFD_ASSERT (dist > 0 && dist < 0xffffff);
13028 /* Use high 16 bits of addend to record the _FP_BASE_ matched
13029 relocation. And get the base value when relocating. */
1c8f6a4d 13030 begin_rel->r_addend &= (0x1 << 16) - 1;
35c08157
KLC
13031 begin_rel->r_addend |= dist << 16;
13032
13033 nds32_fag_free_list (&fag_head);
13034 begin_rel = NULL;
13035 }
13036
1c8f6a4d 13037 if (begin_rel == NULL || ifc_inside)
35c08157
KLC
13038 /* Skip if we are not in the region of fp-as-gp. */
13039 continue;
13040
13041 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13042 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13043 {
13044 bfd_vma addr;
13045 uint32_t insn;
13046
13047 /* A gp-relative access is found. Insert it to the fag-list. */
13048
13049 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
13050 insn = bfd_getb32 (contents + irel->r_offset);
13051 if (!N32_IS_RT3 (insn))
13052 continue;
13053
13054 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13055 nds32_fag_insert (&fag_head, addr, irel);
13056 }
13057 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13058 {
13059 begin_rel = NULL;
13060 }
1c8f6a4d
KLC
13061 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13062 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13063 {
13064 /* Suppress fp as gp when encounter ifc. */
13065 ifc_inside = TRUE;
13066 }
35c08157
KLC
13067 }
13068
13069 return TRUE;
13070}
13071
13072/* Remove unused `la $fp, _FD_BASE_' instruction. */
13073
13074static bfd_boolean
13075nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13076 Elf_Internal_Rela *internal_relocs,
13077 Elf_Internal_Rela *irelend)
13078{
13079 Elf_Internal_Rela *irel;
13080 Elf_Internal_Shdr *symtab_hdr;
13081 bfd_byte *contents = NULL;
13082 nds32_elf_blank_t *relax_blank_list = NULL;
13083 bfd_boolean result = TRUE;
13084 bfd_boolean unused_region = FALSE;
13085
13086 /*
fbaf61ad
NC
13087 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13088 R_NDS32_17IFC_PCREL_RELA
13089 R_NDS32_10IFCU_PCREL_RELA. */
35c08157
KLC
13090
13091 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
0c4bd9d9 13092 nds32_get_section_contents (abfd, sec, &contents, TRUE);
35c08157
KLC
13093
13094 for (irel = internal_relocs; irel < irelend; irel++)
13095 {
13096 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13097 we marked to in previous pass.
13098 DO NOT scan relocations again, since we've alreadly decided it
13099 and set the flag. */
13100 const char *syname;
13101 int syndx;
13102 uint32_t insn;
13103
13104 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13105 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13106 unused_region = TRUE;
13107 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13108 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13109 unused_region = FALSE;
13110
13111 /* We're not in the region. */
13112 if (!unused_region)
13113 continue;
13114
13115 /* _FP_BASE_ must be a GLOBAL symbol. */
13116 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13117 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13118 continue;
13119
13120 /* The symbol name must be _FP_BASE_. */
13121 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13122 if (strcmp (syname, FP_BASE_NAME) != 0)
13123 continue;
13124
13125 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13126 {
13127 /* addi.gp $fp, -256 */
13128 insn = bfd_getb32 (contents + irel->r_offset);
13129 if (insn != INSN_ADDIGP_TO_FP)
13130 continue;
13131 }
13132 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13133 {
13134 /* addi $fp, $gp, -256 */
13135 insn = bfd_getb32 (contents + irel->r_offset);
13136 if (insn != INSN_ADDI_GP_TO_FP)
13137 continue;
13138 }
13139 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13140 {
13141 /* movi $fp, FP_BASE */
13142 insn = bfd_getb32 (contents + irel->r_offset);
13143 if (insn != INSN_MOVI_TO_FP)
13144 continue;
13145 }
13146 else
13147 continue;
13148
13149 /* We got here because a FP_BASE instruction is found. */
13150 if (!insert_nds32_elf_blank_recalc_total
13151 (&relax_blank_list, irel->r_offset, 4))
13152 goto error_return;
13153 }
13154
13155finish:
13156 if (relax_blank_list)
13157 {
13158 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13159 relax_blank_list = NULL;
13160 }
13161 return result;
13162
13163error_return:
13164 result = FALSE;
13165 goto finish;
13166}
1c8f6a4d
KLC
13167
13168/* This is a version of bfd_generic_get_relocated_section_contents.
13169 We need this variety because relaxation will modify the dwarf
13170 infomation. When there is undefined symbol reference error mesage,
13171 linker need to dump line number where the symbol be used. However
13172 the address is be relaxed, it can not get the original dwarf contents.
13173 The variety only modify function call for reading in the section. */
13174
13175static bfd_byte *
13176nds32_elf_get_relocated_section_contents (bfd *abfd,
13177 struct bfd_link_info *link_info,
13178 struct bfd_link_order *link_order,
13179 bfd_byte *data,
13180 bfd_boolean relocatable,
13181 asymbol **symbols)
13182{
13183 bfd *input_bfd = link_order->u.indirect.section->owner;
13184 asection *input_section = link_order->u.indirect.section;
13185 long reloc_size;
13186 arelent **reloc_vector;
13187 long reloc_count;
13188
13189 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13190 if (reloc_size < 0)
13191 return NULL;
13192
13193 /* Read in the section. */
0c4bd9d9 13194 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
1c8f6a4d
KLC
13195 return NULL;
13196
13197 if (reloc_size == 0)
13198 return data;
13199
13200 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13201 if (reloc_vector == NULL)
13202 return NULL;
13203
13204 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13205 reloc_vector, symbols);
13206 if (reloc_count < 0)
13207 goto error_return;
13208
13209 if (reloc_count > 0)
13210 {
13211 arelent **parent;
13212 for (parent = reloc_vector; *parent != NULL; parent++)
13213 {
13214 char *error_message = NULL;
13215 asymbol *symbol;
13216 bfd_reloc_status_type r;
13217
13218 symbol = *(*parent)->sym_ptr_ptr;
13219 if (symbol->section && discarded_section (symbol->section))
13220 {
0930cb30 13221 bfd_vma off;
1c8f6a4d
KLC
13222 static reloc_howto_type none_howto
13223 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13224 "unused", FALSE, 0, 0, FALSE);
13225
bb294208
AM
13226 off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13227 input_section);
0930cb30
AM
13228 _bfd_clear_contents ((*parent)->howto, input_bfd,
13229 input_section, data, off);
1c8f6a4d
KLC
13230 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13231 (*parent)->addend = 0;
13232 (*parent)->howto = &none_howto;
13233 r = bfd_reloc_ok;
13234 }
13235 else
13236 r = bfd_perform_relocation (input_bfd, *parent, data,
13237 input_section,
13238 relocatable ? abfd : NULL,
13239 &error_message);
13240
13241 if (relocatable)
13242 {
13243 asection *os = input_section->output_section;
13244
13245 /* A partial link, so keep the relocs. */
13246 os->orelocation[os->reloc_count] = *parent;
13247 os->reloc_count++;
13248 }
13249
13250 if (r != bfd_reloc_ok)
13251 {
13252 switch (r)
13253 {
13254 case bfd_reloc_undefined:
1a72702b
AM
13255 (*link_info->callbacks->undefined_symbol)
13256 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13257 input_bfd, input_section, (*parent)->address, TRUE);
1c8f6a4d
KLC
13258 break;
13259 case bfd_reloc_dangerous:
13260 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13261 (*link_info->callbacks->reloc_dangerous)
13262 (link_info, error_message,
13263 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13264 break;
13265 case bfd_reloc_overflow:
1a72702b
AM
13266 (*link_info->callbacks->reloc_overflow)
13267 (link_info, NULL,
13268 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13269 (*parent)->howto->name, (*parent)->addend,
13270 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13271 break;
13272 case bfd_reloc_outofrange:
13273 /* PR ld/13730:
13274 This error can result when processing some partially
13275 complete binaries. Do not abort, but issue an error
13276 message instead. */
13277 link_info->callbacks->einfo
695344c0 13278 /* xgettext:c-format */
c1c8c1ef 13279 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
1c8f6a4d
KLC
13280 abfd, input_section, * parent);
13281 goto error_return;
13282
13283 default:
13284 abort ();
13285 break;
13286 }
13287 }
13288 }
13289 }
13290
13291 free (reloc_vector);
13292 return data;
13293
13294error_return:
13295 free (reloc_vector);
13296 return NULL;
13297}
fbaf61ad
NC
13298
13299/* Check target symbol. */
13300
13301static bfd_boolean
13302nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13303{
13304 if (!sym || !sym->name || sym->name[0] != '$')
13305 return FALSE;
13306 return TRUE;
13307}
13308
13309/* nds32 find maybe function sym. Ignore target special symbol
13310 first, and then go the general function. */
13311
13312static bfd_size_type
13313nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13314 bfd_vma *code_off)
13315{
13316 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13317 return 0;
13318
13319 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13320}
35c08157 13321\f
35c08157 13322
fbaf61ad
NC
13323/* Do TLS model conversion. */
13324
13325typedef struct relax_group_list_t
13326{
13327 Elf_Internal_Rela *relo;
13328 struct relax_group_list_t *next;
13329 struct relax_group_list_t *next_sibling;
13330 int id;
13331} relax_group_list_t;
13332
13333int
13334list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13335
13336int
13337list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13338
13339void
13340dump_chain (relax_group_list_t *pHead);
13341
13342int
13343list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13344{
13345 relax_group_list_t *pNext = pHead;
13346
13347 /* Find place. */
13348 while (pNext->next)
13349 {
13350 if (pNext->next->id > (int) pElem->r_addend)
13351 break;
13352
13353 pNext = pNext->next;
13354 }
13355
13356 /* Insert node. */
13357 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13358 if (!pNew)
13359 return FALSE;
13360
13361 relax_group_list_t *tmp = pNext->next;
13362 pNext->next = pNew;
13363
13364 pNew->id = pElem->r_addend;
13365 pNew->relo = pElem;
13366 pNew->next = tmp;
13367 pNew->next_sibling = NULL;
13368
13369 return TRUE;
13370}
13371
13372int
13373list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13374{
13375 relax_group_list_t *pNext = pNode;
13376
13377 /* Find place. */
13378 while (pNext->next_sibling)
13379 {
13380 pNext = pNext->next_sibling;
13381 }
13382
13383 /* Insert node. */
13384 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13385 if (!pNew)
13386 return FALSE;
13387
13388 relax_group_list_t *tmp = pNext->next_sibling;
13389 pNext->next_sibling = pNew;
13390
13391 pNew->id = -1;
13392 pNew->relo = pElem;
13393 pNew->next = NULL;
13394 pNew->next_sibling = tmp;
13395
13396 return TRUE;
13397}
13398
13399void
13400dump_chain (relax_group_list_t *pHead)
13401{
13402 relax_group_list_t *pNext = pHead->next;
13403 while (pNext)
13404 {
13405 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13406 relax_group_list_t *pNextSib = pNext->next_sibling;
13407 while (pNextSib)
13408 {
13409 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13410 pNextSib = pNextSib->next_sibling;
13411 }
13412 pNext = pNext->next;
13413 printf("\n");
13414 }
13415}
13416
13417/* Check R_NDS32_RELAX_GROUP of each section.
13418 There might be multiple sections in one object file. */
13419
13420int
13421elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13422{
13423 elf32_nds32_relax_group_t *relax_group_ptr =
13424 elf32_nds32_relax_group_ptr (abfd);
13425
13426 int min_id = relax_group_ptr->min_id;
13427 int max_id = relax_group_ptr->max_id;
13428
13429 Elf_Internal_Rela *rel;
13430 Elf_Internal_Rela *relend;
13431 Elf_Internal_Rela *relocs;
13432 enum elf_nds32_reloc_type rtype;
13433
13434 do
13435 {
13436 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13437 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13438 TRUE /* keep_memory */);
13439 if (relocs == NULL)
13440 break;
13441
13442 /* Check R_NDS32_RELAX_GROUP. */
13443 relend = relocs + asec->reloc_count;
13444 for (rel = relocs; rel < relend; rel++)
13445 {
13446 int id;
13447 rtype = ELF32_R_TYPE (rel->r_info);
13448 if (rtype != R_NDS32_RELAX_GROUP)
13449 continue;
13450
13451 id = rel->r_addend;
13452 if (id < min_id)
13453 min_id = id;
13454 else if (id > max_id)
13455 max_id = id;
13456 }
13457 }
13458 while (FALSE);
13459
13460 if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs))
13461 free (relocs);
13462
13463 if ((min_id != relax_group_ptr->min_id)
13464 || (max_id != relax_group_ptr->max_id))
13465 {
13466 relax_group_ptr->count = max_id - min_id + 1;
13467 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13468 relax_group_ptr->min_id = min_id;
13469 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13470 relax_group_ptr->max_id = max_id;
13471 }
13472
13473 return relax_group_ptr->count;
13474}
13475
13476/* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13477struct section_id_list_t *relax_group_section_id_list = NULL;
13478
13479struct section_id_list_t *
13480elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13481{
13482 struct section_id_list_t *result = NULL;
13483 struct section_id_list_t *lst = *lst_ptr;
13484
13485 if (NULL == lst)
13486 {
13487 result = (struct section_id_list_t *) calloc
13488 (1, sizeof (struct section_id_list_t));
13489 BFD_ASSERT (result); /* Feed me. */
13490 result->id = id;
13491 *lst_ptr = result;
13492 }
13493 else
13494 {
13495 struct section_id_list_t *cur = lst;
13496 struct section_id_list_t *prv = NULL;
13497 struct section_id_list_t *sec = NULL;
13498
13499 while (cur)
13500 {
13501 if (cur->id < id)
13502 {
13503 prv = cur;
13504 cur = cur->next;
13505 continue;
13506 }
13507
13508 if (cur->id > id)
13509 {
13510 cur = NULL; /* To insert after prv. */
13511 sec = cur; /* In case prv == NULL. */
13512 }
13513
13514 break;
13515 }
13516
13517 if (NULL == cur)
13518 {
13519 /* Insert after prv. */
13520 result = (struct section_id_list_t *) calloc
13521 (1, sizeof (struct section_id_list_t));
13522 BFD_ASSERT (result); /* Feed me. */
13523 result->id = id;
13524 if (NULL != prv)
13525 {
13526 result->next = prv->next;
13527 prv->next = result;
13528 }
13529 else
13530 {
13531 *lst_ptr = result;
13532 result->next = sec;
13533 }
13534 }
13535 }
13536
13537 return result;
13538}
13539
13540int
13541elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13542{
13543 static int next_relax_group_bias = 0;
13544
13545 elf32_nds32_relax_group_t *relax_group_ptr =
13546 elf32_nds32_relax_group_ptr (abfd);
13547
13548 bfd_boolean result = TRUE;
13549 Elf_Internal_Rela *rel;
13550 Elf_Internal_Rela *relend;
13551 Elf_Internal_Rela *relocs = NULL;
13552 enum elf_nds32_reloc_type rtype;
13553 struct section_id_list_t *node = NULL;
13554 int count = 0;
13555
13556 do
13557 {
13558 if (0 == relax_group_ptr->count)
13559 break;
13560
13561 /* Check if this section has been handled. */
13562 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13563 if (NULL == node)
13564 break; /* Hit, the section id has handled. */
13565
13566 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13567 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13568 TRUE /* keep_memory */);
13569 if (relocs == NULL)
13570 {
13571 BFD_ASSERT (0); /* feed me */
13572 break;
13573 }
13574
13575 /* Allocate group id bias for this bfd! */
13576 if (0 == relax_group_ptr->init)
13577 {
13578 relax_group_ptr->bias = next_relax_group_bias;
13579 next_relax_group_bias += relax_group_ptr->count;
13580 relax_group_ptr->init = 1;
13581 }
13582
13583 /* Reorder relax group groups. */
13584 relend = relocs + asec->reloc_count;
13585 for (rel = relocs; rel < relend; rel++)
13586 {
13587 rtype = ELF32_R_TYPE(rel->r_info);
13588 if (rtype != R_NDS32_RELAX_GROUP)
13589 continue;
13590
13591 /* Change it. */
13592 rel->r_addend += relax_group_ptr->bias;
13593 /* Debugging count. */
13594 count++;
13595 }
13596 }
13597 while (FALSE);
13598
13599 if (relocs != NULL && elf_section_data (asec)->relocs != relocs)
13600 free (relocs);
13601
13602 return result;
13603}
13604
13605int
13606nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13607 struct bfd_link_info *lnkinfo)
13608{
13609 bfd_boolean result = TRUE;
13610 Elf_Internal_Rela *irel;
13611 Elf_Internal_Rela *irelend;
13612 Elf_Internal_Rela *internal_relocs;
13613 unsigned long r_symndx;
13614 enum elf_nds32_reloc_type r_type;
13615
13616 Elf_Internal_Sym *local_syms = NULL;
13617 bfd_byte *contents = NULL;
13618
13619 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13620
13621 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13622 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13623 sym_hashes = elf_sym_hashes (inbfd);
13624 sym_hashes_end =
13625 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13626 if (!elf_bad_symtab (inbfd))
13627 sym_hashes_end -= symtab_hdr->sh_info;
13628
13629 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13630 if (bfd_link_relocatable (lnkinfo))
13631 {
13632 elf32_nds32_unify_relax_group (inbfd, insec);
13633 return result;
13634 }
13635
13636 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13637 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13638 TRUE /* keep_memory */);
13639 if (internal_relocs == NULL)
13640 goto error_return;
13641
13642 irelend = internal_relocs + insec->reloc_count;
13643 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13644 irelend, R_NDS32_RELAX_ENTRY);
13645 if (irel == irelend)
13646 goto finish;
13647
13648 /* Chain/remove groups. */
13649 for (irel = internal_relocs; irel < irelend; irel++)
13650 {
13651 r_symndx = ELF32_R_SYM (irel->r_info);
13652 r_type = ELF32_R_TYPE (irel->r_info);
13653 if (r_type != R_NDS32_RELAX_GROUP)
13654 continue;
13655
13656 /* Remove it. */
13657 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13658 /* Chain it now. */
13659 if (!list_insert (&chain, irel))
13660 goto error_return;
13661 }
13662
13663 /* Collect group relocations. */
13664 /* Presume relocations are sorted. */
13665 relax_group_list_t *pNext = chain.next;
13666 while (pNext)
13667 {
13668 for (irel = internal_relocs; irel < irelend; irel++)
13669 {
13670 if (irel->r_offset == pNext->relo->r_offset)
13671 {
13672 /* Ignore Non-TLS relocation types. */
13673 r_type = ELF32_R_TYPE (irel->r_info);
13674 if ((R_NDS32_TLS_LE_HI20 > r_type)
13675 || (R_NDS32_RELAX_ENTRY == r_type))
13676 continue;
13677
13678 if (!list_insert_sibling (pNext, irel))
13679 goto error_return;
13680 }
13681 else if (irel->r_offset > pNext->relo->r_offset)
13682 {
13683 pNext = pNext->next;
13684 if (!pNext)
13685 break;
13686
13687 bfd_vma current_offset = pNext->relo->r_offset;
13688 if (irel->r_offset > current_offset)
13689 irel = internal_relocs; /* restart from head */
13690 else
13691 --irel; /* Check current irel again. */
13692 continue;
13693 }
13694 else
13695 {
13696 /* This shouldn't be reached. */
13697 }
13698 }
13699 if (pNext)
13700 pNext = pNext->next;
13701 }
13702
13703#ifdef DUBUG_VERBOSE
13704 dump_chain(&chain);
13705#endif
13706
13707 /* Get symbol table and section content. */
13708 if (incontents)
13709 contents = incontents;
13710 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13711 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13712 goto error_return;
13713
13714 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13715
13716 /* Convert TLS model each group if necessary. */
13717 pNext = chain.next;
13718
13719 int cur_grp_id = -1;
13720 int sethi_rt = -1;
13721 int add_rt = -1;
13722 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13723
13724 tls_type = org_tls_type = eff_tls_type = 0;
13725
13726 while (pNext)
13727 {
13728 relax_group_list_t *pNextSig = pNext->next_sibling;
13729 while (pNextSig)
13730 {
13731 struct elf_link_hash_entry *h = NULL;
13732
13733 irel = pNextSig->relo;
13734 r_symndx = ELF32_R_SYM(irel->r_info);
13735 r_type = ELF32_R_TYPE(irel->r_info);
13736
13737 if (pNext->id != cur_grp_id)
13738 {
13739 cur_grp_id = pNext->id;
13740 org_tls_type = get_tls_type (r_type, NULL);
13741 if (r_symndx >= symtab_hdr->sh_info)
13742 {
13743 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13744 while (h->root.type == bfd_link_hash_indirect
13745 || h->root.type == bfd_link_hash_warning)
13746 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13747 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13748 }
13749 else
13750 {
13751 tls_type = local_got_tls_type
13752 ? local_got_tls_type[r_symndx]
13753 : GOT_NORMAL;
13754 }
13755
13756 eff_tls_type = 1 << (fls (tls_type) - 1);
13757 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13758 }
13759
13760 if (eff_tls_type != org_tls_type)
13761 {
13762 switch (org_tls_type)
13763 {
13764 /* DESC to IEGP/IE/LE. */
13765 case GOT_TLS_DESC:
13766 switch (eff_tls_type)
13767 {
13768 case GOT_TLS_IE:
13769 switch (r_type)
13770 {
13771 case R_NDS32_TLS_DESC_HI20:
13772 irel->r_info = ELF32_R_INFO(r_symndx,
13773 R_NDS32_TLS_IE_HI20);
13774 break;
13775 case R_NDS32_TLS_DESC_LO12:
13776 irel->r_info = ELF32_R_INFO(r_symndx,
13777 R_NDS32_TLS_IE_LO12);
13778 break;
13779 case R_NDS32_TLS_DESC_ADD:
13780 {
13781 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13782 add_rt = N32_RT5 (insn);
13783 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13784 bfd_putb32 (insn, contents + irel->r_offset);
13785
13786 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13787 }
13788 break;
13789 case R_NDS32_TLS_DESC_FUNC:
13790 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13791 irel->r_info = ELF32_R_INFO(r_symndx,
13792 R_NDS32_RELAX_REMOVE);
13793 break;
13794 case R_NDS32_TLS_DESC_CALL:
13795 {
13796 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13797 REG_TP);
13798 bfd_putb32 (insn, contents + irel->r_offset);
13799
13800 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13801 }
13802 break;
13803 case R_NDS32_LOADSTORE:
13804 case R_NDS32_PTR:
13805 case R_NDS32_PTR_RESOLVED:
13806 case R_NDS32_NONE:
13807 case R_NDS32_LABEL:
13808 break;
13809 default:
13810 BFD_ASSERT(0);
13811 break;
13812 }
13813 break;
13814 case GOT_TLS_IEGP:
13815 switch (r_type)
13816 {
13817 case R_NDS32_TLS_DESC_HI20:
13818 irel->r_info = ELF32_R_INFO(r_symndx,
13819 R_NDS32_TLS_IEGP_HI20);
13820 break;
13821 case R_NDS32_TLS_DESC_LO12:
13822 irel->r_info = ELF32_R_INFO(r_symndx,
13823 R_NDS32_TLS_IEGP_LO12);
13824 break;
13825 case R_NDS32_TLS_DESC_ADD:
13826 {
13827 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13828 add_rt = N32_RT5 (insn);
13829 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13830 bfd_putb32 (insn, contents + irel->r_offset);
13831
13832 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13833 }
13834 break;
13835 case R_NDS32_TLS_DESC_FUNC:
13836 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13837 irel->r_info = ELF32_R_INFO(r_symndx,
13838 R_NDS32_RELAX_REMOVE);
13839 break;
13840 case R_NDS32_TLS_DESC_CALL:
13841 {
13842 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13843 REG_TP);
13844 bfd_putb32 (insn, contents + irel->r_offset);
13845
13846 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13847 }
13848 break;
13849 case R_NDS32_LOADSTORE:
13850 case R_NDS32_PTR:
13851 case R_NDS32_PTR_RESOLVED:
13852 case R_NDS32_NONE:
13853 case R_NDS32_LABEL:
13854 break;
13855 default:
13856 BFD_ASSERT(0);
13857 break;
13858 }
13859 break;
13860 case GOT_TLS_LE:
13861 switch (r_type)
13862 {
13863 case R_NDS32_TLS_DESC_HI20:
13864 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13865 break;
13866 case R_NDS32_TLS_DESC_LO12:
13867 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13868 break;
13869 case R_NDS32_TLS_DESC_ADD:
13870 {
13871 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13872
13873 add_rt = N32_RT5 (insn);
13874 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13875 bfd_putb32 (insn, contents + irel->r_offset);
13876
13877 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13878 }
13879 break;
13880 case R_NDS32_TLS_DESC_FUNC:
13881 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13882 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13883 break;
13884 case R_NDS32_TLS_DESC_CALL:
13885 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13886 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13887 break;
13888 case R_NDS32_LOADSTORE:
13889 case R_NDS32_PTR:
13890 case R_NDS32_PTR_RESOLVED:
13891 case R_NDS32_NONE:
13892 case R_NDS32_LABEL:
13893 break;
13894 default:
13895 BFD_ASSERT(0);
13896 break;
13897 }
13898 break;
13899 default:
13900 break;
13901 }
13902 break;
13903 /* IEGP to IE/LE. */
13904 case GOT_TLS_IEGP:
13905 switch (eff_tls_type)
13906 {
13907 case GOT_TLS_IE:
13908 switch (r_type)
13909 {
13910 case R_NDS32_TLS_IEGP_HI20:
13911 irel->r_info = ELF32_R_INFO(r_symndx,
13912 R_NDS32_TLS_IE_HI20);
13913 break;
13914 case R_NDS32_TLS_IEGP_LO12:
13915 irel->r_info = ELF32_R_INFO(r_symndx,
13916 R_NDS32_TLS_IE_LO12);
13917 break;
13918 case R_NDS32_PTR_RESOLVED:
13919 {
13920 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13921
13922 add_rt = N32_RT5 (insn);
13923 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13924 bfd_putb32 (insn, contents + irel->r_offset);
13925 }
13926 break;
13927 case R_NDS32_TLS_IEGP_LW:
13928 break;
13929 case R_NDS32_LOADSTORE:
13930 case R_NDS32_PTR:
13931 case R_NDS32_NONE:
13932 case R_NDS32_LABEL:
13933 break;
13934 default:
13935 BFD_ASSERT(0);
13936 break;
13937 }
13938 break;
13939 case GOT_TLS_LE:
13940 switch (r_type)
13941 {
13942 case R_NDS32_TLS_IEGP_HI20:
13943 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13944 break;
13945 case R_NDS32_TLS_IEGP_LO12:
13946 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13947 break;
13948 case R_NDS32_TLS_IEGP_LW:
13949 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13950 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13951 break;
13952 case R_NDS32_LOADSTORE:
13953 case R_NDS32_PTR:
13954 case R_NDS32_NONE:
13955 case R_NDS32_LABEL:
13956 case R_NDS32_PTR_RESOLVED:
13957 break;
13958 default:
13959 BFD_ASSERT(0);
13960 break;
13961 }
13962 break;
13963 default:
13964 break;
13965 }
13966 break;
13967 /* IE to LE. */
13968 case GOT_TLS_IE:
13969 switch (eff_tls_type)
13970 {
13971 case GOT_TLS_LE:
13972 switch (r_type)
13973 {
13974 case R_NDS32_TLS_IE_HI20:
13975 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13976 break;
13977 case R_NDS32_TLS_IE_LO12S2:
13978 {
13979 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13980
13981 add_rt = N32_RT5 (insn);
13982 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13983 bfd_putb32 (insn, contents + irel->r_offset);
13984
13985 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13986 }
13987 break;
13988 case R_NDS32_LOADSTORE:
13989 case R_NDS32_PTR:
13990 case R_NDS32_NONE:
13991 case R_NDS32_LABEL:
13992 break;
13993 default:
13994 BFD_ASSERT(0);
13995 break;
13996 }
13997 break;
13998 default:
13999 break;
14000 }
14001 break;
14002 default:
14003 break;
14004 }
14005 }
14006 pNextSig = pNextSig->next_sibling;
14007 }
14008
14009#if 1
14010 pNext = pNext->next;
14011#else
14012 while (pNext)
14013 {
14014 if (pNext->id != cur_grp_id)
14015 break;
14016 pNext = pNext->next;
14017 }
14018#endif
14019 }
14020
14021finish:
14022 if (incontents)
14023 contents = NULL;
14024
14025 if (internal_relocs != NULL
14026 && elf_section_data (insec)->relocs != internal_relocs)
14027 free (internal_relocs);
14028
14029 if (contents != NULL
14030 && elf_section_data (insec)->this_hdr.contents != contents)
14031 free (contents);
14032
14033 if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms)
14034 free (local_syms);
14035
14036 if (chain.next)
14037 {
14038 pNext = chain.next;
14039 relax_group_list_t *pDel;
14040 while (pNext)
14041 {
14042 pDel = pNext;
14043 pNext = pNext->next;
14044 free (pDel);
14045 }
14046 }
14047
14048 return result;
14049
14050error_return:
14051 result = FALSE;
14052 goto finish;
14053}
14054
14055/* End TLS model conversion. */
14056
35c08157
KLC
14057#define ELF_ARCH bfd_arch_nds32
14058#define ELF_MACHINE_CODE EM_NDS32
14059#define ELF_MAXPAGESIZE 0x1000
07d6d2b8 14060#define ELF_TARGET_ID NDS32_ELF_DATA
35c08157 14061
6d00b590 14062#define TARGET_BIG_SYM nds32_elf32_be_vec
35c08157 14063#define TARGET_BIG_NAME "elf32-nds32be"
6d00b590 14064#define TARGET_LITTLE_SYM nds32_elf32_le_vec
35c08157
KLC
14065#define TARGET_LITTLE_NAME "elf32-nds32le"
14066
14067#define elf_info_to_howto nds32_info_to_howto
14068#define elf_info_to_howto_rel nds32_info_to_howto_rel
14069
14070#define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14071#define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14072#define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14073#define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14074#define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14075
07d6d2b8 14076#define bfd_elf32_mkobject nds32_elf_mkobject
35c08157
KLC
14077#define elf_backend_action_discarded nds32_elf_action_discarded
14078#define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14079#define elf_backend_check_relocs nds32_elf_check_relocs
14080#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14081#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14082#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14083#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14084#define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14085#define elf_backend_relocate_section nds32_elf_relocate_section
14086#define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
35c08157
KLC
14087#define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14088#define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14089#define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14090#define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14091#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14092#define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14093#define elf_backend_object_p nds32_elf_object_p
14094#define elf_backend_final_write_processing nds32_elf_final_write_processing
14095#define elf_backend_special_sections nds32_elf_special_sections
1c8f6a4d 14096#define bfd_elf32_bfd_get_relocated_section_contents \
07d6d2b8 14097 nds32_elf_get_relocated_section_contents
fbaf61ad
NC
14098#define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14099#define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
35c08157
KLC
14100
14101#define elf_backend_can_gc_sections 1
14102#define elf_backend_can_refcount 1
14103#define elf_backend_want_got_plt 1
14104#define elf_backend_plt_readonly 1
14105#define elf_backend_want_plt_sym 0
14106#define elf_backend_got_header_size 12
14107#define elf_backend_may_use_rel_p 1
14108#define elf_backend_default_use_rela_p 1
14109#define elf_backend_may_use_rela_p 1
fbaf61ad 14110#define elf_backend_dtrel_excludes_plt 0
35c08157
KLC
14111
14112#include "elf32-target.h"
14113
14114#undef ELF_MAXPAGESIZE
14115#define ELF_MAXPAGESIZE 0x2000
14116
fbaf61ad 14117#undef TARGET_BIG_SYM
6d00b590 14118#define TARGET_BIG_SYM nds32_elf32_linux_be_vec
fbaf61ad 14119#undef TARGET_BIG_NAME
35c08157 14120#define TARGET_BIG_NAME "elf32-nds32be-linux"
fbaf61ad 14121#undef TARGET_LITTLE_SYM
6d00b590 14122#define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
fbaf61ad 14123#undef TARGET_LITTLE_NAME
35c08157 14124#define TARGET_LITTLE_NAME "elf32-nds32le-linux"
fbaf61ad 14125#undef elf32_bed
35c08157
KLC
14126#define elf32_bed elf32_nds32_lin_bed
14127
14128#include "elf32-target.h"
This page took 1.015183 seconds and 4 git commands to generate.