[PATCH v3] aarch64: Emit jump slot for conditional branch to undefined symbols
[deliverable/binutils-gdb.git] / bfd / elf32-nds32.c
CommitLineData
35c08157 1/* NDS32-specific support for 32-bit ELF.
b3adc24a 2 Copyright (C) 2012-2020 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
986f0783 3687 size_t amt = sizeof (struct elf_nds32_link_hash_table);
35c08157
KLC
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
dc1e8a47 4284 keep:;
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
765cf5f6 4865 source = bfd_get_filename (input_sec->owner);
35c08157
KLC
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 {
765cf5f6 5050 printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
fbaf61ad
NC
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;
dc1e8a47 5830 handle_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
dc1e8a47 6158 check_reloc:
35c08157
KLC
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
dc1e8a47 6207 common_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 6490 s = ehtab->srelplt->output_section;
dc1e8a47 6491 get_vma:
fbaf61ad
NC
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
6b728d32
AM
6820 /* FIXME: What should be checked when linking shared libraries? */
6821 if ((ibfd->flags & DYNAMIC) != 0)
6822 return TRUE;
6823
35c08157
KLC
6824 /* TODO: Revise to use object-attributes instead. */
6825 if (!nds32_check_vec_size (ibfd))
6826 return FALSE;
6827
6828 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6829 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6830 return TRUE;
6831
6832 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6833 {
4eca0228 6834 _bfd_error_handler
38f14ab8 6835 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
35c08157
KLC
6836
6837 bfd_set_error (bfd_error_bad_value);
6838 return FALSE;
6839 }
6840
fbaf61ad
NC
6841 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6842 treat as generic one without checking and merging. */
6843 if (elf_elfheader (ibfd)->e_flags)
35c08157 6844 {
fbaf61ad
NC
6845 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6846 if (in_version == E_NDS32_ELF_VER_1_2)
35c08157 6847 {
fbaf61ad
NC
6848 _bfd_error_handler
6849 (_("%pB: warning: older version of object file encountered, "
6850 "please recompile with current tool chain"), ibfd);
35c08157 6851 }
35c08157 6852
fbaf61ad
NC
6853 /* We may need to merge V1 and V2 arch object files to V2. */
6854 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6855 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
35c08157 6856 {
fbaf61ad
NC
6857 /* Need to convert version. */
6858 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6859 == E_NDS_ARCH_STAR_RESERVED)
6860 {
6861 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6862 }
6863 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6864 == E_NDS_ARCH_STAR_V3_M
6865 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6866 == E_NDS_ARCH_STAR_V3_0)
6867 {
6868 elf_elfheader (ibfd)->e_flags =
6869 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6870 | E_NDS_ARCH_STAR_V3_0;
6871 }
6872 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6873 == E_NDS_ARCH_STAR_V0_9
6874 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6875 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6876 {
6877 elf_elfheader (obfd)->e_flags =
6878 convert_e_flags (elf_elfheader (obfd)->e_flags,
6879 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6880 }
6881 else
6882 {
6883 elf_elfheader (ibfd)->e_flags =
6884 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6885 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6886 }
35c08157
KLC
6887 }
6888
fbaf61ad
NC
6889 /* Extract some flags. */
6890 in_flags = elf_elfheader (ibfd)->e_flags
6891 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6892 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6893
6894 /* The following flags need special treatment. */
6895 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6896 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6897 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6898
6899 /* Extract some flags. */
6900 out_flags = elf_elfheader (obfd)->e_flags
6901 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6902 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6903
6904 /* The following flags need special treatment. */
6905 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6906 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6907 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6908 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6909 if (!elf_flags_init (obfd))
6910 {
6911 /* If the input is the default architecture then do not
6912 bother setting the flags for the output architecture,
6913 instead allow future merges to do this. If no future
6914 merges ever set these flags then they will retain their
6915 unitialised values, which surprise surprise, correspond
6916 to the default values. */
6917 if (bfd_get_arch_info (ibfd)->the_default)
6918 return TRUE;
6919
6920 elf_flags_init (obfd) = TRUE;
6921 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
35c08157 6922
fbaf61ad
NC
6923 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6924 && bfd_get_arch_info (obfd)->the_default)
6925 {
6926 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6927 bfd_get_mach (ibfd));
6928 }
35c08157 6929
fbaf61ad
NC
6930 return TRUE;
6931 }
35c08157 6932
fbaf61ad
NC
6933 /* Check flag compatibility. */
6934 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
35c08157 6935 {
4eca0228 6936 _bfd_error_handler
fbaf61ad 6937 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
35c08157
KLC
6938 bfd_set_error (bfd_error_bad_value);
6939 return FALSE;
6940 }
35c08157 6941
fbaf61ad
NC
6942 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6943 {
6944 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6945 {
6946 _bfd_error_handler
6947 (_("%pB: error: instruction set mismatch with previous modules"),
6948 ibfd);
6949
6950 bfd_set_error (bfd_error_bad_value);
6951 return FALSE;
6952 }
6953 }
6954
6955 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6956 and perf ext1 and DIV are mergerd to perf ext1. */
6957 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6958 {
6959 elf_elfheader (obfd)->e_flags =
6960 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6961 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6962 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6963 ? E_NDS32_HAS_EXT_INST : 0)
6964 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6965 ? E_NDS32_HAS_EXT_INST : 0)
6966 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6967 | ((in_version > out_version) ? out_version : in_version);
6968 }
6969 else
6970 {
6971 if (in_version != out_version)
6972 _bfd_error_handler
6973 /* xgettext:c-format */
6974 (_("%pB: warning: incompatible elf-versions %s and %s"),
6975 ibfd, nds32_elfver_strtab[out_version],
6976 nds32_elfver_strtab[in_version]);
35c08157 6977
fbaf61ad
NC
6978 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6979 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6980 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6981 | (in_version > out_version ? out_version : in_version);
6982 }
35c08157
KLC
6983 }
6984
6985 return TRUE;
6986}
6987
6988/* Display the flags field. */
6989
6990static bfd_boolean
6991nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6992{
6993 FILE *file = (FILE *) ptr;
6994
6995 BFD_ASSERT (abfd != NULL && ptr != NULL);
6996
6997 _bfd_elf_print_private_bfd_data (abfd, ptr);
6998
6999 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
7000
7001 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
7002 {
7003 default:
7004 case E_N1_ARCH:
7005 fprintf (file, _(": n1 instructions"));
7006 break;
7007 case E_N1H_ARCH:
7008 fprintf (file, _(": n1h instructions"));
7009 break;
7010 }
7011
7012 fputc ('\n', file);
7013
7014 return TRUE;
7015}
7016
7017static unsigned int
7018nds32_elf_action_discarded (asection *sec)
7019{
7020
7021 if (strncmp
7022 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7023 return 0;
7024
7025 return _bfd_elf_default_action_discarded (sec);
7026}
7027
7028static asection *
7029nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7030 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7031 Elf_Internal_Sym *sym)
7032{
7033 if (h != NULL)
7034 switch (ELF32_R_TYPE (rel->r_info))
7035 {
7036 case R_NDS32_GNU_VTINHERIT:
7037 case R_NDS32_GNU_VTENTRY:
7038 case R_NDS32_RELA_GNU_VTINHERIT:
7039 case R_NDS32_RELA_GNU_VTENTRY:
7040 return NULL;
7041 }
7042
7043 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7044}
7045
fbaf61ad
NC
7046static enum elf_nds32_tls_type
7047get_tls_type (enum elf_nds32_reloc_type r_type,
7048 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7049{
7050 enum elf_nds32_tls_type tls_type;
7051
7052 switch (r_type)
7053 {
7054 case R_NDS32_TLS_LE_HI20:
7055 case R_NDS32_TLS_LE_LO12:
7056 tls_type = GOT_TLS_LE;
7057 break;
7058 case R_NDS32_TLS_IE_HI20:
7059 case R_NDS32_TLS_IE_LO12S2:
7060 case R_NDS32_TLS_IE_LO12:
7061 tls_type = GOT_TLS_IE;
7062 break;
7063 case R_NDS32_TLS_IEGP_HI20:
7064 case R_NDS32_TLS_IEGP_LO12:
7065 case R_NDS32_TLS_IEGP_LO12S2:
7066 tls_type = GOT_TLS_IEGP;
7067 break;
7068 case R_NDS32_TLS_DESC_HI20:
7069 case R_NDS32_TLS_DESC_LO12:
7070 case R_NDS32_TLS_DESC_ADD:
7071 case R_NDS32_TLS_DESC_FUNC:
7072 case R_NDS32_TLS_DESC_CALL:
7073 tls_type = GOT_TLS_DESC;
7074 break;
7075 default:
7076 tls_type = GOT_NORMAL;
7077 break;
7078 }
7079
7080 return tls_type;
7081}
7082
7083/* Ensure that we have allocated bookkeeping structures for ABFD's local
7084 symbols. */
7085
7086static bfd_boolean
7087elf32_nds32_allocate_local_sym_info (bfd *abfd)
7088{
7089 if (elf_local_got_refcounts (abfd) == NULL)
7090 {
7091 bfd_size_type num_syms;
7092 bfd_size_type size;
7093 char *data;
7094
7095 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7096 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7097 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7098 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7099 + sizeof (bfd_vma) + sizeof (int)
7100 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7101 data = bfd_zalloc (abfd, size);
7102 if (data == NULL)
7103 return FALSE;
7104
7105 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7106 data += num_syms * sizeof (bfd_signed_vma);
7107
7108 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7109 data += num_syms * sizeof (char);
7110
7111 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7112 data += num_syms * sizeof (bfd_vma);
7113
7114 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7115 data += num_syms * sizeof (int);
7116 }
7117
7118 return TRUE;
7119}
7120
35c08157
KLC
7121/* Look through the relocs for a section during the first phase.
7122 Since we don't do .gots or .plts, we just need to consider the
7123 virtual table relocs for gc. */
7124
7125static bfd_boolean
7126nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7127 asection *sec, const Elf_Internal_Rela *relocs)
7128{
7129 Elf_Internal_Shdr *symtab_hdr;
7130 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7131 const Elf_Internal_Rela *rel;
7132 const Elf_Internal_Rela *rel_end;
fbaf61ad 7133 struct elf_link_hash_table *ehtab;
35c08157
KLC
7134 struct elf_nds32_link_hash_table *htab;
7135 bfd *dynobj;
7136 asection *sreloc = NULL;
7137
fbaf61ad 7138 /* No need for relocation if relocatable already. */
0e1862bb 7139 if (bfd_link_relocatable (info))
fbaf61ad
NC
7140 {
7141 elf32_nds32_check_relax_group (abfd, sec);
7142 return TRUE;
7143 }
35c08157 7144
65281396
AM
7145 /* Don't do anything special with non-loaded, non-alloced sections.
7146 In particular, any relocs in such sections should not affect GOT
7147 and PLT reference counting (ie. we don't allow them to create GOT
7148 or PLT entries), there's no possibility or desire to optimize TLS
7149 relocs, and there's not much point in propagating relocs to shared
7150 libs that the dynamic linker won't relocate. */
7151 if ((sec->flags & SEC_ALLOC) == 0)
7152 return TRUE;
7153
35c08157
KLC
7154 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7155 sym_hashes = elf_sym_hashes (abfd);
7156 sym_hashes_end =
7157 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7158 if (!elf_bad_symtab (abfd))
7159 sym_hashes_end -= symtab_hdr->sh_info;
7160
fbaf61ad 7161 ehtab = elf_hash_table (info);
35c08157
KLC
7162 htab = nds32_elf_hash_table (info);
7163 dynobj = htab->root.dynobj;
7164
7165 rel_end = relocs + sec->reloc_count;
7166 for (rel = relocs; rel < rel_end; rel++)
7167 {
7168 enum elf_nds32_reloc_type r_type;
7169 struct elf_link_hash_entry *h;
7170 unsigned long r_symndx;
fbaf61ad 7171 enum elf_nds32_tls_type tls_type, old_tls_type;
35c08157
KLC
7172
7173 r_symndx = ELF32_R_SYM (rel->r_info);
7174 r_type = ELF32_R_TYPE (rel->r_info);
7175 if (r_symndx < symtab_hdr->sh_info)
7176 h = NULL;
7177 else
7178 {
7179 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7180 while (h->root.type == bfd_link_hash_indirect
7181 || h->root.type == bfd_link_hash_warning)
7182 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7183 }
7184
fbaf61ad
NC
7185 /* Create .got section if necessary.
7186 Some relocs require a global offset table. We create
7187 got section here, since these relocation need a got section
7188 and if it is not created yet. */
7189 if (ehtab->sgot == NULL)
35c08157
KLC
7190 {
7191 switch (r_type)
7192 {
7193 case R_NDS32_GOT_HI20:
7194 case R_NDS32_GOT_LO12:
7195 case R_NDS32_GOT_LO15:
7196 case R_NDS32_GOT_LO19:
7197 case R_NDS32_GOT17S2_RELA:
7198 case R_NDS32_GOT15S2_RELA:
7199 case R_NDS32_GOTOFF:
7200 case R_NDS32_GOTOFF_HI20:
7201 case R_NDS32_GOTOFF_LO12:
7202 case R_NDS32_GOTOFF_LO15:
7203 case R_NDS32_GOTOFF_LO19:
7204 case R_NDS32_GOTPC20:
7205 case R_NDS32_GOTPC_HI20:
7206 case R_NDS32_GOTPC_LO12:
7207 case R_NDS32_GOT20:
1c8f6a4d 7208 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7209 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7210 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7211 case R_NDS32_TLS_IEGP_HI20:
7212 case R_NDS32_TLS_IEGP_LO12:
7213 case R_NDS32_TLS_IEGP_LO12S2:
7214 case R_NDS32_TLS_DESC_HI20:
7215 case R_NDS32_TLS_DESC_LO12:
35c08157
KLC
7216 if (dynobj == NULL)
7217 htab->root.dynobj = dynobj = abfd;
fbaf61ad 7218 if (!create_got_section (dynobj, info))
35c08157
KLC
7219 return FALSE;
7220 break;
7221
7222 default:
7223 break;
7224 }
7225 }
7226
fbaf61ad 7227 /* Check relocation type. */
35c08157
KLC
7228 switch ((int) r_type)
7229 {
7230 case R_NDS32_GOT_HI20:
7231 case R_NDS32_GOT_LO12:
7232 case R_NDS32_GOT_LO15:
7233 case R_NDS32_GOT_LO19:
7234 case R_NDS32_GOT20:
fbaf61ad
NC
7235 case R_NDS32_TLS_LE_HI20:
7236 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 7237 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7238 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7239 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7240 case R_NDS32_TLS_IEGP_HI20:
7241 case R_NDS32_TLS_IEGP_LO12:
7242 case R_NDS32_TLS_IEGP_LO12S2:
7243 case R_NDS32_TLS_DESC_HI20:
7244 case R_NDS32_TLS_DESC_LO12:
7245 tls_type = get_tls_type (r_type, h);
7246 if (h)
1c8f6a4d 7247 {
fbaf61ad
NC
7248 if (tls_type != GOT_TLS_LE)
7249 h->got.refcount += 1;
1c8f6a4d 7250 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
1c8f6a4d 7251 }
35c08157
KLC
7252 else
7253 {
fbaf61ad
NC
7254 /* This is a global offset table entry for a local symbol. */
7255 if (!elf32_nds32_allocate_local_sym_info (abfd))
7256 return FALSE;
35c08157 7257
fbaf61ad
NC
7258 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7259 if (tls_type != GOT_TLS_LE)
7260 elf_local_got_refcounts (abfd)[r_symndx] += 1;
1c8f6a4d 7261 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
35c08157 7262 }
35c08157 7263
fbaf61ad 7264 /* We would already have issued an error message if there
1c8f6a4d
KLC
7265 is a TLS/non-TLS mismatch, based on the symbol
7266 type. So just combine any TLS types needed. */
7267 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7268 && tls_type != GOT_NORMAL)
7269 tls_type |= old_tls_type;
7270
fbaf61ad
NC
7271 /* DESC to IE/IEGP if link to executable. */
7272 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7273 && (bfd_link_executable (info)))
7274 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7275
1c8f6a4d
KLC
7276 if (old_tls_type != tls_type)
7277 {
7278 if (h != NULL)
7279 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7280 else
7281 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7282 }
7283 break;
7284 case R_NDS32_9_PLTREL:
35c08157
KLC
7285 case R_NDS32_25_PLTREL:
7286 case R_NDS32_PLTREL_HI20:
7287 case R_NDS32_PLTREL_LO12:
7288 case R_NDS32_PLT_GOTREL_HI20:
7289 case R_NDS32_PLT_GOTREL_LO12:
7290 case R_NDS32_PLT_GOTREL_LO15:
7291 case R_NDS32_PLT_GOTREL_LO19:
7292 case R_NDS32_PLT_GOTREL_LO20:
7293
7294 /* This symbol requires a procedure linkage table entry. We
7295 actually build the entry in adjust_dynamic_symbol,
7296 because this might be a case of linking PIC code without
7297 linking in any dynamic objects, in which case we don't
7298 need to generate a procedure linkage table after all. */
7299
7300 /* If this is a local symbol, we resolve it directly without
7301 creating a procedure linkage table entry. */
7302 if (h == NULL)
7303 continue;
7304
fbaf61ad
NC
7305 if (h->forced_local
7306 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
7307 break;
7308
1c8f6a4d 7309 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
35c08157
KLC
7310 h->needs_plt = 1;
7311 h->plt.refcount += 1;
7312 break;
7313
7314 case R_NDS32_16_RELA:
7315 case R_NDS32_20_RELA:
7316 case R_NDS32_5_RELA:
7317 case R_NDS32_32_RELA:
7318 case R_NDS32_HI20_RELA:
7319 case R_NDS32_LO12S3_RELA:
7320 case R_NDS32_LO12S2_RELA:
7321 case R_NDS32_LO12S2_DP_RELA:
7322 case R_NDS32_LO12S2_SP_RELA:
7323 case R_NDS32_LO12S1_RELA:
7324 case R_NDS32_LO12S0_RELA:
7325 case R_NDS32_LO12S0_ORI_RELA:
7326 case R_NDS32_SDA16S3_RELA:
7327 case R_NDS32_SDA17S2_RELA:
7328 case R_NDS32_SDA18S1_RELA:
7329 case R_NDS32_SDA19S0_RELA:
7330 case R_NDS32_SDA15S3_RELA:
7331 case R_NDS32_SDA15S2_RELA:
7332 case R_NDS32_SDA12S2_DP_RELA:
7333 case R_NDS32_SDA12S2_SP_RELA:
7334 case R_NDS32_SDA15S1_RELA:
7335 case R_NDS32_SDA15S0_RELA:
7336 case R_NDS32_SDA_FP7U2_RELA:
7337 case R_NDS32_15_PCREL_RELA:
7338 case R_NDS32_17_PCREL_RELA:
7339 case R_NDS32_25_PCREL_RELA:
7340
0e1862bb 7341 if (h != NULL && !bfd_link_pic (info))
35c08157
KLC
7342 {
7343 h->non_got_ref = 1;
7344 h->plt.refcount += 1;
7345 }
7346
7347 /* If we are creating a shared library, and this is a reloc against
7348 a global symbol, or a non PC relative reloc against a local
7349 symbol, then we need to copy the reloc into the shared library.
7350 However, if we are linking with -Bsymbolic, we do not need to
7351 copy a reloc against a global symbol which is defined in an
7352 object we are including in the link (i.e., DEF_REGULAR is set).
7353 At this point we have not seen all the input files, so it is
7354 possible that DEF_REGULAR is not set now but will be set later
7355 (it is never cleared). We account for that possibility below by
7356 storing information in the dyn_relocs field of the hash table
7357 entry. A similar situation occurs when creating shared libraries
7358 and symbol visibility changes render the symbol local.
7359
7360 If on the other hand, we are creating an executable, we may need
7361 to keep relocations for symbols satisfied by a dynamic library
7362 if we manage to avoid copy relocs for the symbol. */
0e1862bb 7363 if ((bfd_link_pic (info)
35c08157
KLC
7364 && (sec->flags & SEC_ALLOC) != 0
7365 && ((r_type != R_NDS32_25_PCREL_RELA
7366 && r_type != R_NDS32_15_PCREL_RELA
7367 && r_type != R_NDS32_17_PCREL_RELA
7368 && !(r_type == R_NDS32_32_RELA
7369 && strcmp (sec->name, ".eh_frame") == 0))
7370 || (h != NULL
7371 && (!info->symbolic
7372 || h->root.type == bfd_link_hash_defweak
7373 || !h->def_regular))))
0e1862bb 7374 || (!bfd_link_pic (info)
35c08157
KLC
7375 && (sec->flags & SEC_ALLOC) != 0
7376 && h != NULL
7377 && (h->root.type == bfd_link_hash_defweak
7378 || !h->def_regular)))
7379 {
3bf083ed
AM
7380 struct elf_dyn_relocs *p;
7381 struct elf_dyn_relocs **head;
35c08157
KLC
7382
7383 if (dynobj == NULL)
7384 htab->root.dynobj = dynobj = abfd;
7385
7386 /* When creating a shared object, we must copy these
7387 relocs into the output file. We create a reloc
7388 section in dynobj and make room for the reloc. */
7389 if (sreloc == NULL)
7390 {
7391 const char *name;
7392
7393 name = bfd_elf_string_from_elf_section
7394 (abfd, elf_elfheader (abfd)->e_shstrndx,
7395 elf_section_data (sec)->rela.hdr->sh_name);
7396 if (name == NULL)
7397 return FALSE;
7398
7399 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
fd361982 7400 && strcmp (bfd_section_name (sec),
35c08157
KLC
7401 name + 5) == 0);
7402
7403 sreloc = bfd_get_section_by_name (dynobj, name);
7404 if (sreloc == NULL)
7405 {
7406 flagword flags;
7407
7408 sreloc = bfd_make_section (dynobj, name);
7409 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7410 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7411 if ((sec->flags & SEC_ALLOC) != 0)
7412 flags |= SEC_ALLOC | SEC_LOAD;
7413 if (sreloc == NULL
fd361982
AM
7414 || !bfd_set_section_flags (sreloc, flags)
7415 || !bfd_set_section_alignment (sreloc, 2))
35c08157
KLC
7416 return FALSE;
7417
7418 elf_section_type (sreloc) = SHT_RELA;
7419 }
7420 elf_section_data (sec)->sreloc = sreloc;
7421 }
7422
7423 /* If this is a global symbol, we count the number of
7424 relocations we need for this symbol. */
7425 if (h != NULL)
7426 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
7427 else
7428 {
7429 asection *s;
61034b0b 7430 void *vpp;
35c08157
KLC
7431
7432 Elf_Internal_Sym *isym;
7433 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7434 if (isym == NULL)
7435 return FALSE;
7436
7437 /* Track dynamic relocs needed for local syms too. */
7438 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7439 if (s == NULL)
7440 return FALSE;
7441
61034b0b 7442 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 7443 head = (struct elf_dyn_relocs **) vpp;
35c08157
KLC
7444 }
7445
7446 p = *head;
7447 if (p == NULL || p->sec != sec)
7448 {
986f0783 7449 size_t amt = sizeof (*p);
3bf083ed 7450 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
35c08157
KLC
7451 if (p == NULL)
7452 return FALSE;
7453 p->next = *head;
7454 *head = p;
7455 p->sec = sec;
7456 p->count = 0;
7457 p->pc_count = 0;
7458 }
7459
7460 p->count += 1;
fbaf61ad
NC
7461
7462 /* Since eh_frame is readonly, R_NDS32_32_RELA
7463 reloc for eh_frame will cause shared library has
7464 TEXTREL entry in the dynamic section. This lead glibc
7465 testsuites to failure (bug-13092) and cause kernel fail
7466 (bug-11819). I think the best solution is to replace
7467 absolute reloc with pc relative reloc in the eh_frame.
7468 To do that, we need to support the following issues:
7469
7470 === For GCC ===
7471 * gcc/config/nds32/nds32.h: Define
7472 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7473 and DW_EH_PE_sdata4 into DWARF exception header when
7474 option have '-fpic'.
7475
7476 === For binutils ===
7477 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7478 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7479 may break our nds DIFF mechanism, therefore, we
7480 must disable all linker relaxations to ensure
7481 correctness.
7482 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7483 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7484 do the necessary modification.
7485
7486 Unfortunately, it still have some problems for nds32
7487 to support pc relative reloc in the eh_frame. So I use
7488 another solution to fix this issue.
7489
7490 However, I find that ld always emit TEXTREL marker for
7491 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7492 correspond to R_NDS32_32_RELA for .eh_frame section.
7493 It means that we always reserve redundant entries of rel.dyn
7494 for these relocs which actually do nothing in dynamic linker.
7495
7496 Therefore, we regard these relocs as pc relative relocs
7497 here and increase the pc_count. */
35c08157
KLC
7498 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7499 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
fbaf61ad
NC
7500 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7501 || (r_type == R_NDS32_32_RELA
7502 && strcmp (sec->name, ".eh_frame") == 0))
35c08157
KLC
7503 p->pc_count += 1;
7504 }
7505 break;
7506
7507 /* This relocation describes the C++ object vtable hierarchy.
7508 Reconstruct it for later use during GC. */
7509 case R_NDS32_RELA_GNU_VTINHERIT:
7510 case R_NDS32_GNU_VTINHERIT:
7511 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7512 return FALSE;
7513 break;
7514
7515 /* This relocation describes which C++ vtable entries are actually
7516 used. Record for later use during GC. */
7517 case R_NDS32_GNU_VTENTRY:
7518 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7519 return FALSE;
7520 break;
7521 case R_NDS32_RELA_GNU_VTENTRY:
7522 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7523 return FALSE;
7524 break;
7525 }
7526 }
7527
7528 return TRUE;
7529}
7530
7531/* Write VAL in uleb128 format to P, returning a pointer to the
7532 following byte.
7533 This code is copied from elf-attr.c. */
7534
7535static bfd_byte *
7536write_uleb128 (bfd_byte *p, unsigned int val)
7537{
7538 bfd_byte c;
7539 do
7540 {
7541 c = val & 0x7f;
7542 val >>= 7;
7543 if (val)
7544 c |= 0x80;
7545 *(p++) = c;
7546 }
7547 while (val);
7548 return p;
7549}
7550
7551static bfd_signed_vma
7552calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
fbaf61ad 7553 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
35c08157
KLC
7554{
7555 bfd_signed_vma foff;
7556 bfd_vma symval, addend;
7557 asection *sym_sec;
7558
7559 /* Get the value of the symbol referred to by the reloc. */
7560 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7561 {
7562 Elf_Internal_Sym *isym;
7563
7564 /* A local symbol. */
7565 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7566
7567 if (isym->st_shndx == SHN_UNDEF)
7568 sym_sec = bfd_und_section_ptr;
7569 else if (isym->st_shndx == SHN_ABS)
7570 sym_sec = bfd_abs_section_ptr;
7571 else if (isym->st_shndx == SHN_COMMON)
7572 sym_sec = bfd_com_section_ptr;
7573 else
7574 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7575 symval = isym->st_value + sym_sec->output_section->vma
7576 + sym_sec->output_offset;
7577 }
7578 else
7579 {
7580 unsigned long indx;
7581 struct elf_link_hash_entry *h;
35c08157
KLC
7582
7583 /* An external symbol. */
7584 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7585 h = elf_sym_hashes (abfd)[indx];
7586 BFD_ASSERT (h != NULL);
7587
7588 if (h->root.type != bfd_link_hash_defined
7589 && h->root.type != bfd_link_hash_defweak)
7590 /* This appears to be a reference to an undefined
7591 symbol. Just ignore it--it will be caught by the
7592 regular reloc processing. */
7593 return 0;
35c08157
KLC
7594
7595 if (h->root.u.def.section->flags & SEC_MERGE)
7596 {
7597 sym_sec = h->root.u.def.section;
7598 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7599 elf_section_data (sym_sec)->sec_info,
7600 h->root.u.def.value);
7601 symval = symval + sym_sec->output_section->vma
7602 + sym_sec->output_offset;
7603 }
7604 else
7605 symval = (h->root.u.def.value
7606 + h->root.u.def.section->output_section->vma
7607 + h->root.u.def.section->output_offset);
7608 }
7609
7610 addend = irel->r_addend;
7611
7612 foff = (symval + addend
7613 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7614 return foff;
7615}
35c08157 7616\f
fbaf61ad 7617
35c08157
KLC
7618/* Convert a 32-bit instruction to 16-bit one.
7619 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7620 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7621 type of INSN16. Return 1 if successful. */
7622
7623static int
7624nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7625 int *pinsn_type)
7626{
7627 uint16_t insn16 = 0;
6cae483a 7628 int insn_type = 0;
35c08157
KLC
7629 unsigned long mach = bfd_get_mach (abfd);
7630
7631 if (N32_SH5 (insn) != 0)
7632 return 0;
7633
7634 switch (N32_SUB5 (insn))
7635 {
7636 case N32_ALU1_ADD_SLLI:
7637 case N32_ALU1_ADD_SRLI:
7638 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7639 {
7640 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7641 N32_RB5 (insn));
7642 insn_type = NDS32_INSN_ADD333;
7643 }
7644 else if (N32_IS_RT4 (insn))
7645 {
7646 if (N32_RT5 (insn) == N32_RA5 (insn))
7647 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7648 else if (N32_RT5 (insn) == N32_RB5 (insn))
7649 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7650 insn_type = NDS32_INSN_ADD45;
7651 }
7652 break;
7653
7654 case N32_ALU1_SUB_SLLI:
7655 case N32_ALU1_SUB_SRLI:
7656 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7657 {
7658 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7659 N32_RB5 (insn));
7660 insn_type = NDS32_INSN_SUB333;
7661 }
7662 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7663 {
7664 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7665 insn_type = NDS32_INSN_SUB45;
7666 }
7667 break;
7668
7669 case N32_ALU1_AND_SLLI:
7670 case N32_ALU1_AND_SRLI:
7671 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7672 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7673 && N32_IS_RB3 (insn))
7674 {
7675 if (N32_RT5 (insn) == N32_RA5 (insn))
7676 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7677 else if (N32_RT5 (insn) == N32_RB5 (insn))
7678 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7679 if (insn16)
7680 insn_type = NDS32_INSN_AND33;
7681 }
7682 break;
7683
7684 case N32_ALU1_XOR_SLLI:
7685 case N32_ALU1_XOR_SRLI:
7686 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7687 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7688 && N32_IS_RB3 (insn))
7689 {
7690 if (N32_RT5 (insn) == N32_RA5 (insn))
7691 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7692 else if (N32_RT5 (insn) == N32_RB5 (insn))
7693 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7694 if (insn16)
7695 insn_type = NDS32_INSN_XOR33;
7696 }
7697 break;
7698
7699 case N32_ALU1_OR_SLLI:
7700 case N32_ALU1_OR_SRLI:
7701 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7702 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7703 && N32_IS_RB3 (insn))
7704 {
7705 if (N32_RT5 (insn) == N32_RA5 (insn))
7706 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7707 else if (N32_RT5 (insn) == N32_RB5 (insn))
7708 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7709 if (insn16)
7710 insn_type = NDS32_INSN_OR33;
7711 }
7712 break;
7713 case N32_ALU1_NOR:
7714 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7715 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7716 && N32_RA5 (insn) == N32_RB5 (insn))
7717 {
7718 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7719 insn_type = NDS32_INSN_NOT33;
7720 }
7721 break;
7722 case N32_ALU1_SRAI:
7723 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7724 {
7725 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7726 insn_type = NDS32_INSN_SRAI45;
7727 }
7728 break;
7729
7730 case N32_ALU1_SRLI:
7731 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7732 {
7733 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7734 insn_type = NDS32_INSN_SRLI45;
7735 }
7736 break;
7737
7738 case N32_ALU1_SLLI:
7739 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7740 {
7741 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7742 N32_UB5 (insn));
7743 insn_type = NDS32_INSN_SLLI333;
7744 }
7745 break;
7746
7747 case N32_ALU1_ZEH:
7748 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7749 {
7750 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7751 insn_type = NDS32_INSN_ZEH33;
7752 }
7753 break;
7754
7755 case N32_ALU1_SEB:
7756 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7757 {
7758 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7759 insn_type = NDS32_INSN_SEB33;
7760 }
7761 break;
7762
7763 case N32_ALU1_SEH:
7764 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7765 {
7766 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7767 insn_type = NDS32_INSN_SEH33;
7768 }
7769 break;
7770
7771 case N32_ALU1_SLT:
7772 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7773 {
7774 /* Implicit r15. */
7775 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7776 insn_type = NDS32_INSN_SLT45;
7777 }
7778 break;
7779
7780 case N32_ALU1_SLTS:
7781 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7782 {
7783 /* Implicit r15. */
7784 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7785 insn_type = NDS32_INSN_SLTS45;
7786 }
7787 break;
7788 }
7789
7790 if ((insn16 & 0x8000) == 0)
7791 return 0;
7792
7793 if (pinsn16)
7794 *pinsn16 = insn16;
7795 if (pinsn_type)
7796 *pinsn_type = insn_type;
7797 return 1;
7798}
7799
7800static int
7801nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7802 int *pinsn_type)
7803{
7804 uint16_t insn16 = 0;
7805 int insn_type;
7806 unsigned long mach = bfd_get_mach (abfd);
7807
7808 /* TODO: bset, bclr, btgl, btst. */
7809 if (__GF (insn, 6, 4) != 0)
7810 return 0;
7811
7812 switch (N32_IMMU (insn, 6))
7813 {
7814 case N32_ALU2_MUL:
7815 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7816 && N32_IS_RB3 (insn))
7817 {
7818 if (N32_RT5 (insn) == N32_RA5 (insn))
7819 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7820 else if (N32_RT5 (insn) == N32_RB5 (insn))
7821 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7822 if (insn16)
7823 insn_type = NDS32_INSN_MUL33;
7824 }
7825 }
7826
7827 if ((insn16 & 0x8000) == 0)
7828 return 0;
7829
7830 if (pinsn16)
7831 *pinsn16 = insn16;
7832 if (pinsn_type)
7833 *pinsn_type = insn_type;
7834 return 1;
7835}
7836
7837int
7838nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7839 int *pinsn_type)
7840{
7841 int op6;
7842 uint16_t insn16 = 0;
1624c9ca 7843 int insn_type = 0;
35c08157
KLC
7844 unsigned long mach = bfd_get_mach (abfd);
7845
7846 /* Decode 32-bit instruction. */
7847 if (insn & 0x80000000)
7848 {
7849 /* Not 32-bit insn. */
7850 return 0;
7851 }
7852
7853 op6 = N32_OP6 (insn);
7854
7855 /* Convert it to 16-bit instruction. */
7856 switch (op6)
7857 {
7858 case N32_OP6_MOVI:
7859 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7860 {
7861 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7862 insn_type = NDS32_INSN_MOVI55;
7863 }
7864 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7865 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7866 {
7867 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7868 N32_IMM20S (insn) - 16);
7869 insn_type = NDS32_INSN_MOVPI45;
7870 }
7871 break;
7872
7873 case N32_OP6_ADDI:
7874 if (N32_IMM15S (insn) == 0)
7875 {
7876 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7877 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7878 if (mach <= MACH_V2
7879 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7880 {
7881 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7882 insn_type = NDS32_INSN_MOV55;
7883 }
7884 }
7885 else if (N32_IMM15S (insn) > 0)
7886 {
7887 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7888 {
7889 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7890 N32_IMM15S (insn));
7891 insn_type = NDS32_INSN_ADDI333;
7892 }
7893 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7894 && N32_IMM15S (insn) < 32)
7895 {
7896 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7897 insn_type = NDS32_INSN_ADDI45;
7898 }
7899 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7900 && N32_RT5 (insn) == N32_RA5 (insn)
7901 && N32_IMM15S (insn) < 512)
7902 {
7903 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7904 insn_type = NDS32_INSN_ADDI10_SP;
7905 }
7906 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7907 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7908 && (N32_IMM15S (insn) % 4 == 0))
7909 {
7910 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7911 N32_IMM15S (insn) >> 2);
7912 insn_type = NDS32_INSN_ADDRI36_SP;
7913 }
7914 }
7915 else
7916 {
7917 /* Less than 0. */
7918 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7919 {
7920 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7921 0 - N32_IMM15S (insn));
7922 insn_type = NDS32_INSN_SUBI333;
7923 }
7924 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7925 && N32_IMM15S (insn) > -32)
7926 {
1c8f6a4d
KLC
7927 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7928 0 - N32_IMM15S (insn));
35c08157
KLC
7929 insn_type = NDS32_INSN_SUBI45;
7930 }
7931 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7932 && N32_RT5 (insn) == N32_RA5 (insn)
7933 && N32_IMM15S (insn) >= -512)
7934 {
7935 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7936 insn_type = NDS32_INSN_ADDI10_SP;
7937 }
7938 }
7939 break;
7940
7941 case N32_OP6_ORI:
7942 if (N32_IMM15S (insn) == 0)
7943 {
7944 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7945 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7946 if (mach <= MACH_V2
7947 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7948 {
7949 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7950 insn_type = NDS32_INSN_MOV55;
7951 }
7952 }
7953 break;
7954
7955 case N32_OP6_SUBRI:
7956 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7957 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7958 {
7959 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7960 insn_type = NDS32_INSN_NEG33;
7961 }
7962 break;
7963
7964 case N32_OP6_ANDI:
7965 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7966 {
7967 if (N32_IMM15U (insn) == 1)
7968 {
7969 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7970 insn_type = NDS32_INSN_XLSB33;
7971 }
7972 else if (N32_IMM15U (insn) == 0x7ff)
7973 {
7974 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7975 insn_type = NDS32_INSN_X11B33;
7976 }
7977 else if (N32_IMM15U (insn) == 0xff)
7978 {
7979 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7980 insn_type = NDS32_INSN_ZEB33;
7981 }
7982 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7983 && N32_IMM15U (insn) < 256)
7984 {
7985 int imm15u = N32_IMM15U (insn);
7986
7987 if (__builtin_popcount (imm15u) == 1)
7988 {
7989 /* BMSKI33 */
7990 int imm3u = __builtin_ctz (imm15u);
7991
7992 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7993 insn_type = NDS32_INSN_BMSKI33;
7994 }
7995 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7996 {
7997 /* FEXTI33 */
7998 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7999
8000 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
8001 insn_type = NDS32_INSN_FEXTI33;
8002 }
8003 }
8004 }
8005 break;
8006
8007 case N32_OP6_SLTI:
8008 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8009 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8010 {
8011 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8012 insn_type = NDS32_INSN_SLTI45;
8013 }
8014 break;
8015
8016 case N32_OP6_SLTSI:
8017 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8018 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8019 {
8020 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8021 insn_type = NDS32_INSN_SLTSI45;
8022 }
8023 break;
8024
8025 case N32_OP6_LWI:
8026 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8027 {
8028 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8029 insn_type = NDS32_INSN_LWI450;
8030 }
8031 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8032 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8033 {
8034 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8035 N32_IMM15S (insn));
8036 insn_type = NDS32_INSN_LWI333;
8037 }
8038 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8039 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8040 {
8041 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8042 insn_type = NDS32_INSN_LWI37;
8043 }
8044 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8045 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8046 {
8047 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8048 insn_type = NDS32_INSN_LWI37_SP;
8049 }
8050 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8051 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8052 {
1c8f6a4d
KLC
8053 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8054 N32_IMM15S (insn) + 32);
35c08157
KLC
8055 insn_type = NDS32_INSN_LWI45_FE;
8056 }
8057 break;
8058
8059 case N32_OP6_SWI:
8060 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8061 {
8062 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8063 insn_type = NDS32_INSN_SWI450;
8064 }
8065 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8066 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8067 {
1c8f6a4d
KLC
8068 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8069 N32_IMM15S (insn));
35c08157
KLC
8070 insn_type = NDS32_INSN_SWI333;
8071 }
8072 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8073 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8074 {
8075 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8076 insn_type = NDS32_INSN_SWI37;
8077 }
8078 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8079 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8080 {
8081 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8082 insn_type = NDS32_INSN_SWI37_SP;
8083 }
8084 break;
8085
8086 case N32_OP6_LWI_BI:
8087 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8088 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8089 {
8090 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8091 N32_IMM15S (insn));
8092 insn_type = NDS32_INSN_LWI333_BI;
8093 }
8094 break;
8095
8096 case N32_OP6_SWI_BI:
8097 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8098 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8099 {
8100 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8101 N32_IMM15S (insn));
8102 insn_type = NDS32_INSN_SWI333_BI;
8103 }
8104 break;
8105
8106 case N32_OP6_LHI:
8107 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8108 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8109 {
8110 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8111 N32_IMM15S (insn));
8112 insn_type = NDS32_INSN_LHI333;
8113 }
8114 break;
8115
8116 case N32_OP6_SHI:
8117 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8118 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8119 {
8120 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8121 N32_IMM15S (insn));
8122 insn_type = NDS32_INSN_SHI333;
8123 }
8124 break;
8125
8126 case N32_OP6_LBI:
8127 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8128 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8129 {
8130 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8131 N32_IMM15S (insn));
8132 insn_type = NDS32_INSN_LBI333;
8133 }
8134 break;
8135
8136 case N32_OP6_SBI:
8137 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8138 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8139 {
8140 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8141 N32_IMM15S (insn));
8142 insn_type = NDS32_INSN_SBI333;
8143 }
8144 break;
8145
8146 case N32_OP6_ALU1:
8147 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8148
8149 case N32_OP6_ALU2:
8150 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8151
8152 case N32_OP6_BR1:
8153 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8154 goto done;
8155
4ec521f2 8156 if ((insn & N32_BIT (14)) == 0)
35c08157
KLC
8157 {
8158 /* N32_BR1_BEQ */
8159 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8160 && N32_RT5 (insn) != REG_R5)
8161 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8162 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8163 && N32_RA5 (insn) != REG_R5)
8164 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8165 insn_type = NDS32_INSN_BEQS38;
8166 break;
8167 }
8168 else
8169 {
8170 /* N32_BR1_BNE */
8171 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8172 && N32_RT5 (insn) != REG_R5)
8173 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8174 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8175 && N32_RA5 (insn) != REG_R5)
8176 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8177 insn_type = NDS32_INSN_BNES38;
8178 break;
8179 }
8180 break;
8181
8182 case N32_OP6_BR2:
8183 switch (N32_BR2_SUB (insn))
8184 {
8185 case N32_BR2_BEQZ:
8186 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8187 {
8188 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8189 insn_type = NDS32_INSN_BEQZ38;
8190 }
1c8f6a4d
KLC
8191 else if (N32_RT5 (insn) == REG_R15
8192 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8193 {
8194 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8195 insn_type = NDS32_INSN_BEQZS8;
8196 }
8197 break;
8198
8199 case N32_BR2_BNEZ:
8200 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8201 {
8202 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8203 insn_type = NDS32_INSN_BNEZ38;
8204 }
1c8f6a4d
KLC
8205 else if (N32_RT5 (insn) == REG_R15
8206 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8207 {
8208 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8209 insn_type = NDS32_INSN_BNEZS8;
8210 }
8211 break;
8212
fbaf61ad
NC
8213 case N32_BR2_SOP0:
8214 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
35c08157
KLC
8215 {
8216 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8217 insn_type = NDS32_INSN_IFCALL9;
8218 }
8219 break;
8220 }
8221 break;
8222
8223 case N32_OP6_JI:
4ec521f2 8224 if ((insn & N32_BIT (24)) == 0)
35c08157
KLC
8225 {
8226 /* N32_JI_J */
8227 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8228 {
8229 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8230 insn_type = NDS32_INSN_J8;
8231 }
8232 }
8233 break;
8234
8235 case N32_OP6_JREG:
8236 if (__GF (insn, 8, 2) != 0)
8237 goto done;
8238
8239 switch (N32_IMMU (insn, 5))
8240 {
8241 case N32_JREG_JR:
8242 if (N32_JREG_HINT (insn) == 0)
8243 {
8244 /* jr */
8245 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8246 insn_type = NDS32_INSN_JR5;
8247 }
8248 else if (N32_JREG_HINT (insn) == 1)
8249 {
8250 /* ret */
8251 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8252 insn_type = NDS32_INSN_RET5;
8253 }
8254 else if (N32_JREG_HINT (insn) == 3)
8255 {
8256 /* ifret = mov55 $sp, $sp */
8257 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8258 insn_type = NDS32_INSN_IFRET;
8259 }
8260 break;
8261
8262 case N32_JREG_JRAL:
8263 /* It's convertible when return rt5 is $lp and address
8264 translation is kept. */
8265 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8266 {
8267 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8268 insn_type = NDS32_INSN_JRAL5;
8269 }
8270 break;
8271 }
8272 break;
8273
8274 case N32_OP6_MISC:
8275 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8276 {
8277 /* For v3, swid above 31 are used for ex9.it. */
8278 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8279 insn_type = NDS32_INSN_BREAK16;
8280 }
8281 break;
8282
8283 default:
8284 /* This instruction has no 16-bit variant. */
8285 goto done;
8286 }
8287
dc1e8a47 8288 done:
35c08157
KLC
8289 /* Bit-15 of insn16 should be set for a valid instruction. */
8290 if ((insn16 & 0x8000) == 0)
8291 return 0;
8292
8293 if (pinsn16)
8294 *pinsn16 = insn16;
8295 if (pinsn_type)
8296 *pinsn_type = insn_type;
8297 return 1;
8298}
8299
8300static int
8301special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8302 Elf_Internal_Rela *reloc)
8303{
8304 uint16_t insn16 = 0;
8305
8306 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8307 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8308 return 0;
8309
8310 if (!N32_IS_RT3 (insn))
8311 return 0;
8312
8313 switch (N32_OP6 (insn))
8314 {
8315 case N32_OP6_LWI:
8316 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8317 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8318 break;
8319 case N32_OP6_SWI:
8320 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8321 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8322 break;
8323 case N32_OP6_HWGP:
8324 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8325 break;
8326
8327 if (__GF (insn, 17, 3) == 6)
8328 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8329 else if (__GF (insn, 17, 3) == 7)
8330 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8331 break;
8332 }
8333
8334 if ((insn16 & 0x8000) == 0)
8335 return 0;
8336
8337 *pinsn16 = insn16;
8338 return 1;
8339}
8340
8341/* Convert a 16-bit instruction to 32-bit one.
8342 INSN16 it the input and PINSN it the point to output.
8343 Return non-zero on successful. Otherwise 0 is returned. */
8344
8345int
8346nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8347{
8348 uint32_t insn = 0xffffffff;
8349 unsigned long mach = bfd_get_mach (abfd);
8350
8351 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8352
8353 switch (__GF (insn16, 9, 6))
8354 {
8355 case 0x4: /* add45 */
1c8f6a4d
KLC
8356 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8357 N16_RA5 (insn16));
35c08157
KLC
8358 goto done;
8359 case 0x5: /* sub45 */
1c8f6a4d
KLC
8360 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8361 N16_RA5 (insn16));
35c08157
KLC
8362 goto done;
8363 case 0x6: /* addi45 */
1c8f6a4d
KLC
8364 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8365 N16_IMM5U (insn16));
35c08157
KLC
8366 goto done;
8367 case 0x7: /* subi45 */
1c8f6a4d
KLC
8368 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8369 -N16_IMM5U (insn16));
35c08157
KLC
8370 goto done;
8371 case 0x8: /* srai45 */
1c8f6a4d
KLC
8372 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8373 N16_IMM5U (insn16));
35c08157
KLC
8374 goto done;
8375 case 0x9: /* srli45 */
1c8f6a4d
KLC
8376 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8377 N16_IMM5U (insn16));
35c08157 8378 goto done;
35c08157 8379 case 0xa: /* slli333 */
1c8f6a4d
KLC
8380 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8381 N16_IMM3U (insn16));
35c08157
KLC
8382 goto done;
8383 case 0xc: /* add333 */
1c8f6a4d
KLC
8384 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8385 N16_RB3 (insn16));
35c08157
KLC
8386 goto done;
8387 case 0xd: /* sub333 */
1c8f6a4d
KLC
8388 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8389 N16_RB3 (insn16));
35c08157
KLC
8390 goto done;
8391 case 0xe: /* addi333 */
1c8f6a4d
KLC
8392 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8393 N16_IMM3U (insn16));
35c08157
KLC
8394 goto done;
8395 case 0xf: /* subi333 */
1c8f6a4d
KLC
8396 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8397 -N16_IMM3U (insn16));
35c08157 8398 goto done;
35c08157 8399 case 0x10: /* lwi333 */
1c8f6a4d
KLC
8400 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8401 N16_IMM3U (insn16));
35c08157
KLC
8402 goto done;
8403 case 0x12: /* lhi333 */
1c8f6a4d
KLC
8404 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8405 N16_IMM3U (insn16));
35c08157
KLC
8406 goto done;
8407 case 0x13: /* lbi333 */
1c8f6a4d
KLC
8408 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8409 N16_IMM3U (insn16));
35c08157
KLC
8410 goto done;
8411 case 0x11: /* lwi333.bi */
1c8f6a4d
KLC
8412 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8413 N16_IMM3U (insn16));
35c08157
KLC
8414 goto done;
8415 case 0x14: /* swi333 */
1c8f6a4d
KLC
8416 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8417 N16_IMM3U (insn16));
35c08157
KLC
8418 goto done;
8419 case 0x16: /* shi333 */
1c8f6a4d
KLC
8420 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8421 N16_IMM3U (insn16));
35c08157
KLC
8422 goto done;
8423 case 0x17: /* sbi333 */
1c8f6a4d
KLC
8424 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8425 N16_IMM3U (insn16));
35c08157
KLC
8426 goto done;
8427 case 0x15: /* swi333.bi */
1c8f6a4d
KLC
8428 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8429 N16_IMM3U (insn16));
35c08157 8430 goto done;
35c08157 8431 case 0x18: /* addri36.sp */
1c8f6a4d
KLC
8432 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8433 N16_IMM6U (insn16) << 2);
35c08157 8434 goto done;
35c08157 8435 case 0x19: /* lwi45.fe */
1c8f6a4d
KLC
8436 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8437 (N16_IMM5U (insn16) - 32));
35c08157
KLC
8438 goto done;
8439 case 0x1a: /* lwi450 */
8440 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8441 goto done;
8442 case 0x1b: /* swi450 */
8443 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8444 goto done;
8445
1c8f6a4d 8446 /* These are r15 implied instructions. */
35c08157
KLC
8447 case 0x30: /* slts45 */
8448 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8449 goto done;
8450 case 0x31: /* slt45 */
8451 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8452 goto done;
8453 case 0x32: /* sltsi45 */
8454 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8455 goto done;
8456 case 0x33: /* slti45 */
8457 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8458 goto done;
8459 case 0x34: /* beqzs8, bnezs8 */
4ec521f2 8460 if (insn16 & N32_BIT (8))
35c08157
KLC
8461 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8462 else
8463 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8464 goto done;
8465
8466 case 0x35: /* break16, ex9.it */
8467 /* Only consider range of v3 break16. */
8468 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8469 goto done;
8470
8471 case 0x3c: /* ifcall9 */
fbaf61ad 8472 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
35c08157
KLC
8473 goto done;
8474 case 0x3d: /* movpi45 */
8475 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8476 goto done;
8477
8478 case 0x3f: /* MISC33 */
1c8f6a4d 8479 switch (insn16 & 0x7)
35c08157
KLC
8480 {
8481 case 2: /* neg33 */
8482 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8483 break;
8484 case 3: /* not33 */
1c8f6a4d
KLC
8485 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8486 N16_RA3 (insn16));
35c08157
KLC
8487 break;
8488 case 4: /* mul33 */
1c8f6a4d
KLC
8489 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8490 N16_RA3 (insn16));
35c08157
KLC
8491 break;
8492 case 5: /* xor33 */
1c8f6a4d
KLC
8493 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8494 N16_RA3 (insn16));
35c08157
KLC
8495 break;
8496 case 6: /* and33 */
1c8f6a4d
KLC
8497 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8498 N16_RA3 (insn16));
35c08157
KLC
8499 break;
8500 case 7: /* or33 */
1c8f6a4d
KLC
8501 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8502 N16_RA3 (insn16));
35c08157
KLC
8503 break;
8504 }
8505 goto done;
8506
1c8f6a4d 8507 case 0xb:
35c08157
KLC
8508 switch (insn16 & 0x7)
8509 {
8510 case 0: /* zeb33 */
8511 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8512 break;
8513 case 1: /* zeh33 */
8514 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8515 break;
8516 case 2: /* seb33 */
8517 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8518 break;
8519 case 3: /* seh33 */
8520 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8521 break;
8522 case 4: /* xlsb33 */
8523 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8524 break;
8525 case 5: /* x11b33 */
8526 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8527 break;
8528 case 6: /* bmski33 */
8529 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8530 1 << __GF (insn16, 3, 3));
35c08157
KLC
8531 break;
8532 case 7: /* fexti33 */
8533 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8534 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
35c08157
KLC
8535 break;
8536 }
8537 goto done;
8538 }
8539
8540 switch (__GF (insn16, 10, 5))
8541 {
8542 case 0x0: /* mov55 or ifret16 */
8543 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8544 && N16_RT5 (insn16) == N16_RA5 (insn16))
1c8f6a4d 8545 insn = N32_JREG (JR, 0, 0, 0, 3);
35c08157 8546 else
1c8f6a4d 8547 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
35c08157
KLC
8548 goto done;
8549 case 0x1: /* movi55 */
8550 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8551 goto done;
8552 case 0x1b: /* addi10s (V2) */
8553 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8554 goto done;
8555 }
8556
8557 switch (__GF (insn16, 11, 4))
8558 {
8559 case 0x7: /* lwi37.fp/swi37.fp */
4ec521f2 8560 if (insn16 & N32_BIT (7)) /* swi37.fp */
35c08157
KLC
8561 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8562 else /* lwi37.fp */
8563 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8564 goto done;
8565 case 0x8: /* beqz38 */
8566 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8567 goto done;
8568 case 0x9: /* bnez38 */
8569 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8570 goto done;
8571 case 0xa: /* beqs38/j8, implied r5 */
8572 if (N16_RT38 (insn16) == 5)
8573 insn = N32_JI (J, N16_IMM8S (insn16));
8574 else
8575 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8576 goto done;
fbaf61ad 8577 case 0xb: /* bnes38 and others. */
35c08157
KLC
8578 if (N16_RT38 (insn16) == 5)
8579 {
8580 switch (__GF (insn16, 5, 3))
8581 {
8582 case 0: /* jr5 */
8583 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8584 break;
8585 case 4: /* ret5 */
8586 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8587 break;
8588 case 1: /* jral5 */
8589 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8590 break;
8591 case 2: /* ex9.it imm5 */
8592 /* ex9.it had no 32-bit variantl. */
8593 break;
8594 case 5: /* add5.pc */
8595 /* add5.pc had no 32-bit variantl. */
8596 break;
8597 }
8598 }
8599 else /* bnes38 */
8600 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8601 goto done;
8602 case 0xe: /* lwi37/swi37 */
8603 if (insn16 & (1 << 7)) /* swi37.sp */
8604 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8605 else /* lwi37.sp */
8606 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8607 goto done;
8608 }
8609
dc1e8a47 8610 done:
35c08157
KLC
8611 if (insn & 0x80000000)
8612 return 0;
8613
8614 if (pinsn)
8615 *pinsn = insn;
8616 return 1;
8617}
8618\f
fbaf61ad 8619
35c08157
KLC
8620static bfd_boolean
8621is_sda_access_insn (unsigned long insn)
8622{
8623 switch (N32_OP6 (insn))
8624 {
8625 case N32_OP6_LWI:
8626 case N32_OP6_LHI:
8627 case N32_OP6_LHSI:
8628 case N32_OP6_LBI:
8629 case N32_OP6_LBSI:
8630 case N32_OP6_SWI:
8631 case N32_OP6_SHI:
8632 case N32_OP6_SBI:
8633 case N32_OP6_LWC:
8634 case N32_OP6_LDC:
8635 case N32_OP6_SWC:
8636 case N32_OP6_SDC:
8637 return TRUE;
8638 default:
8639 ;
8640 }
8641 return FALSE;
8642}
8643
8644static unsigned long
8645turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8646{
8647 uint32_t oinsn = 0;
8648
8649 switch (type)
8650 {
8651 case R_NDS32_GOT_LO12:
8652 case R_NDS32_GOTOFF_LO12:
8653 case R_NDS32_PLTREL_LO12:
8654 case R_NDS32_PLT_GOTREL_LO12:
8655 case R_NDS32_LO12S0_RELA:
8656 switch (N32_OP6 (insn))
8657 {
8658 case N32_OP6_LBI:
8659 /* lbi.gp */
8660 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8661 break;
8662 case N32_OP6_LBSI:
8663 /* lbsi.gp */
4ec521f2 8664 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8665 break;
8666 case N32_OP6_SBI:
8667 /* sbi.gp */
8668 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8669 break;
8670 case N32_OP6_ORI:
8671 /* addi.gp */
4ec521f2 8672 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8673 break;
8674 }
8675 break;
8676
8677 case R_NDS32_LO12S1_RELA:
8678 switch (N32_OP6 (insn))
8679 {
8680 case N32_OP6_LHI:
8681 /* lhi.gp */
8682 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8683 break;
8684 case N32_OP6_LHSI:
8685 /* lhsi.gp */
4ec521f2 8686 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
35c08157
KLC
8687 break;
8688 case N32_OP6_SHI:
8689 /* shi.gp */
4ec521f2 8690 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8691 break;
8692 }
8693 break;
8694
8695 case R_NDS32_LO12S2_RELA:
8696 switch (N32_OP6 (insn))
8697 {
8698 case N32_OP6_LWI:
8699 /* lwi.gp */
8700 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8701 break;
8702 case N32_OP6_SWI:
8703 /* swi.gp */
8704 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8705 break;
8706 }
8707 break;
8708
8709 case R_NDS32_LO12S2_DP_RELA:
8710 case R_NDS32_LO12S2_SP_RELA:
8711 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8712 break;
8713 }
8714
8715 if (oinsn)
8716 *pinsn = oinsn;
8717
8718 return oinsn != 0;
8719}
8720
8721/* Linker hasn't found the correct merge section for non-section symbol
8722 in relax time, this work is left to the function elf_link_input_bfd().
8723 So for non-section symbol, _bfd_merged_section_offset is also needed
8724 to find the correct symbol address. */
8725
8726static bfd_vma
8727nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8728 asection **psec, Elf_Internal_Rela *rel)
8729{
8730 asection *sec = *psec;
8731 bfd_vma relocation;
8732
8733 relocation = (sec->output_section->vma
8734 + sec->output_offset + sym->st_value);
8735 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8736 {
8737 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8738 rel->r_addend =
8739 _bfd_merged_section_offset (abfd, psec,
8740 elf_section_data (sec)->sec_info,
8741 sym->st_value + rel->r_addend);
8742 else
8743 rel->r_addend =
8744 _bfd_merged_section_offset (abfd, psec,
8745 elf_section_data (sec)->sec_info,
8746 sym->st_value) + rel->r_addend;
8747
8748 if (sec != *psec)
8749 {
8750 /* If we have changed the section, and our original section is
8751 marked with SEC_EXCLUDE, it means that the original
8752 SEC_MERGE section has been completely subsumed in some
8753 other SEC_MERGE section. In this case, we need to leave
8754 some info around for --emit-relocs. */
8755 if ((sec->flags & SEC_EXCLUDE) != 0)
8756 sec->kept_section = *psec;
8757 sec = *psec;
8758 }
8759 rel->r_addend -= relocation;
8760 rel->r_addend += sec->output_section->vma + sec->output_offset;
8761 }
8762 return relocation;
8763}
8764
8765static bfd_vma
8766calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8767 Elf_Internal_Sym *isymbuf,
8768 Elf_Internal_Shdr *symtab_hdr)
8769{
8770 bfd_signed_vma foff;
8771 bfd_vma symval, addend;
8772 Elf_Internal_Rela irel_fn;
8773 Elf_Internal_Sym *isym;
8774 asection *sym_sec;
8775
8776 /* Get the value of the symbol referred to by the reloc. */
8777 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8778 {
8779 /* A local symbol. */
8780 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8781
8782 if (isym->st_shndx == SHN_UNDEF)
8783 sym_sec = bfd_und_section_ptr;
8784 else if (isym->st_shndx == SHN_ABS)
8785 sym_sec = bfd_abs_section_ptr;
8786 else if (isym->st_shndx == SHN_COMMON)
8787 sym_sec = bfd_com_section_ptr;
8788 else
8789 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8790 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8791 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8792 addend = irel_fn.r_addend;
8793 }
8794 else
8795 {
8796 unsigned long indx;
8797 struct elf_link_hash_entry *h;
8798
8799 /* An external symbol. */
8800 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8801 h = elf_sym_hashes (abfd)[indx];
8802 BFD_ASSERT (h != NULL);
8803
8804 while (h->root.type == bfd_link_hash_indirect
8805 || h->root.type == bfd_link_hash_warning)
8806 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8807
8808 if (h->root.type != bfd_link_hash_defined
8809 && h->root.type != bfd_link_hash_defweak)
8810 /* This appears to be a reference to an undefined
8811 symbol. Just ignore it--it will be caught by the
8812 regular reloc processing. */
8813 return 0;
8814
8815 if (h->root.u.def.section->flags & SEC_MERGE)
8816 {
8817 sym_sec = h->root.u.def.section;
8818 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8819 (sym_sec)->sec_info, h->root.u.def.value);
8820 symval = symval + sym_sec->output_section->vma
8821 + sym_sec->output_offset;
8822 }
8823 else
8824 symval = (h->root.u.def.value
8825 + h->root.u.def.section->output_section->vma
8826 + h->root.u.def.section->output_offset);
8827 addend = irel->r_addend;
8828 }
8829
8830 foff = symval + addend;
8831
8832 return foff;
8833}
8834
35c08157
KLC
8835static int
8836is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8837 asection *sec, Elf_Internal_Rela *rel)
8838{
8839 bfd_byte *contents;
8840 unsigned short insn16;
8841
8842 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8843 return FALSE;
8844 contents = elf_section_data (sec)->this_hdr.contents;
8845 insn16 = bfd_getb16 (contents + rel->r_offset);
8846 if (insn16 == NDS32_NOP16)
8847 return TRUE;
8848 return FALSE;
8849}
8850
8851/* It checks whether the instruction could be converted to
8852 16-bit form and returns the converted one.
8853
8854 `internal_relocs' is supposed to be sorted. */
8855
8856static int
8857is_convert_32_to_16 (bfd *abfd, asection *sec,
8858 Elf_Internal_Rela *reloc,
8859 Elf_Internal_Rela *internal_relocs,
8860 Elf_Internal_Rela *irelend,
8861 uint16_t *insn16)
8862{
8863#define NORMAL_32_TO_16 (1 << 0)
8864#define SPECIAL_32_TO_16 (1 << 1)
8865 bfd_byte *contents = NULL;
8866 bfd_signed_vma off;
8867 bfd_vma mem_addr;
8868 uint32_t insn = 0;
8869 Elf_Internal_Rela *pc_rel;
35c08157
KLC
8870 Elf_Internal_Shdr *symtab_hdr;
8871 Elf_Internal_Sym *isymbuf = NULL;
8872 int convert_type;
8873 bfd_vma offset;
8874
8875 if (reloc->r_offset + 4 > sec->size)
8876 return FALSE;
8877
8878 offset = reloc->r_offset;
8879
0c4bd9d9 8880 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
35c08157
KLC
8881 return FALSE;
8882 insn = bfd_getb32 (contents + offset);
8883
8884 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8885 convert_type = NORMAL_32_TO_16;
8886 else if (special_convert_32_to_16 (insn, insn16, reloc))
8887 convert_type = SPECIAL_32_TO_16;
8888 else
8889 return FALSE;
8890
8891 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8892 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8893 return FALSE;
8894
8895 /* Find the first relocation of the same relocation-type,
8896 so we iteratie them forward. */
8897 pc_rel = reloc;
1c8f6a4d 8898 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
35c08157
KLC
8899 pc_rel--;
8900
8901 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8902 {
8903 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8904 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8905 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8906 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8907 {
fbaf61ad 8908 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8909 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8910 || off == 0)
35c08157
KLC
8911 return FALSE;
8912 break;
8913 }
8914 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8915 {
8916 /* movi => movi55 */
1c8f6a4d
KLC
8917 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8918 symtab_hdr);
8919 /* mem_addr is unsigned, but the value should
8920 be between [-16, 15]. */
35c08157
KLC
8921 if ((mem_addr + 0x10) >> 5)
8922 return FALSE;
8923 break;
8924 }
1c8f6a4d
KLC
8925 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8926 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8927 {
8928 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8929 because it can be relaxed to addi for TLS_LE_ADD. */
8930 return FALSE;
8931 }
35c08157
KLC
8932 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8933 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8934 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8935 && convert_type == SPECIAL_32_TO_16)
8936 {
8937 /* fp-as-gp
8938 We've selected a best fp-base for this access, so we can
8939 always resolve it anyway. Do nothing. */
8940 break;
8941 }
8942 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8943 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8944 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8945 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8946 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8947 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8948 {
1c8f6a4d
KLC
8949 /* Prevent unresolved addi instruction translate
8950 to addi45 or addi333. */
35c08157
KLC
8951 return FALSE;
8952 }
1c8f6a4d
KLC
8953 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8954 {
fbaf61ad 8955 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8956 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8957 return FALSE;
8958 break;
8959 }
35c08157
KLC
8960 }
8961
8962 return TRUE;
8963}
8964
8965static void
8966nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8967 Elf_Internal_Rela *reloc,
8968 Elf_Internal_Rela *internal_relocs,
8969 Elf_Internal_Rela *irelend,
8970 unsigned short insn16)
8971{
8972 Elf_Internal_Rela *pc_rel;
8973 bfd_vma offset;
8974
8975 offset = reloc->r_offset;
8976 bfd_putb16 (insn16, contents + offset);
8977 /* Find the first relocation of the same relocation-type,
8978 so we iteratie them forward. */
8979 pc_rel = reloc;
8980 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8981 pc_rel--;
8982
8983 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8984 {
8985 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8986 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8987 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8988 {
8989 pc_rel->r_info =
8990 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8991 }
8992 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8993 pc_rel->r_info =
8994 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8995 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8996 pc_rel->r_info =
8997 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8998 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8999 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
9000 pc_rel->r_info =
9001 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
1c8f6a4d
KLC
9002 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
9003 pc_rel->r_info =
9004 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
35c08157
KLC
9005 }
9006}
9007
9008/* Find a relocation of type specified by `reloc_type'
9009 of the same r_offset with reloc.
9010 If not found, return irelend.
9011
9012 Assuming relocations are sorted by r_offset,
9013 we find the relocation from `reloc' backward untill relocs,
9014 or find it from `reloc' forward untill irelend. */
9015
9016static Elf_Internal_Rela *
9017find_relocs_at_address (Elf_Internal_Rela *reloc,
9018 Elf_Internal_Rela *relocs,
9019 Elf_Internal_Rela *irelend,
9020 enum elf_nds32_reloc_type reloc_type)
9021{
9022 Elf_Internal_Rela *rel_t;
9023
9024 /* Find backward. */
9025 for (rel_t = reloc;
9026 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9027 rel_t--)
9028 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9029 return rel_t;
9030
1c8f6a4d 9031 /* We didn't find it backward. Try find it forward. */
35c08157
KLC
9032 for (rel_t = reloc;
9033 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9034 rel_t++)
9035 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9036 return rel_t;
9037
9038 return irelend;
9039}
9040
9041/* Find a relocation of specified type and offset.
9042 `reloc' is just a refence point to find a relocation at specified offset.
9043 If not found, return irelend.
9044
9045 Assuming relocations are sorted by r_offset,
9046 we find the relocation from `reloc' backward untill relocs,
9047 or find it from `reloc' forward untill irelend. */
9048
9049static Elf_Internal_Rela *
9050find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9051 Elf_Internal_Rela *relocs,
9052 Elf_Internal_Rela *irelend,
6cae483a 9053 enum elf_nds32_reloc_type reloc_type,
35c08157
KLC
9054 bfd_vma offset_p)
9055{
9056 Elf_Internal_Rela *rel_t = NULL;
9057
9058 /* First, we try to find a relocation of offset `offset_p',
9059 and then we use find_relocs_at_address to find specific type. */
9060
9061 if (reloc->r_offset > offset_p)
9062 {
9063 /* Find backward. */
9064 for (rel_t = reloc;
9065 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9066 /* Do nothing. */;
9067 }
9068 else if (reloc->r_offset < offset_p)
9069 {
9070 /* Find forward. */
9071 for (rel_t = reloc;
9072 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9073 /* Do nothing. */;
9074 }
9075 else
9076 rel_t = reloc;
9077
9078 /* Not found? */
9079 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9080 return irelend;
9081
9082 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9083}
9084
fbaf61ad
NC
9085typedef struct nds32_elf_blank nds32_elf_blank_t;
9086struct nds32_elf_blank
35c08157 9087{
fbaf61ad
NC
9088 /* Where the blank begins. */
9089 bfd_vma offset;
9090 /* The size of the blank. */
9091 bfd_vma size;
9092 /* The accumulative size before this blank. */
9093 bfd_vma total_size;
9094 nds32_elf_blank_t *next;
9095 nds32_elf_blank_t *prev;
9096};
35c08157
KLC
9097
9098static nds32_elf_blank_t *blank_free_list = NULL;
9099
9100static nds32_elf_blank_t *
9101create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9102{
9103 nds32_elf_blank_t *blank_t;
9104
9105 if (blank_free_list)
9106 {
9107 blank_t = blank_free_list;
9108 blank_free_list = blank_free_list->next;
9109 }
9110 else
9111 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9112
9113 if (blank_t == NULL)
9114 return NULL;
9115
9116 blank_t->offset = offset_p;
9117 blank_t->size = size_p;
9118 blank_t->total_size = 0;
9119 blank_t->next = NULL;
9120 blank_t->prev = NULL;
9121
9122 return blank_t;
9123}
9124
9125static void
9126remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9127{
9128 if (blank_free_list)
9129 {
9130 blank_free_list->prev = blank_p;
9131 blank_p->next = blank_free_list;
9132 }
9133 else
9134 blank_p->next = NULL;
9135
9136 blank_p->prev = NULL;
9137 blank_free_list = blank_p;
9138}
9139
9140static void
9141clean_nds32_elf_blank (void)
9142{
9143 nds32_elf_blank_t *blank_t;
9144
9145 while (blank_free_list)
9146 {
9147 blank_t = blank_free_list;
9148 blank_free_list = blank_free_list->next;
9149 free (blank_t);
9150 }
9151}
9152
9153static nds32_elf_blank_t *
9154search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9155{
9156 nds32_elf_blank_t *blank_t;
9157
9158 if (!blank_p)
9159 return NULL;
9160 blank_t = blank_p;
9161
9162 while (blank_t && addr < blank_t->offset)
9163 blank_t = blank_t->prev;
9164 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9165 blank_t = blank_t->next;
9166
9167 return blank_t;
9168}
9169
9170static bfd_vma
9171get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9172 int overwrite)
9173{
9174 nds32_elf_blank_t *blank_t;
9175
9176 blank_t = search_nds32_elf_blank (*blank_p, addr);
9177 if (!blank_t)
9178 return 0;
9179
9180 if (overwrite)
9181 *blank_p = blank_t;
9182
9183 if (addr < blank_t->offset + blank_t->size)
9184 return blank_t->total_size + (addr - blank_t->offset);
9185 else
9186 return blank_t->total_size + blank_t->size;
9187}
9188
9189static bfd_boolean
9190insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9191{
9192 nds32_elf_blank_t *blank_t, *blank_t2;
9193
9194 if (!*blank_p)
9195 {
9196 *blank_p = create_nds32_elf_blank (addr, len);
9197 return *blank_p ? TRUE : FALSE;
9198 }
9199
9200 blank_t = search_nds32_elf_blank (*blank_p, addr);
9201
9202 if (blank_t == NULL)
9203 {
9204 blank_t = create_nds32_elf_blank (addr, len);
9205 if (!blank_t)
9206 return FALSE;
9207 while ((*blank_p)->prev != NULL)
9208 *blank_p = (*blank_p)->prev;
9209 blank_t->next = *blank_p;
9210 (*blank_p)->prev = blank_t;
9211 (*blank_p) = blank_t;
9212 return TRUE;
9213 }
9214
9215 if (addr < blank_t->offset + blank_t->size)
9216 {
fbaf61ad
NC
9217 /* Extend the origin blank. */
9218 if (addr + len > blank_t->offset + blank_t->size)
9219 blank_t->size = addr + len - blank_t->offset;
35c08157
KLC
9220 }
9221 else
9222 {
9223 blank_t2 = create_nds32_elf_blank (addr, len);
9224 if (!blank_t2)
9225 return FALSE;
9226 if (blank_t->next)
9227 {
9228 blank_t->next->prev = blank_t2;
9229 blank_t2->next = blank_t->next;
9230 }
9231 blank_t2->prev = blank_t;
9232 blank_t->next = blank_t2;
9233 *blank_p = blank_t2;
9234 }
9235
9236 return TRUE;
9237}
9238
9239static bfd_boolean
9240insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9241 bfd_vma len)
9242{
9243 nds32_elf_blank_t *blank_t;
9244
9245 if (!insert_nds32_elf_blank (blank_p, addr, len))
9246 return FALSE;
9247
9248 blank_t = *blank_p;
9249
9250 if (!blank_t->prev)
9251 {
9252 blank_t->total_size = 0;
9253 blank_t = blank_t->next;
9254 }
9255
9256 while (blank_t)
9257 {
9258 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9259 blank_t = blank_t->next;
9260 }
9261
9262 return TRUE;
9263}
9264
9265static void
9266calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9267{
9268 nds32_elf_blank_t *blank_t;
9269 bfd_vma total_size = 0;
9270
9271 if (!blank_p)
9272 return;
9273
9274 blank_t = blank_p;
9275 while (blank_t->prev)
9276 blank_t = blank_t->prev;
9277 while (blank_t)
9278 {
9279 blank_t->total_size = total_size;
9280 total_size += blank_t->size;
9281 blank_t = blank_t->next;
9282 }
9283}
9284
9285static bfd_boolean
9286nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9287 nds32_elf_blank_t *blank_p)
9288{
9289 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
1c8f6a4d 9290 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
35c08157
KLC
9291 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9292 unsigned int sec_shndx; /* The section the be relaxed. */
9293 bfd_byte *contents; /* Contents data of iterating section. */
9294 Elf_Internal_Rela *internal_relocs;
9295 Elf_Internal_Rela *irel;
9296 Elf_Internal_Rela *irelend;
9297 struct elf_link_hash_entry **sym_hashes;
9298 struct elf_link_hash_entry **end_hashes;
9299 unsigned int symcount;
9300 asection *sect;
9301 nds32_elf_blank_t *blank_t;
9302 nds32_elf_blank_t *blank_t2;
9303 nds32_elf_blank_t *blank_head;
9304
9305 blank_head = blank_t = blank_p;
9306 while (blank_head->prev != NULL)
9307 blank_head = blank_head->prev;
9308 while (blank_t->next != NULL)
9309 blank_t = blank_t->next;
9310
9311 if (blank_t->offset + blank_t->size <= sec->size)
9312 {
9313 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9314 blank_t->next->prev = blank_t;
9315 }
9316 if (blank_head->offset > 0)
9317 {
9318 blank_head->prev = create_nds32_elf_blank (0, 0);
9319 blank_head->prev->next = blank_head;
9320 blank_head = blank_head->prev;
9321 }
9322
9323 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9324
9325 /* The deletion must stop at the next ALIGN reloc for an alignment
9326 power larger than the number of bytes we are deleting. */
9327
9328 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9329 if (!nds32_get_local_syms (abfd, sec, &isym))
9330 return FALSE;
9331
9332 if (isym == NULL)
9333 {
9334 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9335 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9336 symtab_hdr->contents = (bfd_byte *) isym;
9337 }
9338
9339 if (isym == NULL || symtab_hdr->sh_info == 0)
9340 return FALSE;
9341
9342 blank_t = blank_head;
9343 calc_nds32_blank_total (blank_head);
9344
9345 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9346 {
9347 /* Adjust all the relocs. */
9348
9349 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9350 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9351 TRUE /* keep_memory */);
9352 irelend = internal_relocs + sect->reloc_count;
9353
9354 blank_t = blank_head;
9355 blank_t2 = blank_head;
9356
9357 if (!(sect->flags & SEC_RELOC))
9358 continue;
9359
0c4bd9d9 9360 nds32_get_section_contents (abfd, sect, &contents, TRUE);
35c08157
KLC
9361
9362 for (irel = internal_relocs; irel < irelend; irel++)
9363 {
9364 bfd_vma raddr;
9365
9366 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9367 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9368 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9369 {
9370 unsigned long val = 0;
1c8f6a4d
KLC
9371 unsigned long mask;
9372 long before, between;
6cae483a 9373 long offset = 0;
35c08157
KLC
9374
9375 switch (ELF32_R_TYPE (irel->r_info))
9376 {
9377 case R_NDS32_DIFF8:
1c8f6a4d 9378 offset = bfd_get_8 (abfd, contents + irel->r_offset);
35c08157
KLC
9379 break;
9380 case R_NDS32_DIFF16:
1c8f6a4d 9381 offset = bfd_get_16 (abfd, contents + irel->r_offset);
35c08157
KLC
9382 break;
9383 case R_NDS32_DIFF32:
9384 val = bfd_get_32 (abfd, contents + irel->r_offset);
1c8f6a4d
KLC
9385 /* Get the signed bit and mask for the high part. The
9386 gcc will alarm when right shift 32-bit since the
9387 type size of long may be 32-bit. */
9388 mask = 0 - (val >> 31);
9389 if (mask)
9390 offset = (val | (mask - 0xffffffff));
9391 else
9392 offset = val;
35c08157
KLC
9393 break;
9394 default:
9395 BFD_ASSERT (0);
9396 }
9397
9398 /* DIFF value
9399 0 |encoded in location|
9400 |------------|-------------------|---------
9401 sym+off(addend)
9402 -- before ---| *****************
9403 --------------------- between ---|
9404
1c8f6a4d
KLC
9405 We only care how much data are relax between DIFF,
9406 marked as ***. */
35c08157
KLC
9407
9408 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9409 between = get_nds32_elf_blank_total (&blank_t,
9410 irel->r_addend + offset, 0);
35c08157
KLC
9411 if (between == before)
9412 goto done_adjust_diff;
9413
9414 switch (ELF32_R_TYPE (irel->r_info))
9415 {
9416 case R_NDS32_DIFF8:
1c8f6a4d
KLC
9417 bfd_put_8 (abfd, offset - (between - before),
9418 contents + irel->r_offset);
35c08157
KLC
9419 break;
9420 case R_NDS32_DIFF16:
1c8f6a4d
KLC
9421 bfd_put_16 (abfd, offset - (between - before),
9422 contents + irel->r_offset);
35c08157
KLC
9423 break;
9424 case R_NDS32_DIFF32:
1c8f6a4d
KLC
9425 bfd_put_32 (abfd, offset - (between - before),
9426 contents + irel->r_offset);
35c08157
KLC
9427 break;
9428 }
9429 }
9430 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9431 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9432 {
9433 bfd_vma val = 0;
9434 unsigned int len = 0;
9435 unsigned long before, between;
9436 bfd_byte *endp, *p;
9437
4265548c
PA
9438 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9439 &len);
35c08157
KLC
9440
9441 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9442 between = get_nds32_elf_blank_total (&blank_t,
9443 irel->r_addend + val, 0);
35c08157
KLC
9444 if (between == before)
9445 goto done_adjust_diff;
9446
9447 p = contents + irel->r_offset;
9448 endp = p + len -1;
9449 memset (p, 0x80, len);
9450 *(endp) = 0;
9451 p = write_uleb128 (p, val - (between - before)) - 1;
9452 if (p < endp)
9453 *p |= 0x80;
9454 }
dc1e8a47 9455 done_adjust_diff:
35c08157
KLC
9456
9457 if (sec == sect)
9458 {
9459 raddr = irel->r_offset;
1c8f6a4d
KLC
9460 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9461 irel->r_offset, 1);
35c08157
KLC
9462
9463 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9464 continue;
9465 if (blank_t2 && blank_t2->next
1c8f6a4d
KLC
9466 && (blank_t2->offset > raddr
9467 || blank_t2->next->offset <= raddr))
4eca0228 9468 _bfd_error_handler
38f14ab8
AM
9469 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9470 abfd);
35c08157
KLC
9471
9472 /* Mark reloc in deleted portion as NONE.
9473 For some relocs like R_NDS32_LABEL that doesn't modify the
9474 content in the section. R_NDS32_LABEL doesn't belong to the
9475 instruction in the section, so we should preserve it. */
9476 if (raddr >= blank_t2->offset
9477 && raddr < blank_t2->offset + blank_t2->size
9478 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9479 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9480 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9481 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9482 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9483 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9484 {
9485 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9486 R_NDS32_NONE);
9487 continue;
9488 }
9489 }
9490
9491 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9492 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9493 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9494 continue;
9495
9496 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9497 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9498 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9499 {
9500 if (irel->r_addend <= sec->size)
9501 irel->r_addend -=
9502 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9503 }
9504 }
9505 }
9506
9507 /* Adjust the local symbols defined in this section. */
9508 blank_t = blank_head;
9509 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9510 {
9511 if (isym->st_shndx == sec_shndx)
9512 {
9513 if (isym->st_value <= sec->size)
9514 {
9515 bfd_vma ahead;
9516 bfd_vma orig_addr = isym->st_value;
9517
9518 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9519 isym->st_value -= ahead;
9520
9521 /* Adjust function size. */
1c8f6a4d
KLC
9522 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9523 && isym->st_size > 0)
9524 isym->st_size -=
9525 get_nds32_elf_blank_total
9526 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
35c08157
KLC
9527 }
9528 }
9529 }
9530
9531 /* Now adjust the global symbols defined in this section. */
9532 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9533 - symtab_hdr->sh_info);
9534 sym_hashes = elf_sym_hashes (abfd);
9535 end_hashes = sym_hashes + symcount;
9536 blank_t = blank_head;
9537 for (; sym_hashes < end_hashes; sym_hashes++)
9538 {
9539 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9540
9541 if ((sym_hash->root.type == bfd_link_hash_defined
9542 || sym_hash->root.type == bfd_link_hash_defweak)
9543 && sym_hash->root.u.def.section == sec)
9544 {
9545 if (sym_hash->root.u.def.value <= sec->size)
9546 {
9547 bfd_vma ahead;
9548 bfd_vma orig_addr = sym_hash->root.u.def.value;
9549
9550 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9551 sym_hash->root.u.def.value -= ahead;
9552
9553 /* Adjust function size. */
9554 if (sym_hash->type == STT_FUNC)
1c8f6a4d
KLC
9555 sym_hash->size -=
9556 get_nds32_elf_blank_total
9557 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
35c08157
KLC
9558
9559 }
9560 }
9561 }
9562
9563 contents = elf_section_data (sec)->this_hdr.contents;
9564 blank_t = blank_head;
9565 while (blank_t->next)
9566 {
9567 /* Actually delete the bytes. */
9568
9569 /* If current blank is the last blank overlap with current section,
9570 go to finish process. */
9571 if (sec->size <= (blank_t->next->offset))
9572 break;
9573
9574 memmove (contents + blank_t->offset - blank_t->total_size,
9575 contents + blank_t->offset + blank_t->size,
9576 blank_t->next->offset - (blank_t->offset + blank_t->size));
9577
9578 blank_t = blank_t->next;
9579 }
9580
9581 if (sec->size > (blank_t->offset + blank_t->size))
9582 {
9583 /* There are remaining code between blank and section boundary.
9584 Move the remaining code to appropriate location. */
9585 memmove (contents + blank_t->offset - blank_t->total_size,
9586 contents + blank_t->offset + blank_t->size,
9587 sec->size - (blank_t->offset + blank_t->size));
9588 sec->size -= blank_t->total_size + blank_t->size;
9589 }
9590 else
9591 /* This blank is not entirely included in the section,
9592 reduce the section size by only part of the blank size. */
9593 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9594
9595 while (blank_head)
9596 {
9597 blank_t = blank_head;
9598 blank_head = blank_head->next;
9599 remove_nds32_elf_blank (blank_t);
9600 }
9601
9602 return TRUE;
9603}
9604
9605/* Get the contents of a section. */
9606
9607static int
0c4bd9d9
KLC
9608nds32_get_section_contents (bfd *abfd, asection *sec,
9609 bfd_byte **contents_p, bfd_boolean cache)
35c08157
KLC
9610{
9611 /* Get the section contents. */
9612 if (elf_section_data (sec)->this_hdr.contents != NULL)
9613 *contents_p = elf_section_data (sec)->this_hdr.contents;
9614 else
9615 {
9616 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9617 return FALSE;
0c4bd9d9
KLC
9618 if (cache)
9619 elf_section_data (sec)->this_hdr.contents = *contents_p;
35c08157
KLC
9620 }
9621
9622 return TRUE;
9623}
9624
9625/* Get the contents of the internal symbol of abfd. */
9626
9627static int
9628nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9629 Elf_Internal_Sym **isymbuf_p)
9630{
9631 Elf_Internal_Shdr *symtab_hdr;
9632 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9633
9634 /* Read this BFD's local symbols if we haven't done so already. */
9635 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9636 {
9637 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9638 if (*isymbuf_p == NULL)
9639 {
9640 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9641 symtab_hdr->sh_info, 0,
9642 NULL, NULL, NULL);
9643 if (*isymbuf_p == NULL)
9644 return FALSE;
9645 }
9646 }
9647 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9648
9649 return TRUE;
9650}
9651
9652/* Range of small data. */
1c8f6a4d
KLC
9653static bfd_vma sdata_range[2][2];
9654static bfd_vma const sdata_init_range[2] =
9655{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
35c08157
KLC
9656
9657static int
9658nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9659 bfd_byte *contents, bfd_vma addr)
9660{
9661 unsigned long insn = bfd_getb32 (contents + addr);
9662
9663 if (insn & 0x80000000)
9664 return 2;
9665
9666 return 4;
9667}
9668
9669/* Set the gp relax range. We have to measure the safe range
9670 to do gp relaxation. */
9671
9672static void
9673relax_range_measurement (bfd *abfd)
9674{
9675 asection *sec_f, *sec_b;
9676 /* For upper bound. */
9677 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9678 bfd_vma align;
35c08157
KLC
9679 static int decide_relax_range = 0;
9680 int i;
fbaf61ad 9681 int range_number = ARRAY_SIZE (sdata_init_range);
35c08157
KLC
9682
9683 if (decide_relax_range)
9684 return;
9685 decide_relax_range = 1;
9686
9687 if (sda_rela_sec == NULL)
9688 {
9689 /* Since there is no data sections, we assume the range is page size. */
1c8f6a4d 9690 for (i = 0; i < range_number; i++)
35c08157
KLC
9691 {
9692 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9693 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9694 }
9695 return;
9696 }
9697
9698 /* Get the biggest alignment power after the gp located section. */
9699 sec_f = sda_rela_sec->output_section;
9700 sec_b = sec_f->next;
9701 align = 0;
9702 while (sec_b != NULL)
9703 {
9704 if ((unsigned)(1 << sec_b->alignment_power) > align)
9705 align = (1 << sec_b->alignment_power);
9706 sec_b = sec_b->next;
9707 }
9708
9709 /* I guess we can not determine the section before
9710 gp located section, so we assume the align is max page size. */
1c8f6a4d 9711 for (i = 0; i < range_number; i++)
35c08157 9712 {
1c8f6a4d 9713 sdata_range[i][1] = sdata_init_range[i] - align;
35c08157 9714 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
1c8f6a4d 9715 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
35c08157
KLC
9716 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9717 }
9718}
9719
9720/* These are macros used to check flags encoded in r_addend.
9721 They are only used by nds32_elf_relax_section (). */
9722#define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9723#define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9724#define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9725#define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9726
695344c0
NC
9727static const char * unrecognized_reloc_msg =
9728 /* xgettext:c-format */
2dcf00ce 9729 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
695344c0 9730
1c8f6a4d
KLC
9731/* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9732
35c08157 9733static bfd_boolean
1c8f6a4d
KLC
9734nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9735 Elf_Internal_Rela *internal_relocs, int *insn_len,
9736 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9737 Elf_Internal_Shdr *symtab_hdr)
9738{
9739 /* There are 3 variations for LONGCALL1
9740 case 4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8 9741 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9742 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9743 jral5 ta ;
1c8f6a4d
KLC
9744
9745 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8 9746 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9747 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9748 jral ta ;
1c8f6a4d
KLC
9749
9750 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8 9751 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9752 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9753 jral ta ;
1c8f6a4d
KLC
9754 Check code for -mlong-calls output. */
9755
9756 /* Get the reloc for the address from which the register is
9757 being loaded. This reloc will tell us which function is
9758 actually being called. */
9759
9760 bfd_vma laddr;
9761 int seq_len; /* Original length of instruction sequence. */
35c08157 9762 uint32_t insn;
1c8f6a4d 9763 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d 9764 bfd_signed_vma foff;
35c08157 9765 uint16_t insn16;
35c08157 9766
1c8f6a4d
KLC
9767 irelend = internal_relocs + sec->reloc_count;
9768 seq_len = GET_SEQ_LEN (irel->r_addend);
9769 laddr = irel->r_offset;
9770 *insn_len = seq_len;
35c08157 9771
1c8f6a4d
KLC
9772 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9773 R_NDS32_HI20_RELA, laddr);
9774 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9775 R_NDS32_LO12S0_ORI_RELA,
9776 laddr + 4);
35c08157 9777
1c8f6a4d 9778 if (hi_irelfn == irelend || lo_irelfn == irelend)
35c08157 9779 {
695344c0 9780 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
2dcf00ce 9781 (uint64_t) irel->r_offset);
1c8f6a4d 9782 return FALSE;
35c08157
KLC
9783 }
9784
1c8f6a4d 9785 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9786 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9787
1c8f6a4d 9788 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
9789 if (foff == 0
9790 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9791 || foff >= CONSERVATIVE_24BIT_S1)
9792 return FALSE;
35c08157 9793
fbaf61ad 9794 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
9795 /* For simplicity of coding, we are going to modify the section
9796 contents, the section relocs, and the BFD symbol table. We
9797 must tell the rest of the code not to free up this
9798 information. It would be possible to instead create a table
9799 of changes which have to be made, as is done in coff-mips.c;
9800 that would be more work, but would require less memory when
9801 the linker is run. */
9802
9803 /* Replace the long call with a jal. */
9804 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9805 R_NDS32_25_PCREL_RELA);
9806 irel->r_addend = hi_irelfn->r_addend;
9807
9808 /* We don't resolve this here but resolve it in relocate_section. */
9809 insn = INSN_JAL;
9810 bfd_putb32 (insn, contents + irel->r_offset);
9811
9812 hi_irelfn->r_info =
9813 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9814 lo_irelfn->r_info =
9815 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9816 *insn_len = 4;
9817
9818 if (seq_len & 0x2)
9819 {
9820 insn16 = NDS32_NOP16;
9821 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9822 lo_irelfn->r_info =
9823 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9824 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9825 *insn_len += 2;
9826 }
9827 return TRUE;
9828}
35c08157 9829
1c8f6a4d
KLC
9830#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9831/* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
35c08157 9832
1c8f6a4d
KLC
9833static bfd_boolean
9834nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9835 Elf_Internal_Rela *internal_relocs, int *insn_len,
9836 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9837 Elf_Internal_Shdr *symtab_hdr)
9838{
9839 /* bltz rt, .L1 ; LONGCALL2
9840 jal symbol ; 25_PCREL
9841 .L1: */
35c08157 9842
1c8f6a4d
KLC
9843 /* Get the reloc for the address from which the register is
9844 being loaded. This reloc will tell us which function is
9845 actually being called. */
35c08157 9846
1c8f6a4d
KLC
9847 bfd_vma laddr;
9848 uint32_t insn;
9849 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
1c8f6a4d 9850 bfd_signed_vma foff;
35c08157 9851
1c8f6a4d
KLC
9852 irelend = internal_relocs + sec->reloc_count;
9853 laddr = irel->r_offset;
9854 i1_irelfn =
9855 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9856 R_NDS32_25_PCREL_RELA, laddr + 4);
9857
9858 if (i1_irelfn == irelend)
35c08157 9859 {
695344c0 9860 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
2dcf00ce 9861 (uint64_t) irel->r_offset);
1c8f6a4d 9862 return FALSE;
35c08157
KLC
9863 }
9864
1c8f6a4d 9865 insn = bfd_getb32 (contents + laddr);
35c08157 9866
1c8f6a4d 9867 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9868 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
35c08157 9869
fbaf61ad
NC
9870 if (foff == 0
9871 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
9872 || foff >= CONSERVATIVE_16BIT_S1)
9873 return FALSE;
35c08157 9874
1c8f6a4d
KLC
9875 /* Relax to bgezal rt, label ; 17_PCREL
9876 or bltzal rt, label ; 17_PCREL */
9877
9878 /* Convert to complimentary conditional call. */
9879 insn = CONVERT_CONDITION_CALL (insn);
9880
9881 /* For simplicity of coding, we are going to modify the section
9882 contents, the section relocs, and the BFD symbol table. We
9883 must tell the rest of the code not to free up this
9884 information. It would be possible to instead create a table
9885 of changes which have to be made, as is done in coff-mips.c;
9886 that would be more work, but would require less memory when
9887 the linker is run. */
9888
9889 /* Clean unnessary relocations. */
9890 i1_irelfn->r_info =
9891 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9892 cond_irelfn =
9893 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9894 R_NDS32_17_PCREL_RELA, laddr);
9895 if (cond_irelfn != irelend)
9896 cond_irelfn->r_info =
9897 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9898
9899 /* Replace the long call with a bgezal. */
9900 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9901 R_NDS32_17_PCREL_RELA);
9902 irel->r_addend = i1_irelfn->r_addend;
9903
9904 bfd_putb32 (insn, contents + irel->r_offset);
9905
9906 *insn_len = 4;
9907 return TRUE;
9908}
35c08157 9909
1c8f6a4d
KLC
9910/* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9911
9912static bfd_boolean
9913nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9914 Elf_Internal_Rela *internal_relocs, int *insn_len,
9915 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9916 Elf_Internal_Shdr *symtab_hdr)
9917{
9918 /* There are 3 variations for LONGCALL3
9919 case 4-4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8
AM
9920 bltz rt, $1 ; LONGCALL3
9921 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9922 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9923 jral5 ta ;
1c8f6a4d
KLC
9924 $1
9925
9926 case 4-4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
9927 bltz rt, $1 ; LONGCALL3
9928 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9929 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9930 jral ta ;
1c8f6a4d
KLC
9931 $1
9932
9933 case 4-4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
9934 bltz rt, $1 ; LONGCALL3
9935 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9936 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9937 jral ta ;
1c8f6a4d
KLC
9938 $1 */
9939
9940 /* Get the reloc for the address from which the register is
9941 being loaded. This reloc will tell us which function is
9942 actually being called. */
9943
9944 bfd_vma laddr;
9945 int seq_len; /* Original length of instruction sequence. */
9946 uint32_t insn;
9947 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
1c8f6a4d
KLC
9948 bfd_signed_vma foff;
9949 uint16_t insn16;
9950
9951 irelend = internal_relocs + sec->reloc_count;
9952 seq_len = GET_SEQ_LEN (irel->r_addend);
9953 laddr = irel->r_offset;
9954 *insn_len = seq_len;
9955
9956 hi_irelfn =
9957 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9958 R_NDS32_HI20_RELA, laddr + 4);
9959 lo_irelfn =
9960 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9961 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9962
9963 if (hi_irelfn == irelend || lo_irelfn == irelend)
9964 {
695344c0 9965 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
2dcf00ce 9966 (uint64_t) irel->r_offset);
1c8f6a4d 9967 return FALSE;
35c08157
KLC
9968 }
9969
1c8f6a4d 9970 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9971 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9972
fbaf61ad
NC
9973 if (foff == 0
9974 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9975 || foff >= CONSERVATIVE_24BIT_S1)
9976 return FALSE;
35c08157 9977
1c8f6a4d
KLC
9978 insn = bfd_getb32 (contents + laddr);
9979 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
35c08157 9980 {
1c8f6a4d
KLC
9981 /* Relax to bgezal rt, label ; 17_PCREL
9982 or bltzal rt, label ; 17_PCREL */
35c08157 9983
1c8f6a4d
KLC
9984 /* Convert to complimentary conditional call. */
9985 insn = CONVERT_CONDITION_CALL (insn);
9986 bfd_putb32 (insn, contents + irel->r_offset);
35c08157 9987
1c8f6a4d
KLC
9988 *insn_len = 4;
9989 irel->r_info =
9990 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9991 hi_irelfn->r_info =
9992 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9993 lo_irelfn->r_info =
9994 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9995
9996 cond_irelfn =
9997 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9998 R_NDS32_17_PCREL_RELA, laddr);
9999 if (cond_irelfn != irelend)
35c08157 10000 {
1c8f6a4d
KLC
10001 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10002 R_NDS32_17_PCREL_RELA);
10003 cond_irelfn->r_addend = hi_irelfn->r_addend;
35c08157 10004 }
1c8f6a4d
KLC
10005
10006 if (seq_len & 0x2)
35c08157 10007 {
1c8f6a4d
KLC
10008 insn16 = NDS32_NOP16;
10009 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10010 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10011 R_NDS32_INSN16);
10012 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10013 insn_len += 2;
35c08157 10014 }
1c8f6a4d
KLC
10015 }
10016 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10017 {
10018 /* Relax to the following instruction sequence
10019 bltz rt, $1 ; LONGCALL2
10020 jal symbol ; 25_PCREL
10021 $1 */
10022 *insn_len = 8;
10023 insn = INSN_JAL;
10024 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
35c08157 10025
1c8f6a4d
KLC
10026 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10027 R_NDS32_25_PCREL_RELA);
10028 irel->r_info =
10029 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10030
10031 lo_irelfn->r_info =
10032 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10033
1c8f6a4d 10034 if (seq_len & 0x2)
35c08157 10035 {
1c8f6a4d
KLC
10036 insn16 = NDS32_NOP16;
10037 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10038 lo_irelfn->r_info =
10039 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10040 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10041 insn_len += 2;
10042 }
10043 }
10044 return TRUE;
10045}
35c08157 10046
1c8f6a4d 10047/* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
35c08157 10048
1c8f6a4d
KLC
10049static bfd_boolean
10050nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10051 Elf_Internal_Rela *internal_relocs, int *insn_len,
10052 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10053 Elf_Internal_Shdr *symtab_hdr)
10054{
10055 /* There are 3 variations for LONGJUMP1
10056 case 4-4-2; 16-bit bit on, optimize off or optimize for space
07d6d2b8
AM
10057 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10058 ori ta, ta, lo12(symbol) ; LO12S0
10059 jr5 ta ;
1c8f6a4d
KLC
10060
10061 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
10062 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10063 ori ta, ta, lo12(symbol) ; LO12S0
10064 jr ta ;
1c8f6a4d
KLC
10065
10066 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
10067 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10068 ori ta, ta, lo12(symbol) ; LO12S0
10069 jr ta ; */
1c8f6a4d
KLC
10070
10071 /* Get the reloc for the address from which the register is
10072 being loaded. This reloc will tell us which function is
10073 actually being called. */
10074
10075 bfd_vma laddr;
10076 int seq_len; /* Original length of instruction sequence. */
10077 int insn16_on; /* 16-bit on/off. */
10078 uint32_t insn;
10079 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d
KLC
10080 bfd_signed_vma foff;
10081 uint16_t insn16;
10082 unsigned long reloc;
35c08157 10083
1c8f6a4d
KLC
10084 irelend = internal_relocs + sec->reloc_count;
10085 seq_len = GET_SEQ_LEN (irel->r_addend);
10086 laddr = irel->r_offset;
10087 *insn_len = seq_len;
10088 insn16_on = IS_16BIT_ON (irel->r_addend);
10089
10090 hi_irelfn =
10091 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10092 R_NDS32_HI20_RELA, laddr);
10093 lo_irelfn =
10094 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10095 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10096 if (hi_irelfn == irelend || lo_irelfn == irelend)
10097 {
695344c0 10098 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
2dcf00ce 10099 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10100 return FALSE;
10101 }
35c08157 10102
1c8f6a4d 10103 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10104 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10105
fbaf61ad
NC
10106 if (foff == 0
10107 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10108 || foff < -CONSERVATIVE_24BIT_S1)
10109 return FALSE;
35c08157 10110
fbaf61ad
NC
10111 if (insn16_on
10112 && foff >= -ACCURATE_8BIT_S1
10113 && foff < ACCURATE_8BIT_S1
10114 && (seq_len & 0x2))
1c8f6a4d
KLC
10115 {
10116 /* j8 label */
10117 /* 16-bit on, but not optimized for speed. */
10118 reloc = R_NDS32_9_PCREL_RELA;
10119 insn16 = INSN_J8;
10120 bfd_putb16 (insn16, contents + irel->r_offset);
10121 *insn_len = 2;
10122 irel->r_info =
10123 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10124 }
10125 else
10126 {
10127 /* j label */
10128 reloc = R_NDS32_25_PCREL_RELA;
10129 insn = INSN_J;
10130 bfd_putb32 (insn, contents + irel->r_offset);
10131 *insn_len = 4;
10132 irel->r_info =
10133 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10134 irel->r_addend = 0;
10135 }
35c08157 10136
1c8f6a4d
KLC
10137 hi_irelfn->r_info =
10138 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10139 lo_irelfn->r_info =
10140 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10141
1c8f6a4d
KLC
10142 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10143 {
10144 insn16 = NDS32_NOP16;
10145 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10146 lo_irelfn->r_info =
10147 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10148 R_NDS32_INSN16);
10149 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10150 *insn_len += 2;
10151 }
10152 return TRUE;
10153}
10154
10155/* Revert condition branch. This function does not check if the input
10156 instruction is condition branch or not. */
10157
10158static void
10159nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10160 uint16_t *re_insn16, uint32_t *re_insn)
10161{
10162 uint32_t comp_insn = 0;
10163 uint16_t comp_insn16 = 0;
10164
10165 if (insn)
10166 {
10167 if (N32_OP6 (insn) == N32_OP6_BR1)
10168 {
10169 /* beqs label. */
10170 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10171 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
35c08157 10172 {
1c8f6a4d
KLC
10173 /* Insn can be contracted to 16-bit implied r5. */
10174 comp_insn16 =
10175 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10176 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
35c08157 10177 }
1c8f6a4d
KLC
10178 }
10179 else if (N32_OP6 (insn) == N32_OP6_BR3)
10180 {
10181 /* bnec $ta, imm11, label. */
10182 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10183 }
10184 else
10185 {
10186 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10187 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10188 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
35c08157 10189 {
1c8f6a4d 10190 if (N32_IS_RT3 (insn))
35c08157 10191 {
1c8f6a4d
KLC
10192 /* Insn can be contracted to 16-bit. */
10193 comp_insn16 =
10194 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10195 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10196 }
10197 else if (N32_RT5 (insn) == REG_R15)
10198 {
10199 /* Insn can be contracted to 16-bit. */
10200 comp_insn16 =
10201 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
35c08157 10202 }
35c08157
KLC
10203 }
10204 }
1c8f6a4d
KLC
10205 }
10206 else
10207 {
10208 switch ((insn16 & 0xf000) >> 12)
35c08157 10209 {
1c8f6a4d
KLC
10210 case 0xc:
10211 /* beqz38 or bnez38 */
10212 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10213 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10214 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10215 break;
35c08157 10216
1c8f6a4d
KLC
10217 case 0xd:
10218 /* beqs38 or bnes38 */
10219 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10220 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10221 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10222 | (REG_R5 << 15);
10223 break;
35c08157 10224
1c8f6a4d
KLC
10225 case 0xe:
10226 /* beqzS8 or bnezS8 */
10227 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10228 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10229 comp_insn |= REG_R15 << 20;
10230 break;
35c08157 10231
1c8f6a4d
KLC
10232 default:
10233 break;
10234 }
10235 }
10236 if (comp_insn && re_insn)
10237 *re_insn = comp_insn;
10238 if (comp_insn16 && re_insn16)
10239 *re_insn16 = comp_insn16;
10240}
35c08157 10241
1c8f6a4d 10242/* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
35c08157 10243
1c8f6a4d
KLC
10244static bfd_boolean
10245nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10246 Elf_Internal_Rela *internal_relocs, int *insn_len,
10247 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10248 Elf_Internal_Shdr *symtab_hdr)
10249{
10250 /* There are 3 variations for LONGJUMP2
10251 case 2-4; 1st insn convertible, 16-bit on,
10252 optimize off or optimize for space
10253 bnes38 rt, ra, $1 ; LONGJUMP2
10254 j label ; 25_PCREL
10255 $1:
10256
10257 case 4-4; 1st insn not convertible
10258 bne rt, ra, $1 ; LONGJUMP2
10259 j label ; 25_PCREL
10260 $1:
10261
10262 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10263 bne rt, ra, $1 ; LONGJUMP2
10264 j label ; 25_PCREL
10265 $1: */
10266
10267 /* Get the reloc for the address from which the register is
10268 being loaded. This reloc will tell us which function is
10269 actually being called. */
10270
10271 bfd_vma laddr;
10272 int seq_len; /* Original length of instruction sequence. */
10273 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10274 int first_size;
1c8f6a4d
KLC
10275 unsigned int i;
10276 bfd_signed_vma foff;
10277 uint32_t insn, re_insn = 0;
10278 uint16_t insn16, re_insn16 = 0;
10279 unsigned long reloc, cond_reloc;
35c08157 10280
1c8f6a4d
KLC
10281 enum elf_nds32_reloc_type checked_types[] =
10282 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
35c08157 10283
1c8f6a4d
KLC
10284 irelend = internal_relocs + sec->reloc_count;
10285 seq_len = GET_SEQ_LEN (irel->r_addend);
10286 laddr = irel->r_offset;
10287 *insn_len = seq_len;
10288 first_size = (seq_len == 6) ? 2 : 4;
10289
10290 i2_irelfn =
10291 find_relocs_at_address_addr (irel, internal_relocs,
10292 irelend, R_NDS32_25_PCREL_RELA,
10293 laddr + first_size);
10294
fbaf61ad 10295 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10296 {
10297 cond_irelfn =
10298 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10299 checked_types[i], laddr);
10300 if (cond_irelfn != irelend)
10301 break;
10302 }
35c08157 10303
1c8f6a4d
KLC
10304 if (i2_irelfn == irelend || cond_irelfn == irelend)
10305 {
695344c0 10306 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
2dcf00ce 10307 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10308 return FALSE;
10309 }
35c08157 10310
1c8f6a4d 10311 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad
NC
10312 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10313 if (foff == 0
10314 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10315 || foff >= CONSERVATIVE_16BIT_S1)
10316 return FALSE;
35c08157 10317
1c8f6a4d
KLC
10318 /* Get the all corresponding instructions. */
10319 if (first_size == 4)
10320 {
10321 insn = bfd_getb32 (contents + laddr);
10322 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10323 }
10324 else
10325 {
10326 insn16 = bfd_getb16 (contents + laddr);
10327 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10328 }
35c08157 10329
1c8f6a4d
KLC
10330 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10331 && foff < ACCURATE_8BIT_S1 - first_size)
10332 {
10333 if (first_size == 4)
10334 {
10335 /* Don't convert it to 16-bit now, keep this as relaxable for
10336 ``label reloc; INSN16''. */
35c08157 10337
1c8f6a4d
KLC
10338 /* Save comp_insn32 to buffer. */
10339 bfd_putb32 (re_insn, contents + irel->r_offset);
10340 *insn_len = 4;
10341 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10342 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10343 cond_reloc = R_NDS32_INSN16;
10344 }
10345 else
10346 {
10347 bfd_putb16 (re_insn16, contents + irel->r_offset);
10348 *insn_len = 2;
10349 reloc = R_NDS32_9_PCREL_RELA;
10350 cond_reloc = R_NDS32_NONE;
10351 }
10352 }
10353 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10354 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10355 && foff < ACCURATE_14BIT_S1 - first_size))
10356 {
10357 /* beqs label ; 15_PCREL */
10358 bfd_putb32 (re_insn, contents + irel->r_offset);
10359 *insn_len = 4;
10360 reloc = R_NDS32_15_PCREL_RELA;
10361 cond_reloc = R_NDS32_NONE;
10362 }
10363 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10364 && foff >= -CONSERVATIVE_16BIT_S1
10365 && foff < CONSERVATIVE_16BIT_S1)
10366 {
10367 /* beqz label ; 17_PCREL */
10368 bfd_putb32 (re_insn, contents + irel->r_offset);
10369 *insn_len = 4;
10370 reloc = R_NDS32_17_PCREL_RELA;
10371 cond_reloc = R_NDS32_NONE;
10372 }
10373 else
10374 return FALSE;
35c08157 10375
1c8f6a4d
KLC
10376 /* Set all relocations. */
10377 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10378 irel->r_addend = i2_irelfn->r_addend;
35c08157 10379
1c8f6a4d
KLC
10380 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10381 cond_reloc);
10382 cond_irelfn->r_addend = 0;
35c08157 10383
1c8f6a4d
KLC
10384 if ((seq_len ^ *insn_len ) & 0x2)
10385 {
10386 insn16 = NDS32_NOP16;
10387 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10388 i2_irelfn->r_offset = 4;
10389 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10390 R_NDS32_INSN16);
10391 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10392 *insn_len += 2;
10393 }
10394 else
10395 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10396 R_NDS32_NONE);
10397 return TRUE;
10398}
35c08157 10399
1c8f6a4d 10400/* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
35c08157 10401
1c8f6a4d
KLC
10402static bfd_boolean
10403nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10404 Elf_Internal_Rela *internal_relocs, int *insn_len,
10405 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10406 Elf_Internal_Shdr *symtab_hdr)
10407{
10408 /* There are 5 variations for LONGJUMP3
10409 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10410 optimize off or optimize for space
07d6d2b8
AM
10411 bnes38 rt, ra, $1 ; LONGJUMP3
10412 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10413 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10414 jr5 ta ;
10415 $1: ;
1c8f6a4d
KLC
10416
10417 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
07d6d2b8
AM
10418 bnes38 rt, ra, $1 ; LONGJUMP3
10419 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10420 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10421 jr5 ta ;
10422 $1: ; LABEL
1c8f6a4d
KLC
10423
10424 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10425 optimize off or optimize for space
07d6d2b8
AM
10426 bne rt, ra, $1 ; LONGJUMP3
10427 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10428 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10429 jr5 ta ;
10430 $1: ;
1c8f6a4d
KLC
10431
10432 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10433 16-bit off if no INSN16
07d6d2b8
AM
10434 bne rt, ra, $1 ; LONGJUMP3
10435 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10436 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10437 jr ta ;
10438 $1: ;
1c8f6a4d
KLC
10439
10440 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10441 16-bit off if no INSN16
07d6d2b8
AM
10442 bne rt, ra, $1 ; LONGJUMP3
10443 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10444 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10445 jr ta ;
10446 $1: ; LABEL */
1c8f6a4d
KLC
10447
10448 /* Get the reloc for the address from which the register is
10449 being loaded. This reloc will tell us which function is
10450 actually being called. */
10451 enum elf_nds32_reloc_type checked_types[] =
10452 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10453
10454 int reloc_off = 0, cond_removed = 0, convertible;
10455 bfd_vma laddr;
10456 int seq_len; /* Original length of instruction sequence. */
10457 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10458 int first_size;
1c8f6a4d
KLC
10459 unsigned int i;
10460 bfd_signed_vma foff;
10461 uint32_t insn, re_insn = 0;
10462 uint16_t insn16, re_insn16 = 0;
10463 unsigned long reloc, cond_reloc;
35c08157 10464
1c8f6a4d
KLC
10465 irelend = internal_relocs + sec->reloc_count;
10466 seq_len = GET_SEQ_LEN (irel->r_addend);
10467 laddr = irel->r_offset;
10468 *insn_len = seq_len;
35c08157 10469
1c8f6a4d 10470 convertible = IS_1ST_CONVERT (irel->r_addend);
35c08157 10471
1c8f6a4d
KLC
10472 if (convertible)
10473 first_size = 2;
10474 else
10475 first_size = 4;
10476
10477 /* Get all needed relocations. */
10478 hi_irelfn =
10479 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10480 R_NDS32_HI20_RELA, laddr + first_size);
10481 lo_irelfn =
10482 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10483 R_NDS32_LO12S0_ORI_RELA,
10484 laddr + first_size + 4);
10485
fbaf61ad 10486 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10487 {
10488 cond_irelfn =
10489 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10490 checked_types[i], laddr);
10491 if (cond_irelfn != irelend)
10492 break;
10493 }
35c08157 10494
fbaf61ad
NC
10495 if (hi_irelfn == irelend
10496 || lo_irelfn == irelend
10497 || cond_irelfn == irelend)
1c8f6a4d 10498 {
695344c0 10499 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
2dcf00ce 10500 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10501 return FALSE;
10502 }
35c08157 10503
1c8f6a4d 10504 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10505 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10506
fbaf61ad
NC
10507 if (foff == 0
10508 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10509 || foff >= CONSERVATIVE_24BIT_S1)
10510 return FALSE;
35c08157 10511
1c8f6a4d
KLC
10512 /* Get the all corresponding instructions. */
10513 if (first_size == 4)
10514 {
10515 insn = bfd_getb32 (contents + laddr);
10516 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10517 }
10518 else
10519 {
10520 insn16 = bfd_getb16 (contents + laddr);
10521 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10522 }
35c08157 10523
1c8f6a4d
KLC
10524 /* For simplicity of coding, we are going to modify the section
10525 contents, the section relocs, and the BFD symbol table. We
10526 must tell the rest of the code not to free up this
10527 information. It would be possible to instead create a table
10528 of changes which have to be made, as is done in coff-mips.c;
10529 that would be more work, but would require less memory when
10530 the linker is run. */
35c08157 10531
fbaf61ad
NC
10532 if (re_insn16
10533 && foff >= -ACCURATE_8BIT_S1 - first_size
1c8f6a4d
KLC
10534 && foff < ACCURATE_8BIT_S1 - first_size)
10535 {
10536 if (!(seq_len & 0x2))
10537 {
10538 /* Don't convert it to 16-bit now, keep this as relaxable
10539 for ``label reloc; INSN1a''6. */
10540 /* Save comp_insn32 to buffer. */
10541 bfd_putb32 (re_insn, contents + irel->r_offset);
10542 *insn_len = 4;
10543 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10544 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10545 cond_reloc = R_NDS32_INSN16;
10546 }
10547 else
10548 {
10549 /* Not optimize for speed; convert sequence to 16-bit. */
10550 /* Save comp_insn16 to buffer. */
10551 bfd_putb16 (re_insn16, contents + irel->r_offset);
10552 *insn_len = 2;
10553 reloc = R_NDS32_9_PCREL_RELA;
10554 cond_reloc = R_NDS32_NONE;
10555 }
10556 cond_removed = 1;
10557 }
10558 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10559 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10560 && foff < ACCURATE_14BIT_S1 - first_size))
10561 {
10562 /* beqs label ; 15_PCREL */
10563 bfd_putb32 (re_insn, contents + irel->r_offset);
10564 *insn_len = 4;
10565 reloc = R_NDS32_15_PCREL_RELA;
10566 cond_reloc = R_NDS32_NONE;
10567 cond_removed = 1;
10568 }
10569 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10570 && foff >= -CONSERVATIVE_16BIT_S1
10571 && foff < CONSERVATIVE_16BIT_S1)
10572 {
10573 /* beqz label ; 17_PCREL */
10574 bfd_putb32 (re_insn, contents + irel->r_offset);
10575 *insn_len = 4;
10576 reloc = R_NDS32_17_PCREL_RELA;
10577 cond_reloc = R_NDS32_NONE;
10578 cond_removed = 1;
10579 }
10580 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10581 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10582 {
10583 /* Relax to one of the following 3 variations
10584
10585 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10586 for space
10587 bnes38 rt, $1 ; LONGJUMP2
10588 j label ; 25_PCREL
10589 $1
10590
10591 case 4-4; 1st insn not convertible, others don't care
10592 bne rt, ra, $1 ; LONGJUMP2
10593 j label ; 25_PCREL
10594 $1
10595
10596 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10597 bne rt, ra, $1 ; LONGJUMP2
10598 j label ; 25_PCREL
10599 $1 */
10600
10601 /* Offset for first instruction. */
10602
10603 /* Use j label as second instruction. */
10604 *insn_len = 4 + first_size;
10605 insn = INSN_J;
10606 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10607 reloc = R_NDS32_LONGJUMP2;
10608 cond_reloc = R_NDS32_25_PLTREL;
10609 }
10610 else
10611 return FALSE;
35c08157 10612
1c8f6a4d
KLC
10613 if (cond_removed == 1)
10614 {
10615 /* Set all relocations. */
10616 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10617 irel->r_addend = hi_irelfn->r_addend;
10618
10619 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10620 cond_reloc);
10621 cond_irelfn->r_addend = 0;
10622 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10623 R_NDS32_NONE);
10624 }
10625 else
10626 {
10627 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
fbaf61ad 10628 irel->r_addend = irel->r_addend;
1c8f6a4d
KLC
10629 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10630 cond_reloc);
10631 }
35c08157 10632
1c8f6a4d
KLC
10633 if ((seq_len ^ *insn_len ) & 0x2)
10634 {
10635 insn16 = NDS32_NOP16;
10636 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10637 lo_irelfn->r_offset = *insn_len;
10638 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10639 R_NDS32_INSN16);
10640 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10641 *insn_len += 2;
10642 }
10643 else
10644 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10645 R_NDS32_NONE);
10646 return TRUE;
10647}
35c08157 10648
1c8f6a4d 10649/* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
35c08157 10650
1c8f6a4d
KLC
10651static bfd_boolean
10652nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10653 Elf_Internal_Rela *internal_relocs, int *insn_len,
10654 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10655 Elf_Internal_Shdr *symtab_hdr)
10656{
10657 /* The pattern for LONGCALL4. Support for function cse.
10658 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10659 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10660 jral ta ; PTR_RES/EMPTY/INSN16 */
35c08157 10661
1c8f6a4d
KLC
10662 bfd_vma laddr;
10663 uint32_t insn;
10664 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10665 Elf_Internal_Rela *irelend;
1c8f6a4d 10666 bfd_signed_vma foff;
35c08157 10667
1c8f6a4d
KLC
10668 irelend = internal_relocs + sec->reloc_count;
10669 laddr = irel->r_offset;
35c08157 10670
1c8f6a4d
KLC
10671 /* Get the reloc for the address from which the register is
10672 being loaded. This reloc will tell us which function is
10673 actually being called. */
10674 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10675 R_NDS32_HI20_RELA, laddr);
35c08157 10676
1c8f6a4d
KLC
10677 if (hi_irel == irelend)
10678 {
695344c0 10679 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10680 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10681 return FALSE;
10682 }
35c08157 10683
1c8f6a4d 10684 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10685 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10686
1c8f6a4d 10687 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
10688 if (foff == 0
10689 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10690 || foff >= CONSERVATIVE_24BIT_S1)
10691 return FALSE;
35c08157 10692
fbaf61ad 10693 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
10694 /* For simplicity of coding, we are going to modify the section
10695 contents, the section relocs, and the BFD symbol table. We
10696 must tell the rest of the code not to free up this
10697 information. It would be possible to instead create a table
10698 of changes which have to be made, as is done in coff-mips.c;
10699 that would be more work, but would require less memory when
10700 the linker is run. */
35c08157 10701
1c8f6a4d
KLC
10702 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10703 R_NDS32_PTR_RESOLVED, irel->r_addend);
10704 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10705 R_NDS32_EMPTY, irel->r_addend);
35c08157 10706
1c8f6a4d
KLC
10707 if (ptr_irel == irelend || em_irel == irelend)
10708 {
695344c0 10709 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10710 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10711 return FALSE;
10712 }
10713 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10714 insn = bfd_getb32 (contents + irel->r_addend);
10715 if (insn & 0x80000000)
10716 return FALSE;
35c08157 10717
1c8f6a4d
KLC
10718 /* Replace the long call with a jal. */
10719 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10720 R_NDS32_25_PCREL_RELA);
10721 ptr_irel->r_addend = 1;
35c08157 10722
1c8f6a4d
KLC
10723 /* We don't resolve this here but resolve it in relocate_section. */
10724 insn = INSN_JAL;
10725 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10726
1c8f6a4d
KLC
10727 irel->r_info =
10728 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10729
1c8f6a4d
KLC
10730 /* If there is function cse, HI20 can not remove now. */
10731 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10732 R_NDS32_LONGCALL4, laddr);
10733 if (call_irel == irelend)
10734 {
10735 *insn_len = 0;
10736 hi_irel->r_info =
10737 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10738 }
35c08157 10739
1c8f6a4d
KLC
10740 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10741 R_NDS32_INSN16, irel->r_addend);
10742 if (insn_irel != irelend)
10743 insn_irel->r_info =
10744 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10745
1c8f6a4d
KLC
10746 return TRUE;
10747}
35c08157 10748
1c8f6a4d 10749/* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
35c08157 10750
1c8f6a4d
KLC
10751static bfd_boolean
10752nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10753 Elf_Internal_Rela *internal_relocs, int *insn_len,
10754 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10755 Elf_Internal_Shdr *symtab_hdr)
10756{
10757 /* The pattern for LONGCALL5.
10758 bltz rt, .L1 ; LONGCALL5/17_PCREL
10759 jal symbol ; 25_PCREL
10760 .L1: */
35c08157 10761
1c8f6a4d
KLC
10762 bfd_vma laddr;
10763 uint32_t insn;
10764 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d 10765 bfd_signed_vma foff;
35c08157 10766
1c8f6a4d
KLC
10767 irelend = internal_relocs + sec->reloc_count;
10768 laddr = irel->r_offset;
10769 insn = bfd_getb32 (contents + laddr);
35c08157 10770
1c8f6a4d
KLC
10771 /* Get the reloc for the address from which the register is
10772 being loaded. This reloc will tell us which function is
10773 actually being called. */
10774 cond_irel =
10775 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10776 R_NDS32_25_PCREL_RELA, irel->r_addend);
10777 if (cond_irel == irelend)
10778 {
695344c0 10779 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
2dcf00ce 10780 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10781 return FALSE;
10782 }
35c08157 10783
1c8f6a4d 10784 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10785 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 10786
fbaf61ad
NC
10787 if (foff == 0
10788 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10789 || foff >= CONSERVATIVE_16BIT_S1)
10790 return FALSE;
35c08157 10791
1c8f6a4d 10792 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10793 or bltzal rt, label ; 17_PCREL. */
35c08157 10794
1c8f6a4d
KLC
10795 /* Convert to complimentary conditional call. */
10796 insn = CONVERT_CONDITION_CALL (insn);
35c08157 10797
1c8f6a4d
KLC
10798 /* For simplicity of coding, we are going to modify the section
10799 contents, the section relocs, and the BFD symbol table. We
10800 must tell the rest of the code not to free up this
10801 information. It would be possible to instead create a table
10802 of changes which have to be made, as is done in coff-mips.c;
10803 that would be more work, but would require less memory when
10804 the linker is run. */
35c08157 10805
1c8f6a4d
KLC
10806 /* Modify relocation and contents. */
10807 cond_irel->r_info =
10808 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
35c08157 10809
1c8f6a4d
KLC
10810 /* Replace the long call with a bgezal. */
10811 bfd_putb32 (insn, contents + cond_irel->r_offset);
10812 *insn_len = 0;
35c08157 10813
1c8f6a4d
KLC
10814 /* Clean unnessary relocations. */
10815 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10816
1c8f6a4d
KLC
10817 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10818 R_NDS32_17_PCREL_RELA, laddr);
10819 cond_irel->r_info =
10820 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10821
1c8f6a4d
KLC
10822 return TRUE;
10823}
35c08157 10824
1c8f6a4d 10825/* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
35c08157 10826
1c8f6a4d
KLC
10827static bfd_boolean
10828nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10829 Elf_Internal_Rela *internal_relocs, int *insn_len,
10830 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10831 Elf_Internal_Shdr *symtab_hdr)
10832{
10833 /* The pattern for LONGCALL6.
10834 bltz rt, .L1 ; LONGCALL6/17_PCREL
10835 sethi ta, hi20(symbol) ; HI20/PTR
10836 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10837 jral ta ; PTR_RES/EMPTY/INSN16
10838 .L1 */
10839
10840 bfd_vma laddr;
10841 uint32_t insn;
10842 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
1c8f6a4d 10843 bfd_signed_vma foff;
35c08157 10844
1c8f6a4d
KLC
10845 irelend = internal_relocs + sec->reloc_count;
10846 laddr = irel->r_offset;
35c08157 10847
1c8f6a4d
KLC
10848 /* Get the reloc for the address from which the register is
10849 being loaded. This reloc will tell us which function is
10850 actually being called. */
10851 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10852 R_NDS32_EMPTY, irel->r_addend);
35c08157 10853
1c8f6a4d
KLC
10854 if (em_irel == irelend)
10855 {
695344c0 10856 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
2dcf00ce 10857 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10858 return FALSE;
10859 }
35c08157 10860
1c8f6a4d 10861 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10862 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 10863
fbaf61ad
NC
10864 if (foff == 0
10865 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10866 || foff >= CONSERVATIVE_24BIT_S1)
10867 return FALSE;
35c08157 10868
1c8f6a4d
KLC
10869 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10870 insn = bfd_getb32 (contents + irel->r_addend);
10871 if (insn & 0x80000000)
10872 return FALSE;
35c08157 10873
1c8f6a4d
KLC
10874 insn = bfd_getb32 (contents + laddr);
10875 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10876 {
10877 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10878 or bltzal rt, label ; 17_PCREL. */
35c08157 10879
1c8f6a4d
KLC
10880 /* Convert to complimentary conditional call. */
10881 *insn_len = 0;
10882 insn = CONVERT_CONDITION_CALL (insn);
10883 bfd_putb32 (insn, contents + em_irel->r_offset);
10884
10885 em_irel->r_info =
10886 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10887
10888 /* Set resolved relocation. */
10889 cond_irel =
10890 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10891 R_NDS32_PTR_RESOLVED, irel->r_addend);
10892 if (cond_irel == irelend)
35c08157 10893 {
695344c0 10894 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10895 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10896 return FALSE;
10897 }
10898 cond_irel->r_addend = 1;
35c08157 10899
1c8f6a4d 10900 /* Clear relocations. */
35c08157 10901
1c8f6a4d
KLC
10902 irel->r_info =
10903 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10904
1c8f6a4d
KLC
10905 cond_irel =
10906 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10907 R_NDS32_17_PCREL_RELA, laddr);
10908 if (cond_irel != irelend)
10909 cond_irel->r_info =
10910 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10911
1c8f6a4d
KLC
10912 cond_irel =
10913 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10914 R_NDS32_INSN16, irel->r_addend);
10915 if (cond_irel != irelend)
10916 cond_irel->r_info =
10917 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10918
1c8f6a4d
KLC
10919 }
10920 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10921 {
10922 /* Relax to the following instruction sequence
10923 bltz rt, .L1 ; LONGCALL2/17_PCREL
10924 jal symbol ; 25_PCREL/PTR_RES
10925 .L1 */
10926 *insn_len = 4;
10927 /* Convert instruction. */
10928 insn = INSN_JAL;
10929 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10930
1c8f6a4d
KLC
10931 /* Convert relocations. */
10932 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10933 R_NDS32_25_PCREL_RELA);
10934 irel->r_info =
10935 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
35c08157 10936
1c8f6a4d
KLC
10937 /* Set resolved relocation. */
10938 cond_irel =
10939 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10940 R_NDS32_PTR_RESOLVED, irel->r_addend);
10941 if (cond_irel == irelend)
10942 {
695344c0 10943 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10944 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10945 return FALSE;
10946 }
10947 cond_irel->r_addend = 1;
35c08157 10948
1c8f6a4d
KLC
10949 cond_irel =
10950 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10951 R_NDS32_INSN16, irel->r_addend);
10952 if (cond_irel != irelend)
10953 cond_irel->r_info =
10954 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10955 }
10956 return TRUE;
10957}
35c08157 10958
1c8f6a4d 10959/* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
35c08157 10960
1c8f6a4d
KLC
10961static bfd_boolean
10962nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10963 Elf_Internal_Rela *internal_relocs, int *insn_len,
10964 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10965 Elf_Internal_Shdr *symtab_hdr)
10966{
10967 /* The pattern for LONGJUMP4.
10968 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10969 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10970 jr ta ; PTR_RES/INSN16/EMPTY */
10971
10972 bfd_vma laddr;
10973 int seq_len; /* Original length of instruction sequence. */
10974 uint32_t insn;
10975 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
1c8f6a4d 10976 bfd_signed_vma foff;
35c08157 10977
1c8f6a4d
KLC
10978 irelend = internal_relocs + sec->reloc_count;
10979 seq_len = GET_SEQ_LEN (irel->r_addend);
10980 laddr = irel->r_offset;
10981 *insn_len = seq_len;
35c08157 10982
1c8f6a4d
KLC
10983 /* Get the reloc for the address from which the register is
10984 being loaded. This reloc will tell us which function is
10985 actually being called. */
35c08157 10986
1c8f6a4d
KLC
10987 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10988 R_NDS32_HI20_RELA, laddr);
35c08157 10989
1c8f6a4d
KLC
10990 if (hi_irel == irelend)
10991 {
695344c0 10992 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 10993 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10994 return FALSE;
10995 }
35c08157 10996
1c8f6a4d 10997 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10998 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10999
fbaf61ad
NC
11000 if (foff == 0
11001 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
11002 || foff < -CONSERVATIVE_24BIT_S1)
11003 return FALSE;
35c08157 11004
1c8f6a4d
KLC
11005 /* Convert it to "j label", it may be converted to j8 in the final
11006 pass of relaxation. Therefore, we do not consider this currently. */
11007 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11008 R_NDS32_PTR_RESOLVED, irel->r_addend);
11009 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11010 R_NDS32_EMPTY, irel->r_addend);
35c08157 11011
1c8f6a4d
KLC
11012 if (ptr_irel == irelend || em_irel == irelend)
11013 {
695344c0 11014 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 11015 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11016 return FALSE;
11017 }
35c08157 11018
1c8f6a4d
KLC
11019 em_irel->r_info =
11020 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11021 ptr_irel->r_addend = 1;
35c08157 11022
1c8f6a4d
KLC
11023 /* Write instruction. */
11024 insn = INSN_J;
11025 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 11026
1c8f6a4d
KLC
11027 /* Clear relocations. */
11028 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11029
1c8f6a4d
KLC
11030 /* If there is function cse, HI20 can not remove now. */
11031 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11032 R_NDS32_LONGJUMP4, laddr);
11033 if (call_irel == irelend)
11034 {
11035 *insn_len = 0;
11036 hi_irel->r_info =
11037 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11038 }
35c08157 11039
1c8f6a4d
KLC
11040 return TRUE;
11041}
35c08157 11042
1c8f6a4d 11043/* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
35c08157 11044
1c8f6a4d
KLC
11045static bfd_boolean
11046nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11047 Elf_Internal_Rela *internal_relocs, int *insn_len,
11048 int *seq_len, bfd_byte *contents,
11049 Elf_Internal_Sym *isymbuf,
11050 Elf_Internal_Shdr *symtab_hdr)
11051{
11052 /* There are 2 variations for LONGJUMP5
11053 case 2-4; 1st insn convertible, 16-bit on.
11054 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11055 j label ; 25_PCREL/INSN16
11056 $1:
11057
11058 case 4-4; 1st insn not convertible
11059 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11060 j label ; 25_PCREL/INSN16
11061 .L1: */
11062
11063 bfd_vma laddr;
11064 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d
KLC
11065 unsigned int i;
11066 bfd_signed_vma foff;
11067 uint32_t insn, re_insn = 0;
11068 uint16_t insn16, re_insn16 = 0;
11069 unsigned long reloc;
35c08157 11070
1c8f6a4d
KLC
11071 enum elf_nds32_reloc_type checked_types[] =
11072 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11073 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
35c08157 11074
1c8f6a4d
KLC
11075 irelend = internal_relocs + sec->reloc_count;
11076 laddr = irel->r_offset;
35c08157 11077
1c8f6a4d
KLC
11078 /* Get the reloc for the address from which the register is
11079 being loaded. This reloc will tell us which function is
11080 actually being called. */
35c08157 11081
1c8f6a4d
KLC
11082 cond_irel =
11083 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11084 R_NDS32_25_PCREL_RELA, irel->r_addend);
11085 if (cond_irel == irelend)
11086 {
695344c0 11087 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
2dcf00ce 11088 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11089 return FALSE;
11090 }
35c08157 11091
1c8f6a4d 11092 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11093 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11094
fbaf61ad
NC
11095 if (foff == 0
11096 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
11097 || foff >= CONSERVATIVE_16BIT_S1)
11098 return FALSE;
35c08157 11099
1c8f6a4d
KLC
11100 /* Get the all corresponding instructions. */
11101 insn = bfd_getb32 (contents + laddr);
11102 /* Check instruction size. */
11103 if (insn & 0x80000000)
11104 {
11105 *seq_len = 0;
11106 insn16 = insn >> 16;
11107 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11108 }
11109 else
11110 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11111
1c8f6a4d
KLC
11112 if (N32_OP6 (re_insn) == N32_OP6_BR1
11113 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11114 {
11115 /* beqs label ; 15_PCREL. */
11116 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11117 reloc = R_NDS32_15_PCREL_RELA;
11118 }
11119 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11120 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11121 {
11122 /* beqz label ; 17_PCREL. */
11123 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11124 reloc = R_NDS32_17_PCREL_RELA;
11125 }
11126 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11127 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11128 {
11129 /* beqc label ; 9_PCREL. */
11130 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11131 reloc = R_NDS32_WORD_9_PCREL_RELA;
11132 }
11133 else
11134 return FALSE;
35c08157 11135
1c8f6a4d
KLC
11136 /* Set all relocations. */
11137 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
35c08157 11138
1c8f6a4d
KLC
11139 /* Clean relocations. */
11140 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
fbaf61ad 11141 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11142 {
11143 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11144 checked_types[i], laddr);
11145 if (cond_irel != irelend)
11146 {
11147 if (*seq_len == 0
11148 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
35c08157 11149 {
1c8f6a4d
KLC
11150 /* If the branch instruction is 2 byte, it cannot remove
11151 directly. Only convert it to nop16 and remove it after
11152 checking alignment issue. */
11153 insn16 = NDS32_NOP16;
11154 bfd_putb16 (insn16, contents + laddr);
11155 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157
KLC
11156 }
11157 else
1c8f6a4d
KLC
11158 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11159 R_NDS32_NONE);
35c08157 11160 }
1c8f6a4d
KLC
11161 }
11162 *insn_len = 0;
35c08157 11163
1c8f6a4d
KLC
11164 return TRUE;
11165}
35c08157 11166
1c8f6a4d 11167/* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
35c08157 11168
1c8f6a4d
KLC
11169static bfd_boolean
11170nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11171 Elf_Internal_Rela *internal_relocs, int *insn_len,
11172 int *seq_len, bfd_byte *contents,
11173 Elf_Internal_Sym *isymbuf,
11174 Elf_Internal_Shdr *symtab_hdr)
11175{
11176 /* There are 5 variations for LONGJUMP6
11177 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11178 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11179 sethi ta, hi20(symbol) ; HI20/PTR
11180 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11181 jr ta ; PTR_RES/INSN16/EMPTY
11182 .L1:
11183
11184 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11185 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11186 sethi ta, hi20(symbol) ; HI20/PTR
11187 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11188 jr ta ; PTR_RES/INSN16/EMPTY
11189 .L1: */
11190
11191 enum elf_nds32_reloc_type checked_types[] =
11192 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11193 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11194
11195 int reloc_off = 0, cond_removed = 0;
11196 bfd_vma laddr;
11197 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11198 unsigned int i;
11199 bfd_signed_vma foff;
11200 uint32_t insn, re_insn = 0;
11201 uint16_t insn16, re_insn16 = 0;
11202 unsigned long reloc;
35c08157 11203
1c8f6a4d
KLC
11204 irelend = internal_relocs + sec->reloc_count;
11205 laddr = irel->r_offset;
35c08157 11206
1c8f6a4d
KLC
11207 /* Get the reloc for the address from which the register is
11208 being loaded. This reloc will tell us which function is
11209 actually being called. */
11210 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11211 R_NDS32_EMPTY, irel->r_addend);
35c08157 11212
1c8f6a4d
KLC
11213 if (em_irel == irelend)
11214 {
695344c0 11215 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
2dcf00ce 11216 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11217 return FALSE;
11218 }
35c08157 11219
1c8f6a4d 11220 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11221 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 11222
fbaf61ad
NC
11223 if (foff == 0
11224 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
11225 || foff >= CONSERVATIVE_24BIT_S1)
11226 return FALSE;
35c08157 11227
1c8f6a4d
KLC
11228 insn = bfd_getb32 (contents + laddr);
11229 /* Check instruction size. */
11230 if (insn & 0x80000000)
11231 {
11232 *seq_len = 0;
11233 insn16 = insn >> 16;
11234 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11235 }
11236 else
11237 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11238
1c8f6a4d
KLC
11239 /* For simplicity of coding, we are going to modify the section
11240 contents, the section relocs, and the BFD symbol table. We
11241 must tell the rest of the code not to free up this
11242 information. It would be possible to instead create a table
11243 of changes which have to be made, as is done in coff-mips.c;
11244 that would be more work, but would require less memory when
11245 the linker is run. */
35c08157 11246
1c8f6a4d
KLC
11247 if (N32_OP6 (re_insn) == N32_OP6_BR1
11248 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11249 {
fbaf61ad 11250 /* beqs label ; 15_PCREL. */
1c8f6a4d
KLC
11251 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11252 reloc = R_NDS32_15_PCREL_RELA;
11253 cond_removed = 1;
11254 }
11255 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11256 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11257 {
fbaf61ad 11258 /* beqz label ; 17_PCREL. */
1c8f6a4d
KLC
11259 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11260 reloc = R_NDS32_17_PCREL_RELA;
11261 cond_removed = 1;
11262 }
11263 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11264 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11265 {
11266 /* Relax to one of the following 2 variations
35c08157 11267
1c8f6a4d
KLC
11268 case 2-4; 1st insn convertible, 16-bit on.
11269 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11270 j label ; 25_PCREL/INSN16
11271 $1:
35c08157 11272
1c8f6a4d
KLC
11273 case 4-4; 1st insn not convertible
11274 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11275 j label ; 25_PCREL/INSN16
11276 .L1: */
35c08157 11277
1c8f6a4d
KLC
11278 /* Use j label as second instruction. */
11279 insn = INSN_J;
11280 reloc = R_NDS32_25_PCREL_RELA;
11281 bfd_putb32 (insn, contents + em_irel->r_offset);
11282 }
11283 else
11284 return FALSE;
35c08157 11285
1c8f6a4d
KLC
11286 /* Set all relocations. */
11287 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
35c08157 11288
1c8f6a4d
KLC
11289 cond_irel =
11290 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11291 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11292 cond_irel->r_addend = 1;
35c08157 11293
1c8f6a4d
KLC
11294 /* Use INSN16 of first branch instruction to distinguish if keeping
11295 INSN16 of final instruction or not. */
11296 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11297 R_NDS32_INSN16, irel->r_offset);
11298 if (insn_irel == irelend)
11299 {
11300 /* Clean the final INSN16. */
11301 insn_irel =
11302 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11303 R_NDS32_INSN16, em_irel->r_offset);
11304 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11305 R_NDS32_NONE);
11306 }
11307
11308 if (cond_removed == 1)
11309 {
11310 *insn_len = 0;
11311
11312 /* Clear relocations. */
11313 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11314
fbaf61ad 11315 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11316 {
11317 cond_irel =
11318 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11319 checked_types[i], laddr);
11320 if (cond_irel != irelend)
35c08157 11321 {
1c8f6a4d
KLC
11322 if (*seq_len == 0
11323 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11324 {
11325 /* If the branch instruction is 2 byte, it cannot remove
11326 directly. Only convert it to nop16 and remove it after
11327 checking alignment issue. */
11328 insn16 = NDS32_NOP16;
11329 bfd_putb16 (insn16, contents + laddr);
11330 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11331 }
11332 else
11333 cond_irel->r_info =
11334 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 11335 }
35c08157 11336 }
1c8f6a4d
KLC
11337 }
11338 else
11339 {
11340 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11341 R_NDS32_LONGJUMP5);
11342 }
35c08157 11343
1c8f6a4d
KLC
11344 return TRUE;
11345}
35c08157 11346
1c8f6a4d 11347/* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
35c08157 11348
1c8f6a4d
KLC
11349static bfd_boolean
11350nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11351 Elf_Internal_Rela *internal_relocs, int *insn_len,
11352 int *seq_len, bfd_byte *contents,
11353 Elf_Internal_Sym *isymbuf,
11354 Elf_Internal_Shdr *symtab_hdr)
11355{
11356 /* There are 2 variations for LONGJUMP5
11357 case 2-4; 1st insn convertible, 16-bit on.
11358 movi55 ta, imm11 ; LONGJUMP7/INSN16
11359 beq rt, ta, label ; 15_PCREL
11360
11361 case 4-4; 1st insn not convertible
11362 movi55 ta, imm11 ; LONGJUMP7/INSN16
11363 beq rt, ta, label ; 15_PCREL */
11364
11365 bfd_vma laddr;
11366 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11367 bfd_signed_vma foff;
11368 uint32_t insn, re_insn = 0;
11369 uint16_t insn16;
11370 uint32_t imm11;
35c08157 11371
1c8f6a4d
KLC
11372 irelend = internal_relocs + sec->reloc_count;
11373 laddr = irel->r_offset;
35c08157 11374
1c8f6a4d
KLC
11375 /* Get the reloc for the address from which the register is
11376 being loaded. This reloc will tell us which function is
11377 actually being called. */
35c08157 11378
1c8f6a4d
KLC
11379 cond_irel =
11380 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11381 R_NDS32_15_PCREL_RELA, irel->r_addend);
11382 if (cond_irel == irelend)
11383 {
695344c0 11384 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
2dcf00ce 11385 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11386 return FALSE;
11387 }
35c08157 11388
1c8f6a4d 11389 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11390 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11391
fbaf61ad
NC
11392 if (foff == 0
11393 || foff < -CONSERVATIVE_8BIT_S1
1c8f6a4d
KLC
11394 || foff >= CONSERVATIVE_8BIT_S1)
11395 return FALSE;
35c08157 11396
1c8f6a4d
KLC
11397 /* Get the first instruction for its size. */
11398 insn = bfd_getb32 (contents + laddr);
11399 if (insn & 0x80000000)
11400 {
11401 *seq_len = 0;
11402 /* Get the immediate from movi55. */
11403 imm11 = N16_IMM5S (insn >> 16);
11404 }
11405 else
11406 {
11407 /* Get the immediate from movi. */
11408 imm11 = N32_IMM20S (insn);
35c08157
KLC
11409 }
11410
1c8f6a4d
KLC
11411 /* Get the branch instruction. */
11412 insn = bfd_getb32 (contents + irel->r_addend);
11413 /* Convert instruction to BR3. */
11414 if ((insn >> 14) & 0x1)
11415 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11416 else
11417 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
35c08157 11418
1c8f6a4d 11419 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
35c08157 11420
1c8f6a4d
KLC
11421 /* Set all relocations. */
11422 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11423 R_NDS32_WORD_9_PCREL_RELA);
11424
11425 /* Clean relocations. */
11426 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11427 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11428 R_NDS32_INSN16, irel->r_offset);
11429 if (insn_irel != irelend)
11430 {
11431 if (*seq_len == 0)
35c08157 11432 {
1c8f6a4d
KLC
11433 /* If the first insntruction is 16bit, convert it to nop16. */
11434 insn16 = NDS32_NOP16;
11435 bfd_putb16 (insn16, contents + laddr);
11436 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157 11437 }
1c8f6a4d
KLC
11438 else
11439 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11440 R_NDS32_NONE);
35c08157 11441 }
1c8f6a4d 11442 *insn_len = 0;
35c08157 11443
1c8f6a4d
KLC
11444 return TRUE;
11445}
35c08157 11446
fbaf61ad
NC
11447/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11448 for each relax round. But the gp may changed dramatically and then cause
11449 the truncated to fit errors for the the converted gp instructions.
11450 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11451
11452static bfd_boolean
11453nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11454 Elf_Internal_Rela *irel, bfd_boolean *again,
11455 bfd_boolean init,
11456 struct elf_nds32_link_hash_table *table,
11457 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11458
11459{
11460 int offset_to_gp;
11461 static bfd_boolean sec_pass = FALSE;
11462 static asection *first_sec = NULL, *sym_sec;
11463 /* Record the number of instructions which may be removed. */
11464 static int count = 0, record_count;
11465 Elf_Internal_Sym *isym;
11466 struct elf_link_hash_entry *h = NULL;
11467 int indx;
11468 unsigned long r_symndx;
11469 bfd *abfd = sec->owner;
11470 static bfd_vma record_sda = 0;
11471 int sda_offset = 0;
11472
11473 /* Force doing relaxation when hyper-relax is high. */
11474 if (table->hyper_relax == 2)
11475 return TRUE;
11476
11477 /* Do not relax the load/store patterns for the first
11478 relax round. */
11479 if (init)
11480 {
11481 if (!first_sec)
11482 first_sec = sec;
11483 else if (first_sec == sec)
11484 {
11485 record_count = count;
11486 count = 0;
11487 sec_pass = TRUE;
11488 }
11489
11490 if (!sec_pass)
11491 *again = TRUE;
11492
11493 return TRUE;
11494 }
11495
11496 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11497 DATA_SEGMENT_ALIGN size in the linker script may make it
11498 get even bigger. */
11499 if (record_sda == 0)
11500 record_sda = local_sda;
11501 else if (local_sda > record_sda)
11502 sda_offset = local_sda - record_sda;
11503
11504 /* Assume the instruction will be removed in the best case. */
11505 count++;
11506
11507 /* We record the offset to gp for each symbol, and then check
11508 if it is changed dramatically after relaxing.
11509 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11510 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11511 r_symndx = ELF32_R_SYM (irel->r_info);
11512 if (r_symndx >= symtab_hdr->sh_info)
11513 {
11514 /* Global symbols. */
11515 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11516 h = elf_sym_hashes (abfd)[indx];
11517 sym_sec = h->root.u.def.section;
11518 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11519 || bfd_is_abs_section (sym_sec))
11520 {
11521 /* Forbid doing relaxation when hyper-relax is low. */
11522 if (table->hyper_relax == 0)
11523 return FALSE;
11524
11525 offset_to_gp = *access_addr - local_sda;
11526 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11527 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11528 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11529 < abs (offset_to_gp) - sda_offset)
11530 {
11531 /* This may cause the error, so we reserve the
11532 safe enough size for relaxing. */
11533 if (*access_addr >= local_sda)
11534 *access_addr += (record_count * 4);
11535 else
11536 *access_addr -= (record_count * 4);
11537 }
11538 return sec_pass;
11539 }
11540 }
11541 else
11542 {
11543 /* Local symbols. */
11544 if (!elf32_nds32_allocate_local_sym_info (abfd))
11545 return FALSE;
11546 isym = isymbuf + r_symndx;
11547
11548 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11549 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11550 {
11551 /* Forbid doing relaxation when hyper-relax is low. */
11552 if (table->hyper_relax == 0)
11553 return FALSE;
11554
11555 offset_to_gp = *access_addr - local_sda;
11556 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11557 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11558 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11559 < abs (offset_to_gp) - sda_offset)
11560 {
11561 /* This may cause the error, so we reserve the
11562 safe enough size for relaxing. */
11563 if (*access_addr >= local_sda)
11564 *access_addr += (record_count * 4);
11565 else
11566 *access_addr -= (record_count * 4);
11567 }
11568 return sec_pass;
11569 }
11570 }
11571
11572 return TRUE;
11573}
11574
1c8f6a4d 11575#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
35c08157 11576
1c8f6a4d 11577/* Relax LOADSTORE relocation for nds32_elf_relax_section. */
35c08157 11578
1c8f6a4d
KLC
11579static bfd_boolean
11580nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11581 asection *sec, Elf_Internal_Rela *irel,
11582 Elf_Internal_Rela *internal_relocs, int *insn_len,
11583 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
fbaf61ad
NC
11584 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11585 struct elf_nds32_link_hash_table *table)
1c8f6a4d 11586{
6cae483a
AM
11587 int eliminate_sethi = 0, range_type;
11588 unsigned int i;
1c8f6a4d
KLC
11589 bfd_vma local_sda, laddr;
11590 int seq_len; /* Original length of instruction sequence. */
11591 uint32_t insn;
11592 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11593 bfd_vma access_addr = 0;
11594 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
fbaf61ad
NC
11595 struct elf_link_hash_entry *h = NULL;
11596 int indx;
1c8f6a4d
KLC
11597 enum elf_nds32_reloc_type checked_types[] =
11598 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11599 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11600 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11601 R_NDS32_TLS_LE_HI20
11602 };
35c08157 11603
1c8f6a4d
KLC
11604 irelend = internal_relocs + sec->reloc_count;
11605 seq_len = GET_SEQ_LEN (irel->r_addend);
11606 laddr = irel->r_offset;
11607 *insn_len = seq_len;
35c08157 11608
1c8f6a4d 11609 /* Get the high part relocation. */
6cae483a 11610 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11611 {
11612 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11613 checked_types[i], laddr);
11614 if (hi_irelfn != irelend)
11615 break;
11616 }
35c08157 11617
1c8f6a4d
KLC
11618 if (hi_irelfn == irelend)
11619 {
fbaf61ad
NC
11620 /* Not R_NDS32_HI20_RELA. */
11621 if (i != 0)
11622 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11623 (uint64_t) irel->r_offset);
11624 return FALSE;
1c8f6a4d 11625 }
35c08157 11626
1c8f6a4d
KLC
11627 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11628 nds32_elf_final_sda_base (sec->output_section->owner,
11629 link_info, &local_sda, FALSE);
35c08157 11630
1c8f6a4d
KLC
11631 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11632 {
11633 case R_NDS32_HI20_RELA:
11634 insn = bfd_getb32 (contents + laddr);
11635 access_addr =
11636 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11637
fbaf61ad 11638 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
1c8f6a4d 11639 {
fbaf61ad
NC
11640 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11641 h = elf_sym_hashes (abfd)[indx];
11642 }
35c08157 11643
fbaf61ad
NC
11644 /* Try movi. */
11645 if (range_type == NDS32_LOADSTORE_IMM
11646 && access_addr < CONSERVATIVE_20BIT
11647 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11648 {
11649 eliminate_sethi = 1;
11650 break;
11651 }
1c8f6a4d 11652
fbaf61ad
NC
11653 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11654 {
11655 eliminate_sethi = 1;
11656 break;
1c8f6a4d 11657 }
fbaf61ad
NC
11658 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11659 NULL, FALSE, table, isymbuf, symtab_hdr))
11660 return FALSE;
1c8f6a4d
KLC
11661
11662 if (!load_store_relax)
11663 return FALSE;
11664
11665 /* Case for set gp register. */
11666 if (N32_RT5 (insn) == REG_GP)
fbaf61ad 11667 return FALSE;
1c8f6a4d
KLC
11668
11669 if (range_type == NDS32_LOADSTORE_FLOAT_S
6cef73f9 11670 || range_type == NDS32_LOADSTORE_FLOAT_D)
1c8f6a4d
KLC
11671 {
11672 range_l = sdata_range[0][0];
11673 range_h = sdata_range[0][1];
11674 }
11675 else
11676 {
11677 range_l = sdata_range[1][0];
11678 range_h = sdata_range[1][1];
11679 }
11680 break;
11681
1c8f6a4d
KLC
11682 default:
11683 return FALSE;
11684 }
11685
11686 /* Delete sethi instruction. */
11687 if (eliminate_sethi == 1
11688 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11689 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11690 {
11691 hi_irelfn->r_info =
11692 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11693 irel->r_info =
11694 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11695 *insn_len = 0;
fbaf61ad 11696 return TRUE;
1c8f6a4d 11697 }
fbaf61ad
NC
11698
11699 return FALSE;
1c8f6a4d
KLC
11700}
11701
11702/* Relax LO12 relocation for nds32_elf_relax_section. */
11703
11704static void
11705nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11706 asection *sec, Elf_Internal_Rela *irel,
11707 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
fbaf61ad
NC
11708 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11709 struct elf_nds32_link_hash_table *table)
1c8f6a4d
KLC
11710{
11711 uint32_t insn;
11712 bfd_vma local_sda, laddr;
11713 unsigned long reloc;
11714 bfd_vma access_addr;
11715 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11716 Elf_Internal_Rela *irelfn = NULL, *irelend;
11717 struct elf_link_hash_entry *h = NULL;
11718 int indx;
11719
11720 /* For SDA base relative relaxation. */
11721 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11722 &local_sda, FALSE);
11723
11724 irelend = internal_relocs + sec->reloc_count;
11725 laddr = irel->r_offset;
11726 insn = bfd_getb32 (contents + laddr);
11727
11728 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11729 return;
11730
11731 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11732
11733 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11734 {
11735 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11736 h = elf_sym_hashes (abfd)[indx];
11737 }
11738
fbaf61ad 11739 /* Try movi. */
1c8f6a4d
KLC
11740 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11741 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11742 {
11743 reloc = R_NDS32_20_RELA;
11744 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11745 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11746 bfd_putb32 (insn, contents + laddr);
11747 }
1c8f6a4d
KLC
11748 else
11749 {
fbaf61ad
NC
11750 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11751 {
11752 /* Fall through. */
11753 }
11754 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11755 FALSE, table, isymbuf, symtab_hdr))
11756 return;
11757
1c8f6a4d
KLC
11758 range_l = sdata_range[1][0];
11759 range_h = sdata_range[1][1];
11760 switch (ELF32_R_TYPE (irel->r_info))
11761 {
11762 case R_NDS32_LO12S0_RELA:
11763 reloc = R_NDS32_SDA19S0_RELA;
11764 break;
11765 case R_NDS32_LO12S1_RELA:
11766 reloc = R_NDS32_SDA18S1_RELA;
11767 break;
11768 case R_NDS32_LO12S2_RELA:
11769 reloc = R_NDS32_SDA17S2_RELA;
11770 break;
11771 case R_NDS32_LO12S2_DP_RELA:
11772 range_l = sdata_range[0][0];
11773 range_h = sdata_range[0][1];
11774 reloc = R_NDS32_SDA12S2_DP_RELA;
11775 break;
11776 case R_NDS32_LO12S2_SP_RELA:
11777 range_l = sdata_range[0][0];
11778 range_h = sdata_range[0][1];
11779 reloc = R_NDS32_SDA12S2_SP_RELA;
11780 break;
11781 default:
11782 return;
11783 }
11784
11785 /* There are range_h and range_l because linker has to promise
11786 all sections move cross one page together. */
11787 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
fbaf61ad
NC
11788 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11789 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
1c8f6a4d
KLC
11790 {
11791 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
35c08157 11792 {
1c8f6a4d
KLC
11793 /* Maybe we should add R_NDS32_INSN16 reloc type here
11794 or manually do some optimization. sethi can't be
11795 eliminated when updating $gp so the relative ori
11796 needs to be preserved. */
11797 return;
35c08157 11798 }
1c8f6a4d
KLC
11799 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11800 &insn))
11801 return;
11802 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11803 bfd_putb32 (insn, contents + laddr);
11804
11805 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11806 R_NDS32_INSN16);
11807 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11808 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11809 irelfn->r_info =
11810 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11811
35c08157 11812 }
1c8f6a4d
KLC
11813 }
11814 return;
11815}
35c08157 11816
1c8f6a4d 11817/* Relax PTR relocation for nds32_elf_relax_section. */
35c08157 11818
1c8f6a4d
KLC
11819static bfd_boolean
11820nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11821 Elf_Internal_Rela *internal_relocs, int *insn_len,
11822 int *seq_len, bfd_byte *contents)
11823{
11824 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
35c08157 11825
1c8f6a4d 11826 irelend = internal_relocs + sec->reloc_count;
35c08157 11827
1c8f6a4d
KLC
11828 re_irel =
11829 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11830 R_NDS32_PTR_RESOLVED, irel->r_addend);
35c08157 11831
1c8f6a4d
KLC
11832 if (re_irel == irelend)
11833 {
695344c0 11834 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
2dcf00ce 11835 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11836 return FALSE;
11837 }
35c08157 11838
1c8f6a4d
KLC
11839 if (re_irel->r_addend != 1)
11840 return FALSE;
35c08157 11841
1c8f6a4d
KLC
11842 /* Pointed target is relaxed and no longer needs this void *,
11843 change the type to NONE. */
11844 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11845
1c8f6a4d
KLC
11846 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11847 not exist, it means only count 1 and remove it directly. */
11848 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11849 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11850 R_NDS32_PTR_COUNT);
11851 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11852 R_NDS32_PTR);
11853 if (count_irel != irelend)
11854 {
11855 if (--count_irel->r_addend > 0)
11856 return FALSE;
11857 }
11858
11859 if (ptr_irel != irelend)
11860 return FALSE;
11861
11862 /* If the PTR_COUNT is already 0, remove current instruction. */
11863 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11864 *insn_len = 0;
11865 return TRUE;
11866}
11867
fbaf61ad 11868/* Relax LWC relocation for nds32_elf_relax_section. */
1c8f6a4d
KLC
11869
11870static void
fbaf61ad
NC
11871nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11872 asection *sec, Elf_Internal_Rela *irel,
11873 Elf_Internal_Rela *internal_relocs,
11874 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11875 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
1c8f6a4d 11876{
fbaf61ad
NC
11877 /* Pattern:
11878 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11879 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11880 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11881 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11882 ... */
11883
1c8f6a4d 11884 uint32_t insn;
1c8f6a4d 11885 bfd_vma local_sda, laddr;
fbaf61ad
NC
11886 unsigned long reloc;
11887 bfd_vma access_addr, flsi_offset;
11888 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11889 Elf_Internal_Rela *irelend, *re_irel;
11890 unsigned int opcode;
1c8f6a4d
KLC
11891
11892 irelend = internal_relocs + sec->reloc_count;
11893 laddr = irel->r_offset;
11894 insn = bfd_getb32 (contents + laddr);
11895
fbaf61ad 11896 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
1c8f6a4d
KLC
11897 return;
11898
fbaf61ad
NC
11899 /* Can not do relaxation for bi format. */
11900 if ((insn & 0x1000))
1c8f6a4d
KLC
11901 return;
11902
fbaf61ad
NC
11903 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11904 opcode = N32_OP6 (insn);
11905 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11906 reloc = R_NDS32_SDA12S2_SP_RELA;
11907 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11908 reloc = R_NDS32_SDA12S2_DP_RELA;
1c8f6a4d
KLC
11909 else
11910 return;
11911
fbaf61ad
NC
11912 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11913 R_NDS32_PTR_RESOLVED);
11914 if (re_irel == irelend)
1c8f6a4d 11915 {
fbaf61ad
NC
11916 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11917 (uint64_t) irel->r_offset);
11918 return;
1c8f6a4d 11919 }
1c8f6a4d 11920
fbaf61ad 11921 /* For SDA base relative relaxation. */
1c8f6a4d
KLC
11922 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11923 &local_sda, FALSE);
fbaf61ad
NC
11924 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11925 flsi_offset = (insn & 0xfff) << 2;
11926 access_addr += flsi_offset;
11927 range_l = sdata_range[0][0];
11928 range_h = sdata_range[0][1];
1c8f6a4d 11929
fbaf61ad
NC
11930 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11931 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
1c8f6a4d 11932 {
fbaf61ad
NC
11933 /* Turn flsi instruction into sda access format. */
11934 insn = (insn & 0x7ff07000) | (REG_GP << 15);
1c8f6a4d 11935
fbaf61ad
NC
11936 /* Add relocation type to flsi. */
11937 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11938 irel->r_addend += flsi_offset;
11939 bfd_putb32 (insn, contents + re_irel->r_offset);
1c8f6a4d 11940
fbaf61ad 11941 re_irel->r_addend |= 1;
1c8f6a4d
KLC
11942 *again = TRUE;
11943 }
1c8f6a4d
KLC
11944}
11945
11946static bfd_boolean
11947nds32_relax_adjust_label (bfd *abfd, asection *sec,
11948 Elf_Internal_Rela *internal_relocs,
11949 bfd_byte *contents,
11950 nds32_elf_blank_t **relax_blank_list,
11951 int optimize, int opt_size)
11952{
11953 /* This code block is used to adjust 4-byte alignment by relax a pair
11954 of instruction a time.
11955
11956 It recognizes three types of relocations.
de194d85 11957 1. R_NDS32_LABEL - a alignment.
1c8f6a4d
KLC
11958 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11959 3. is_16bit_NOP () - remove a 16-bit instruction. */
11960
de194d85
YC
11961 /* TODO: It seems currently implementation only support 4-byte alignment.
11962 We should handle any-alignment. */
1c8f6a4d
KLC
11963
11964 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11965 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11966 Elf_Internal_Rela rel_temp;
11967 Elf_Internal_Rela *irelend;
11968 bfd_vma address;
11969 uint16_t insn16;
11970
11971 /* Checking for branch relaxation relies on the relocations to
11972 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11973 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11974 sizeof (Elf_Internal_Rela), compar_reloc);
11975
11976 irelend = internal_relocs + sec->reloc_count;
11977
11978 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11979 /* FIXME: Can we generate the right order in assembler?
11980 So we don't have to swapping them here. */
11981
11982 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11983 label_rel < irelend; label_rel++)
11984 {
11985 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11986 continue;
11987
11988 /* Find the first reloc has the same offset with label_rel. */
11989 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11990 insn_rel++;
11991
11992 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11993 insn_rel++)
11994 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11995 address. */
11996 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11997 break;
11998
11999 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
12000 && insn_rel < label_rel)
12001 {
12002 /* Swap the two reloc if the R_NDS32_INSN16 is
12003 before R_NDS32_LABEL. */
12004 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
12005 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
12006 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
12007 }
12008 }
12009
12010 label_rel = NULL;
12011 insn_rel = NULL;
12012 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12013 or higher, remove other R_NDS32_LABEL with lower alignment.
12014 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12015 then the R_NDS32_LABEL sequence is broke. */
12016 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12017 {
12018 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12019 {
12020 if (label_rel == NULL)
12021 {
12022 if (tmp_rel->r_addend < 2)
12023 label_rel = tmp_rel;
12024 continue;
12025 }
12026 else if (tmp_rel->r_addend > 1)
12027 {
12028 /* Remove all LABEL relocation from label_rel to tmp_rel
12029 including relocations with same offset as tmp_rel. */
fbaf61ad 12030 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
1c8f6a4d 12031 {
fbaf61ad
NC
12032 if (tmp2_rel->r_offset == tmp_rel->r_offset)
12033 break;
12034
1c8f6a4d
KLC
12035 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12036 && tmp2_rel->r_addend < 2)
12037 tmp2_rel->r_info =
12038 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12039 R_NDS32_NONE);
12040 }
12041 label_rel = NULL;
12042 }
12043 }
12044 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12045 {
12046 /* A new INSN16 which can be converted, so clear label_rel. */
12047 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12048 irelend, &insn16)
12049 || is_16bit_NOP (abfd, sec, tmp_rel))
12050 label_rel = NULL;
12051 }
12052 }
12053
12054 label_rel = NULL;
12055 insn_rel = NULL;
12056 /* Optimized for speed and nothing has not been relaxed.
12057 It's time to align labels.
12058 We may convert a 16-bit instruction right before a label to
12059 32-bit, in order to align the label if necessary
12060 all reloc entries has been sorted by r_offset. */
fbaf61ad
NC
12061 for (irel = internal_relocs;
12062 irel < irelend && irel->r_offset < sec->size; irel++)
1c8f6a4d
KLC
12063 {
12064 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12065 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12066 continue;
12067
12068 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12069 {
12070 /* A new INSN16 found, resize the old one. */
12071 if (is_convert_32_to_16
12072 (abfd, sec, irel, internal_relocs, irelend, &insn16)
12073 || is_16bit_NOP (abfd, sec, irel))
12074 {
12075 if (insn_rel)
12076 {
12077 /* Previous INSN16 reloc exists, reduce its
12078 size to 16-bit. */
12079 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12080 irelend, &insn16))
35c08157 12081 {
1c8f6a4d
KLC
12082 nds32_elf_write_16 (abfd, contents, insn_rel,
12083 internal_relocs, irelend, insn16);
12084
12085 if (!insert_nds32_elf_blank_recalc_total
12086 (relax_blank_list, insn_rel->r_offset + 2, 2))
12087 return FALSE;
12088 }
12089 else if (is_16bit_NOP (abfd, sec, insn_rel))
12090 {
12091 if (!insert_nds32_elf_blank_recalc_total
12092 (relax_blank_list, insn_rel->r_offset, 2))
12093 return FALSE;
12094 }
12095 insn_rel->r_info =
12096 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12097 }
12098 /* Save the new one for later use. */
12099 insn_rel = irel;
12100 }
12101 else
12102 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12103 R_NDS32_NONE);
12104 }
12105 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12106 {
12107 /* Search for label. */
12108 int force_relax = 0;
12109
12110 /* Label on 16-bit instruction or optimization
12111 needless, just reset this reloc. */
12112 insn16 = bfd_getb16 (contents + irel->r_offset);
12113 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12114 {
12115 irel->r_info =
12116 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12117 continue;
12118 }
12119
12120 address =
12121 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12122 irel->r_offset, 1);
12123
12124 if (!insn_rel)
12125 {
12126 /* Check if there is case which can not be aligned. */
12127 if (irel->r_addend == 2 && address & 0x2)
12128 return FALSE;
12129 continue;
12130 }
12131
12132 /* Try to align this label. */
12133
12134 if ((irel->r_addend & 0x1f) < 2)
12135 {
12136 /* Check if there is a INSN16 at the same address.
12137 Label_rel always seats before insn_rel after
12138 our sort. */
12139
12140 /* Search for INSN16 at LABEL location. If INSN16 is at
12141 same location and this LABEL alignment is lower than 2,
12142 the INSN16 can be converted to 2-byte. */
12143 for (tmp_rel = irel;
12144 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12145 tmp_rel++)
12146 {
12147 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12148 && (is_convert_32_to_16
12149 (abfd, sec, tmp_rel, internal_relocs,
12150 irelend, &insn16)
12151 || is_16bit_NOP (abfd, sec, tmp_rel)))
12152 {
12153 force_relax = 1;
12154 break;
12155 }
12156 }
12157 }
12158
12159 if (force_relax || irel->r_addend == 1 || address & 0x2)
12160 {
12161 /* Label not aligned. */
12162 /* Previous reloc exists, reduce its size to 16-bit. */
12163 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12164 internal_relocs, irelend, &insn16))
12165 {
12166 nds32_elf_write_16 (abfd, contents, insn_rel,
12167 internal_relocs, irelend, insn16);
12168
12169 if (!insert_nds32_elf_blank_recalc_total
12170 (relax_blank_list, insn_rel->r_offset + 2, 2))
12171 return FALSE;
12172 }
12173 else if (is_16bit_NOP (abfd, sec, insn_rel))
12174 {
12175 if (!insert_nds32_elf_blank_recalc_total
12176 (relax_blank_list, insn_rel->r_offset, 2))
12177 return FALSE;
12178 }
12179
12180 }
12181 /* INSN16 reloc is used. */
12182 insn_rel = NULL;
12183 }
12184 }
12185
12186 address =
12187 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12188 if (insn_rel && (address & 0x2 || opt_size))
12189 {
12190 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12191 irelend, &insn16))
12192 {
12193 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12194 irelend, insn16);
12195 if (!insert_nds32_elf_blank_recalc_total
12196 (relax_blank_list, insn_rel->r_offset + 2, 2))
12197 return FALSE;
12198 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12199 R_NDS32_NONE);
12200 }
12201 else if (is_16bit_NOP (abfd, sec, insn_rel))
12202 {
12203 if (!insert_nds32_elf_blank_recalc_total
12204 (relax_blank_list, insn_rel->r_offset, 2))
12205 return FALSE;
12206 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12207 R_NDS32_NONE);
12208 }
12209 }
12210 insn_rel = NULL;
12211 return TRUE;
12212}
12213
1c8f6a4d
KLC
12214static bfd_boolean
12215nds32_elf_relax_section (bfd *abfd, asection *sec,
12216 struct bfd_link_info *link_info, bfd_boolean *again)
12217{
12218 nds32_elf_blank_t *relax_blank_list = NULL;
12219 Elf_Internal_Shdr *symtab_hdr;
12220 Elf_Internal_Rela *internal_relocs;
12221 Elf_Internal_Rela *irel;
12222 Elf_Internal_Rela *irelend;
12223 Elf_Internal_Sym *isymbuf = NULL;
12224 bfd_byte *contents = NULL;
12225 bfd_boolean result = TRUE;
12226 int optimize = 0;
12227 int opt_size = 0;
12228 uint32_t insn;
12229 uint16_t insn16;
12230
12231 /* Target dependnet option. */
12232 struct elf_nds32_link_hash_table *table;
12233 int load_store_relax;
1c8f6a4d
KLC
12234
12235 relax_blank_list = NULL;
12236
12237 *again = FALSE;
12238
12239 /* Nothing to do for
12240 * relocatable link or
12241 * non-relocatable section or
12242 * non-code section or
12243 * empty content or
12244 * no reloc entry. */
0e1862bb 12245 if (bfd_link_relocatable (link_info)
1c8f6a4d 12246 || (sec->flags & SEC_RELOC) == 0
cd28e7aa 12247 || (sec->flags & SEC_EXCLUDE) != 0
1c8f6a4d 12248 || (sec->flags & SEC_CODE) == 0
fbaf61ad
NC
12249 || sec->size == 0
12250 || sec->reloc_count == 0)
1c8f6a4d
KLC
12251 return TRUE;
12252
12253 /* 09.12.11 Workaround. */
12254 /* We have to adjust align for R_NDS32_LABEL if needed.
12255 The adjust approach only can fix 2-byte align once. */
12256 if (sec->alignment_power > 2)
12257 return TRUE;
12258
fbaf61ad
NC
12259 /* Do TLS model conversion once at first. */
12260 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12261
1c8f6a4d
KLC
12262 /* The optimization type to do. */
12263
12264 table = nds32_elf_hash_table (link_info);
1c8f6a4d 12265
fbaf61ad
NC
12266 /* Save the first section for abs symbol relaxation.
12267 This is used for checking gp relaxation in the
12268 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12269 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12270 table, NULL, NULL);
12271
1c8f6a4d
KLC
12272 /* The begining of general relaxation. */
12273
12274 if (is_SDA_BASE_set == 0)
12275 {
12276 bfd_vma gp;
12277 is_SDA_BASE_set = 1;
12278 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12279 &gp, FALSE);
12280 relax_range_measurement (abfd);
12281 }
12282
1c8f6a4d
KLC
12283 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12284 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12285 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12286 TRUE /* keep_memory */);
12287 if (internal_relocs == NULL)
12288 goto error_return;
12289
12290 irelend = internal_relocs + sec->reloc_count;
12291 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12292 irelend, R_NDS32_RELAX_ENTRY);
12293
12294 if (irel == irelend)
12295 return TRUE;
12296
12297 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12298 {
12299 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
f9671640 12300 return TRUE;
1c8f6a4d
KLC
12301
12302 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12303 optimize = 1;
12304
12305 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12306 opt_size = 1;
12307 }
12308
12309 load_store_relax = table->load_store_relax;
12310
12311 /* Get symbol table and section content. */
0c4bd9d9 12312 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
1c8f6a4d
KLC
12313 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12314 goto error_return;
12315
12316 /* Do relax loop only when finalize is not done.
12317 Take care of relaxable relocs except INSN16. */
12318 for (irel = internal_relocs; irel < irelend; irel++)
12319 {
12320 int seq_len; /* Original length of instruction sequence. */
12321 int insn_len = 0; /* Final length of instruction sequence. */
12322 bfd_boolean removed;
12323
12324 insn = 0;
12325 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12326 && (irel->r_addend & 0x1f) >= 2)
12327 optimize = 1;
12328
12329 /* Relocation Types
12330 R_NDS32_LONGCALL1 53
12331 R_NDS32_LONGCALL2 54
12332 R_NDS32_LONGCALL3 55
12333 R_NDS32_LONGJUMP1 56
12334 R_NDS32_LONGJUMP2 57
12335 R_NDS32_LONGJUMP3 58
12336 R_NDS32_LOADSTORE 59 */
12337 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12338 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12339 seq_len = GET_SEQ_LEN (irel->r_addend);
12340
12341 /* Relocation Types
12342 R_NDS32_LONGCALL4 107
12343 R_NDS32_LONGCALL5 108
12344 R_NDS32_LONGCALL6 109
12345 R_NDS32_LONGJUMP4 110
12346 R_NDS32_LONGJUMP5 111
12347 R_NDS32_LONGJUMP6 112
12348 R_NDS32_LONGJUMP7 113 */
12349 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12350 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12351 seq_len = 4;
12352
12353 /* Relocation Types
12354 R_NDS32_LO12S0_RELA 30
12355 R_NDS32_LO12S1_RELA 29
12356 R_NDS32_LO12S2_RELA 28
12357 R_NDS32_LO12S2_SP_RELA 71
12358 R_NDS32_LO12S2_DP_RELA 70
12359 R_NDS32_GOT_LO12 46
12360 R_NDS32_GOTOFF_LO12 50
12361 R_NDS32_PLTREL_LO12 65
12362 R_NDS32_PLT_GOTREL_LO12 67
12363 R_NDS32_17IFC_PCREL_RELA 96
12364 R_NDS32_GOT_SUFF 193
12365 R_NDS32_GOTOFF_SUFF 194
12366 R_NDS32_PLT_GOT_SUFF 195
12367 R_NDS32_MULCALL_SUFF 196
12368 R_NDS32_PTR 197 */
12369 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12370 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12371 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12372 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12373 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12374 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12375 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12376 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12377 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12378 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12379 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12380 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12381 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12382 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
fbaf61ad
NC
12383 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12384 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
1c8f6a4d
KLC
12385 seq_len = 0;
12386 else
12387 continue;
12388
12389 insn_len = seq_len;
12390 removed = FALSE;
12391
12392 switch (ELF32_R_TYPE (irel->r_info))
12393 {
12394 case R_NDS32_LONGCALL1:
12395 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12396 &insn_len, contents, isymbuf,
12397 symtab_hdr);
12398 break;
12399 case R_NDS32_LONGCALL2:
12400 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12401 &insn_len, contents, isymbuf,
12402 symtab_hdr);
12403 break;
12404 case R_NDS32_LONGCALL3:
12405 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12406 &insn_len, contents, isymbuf,
12407 symtab_hdr);
12408 break;
12409 case R_NDS32_LONGJUMP1:
12410 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12411 &insn_len, contents, isymbuf,
12412 symtab_hdr);
12413 break;
12414 case R_NDS32_LONGJUMP2:
12415 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12416 &insn_len, contents, isymbuf,
12417 symtab_hdr);
12418 break;
12419 case R_NDS32_LONGJUMP3:
12420 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12421 &insn_len, contents, isymbuf,
12422 symtab_hdr);
12423 break;
12424 case R_NDS32_LONGCALL4:
12425 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12426 &insn_len, contents, isymbuf,
12427 symtab_hdr);
12428 break;
12429 case R_NDS32_LONGCALL5:
12430 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12431 &insn_len, contents, isymbuf,
12432 symtab_hdr);
12433 break;
12434 case R_NDS32_LONGCALL6:
12435 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12436 &insn_len, contents, isymbuf,
12437 symtab_hdr);
12438 break;
12439 case R_NDS32_LONGJUMP4:
12440 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12441 &insn_len, contents, isymbuf,
12442 symtab_hdr);
12443 break;
12444 case R_NDS32_LONGJUMP5:
12445 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12446 &insn_len, &seq_len, contents,
12447 isymbuf, symtab_hdr);
12448 break;
12449 case R_NDS32_LONGJUMP6:
12450 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12451 &insn_len, &seq_len, contents,
12452 isymbuf, symtab_hdr);
12453 break;
12454 case R_NDS32_LONGJUMP7:
12455 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12456 &insn_len, &seq_len, contents,
12457 isymbuf, symtab_hdr);
12458 break;
12459 case R_NDS32_LOADSTORE:
12460 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12461 internal_relocs, &insn_len,
12462 contents, isymbuf, symtab_hdr,
fbaf61ad 12463 load_store_relax, table);
1c8f6a4d
KLC
12464 break;
12465 case R_NDS32_LO12S0_RELA:
12466 case R_NDS32_LO12S1_RELA:
fbaf61ad 12467 case R_NDS32_LO12S2_RELA:
1c8f6a4d
KLC
12468 case R_NDS32_LO12S2_DP_RELA:
12469 case R_NDS32_LO12S2_SP_RELA:
1c8f6a4d
KLC
12470 /* Relax for low part. */
12471 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
fbaf61ad 12472 contents, isymbuf, symtab_hdr, table);
1c8f6a4d
KLC
12473
12474 /* It is impossible to delete blank, so just continue. */
12475 continue;
fbaf61ad
NC
12476 case R_NDS32_PTR:
12477 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12478 &insn_len, &seq_len, contents);
12479 break;
12480 case R_NDS32_LSI:
12481 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12482 contents, isymbuf, symtab_hdr, again);
12483 continue;
1c8f6a4d
KLC
12484 case R_NDS32_GOT_LO12:
12485 case R_NDS32_GOTOFF_LO12:
12486 case R_NDS32_PLTREL_LO12:
12487 case R_NDS32_PLT_GOTREL_LO12:
12488 case R_NDS32_GOTPC_LO12:
1c8f6a4d 12489 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 12490 case R_NDS32_TLS_LE_ADD:
1c8f6a4d 12491 case R_NDS32_TLS_LE_LS:
1c8f6a4d 12492 case R_NDS32_PLT_GOT_SUFF:
1c8f6a4d 12493 case R_NDS32_GOT_SUFF:
1c8f6a4d 12494 case R_NDS32_GOTOFF_SUFF:
1c8f6a4d
KLC
12495 continue;
12496 default:
12497 continue;
1c8f6a4d 12498 }
fbaf61ad 12499
1c8f6a4d
KLC
12500 if (removed && seq_len - insn_len > 0)
12501 {
12502 if (!insert_nds32_elf_blank
12503 (&relax_blank_list, irel->r_offset + insn_len,
12504 seq_len - insn_len))
12505 goto error_return;
12506 *again = TRUE;
35c08157 12507 }
1c8f6a4d
KLC
12508 }
12509
12510 calc_nds32_blank_total (relax_blank_list);
12511
12512 if (table->relax_fp_as_gp)
12513 {
12514 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12515 irelend, isymbuf))
12516 goto error_return;
35c08157 12517
535b785f 12518 if (!*again)
35c08157 12519 {
1c8f6a4d
KLC
12520 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12521 irelend))
12522 goto error_return;
35c08157
KLC
12523 }
12524 }
1c8f6a4d 12525
535b785f 12526 if (!*again)
1c8f6a4d
KLC
12527 {
12528 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12529 &relax_blank_list, optimize, opt_size))
12530 goto error_return;
12531 }
12532
12533 /* It doesn't matter optimize_for_space_no_align anymore.
35c08157
KLC
12534 If object file is assembled with flag '-Os',
12535 the we don't adjust jump-destination on 4-byte boundary. */
12536
12537 if (relax_blank_list)
12538 {
12539 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12540 relax_blank_list = NULL;
12541 }
12542
535b785f 12543 if (!*again)
35c08157
KLC
12544 {
12545 /* Closing the section, so we don't relax it anymore. */
12546 bfd_vma sec_size_align;
12547 Elf_Internal_Rela *tmp_rel;
12548
12549 /* Pad to alignment boundary. Only handle current section alignment. */
609332f1
NC
12550 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12551 & ((-1U) << sec->alignment_power);
35c08157
KLC
12552 if ((sec_size_align - sec->size) & 0x2)
12553 {
12554 insn16 = NDS32_NOP16;
12555 bfd_putb16 (insn16, contents + sec->size);
12556 sec->size += 2;
12557 }
12558
12559 while (sec_size_align != sec->size)
12560 {
12561 insn = NDS32_NOP32;
12562 bfd_putb32 (insn, contents + sec->size);
12563 sec->size += 4;
12564 }
12565
1c8f6a4d
KLC
12566 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12567 irelend, R_NDS32_RELAX_ENTRY);
35c08157
KLC
12568 if (tmp_rel != irelend)
12569 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12570
12571 clean_nds32_elf_blank ();
12572 }
12573
dc1e8a47 12574 finish:
35c08157
KLC
12575 if (internal_relocs != NULL
12576 && elf_section_data (sec)->relocs != internal_relocs)
12577 free (internal_relocs);
12578
12579 if (contents != NULL
12580 && elf_section_data (sec)->this_hdr.contents != contents)
12581 free (contents);
12582
12583 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12584 free (isymbuf);
12585
12586 return result;
12587
dc1e8a47 12588 error_return:
35c08157
KLC
12589 result = FALSE;
12590 goto finish;
12591}
12592
12593static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12594{
12595 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12596 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12597 {NULL, 0, 0, 0, 0}
12598};
12599
bf577467
AM
12600static bfd_boolean
12601nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12602{
12603 const char *name = hdr->bfd_section->name;
12604
12605 if (strncmp (name, ".sbss", 5) == 0
12606 || strncmp (name, ".sdata", 6) == 0)
12607 hdr->bfd_section->flags |= SEC_SMALL_DATA;
12608
12609 return TRUE;
12610}
12611
35c08157
KLC
12612static bfd_boolean
12613nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12614 struct bfd_link_info *info,
12615 void *finfo ATTRIBUTE_UNUSED,
12616 bfd_boolean (*func) (void *, const char *,
12617 Elf_Internal_Sym *,
12618 asection *,
12619 struct elf_link_hash_entry *)
12620 ATTRIBUTE_UNUSED)
12621{
12622 FILE *sym_ld_script = NULL;
12623 struct elf_nds32_link_hash_table *table;
12624
12625 table = nds32_elf_hash_table (info);
12626 sym_ld_script = table->sym_ld_script;
12627
12628 if (check_start_export_sym)
12629 fprintf (sym_ld_script, "}\n");
12630
12631 return TRUE;
12632}
12633
12634static enum elf_reloc_type_class
12635nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12636 const asection *rel_sec ATTRIBUTE_UNUSED,
12637 const Elf_Internal_Rela *rela)
12638{
12639 switch ((int) ELF32_R_TYPE (rela->r_info))
12640 {
12641 case R_NDS32_RELATIVE:
12642 return reloc_class_relative;
12643 case R_NDS32_JMP_SLOT:
12644 return reloc_class_plt;
12645 case R_NDS32_COPY:
12646 return reloc_class_copy;
12647 default:
12648 return reloc_class_normal;
12649 }
12650}
12651
12652/* Put target dependent option into info hash table. */
12653void
12654bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12655 int relax_fp_as_gp,
12656 int eliminate_gc_relocs,
fbaf61ad
NC
12657 FILE * sym_ld_script,
12658 int hyper_relax,
12659 int tls_desc_trampoline,
12660 int load_store_relax)
35c08157
KLC
12661{
12662 struct elf_nds32_link_hash_table *table;
12663
12664 table = nds32_elf_hash_table (link_info);
12665 if (table == NULL)
12666 return;
12667
12668 table->relax_fp_as_gp = relax_fp_as_gp;
12669 table->eliminate_gc_relocs = eliminate_gc_relocs;
12670 table->sym_ld_script = sym_ld_script;
fbaf61ad
NC
12671 table->hyper_relax = hyper_relax;
12672 table->tls_desc_trampoline = tls_desc_trampoline;
35c08157 12673 table ->load_store_relax = load_store_relax;
35c08157
KLC
12674}
12675\f
fbaf61ad 12676
35c08157
KLC
12677/* These functions and data-structures are used for fp-as-gp
12678 optimization. */
12679
12680#define FAG_THRESHOLD 3 /* At least 3 gp-access. */
1c8f6a4d
KLC
12681/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12682 the read-only section and read-write section. */
12683#define FAG_WINDOW (508 - 32)
35c08157
KLC
12684
12685/* An nds32_fag represent a gp-relative access.
12686 We find best fp-base by using a sliding window
12687 to find a base address which can cover most gp-access. */
12688struct nds32_fag
12689{
12690 struct nds32_fag *next; /* NULL-teminated linked list. */
12691 bfd_vma addr; /* The address of this fag. */
12692 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12693 It is used for applying FP7U2_FLAG. */
12694 int count; /* How many times this address is referred.
12695 There should be exactly `count' relocations
12696 in relas. */
12697 int relas_capcity; /* The buffer size of relas.
12698 We use an array instead of linked-list,
12699 and realloc is used to adjust buffer size. */
12700};
12701
12702static void
12703nds32_fag_init (struct nds32_fag *head)
12704{
12705 memset (head, 0, sizeof (struct nds32_fag));
12706}
12707
12708static void
12709nds32_fag_verify (struct nds32_fag *head)
12710{
12711 struct nds32_fag *iter;
12712 struct nds32_fag *prev;
12713
12714 prev = NULL;
12715 iter = head->next;
12716 while (iter)
12717 {
12718 if (prev && prev->addr >= iter->addr)
12719 puts ("Bug in fp-as-gp insertion.");
12720 prev = iter;
12721 iter = iter->next;
12722 }
12723}
12724
12725/* Insert a fag in ascending order.
12726 If a fag of the same address already exists,
12727 they are chained by relas array. */
12728
12729static void
12730nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12731 Elf_Internal_Rela * rel)
12732{
12733 struct nds32_fag *iter;
12734 struct nds32_fag *new_fag;
12735 const int INIT_RELAS_CAP = 4;
12736
12737 for (iter = head;
12738 iter->next && iter->next->addr <= addr;
12739 iter = iter->next)
12740 /* Find somewhere to insert. */ ;
12741
12742 /* `iter' will be equal to `head' if the list is empty. */
12743 if (iter != head && iter->addr == addr)
12744 {
12745 /* The address exists in the list.
12746 Insert `rel' into relocation list, relas. */
12747
12748 /* Check whether relas is big enough. */
12749 if (iter->count >= iter->relas_capcity)
12750 {
12751 iter->relas_capcity *= 2;
12752 iter->relas = bfd_realloc
12753 (iter->relas, iter->relas_capcity * sizeof (void *));
12754 }
12755 iter->relas[iter->count++] = rel;
12756 return;
12757 }
12758
12759 /* This is a new address. Create a fag node for it. */
12760 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12761 memset (new_fag, 0, sizeof (*new_fag));
12762 new_fag->addr = addr;
12763 new_fag->count = 1;
12764 new_fag->next = iter->next;
12765 new_fag->relas_capcity = INIT_RELAS_CAP;
12766 new_fag->relas = (Elf_Internal_Rela **)
12767 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12768 new_fag->relas[0] = rel;
12769 iter->next = new_fag;
12770
12771 nds32_fag_verify (head);
12772}
12773
12774static void
12775nds32_fag_free_list (struct nds32_fag *head)
12776{
12777 struct nds32_fag *iter;
12778
12779 iter = head->next;
12780 while (iter)
12781 {
12782 struct nds32_fag *tmp = iter;
12783 iter = iter->next;
12784 free (tmp->relas);
12785 tmp->relas = NULL;
12786 free (tmp);
12787 }
12788}
12789
35c08157
KLC
12790/* Find the best fp-base address.
12791 The relocation associated with that address is returned,
12792 so we can track the symbol instead of a fixed address.
12793
12794 When relaxation, the address of an datum may change,
12795 because a text section is shrinked, so the data section
1c8f6a4d 12796 moves forward. If the aligments of text and data section
35c08157
KLC
12797 are different, their distance may change too.
12798 Therefore, tracking a fixed address is not appriate. */
12799
12800static int
12801nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12802{
12803 struct nds32_fag *base; /* First fag in the window. */
12804 struct nds32_fag *last; /* First fag outside the window. */
12805 int accu = 0; /* Usage accumulation. */
12806 struct nds32_fag *best; /* Best fag. */
12807 int baccu = 0; /* Best accumulation. */
12808
12809 /* Use first fag for initial, and find the last fag in the window.
12810
12811 In each iteration, we could simply subtract previous fag
12812 and accumulate following fags which are inside the window,
12813 untill we each the end. */
12814
1c8f6a4d
KLC
12815 if (head->next == NULL)
12816 {
12817 *bestpp = NULL;
12818 return 0;
12819 }
35c08157
KLC
12820
12821 /* Initialize base. */
12822 base = head->next;
12823 best = base;
12824 for (last = base;
12825 last && last->addr < base->addr + FAG_WINDOW;
12826 last = last->next)
12827 accu += last->count;
12828
12829 baccu = accu;
12830
12831 /* Record the best base in each iteration. */
12832 while (base->next)
1c8f6a4d
KLC
12833 {
12834 accu -= base->count;
12835 base = base->next;
12836 /* Account fags in window. */
12837 for (/* Nothing. */;
12838 last && last->addr < base->addr + FAG_WINDOW;
12839 last = last->next)
12840 accu += last->count;
12841
12842 /* A better fp-base? */
12843 if (accu > baccu)
12844 {
12845 best = base;
12846 baccu = accu;
12847 }
12848 }
35c08157
KLC
12849
12850 if (bestpp)
12851 *bestpp = best;
12852 return baccu;
12853}
12854
12855/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12856 so we can convert it fo fp-relative access later.
12857 `best_fag' is the best fp-base. Only those inside the window
12858 of best_fag is applied the flag. */
12859
12860static bfd_boolean
12861nds32_fag_mark_relax (struct bfd_link_info *link_info,
fbaf61ad 12862 asection *sec, struct nds32_fag *best_fag,
35c08157
KLC
12863 Elf_Internal_Rela *internal_relocs,
12864 Elf_Internal_Rela *irelend)
12865{
12866 struct nds32_fag *ifag;
12867 bfd_vma best_fpbase, gp;
12868 bfd *output_bfd;
12869
fbaf61ad 12870 output_bfd = sec->output_section->owner;
35c08157
KLC
12871 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12872 best_fpbase = best_fag->addr;
12873
1c8f6a4d
KLC
12874 if (best_fpbase > gp + sdata_range[1][1]
12875 || best_fpbase < gp - sdata_range[1][0])
35c08157
KLC
12876 return FALSE;
12877
12878 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12879 so we know they can be converted to lwi37.fp. */
12880 for (ifag = best_fag;
12881 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12882 {
12883 int i;
12884
12885 for (i = 0; i < ifag->count; i++)
12886 {
12887 Elf_Internal_Rela *insn16_rel;
12888 Elf_Internal_Rela *fag_rel;
12889
12890 fag_rel = ifag->relas[i];
12891
12892 /* Only if this is within the WINDOWS, FP7U2_FLAG
12893 is applied. */
12894
12895 insn16_rel = find_relocs_at_address
12896 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12897
12898 if (insn16_rel != irelend)
12899 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12900 }
12901 }
12902 return TRUE;
12903}
12904
1c8f6a4d
KLC
12905/* Reset INSN16 to clean fp as gp. */
12906
12907static void
12908nds32_fag_unmark_relax (struct nds32_fag *fag,
12909 Elf_Internal_Rela *internal_relocs,
12910 Elf_Internal_Rela *irelend)
12911{
12912 struct nds32_fag *ifag;
12913 int i;
12914 Elf_Internal_Rela *insn16_rel;
12915 Elf_Internal_Rela *fag_rel;
12916
12917 for (ifag = fag; ifag; ifag = ifag->next)
12918 {
12919 for (i = 0; i < ifag->count; i++)
12920 {
12921 fag_rel = ifag->relas[i];
12922
12923 /* Restore the INSN16 relocation. */
12924 insn16_rel = find_relocs_at_address
12925 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12926
12927 if (insn16_rel != irelend)
12928 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12929 }
12930 }
12931}
12932
35c08157
KLC
12933/* This is the main function of fp-as-gp optimization.
12934 It should be called by relax_section. */
12935
12936static bfd_boolean
12937nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12938 bfd *abfd, asection *sec,
12939 Elf_Internal_Rela *internal_relocs,
12940 Elf_Internal_Rela *irelend,
12941 Elf_Internal_Sym *isymbuf)
12942{
12943 Elf_Internal_Rela *begin_rel = NULL;
12944 Elf_Internal_Rela *irel;
12945 struct nds32_fag fag_head;
12946 Elf_Internal_Shdr *symtab_hdr;
12947 bfd_byte *contents;
1c8f6a4d 12948 bfd_boolean ifc_inside = FALSE;
35c08157
KLC
12949
12950 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12951
12952 /* Per-function fp-base selection.
12953 1. Create a list for all the gp-relative access.
12954 2. Base on those gp-relative address,
12955 find a fp-base which can cover most access.
12956 3. Use the fp-base for fp-as-gp relaxation.
12957
12958 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12959 we should
12960 1. delete the `la $fp, _FP_BASE_' instruction and
12961 2. not convert lwi.gp to lwi37.fp.
12962
12963 To delete the _FP_BASE_ instruction, we simply apply
12964 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12965
12966 To suppress the conversion, we simply NOT to apply
12967 R_NDS32_INSN16_FP7U2_FLAG flag. */
12968
12969 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12970
0c4bd9d9 12971 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
35c08157
KLC
12972 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12973 return FALSE;
12974
12975 /* Check whether it is worth for fp-as-gp optimization,
12976 i.e., at least 3 gp-load.
12977
12978 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12979 apply this optimization. */
12980
12981 for (irel = internal_relocs; irel < irelend; irel++)
12982 {
12983 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12984 One we enter the begin of the region, we track all the LW/ST
12985 instructions, so when we leave the region, we try to find
12986 the best fp-base address for those LW/ST instructions. */
12987
12988 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12989 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12990 {
12991 /* Begin of the region. */
12992 if (begin_rel)
695344c0 12993 /* xgettext:c-format */
38f14ab8 12994 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
35c08157
KLC
12995
12996 begin_rel = irel;
12997 nds32_fag_init (&fag_head);
1c8f6a4d 12998 ifc_inside = FALSE;
35c08157
KLC
12999 }
13000 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13001 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
13002 {
13003 int accu;
1c8f6a4d 13004 struct nds32_fag *best_fag, *tmp_fag;
35c08157
KLC
13005 int dist;
13006
13007 /* End of the region.
13008 Check whether it is worth to do fp-as-gp. */
13009
13010 if (begin_rel == NULL)
13011 {
695344c0 13012 /* xgettext:c-format */
38f14ab8
AM
13013 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
13014 abfd, sec);
35c08157
KLC
13015 continue;
13016 }
13017
13018 accu = nds32_fag_find_base (&fag_head, &best_fag);
13019
1c8f6a4d
KLC
13020 /* Clean FP7U2_FLAG because they may set ever. */
13021 tmp_fag = fag_head.next;
13022 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13023
35c08157
KLC
13024 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
13025 if (accu < FAG_THRESHOLD
fbaf61ad 13026 || !nds32_fag_mark_relax (link_info, sec, best_fag,
35c08157
KLC
13027 internal_relocs, irelend))
13028 {
13029 /* Not worth to do fp-as-gp. */
13030 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13031 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13032 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13033 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13034 nds32_fag_free_list (&fag_head);
13035 begin_rel = NULL;
13036 continue;
13037 }
13038
13039 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13040 so we use it to record the distance to the reloction of best
13041 fp-base. */
13042 dist = best_fag->relas[0] - begin_rel;
13043 BFD_ASSERT (dist > 0 && dist < 0xffffff);
13044 /* Use high 16 bits of addend to record the _FP_BASE_ matched
13045 relocation. And get the base value when relocating. */
1c8f6a4d 13046 begin_rel->r_addend &= (0x1 << 16) - 1;
35c08157
KLC
13047 begin_rel->r_addend |= dist << 16;
13048
13049 nds32_fag_free_list (&fag_head);
13050 begin_rel = NULL;
13051 }
13052
1c8f6a4d 13053 if (begin_rel == NULL || ifc_inside)
35c08157
KLC
13054 /* Skip if we are not in the region of fp-as-gp. */
13055 continue;
13056
13057 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13058 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13059 {
13060 bfd_vma addr;
13061 uint32_t insn;
13062
13063 /* A gp-relative access is found. Insert it to the fag-list. */
13064
13065 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
13066 insn = bfd_getb32 (contents + irel->r_offset);
13067 if (!N32_IS_RT3 (insn))
13068 continue;
13069
13070 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13071 nds32_fag_insert (&fag_head, addr, irel);
13072 }
13073 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13074 {
13075 begin_rel = NULL;
13076 }
1c8f6a4d
KLC
13077 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13078 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13079 {
13080 /* Suppress fp as gp when encounter ifc. */
13081 ifc_inside = TRUE;
13082 }
35c08157
KLC
13083 }
13084
13085 return TRUE;
13086}
13087
13088/* Remove unused `la $fp, _FD_BASE_' instruction. */
13089
13090static bfd_boolean
13091nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13092 Elf_Internal_Rela *internal_relocs,
13093 Elf_Internal_Rela *irelend)
13094{
13095 Elf_Internal_Rela *irel;
13096 Elf_Internal_Shdr *symtab_hdr;
13097 bfd_byte *contents = NULL;
13098 nds32_elf_blank_t *relax_blank_list = NULL;
13099 bfd_boolean result = TRUE;
13100 bfd_boolean unused_region = FALSE;
13101
13102 /*
fbaf61ad
NC
13103 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13104 R_NDS32_17IFC_PCREL_RELA
13105 R_NDS32_10IFCU_PCREL_RELA. */
35c08157
KLC
13106
13107 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
0c4bd9d9 13108 nds32_get_section_contents (abfd, sec, &contents, TRUE);
35c08157
KLC
13109
13110 for (irel = internal_relocs; irel < irelend; irel++)
13111 {
13112 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13113 we marked to in previous pass.
13114 DO NOT scan relocations again, since we've alreadly decided it
13115 and set the flag. */
13116 const char *syname;
13117 int syndx;
13118 uint32_t insn;
13119
13120 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13121 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13122 unused_region = TRUE;
13123 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13124 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13125 unused_region = FALSE;
13126
13127 /* We're not in the region. */
13128 if (!unused_region)
13129 continue;
13130
13131 /* _FP_BASE_ must be a GLOBAL symbol. */
13132 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13133 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13134 continue;
13135
13136 /* The symbol name must be _FP_BASE_. */
13137 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13138 if (strcmp (syname, FP_BASE_NAME) != 0)
13139 continue;
13140
13141 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13142 {
13143 /* addi.gp $fp, -256 */
13144 insn = bfd_getb32 (contents + irel->r_offset);
13145 if (insn != INSN_ADDIGP_TO_FP)
13146 continue;
13147 }
13148 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13149 {
13150 /* addi $fp, $gp, -256 */
13151 insn = bfd_getb32 (contents + irel->r_offset);
13152 if (insn != INSN_ADDI_GP_TO_FP)
13153 continue;
13154 }
13155 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13156 {
13157 /* movi $fp, FP_BASE */
13158 insn = bfd_getb32 (contents + irel->r_offset);
13159 if (insn != INSN_MOVI_TO_FP)
13160 continue;
13161 }
13162 else
13163 continue;
13164
13165 /* We got here because a FP_BASE instruction is found. */
13166 if (!insert_nds32_elf_blank_recalc_total
13167 (&relax_blank_list, irel->r_offset, 4))
13168 goto error_return;
13169 }
13170
dc1e8a47 13171 finish:
35c08157
KLC
13172 if (relax_blank_list)
13173 {
13174 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13175 relax_blank_list = NULL;
13176 }
13177 return result;
13178
dc1e8a47 13179 error_return:
35c08157
KLC
13180 result = FALSE;
13181 goto finish;
13182}
1c8f6a4d
KLC
13183
13184/* This is a version of bfd_generic_get_relocated_section_contents.
13185 We need this variety because relaxation will modify the dwarf
13186 infomation. When there is undefined symbol reference error mesage,
13187 linker need to dump line number where the symbol be used. However
13188 the address is be relaxed, it can not get the original dwarf contents.
13189 The variety only modify function call for reading in the section. */
13190
13191static bfd_byte *
13192nds32_elf_get_relocated_section_contents (bfd *abfd,
13193 struct bfd_link_info *link_info,
13194 struct bfd_link_order *link_order,
13195 bfd_byte *data,
13196 bfd_boolean relocatable,
13197 asymbol **symbols)
13198{
13199 bfd *input_bfd = link_order->u.indirect.section->owner;
13200 asection *input_section = link_order->u.indirect.section;
13201 long reloc_size;
13202 arelent **reloc_vector;
13203 long reloc_count;
13204
13205 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13206 if (reloc_size < 0)
13207 return NULL;
13208
13209 /* Read in the section. */
0c4bd9d9 13210 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
1c8f6a4d
KLC
13211 return NULL;
13212
13213 if (reloc_size == 0)
13214 return data;
13215
13216 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13217 if (reloc_vector == NULL)
13218 return NULL;
13219
13220 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13221 reloc_vector, symbols);
13222 if (reloc_count < 0)
13223 goto error_return;
13224
13225 if (reloc_count > 0)
13226 {
13227 arelent **parent;
13228 for (parent = reloc_vector; *parent != NULL; parent++)
13229 {
13230 char *error_message = NULL;
13231 asymbol *symbol;
13232 bfd_reloc_status_type r;
13233
13234 symbol = *(*parent)->sym_ptr_ptr;
13235 if (symbol->section && discarded_section (symbol->section))
13236 {
0930cb30 13237 bfd_vma off;
1c8f6a4d
KLC
13238 static reloc_howto_type none_howto
13239 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13240 "unused", FALSE, 0, 0, FALSE);
13241
bb294208
AM
13242 off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13243 input_section);
0930cb30
AM
13244 _bfd_clear_contents ((*parent)->howto, input_bfd,
13245 input_section, data, off);
1c8f6a4d
KLC
13246 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13247 (*parent)->addend = 0;
13248 (*parent)->howto = &none_howto;
13249 r = bfd_reloc_ok;
13250 }
13251 else
13252 r = bfd_perform_relocation (input_bfd, *parent, data,
13253 input_section,
13254 relocatable ? abfd : NULL,
13255 &error_message);
13256
13257 if (relocatable)
13258 {
13259 asection *os = input_section->output_section;
13260
13261 /* A partial link, so keep the relocs. */
13262 os->orelocation[os->reloc_count] = *parent;
13263 os->reloc_count++;
13264 }
13265
13266 if (r != bfd_reloc_ok)
13267 {
13268 switch (r)
13269 {
13270 case bfd_reloc_undefined:
1a72702b
AM
13271 (*link_info->callbacks->undefined_symbol)
13272 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13273 input_bfd, input_section, (*parent)->address, TRUE);
1c8f6a4d
KLC
13274 break;
13275 case bfd_reloc_dangerous:
13276 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13277 (*link_info->callbacks->reloc_dangerous)
13278 (link_info, error_message,
13279 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13280 break;
13281 case bfd_reloc_overflow:
1a72702b
AM
13282 (*link_info->callbacks->reloc_overflow)
13283 (link_info, NULL,
13284 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13285 (*parent)->howto->name, (*parent)->addend,
13286 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13287 break;
13288 case bfd_reloc_outofrange:
13289 /* PR ld/13730:
13290 This error can result when processing some partially
13291 complete binaries. Do not abort, but issue an error
13292 message instead. */
13293 link_info->callbacks->einfo
695344c0 13294 /* xgettext:c-format */
c1c8c1ef 13295 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
1c8f6a4d
KLC
13296 abfd, input_section, * parent);
13297 goto error_return;
13298
13299 default:
13300 abort ();
13301 break;
13302 }
13303 }
13304 }
13305 }
13306
13307 free (reloc_vector);
13308 return data;
13309
dc1e8a47 13310 error_return:
1c8f6a4d
KLC
13311 free (reloc_vector);
13312 return NULL;
13313}
fbaf61ad
NC
13314
13315/* Check target symbol. */
13316
13317static bfd_boolean
13318nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13319{
13320 if (!sym || !sym->name || sym->name[0] != '$')
13321 return FALSE;
13322 return TRUE;
13323}
13324
13325/* nds32 find maybe function sym. Ignore target special symbol
13326 first, and then go the general function. */
13327
13328static bfd_size_type
13329nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13330 bfd_vma *code_off)
13331{
13332 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13333 return 0;
13334
13335 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13336}
35c08157 13337\f
35c08157 13338
fbaf61ad
NC
13339/* Do TLS model conversion. */
13340
13341typedef struct relax_group_list_t
13342{
13343 Elf_Internal_Rela *relo;
13344 struct relax_group_list_t *next;
13345 struct relax_group_list_t *next_sibling;
13346 int id;
13347} relax_group_list_t;
13348
13349int
13350list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13351
13352int
13353list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13354
13355void
13356dump_chain (relax_group_list_t *pHead);
13357
13358int
13359list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13360{
13361 relax_group_list_t *pNext = pHead;
13362
13363 /* Find place. */
13364 while (pNext->next)
13365 {
13366 if (pNext->next->id > (int) pElem->r_addend)
13367 break;
13368
13369 pNext = pNext->next;
13370 }
13371
13372 /* Insert node. */
13373 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13374 if (!pNew)
13375 return FALSE;
13376
13377 relax_group_list_t *tmp = pNext->next;
13378 pNext->next = pNew;
13379
13380 pNew->id = pElem->r_addend;
13381 pNew->relo = pElem;
13382 pNew->next = tmp;
13383 pNew->next_sibling = NULL;
13384
13385 return TRUE;
13386}
13387
13388int
13389list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13390{
13391 relax_group_list_t *pNext = pNode;
13392
13393 /* Find place. */
13394 while (pNext->next_sibling)
13395 {
13396 pNext = pNext->next_sibling;
13397 }
13398
13399 /* Insert node. */
13400 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13401 if (!pNew)
13402 return FALSE;
13403
13404 relax_group_list_t *tmp = pNext->next_sibling;
13405 pNext->next_sibling = pNew;
13406
13407 pNew->id = -1;
13408 pNew->relo = pElem;
13409 pNew->next = NULL;
13410 pNew->next_sibling = tmp;
13411
13412 return TRUE;
13413}
13414
13415void
13416dump_chain (relax_group_list_t *pHead)
13417{
13418 relax_group_list_t *pNext = pHead->next;
13419 while (pNext)
13420 {
13421 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13422 relax_group_list_t *pNextSib = pNext->next_sibling;
13423 while (pNextSib)
13424 {
13425 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13426 pNextSib = pNextSib->next_sibling;
13427 }
13428 pNext = pNext->next;
13429 printf("\n");
13430 }
13431}
13432
13433/* Check R_NDS32_RELAX_GROUP of each section.
13434 There might be multiple sections in one object file. */
13435
13436int
13437elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13438{
13439 elf32_nds32_relax_group_t *relax_group_ptr =
13440 elf32_nds32_relax_group_ptr (abfd);
13441
13442 int min_id = relax_group_ptr->min_id;
13443 int max_id = relax_group_ptr->max_id;
13444
13445 Elf_Internal_Rela *rel;
13446 Elf_Internal_Rela *relend;
13447 Elf_Internal_Rela *relocs;
13448 enum elf_nds32_reloc_type rtype;
13449
13450 do
13451 {
13452 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13453 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13454 TRUE /* keep_memory */);
13455 if (relocs == NULL)
13456 break;
13457
13458 /* Check R_NDS32_RELAX_GROUP. */
13459 relend = relocs + asec->reloc_count;
13460 for (rel = relocs; rel < relend; rel++)
13461 {
13462 int id;
13463 rtype = ELF32_R_TYPE (rel->r_info);
13464 if (rtype != R_NDS32_RELAX_GROUP)
13465 continue;
13466
13467 id = rel->r_addend;
13468 if (id < min_id)
13469 min_id = id;
13470 else if (id > max_id)
13471 max_id = id;
13472 }
13473 }
13474 while (FALSE);
13475
13476 if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs))
13477 free (relocs);
13478
13479 if ((min_id != relax_group_ptr->min_id)
13480 || (max_id != relax_group_ptr->max_id))
13481 {
13482 relax_group_ptr->count = max_id - min_id + 1;
13483 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13484 relax_group_ptr->min_id = min_id;
13485 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13486 relax_group_ptr->max_id = max_id;
13487 }
13488
13489 return relax_group_ptr->count;
13490}
13491
13492/* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13493struct section_id_list_t *relax_group_section_id_list = NULL;
13494
13495struct section_id_list_t *
13496elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13497{
13498 struct section_id_list_t *result = NULL;
13499 struct section_id_list_t *lst = *lst_ptr;
13500
13501 if (NULL == lst)
13502 {
13503 result = (struct section_id_list_t *) calloc
13504 (1, sizeof (struct section_id_list_t));
13505 BFD_ASSERT (result); /* Feed me. */
13506 result->id = id;
13507 *lst_ptr = result;
13508 }
13509 else
13510 {
13511 struct section_id_list_t *cur = lst;
13512 struct section_id_list_t *prv = NULL;
13513 struct section_id_list_t *sec = NULL;
13514
13515 while (cur)
13516 {
13517 if (cur->id < id)
13518 {
13519 prv = cur;
13520 cur = cur->next;
13521 continue;
13522 }
13523
13524 if (cur->id > id)
13525 {
13526 cur = NULL; /* To insert after prv. */
13527 sec = cur; /* In case prv == NULL. */
13528 }
13529
13530 break;
13531 }
13532
13533 if (NULL == cur)
13534 {
13535 /* Insert after prv. */
13536 result = (struct section_id_list_t *) calloc
13537 (1, sizeof (struct section_id_list_t));
13538 BFD_ASSERT (result); /* Feed me. */
13539 result->id = id;
13540 if (NULL != prv)
13541 {
13542 result->next = prv->next;
13543 prv->next = result;
13544 }
13545 else
13546 {
13547 *lst_ptr = result;
13548 result->next = sec;
13549 }
13550 }
13551 }
13552
13553 return result;
13554}
13555
13556int
13557elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13558{
13559 static int next_relax_group_bias = 0;
13560
13561 elf32_nds32_relax_group_t *relax_group_ptr =
13562 elf32_nds32_relax_group_ptr (abfd);
13563
13564 bfd_boolean result = TRUE;
13565 Elf_Internal_Rela *rel;
13566 Elf_Internal_Rela *relend;
13567 Elf_Internal_Rela *relocs = NULL;
13568 enum elf_nds32_reloc_type rtype;
13569 struct section_id_list_t *node = NULL;
13570 int count = 0;
13571
13572 do
13573 {
13574 if (0 == relax_group_ptr->count)
13575 break;
13576
13577 /* Check if this section has been handled. */
13578 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13579 if (NULL == node)
13580 break; /* Hit, the section id has handled. */
13581
13582 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13583 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13584 TRUE /* keep_memory */);
13585 if (relocs == NULL)
13586 {
13587 BFD_ASSERT (0); /* feed me */
13588 break;
13589 }
13590
13591 /* Allocate group id bias for this bfd! */
13592 if (0 == relax_group_ptr->init)
13593 {
13594 relax_group_ptr->bias = next_relax_group_bias;
13595 next_relax_group_bias += relax_group_ptr->count;
13596 relax_group_ptr->init = 1;
13597 }
13598
13599 /* Reorder relax group groups. */
13600 relend = relocs + asec->reloc_count;
13601 for (rel = relocs; rel < relend; rel++)
13602 {
13603 rtype = ELF32_R_TYPE(rel->r_info);
13604 if (rtype != R_NDS32_RELAX_GROUP)
13605 continue;
13606
13607 /* Change it. */
13608 rel->r_addend += relax_group_ptr->bias;
13609 /* Debugging count. */
13610 count++;
13611 }
13612 }
13613 while (FALSE);
13614
13615 if (relocs != NULL && elf_section_data (asec)->relocs != relocs)
13616 free (relocs);
13617
13618 return result;
13619}
13620
13621int
13622nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13623 struct bfd_link_info *lnkinfo)
13624{
13625 bfd_boolean result = TRUE;
13626 Elf_Internal_Rela *irel;
13627 Elf_Internal_Rela *irelend;
13628 Elf_Internal_Rela *internal_relocs;
13629 unsigned long r_symndx;
13630 enum elf_nds32_reloc_type r_type;
13631
13632 Elf_Internal_Sym *local_syms = NULL;
13633 bfd_byte *contents = NULL;
13634
13635 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13636
13637 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13638 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13639 sym_hashes = elf_sym_hashes (inbfd);
13640 sym_hashes_end =
13641 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13642 if (!elf_bad_symtab (inbfd))
13643 sym_hashes_end -= symtab_hdr->sh_info;
13644
13645 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13646 if (bfd_link_relocatable (lnkinfo))
13647 {
13648 elf32_nds32_unify_relax_group (inbfd, insec);
13649 return result;
13650 }
13651
13652 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13653 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13654 TRUE /* keep_memory */);
13655 if (internal_relocs == NULL)
13656 goto error_return;
13657
13658 irelend = internal_relocs + insec->reloc_count;
13659 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13660 irelend, R_NDS32_RELAX_ENTRY);
13661 if (irel == irelend)
13662 goto finish;
13663
13664 /* Chain/remove groups. */
13665 for (irel = internal_relocs; irel < irelend; irel++)
13666 {
13667 r_symndx = ELF32_R_SYM (irel->r_info);
13668 r_type = ELF32_R_TYPE (irel->r_info);
13669 if (r_type != R_NDS32_RELAX_GROUP)
13670 continue;
13671
13672 /* Remove it. */
13673 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13674 /* Chain it now. */
13675 if (!list_insert (&chain, irel))
13676 goto error_return;
13677 }
13678
13679 /* Collect group relocations. */
13680 /* Presume relocations are sorted. */
13681 relax_group_list_t *pNext = chain.next;
13682 while (pNext)
13683 {
13684 for (irel = internal_relocs; irel < irelend; irel++)
13685 {
13686 if (irel->r_offset == pNext->relo->r_offset)
13687 {
13688 /* Ignore Non-TLS relocation types. */
13689 r_type = ELF32_R_TYPE (irel->r_info);
13690 if ((R_NDS32_TLS_LE_HI20 > r_type)
13691 || (R_NDS32_RELAX_ENTRY == r_type))
13692 continue;
13693
13694 if (!list_insert_sibling (pNext, irel))
13695 goto error_return;
13696 }
13697 else if (irel->r_offset > pNext->relo->r_offset)
13698 {
13699 pNext = pNext->next;
13700 if (!pNext)
13701 break;
13702
13703 bfd_vma current_offset = pNext->relo->r_offset;
13704 if (irel->r_offset > current_offset)
13705 irel = internal_relocs; /* restart from head */
13706 else
13707 --irel; /* Check current irel again. */
13708 continue;
13709 }
13710 else
13711 {
13712 /* This shouldn't be reached. */
13713 }
13714 }
13715 if (pNext)
13716 pNext = pNext->next;
13717 }
13718
13719#ifdef DUBUG_VERBOSE
13720 dump_chain(&chain);
13721#endif
13722
13723 /* Get symbol table and section content. */
13724 if (incontents)
13725 contents = incontents;
13726 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13727 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13728 goto error_return;
13729
13730 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13731
13732 /* Convert TLS model each group if necessary. */
13733 pNext = chain.next;
13734
13735 int cur_grp_id = -1;
13736 int sethi_rt = -1;
13737 int add_rt = -1;
13738 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13739
13740 tls_type = org_tls_type = eff_tls_type = 0;
13741
13742 while (pNext)
13743 {
13744 relax_group_list_t *pNextSig = pNext->next_sibling;
13745 while (pNextSig)
13746 {
13747 struct elf_link_hash_entry *h = NULL;
13748
13749 irel = pNextSig->relo;
13750 r_symndx = ELF32_R_SYM(irel->r_info);
13751 r_type = ELF32_R_TYPE(irel->r_info);
13752
13753 if (pNext->id != cur_grp_id)
13754 {
13755 cur_grp_id = pNext->id;
13756 org_tls_type = get_tls_type (r_type, NULL);
13757 if (r_symndx >= symtab_hdr->sh_info)
13758 {
13759 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13760 while (h->root.type == bfd_link_hash_indirect
13761 || h->root.type == bfd_link_hash_warning)
13762 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13763 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13764 }
13765 else
13766 {
13767 tls_type = local_got_tls_type
13768 ? local_got_tls_type[r_symndx]
13769 : GOT_NORMAL;
13770 }
13771
13772 eff_tls_type = 1 << (fls (tls_type) - 1);
13773 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13774 }
13775
13776 if (eff_tls_type != org_tls_type)
13777 {
13778 switch (org_tls_type)
13779 {
13780 /* DESC to IEGP/IE/LE. */
13781 case GOT_TLS_DESC:
13782 switch (eff_tls_type)
13783 {
13784 case GOT_TLS_IE:
13785 switch (r_type)
13786 {
13787 case R_NDS32_TLS_DESC_HI20:
13788 irel->r_info = ELF32_R_INFO(r_symndx,
13789 R_NDS32_TLS_IE_HI20);
13790 break;
13791 case R_NDS32_TLS_DESC_LO12:
13792 irel->r_info = ELF32_R_INFO(r_symndx,
13793 R_NDS32_TLS_IE_LO12);
13794 break;
13795 case R_NDS32_TLS_DESC_ADD:
13796 {
13797 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13798 add_rt = N32_RT5 (insn);
13799 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13800 bfd_putb32 (insn, contents + irel->r_offset);
13801
13802 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13803 }
13804 break;
13805 case R_NDS32_TLS_DESC_FUNC:
13806 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13807 irel->r_info = ELF32_R_INFO(r_symndx,
13808 R_NDS32_RELAX_REMOVE);
13809 break;
13810 case R_NDS32_TLS_DESC_CALL:
13811 {
13812 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13813 REG_TP);
13814 bfd_putb32 (insn, contents + irel->r_offset);
13815
13816 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13817 }
13818 break;
13819 case R_NDS32_LOADSTORE:
13820 case R_NDS32_PTR:
13821 case R_NDS32_PTR_RESOLVED:
13822 case R_NDS32_NONE:
13823 case R_NDS32_LABEL:
13824 break;
13825 default:
13826 BFD_ASSERT(0);
13827 break;
13828 }
13829 break;
13830 case GOT_TLS_IEGP:
13831 switch (r_type)
13832 {
13833 case R_NDS32_TLS_DESC_HI20:
13834 irel->r_info = ELF32_R_INFO(r_symndx,
13835 R_NDS32_TLS_IEGP_HI20);
13836 break;
13837 case R_NDS32_TLS_DESC_LO12:
13838 irel->r_info = ELF32_R_INFO(r_symndx,
13839 R_NDS32_TLS_IEGP_LO12);
13840 break;
13841 case R_NDS32_TLS_DESC_ADD:
13842 {
13843 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13844 add_rt = N32_RT5 (insn);
13845 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13846 bfd_putb32 (insn, contents + irel->r_offset);
13847
13848 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13849 }
13850 break;
13851 case R_NDS32_TLS_DESC_FUNC:
13852 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13853 irel->r_info = ELF32_R_INFO(r_symndx,
13854 R_NDS32_RELAX_REMOVE);
13855 break;
13856 case R_NDS32_TLS_DESC_CALL:
13857 {
13858 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13859 REG_TP);
13860 bfd_putb32 (insn, contents + irel->r_offset);
13861
13862 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13863 }
13864 break;
13865 case R_NDS32_LOADSTORE:
13866 case R_NDS32_PTR:
13867 case R_NDS32_PTR_RESOLVED:
13868 case R_NDS32_NONE:
13869 case R_NDS32_LABEL:
13870 break;
13871 default:
13872 BFD_ASSERT(0);
13873 break;
13874 }
13875 break;
13876 case GOT_TLS_LE:
13877 switch (r_type)
13878 {
13879 case R_NDS32_TLS_DESC_HI20:
13880 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13881 break;
13882 case R_NDS32_TLS_DESC_LO12:
13883 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13884 break;
13885 case R_NDS32_TLS_DESC_ADD:
13886 {
13887 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13888
13889 add_rt = N32_RT5 (insn);
13890 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13891 bfd_putb32 (insn, contents + irel->r_offset);
13892
13893 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13894 }
13895 break;
13896 case R_NDS32_TLS_DESC_FUNC:
13897 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13898 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13899 break;
13900 case R_NDS32_TLS_DESC_CALL:
13901 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13902 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13903 break;
13904 case R_NDS32_LOADSTORE:
13905 case R_NDS32_PTR:
13906 case R_NDS32_PTR_RESOLVED:
13907 case R_NDS32_NONE:
13908 case R_NDS32_LABEL:
13909 break;
13910 default:
13911 BFD_ASSERT(0);
13912 break;
13913 }
13914 break;
13915 default:
13916 break;
13917 }
13918 break;
13919 /* IEGP to IE/LE. */
13920 case GOT_TLS_IEGP:
13921 switch (eff_tls_type)
13922 {
13923 case GOT_TLS_IE:
13924 switch (r_type)
13925 {
13926 case R_NDS32_TLS_IEGP_HI20:
13927 irel->r_info = ELF32_R_INFO(r_symndx,
13928 R_NDS32_TLS_IE_HI20);
13929 break;
13930 case R_NDS32_TLS_IEGP_LO12:
13931 irel->r_info = ELF32_R_INFO(r_symndx,
13932 R_NDS32_TLS_IE_LO12);
13933 break;
13934 case R_NDS32_PTR_RESOLVED:
13935 {
13936 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13937
13938 add_rt = N32_RT5 (insn);
13939 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13940 bfd_putb32 (insn, contents + irel->r_offset);
13941 }
13942 break;
13943 case R_NDS32_TLS_IEGP_LW:
13944 break;
13945 case R_NDS32_LOADSTORE:
13946 case R_NDS32_PTR:
13947 case R_NDS32_NONE:
13948 case R_NDS32_LABEL:
13949 break;
13950 default:
13951 BFD_ASSERT(0);
13952 break;
13953 }
13954 break;
13955 case GOT_TLS_LE:
13956 switch (r_type)
13957 {
13958 case R_NDS32_TLS_IEGP_HI20:
13959 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13960 break;
13961 case R_NDS32_TLS_IEGP_LO12:
13962 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13963 break;
13964 case R_NDS32_TLS_IEGP_LW:
13965 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13966 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13967 break;
13968 case R_NDS32_LOADSTORE:
13969 case R_NDS32_PTR:
13970 case R_NDS32_NONE:
13971 case R_NDS32_LABEL:
13972 case R_NDS32_PTR_RESOLVED:
13973 break;
13974 default:
13975 BFD_ASSERT(0);
13976 break;
13977 }
13978 break;
13979 default:
13980 break;
13981 }
13982 break;
13983 /* IE to LE. */
13984 case GOT_TLS_IE:
13985 switch (eff_tls_type)
13986 {
13987 case GOT_TLS_LE:
13988 switch (r_type)
13989 {
13990 case R_NDS32_TLS_IE_HI20:
13991 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13992 break;
13993 case R_NDS32_TLS_IE_LO12S2:
13994 {
13995 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13996
13997 add_rt = N32_RT5 (insn);
13998 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13999 bfd_putb32 (insn, contents + irel->r_offset);
14000
14001 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
14002 }
14003 break;
14004 case R_NDS32_LOADSTORE:
14005 case R_NDS32_PTR:
14006 case R_NDS32_NONE:
14007 case R_NDS32_LABEL:
14008 break;
14009 default:
14010 BFD_ASSERT(0);
14011 break;
14012 }
14013 break;
14014 default:
14015 break;
14016 }
14017 break;
14018 default:
14019 break;
14020 }
14021 }
14022 pNextSig = pNextSig->next_sibling;
14023 }
14024
14025#if 1
14026 pNext = pNext->next;
14027#else
14028 while (pNext)
14029 {
14030 if (pNext->id != cur_grp_id)
14031 break;
14032 pNext = pNext->next;
14033 }
14034#endif
14035 }
14036
dc1e8a47 14037 finish:
fbaf61ad
NC
14038 if (incontents)
14039 contents = NULL;
14040
14041 if (internal_relocs != NULL
14042 && elf_section_data (insec)->relocs != internal_relocs)
14043 free (internal_relocs);
14044
14045 if (contents != NULL
14046 && elf_section_data (insec)->this_hdr.contents != contents)
14047 free (contents);
14048
14049 if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms)
14050 free (local_syms);
14051
14052 if (chain.next)
14053 {
14054 pNext = chain.next;
14055 relax_group_list_t *pDel;
14056 while (pNext)
14057 {
14058 pDel = pNext;
14059 pNext = pNext->next;
14060 free (pDel);
14061 }
14062 }
14063
14064 return result;
14065
dc1e8a47 14066 error_return:
fbaf61ad
NC
14067 result = FALSE;
14068 goto finish;
14069}
14070
14071/* End TLS model conversion. */
14072
35c08157
KLC
14073#define ELF_ARCH bfd_arch_nds32
14074#define ELF_MACHINE_CODE EM_NDS32
14075#define ELF_MAXPAGESIZE 0x1000
07d6d2b8 14076#define ELF_TARGET_ID NDS32_ELF_DATA
35c08157 14077
6d00b590 14078#define TARGET_BIG_SYM nds32_elf32_be_vec
35c08157 14079#define TARGET_BIG_NAME "elf32-nds32be"
6d00b590 14080#define TARGET_LITTLE_SYM nds32_elf32_le_vec
35c08157
KLC
14081#define TARGET_LITTLE_NAME "elf32-nds32le"
14082
14083#define elf_info_to_howto nds32_info_to_howto
14084#define elf_info_to_howto_rel nds32_info_to_howto_rel
14085
14086#define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14087#define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14088#define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14089#define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14090#define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14091
07d6d2b8 14092#define bfd_elf32_mkobject nds32_elf_mkobject
35c08157
KLC
14093#define elf_backend_action_discarded nds32_elf_action_discarded
14094#define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14095#define elf_backend_check_relocs nds32_elf_check_relocs
14096#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14097#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14098#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14099#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14100#define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14101#define elf_backend_relocate_section nds32_elf_relocate_section
14102#define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
35c08157
KLC
14103#define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14104#define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14105#define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14106#define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14107#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14108#define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14109#define elf_backend_object_p nds32_elf_object_p
14110#define elf_backend_final_write_processing nds32_elf_final_write_processing
14111#define elf_backend_special_sections nds32_elf_special_sections
bf577467 14112#define elf_backend_section_flags nds32_elf_section_flags
1c8f6a4d 14113#define bfd_elf32_bfd_get_relocated_section_contents \
07d6d2b8 14114 nds32_elf_get_relocated_section_contents
fbaf61ad
NC
14115#define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14116#define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
35c08157
KLC
14117
14118#define elf_backend_can_gc_sections 1
14119#define elf_backend_can_refcount 1
14120#define elf_backend_want_got_plt 1
14121#define elf_backend_plt_readonly 1
14122#define elf_backend_want_plt_sym 0
14123#define elf_backend_got_header_size 12
14124#define elf_backend_may_use_rel_p 1
14125#define elf_backend_default_use_rela_p 1
14126#define elf_backend_may_use_rela_p 1
fbaf61ad 14127#define elf_backend_dtrel_excludes_plt 0
35c08157
KLC
14128
14129#include "elf32-target.h"
14130
14131#undef ELF_MAXPAGESIZE
14132#define ELF_MAXPAGESIZE 0x2000
14133
fbaf61ad 14134#undef TARGET_BIG_SYM
6d00b590 14135#define TARGET_BIG_SYM nds32_elf32_linux_be_vec
fbaf61ad 14136#undef TARGET_BIG_NAME
35c08157 14137#define TARGET_BIG_NAME "elf32-nds32be-linux"
fbaf61ad 14138#undef TARGET_LITTLE_SYM
6d00b590 14139#define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
fbaf61ad 14140#undef TARGET_LITTLE_NAME
35c08157 14141#define TARGET_LITTLE_NAME "elf32-nds32le-linux"
fbaf61ad 14142#undef elf32_bed
35c08157
KLC
14143#define elf32_bed elf32_nds32_lin_bed
14144
14145#include "elf32-target.h"
This page took 1.07973 seconds and 4 git commands to generate.