]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-nds32.c
Andes Technology has good news for you, we plan to update the nds32 port of binutils...
[thirdparty/binutils-gdb.git] / bfd / elf32-nds32.c
CommitLineData
35c08157 1/* NDS32-specific support for 32-bit ELF.
219d1afa 2 Copyright (C) 2012-2018 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"
25#include "bfd_stdint.h"
26#include "bfdlink.h"
27#include "libbfd.h"
28#include "elf-bfd.h"
29#include "libiberty.h"
30#include "bfd_stdint.h"
31#include "elf/nds32.h"
32#include "opcode/nds32.h"
33#include "elf32-nds32.h"
34#include "opcode/cgen.h"
35#include "../opcodes/nds32-opc.h"
36
37/* Relocation HOWTO functions. */
38static bfd_reloc_status_type nds32_elf_ignore_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42static bfd_reloc_status_type nds32_elf_hi20_reloc
43 (bfd *, arelent *, asymbol *, void *,
44 asection *, bfd *, char **);
45static bfd_reloc_status_type nds32_elf_lo12_reloc
46 (bfd *, arelent *, asymbol *, void *,
47 asection *, bfd *, char **);
48static bfd_reloc_status_type nds32_elf_generic_reloc
49 (bfd *, arelent *, asymbol *, void *,
50 asection *, bfd *, char **);
51static bfd_reloc_status_type nds32_elf_sda15_reloc
52 (bfd *, arelent *, asymbol *, void *,
53 asection *, bfd *, char **);
54
55/* Helper functions for HOWTO. */
56static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
57 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
58 asection *, bfd_vma, bfd_vma);
35c08157
KLC
59
60/* Nds32 helper functions. */
35c08157 61static bfd_vma calculate_memory_address
fbaf61ad 62 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
0c4bd9d9
KLC
63static int nds32_get_section_contents (bfd *, asection *,
64 bfd_byte **, bfd_boolean);
35c08157
KLC
65static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
66 Elf_Internal_Sym **);
35c08157
KLC
67static bfd_boolean nds32_relax_fp_as_gp
68 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
69 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
70 Elf_Internal_Sym *isymbuf);
71static bfd_boolean nds32_fag_remove_unused_fpbase
72 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
73 Elf_Internal_Rela *irelend);
74
75enum
76{
77 MACH_V1 = bfd_mach_n1h,
78 MACH_V2 = bfd_mach_n1h_v2,
79 MACH_V3 = bfd_mach_n1h_v3,
80 MACH_V3M = bfd_mach_n1h_v3m
81};
82
83#define MIN(a, b) ((a) > (b) ? (b) : (a))
84#define MAX(a, b) ((a) > (b) ? (a) : (b))
85
86/* The name of the dynamic interpreter. This is put in the .interp
87 section. */
88#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
89
fbaf61ad
NC
90#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
91 && (flags) & SEC_LOAD \
92 && (flags) & SEC_READONLY)
93
35c08157
KLC
94/* The nop opcode we use. */
95#define NDS32_NOP32 0x40000009
96#define NDS32_NOP16 0x9200
97
98/* The size in bytes of an entry in the procedure linkage table. */
99#define PLT_ENTRY_SIZE 24
100#define PLT_HEADER_SIZE 24
101
102/* The first entry in a procedure linkage table are reserved,
103 and the initial contents are unimportant (we zero them out).
104 Subsequent entries look like this. */
07d6d2b8
AM
105#define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
106#define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
107#define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
108#define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
109#define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
35c08157
KLC
110
111/* $ta is change to $r15 (from $r25). */
112#define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
07d6d2b8
AM
113#define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
114#define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
115#define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
116#define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
117#define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
118
119#define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
120#define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
121#define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
122#define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
123#define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
35c08157
KLC
124
125#define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
07d6d2b8
AM
126#define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
127#define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
128#define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
129#define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
130#define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
35c08157 131
1c8f6a4d
KLC
132/* These are macros used to get the relocation accurate value. */
133#define ACCURATE_8BIT_S1 (0x100)
134#define ACCURATE_U9BIT_S1 (0x400)
135#define ACCURATE_12BIT_S1 (0x2000)
136#define ACCURATE_14BIT_S1 (0x4000)
137#define ACCURATE_19BIT (0x40000)
138
139/* These are macros used to get the relocation conservative value. */
140#define CONSERVATIVE_8BIT_S1 (0x100 - 4)
141#define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
142#define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
143#define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
144/* These must be more conservative because the address may be in
145 different segment. */
146#define CONSERVATIVE_15BIT (0x4000 - 0x1000)
147#define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
148#define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
149#define CONSERVATIVE_19BIT (0x40000 - 0x1000)
150#define CONSERVATIVE_20BIT (0x80000 - 0x1000)
151
35c08157
KLC
152/* Size of small data/bss sections, used to calculate SDA_BASE. */
153static long got_size = 0;
154static int is_SDA_BASE_set = 0;
35c08157 155
35c08157
KLC
156/* Convert ELF-VER in eflags to string for debugging purpose. */
157static const char *const nds32_elfver_strtab[] =
158{
159 "ELF-1.2",
160 "ELF-1.3",
161 "ELF-1.4",
162};
163
164/* The nds32 linker needs to keep track of the number of relocs that it
165 decides to copy in check_relocs for each symbol. This is so that
166 it can discard PC relative relocs if it doesn't need them when
167 linking with -Bsymbolic. We store the information in a field
168 extending the regular ELF linker hash table. */
169
170/* This structure keeps track of the number of PC relative relocs we
171 have copied for a given symbol. */
172
173struct elf_nds32_pcrel_relocs_copied
174{
175 /* Next section. */
176 struct elf_nds32_pcrel_relocs_copied *next;
177 /* A section in dynobj. */
178 asection *section;
179 /* Number of relocs copied in this section. */
180 bfd_size_type count;
181};
182
fbaf61ad
NC
183enum elf_nds32_tls_type
184{
185 GOT_UNKNOWN = (0),
186 GOT_NORMAL = (1 << 0),
187 GOT_TLS_LE = (1 << 1),
188 GOT_TLS_IE = (1 << 2),
189 GOT_TLS_IEGP = (1 << 3),
190 GOT_TLS_LD = (1 << 4),
191 GOT_TLS_GD = (1 << 5),
192 GOT_TLS_DESC = (1 << 6),
193};
194
35c08157
KLC
195/* Nds32 ELF linker hash entry. */
196
197struct elf_nds32_link_hash_entry
198{
199 struct elf_link_hash_entry root;
200
201 /* Track dynamic relocs copied for this symbol. */
3bf083ed 202 struct elf_dyn_relocs *dyn_relocs;
1c8f6a4d
KLC
203
204 /* For checking relocation type. */
fbaf61ad
NC
205 enum elf_nds32_tls_type tls_type;
206
207 int offset_to_gp;
35c08157
KLC
208};
209
210/* Get the nds32 ELF linker hash table from a link_info structure. */
211
212#define FP_BASE_NAME "_FP_BASE_"
213static int check_start_export_sym = 0;
35c08157 214
1c8f6a4d
KLC
215/* The offset for executable tls relaxation. */
216#define TP_OFFSET 0x0
217
fbaf61ad
NC
218typedef struct
219{
220 int min_id;
221 int max_id;
222 int count;
223 int bias;
224 int init;
225} elf32_nds32_relax_group_t;
226
1c8f6a4d
KLC
227struct elf_nds32_obj_tdata
228{
229 struct elf_obj_tdata root;
230
231 /* tls_type for each local got entry. */
232 char *local_got_tls_type;
fbaf61ad
NC
233
234 /* GOTPLT entries for TLS descriptors. */
235 bfd_vma *local_tlsdesc_gotent;
236
237 /* for R_NDS32_RELAX_GROUP handling. */
238 elf32_nds32_relax_group_t relax_group;
239
240 unsigned int hdr_size;
241 int* offset_to_gp;
1c8f6a4d
KLC
242};
243
244#define elf_nds32_tdata(bfd) \
245 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
246
247#define elf32_nds32_local_got_tls_type(bfd) \
248 (elf_nds32_tdata (bfd)->local_got_tls_type)
249
fbaf61ad
NC
250#define elf32_nds32_local_gp_offset(bfd) \
251 (elf_nds32_tdata (bfd)->offset_to_gp)
252
1c8f6a4d
KLC
253#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
254
fbaf61ad
NC
255#define elf32_nds32_relax_group_ptr(bfd) \
256 &(elf_nds32_tdata (bfd)->relax_group)
257
1c8f6a4d
KLC
258static bfd_boolean
259nds32_elf_mkobject (bfd *abfd)
260{
261 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
262 NDS32_ELF_DATA);
263}
264
35c08157 265/* Relocations used for relocation. */
fbaf61ad
NC
266/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
267 initialize array nds32_elf_howto_table in any order. The benefit
268 is that we can add any new relocations with any numbers and don't
269 need to fill the gap by lots of EMPTY_HOWTO. */
270#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
271 [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
272
35c08157
KLC
273static reloc_howto_type nds32_elf_howto_table[] =
274{
275 /* This reloc does nothing. */
fbaf61ad
NC
276 HOWTO2 (R_NDS32_NONE, /* type */
277 0, /* rightshift */
278 2, /* size (0 = byte, 1 = short, 2 = long) */
279 32, /* bitsize */
280 FALSE, /* pc_relative */
281 0, /* bitpos */
282 complain_overflow_bitfield,/* complain_on_overflow */
283 bfd_elf_generic_reloc, /* special_function */
284 "R_NDS32_NONE", /* name */
285 FALSE, /* partial_inplace */
286 0, /* src_mask */
287 0, /* dst_mask */
288 FALSE), /* pcrel_offset */
35c08157
KLC
289
290 /* A 16 bit absolute relocation. */
fbaf61ad
NC
291 HOWTO2 (R_NDS32_16, /* type */
292 0, /* rightshift */
293 1, /* size (0 = byte, 1 = short, 2 = long) */
294 16, /* bitsize */
295 FALSE, /* pc_relative */
296 0, /* bitpos */
297 complain_overflow_bitfield,/* complain_on_overflow */
298 nds32_elf_generic_reloc,/* special_function */
299 "R_NDS32_16", /* name */
300 FALSE, /* partial_inplace */
301 0xffff, /* src_mask */
302 0xffff, /* dst_mask */
303 FALSE), /* pcrel_offset */
35c08157
KLC
304
305 /* A 32 bit absolute relocation. */
fbaf61ad
NC
306 HOWTO2 (R_NDS32_32, /* type */
307 0, /* rightshift */
308 2, /* size (0 = byte, 1 = short, 2 = long) */
309 32, /* bitsize */
310 FALSE, /* pc_relative */
311 0, /* bitpos */
312 complain_overflow_bitfield,/* complain_on_overflow */
313 nds32_elf_generic_reloc,/* special_function */
314 "R_NDS32_32", /* name */
315 FALSE, /* partial_inplace */
316 0xffffffff, /* src_mask */
317 0xffffffff, /* dst_mask */
318 FALSE), /* pcrel_offset */
35c08157
KLC
319
320 /* A 20 bit address. */
fbaf61ad
NC
321 HOWTO2 (R_NDS32_20, /* type */
322 0, /* rightshift */
323 2, /* size (0 = byte, 1 = short, 2 = long) */
324 20, /* bitsize */
325 FALSE, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_unsigned,/* complain_on_overflow */
328 nds32_elf_generic_reloc,/* special_function */
329 "R_NDS32_20", /* name */
330 FALSE, /* partial_inplace */
331 0xfffff, /* src_mask */
332 0xfffff, /* dst_mask */
333 FALSE), /* pcrel_offset */
35c08157
KLC
334
335 /* An PC Relative 9-bit relocation, shifted by 2.
336 This reloc is complicated because relocations are relative to pc & -4.
337 i.e. branches in the right insn slot use the address of the left insn
338 slot for pc. */
fbaf61ad 339 /* It's not clear whether this should have partial_inplace set or not.
35c08157
KLC
340 Branch relaxing in the assembler can store the addend in the insn,
341 and if bfd_install_relocation gets called the addend may get added
342 again. */
fbaf61ad
NC
343 HOWTO2 (R_NDS32_9_PCREL, /* type */
344 1, /* rightshift */
345 1, /* size (0 = byte, 1 = short, 2 = long) */
346 8, /* bitsize */
347 TRUE, /* pc_relative */
348 0, /* bitpos */
349 complain_overflow_signed,/* complain_on_overflow */
350 nds32_elf_9_pcrel_reloc,/* special_function */
351 "R_NDS32_9_PCREL", /* name */
352 FALSE, /* partial_inplace */
353 0xff, /* src_mask */
354 0xff, /* dst_mask */
355 TRUE), /* pcrel_offset */
35c08157
KLC
356
357 /* A relative 15 bit relocation, right shifted by 1. */
fbaf61ad
NC
358 HOWTO2 (R_NDS32_15_PCREL, /* type */
359 1, /* rightshift */
360 2, /* size (0 = byte, 1 = short, 2 = long) */
361 14, /* bitsize */
362 TRUE, /* pc_relative */
363 0, /* bitpos */
364 complain_overflow_signed,/* complain_on_overflow */
365 bfd_elf_generic_reloc, /* special_function */
366 "R_NDS32_15_PCREL", /* name */
367 FALSE, /* partial_inplace */
368 0x3fff, /* src_mask */
369 0x3fff, /* dst_mask */
370 TRUE), /* pcrel_offset */
35c08157
KLC
371
372 /* A relative 17 bit relocation, right shifted by 1. */
fbaf61ad
NC
373 HOWTO2 (R_NDS32_17_PCREL, /* type */
374 1, /* rightshift */
375 2, /* size (0 = byte, 1 = short, 2 = long) */
376 16, /* bitsize */
377 TRUE, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_signed,/* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_NDS32_17_PCREL", /* name */
382 FALSE, /* partial_inplace */
383 0xffff, /* src_mask */
384 0xffff, /* dst_mask */
385 TRUE), /* pcrel_offset */
35c08157
KLC
386
387 /* A relative 25 bit relocation, right shifted by 1. */
fbaf61ad 388 /* It's not clear whether this should have partial_inplace set or not.
35c08157
KLC
389 Branch relaxing in the assembler can store the addend in the insn,
390 and if bfd_install_relocation gets called the addend may get added
391 again. */
fbaf61ad
NC
392 HOWTO2 (R_NDS32_25_PCREL, /* type */
393 1, /* rightshift */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
395 24, /* bitsize */
396 TRUE, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_signed,/* complain_on_overflow */
399 bfd_elf_generic_reloc, /* special_function */
400 "R_NDS32_25_PCREL", /* name */
401 FALSE, /* partial_inplace */
402 0xffffff, /* src_mask */
403 0xffffff, /* dst_mask */
404 TRUE), /* pcrel_offset */
35c08157
KLC
405
406 /* High 20 bits of address when lower 12 is or'd in. */
fbaf61ad
NC
407 HOWTO2 (R_NDS32_HI20, /* type */
408 12, /* rightshift */
409 2, /* size (0 = byte, 1 = short, 2 = long) */
410 20, /* bitsize */
411 FALSE, /* pc_relative */
412 0, /* bitpos */
413 complain_overflow_dont,/* complain_on_overflow */
414 nds32_elf_hi20_reloc, /* special_function */
415 "R_NDS32_HI20", /* name */
416 FALSE, /* partial_inplace */
417 0x000fffff, /* src_mask */
418 0x000fffff, /* dst_mask */
419 FALSE), /* pcrel_offset */
35c08157
KLC
420
421 /* Lower 12 bits of address. */
fbaf61ad
NC
422 HOWTO2 (R_NDS32_LO12S3, /* type */
423 3, /* rightshift */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
425 9, /* bitsize */
426 FALSE, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_dont,/* complain_on_overflow */
429 nds32_elf_lo12_reloc, /* special_function */
430 "R_NDS32_LO12S3", /* name */
431 FALSE, /* partial_inplace */
432 0x000001ff, /* src_mask */
433 0x000001ff, /* dst_mask */
434 FALSE), /* pcrel_offset */
35c08157
KLC
435
436 /* Lower 12 bits of address. */
fbaf61ad
NC
437 HOWTO2 (R_NDS32_LO12S2, /* type */
438 2, /* rightshift */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
440 10, /* bitsize */
441 FALSE, /* pc_relative */
442 0, /* bitpos */
443 complain_overflow_dont,/* complain_on_overflow */
444 nds32_elf_lo12_reloc, /* special_function */
445 "R_NDS32_LO12S2", /* name */
446 FALSE, /* partial_inplace */
447 0x000003ff, /* src_mask */
448 0x000003ff, /* dst_mask */
449 FALSE), /* pcrel_offset */
35c08157
KLC
450
451 /* Lower 12 bits of address. */
fbaf61ad
NC
452 HOWTO2 (R_NDS32_LO12S1, /* type */
453 1, /* rightshift */
454 2, /* size (0 = byte, 1 = short, 2 = long) */
455 11, /* bitsize */
456 FALSE, /* pc_relative */
457 0, /* bitpos */
458 complain_overflow_dont,/* complain_on_overflow */
459 nds32_elf_lo12_reloc, /* special_function */
460 "R_NDS32_LO12S1", /* name */
461 FALSE, /* partial_inplace */
462 0x000007ff, /* src_mask */
463 0x000007ff, /* dst_mask */
464 FALSE), /* pcrel_offset */
35c08157
KLC
465
466 /* Lower 12 bits of address. */
fbaf61ad
NC
467 HOWTO2 (R_NDS32_LO12S0, /* type */
468 0, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 12, /* bitsize */
471 FALSE, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_dont,/* complain_on_overflow */
474 nds32_elf_lo12_reloc, /* special_function */
475 "R_NDS32_LO12S0", /* name */
476 FALSE, /* partial_inplace */
477 0x00000fff, /* src_mask */
478 0x00000fff, /* dst_mask */
479 FALSE), /* pcrel_offset */
35c08157
KLC
480
481 /* Small data area 15 bits offset. */
fbaf61ad
NC
482 HOWTO2 (R_NDS32_SDA15S3, /* type */
483 3, /* rightshift */
484 2, /* size (0 = byte, 1 = short, 2 = long) */
485 15, /* bitsize */
486 FALSE, /* pc_relative */
487 0, /* bitpos */
488 complain_overflow_signed,/* complain_on_overflow */
489 nds32_elf_sda15_reloc, /* special_function */
490 "R_NDS32_SDA15S3", /* name */
491 FALSE, /* partial_inplace */
492 0x00007fff, /* src_mask */
493 0x00007fff, /* dst_mask */
494 FALSE), /* pcrel_offset */
35c08157
KLC
495
496 /* Small data area 15 bits offset. */
fbaf61ad
NC
497 HOWTO2 (R_NDS32_SDA15S2, /* type */
498 2, /* rightshift */
499 2, /* size (0 = byte, 1 = short, 2 = long) */
500 15, /* bitsize */
501 FALSE, /* pc_relative */
502 0, /* bitpos */
503 complain_overflow_signed,/* complain_on_overflow */
504 nds32_elf_sda15_reloc, /* special_function */
505 "R_NDS32_SDA15S2", /* name */
506 FALSE, /* partial_inplace */
507 0x00007fff, /* src_mask */
508 0x00007fff, /* dst_mask */
509 FALSE), /* pcrel_offset */
35c08157
KLC
510
511 /* Small data area 15 bits offset. */
fbaf61ad
NC
512 HOWTO2 (R_NDS32_SDA15S1, /* type */
513 1, /* rightshift */
514 2, /* size (0 = byte, 1 = short, 2 = long) */
515 15, /* bitsize */
516 FALSE, /* pc_relative */
517 0, /* bitpos */
518 complain_overflow_signed,/* complain_on_overflow */
519 nds32_elf_sda15_reloc, /* special_function */
520 "R_NDS32_SDA15S1", /* name */
521 FALSE, /* partial_inplace */
522 0x00007fff, /* src_mask */
523 0x00007fff, /* dst_mask */
524 FALSE), /* pcrel_offset */
35c08157
KLC
525
526 /* Small data area 15 bits offset. */
fbaf61ad
NC
527 HOWTO2 (R_NDS32_SDA15S0, /* type */
528 0, /* rightshift */
529 2, /* size (0 = byte, 1 = short, 2 = long) */
530 15, /* bitsize */
531 FALSE, /* pc_relative */
532 0, /* bitpos */
533 complain_overflow_signed,/* complain_on_overflow */
534 nds32_elf_sda15_reloc, /* special_function */
535 "R_NDS32_SDA15S0", /* name */
536 FALSE, /* partial_inplace */
537 0x00007fff, /* src_mask */
538 0x00007fff, /* dst_mask */
539 FALSE), /* pcrel_offset */
540
541 /* GNU extension to record C++ vtable hierarchy */
542 HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
543 0, /* rightshift */
544 2, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
546 FALSE, /* pc_relative */
547 0, /* bitpos */
548 complain_overflow_dont,/* complain_on_overflow */
549 NULL, /* special_function */
550 "R_NDS32_GNU_VTINHERIT",/* name */
551 FALSE, /* partial_inplace */
552 0, /* src_mask */
553 0, /* dst_mask */
554 FALSE), /* pcrel_offset */
555
556 /* GNU extension to record C++ vtable member usage */
557 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
558 0, /* rightshift */
559 2, /* size (0 = byte, 1 = short, 2 = long) */
560 0, /* bitsize */
561 FALSE, /* pc_relative */
562 0, /* bitpos */
563 complain_overflow_dont,/* complain_on_overflow */
564 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
565 "R_NDS32_GNU_VTENTRY", /* name */
566 FALSE, /* partial_inplace */
567 0, /* src_mask */
568 0, /* dst_mask */
569 FALSE), /* pcrel_offset */
35c08157
KLC
570
571 /* A 16 bit absolute relocation. */
fbaf61ad
NC
572 HOWTO2 (R_NDS32_16_RELA, /* type */
573 0, /* rightshift */
574 1, /* size (0 = byte, 1 = short, 2 = long) */
575 16, /* bitsize */
576 FALSE, /* pc_relative */
577 0, /* bitpos */
578 complain_overflow_bitfield,/* complain_on_overflow */
579 bfd_elf_generic_reloc, /* special_function */
580 "R_NDS32_16_RELA", /* name */
581 FALSE, /* partial_inplace */
582 0xffff, /* src_mask */
583 0xffff, /* dst_mask */
584 FALSE), /* pcrel_offset */
35c08157
KLC
585
586 /* A 32 bit absolute relocation. */
fbaf61ad
NC
587 HOWTO2 (R_NDS32_32_RELA, /* type */
588 0, /* rightshift */
589 2, /* size (0 = byte, 1 = short, 2 = long) */
590 32, /* bitsize */
591 FALSE, /* pc_relative */
592 0, /* bitpos */
593 complain_overflow_bitfield,/* complain_on_overflow */
594 bfd_elf_generic_reloc, /* special_function */
595 "R_NDS32_32_RELA", /* name */
596 FALSE, /* partial_inplace */
597 0xffffffff, /* src_mask */
598 0xffffffff, /* dst_mask */
599 FALSE), /* pcrel_offset */
35c08157
KLC
600
601 /* A 20 bit address. */
fbaf61ad
NC
602 HOWTO2 (R_NDS32_20_RELA, /* type */
603 0, /* rightshift */
604 2, /* size (0 = byte, 1 = short, 2 = long) */
605 20, /* bitsize */
606 FALSE, /* pc_relative */
607 0, /* bitpos */
608 complain_overflow_signed,/* complain_on_overflow */
609 bfd_elf_generic_reloc, /* special_function */
610 "R_NDS32_20_RELA", /* name */
611 FALSE, /* partial_inplace */
612 0xfffff, /* src_mask */
613 0xfffff, /* dst_mask */
614 FALSE), /* pcrel_offset */
615
616 HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
617 1, /* rightshift */
618 1, /* size (0 = byte, 1 = short, 2 = long) */
619 8, /* bitsize */
620 TRUE, /* pc_relative */
621 0, /* bitpos */
622 complain_overflow_signed,/* complain_on_overflow */
623 bfd_elf_generic_reloc, /* special_function */
624 "R_NDS32_9_PCREL_RELA",/* name */
625 FALSE, /* partial_inplace */
626 0xff, /* src_mask */
627 0xff, /* dst_mask */
628 TRUE), /* pcrel_offset */
35c08157
KLC
629
630 /* A relative 15 bit relocation, right shifted by 1. */
fbaf61ad
NC
631 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
632 1, /* rightshift */
633 2, /* size (0 = byte, 1 = short, 2 = long) */
634 14, /* bitsize */
635 TRUE, /* pc_relative */
636 0, /* bitpos */
637 complain_overflow_signed,/* complain_on_overflow */
638 bfd_elf_generic_reloc, /* special_function */
639 "R_NDS32_15_PCREL_RELA",/* name */
640 FALSE, /* partial_inplace */
641 0x3fff, /* src_mask */
642 0x3fff, /* dst_mask */
643 TRUE), /* pcrel_offset */
35c08157
KLC
644
645 /* A relative 17 bit relocation, right shifted by 1. */
fbaf61ad
NC
646 HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
647 1, /* rightshift */
648 2, /* size (0 = byte, 1 = short, 2 = long) */
649 16, /* bitsize */
650 TRUE, /* pc_relative */
651 0, /* bitpos */
652 complain_overflow_signed,/* complain_on_overflow */
653 bfd_elf_generic_reloc, /* special_function */
654 "R_NDS32_17_PCREL_RELA",/* name */
655 FALSE, /* partial_inplace */
656 0xffff, /* src_mask */
657 0xffff, /* dst_mask */
658 TRUE), /* pcrel_offset */
35c08157
KLC
659
660 /* A relative 25 bit relocation, right shifted by 2. */
fbaf61ad
NC
661 HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
662 1, /* rightshift */
663 2, /* size (0 = byte, 1 = short, 2 = long) */
664 24, /* bitsize */
665 TRUE, /* pc_relative */
666 0, /* bitpos */
667 complain_overflow_signed,/* complain_on_overflow */
668 bfd_elf_generic_reloc, /* special_function */
669 "R_NDS32_25_PCREL_RELA",/* name */
670 FALSE, /* partial_inplace */
671 0xffffff, /* src_mask */
672 0xffffff, /* dst_mask */
673 TRUE), /* pcrel_offset */
35c08157
KLC
674
675 /* High 20 bits of address when lower 16 is or'd in. */
fbaf61ad
NC
676 HOWTO2 (R_NDS32_HI20_RELA, /* type */
677 12, /* rightshift */
678 2, /* size (0 = byte, 1 = short, 2 = long) */
679 20, /* bitsize */
680 FALSE, /* pc_relative */
681 0, /* bitpos */
682 complain_overflow_dont,/* complain_on_overflow */
683 bfd_elf_generic_reloc, /* special_function */
684 "R_NDS32_HI20_RELA", /* name */
685 FALSE, /* partial_inplace */
686 0x000fffff, /* src_mask */
687 0x000fffff, /* dst_mask */
688 FALSE), /* pcrel_offset */
35c08157
KLC
689
690 /* Lower 12 bits of address. */
fbaf61ad
NC
691 HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
692 3, /* rightshift */
693 2, /* size (0 = byte, 1 = short, 2 = long) */
694 9, /* bitsize */
695 FALSE, /* pc_relative */
696 0, /* bitpos */
697 complain_overflow_dont,/* complain_on_overflow */
698 bfd_elf_generic_reloc, /* special_function */
699 "R_NDS32_LO12S3_RELA", /* name */
700 FALSE, /* partial_inplace */
701 0x000001ff, /* src_mask */
702 0x000001ff, /* dst_mask */
703 FALSE), /* pcrel_offset */
35c08157
KLC
704
705 /* Lower 12 bits of address. */
fbaf61ad
NC
706 HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
707 2, /* rightshift */
708 2, /* size (0 = byte, 1 = short, 2 = long) */
709 10, /* bitsize */
710 FALSE, /* pc_relative */
711 0, /* bitpos */
712 complain_overflow_dont,/* complain_on_overflow */
713 bfd_elf_generic_reloc, /* special_function */
714 "R_NDS32_LO12S2_RELA", /* name */
715 FALSE, /* partial_inplace */
716 0x000003ff, /* src_mask */
717 0x000003ff, /* dst_mask */
718 FALSE), /* pcrel_offset */
35c08157
KLC
719
720 /* Lower 12 bits of address. */
fbaf61ad
NC
721 HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
722 1, /* rightshift */
723 2, /* size (0 = byte, 1 = short, 2 = long) */
724 11, /* bitsize */
725 FALSE, /* pc_relative */
726 0, /* bitpos */
727 complain_overflow_dont,/* complain_on_overflow */
728 bfd_elf_generic_reloc, /* special_function */
729 "R_NDS32_LO12S1_RELA", /* name */
730 FALSE, /* partial_inplace */
731 0x000007ff, /* src_mask */
732 0x000007ff, /* dst_mask */
733 FALSE), /* pcrel_offset */
35c08157
KLC
734
735 /* Lower 12 bits of address. */
fbaf61ad
NC
736 HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
737 0, /* rightshift */
738 2, /* size (0 = byte, 1 = short, 2 = long) */
739 12, /* bitsize */
740 FALSE, /* pc_relative */
741 0, /* bitpos */
742 complain_overflow_dont,/* complain_on_overflow */
743 bfd_elf_generic_reloc, /* special_function */
744 "R_NDS32_LO12S0_RELA", /* name */
745 FALSE, /* partial_inplace */
746 0x00000fff, /* src_mask */
747 0x00000fff, /* dst_mask */
748 FALSE), /* pcrel_offset */
35c08157
KLC
749
750 /* Small data area 15 bits offset. */
fbaf61ad
NC
751 HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
752 3, /* rightshift */
753 2, /* size (0 = byte, 1 = short, 2 = long) */
754 15, /* bitsize */
755 FALSE, /* pc_relative */
756 0, /* bitpos */
757 complain_overflow_signed,/* complain_on_overflow */
758 bfd_elf_generic_reloc, /* special_function */
759 "R_NDS32_SDA15S3_RELA",/* name */
760 FALSE, /* partial_inplace */
761 0x00007fff, /* src_mask */
762 0x00007fff, /* dst_mask */
763 FALSE), /* pcrel_offset */
35c08157
KLC
764
765 /* Small data area 15 bits offset. */
fbaf61ad
NC
766 HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
767 2, /* rightshift */
768 2, /* size (0 = byte, 1 = short, 2 = long) */
769 15, /* bitsize */
770 FALSE, /* pc_relative */
771 0, /* bitpos */
772 complain_overflow_signed,/* complain_on_overflow */
773 bfd_elf_generic_reloc, /* special_function */
774 "R_NDS32_SDA15S2_RELA",/* name */
775 FALSE, /* partial_inplace */
776 0x00007fff, /* src_mask */
777 0x00007fff, /* dst_mask */
778 FALSE), /* pcrel_offset */
779
780 HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
781 1, /* rightshift */
782 2, /* size (0 = byte, 1 = short, 2 = long) */
783 15, /* bitsize */
784 FALSE, /* pc_relative */
785 0, /* bitpos */
786 complain_overflow_signed,/* complain_on_overflow */
787 bfd_elf_generic_reloc, /* special_function */
788 "R_NDS32_SDA15S1_RELA",/* name */
789 FALSE, /* partial_inplace */
790 0x00007fff, /* src_mask */
791 0x00007fff, /* dst_mask */
792 FALSE), /* pcrel_offset */
793
794 HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
795 0, /* rightshift */
796 2, /* size (0 = byte, 1 = short, 2 = long) */
797 15, /* bitsize */
798 FALSE, /* pc_relative */
799 0, /* bitpos */
800 complain_overflow_signed,/* complain_on_overflow */
801 bfd_elf_generic_reloc, /* special_function */
802 "R_NDS32_SDA15S0_RELA",/* name */
803 FALSE, /* partial_inplace */
804 0x00007fff, /* src_mask */
805 0x00007fff, /* dst_mask */
806 FALSE), /* pcrel_offset */
807
808 /* GNU extension to record C++ vtable hierarchy */
809 HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
810 0, /* rightshift */
811 2, /* size (0 = byte, 1 = short, 2 = long) */
812 0, /* bitsize */
813 FALSE, /* pc_relative */
814 0, /* bitpos */
815 complain_overflow_dont,/* complain_on_overflow */
816 NULL, /* special_function */
817 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
818 FALSE, /* partial_inplace */
819 0, /* src_mask */
820 0, /* dst_mask */
821 FALSE), /* pcrel_offset */
822
823 /* GNU extension to record C++ vtable member usage */
824 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
825 0, /* rightshift */
826 2, /* size (0 = byte, 1 = short, 2 = long) */
827 0, /* bitsize */
828 FALSE, /* pc_relative */
829 0, /* bitpos */
830 complain_overflow_dont,/* complain_on_overflow */
831 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
832 "R_NDS32_RELA_GNU_VTENTRY",/* name */
833 FALSE, /* partial_inplace */
834 0, /* src_mask */
835 0, /* dst_mask */
836 FALSE), /* pcrel_offset */
35c08157
KLC
837
838 /* Like R_NDS32_20, but referring to the GOT table entry for
839 the symbol. */
fbaf61ad
NC
840 HOWTO2 (R_NDS32_GOT20, /* type */
841 0, /* rightshift */
842 2, /* size (0 = byte, 1 = short, 2 = long) */
843 20, /* bitsize */
844 FALSE, /* pc_relative */
845 0, /* bitpos */
846 complain_overflow_signed,/* complain_on_overflow */
847 bfd_elf_generic_reloc, /* special_function */
848 "R_NDS32_GOT20", /* name */
849 FALSE, /* partial_inplace */
850 0xfffff, /* src_mask */
851 0xfffff, /* dst_mask */
852 FALSE), /* pcrel_offset */
35c08157
KLC
853
854 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
855 entry for the symbol. */
fbaf61ad
NC
856 HOWTO2 (R_NDS32_25_PLTREL, /* type */
857 1, /* rightshift */
858 2, /* size (0 = byte, 1 = short, 2 = long) */
859 24, /* bitsize */
860 TRUE, /* pc_relative */
861 0, /* bitpos */
862 complain_overflow_signed,/* complain_on_overflow */
863 bfd_elf_generic_reloc, /* special_function */
864 "R_NDS32_25_PLTREL", /* name */
865 FALSE, /* partial_inplace */
866 0xffffff, /* src_mask */
867 0xffffff, /* dst_mask */
868 TRUE), /* pcrel_offset */
35c08157
KLC
869
870 /* This is used only by the dynamic linker. The symbol should exist
871 both in the object being run and in some shared library. The
872 dynamic linker copies the data addressed by the symbol from the
873 shared library into the object, because the object being
874 run has to have the data at some particular address. */
fbaf61ad
NC
875 HOWTO2 (R_NDS32_COPY, /* type */
876 0, /* rightshift */
877 2, /* size (0 = byte, 1 = short, 2 = long) */
878 32, /* bitsize */
879 FALSE, /* pc_relative */
880 0, /* bitpos */
881 complain_overflow_bitfield,/* complain_on_overflow */
882 bfd_elf_generic_reloc, /* special_function */
883 "R_NDS32_COPY", /* name */
884 FALSE, /* partial_inplace */
885 0xffffffff, /* src_mask */
886 0xffffffff, /* dst_mask */
887 FALSE), /* pcrel_offset */
35c08157
KLC
888
889 /* Like R_NDS32_20, but used when setting global offset table
890 entries. */
fbaf61ad
NC
891 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
892 0, /* rightshift */
893 2, /* size (0 = byte, 1 = short, 2 = long) */
894 32, /* bitsize */
895 FALSE, /* pc_relative */
896 0, /* bitpos */
897 complain_overflow_bitfield,/* complain_on_overflow */
898 bfd_elf_generic_reloc, /* special_function */
899 "R_NDS32_GLOB_DAT", /* name */
900 FALSE, /* partial_inplace */
901 0xffffffff, /* src_mask */
902 0xffffffff, /* dst_mask */
903 FALSE), /* pcrel_offset */
35c08157
KLC
904
905 /* Marks a procedure linkage table entry for a symbol. */
fbaf61ad
NC
906 HOWTO2 (R_NDS32_JMP_SLOT, /* type */
907 0, /* rightshift */
908 2, /* size (0 = byte, 1 = short, 2 = long) */
909 32, /* bitsize */
910 FALSE, /* pc_relative */
911 0, /* bitpos */
912 complain_overflow_bitfield,/* complain_on_overflow */
913 bfd_elf_generic_reloc, /* special_function */
914 "R_NDS32_JMP_SLOT", /* name */
915 FALSE, /* partial_inplace */
916 0xffffffff, /* src_mask */
917 0xffffffff, /* dst_mask */
918 FALSE), /* pcrel_offset */
35c08157
KLC
919
920 /* Used only by the dynamic linker. When the object is run, this
921 longword is set to the load address of the object, plus the
922 addend. */
fbaf61ad
NC
923 HOWTO2 (R_NDS32_RELATIVE, /* type */
924 0, /* rightshift */
925 2, /* size (0 = byte, 1 = short, 2 = long) */
926 32, /* bitsize */
927 FALSE, /* pc_relative */
928 0, /* bitpos */
929 complain_overflow_bitfield,/* complain_on_overflow */
930 bfd_elf_generic_reloc, /* special_function */
931 "R_NDS32_RELATIVE", /* name */
932 FALSE, /* partial_inplace */
933 0xffffffff, /* src_mask */
934 0xffffffff, /* dst_mask */
935 FALSE), /* pcrel_offset */
936
937 HOWTO2 (R_NDS32_GOTOFF, /* type */
938 0, /* rightshift */
939 2, /* size (0 = byte, 1 = short, 2 = long) */
940 20, /* bitsize */
941 FALSE, /* pc_relative */
942 0, /* bitpos */
943 complain_overflow_signed,/* complain_on_overflow */
944 bfd_elf_generic_reloc, /* special_function */
945 "R_NDS32_GOTOFF", /* name */
946 FALSE, /* partial_inplace */
947 0xfffff, /* src_mask */
948 0xfffff, /* dst_mask */
949 FALSE), /* pcrel_offset */
35c08157
KLC
950
951 /* An PC Relative 20-bit relocation used when setting PIC offset
952 table register. */
fbaf61ad
NC
953 HOWTO2 (R_NDS32_GOTPC20, /* type */
954 0, /* rightshift */
955 2, /* size (0 = byte, 1 = short, 2 = long) */
956 20, /* bitsize */
957 TRUE, /* pc_relative */
958 0, /* bitpos */
959 complain_overflow_signed,/* complain_on_overflow */
960 bfd_elf_generic_reloc, /* special_function */
961 "R_NDS32_GOTPC20", /* name */
962 FALSE, /* partial_inplace */
963 0xfffff, /* src_mask */
964 0xfffff, /* dst_mask */
965 TRUE), /* pcrel_offset */
35c08157
KLC
966
967 /* Like R_NDS32_HI20, but referring to the GOT table entry for
968 the symbol. */
fbaf61ad
NC
969 HOWTO2 (R_NDS32_GOT_HI20, /* type */
970 12, /* rightshift */
971 2, /* size (0 = byte, 1 = short, 2 = long) */
972 20, /* bitsize */
973 FALSE, /* pc_relative */
974 0, /* bitpos */
975 complain_overflow_dont,/* complain_on_overflow */
976 bfd_elf_generic_reloc, /* special_function */
977 "R_NDS32_GOT_HI20", /* name */
978 FALSE, /* partial_inplace */
979 0x000fffff, /* src_mask */
980 0x000fffff, /* dst_mask */
981 FALSE), /* pcrel_offset */
982 HOWTO2 (R_NDS32_GOT_LO12, /* type */
983 0, /* rightshift */
984 2, /* size (0 = byte, 1 = short, 2 = long) */
985 12, /* bitsize */
986 FALSE, /* pc_relative */
987 0, /* bitpos */
988 complain_overflow_dont,/* complain_on_overflow */
989 bfd_elf_generic_reloc, /* special_function */
990 "R_NDS32_GOT_LO12", /* name */
991 FALSE, /* partial_inplace */
992 0x00000fff, /* src_mask */
993 0x00000fff, /* dst_mask */
994 FALSE), /* pcrel_offset */
35c08157
KLC
995
996 /* An PC Relative relocation used when setting PIC offset table register.
997 Like R_NDS32_HI20, but referring to the GOT table entry for
998 the symbol. */
fbaf61ad
NC
999 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
1000 12, /* rightshift */
1001 2, /* size (0 = byte, 1 = short, 2 = long) */
1002 20, /* bitsize */
1003 FALSE, /* pc_relative */
1004 0, /* bitpos */
1005 complain_overflow_dont,/* complain_on_overflow */
1006 bfd_elf_generic_reloc, /* special_function */
1007 "R_NDS32_GOTPC_HI20", /* name */
1008 FALSE, /* partial_inplace */
1009 0x000fffff, /* src_mask */
1010 0x000fffff, /* dst_mask */
1011 TRUE), /* pcrel_offset */
1012 HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
1013 0, /* rightshift */
1014 2, /* size (0 = byte, 1 = short, 2 = long) */
1015 12, /* bitsize */
1016 FALSE, /* pc_relative */
1017 0, /* bitpos */
1018 complain_overflow_dont,/* complain_on_overflow */
1019 bfd_elf_generic_reloc, /* special_function */
1020 "R_NDS32_GOTPC_LO12", /* name */
1021 FALSE, /* partial_inplace */
1022 0x00000fff, /* src_mask */
1023 0x00000fff, /* dst_mask */
1024 TRUE), /* pcrel_offset */
1025
1026 HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
1027 12, /* rightshift */
1028 2, /* size (0 = byte, 1 = short, 2 = long) */
1029 20, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_dont,/* complain_on_overflow */
1033 bfd_elf_generic_reloc, /* special_function */
1034 "R_NDS32_GOTOFF_HI20", /* name */
1035 FALSE, /* partial_inplace */
1036 0x000fffff, /* src_mask */
1037 0x000fffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1039 HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
1040 0, /* rightshift */
1041 2, /* size (0 = byte, 1 = short, 2 = long) */
1042 12, /* bitsize */
1043 FALSE, /* pc_relative */
1044 0, /* bitpos */
1045 complain_overflow_dont,/* complain_on_overflow */
1046 bfd_elf_generic_reloc, /* special_function */
1047 "R_NDS32_GOTOFF_LO12", /* name */
1048 FALSE, /* partial_inplace */
1049 0x00000fff, /* src_mask */
1050 0x00000fff, /* dst_mask */
1051 FALSE), /* pcrel_offset */
35c08157
KLC
1052
1053 /* Alignment hint for relaxable instruction. This is used with
1054 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1055 in order to make next label aligned on word boundary. */
fbaf61ad
NC
1056 HOWTO2 (R_NDS32_INSN16, /* type */
1057 0, /* rightshift */
1058 2, /* size (0 = byte, 1 = short, 2 = long) */
1059 32, /* bitsize */
1060 FALSE, /* pc_relative */
1061 0, /* bitpos */
1062 complain_overflow_dont,/* complain_on_overflow */
1063 nds32_elf_ignore_reloc,/* special_function */
1064 "R_NDS32_INSN16", /* name */
1065 FALSE, /* partial_inplace */
1066 0x00000fff, /* src_mask */
1067 0x00000fff, /* dst_mask */
1068 FALSE), /* pcrel_offset */
35c08157
KLC
1069
1070 /* Alignment hint for label. */
fbaf61ad
NC
1071 HOWTO2 (R_NDS32_LABEL, /* type */
1072 0, /* rightshift */
1073 2, /* size (0 = byte, 1 = short, 2 = long) */
1074 32, /* bitsize */
1075 FALSE, /* pc_relative */
1076 0, /* bitpos */
1077 complain_overflow_dont,/* complain_on_overflow */
1078 nds32_elf_ignore_reloc,/* special_function */
1079 "R_NDS32_LABEL", /* name */
1080 FALSE, /* partial_inplace */
1081 0xffffffff, /* src_mask */
1082 0xffffffff, /* dst_mask */
1083 FALSE), /* pcrel_offset */
35c08157
KLC
1084
1085 /* Relax hint for unconditional call sequence */
fbaf61ad
NC
1086 HOWTO2 (R_NDS32_LONGCALL1, /* type */
1087 0, /* rightshift */
1088 2, /* size (0 = byte, 1 = short, 2 = long) */
1089 32, /* bitsize */
1090 FALSE, /* pc_relative */
1091 0, /* bitpos */
1092 complain_overflow_dont,/* complain_on_overflow */
1093 nds32_elf_ignore_reloc,/* special_function */
1094 "R_NDS32_LONGCALL1", /* name */
1095 FALSE, /* partial_inplace */
1096 0xffffffff, /* src_mask */
1097 0xffffffff, /* dst_mask */
1098 FALSE), /* pcrel_offset */
35c08157
KLC
1099
1100 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1101 HOWTO2 (R_NDS32_LONGCALL2, /* type */
1102 0, /* rightshift */
1103 2, /* size (0 = byte, 1 = short, 2 = long) */
1104 32, /* bitsize */
1105 FALSE, /* pc_relative */
1106 0, /* bitpos */
1107 complain_overflow_dont,/* complain_on_overflow */
1108 nds32_elf_ignore_reloc,/* special_function */
1109 "R_NDS32_LONGCALL2", /* name */
1110 FALSE, /* partial_inplace */
1111 0xffffffff, /* src_mask */
1112 0xffffffff, /* dst_mask */
1113 FALSE), /* pcrel_offset */
35c08157
KLC
1114
1115 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1116 HOWTO2 (R_NDS32_LONGCALL3, /* type */
1117 0, /* rightshift */
1118 2, /* size (0 = byte, 1 = short, 2 = long) */
1119 32, /* bitsize */
1120 FALSE, /* pc_relative */
1121 0, /* bitpos */
1122 complain_overflow_dont,/* complain_on_overflow */
1123 nds32_elf_ignore_reloc,/* special_function */
1124 "R_NDS32_LONGCALL3", /* name */
1125 FALSE, /* partial_inplace */
1126 0xffffffff, /* src_mask */
1127 0xffffffff, /* dst_mask */
1128 FALSE), /* pcrel_offset */
35c08157
KLC
1129
1130 /* Relax hint for unconditional branch sequence. */
fbaf61ad
NC
1131 HOWTO2 (R_NDS32_LONGJUMP1, /* type */
1132 0, /* rightshift */
1133 2, /* size (0 = byte, 1 = short, 2 = long) */
1134 32, /* bitsize */
1135 FALSE, /* pc_relative */
1136 0, /* bitpos */
1137 complain_overflow_dont,/* complain_on_overflow */
1138 nds32_elf_ignore_reloc,/* special_function */
1139 "R_NDS32_LONGJUMP1", /* name */
1140 FALSE, /* partial_inplace */
1141 0xffffffff, /* src_mask */
1142 0xffffffff, /* dst_mask */
1143 FALSE), /* pcrel_offset */
35c08157
KLC
1144
1145 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1146 HOWTO2 (R_NDS32_LONGJUMP2, /* type */
1147 0, /* rightshift */
1148 2, /* size (0 = byte, 1 = short, 2 = long) */
1149 32, /* bitsize */
1150 FALSE, /* pc_relative */
1151 0, /* bitpos */
1152 complain_overflow_dont,/* complain_on_overflow */
1153 nds32_elf_ignore_reloc,/* special_function */
1154 "R_NDS32_LONGJUMP2", /* name */
1155 FALSE, /* partial_inplace */
1156 0xffffffff, /* src_mask */
1157 0xffffffff, /* dst_mask */
1158 FALSE), /* pcrel_offset */
35c08157
KLC
1159
1160 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1161 HOWTO2 (R_NDS32_LONGJUMP3, /* type */
1162 0, /* rightshift */
1163 2, /* size (0 = byte, 1 = short, 2 = long) */
1164 32, /* bitsize */
1165 FALSE, /* pc_relative */
1166 0, /* bitpos */
1167 complain_overflow_dont,/* complain_on_overflow */
1168 nds32_elf_ignore_reloc,/* special_function */
1169 "R_NDS32_LONGJUMP3", /* name */
1170 FALSE, /* partial_inplace */
1171 0xffffffff, /* src_mask */
1172 0xffffffff, /* dst_mask */
1173 FALSE), /* pcrel_offset */
35c08157
KLC
1174
1175 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1176 HOWTO2 (R_NDS32_LOADSTORE, /* type */
1177 0, /* rightshift */
1178 2, /* size (0 = byte, 1 = short, 2 = long) */
1179 32, /* bitsize */
1180 FALSE, /* pc_relative */
1181 0, /* bitpos */
1182 complain_overflow_dont,/* complain_on_overflow */
1183 nds32_elf_ignore_reloc,/* special_function */
1184 "R_NDS32_LOADSTORE", /* name */
1185 FALSE, /* partial_inplace */
1186 0xffffffff, /* src_mask */
1187 0xffffffff, /* dst_mask */
1188 FALSE), /* pcrel_offset */
35c08157
KLC
1189
1190 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1191 HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
1192 0, /* rightshift */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1194 16, /* bitsize */
1195 FALSE, /* pc_relative */
1196 0, /* bitpos */
1197 complain_overflow_dont,/* complain_on_overflow */
1198 nds32_elf_ignore_reloc,/* special_function */
1199 "R_NDS32_9_FIXED_RELA",/* name */
1200 FALSE, /* partial_inplace */
1201 0x000000ff, /* src_mask */
1202 0x000000ff, /* dst_mask */
1203 FALSE), /* pcrel_offset */
35c08157
KLC
1204
1205 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1206 HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
1207 0, /* rightshift */
1208 2, /* size (0 = byte, 1 = short, 2 = long) */
1209 32, /* bitsize */
1210 FALSE, /* pc_relative */
1211 0, /* bitpos */
1212 complain_overflow_dont,/* complain_on_overflow */
1213 nds32_elf_ignore_reloc,/* special_function */
1214 "R_NDS32_15_FIXED_RELA",/* name */
1215 FALSE, /* partial_inplace */
1216 0x00003fff, /* src_mask */
1217 0x00003fff, /* dst_mask */
1218 FALSE), /* pcrel_offset */
35c08157
KLC
1219
1220 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1221 HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
1222 0, /* rightshift */
1223 2, /* size (0 = byte, 1 = short, 2 = long) */
1224 32, /* bitsize */
1225 FALSE, /* pc_relative */
1226 0, /* bitpos */
1227 complain_overflow_dont,/* complain_on_overflow */
1228 nds32_elf_ignore_reloc,/* special_function */
1229 "R_NDS32_17_FIXED_RELA",/* name */
1230 FALSE, /* partial_inplace */
1231 0x0000ffff, /* src_mask */
1232 0x0000ffff, /* dst_mask */
1233 FALSE), /* pcrel_offset */
35c08157
KLC
1234
1235 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1236 HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
1237 0, /* rightshift */
1238 2, /* size (0 = byte, 1 = short, 2 = long) */
1239 32, /* bitsize */
1240 FALSE, /* pc_relative */
1241 0, /* bitpos */
1242 complain_overflow_dont,/* complain_on_overflow */
1243 nds32_elf_ignore_reloc,/* special_function */
1244 "R_NDS32_25_FIXED_RELA",/* name */
1245 FALSE, /* partial_inplace */
1246 0x00ffffff, /* src_mask */
1247 0x00ffffff, /* dst_mask */
1248 FALSE), /* pcrel_offset */
35c08157
KLC
1249
1250 /* High 20 bits of PLT symbol offset relative to PC. */
fbaf61ad
NC
1251 HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
1252 12, /* rightshift */
1253 2, /* size (0 = byte, 1 = short, 2 = long) */
1254 20, /* bitsize */
1255 FALSE, /* pc_relative */
1256 0, /* bitpos */
1257 complain_overflow_dont,/* complain_on_overflow */
1258 bfd_elf_generic_reloc, /* special_function */
1259 "R_NDS32_PLTREL_HI20", /* name */
1260 FALSE, /* partial_inplace */
1261 0x000fffff, /* src_mask */
1262 0x000fffff, /* dst_mask */
1263 FALSE), /* pcrel_offset */
35c08157
KLC
1264
1265 /* Low 12 bits of PLT symbol offset relative to PC. */
fbaf61ad
NC
1266 HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
1267 0, /* rightshift */
1268 2, /* size (0 = byte, 1 = short, 2 = long) */
1269 12, /* bitsize */
1270 FALSE, /* pc_relative */
1271 0, /* bitpos */
1272 complain_overflow_dont,/* complain_on_overflow */
1273 bfd_elf_generic_reloc, /* special_function */
1274 "R_NDS32_PLTREL_LO12", /* name */
1275 FALSE, /* partial_inplace */
1276 0x00000fff, /* src_mask */
1277 0x00000fff, /* dst_mask */
1278 FALSE), /* pcrel_offset */
35c08157
KLC
1279
1280 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1281 HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
1282 12, /* rightshift */
1283 2, /* size (0 = byte, 1 = short, 2 = long) */
1284 20, /* bitsize */
1285 FALSE, /* pc_relative */
1286 0, /* bitpos */
1287 complain_overflow_dont,/* complain_on_overflow */
1288 bfd_elf_generic_reloc, /* special_function */
1289 "R_NDS32_PLT_GOTREL_HI20",/* name */
1290 FALSE, /* partial_inplace */
1291 0x000fffff, /* src_mask */
1292 0x000fffff, /* dst_mask */
1293 FALSE), /* pcrel_offset */
35c08157
KLC
1294
1295 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1296 HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
1297 0, /* rightshift */
1298 2, /* size (0 = byte, 1 = short, 2 = long) */
1299 12, /* bitsize */
1300 FALSE, /* pc_relative */
1301 0, /* bitpos */
1302 complain_overflow_dont,/* complain_on_overflow */
1303 bfd_elf_generic_reloc, /* special_function */
1304 "R_NDS32_PLT_GOTREL_LO12",/* name */
1305 FALSE, /* partial_inplace */
1306 0x00000fff, /* src_mask */
1307 0x00000fff, /* dst_mask */
1308 FALSE), /* pcrel_offset */
35c08157
KLC
1309
1310 /* Small data area 12 bits offset. */
fbaf61ad
NC
1311 HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
1312 2, /* rightshift */
1313 2, /* size (0 = byte, 1 = short, 2 = long) */
1314 12, /* bitsize */
1315 FALSE, /* pc_relative */
1316 0, /* bitpos */
1317 complain_overflow_signed,/* complain_on_overflow */
1318 bfd_elf_generic_reloc, /* special_function */
1319 "R_NDS32_SDA12S2_DP_RELA",/* name */
1320 FALSE, /* partial_inplace */
1321 0x00000fff, /* src_mask */
1322 0x00000fff, /* dst_mask */
1323 FALSE), /* pcrel_offset */
35c08157
KLC
1324
1325 /* Small data area 12 bits offset. */
fbaf61ad
NC
1326 HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
1327 2, /* rightshift */
1328 2, /* size (0 = byte, 1 = short, 2 = long) */
1329 12, /* bitsize */
1330 FALSE, /* pc_relative */
1331 0, /* bitpos */
1332 complain_overflow_signed,/* complain_on_overflow */
1333 bfd_elf_generic_reloc, /* special_function */
1334 "R_NDS32_SDA12S2_SP_RELA",/* name */
1335 FALSE, /* partial_inplace */
1336 0x00000fff, /* src_mask */
1337 0x00000fff, /* dst_mask */
1338 FALSE), /* pcrel_offset */
35c08157
KLC
1339 /* Lower 12 bits of address. */
1340
fbaf61ad
NC
1341 HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
1342 2, /* rightshift */
1343 2, /* size (0 = byte, 1 = short, 2 = long) */
1344 10, /* bitsize */
1345 FALSE, /* pc_relative */
1346 0, /* bitpos */
1347 complain_overflow_dont,/* complain_on_overflow */
1348 bfd_elf_generic_reloc, /* special_function */
1349 "R_NDS32_LO12S2_DP_RELA",/* name */
1350 FALSE, /* partial_inplace */
1351 0x000003ff, /* src_mask */
1352 0x000003ff, /* dst_mask */
1353 FALSE), /* pcrel_offset */
35c08157
KLC
1354
1355 /* Lower 12 bits of address. */
fbaf61ad
NC
1356 HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
1357 2, /* rightshift */
1358 2, /* size (0 = byte, 1 = short, 2 = long) */
1359 10, /* bitsize */
1360 FALSE, /* pc_relative */
1361 0, /* bitpos */
1362 complain_overflow_dont,/* complain_on_overflow */
1363 bfd_elf_generic_reloc, /* special_function */
1364 "R_NDS32_LO12S2_SP_RELA",/* name */
1365 FALSE, /* partial_inplace */
1366 0x000003ff, /* src_mask */
1367 0x000003ff, /* dst_mask */
1368 FALSE), /* pcrel_offset */
35c08157 1369 /* Lower 12 bits of address. Special identity for or case. */
fbaf61ad
NC
1370 HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
1371 0, /* rightshift */
1372 2, /* size (0 = byte, 1 = short, 2 = long) */
1373 12, /* bitsize */
1374 FALSE, /* pc_relative */
1375 0, /* bitpos */
1376 complain_overflow_dont,/* complain_on_overflow */
1377 bfd_elf_generic_reloc, /* special_function */
1378 "R_NDS32_LO12S0_ORI_RELA",/* name */
1379 FALSE, /* partial_inplace */
1380 0x00000fff, /* src_mask */
1381 0x00000fff, /* dst_mask */
1382 FALSE), /* pcrel_offset */
35c08157 1383 /* Small data area 19 bits offset. */
fbaf61ad
NC
1384 HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
1385 3, /* rightshift */
1386 2, /* size (0 = byte, 1 = short, 2 = long) */
1387 16, /* bitsize */
1388 FALSE, /* pc_relative */
1389 0, /* bitpos */
1390 complain_overflow_signed,/* complain_on_overflow */
1391 bfd_elf_generic_reloc, /* special_function */
1392 "R_NDS32_SDA16S3_RELA",/* name */
1393 FALSE, /* partial_inplace */
1394 0x0000ffff, /* src_mask */
1395 0x0000ffff, /* dst_mask */
1396 FALSE), /* pcrel_offset */
35c08157
KLC
1397
1398 /* Small data area 15 bits offset. */
fbaf61ad
NC
1399 HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
1400 2, /* rightshift */
1401 2, /* size (0 = byte, 1 = short, 2 = long) */
1402 17, /* bitsize */
1403 FALSE, /* pc_relative */
1404 0, /* bitpos */
1405 complain_overflow_signed,/* complain_on_overflow */
1406 bfd_elf_generic_reloc, /* special_function */
1407 "R_NDS32_SDA17S2_RELA",/* name */
1408 FALSE, /* partial_inplace */
1409 0x0001ffff, /* src_mask */
1410 0x0001ffff, /* dst_mask */
1411 FALSE), /* pcrel_offset */
1412
1413 HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
1414 1, /* rightshift */
1415 2, /* size (0 = byte, 1 = short, 2 = long) */
1416 18, /* bitsize */
1417 FALSE, /* pc_relative */
1418 0, /* bitpos */
1419 complain_overflow_signed,/* complain_on_overflow */
1420 bfd_elf_generic_reloc, /* special_function */
1421 "R_NDS32_SDA18S1_RELA",/* name */
1422 FALSE, /* partial_inplace */
1423 0x0003ffff, /* src_mask */
1424 0x0003ffff, /* dst_mask */
1425 FALSE), /* pcrel_offset */
1426
1427 HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
1428 0, /* rightshift */
1429 2, /* size (0 = byte, 1 = short, 2 = long) */
1430 19, /* bitsize */
1431 FALSE, /* pc_relative */
1432 0, /* bitpos */
1433 complain_overflow_signed,/* complain_on_overflow */
1434 bfd_elf_generic_reloc, /* special_function */
1435 "R_NDS32_SDA19S0_RELA",/* name */
1436 FALSE, /* partial_inplace */
1437 0x0007ffff, /* src_mask */
1438 0x0007ffff, /* dst_mask */
1439 FALSE), /* pcrel_offset */
1440 HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
1441 0, /* rightshift */
1442 0, /* size (0 = byte, 1 = short, 2 = long) */
1443 8, /* bitsize */
1444 FALSE, /* pc_relative */
1445 0, /* bitpos */
1446 complain_overflow_dont,/* complain_on_overflow */
1447 nds32_elf_ignore_reloc,/* special_function */
1448 "R_NDS32_DWARF2_OP1_RELA",/* name */
1449 FALSE, /* partial_inplace */
1450 0xff, /* src_mask */
1451 0xff, /* dst_mask */
1452 FALSE), /* pcrel_offset */
1453 HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_dont,/* complain_on_overflow */
1460 nds32_elf_ignore_reloc,/* special_function */
1461 "R_NDS32_DWARF2_OP2_RELA",/* name */
1462 FALSE, /* partial_inplace */
1463 0xffff, /* src_mask */
1464 0xffff, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466 HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
1467 0, /* rightshift */
1468 2, /* size (0 = byte, 1 = short, 2 = long) */
1469 32, /* bitsize */
1470 FALSE, /* pc_relative */
1471 0, /* bitpos */
1472 complain_overflow_dont,/* complain_on_overflow */
1473 nds32_elf_ignore_reloc,/* special_function */
1474 "R_NDS32_DWARF2_LEB_RELA",/* name */
1475 FALSE, /* partial_inplace */
1476 0xffffffff, /* src_mask */
1477 0xffffffff, /* dst_mask */
1478 FALSE), /* pcrel_offset */
1479 HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
1480 0, /* rightshift */
1481 1, /* size (0 = byte, 1 = short, 2 = long) */
1482 16, /* bitsize */
1483 FALSE, /* pc_relative */
1484 0, /* bitpos */
1485 complain_overflow_dont,/* complain_on_overflow */
1486 nds32_elf_ignore_reloc,/* special_function */
1487 "R_NDS32_UPDATE_TA_RELA",/* name */
1488 FALSE, /* partial_inplace */
1489 0xffff, /* src_mask */
1490 0xffff, /* dst_mask */
1491 FALSE), /* pcrel_offset */
35c08157
KLC
1492 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1493 entry for the symbol. */
fbaf61ad
NC
1494 HOWTO2 (R_NDS32_9_PLTREL, /* type */
1495 1, /* rightshift */
1496 1, /* size (0 = byte, 1 = short, 2 = long) */
1497 8, /* bitsize */
1498 TRUE, /* pc_relative */
1499 0, /* bitpos */
1500 complain_overflow_signed,/* complain_on_overflow */
1501 bfd_elf_generic_reloc, /* special_function */
1502 "R_NDS32_9_PLTREL", /* name */
1503 FALSE, /* partial_inplace */
1504 0xff, /* src_mask */
1505 0xff, /* dst_mask */
1506 TRUE), /* pcrel_offset */
35c08157 1507 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1508 HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
1509 0, /* rightshift */
1510 2, /* size (0 = byte, 1 = short, 2 = long) */
1511 20, /* bitsize */
1512 FALSE, /* pc_relative */
1513 0, /* bitpos */
1514 complain_overflow_dont,/* complain_on_overflow */
1515 bfd_elf_generic_reloc, /* special_function */
1516 "R_NDS32_PLT_GOTREL_LO20",/* name */
1517 FALSE, /* partial_inplace */
1518 0x000fffff, /* src_mask */
1519 0x000fffff, /* dst_mask */
1520 FALSE), /* pcrel_offset */
1521 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1522 HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
1523 0, /* rightshift */
1524 2, /* size (0 = byte, 1 = short, 2 = long) */
1525 15, /* bitsize */
1526 FALSE, /* pc_relative */
1527 0, /* bitpos */
1528 complain_overflow_dont,/* complain_on_overflow */
1529 bfd_elf_generic_reloc, /* special_function */
1530 "R_NDS32_PLT_GOTREL_LO15",/* name */
1531 FALSE, /* partial_inplace */
1532 0x00007fff, /* src_mask */
1533 0x00007fff, /* dst_mask */
1534 FALSE), /* pcrel_offset */
35c08157 1535 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1536 HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
1537 0, /* rightshift */
1538 2, /* size (0 = byte, 1 = short, 2 = long) */
1539 19, /* bitsize */
1540 FALSE, /* pc_relative */
1541 0, /* bitpos */
1542 complain_overflow_dont,/* complain_on_overflow */
1543 bfd_elf_generic_reloc, /* special_function */
1544 "R_NDS32_PLT_GOTREL_LO19",/* name */
1545 FALSE, /* partial_inplace */
1546 0x0007ffff, /* src_mask */
1547 0x0007ffff, /* dst_mask */
1548 FALSE), /* pcrel_offset */
1549 HOWTO2 (R_NDS32_GOT_LO15, /* type */
1550 0, /* rightshift */
1551 2, /* size (0 = byte, 1 = short, 2 = long) */
1552 15, /* bitsize */
1553 FALSE, /* pc_relative */
1554 0, /* bitpos */
1555 complain_overflow_dont,/* complain_on_overflow */
1556 bfd_elf_generic_reloc, /* special_function */
1557 "R_NDS32_GOT_LO15", /* name */
1558 FALSE, /* partial_inplace */
1559 0x00007fff, /* src_mask */
1560 0x00007fff, /* dst_mask */
1561 FALSE), /* pcrel_offset */
1562 HOWTO2 (R_NDS32_GOT_LO19, /* type */
1563 0, /* rightshift */
1564 2, /* size (0 = byte, 1 = short, 2 = long) */
1565 19, /* bitsize */
1566 FALSE, /* pc_relative */
1567 0, /* bitpos */
1568 complain_overflow_dont,/* complain_on_overflow */
1569 bfd_elf_generic_reloc, /* special_function */
1570 "R_NDS32_GOT_LO19", /* name */
1571 FALSE, /* partial_inplace */
1572 0x0007ffff, /* src_mask */
1573 0x0007ffff, /* dst_mask */
1574 FALSE), /* pcrel_offset */
1575 HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
1576 0, /* rightshift */
1577 2, /* size (0 = byte, 1 = short, 2 = long) */
1578 15, /* bitsize */
1579 FALSE, /* pc_relative */
1580 0, /* bitpos */
1581 complain_overflow_dont,/* complain_on_overflow */
1582 bfd_elf_generic_reloc, /* special_function */
1583 "R_NDS32_GOTOFF_LO15", /* name */
1584 FALSE, /* partial_inplace */
1585 0x00007fff, /* src_mask */
1586 0x00007fff, /* dst_mask */
1587 FALSE), /* pcrel_offset */
1588 HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
1589 0, /* rightshift */
1590 2, /* size (0 = byte, 1 = short, 2 = long) */
1591 19, /* bitsize */
1592 FALSE, /* pc_relative */
1593 0, /* bitpos */
1594 complain_overflow_dont,/* complain_on_overflow */
1595 bfd_elf_generic_reloc, /* special_function */
1596 "R_NDS32_GOTOFF_LO19", /* name */
1597 FALSE, /* partial_inplace */
1598 0x0007ffff, /* src_mask */
1599 0x0007ffff, /* dst_mask */
1600 FALSE), /* pcrel_offset */
35c08157 1601 /* GOT 15 bits offset. */
fbaf61ad
NC
1602 HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
1603 2, /* rightshift */
1604 2, /* size (0 = byte, 1 = short, 2 = long) */
1605 15, /* bitsize */
1606 FALSE, /* pc_relative */
1607 0, /* bitpos */
1608 complain_overflow_signed,/* complain_on_overflow */
1609 bfd_elf_generic_reloc, /* special_function */
1610 "R_NDS32_GOT15S2_RELA",/* name */
1611 FALSE, /* partial_inplace */
1612 0x00007fff, /* src_mask */
1613 0x00007fff, /* dst_mask */
1614 FALSE), /* pcrel_offset */
35c08157 1615 /* GOT 17 bits offset. */
fbaf61ad
NC
1616 HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
1617 2, /* rightshift */
1618 2, /* size (0 = byte, 1 = short, 2 = long) */
1619 17, /* bitsize */
1620 FALSE, /* pc_relative */
1621 0, /* bitpos */
1622 complain_overflow_signed,/* complain_on_overflow */
1623 bfd_elf_generic_reloc, /* special_function */
1624 "R_NDS32_GOT17S2_RELA",/* name */
1625 FALSE, /* partial_inplace */
1626 0x0001ffff, /* src_mask */
1627 0x0001ffff, /* dst_mask */
1628 FALSE), /* pcrel_offset */
35c08157 1629 /* A 5 bit address. */
fbaf61ad
NC
1630 HOWTO2 (R_NDS32_5_RELA, /* type */
1631 0, /* rightshift */
1632 1, /* size (0 = byte, 1 = short, 2 = long) */
1633 5, /* bitsize */
1634 FALSE, /* pc_relative */
1635 0, /* bitpos */
1636 complain_overflow_signed,/* complain_on_overflow */
1637 bfd_elf_generic_reloc, /* special_function */
1638 "R_NDS32_5_RELA", /* name */
1639 FALSE, /* partial_inplace */
1640 0x1f, /* src_mask */
1641 0x1f, /* dst_mask */
1642 FALSE), /* pcrel_offset */
1643 HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
1644 1, /* rightshift */
1645 1, /* size (0 = byte, 1 = short, 2 = long) */
1646 9, /* bitsize */
1647 TRUE, /* pc_relative */
1648 0, /* bitpos */
1649 complain_overflow_unsigned,/* complain_on_overflow */
1650 bfd_elf_generic_reloc, /* special_function */
1651 "R_NDS32_10_UPCREL_RELA",/* name */
1652 FALSE, /* partial_inplace */
1653 0x1ff, /* src_mask */
1654 0x1ff, /* dst_mask */
1655 TRUE), /* pcrel_offset */
1656 HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
1657 2, /* rightshift */
1658 1, /* size (0 = byte, 1 = short, 2 = long) */
1659 7, /* bitsize */
1660 FALSE, /* pc_relative */
1661 0, /* bitpos */
1662 complain_overflow_unsigned,/* complain_on_overflow */
1663 bfd_elf_generic_reloc, /* special_function */
1664 "R_NDS32_SDA_FP7U2_RELA",/* name */
1665 FALSE, /* partial_inplace */
1666 0x0000007f, /* src_mask */
1667 0x0000007f, /* dst_mask */
1668 FALSE), /* pcrel_offset */
1669 HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
1670 1, /* rightshift */
1671 2, /* size (0 = byte, 1 = short, 2 = long) */
1672 8, /* bitsize */
1673 TRUE, /* pc_relative */
1674 0, /* bitpos */
1675 complain_overflow_signed,/* complain_on_overflow */
1676 bfd_elf_generic_reloc, /* special_function */
1677 "R_NDS32_WORD_9_PCREL_RELA",/* name */
1678 FALSE, /* partial_inplace */
1679 0xff, /* src_mask */
1680 0xff, /* dst_mask */
1681 TRUE), /* pcrel_offset */
1682 HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
1683 1, /* rightshift */
1684 2, /* size (0 = byte, 1 = short, 2 = long) */
1685 24, /* bitsize */
1686 FALSE, /* pc_relative */
1687 0, /* bitpos */
1688 complain_overflow_dont,/* complain_on_overflow */
1689 bfd_elf_generic_reloc, /* special_function */
1690 "R_NDS32_25_ABS_RELA", /* name */
1691 FALSE, /* partial_inplace */
1692 0xffffff, /* src_mask */
1693 0xffffff, /* dst_mask */
1694 FALSE), /* pcrel_offset */
35c08157
KLC
1695
1696 /* A relative 17 bit relocation for ifc, right shifted by 1. */
fbaf61ad
NC
1697 HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
1698 1, /* rightshift */
1699 2, /* size (0 = byte, 1 = short, 2 = long) */
1700 16, /* bitsize */
1701 TRUE, /* pc_relative */
1702 0, /* bitpos */
1703 complain_overflow_signed,/* complain_on_overflow */
1704 bfd_elf_generic_reloc, /* special_function */
1705 "R_NDS32_17IFC_PCREL_RELA",/* name */
1706 FALSE, /* partial_inplace */
1707 0xffff, /* src_mask */
1708 0xffff, /* dst_mask */
1709 TRUE), /* pcrel_offset */
35c08157
KLC
1710
1711 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
fbaf61ad
NC
1712 HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
1713 1, /* rightshift */
1714 1, /* size (0 = byte, 1 = short, 2 = long) */
1715 9, /* bitsize */
1716 TRUE, /* pc_relative */
1717 0, /* bitpos */
1718 complain_overflow_unsigned,/* complain_on_overflow */
1719 bfd_elf_generic_reloc, /* special_function */
1720 "R_NDS32_10IFCU_PCREL_RELA",/* name */
1721 FALSE, /* partial_inplace */
1722 0x1ff, /* src_mask */
1723 0x1ff, /* dst_mask */
1724 TRUE), /* pcrel_offset */
1725
1726 /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
1727 HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
1728 12, /* rightshift */
1729 2, /* size (0 = byte, 1 = short, 2 = long) */
1730 20, /* bitsize */
1731 FALSE, /* pc_relative */
1732 0, /* bitpos */
1733 complain_overflow_dont,/* complain_on_overflow */
1734 bfd_elf_generic_reloc, /* special_function */
1735 "R_NDS32_TLS_LE_HI20", /* name */
1736 FALSE, /* partial_inplace */
1737 0x000fffff, /* src_mask */
1738 0x000fffff, /* dst_mask */
1739 FALSE), /* pcrel_offset */
1740
1741 HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
1742 0, /* rightshift */
1743 2, /* size (0 = byte, 1 = short, 2 = long) */
1744 12, /* bitsize */
1745 FALSE, /* pc_relative */
1746 0, /* bitpos */
1747 complain_overflow_dont,/* complain_on_overflow */
1748 bfd_elf_generic_reloc, /* special_function */
1749 "R_NDS32_TLS_LE_LO12", /* name */
1750 FALSE, /* partial_inplace */
1751 0x00000fff, /* src_mask */
1752 0x00000fff, /* dst_mask */
1753 FALSE), /* pcrel_offset */
1754
1755 /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
1756 HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
1757 12, /* rightshift */
1758 2, /* size (0 = byte, 1 = short, 2 = long) */
1759 20, /* bitsize */
1760 FALSE, /* pc_relative */
1761 0, /* bitpos */
1762 complain_overflow_dont,/* complain_on_overflow */
1763 bfd_elf_generic_reloc, /* special_function */
1764 "R_NDS32_TLS_IE_HI20", /* name */
1765 FALSE, /* partial_inplace */
1766 0x000fffff, /* src_mask */
1767 0x000fffff, /* dst_mask */
1768 FALSE), /* pcrel_offset */
1769
1770 HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
1771 2, /* rightshift */
1772 2, /* size (0 = byte, 1 = short, 2 = long) */
1773 10, /* bitsize */
1774 FALSE, /* pc_relative */
1775 0, /* bitpos */
1776 complain_overflow_dont,/* complain_on_overflow */
1777 bfd_elf_generic_reloc, /* special_function */
1778 "R_NDS32_TLS_IE_LO12S2",/* name */
1779 FALSE, /* partial_inplace */
1780 0x000003ff, /* src_mask */
1781 0x000003ff, /* dst_mask */
1782 FALSE), /* pcrel_offset */
1783
1784 /* TLS LE TP offset relocation */
1785 HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
1786 0, /* rightshift */
1787 2, /* size (0 = byte, 1 = short, 2 = long) */
1788 32, /* bitsize */
1789 FALSE, /* pc_relative */
1790 0, /* bitpos */
1791 complain_overflow_bitfield,/* complain_on_overflow */
1792 bfd_elf_generic_reloc, /* special_function */
1793 "R_NDS32_TLS_TPOFF", /* name */
1794 FALSE, /* partial_inplace */
1795 0xffffffff, /* src_mask */
1796 0xffffffff, /* dst_mask */
1797 FALSE), /* pcrel_offset */
1798
1c8f6a4d 1799 /* A 20 bit address. */
fbaf61ad
NC
1800 HOWTO2 (R_NDS32_TLS_LE_20, /* type */
1801 0, /* rightshift */
1802 2, /* size (0 = byte, 1 = short, 2 = long) */
1803 20, /* bitsize */
1804 FALSE, /* pc_relative */
1805 0, /* bitpos */
1806 complain_overflow_signed,/* complain_on_overflow */
1807 bfd_elf_generic_reloc, /* special_function */
1808 "R_NDS32_TLS_LE_20", /* name */
1809 FALSE, /* partial_inplace */
1810 0xfffff, /* src_mask */
1811 0xfffff, /* dst_mask */
1812 FALSE), /* pcrel_offset */
1813
1814 HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
1815 0, /* rightshift */
1816 2, /* size (0 = byte, 1 = short, 2 = long) */
1817 15, /* bitsize */
1818 FALSE, /* pc_relative */
1819 0, /* bitpos */
1820 complain_overflow_signed,/* complain_on_overflow */
1821 bfd_elf_generic_reloc, /* special_function */
1822 "R_NDS32_TLS_LE_15S0", /* name */
1823 FALSE, /* partial_inplace */
1824 0x7fff, /* src_mask */
1825 0x7fff, /* dst_mask */
1826 FALSE), /* pcrel_offset */
1827 HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
1828 1, /* rightshift */
1829 2, /* size (0 = byte, 1 = short, 2 = long) */
1830 15, /* bitsize */
1831 FALSE, /* pc_relative */
1832 0, /* bitpos */
1833 complain_overflow_signed,/* complain_on_overflow */
1834 bfd_elf_generic_reloc, /* special_function */
1835 "R_NDS32_TLS_LE_15S1", /* name */
1836 FALSE, /* partial_inplace */
1837 0x7fff, /* src_mask */
1838 0x7fff, /* dst_mask */
1839 FALSE), /* pcrel_offset */
1840 HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
1841 2, /* rightshift */
1842 2, /* size (0 = byte, 1 = short, 2 = long) */
1843 15, /* bitsize */
1844 FALSE, /* pc_relative */
1845 0, /* bitpos */
1846 complain_overflow_signed,/* complain_on_overflow */
1847 bfd_elf_generic_reloc, /* special_function */
1848 "R_NDS32_TLS_LE_15S2", /* name */
1849 FALSE, /* partial_inplace */
1850 0x7fff, /* src_mask */
1851 0x7fff, /* dst_mask */
1852 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1853
1854 /* Relax hint for unconditional call sequence */
fbaf61ad
NC
1855 HOWTO2 (R_NDS32_LONGCALL4, /* type */
1856 0, /* rightshift */
1857 2, /* size (0 = byte, 1 = short, 2 = long) */
1858 32, /* bitsize */
1859 FALSE, /* pc_relative */
1860 0, /* bitpos */
1861 complain_overflow_dont,/* complain_on_overflow */
1862 nds32_elf_ignore_reloc,/* special_function */
1863 "R_NDS32_LONGCALL4", /* name */
1864 FALSE, /* partial_inplace */
1865 0xffffffff, /* src_mask */
1866 0xffffffff, /* dst_mask */
1867 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1868
1869 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1870 HOWTO2 (R_NDS32_LONGCALL5, /* type */
1871 0, /* rightshift */
1872 2, /* size (0 = byte, 1 = short, 2 = long) */
1873 32, /* bitsize */
1874 FALSE, /* pc_relative */
1875 0, /* bitpos */
1876 complain_overflow_dont,/* complain_on_overflow */
1877 nds32_elf_ignore_reloc,/* special_function */
1878 "R_NDS32_LONGCALL5", /* name */
1879 FALSE, /* partial_inplace */
1880 0xffffffff, /* src_mask */
1881 0xffffffff, /* dst_mask */
1882 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1883
1884 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1885 HOWTO2 (R_NDS32_LONGCALL6, /* type */
1886 0, /* rightshift */
1887 2, /* size (0 = byte, 1 = short, 2 = long) */
1888 32, /* bitsize */
1889 FALSE, /* pc_relative */
1890 0, /* bitpos */
1891 complain_overflow_dont,/* complain_on_overflow */
1892 nds32_elf_ignore_reloc,/* special_function */
1893 "R_NDS32_LONGCALL6", /* name */
1894 FALSE, /* partial_inplace */
1895 0xffffffff, /* src_mask */
1896 0xffffffff, /* dst_mask */
1897 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1898
1899 /* Relax hint for unconditional branch sequence. */
fbaf61ad
NC
1900 HOWTO2 (R_NDS32_LONGJUMP4, /* type */
1901 0, /* rightshift */
1902 2, /* size (0 = byte, 1 = short, 2 = long) */
1903 32, /* bitsize */
1904 FALSE, /* pc_relative */
1905 0, /* bitpos */
1906 complain_overflow_dont,/* complain_on_overflow */
1907 nds32_elf_ignore_reloc,/* special_function */
1908 "R_NDS32_LONGJUMP4", /* name */
1909 FALSE, /* partial_inplace */
1910 0xffffffff, /* src_mask */
1911 0xffffffff, /* dst_mask */
1912 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1913
1914 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1915 HOWTO2 (R_NDS32_LONGJUMP5, /* type */
1916 0, /* rightshift */
1917 2, /* size (0 = byte, 1 = short, 2 = long) */
1918 32, /* bitsize */
1919 FALSE, /* pc_relative */
1920 0, /* bitpos */
1921 complain_overflow_dont,/* complain_on_overflow */
1922 nds32_elf_ignore_reloc,/* special_function */
1923 "R_NDS32_LONGJUMP5", /* name */
1924 FALSE, /* partial_inplace */
1925 0xffffffff, /* src_mask */
1926 0xffffffff, /* dst_mask */
1927 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1928
1929 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1930 HOWTO2 (R_NDS32_LONGJUMP6, /* type */
1931 0, /* rightshift */
1932 2, /* size (0 = byte, 1 = short, 2 = long) */
1933 32, /* bitsize */
1934 FALSE, /* pc_relative */
1935 0, /* bitpos */
1936 complain_overflow_dont,/* complain_on_overflow */
1937 nds32_elf_ignore_reloc,/* special_function */
1938 "R_NDS32_LONGJUMP6", /* name */
1939 FALSE, /* partial_inplace */
1940 0xffffffff, /* src_mask */
1941 0xffffffff, /* dst_mask */
1942 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1943
1944 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1945 HOWTO2 (R_NDS32_LONGJUMP7, /* type */
1946 0, /* rightshift */
1947 2, /* size (0 = byte, 1 = short, 2 = long) */
1948 32, /* bitsize */
1949 FALSE, /* pc_relative */
1950 0, /* bitpos */
1951 complain_overflow_dont,/* complain_on_overflow */
1952 nds32_elf_ignore_reloc,/* special_function */
1953 "R_NDS32_LONGJUMP7", /* name */
1954 FALSE, /* partial_inplace */
1955 0xffffffff, /* src_mask */
1956 0xffffffff, /* dst_mask */
1957 FALSE), /* pcrel_offset */
1958
1959 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1960 0, /* rightshift */
1961 2, /* size (0 = byte, 1 = short, 2 = long) */
1962 12, /* bitsize */
1963 FALSE, /* pc_relative */
1964 0, /* bitpos */
1965 complain_overflow_dont,/* complain_on_overflow */
1966 bfd_elf_generic_reloc, /* special_function */
1967 "R_NDS32_TLS_IE_LO12", /* name */
1968 FALSE, /* partial_inplace */
1969 0x00000fff, /* src_mask */
1970 0x00000fff, /* dst_mask */
1971 FALSE), /* pcrel_offset */
1972
1973 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1974 entry for the symbol. */
1975 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1976 12, /* rightshift */
1977 2, /* size (0 = byte, 1 = short, 2 = long) */
1978 20, /* bitsize */
1979 FALSE, /* pc_relative */
1980 0, /* bitpos */
1981 complain_overflow_dont,/* complain_on_overflow */
1982 bfd_elf_generic_reloc, /* special_function */
1983 "R_NDS32_TLS_IEGP_HI20",/* name */
1984 FALSE, /* partial_inplace */
1985 0x000fffff, /* src_mask */
1986 0x000fffff, /* dst_mask */
1987 FALSE), /* pcrel_offset */
1988
1989 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1990 0, /* rightshift */
1991 2, /* size (0 = byte, 1 = short, 2 = long) */
1992 12, /* bitsize */
1993 FALSE, /* pc_relative */
1994 0, /* bitpos */
1995 complain_overflow_dont,/* complain_on_overflow */
1996 bfd_elf_generic_reloc, /* special_function */
1997 "R_NDS32_TLS_IEGP_LO12",/* name */
1998 FALSE, /* partial_inplace */
1999 0x00000fff, /* src_mask */
2000 0x00000fff, /* dst_mask */
2001 FALSE), /* pcrel_offset */
2002
2003 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2004 2, /* rightshift */
2005 2, /* size (0 = byte, 1 = short, 2 = long) */
2006 10, /* bitsize */
2007 FALSE, /* pc_relative */
2008 0, /* bitpos */
2009 complain_overflow_dont,/* complain_on_overflow */
2010 bfd_elf_generic_reloc, /* special_function */
2011 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2012 FALSE, /* partial_inplace */
2013 0x000003ff, /* src_mask */
2014 0x000003ff, /* dst_mask */
2015 FALSE), /* pcrel_offset */
2016
2017 /* TLS description relocation */
2018 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2019 12, /* rightshift */
2020 2, /* size (0 = byte, 1 = short, 2 = long) */
2021 20, /* bitsize */
2022 FALSE, /* pc_relative */
2023 0, /* bitpos */
2024 complain_overflow_dont,/* complain_on_overflow */
2025 nds32_elf_hi20_reloc, /* special_function */
2026 "R_NDS32_TLS_DESC_HI20",/* name */
2027 FALSE, /* partial_inplace */
2028 0x000fffff, /* src_mask */
2029 0x000fffff, /* dst_mask */
2030 FALSE), /* pcrel_offset */
2031
2032 /* TLS GD/LD description offset high part. */
2033 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2034 12, /* rightshift */
2035 2, /* size (0 = byte, 1 = short, 2 = long) */
2036 20, /* bitsize */
2037 FALSE, /* pc_relative */
2038 0, /* bitpos */
2039 complain_overflow_dont,/* complain_on_overflow */
2040 nds32_elf_hi20_reloc, /* special_function */
2041 "R_NDS32_TLS_DESC_HI20",/* name */
2042 FALSE, /* partial_inplace */
2043 0x000fffff, /* src_mask */
2044 0x000fffff, /* dst_mask */
2045 FALSE), /* pcrel_offset */
2046
2047 /* TLS GD/LD description offset low part. */
2048 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2049 0, /* rightshift */
2050 2, /* size (0 = byte, 1 = short, 2 = long) */
2051 12, /* bitsize */
2052 FALSE, /* pc_relative */
2053 0, /* bitpos */
2054 complain_overflow_dont,/* complain_on_overflow */
2055 nds32_elf_lo12_reloc, /* special_function */
2056 "R_NDS32_TLS_DESC_LO12",/* name */
2057 FALSE, /* partial_inplace */
2058 0x00000fff, /* src_mask */
2059 0x00000fff, /* dst_mask */
2060 FALSE), /* pcrel_offset */
2061
2062 /* TLS GD/LD description offset set (movi). */
2063 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2064 0, /* rightshift */
2065 2, /* size (0 = byte, 1 = short, 2 = long) */
2066 20, /* bitsize */
2067 FALSE, /* pc_relative */
2068 0, /* bitpos */
2069 complain_overflow_signed,/* complain_on_overflow */
2070 bfd_elf_generic_reloc, /* special_function */
2071 "R_NDS32_TLS_DESC_20", /* name */
2072 FALSE, /* partial_inplace */
2073 0x000fffff, /* src_mask */
2074 0x000fffff, /* dst_mask */
2075 FALSE), /* pcrel_offset */
2076
2077 /* TLS GD/LD description offset set (lwi.gp). */
2078 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2079 2, /* rightshift */
2080 2, /* size (0 = byte, 1 = short, 2 = long) */
2081 17, /* bitsize */
2082 FALSE, /* pc_relative */
2083 0, /* bitpos */
2084 complain_overflow_signed,/* complain_on_overflow */
2085 bfd_elf_generic_reloc, /* special_function */
2086 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2087 FALSE, /* partial_inplace */
2088 0x0001ffff, /* src_mask */
2089 0x0001ffff, /* dst_mask */
2090 FALSE), /* pcrel_offset */
35c08157
KLC
2091};
2092
2093/* Relocations used for relaxation. */
fbaf61ad
NC
2094#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2095 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2096
2097static reloc_howto_type nds32_elf_relax_howto_table[] = {
2098 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2099 0, /* rightshift */
2100 2, /* size (0 = byte, 1 = short, 2 = long) */
2101 32, /* bitsize */
2102 FALSE, /* pc_relative */
2103 0, /* bitpos */
2104 complain_overflow_dont,/* complain_on_overflow */
2105 nds32_elf_ignore_reloc,/* special_function */
2106 "R_NDS32_RELAX_ENTRY", /* name */
2107 FALSE, /* partial_inplace */
2108 0xffffffff, /* src_mask */
2109 0xffffffff, /* dst_mask */
2110 FALSE), /* pcrel_offset */
2111 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2112 0, /* rightshift */
2113 2, /* size (0 = byte, 1 = short, 2 = long) */
2114 32, /* bitsize */
2115 FALSE, /* pc_relative */
2116 0, /* bitpos */
2117 complain_overflow_dont,/* complain_on_overflow */
2118 nds32_elf_ignore_reloc,/* special_function */
2119 "R_NDS32_GOT_SUFF", /* name */
2120 FALSE, /* partial_inplace */
2121 0xffffffff, /* src_mask */
2122 0xffffffff, /* dst_mask */
2123 FALSE), /* pcrel_offset */
2124 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2125 0, /* rightshift */
2126 2, /* size (0 = byte, 1 = short, 2 = long) */
2127 32, /* bitsize */
2128 FALSE, /* pc_relative */
2129 0, /* bitpos */
2130 complain_overflow_bitfield,/* complain_on_overflow */
2131 nds32_elf_ignore_reloc,/* special_function */
2132 "R_NDS32_GOTOFF_SUFF", /* name */
2133 FALSE, /* partial_inplace */
2134 0xffffffff, /* src_mask */
2135 0xffffffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2137 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2138 0, /* rightshift */
2139 2, /* size (0 = byte, 1 = short, 2 = long) */
2140 32, /* bitsize */
2141 FALSE, /* pc_relative */
2142 0, /* bitpos */
2143 complain_overflow_dont,/* complain_on_overflow */
2144 nds32_elf_ignore_reloc,/* special_function */
2145 "R_NDS32_PLT_GOT_SUFF",/* name */
2146 FALSE, /* partial_inplace */
2147 0xffffffff, /* src_mask */
2148 0xffffffff, /* dst_mask */
2149 FALSE), /* pcrel_offset */
2150 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2151 0, /* rightshift */
2152 2, /* size (0 = byte, 1 = short, 2 = long) */
2153 32, /* bitsize */
2154 FALSE, /* pc_relative */
2155 0, /* bitpos */
2156 complain_overflow_dont,/* complain_on_overflow */
2157 nds32_elf_ignore_reloc,/* special_function */
2158 "R_NDS32_MULCALL_SUFF",/* name */
2159 FALSE, /* partial_inplace */
2160 0xffffffff, /* src_mask */
2161 0xffffffff, /* dst_mask */
2162 FALSE), /* pcrel_offset */
2163 HOWTO3 (R_NDS32_PTR, /* type */
2164 0, /* rightshift */
2165 2, /* size (0 = byte, 1 = short, 2 = long) */
2166 32, /* bitsize */
2167 FALSE, /* pc_relative */
2168 0, /* bitpos */
2169 complain_overflow_dont,/* complain_on_overflow */
2170 nds32_elf_ignore_reloc,/* special_function */
2171 "R_NDS32_PTR", /* name */
2172 FALSE, /* partial_inplace */
2173 0xffffffff, /* src_mask */
2174 0xffffffff, /* dst_mask */
2175 FALSE), /* pcrel_offset */
2176 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2177 0, /* rightshift */
2178 2, /* size (0 = byte, 1 = short, 2 = long) */
2179 32, /* bitsize */
2180 FALSE, /* pc_relative */
2181 0, /* bitpos */
2182 complain_overflow_dont,/* complain_on_overflow */
2183 nds32_elf_ignore_reloc,/* special_function */
2184 "R_NDS32_PTR_COUNT", /* name */
2185 FALSE, /* partial_inplace */
2186 0xffffffff, /* src_mask */
2187 0xffffffff, /* dst_mask */
2188 FALSE), /* pcrel_offset */
2189 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2190 0, /* rightshift */
2191 2, /* size (0 = byte, 1 = short, 2 = long) */
2192 32, /* bitsize */
2193 FALSE, /* pc_relative */
2194 0, /* bitpos */
2195 complain_overflow_dont,/* complain_on_overflow */
2196 nds32_elf_ignore_reloc,/* special_function */
2197 "R_NDS32_PTR_RESOLVED",/* name */
2198 FALSE, /* partial_inplace */
2199 0xffffffff, /* src_mask */
2200 0xffffffff, /* dst_mask */
2201 FALSE), /* pcrel_offset */
2202 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2203 0, /* rightshift */
2204 2, /* size (0 = byte, 1 = short, 2 = long) */
2205 32, /* bitsize */
2206 FALSE, /* pc_relative */
2207 0, /* bitpos */
2208 complain_overflow_dont,/* complain_on_overflow */
2209 nds32_elf_ignore_reloc,/* special_function */
2210 "R_NDS32_PLTBLOCK", /* name */
2211 FALSE, /* partial_inplace */
2212 0xffffffff, /* src_mask */
2213 0xffffffff, /* dst_mask */
2214 FALSE), /* pcrel_offset */
2215 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2216 0, /* rightshift */
2217 2, /* size (0 = byte, 1 = short, 2 = long) */
2218 32, /* bitsize */
2219 FALSE, /* pc_relative */
2220 0, /* bitpos */
2221 complain_overflow_dont,/* complain_on_overflow */
2222 nds32_elf_ignore_reloc,/* special_function */
2223 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2224 FALSE, /* partial_inplace */
2225 0xffffffff, /* src_mask */
2226 0xffffffff, /* dst_mask */
2227 FALSE), /* pcrel_offset */
2228 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2229 0, /* rightshift */
2230 2, /* size (0 = byte, 1 = short, 2 = long) */
2231 32, /* bitsize */
2232 FALSE, /* pc_relative */
2233 0, /* bitpos */
2234 complain_overflow_dont,/* complain_on_overflow */
2235 nds32_elf_ignore_reloc,/* special_function */
2236 "R_NDS32_RELAX_REGION_END",/* name */
2237 FALSE, /* partial_inplace */
2238 0xffffffff, /* src_mask */
2239 0xffffffff, /* dst_mask */
2240 FALSE), /* pcrel_offset */
2241 HOWTO3 (R_NDS32_MINUEND, /* type */
2242 0, /* rightshift */
2243 2, /* size (0 = byte, 1 = short, 2 = long) */
2244 32, /* bitsize */
2245 FALSE, /* pc_relative */
2246 0, /* bitpos */
2247 complain_overflow_dont,/* complain_on_overflow */
2248 nds32_elf_ignore_reloc,/* special_function */
2249 "R_NDS32_MINUEND", /* name */
2250 FALSE, /* partial_inplace */
2251 0xffffffff, /* src_mask */
2252 0xffffffff, /* dst_mask */
2253 FALSE), /* pcrel_offset */
2254 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2255 0, /* rightshift */
2256 2, /* size (0 = byte, 1 = short, 2 = long) */
2257 32, /* bitsize */
2258 FALSE, /* pc_relative */
2259 0, /* bitpos */
2260 complain_overflow_dont,/* complain_on_overflow */
2261 nds32_elf_ignore_reloc,/* special_function */
2262 "R_NDS32_SUBTRAHEND", /* name */
2263 FALSE, /* partial_inplace */
2264 0xffffffff, /* src_mask */
2265 0xffffffff, /* dst_mask */
2266 FALSE), /* pcrel_offset */
2267 HOWTO3 (R_NDS32_DIFF8, /* type */
2268 0, /* rightshift */
2269 0, /* size (0 = byte, 1 = short, 2 = long) */
2270 8, /* bitsize */
2271 FALSE, /* pc_relative */
2272 0, /* bitpos */
2273 complain_overflow_dont,/* complain_on_overflow */
2274 nds32_elf_ignore_reloc,/* special_function */
2275 "R_NDS32_DIFF8", /* name */
2276 FALSE, /* partial_inplace */
2277 0x000000ff, /* src_mask */
2278 0x000000ff, /* dst_mask */
2279 FALSE), /* pcrel_offset */
2280 HOWTO3 (R_NDS32_DIFF16, /* type */
2281 0, /* rightshift */
2282 1, /* size (0 = byte, 1 = short, 2 = long) */
2283 16, /* bitsize */
2284 FALSE, /* pc_relative */
2285 0, /* bitpos */
2286 complain_overflow_dont,/* complain_on_overflow */
2287 nds32_elf_ignore_reloc,/* special_function */
2288 "R_NDS32_DIFF16", /* name */
2289 FALSE, /* partial_inplace */
2290 0x0000ffff, /* src_mask */
2291 0x0000ffff, /* dst_mask */
2292 FALSE), /* pcrel_offset */
2293 HOWTO3 (R_NDS32_DIFF32, /* type */
2294 0, /* rightshift */
2295 2, /* size (0 = byte, 1 = short, 2 = long) */
2296 32, /* bitsize */
2297 FALSE, /* pc_relative */
2298 0, /* bitpos */
2299 complain_overflow_dont,/* complain_on_overflow */
2300 nds32_elf_ignore_reloc,/* special_function */
2301 "R_NDS32_DIFF32", /* name */
2302 FALSE, /* partial_inplace */
2303 0xffffffff, /* src_mask */
2304 0xffffffff, /* dst_mask */
2305 FALSE), /* pcrel_offset */
2306 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2307 0, /* rightshift */
2308 0, /* size (0 = byte, 1 = short, 2 = long) */
2309 0, /* bitsize */
2310 FALSE, /* pc_relative */
2311 0, /* bitpos */
2312 complain_overflow_dont,/* complain_on_overflow */
2313 nds32_elf_ignore_reloc,/* special_function */
2314 "R_NDS32_DIFF_ULEB128",/* name */
2315 FALSE, /* partial_inplace */
2316 0xffffffff, /* src_mask */
2317 0xffffffff, /* dst_mask */
2318 FALSE), /* pcrel_offset */
2319 HOWTO3 (R_NDS32_DATA, /* type */
2320 0, /* rightshift */
2321 2, /* size (0 = byte, 1 = short, 2 = long) */
2322 32, /* bitsize */
2323 FALSE, /* pc_relative */
2324 0, /* bitpos */
2325 complain_overflow_dont,/* complain_on_overflow */
2326 nds32_elf_ignore_reloc,/* special_function */
2327 "R_NDS32_DATA", /* name */
2328 FALSE, /* partial_inplace */
2329 0xffffffff, /* src_mask */
2330 0xffffffff, /* dst_mask */
2331 FALSE), /* pcrel_offset */
2332 HOWTO3 (R_NDS32_TRAN, /* type */
2333 0, /* rightshift */
2334 2, /* size (0 = byte, 1 = short, 2 = long) */
2335 32, /* bitsize */
2336 FALSE, /* pc_relative */
2337 0, /* bitpos */
2338 complain_overflow_dont,/* complain_on_overflow */
2339 nds32_elf_ignore_reloc,/* special_function */
2340 "R_NDS32_TRAN", /* name */
2341 FALSE, /* partial_inplace */
2342 0xffffffff, /* src_mask */
2343 0xffffffff, /* dst_mask */
2344 FALSE), /* pcrel_offset */
2345 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2346 0, /* rightshift */
2347 2, /* size (0 = byte, 1 = short, 2 = long) */
2348 32, /* bitsize */
2349 FALSE, /* pc_relative */
2350 0, /* bitpos */
2351 complain_overflow_dont,/* complain_on_overflow */
2352 nds32_elf_ignore_reloc,/* special_function */
2353 "R_NDS32_TLS_LE_ADD", /* name */
2354 FALSE, /* partial_inplace */
2355 0xffffffff, /* src_mask */
2356 0xffffffff, /* dst_mask */
2357 FALSE), /* pcrel_offset */
2358 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2359 0, /* rightshift */
2360 2, /* size (0 = byte, 1 = short, 2 = long) */
2361 32, /* bitsize */
2362 FALSE, /* pc_relative */
2363 0, /* bitpos */
2364 complain_overflow_dont,/* complain_on_overflow */
2365 nds32_elf_ignore_reloc,/* special_function */
2366 "R_NDS32_TLS_LE_LS", /* name */
2367 FALSE, /* partial_inplace */
2368 0xffffffff, /* src_mask */
2369 0xffffffff, /* dst_mask */
2370 FALSE), /* pcrel_offset */
2371 HOWTO3 (R_NDS32_EMPTY, /* type */
2372 0, /* rightshift */
2373 2, /* size (0 = byte, 1 = short, 2 = long) */
2374 32, /* bitsize */
2375 FALSE, /* pc_relative */
2376 0, /* bitpos */
2377 complain_overflow_dont,/* complain_on_overflow */
2378 nds32_elf_ignore_reloc,/* special_function */
2379 "R_NDS32_EMPTY", /* name */
2380 FALSE, /* partial_inplace */
2381 0xffffffff, /* src_mask */
2382 0xffffffff, /* dst_mask */
2383 FALSE), /* pcrel_offset */
2384 /* TLS GD/LD description address base addition. */
2385 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2386 0, /* rightshift */
2387 2, /* size (0 = byte, 1 = short, 2 = long) */
2388 32, /* bitsize */
2389 FALSE, /* pc_relative */
2390 0, /* bitpos */
2391 complain_overflow_dont,/* complain_on_overflow */
2392 nds32_elf_ignore_reloc,/* special_function */
2393 "R_NDS32_TLS_DESC_ADD",/* name */
2394 FALSE, /* partial_inplace */
2395 0xffffffff, /* src_mask */
2396 0xffffffff, /* dst_mask */
2397 FALSE), /* pcrel_offset */
2398 /* TLS GD/LD description function load. */
2399 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2400 0, /* rightshift */
2401 2, /* size (0 = byte, 1 = short, 2 = long) */
2402 32, /* bitsize */
2403 FALSE, /* pc_relative */
2404 0, /* bitpos */
2405 complain_overflow_dont,/* complain_on_overflow */
2406 nds32_elf_ignore_reloc,/* special_function */
2407 "R_NDS32_TLS_DESC_FUNC",/* name */
2408 FALSE, /* partial_inplace */
2409 0xffffffff, /* src_mask */
2410 0xffffffff, /* dst_mask */
2411 FALSE), /* pcrel_offset */
2412 /* TLS DESC resolve function call. */
2413 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2414 0, /* rightshift */
2415 2, /* size (0 = byte, 1 = short, 2 = long) */
2416 32, /* bitsize */
2417 FALSE, /* pc_relative */
2418 0, /* bitpos */
2419 complain_overflow_dont,/* complain_on_overflow */
2420 nds32_elf_ignore_reloc,/* special_function */
2421 "R_NDS32_TLS_DESC_CALL",/* name */
2422 FALSE, /* partial_inplace */
2423 0xffffffff, /* src_mask */
2424 0xffffffff, /* dst_mask */
2425 FALSE), /* pcrel_offset */
2426 /* TLS DESC variable access. */
2427 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2428 0, /* rightshift */
2429 2, /* size (0 = byte, 1 = short, 2 = long) */
2430 32, /* bitsize */
2431 FALSE, /* pc_relative */
2432 0, /* bitpos */
2433 complain_overflow_dont,/* complain_on_overflow */
2434 nds32_elf_ignore_reloc,/* special_function */
2435 "R_NDS32_TLS_DESC_MEM",/* name */
2436 FALSE, /* partial_inplace */
2437 0xffffffff, /* src_mask */
2438 0xffffffff, /* dst_mask */
2439 FALSE), /* pcrel_offset */
2440 /* TLS GD/LD description mark (@tlsdec). */
2441 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2442 0, /* rightshift */
2443 2, /* size (0 = byte, 1 = short, 2 = long) */
2444 32, /* bitsize */
2445 FALSE, /* pc_relative */
2446 0, /* bitpos */
2447 complain_overflow_dont,/* complain_on_overflow */
2448 nds32_elf_ignore_reloc,/* special_function */
2449 "R_NDS32_REMOVE", /* name */
2450 FALSE, /* partial_inplace */
2451 0xffffffff, /* src_mask */
2452 0xffffffff, /* dst_mask */
2453 FALSE), /* pcrel_offset */
2454 /* TLS GD/LD description mark (@tlsdec). */
2455 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2456 0, /* rightshift */
2457 2, /* size (0 = byte, 1 = short, 2 = long) */
2458 32, /* bitsize */
2459 FALSE, /* pc_relative */
2460 0, /* bitpos */
2461 complain_overflow_dont,/* complain_on_overflow */
2462 nds32_elf_ignore_reloc,/* special_function */
2463 "R_NDS32_GROUP", /* name */
2464 FALSE, /* partial_inplace */
2465 0xffffffff, /* src_mask */
2466 0xffffffff, /* dst_mask */
2467 FALSE), /* pcrel_offset */
2468 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2469 0, /* rightshift */
2470 2, /* size (0 = byte, 1 = short, 2 = long) */
2471 32, /* bitsize */
2472 FALSE, /* pc_relative */
2473 0, /* bitpos */
2474 complain_overflow_dont,/* complain_on_overflow */
2475 nds32_elf_ignore_reloc,/* special_function */
2476 "R_NDS32_TLS_IEGP_LW", /* name */
2477 FALSE, /* partial_inplace */
2478 0xffffffff, /* src_mask */
2479 0xffffffff, /* dst_mask */
2480 FALSE), /* pcrel_offset */
2481 /* LA and FLSI relaxation. */
2482 HOWTO3 (R_NDS32_LSI, /* type */
2483 0, /* rightshift */
2484 2, /* size (0 = byte, 1 = short, 2 = long) */
2485 32, /* bitsize */
2486 FALSE, /* pc_relative */
2487 0, /* bitpos */
2488 complain_overflow_dont,/* complain_on_overflow */
2489 nds32_elf_ignore_reloc,/* special_function */
2490 "R_NDS32_LSI", /* name */
2491 FALSE, /* partial_inplace */
2492 0xffffffff, /* src_mask */
2493 0xffffffff, /* dst_mask */
2494 FALSE),
35c08157
KLC
2495};
2496
fbaf61ad
NC
2497static unsigned long dl_tlsdesc_lazy_trampoline[] =
2498{
2499 0x46200000, /* sethi $r2,#0x0 */
2500 0x58210000, /* ori $r2,$r2,#0x0 */
2501 0x40217400, /* add $r2,$r2,$gp */
2502 0x04210000, /* lwi $r2,[$r2+#0x0] */
2503 0x46300000, /* sethi $r3,#0x0 */
2504 0x58318000, /* ori $r3,$r3,#0x0 */
2505 0x4031f400, /* add $r3,$r3,$gp */
2506 0x4a000800, /* jr $r2 */
2507};
2508
2509static void
2510nds32_put_trampoline (void *contents, const unsigned long *template,
2511 unsigned count)
2512{
2513 unsigned ix;
2514
2515 for (ix = 0; ix != count; ix++)
2516 {
2517 unsigned long insn = template[ix];
2518 bfd_putb32 (insn, (char *) contents + ix * 4);
2519 }
2520}
2521
35c08157
KLC
2522/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2523 This prototype is the same as qsort (). */
2524
2525void
2526nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2527 int (*compar) (const void *lhs, const void *rhs))
2528{
2529 char *ptr = (char *) base;
1c8f6a4d 2530 int i, j;
e1fa0163 2531 char *tmp = xmalloc (size);
35c08157
KLC
2532
2533 /* If i is less than j, i is inserted before j.
2534
2535 |---- j ----- i --------------|
2536 \ / \ /
2537 sorted unsorted
2538 */
2539
1c8f6a4d 2540 for (i = 1; i < (int) nmemb; i++)
35c08157 2541 {
1c8f6a4d
KLC
2542 for (j = (i - 1); j >= 0; j--)
2543 if (compar (ptr + i * size, ptr + j * size) >= 0)
35c08157
KLC
2544 break;
2545
1c8f6a4d
KLC
2546 j++;
2547
35c08157 2548 if (i == j)
1c8f6a4d 2549 continue; /* i is in order. */
35c08157
KLC
2550
2551 memcpy (tmp, ptr + i * size, size);
2552 memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2553 memcpy (ptr + j * size, tmp, size);
2554 }
e1fa0163 2555 free (tmp);
35c08157
KLC
2556}
2557
2558/* Sort relocation by r_offset.
2559
2560 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2561 algorithm. Relocations at the same r_offset must keep their order.
2562 For example, RELAX_ENTRY must be the very first relocation entry.
2563
2564 Currently, this function implements insertion-sort.
2565
2566 FIXME: If we already sort them in assembler, why bother sort them
2567 here again? */
2568
2569static int
2570compar_reloc (const void *lhs, const void *rhs)
2571{
2572 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2573 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2574
2575 if (l->r_offset > r->r_offset)
2576 return 1;
2577 else if (l->r_offset == r->r_offset)
2578 return 0;
2579 else
2580 return -1;
2581}
2582
2583/* Functions listed below are only used for old relocs.
fbaf61ad
NC
2584 nds32_elf_9_pcrel_reloc
2585 nds32_elf_do_9_pcrel_reloc
2586 nds32_elf_hi20_reloc
2587 nds32_elf_relocate_hi20
2588 nds32_elf_lo12_reloc
2589 nds32_elf_sda15_reloc
2590 nds32_elf_generic_reloc. */
35c08157
KLC
2591
2592/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2593
2594static bfd_reloc_status_type
fbaf61ad
NC
2595nds32_elf_9_pcrel_reloc (bfd * abfd,
2596 arelent * reloc_entry,
2597 asymbol * symbol,
2598 void * data,
2599 asection * input_section,
2600 bfd * output_bfd,
2601 char ** error_message ATTRIBUTE_UNUSED)
35c08157
KLC
2602{
2603 /* This part is from bfd_elf_generic_reloc. */
2604 if (output_bfd != (bfd *) NULL
2605 && (symbol->flags & BSF_SECTION_SYM) == 0
2606 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2607 {
2608 reloc_entry->address += input_section->output_offset;
2609 return bfd_reloc_ok;
2610 }
2611
2612 if (output_bfd != NULL)
2613 {
2614 /* FIXME: See bfd_perform_relocation. Is this right? */
2615 return bfd_reloc_continue;
2616 }
2617
2618 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2619 input_section,
2620 data, reloc_entry->address,
2621 symbol->section,
2622 (symbol->value
2623 + symbol->section->output_section->vma
2624 + symbol->section->output_offset),
2625 reloc_entry->addend);
2626}
2627
2628/* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2629#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2630
2631static bfd_reloc_status_type
fbaf61ad
NC
2632nds32_elf_do_9_pcrel_reloc (bfd * abfd,
2633 reloc_howto_type * howto,
2634 asection * input_section,
2635 bfd_byte * data,
2636 bfd_vma offset,
2637 asection * symbol_section ATTRIBUTE_UNUSED,
2638 bfd_vma symbol_value,
2639 bfd_vma addend)
35c08157
KLC
2640{
2641 bfd_signed_vma relocation;
2642 unsigned short x;
2643 bfd_reloc_status_type status;
2644
2645 /* Sanity check the address (offset in section). */
2646 if (offset > bfd_get_section_limit (abfd, input_section))
2647 return bfd_reloc_outofrange;
2648
2649 relocation = symbol_value + addend;
2650 /* Make it pc relative. */
2651 relocation -= (input_section->output_section->vma
2652 + input_section->output_offset);
2653 /* These jumps mask off the lower two bits of the current address
2654 before doing pcrel calculations. */
2655 relocation -= (offset & -(bfd_vma) 2);
2656
1c8f6a4d 2657 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
35c08157
KLC
2658 status = bfd_reloc_overflow;
2659 else
2660 status = bfd_reloc_ok;
2661
2662 x = bfd_getb16 (data + offset);
2663
2664 relocation >>= howto->rightshift;
2665 relocation <<= howto->bitpos;
2666 x = (x & ~howto->dst_mask)
2667 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2668
2669 bfd_putb16 ((bfd_vma) x, data + offset);
2670
2671 return status;
2672}
2673
2674/* Handle the R_NDS32_HI20_[SU]LO relocs.
2675 HI20_SLO is for the add3 and load/store with displacement instructions.
2676 HI20 is for the or3 instruction.
2677 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2678 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2679 we must add one to the high 16 bytes (which will get subtracted off when
2680 the low 16 bits are added).
2681 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2682 because there is a carry from the LO12 to the HI20. Here we just save
2683 the information we need; we do the actual relocation when we see the LO12.
2684 This code is copied from the elf32-mips.c. We also support an arbitrary
2685 number of HI20 relocs to be associated with a single LO12 reloc. The
2686 assembler sorts the relocs to ensure each HI20 immediately precedes its
2687 LO12. However if there are multiple copies, the assembler may not find
2688 the real LO12 so it picks the first one it finds. */
2689
2690struct nds32_hi20
2691{
2692 struct nds32_hi20 *next;
2693 bfd_byte *addr;
2694 bfd_vma addend;
2695};
2696
2697static struct nds32_hi20 *nds32_hi20_list;
2698
2699static bfd_reloc_status_type
fbaf61ad
NC
2700nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2701 arelent *reloc_entry,
2702 asymbol *symbol,
2703 void *data,
2704 asection *input_section,
2705 bfd *output_bfd,
2706 char **error_message ATTRIBUTE_UNUSED)
35c08157
KLC
2707{
2708 bfd_reloc_status_type ret;
2709 bfd_vma relocation;
2710 struct nds32_hi20 *n;
2711
2712 /* This part is from bfd_elf_generic_reloc.
2713 If we're relocating, and this an external symbol, we don't want
2714 to change anything. */
2715 if (output_bfd != (bfd *) NULL
2716 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2717 {
2718 reloc_entry->address += input_section->output_offset;
2719 return bfd_reloc_ok;
2720 }
2721
2722 /* Sanity check the address (offset in section). */
2723 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2724 return bfd_reloc_outofrange;
2725
2726 ret = bfd_reloc_ok;
2727 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2728 ret = bfd_reloc_undefined;
2729
2730 if (bfd_is_com_section (symbol->section))
2731 relocation = 0;
2732 else
2733 relocation = symbol->value;
2734
2735 relocation += symbol->section->output_section->vma;
2736 relocation += symbol->section->output_offset;
2737 relocation += reloc_entry->addend;
2738
2739 /* Save the information, and let LO12 do the actual relocation. */
2740 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2741 if (n == NULL)
2742 return bfd_reloc_outofrange;
2743
2744 n->addr = (bfd_byte *) data + reloc_entry->address;
2745 n->addend = relocation;
2746 n->next = nds32_hi20_list;
2747 nds32_hi20_list = n;
2748
2749 if (output_bfd != (bfd *) NULL)
2750 reloc_entry->address += input_section->output_offset;
2751
2752 return ret;
2753}
2754
2755/* Handle an NDS32 ELF HI20 reloc. */
2756
2757static void
2758nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
fbaf61ad
NC
2759 int type ATTRIBUTE_UNUSED,
2760 Elf_Internal_Rela *relhi,
2761 Elf_Internal_Rela *rello,
2762 bfd_byte *contents,
35c08157
KLC
2763 bfd_vma addend)
2764{
2765 unsigned long insn;
2766 bfd_vma addlo;
2767
2768 insn = bfd_getb32 (contents + relhi->r_offset);
2769
2770 addlo = bfd_getb32 (contents + rello->r_offset);
2771 addlo &= 0xfff;
2772
2773 addend += ((insn & 0xfffff) << 20) + addlo;
2774
2775 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2776 bfd_putb32 (insn, contents + relhi->r_offset);
2777}
2778
2779/* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2780 inplace relocation; this function exists in order to do the
2781 R_NDS32_HI20_[SU]LO relocation described above. */
2782
2783static bfd_reloc_status_type
2784nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2785 void *data, asection *input_section, bfd *output_bfd,
2786 char **error_message)
2787{
2788 /* This part is from bfd_elf_generic_reloc.
2789 If we're relocating, and this an external symbol, we don't want
2790 to change anything. */
2791 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2792 && reloc_entry->addend == 0)
2793 {
2794 reloc_entry->address += input_section->output_offset;
2795 return bfd_reloc_ok;
2796 }
2797
2798 if (nds32_hi20_list != NULL)
2799 {
2800 struct nds32_hi20 *l;
2801
2802 l = nds32_hi20_list;
2803 while (l != NULL)
2804 {
2805 unsigned long insn;
2806 unsigned long val;
2807 unsigned long vallo;
2808 struct nds32_hi20 *next;
2809
2810 /* Do the HI20 relocation. Note that we actually don't need
2811 to know anything about the LO12 itself, except where to
2812 find the low 12 bits of the addend needed by the LO12. */
2813 insn = bfd_getb32 (l->addr);
2814 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2815 vallo &= 0xfff;
2816 switch (reloc_entry->howto->type)
2817 {
2818 case R_NDS32_LO12S3:
2819 vallo <<= 3;
2820 break;
2821
2822 case R_NDS32_LO12S2:
2823 vallo <<= 2;
2824 break;
2825
2826 case R_NDS32_LO12S1:
2827 vallo <<= 1;
2828 break;
2829
2830 case R_NDS32_LO12S0:
2831 vallo <<= 0;
2832 break;
2833 }
2834
2835 val = ((insn & 0xfffff) << 12) + vallo;
2836 val += l->addend;
2837
2838 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2839 bfd_putb32 ((bfd_vma) insn, l->addr);
2840
2841 next = l->next;
2842 free (l);
2843 l = next;
2844 }
2845
2846 nds32_hi20_list = NULL;
2847 }
2848
2849 /* Now do the LO12 reloc in the usual way.
2850 ??? It would be nice to call bfd_elf_generic_reloc here,
2851 but we have partial_inplace set. bfd_elf_generic_reloc will
2852 pass the handling back to bfd_install_relocation which will install
2853 a section relative addend which is wrong. */
2854 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2855 input_section, output_bfd, error_message);
2856}
2857
2858/* Do generic partial_inplace relocation.
2859 This is a local replacement for bfd_elf_generic_reloc. */
2860
2861static bfd_reloc_status_type
2862nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2863 asymbol *symbol, void *data, asection *input_section,
2864 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2865{
2866 bfd_reloc_status_type ret;
2867 bfd_vma relocation;
2868 bfd_byte *inplace_address;
2869
2870 /* This part is from bfd_elf_generic_reloc.
2871 If we're relocating, and this an external symbol, we don't want
2872 to change anything. */
2873 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2874 && reloc_entry->addend == 0)
2875 {
2876 reloc_entry->address += input_section->output_offset;
2877 return bfd_reloc_ok;
2878 }
2879
2880 /* Now do the reloc in the usual way.
2881 ??? It would be nice to call bfd_elf_generic_reloc here,
2882 but we have partial_inplace set. bfd_elf_generic_reloc will
2883 pass the handling back to bfd_install_relocation which will install
2884 a section relative addend which is wrong. */
2885
2886 /* Sanity check the address (offset in section). */
2887 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2888 return bfd_reloc_outofrange;
2889
2890 ret = bfd_reloc_ok;
2891 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2892 ret = bfd_reloc_undefined;
2893
2894 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2895 relocation = 0;
2896 else
2897 relocation = symbol->value;
2898
2899 /* Only do this for a final link. */
2900 if (output_bfd == (bfd *) NULL)
2901 {
2902 relocation += symbol->section->output_section->vma;
2903 relocation += symbol->section->output_offset;
2904 }
2905
2906 relocation += reloc_entry->addend;
2907 switch (reloc_entry->howto->type)
2908 {
2909 case R_NDS32_LO12S3:
2910 relocation >>= 3;
2911 break;
2912
2913 case R_NDS32_LO12S2:
2914 relocation >>= 2;
2915 break;
2916
2917 case R_NDS32_LO12S1:
2918 relocation >>= 1;
2919 break;
2920
2921 case R_NDS32_LO12S0:
2922 default:
2923 relocation >>= 0;
2924 break;
2925 }
2926
2927 inplace_address = (bfd_byte *) data + reloc_entry->address;
2928
2929#define DOIT(x) \
2930 x = ((x & ~reloc_entry->howto->dst_mask) | \
2931 (((x & reloc_entry->howto->src_mask) + relocation) & \
2932 reloc_entry->howto->dst_mask))
2933
2934 switch (reloc_entry->howto->size)
2935 {
2936 case 1:
2937 {
2938 short x = bfd_getb16 (inplace_address);
2939
2940 DOIT (x);
2941 bfd_putb16 ((bfd_vma) x, inplace_address);
2942 }
2943 break;
2944 case 2:
2945 {
2946 unsigned long x = bfd_getb32 (inplace_address);
2947
2948 DOIT (x);
2949 bfd_putb32 ((bfd_vma) x, inplace_address);
2950 }
2951 break;
2952 default:
2953 BFD_ASSERT (0);
2954 }
2955
2956 if (output_bfd != (bfd *) NULL)
2957 reloc_entry->address += input_section->output_offset;
2958
2959 return ret;
2960}
2961
2962/* Handle the R_NDS32_SDA15 reloc.
2963 This reloc is used to compute the address of objects in the small data area
2964 and to perform loads and stores from that area.
2965 The lower 15 bits are sign extended and added to the register specified
2966 in the instruction, which is assumed to point to _SDA_BASE_.
2967
2968 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2969 the access size, this must be taken care of. */
2970
2971static bfd_reloc_status_type
2972nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2973 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2974 asection *input_section, bfd *output_bfd,
2975 char **error_message ATTRIBUTE_UNUSED)
2976{
2977 /* This part is from bfd_elf_generic_reloc. */
2978 if (output_bfd != (bfd *) NULL
2979 && (symbol->flags & BSF_SECTION_SYM) == 0
2980 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2981 {
2982 reloc_entry->address += input_section->output_offset;
2983 return bfd_reloc_ok;
2984 }
2985
2986 if (output_bfd != NULL)
2987 {
2988 /* FIXME: See bfd_perform_relocation. Is this right? */
2989 return bfd_reloc_continue;
2990 }
2991
2992 /* FIXME: not sure what to do here yet. But then again, the linker
2993 may never call us. */
2994 abort ();
2995}
2996
2997/* nds32_elf_ignore_reloc is the special function for
2998 relocation types which don't need to be relocated
2999 like relaxation relocation types.
3000 This function simply return bfd_reloc_ok when it is
3001 invoked. */
3002
3003static bfd_reloc_status_type
3004nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3005 asymbol *symbol ATTRIBUTE_UNUSED,
3006 void *data ATTRIBUTE_UNUSED, asection *input_section,
3007 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3008{
3009 if (output_bfd != NULL)
3010 reloc_entry->address += input_section->output_offset;
3011
3012 return bfd_reloc_ok;
3013}
3014\f
3015
3016/* Map BFD reloc types to NDS32 ELF reloc types. */
3017
3018struct nds32_reloc_map_entry
3019{
3020 bfd_reloc_code_real_type bfd_reloc_val;
3021 unsigned char elf_reloc_val;
3022};
3023
3024static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3025{
3026 {BFD_RELOC_NONE, R_NDS32_NONE},
3027 {BFD_RELOC_16, R_NDS32_16_RELA},
3028 {BFD_RELOC_32, R_NDS32_32_RELA},
fbaf61ad
NC
3029 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3030 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3031
35c08157 3032 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
35c08157
KLC
3033 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3034 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3035 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3036 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3037 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
35c08157
KLC
3038 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3039 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3040 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3041 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3042 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3043 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3044 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3045 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3046 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3047 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
fbaf61ad
NC
3048 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3049 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3050 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3051 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
35c08157
KLC
3052 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3053 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3054 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3055 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3056 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3057 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3058 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3059 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
fbaf61ad
NC
3060 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3061 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
35c08157
KLC
3062 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3063 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3064 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
35c08157
KLC
3065 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3066 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
35c08157
KLC
3067 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3068 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3069 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3070 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3071 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3072 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3073 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3074 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3075 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3076 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3077 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3078 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3079 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
fbaf61ad
NC
3080 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3081 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3082 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3083 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3084 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3085 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3086 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
35c08157
KLC
3087 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3088 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3089 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3090 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
35c08157
KLC
3091 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3092 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3093 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3094 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
35c08157
KLC
3095 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3096 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3097 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3098 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
fbaf61ad
NC
3099 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3100 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3101 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3102 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3103 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3104 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3105 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
35c08157
KLC
3106 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3107 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
fbaf61ad
NC
3108 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3109 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3110 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3111 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3112 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3113 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3114 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3115 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
35c08157
KLC
3116 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3117 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
35c08157 3118 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
35c08157
KLC
3119 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3120 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3121 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3122 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3123 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
35c08157
KLC
3124 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3125 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3126 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3127 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
fbaf61ad 3128 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
35c08157
KLC
3129 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3130 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3131 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3132 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3133 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
fbaf61ad
NC
3134 /* Not sure. */
3135 {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3136 /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
1c8f6a4d
KLC
3137 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3138 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
1c8f6a4d
KLC
3139 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3140 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3141 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3142 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
fbaf61ad
NC
3143 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3144 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3145 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3146 {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3147 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3148 {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3149 {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3150 {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3151 {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3152 {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3153 {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3154 {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3155 {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3156 {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3157 {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3158 {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3159 {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3160 {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3161 {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3162 {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3163 {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
35c08157
KLC
3164};
3165
3166/* Patch tag. */
3167
fbaf61ad
NC
3168/* Reserve space for COUNT dynamic relocations in relocation selection
3169 SRELOC. */
3170
3171static inline void
3172elf32_nds32_allocate_dynrelocs (struct bfd_link_info *info, asection *sreloc,
3173 bfd_size_type count)
3174{
3175 BFD_ASSERT (elf_hash_table (info)->dynamic_sections_created);
3176 if (sreloc == NULL)
3177 abort ();
3178 sreloc->size += sizeof (Elf32_External_Rela) * count;
3179}
3180
35c08157
KLC
3181static reloc_howto_type *
3182bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3183 const char *r_name)
3184{
3185 unsigned int i;
3186
3187 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3188 if (nds32_elf_howto_table[i].name != NULL
3189 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3190 return &nds32_elf_howto_table[i];
3191
3192 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3193 if (nds32_elf_relax_howto_table[i].name != NULL
3194 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3195 return &nds32_elf_relax_howto_table[i];
3196
3197 return NULL;
3198}
3199
3200static reloc_howto_type *
3201bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3202{
3203 if (code < R_NDS32_RELAX_ENTRY)
3204 {
3205 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3206 return &nds32_elf_howto_table[code];
3207 }
3208 else
3209 {
fbaf61ad
NC
3210 if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3211 >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3212 {
3213 int i = code;
3214 i += 1;
3215 }
3216
35c08157
KLC
3217 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3218 < ARRAY_SIZE (nds32_elf_relax_howto_table));
3219 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3220 }
3221}
3222
3223static reloc_howto_type *
3224bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3225 bfd_reloc_code_real_type code)
3226{
3227 unsigned int i;
3228
3229 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3230 {
3231 if (nds32_reloc_map[i].bfd_reloc_val == code)
3232 return bfd_elf32_bfd_reloc_type_table_lookup
fbaf61ad 3233 (nds32_reloc_map[i].elf_reloc_val);
35c08157
KLC
3234 }
3235
3236 return NULL;
3237}
3238
3239/* Set the howto pointer for an NDS32 ELF reloc. */
3240
f3185997
NC
3241static bfd_boolean
3242nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
35c08157
KLC
3243 Elf_Internal_Rela *dst)
3244{
3245 enum elf_nds32_reloc_type r_type;
3246
3247 r_type = ELF32_R_TYPE (dst->r_info);
5860e3f8
NC
3248 if (r_type > R_NDS32_GNU_VTENTRY)
3249 {
695344c0 3250 /* xgettext:c-format */
0aa13fee
AM
3251 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3252 abfd, r_type);
f3185997
NC
3253 bfd_set_error (bfd_error_bad_value);
3254 return FALSE;
5860e3f8 3255 }
fbaf61ad
NC
3256
3257 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
35c08157 3258 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
f3185997 3259 return TRUE;
35c08157
KLC
3260}
3261
f3185997 3262static bfd_boolean
35c08157
KLC
3263nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3264 Elf_Internal_Rela *dst)
3265{
f3185997
NC
3266 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3267
3268 if ((r_type == R_NDS32_NONE)
3269 || ((r_type > R_NDS32_GNU_VTENTRY)
3270 && (r_type < R_NDS32_max)))
3271 {
3272 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3273 return TRUE;
3274 }
3275
3276 /* xgettext:c-format */
3277 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3278 bfd_set_error (bfd_error_bad_value);
fbaf61ad 3279 return FALSE;
35c08157
KLC
3280}
3281
3282/* Support for core dump NOTE sections.
3283 Reference to include/linux/elfcore.h in Linux. */
3284
3285static bfd_boolean
3286nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3287{
3288 int offset;
3289 size_t size;
3290
3291 switch (note->descsz)
3292 {
3293 case 0x114:
fbaf61ad 3294 /* Linux/NDS32 32-bit, ABI1. */
35c08157
KLC
3295
3296 /* pr_cursig */
3297 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3298
3299 /* pr_pid */
3300 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3301
3302 /* pr_reg */
3303 offset = 72;
3304 size = 200;
3305 break;
3306
3307 case 0xfc:
fbaf61ad 3308 /* Linux/NDS32 32-bit. */
35c08157
KLC
3309
3310 /* pr_cursig */
3311 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3312
3313 /* pr_pid */
3314 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3315
3316 /* pr_reg */
3317 offset = 72;
3318 size = 176;
3319 break;
3320
3321 default:
3322 return FALSE;
3323 }
3324
3325 /* Make a ".reg" section. */
3326 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3327 size, note->descpos + offset);
3328}
3329
3330static bfd_boolean
3331nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3332{
3333 switch (note->descsz)
3334 {
3335 case 124:
fbaf61ad 3336 /* Linux/NDS32. */
35c08157
KLC
3337
3338 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3339 elf_tdata (abfd)->core->program =
3340 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3341 elf_tdata (abfd)->core->command =
3342 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2b804145 3343 break;
35c08157
KLC
3344
3345 default:
3346 return FALSE;
3347 }
3348
3349 /* Note that for some reason, a spurious space is tacked
3350 onto the end of the args in some (at least one anyway)
3351 implementations, so strip it off if it exists. */
3352 {
3353 char *command = elf_tdata (abfd)->core->command;
3354 int n = strlen (command);
3355
3356 if (0 < n && command[n - 1] == ' ')
3357 command[n - 1] = '\0';
3358 }
3359
3360 return TRUE;
3361}
3362
3363/* Hook called by the linker routine which adds symbols from an object
3364 file. We must handle the special NDS32 section numbers here.
3365 We also keep watching for whether we need to create the sdata special
3366 linker sections. */
3367
3368static bfd_boolean
3369nds32_elf_add_symbol_hook (bfd *abfd,
3370 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3371 Elf_Internal_Sym *sym,
3372 const char **namep ATTRIBUTE_UNUSED,
3373 flagword *flagsp ATTRIBUTE_UNUSED,
3374 asection **secp, bfd_vma *valp)
3375{
3376 switch (sym->st_shndx)
3377 {
3378 case SHN_COMMON:
3379 /* Common symbols less than the GP size are automatically
3380 treated as SHN_MIPS_SCOMMON symbols. */
3381 if (sym->st_size > elf_gp_size (abfd)
3382 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3383 break;
3384
3385 /* st_value is the alignemnt constraint.
3386 That might be its actual size if it is an array or structure. */
3387 switch (sym->st_value)
3388 {
3389 case 1:
3390 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3391 break;
3392 case 2:
3393 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3394 break;
3395 case 4:
3396 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3397 break;
3398 case 8:
3399 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3400 break;
3401 default:
3402 return TRUE;
3403 }
3404
3405 (*secp)->flags |= SEC_IS_COMMON;
3406 *valp = sym->st_size;
3407 break;
3408 }
3409
3410 return TRUE;
3411}
3412
35c08157
KLC
3413/* This function can figure out the best location for a base register to access
3414 data relative to this base register
3415 INPUT:
3416 sda_d0: size of first DOUBLE WORD data section
3417 sda_w0: size of first WORD data section
3418 sda_h0: size of first HALF WORD data section
3419 sda_b : size of BYTE data section
3420 sda_hi: size of second HALF WORD data section
3421 sda_w1: size of second WORD data section
3422 sda_d1: size of second DOUBLE WORD data section
3423 OUTPUT:
3424 offset (always positive) from the beginning of sda_d0 if OK
3425 a negative error value if fail
3426 NOTE:
3427 these 7 sections have to be located back to back if exist
3428 a pass in 0 value for non-existing section */
3429
3430/* Due to the interpretation of simm15 field of load/store depending on
3431 data accessing size, the organization of base register relative data shall
3432 like the following figure
3433 -------------------------------------------
3434 | DOUBLE WORD sized data (range +/- 128K)
3435 -------------------------------------------
3436 | WORD sized data (range +/- 64K)
3437 -------------------------------------------
3438 | HALF WORD sized data (range +/- 32K)
3439 -------------------------------------------
3440 | BYTE sized data (range +/- 16K)
3441 -------------------------------------------
3442 | HALF WORD sized data (range +/- 32K)
3443 -------------------------------------------
3444 | WORD sized data (range +/- 64K)
3445 -------------------------------------------
3446 | DOUBLE WORD sized data (range +/- 128K)
3447 -------------------------------------------
3448 Its base register shall be set to access these data freely. */
3449
3450/* We have to figure out the SDA_BASE value, so that we can adjust the
3451 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3452 BFD. If we can't find it, we're stuck. We cache it in the ELF
3453 target data. We don't need to adjust the symbol value for an
3454 external symbol if we are producing relocatable output. */
3455
3456static asection *sda_rela_sec = NULL;
3457
1c8f6a4d 3458#define SDA_SECTION_NUM 10
35c08157
KLC
3459
3460static bfd_reloc_status_type
fbaf61ad
NC
3461nds32_elf_final_sda_base (bfd * output_bfd,
3462 struct bfd_link_info * info,
3463 bfd_vma * psb,
3464 bfd_boolean add_symbol)
35c08157
KLC
3465{
3466 int relax_fp_as_gp;
3467 struct elf_nds32_link_hash_table *table;
3468 struct bfd_link_hash_entry *h, *h2;
1c8f6a4d 3469 long unsigned int total = 0;
fbaf61ad
NC
3470 asection *first = NULL, *final = NULL, *temp;
3471 bfd_vma sda_base = 0;
35c08157
KLC
3472
3473 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
fbaf61ad
NC
3474 if (!h || (h->type != bfd_link_hash_defined
3475 && h->type != bfd_link_hash_defweak))
35c08157 3476 {
35c08157
KLC
3477 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3478 4 byte-aligned. Therefore, it has to set the first section ".data"
3479 4 byte-aligned. */
3480 static const char sec_name[SDA_SECTION_NUM][10] =
3481 {
3482 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
1c8f6a4d 3483 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
35c08157
KLC
3484 };
3485 size_t i = 0;
3486
3487 if (output_bfd->sections == NULL)
3488 {
3489 *psb = elf_gp (output_bfd);
3490 return bfd_reloc_ok;
3491 }
3492
3493 /* Get the first and final section. */
fbaf61ad 3494 while (i < ARRAY_SIZE (sec_name))
35c08157
KLC
3495 {
3496 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3497 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3498 first = temp;
3499 if (temp && (temp->size != 0 || temp->rawsize != 0))
3500 final = temp;
1c8f6a4d
KLC
3501
3502 /* Summarize the sections in order to check if joining .bss. */
3503 if (temp && temp->size != 0)
3504 total += temp->size;
3505 else if (temp && temp->rawsize != 0)
3506 total += temp->rawsize;
3507
35c08157
KLC
3508 i++;
3509 }
3510
1c8f6a4d
KLC
3511 /* Check .bss size. */
3512 temp = bfd_get_section_by_name (output_bfd, ".bss");
3513 if (temp)
3514 {
3515 if (temp->size != 0)
3516 total += temp->size;
3517 else if (temp->rawsize != 0)
3518 total += temp->rawsize;
3519
3520 if (total < 0x80000)
3521 {
3522 if (!first && (temp->size != 0 || temp->rawsize != 0))
3523 first = temp;
3524 if ((temp->size != 0 || temp->rawsize != 0))
3525 final = temp;
3526 }
3527 }
3528
35c08157
KLC
3529 if (first && final)
3530 {
3531 /* The middle of data region. */
1c8f6a4d 3532 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
35c08157
KLC
3533
3534 /* Find the section sda_base located. */
3535 i = 0;
fbaf61ad 3536 while (i < ARRAY_SIZE (sec_name))
35c08157
KLC
3537 {
3538 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3539 if (final && (final->size != 0 || final->rawsize != 0)
3540 && sda_base >= final->vma)
3541 {
3542 first = final;
3543 i++;
3544 }
3545 else
3546 break;
3547 }
3548 }
3549 else
3550 {
fbaf61ad
NC
3551 /* If there is not any default data section in output bfd, try to find
3552 the first data section. If no data section be found, just simplily
3553 choose the first output section. */
3554 temp = output_bfd->sections;
3555 while (temp)
3556 {
3557 if (temp->flags & SEC_ALLOC
3558 && (((temp->flags & SEC_DATA)
3559 && ((temp->flags & SEC_READONLY) == 0))
3560 || (temp->flags & SEC_LOAD) == 0)
3561 && (temp->size != 0 || temp->rawsize != 0))
3562 {
3563 if (!first)
3564 first = temp;
3565 final = temp;
3566 }
3567 temp = temp->next;
3568 }
3569
3570 /* There is no data or bss section. */
3571 if (!first || (first->size == 0 && first->rawsize == 0))
3572 {
3573 first = output_bfd->sections;
3574 while (first && first->size == 0 && first->rawsize == 0)
3575 first = first->next;
3576 }
3577
3578 /* There is no concrete section. */
35c08157
KLC
3579 if (!first)
3580 {
3581 *psb = elf_gp (output_bfd);
3582 return bfd_reloc_ok;
3583 }
fbaf61ad
NC
3584
3585 if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3586 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3587 else
3588 sda_base = first->vma + 0x2000;
35c08157
KLC
3589 }
3590
3591 sda_base -= first->vma;
3592 sda_base = sda_base & (~7);
3593
3594 if (!_bfd_generic_link_add_one_symbol
fbaf61ad
NC
3595 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3596 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3597 get_elf_backend_data (output_bfd)->collect, &h))
35c08157
KLC
3598 return FALSE;
3599
3600 sda_rela_sec = first;
fbaf61ad 3601 }
35c08157 3602
fbaf61ad
NC
3603 /* Set _FP_BASE_ to _SDA_BASE_. */
3604 table = nds32_elf_hash_table (info);
3605 relax_fp_as_gp = table->relax_fp_as_gp;
3606 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3607 /* _SDA_BASE_ is difined in linker script. */
3608 if (!first)
3609 {
3610 first = h->u.def.section;
3611 sda_base = h->u.def.value;
3612 }
3613
3614 if (relax_fp_as_gp && h2
3615 && (h2->type == bfd_link_hash_undefweak
3616 || h2->type == bfd_link_hash_undefined))
3617 {
3618 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3619 And set FP equal to SDA_BASE to do relaxation for
3620 la $fp, _FP_BASE_. */
3621 if (!_bfd_generic_link_add_one_symbol
3622 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3623 first, sda_base, (const char *) NULL,
3624 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3625 return FALSE;
35c08157
KLC
3626 }
3627
535b785f 3628 if (add_symbol)
35c08157
KLC
3629 {
3630 if (h)
3631 {
3632 /* Now set gp. */
3633 elf_gp (output_bfd) = (h->u.def.value
3634 + h->u.def.section->output_section->vma
3635 + h->u.def.section->output_offset);
3636 }
3637 else
3638 {
38f14ab8 3639 _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
35c08157
KLC
3640 return bfd_reloc_dangerous;
3641 }
3642 }
3643
fbaf61ad
NC
3644 *psb = h->u.def.value
3645 + h->u.def.section->output_section->vma
3646 + h->u.def.section->output_offset;
35c08157
KLC
3647 return bfd_reloc_ok;
3648}
3649\f
3650
3651/* Return size of a PLT entry. */
3652#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3653
35c08157
KLC
3654/* Create an entry in an nds32 ELF linker hash table. */
3655
3656static struct bfd_hash_entry *
3657nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3658 struct bfd_hash_table *table,
3659 const char *string)
3660{
3661 struct elf_nds32_link_hash_entry *ret;
3662
3663 ret = (struct elf_nds32_link_hash_entry *) entry;
3664
3665 /* Allocate the structure if it has not already been allocated by a
3666 subclass. */
3667 if (ret == NULL)
3668 ret = (struct elf_nds32_link_hash_entry *)
3669 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3670
3671 if (ret == NULL)
3672 return (struct bfd_hash_entry *) ret;
3673
3674 /* Call the allocation method of the superclass. */
3675 ret = (struct elf_nds32_link_hash_entry *)
3676 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3677
3678 if (ret != NULL)
3679 {
3680 struct elf_nds32_link_hash_entry *eh;
3681
3682 eh = (struct elf_nds32_link_hash_entry *) ret;
3683 eh->dyn_relocs = NULL;
1c8f6a4d 3684 eh->tls_type = GOT_UNKNOWN;
fbaf61ad 3685 eh->offset_to_gp = 0;
35c08157
KLC
3686 }
3687
3688 return (struct bfd_hash_entry *) ret;
3689}
3690
3691/* Create an nds32 ELF linker hash table. */
3692
3693static struct bfd_link_hash_table *
3694nds32_elf_link_hash_table_create (bfd *abfd)
3695{
3696 struct elf_nds32_link_hash_table *ret;
3697
3698 bfd_size_type amt = sizeof (struct elf_nds32_link_hash_table);
3699
3700 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3701 if (ret == NULL)
3702 return NULL;
3703
fbaf61ad 3704 /* Patch tag. */
35c08157
KLC
3705 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3706 nds32_elf_link_hash_newfunc,
3707 sizeof (struct elf_nds32_link_hash_entry),
3708 NDS32_ELF_DATA))
3709 {
3710 free (ret);
3711 return NULL;
3712 }
3713
fbaf61ad
NC
3714 ret->sdynbss = NULL;
3715 ret->srelbss = NULL;
3716 ret->sym_ld_script = NULL;
3717
35c08157
KLC
3718 return &ret->root.root;
3719}
3720
fbaf61ad
NC
3721/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3722 shortcuts to them in our hash table. */
3723
3724static bfd_boolean
3725create_got_section (bfd *dynobj, struct bfd_link_info *info)
3726{
3727 struct elf_link_hash_table *ehtab;
3728
3729 if (!_bfd_elf_create_got_section (dynobj, info))
3730 return FALSE;
3731
3732 ehtab = elf_hash_table (info);
3733 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3734 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3735 if (!ehtab->sgot || !ehtab->sgotplt)
3736 abort ();
3737
3738 /* _bfd_elf_create_got_section will create it for us. */
3739 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3740 if (ehtab->srelgot == NULL
3741 || !bfd_set_section_flags (dynobj, ehtab->srelgot,
3742 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3743 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3744 | SEC_READONLY))
3745 || !bfd_set_section_alignment (dynobj, ehtab->srelgot, 2))
3746 return FALSE;
3747
3748 return TRUE;
3749}
3750
35c08157
KLC
3751/* Create dynamic sections when linking against a dynamic object. */
3752
3753static bfd_boolean
3754nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3755{
fbaf61ad 3756 struct elf_link_hash_table *ehtab;
35c08157
KLC
3757 struct elf_nds32_link_hash_table *htab;
3758 flagword flags, pltflags;
3759 register asection *s;
3760 const struct elf_backend_data *bed;
3761 int ptralign = 2; /* 32-bit */
fbaf61ad
NC
3762 const char *secname;
3763 char *relname;
3764 flagword secflags;
3765 asection *sec;
35c08157
KLC
3766
3767 bed = get_elf_backend_data (abfd);
fbaf61ad 3768 ehtab = elf_hash_table (info);
35c08157
KLC
3769 htab = nds32_elf_hash_table (info);
3770
3771 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3772 .rel[a].bss sections. */
3773
3774 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3775 | SEC_LINKER_CREATED);
3776
3777 pltflags = flags;
3778 pltflags |= SEC_CODE;
3779 if (bed->plt_not_loaded)
3780 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3781 if (bed->plt_readonly)
3782 pltflags |= SEC_READONLY;
3783
3784 s = bfd_make_section (abfd, ".plt");
fbaf61ad 3785 ehtab->splt = s;
35c08157
KLC
3786 if (s == NULL
3787 || !bfd_set_section_flags (abfd, s, pltflags)
3788 || !bfd_set_section_alignment (abfd, s, bed->plt_alignment))
3789 return FALSE;
3790
3791 if (bed->want_plt_sym)
3792 {
3793 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3794 .plt section. */
3795 struct bfd_link_hash_entry *bh = NULL;
3796 struct elf_link_hash_entry *h;
3797
3798 if (!(_bfd_generic_link_add_one_symbol
3799 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3800 (bfd_vma) 0, (const char *) NULL, FALSE,
3801 get_elf_backend_data (abfd)->collect, &bh)))
3802 return FALSE;
3803
3804 h = (struct elf_link_hash_entry *) bh;
3805 h->def_regular = 1;
3806 h->type = STT_OBJECT;
3807
0e1862bb 3808 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
35c08157
KLC
3809 return FALSE;
3810 }
3811
3812 s = bfd_make_section (abfd,
3813 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
fbaf61ad 3814 ehtab->srelplt = s;
35c08157
KLC
3815 if (s == NULL
3816 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3817 || !bfd_set_section_alignment (abfd, s, ptralign))
3818 return FALSE;
3819
fbaf61ad 3820 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
35c08157
KLC
3821 return FALSE;
3822
fbaf61ad
NC
3823 for (sec = abfd->sections; sec; sec = sec->next)
3824 {
3825 secflags = bfd_get_section_flags (abfd, sec);
3826 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3827 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3828 continue;
3829 secname = bfd_get_section_name (abfd, sec);
3830 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3831 strcpy (relname, ".rela");
3832 strcat (relname, secname);
3833 if (bfd_get_section_by_name (abfd, secname))
3834 continue;
3835 s = bfd_make_section (abfd, relname);
3836 if (s == NULL
3837 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3838 || !bfd_set_section_alignment (abfd, s, ptralign))
3839 return FALSE;
3840 }
35c08157
KLC
3841
3842 if (bed->want_dynbss)
3843 {
3844 /* The .dynbss section is a place to put symbols which are defined
3845 by dynamic objects, are referenced by regular objects, and are
3846 not functions. We must allocate space for them in the process
3847 image and use a R_*_COPY reloc to tell the dynamic linker to
3848 initialize them at run time. The linker script puts the .dynbss
3849 section into the .bss section of the final image. */
3850 s = bfd_make_section (abfd, ".dynbss");
3851 htab->sdynbss = s;
3852 if (s == NULL
3853 || !bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
3854 return FALSE;
3855 /* The .rel[a].bss section holds copy relocs. This section is not
3856 normally needed. We need to create it here, though, so that the
3857 linker will map it to an output section. We can't just create it
3858 only if we need it, because we will not know whether we need it
3859 until we have seen all the input files, and the first time the
3860 main linker code calls BFD after examining all the input files
3861 (size_dynamic_sections) the input sections have already been
3862 mapped to the output sections. If the section turns out not to
3863 be needed, we can discard it later. We will never need this
3864 section when generating a shared object, since they do not use
3865 copy relocs. */
0e1862bb 3866 if (!bfd_link_pic (info))
35c08157
KLC
3867 {
3868 s = bfd_make_section (abfd, (bed->default_use_rela_p
3869 ? ".rela.bss" : ".rel.bss"));
3870 htab->srelbss = s;
3871 if (s == NULL
3872 || !bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3873 || !bfd_set_section_alignment (abfd, s, ptralign))
3874 return FALSE;
3875 }
3876 }
3877
3878 return TRUE;
3879}
3880
3881/* Copy the extra info we tack onto an elf_link_hash_entry. */
3882static void
3883nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3884 struct elf_link_hash_entry *dir,
3885 struct elf_link_hash_entry *ind)
3886{
3887 struct elf_nds32_link_hash_entry *edir, *eind;
3888
3889 edir = (struct elf_nds32_link_hash_entry *) dir;
3890 eind = (struct elf_nds32_link_hash_entry *) ind;
3891
3892 if (eind->dyn_relocs != NULL)
3893 {
3894 if (edir->dyn_relocs != NULL)
3895 {
3bf083ed
AM
3896 struct elf_dyn_relocs **pp;
3897 struct elf_dyn_relocs *p;
35c08157
KLC
3898
3899 if (ind->root.type == bfd_link_hash_indirect)
3900 abort ();
3901
3902 /* Add reloc counts against the weak sym to the strong sym
3903 list. Merge any entries against the same section. */
3904 for (pp = &eind->dyn_relocs; (p = *pp) != NULL;)
3905 {
3bf083ed 3906 struct elf_dyn_relocs *q;
35c08157
KLC
3907
3908 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3909 if (q->sec == p->sec)
3910 {
3911 q->pc_count += p->pc_count;
3912 q->count += p->count;
3913 *pp = p->next;
3914 break;
3915 }
3916 if (q == NULL)
3917 pp = &p->next;
3918 }
3919 *pp = edir->dyn_relocs;
3920 }
3921
3922 edir->dyn_relocs = eind->dyn_relocs;
3923 eind->dyn_relocs = NULL;
3924 }
3925
fbaf61ad
NC
3926 if (ind->root.type == bfd_link_hash_indirect)
3927 {
3928 if (dir->got.refcount <= 0)
3929 {
3930 edir->tls_type = eind->tls_type;
3931 eind->tls_type = GOT_UNKNOWN;
3932 }
3933 }
3934
35c08157
KLC
3935 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3936}
3937\f
fbaf61ad 3938
63c1f59d
AM
3939/* Find dynamic relocs for H that apply to read-only sections. */
3940
3941static asection *
3942readonly_dynrelocs (struct elf_link_hash_entry *h)
3943{
3bf083ed 3944 struct elf_dyn_relocs *p;
63c1f59d
AM
3945
3946 for (p = elf32_nds32_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
3947 {
3948 asection *s = p->sec->output_section;
3949
3950 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3951 return p->sec;
3952 }
3953 return NULL;
3954}
35c08157
KLC
3955
3956/* Adjust a symbol defined by a dynamic object and referenced by a
3957 regular object. The current definition is in some section of the
3958 dynamic object, but we're not including those sections. We have to
3959 change the definition to something the rest of the link can
3960 understand. */
3961
3962static bfd_boolean
3963nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3964 struct elf_link_hash_entry *h)
3965{
3966 struct elf_nds32_link_hash_table *htab;
35c08157
KLC
3967 bfd *dynobj;
3968 asection *s;
3969 unsigned int power_of_two;
3970
3971 dynobj = elf_hash_table (info)->dynobj;
3972
3973 /* Make sure we know what is going on here. */
3974 BFD_ASSERT (dynobj != NULL
3975 && (h->needs_plt
60d67dc8 3976 || h->is_weakalias
35c08157
KLC
3977 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3978
3979
3980 /* If this is a function, put it in the procedure linkage table. We
3981 will fill in the contents of the procedure linkage table later,
3982 when we know the address of the .got section. */
3983 if (h->type == STT_FUNC || h->needs_plt)
3984 {
0e1862bb 3985 if (!bfd_link_pic (info)
35c08157
KLC
3986 && !h->def_dynamic
3987 && !h->ref_dynamic
3988 && h->root.type != bfd_link_hash_undefweak
3989 && h->root.type != bfd_link_hash_undefined)
3990 {
3991 /* This case can occur if we saw a PLT reloc in an input
3992 file, but the symbol was never referred to by a dynamic
3993 object. In such a case, we don't actually need to build
3994 a procedure linkage table, and we can just do a PCREL
3995 reloc instead. */
3996 h->plt.offset = (bfd_vma) - 1;
3997 h->needs_plt = 0;
3998 }
3999
4000 return TRUE;
4001 }
4002 else
4003 h->plt.offset = (bfd_vma) - 1;
4004
4005 /* If this is a weak symbol, and there is a real definition, the
4006 processor independent code will have arranged for us to see the
4007 real definition first, and we can just use the same value. */
60d67dc8 4008 if (h->is_weakalias)
35c08157 4009 {
60d67dc8
AM
4010 struct elf_link_hash_entry *def = weakdef (h);
4011 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4012 h->root.u.def.section = def->root.u.def.section;
4013 h->root.u.def.value = def->root.u.def.value;
35c08157
KLC
4014 return TRUE;
4015 }
4016
4017 /* This is a reference to a symbol defined by a dynamic object which
4018 is not a function. */
4019
4020 /* If we are creating a shared library, we must presume that the
4021 only references to the symbol are via the global offset table.
4022 For such cases we need not do anything here; the relocations will
4023 be handled correctly by relocate_section. */
0e1862bb 4024 if (bfd_link_pic (info))
35c08157
KLC
4025 return TRUE;
4026
4027 /* If there are no references to this symbol that do not use the
4028 GOT, we don't need to generate a copy reloc. */
4029 if (!h->non_got_ref)
4030 return TRUE;
4031
4032 /* If -z nocopyreloc was given, we won't generate them either. */
3bf083ed 4033 if (0 && info->nocopyreloc)
35c08157
KLC
4034 {
4035 h->non_got_ref = 0;
4036 return TRUE;
4037 }
4038
3bf083ed
AM
4039 /* If we don't find any dynamic relocs in read-only sections, then
4040 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
fbaf61ad 4041 if (!readonly_dynrelocs (h))
35c08157
KLC
4042 {
4043 h->non_got_ref = 0;
4044 return TRUE;
4045 }
4046
4047 /* We must allocate the symbol in our .dynbss section, which will
4048 become part of the .bss section of the executable. There will be
4049 an entry for this symbol in the .dynsym section. The dynamic
4050 object will contain position independent code, so all references
4051 from the dynamic object to this symbol will go through the global
4052 offset table. The dynamic linker will use the .dynsym entry to
4053 determine the address it must put in the global offset table, so
4054 both the dynamic object and the regular object will refer to the
4055 same memory location for the variable. */
4056
4057 htab = nds32_elf_hash_table (info);
4058 s = htab->sdynbss;
4059 BFD_ASSERT (s != NULL);
4060
4061 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4062 to copy the initial value out of the dynamic object and into the
4063 runtime process image. We need to remember the offset into the
4064 .rela.bss section we are going to use. */
4065 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4066 {
4067 asection *srel;
4068
4069 srel = htab->srelbss;
4070 BFD_ASSERT (srel != NULL);
4071 srel->size += sizeof (Elf32_External_Rela);
4072 h->needs_copy = 1;
4073 }
4074
4075 /* We need to figure out the alignment required for this symbol. I
4076 have no idea how ELF linkers handle this. */
4077 power_of_two = bfd_log2 (h->size);
4078 if (power_of_two > 3)
4079 power_of_two = 3;
4080
4081 /* Apply the required alignment. */
4082 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4083 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4084 {
4085 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
4086 return FALSE;
4087 }
4088
4089 /* Define the symbol as being at this point in the section. */
4090 h->root.u.def.section = s;
4091 h->root.u.def.value = s->size;
4092
4093 /* Increment the section size to make room for the symbol. */
4094 s->size += h->size;
4095
4096 return TRUE;
4097}
4098
4099/* Allocate space in .plt, .got and associated reloc sections for
4100 dynamic relocs. */
4101
4102static bfd_boolean
4103allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4104{
4105 struct bfd_link_info *info;
fbaf61ad 4106 struct elf_link_hash_table *ehtab;
35c08157
KLC
4107 struct elf_nds32_link_hash_table *htab;
4108 struct elf_nds32_link_hash_entry *eh;
3bf083ed 4109 struct elf_dyn_relocs *p;
35c08157
KLC
4110
4111 if (h->root.type == bfd_link_hash_indirect)
4112 return TRUE;
4113
fbaf61ad
NC
4114 /* When warning symbols are created, they **replace** the "real"
4115 entry in the hash table, thus we never get to see the real
4116 symbol in a hash traversal. So look at it now. */
35c08157 4117 if (h->root.type == bfd_link_hash_warning)
35c08157
KLC
4118 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4119
fbaf61ad
NC
4120 eh = (struct elf_nds32_link_hash_entry *) h;
4121
35c08157 4122 info = (struct bfd_link_info *) inf;
fbaf61ad 4123 ehtab = elf_hash_table (info);
35c08157 4124 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4125 if (htab == NULL)
4126 return FALSE;
35c08157
KLC
4127
4128 eh = (struct elf_nds32_link_hash_entry *) h;
4129
fbaf61ad
NC
4130 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4131 && h->plt.refcount > 0
4132 && !(bfd_link_pie (info) && h->def_regular))
35c08157
KLC
4133 {
4134 /* Make sure this symbol is output as a dynamic symbol.
4135 Undefined weak syms won't yet be marked as dynamic. */
4136 if (h->dynindx == -1 && !h->forced_local)
4137 {
4138 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4139 return FALSE;
4140 }
4141
0e1862bb 4142 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
35c08157 4143 {
fbaf61ad 4144 asection *s = ehtab->splt;
35c08157
KLC
4145
4146 /* If this is the first .plt entry, make room for the special
4147 first entry. */
4148 if (s->size == 0)
4149 s->size += PLT_ENTRY_SIZE;
4150
4151 h->plt.offset = s->size;
4152
4153 /* If this symbol is not defined in a regular file, and we are
4154 not generating a shared library, then set the symbol to this
4155 location in the .plt. This is required to make function
4156 pointers compare as equal between the normal executable and
4157 the shared library. */
0e1862bb 4158 if (!bfd_link_pic (info) && !h->def_regular)
35c08157
KLC
4159 {
4160 h->root.u.def.section = s;
4161 h->root.u.def.value = h->plt.offset;
4162 }
4163
4164 /* Make room for this entry. */
4165 s->size += PLT_ENTRY_SIZE;
4166
4167 /* We also need to make an entry in the .got.plt section, which
4168 will be placed in the .got section by the linker script. */
fbaf61ad 4169 ehtab->sgotplt->size += 4;
35c08157
KLC
4170
4171 /* We also need to make an entry in the .rel.plt section. */
fbaf61ad
NC
4172 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4173 if (htab->tls_desc_trampoline)
4174 htab->next_tls_desc_index++;
35c08157
KLC
4175 }
4176 else
4177 {
4178 h->plt.offset = (bfd_vma) - 1;
4179 h->needs_plt = 0;
4180 }
4181 }
4182 else
4183 {
4184 h->plt.offset = (bfd_vma) - 1;
4185 h->needs_plt = 0;
4186 }
4187
4188 if (h->got.refcount > 0)
4189 {
fbaf61ad 4190 asection *sgot;
35c08157 4191 bfd_boolean dyn;
1c8f6a4d 4192 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
35c08157
KLC
4193
4194 /* Make sure this symbol is output as a dynamic symbol.
4195 Undefined weak syms won't yet be marked as dynamic. */
4196 if (h->dynindx == -1 && !h->forced_local)
4197 {
4198 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4199 return FALSE;
4200 }
4201
fbaf61ad
NC
4202 sgot = elf_hash_table (info)->sgot;
4203 h->got.offset = sgot->size;
1c8f6a4d
KLC
4204
4205 if (tls_type == GOT_UNKNOWN)
4206 abort ();
fbaf61ad
NC
4207
4208 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4209 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4210 sgot->size += 4;
4211 else
4212 {
4213 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4214 if (tls_type & GOT_TLS_DESC)
4215 sgot->size += 8;
4216 }
1c8f6a4d 4217
35c08157 4218 dyn = htab->root.dynamic_sections_created;
fbaf61ad 4219
0e1862bb 4220 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
fbaf61ad
NC
4221 {
4222 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4223 {
4224 /* TLS_DESC with trampoline needs a relocation slot
4225 within .rela.plt. */
4226 htab->num_tls_desc++;
4227 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4228 htab->tls_trampoline = -1;
4229 }
4230 else
4231 {
4232 /* other relocations, including TLS_DESC without trampoline, need
4233 a relocation slot within .rela.got. */
4234 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4235 }
4236 }
35c08157
KLC
4237 }
4238 else
fbaf61ad 4239 h->got.offset = (bfd_vma)-1;
35c08157
KLC
4240
4241 if (eh->dyn_relocs == NULL)
4242 return TRUE;
4243
4244 /* In the shared -Bsymbolic case, discard space allocated for
4245 dynamic pc-relative relocs against symbols which turn out to be
4246 defined in regular objects. For the normal shared case, discard
4247 space for pc-relative relocs that have become local due to symbol
4248 visibility changes. */
4249
0e1862bb 4250 if (bfd_link_pic (info))
35c08157
KLC
4251 {
4252 if (h->def_regular && (h->forced_local || info->symbolic))
4253 {
3bf083ed 4254 struct elf_dyn_relocs **pp;
35c08157
KLC
4255
4256 for (pp = &eh->dyn_relocs; (p = *pp) != NULL;)
4257 {
4258 p->count -= p->pc_count;
4259 p->pc_count = 0;
4260 if (p->count == 0)
4261 *pp = p->next;
4262 else
4263 pp = &p->next;
4264 }
4265 }
4266 }
4267 else
4268 {
4269 /* For the non-shared case, discard space for relocs against
4270 symbols which turn out to need copy relocs or are not dynamic. */
4271
4272 if (!h->non_got_ref
4273 && ((h->def_dynamic
4274 && !h->def_regular)
4275 || (htab->root.dynamic_sections_created
4276 && (h->root.type == bfd_link_hash_undefweak
4277 || h->root.type == bfd_link_hash_undefined))))
4278 {
4279 /* Make sure this symbol is output as a dynamic symbol.
4280 Undefined weak syms won't yet be marked as dynamic. */
4281 if (h->dynindx == -1 && !h->forced_local)
4282 {
4283 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4284 return FALSE;
4285 }
4286
4287 /* If that succeeded, we know we'll be keeping all the
4288 relocs. */
4289 if (h->dynindx != -1)
4290 goto keep;
4291 }
4292
4293 eh->dyn_relocs = NULL;
4294
fbaf61ad 4295keep:;
35c08157
KLC
4296 }
4297
4298 /* Finally, allocate space. */
4299 for (p = eh->dyn_relocs; p != NULL; p = p->next)
4300 {
4301 asection *sreloc = elf_section_data (p->sec)->sreloc;
4302 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4303 }
4304
4305 return TRUE;
4306}
4307
fbaf61ad
NC
4308/* Add relocation REL to the end of relocation section SRELOC. */
4309
4310static void
4311elf32_nds32_add_dynreloc (bfd *output_bfd,
4312 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4313 asection *sreloc, Elf_Internal_Rela *rel)
4314{
4315 bfd_byte *loc;
4316 if (sreloc == NULL)
4317 abort ();
4318
4319 loc = sreloc->contents;
4320 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4321 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4322 abort ();
4323
4324 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4325}
4326
63c1f59d
AM
4327/* Set DF_TEXTREL if we find any dynamic relocs that apply to
4328 read-only sections. */
35c08157
KLC
4329
4330static bfd_boolean
63c1f59d 4331maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
35c08157 4332{
63c1f59d 4333 asection *sec;
35c08157 4334
63c1f59d
AM
4335 if (h->root.type == bfd_link_hash_indirect)
4336 return TRUE;
35c08157 4337
63c1f59d
AM
4338 sec = readonly_dynrelocs (h);
4339 if (sec != NULL)
35c08157 4340 {
63c1f59d 4341 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
35c08157 4342
63c1f59d
AM
4343 info->flags |= DF_TEXTREL;
4344 info->callbacks->minfo
c1c8c1ef 4345 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 4346 sec->owner, h->root.root.string, sec);
35c08157 4347
63c1f59d
AM
4348 /* Not an error, just cut short the traversal. */
4349 return FALSE;
35c08157
KLC
4350 }
4351 return TRUE;
4352}
4353
4354/* Set the sizes of the dynamic sections. */
4355
4356static bfd_boolean
4357nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4358 struct bfd_link_info *info)
4359{
4360 struct elf_nds32_link_hash_table *htab;
4361 bfd *dynobj;
4362 asection *s;
4363 bfd_boolean relocs;
fbaf61ad 4364 bfd_boolean plt;
35c08157
KLC
4365 bfd *ibfd;
4366
4367 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4368 if (htab == NULL)
4369 return FALSE;
4370
4371 dynobj = elf_hash_table (info)->dynobj;
35c08157
KLC
4372 BFD_ASSERT (dynobj != NULL);
4373
fbaf61ad 4374 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
4375 {
4376 /* Set the contents of the .interp section to the interpreter. */
f5233a16 4377 if (bfd_link_executable (info) && !info->nointerp)
35c08157
KLC
4378 {
4379 s = bfd_get_section_by_name (dynobj, ".interp");
4380 BFD_ASSERT (s != NULL);
4381 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4382 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4383 }
4384 }
4385
4386 /* Set up .got offsets for local syms, and space for local dynamic
4387 relocs. */
c72f2fb2 4388 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
35c08157
KLC
4389 {
4390 bfd_signed_vma *local_got;
4391 bfd_signed_vma *end_local_got;
4392 bfd_size_type locsymcount;
4393 Elf_Internal_Shdr *symtab_hdr;
fbaf61ad
NC
4394 asection *sgot;
4395 char *local_tls_type;
4396 unsigned long symndx;
4397 bfd_vma *local_tlsdesc_gotent;
35c08157
KLC
4398
4399 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4400 continue;
4401
4402 for (s = ibfd->sections; s != NULL; s = s->next)
4403 {
3bf083ed 4404 struct elf_dyn_relocs *p;
35c08157 4405
3bf083ed 4406 for (p = ((struct elf_dyn_relocs *)
35c08157
KLC
4407 elf_section_data (s)->local_dynrel);
4408 p != NULL; p = p->next)
4409 {
4410 if (!bfd_is_abs_section (p->sec)
4411 && bfd_is_abs_section (p->sec->output_section))
4412 {
4413 /* Input section has been discarded, either because
4414 it is a copy of a linkonce section or due to
4415 linker script /DISCARD/, so we'll be discarding
4416 the relocs too. */
4417 }
4418 else if (p->count != 0)
4419 {
fbaf61ad
NC
4420 asection *sreloc = elf_section_data (p->sec)->sreloc;
4421 sreloc->size += p->count * sizeof (Elf32_External_Rela);
35c08157
KLC
4422 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4423 info->flags |= DF_TEXTREL;
4424 }
4425 }
4426 }
4427
4428 local_got = elf_local_got_refcounts (ibfd);
4429 if (!local_got)
4430 continue;
4431
4432 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4433 locsymcount = symtab_hdr->sh_info;
4434 end_local_got = local_got + locsymcount;
fbaf61ad
NC
4435 sgot = elf_hash_table (info)->sgot;
4436 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4437 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4438 for (symndx = 0; local_got < end_local_got;
4439 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
35c08157
KLC
4440 {
4441 if (*local_got > 0)
4442 {
fbaf61ad
NC
4443 int num_of_got_entry_needed = 0;
4444 *local_got = sgot->size;
4445 *local_tlsdesc_gotent = sgot->size;
4446
4447 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4448 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4449 num_of_got_entry_needed = 1;
4450 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4451 else if (*local_tls_type & GOT_TLS_DESC)
4452 num_of_got_entry_needed = 2;
4453
4454 sgot->size += (num_of_got_entry_needed << 2);
4455
4456 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4457 others need a slot in .rela.got. */
4458 if (*local_tls_type == GOT_TLS_DESC)
4459 {
4460 if (bfd_link_pic (info))
4461 {
4462 if (htab->tls_desc_trampoline)
4463 {
4464 htab->num_tls_desc++;
4465 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4466 htab->tls_trampoline = -1;
4467 }
4468 else
4469 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4470 }
4471 else
4472 {
4473 /* TLS_DESC -> TLS_LE */
4474 }
4475 }
4476 else
4477 {
4478 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4479 }
35c08157
KLC
4480 }
4481 else
fbaf61ad
NC
4482 {
4483 *local_got = (bfd_vma) -1;
4484 *local_tlsdesc_gotent = (bfd_vma) -1;
4485 }
35c08157
KLC
4486 }
4487 }
4488
4489 /* Allocate global sym .plt and .got entries, and space for global
4490 sym dynamic relocs. */
4491 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4492
fbaf61ad
NC
4493 /* For every jump slot reserved in the sgotplt, reloc_count is
4494 incremented. However, when we reserve space for TLS descriptors,
4495 it's not incremented, so in order to compute the space reserved
4496 for them, it suffices to multiply the reloc count by the jump
4497 slot size. */
4498 if (htab->tls_desc_trampoline && htab->root.srelplt)
4499 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4500
4501 if (htab->tls_trampoline)
4502 {
4503 htab->tls_trampoline = htab->root.splt->size;
4504
4505 /* If we're not using lazy TLS relocations, don't generate the
4506 PLT and GOT entries they require. */
4507 if (!(info->flags & DF_BIND_NOW))
4508 {
4509 htab->dt_tlsdesc_got = htab->root.sgot->size;
4510 htab->root.sgot->size += 4;
4511
4512 htab->dt_tlsdesc_plt = htab->root.splt->size;
4513 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4514 }
4515 }
4516
35c08157
KLC
4517 /* We now have determined the sizes of the various dynamic sections.
4518 Allocate memory for them. */
fbaf61ad
NC
4519 /* The check_relocs and adjust_dynamic_symbol entry points have
4520 determined the sizes of the various dynamic sections. Allocate
4521 memory for them. */
4522 plt = FALSE;
35c08157
KLC
4523 relocs = FALSE;
4524 for (s = dynobj->sections; s != NULL; s = s->next)
4525 {
4526 if ((s->flags & SEC_LINKER_CREATED) == 0)
4527 continue;
4528
ce558b89 4529 if (s == htab->root.splt)
35c08157
KLC
4530 {
4531 /* Strip this section if we don't need it; see the
4532 comment below. */
fbaf61ad 4533 plt = s->size != 0;
35c08157 4534 }
fbaf61ad 4535 else if (s == elf_hash_table (info)->sgot)
35c08157
KLC
4536 {
4537 got_size += s->size;
4538 }
fbaf61ad 4539 else if (s == elf_hash_table (info)->sgotplt)
35c08157
KLC
4540 {
4541 got_size += s->size;
4542 }
4543 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
4544 {
fbaf61ad 4545 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
35c08157
KLC
4546 relocs = TRUE;
4547
4548 /* We use the reloc_count field as a counter if we need
4549 to copy relocs into the output file. */
4550 s->reloc_count = 0;
4551 }
4552 else
4553 {
4554 /* It's not one of our sections, so don't allocate space. */
4555 continue;
4556 }
4557
4558 if (s->size == 0)
4559 {
4560 /* If we don't need this section, strip it from the
4561 output file. This is mostly to handle .rela.bss and
4562 .rela.plt. We must create both sections in
4563 create_dynamic_sections, because they must be created
4564 before the linker maps input sections to output
4565 sections. The linker does that before
4566 adjust_dynamic_symbol is called, and it is that
4567 function which decides whether anything needs to go
4568 into these sections. */
4569 s->flags |= SEC_EXCLUDE;
4570 continue;
4571 }
4572
4573 /* Allocate memory for the section contents. We use bfd_zalloc
4574 here in case unused entries are not reclaimed before the
4575 section's contents are written out. This should not happen,
4576 but this way if it does, we get a R_NDS32_NONE reloc instead
4577 of garbage. */
4578 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4579 if (s->contents == NULL)
4580 return FALSE;
4581 }
4582
4583
4584 if (htab->root.dynamic_sections_created)
4585 {
4586 /* Add some entries to the .dynamic section. We fill in the
4587 values later, in nds32_elf_finish_dynamic_sections, but we
4588 must add the entries now so that we get the correct size for
4589 the .dynamic section. The DT_DEBUG entry is filled in by the
4590 dynamic linker and used by the debugger. */
4591#define add_dynamic_entry(TAG, VAL) \
4592 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4593
fbaf61ad 4594 if (bfd_link_executable (info))
35c08157
KLC
4595 {
4596 if (!add_dynamic_entry (DT_DEBUG, 0))
4597 return FALSE;
4598 }
4599
fbaf61ad 4600 if (elf_hash_table (info)->splt->size != 0)
35c08157
KLC
4601 {
4602 if (!add_dynamic_entry (DT_PLTGOT, 0)
4603 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4604 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4605 || !add_dynamic_entry (DT_JMPREL, 0))
4606 return FALSE;
4607 }
4608
fbaf61ad
NC
4609 if (htab->tls_desc_trampoline && plt)
4610 {
4611 if (htab->dt_tlsdesc_plt
4612 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4613 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4614 return FALSE;
4615 }
4616
35c08157
KLC
4617 if (relocs)
4618 {
4619 if (!add_dynamic_entry (DT_RELA, 0)
4620 || !add_dynamic_entry (DT_RELASZ, 0)
4621 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4622 return FALSE;
4623
4624 /* If any dynamic relocs apply to a read-only section,
4625 then we need a DT_TEXTREL entry. */
4626 if ((info->flags & DF_TEXTREL) == 0)
63c1f59d 4627 elf_link_hash_traverse (&htab->root, maybe_set_textrel,
35c08157
KLC
4628 (void *) info);
4629
4630 if ((info->flags & DF_TEXTREL) != 0)
4631 {
4632 if (!add_dynamic_entry (DT_TEXTREL, 0))
4633 return FALSE;
4634 }
4635 }
4636 }
4637#undef add_dynamic_entry
4638
4639 return TRUE;
4640}
4641
4642static bfd_reloc_status_type
4643nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4644 bfd_vma relocation, bfd_byte *location)
4645{
4646 int size;
4647 bfd_vma x = 0;
4648 bfd_reloc_status_type flag;
4649 unsigned int rightshift = howto->rightshift;
4650 unsigned int bitpos = howto->bitpos;
4651
4652 /* If the size is negative, negate RELOCATION. This isn't very
4653 general. */
4654 if (howto->size < 0)
4655 relocation = -relocation;
4656
4657 /* Get the value we are going to relocate. */
4658 size = bfd_get_reloc_size (howto);
4659 switch (size)
4660 {
4661 default:
35c08157
KLC
4662 abort ();
4663 break;
6346d5ca
AM
4664 case 0:
4665 return bfd_reloc_ok;
35c08157
KLC
4666 case 2:
4667 x = bfd_getb16 (location);
4668 break;
4669 case 4:
4670 x = bfd_getb32 (location);
4671 break;
4672 }
4673
4674 /* Check for overflow. FIXME: We may drop bits during the addition
4675 which we don't check for. We must either check at every single
4676 operation, which would be tedious, or we must do the computations
4677 in a type larger than bfd_vma, which would be inefficient. */
4678 flag = bfd_reloc_ok;
4679 if (howto->complain_on_overflow != complain_overflow_dont)
4680 {
4681 bfd_vma addrmask, fieldmask, signmask, ss;
4682 bfd_vma a, b, sum;
4683
4684 /* Get the values to be added together. For signed and unsigned
4685 relocations, we assume that all values should be truncated to
4686 the size of an address. For bitfields, all the bits matter.
4687 See also bfd_check_overflow. */
4688 fieldmask = N_ONES (howto->bitsize);
4689 signmask = ~fieldmask;
4690 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4691 a = (relocation & addrmask) >> rightshift;
4692 b = (x & howto->src_mask & addrmask) >> bitpos;
4693
4694 switch (howto->complain_on_overflow)
4695 {
4696 case complain_overflow_signed:
4697 /* If any sign bits are set, all sign bits must be set.
4698 That is, A must be a valid negative address after
4699 shifting. */
4700 signmask = ~(fieldmask >> 1);
4701 /* Fall through. */
4702
4703 case complain_overflow_bitfield:
4704 /* Much like the signed check, but for a field one bit
4705 wider. We allow a bitfield to represent numbers in the
4706 range -2**n to 2**n-1, where n is the number of bits in the
4707 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4708 can't overflow, which is exactly what we want. */
4709 ss = a & signmask;
4710 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4711 flag = bfd_reloc_overflow;
4712
4713 /* We only need this next bit of code if the sign bit of B
4714 is below the sign bit of A. This would only happen if
4715 SRC_MASK had fewer bits than BITSIZE. Note that if
4716 SRC_MASK has more bits than BITSIZE, we can get into
4717 trouble; we would need to verify that B is in range, as
4718 we do for A above. */
4719 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4720 ss >>= bitpos;
4721
4722 /* Set all the bits above the sign bit. */
4723 b = (b ^ ss) - ss;
4724
4725 /* Now we can do the addition. */
4726 sum = a + b;
4727
4728 /* See if the result has the correct sign. Bits above the
4729 sign bit are junk now; ignore them. If the sum is
4730 positive, make sure we did not have all negative inputs;
4731 if the sum is negative, make sure we did not have all
4732 positive inputs. The test below looks only at the sign
4733 bits, and it really just
4734 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4735
4736 We mask with addrmask here to explicitly allow an address
4737 wrap-around. The Linux kernel relies on it, and it is
4738 the only way to write assembler code which can run when
4739 loaded at a location 0x80000000 away from the location at
4740 which it is linked. */
4741 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4742 flag = bfd_reloc_overflow;
4743
4744 break;
4745
4746 case complain_overflow_unsigned:
4747 /* Checking for an unsigned overflow is relatively easy:
4748 trim the addresses and add, and trim the result as well.
4749 Overflow is normally indicated when the result does not
4750 fit in the field. However, we also need to consider the
4751 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4752 input is 0x80000000, and bfd_vma is only 32 bits; then we
4753 will get sum == 0, but there is an overflow, since the
4754 inputs did not fit in the field. Instead of doing a
4755 separate test, we can check for this by or-ing in the
4756 operands when testing for the sum overflowing its final
4757 field. */
4758 sum = (a + b) & addrmask;
4759 if ((a | b | sum) & signmask)
4760 flag = bfd_reloc_overflow;
4761 break;
4762
4763 default:
4764 abort ();
4765 }
4766 }
4767
4768 /* Put RELOCATION in the right bits. */
4769 relocation >>= (bfd_vma) rightshift;
4770 relocation <<= (bfd_vma) bitpos;
4771
4772 /* Add RELOCATION to the right bits of X. */
4773 /* FIXME : 090616
4774 Because the relaxation may generate duplicate relocation at one address,
4775 an addition to immediate in the instruction may cause the relocation added
4776 several times.
4777 This bug should be fixed in assembler, but a check is also needed here. */
4778 if (howto->partial_inplace)
4779 x = ((x & ~howto->dst_mask)
4780 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4781 else
4782 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4783
4784
4785 /* Put the relocated value back in the object file. */
4786 switch (size)
4787 {
4788 default:
4789 case 0:
4790 case 1:
4791 case 8:
4792 abort ();
4793 break;
4794 case 2:
4795 bfd_putb16 (x, location);
4796 break;
4797 case 4:
4798 bfd_putb32 (x, location);
4799 break;
4800 }
4801
4802 return flag;
4803}
4804
4805static bfd_reloc_status_type
4806nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4807 asection *input_section, bfd_byte *contents,
4808 bfd_vma address, bfd_vma value, bfd_vma addend)
4809{
4810 bfd_vma relocation;
4811
4812 /* Sanity check the address. */
4813 if (address > bfd_get_section_limit (input_bfd, input_section))
4814 return bfd_reloc_outofrange;
4815
4816 /* This function assumes that we are dealing with a basic relocation
4817 against a symbol. We want to compute the value of the symbol to
4818 relocate to. This is just VALUE, the value of the symbol, plus
4819 ADDEND, any addend associated with the reloc. */
4820 relocation = value + addend;
4821
4822 /* If the relocation is PC relative, we want to set RELOCATION to
4823 the distance between the symbol (currently in RELOCATION) and the
c2bf1eec 4824 location we are relocating. If pcrel_offset is FALSE we do not
35c08157
KLC
4825 need to subtract out the offset of the location within the
4826 section (which is just ADDRESS). */
4827 if (howto->pc_relative)
4828 {
4829 relocation -= (input_section->output_section->vma
4830 + input_section->output_offset);
4831 if (howto->pcrel_offset)
4832 relocation -= address;
4833 }
4834
4835 return nds32_relocate_contents (howto, input_bfd, relocation,
4836 contents + address);
4837}
4838
4839static bfd_boolean
4840nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4841 const char *name,
4842 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4843 asection *input_sec,
4844 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4845{
4846 const char *source;
4847 FILE *sym_ld_script = NULL;
4848 struct elf_nds32_link_hash_table *table;
4849
4850 table = nds32_elf_hash_table (info);
4851 sym_ld_script = table->sym_ld_script;
4852 if (!sym_ld_script)
4853 return TRUE;
4854
4855 if (!h || !name || *name == '\0')
4856 return TRUE;
4857
4858 if (input_sec->flags & SEC_EXCLUDE)
4859 return TRUE;
4860
4861 if (!check_start_export_sym)
4862 {
4863 fprintf (sym_ld_script, "SECTIONS\n{\n");
4864 check_start_export_sym = 1;
4865 }
4866
4867 if (h->root.type == bfd_link_hash_defined
4868 || h->root.type == bfd_link_hash_defweak)
4869 {
4870 if (!h->root.u.def.section->output_section)
4871 return TRUE;
4872
4873 if (bfd_is_const_section (input_sec))
4874 source = input_sec->name;
4875 else
4876 source = input_sec->owner->filename;
4877
4878 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4879 h->root.root.string,
4880 (long) (h->root.u.def.value
4881 + h->root.u.def.section->output_section->vma
4882 + h->root.u.def.section->output_offset), source);
4883 }
4884
4885 return TRUE;
4886}
4887
4888/* Relocate an NDS32/D ELF section.
4889 There is some attempt to make this function usable for many architectures,
4890 both for RELA and REL type relocs, if only to serve as a learning tool.
4891
4892 The RELOCATE_SECTION function is called by the new ELF backend linker
4893 to handle the relocations for a section.
4894
4895 The relocs are always passed as Rela structures; if the section
4896 actually uses Rel structures, the r_addend field will always be
4897 zero.
4898
4899 This function is responsible for adjust the section contents as
4900 necessary, and (if using Rela relocs and generating a
4901 relocatable output file) adjusting the reloc addend as
4902 necessary.
4903
4904 This function does not have to worry about setting the reloc
4905 address or the reloc symbol index.
4906
4907 LOCAL_SYMS is a pointer to the swapped in local symbols.
4908
4909 LOCAL_SECTIONS is an array giving the section in the input file
4910 corresponding to the st_shndx field of each local symbol.
4911
4912 The global hash table entry for the global symbols can be found
4913 via elf_sym_hashes (input_bfd).
4914
4915 When generating relocatable output, this function must handle
4916 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4917 going to be the section symbol corresponding to the output
4918 section, which means that the addend must be adjusted
4919 accordingly. */
4920
fbaf61ad
NC
4921/* Return the base VMA address which should be subtracted from real addresses
4922 when resolving @dtpoff relocation.
4923 This is PT_TLS segment p_vaddr. */
4924
4925/* Return the relocation value for @tpoff relocation
4926 if STT_TLS virtual address is ADDRESS. */
4927
4928/* Return the relocation value for @gottpoff relocation
4929 if STT_TLS virtual address is ADDRESS. */
4930
1c8f6a4d 4931static bfd_vma
fbaf61ad 4932gottpoff (struct bfd_link_info *info, bfd_vma address)
1c8f6a4d 4933{
fbaf61ad
NC
4934 bfd_vma tp_base;
4935 bfd_vma tp_offset;
4936
1c8f6a4d
KLC
4937 /* If tls_sec is NULL, we should have signalled an error already. */
4938 if (elf_hash_table (info)->tls_sec == NULL)
4939 return 0;
fbaf61ad
NC
4940
4941 tp_base = elf_hash_table (info)->tls_sec->vma;
4942 tp_offset = address - tp_base;
4943
4944 return tp_offset;
1c8f6a4d
KLC
4945}
4946
35c08157 4947static bfd_boolean
fbaf61ad
NC
4948patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4949{
4950 /* TLS_GD/TLS_LD model #1
4951 46 00 00 00 sethi $r0,#0x0
4952 58 00 00 00 ori $r0,$r0,#0x0
4953 40 00 74 00 add $r0,$r0,$gp
4954 04 10 00 00 lwi $r1,[$r0+#0x0]
4955 4b e0 04 01 jral $lp,$r1 */
4956
4957 /* TLS_GD/TLS_LD model #2
4958 46 00 00 00 sethi $r0,#0x0
4959 58 00 00 00 ori $r0,$r0,#0x0
4960 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4961 40 00 74 00 add $r0,$r0,$gp
4962 4b e0 04 01 jral $lp,$r1 */
4963
4964 /* TLS_IE model (non-PIC)
4965 46 00 00 00 sethi $r0,#0x0
4966 04 00 00 00 lwi $r0,[$r0+#0x0]
4967 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4968
4969 /* TLS_IE model (PIC)
4970 46 00 00 00 sethi $r0,#0x0
4971 58 00 00 00 ori $r0,$r0,#0x0
4972 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4973 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4974
4975 /* TLS_GD_TO_IE model
4976 46 00 00 00 sethi $r0,#0x0
4977 58 00 00 00 ori $r0,$r0,#0x0
4978 40 00 74 00 add $r0,$rM,$gp
4979 04 00 00 01 lwi $r0,[$r0+#0x4]
4980 40 00 64 00 add $r0,$r0,$r25 */
4981
4982 bfd_boolean rz = FALSE;
4983
4984 typedef struct
4985 {
4986 uint32_t opcode;
4987 uint32_t mask;
4988 } pat_t;
4989
4990 uint32_t patch[3] =
4991 {
4992 0x40007400, /* add $r0,$rM,$gp */
4993 0x04000001, /* lwi $r0,[$r0+#0x4] */
4994 0x40006400, /* add $r0,$r0,$r25 */
4995 };
4996
4997 pat_t mode0[3] =
4998 {
4999 { 0x40000000, 0xfe0003ff },
5000 { 0x04000000, 0xfe000000 },
5001 { 0x4be00001, 0xffff83ff },
5002 };
5003
5004 pat_t mode1[3] =
5005 {
5006 { 0x38007402, 0xfe007fff },
5007 { 0x40007400, 0xfe007fff },
5008 { 0x4be00001, 0xffff83ff },
5009 };
5010
5011 unsigned char *p = contents + rel->r_offset;
5012
5013 uint32_t insn;
5014 uint32_t regidx = 0;
5015 insn = bfd_getb32 (p);
5016 if (INSN_SETHI == (0xfe0fffffu & insn))
5017 {
5018 regidx = 0x1f & (insn >> 20);
5019 p += 4;
5020 }
5021
5022 insn = bfd_getb32 (p);
5023 if (INSN_ORI == (0xfe007fffu & insn))
5024 {
5025 regidx = 0x1f & (insn >> 20);
5026 p += 4;
5027 }
5028
5029 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
5030 {
5031 /* already patched? */
5032 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
5033 (patch[1] == bfd_getb32 (p + 4)))
5034 rz = TRUE;
5035 }
5036 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
5037 {
5038 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
5039 (mode0[2].opcode == (mode0[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 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
5048 {
5049 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
5050 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5051 {
5052 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5053 bfd_putb32 (patch[1], p + 4);
5054 bfd_putb32 (patch[2], p + 8);
5055 rz = TRUE;
5056 }
5057 }
5058
5059 if (!rz)
5060 {
5061 printf ("%s: %s @ 0x%08x\n", __func__, ibfd->filename,
5062 (int) rel->r_offset);
5063 BFD_ASSERT(0); /* Unsupported pattern. */
5064 }
5065
5066 return rz;
5067}
5068
5069static enum elf_nds32_tls_type
5070get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5071
5072static unsigned int
5073ones32 (register unsigned int x)
5074{
5075 /* 32-bit recursive reduction using SWAR...
5076 but first step is mapping 2-bit values
5077 into sum of 2 1-bit values in sneaky way. */
5078 x -= ((x >> 1) & 0x55555555);
5079 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5080 x = (((x >> 4) + x) & 0x0f0f0f0f);
5081 x += (x >> 8);
5082 x += (x >> 16);
5083 return (x & 0x0000003f);
5084}
5085
5086static unsigned int
5087fls (register unsigned int x)
5088{
5089 return ffs (x & (-x));
5090}
5091
5092#define nds32_elf_local_tlsdesc_gotent(bfd) \
5093 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5094
5095static bfd_boolean
5096nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
5097 struct bfd_link_info * info,
5098 bfd * input_bfd,
5099 asection * input_section,
5100 bfd_byte * contents,
5101 Elf_Internal_Rela * relocs,
5102 Elf_Internal_Sym * local_syms,
5103 asection ** local_sections)
5104{
5105 Elf_Internal_Shdr *symtab_hdr;
5106 struct elf_link_hash_entry **sym_hashes;
5107 Elf_Internal_Rela *rel, *relend;
5108 bfd_boolean ret = TRUE; /* Assume success. */
5109 int align = 0;
5110 bfd_reloc_status_type r;
5111 const char *errmsg = NULL;
5112 bfd_vma gp;
5113 struct elf_link_hash_table *ehtab;
5114 struct elf_nds32_link_hash_table *htab;
5115 bfd *dynobj;
5116 bfd_vma *local_got_offsets;
5117 asection *sgot, *splt, *sreloc;
5118 bfd_vma high_address;
5119 struct elf_nds32_link_hash_table *table;
5120 int eliminate_gc_relocs;
35c08157
KLC
5121 bfd_vma fpbase_addr;
5122
5123 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5124 sym_hashes = elf_sym_hashes (input_bfd);
fbaf61ad 5125 ehtab = elf_hash_table (info);
35c08157
KLC
5126 htab = nds32_elf_hash_table (info);
5127 high_address = bfd_get_section_limit (input_bfd, input_section);
5128
5129 dynobj = htab->root.dynobj;
5130 local_got_offsets = elf_local_got_offsets (input_bfd);
5131
fbaf61ad
NC
5132 sgot = ehtab->sgot;
5133 splt = ehtab->splt;
35c08157
KLC
5134 sreloc = NULL;
5135
5136 rel = relocs;
5137 relend = relocs + input_section->reloc_count;
5138
5139 table = nds32_elf_hash_table (info);
5140 eliminate_gc_relocs = table->eliminate_gc_relocs;
fbaf61ad 5141
35c08157 5142 /* By this time, we can adjust the value of _SDA_BASE_. */
fbaf61ad 5143 /* Explain _SDA_BASE_ */
0e1862bb 5144 if ((!bfd_link_relocatable (info)))
35c08157
KLC
5145 {
5146 is_SDA_BASE_set = 1;
5147 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5148 if (r != bfd_reloc_ok)
5149 return FALSE;
5150 }
5151
fbaf61ad
NC
5152 /* Do TLS model conversion once at first. */
5153 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5154
35c08157
KLC
5155 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5156 the fp value is set as gp, and it has be reverted for instruction
5157 setting fp. */
5158 fpbase_addr = elf_gp (output_bfd);
5159
fbaf61ad 5160 /* Deal with (dynamic) relocations. */
35c08157
KLC
5161 for (rel = relocs; rel < relend; rel++)
5162 {
5163 enum elf_nds32_reloc_type r_type;
5164 reloc_howto_type *howto = NULL;
5165 unsigned long r_symndx;
5166 struct elf_link_hash_entry *h = NULL;
5167 Elf_Internal_Sym *sym = NULL;
5168 asection *sec;
5169 bfd_vma relocation;
fbaf61ad
NC
5170 bfd_vma relocation_sym = 0xdeadbeef;
5171 Elf_Internal_Rela *lorel;
5172 bfd_vma off;
35c08157
KLC
5173
5174 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5175 ensure it's zero (we use REL relocs, not RELA). Therefore this
5176 should be assigning zero to `addend', but for clarity we use
5177 `r_addend'. */
5178
5179 bfd_vma addend = rel->r_addend;
5180 bfd_vma offset = rel->r_offset;
5181
5182 r_type = ELF32_R_TYPE (rel->r_info);
5183 if (r_type >= R_NDS32_max)
5184 {
695344c0 5185 /* xgettext:c-format */
0aa13fee 5186 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4eca0228 5187 input_bfd, r_type);
35c08157
KLC
5188 bfd_set_error (bfd_error_bad_value);
5189 ret = FALSE;
5190 continue;
5191 }
5192
5193 if (r_type == R_NDS32_GNU_VTENTRY
5194 || r_type == R_NDS32_GNU_VTINHERIT
5195 || r_type == R_NDS32_NONE
5196 || r_type == R_NDS32_RELA_GNU_VTENTRY
5197 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5198 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5199 || r_type == R_NDS32_DATA
fbaf61ad 5200 || r_type == R_NDS32_TRAN)
35c08157
KLC
5201 continue;
5202
0c4bd9d9
KLC
5203 /* If we enter the fp-as-gp region. Resolve the address
5204 of best fp-base. */
35c08157
KLC
5205 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5206 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5207 {
5208 int dist;
5209
5210 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5211 dist = rel->r_addend >> 16;
5212 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5213 local_syms, symtab_hdr);
5214 }
5215 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5216 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5217 {
5218 fpbase_addr = elf_gp (output_bfd);
5219 }
5220
fbaf61ad
NC
5221 /* Skip the relocations used for relaxation. */
5222 /* We have to update LONGCALL and LONGJUMP
5223 relocations when generating the relocatable files. */
5224 if (!bfd_link_relocatable (info)
5225 && (r_type >= R_NDS32_RELAX_ENTRY
5226 || (r_type >= R_NDS32_LONGCALL4
5227 && r_type <= R_NDS32_LONGJUMP7)))
35c08157
KLC
5228 continue;
5229
5230 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5231 r_symndx = ELF32_R_SYM (rel->r_info);
5232
5233 /* This is a final link. */
5234 sym = NULL;
5235 sec = NULL;
5236 h = NULL;
5237
5238 if (r_symndx < symtab_hdr->sh_info)
5239 {
5240 /* Local symbol. */
5241 sym = local_syms + r_symndx;
5242 sec = local_sections[r_symndx];
5243
5244 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5245 addend = rel->r_addend;
fbaf61ad
NC
5246
5247 /* keep symbol location for static TLS_IE GOT entry */
5248 relocation_sym = relocation;
5249 if (bfd_link_relocatable (info))
5250 {
5251 /* This is a relocatable link. We don't have to change
5252 anything, unless the reloc is against a section symbol,
5253 in which case we have to adjust according to where the
5254 section symbol winds up in the output section. */
5255 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5256 rel->r_addend += sec->output_offset + sym->st_value;
5257
5258 continue;
5259 }
35c08157
KLC
5260 }
5261 else
5262 {
5263 /* External symbol. */
fbaf61ad
NC
5264 if (bfd_link_relocatable (info))
5265 continue;
35c08157
KLC
5266 bfd_boolean warned, ignored, unresolved_reloc;
5267 int symndx = r_symndx - symtab_hdr->sh_info;
5268
5269 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5270 r_symndx, symtab_hdr, sym_hashes, h, sec,
5271 relocation, unresolved_reloc, warned,
5272 ignored);
5273
fbaf61ad
NC
5274 /* keep symbol location for static TLS_IE GOT entry */
5275 relocation_sym = relocation;
5276
35c08157
KLC
5277 /* la $fp, _FP_BASE_ is per-function (region).
5278 Handle it specially. */
5279 switch ((int) r_type)
5280 {
fbaf61ad
NC
5281 case R_NDS32_HI20_RELA:
5282 case R_NDS32_LO12S0_RELA:
5283 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5284 FP_BASE_NAME) == 0)
5285 {
5286 if (!bfd_link_pie (info))
5287 {
5288 _bfd_error_handler
5289 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5290 input_bfd);
5291 }
5292 relocation = fpbase_addr;
5293 }
5294 break;
35c08157
KLC
5295 case R_NDS32_SDA19S0_RELA:
5296 case R_NDS32_SDA15S0_RELA:
5297 case R_NDS32_20_RELA:
5298 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5299 FP_BASE_NAME) == 0)
5300 {
5301 relocation = fpbase_addr;
5302 break;
5303 }
5304 }
35c08157
KLC
5305 }
5306
5307 /* Sanity check the address. */
5308 if (offset > high_address)
5309 {
5310 r = bfd_reloc_outofrange;
5311 goto check_reloc;
5312 }
5313
fbaf61ad 5314 if (r_type >= R_NDS32_RELAX_ENTRY)
35c08157
KLC
5315 continue;
5316
5317 switch ((int) r_type)
5318 {
5319 case R_NDS32_GOTOFF:
5320 /* Relocation is relative to the start of the global offset
5321 table (for ld24 rx, #uimm24), e.g. access at label+addend
5322
5323 ld24 rx. #label@GOTOFF + addend
5324 sub rx, r12. */
5325 case R_NDS32_GOTOFF_HI20:
5326 case R_NDS32_GOTOFF_LO12:
5327 case R_NDS32_GOTOFF_LO15:
5328 case R_NDS32_GOTOFF_LO19:
5329 BFD_ASSERT (sgot != NULL);
5330
5331 relocation -= elf_gp (output_bfd);
5332 break;
5333
5334 case R_NDS32_9_PLTREL:
5335 case R_NDS32_25_PLTREL:
5336 /* Relocation is to the entry for this symbol in the
5337 procedure linkage table. */
5338
5339 /* The native assembler will generate a 25_PLTREL reloc
5340 for a local symbol if you assemble a call from one
5341 section to another when using -K pic. */
5342 if (h == NULL)
5343 break;
5344
5345 if (h->forced_local)
5346 break;
5347
5348 /* We didn't make a PLT entry for this symbol. This
5349 happens when statically linking PIC code, or when
5350 using -Bsymbolic. */
5351 if (h->plt.offset == (bfd_vma) - 1)
5352 break;
5353
5354 relocation = (splt->output_section->vma
5355 + splt->output_offset + h->plt.offset);
5356 break;
5357
5358 case R_NDS32_PLT_GOTREL_HI20:
5359 case R_NDS32_PLT_GOTREL_LO12:
5360 case R_NDS32_PLT_GOTREL_LO15:
5361 case R_NDS32_PLT_GOTREL_LO19:
5362 case R_NDS32_PLT_GOTREL_LO20:
fbaf61ad
NC
5363 if (h == NULL
5364 || h->forced_local
5365 || h->plt.offset == (bfd_vma) -1
5366 || (bfd_link_pie (info) && h->def_regular))
35c08157 5367 {
fbaf61ad
NC
5368 /* Maybe we should find better checking to optimize
5369 PIE PLT relocations. */
35c08157
KLC
5370 /* We didn't make a PLT entry for this symbol. This
5371 happens when statically linking PIC code, or when
5372 using -Bsymbolic. */
fbaf61ad
NC
5373 if (h)
5374 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
35c08157
KLC
5375 relocation -= elf_gp (output_bfd);
5376 break;
5377 }
5378
5379 relocation = (splt->output_section->vma
5380 + splt->output_offset + h->plt.offset);
5381
5382 relocation -= elf_gp (output_bfd);
5383 break;
5384
5385 case R_NDS32_PLTREL_HI20:
5386 case R_NDS32_PLTREL_LO12:
5387
5388 /* Relocation is to the entry for this symbol in the
5389 procedure linkage table. */
5390
5391 /* The native assembler will generate a 25_PLTREL reloc
5392 for a local symbol if you assemble a call from one
5393 section to another when using -K pic. */
5394 if (h == NULL)
5395 break;
5396
5397 if (h->forced_local)
5398 break;
5399
5400 if (h->plt.offset == (bfd_vma) - 1)
5401 /* We didn't make a PLT entry for this symbol. This
5402 happens when statically linking PIC code, or when
5403 using -Bsymbolic. */
5404 break;
5405
5406 if (splt == NULL)
5407 break;
5408
5409 relocation = (splt->output_section->vma
5410 + splt->output_offset
5411 + h->plt.offset + 4)
5412 - (input_section->output_section->vma
5413 + input_section->output_offset
5414 + rel->r_offset);
5415
5416 break;
5417
5418 case R_NDS32_GOTPC20:
5419 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5420 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5421 relocation = elf_gp (output_bfd);
5422 break;
5423
5424 case R_NDS32_GOTPC_HI20:
5425 case R_NDS32_GOTPC_LO12:
fbaf61ad
NC
5426 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5427 bl .+4
5428 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5429 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5430 or
5431 bl .+4
5432 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5433 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5434 relocation = elf_gp (output_bfd);
5435 relocation -= (input_section->output_section->vma
5436 + input_section->output_offset + rel->r_offset);
5437 break;
35c08157
KLC
5438
5439 case R_NDS32_GOT20:
5440 /* Fall through. */
5441 case R_NDS32_GOT_HI20:
5442 case R_NDS32_GOT_LO12:
5443 case R_NDS32_GOT_LO15:
5444 case R_NDS32_GOT_LO19:
5445 /* Relocation is to the entry for this symbol in the global
5446 offset table. */
5447 BFD_ASSERT (sgot != NULL);
5448
5449 if (h != NULL)
5450 {
fbaf61ad 5451 /* External symbol */
35c08157 5452 bfd_boolean dyn;
35c08157
KLC
5453
5454 off = h->got.offset;
5455 BFD_ASSERT (off != (bfd_vma) - 1);
5456 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
5457 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5458 bfd_link_pic (info),
5459 h)
5460 || (bfd_link_pic (info)
35c08157
KLC
5461 && (info->symbolic
5462 || h->dynindx == -1
5463 || h->forced_local) && h->def_regular))
5464 {
5465 /* This is actually a static link, or it is a
5466 -Bsymbolic link and the symbol is defined
5467 locally, or the symbol was forced to be local
5468 because of a version file. We must initialize
5469 this entry in the global offset table. Since the
5470 offset must always be a multiple of 4, we use the
5471 least significant bit to record whether we have
5472 initialized it already.
5473
5474 When doing a dynamic link, we create a .rela.got
5475 relocation entry to initialize the value. This
5476 is done in the finish_dynamic_symbol routine. */
fbaf61ad 5477 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5478 off &= ~1;
5479 else
5480 {
5481 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5482 h->got.offset |= 1;
5483 }
5484 }
5485 relocation = sgot->output_section->vma + sgot->output_offset + off
5486 - elf_gp (output_bfd);
5487 }
5488 else
5489 {
fbaf61ad 5490 /* Local symbol */
35c08157
KLC
5491 bfd_byte *loc;
5492
5493 BFD_ASSERT (local_got_offsets != NULL
5494 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5495
5496 off = local_got_offsets[r_symndx];
5497
5498 /* The offset must always be a multiple of 4. We use
5499 the least significant bit to record whether we have
5500 already processed this entry. */
fbaf61ad 5501 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5502 off &= ~1;
5503 else
5504 {
5505 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5506
0e1862bb 5507 if (bfd_link_pic (info))
35c08157
KLC
5508 {
5509 asection *srelgot;
5510 Elf_Internal_Rela outrel;
5511
5512 /* We need to generate a R_NDS32_RELATIVE reloc
5513 for the dynamic linker. */
fbaf61ad 5514 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
35c08157
KLC
5515 BFD_ASSERT (srelgot != NULL);
5516
5517 outrel.r_offset = (elf_gp (output_bfd)
5518 + sgot->output_offset + off);
5519 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5520 outrel.r_addend = relocation;
5521 loc = srelgot->contents;
5522 loc +=
5523 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5524 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5525 ++srelgot->reloc_count;
5526 }
5527 local_got_offsets[r_symndx] |= 1;
5528 }
5529 relocation = sgot->output_section->vma + sgot->output_offset + off
5530 - elf_gp (output_bfd);
5531 }
5532
5533 break;
5534
5535 case R_NDS32_16_RELA:
5536 case R_NDS32_20_RELA:
5537 case R_NDS32_5_RELA:
5538 case R_NDS32_32_RELA:
5539 case R_NDS32_9_PCREL_RELA:
5540 case R_NDS32_WORD_9_PCREL_RELA:
5541 case R_NDS32_10_UPCREL_RELA:
5542 case R_NDS32_15_PCREL_RELA:
5543 case R_NDS32_17_PCREL_RELA:
5544 case R_NDS32_25_PCREL_RELA:
5545 case R_NDS32_HI20_RELA:
5546 case R_NDS32_LO12S3_RELA:
5547 case R_NDS32_LO12S2_RELA:
5548 case R_NDS32_LO12S2_DP_RELA:
5549 case R_NDS32_LO12S2_SP_RELA:
5550 case R_NDS32_LO12S1_RELA:
5551 case R_NDS32_LO12S0_RELA:
5552 case R_NDS32_LO12S0_ORI_RELA:
0e1862bb 5553 if (bfd_link_pic (info) && r_symndx != 0
35c08157
KLC
5554 && (input_section->flags & SEC_ALLOC) != 0
5555 && (eliminate_gc_relocs == 0
5556 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5557 && ((r_type != R_NDS32_9_PCREL_RELA
5558 && r_type != R_NDS32_WORD_9_PCREL_RELA
5559 && r_type != R_NDS32_10_UPCREL_RELA
5560 && r_type != R_NDS32_15_PCREL_RELA
5561 && r_type != R_NDS32_17_PCREL_RELA
5562 && r_type != R_NDS32_25_PCREL_RELA
5563 && !(r_type == R_NDS32_32_RELA
5564 && strcmp (input_section->name, ".eh_frame") == 0))
5565 || (h != NULL && h->dynindx != -1
5566 && (!info->symbolic || !h->def_regular))))
5567 {
5568 Elf_Internal_Rela outrel;
5569 bfd_boolean skip, relocate;
5570 bfd_byte *loc;
5571
5572 /* When generating a shared object, these relocations
5573 are copied into the output file to be resolved at run
5574 time. */
5575
5576 if (sreloc == NULL)
5577 {
5578 const char *name;
5579
5580 name = bfd_elf_string_from_elf_section
5581 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5582 elf_section_data (input_section)->rela.hdr->sh_name);
5583 if (name == NULL)
5584 return FALSE;
5585
5586 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
5587 && strcmp (bfd_get_section_name (input_bfd,
5588 input_section),
5589 name + 5) == 0);
5590
5591 sreloc = bfd_get_section_by_name (dynobj, name);
5592 BFD_ASSERT (sreloc != NULL);
5593 }
5594
5595 skip = FALSE;
5596 relocate = FALSE;
5597
5598 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5599 info,
5600 input_section,
5601 rel->r_offset);
5602 if (outrel.r_offset == (bfd_vma) - 1)
5603 skip = TRUE;
5604 else if (outrel.r_offset == (bfd_vma) - 2)
5605 skip = TRUE, relocate = TRUE;
5606 outrel.r_offset += (input_section->output_section->vma
5607 + input_section->output_offset);
5608
5609 if (skip)
5610 memset (&outrel, 0, sizeof outrel);
5611 else if (r_type == R_NDS32_17_PCREL_RELA
5612 || r_type == R_NDS32_15_PCREL_RELA
5613 || r_type == R_NDS32_25_PCREL_RELA)
5614 {
5615 BFD_ASSERT (h != NULL && h->dynindx != -1);
5616 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5617 outrel.r_addend = rel->r_addend;
5618 }
5619 else
5620 {
5621 /* h->dynindx may be -1 if this symbol was marked to
5622 become local. */
5623 if (h == NULL
5624 || ((info->symbolic || h->dynindx == -1)
fbaf61ad
NC
5625 && h->def_regular)
5626 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
5627 {
5628 relocate = TRUE;
5629 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5630 outrel.r_addend = relocation + rel->r_addend;
fbaf61ad
NC
5631
5632 if (h)
5633 {
5634 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5635
5636 BFD_ASSERT (sgot != NULL);
5637 /* If we did not allocate got entry for the symbol,
5638 we can not fill the nonexistent got entry. */
5639 if (h->got.offset != (bfd_vma) -1
5640 && (h->got.offset & 1) == 0)
5641 {
5642 bfd_put_32 (output_bfd, outrel.r_addend,
5643 sgot->contents + h->got.offset);
5644 }
5645 }
35c08157
KLC
5646 }
5647 else
5648 {
fbaf61ad
NC
5649 if (h->dynindx == -1)
5650 {
5651 _bfd_error_handler
5652 (_("%pB: relocation %s against `%s' can not be used when"
5653 "making a shared object; recompile with -fPIC"),
5654 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5655 bfd_set_error (bfd_error_bad_value);
5656 return FALSE;
5657 }
5658
35c08157
KLC
5659 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5660 outrel.r_addend = rel->r_addend;
5661 }
5662 }
5663
5664 loc = sreloc->contents;
5665 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5666 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5667 ++sreloc->reloc_count;
5668
5669 /* If this reloc is against an external symbol, we do
5670 not want to fiddle with the addend. Otherwise, we
5671 need to include the symbol value so that it becomes
5672 an addend for the dynamic reloc. */
5673 if (!relocate)
5674 continue;
5675 }
5676 break;
5677
5678 case R_NDS32_25_ABS_RELA:
0e1862bb 5679 if (bfd_link_pic (info))
35c08157 5680 {
4eca0228 5681 _bfd_error_handler
38f14ab8
AM
5682 (_("%pB: warning: %s unsupported in shared mode"),
5683 input_bfd, "R_NDS32_25_ABS_RELA");
35c08157
KLC
5684 return FALSE;
5685 }
5686 break;
5687
5688 case R_NDS32_9_PCREL:
5689 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5690 contents, offset,
5691 sec, relocation, addend);
5692 goto check_reloc;
5693
5694 case R_NDS32_HI20:
fbaf61ad
NC
5695 /* We allow an arbitrary number of HI20 relocs before the
5696 LO12 reloc. This permits gcc to emit the HI and LO relocs
5697 itself. */
5698 for (lorel = rel + 1;
5699 (lorel < relend
5700 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5701 continue;
5702 if (lorel < relend
5703 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5704 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5705 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5706 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
35c08157 5707 {
fbaf61ad
NC
5708 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5709 contents, relocation + addend);
5710 r = bfd_reloc_ok;
35c08157 5711 }
fbaf61ad
NC
5712 else
5713 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5714 contents, offset, relocation,
5715 addend);
35c08157
KLC
5716 goto check_reloc;
5717
5718 case R_NDS32_GOT17S2_RELA:
5719 case R_NDS32_GOT15S2_RELA:
fbaf61ad
NC
5720 BFD_ASSERT (sgot != NULL);
5721
5722 if (h != NULL)
35c08157 5723 {
fbaf61ad 5724 bfd_boolean dyn;
35c08157 5725
fbaf61ad
NC
5726 off = h->got.offset;
5727 BFD_ASSERT (off != (bfd_vma) - 1);
35c08157 5728
fbaf61ad
NC
5729 dyn = htab->root.dynamic_sections_created;
5730 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5731 (dyn, bfd_link_pic (info), h)
5732 || (bfd_link_pic (info)
5733 && (info->symbolic
5734 || h->dynindx == -1
5735 || h->forced_local)
5736 && h->def_regular))
35c08157 5737 {
fbaf61ad
NC
5738 /* This is actually a static link, or it is a
5739 -Bsymbolic link and the symbol is defined
5740 locally, or the symbol was forced to be local
5741 because of a version file. We must initialize
5742 this entry in the global offset table. Since the
5743 offset must always be a multiple of 4, we use the
5744 least significant bit to record whether we have
5745 initialized it already.
5746
5747 When doing a dynamic link, we create a .rela.got
5748 relocation entry to initialize the value. This
5749 is done in the finish_dynamic_symbol routine. */
5750 if ((off & 1) != 0)
5751 off &= ~1;
5752 else
35c08157 5753 {
fbaf61ad
NC
5754 bfd_put_32 (output_bfd, relocation,
5755 sgot->contents + off);
5756 h->got.offset |= 1;
35c08157
KLC
5757 }
5758 }
fbaf61ad
NC
5759 }
5760 else
5761 {
5762 bfd_byte *loc;
35c08157 5763
fbaf61ad
NC
5764 BFD_ASSERT (local_got_offsets != NULL
5765 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
35c08157 5766
fbaf61ad 5767 off = local_got_offsets[r_symndx];
35c08157 5768
fbaf61ad
NC
5769 /* The offset must always be a multiple of 4. We use
5770 the least significant bit to record whether we have
5771 already processed this entry. */
5772 if ((off & 1) != 0)
5773 off &= ~1;
5774 else
5775 {
5776 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5777
5778 if (bfd_link_pic (info))
35c08157 5779 {
fbaf61ad
NC
5780 asection *srelgot;
5781 Elf_Internal_Rela outrel;
35c08157 5782
fbaf61ad
NC
5783 /* We need to generate a R_NDS32_RELATIVE reloc
5784 for the dynamic linker. */
5785 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5786 BFD_ASSERT (srelgot != NULL);
5787
5788 outrel.r_offset = (elf_gp (output_bfd)
5789 + sgot->output_offset + off);
5790 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5791 outrel.r_addend = relocation;
5792 loc = srelgot->contents;
5793 loc +=
5794 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5795 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5796 ++srelgot->reloc_count;
35c08157 5797 }
fbaf61ad 5798 local_got_offsets[r_symndx] |= 1;
35c08157 5799 }
35c08157 5800 }
fbaf61ad
NC
5801 relocation = sgot->output_section->vma + sgot->output_offset + off
5802 - elf_gp (output_bfd);
5803
35c08157
KLC
5804 if (relocation & align)
5805 {
5806 /* Incorrect alignment. */
4eca0228 5807 _bfd_error_handler
38f14ab8 5808 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
35c08157
KLC
5809 ret = FALSE;
5810 r = bfd_reloc_dangerous;
5811 goto check_reloc;
5812 }
5813 break;
5814
5815 case R_NDS32_SDA16S3_RELA:
5816 case R_NDS32_SDA15S3_RELA:
5817 case R_NDS32_SDA15S3:
5818 align = 0x7;
5819 goto handle_sda;
5820
5821 case R_NDS32_SDA17S2_RELA:
5822 case R_NDS32_SDA15S2_RELA:
5823 case R_NDS32_SDA12S2_SP_RELA:
5824 case R_NDS32_SDA12S2_DP_RELA:
5825 case R_NDS32_SDA15S2:
5826 case R_NDS32_SDA_FP7U2_RELA:
5827 align = 0x3;
5828 goto handle_sda;
5829
5830 case R_NDS32_SDA18S1_RELA:
5831 case R_NDS32_SDA15S1_RELA:
5832 case R_NDS32_SDA15S1:
5833 align = 0x1;
5834 goto handle_sda;
5835
5836 case R_NDS32_SDA19S0_RELA:
5837 case R_NDS32_SDA15S0_RELA:
5838 case R_NDS32_SDA15S0:
fbaf61ad 5839 align = 0x0;
35c08157 5840handle_sda:
fbaf61ad 5841 BFD_ASSERT (sec != NULL);
35c08157 5842
fbaf61ad
NC
5843 /* If the symbol is in the abs section, the out_bfd will be null.
5844 This happens when the relocation has a symbol@GOTOFF. */
5845 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5846 if (r != bfd_reloc_ok)
5847 {
5848 _bfd_error_handler
5849 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5850 ret = FALSE;
5851 goto check_reloc;
5852 }
35c08157 5853
fbaf61ad
NC
5854 /* At this point `relocation' contains the object's
5855 address. */
5856 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5857 {
5858 relocation -= fpbase_addr;
5859 }
5860 else
5861 relocation -= gp;
5862 /* Now it contains the offset from _SDA_BASE_. */
35c08157 5863
fbaf61ad 5864 /* Make sure alignment is correct. */
35c08157 5865
fbaf61ad
NC
5866 if (relocation & align)
5867 {
5868 /* Incorrect alignment. */
5869 _bfd_error_handler
5870 /* xgettext:c-format */
5871 (_("%pB(%pA): warning: unaligned small data access"
5872 " of type %d"),
5873 input_bfd, input_section, r_type);
5874 ret = FALSE;
5875 goto check_reloc;
35c08157 5876 }
35c08157 5877 break;
fbaf61ad 5878
35c08157
KLC
5879 case R_NDS32_17IFC_PCREL_RELA:
5880 case R_NDS32_10IFCU_PCREL_RELA:
fbaf61ad 5881 /* Do nothing. */
35c08157
KLC
5882 break;
5883
1c8f6a4d
KLC
5884 case R_NDS32_TLS_LE_HI20:
5885 case R_NDS32_TLS_LE_LO12:
5886 case R_NDS32_TLS_LE_20:
5887 case R_NDS32_TLS_LE_15S0:
5888 case R_NDS32_TLS_LE_15S1:
5889 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
5890 /* We do not have garbage collection for got entries.
5891 Therefore, IE to LE may have one empty entry, and DESC to
5892 LE may have two. */
1c8f6a4d
KLC
5893 if (elf_hash_table (info)->tls_sec != NULL)
5894 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5895 break;
fbaf61ad 5896
1c8f6a4d
KLC
5897 case R_NDS32_TLS_IE_HI20:
5898 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
5899 case R_NDS32_TLS_DESC_HI20:
5900 case R_NDS32_TLS_DESC_LO12:
5901 case R_NDS32_TLS_IE_LO12:
5902 case R_NDS32_TLS_IEGP_HI20:
5903 case R_NDS32_TLS_IEGP_LO12:
5904 case R_NDS32_TLS_IEGP_LO12S2:
1c8f6a4d
KLC
5905 {
5906 /* Relocation is to the entry for this symbol in the global
5907 offset table. */
fbaf61ad 5908 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
1c8f6a4d
KLC
5909 asection *srelgot;
5910 Elf_Internal_Rela outrel;
1c8f6a4d
KLC
5911 bfd_byte *loc;
5912 int indx = 0;
5913
fbaf61ad
NC
5914 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5915
1c8f6a4d
KLC
5916 BFD_ASSERT (sgot != NULL);
5917 if (h != NULL)
5918 {
5919 bfd_boolean dyn;
5920
5921 off = h->got.offset;
fbaf61ad 5922 BFD_ASSERT (off != (bfd_vma) -1);
1c8f6a4d
KLC
5923 dyn = htab->root.dynamic_sections_created;
5924 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
0e1862bb
L
5925 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5926 && (!bfd_link_pic (info)
1c8f6a4d
KLC
5927 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5928 indx = h->dynindx;
5929 }
5930 else
5931 {
1c8f6a4d
KLC
5932 BFD_ASSERT (local_got_offsets != NULL
5933 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
1c8f6a4d 5934 off = local_got_offsets[r_symndx];
1c8f6a4d
KLC
5935 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5936 }
fbaf61ad 5937
1c8f6a4d
KLC
5938 relocation = sgot->output_section->vma + sgot->output_offset + off;
5939
fbaf61ad
NC
5940 if (1 < ones32 (tls_type))
5941 {
5942 eff_tls_type = 1 << (fls (tls_type) - 1);
5943 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5944
5945 /* TLS model X -> LE is not implement yet!
5946 workaround here! */
5947 if (eff_tls_type == GOT_TLS_LE)
5948 {
5949 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5950 }
5951 }
1c8f6a4d
KLC
5952
5953 /* The offset must always be a multiple of 4. We use
5954 the least significant bit to record whether we have
5955 already processed this entry. */
fbaf61ad
NC
5956 bfd_boolean need_relocs = FALSE;
5957 srelgot = ehtab->srelgot;
5958 if ((bfd_link_pic (info) || indx != 0)
5959 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5960 || h->root.type != bfd_link_hash_undefweak))
5961 {
5962 need_relocs = TRUE;
5963 BFD_ASSERT (srelgot != NULL);
5964 }
5965
5966 if (off & 1)
5967 {
5968 off &= ~1;
5969 relocation &= ~1;
5970
5971 if (eff_tls_type & GOT_TLS_DESC)
5972 {
5973 relocation -= elf_gp (output_bfd);
5974 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5975 {
5976 /* TLS model shall be converted. */
5977 BFD_ASSERT(0);
5978 }
5979 }
5980 else if (eff_tls_type & GOT_TLS_IEGP)
5981 {
5982 relocation -= elf_gp (output_bfd);
5983 }
5984 }
1c8f6a4d
KLC
5985 else
5986 {
fbaf61ad 5987 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
1c8f6a4d 5988 {
fbaf61ad
NC
5989 /* TLS model workaround shall be applied. */
5990 BFD_ASSERT(0);
1c8f6a4d 5991 }
fbaf61ad 5992 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
1c8f6a4d 5993 {
fbaf61ad
NC
5994 if (eff_tls_type & GOT_TLS_IEGP)
5995 relocation -= elf_gp(output_bfd);
5996
1c8f6a4d
KLC
5997 if (need_relocs)
5998 {
fbaf61ad
NC
5999 if (indx == 0)
6000 outrel.r_addend = gottpoff (info, relocation_sym);
1c8f6a4d
KLC
6001 else
6002 outrel.r_addend = 0;
6003 outrel.r_offset = (sgot->output_section->vma
fbaf61ad
NC
6004 + sgot->output_offset + off);
6005 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
6006
6007 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
6008 &outrel);
1c8f6a4d
KLC
6009 }
6010 else
fbaf61ad
NC
6011 {
6012 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6013 sgot->contents + off);
6014 }
1c8f6a4d 6015 }
fbaf61ad
NC
6016 else if (eff_tls_type & GOT_TLS_DESC)
6017 {
6018 relocation -= elf_gp (output_bfd);
6019 if (need_relocs)
6020 {
6021 if (indx == 0)
6022 outrel.r_addend = gottpoff (info, relocation_sym);
6023 else
6024 outrel.r_addend = 0;
6025 outrel.r_offset = (sgot->output_section->vma
6026 + sgot->output_offset + off);
6027 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
6028
6029 if (htab->tls_desc_trampoline)
6030 {
6031 asection *srelplt;
6032 srelplt = ehtab->srelplt;
6033 loc = srelplt->contents;
6034 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
6035 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
6036 <= srelplt->contents + srelplt->size);
6037
6038 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6039 }
6040 else
6041 {
6042 loc = srelgot->contents;
6043 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
6044 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6045 ++srelgot->reloc_count;
6046 }
6047 }
6048 else
6049 {
6050 /* feed me! */
6051 bfd_put_32 (output_bfd, 0xdeadbeef,
6052 sgot->contents + off);
6053 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6054 sgot->contents + off + 4);
6055 patch_tls_desc_to_ie (contents, rel, input_bfd);
6056 BFD_ASSERT(0);
6057 }
6058 }
6059 else
6060 {
6061 /* TLS model workaround shall be applied. */
6062 BFD_ASSERT(0);
6063 }
6064
6065 if (h != NULL)
6066 h->got.offset |= 1;
6067 else
6068 local_got_offsets[r_symndx] |= 1;
6069 }
6070 }
6071 break;
6072 /* DON'T fall through. */
6073
6074 default:
6075 /* OLD_NDS32_RELOC. */
6076
6077 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6078 contents, offset, relocation, addend);
6079 goto check_reloc;
6080 }
6081
6082 switch ((int) r_type)
6083 {
35c08157
KLC
6084 case R_NDS32_20_RELA:
6085 case R_NDS32_5_RELA:
6086 case R_NDS32_9_PCREL_RELA:
6087 case R_NDS32_WORD_9_PCREL_RELA:
6088 case R_NDS32_10_UPCREL_RELA:
6089 case R_NDS32_15_PCREL_RELA:
6090 case R_NDS32_17_PCREL_RELA:
6091 case R_NDS32_25_PCREL_RELA:
6092 case R_NDS32_25_ABS_RELA:
6093 case R_NDS32_HI20_RELA:
6094 case R_NDS32_LO12S3_RELA:
6095 case R_NDS32_LO12S2_RELA:
6096 case R_NDS32_LO12S2_DP_RELA:
6097 case R_NDS32_LO12S2_SP_RELA:
6098 case R_NDS32_LO12S1_RELA:
6099 case R_NDS32_LO12S0_RELA:
6100 case R_NDS32_LO12S0_ORI_RELA:
6101 case R_NDS32_SDA16S3_RELA:
6102 case R_NDS32_SDA17S2_RELA:
6103 case R_NDS32_SDA18S1_RELA:
6104 case R_NDS32_SDA19S0_RELA:
6105 case R_NDS32_SDA15S3_RELA:
6106 case R_NDS32_SDA15S2_RELA:
6107 case R_NDS32_SDA12S2_DP_RELA:
6108 case R_NDS32_SDA12S2_SP_RELA:
6109 case R_NDS32_SDA15S1_RELA:
6110 case R_NDS32_SDA15S0_RELA:
6111 case R_NDS32_SDA_FP7U2_RELA:
6112 case R_NDS32_9_PLTREL:
6113 case R_NDS32_25_PLTREL:
6114 case R_NDS32_GOT20:
6115 case R_NDS32_GOT_HI20:
6116 case R_NDS32_GOT_LO12:
6117 case R_NDS32_GOT_LO15:
6118 case R_NDS32_GOT_LO19:
6119 case R_NDS32_GOT15S2_RELA:
6120 case R_NDS32_GOT17S2_RELA:
6121 case R_NDS32_GOTPC20:
6122 case R_NDS32_GOTPC_HI20:
6123 case R_NDS32_GOTPC_LO12:
6124 case R_NDS32_GOTOFF:
6125 case R_NDS32_GOTOFF_HI20:
6126 case R_NDS32_GOTOFF_LO12:
6127 case R_NDS32_GOTOFF_LO15:
6128 case R_NDS32_GOTOFF_LO19:
6129 case R_NDS32_PLTREL_HI20:
6130 case R_NDS32_PLTREL_LO12:
6131 case R_NDS32_PLT_GOTREL_HI20:
6132 case R_NDS32_PLT_GOTREL_LO12:
6133 case R_NDS32_PLT_GOTREL_LO15:
6134 case R_NDS32_PLT_GOTREL_LO19:
6135 case R_NDS32_PLT_GOTREL_LO20:
6136 case R_NDS32_17IFC_PCREL_RELA:
6137 case R_NDS32_10IFCU_PCREL_RELA:
1c8f6a4d
KLC
6138 case R_NDS32_TLS_LE_HI20:
6139 case R_NDS32_TLS_LE_LO12:
6140 case R_NDS32_TLS_IE_HI20:
6141 case R_NDS32_TLS_IE_LO12S2:
6142 case R_NDS32_TLS_LE_20:
6143 case R_NDS32_TLS_LE_15S0:
6144 case R_NDS32_TLS_LE_15S1:
6145 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
6146 case R_NDS32_TLS_DESC_HI20:
6147 case R_NDS32_TLS_DESC_LO12:
6148 case R_NDS32_TLS_IE_LO12:
6149 case R_NDS32_TLS_IEGP_HI20:
6150 case R_NDS32_TLS_IEGP_LO12:
6151 case R_NDS32_TLS_IEGP_LO12S2:
35c08157 6152 /* Instruction related relocs must handle endian properly. */
1c8f6a4d 6153 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
35c08157
KLC
6154 r = nds32_elf_final_link_relocate (howto, input_bfd,
6155 input_section, contents,
6156 rel->r_offset, relocation,
6157 rel->r_addend);
6158 break;
6159
6160 default:
6161 /* All other relocs can use default handler. */
6162 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6163 contents, rel->r_offset,
6164 relocation, rel->r_addend);
6165 break;
6166 }
6167
6168check_reloc:
6169
6170 if (r != bfd_reloc_ok)
6171 {
6172 /* FIXME: This should be generic enough to go in a utility. */
6173 const char *name;
6174
6175 if (h != NULL)
6176 name = h->root.root.string;
6177 else
6178 {
6179 name = bfd_elf_string_from_elf_section
6180 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6181 if (name == NULL || *name == '\0')
6182 name = bfd_section_name (input_bfd, sec);
6183 }
6184
6185 if (errmsg != NULL)
6186 goto common_error;
6187
6188 switch (r)
6189 {
6190 case bfd_reloc_overflow:
1a72702b
AM
6191 (*info->callbacks->reloc_overflow)
6192 (info, (h ? &h->root : NULL), name, howto->name,
6193 (bfd_vma) 0, input_bfd, input_section, offset);
35c08157
KLC
6194 break;
6195
6196 case bfd_reloc_undefined:
1a72702b
AM
6197 (*info->callbacks->undefined_symbol)
6198 (info, name, input_bfd, input_section, offset, TRUE);
35c08157
KLC
6199 break;
6200
6201 case bfd_reloc_outofrange:
6202 errmsg = _("internal error: out of range error");
6203 goto common_error;
6204
6205 case bfd_reloc_notsupported:
6206 errmsg = _("internal error: unsupported relocation error");
6207 goto common_error;
6208
6209 case bfd_reloc_dangerous:
6210 errmsg = _("internal error: dangerous error");
6211 goto common_error;
6212
6213 default:
6214 errmsg = _("internal error: unknown error");
6215 /* Fall through. */
6216
fbaf61ad 6217common_error:
1a72702b
AM
6218 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6219 input_section, offset);
35c08157
KLC
6220 break;
6221 }
6222 }
6223 }
6224
fbaf61ad
NC
6225 /* Resotre header size to avoid overflow load. */
6226 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6227 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6228
35c08157
KLC
6229 return ret;
6230}
6231
6232/* Finish up dynamic symbol handling. We set the contents of various
6233 dynamic sections here. */
6234
6235static bfd_boolean
6236nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6237 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6238{
fbaf61ad
NC
6239 struct elf_link_hash_table *ehtab;
6240 struct elf_nds32_link_hash_entry *hent;
35c08157
KLC
6241 bfd_byte *loc;
6242
fbaf61ad
NC
6243 ehtab = elf_hash_table (info);
6244 hent = (struct elf_nds32_link_hash_entry *) h;
35c08157
KLC
6245
6246 if (h->plt.offset != (bfd_vma) - 1)
6247 {
6248 asection *splt;
6249 asection *sgot;
6250 asection *srela;
6251
6252 bfd_vma plt_index;
6253 bfd_vma got_offset;
6254 bfd_vma local_plt_offset;
6255 Elf_Internal_Rela rela;
6256
6257 /* This symbol has an entry in the procedure linkage table. Set
6258 it up. */
6259
6260 BFD_ASSERT (h->dynindx != -1);
6261
fbaf61ad
NC
6262 splt = ehtab->splt;
6263 sgot = ehtab->sgotplt;
6264 srela = ehtab->srelplt;
35c08157
KLC
6265 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6266
6267 /* Get the index in the procedure linkage table which
6268 corresponds to this symbol. This is the index of this symbol
6269 in all the symbols for which we are making plt entries. The
6270 first entry in the procedure linkage table is reserved. */
6271 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6272
6273 /* Get the offset into the .got table of the entry that
6274 corresponds to this function. Each .got entry is 4 bytes.
6275 The first three are reserved. */
6276 got_offset = (plt_index + 3) * 4;
6277
6278 /* Fill in the entry in the procedure linkage table. */
0e1862bb 6279 if (!bfd_link_pic (info))
35c08157
KLC
6280 {
6281 unsigned long insn;
6282
6283 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6284 + sgot->output_offset + got_offset) >> 12)
6285 & 0xfffff);
6286 bfd_putb32 (insn, splt->contents + h->plt.offset);
6287
6288 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6289 + sgot->output_offset + got_offset) & 0x0fff)
6290 >> 2);
6291 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6292
6293 insn = PLT_ENTRY_WORD2;
6294 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6295
6296 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6297 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6298
6299 insn = PLT_ENTRY_WORD4
6300 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6301 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6302 local_plt_offset = 12;
6303 }
6304 else
6305 {
6306 /* sda_base must be set at this time. */
6307 unsigned long insn;
6308 long offset;
6309
35c08157
KLC
6310 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6311 - elf_gp (output_bfd);
6312 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6313 bfd_putb32 (insn, splt->contents + h->plt.offset);
6314
6315 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6316 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6317
6318 insn = PLT_PIC_ENTRY_WORD2;
6319 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6320
6321 insn = PLT_PIC_ENTRY_WORD3;
6322 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6323
6324 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6325 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6326
6327 insn = PLT_PIC_ENTRY_WORD5
6328 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6329 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6330
6331 local_plt_offset = 16;
6332 }
6333
6334 /* Fill in the entry in the global offset table,
6335 so it will fall through to the next instruction for the first time. */
6336 bfd_put_32 (output_bfd,
6337 (splt->output_section->vma + splt->output_offset
6338 + h->plt.offset + local_plt_offset),
6339 sgot->contents + got_offset);
6340
6341 /* Fill in the entry in the .rela.plt section. */
6342 rela.r_offset = (sgot->output_section->vma
6343 + sgot->output_offset + got_offset);
6344 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6345 rela.r_addend = 0;
6346 loc = srela->contents;
6347 loc += plt_index * sizeof (Elf32_External_Rela);
6348 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6349
6350 if (!h->def_regular)
6351 {
6352 /* Mark the symbol as undefined, rather than as defined in
6353 the .plt section. Leave the value alone. */
6354 sym->st_shndx = SHN_UNDEF;
6355 if (!h->ref_regular_nonweak)
6356 sym->st_value = 0;
6357 }
6358 }
6359
fbaf61ad
NC
6360 if (h->got.offset != (bfd_vma) - 1
6361 && hent->tls_type == GOT_NORMAL)
35c08157
KLC
6362 {
6363 asection *sgot;
fbaf61ad 6364 asection *srelagot;
35c08157
KLC
6365 Elf_Internal_Rela rela;
6366
6367 /* This symbol has an entry in the global offset table.
6368 Set it up. */
6369
fbaf61ad
NC
6370 sgot = ehtab->sgot;
6371 srelagot = ehtab->srelgot;
6372 BFD_ASSERT (sgot != NULL && srelagot != NULL);
35c08157
KLC
6373
6374 rela.r_offset = (sgot->output_section->vma
6375 + sgot->output_offset + (h->got.offset & ~1));
6376
6377 /* If this is a -Bsymbolic link, and the symbol is defined
6378 locally, we just want to emit a RELATIVE reloc. Likewise if
6379 the symbol was forced to be local because of a version file.
6380 The entry in the global offset table will already have been
6381 initialized in the relocate_section function. */
fbaf61ad
NC
6382 if ((bfd_link_pic (info)
6383 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6384 && h->def_regular)
6385 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
6386 {
6387 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6388 rela.r_addend = (h->root.u.def.value
6389 + h->root.u.def.section->output_section->vma
6390 + h->root.u.def.section->output_offset);
fbaf61ad
NC
6391
6392 if ((h->got.offset & 1) == 0)
6393 {
6394 bfd_put_32 (output_bfd, rela.r_addend,
6395 sgot->contents + h->got.offset);
6396 }
35c08157
KLC
6397 }
6398 else
6399 {
6400 BFD_ASSERT ((h->got.offset & 1) == 0);
6401 bfd_put_32 (output_bfd, (bfd_vma) 0,
6402 sgot->contents + h->got.offset);
6403 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6404 rela.r_addend = 0;
6405 }
6406
fbaf61ad
NC
6407 loc = srelagot->contents;
6408 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
35c08157 6409 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
fbaf61ad
NC
6410 ++srelagot->reloc_count;
6411 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
35c08157
KLC
6412 }
6413
6414 if (h->needs_copy)
6415 {
6416 asection *s;
6417 Elf_Internal_Rela rela;
6418
6419 /* This symbols needs a copy reloc. Set it up. */
6420
6421 BFD_ASSERT (h->dynindx != -1
6422 && (h->root.type == bfd_link_hash_defined
6423 || h->root.type == bfd_link_hash_defweak));
6424
6425 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6426 BFD_ASSERT (s != NULL);
6427
6428 rela.r_offset = (h->root.u.def.value
6429 + h->root.u.def.section->output_section->vma
6430 + h->root.u.def.section->output_offset);
6431 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6432 rela.r_addend = 0;
6433 loc = s->contents;
6434 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6435 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6436 ++s->reloc_count;
6437 }
6438
6439 /* Mark some specially defined symbols as absolute. */
6440 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6441 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6442 sym->st_shndx = SHN_ABS;
6443
6444 return TRUE;
6445}
6446
6447
6448/* Finish up the dynamic sections. */
6449
6450static bfd_boolean
6451nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6452{
35c08157
KLC
6453 bfd *dynobj;
6454 asection *sdyn;
fbaf61ad
NC
6455 asection *sgotplt;
6456 struct elf_link_hash_table *ehtab;
6457 struct elf_nds32_link_hash_table *htab;
35c08157 6458
fbaf61ad 6459 ehtab = elf_hash_table (info);
35c08157 6460 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
6461 if (htab == NULL)
6462 return FALSE;
6463
6464 dynobj = elf_hash_table (info)->dynobj;
35c08157 6465
fbaf61ad
NC
6466 sgotplt = ehtab->sgotplt;
6467 /* A broken linker script might have discarded the dynamic sections.
6468 Catch this here so that we do not seg-fault later on. */
6469 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6470 return FALSE;
35c08157
KLC
6471 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6472
fbaf61ad 6473 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
6474 {
6475 asection *splt;
6476 Elf32_External_Dyn *dyncon, *dynconend;
6477
fbaf61ad 6478 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
35c08157
KLC
6479
6480 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6481 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6482
6483 for (; dyncon < dynconend; dyncon++)
6484 {
6485 Elf_Internal_Dyn dyn;
6486 asection *s;
6487
6488 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6489
6490 switch (dyn.d_tag)
6491 {
6492 default:
6493 break;
6494
6495 case DT_PLTGOT:
fbaf61ad
NC
6496 /* name = ".got"; */
6497 s = ehtab->sgot->output_section;
35c08157
KLC
6498 goto get_vma;
6499 case DT_JMPREL:
fbaf61ad
NC
6500 s = ehtab->srelplt->output_section;
6501get_vma:
6502 BFD_ASSERT (s != NULL);
6503 dyn.d_un.d_ptr = s->vma;
35c08157
KLC
6504 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6505 break;
6506
6507 case DT_PLTRELSZ:
fbaf61ad
NC
6508 s = ehtab->srelplt->output_section;
6509 BFD_ASSERT (s != NULL);
35c08157
KLC
6510 dyn.d_un.d_val = s->size;
6511 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6512 break;
fbaf61ad
NC
6513
6514 case DT_RELASZ:
6515 /* My reading of the SVR4 ABI indicates that the
6516 procedure linkage table relocs (DT_JMPREL) should be
6517 included in the overall relocs (DT_RELA). This is
6518 what Solaris does. However, UnixWare can not handle
6519 that case. Therefore, we override the DT_RELASZ entry
6520 here to make it not include the JMPREL relocs. Since
6521 the linker script arranges for .rela.plt to follow all
6522 other relocation sections, we don't have to worry
6523 about changing the DT_RELA entry. */
6524 if (ehtab->srelplt != NULL)
6525 {
6526 s = ehtab->srelplt->output_section;
6527 dyn.d_un.d_val -= s->size;
6528 }
6529 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6530 break;
6531
6532 case DT_TLSDESC_PLT:
6533 s = htab->root.splt;
6534 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6535 + htab->dt_tlsdesc_plt);
6536 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6537 break;
6538
6539 case DT_TLSDESC_GOT:
6540 s = htab->root.sgot;
6541 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6542 + htab->dt_tlsdesc_got);
6543 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6544 break;
35c08157
KLC
6545 }
6546 }
6547
6548 /* Fill in the first entry in the procedure linkage table. */
fbaf61ad 6549 splt = ehtab->splt;
35c08157
KLC
6550 if (splt && splt->size > 0)
6551 {
0e1862bb 6552 if (bfd_link_pic (info))
35c08157
KLC
6553 {
6554 unsigned long insn;
6555 long offset;
6556
fbaf61ad
NC
6557 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6558 - elf_gp (output_bfd);
35c08157
KLC
6559 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6560 bfd_putb32 (insn, splt->contents);
6561
35c08157
KLC
6562 /* here has a typo? */
6563 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6564 bfd_putb32 (insn, splt->contents + 4);
6565
6566 insn = PLT0_PIC_ENTRY_WORD2;
6567 bfd_putb32 (insn, splt->contents + 8);
6568
6569 insn = PLT0_PIC_ENTRY_WORD3;
6570 bfd_putb32 (insn, splt->contents + 12);
6571
6572 insn = PLT0_PIC_ENTRY_WORD4;
6573 bfd_putb32 (insn, splt->contents + 16);
6574
6575 insn = PLT0_PIC_ENTRY_WORD5;
6576 bfd_putb32 (insn, splt->contents + 20);
6577 }
6578 else
6579 {
6580 unsigned long insn;
6581 unsigned long addr;
6582
6583 /* addr = .got + 4 */
fbaf61ad 6584 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
35c08157
KLC
6585 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6586 bfd_putb32 (insn, splt->contents);
6587
6588 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6589 bfd_putb32 (insn, splt->contents + 4);
6590
6591 insn = PLT0_ENTRY_WORD2;
6592 bfd_putb32 (insn, splt->contents + 8);
6593
6594 insn = PLT0_ENTRY_WORD3;
6595 bfd_putb32 (insn, splt->contents + 12);
6596
6597 insn = PLT0_ENTRY_WORD4;
6598 bfd_putb32 (insn, splt->contents + 16);
6599 }
6600
6601 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6602 PLT_ENTRY_SIZE;
6603 }
fbaf61ad
NC
6604
6605 if (htab->dt_tlsdesc_plt)
6606 {
6607 /* Calculate addresses. */
6608 asection *sgot = sgot = ehtab->sgot;
6609 bfd_vma pltgot = sgotplt->output_section->vma
6610 + sgotplt->output_offset;
6611 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6612 + htab->dt_tlsdesc_got;
6613
6614 /* Get GP offset. */
6615 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6616 tlsdesc_got -= elf_gp (output_bfd);
6617
6618 /* Do relocation. */
6619 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6620 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6621 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6622 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6623
6624 /* Insert .plt. */
6625 nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6626 dl_tlsdesc_lazy_trampoline,
6627 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6628 }
35c08157
KLC
6629 }
6630
6631 /* Fill in the first three entries in the global offset table. */
fbaf61ad 6632 if (sgotplt && sgotplt->size > 0)
35c08157
KLC
6633 {
6634 if (sdyn == NULL)
fbaf61ad 6635 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
35c08157
KLC
6636 else
6637 bfd_put_32 (output_bfd,
6638 sdyn->output_section->vma + sdyn->output_offset,
fbaf61ad
NC
6639 sgotplt->contents);
6640 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6641 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
35c08157 6642
fbaf61ad 6643 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
35c08157
KLC
6644 }
6645
6646 return TRUE;
6647}
6648\f
6649
6650/* Set the right machine number. */
6651
6652static bfd_boolean
6653nds32_elf_object_p (bfd *abfd)
6654{
6655 static unsigned int cur_arch = 0;
6656
6657 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6658 {
6659 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6660 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6661 }
6662
6663 switch (cur_arch)
6664 {
6665 default:
6666 case E_N1_ARCH:
6667 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6668 break;
6669 case E_N1H_ARCH:
6670 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6671 break;
6672 case E_NDS_ARCH_STAR_V2_0:
6673 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6674 break;
6675 case E_NDS_ARCH_STAR_V3_0:
6676 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6677 break;
6678 case E_NDS_ARCH_STAR_V3_M:
6679 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6680 break;
6681 }
6682
6683 return TRUE;
6684}
6685
6686/* Store the machine number in the flags field. */
6687
6688static void
6689nds32_elf_final_write_processing (bfd *abfd,
6690 bfd_boolean linker ATTRIBUTE_UNUSED)
6691{
6692 unsigned long val;
6693 static unsigned int cur_mach = 0;
6694
6695 if (bfd_mach_n1 != bfd_get_mach (abfd))
6696 {
6697 cur_mach = bfd_get_mach (abfd);
6698 }
6699
6700 switch (cur_mach)
6701 {
6702 case bfd_mach_n1:
6703 /* Only happen when object is empty, since the case is abandon. */
6704 val = E_N1_ARCH;
6705 val |= E_NDS_ABI_AABI;
6706 val |= E_NDS32_ELF_VER_1_4;
6707 break;
6708 case bfd_mach_n1h:
6709 val = E_N1H_ARCH;
6710 break;
6711 case bfd_mach_n1h_v2:
6712 val = E_NDS_ARCH_STAR_V2_0;
6713 break;
6714 case bfd_mach_n1h_v3:
6715 val = E_NDS_ARCH_STAR_V3_0;
6716 break;
6717 case bfd_mach_n1h_v3m:
6718 val = E_NDS_ARCH_STAR_V3_M;
6719 break;
6720 default:
6721 val = 0;
6722 break;
6723 }
6724
6725 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6726 elf_elfheader (abfd)->e_flags |= val;
6727}
6728
6729/* Function to keep NDS32 specific file flags. */
6730
6731static bfd_boolean
6732nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6733{
6734 BFD_ASSERT (!elf_flags_init (abfd)
6735 || elf_elfheader (abfd)->e_flags == flags);
6736
6737 elf_elfheader (abfd)->e_flags = flags;
6738 elf_flags_init (abfd) = TRUE;
6739 return TRUE;
6740}
6741
6742static unsigned int
6743convert_e_flags (unsigned int e_flags, unsigned int arch)
6744{
6745 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6746 {
6747 /* From 0.9 to 1.0. */
6748 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6749
6750 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6751 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6752 if (arch == E_NDS_ARCH_STAR_V1_0)
6753 {
6754 /* Done. */
6755 return e_flags;
6756 }
6757 }
6758
6759 /* From 1.0 to 2.0. */
6760 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6761
6762 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6763 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6764
6765 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6766 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6767 return e_flags;
6768}
6769
6770static bfd_boolean
6771nds32_check_vec_size (bfd *ibfd)
6772{
6773 static unsigned int nds32_vec_size = 0;
6774
6775 asection *sec_t = NULL;
6776 bfd_byte *contents = NULL;
6777
6778 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6779
6780 if (sec_t && sec_t->size >= 4)
6781 {
6782 /* Get vec_size in file. */
6783 unsigned int flag_t;
6784
0c4bd9d9 6785 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
35c08157
KLC
6786 flag_t = bfd_get_32 (ibfd, contents);
6787
6788 /* The value could only be 4 or 16. */
6789
6790 if (!nds32_vec_size)
6791 /* Set if not set yet. */
6792 nds32_vec_size = (flag_t & 0x3);
6793 else if (nds32_vec_size != (flag_t & 0x3))
6794 {
4eca0228 6795 _bfd_error_handler
695344c0 6796 /* xgettext:c-format */
871b3ab2 6797 (_("%pB: ISR vector size mismatch"
4eca0228
AM
6798 " with previous modules, previous %u-byte, current %u-byte"),
6799 ibfd,
6800 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6801 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
35c08157
KLC
6802 return FALSE;
6803 }
6804 else
6805 /* Only keep the first vec_size section. */
6806 sec_t->flags |= SEC_EXCLUDE;
6807 }
6808
6809 return TRUE;
6810}
6811
6812/* Merge backend specific data from an object file to the output
6813 object file when linking. */
6814
6815static bfd_boolean
50e03d47 6816nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
35c08157 6817{
50e03d47 6818 bfd *obfd = info->output_bfd;
35c08157
KLC
6819 flagword out_flags;
6820 flagword in_flags;
6821 flagword out_16regs;
6822 flagword in_no_mac;
6823 flagword out_no_mac;
6824 flagword in_16regs;
6825 flagword out_version;
6826 flagword in_version;
6827 flagword out_fpu_config;
6828 flagword in_fpu_config;
6829
6830 /* TODO: Revise to use object-attributes instead. */
6831 if (!nds32_check_vec_size (ibfd))
6832 return FALSE;
6833
6834 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6835 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6836 return TRUE;
6837
6838 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6839 {
4eca0228 6840 _bfd_error_handler
38f14ab8 6841 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
35c08157
KLC
6842
6843 bfd_set_error (bfd_error_bad_value);
6844 return FALSE;
6845 }
6846
fbaf61ad
NC
6847 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6848 treat as generic one without checking and merging. */
6849 if (elf_elfheader (ibfd)->e_flags)
35c08157 6850 {
fbaf61ad
NC
6851 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6852 if (in_version == E_NDS32_ELF_VER_1_2)
35c08157 6853 {
fbaf61ad
NC
6854 _bfd_error_handler
6855 (_("%pB: warning: older version of object file encountered, "
6856 "please recompile with current tool chain"), ibfd);
35c08157 6857 }
35c08157 6858
fbaf61ad
NC
6859 /* We may need to merge V1 and V2 arch object files to V2. */
6860 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6861 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
35c08157 6862 {
fbaf61ad
NC
6863 /* Need to convert version. */
6864 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6865 == E_NDS_ARCH_STAR_RESERVED)
6866 {
6867 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6868 }
6869 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6870 == E_NDS_ARCH_STAR_V3_M
6871 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6872 == E_NDS_ARCH_STAR_V3_0)
6873 {
6874 elf_elfheader (ibfd)->e_flags =
6875 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6876 | E_NDS_ARCH_STAR_V3_0;
6877 }
6878 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6879 == E_NDS_ARCH_STAR_V0_9
6880 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6881 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6882 {
6883 elf_elfheader (obfd)->e_flags =
6884 convert_e_flags (elf_elfheader (obfd)->e_flags,
6885 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6886 }
6887 else
6888 {
6889 elf_elfheader (ibfd)->e_flags =
6890 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6891 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6892 }
35c08157
KLC
6893 }
6894
fbaf61ad
NC
6895 /* Extract some flags. */
6896 in_flags = elf_elfheader (ibfd)->e_flags
6897 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6898 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6899
6900 /* The following flags need special treatment. */
6901 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6902 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6903 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6904
6905 /* Extract some flags. */
6906 out_flags = elf_elfheader (obfd)->e_flags
6907 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6908 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6909
6910 /* The following flags need special treatment. */
6911 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6912 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6913 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6914 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6915 if (!elf_flags_init (obfd))
6916 {
6917 /* If the input is the default architecture then do not
6918 bother setting the flags for the output architecture,
6919 instead allow future merges to do this. If no future
6920 merges ever set these flags then they will retain their
6921 unitialised values, which surprise surprise, correspond
6922 to the default values. */
6923 if (bfd_get_arch_info (ibfd)->the_default)
6924 return TRUE;
6925
6926 elf_flags_init (obfd) = TRUE;
6927 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
35c08157 6928
fbaf61ad
NC
6929 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6930 && bfd_get_arch_info (obfd)->the_default)
6931 {
6932 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6933 bfd_get_mach (ibfd));
6934 }
35c08157 6935
fbaf61ad
NC
6936 return TRUE;
6937 }
35c08157 6938
fbaf61ad
NC
6939 /* Check flag compatibility. */
6940 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
35c08157 6941 {
4eca0228 6942 _bfd_error_handler
fbaf61ad 6943 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
35c08157
KLC
6944 bfd_set_error (bfd_error_bad_value);
6945 return FALSE;
6946 }
35c08157 6947
fbaf61ad
NC
6948 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6949 {
6950 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6951 {
6952 _bfd_error_handler
6953 (_("%pB: error: instruction set mismatch with previous modules"),
6954 ibfd);
6955
6956 bfd_set_error (bfd_error_bad_value);
6957 return FALSE;
6958 }
6959 }
6960
6961 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6962 and perf ext1 and DIV are mergerd to perf ext1. */
6963 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6964 {
6965 elf_elfheader (obfd)->e_flags =
6966 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6967 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6968 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6969 ? E_NDS32_HAS_EXT_INST : 0)
6970 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6971 ? E_NDS32_HAS_EXT_INST : 0)
6972 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6973 | ((in_version > out_version) ? out_version : in_version);
6974 }
6975 else
6976 {
6977 if (in_version != out_version)
6978 _bfd_error_handler
6979 /* xgettext:c-format */
6980 (_("%pB: warning: incompatible elf-versions %s and %s"),
6981 ibfd, nds32_elfver_strtab[out_version],
6982 nds32_elfver_strtab[in_version]);
35c08157 6983
fbaf61ad
NC
6984 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6985 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6986 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6987 | (in_version > out_version ? out_version : in_version);
6988 }
35c08157
KLC
6989 }
6990
6991 return TRUE;
6992}
6993
6994/* Display the flags field. */
6995
6996static bfd_boolean
6997nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6998{
6999 FILE *file = (FILE *) ptr;
7000
7001 BFD_ASSERT (abfd != NULL && ptr != NULL);
7002
7003 _bfd_elf_print_private_bfd_data (abfd, ptr);
7004
7005 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
7006
7007 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
7008 {
7009 default:
7010 case E_N1_ARCH:
7011 fprintf (file, _(": n1 instructions"));
7012 break;
7013 case E_N1H_ARCH:
7014 fprintf (file, _(": n1h instructions"));
7015 break;
7016 }
7017
7018 fputc ('\n', file);
7019
7020 return TRUE;
7021}
7022
7023static unsigned int
7024nds32_elf_action_discarded (asection *sec)
7025{
7026
7027 if (strncmp
7028 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7029 return 0;
7030
7031 return _bfd_elf_default_action_discarded (sec);
7032}
7033
7034static asection *
7035nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7036 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7037 Elf_Internal_Sym *sym)
7038{
7039 if (h != NULL)
7040 switch (ELF32_R_TYPE (rel->r_info))
7041 {
7042 case R_NDS32_GNU_VTINHERIT:
7043 case R_NDS32_GNU_VTENTRY:
7044 case R_NDS32_RELA_GNU_VTINHERIT:
7045 case R_NDS32_RELA_GNU_VTENTRY:
7046 return NULL;
7047 }
7048
7049 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7050}
7051
fbaf61ad
NC
7052static enum elf_nds32_tls_type
7053get_tls_type (enum elf_nds32_reloc_type r_type,
7054 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7055{
7056 enum elf_nds32_tls_type tls_type;
7057
7058 switch (r_type)
7059 {
7060 case R_NDS32_TLS_LE_HI20:
7061 case R_NDS32_TLS_LE_LO12:
7062 tls_type = GOT_TLS_LE;
7063 break;
7064 case R_NDS32_TLS_IE_HI20:
7065 case R_NDS32_TLS_IE_LO12S2:
7066 case R_NDS32_TLS_IE_LO12:
7067 tls_type = GOT_TLS_IE;
7068 break;
7069 case R_NDS32_TLS_IEGP_HI20:
7070 case R_NDS32_TLS_IEGP_LO12:
7071 case R_NDS32_TLS_IEGP_LO12S2:
7072 tls_type = GOT_TLS_IEGP;
7073 break;
7074 case R_NDS32_TLS_DESC_HI20:
7075 case R_NDS32_TLS_DESC_LO12:
7076 case R_NDS32_TLS_DESC_ADD:
7077 case R_NDS32_TLS_DESC_FUNC:
7078 case R_NDS32_TLS_DESC_CALL:
7079 tls_type = GOT_TLS_DESC;
7080 break;
7081 default:
7082 tls_type = GOT_NORMAL;
7083 break;
7084 }
7085
7086 return tls_type;
7087}
7088
7089/* Ensure that we have allocated bookkeeping structures for ABFD's local
7090 symbols. */
7091
7092static bfd_boolean
7093elf32_nds32_allocate_local_sym_info (bfd *abfd)
7094{
7095 if (elf_local_got_refcounts (abfd) == NULL)
7096 {
7097 bfd_size_type num_syms;
7098 bfd_size_type size;
7099 char *data;
7100
7101 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7102 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7103 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7104 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7105 + sizeof (bfd_vma) + sizeof (int)
7106 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7107 data = bfd_zalloc (abfd, size);
7108 if (data == NULL)
7109 return FALSE;
7110
7111 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7112 data += num_syms * sizeof (bfd_signed_vma);
7113
7114 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7115 data += num_syms * sizeof (char);
7116
7117 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7118 data += num_syms * sizeof (bfd_vma);
7119
7120 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7121 data += num_syms * sizeof (int);
7122 }
7123
7124 return TRUE;
7125}
7126
35c08157
KLC
7127/* Look through the relocs for a section during the first phase.
7128 Since we don't do .gots or .plts, we just need to consider the
7129 virtual table relocs for gc. */
7130
7131static bfd_boolean
7132nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7133 asection *sec, const Elf_Internal_Rela *relocs)
7134{
7135 Elf_Internal_Shdr *symtab_hdr;
7136 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7137 const Elf_Internal_Rela *rel;
7138 const Elf_Internal_Rela *rel_end;
fbaf61ad 7139 struct elf_link_hash_table *ehtab;
35c08157
KLC
7140 struct elf_nds32_link_hash_table *htab;
7141 bfd *dynobj;
7142 asection *sreloc = NULL;
7143
fbaf61ad 7144 /* No need for relocation if relocatable already. */
0e1862bb 7145 if (bfd_link_relocatable (info))
fbaf61ad
NC
7146 {
7147 elf32_nds32_check_relax_group (abfd, sec);
7148 return TRUE;
7149 }
35c08157 7150
65281396
AM
7151 /* Don't do anything special with non-loaded, non-alloced sections.
7152 In particular, any relocs in such sections should not affect GOT
7153 and PLT reference counting (ie. we don't allow them to create GOT
7154 or PLT entries), there's no possibility or desire to optimize TLS
7155 relocs, and there's not much point in propagating relocs to shared
7156 libs that the dynamic linker won't relocate. */
7157 if ((sec->flags & SEC_ALLOC) == 0)
7158 return TRUE;
7159
35c08157
KLC
7160 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7161 sym_hashes = elf_sym_hashes (abfd);
7162 sym_hashes_end =
7163 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7164 if (!elf_bad_symtab (abfd))
7165 sym_hashes_end -= symtab_hdr->sh_info;
7166
fbaf61ad 7167 ehtab = elf_hash_table (info);
35c08157
KLC
7168 htab = nds32_elf_hash_table (info);
7169 dynobj = htab->root.dynobj;
7170
7171 rel_end = relocs + sec->reloc_count;
7172 for (rel = relocs; rel < rel_end; rel++)
7173 {
7174 enum elf_nds32_reloc_type r_type;
7175 struct elf_link_hash_entry *h;
7176 unsigned long r_symndx;
fbaf61ad 7177 enum elf_nds32_tls_type tls_type, old_tls_type;
35c08157
KLC
7178
7179 r_symndx = ELF32_R_SYM (rel->r_info);
7180 r_type = ELF32_R_TYPE (rel->r_info);
7181 if (r_symndx < symtab_hdr->sh_info)
7182 h = NULL;
7183 else
7184 {
7185 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7186 while (h->root.type == bfd_link_hash_indirect
7187 || h->root.type == bfd_link_hash_warning)
7188 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7189 }
7190
fbaf61ad
NC
7191 /* Create .got section if necessary.
7192 Some relocs require a global offset table. We create
7193 got section here, since these relocation need a got section
7194 and if it is not created yet. */
7195 if (ehtab->sgot == NULL)
35c08157
KLC
7196 {
7197 switch (r_type)
7198 {
7199 case R_NDS32_GOT_HI20:
7200 case R_NDS32_GOT_LO12:
7201 case R_NDS32_GOT_LO15:
7202 case R_NDS32_GOT_LO19:
7203 case R_NDS32_GOT17S2_RELA:
7204 case R_NDS32_GOT15S2_RELA:
7205 case R_NDS32_GOTOFF:
7206 case R_NDS32_GOTOFF_HI20:
7207 case R_NDS32_GOTOFF_LO12:
7208 case R_NDS32_GOTOFF_LO15:
7209 case R_NDS32_GOTOFF_LO19:
7210 case R_NDS32_GOTPC20:
7211 case R_NDS32_GOTPC_HI20:
7212 case R_NDS32_GOTPC_LO12:
7213 case R_NDS32_GOT20:
1c8f6a4d 7214 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7215 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7216 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7217 case R_NDS32_TLS_IEGP_HI20:
7218 case R_NDS32_TLS_IEGP_LO12:
7219 case R_NDS32_TLS_IEGP_LO12S2:
7220 case R_NDS32_TLS_DESC_HI20:
7221 case R_NDS32_TLS_DESC_LO12:
35c08157
KLC
7222 if (dynobj == NULL)
7223 htab->root.dynobj = dynobj = abfd;
fbaf61ad 7224 if (!create_got_section (dynobj, info))
35c08157
KLC
7225 return FALSE;
7226 break;
7227
7228 default:
7229 break;
7230 }
7231 }
7232
fbaf61ad 7233 /* Check relocation type. */
35c08157
KLC
7234 switch ((int) r_type)
7235 {
7236 case R_NDS32_GOT_HI20:
7237 case R_NDS32_GOT_LO12:
7238 case R_NDS32_GOT_LO15:
7239 case R_NDS32_GOT_LO19:
7240 case R_NDS32_GOT20:
fbaf61ad
NC
7241 case R_NDS32_TLS_LE_HI20:
7242 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 7243 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7244 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7245 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7246 case R_NDS32_TLS_IEGP_HI20:
7247 case R_NDS32_TLS_IEGP_LO12:
7248 case R_NDS32_TLS_IEGP_LO12S2:
7249 case R_NDS32_TLS_DESC_HI20:
7250 case R_NDS32_TLS_DESC_LO12:
7251 tls_type = get_tls_type (r_type, h);
7252 if (h)
1c8f6a4d 7253 {
fbaf61ad
NC
7254 if (tls_type != GOT_TLS_LE)
7255 h->got.refcount += 1;
1c8f6a4d 7256 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
1c8f6a4d 7257 }
35c08157
KLC
7258 else
7259 {
fbaf61ad
NC
7260 /* This is a global offset table entry for a local symbol. */
7261 if (!elf32_nds32_allocate_local_sym_info (abfd))
7262 return FALSE;
35c08157 7263
fbaf61ad
NC
7264 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7265 if (tls_type != GOT_TLS_LE)
7266 elf_local_got_refcounts (abfd)[r_symndx] += 1;
1c8f6a4d 7267 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
35c08157 7268 }
35c08157 7269
fbaf61ad 7270 /* We would already have issued an error message if there
1c8f6a4d
KLC
7271 is a TLS/non-TLS mismatch, based on the symbol
7272 type. So just combine any TLS types needed. */
7273 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7274 && tls_type != GOT_NORMAL)
7275 tls_type |= old_tls_type;
7276
fbaf61ad
NC
7277 /* DESC to IE/IEGP if link to executable. */
7278 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7279 && (bfd_link_executable (info)))
7280 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7281
1c8f6a4d
KLC
7282 if (old_tls_type != tls_type)
7283 {
7284 if (h != NULL)
7285 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7286 else
7287 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7288 }
7289 break;
7290 case R_NDS32_9_PLTREL:
35c08157
KLC
7291 case R_NDS32_25_PLTREL:
7292 case R_NDS32_PLTREL_HI20:
7293 case R_NDS32_PLTREL_LO12:
7294 case R_NDS32_PLT_GOTREL_HI20:
7295 case R_NDS32_PLT_GOTREL_LO12:
7296 case R_NDS32_PLT_GOTREL_LO15:
7297 case R_NDS32_PLT_GOTREL_LO19:
7298 case R_NDS32_PLT_GOTREL_LO20:
7299
7300 /* This symbol requires a procedure linkage table entry. We
7301 actually build the entry in adjust_dynamic_symbol,
7302 because this might be a case of linking PIC code without
7303 linking in any dynamic objects, in which case we don't
7304 need to generate a procedure linkage table after all. */
7305
7306 /* If this is a local symbol, we resolve it directly without
7307 creating a procedure linkage table entry. */
7308 if (h == NULL)
7309 continue;
7310
fbaf61ad
NC
7311 if (h->forced_local
7312 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
7313 break;
7314
1c8f6a4d 7315 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
35c08157
KLC
7316 h->needs_plt = 1;
7317 h->plt.refcount += 1;
7318 break;
7319
7320 case R_NDS32_16_RELA:
7321 case R_NDS32_20_RELA:
7322 case R_NDS32_5_RELA:
7323 case R_NDS32_32_RELA:
7324 case R_NDS32_HI20_RELA:
7325 case R_NDS32_LO12S3_RELA:
7326 case R_NDS32_LO12S2_RELA:
7327 case R_NDS32_LO12S2_DP_RELA:
7328 case R_NDS32_LO12S2_SP_RELA:
7329 case R_NDS32_LO12S1_RELA:
7330 case R_NDS32_LO12S0_RELA:
7331 case R_NDS32_LO12S0_ORI_RELA:
7332 case R_NDS32_SDA16S3_RELA:
7333 case R_NDS32_SDA17S2_RELA:
7334 case R_NDS32_SDA18S1_RELA:
7335 case R_NDS32_SDA19S0_RELA:
7336 case R_NDS32_SDA15S3_RELA:
7337 case R_NDS32_SDA15S2_RELA:
7338 case R_NDS32_SDA12S2_DP_RELA:
7339 case R_NDS32_SDA12S2_SP_RELA:
7340 case R_NDS32_SDA15S1_RELA:
7341 case R_NDS32_SDA15S0_RELA:
7342 case R_NDS32_SDA_FP7U2_RELA:
7343 case R_NDS32_15_PCREL_RELA:
7344 case R_NDS32_17_PCREL_RELA:
7345 case R_NDS32_25_PCREL_RELA:
7346
0e1862bb 7347 if (h != NULL && !bfd_link_pic (info))
35c08157
KLC
7348 {
7349 h->non_got_ref = 1;
7350 h->plt.refcount += 1;
7351 }
7352
7353 /* If we are creating a shared library, and this is a reloc against
7354 a global symbol, or a non PC relative reloc against a local
7355 symbol, then we need to copy the reloc into the shared library.
7356 However, if we are linking with -Bsymbolic, we do not need to
7357 copy a reloc against a global symbol which is defined in an
7358 object we are including in the link (i.e., DEF_REGULAR is set).
7359 At this point we have not seen all the input files, so it is
7360 possible that DEF_REGULAR is not set now but will be set later
7361 (it is never cleared). We account for that possibility below by
7362 storing information in the dyn_relocs field of the hash table
7363 entry. A similar situation occurs when creating shared libraries
7364 and symbol visibility changes render the symbol local.
7365
7366 If on the other hand, we are creating an executable, we may need
7367 to keep relocations for symbols satisfied by a dynamic library
7368 if we manage to avoid copy relocs for the symbol. */
0e1862bb 7369 if ((bfd_link_pic (info)
35c08157
KLC
7370 && (sec->flags & SEC_ALLOC) != 0
7371 && ((r_type != R_NDS32_25_PCREL_RELA
7372 && r_type != R_NDS32_15_PCREL_RELA
7373 && r_type != R_NDS32_17_PCREL_RELA
7374 && !(r_type == R_NDS32_32_RELA
7375 && strcmp (sec->name, ".eh_frame") == 0))
7376 || (h != NULL
7377 && (!info->symbolic
7378 || h->root.type == bfd_link_hash_defweak
7379 || !h->def_regular))))
0e1862bb 7380 || (!bfd_link_pic (info)
35c08157
KLC
7381 && (sec->flags & SEC_ALLOC) != 0
7382 && h != NULL
7383 && (h->root.type == bfd_link_hash_defweak
7384 || !h->def_regular)))
7385 {
3bf083ed
AM
7386 struct elf_dyn_relocs *p;
7387 struct elf_dyn_relocs **head;
35c08157
KLC
7388
7389 if (dynobj == NULL)
7390 htab->root.dynobj = dynobj = abfd;
7391
7392 /* When creating a shared object, we must copy these
7393 relocs into the output file. We create a reloc
7394 section in dynobj and make room for the reloc. */
7395 if (sreloc == NULL)
7396 {
7397 const char *name;
7398
7399 name = bfd_elf_string_from_elf_section
7400 (abfd, elf_elfheader (abfd)->e_shstrndx,
7401 elf_section_data (sec)->rela.hdr->sh_name);
7402 if (name == NULL)
7403 return FALSE;
7404
7405 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
7406 && strcmp (bfd_get_section_name (abfd, sec),
7407 name + 5) == 0);
7408
7409 sreloc = bfd_get_section_by_name (dynobj, name);
7410 if (sreloc == NULL)
7411 {
7412 flagword flags;
7413
7414 sreloc = bfd_make_section (dynobj, name);
7415 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7416 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7417 if ((sec->flags & SEC_ALLOC) != 0)
7418 flags |= SEC_ALLOC | SEC_LOAD;
7419 if (sreloc == NULL
7420 || !bfd_set_section_flags (dynobj, sreloc, flags)
7421 || !bfd_set_section_alignment (dynobj, sreloc, 2))
7422 return FALSE;
7423
7424 elf_section_type (sreloc) = SHT_RELA;
7425 }
7426 elf_section_data (sec)->sreloc = sreloc;
7427 }
7428
7429 /* If this is a global symbol, we count the number of
7430 relocations we need for this symbol. */
7431 if (h != NULL)
7432 head = &((struct elf_nds32_link_hash_entry *) h)->dyn_relocs;
7433 else
7434 {
7435 asection *s;
61034b0b 7436 void *vpp;
35c08157
KLC
7437
7438 Elf_Internal_Sym *isym;
7439 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7440 if (isym == NULL)
7441 return FALSE;
7442
7443 /* Track dynamic relocs needed for local syms too. */
7444 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7445 if (s == NULL)
7446 return FALSE;
7447
61034b0b 7448 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 7449 head = (struct elf_dyn_relocs **) vpp;
35c08157
KLC
7450 }
7451
7452 p = *head;
7453 if (p == NULL || p->sec != sec)
7454 {
7455 bfd_size_type amt = sizeof (*p);
3bf083ed 7456 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
35c08157
KLC
7457 if (p == NULL)
7458 return FALSE;
7459 p->next = *head;
7460 *head = p;
7461 p->sec = sec;
7462 p->count = 0;
7463 p->pc_count = 0;
7464 }
7465
7466 p->count += 1;
fbaf61ad
NC
7467
7468 /* Since eh_frame is readonly, R_NDS32_32_RELA
7469 reloc for eh_frame will cause shared library has
7470 TEXTREL entry in the dynamic section. This lead glibc
7471 testsuites to failure (bug-13092) and cause kernel fail
7472 (bug-11819). I think the best solution is to replace
7473 absolute reloc with pc relative reloc in the eh_frame.
7474 To do that, we need to support the following issues:
7475
7476 === For GCC ===
7477 * gcc/config/nds32/nds32.h: Define
7478 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7479 and DW_EH_PE_sdata4 into DWARF exception header when
7480 option have '-fpic'.
7481
7482 === For binutils ===
7483 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7484 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7485 may break our nds DIFF mechanism, therefore, we
7486 must disable all linker relaxations to ensure
7487 correctness.
7488 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7489 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7490 do the necessary modification.
7491
7492 Unfortunately, it still have some problems for nds32
7493 to support pc relative reloc in the eh_frame. So I use
7494 another solution to fix this issue.
7495
7496 However, I find that ld always emit TEXTREL marker for
7497 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7498 correspond to R_NDS32_32_RELA for .eh_frame section.
7499 It means that we always reserve redundant entries of rel.dyn
7500 for these relocs which actually do nothing in dynamic linker.
7501
7502 Therefore, we regard these relocs as pc relative relocs
7503 here and increase the pc_count. */
35c08157
KLC
7504 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7505 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
fbaf61ad
NC
7506 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7507 || (r_type == R_NDS32_32_RELA
7508 && strcmp (sec->name, ".eh_frame") == 0))
35c08157
KLC
7509 p->pc_count += 1;
7510 }
7511 break;
7512
7513 /* This relocation describes the C++ object vtable hierarchy.
7514 Reconstruct it for later use during GC. */
7515 case R_NDS32_RELA_GNU_VTINHERIT:
7516 case R_NDS32_GNU_VTINHERIT:
7517 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7518 return FALSE;
7519 break;
7520
7521 /* This relocation describes which C++ vtable entries are actually
7522 used. Record for later use during GC. */
7523 case R_NDS32_GNU_VTENTRY:
7524 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7525 return FALSE;
7526 break;
7527 case R_NDS32_RELA_GNU_VTENTRY:
7528 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7529 return FALSE;
7530 break;
7531 }
7532 }
7533
7534 return TRUE;
7535}
7536
7537/* Write VAL in uleb128 format to P, returning a pointer to the
7538 following byte.
7539 This code is copied from elf-attr.c. */
7540
7541static bfd_byte *
7542write_uleb128 (bfd_byte *p, unsigned int val)
7543{
7544 bfd_byte c;
7545 do
7546 {
7547 c = val & 0x7f;
7548 val >>= 7;
7549 if (val)
7550 c |= 0x80;
7551 *(p++) = c;
7552 }
7553 while (val);
7554 return p;
7555}
7556
7557static bfd_signed_vma
7558calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
fbaf61ad 7559 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
35c08157
KLC
7560{
7561 bfd_signed_vma foff;
7562 bfd_vma symval, addend;
7563 asection *sym_sec;
7564
7565 /* Get the value of the symbol referred to by the reloc. */
7566 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7567 {
7568 Elf_Internal_Sym *isym;
7569
7570 /* A local symbol. */
7571 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7572
7573 if (isym->st_shndx == SHN_UNDEF)
7574 sym_sec = bfd_und_section_ptr;
7575 else if (isym->st_shndx == SHN_ABS)
7576 sym_sec = bfd_abs_section_ptr;
7577 else if (isym->st_shndx == SHN_COMMON)
7578 sym_sec = bfd_com_section_ptr;
7579 else
7580 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7581 symval = isym->st_value + sym_sec->output_section->vma
7582 + sym_sec->output_offset;
7583 }
7584 else
7585 {
7586 unsigned long indx;
7587 struct elf_link_hash_entry *h;
35c08157
KLC
7588
7589 /* An external symbol. */
7590 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7591 h = elf_sym_hashes (abfd)[indx];
7592 BFD_ASSERT (h != NULL);
7593
7594 if (h->root.type != bfd_link_hash_defined
7595 && h->root.type != bfd_link_hash_defweak)
7596 /* This appears to be a reference to an undefined
7597 symbol. Just ignore it--it will be caught by the
7598 regular reloc processing. */
7599 return 0;
35c08157
KLC
7600
7601 if (h->root.u.def.section->flags & SEC_MERGE)
7602 {
7603 sym_sec = h->root.u.def.section;
7604 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7605 elf_section_data (sym_sec)->sec_info,
7606 h->root.u.def.value);
7607 symval = symval + sym_sec->output_section->vma
7608 + sym_sec->output_offset;
7609 }
7610 else
7611 symval = (h->root.u.def.value
7612 + h->root.u.def.section->output_section->vma
7613 + h->root.u.def.section->output_offset);
7614 }
7615
7616 addend = irel->r_addend;
7617
7618 foff = (symval + addend
7619 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7620 return foff;
7621}
35c08157 7622\f
fbaf61ad 7623
35c08157
KLC
7624/* Convert a 32-bit instruction to 16-bit one.
7625 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7626 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7627 type of INSN16. Return 1 if successful. */
7628
7629static int
7630nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7631 int *pinsn_type)
7632{
7633 uint16_t insn16 = 0;
6cae483a 7634 int insn_type = 0;
35c08157
KLC
7635 unsigned long mach = bfd_get_mach (abfd);
7636
7637 if (N32_SH5 (insn) != 0)
7638 return 0;
7639
7640 switch (N32_SUB5 (insn))
7641 {
7642 case N32_ALU1_ADD_SLLI:
7643 case N32_ALU1_ADD_SRLI:
7644 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7645 {
7646 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7647 N32_RB5 (insn));
7648 insn_type = NDS32_INSN_ADD333;
7649 }
7650 else if (N32_IS_RT4 (insn))
7651 {
7652 if (N32_RT5 (insn) == N32_RA5 (insn))
7653 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7654 else if (N32_RT5 (insn) == N32_RB5 (insn))
7655 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7656 insn_type = NDS32_INSN_ADD45;
7657 }
7658 break;
7659
7660 case N32_ALU1_SUB_SLLI:
7661 case N32_ALU1_SUB_SRLI:
7662 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7663 {
7664 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7665 N32_RB5 (insn));
7666 insn_type = NDS32_INSN_SUB333;
7667 }
7668 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7669 {
7670 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7671 insn_type = NDS32_INSN_SUB45;
7672 }
7673 break;
7674
7675 case N32_ALU1_AND_SLLI:
7676 case N32_ALU1_AND_SRLI:
7677 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7678 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7679 && N32_IS_RB3 (insn))
7680 {
7681 if (N32_RT5 (insn) == N32_RA5 (insn))
7682 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7683 else if (N32_RT5 (insn) == N32_RB5 (insn))
7684 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7685 if (insn16)
7686 insn_type = NDS32_INSN_AND33;
7687 }
7688 break;
7689
7690 case N32_ALU1_XOR_SLLI:
7691 case N32_ALU1_XOR_SRLI:
7692 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7693 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7694 && N32_IS_RB3 (insn))
7695 {
7696 if (N32_RT5 (insn) == N32_RA5 (insn))
7697 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7698 else if (N32_RT5 (insn) == N32_RB5 (insn))
7699 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7700 if (insn16)
7701 insn_type = NDS32_INSN_XOR33;
7702 }
7703 break;
7704
7705 case N32_ALU1_OR_SLLI:
7706 case N32_ALU1_OR_SRLI:
7707 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7708 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7709 && N32_IS_RB3 (insn))
7710 {
7711 if (N32_RT5 (insn) == N32_RA5 (insn))
7712 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7713 else if (N32_RT5 (insn) == N32_RB5 (insn))
7714 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7715 if (insn16)
7716 insn_type = NDS32_INSN_OR33;
7717 }
7718 break;
7719 case N32_ALU1_NOR:
7720 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7721 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7722 && N32_RA5 (insn) == N32_RB5 (insn))
7723 {
7724 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7725 insn_type = NDS32_INSN_NOT33;
7726 }
7727 break;
7728 case N32_ALU1_SRAI:
7729 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7730 {
7731 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7732 insn_type = NDS32_INSN_SRAI45;
7733 }
7734 break;
7735
7736 case N32_ALU1_SRLI:
7737 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7738 {
7739 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7740 insn_type = NDS32_INSN_SRLI45;
7741 }
7742 break;
7743
7744 case N32_ALU1_SLLI:
7745 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7746 {
7747 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7748 N32_UB5 (insn));
7749 insn_type = NDS32_INSN_SLLI333;
7750 }
7751 break;
7752
7753 case N32_ALU1_ZEH:
7754 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7755 {
7756 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7757 insn_type = NDS32_INSN_ZEH33;
7758 }
7759 break;
7760
7761 case N32_ALU1_SEB:
7762 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7763 {
7764 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7765 insn_type = NDS32_INSN_SEB33;
7766 }
7767 break;
7768
7769 case N32_ALU1_SEH:
7770 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7771 {
7772 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7773 insn_type = NDS32_INSN_SEH33;
7774 }
7775 break;
7776
7777 case N32_ALU1_SLT:
7778 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7779 {
7780 /* Implicit r15. */
7781 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7782 insn_type = NDS32_INSN_SLT45;
7783 }
7784 break;
7785
7786 case N32_ALU1_SLTS:
7787 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7788 {
7789 /* Implicit r15. */
7790 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7791 insn_type = NDS32_INSN_SLTS45;
7792 }
7793 break;
7794 }
7795
7796 if ((insn16 & 0x8000) == 0)
7797 return 0;
7798
7799 if (pinsn16)
7800 *pinsn16 = insn16;
7801 if (pinsn_type)
7802 *pinsn_type = insn_type;
7803 return 1;
7804}
7805
7806static int
7807nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7808 int *pinsn_type)
7809{
7810 uint16_t insn16 = 0;
7811 int insn_type;
7812 unsigned long mach = bfd_get_mach (abfd);
7813
7814 /* TODO: bset, bclr, btgl, btst. */
7815 if (__GF (insn, 6, 4) != 0)
7816 return 0;
7817
7818 switch (N32_IMMU (insn, 6))
7819 {
7820 case N32_ALU2_MUL:
7821 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7822 && N32_IS_RB3 (insn))
7823 {
7824 if (N32_RT5 (insn) == N32_RA5 (insn))
7825 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7826 else if (N32_RT5 (insn) == N32_RB5 (insn))
7827 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7828 if (insn16)
7829 insn_type = NDS32_INSN_MUL33;
7830 }
7831 }
7832
7833 if ((insn16 & 0x8000) == 0)
7834 return 0;
7835
7836 if (pinsn16)
7837 *pinsn16 = insn16;
7838 if (pinsn_type)
7839 *pinsn_type = insn_type;
7840 return 1;
7841}
7842
7843int
7844nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7845 int *pinsn_type)
7846{
7847 int op6;
7848 uint16_t insn16 = 0;
1624c9ca 7849 int insn_type = 0;
35c08157
KLC
7850 unsigned long mach = bfd_get_mach (abfd);
7851
7852 /* Decode 32-bit instruction. */
7853 if (insn & 0x80000000)
7854 {
7855 /* Not 32-bit insn. */
7856 return 0;
7857 }
7858
7859 op6 = N32_OP6 (insn);
7860
7861 /* Convert it to 16-bit instruction. */
7862 switch (op6)
7863 {
7864 case N32_OP6_MOVI:
7865 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7866 {
7867 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7868 insn_type = NDS32_INSN_MOVI55;
7869 }
7870 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7871 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7872 {
7873 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7874 N32_IMM20S (insn) - 16);
7875 insn_type = NDS32_INSN_MOVPI45;
7876 }
7877 break;
7878
7879 case N32_OP6_ADDI:
7880 if (N32_IMM15S (insn) == 0)
7881 {
7882 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7883 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7884 if (mach <= MACH_V2
7885 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7886 {
7887 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7888 insn_type = NDS32_INSN_MOV55;
7889 }
7890 }
7891 else if (N32_IMM15S (insn) > 0)
7892 {
7893 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7894 {
7895 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7896 N32_IMM15S (insn));
7897 insn_type = NDS32_INSN_ADDI333;
7898 }
7899 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7900 && N32_IMM15S (insn) < 32)
7901 {
7902 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7903 insn_type = NDS32_INSN_ADDI45;
7904 }
7905 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7906 && N32_RT5 (insn) == N32_RA5 (insn)
7907 && N32_IMM15S (insn) < 512)
7908 {
7909 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7910 insn_type = NDS32_INSN_ADDI10_SP;
7911 }
7912 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7913 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7914 && (N32_IMM15S (insn) % 4 == 0))
7915 {
7916 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7917 N32_IMM15S (insn) >> 2);
7918 insn_type = NDS32_INSN_ADDRI36_SP;
7919 }
7920 }
7921 else
7922 {
7923 /* Less than 0. */
7924 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7925 {
7926 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7927 0 - N32_IMM15S (insn));
7928 insn_type = NDS32_INSN_SUBI333;
7929 }
7930 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7931 && N32_IMM15S (insn) > -32)
7932 {
1c8f6a4d
KLC
7933 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7934 0 - N32_IMM15S (insn));
35c08157
KLC
7935 insn_type = NDS32_INSN_SUBI45;
7936 }
7937 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7938 && N32_RT5 (insn) == N32_RA5 (insn)
7939 && N32_IMM15S (insn) >= -512)
7940 {
7941 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7942 insn_type = NDS32_INSN_ADDI10_SP;
7943 }
7944 }
7945 break;
7946
7947 case N32_OP6_ORI:
7948 if (N32_IMM15S (insn) == 0)
7949 {
7950 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7951 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7952 if (mach <= MACH_V2
7953 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7954 {
7955 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7956 insn_type = NDS32_INSN_MOV55;
7957 }
7958 }
7959 break;
7960
7961 case N32_OP6_SUBRI:
7962 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7963 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7964 {
7965 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7966 insn_type = NDS32_INSN_NEG33;
7967 }
7968 break;
7969
7970 case N32_OP6_ANDI:
7971 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7972 {
7973 if (N32_IMM15U (insn) == 1)
7974 {
7975 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7976 insn_type = NDS32_INSN_XLSB33;
7977 }
7978 else if (N32_IMM15U (insn) == 0x7ff)
7979 {
7980 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7981 insn_type = NDS32_INSN_X11B33;
7982 }
7983 else if (N32_IMM15U (insn) == 0xff)
7984 {
7985 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7986 insn_type = NDS32_INSN_ZEB33;
7987 }
7988 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7989 && N32_IMM15U (insn) < 256)
7990 {
7991 int imm15u = N32_IMM15U (insn);
7992
7993 if (__builtin_popcount (imm15u) == 1)
7994 {
7995 /* BMSKI33 */
7996 int imm3u = __builtin_ctz (imm15u);
7997
7998 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7999 insn_type = NDS32_INSN_BMSKI33;
8000 }
8001 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
8002 {
8003 /* FEXTI33 */
8004 int imm3u = __builtin_ctz (imm15u + 1) - 1;
8005
8006 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
8007 insn_type = NDS32_INSN_FEXTI33;
8008 }
8009 }
8010 }
8011 break;
8012
8013 case N32_OP6_SLTI:
8014 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8015 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8016 {
8017 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8018 insn_type = NDS32_INSN_SLTI45;
8019 }
8020 break;
8021
8022 case N32_OP6_SLTSI:
8023 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8024 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8025 {
8026 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8027 insn_type = NDS32_INSN_SLTSI45;
8028 }
8029 break;
8030
8031 case N32_OP6_LWI:
8032 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8033 {
8034 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8035 insn_type = NDS32_INSN_LWI450;
8036 }
8037 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8038 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8039 {
8040 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8041 N32_IMM15S (insn));
8042 insn_type = NDS32_INSN_LWI333;
8043 }
8044 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8045 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8046 {
8047 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8048 insn_type = NDS32_INSN_LWI37;
8049 }
8050 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8051 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8052 {
8053 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8054 insn_type = NDS32_INSN_LWI37_SP;
8055 }
8056 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8057 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8058 {
1c8f6a4d
KLC
8059 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8060 N32_IMM15S (insn) + 32);
35c08157
KLC
8061 insn_type = NDS32_INSN_LWI45_FE;
8062 }
8063 break;
8064
8065 case N32_OP6_SWI:
8066 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8067 {
8068 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8069 insn_type = NDS32_INSN_SWI450;
8070 }
8071 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8072 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8073 {
1c8f6a4d
KLC
8074 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8075 N32_IMM15S (insn));
35c08157
KLC
8076 insn_type = NDS32_INSN_SWI333;
8077 }
8078 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8079 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8080 {
8081 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8082 insn_type = NDS32_INSN_SWI37;
8083 }
8084 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8085 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8086 {
8087 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8088 insn_type = NDS32_INSN_SWI37_SP;
8089 }
8090 break;
8091
8092 case N32_OP6_LWI_BI:
8093 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8094 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8095 {
8096 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8097 N32_IMM15S (insn));
8098 insn_type = NDS32_INSN_LWI333_BI;
8099 }
8100 break;
8101
8102 case N32_OP6_SWI_BI:
8103 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8104 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8105 {
8106 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8107 N32_IMM15S (insn));
8108 insn_type = NDS32_INSN_SWI333_BI;
8109 }
8110 break;
8111
8112 case N32_OP6_LHI:
8113 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8114 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8115 {
8116 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8117 N32_IMM15S (insn));
8118 insn_type = NDS32_INSN_LHI333;
8119 }
8120 break;
8121
8122 case N32_OP6_SHI:
8123 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8124 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8125 {
8126 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8127 N32_IMM15S (insn));
8128 insn_type = NDS32_INSN_SHI333;
8129 }
8130 break;
8131
8132 case N32_OP6_LBI:
8133 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8134 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8135 {
8136 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8137 N32_IMM15S (insn));
8138 insn_type = NDS32_INSN_LBI333;
8139 }
8140 break;
8141
8142 case N32_OP6_SBI:
8143 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8144 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8145 {
8146 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8147 N32_IMM15S (insn));
8148 insn_type = NDS32_INSN_SBI333;
8149 }
8150 break;
8151
8152 case N32_OP6_ALU1:
8153 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8154
8155 case N32_OP6_ALU2:
8156 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8157
8158 case N32_OP6_BR1:
8159 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8160 goto done;
8161
4ec521f2 8162 if ((insn & N32_BIT (14)) == 0)
35c08157
KLC
8163 {
8164 /* N32_BR1_BEQ */
8165 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8166 && N32_RT5 (insn) != REG_R5)
8167 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8168 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8169 && N32_RA5 (insn) != REG_R5)
8170 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8171 insn_type = NDS32_INSN_BEQS38;
8172 break;
8173 }
8174 else
8175 {
8176 /* N32_BR1_BNE */
8177 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8178 && N32_RT5 (insn) != REG_R5)
8179 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8180 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8181 && N32_RA5 (insn) != REG_R5)
8182 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8183 insn_type = NDS32_INSN_BNES38;
8184 break;
8185 }
8186 break;
8187
8188 case N32_OP6_BR2:
8189 switch (N32_BR2_SUB (insn))
8190 {
8191 case N32_BR2_BEQZ:
8192 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8193 {
8194 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8195 insn_type = NDS32_INSN_BEQZ38;
8196 }
1c8f6a4d
KLC
8197 else if (N32_RT5 (insn) == REG_R15
8198 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8199 {
8200 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8201 insn_type = NDS32_INSN_BEQZS8;
8202 }
8203 break;
8204
8205 case N32_BR2_BNEZ:
8206 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8207 {
8208 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8209 insn_type = NDS32_INSN_BNEZ38;
8210 }
1c8f6a4d
KLC
8211 else if (N32_RT5 (insn) == REG_R15
8212 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8213 {
8214 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8215 insn_type = NDS32_INSN_BNEZS8;
8216 }
8217 break;
8218
fbaf61ad
NC
8219 case N32_BR2_SOP0:
8220 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
35c08157
KLC
8221 {
8222 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8223 insn_type = NDS32_INSN_IFCALL9;
8224 }
8225 break;
8226 }
8227 break;
8228
8229 case N32_OP6_JI:
4ec521f2 8230 if ((insn & N32_BIT (24)) == 0)
35c08157
KLC
8231 {
8232 /* N32_JI_J */
8233 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8234 {
8235 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8236 insn_type = NDS32_INSN_J8;
8237 }
8238 }
8239 break;
8240
8241 case N32_OP6_JREG:
8242 if (__GF (insn, 8, 2) != 0)
8243 goto done;
8244
8245 switch (N32_IMMU (insn, 5))
8246 {
8247 case N32_JREG_JR:
8248 if (N32_JREG_HINT (insn) == 0)
8249 {
8250 /* jr */
8251 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8252 insn_type = NDS32_INSN_JR5;
8253 }
8254 else if (N32_JREG_HINT (insn) == 1)
8255 {
8256 /* ret */
8257 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8258 insn_type = NDS32_INSN_RET5;
8259 }
8260 else if (N32_JREG_HINT (insn) == 3)
8261 {
8262 /* ifret = mov55 $sp, $sp */
8263 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8264 insn_type = NDS32_INSN_IFRET;
8265 }
8266 break;
8267
8268 case N32_JREG_JRAL:
8269 /* It's convertible when return rt5 is $lp and address
8270 translation is kept. */
8271 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8272 {
8273 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8274 insn_type = NDS32_INSN_JRAL5;
8275 }
8276 break;
8277 }
8278 break;
8279
8280 case N32_OP6_MISC:
8281 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8282 {
8283 /* For v3, swid above 31 are used for ex9.it. */
8284 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8285 insn_type = NDS32_INSN_BREAK16;
8286 }
8287 break;
8288
8289 default:
8290 /* This instruction has no 16-bit variant. */
8291 goto done;
8292 }
8293
8294done:
8295 /* Bit-15 of insn16 should be set for a valid instruction. */
8296 if ((insn16 & 0x8000) == 0)
8297 return 0;
8298
8299 if (pinsn16)
8300 *pinsn16 = insn16;
8301 if (pinsn_type)
8302 *pinsn_type = insn_type;
8303 return 1;
8304}
8305
8306static int
8307special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8308 Elf_Internal_Rela *reloc)
8309{
8310 uint16_t insn16 = 0;
8311
8312 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8313 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8314 return 0;
8315
8316 if (!N32_IS_RT3 (insn))
8317 return 0;
8318
8319 switch (N32_OP6 (insn))
8320 {
8321 case N32_OP6_LWI:
8322 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8323 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8324 break;
8325 case N32_OP6_SWI:
8326 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8327 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8328 break;
8329 case N32_OP6_HWGP:
8330 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8331 break;
8332
8333 if (__GF (insn, 17, 3) == 6)
8334 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8335 else if (__GF (insn, 17, 3) == 7)
8336 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8337 break;
8338 }
8339
8340 if ((insn16 & 0x8000) == 0)
8341 return 0;
8342
8343 *pinsn16 = insn16;
8344 return 1;
8345}
8346
8347/* Convert a 16-bit instruction to 32-bit one.
8348 INSN16 it the input and PINSN it the point to output.
8349 Return non-zero on successful. Otherwise 0 is returned. */
8350
8351int
8352nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8353{
8354 uint32_t insn = 0xffffffff;
8355 unsigned long mach = bfd_get_mach (abfd);
8356
8357 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8358
8359 switch (__GF (insn16, 9, 6))
8360 {
8361 case 0x4: /* add45 */
1c8f6a4d
KLC
8362 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8363 N16_RA5 (insn16));
35c08157
KLC
8364 goto done;
8365 case 0x5: /* sub45 */
1c8f6a4d
KLC
8366 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8367 N16_RA5 (insn16));
35c08157
KLC
8368 goto done;
8369 case 0x6: /* addi45 */
1c8f6a4d
KLC
8370 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8371 N16_IMM5U (insn16));
35c08157
KLC
8372 goto done;
8373 case 0x7: /* subi45 */
1c8f6a4d
KLC
8374 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8375 -N16_IMM5U (insn16));
35c08157
KLC
8376 goto done;
8377 case 0x8: /* srai45 */
1c8f6a4d
KLC
8378 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8379 N16_IMM5U (insn16));
35c08157
KLC
8380 goto done;
8381 case 0x9: /* srli45 */
1c8f6a4d
KLC
8382 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8383 N16_IMM5U (insn16));
35c08157 8384 goto done;
35c08157 8385 case 0xa: /* slli333 */
1c8f6a4d
KLC
8386 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8387 N16_IMM3U (insn16));
35c08157
KLC
8388 goto done;
8389 case 0xc: /* add333 */
1c8f6a4d
KLC
8390 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8391 N16_RB3 (insn16));
35c08157
KLC
8392 goto done;
8393 case 0xd: /* sub333 */
1c8f6a4d
KLC
8394 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8395 N16_RB3 (insn16));
35c08157
KLC
8396 goto done;
8397 case 0xe: /* addi333 */
1c8f6a4d
KLC
8398 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8399 N16_IMM3U (insn16));
35c08157
KLC
8400 goto done;
8401 case 0xf: /* subi333 */
1c8f6a4d
KLC
8402 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8403 -N16_IMM3U (insn16));
35c08157 8404 goto done;
35c08157 8405 case 0x10: /* lwi333 */
1c8f6a4d
KLC
8406 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8407 N16_IMM3U (insn16));
35c08157
KLC
8408 goto done;
8409 case 0x12: /* lhi333 */
1c8f6a4d
KLC
8410 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8411 N16_IMM3U (insn16));
35c08157
KLC
8412 goto done;
8413 case 0x13: /* lbi333 */
1c8f6a4d
KLC
8414 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8415 N16_IMM3U (insn16));
35c08157
KLC
8416 goto done;
8417 case 0x11: /* lwi333.bi */
1c8f6a4d
KLC
8418 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8419 N16_IMM3U (insn16));
35c08157
KLC
8420 goto done;
8421 case 0x14: /* swi333 */
1c8f6a4d
KLC
8422 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8423 N16_IMM3U (insn16));
35c08157
KLC
8424 goto done;
8425 case 0x16: /* shi333 */
1c8f6a4d
KLC
8426 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8427 N16_IMM3U (insn16));
35c08157
KLC
8428 goto done;
8429 case 0x17: /* sbi333 */
1c8f6a4d
KLC
8430 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8431 N16_IMM3U (insn16));
35c08157
KLC
8432 goto done;
8433 case 0x15: /* swi333.bi */
1c8f6a4d
KLC
8434 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8435 N16_IMM3U (insn16));
35c08157 8436 goto done;
35c08157 8437 case 0x18: /* addri36.sp */
1c8f6a4d
KLC
8438 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8439 N16_IMM6U (insn16) << 2);
35c08157 8440 goto done;
35c08157 8441 case 0x19: /* lwi45.fe */
1c8f6a4d
KLC
8442 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8443 (N16_IMM5U (insn16) - 32));
35c08157
KLC
8444 goto done;
8445 case 0x1a: /* lwi450 */
8446 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8447 goto done;
8448 case 0x1b: /* swi450 */
8449 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8450 goto done;
8451
1c8f6a4d 8452 /* These are r15 implied instructions. */
35c08157
KLC
8453 case 0x30: /* slts45 */
8454 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8455 goto done;
8456 case 0x31: /* slt45 */
8457 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8458 goto done;
8459 case 0x32: /* sltsi45 */
8460 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8461 goto done;
8462 case 0x33: /* slti45 */
8463 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8464 goto done;
8465 case 0x34: /* beqzs8, bnezs8 */
4ec521f2 8466 if (insn16 & N32_BIT (8))
35c08157
KLC
8467 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8468 else
8469 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8470 goto done;
8471
8472 case 0x35: /* break16, ex9.it */
8473 /* Only consider range of v3 break16. */
8474 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8475 goto done;
8476
8477 case 0x3c: /* ifcall9 */
fbaf61ad 8478 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
35c08157
KLC
8479 goto done;
8480 case 0x3d: /* movpi45 */
8481 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8482 goto done;
8483
8484 case 0x3f: /* MISC33 */
1c8f6a4d 8485 switch (insn16 & 0x7)
35c08157
KLC
8486 {
8487 case 2: /* neg33 */
8488 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8489 break;
8490 case 3: /* not33 */
1c8f6a4d
KLC
8491 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8492 N16_RA3 (insn16));
35c08157
KLC
8493 break;
8494 case 4: /* mul33 */
1c8f6a4d
KLC
8495 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8496 N16_RA3 (insn16));
35c08157
KLC
8497 break;
8498 case 5: /* xor33 */
1c8f6a4d
KLC
8499 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8500 N16_RA3 (insn16));
35c08157
KLC
8501 break;
8502 case 6: /* and33 */
1c8f6a4d
KLC
8503 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8504 N16_RA3 (insn16));
35c08157
KLC
8505 break;
8506 case 7: /* or33 */
1c8f6a4d
KLC
8507 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8508 N16_RA3 (insn16));
35c08157
KLC
8509 break;
8510 }
8511 goto done;
8512
1c8f6a4d 8513 case 0xb:
35c08157
KLC
8514 switch (insn16 & 0x7)
8515 {
8516 case 0: /* zeb33 */
8517 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8518 break;
8519 case 1: /* zeh33 */
8520 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8521 break;
8522 case 2: /* seb33 */
8523 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8524 break;
8525 case 3: /* seh33 */
8526 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8527 break;
8528 case 4: /* xlsb33 */
8529 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8530 break;
8531 case 5: /* x11b33 */
8532 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8533 break;
8534 case 6: /* bmski33 */
8535 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8536 1 << __GF (insn16, 3, 3));
35c08157
KLC
8537 break;
8538 case 7: /* fexti33 */
8539 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8540 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
35c08157
KLC
8541 break;
8542 }
8543 goto done;
8544 }
8545
8546 switch (__GF (insn16, 10, 5))
8547 {
8548 case 0x0: /* mov55 or ifret16 */
8549 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8550 && N16_RT5 (insn16) == N16_RA5 (insn16))
1c8f6a4d 8551 insn = N32_JREG (JR, 0, 0, 0, 3);
35c08157 8552 else
1c8f6a4d 8553 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
35c08157
KLC
8554 goto done;
8555 case 0x1: /* movi55 */
8556 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8557 goto done;
8558 case 0x1b: /* addi10s (V2) */
8559 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8560 goto done;
8561 }
8562
8563 switch (__GF (insn16, 11, 4))
8564 {
8565 case 0x7: /* lwi37.fp/swi37.fp */
4ec521f2 8566 if (insn16 & N32_BIT (7)) /* swi37.fp */
35c08157
KLC
8567 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8568 else /* lwi37.fp */
8569 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8570 goto done;
8571 case 0x8: /* beqz38 */
8572 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8573 goto done;
8574 case 0x9: /* bnez38 */
8575 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8576 goto done;
8577 case 0xa: /* beqs38/j8, implied r5 */
8578 if (N16_RT38 (insn16) == 5)
8579 insn = N32_JI (J, N16_IMM8S (insn16));
8580 else
8581 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8582 goto done;
fbaf61ad 8583 case 0xb: /* bnes38 and others. */
35c08157
KLC
8584 if (N16_RT38 (insn16) == 5)
8585 {
8586 switch (__GF (insn16, 5, 3))
8587 {
8588 case 0: /* jr5 */
8589 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8590 break;
8591 case 4: /* ret5 */
8592 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8593 break;
8594 case 1: /* jral5 */
8595 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8596 break;
8597 case 2: /* ex9.it imm5 */
8598 /* ex9.it had no 32-bit variantl. */
8599 break;
8600 case 5: /* add5.pc */
8601 /* add5.pc had no 32-bit variantl. */
8602 break;
8603 }
8604 }
8605 else /* bnes38 */
8606 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8607 goto done;
8608 case 0xe: /* lwi37/swi37 */
8609 if (insn16 & (1 << 7)) /* swi37.sp */
8610 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8611 else /* lwi37.sp */
8612 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8613 goto done;
8614 }
8615
8616done:
8617 if (insn & 0x80000000)
8618 return 0;
8619
8620 if (pinsn)
8621 *pinsn = insn;
8622 return 1;
8623}
8624\f
fbaf61ad 8625
35c08157
KLC
8626static bfd_boolean
8627is_sda_access_insn (unsigned long insn)
8628{
8629 switch (N32_OP6 (insn))
8630 {
8631 case N32_OP6_LWI:
8632 case N32_OP6_LHI:
8633 case N32_OP6_LHSI:
8634 case N32_OP6_LBI:
8635 case N32_OP6_LBSI:
8636 case N32_OP6_SWI:
8637 case N32_OP6_SHI:
8638 case N32_OP6_SBI:
8639 case N32_OP6_LWC:
8640 case N32_OP6_LDC:
8641 case N32_OP6_SWC:
8642 case N32_OP6_SDC:
8643 return TRUE;
8644 default:
8645 ;
8646 }
8647 return FALSE;
8648}
8649
8650static unsigned long
8651turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8652{
8653 uint32_t oinsn = 0;
8654
8655 switch (type)
8656 {
8657 case R_NDS32_GOT_LO12:
8658 case R_NDS32_GOTOFF_LO12:
8659 case R_NDS32_PLTREL_LO12:
8660 case R_NDS32_PLT_GOTREL_LO12:
8661 case R_NDS32_LO12S0_RELA:
8662 switch (N32_OP6 (insn))
8663 {
8664 case N32_OP6_LBI:
8665 /* lbi.gp */
8666 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8667 break;
8668 case N32_OP6_LBSI:
8669 /* lbsi.gp */
4ec521f2 8670 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8671 break;
8672 case N32_OP6_SBI:
8673 /* sbi.gp */
8674 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8675 break;
8676 case N32_OP6_ORI:
8677 /* addi.gp */
4ec521f2 8678 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8679 break;
8680 }
8681 break;
8682
8683 case R_NDS32_LO12S1_RELA:
8684 switch (N32_OP6 (insn))
8685 {
8686 case N32_OP6_LHI:
8687 /* lhi.gp */
8688 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8689 break;
8690 case N32_OP6_LHSI:
8691 /* lhsi.gp */
4ec521f2 8692 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
35c08157
KLC
8693 break;
8694 case N32_OP6_SHI:
8695 /* shi.gp */
4ec521f2 8696 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8697 break;
8698 }
8699 break;
8700
8701 case R_NDS32_LO12S2_RELA:
8702 switch (N32_OP6 (insn))
8703 {
8704 case N32_OP6_LWI:
8705 /* lwi.gp */
8706 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8707 break;
8708 case N32_OP6_SWI:
8709 /* swi.gp */
8710 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8711 break;
8712 }
8713 break;
8714
8715 case R_NDS32_LO12S2_DP_RELA:
8716 case R_NDS32_LO12S2_SP_RELA:
8717 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8718 break;
8719 }
8720
8721 if (oinsn)
8722 *pinsn = oinsn;
8723
8724 return oinsn != 0;
8725}
8726
8727/* Linker hasn't found the correct merge section for non-section symbol
8728 in relax time, this work is left to the function elf_link_input_bfd().
8729 So for non-section symbol, _bfd_merged_section_offset is also needed
8730 to find the correct symbol address. */
8731
8732static bfd_vma
8733nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8734 asection **psec, Elf_Internal_Rela *rel)
8735{
8736 asection *sec = *psec;
8737 bfd_vma relocation;
8738
8739 relocation = (sec->output_section->vma
8740 + sec->output_offset + sym->st_value);
8741 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8742 {
8743 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8744 rel->r_addend =
8745 _bfd_merged_section_offset (abfd, psec,
8746 elf_section_data (sec)->sec_info,
8747 sym->st_value + rel->r_addend);
8748 else
8749 rel->r_addend =
8750 _bfd_merged_section_offset (abfd, psec,
8751 elf_section_data (sec)->sec_info,
8752 sym->st_value) + rel->r_addend;
8753
8754 if (sec != *psec)
8755 {
8756 /* If we have changed the section, and our original section is
8757 marked with SEC_EXCLUDE, it means that the original
8758 SEC_MERGE section has been completely subsumed in some
8759 other SEC_MERGE section. In this case, we need to leave
8760 some info around for --emit-relocs. */
8761 if ((sec->flags & SEC_EXCLUDE) != 0)
8762 sec->kept_section = *psec;
8763 sec = *psec;
8764 }
8765 rel->r_addend -= relocation;
8766 rel->r_addend += sec->output_section->vma + sec->output_offset;
8767 }
8768 return relocation;
8769}
8770
8771static bfd_vma
8772calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8773 Elf_Internal_Sym *isymbuf,
8774 Elf_Internal_Shdr *symtab_hdr)
8775{
8776 bfd_signed_vma foff;
8777 bfd_vma symval, addend;
8778 Elf_Internal_Rela irel_fn;
8779 Elf_Internal_Sym *isym;
8780 asection *sym_sec;
8781
8782 /* Get the value of the symbol referred to by the reloc. */
8783 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8784 {
8785 /* A local symbol. */
8786 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8787
8788 if (isym->st_shndx == SHN_UNDEF)
8789 sym_sec = bfd_und_section_ptr;
8790 else if (isym->st_shndx == SHN_ABS)
8791 sym_sec = bfd_abs_section_ptr;
8792 else if (isym->st_shndx == SHN_COMMON)
8793 sym_sec = bfd_com_section_ptr;
8794 else
8795 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8796 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8797 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8798 addend = irel_fn.r_addend;
8799 }
8800 else
8801 {
8802 unsigned long indx;
8803 struct elf_link_hash_entry *h;
8804
8805 /* An external symbol. */
8806 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8807 h = elf_sym_hashes (abfd)[indx];
8808 BFD_ASSERT (h != NULL);
8809
8810 while (h->root.type == bfd_link_hash_indirect
8811 || h->root.type == bfd_link_hash_warning)
8812 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8813
8814 if (h->root.type != bfd_link_hash_defined
8815 && h->root.type != bfd_link_hash_defweak)
8816 /* This appears to be a reference to an undefined
8817 symbol. Just ignore it--it will be caught by the
8818 regular reloc processing. */
8819 return 0;
8820
8821 if (h->root.u.def.section->flags & SEC_MERGE)
8822 {
8823 sym_sec = h->root.u.def.section;
8824 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8825 (sym_sec)->sec_info, h->root.u.def.value);
8826 symval = symval + sym_sec->output_section->vma
8827 + sym_sec->output_offset;
8828 }
8829 else
8830 symval = (h->root.u.def.value
8831 + h->root.u.def.section->output_section->vma
8832 + h->root.u.def.section->output_offset);
8833 addend = irel->r_addend;
8834 }
8835
8836 foff = symval + addend;
8837
8838 return foff;
8839}
8840
35c08157
KLC
8841static int
8842is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8843 asection *sec, Elf_Internal_Rela *rel)
8844{
8845 bfd_byte *contents;
8846 unsigned short insn16;
8847
8848 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8849 return FALSE;
8850 contents = elf_section_data (sec)->this_hdr.contents;
8851 insn16 = bfd_getb16 (contents + rel->r_offset);
8852 if (insn16 == NDS32_NOP16)
8853 return TRUE;
8854 return FALSE;
8855}
8856
8857/* It checks whether the instruction could be converted to
8858 16-bit form and returns the converted one.
8859
8860 `internal_relocs' is supposed to be sorted. */
8861
8862static int
8863is_convert_32_to_16 (bfd *abfd, asection *sec,
8864 Elf_Internal_Rela *reloc,
8865 Elf_Internal_Rela *internal_relocs,
8866 Elf_Internal_Rela *irelend,
8867 uint16_t *insn16)
8868{
8869#define NORMAL_32_TO_16 (1 << 0)
8870#define SPECIAL_32_TO_16 (1 << 1)
8871 bfd_byte *contents = NULL;
8872 bfd_signed_vma off;
8873 bfd_vma mem_addr;
8874 uint32_t insn = 0;
8875 Elf_Internal_Rela *pc_rel;
35c08157
KLC
8876 Elf_Internal_Shdr *symtab_hdr;
8877 Elf_Internal_Sym *isymbuf = NULL;
8878 int convert_type;
8879 bfd_vma offset;
8880
8881 if (reloc->r_offset + 4 > sec->size)
8882 return FALSE;
8883
8884 offset = reloc->r_offset;
8885
0c4bd9d9 8886 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
35c08157
KLC
8887 return FALSE;
8888 insn = bfd_getb32 (contents + offset);
8889
8890 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8891 convert_type = NORMAL_32_TO_16;
8892 else if (special_convert_32_to_16 (insn, insn16, reloc))
8893 convert_type = SPECIAL_32_TO_16;
8894 else
8895 return FALSE;
8896
8897 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8898 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8899 return FALSE;
8900
8901 /* Find the first relocation of the same relocation-type,
8902 so we iteratie them forward. */
8903 pc_rel = reloc;
1c8f6a4d 8904 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
35c08157
KLC
8905 pc_rel--;
8906
8907 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8908 {
8909 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8910 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8911 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8912 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8913 {
fbaf61ad 8914 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8915 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8916 || off == 0)
35c08157
KLC
8917 return FALSE;
8918 break;
8919 }
8920 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8921 {
8922 /* movi => movi55 */
1c8f6a4d
KLC
8923 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8924 symtab_hdr);
8925 /* mem_addr is unsigned, but the value should
8926 be between [-16, 15]. */
35c08157
KLC
8927 if ((mem_addr + 0x10) >> 5)
8928 return FALSE;
8929 break;
8930 }
1c8f6a4d
KLC
8931 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8932 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8933 {
8934 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8935 because it can be relaxed to addi for TLS_LE_ADD. */
8936 return FALSE;
8937 }
35c08157
KLC
8938 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8939 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8940 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8941 && convert_type == SPECIAL_32_TO_16)
8942 {
8943 /* fp-as-gp
8944 We've selected a best fp-base for this access, so we can
8945 always resolve it anyway. Do nothing. */
8946 break;
8947 }
8948 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8949 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8950 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8951 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8952 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8953 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8954 {
1c8f6a4d
KLC
8955 /* Prevent unresolved addi instruction translate
8956 to addi45 or addi333. */
35c08157
KLC
8957 return FALSE;
8958 }
1c8f6a4d
KLC
8959 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8960 {
fbaf61ad 8961 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8962 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8963 return FALSE;
8964 break;
8965 }
35c08157
KLC
8966 }
8967
8968 return TRUE;
8969}
8970
8971static void
8972nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8973 Elf_Internal_Rela *reloc,
8974 Elf_Internal_Rela *internal_relocs,
8975 Elf_Internal_Rela *irelend,
8976 unsigned short insn16)
8977{
8978 Elf_Internal_Rela *pc_rel;
8979 bfd_vma offset;
8980
8981 offset = reloc->r_offset;
8982 bfd_putb16 (insn16, contents + offset);
8983 /* Find the first relocation of the same relocation-type,
8984 so we iteratie them forward. */
8985 pc_rel = reloc;
8986 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8987 pc_rel--;
8988
8989 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8990 {
8991 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8992 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8993 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8994 {
8995 pc_rel->r_info =
8996 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8997 }
8998 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8999 pc_rel->r_info =
9000 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
9001 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
9002 pc_rel->r_info =
9003 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
9004 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
9005 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
9006 pc_rel->r_info =
9007 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
1c8f6a4d
KLC
9008 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
9009 pc_rel->r_info =
9010 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
35c08157
KLC
9011 }
9012}
9013
9014/* Find a relocation of type specified by `reloc_type'
9015 of the same r_offset with reloc.
9016 If not found, return irelend.
9017
9018 Assuming relocations are sorted by r_offset,
9019 we find the relocation from `reloc' backward untill relocs,
9020 or find it from `reloc' forward untill irelend. */
9021
9022static Elf_Internal_Rela *
9023find_relocs_at_address (Elf_Internal_Rela *reloc,
9024 Elf_Internal_Rela *relocs,
9025 Elf_Internal_Rela *irelend,
9026 enum elf_nds32_reloc_type reloc_type)
9027{
9028 Elf_Internal_Rela *rel_t;
9029
9030 /* Find backward. */
9031 for (rel_t = reloc;
9032 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9033 rel_t--)
9034 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9035 return rel_t;
9036
1c8f6a4d 9037 /* We didn't find it backward. Try find it forward. */
35c08157
KLC
9038 for (rel_t = reloc;
9039 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9040 rel_t++)
9041 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9042 return rel_t;
9043
9044 return irelend;
9045}
9046
9047/* Find a relocation of specified type and offset.
9048 `reloc' is just a refence point to find a relocation at specified offset.
9049 If not found, return irelend.
9050
9051 Assuming relocations are sorted by r_offset,
9052 we find the relocation from `reloc' backward untill relocs,
9053 or find it from `reloc' forward untill irelend. */
9054
9055static Elf_Internal_Rela *
9056find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9057 Elf_Internal_Rela *relocs,
9058 Elf_Internal_Rela *irelend,
6cae483a 9059 enum elf_nds32_reloc_type reloc_type,
35c08157
KLC
9060 bfd_vma offset_p)
9061{
9062 Elf_Internal_Rela *rel_t = NULL;
9063
9064 /* First, we try to find a relocation of offset `offset_p',
9065 and then we use find_relocs_at_address to find specific type. */
9066
9067 if (reloc->r_offset > offset_p)
9068 {
9069 /* Find backward. */
9070 for (rel_t = reloc;
9071 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9072 /* Do nothing. */;
9073 }
9074 else if (reloc->r_offset < offset_p)
9075 {
9076 /* Find forward. */
9077 for (rel_t = reloc;
9078 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9079 /* Do nothing. */;
9080 }
9081 else
9082 rel_t = reloc;
9083
9084 /* Not found? */
9085 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9086 return irelend;
9087
9088 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9089}
9090
fbaf61ad
NC
9091typedef struct nds32_elf_blank nds32_elf_blank_t;
9092struct nds32_elf_blank
35c08157 9093{
fbaf61ad
NC
9094 /* Where the blank begins. */
9095 bfd_vma offset;
9096 /* The size of the blank. */
9097 bfd_vma size;
9098 /* The accumulative size before this blank. */
9099 bfd_vma total_size;
9100 nds32_elf_blank_t *next;
9101 nds32_elf_blank_t *prev;
9102};
35c08157
KLC
9103
9104static nds32_elf_blank_t *blank_free_list = NULL;
9105
9106static nds32_elf_blank_t *
9107create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9108{
9109 nds32_elf_blank_t *blank_t;
9110
9111 if (blank_free_list)
9112 {
9113 blank_t = blank_free_list;
9114 blank_free_list = blank_free_list->next;
9115 }
9116 else
9117 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9118
9119 if (blank_t == NULL)
9120 return NULL;
9121
9122 blank_t->offset = offset_p;
9123 blank_t->size = size_p;
9124 blank_t->total_size = 0;
9125 blank_t->next = NULL;
9126 blank_t->prev = NULL;
9127
9128 return blank_t;
9129}
9130
9131static void
9132remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9133{
9134 if (blank_free_list)
9135 {
9136 blank_free_list->prev = blank_p;
9137 blank_p->next = blank_free_list;
9138 }
9139 else
9140 blank_p->next = NULL;
9141
9142 blank_p->prev = NULL;
9143 blank_free_list = blank_p;
9144}
9145
9146static void
9147clean_nds32_elf_blank (void)
9148{
9149 nds32_elf_blank_t *blank_t;
9150
9151 while (blank_free_list)
9152 {
9153 blank_t = blank_free_list;
9154 blank_free_list = blank_free_list->next;
9155 free (blank_t);
9156 }
9157}
9158
9159static nds32_elf_blank_t *
9160search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9161{
9162 nds32_elf_blank_t *blank_t;
9163
9164 if (!blank_p)
9165 return NULL;
9166 blank_t = blank_p;
9167
9168 while (blank_t && addr < blank_t->offset)
9169 blank_t = blank_t->prev;
9170 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9171 blank_t = blank_t->next;
9172
9173 return blank_t;
9174}
9175
9176static bfd_vma
9177get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9178 int overwrite)
9179{
9180 nds32_elf_blank_t *blank_t;
9181
9182 blank_t = search_nds32_elf_blank (*blank_p, addr);
9183 if (!blank_t)
9184 return 0;
9185
9186 if (overwrite)
9187 *blank_p = blank_t;
9188
9189 if (addr < blank_t->offset + blank_t->size)
9190 return blank_t->total_size + (addr - blank_t->offset);
9191 else
9192 return blank_t->total_size + blank_t->size;
9193}
9194
9195static bfd_boolean
9196insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9197{
9198 nds32_elf_blank_t *blank_t, *blank_t2;
9199
9200 if (!*blank_p)
9201 {
9202 *blank_p = create_nds32_elf_blank (addr, len);
9203 return *blank_p ? TRUE : FALSE;
9204 }
9205
9206 blank_t = search_nds32_elf_blank (*blank_p, addr);
9207
9208 if (blank_t == NULL)
9209 {
9210 blank_t = create_nds32_elf_blank (addr, len);
9211 if (!blank_t)
9212 return FALSE;
9213 while ((*blank_p)->prev != NULL)
9214 *blank_p = (*blank_p)->prev;
9215 blank_t->next = *blank_p;
9216 (*blank_p)->prev = blank_t;
9217 (*blank_p) = blank_t;
9218 return TRUE;
9219 }
9220
9221 if (addr < blank_t->offset + blank_t->size)
9222 {
fbaf61ad
NC
9223 /* Extend the origin blank. */
9224 if (addr + len > blank_t->offset + blank_t->size)
9225 blank_t->size = addr + len - blank_t->offset;
35c08157
KLC
9226 }
9227 else
9228 {
9229 blank_t2 = create_nds32_elf_blank (addr, len);
9230 if (!blank_t2)
9231 return FALSE;
9232 if (blank_t->next)
9233 {
9234 blank_t->next->prev = blank_t2;
9235 blank_t2->next = blank_t->next;
9236 }
9237 blank_t2->prev = blank_t;
9238 blank_t->next = blank_t2;
9239 *blank_p = blank_t2;
9240 }
9241
9242 return TRUE;
9243}
9244
9245static bfd_boolean
9246insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9247 bfd_vma len)
9248{
9249 nds32_elf_blank_t *blank_t;
9250
9251 if (!insert_nds32_elf_blank (blank_p, addr, len))
9252 return FALSE;
9253
9254 blank_t = *blank_p;
9255
9256 if (!blank_t->prev)
9257 {
9258 blank_t->total_size = 0;
9259 blank_t = blank_t->next;
9260 }
9261
9262 while (blank_t)
9263 {
9264 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9265 blank_t = blank_t->next;
9266 }
9267
9268 return TRUE;
9269}
9270
9271static void
9272calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9273{
9274 nds32_elf_blank_t *blank_t;
9275 bfd_vma total_size = 0;
9276
9277 if (!blank_p)
9278 return;
9279
9280 blank_t = blank_p;
9281 while (blank_t->prev)
9282 blank_t = blank_t->prev;
9283 while (blank_t)
9284 {
9285 blank_t->total_size = total_size;
9286 total_size += blank_t->size;
9287 blank_t = blank_t->next;
9288 }
9289}
9290
9291static bfd_boolean
9292nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9293 nds32_elf_blank_t *blank_p)
9294{
9295 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
1c8f6a4d 9296 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
35c08157
KLC
9297 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9298 unsigned int sec_shndx; /* The section the be relaxed. */
9299 bfd_byte *contents; /* Contents data of iterating section. */
9300 Elf_Internal_Rela *internal_relocs;
9301 Elf_Internal_Rela *irel;
9302 Elf_Internal_Rela *irelend;
9303 struct elf_link_hash_entry **sym_hashes;
9304 struct elf_link_hash_entry **end_hashes;
9305 unsigned int symcount;
9306 asection *sect;
9307 nds32_elf_blank_t *blank_t;
9308 nds32_elf_blank_t *blank_t2;
9309 nds32_elf_blank_t *blank_head;
9310
9311 blank_head = blank_t = blank_p;
9312 while (blank_head->prev != NULL)
9313 blank_head = blank_head->prev;
9314 while (blank_t->next != NULL)
9315 blank_t = blank_t->next;
9316
9317 if (blank_t->offset + blank_t->size <= sec->size)
9318 {
9319 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9320 blank_t->next->prev = blank_t;
9321 }
9322 if (blank_head->offset > 0)
9323 {
9324 blank_head->prev = create_nds32_elf_blank (0, 0);
9325 blank_head->prev->next = blank_head;
9326 blank_head = blank_head->prev;
9327 }
9328
9329 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9330
9331 /* The deletion must stop at the next ALIGN reloc for an alignment
9332 power larger than the number of bytes we are deleting. */
9333
9334 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9335 if (!nds32_get_local_syms (abfd, sec, &isym))
9336 return FALSE;
9337
9338 if (isym == NULL)
9339 {
9340 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9341 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9342 symtab_hdr->contents = (bfd_byte *) isym;
9343 }
9344
9345 if (isym == NULL || symtab_hdr->sh_info == 0)
9346 return FALSE;
9347
9348 blank_t = blank_head;
9349 calc_nds32_blank_total (blank_head);
9350
9351 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9352 {
9353 /* Adjust all the relocs. */
9354
9355 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9356 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9357 TRUE /* keep_memory */);
9358 irelend = internal_relocs + sect->reloc_count;
9359
9360 blank_t = blank_head;
9361 blank_t2 = blank_head;
9362
9363 if (!(sect->flags & SEC_RELOC))
9364 continue;
9365
0c4bd9d9 9366 nds32_get_section_contents (abfd, sect, &contents, TRUE);
35c08157
KLC
9367
9368 for (irel = internal_relocs; irel < irelend; irel++)
9369 {
9370 bfd_vma raddr;
9371
9372 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9373 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9374 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9375 {
9376 unsigned long val = 0;
1c8f6a4d
KLC
9377 unsigned long mask;
9378 long before, between;
6cae483a 9379 long offset = 0;
35c08157
KLC
9380
9381 switch (ELF32_R_TYPE (irel->r_info))
9382 {
9383 case R_NDS32_DIFF8:
1c8f6a4d 9384 offset = bfd_get_8 (abfd, contents + irel->r_offset);
35c08157
KLC
9385 break;
9386 case R_NDS32_DIFF16:
1c8f6a4d 9387 offset = bfd_get_16 (abfd, contents + irel->r_offset);
35c08157
KLC
9388 break;
9389 case R_NDS32_DIFF32:
9390 val = bfd_get_32 (abfd, contents + irel->r_offset);
1c8f6a4d
KLC
9391 /* Get the signed bit and mask for the high part. The
9392 gcc will alarm when right shift 32-bit since the
9393 type size of long may be 32-bit. */
9394 mask = 0 - (val >> 31);
9395 if (mask)
9396 offset = (val | (mask - 0xffffffff));
9397 else
9398 offset = val;
35c08157
KLC
9399 break;
9400 default:
9401 BFD_ASSERT (0);
9402 }
9403
9404 /* DIFF value
9405 0 |encoded in location|
9406 |------------|-------------------|---------
9407 sym+off(addend)
9408 -- before ---| *****************
9409 --------------------- between ---|
9410
1c8f6a4d
KLC
9411 We only care how much data are relax between DIFF,
9412 marked as ***. */
35c08157
KLC
9413
9414 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9415 between = get_nds32_elf_blank_total (&blank_t,
9416 irel->r_addend + offset, 0);
35c08157
KLC
9417 if (between == before)
9418 goto done_adjust_diff;
9419
9420 switch (ELF32_R_TYPE (irel->r_info))
9421 {
9422 case R_NDS32_DIFF8:
1c8f6a4d
KLC
9423 bfd_put_8 (abfd, offset - (between - before),
9424 contents + irel->r_offset);
35c08157
KLC
9425 break;
9426 case R_NDS32_DIFF16:
1c8f6a4d
KLC
9427 bfd_put_16 (abfd, offset - (between - before),
9428 contents + irel->r_offset);
35c08157
KLC
9429 break;
9430 case R_NDS32_DIFF32:
1c8f6a4d
KLC
9431 bfd_put_32 (abfd, offset - (between - before),
9432 contents + irel->r_offset);
35c08157
KLC
9433 break;
9434 }
9435 }
9436 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9437 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9438 {
9439 bfd_vma val = 0;
9440 unsigned int len = 0;
9441 unsigned long before, between;
9442 bfd_byte *endp, *p;
9443
4265548c
PA
9444 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9445 &len);
35c08157
KLC
9446
9447 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9448 between = get_nds32_elf_blank_total (&blank_t,
9449 irel->r_addend + val, 0);
35c08157
KLC
9450 if (between == before)
9451 goto done_adjust_diff;
9452
9453 p = contents + irel->r_offset;
9454 endp = p + len -1;
9455 memset (p, 0x80, len);
9456 *(endp) = 0;
9457 p = write_uleb128 (p, val - (between - before)) - 1;
9458 if (p < endp)
9459 *p |= 0x80;
9460 }
9461done_adjust_diff:
9462
9463 if (sec == sect)
9464 {
9465 raddr = irel->r_offset;
1c8f6a4d
KLC
9466 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9467 irel->r_offset, 1);
35c08157
KLC
9468
9469 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9470 continue;
9471 if (blank_t2 && blank_t2->next
1c8f6a4d
KLC
9472 && (blank_t2->offset > raddr
9473 || blank_t2->next->offset <= raddr))
4eca0228 9474 _bfd_error_handler
38f14ab8
AM
9475 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9476 abfd);
35c08157
KLC
9477
9478 /* Mark reloc in deleted portion as NONE.
9479 For some relocs like R_NDS32_LABEL that doesn't modify the
9480 content in the section. R_NDS32_LABEL doesn't belong to the
9481 instruction in the section, so we should preserve it. */
9482 if (raddr >= blank_t2->offset
9483 && raddr < blank_t2->offset + blank_t2->size
9484 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9485 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9486 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9487 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9488 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9489 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9490 {
9491 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9492 R_NDS32_NONE);
9493 continue;
9494 }
9495 }
9496
9497 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9498 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9499 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9500 continue;
9501
9502 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9503 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9504 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9505 {
9506 if (irel->r_addend <= sec->size)
9507 irel->r_addend -=
9508 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9509 }
9510 }
9511 }
9512
9513 /* Adjust the local symbols defined in this section. */
9514 blank_t = blank_head;
9515 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9516 {
9517 if (isym->st_shndx == sec_shndx)
9518 {
9519 if (isym->st_value <= sec->size)
9520 {
9521 bfd_vma ahead;
9522 bfd_vma orig_addr = isym->st_value;
9523
9524 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9525 isym->st_value -= ahead;
9526
9527 /* Adjust function size. */
1c8f6a4d
KLC
9528 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9529 && isym->st_size > 0)
9530 isym->st_size -=
9531 get_nds32_elf_blank_total
9532 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
35c08157
KLC
9533 }
9534 }
9535 }
9536
9537 /* Now adjust the global symbols defined in this section. */
9538 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9539 - symtab_hdr->sh_info);
9540 sym_hashes = elf_sym_hashes (abfd);
9541 end_hashes = sym_hashes + symcount;
9542 blank_t = blank_head;
9543 for (; sym_hashes < end_hashes; sym_hashes++)
9544 {
9545 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9546
9547 if ((sym_hash->root.type == bfd_link_hash_defined
9548 || sym_hash->root.type == bfd_link_hash_defweak)
9549 && sym_hash->root.u.def.section == sec)
9550 {
9551 if (sym_hash->root.u.def.value <= sec->size)
9552 {
9553 bfd_vma ahead;
9554 bfd_vma orig_addr = sym_hash->root.u.def.value;
9555
9556 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9557 sym_hash->root.u.def.value -= ahead;
9558
9559 /* Adjust function size. */
9560 if (sym_hash->type == STT_FUNC)
1c8f6a4d
KLC
9561 sym_hash->size -=
9562 get_nds32_elf_blank_total
9563 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
35c08157
KLC
9564
9565 }
9566 }
9567 }
9568
9569 contents = elf_section_data (sec)->this_hdr.contents;
9570 blank_t = blank_head;
9571 while (blank_t->next)
9572 {
9573 /* Actually delete the bytes. */
9574
9575 /* If current blank is the last blank overlap with current section,
9576 go to finish process. */
9577 if (sec->size <= (blank_t->next->offset))
9578 break;
9579
9580 memmove (contents + blank_t->offset - blank_t->total_size,
9581 contents + blank_t->offset + blank_t->size,
9582 blank_t->next->offset - (blank_t->offset + blank_t->size));
9583
9584 blank_t = blank_t->next;
9585 }
9586
9587 if (sec->size > (blank_t->offset + blank_t->size))
9588 {
9589 /* There are remaining code between blank and section boundary.
9590 Move the remaining code to appropriate location. */
9591 memmove (contents + blank_t->offset - blank_t->total_size,
9592 contents + blank_t->offset + blank_t->size,
9593 sec->size - (blank_t->offset + blank_t->size));
9594 sec->size -= blank_t->total_size + blank_t->size;
9595 }
9596 else
9597 /* This blank is not entirely included in the section,
9598 reduce the section size by only part of the blank size. */
9599 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9600
9601 while (blank_head)
9602 {
9603 blank_t = blank_head;
9604 blank_head = blank_head->next;
9605 remove_nds32_elf_blank (blank_t);
9606 }
9607
9608 return TRUE;
9609}
9610
9611/* Get the contents of a section. */
9612
9613static int
0c4bd9d9
KLC
9614nds32_get_section_contents (bfd *abfd, asection *sec,
9615 bfd_byte **contents_p, bfd_boolean cache)
35c08157
KLC
9616{
9617 /* Get the section contents. */
9618 if (elf_section_data (sec)->this_hdr.contents != NULL)
9619 *contents_p = elf_section_data (sec)->this_hdr.contents;
9620 else
9621 {
9622 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9623 return FALSE;
0c4bd9d9
KLC
9624 if (cache)
9625 elf_section_data (sec)->this_hdr.contents = *contents_p;
35c08157
KLC
9626 }
9627
9628 return TRUE;
9629}
9630
9631/* Get the contents of the internal symbol of abfd. */
9632
9633static int
9634nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9635 Elf_Internal_Sym **isymbuf_p)
9636{
9637 Elf_Internal_Shdr *symtab_hdr;
9638 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9639
9640 /* Read this BFD's local symbols if we haven't done so already. */
9641 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9642 {
9643 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9644 if (*isymbuf_p == NULL)
9645 {
9646 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9647 symtab_hdr->sh_info, 0,
9648 NULL, NULL, NULL);
9649 if (*isymbuf_p == NULL)
9650 return FALSE;
9651 }
9652 }
9653 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9654
9655 return TRUE;
9656}
9657
9658/* Range of small data. */
1c8f6a4d
KLC
9659static bfd_vma sdata_range[2][2];
9660static bfd_vma const sdata_init_range[2] =
9661{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
35c08157
KLC
9662
9663static int
9664nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9665 bfd_byte *contents, bfd_vma addr)
9666{
9667 unsigned long insn = bfd_getb32 (contents + addr);
9668
9669 if (insn & 0x80000000)
9670 return 2;
9671
9672 return 4;
9673}
9674
9675/* Set the gp relax range. We have to measure the safe range
9676 to do gp relaxation. */
9677
9678static void
9679relax_range_measurement (bfd *abfd)
9680{
9681 asection *sec_f, *sec_b;
9682 /* For upper bound. */
9683 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9684 bfd_vma align;
35c08157
KLC
9685 static int decide_relax_range = 0;
9686 int i;
fbaf61ad 9687 int range_number = ARRAY_SIZE (sdata_init_range);
35c08157
KLC
9688
9689 if (decide_relax_range)
9690 return;
9691 decide_relax_range = 1;
9692
9693 if (sda_rela_sec == NULL)
9694 {
9695 /* Since there is no data sections, we assume the range is page size. */
1c8f6a4d 9696 for (i = 0; i < range_number; i++)
35c08157
KLC
9697 {
9698 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9699 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9700 }
9701 return;
9702 }
9703
9704 /* Get the biggest alignment power after the gp located section. */
9705 sec_f = sda_rela_sec->output_section;
9706 sec_b = sec_f->next;
9707 align = 0;
9708 while (sec_b != NULL)
9709 {
9710 if ((unsigned)(1 << sec_b->alignment_power) > align)
9711 align = (1 << sec_b->alignment_power);
9712 sec_b = sec_b->next;
9713 }
9714
9715 /* I guess we can not determine the section before
9716 gp located section, so we assume the align is max page size. */
1c8f6a4d 9717 for (i = 0; i < range_number; i++)
35c08157 9718 {
1c8f6a4d 9719 sdata_range[i][1] = sdata_init_range[i] - align;
35c08157 9720 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
1c8f6a4d 9721 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
35c08157
KLC
9722 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9723 }
9724}
9725
9726/* These are macros used to check flags encoded in r_addend.
9727 They are only used by nds32_elf_relax_section (). */
9728#define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9729#define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9730#define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9731#define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9732
695344c0
NC
9733static const char * unrecognized_reloc_msg =
9734 /* xgettext:c-format */
2dcf00ce 9735 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
695344c0 9736
1c8f6a4d
KLC
9737/* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9738
35c08157 9739static bfd_boolean
1c8f6a4d
KLC
9740nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9741 Elf_Internal_Rela *internal_relocs, int *insn_len,
9742 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9743 Elf_Internal_Shdr *symtab_hdr)
9744{
9745 /* There are 3 variations for LONGCALL1
9746 case 4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8 9747 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9748 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9749 jral5 ta ;
1c8f6a4d
KLC
9750
9751 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8 9752 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9753 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9754 jral ta ;
1c8f6a4d
KLC
9755
9756 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8 9757 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9758 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9759 jral ta ;
1c8f6a4d
KLC
9760 Check code for -mlong-calls output. */
9761
9762 /* Get the reloc for the address from which the register is
9763 being loaded. This reloc will tell us which function is
9764 actually being called. */
9765
9766 bfd_vma laddr;
9767 int seq_len; /* Original length of instruction sequence. */
35c08157 9768 uint32_t insn;
1c8f6a4d 9769 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d 9770 bfd_signed_vma foff;
35c08157 9771 uint16_t insn16;
35c08157 9772
1c8f6a4d
KLC
9773 irelend = internal_relocs + sec->reloc_count;
9774 seq_len = GET_SEQ_LEN (irel->r_addend);
9775 laddr = irel->r_offset;
9776 *insn_len = seq_len;
35c08157 9777
1c8f6a4d
KLC
9778 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9779 R_NDS32_HI20_RELA, laddr);
9780 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9781 R_NDS32_LO12S0_ORI_RELA,
9782 laddr + 4);
35c08157 9783
1c8f6a4d 9784 if (hi_irelfn == irelend || lo_irelfn == irelend)
35c08157 9785 {
695344c0 9786 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
2dcf00ce 9787 (uint64_t) irel->r_offset);
1c8f6a4d 9788 return FALSE;
35c08157
KLC
9789 }
9790
1c8f6a4d 9791 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9792 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9793
1c8f6a4d 9794 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
9795 if (foff == 0
9796 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9797 || foff >= CONSERVATIVE_24BIT_S1)
9798 return FALSE;
35c08157 9799
fbaf61ad 9800 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
9801 /* For simplicity of coding, we are going to modify the section
9802 contents, the section relocs, and the BFD symbol table. We
9803 must tell the rest of the code not to free up this
9804 information. It would be possible to instead create a table
9805 of changes which have to be made, as is done in coff-mips.c;
9806 that would be more work, but would require less memory when
9807 the linker is run. */
9808
9809 /* Replace the long call with a jal. */
9810 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9811 R_NDS32_25_PCREL_RELA);
9812 irel->r_addend = hi_irelfn->r_addend;
9813
9814 /* We don't resolve this here but resolve it in relocate_section. */
9815 insn = INSN_JAL;
9816 bfd_putb32 (insn, contents + irel->r_offset);
9817
9818 hi_irelfn->r_info =
9819 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9820 lo_irelfn->r_info =
9821 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9822 *insn_len = 4;
9823
9824 if (seq_len & 0x2)
9825 {
9826 insn16 = NDS32_NOP16;
9827 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9828 lo_irelfn->r_info =
9829 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9830 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9831 *insn_len += 2;
9832 }
9833 return TRUE;
9834}
35c08157 9835
1c8f6a4d
KLC
9836#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9837/* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
35c08157 9838
1c8f6a4d
KLC
9839static bfd_boolean
9840nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9841 Elf_Internal_Rela *internal_relocs, int *insn_len,
9842 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9843 Elf_Internal_Shdr *symtab_hdr)
9844{
9845 /* bltz rt, .L1 ; LONGCALL2
9846 jal symbol ; 25_PCREL
9847 .L1: */
35c08157 9848
1c8f6a4d
KLC
9849 /* Get the reloc for the address from which the register is
9850 being loaded. This reloc will tell us which function is
9851 actually being called. */
35c08157 9852
1c8f6a4d
KLC
9853 bfd_vma laddr;
9854 uint32_t insn;
9855 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
1c8f6a4d 9856 bfd_signed_vma foff;
35c08157 9857
1c8f6a4d
KLC
9858 irelend = internal_relocs + sec->reloc_count;
9859 laddr = irel->r_offset;
9860 i1_irelfn =
9861 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9862 R_NDS32_25_PCREL_RELA, laddr + 4);
9863
9864 if (i1_irelfn == irelend)
35c08157 9865 {
695344c0 9866 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
2dcf00ce 9867 (uint64_t) irel->r_offset);
1c8f6a4d 9868 return FALSE;
35c08157
KLC
9869 }
9870
1c8f6a4d 9871 insn = bfd_getb32 (contents + laddr);
35c08157 9872
1c8f6a4d 9873 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9874 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
35c08157 9875
fbaf61ad
NC
9876 if (foff == 0
9877 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
9878 || foff >= CONSERVATIVE_16BIT_S1)
9879 return FALSE;
35c08157 9880
1c8f6a4d
KLC
9881 /* Relax to bgezal rt, label ; 17_PCREL
9882 or bltzal rt, label ; 17_PCREL */
9883
9884 /* Convert to complimentary conditional call. */
9885 insn = CONVERT_CONDITION_CALL (insn);
9886
9887 /* For simplicity of coding, we are going to modify the section
9888 contents, the section relocs, and the BFD symbol table. We
9889 must tell the rest of the code not to free up this
9890 information. It would be possible to instead create a table
9891 of changes which have to be made, as is done in coff-mips.c;
9892 that would be more work, but would require less memory when
9893 the linker is run. */
9894
9895 /* Clean unnessary relocations. */
9896 i1_irelfn->r_info =
9897 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9898 cond_irelfn =
9899 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9900 R_NDS32_17_PCREL_RELA, laddr);
9901 if (cond_irelfn != irelend)
9902 cond_irelfn->r_info =
9903 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9904
9905 /* Replace the long call with a bgezal. */
9906 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9907 R_NDS32_17_PCREL_RELA);
9908 irel->r_addend = i1_irelfn->r_addend;
9909
9910 bfd_putb32 (insn, contents + irel->r_offset);
9911
9912 *insn_len = 4;
9913 return TRUE;
9914}
35c08157 9915
1c8f6a4d
KLC
9916/* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9917
9918static bfd_boolean
9919nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9920 Elf_Internal_Rela *internal_relocs, int *insn_len,
9921 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9922 Elf_Internal_Shdr *symtab_hdr)
9923{
9924 /* There are 3 variations for LONGCALL3
9925 case 4-4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8
AM
9926 bltz rt, $1 ; LONGCALL3
9927 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9928 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9929 jral5 ta ;
1c8f6a4d
KLC
9930 $1
9931
9932 case 4-4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
9933 bltz rt, $1 ; LONGCALL3
9934 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9935 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9936 jral ta ;
1c8f6a4d
KLC
9937 $1
9938
9939 case 4-4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
9940 bltz rt, $1 ; LONGCALL3
9941 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9942 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9943 jral ta ;
1c8f6a4d
KLC
9944 $1 */
9945
9946 /* Get the reloc for the address from which the register is
9947 being loaded. This reloc will tell us which function is
9948 actually being called. */
9949
9950 bfd_vma laddr;
9951 int seq_len; /* Original length of instruction sequence. */
9952 uint32_t insn;
9953 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
1c8f6a4d
KLC
9954 bfd_signed_vma foff;
9955 uint16_t insn16;
9956
9957 irelend = internal_relocs + sec->reloc_count;
9958 seq_len = GET_SEQ_LEN (irel->r_addend);
9959 laddr = irel->r_offset;
9960 *insn_len = seq_len;
9961
9962 hi_irelfn =
9963 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9964 R_NDS32_HI20_RELA, laddr + 4);
9965 lo_irelfn =
9966 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9967 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9968
9969 if (hi_irelfn == irelend || lo_irelfn == irelend)
9970 {
695344c0 9971 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
2dcf00ce 9972 (uint64_t) irel->r_offset);
1c8f6a4d 9973 return FALSE;
35c08157
KLC
9974 }
9975
1c8f6a4d 9976 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9977 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9978
fbaf61ad
NC
9979 if (foff == 0
9980 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9981 || foff >= CONSERVATIVE_24BIT_S1)
9982 return FALSE;
35c08157 9983
1c8f6a4d
KLC
9984 insn = bfd_getb32 (contents + laddr);
9985 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
35c08157 9986 {
1c8f6a4d
KLC
9987 /* Relax to bgezal rt, label ; 17_PCREL
9988 or bltzal rt, label ; 17_PCREL */
35c08157 9989
1c8f6a4d
KLC
9990 /* Convert to complimentary conditional call. */
9991 insn = CONVERT_CONDITION_CALL (insn);
9992 bfd_putb32 (insn, contents + irel->r_offset);
35c08157 9993
1c8f6a4d
KLC
9994 *insn_len = 4;
9995 irel->r_info =
9996 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9997 hi_irelfn->r_info =
9998 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9999 lo_irelfn->r_info =
10000 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10001
10002 cond_irelfn =
10003 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10004 R_NDS32_17_PCREL_RELA, laddr);
10005 if (cond_irelfn != irelend)
35c08157 10006 {
1c8f6a4d
KLC
10007 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10008 R_NDS32_17_PCREL_RELA);
10009 cond_irelfn->r_addend = hi_irelfn->r_addend;
35c08157 10010 }
1c8f6a4d
KLC
10011
10012 if (seq_len & 0x2)
35c08157 10013 {
1c8f6a4d
KLC
10014 insn16 = NDS32_NOP16;
10015 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10016 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10017 R_NDS32_INSN16);
10018 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10019 insn_len += 2;
35c08157 10020 }
1c8f6a4d
KLC
10021 }
10022 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10023 {
10024 /* Relax to the following instruction sequence
10025 bltz rt, $1 ; LONGCALL2
10026 jal symbol ; 25_PCREL
10027 $1 */
10028 *insn_len = 8;
10029 insn = INSN_JAL;
10030 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
35c08157 10031
1c8f6a4d
KLC
10032 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10033 R_NDS32_25_PCREL_RELA);
10034 irel->r_info =
10035 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10036
10037 lo_irelfn->r_info =
10038 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10039
1c8f6a4d 10040 if (seq_len & 0x2)
35c08157 10041 {
1c8f6a4d
KLC
10042 insn16 = NDS32_NOP16;
10043 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10044 lo_irelfn->r_info =
10045 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10046 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10047 insn_len += 2;
10048 }
10049 }
10050 return TRUE;
10051}
35c08157 10052
1c8f6a4d 10053/* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
35c08157 10054
1c8f6a4d
KLC
10055static bfd_boolean
10056nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10057 Elf_Internal_Rela *internal_relocs, int *insn_len,
10058 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10059 Elf_Internal_Shdr *symtab_hdr)
10060{
10061 /* There are 3 variations for LONGJUMP1
10062 case 4-4-2; 16-bit bit on, optimize off or optimize for space
07d6d2b8
AM
10063 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10064 ori ta, ta, lo12(symbol) ; LO12S0
10065 jr5 ta ;
1c8f6a4d
KLC
10066
10067 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
10068 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10069 ori ta, ta, lo12(symbol) ; LO12S0
10070 jr ta ;
1c8f6a4d
KLC
10071
10072 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
10073 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10074 ori ta, ta, lo12(symbol) ; LO12S0
10075 jr ta ; */
1c8f6a4d
KLC
10076
10077 /* Get the reloc for the address from which the register is
10078 being loaded. This reloc will tell us which function is
10079 actually being called. */
10080
10081 bfd_vma laddr;
10082 int seq_len; /* Original length of instruction sequence. */
10083 int insn16_on; /* 16-bit on/off. */
10084 uint32_t insn;
10085 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d
KLC
10086 bfd_signed_vma foff;
10087 uint16_t insn16;
10088 unsigned long reloc;
35c08157 10089
1c8f6a4d
KLC
10090 irelend = internal_relocs + sec->reloc_count;
10091 seq_len = GET_SEQ_LEN (irel->r_addend);
10092 laddr = irel->r_offset;
10093 *insn_len = seq_len;
10094 insn16_on = IS_16BIT_ON (irel->r_addend);
10095
10096 hi_irelfn =
10097 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10098 R_NDS32_HI20_RELA, laddr);
10099 lo_irelfn =
10100 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10101 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10102 if (hi_irelfn == irelend || lo_irelfn == irelend)
10103 {
695344c0 10104 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
2dcf00ce 10105 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10106 return FALSE;
10107 }
35c08157 10108
1c8f6a4d 10109 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10110 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10111
fbaf61ad
NC
10112 if (foff == 0
10113 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10114 || foff < -CONSERVATIVE_24BIT_S1)
10115 return FALSE;
35c08157 10116
fbaf61ad
NC
10117 if (insn16_on
10118 && foff >= -ACCURATE_8BIT_S1
10119 && foff < ACCURATE_8BIT_S1
10120 && (seq_len & 0x2))
1c8f6a4d
KLC
10121 {
10122 /* j8 label */
10123 /* 16-bit on, but not optimized for speed. */
10124 reloc = R_NDS32_9_PCREL_RELA;
10125 insn16 = INSN_J8;
10126 bfd_putb16 (insn16, contents + irel->r_offset);
10127 *insn_len = 2;
10128 irel->r_info =
10129 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10130 }
10131 else
10132 {
10133 /* j label */
10134 reloc = R_NDS32_25_PCREL_RELA;
10135 insn = INSN_J;
10136 bfd_putb32 (insn, contents + irel->r_offset);
10137 *insn_len = 4;
10138 irel->r_info =
10139 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10140 irel->r_addend = 0;
10141 }
35c08157 10142
1c8f6a4d
KLC
10143 hi_irelfn->r_info =
10144 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10145 lo_irelfn->r_info =
10146 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10147
1c8f6a4d
KLC
10148 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10149 {
10150 insn16 = NDS32_NOP16;
10151 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10152 lo_irelfn->r_info =
10153 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10154 R_NDS32_INSN16);
10155 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10156 *insn_len += 2;
10157 }
10158 return TRUE;
10159}
10160
10161/* Revert condition branch. This function does not check if the input
10162 instruction is condition branch or not. */
10163
10164static void
10165nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10166 uint16_t *re_insn16, uint32_t *re_insn)
10167{
10168 uint32_t comp_insn = 0;
10169 uint16_t comp_insn16 = 0;
10170
10171 if (insn)
10172 {
10173 if (N32_OP6 (insn) == N32_OP6_BR1)
10174 {
10175 /* beqs label. */
10176 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10177 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
35c08157 10178 {
1c8f6a4d
KLC
10179 /* Insn can be contracted to 16-bit implied r5. */
10180 comp_insn16 =
10181 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10182 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
35c08157 10183 }
1c8f6a4d
KLC
10184 }
10185 else if (N32_OP6 (insn) == N32_OP6_BR3)
10186 {
10187 /* bnec $ta, imm11, label. */
10188 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10189 }
10190 else
10191 {
10192 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10193 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10194 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
35c08157 10195 {
1c8f6a4d 10196 if (N32_IS_RT3 (insn))
35c08157 10197 {
1c8f6a4d
KLC
10198 /* Insn can be contracted to 16-bit. */
10199 comp_insn16 =
10200 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10201 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10202 }
10203 else if (N32_RT5 (insn) == REG_R15)
10204 {
10205 /* Insn can be contracted to 16-bit. */
10206 comp_insn16 =
10207 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
35c08157 10208 }
35c08157
KLC
10209 }
10210 }
1c8f6a4d
KLC
10211 }
10212 else
10213 {
10214 switch ((insn16 & 0xf000) >> 12)
35c08157 10215 {
1c8f6a4d
KLC
10216 case 0xc:
10217 /* beqz38 or bnez38 */
10218 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10219 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10220 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10221 break;
35c08157 10222
1c8f6a4d
KLC
10223 case 0xd:
10224 /* beqs38 or bnes38 */
10225 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10226 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10227 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10228 | (REG_R5 << 15);
10229 break;
35c08157 10230
1c8f6a4d
KLC
10231 case 0xe:
10232 /* beqzS8 or bnezS8 */
10233 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10234 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10235 comp_insn |= REG_R15 << 20;
10236 break;
35c08157 10237
1c8f6a4d
KLC
10238 default:
10239 break;
10240 }
10241 }
10242 if (comp_insn && re_insn)
10243 *re_insn = comp_insn;
10244 if (comp_insn16 && re_insn16)
10245 *re_insn16 = comp_insn16;
10246}
35c08157 10247
1c8f6a4d 10248/* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
35c08157 10249
1c8f6a4d
KLC
10250static bfd_boolean
10251nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10252 Elf_Internal_Rela *internal_relocs, int *insn_len,
10253 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10254 Elf_Internal_Shdr *symtab_hdr)
10255{
10256 /* There are 3 variations for LONGJUMP2
10257 case 2-4; 1st insn convertible, 16-bit on,
10258 optimize off or optimize for space
10259 bnes38 rt, ra, $1 ; LONGJUMP2
10260 j label ; 25_PCREL
10261 $1:
10262
10263 case 4-4; 1st insn not convertible
10264 bne rt, ra, $1 ; LONGJUMP2
10265 j label ; 25_PCREL
10266 $1:
10267
10268 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10269 bne rt, ra, $1 ; LONGJUMP2
10270 j label ; 25_PCREL
10271 $1: */
10272
10273 /* Get the reloc for the address from which the register is
10274 being loaded. This reloc will tell us which function is
10275 actually being called. */
10276
10277 bfd_vma laddr;
10278 int seq_len; /* Original length of instruction sequence. */
10279 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10280 int first_size;
1c8f6a4d
KLC
10281 unsigned int i;
10282 bfd_signed_vma foff;
10283 uint32_t insn, re_insn = 0;
10284 uint16_t insn16, re_insn16 = 0;
10285 unsigned long reloc, cond_reloc;
35c08157 10286
1c8f6a4d
KLC
10287 enum elf_nds32_reloc_type checked_types[] =
10288 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
35c08157 10289
1c8f6a4d
KLC
10290 irelend = internal_relocs + sec->reloc_count;
10291 seq_len = GET_SEQ_LEN (irel->r_addend);
10292 laddr = irel->r_offset;
10293 *insn_len = seq_len;
10294 first_size = (seq_len == 6) ? 2 : 4;
10295
10296 i2_irelfn =
10297 find_relocs_at_address_addr (irel, internal_relocs,
10298 irelend, R_NDS32_25_PCREL_RELA,
10299 laddr + first_size);
10300
fbaf61ad 10301 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10302 {
10303 cond_irelfn =
10304 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10305 checked_types[i], laddr);
10306 if (cond_irelfn != irelend)
10307 break;
10308 }
35c08157 10309
1c8f6a4d
KLC
10310 if (i2_irelfn == irelend || cond_irelfn == irelend)
10311 {
695344c0 10312 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
2dcf00ce 10313 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10314 return FALSE;
10315 }
35c08157 10316
1c8f6a4d 10317 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad
NC
10318 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10319 if (foff == 0
10320 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10321 || foff >= CONSERVATIVE_16BIT_S1)
10322 return FALSE;
35c08157 10323
1c8f6a4d
KLC
10324 /* Get the all corresponding instructions. */
10325 if (first_size == 4)
10326 {
10327 insn = bfd_getb32 (contents + laddr);
10328 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10329 }
10330 else
10331 {
10332 insn16 = bfd_getb16 (contents + laddr);
10333 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10334 }
35c08157 10335
1c8f6a4d
KLC
10336 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10337 && foff < ACCURATE_8BIT_S1 - first_size)
10338 {
10339 if (first_size == 4)
10340 {
10341 /* Don't convert it to 16-bit now, keep this as relaxable for
10342 ``label reloc; INSN16''. */
35c08157 10343
1c8f6a4d
KLC
10344 /* Save comp_insn32 to buffer. */
10345 bfd_putb32 (re_insn, contents + irel->r_offset);
10346 *insn_len = 4;
10347 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10348 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10349 cond_reloc = R_NDS32_INSN16;
10350 }
10351 else
10352 {
10353 bfd_putb16 (re_insn16, contents + irel->r_offset);
10354 *insn_len = 2;
10355 reloc = R_NDS32_9_PCREL_RELA;
10356 cond_reloc = R_NDS32_NONE;
10357 }
10358 }
10359 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10360 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10361 && foff < ACCURATE_14BIT_S1 - first_size))
10362 {
10363 /* beqs label ; 15_PCREL */
10364 bfd_putb32 (re_insn, contents + irel->r_offset);
10365 *insn_len = 4;
10366 reloc = R_NDS32_15_PCREL_RELA;
10367 cond_reloc = R_NDS32_NONE;
10368 }
10369 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10370 && foff >= -CONSERVATIVE_16BIT_S1
10371 && foff < CONSERVATIVE_16BIT_S1)
10372 {
10373 /* beqz label ; 17_PCREL */
10374 bfd_putb32 (re_insn, contents + irel->r_offset);
10375 *insn_len = 4;
10376 reloc = R_NDS32_17_PCREL_RELA;
10377 cond_reloc = R_NDS32_NONE;
10378 }
10379 else
10380 return FALSE;
35c08157 10381
1c8f6a4d
KLC
10382 /* Set all relocations. */
10383 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10384 irel->r_addend = i2_irelfn->r_addend;
35c08157 10385
1c8f6a4d
KLC
10386 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10387 cond_reloc);
10388 cond_irelfn->r_addend = 0;
35c08157 10389
1c8f6a4d
KLC
10390 if ((seq_len ^ *insn_len ) & 0x2)
10391 {
10392 insn16 = NDS32_NOP16;
10393 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10394 i2_irelfn->r_offset = 4;
10395 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10396 R_NDS32_INSN16);
10397 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10398 *insn_len += 2;
10399 }
10400 else
10401 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10402 R_NDS32_NONE);
10403 return TRUE;
10404}
35c08157 10405
1c8f6a4d 10406/* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
35c08157 10407
1c8f6a4d
KLC
10408static bfd_boolean
10409nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10410 Elf_Internal_Rela *internal_relocs, int *insn_len,
10411 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10412 Elf_Internal_Shdr *symtab_hdr)
10413{
10414 /* There are 5 variations for LONGJUMP3
10415 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10416 optimize off or optimize for space
07d6d2b8
AM
10417 bnes38 rt, ra, $1 ; LONGJUMP3
10418 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10419 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10420 jr5 ta ;
10421 $1: ;
1c8f6a4d
KLC
10422
10423 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
07d6d2b8
AM
10424 bnes38 rt, ra, $1 ; LONGJUMP3
10425 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10426 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10427 jr5 ta ;
10428 $1: ; LABEL
1c8f6a4d
KLC
10429
10430 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10431 optimize off or optimize for space
07d6d2b8
AM
10432 bne rt, ra, $1 ; LONGJUMP3
10433 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10434 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10435 jr5 ta ;
10436 $1: ;
1c8f6a4d
KLC
10437
10438 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10439 16-bit off if no INSN16
07d6d2b8
AM
10440 bne rt, ra, $1 ; LONGJUMP3
10441 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10442 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10443 jr ta ;
10444 $1: ;
1c8f6a4d
KLC
10445
10446 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10447 16-bit off if no INSN16
07d6d2b8
AM
10448 bne rt, ra, $1 ; LONGJUMP3
10449 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10450 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10451 jr ta ;
10452 $1: ; LABEL */
1c8f6a4d
KLC
10453
10454 /* Get the reloc for the address from which the register is
10455 being loaded. This reloc will tell us which function is
10456 actually being called. */
10457 enum elf_nds32_reloc_type checked_types[] =
10458 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10459
10460 int reloc_off = 0, cond_removed = 0, convertible;
10461 bfd_vma laddr;
10462 int seq_len; /* Original length of instruction sequence. */
10463 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10464 int first_size;
1c8f6a4d
KLC
10465 unsigned int i;
10466 bfd_signed_vma foff;
10467 uint32_t insn, re_insn = 0;
10468 uint16_t insn16, re_insn16 = 0;
10469 unsigned long reloc, cond_reloc;
35c08157 10470
1c8f6a4d
KLC
10471 irelend = internal_relocs + sec->reloc_count;
10472 seq_len = GET_SEQ_LEN (irel->r_addend);
10473 laddr = irel->r_offset;
10474 *insn_len = seq_len;
35c08157 10475
1c8f6a4d 10476 convertible = IS_1ST_CONVERT (irel->r_addend);
35c08157 10477
1c8f6a4d
KLC
10478 if (convertible)
10479 first_size = 2;
10480 else
10481 first_size = 4;
10482
10483 /* Get all needed relocations. */
10484 hi_irelfn =
10485 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10486 R_NDS32_HI20_RELA, laddr + first_size);
10487 lo_irelfn =
10488 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10489 R_NDS32_LO12S0_ORI_RELA,
10490 laddr + first_size + 4);
10491
fbaf61ad 10492 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10493 {
10494 cond_irelfn =
10495 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10496 checked_types[i], laddr);
10497 if (cond_irelfn != irelend)
10498 break;
10499 }
35c08157 10500
fbaf61ad
NC
10501 if (hi_irelfn == irelend
10502 || lo_irelfn == irelend
10503 || cond_irelfn == irelend)
1c8f6a4d 10504 {
695344c0 10505 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
2dcf00ce 10506 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10507 return FALSE;
10508 }
35c08157 10509
1c8f6a4d 10510 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10511 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10512
fbaf61ad
NC
10513 if (foff == 0
10514 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10515 || foff >= CONSERVATIVE_24BIT_S1)
10516 return FALSE;
35c08157 10517
1c8f6a4d
KLC
10518 /* Get the all corresponding instructions. */
10519 if (first_size == 4)
10520 {
10521 insn = bfd_getb32 (contents + laddr);
10522 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10523 }
10524 else
10525 {
10526 insn16 = bfd_getb16 (contents + laddr);
10527 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10528 }
35c08157 10529
1c8f6a4d
KLC
10530 /* For simplicity of coding, we are going to modify the section
10531 contents, the section relocs, and the BFD symbol table. We
10532 must tell the rest of the code not to free up this
10533 information. It would be possible to instead create a table
10534 of changes which have to be made, as is done in coff-mips.c;
10535 that would be more work, but would require less memory when
10536 the linker is run. */
35c08157 10537
fbaf61ad
NC
10538 if (re_insn16
10539 && foff >= -ACCURATE_8BIT_S1 - first_size
1c8f6a4d
KLC
10540 && foff < ACCURATE_8BIT_S1 - first_size)
10541 {
10542 if (!(seq_len & 0x2))
10543 {
10544 /* Don't convert it to 16-bit now, keep this as relaxable
10545 for ``label reloc; INSN1a''6. */
10546 /* Save comp_insn32 to buffer. */
10547 bfd_putb32 (re_insn, contents + irel->r_offset);
10548 *insn_len = 4;
10549 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10550 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10551 cond_reloc = R_NDS32_INSN16;
10552 }
10553 else
10554 {
10555 /* Not optimize for speed; convert sequence to 16-bit. */
10556 /* Save comp_insn16 to buffer. */
10557 bfd_putb16 (re_insn16, contents + irel->r_offset);
10558 *insn_len = 2;
10559 reloc = R_NDS32_9_PCREL_RELA;
10560 cond_reloc = R_NDS32_NONE;
10561 }
10562 cond_removed = 1;
10563 }
10564 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10565 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10566 && foff < ACCURATE_14BIT_S1 - first_size))
10567 {
10568 /* beqs label ; 15_PCREL */
10569 bfd_putb32 (re_insn, contents + irel->r_offset);
10570 *insn_len = 4;
10571 reloc = R_NDS32_15_PCREL_RELA;
10572 cond_reloc = R_NDS32_NONE;
10573 cond_removed = 1;
10574 }
10575 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10576 && foff >= -CONSERVATIVE_16BIT_S1
10577 && foff < CONSERVATIVE_16BIT_S1)
10578 {
10579 /* beqz label ; 17_PCREL */
10580 bfd_putb32 (re_insn, contents + irel->r_offset);
10581 *insn_len = 4;
10582 reloc = R_NDS32_17_PCREL_RELA;
10583 cond_reloc = R_NDS32_NONE;
10584 cond_removed = 1;
10585 }
10586 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10587 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10588 {
10589 /* Relax to one of the following 3 variations
10590
10591 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10592 for space
10593 bnes38 rt, $1 ; LONGJUMP2
10594 j label ; 25_PCREL
10595 $1
10596
10597 case 4-4; 1st insn not convertible, others don't care
10598 bne rt, ra, $1 ; LONGJUMP2
10599 j label ; 25_PCREL
10600 $1
10601
10602 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10603 bne rt, ra, $1 ; LONGJUMP2
10604 j label ; 25_PCREL
10605 $1 */
10606
10607 /* Offset for first instruction. */
10608
10609 /* Use j label as second instruction. */
10610 *insn_len = 4 + first_size;
10611 insn = INSN_J;
10612 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10613 reloc = R_NDS32_LONGJUMP2;
10614 cond_reloc = R_NDS32_25_PLTREL;
10615 }
10616 else
10617 return FALSE;
35c08157 10618
1c8f6a4d
KLC
10619 if (cond_removed == 1)
10620 {
10621 /* Set all relocations. */
10622 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10623 irel->r_addend = hi_irelfn->r_addend;
10624
10625 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10626 cond_reloc);
10627 cond_irelfn->r_addend = 0;
10628 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10629 R_NDS32_NONE);
10630 }
10631 else
10632 {
10633 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
fbaf61ad 10634 irel->r_addend = irel->r_addend;
1c8f6a4d
KLC
10635 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10636 cond_reloc);
10637 }
35c08157 10638
1c8f6a4d
KLC
10639 if ((seq_len ^ *insn_len ) & 0x2)
10640 {
10641 insn16 = NDS32_NOP16;
10642 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10643 lo_irelfn->r_offset = *insn_len;
10644 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10645 R_NDS32_INSN16);
10646 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10647 *insn_len += 2;
10648 }
10649 else
10650 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10651 R_NDS32_NONE);
10652 return TRUE;
10653}
35c08157 10654
1c8f6a4d 10655/* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
35c08157 10656
1c8f6a4d
KLC
10657static bfd_boolean
10658nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10659 Elf_Internal_Rela *internal_relocs, int *insn_len,
10660 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10661 Elf_Internal_Shdr *symtab_hdr)
10662{
10663 /* The pattern for LONGCALL4. Support for function cse.
10664 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10665 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10666 jral ta ; PTR_RES/EMPTY/INSN16 */
35c08157 10667
1c8f6a4d
KLC
10668 bfd_vma laddr;
10669 uint32_t insn;
10670 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10671 Elf_Internal_Rela *irelend;
1c8f6a4d 10672 bfd_signed_vma foff;
35c08157 10673
1c8f6a4d
KLC
10674 irelend = internal_relocs + sec->reloc_count;
10675 laddr = irel->r_offset;
35c08157 10676
1c8f6a4d
KLC
10677 /* Get the reloc for the address from which the register is
10678 being loaded. This reloc will tell us which function is
10679 actually being called. */
10680 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10681 R_NDS32_HI20_RELA, laddr);
35c08157 10682
1c8f6a4d
KLC
10683 if (hi_irel == irelend)
10684 {
695344c0 10685 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10686 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10687 return FALSE;
10688 }
35c08157 10689
1c8f6a4d 10690 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10691 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10692
1c8f6a4d 10693 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
10694 if (foff == 0
10695 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10696 || foff >= CONSERVATIVE_24BIT_S1)
10697 return FALSE;
35c08157 10698
fbaf61ad 10699 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
10700 /* For simplicity of coding, we are going to modify the section
10701 contents, the section relocs, and the BFD symbol table. We
10702 must tell the rest of the code not to free up this
10703 information. It would be possible to instead create a table
10704 of changes which have to be made, as is done in coff-mips.c;
10705 that would be more work, but would require less memory when
10706 the linker is run. */
35c08157 10707
1c8f6a4d
KLC
10708 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10709 R_NDS32_PTR_RESOLVED, irel->r_addend);
10710 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10711 R_NDS32_EMPTY, irel->r_addend);
35c08157 10712
1c8f6a4d
KLC
10713 if (ptr_irel == irelend || em_irel == irelend)
10714 {
695344c0 10715 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10716 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10717 return FALSE;
10718 }
10719 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10720 insn = bfd_getb32 (contents + irel->r_addend);
10721 if (insn & 0x80000000)
10722 return FALSE;
35c08157 10723
1c8f6a4d
KLC
10724 /* Replace the long call with a jal. */
10725 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10726 R_NDS32_25_PCREL_RELA);
10727 ptr_irel->r_addend = 1;
35c08157 10728
1c8f6a4d
KLC
10729 /* We don't resolve this here but resolve it in relocate_section. */
10730 insn = INSN_JAL;
10731 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10732
1c8f6a4d
KLC
10733 irel->r_info =
10734 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10735
1c8f6a4d
KLC
10736 /* If there is function cse, HI20 can not remove now. */
10737 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10738 R_NDS32_LONGCALL4, laddr);
10739 if (call_irel == irelend)
10740 {
10741 *insn_len = 0;
10742 hi_irel->r_info =
10743 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10744 }
35c08157 10745
1c8f6a4d
KLC
10746 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10747 R_NDS32_INSN16, irel->r_addend);
10748 if (insn_irel != irelend)
10749 insn_irel->r_info =
10750 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10751
1c8f6a4d
KLC
10752 return TRUE;
10753}
35c08157 10754
1c8f6a4d 10755/* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
35c08157 10756
1c8f6a4d
KLC
10757static bfd_boolean
10758nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10759 Elf_Internal_Rela *internal_relocs, int *insn_len,
10760 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10761 Elf_Internal_Shdr *symtab_hdr)
10762{
10763 /* The pattern for LONGCALL5.
10764 bltz rt, .L1 ; LONGCALL5/17_PCREL
10765 jal symbol ; 25_PCREL
10766 .L1: */
35c08157 10767
1c8f6a4d
KLC
10768 bfd_vma laddr;
10769 uint32_t insn;
10770 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d 10771 bfd_signed_vma foff;
35c08157 10772
1c8f6a4d
KLC
10773 irelend = internal_relocs + sec->reloc_count;
10774 laddr = irel->r_offset;
10775 insn = bfd_getb32 (contents + laddr);
35c08157 10776
1c8f6a4d
KLC
10777 /* Get the reloc for the address from which the register is
10778 being loaded. This reloc will tell us which function is
10779 actually being called. */
10780 cond_irel =
10781 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10782 R_NDS32_25_PCREL_RELA, irel->r_addend);
10783 if (cond_irel == irelend)
10784 {
695344c0 10785 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
2dcf00ce 10786 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10787 return FALSE;
10788 }
35c08157 10789
1c8f6a4d 10790 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10791 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 10792
fbaf61ad
NC
10793 if (foff == 0
10794 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10795 || foff >= CONSERVATIVE_16BIT_S1)
10796 return FALSE;
35c08157 10797
1c8f6a4d 10798 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10799 or bltzal rt, label ; 17_PCREL. */
35c08157 10800
1c8f6a4d
KLC
10801 /* Convert to complimentary conditional call. */
10802 insn = CONVERT_CONDITION_CALL (insn);
35c08157 10803
1c8f6a4d
KLC
10804 /* For simplicity of coding, we are going to modify the section
10805 contents, the section relocs, and the BFD symbol table. We
10806 must tell the rest of the code not to free up this
10807 information. It would be possible to instead create a table
10808 of changes which have to be made, as is done in coff-mips.c;
10809 that would be more work, but would require less memory when
10810 the linker is run. */
35c08157 10811
1c8f6a4d
KLC
10812 /* Modify relocation and contents. */
10813 cond_irel->r_info =
10814 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
35c08157 10815
1c8f6a4d
KLC
10816 /* Replace the long call with a bgezal. */
10817 bfd_putb32 (insn, contents + cond_irel->r_offset);
10818 *insn_len = 0;
35c08157 10819
1c8f6a4d
KLC
10820 /* Clean unnessary relocations. */
10821 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10822
1c8f6a4d
KLC
10823 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10824 R_NDS32_17_PCREL_RELA, laddr);
10825 cond_irel->r_info =
10826 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10827
1c8f6a4d
KLC
10828 return TRUE;
10829}
35c08157 10830
1c8f6a4d 10831/* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
35c08157 10832
1c8f6a4d
KLC
10833static bfd_boolean
10834nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10835 Elf_Internal_Rela *internal_relocs, int *insn_len,
10836 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10837 Elf_Internal_Shdr *symtab_hdr)
10838{
10839 /* The pattern for LONGCALL6.
10840 bltz rt, .L1 ; LONGCALL6/17_PCREL
10841 sethi ta, hi20(symbol) ; HI20/PTR
10842 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10843 jral ta ; PTR_RES/EMPTY/INSN16
10844 .L1 */
10845
10846 bfd_vma laddr;
10847 uint32_t insn;
10848 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
1c8f6a4d 10849 bfd_signed_vma foff;
35c08157 10850
1c8f6a4d
KLC
10851 irelend = internal_relocs + sec->reloc_count;
10852 laddr = irel->r_offset;
35c08157 10853
1c8f6a4d
KLC
10854 /* Get the reloc for the address from which the register is
10855 being loaded. This reloc will tell us which function is
10856 actually being called. */
10857 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10858 R_NDS32_EMPTY, irel->r_addend);
35c08157 10859
1c8f6a4d
KLC
10860 if (em_irel == irelend)
10861 {
695344c0 10862 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
2dcf00ce 10863 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10864 return FALSE;
10865 }
35c08157 10866
1c8f6a4d 10867 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10868 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 10869
fbaf61ad
NC
10870 if (foff == 0
10871 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10872 || foff >= CONSERVATIVE_24BIT_S1)
10873 return FALSE;
35c08157 10874
1c8f6a4d
KLC
10875 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10876 insn = bfd_getb32 (contents + irel->r_addend);
10877 if (insn & 0x80000000)
10878 return FALSE;
35c08157 10879
1c8f6a4d
KLC
10880 insn = bfd_getb32 (contents + laddr);
10881 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10882 {
10883 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10884 or bltzal rt, label ; 17_PCREL. */
35c08157 10885
1c8f6a4d
KLC
10886 /* Convert to complimentary conditional call. */
10887 *insn_len = 0;
10888 insn = CONVERT_CONDITION_CALL (insn);
10889 bfd_putb32 (insn, contents + em_irel->r_offset);
10890
10891 em_irel->r_info =
10892 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10893
10894 /* Set resolved relocation. */
10895 cond_irel =
10896 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10897 R_NDS32_PTR_RESOLVED, irel->r_addend);
10898 if (cond_irel == irelend)
35c08157 10899 {
695344c0 10900 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10901 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10902 return FALSE;
10903 }
10904 cond_irel->r_addend = 1;
35c08157 10905
1c8f6a4d 10906 /* Clear relocations. */
35c08157 10907
1c8f6a4d
KLC
10908 irel->r_info =
10909 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10910
1c8f6a4d
KLC
10911 cond_irel =
10912 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10913 R_NDS32_17_PCREL_RELA, laddr);
10914 if (cond_irel != irelend)
10915 cond_irel->r_info =
10916 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10917
1c8f6a4d
KLC
10918 cond_irel =
10919 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10920 R_NDS32_INSN16, irel->r_addend);
10921 if (cond_irel != irelend)
10922 cond_irel->r_info =
10923 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10924
1c8f6a4d
KLC
10925 }
10926 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10927 {
10928 /* Relax to the following instruction sequence
10929 bltz rt, .L1 ; LONGCALL2/17_PCREL
10930 jal symbol ; 25_PCREL/PTR_RES
10931 .L1 */
10932 *insn_len = 4;
10933 /* Convert instruction. */
10934 insn = INSN_JAL;
10935 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10936
1c8f6a4d
KLC
10937 /* Convert relocations. */
10938 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10939 R_NDS32_25_PCREL_RELA);
10940 irel->r_info =
10941 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
35c08157 10942
1c8f6a4d
KLC
10943 /* Set resolved relocation. */
10944 cond_irel =
10945 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10946 R_NDS32_PTR_RESOLVED, irel->r_addend);
10947 if (cond_irel == irelend)
10948 {
695344c0 10949 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10950 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10951 return FALSE;
10952 }
10953 cond_irel->r_addend = 1;
35c08157 10954
1c8f6a4d
KLC
10955 cond_irel =
10956 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10957 R_NDS32_INSN16, irel->r_addend);
10958 if (cond_irel != irelend)
10959 cond_irel->r_info =
10960 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10961 }
10962 return TRUE;
10963}
35c08157 10964
1c8f6a4d 10965/* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
35c08157 10966
1c8f6a4d
KLC
10967static bfd_boolean
10968nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10969 Elf_Internal_Rela *internal_relocs, int *insn_len,
10970 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10971 Elf_Internal_Shdr *symtab_hdr)
10972{
10973 /* The pattern for LONGJUMP4.
10974 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10975 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10976 jr ta ; PTR_RES/INSN16/EMPTY */
10977
10978 bfd_vma laddr;
10979 int seq_len; /* Original length of instruction sequence. */
10980 uint32_t insn;
10981 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
1c8f6a4d 10982 bfd_signed_vma foff;
35c08157 10983
1c8f6a4d
KLC
10984 irelend = internal_relocs + sec->reloc_count;
10985 seq_len = GET_SEQ_LEN (irel->r_addend);
10986 laddr = irel->r_offset;
10987 *insn_len = seq_len;
35c08157 10988
1c8f6a4d
KLC
10989 /* Get the reloc for the address from which the register is
10990 being loaded. This reloc will tell us which function is
10991 actually being called. */
35c08157 10992
1c8f6a4d
KLC
10993 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10994 R_NDS32_HI20_RELA, laddr);
35c08157 10995
1c8f6a4d
KLC
10996 if (hi_irel == irelend)
10997 {
695344c0 10998 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 10999 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11000 return FALSE;
11001 }
35c08157 11002
1c8f6a4d 11003 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11004 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 11005
fbaf61ad
NC
11006 if (foff == 0
11007 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
11008 || foff < -CONSERVATIVE_24BIT_S1)
11009 return FALSE;
35c08157 11010
1c8f6a4d
KLC
11011 /* Convert it to "j label", it may be converted to j8 in the final
11012 pass of relaxation. Therefore, we do not consider this currently. */
11013 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11014 R_NDS32_PTR_RESOLVED, irel->r_addend);
11015 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11016 R_NDS32_EMPTY, irel->r_addend);
35c08157 11017
1c8f6a4d
KLC
11018 if (ptr_irel == irelend || em_irel == irelend)
11019 {
695344c0 11020 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 11021 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11022 return FALSE;
11023 }
35c08157 11024
1c8f6a4d
KLC
11025 em_irel->r_info =
11026 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11027 ptr_irel->r_addend = 1;
35c08157 11028
1c8f6a4d
KLC
11029 /* Write instruction. */
11030 insn = INSN_J;
11031 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 11032
1c8f6a4d
KLC
11033 /* Clear relocations. */
11034 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11035
1c8f6a4d
KLC
11036 /* If there is function cse, HI20 can not remove now. */
11037 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11038 R_NDS32_LONGJUMP4, laddr);
11039 if (call_irel == irelend)
11040 {
11041 *insn_len = 0;
11042 hi_irel->r_info =
11043 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11044 }
35c08157 11045
1c8f6a4d
KLC
11046 return TRUE;
11047}
35c08157 11048
1c8f6a4d 11049/* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
35c08157 11050
1c8f6a4d
KLC
11051static bfd_boolean
11052nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11053 Elf_Internal_Rela *internal_relocs, int *insn_len,
11054 int *seq_len, bfd_byte *contents,
11055 Elf_Internal_Sym *isymbuf,
11056 Elf_Internal_Shdr *symtab_hdr)
11057{
11058 /* There are 2 variations for LONGJUMP5
11059 case 2-4; 1st insn convertible, 16-bit on.
11060 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11061 j label ; 25_PCREL/INSN16
11062 $1:
11063
11064 case 4-4; 1st insn not convertible
11065 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11066 j label ; 25_PCREL/INSN16
11067 .L1: */
11068
11069 bfd_vma laddr;
11070 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d
KLC
11071 unsigned int i;
11072 bfd_signed_vma foff;
11073 uint32_t insn, re_insn = 0;
11074 uint16_t insn16, re_insn16 = 0;
11075 unsigned long reloc;
35c08157 11076
1c8f6a4d
KLC
11077 enum elf_nds32_reloc_type checked_types[] =
11078 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11079 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
35c08157 11080
1c8f6a4d
KLC
11081 irelend = internal_relocs + sec->reloc_count;
11082 laddr = irel->r_offset;
35c08157 11083
1c8f6a4d
KLC
11084 /* Get the reloc for the address from which the register is
11085 being loaded. This reloc will tell us which function is
11086 actually being called. */
35c08157 11087
1c8f6a4d
KLC
11088 cond_irel =
11089 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11090 R_NDS32_25_PCREL_RELA, irel->r_addend);
11091 if (cond_irel == irelend)
11092 {
695344c0 11093 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
2dcf00ce 11094 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11095 return FALSE;
11096 }
35c08157 11097
1c8f6a4d 11098 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11099 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11100
fbaf61ad
NC
11101 if (foff == 0
11102 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
11103 || foff >= CONSERVATIVE_16BIT_S1)
11104 return FALSE;
35c08157 11105
1c8f6a4d
KLC
11106 /* Get the all corresponding instructions. */
11107 insn = bfd_getb32 (contents + laddr);
11108 /* Check instruction size. */
11109 if (insn & 0x80000000)
11110 {
11111 *seq_len = 0;
11112 insn16 = insn >> 16;
11113 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11114 }
11115 else
11116 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11117
1c8f6a4d
KLC
11118 if (N32_OP6 (re_insn) == N32_OP6_BR1
11119 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11120 {
11121 /* beqs label ; 15_PCREL. */
11122 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11123 reloc = R_NDS32_15_PCREL_RELA;
11124 }
11125 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11126 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11127 {
11128 /* beqz label ; 17_PCREL. */
11129 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11130 reloc = R_NDS32_17_PCREL_RELA;
11131 }
11132 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11133 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11134 {
11135 /* beqc label ; 9_PCREL. */
11136 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11137 reloc = R_NDS32_WORD_9_PCREL_RELA;
11138 }
11139 else
11140 return FALSE;
35c08157 11141
1c8f6a4d
KLC
11142 /* Set all relocations. */
11143 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
35c08157 11144
1c8f6a4d
KLC
11145 /* Clean relocations. */
11146 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
fbaf61ad 11147 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11148 {
11149 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11150 checked_types[i], laddr);
11151 if (cond_irel != irelend)
11152 {
11153 if (*seq_len == 0
11154 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
35c08157 11155 {
1c8f6a4d
KLC
11156 /* If the branch instruction is 2 byte, it cannot remove
11157 directly. Only convert it to nop16 and remove it after
11158 checking alignment issue. */
11159 insn16 = NDS32_NOP16;
11160 bfd_putb16 (insn16, contents + laddr);
11161 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157
KLC
11162 }
11163 else
1c8f6a4d
KLC
11164 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11165 R_NDS32_NONE);
35c08157 11166 }
1c8f6a4d
KLC
11167 }
11168 *insn_len = 0;
35c08157 11169
1c8f6a4d
KLC
11170 return TRUE;
11171}
35c08157 11172
1c8f6a4d 11173/* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
35c08157 11174
1c8f6a4d
KLC
11175static bfd_boolean
11176nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11177 Elf_Internal_Rela *internal_relocs, int *insn_len,
11178 int *seq_len, bfd_byte *contents,
11179 Elf_Internal_Sym *isymbuf,
11180 Elf_Internal_Shdr *symtab_hdr)
11181{
11182 /* There are 5 variations for LONGJUMP6
11183 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11184 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11185 sethi ta, hi20(symbol) ; HI20/PTR
11186 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11187 jr ta ; PTR_RES/INSN16/EMPTY
11188 .L1:
11189
11190 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11191 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11192 sethi ta, hi20(symbol) ; HI20/PTR
11193 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11194 jr ta ; PTR_RES/INSN16/EMPTY
11195 .L1: */
11196
11197 enum elf_nds32_reloc_type checked_types[] =
11198 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11199 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11200
11201 int reloc_off = 0, cond_removed = 0;
11202 bfd_vma laddr;
11203 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11204 unsigned int i;
11205 bfd_signed_vma foff;
11206 uint32_t insn, re_insn = 0;
11207 uint16_t insn16, re_insn16 = 0;
11208 unsigned long reloc;
35c08157 11209
1c8f6a4d
KLC
11210 irelend = internal_relocs + sec->reloc_count;
11211 laddr = irel->r_offset;
35c08157 11212
1c8f6a4d
KLC
11213 /* Get the reloc for the address from which the register is
11214 being loaded. This reloc will tell us which function is
11215 actually being called. */
11216 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11217 R_NDS32_EMPTY, irel->r_addend);
35c08157 11218
1c8f6a4d
KLC
11219 if (em_irel == irelend)
11220 {
695344c0 11221 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
2dcf00ce 11222 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11223 return FALSE;
11224 }
35c08157 11225
1c8f6a4d 11226 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11227 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 11228
fbaf61ad
NC
11229 if (foff == 0
11230 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
11231 || foff >= CONSERVATIVE_24BIT_S1)
11232 return FALSE;
35c08157 11233
1c8f6a4d
KLC
11234 insn = bfd_getb32 (contents + laddr);
11235 /* Check instruction size. */
11236 if (insn & 0x80000000)
11237 {
11238 *seq_len = 0;
11239 insn16 = insn >> 16;
11240 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11241 }
11242 else
11243 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11244
1c8f6a4d
KLC
11245 /* For simplicity of coding, we are going to modify the section
11246 contents, the section relocs, and the BFD symbol table. We
11247 must tell the rest of the code not to free up this
11248 information. It would be possible to instead create a table
11249 of changes which have to be made, as is done in coff-mips.c;
11250 that would be more work, but would require less memory when
11251 the linker is run. */
35c08157 11252
1c8f6a4d
KLC
11253 if (N32_OP6 (re_insn) == N32_OP6_BR1
11254 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11255 {
fbaf61ad 11256 /* beqs label ; 15_PCREL. */
1c8f6a4d
KLC
11257 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11258 reloc = R_NDS32_15_PCREL_RELA;
11259 cond_removed = 1;
11260 }
11261 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11262 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11263 {
fbaf61ad 11264 /* beqz label ; 17_PCREL. */
1c8f6a4d
KLC
11265 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11266 reloc = R_NDS32_17_PCREL_RELA;
11267 cond_removed = 1;
11268 }
11269 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11270 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11271 {
11272 /* Relax to one of the following 2 variations
35c08157 11273
1c8f6a4d
KLC
11274 case 2-4; 1st insn convertible, 16-bit on.
11275 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11276 j label ; 25_PCREL/INSN16
11277 $1:
35c08157 11278
1c8f6a4d
KLC
11279 case 4-4; 1st insn not convertible
11280 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11281 j label ; 25_PCREL/INSN16
11282 .L1: */
35c08157 11283
1c8f6a4d
KLC
11284 /* Use j label as second instruction. */
11285 insn = INSN_J;
11286 reloc = R_NDS32_25_PCREL_RELA;
11287 bfd_putb32 (insn, contents + em_irel->r_offset);
11288 }
11289 else
11290 return FALSE;
35c08157 11291
1c8f6a4d
KLC
11292 /* Set all relocations. */
11293 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
35c08157 11294
1c8f6a4d
KLC
11295 cond_irel =
11296 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11297 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11298 cond_irel->r_addend = 1;
35c08157 11299
1c8f6a4d
KLC
11300 /* Use INSN16 of first branch instruction to distinguish if keeping
11301 INSN16 of final instruction or not. */
11302 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11303 R_NDS32_INSN16, irel->r_offset);
11304 if (insn_irel == irelend)
11305 {
11306 /* Clean the final INSN16. */
11307 insn_irel =
11308 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11309 R_NDS32_INSN16, em_irel->r_offset);
11310 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11311 R_NDS32_NONE);
11312 }
11313
11314 if (cond_removed == 1)
11315 {
11316 *insn_len = 0;
11317
11318 /* Clear relocations. */
11319 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11320
fbaf61ad 11321 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11322 {
11323 cond_irel =
11324 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11325 checked_types[i], laddr);
11326 if (cond_irel != irelend)
35c08157 11327 {
1c8f6a4d
KLC
11328 if (*seq_len == 0
11329 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11330 {
11331 /* If the branch instruction is 2 byte, it cannot remove
11332 directly. Only convert it to nop16 and remove it after
11333 checking alignment issue. */
11334 insn16 = NDS32_NOP16;
11335 bfd_putb16 (insn16, contents + laddr);
11336 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11337 }
11338 else
11339 cond_irel->r_info =
11340 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 11341 }
35c08157 11342 }
1c8f6a4d
KLC
11343 }
11344 else
11345 {
11346 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11347 R_NDS32_LONGJUMP5);
11348 }
35c08157 11349
1c8f6a4d
KLC
11350 return TRUE;
11351}
35c08157 11352
1c8f6a4d 11353/* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
35c08157 11354
1c8f6a4d
KLC
11355static bfd_boolean
11356nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11357 Elf_Internal_Rela *internal_relocs, int *insn_len,
11358 int *seq_len, bfd_byte *contents,
11359 Elf_Internal_Sym *isymbuf,
11360 Elf_Internal_Shdr *symtab_hdr)
11361{
11362 /* There are 2 variations for LONGJUMP5
11363 case 2-4; 1st insn convertible, 16-bit on.
11364 movi55 ta, imm11 ; LONGJUMP7/INSN16
11365 beq rt, ta, label ; 15_PCREL
11366
11367 case 4-4; 1st insn not convertible
11368 movi55 ta, imm11 ; LONGJUMP7/INSN16
11369 beq rt, ta, label ; 15_PCREL */
11370
11371 bfd_vma laddr;
11372 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11373 bfd_signed_vma foff;
11374 uint32_t insn, re_insn = 0;
11375 uint16_t insn16;
11376 uint32_t imm11;
35c08157 11377
1c8f6a4d
KLC
11378 irelend = internal_relocs + sec->reloc_count;
11379 laddr = irel->r_offset;
35c08157 11380
1c8f6a4d
KLC
11381 /* Get the reloc for the address from which the register is
11382 being loaded. This reloc will tell us which function is
11383 actually being called. */
35c08157 11384
1c8f6a4d
KLC
11385 cond_irel =
11386 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11387 R_NDS32_15_PCREL_RELA, irel->r_addend);
11388 if (cond_irel == irelend)
11389 {
695344c0 11390 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
2dcf00ce 11391 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11392 return FALSE;
11393 }
35c08157 11394
1c8f6a4d 11395 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11396 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11397
fbaf61ad
NC
11398 if (foff == 0
11399 || foff < -CONSERVATIVE_8BIT_S1
1c8f6a4d
KLC
11400 || foff >= CONSERVATIVE_8BIT_S1)
11401 return FALSE;
35c08157 11402
1c8f6a4d
KLC
11403 /* Get the first instruction for its size. */
11404 insn = bfd_getb32 (contents + laddr);
11405 if (insn & 0x80000000)
11406 {
11407 *seq_len = 0;
11408 /* Get the immediate from movi55. */
11409 imm11 = N16_IMM5S (insn >> 16);
11410 }
11411 else
11412 {
11413 /* Get the immediate from movi. */
11414 imm11 = N32_IMM20S (insn);
35c08157
KLC
11415 }
11416
1c8f6a4d
KLC
11417 /* Get the branch instruction. */
11418 insn = bfd_getb32 (contents + irel->r_addend);
11419 /* Convert instruction to BR3. */
11420 if ((insn >> 14) & 0x1)
11421 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11422 else
11423 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
35c08157 11424
1c8f6a4d 11425 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
35c08157 11426
1c8f6a4d
KLC
11427 /* Set all relocations. */
11428 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11429 R_NDS32_WORD_9_PCREL_RELA);
11430
11431 /* Clean relocations. */
11432 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11433 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11434 R_NDS32_INSN16, irel->r_offset);
11435 if (insn_irel != irelend)
11436 {
11437 if (*seq_len == 0)
35c08157 11438 {
1c8f6a4d
KLC
11439 /* If the first insntruction is 16bit, convert it to nop16. */
11440 insn16 = NDS32_NOP16;
11441 bfd_putb16 (insn16, contents + laddr);
11442 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157 11443 }
1c8f6a4d
KLC
11444 else
11445 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11446 R_NDS32_NONE);
35c08157 11447 }
1c8f6a4d 11448 *insn_len = 0;
35c08157 11449
1c8f6a4d
KLC
11450 return TRUE;
11451}
35c08157 11452
fbaf61ad
NC
11453/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11454 for each relax round. But the gp may changed dramatically and then cause
11455 the truncated to fit errors for the the converted gp instructions.
11456 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11457
11458static bfd_boolean
11459nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11460 Elf_Internal_Rela *irel, bfd_boolean *again,
11461 bfd_boolean init,
11462 struct elf_nds32_link_hash_table *table,
11463 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11464
11465{
11466 int offset_to_gp;
11467 static bfd_boolean sec_pass = FALSE;
11468 static asection *first_sec = NULL, *sym_sec;
11469 /* Record the number of instructions which may be removed. */
11470 static int count = 0, record_count;
11471 Elf_Internal_Sym *isym;
11472 struct elf_link_hash_entry *h = NULL;
11473 int indx;
11474 unsigned long r_symndx;
11475 bfd *abfd = sec->owner;
11476 static bfd_vma record_sda = 0;
11477 int sda_offset = 0;
11478
11479 /* Force doing relaxation when hyper-relax is high. */
11480 if (table->hyper_relax == 2)
11481 return TRUE;
11482
11483 /* Do not relax the load/store patterns for the first
11484 relax round. */
11485 if (init)
11486 {
11487 if (!first_sec)
11488 first_sec = sec;
11489 else if (first_sec == sec)
11490 {
11491 record_count = count;
11492 count = 0;
11493 sec_pass = TRUE;
11494 }
11495
11496 if (!sec_pass)
11497 *again = TRUE;
11498
11499 return TRUE;
11500 }
11501
11502 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11503 DATA_SEGMENT_ALIGN size in the linker script may make it
11504 get even bigger. */
11505 if (record_sda == 0)
11506 record_sda = local_sda;
11507 else if (local_sda > record_sda)
11508 sda_offset = local_sda - record_sda;
11509
11510 /* Assume the instruction will be removed in the best case. */
11511 count++;
11512
11513 /* We record the offset to gp for each symbol, and then check
11514 if it is changed dramatically after relaxing.
11515 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11516 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11517 r_symndx = ELF32_R_SYM (irel->r_info);
11518 if (r_symndx >= symtab_hdr->sh_info)
11519 {
11520 /* Global symbols. */
11521 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11522 h = elf_sym_hashes (abfd)[indx];
11523 sym_sec = h->root.u.def.section;
11524 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11525 || bfd_is_abs_section (sym_sec))
11526 {
11527 /* Forbid doing relaxation when hyper-relax is low. */
11528 if (table->hyper_relax == 0)
11529 return FALSE;
11530
11531 offset_to_gp = *access_addr - local_sda;
11532 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11533 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11534 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11535 < abs (offset_to_gp) - sda_offset)
11536 {
11537 /* This may cause the error, so we reserve the
11538 safe enough size for relaxing. */
11539 if (*access_addr >= local_sda)
11540 *access_addr += (record_count * 4);
11541 else
11542 *access_addr -= (record_count * 4);
11543 }
11544 return sec_pass;
11545 }
11546 }
11547 else
11548 {
11549 /* Local symbols. */
11550 if (!elf32_nds32_allocate_local_sym_info (abfd))
11551 return FALSE;
11552 isym = isymbuf + r_symndx;
11553
11554 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11555 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11556 {
11557 /* Forbid doing relaxation when hyper-relax is low. */
11558 if (table->hyper_relax == 0)
11559 return FALSE;
11560
11561 offset_to_gp = *access_addr - local_sda;
11562 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11563 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11564 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11565 < abs (offset_to_gp) - sda_offset)
11566 {
11567 /* This may cause the error, so we reserve the
11568 safe enough size for relaxing. */
11569 if (*access_addr >= local_sda)
11570 *access_addr += (record_count * 4);
11571 else
11572 *access_addr -= (record_count * 4);
11573 }
11574 return sec_pass;
11575 }
11576 }
11577
11578 return TRUE;
11579}
11580
1c8f6a4d 11581#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
35c08157 11582
1c8f6a4d 11583/* Relax LOADSTORE relocation for nds32_elf_relax_section. */
35c08157 11584
1c8f6a4d
KLC
11585static bfd_boolean
11586nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11587 asection *sec, Elf_Internal_Rela *irel,
11588 Elf_Internal_Rela *internal_relocs, int *insn_len,
11589 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
fbaf61ad
NC
11590 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11591 struct elf_nds32_link_hash_table *table)
1c8f6a4d 11592{
6cae483a
AM
11593 int eliminate_sethi = 0, range_type;
11594 unsigned int i;
1c8f6a4d
KLC
11595 bfd_vma local_sda, laddr;
11596 int seq_len; /* Original length of instruction sequence. */
11597 uint32_t insn;
11598 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11599 bfd_vma access_addr = 0;
11600 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
fbaf61ad
NC
11601 struct elf_link_hash_entry *h = NULL;
11602 int indx;
1c8f6a4d
KLC
11603 enum elf_nds32_reloc_type checked_types[] =
11604 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11605 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11606 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11607 R_NDS32_TLS_LE_HI20
11608 };
35c08157 11609
1c8f6a4d
KLC
11610 irelend = internal_relocs + sec->reloc_count;
11611 seq_len = GET_SEQ_LEN (irel->r_addend);
11612 laddr = irel->r_offset;
11613 *insn_len = seq_len;
35c08157 11614
1c8f6a4d 11615 /* Get the high part relocation. */
6cae483a 11616 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11617 {
11618 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11619 checked_types[i], laddr);
11620 if (hi_irelfn != irelend)
11621 break;
11622 }
35c08157 11623
1c8f6a4d
KLC
11624 if (hi_irelfn == irelend)
11625 {
fbaf61ad
NC
11626 /* Not R_NDS32_HI20_RELA. */
11627 if (i != 0)
11628 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11629 (uint64_t) irel->r_offset);
11630 return FALSE;
1c8f6a4d 11631 }
35c08157 11632
1c8f6a4d
KLC
11633 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11634 nds32_elf_final_sda_base (sec->output_section->owner,
11635 link_info, &local_sda, FALSE);
35c08157 11636
1c8f6a4d
KLC
11637 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11638 {
11639 case R_NDS32_HI20_RELA:
11640 insn = bfd_getb32 (contents + laddr);
11641 access_addr =
11642 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11643
fbaf61ad 11644 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
1c8f6a4d 11645 {
fbaf61ad
NC
11646 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11647 h = elf_sym_hashes (abfd)[indx];
11648 }
35c08157 11649
fbaf61ad
NC
11650 /* Try movi. */
11651 if (range_type == NDS32_LOADSTORE_IMM
11652 && access_addr < CONSERVATIVE_20BIT
11653 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11654 {
11655 eliminate_sethi = 1;
11656 break;
11657 }
1c8f6a4d 11658
fbaf61ad
NC
11659 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11660 {
11661 eliminate_sethi = 1;
11662 break;
1c8f6a4d 11663 }
fbaf61ad
NC
11664 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11665 NULL, FALSE, table, isymbuf, symtab_hdr))
11666 return FALSE;
1c8f6a4d
KLC
11667
11668 if (!load_store_relax)
11669 return FALSE;
11670
11671 /* Case for set gp register. */
11672 if (N32_RT5 (insn) == REG_GP)
fbaf61ad 11673 return FALSE;
1c8f6a4d
KLC
11674
11675 if (range_type == NDS32_LOADSTORE_FLOAT_S
6cef73f9 11676 || range_type == NDS32_LOADSTORE_FLOAT_D)
1c8f6a4d
KLC
11677 {
11678 range_l = sdata_range[0][0];
11679 range_h = sdata_range[0][1];
11680 }
11681 else
11682 {
11683 range_l = sdata_range[1][0];
11684 range_h = sdata_range[1][1];
11685 }
11686 break;
11687
1c8f6a4d
KLC
11688 default:
11689 return FALSE;
11690 }
11691
11692 /* Delete sethi instruction. */
11693 if (eliminate_sethi == 1
11694 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11695 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11696 {
11697 hi_irelfn->r_info =
11698 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11699 irel->r_info =
11700 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11701 *insn_len = 0;
fbaf61ad 11702 return TRUE;
1c8f6a4d 11703 }
fbaf61ad
NC
11704
11705 return FALSE;
1c8f6a4d
KLC
11706}
11707
11708/* Relax LO12 relocation for nds32_elf_relax_section. */
11709
11710static void
11711nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11712 asection *sec, Elf_Internal_Rela *irel,
11713 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
fbaf61ad
NC
11714 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11715 struct elf_nds32_link_hash_table *table)
1c8f6a4d
KLC
11716{
11717 uint32_t insn;
11718 bfd_vma local_sda, laddr;
11719 unsigned long reloc;
11720 bfd_vma access_addr;
11721 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11722 Elf_Internal_Rela *irelfn = NULL, *irelend;
11723 struct elf_link_hash_entry *h = NULL;
11724 int indx;
11725
11726 /* For SDA base relative relaxation. */
11727 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11728 &local_sda, FALSE);
11729
11730 irelend = internal_relocs + sec->reloc_count;
11731 laddr = irel->r_offset;
11732 insn = bfd_getb32 (contents + laddr);
11733
11734 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11735 return;
11736
11737 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11738
11739 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11740 {
11741 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11742 h = elf_sym_hashes (abfd)[indx];
11743 }
11744
fbaf61ad 11745 /* Try movi. */
1c8f6a4d
KLC
11746 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11747 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11748 {
11749 reloc = R_NDS32_20_RELA;
11750 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11751 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11752 bfd_putb32 (insn, contents + laddr);
11753 }
1c8f6a4d
KLC
11754 else
11755 {
fbaf61ad
NC
11756 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11757 {
11758 /* Fall through. */
11759 }
11760 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11761 FALSE, table, isymbuf, symtab_hdr))
11762 return;
11763
1c8f6a4d
KLC
11764 range_l = sdata_range[1][0];
11765 range_h = sdata_range[1][1];
11766 switch (ELF32_R_TYPE (irel->r_info))
11767 {
11768 case R_NDS32_LO12S0_RELA:
11769 reloc = R_NDS32_SDA19S0_RELA;
11770 break;
11771 case R_NDS32_LO12S1_RELA:
11772 reloc = R_NDS32_SDA18S1_RELA;
11773 break;
11774 case R_NDS32_LO12S2_RELA:
11775 reloc = R_NDS32_SDA17S2_RELA;
11776 break;
11777 case R_NDS32_LO12S2_DP_RELA:
11778 range_l = sdata_range[0][0];
11779 range_h = sdata_range[0][1];
11780 reloc = R_NDS32_SDA12S2_DP_RELA;
11781 break;
11782 case R_NDS32_LO12S2_SP_RELA:
11783 range_l = sdata_range[0][0];
11784 range_h = sdata_range[0][1];
11785 reloc = R_NDS32_SDA12S2_SP_RELA;
11786 break;
11787 default:
11788 return;
11789 }
11790
11791 /* There are range_h and range_l because linker has to promise
11792 all sections move cross one page together. */
11793 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
fbaf61ad
NC
11794 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11795 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
1c8f6a4d
KLC
11796 {
11797 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
35c08157 11798 {
1c8f6a4d
KLC
11799 /* Maybe we should add R_NDS32_INSN16 reloc type here
11800 or manually do some optimization. sethi can't be
11801 eliminated when updating $gp so the relative ori
11802 needs to be preserved. */
11803 return;
35c08157 11804 }
1c8f6a4d
KLC
11805 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11806 &insn))
11807 return;
11808 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11809 bfd_putb32 (insn, contents + laddr);
11810
11811 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11812 R_NDS32_INSN16);
11813 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11814 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11815 irelfn->r_info =
11816 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11817
35c08157 11818 }
1c8f6a4d
KLC
11819 }
11820 return;
11821}
35c08157 11822
1c8f6a4d 11823/* Relax PTR relocation for nds32_elf_relax_section. */
35c08157 11824
1c8f6a4d
KLC
11825static bfd_boolean
11826nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11827 Elf_Internal_Rela *internal_relocs, int *insn_len,
11828 int *seq_len, bfd_byte *contents)
11829{
11830 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
35c08157 11831
1c8f6a4d 11832 irelend = internal_relocs + sec->reloc_count;
35c08157 11833
1c8f6a4d
KLC
11834 re_irel =
11835 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11836 R_NDS32_PTR_RESOLVED, irel->r_addend);
35c08157 11837
1c8f6a4d
KLC
11838 if (re_irel == irelend)
11839 {
695344c0 11840 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
2dcf00ce 11841 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11842 return FALSE;
11843 }
35c08157 11844
1c8f6a4d
KLC
11845 if (re_irel->r_addend != 1)
11846 return FALSE;
35c08157 11847
1c8f6a4d
KLC
11848 /* Pointed target is relaxed and no longer needs this void *,
11849 change the type to NONE. */
11850 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11851
1c8f6a4d
KLC
11852 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11853 not exist, it means only count 1 and remove it directly. */
11854 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11855 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11856 R_NDS32_PTR_COUNT);
11857 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11858 R_NDS32_PTR);
11859 if (count_irel != irelend)
11860 {
11861 if (--count_irel->r_addend > 0)
11862 return FALSE;
11863 }
11864
11865 if (ptr_irel != irelend)
11866 return FALSE;
11867
11868 /* If the PTR_COUNT is already 0, remove current instruction. */
11869 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11870 *insn_len = 0;
11871 return TRUE;
11872}
11873
fbaf61ad 11874/* Relax LWC relocation for nds32_elf_relax_section. */
1c8f6a4d
KLC
11875
11876static void
fbaf61ad
NC
11877nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11878 asection *sec, Elf_Internal_Rela *irel,
11879 Elf_Internal_Rela *internal_relocs,
11880 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11881 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
1c8f6a4d 11882{
fbaf61ad
NC
11883 /* Pattern:
11884 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11885 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11886 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11887 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11888 ... */
11889
1c8f6a4d 11890 uint32_t insn;
1c8f6a4d 11891 bfd_vma local_sda, laddr;
fbaf61ad
NC
11892 unsigned long reloc;
11893 bfd_vma access_addr, flsi_offset;
11894 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11895 Elf_Internal_Rela *irelend, *re_irel;
11896 unsigned int opcode;
1c8f6a4d
KLC
11897
11898 irelend = internal_relocs + sec->reloc_count;
11899 laddr = irel->r_offset;
11900 insn = bfd_getb32 (contents + laddr);
11901
fbaf61ad 11902 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
1c8f6a4d
KLC
11903 return;
11904
fbaf61ad
NC
11905 /* Can not do relaxation for bi format. */
11906 if ((insn & 0x1000))
1c8f6a4d
KLC
11907 return;
11908
fbaf61ad
NC
11909 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11910 opcode = N32_OP6 (insn);
11911 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11912 reloc = R_NDS32_SDA12S2_SP_RELA;
11913 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11914 reloc = R_NDS32_SDA12S2_DP_RELA;
1c8f6a4d
KLC
11915 else
11916 return;
11917
fbaf61ad
NC
11918 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11919 R_NDS32_PTR_RESOLVED);
11920 if (re_irel == irelend)
1c8f6a4d 11921 {
fbaf61ad
NC
11922 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11923 (uint64_t) irel->r_offset);
11924 return;
1c8f6a4d 11925 }
1c8f6a4d 11926
fbaf61ad 11927 /* For SDA base relative relaxation. */
1c8f6a4d
KLC
11928 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11929 &local_sda, FALSE);
fbaf61ad
NC
11930 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11931 flsi_offset = (insn & 0xfff) << 2;
11932 access_addr += flsi_offset;
11933 range_l = sdata_range[0][0];
11934 range_h = sdata_range[0][1];
1c8f6a4d 11935
fbaf61ad
NC
11936 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11937 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
1c8f6a4d 11938 {
fbaf61ad
NC
11939 /* Turn flsi instruction into sda access format. */
11940 insn = (insn & 0x7ff07000) | (REG_GP << 15);
1c8f6a4d 11941
fbaf61ad
NC
11942 /* Add relocation type to flsi. */
11943 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11944 irel->r_addend += flsi_offset;
11945 bfd_putb32 (insn, contents + re_irel->r_offset);
1c8f6a4d 11946
fbaf61ad 11947 re_irel->r_addend |= 1;
1c8f6a4d
KLC
11948 *again = TRUE;
11949 }
1c8f6a4d
KLC
11950}
11951
11952static bfd_boolean
11953nds32_relax_adjust_label (bfd *abfd, asection *sec,
11954 Elf_Internal_Rela *internal_relocs,
11955 bfd_byte *contents,
11956 nds32_elf_blank_t **relax_blank_list,
11957 int optimize, int opt_size)
11958{
11959 /* This code block is used to adjust 4-byte alignment by relax a pair
11960 of instruction a time.
11961
11962 It recognizes three types of relocations.
de194d85 11963 1. R_NDS32_LABEL - a alignment.
1c8f6a4d
KLC
11964 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11965 3. is_16bit_NOP () - remove a 16-bit instruction. */
11966
de194d85
YC
11967 /* TODO: It seems currently implementation only support 4-byte alignment.
11968 We should handle any-alignment. */
1c8f6a4d
KLC
11969
11970 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11971 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11972 Elf_Internal_Rela rel_temp;
11973 Elf_Internal_Rela *irelend;
11974 bfd_vma address;
11975 uint16_t insn16;
11976
11977 /* Checking for branch relaxation relies on the relocations to
11978 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11979 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11980 sizeof (Elf_Internal_Rela), compar_reloc);
11981
11982 irelend = internal_relocs + sec->reloc_count;
11983
11984 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11985 /* FIXME: Can we generate the right order in assembler?
11986 So we don't have to swapping them here. */
11987
11988 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11989 label_rel < irelend; label_rel++)
11990 {
11991 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11992 continue;
11993
11994 /* Find the first reloc has the same offset with label_rel. */
11995 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11996 insn_rel++;
11997
11998 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11999 insn_rel++)
12000 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
12001 address. */
12002 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
12003 break;
12004
12005 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
12006 && insn_rel < label_rel)
12007 {
12008 /* Swap the two reloc if the R_NDS32_INSN16 is
12009 before R_NDS32_LABEL. */
12010 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
12011 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
12012 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
12013 }
12014 }
12015
12016 label_rel = NULL;
12017 insn_rel = NULL;
12018 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12019 or higher, remove other R_NDS32_LABEL with lower alignment.
12020 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12021 then the R_NDS32_LABEL sequence is broke. */
12022 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12023 {
12024 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12025 {
12026 if (label_rel == NULL)
12027 {
12028 if (tmp_rel->r_addend < 2)
12029 label_rel = tmp_rel;
12030 continue;
12031 }
12032 else if (tmp_rel->r_addend > 1)
12033 {
12034 /* Remove all LABEL relocation from label_rel to tmp_rel
12035 including relocations with same offset as tmp_rel. */
fbaf61ad 12036 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
1c8f6a4d 12037 {
fbaf61ad
NC
12038 if (tmp2_rel->r_offset == tmp_rel->r_offset)
12039 break;
12040
1c8f6a4d
KLC
12041 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12042 && tmp2_rel->r_addend < 2)
12043 tmp2_rel->r_info =
12044 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12045 R_NDS32_NONE);
12046 }
12047 label_rel = NULL;
12048 }
12049 }
12050 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12051 {
12052 /* A new INSN16 which can be converted, so clear label_rel. */
12053 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12054 irelend, &insn16)
12055 || is_16bit_NOP (abfd, sec, tmp_rel))
12056 label_rel = NULL;
12057 }
12058 }
12059
12060 label_rel = NULL;
12061 insn_rel = NULL;
12062 /* Optimized for speed and nothing has not been relaxed.
12063 It's time to align labels.
12064 We may convert a 16-bit instruction right before a label to
12065 32-bit, in order to align the label if necessary
12066 all reloc entries has been sorted by r_offset. */
fbaf61ad
NC
12067 for (irel = internal_relocs;
12068 irel < irelend && irel->r_offset < sec->size; irel++)
1c8f6a4d
KLC
12069 {
12070 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12071 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12072 continue;
12073
12074 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12075 {
12076 /* A new INSN16 found, resize the old one. */
12077 if (is_convert_32_to_16
12078 (abfd, sec, irel, internal_relocs, irelend, &insn16)
12079 || is_16bit_NOP (abfd, sec, irel))
12080 {
12081 if (insn_rel)
12082 {
12083 /* Previous INSN16 reloc exists, reduce its
12084 size to 16-bit. */
12085 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12086 irelend, &insn16))
35c08157 12087 {
1c8f6a4d
KLC
12088 nds32_elf_write_16 (abfd, contents, insn_rel,
12089 internal_relocs, irelend, insn16);
12090
12091 if (!insert_nds32_elf_blank_recalc_total
12092 (relax_blank_list, insn_rel->r_offset + 2, 2))
12093 return FALSE;
12094 }
12095 else if (is_16bit_NOP (abfd, sec, insn_rel))
12096 {
12097 if (!insert_nds32_elf_blank_recalc_total
12098 (relax_blank_list, insn_rel->r_offset, 2))
12099 return FALSE;
12100 }
12101 insn_rel->r_info =
12102 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12103 }
12104 /* Save the new one for later use. */
12105 insn_rel = irel;
12106 }
12107 else
12108 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12109 R_NDS32_NONE);
12110 }
12111 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12112 {
12113 /* Search for label. */
12114 int force_relax = 0;
12115
12116 /* Label on 16-bit instruction or optimization
12117 needless, just reset this reloc. */
12118 insn16 = bfd_getb16 (contents + irel->r_offset);
12119 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12120 {
12121 irel->r_info =
12122 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12123 continue;
12124 }
12125
12126 address =
12127 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12128 irel->r_offset, 1);
12129
12130 if (!insn_rel)
12131 {
12132 /* Check if there is case which can not be aligned. */
12133 if (irel->r_addend == 2 && address & 0x2)
12134 return FALSE;
12135 continue;
12136 }
12137
12138 /* Try to align this label. */
12139
12140 if ((irel->r_addend & 0x1f) < 2)
12141 {
12142 /* Check if there is a INSN16 at the same address.
12143 Label_rel always seats before insn_rel after
12144 our sort. */
12145
12146 /* Search for INSN16 at LABEL location. If INSN16 is at
12147 same location and this LABEL alignment is lower than 2,
12148 the INSN16 can be converted to 2-byte. */
12149 for (tmp_rel = irel;
12150 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12151 tmp_rel++)
12152 {
12153 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12154 && (is_convert_32_to_16
12155 (abfd, sec, tmp_rel, internal_relocs,
12156 irelend, &insn16)
12157 || is_16bit_NOP (abfd, sec, tmp_rel)))
12158 {
12159 force_relax = 1;
12160 break;
12161 }
12162 }
12163 }
12164
12165 if (force_relax || irel->r_addend == 1 || address & 0x2)
12166 {
12167 /* Label not aligned. */
12168 /* Previous reloc exists, reduce its size to 16-bit. */
12169 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12170 internal_relocs, irelend, &insn16))
12171 {
12172 nds32_elf_write_16 (abfd, contents, insn_rel,
12173 internal_relocs, irelend, insn16);
12174
12175 if (!insert_nds32_elf_blank_recalc_total
12176 (relax_blank_list, insn_rel->r_offset + 2, 2))
12177 return FALSE;
12178 }
12179 else if (is_16bit_NOP (abfd, sec, insn_rel))
12180 {
12181 if (!insert_nds32_elf_blank_recalc_total
12182 (relax_blank_list, insn_rel->r_offset, 2))
12183 return FALSE;
12184 }
12185
12186 }
12187 /* INSN16 reloc is used. */
12188 insn_rel = NULL;
12189 }
12190 }
12191
12192 address =
12193 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12194 if (insn_rel && (address & 0x2 || opt_size))
12195 {
12196 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12197 irelend, &insn16))
12198 {
12199 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12200 irelend, insn16);
12201 if (!insert_nds32_elf_blank_recalc_total
12202 (relax_blank_list, insn_rel->r_offset + 2, 2))
12203 return FALSE;
12204 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12205 R_NDS32_NONE);
12206 }
12207 else if (is_16bit_NOP (abfd, sec, insn_rel))
12208 {
12209 if (!insert_nds32_elf_blank_recalc_total
12210 (relax_blank_list, insn_rel->r_offset, 2))
12211 return FALSE;
12212 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12213 R_NDS32_NONE);
12214 }
12215 }
12216 insn_rel = NULL;
12217 return TRUE;
12218}
12219
1c8f6a4d
KLC
12220static bfd_boolean
12221nds32_elf_relax_section (bfd *abfd, asection *sec,
12222 struct bfd_link_info *link_info, bfd_boolean *again)
12223{
12224 nds32_elf_blank_t *relax_blank_list = NULL;
12225 Elf_Internal_Shdr *symtab_hdr;
12226 Elf_Internal_Rela *internal_relocs;
12227 Elf_Internal_Rela *irel;
12228 Elf_Internal_Rela *irelend;
12229 Elf_Internal_Sym *isymbuf = NULL;
12230 bfd_byte *contents = NULL;
12231 bfd_boolean result = TRUE;
12232 int optimize = 0;
12233 int opt_size = 0;
12234 uint32_t insn;
12235 uint16_t insn16;
12236
12237 /* Target dependnet option. */
12238 struct elf_nds32_link_hash_table *table;
12239 int load_store_relax;
1c8f6a4d
KLC
12240
12241 relax_blank_list = NULL;
12242
12243 *again = FALSE;
12244
12245 /* Nothing to do for
12246 * relocatable link or
12247 * non-relocatable section or
12248 * non-code section or
12249 * empty content or
12250 * no reloc entry. */
0e1862bb 12251 if (bfd_link_relocatable (link_info)
1c8f6a4d 12252 || (sec->flags & SEC_RELOC) == 0
cd28e7aa 12253 || (sec->flags & SEC_EXCLUDE) != 0
1c8f6a4d 12254 || (sec->flags & SEC_CODE) == 0
fbaf61ad
NC
12255 || sec->size == 0
12256 || sec->reloc_count == 0)
1c8f6a4d
KLC
12257 return TRUE;
12258
12259 /* 09.12.11 Workaround. */
12260 /* We have to adjust align for R_NDS32_LABEL if needed.
12261 The adjust approach only can fix 2-byte align once. */
12262 if (sec->alignment_power > 2)
12263 return TRUE;
12264
fbaf61ad
NC
12265 /* Do TLS model conversion once at first. */
12266 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12267
1c8f6a4d
KLC
12268 /* The optimization type to do. */
12269
12270 table = nds32_elf_hash_table (link_info);
1c8f6a4d 12271
fbaf61ad
NC
12272 /* Save the first section for abs symbol relaxation.
12273 This is used for checking gp relaxation in the
12274 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12275 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12276 table, NULL, NULL);
12277
1c8f6a4d
KLC
12278 /* The begining of general relaxation. */
12279
12280 if (is_SDA_BASE_set == 0)
12281 {
12282 bfd_vma gp;
12283 is_SDA_BASE_set = 1;
12284 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12285 &gp, FALSE);
12286 relax_range_measurement (abfd);
12287 }
12288
1c8f6a4d
KLC
12289 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12290 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12291 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12292 TRUE /* keep_memory */);
12293 if (internal_relocs == NULL)
12294 goto error_return;
12295
12296 irelend = internal_relocs + sec->reloc_count;
12297 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12298 irelend, R_NDS32_RELAX_ENTRY);
12299
12300 if (irel == irelend)
12301 return TRUE;
12302
12303 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12304 {
12305 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
f9671640 12306 return TRUE;
1c8f6a4d
KLC
12307
12308 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12309 optimize = 1;
12310
12311 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12312 opt_size = 1;
12313 }
12314
12315 load_store_relax = table->load_store_relax;
12316
12317 /* Get symbol table and section content. */
0c4bd9d9 12318 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
1c8f6a4d
KLC
12319 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12320 goto error_return;
12321
12322 /* Do relax loop only when finalize is not done.
12323 Take care of relaxable relocs except INSN16. */
12324 for (irel = internal_relocs; irel < irelend; irel++)
12325 {
12326 int seq_len; /* Original length of instruction sequence. */
12327 int insn_len = 0; /* Final length of instruction sequence. */
12328 bfd_boolean removed;
12329
12330 insn = 0;
12331 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12332 && (irel->r_addend & 0x1f) >= 2)
12333 optimize = 1;
12334
12335 /* Relocation Types
12336 R_NDS32_LONGCALL1 53
12337 R_NDS32_LONGCALL2 54
12338 R_NDS32_LONGCALL3 55
12339 R_NDS32_LONGJUMP1 56
12340 R_NDS32_LONGJUMP2 57
12341 R_NDS32_LONGJUMP3 58
12342 R_NDS32_LOADSTORE 59 */
12343 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12344 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12345 seq_len = GET_SEQ_LEN (irel->r_addend);
12346
12347 /* Relocation Types
12348 R_NDS32_LONGCALL4 107
12349 R_NDS32_LONGCALL5 108
12350 R_NDS32_LONGCALL6 109
12351 R_NDS32_LONGJUMP4 110
12352 R_NDS32_LONGJUMP5 111
12353 R_NDS32_LONGJUMP6 112
12354 R_NDS32_LONGJUMP7 113 */
12355 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12356 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12357 seq_len = 4;
12358
12359 /* Relocation Types
12360 R_NDS32_LO12S0_RELA 30
12361 R_NDS32_LO12S1_RELA 29
12362 R_NDS32_LO12S2_RELA 28
12363 R_NDS32_LO12S2_SP_RELA 71
12364 R_NDS32_LO12S2_DP_RELA 70
12365 R_NDS32_GOT_LO12 46
12366 R_NDS32_GOTOFF_LO12 50
12367 R_NDS32_PLTREL_LO12 65
12368 R_NDS32_PLT_GOTREL_LO12 67
12369 R_NDS32_17IFC_PCREL_RELA 96
12370 R_NDS32_GOT_SUFF 193
12371 R_NDS32_GOTOFF_SUFF 194
12372 R_NDS32_PLT_GOT_SUFF 195
12373 R_NDS32_MULCALL_SUFF 196
12374 R_NDS32_PTR 197 */
12375 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12376 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12377 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12378 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12379 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12380 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12381 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12382 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12383 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12384 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12385 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12386 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12387 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12388 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
fbaf61ad
NC
12389 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12390 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
1c8f6a4d
KLC
12391 seq_len = 0;
12392 else
12393 continue;
12394
12395 insn_len = seq_len;
12396 removed = FALSE;
12397
12398 switch (ELF32_R_TYPE (irel->r_info))
12399 {
12400 case R_NDS32_LONGCALL1:
12401 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12402 &insn_len, contents, isymbuf,
12403 symtab_hdr);
12404 break;
12405 case R_NDS32_LONGCALL2:
12406 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12407 &insn_len, contents, isymbuf,
12408 symtab_hdr);
12409 break;
12410 case R_NDS32_LONGCALL3:
12411 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12412 &insn_len, contents, isymbuf,
12413 symtab_hdr);
12414 break;
12415 case R_NDS32_LONGJUMP1:
12416 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12417 &insn_len, contents, isymbuf,
12418 symtab_hdr);
12419 break;
12420 case R_NDS32_LONGJUMP2:
12421 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12422 &insn_len, contents, isymbuf,
12423 symtab_hdr);
12424 break;
12425 case R_NDS32_LONGJUMP3:
12426 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12427 &insn_len, contents, isymbuf,
12428 symtab_hdr);
12429 break;
12430 case R_NDS32_LONGCALL4:
12431 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12432 &insn_len, contents, isymbuf,
12433 symtab_hdr);
12434 break;
12435 case R_NDS32_LONGCALL5:
12436 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12437 &insn_len, contents, isymbuf,
12438 symtab_hdr);
12439 break;
12440 case R_NDS32_LONGCALL6:
12441 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12442 &insn_len, contents, isymbuf,
12443 symtab_hdr);
12444 break;
12445 case R_NDS32_LONGJUMP4:
12446 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12447 &insn_len, contents, isymbuf,
12448 symtab_hdr);
12449 break;
12450 case R_NDS32_LONGJUMP5:
12451 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12452 &insn_len, &seq_len, contents,
12453 isymbuf, symtab_hdr);
12454 break;
12455 case R_NDS32_LONGJUMP6:
12456 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12457 &insn_len, &seq_len, contents,
12458 isymbuf, symtab_hdr);
12459 break;
12460 case R_NDS32_LONGJUMP7:
12461 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12462 &insn_len, &seq_len, contents,
12463 isymbuf, symtab_hdr);
12464 break;
12465 case R_NDS32_LOADSTORE:
12466 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12467 internal_relocs, &insn_len,
12468 contents, isymbuf, symtab_hdr,
fbaf61ad 12469 load_store_relax, table);
1c8f6a4d
KLC
12470 break;
12471 case R_NDS32_LO12S0_RELA:
12472 case R_NDS32_LO12S1_RELA:
fbaf61ad 12473 case R_NDS32_LO12S2_RELA:
1c8f6a4d
KLC
12474 case R_NDS32_LO12S2_DP_RELA:
12475 case R_NDS32_LO12S2_SP_RELA:
1c8f6a4d
KLC
12476 /* Relax for low part. */
12477 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
fbaf61ad 12478 contents, isymbuf, symtab_hdr, table);
1c8f6a4d
KLC
12479
12480 /* It is impossible to delete blank, so just continue. */
12481 continue;
fbaf61ad
NC
12482 case R_NDS32_PTR:
12483 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12484 &insn_len, &seq_len, contents);
12485 break;
12486 case R_NDS32_LSI:
12487 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12488 contents, isymbuf, symtab_hdr, again);
12489 continue;
1c8f6a4d
KLC
12490 case R_NDS32_GOT_LO12:
12491 case R_NDS32_GOTOFF_LO12:
12492 case R_NDS32_PLTREL_LO12:
12493 case R_NDS32_PLT_GOTREL_LO12:
12494 case R_NDS32_GOTPC_LO12:
1c8f6a4d 12495 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 12496 case R_NDS32_TLS_LE_ADD:
1c8f6a4d 12497 case R_NDS32_TLS_LE_LS:
1c8f6a4d 12498 case R_NDS32_PLT_GOT_SUFF:
1c8f6a4d 12499 case R_NDS32_GOT_SUFF:
1c8f6a4d 12500 case R_NDS32_GOTOFF_SUFF:
1c8f6a4d
KLC
12501 continue;
12502 default:
12503 continue;
1c8f6a4d 12504 }
fbaf61ad 12505
1c8f6a4d
KLC
12506 if (removed && seq_len - insn_len > 0)
12507 {
12508 if (!insert_nds32_elf_blank
12509 (&relax_blank_list, irel->r_offset + insn_len,
12510 seq_len - insn_len))
12511 goto error_return;
12512 *again = TRUE;
35c08157 12513 }
1c8f6a4d
KLC
12514 }
12515
12516 calc_nds32_blank_total (relax_blank_list);
12517
12518 if (table->relax_fp_as_gp)
12519 {
12520 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12521 irelend, isymbuf))
12522 goto error_return;
35c08157 12523
535b785f 12524 if (!*again)
35c08157 12525 {
1c8f6a4d
KLC
12526 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12527 irelend))
12528 goto error_return;
35c08157
KLC
12529 }
12530 }
1c8f6a4d 12531
535b785f 12532 if (!*again)
1c8f6a4d
KLC
12533 {
12534 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12535 &relax_blank_list, optimize, opt_size))
12536 goto error_return;
12537 }
12538
12539 /* It doesn't matter optimize_for_space_no_align anymore.
35c08157
KLC
12540 If object file is assembled with flag '-Os',
12541 the we don't adjust jump-destination on 4-byte boundary. */
12542
12543 if (relax_blank_list)
12544 {
12545 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12546 relax_blank_list = NULL;
12547 }
12548
535b785f 12549 if (!*again)
35c08157
KLC
12550 {
12551 /* Closing the section, so we don't relax it anymore. */
12552 bfd_vma sec_size_align;
12553 Elf_Internal_Rela *tmp_rel;
12554
12555 /* Pad to alignment boundary. Only handle current section alignment. */
609332f1
NC
12556 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12557 & ((-1U) << sec->alignment_power);
35c08157
KLC
12558 if ((sec_size_align - sec->size) & 0x2)
12559 {
12560 insn16 = NDS32_NOP16;
12561 bfd_putb16 (insn16, contents + sec->size);
12562 sec->size += 2;
12563 }
12564
12565 while (sec_size_align != sec->size)
12566 {
12567 insn = NDS32_NOP32;
12568 bfd_putb32 (insn, contents + sec->size);
12569 sec->size += 4;
12570 }
12571
1c8f6a4d
KLC
12572 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12573 irelend, R_NDS32_RELAX_ENTRY);
35c08157
KLC
12574 if (tmp_rel != irelend)
12575 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12576
12577 clean_nds32_elf_blank ();
12578 }
12579
12580finish:
12581 if (internal_relocs != NULL
12582 && elf_section_data (sec)->relocs != internal_relocs)
12583 free (internal_relocs);
12584
12585 if (contents != NULL
12586 && elf_section_data (sec)->this_hdr.contents != contents)
12587 free (contents);
12588
12589 if (isymbuf != NULL && symtab_hdr->contents != (bfd_byte *) isymbuf)
12590 free (isymbuf);
12591
12592 return result;
12593
12594error_return:
12595 result = FALSE;
12596 goto finish;
12597}
12598
12599static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12600{
12601 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12602 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12603 {NULL, 0, 0, 0, 0}
12604};
12605
12606static bfd_boolean
12607nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12608 struct bfd_link_info *info,
12609 void *finfo ATTRIBUTE_UNUSED,
12610 bfd_boolean (*func) (void *, const char *,
12611 Elf_Internal_Sym *,
12612 asection *,
12613 struct elf_link_hash_entry *)
12614 ATTRIBUTE_UNUSED)
12615{
12616 FILE *sym_ld_script = NULL;
12617 struct elf_nds32_link_hash_table *table;
12618
12619 table = nds32_elf_hash_table (info);
12620 sym_ld_script = table->sym_ld_script;
12621
12622 if (check_start_export_sym)
12623 fprintf (sym_ld_script, "}\n");
12624
12625 return TRUE;
12626}
12627
12628static enum elf_reloc_type_class
12629nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12630 const asection *rel_sec ATTRIBUTE_UNUSED,
12631 const Elf_Internal_Rela *rela)
12632{
12633 switch ((int) ELF32_R_TYPE (rela->r_info))
12634 {
12635 case R_NDS32_RELATIVE:
12636 return reloc_class_relative;
12637 case R_NDS32_JMP_SLOT:
12638 return reloc_class_plt;
12639 case R_NDS32_COPY:
12640 return reloc_class_copy;
12641 default:
12642 return reloc_class_normal;
12643 }
12644}
12645
12646/* Put target dependent option into info hash table. */
12647void
12648bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12649 int relax_fp_as_gp,
12650 int eliminate_gc_relocs,
fbaf61ad
NC
12651 FILE * sym_ld_script,
12652 int hyper_relax,
12653 int tls_desc_trampoline,
12654 int load_store_relax)
35c08157
KLC
12655{
12656 struct elf_nds32_link_hash_table *table;
12657
12658 table = nds32_elf_hash_table (link_info);
12659 if (table == NULL)
12660 return;
12661
12662 table->relax_fp_as_gp = relax_fp_as_gp;
12663 table->eliminate_gc_relocs = eliminate_gc_relocs;
12664 table->sym_ld_script = sym_ld_script;
fbaf61ad
NC
12665 table->hyper_relax = hyper_relax;
12666 table->tls_desc_trampoline = tls_desc_trampoline;
35c08157 12667 table ->load_store_relax = load_store_relax;
35c08157
KLC
12668}
12669\f
fbaf61ad 12670
35c08157
KLC
12671/* These functions and data-structures are used for fp-as-gp
12672 optimization. */
12673
12674#define FAG_THRESHOLD 3 /* At least 3 gp-access. */
1c8f6a4d
KLC
12675/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12676 the read-only section and read-write section. */
12677#define FAG_WINDOW (508 - 32)
35c08157
KLC
12678
12679/* An nds32_fag represent a gp-relative access.
12680 We find best fp-base by using a sliding window
12681 to find a base address which can cover most gp-access. */
12682struct nds32_fag
12683{
12684 struct nds32_fag *next; /* NULL-teminated linked list. */
12685 bfd_vma addr; /* The address of this fag. */
12686 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12687 It is used for applying FP7U2_FLAG. */
12688 int count; /* How many times this address is referred.
12689 There should be exactly `count' relocations
12690 in relas. */
12691 int relas_capcity; /* The buffer size of relas.
12692 We use an array instead of linked-list,
12693 and realloc is used to adjust buffer size. */
12694};
12695
12696static void
12697nds32_fag_init (struct nds32_fag *head)
12698{
12699 memset (head, 0, sizeof (struct nds32_fag));
12700}
12701
12702static void
12703nds32_fag_verify (struct nds32_fag *head)
12704{
12705 struct nds32_fag *iter;
12706 struct nds32_fag *prev;
12707
12708 prev = NULL;
12709 iter = head->next;
12710 while (iter)
12711 {
12712 if (prev && prev->addr >= iter->addr)
12713 puts ("Bug in fp-as-gp insertion.");
12714 prev = iter;
12715 iter = iter->next;
12716 }
12717}
12718
12719/* Insert a fag in ascending order.
12720 If a fag of the same address already exists,
12721 they are chained by relas array. */
12722
12723static void
12724nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12725 Elf_Internal_Rela * rel)
12726{
12727 struct nds32_fag *iter;
12728 struct nds32_fag *new_fag;
12729 const int INIT_RELAS_CAP = 4;
12730
12731 for (iter = head;
12732 iter->next && iter->next->addr <= addr;
12733 iter = iter->next)
12734 /* Find somewhere to insert. */ ;
12735
12736 /* `iter' will be equal to `head' if the list is empty. */
12737 if (iter != head && iter->addr == addr)
12738 {
12739 /* The address exists in the list.
12740 Insert `rel' into relocation list, relas. */
12741
12742 /* Check whether relas is big enough. */
12743 if (iter->count >= iter->relas_capcity)
12744 {
12745 iter->relas_capcity *= 2;
12746 iter->relas = bfd_realloc
12747 (iter->relas, iter->relas_capcity * sizeof (void *));
12748 }
12749 iter->relas[iter->count++] = rel;
12750 return;
12751 }
12752
12753 /* This is a new address. Create a fag node for it. */
12754 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12755 memset (new_fag, 0, sizeof (*new_fag));
12756 new_fag->addr = addr;
12757 new_fag->count = 1;
12758 new_fag->next = iter->next;
12759 new_fag->relas_capcity = INIT_RELAS_CAP;
12760 new_fag->relas = (Elf_Internal_Rela **)
12761 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12762 new_fag->relas[0] = rel;
12763 iter->next = new_fag;
12764
12765 nds32_fag_verify (head);
12766}
12767
12768static void
12769nds32_fag_free_list (struct nds32_fag *head)
12770{
12771 struct nds32_fag *iter;
12772
12773 iter = head->next;
12774 while (iter)
12775 {
12776 struct nds32_fag *tmp = iter;
12777 iter = iter->next;
12778 free (tmp->relas);
12779 tmp->relas = NULL;
12780 free (tmp);
12781 }
12782}
12783
35c08157
KLC
12784/* Find the best fp-base address.
12785 The relocation associated with that address is returned,
12786 so we can track the symbol instead of a fixed address.
12787
12788 When relaxation, the address of an datum may change,
12789 because a text section is shrinked, so the data section
1c8f6a4d 12790 moves forward. If the aligments of text and data section
35c08157
KLC
12791 are different, their distance may change too.
12792 Therefore, tracking a fixed address is not appriate. */
12793
12794static int
12795nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12796{
12797 struct nds32_fag *base; /* First fag in the window. */
12798 struct nds32_fag *last; /* First fag outside the window. */
12799 int accu = 0; /* Usage accumulation. */
12800 struct nds32_fag *best; /* Best fag. */
12801 int baccu = 0; /* Best accumulation. */
12802
12803 /* Use first fag for initial, and find the last fag in the window.
12804
12805 In each iteration, we could simply subtract previous fag
12806 and accumulate following fags which are inside the window,
12807 untill we each the end. */
12808
1c8f6a4d
KLC
12809 if (head->next == NULL)
12810 {
12811 *bestpp = NULL;
12812 return 0;
12813 }
35c08157
KLC
12814
12815 /* Initialize base. */
12816 base = head->next;
12817 best = base;
12818 for (last = base;
12819 last && last->addr < base->addr + FAG_WINDOW;
12820 last = last->next)
12821 accu += last->count;
12822
12823 baccu = accu;
12824
12825 /* Record the best base in each iteration. */
12826 while (base->next)
1c8f6a4d
KLC
12827 {
12828 accu -= base->count;
12829 base = base->next;
12830 /* Account fags in window. */
12831 for (/* Nothing. */;
12832 last && last->addr < base->addr + FAG_WINDOW;
12833 last = last->next)
12834 accu += last->count;
12835
12836 /* A better fp-base? */
12837 if (accu > baccu)
12838 {
12839 best = base;
12840 baccu = accu;
12841 }
12842 }
35c08157
KLC
12843
12844 if (bestpp)
12845 *bestpp = best;
12846 return baccu;
12847}
12848
12849/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12850 so we can convert it fo fp-relative access later.
12851 `best_fag' is the best fp-base. Only those inside the window
12852 of best_fag is applied the flag. */
12853
12854static bfd_boolean
12855nds32_fag_mark_relax (struct bfd_link_info *link_info,
fbaf61ad 12856 asection *sec, struct nds32_fag *best_fag,
35c08157
KLC
12857 Elf_Internal_Rela *internal_relocs,
12858 Elf_Internal_Rela *irelend)
12859{
12860 struct nds32_fag *ifag;
12861 bfd_vma best_fpbase, gp;
12862 bfd *output_bfd;
12863
fbaf61ad 12864 output_bfd = sec->output_section->owner;
35c08157
KLC
12865 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12866 best_fpbase = best_fag->addr;
12867
1c8f6a4d
KLC
12868 if (best_fpbase > gp + sdata_range[1][1]
12869 || best_fpbase < gp - sdata_range[1][0])
35c08157
KLC
12870 return FALSE;
12871
12872 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12873 so we know they can be converted to lwi37.fp. */
12874 for (ifag = best_fag;
12875 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12876 {
12877 int i;
12878
12879 for (i = 0; i < ifag->count; i++)
12880 {
12881 Elf_Internal_Rela *insn16_rel;
12882 Elf_Internal_Rela *fag_rel;
12883
12884 fag_rel = ifag->relas[i];
12885
12886 /* Only if this is within the WINDOWS, FP7U2_FLAG
12887 is applied. */
12888
12889 insn16_rel = find_relocs_at_address
12890 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12891
12892 if (insn16_rel != irelend)
12893 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12894 }
12895 }
12896 return TRUE;
12897}
12898
1c8f6a4d
KLC
12899/* Reset INSN16 to clean fp as gp. */
12900
12901static void
12902nds32_fag_unmark_relax (struct nds32_fag *fag,
12903 Elf_Internal_Rela *internal_relocs,
12904 Elf_Internal_Rela *irelend)
12905{
12906 struct nds32_fag *ifag;
12907 int i;
12908 Elf_Internal_Rela *insn16_rel;
12909 Elf_Internal_Rela *fag_rel;
12910
12911 for (ifag = fag; ifag; ifag = ifag->next)
12912 {
12913 for (i = 0; i < ifag->count; i++)
12914 {
12915 fag_rel = ifag->relas[i];
12916
12917 /* Restore the INSN16 relocation. */
12918 insn16_rel = find_relocs_at_address
12919 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12920
12921 if (insn16_rel != irelend)
12922 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12923 }
12924 }
12925}
12926
35c08157
KLC
12927/* This is the main function of fp-as-gp optimization.
12928 It should be called by relax_section. */
12929
12930static bfd_boolean
12931nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12932 bfd *abfd, asection *sec,
12933 Elf_Internal_Rela *internal_relocs,
12934 Elf_Internal_Rela *irelend,
12935 Elf_Internal_Sym *isymbuf)
12936{
12937 Elf_Internal_Rela *begin_rel = NULL;
12938 Elf_Internal_Rela *irel;
12939 struct nds32_fag fag_head;
12940 Elf_Internal_Shdr *symtab_hdr;
12941 bfd_byte *contents;
1c8f6a4d 12942 bfd_boolean ifc_inside = FALSE;
35c08157
KLC
12943
12944 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12945
12946 /* Per-function fp-base selection.
12947 1. Create a list for all the gp-relative access.
12948 2. Base on those gp-relative address,
12949 find a fp-base which can cover most access.
12950 3. Use the fp-base for fp-as-gp relaxation.
12951
12952 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12953 we should
12954 1. delete the `la $fp, _FP_BASE_' instruction and
12955 2. not convert lwi.gp to lwi37.fp.
12956
12957 To delete the _FP_BASE_ instruction, we simply apply
12958 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12959
12960 To suppress the conversion, we simply NOT to apply
12961 R_NDS32_INSN16_FP7U2_FLAG flag. */
12962
12963 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12964
0c4bd9d9 12965 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
35c08157
KLC
12966 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12967 return FALSE;
12968
12969 /* Check whether it is worth for fp-as-gp optimization,
12970 i.e., at least 3 gp-load.
12971
12972 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12973 apply this optimization. */
12974
12975 for (irel = internal_relocs; irel < irelend; irel++)
12976 {
12977 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12978 One we enter the begin of the region, we track all the LW/ST
12979 instructions, so when we leave the region, we try to find
12980 the best fp-base address for those LW/ST instructions. */
12981
12982 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12983 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12984 {
12985 /* Begin of the region. */
12986 if (begin_rel)
695344c0 12987 /* xgettext:c-format */
38f14ab8 12988 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
35c08157
KLC
12989
12990 begin_rel = irel;
12991 nds32_fag_init (&fag_head);
1c8f6a4d 12992 ifc_inside = FALSE;
35c08157
KLC
12993 }
12994 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12995 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12996 {
12997 int accu;
1c8f6a4d 12998 struct nds32_fag *best_fag, *tmp_fag;
35c08157
KLC
12999 int dist;
13000
13001 /* End of the region.
13002 Check whether it is worth to do fp-as-gp. */
13003
13004 if (begin_rel == NULL)
13005 {
695344c0 13006 /* xgettext:c-format */
38f14ab8
AM
13007 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
13008 abfd, sec);
35c08157
KLC
13009 continue;
13010 }
13011
13012 accu = nds32_fag_find_base (&fag_head, &best_fag);
13013
1c8f6a4d
KLC
13014 /* Clean FP7U2_FLAG because they may set ever. */
13015 tmp_fag = fag_head.next;
13016 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13017
35c08157
KLC
13018 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
13019 if (accu < FAG_THRESHOLD
fbaf61ad 13020 || !nds32_fag_mark_relax (link_info, sec, best_fag,
35c08157
KLC
13021 internal_relocs, irelend))
13022 {
13023 /* Not worth to do fp-as-gp. */
13024 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13025 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13026 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13027 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13028 nds32_fag_free_list (&fag_head);
13029 begin_rel = NULL;
13030 continue;
13031 }
13032
13033 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13034 so we use it to record the distance to the reloction of best
13035 fp-base. */
13036 dist = best_fag->relas[0] - begin_rel;
13037 BFD_ASSERT (dist > 0 && dist < 0xffffff);
13038 /* Use high 16 bits of addend to record the _FP_BASE_ matched
13039 relocation. And get the base value when relocating. */
1c8f6a4d 13040 begin_rel->r_addend &= (0x1 << 16) - 1;
35c08157
KLC
13041 begin_rel->r_addend |= dist << 16;
13042
13043 nds32_fag_free_list (&fag_head);
13044 begin_rel = NULL;
13045 }
13046
1c8f6a4d 13047 if (begin_rel == NULL || ifc_inside)
35c08157
KLC
13048 /* Skip if we are not in the region of fp-as-gp. */
13049 continue;
13050
13051 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13052 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13053 {
13054 bfd_vma addr;
13055 uint32_t insn;
13056
13057 /* A gp-relative access is found. Insert it to the fag-list. */
13058
13059 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
13060 insn = bfd_getb32 (contents + irel->r_offset);
13061 if (!N32_IS_RT3 (insn))
13062 continue;
13063
13064 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13065 nds32_fag_insert (&fag_head, addr, irel);
13066 }
13067 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13068 {
13069 begin_rel = NULL;
13070 }
1c8f6a4d
KLC
13071 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13072 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13073 {
13074 /* Suppress fp as gp when encounter ifc. */
13075 ifc_inside = TRUE;
13076 }
35c08157
KLC
13077 }
13078
13079 return TRUE;
13080}
13081
13082/* Remove unused `la $fp, _FD_BASE_' instruction. */
13083
13084static bfd_boolean
13085nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13086 Elf_Internal_Rela *internal_relocs,
13087 Elf_Internal_Rela *irelend)
13088{
13089 Elf_Internal_Rela *irel;
13090 Elf_Internal_Shdr *symtab_hdr;
13091 bfd_byte *contents = NULL;
13092 nds32_elf_blank_t *relax_blank_list = NULL;
13093 bfd_boolean result = TRUE;
13094 bfd_boolean unused_region = FALSE;
13095
13096 /*
fbaf61ad
NC
13097 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13098 R_NDS32_17IFC_PCREL_RELA
13099 R_NDS32_10IFCU_PCREL_RELA. */
35c08157
KLC
13100
13101 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
0c4bd9d9 13102 nds32_get_section_contents (abfd, sec, &contents, TRUE);
35c08157
KLC
13103
13104 for (irel = internal_relocs; irel < irelend; irel++)
13105 {
13106 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13107 we marked to in previous pass.
13108 DO NOT scan relocations again, since we've alreadly decided it
13109 and set the flag. */
13110 const char *syname;
13111 int syndx;
13112 uint32_t insn;
13113
13114 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13115 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13116 unused_region = TRUE;
13117 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13118 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13119 unused_region = FALSE;
13120
13121 /* We're not in the region. */
13122 if (!unused_region)
13123 continue;
13124
13125 /* _FP_BASE_ must be a GLOBAL symbol. */
13126 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13127 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13128 continue;
13129
13130 /* The symbol name must be _FP_BASE_. */
13131 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13132 if (strcmp (syname, FP_BASE_NAME) != 0)
13133 continue;
13134
13135 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13136 {
13137 /* addi.gp $fp, -256 */
13138 insn = bfd_getb32 (contents + irel->r_offset);
13139 if (insn != INSN_ADDIGP_TO_FP)
13140 continue;
13141 }
13142 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13143 {
13144 /* addi $fp, $gp, -256 */
13145 insn = bfd_getb32 (contents + irel->r_offset);
13146 if (insn != INSN_ADDI_GP_TO_FP)
13147 continue;
13148 }
13149 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13150 {
13151 /* movi $fp, FP_BASE */
13152 insn = bfd_getb32 (contents + irel->r_offset);
13153 if (insn != INSN_MOVI_TO_FP)
13154 continue;
13155 }
13156 else
13157 continue;
13158
13159 /* We got here because a FP_BASE instruction is found. */
13160 if (!insert_nds32_elf_blank_recalc_total
13161 (&relax_blank_list, irel->r_offset, 4))
13162 goto error_return;
13163 }
13164
13165finish:
13166 if (relax_blank_list)
13167 {
13168 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13169 relax_blank_list = NULL;
13170 }
13171 return result;
13172
13173error_return:
13174 result = FALSE;
13175 goto finish;
13176}
1c8f6a4d
KLC
13177
13178/* This is a version of bfd_generic_get_relocated_section_contents.
13179 We need this variety because relaxation will modify the dwarf
13180 infomation. When there is undefined symbol reference error mesage,
13181 linker need to dump line number where the symbol be used. However
13182 the address is be relaxed, it can not get the original dwarf contents.
13183 The variety only modify function call for reading in the section. */
13184
13185static bfd_byte *
13186nds32_elf_get_relocated_section_contents (bfd *abfd,
13187 struct bfd_link_info *link_info,
13188 struct bfd_link_order *link_order,
13189 bfd_byte *data,
13190 bfd_boolean relocatable,
13191 asymbol **symbols)
13192{
13193 bfd *input_bfd = link_order->u.indirect.section->owner;
13194 asection *input_section = link_order->u.indirect.section;
13195 long reloc_size;
13196 arelent **reloc_vector;
13197 long reloc_count;
13198
13199 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13200 if (reloc_size < 0)
13201 return NULL;
13202
13203 /* Read in the section. */
0c4bd9d9 13204 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
1c8f6a4d
KLC
13205 return NULL;
13206
13207 if (reloc_size == 0)
13208 return data;
13209
13210 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13211 if (reloc_vector == NULL)
13212 return NULL;
13213
13214 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13215 reloc_vector, symbols);
13216 if (reloc_count < 0)
13217 goto error_return;
13218
13219 if (reloc_count > 0)
13220 {
13221 arelent **parent;
13222 for (parent = reloc_vector; *parent != NULL; parent++)
13223 {
13224 char *error_message = NULL;
13225 asymbol *symbol;
13226 bfd_reloc_status_type r;
13227
13228 symbol = *(*parent)->sym_ptr_ptr;
13229 if (symbol->section && discarded_section (symbol->section))
13230 {
13231 bfd_byte *p;
13232 static reloc_howto_type none_howto
13233 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13234 "unused", FALSE, 0, 0, FALSE);
13235
13236 p = data + (*parent)->address * bfd_octets_per_byte (input_bfd);
13237 _bfd_clear_contents ((*parent)->howto, input_bfd, input_section,
13238 p);
13239 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13240 (*parent)->addend = 0;
13241 (*parent)->howto = &none_howto;
13242 r = bfd_reloc_ok;
13243 }
13244 else
13245 r = bfd_perform_relocation (input_bfd, *parent, data,
13246 input_section,
13247 relocatable ? abfd : NULL,
13248 &error_message);
13249
13250 if (relocatable)
13251 {
13252 asection *os = input_section->output_section;
13253
13254 /* A partial link, so keep the relocs. */
13255 os->orelocation[os->reloc_count] = *parent;
13256 os->reloc_count++;
13257 }
13258
13259 if (r != bfd_reloc_ok)
13260 {
13261 switch (r)
13262 {
13263 case bfd_reloc_undefined:
1a72702b
AM
13264 (*link_info->callbacks->undefined_symbol)
13265 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13266 input_bfd, input_section, (*parent)->address, TRUE);
1c8f6a4d
KLC
13267 break;
13268 case bfd_reloc_dangerous:
13269 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13270 (*link_info->callbacks->reloc_dangerous)
13271 (link_info, error_message,
13272 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13273 break;
13274 case bfd_reloc_overflow:
1a72702b
AM
13275 (*link_info->callbacks->reloc_overflow)
13276 (link_info, NULL,
13277 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13278 (*parent)->howto->name, (*parent)->addend,
13279 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13280 break;
13281 case bfd_reloc_outofrange:
13282 /* PR ld/13730:
13283 This error can result when processing some partially
13284 complete binaries. Do not abort, but issue an error
13285 message instead. */
13286 link_info->callbacks->einfo
695344c0 13287 /* xgettext:c-format */
c1c8c1ef 13288 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
1c8f6a4d
KLC
13289 abfd, input_section, * parent);
13290 goto error_return;
13291
13292 default:
13293 abort ();
13294 break;
13295 }
13296 }
13297 }
13298 }
13299
13300 free (reloc_vector);
13301 return data;
13302
13303error_return:
13304 free (reloc_vector);
13305 return NULL;
13306}
fbaf61ad
NC
13307
13308/* Check target symbol. */
13309
13310static bfd_boolean
13311nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13312{
13313 if (!sym || !sym->name || sym->name[0] != '$')
13314 return FALSE;
13315 return TRUE;
13316}
13317
13318/* nds32 find maybe function sym. Ignore target special symbol
13319 first, and then go the general function. */
13320
13321static bfd_size_type
13322nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13323 bfd_vma *code_off)
13324{
13325 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13326 return 0;
13327
13328 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13329}
35c08157 13330\f
35c08157 13331
fbaf61ad
NC
13332/* Do TLS model conversion. */
13333
13334typedef struct relax_group_list_t
13335{
13336 Elf_Internal_Rela *relo;
13337 struct relax_group_list_t *next;
13338 struct relax_group_list_t *next_sibling;
13339 int id;
13340} relax_group_list_t;
13341
13342int
13343list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13344
13345int
13346list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13347
13348void
13349dump_chain (relax_group_list_t *pHead);
13350
13351int
13352list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13353{
13354 relax_group_list_t *pNext = pHead;
13355
13356 /* Find place. */
13357 while (pNext->next)
13358 {
13359 if (pNext->next->id > (int) pElem->r_addend)
13360 break;
13361
13362 pNext = pNext->next;
13363 }
13364
13365 /* Insert node. */
13366 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13367 if (!pNew)
13368 return FALSE;
13369
13370 relax_group_list_t *tmp = pNext->next;
13371 pNext->next = pNew;
13372
13373 pNew->id = pElem->r_addend;
13374 pNew->relo = pElem;
13375 pNew->next = tmp;
13376 pNew->next_sibling = NULL;
13377
13378 return TRUE;
13379}
13380
13381int
13382list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13383{
13384 relax_group_list_t *pNext = pNode;
13385
13386 /* Find place. */
13387 while (pNext->next_sibling)
13388 {
13389 pNext = pNext->next_sibling;
13390 }
13391
13392 /* Insert node. */
13393 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13394 if (!pNew)
13395 return FALSE;
13396
13397 relax_group_list_t *tmp = pNext->next_sibling;
13398 pNext->next_sibling = pNew;
13399
13400 pNew->id = -1;
13401 pNew->relo = pElem;
13402 pNew->next = NULL;
13403 pNew->next_sibling = tmp;
13404
13405 return TRUE;
13406}
13407
13408void
13409dump_chain (relax_group_list_t *pHead)
13410{
13411 relax_group_list_t *pNext = pHead->next;
13412 while (pNext)
13413 {
13414 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13415 relax_group_list_t *pNextSib = pNext->next_sibling;
13416 while (pNextSib)
13417 {
13418 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13419 pNextSib = pNextSib->next_sibling;
13420 }
13421 pNext = pNext->next;
13422 printf("\n");
13423 }
13424}
13425
13426/* Check R_NDS32_RELAX_GROUP of each section.
13427 There might be multiple sections in one object file. */
13428
13429int
13430elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13431{
13432 elf32_nds32_relax_group_t *relax_group_ptr =
13433 elf32_nds32_relax_group_ptr (abfd);
13434
13435 int min_id = relax_group_ptr->min_id;
13436 int max_id = relax_group_ptr->max_id;
13437
13438 Elf_Internal_Rela *rel;
13439 Elf_Internal_Rela *relend;
13440 Elf_Internal_Rela *relocs;
13441 enum elf_nds32_reloc_type rtype;
13442
13443 do
13444 {
13445 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13446 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13447 TRUE /* keep_memory */);
13448 if (relocs == NULL)
13449 break;
13450
13451 /* Check R_NDS32_RELAX_GROUP. */
13452 relend = relocs + asec->reloc_count;
13453 for (rel = relocs; rel < relend; rel++)
13454 {
13455 int id;
13456 rtype = ELF32_R_TYPE (rel->r_info);
13457 if (rtype != R_NDS32_RELAX_GROUP)
13458 continue;
13459
13460 id = rel->r_addend;
13461 if (id < min_id)
13462 min_id = id;
13463 else if (id > max_id)
13464 max_id = id;
13465 }
13466 }
13467 while (FALSE);
13468
13469 if ((relocs != NULL) && (elf_section_data (asec)->relocs != relocs))
13470 free (relocs);
13471
13472 if ((min_id != relax_group_ptr->min_id)
13473 || (max_id != relax_group_ptr->max_id))
13474 {
13475 relax_group_ptr->count = max_id - min_id + 1;
13476 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13477 relax_group_ptr->min_id = min_id;
13478 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13479 relax_group_ptr->max_id = max_id;
13480 }
13481
13482 return relax_group_ptr->count;
13483}
13484
13485/* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13486struct section_id_list_t *relax_group_section_id_list = NULL;
13487
13488struct section_id_list_t *
13489elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13490{
13491 struct section_id_list_t *result = NULL;
13492 struct section_id_list_t *lst = *lst_ptr;
13493
13494 if (NULL == lst)
13495 {
13496 result = (struct section_id_list_t *) calloc
13497 (1, sizeof (struct section_id_list_t));
13498 BFD_ASSERT (result); /* Feed me. */
13499 result->id = id;
13500 *lst_ptr = result;
13501 }
13502 else
13503 {
13504 struct section_id_list_t *cur = lst;
13505 struct section_id_list_t *prv = NULL;
13506 struct section_id_list_t *sec = NULL;
13507
13508 while (cur)
13509 {
13510 if (cur->id < id)
13511 {
13512 prv = cur;
13513 cur = cur->next;
13514 continue;
13515 }
13516
13517 if (cur->id > id)
13518 {
13519 cur = NULL; /* To insert after prv. */
13520 sec = cur; /* In case prv == NULL. */
13521 }
13522
13523 break;
13524 }
13525
13526 if (NULL == cur)
13527 {
13528 /* Insert after prv. */
13529 result = (struct section_id_list_t *) calloc
13530 (1, sizeof (struct section_id_list_t));
13531 BFD_ASSERT (result); /* Feed me. */
13532 result->id = id;
13533 if (NULL != prv)
13534 {
13535 result->next = prv->next;
13536 prv->next = result;
13537 }
13538 else
13539 {
13540 *lst_ptr = result;
13541 result->next = sec;
13542 }
13543 }
13544 }
13545
13546 return result;
13547}
13548
13549int
13550elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13551{
13552 static int next_relax_group_bias = 0;
13553
13554 elf32_nds32_relax_group_t *relax_group_ptr =
13555 elf32_nds32_relax_group_ptr (abfd);
13556
13557 bfd_boolean result = TRUE;
13558 Elf_Internal_Rela *rel;
13559 Elf_Internal_Rela *relend;
13560 Elf_Internal_Rela *relocs = NULL;
13561 enum elf_nds32_reloc_type rtype;
13562 struct section_id_list_t *node = NULL;
13563 int count = 0;
13564
13565 do
13566 {
13567 if (0 == relax_group_ptr->count)
13568 break;
13569
13570 /* Check if this section has been handled. */
13571 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13572 if (NULL == node)
13573 break; /* Hit, the section id has handled. */
13574
13575 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13576 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13577 TRUE /* keep_memory */);
13578 if (relocs == NULL)
13579 {
13580 BFD_ASSERT (0); /* feed me */
13581 break;
13582 }
13583
13584 /* Allocate group id bias for this bfd! */
13585 if (0 == relax_group_ptr->init)
13586 {
13587 relax_group_ptr->bias = next_relax_group_bias;
13588 next_relax_group_bias += relax_group_ptr->count;
13589 relax_group_ptr->init = 1;
13590 }
13591
13592 /* Reorder relax group groups. */
13593 relend = relocs + asec->reloc_count;
13594 for (rel = relocs; rel < relend; rel++)
13595 {
13596 rtype = ELF32_R_TYPE(rel->r_info);
13597 if (rtype != R_NDS32_RELAX_GROUP)
13598 continue;
13599
13600 /* Change it. */
13601 rel->r_addend += relax_group_ptr->bias;
13602 /* Debugging count. */
13603 count++;
13604 }
13605 }
13606 while (FALSE);
13607
13608 if (relocs != NULL && elf_section_data (asec)->relocs != relocs)
13609 free (relocs);
13610
13611 return result;
13612}
13613
13614int
13615nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13616 struct bfd_link_info *lnkinfo)
13617{
13618 bfd_boolean result = TRUE;
13619 Elf_Internal_Rela *irel;
13620 Elf_Internal_Rela *irelend;
13621 Elf_Internal_Rela *internal_relocs;
13622 unsigned long r_symndx;
13623 enum elf_nds32_reloc_type r_type;
13624
13625 Elf_Internal_Sym *local_syms = NULL;
13626 bfd_byte *contents = NULL;
13627
13628 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13629
13630 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13631 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13632 sym_hashes = elf_sym_hashes (inbfd);
13633 sym_hashes_end =
13634 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13635 if (!elf_bad_symtab (inbfd))
13636 sym_hashes_end -= symtab_hdr->sh_info;
13637
13638 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13639 if (bfd_link_relocatable (lnkinfo))
13640 {
13641 elf32_nds32_unify_relax_group (inbfd, insec);
13642 return result;
13643 }
13644
13645 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13646 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13647 TRUE /* keep_memory */);
13648 if (internal_relocs == NULL)
13649 goto error_return;
13650
13651 irelend = internal_relocs + insec->reloc_count;
13652 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13653 irelend, R_NDS32_RELAX_ENTRY);
13654 if (irel == irelend)
13655 goto finish;
13656
13657 /* Chain/remove groups. */
13658 for (irel = internal_relocs; irel < irelend; irel++)
13659 {
13660 r_symndx = ELF32_R_SYM (irel->r_info);
13661 r_type = ELF32_R_TYPE (irel->r_info);
13662 if (r_type != R_NDS32_RELAX_GROUP)
13663 continue;
13664
13665 /* Remove it. */
13666 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13667 /* Chain it now. */
13668 if (!list_insert (&chain, irel))
13669 goto error_return;
13670 }
13671
13672 /* Collect group relocations. */
13673 /* Presume relocations are sorted. */
13674 relax_group_list_t *pNext = chain.next;
13675 while (pNext)
13676 {
13677 for (irel = internal_relocs; irel < irelend; irel++)
13678 {
13679 if (irel->r_offset == pNext->relo->r_offset)
13680 {
13681 /* Ignore Non-TLS relocation types. */
13682 r_type = ELF32_R_TYPE (irel->r_info);
13683 if ((R_NDS32_TLS_LE_HI20 > r_type)
13684 || (R_NDS32_RELAX_ENTRY == r_type))
13685 continue;
13686
13687 if (!list_insert_sibling (pNext, irel))
13688 goto error_return;
13689 }
13690 else if (irel->r_offset > pNext->relo->r_offset)
13691 {
13692 pNext = pNext->next;
13693 if (!pNext)
13694 break;
13695
13696 bfd_vma current_offset = pNext->relo->r_offset;
13697 if (irel->r_offset > current_offset)
13698 irel = internal_relocs; /* restart from head */
13699 else
13700 --irel; /* Check current irel again. */
13701 continue;
13702 }
13703 else
13704 {
13705 /* This shouldn't be reached. */
13706 }
13707 }
13708 if (pNext)
13709 pNext = pNext->next;
13710 }
13711
13712#ifdef DUBUG_VERBOSE
13713 dump_chain(&chain);
13714#endif
13715
13716 /* Get symbol table and section content. */
13717 if (incontents)
13718 contents = incontents;
13719 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13720 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13721 goto error_return;
13722
13723 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13724
13725 /* Convert TLS model each group if necessary. */
13726 pNext = chain.next;
13727
13728 int cur_grp_id = -1;
13729 int sethi_rt = -1;
13730 int add_rt = -1;
13731 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13732
13733 tls_type = org_tls_type = eff_tls_type = 0;
13734
13735 while (pNext)
13736 {
13737 relax_group_list_t *pNextSig = pNext->next_sibling;
13738 while (pNextSig)
13739 {
13740 struct elf_link_hash_entry *h = NULL;
13741
13742 irel = pNextSig->relo;
13743 r_symndx = ELF32_R_SYM(irel->r_info);
13744 r_type = ELF32_R_TYPE(irel->r_info);
13745
13746 if (pNext->id != cur_grp_id)
13747 {
13748 cur_grp_id = pNext->id;
13749 org_tls_type = get_tls_type (r_type, NULL);
13750 if (r_symndx >= symtab_hdr->sh_info)
13751 {
13752 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13753 while (h->root.type == bfd_link_hash_indirect
13754 || h->root.type == bfd_link_hash_warning)
13755 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13756 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13757 }
13758 else
13759 {
13760 tls_type = local_got_tls_type
13761 ? local_got_tls_type[r_symndx]
13762 : GOT_NORMAL;
13763 }
13764
13765 eff_tls_type = 1 << (fls (tls_type) - 1);
13766 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13767 }
13768
13769 if (eff_tls_type != org_tls_type)
13770 {
13771 switch (org_tls_type)
13772 {
13773 /* DESC to IEGP/IE/LE. */
13774 case GOT_TLS_DESC:
13775 switch (eff_tls_type)
13776 {
13777 case GOT_TLS_IE:
13778 switch (r_type)
13779 {
13780 case R_NDS32_TLS_DESC_HI20:
13781 irel->r_info = ELF32_R_INFO(r_symndx,
13782 R_NDS32_TLS_IE_HI20);
13783 break;
13784 case R_NDS32_TLS_DESC_LO12:
13785 irel->r_info = ELF32_R_INFO(r_symndx,
13786 R_NDS32_TLS_IE_LO12);
13787 break;
13788 case R_NDS32_TLS_DESC_ADD:
13789 {
13790 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13791 add_rt = N32_RT5 (insn);
13792 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13793 bfd_putb32 (insn, contents + irel->r_offset);
13794
13795 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13796 }
13797 break;
13798 case R_NDS32_TLS_DESC_FUNC:
13799 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13800 irel->r_info = ELF32_R_INFO(r_symndx,
13801 R_NDS32_RELAX_REMOVE);
13802 break;
13803 case R_NDS32_TLS_DESC_CALL:
13804 {
13805 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13806 REG_TP);
13807 bfd_putb32 (insn, contents + irel->r_offset);
13808
13809 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13810 }
13811 break;
13812 case R_NDS32_LOADSTORE:
13813 case R_NDS32_PTR:
13814 case R_NDS32_PTR_RESOLVED:
13815 case R_NDS32_NONE:
13816 case R_NDS32_LABEL:
13817 break;
13818 default:
13819 BFD_ASSERT(0);
13820 break;
13821 }
13822 break;
13823 case GOT_TLS_IEGP:
13824 switch (r_type)
13825 {
13826 case R_NDS32_TLS_DESC_HI20:
13827 irel->r_info = ELF32_R_INFO(r_symndx,
13828 R_NDS32_TLS_IEGP_HI20);
13829 break;
13830 case R_NDS32_TLS_DESC_LO12:
13831 irel->r_info = ELF32_R_INFO(r_symndx,
13832 R_NDS32_TLS_IEGP_LO12);
13833 break;
13834 case R_NDS32_TLS_DESC_ADD:
13835 {
13836 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13837 add_rt = N32_RT5 (insn);
13838 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13839 bfd_putb32 (insn, contents + irel->r_offset);
13840
13841 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13842 }
13843 break;
13844 case R_NDS32_TLS_DESC_FUNC:
13845 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13846 irel->r_info = ELF32_R_INFO(r_symndx,
13847 R_NDS32_RELAX_REMOVE);
13848 break;
13849 case R_NDS32_TLS_DESC_CALL:
13850 {
13851 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13852 REG_TP);
13853 bfd_putb32 (insn, contents + irel->r_offset);
13854
13855 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13856 }
13857 break;
13858 case R_NDS32_LOADSTORE:
13859 case R_NDS32_PTR:
13860 case R_NDS32_PTR_RESOLVED:
13861 case R_NDS32_NONE:
13862 case R_NDS32_LABEL:
13863 break;
13864 default:
13865 BFD_ASSERT(0);
13866 break;
13867 }
13868 break;
13869 case GOT_TLS_LE:
13870 switch (r_type)
13871 {
13872 case R_NDS32_TLS_DESC_HI20:
13873 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13874 break;
13875 case R_NDS32_TLS_DESC_LO12:
13876 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13877 break;
13878 case R_NDS32_TLS_DESC_ADD:
13879 {
13880 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13881
13882 add_rt = N32_RT5 (insn);
13883 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13884 bfd_putb32 (insn, contents + irel->r_offset);
13885
13886 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13887 }
13888 break;
13889 case R_NDS32_TLS_DESC_FUNC:
13890 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13891 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13892 break;
13893 case R_NDS32_TLS_DESC_CALL:
13894 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13895 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13896 break;
13897 case R_NDS32_LOADSTORE:
13898 case R_NDS32_PTR:
13899 case R_NDS32_PTR_RESOLVED:
13900 case R_NDS32_NONE:
13901 case R_NDS32_LABEL:
13902 break;
13903 default:
13904 BFD_ASSERT(0);
13905 break;
13906 }
13907 break;
13908 default:
13909 break;
13910 }
13911 break;
13912 /* IEGP to IE/LE. */
13913 case GOT_TLS_IEGP:
13914 switch (eff_tls_type)
13915 {
13916 case GOT_TLS_IE:
13917 switch (r_type)
13918 {
13919 case R_NDS32_TLS_IEGP_HI20:
13920 irel->r_info = ELF32_R_INFO(r_symndx,
13921 R_NDS32_TLS_IE_HI20);
13922 break;
13923 case R_NDS32_TLS_IEGP_LO12:
13924 irel->r_info = ELF32_R_INFO(r_symndx,
13925 R_NDS32_TLS_IE_LO12);
13926 break;
13927 case R_NDS32_PTR_RESOLVED:
13928 {
13929 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13930
13931 add_rt = N32_RT5 (insn);
13932 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13933 bfd_putb32 (insn, contents + irel->r_offset);
13934 }
13935 break;
13936 case R_NDS32_TLS_IEGP_LW:
13937 break;
13938 case R_NDS32_LOADSTORE:
13939 case R_NDS32_PTR:
13940 case R_NDS32_NONE:
13941 case R_NDS32_LABEL:
13942 break;
13943 default:
13944 BFD_ASSERT(0);
13945 break;
13946 }
13947 break;
13948 case GOT_TLS_LE:
13949 switch (r_type)
13950 {
13951 case R_NDS32_TLS_IEGP_HI20:
13952 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13953 break;
13954 case R_NDS32_TLS_IEGP_LO12:
13955 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13956 break;
13957 case R_NDS32_TLS_IEGP_LW:
13958 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13959 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13960 break;
13961 case R_NDS32_LOADSTORE:
13962 case R_NDS32_PTR:
13963 case R_NDS32_NONE:
13964 case R_NDS32_LABEL:
13965 case R_NDS32_PTR_RESOLVED:
13966 break;
13967 default:
13968 BFD_ASSERT(0);
13969 break;
13970 }
13971 break;
13972 default:
13973 break;
13974 }
13975 break;
13976 /* IE to LE. */
13977 case GOT_TLS_IE:
13978 switch (eff_tls_type)
13979 {
13980 case GOT_TLS_LE:
13981 switch (r_type)
13982 {
13983 case R_NDS32_TLS_IE_HI20:
13984 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13985 break;
13986 case R_NDS32_TLS_IE_LO12S2:
13987 {
13988 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13989
13990 add_rt = N32_RT5 (insn);
13991 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13992 bfd_putb32 (insn, contents + irel->r_offset);
13993
13994 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13995 }
13996 break;
13997 case R_NDS32_LOADSTORE:
13998 case R_NDS32_PTR:
13999 case R_NDS32_NONE:
14000 case R_NDS32_LABEL:
14001 break;
14002 default:
14003 BFD_ASSERT(0);
14004 break;
14005 }
14006 break;
14007 default:
14008 break;
14009 }
14010 break;
14011 default:
14012 break;
14013 }
14014 }
14015 pNextSig = pNextSig->next_sibling;
14016 }
14017
14018#if 1
14019 pNext = pNext->next;
14020#else
14021 while (pNext)
14022 {
14023 if (pNext->id != cur_grp_id)
14024 break;
14025 pNext = pNext->next;
14026 }
14027#endif
14028 }
14029
14030finish:
14031 if (incontents)
14032 contents = NULL;
14033
14034 if (internal_relocs != NULL
14035 && elf_section_data (insec)->relocs != internal_relocs)
14036 free (internal_relocs);
14037
14038 if (contents != NULL
14039 && elf_section_data (insec)->this_hdr.contents != contents)
14040 free (contents);
14041
14042 if (local_syms != NULL && symtab_hdr->contents != (bfd_byte *) local_syms)
14043 free (local_syms);
14044
14045 if (chain.next)
14046 {
14047 pNext = chain.next;
14048 relax_group_list_t *pDel;
14049 while (pNext)
14050 {
14051 pDel = pNext;
14052 pNext = pNext->next;
14053 free (pDel);
14054 }
14055 }
14056
14057 return result;
14058
14059error_return:
14060 result = FALSE;
14061 goto finish;
14062}
14063
14064/* End TLS model conversion. */
14065
35c08157
KLC
14066#define ELF_ARCH bfd_arch_nds32
14067#define ELF_MACHINE_CODE EM_NDS32
14068#define ELF_MAXPAGESIZE 0x1000
07d6d2b8 14069#define ELF_TARGET_ID NDS32_ELF_DATA
35c08157 14070
6d00b590 14071#define TARGET_BIG_SYM nds32_elf32_be_vec
35c08157 14072#define TARGET_BIG_NAME "elf32-nds32be"
6d00b590 14073#define TARGET_LITTLE_SYM nds32_elf32_le_vec
35c08157
KLC
14074#define TARGET_LITTLE_NAME "elf32-nds32le"
14075
14076#define elf_info_to_howto nds32_info_to_howto
14077#define elf_info_to_howto_rel nds32_info_to_howto_rel
14078
14079#define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14080#define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14081#define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14082#define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14083#define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14084
07d6d2b8 14085#define bfd_elf32_mkobject nds32_elf_mkobject
35c08157
KLC
14086#define elf_backend_action_discarded nds32_elf_action_discarded
14087#define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14088#define elf_backend_check_relocs nds32_elf_check_relocs
14089#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14090#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14091#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14092#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14093#define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14094#define elf_backend_relocate_section nds32_elf_relocate_section
14095#define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
35c08157
KLC
14096#define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14097#define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14098#define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14099#define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14100#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14101#define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14102#define elf_backend_object_p nds32_elf_object_p
14103#define elf_backend_final_write_processing nds32_elf_final_write_processing
14104#define elf_backend_special_sections nds32_elf_special_sections
1c8f6a4d 14105#define bfd_elf32_bfd_get_relocated_section_contents \
07d6d2b8 14106 nds32_elf_get_relocated_section_contents
fbaf61ad
NC
14107#define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14108#define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
35c08157
KLC
14109
14110#define elf_backend_can_gc_sections 1
14111#define elf_backend_can_refcount 1
14112#define elf_backend_want_got_plt 1
14113#define elf_backend_plt_readonly 1
14114#define elf_backend_want_plt_sym 0
14115#define elf_backend_got_header_size 12
14116#define elf_backend_may_use_rel_p 1
14117#define elf_backend_default_use_rela_p 1
14118#define elf_backend_may_use_rela_p 1
fbaf61ad 14119#define elf_backend_dtrel_excludes_plt 0
35c08157
KLC
14120
14121#include "elf32-target.h"
14122
14123#undef ELF_MAXPAGESIZE
14124#define ELF_MAXPAGESIZE 0x2000
14125
fbaf61ad 14126#undef TARGET_BIG_SYM
6d00b590 14127#define TARGET_BIG_SYM nds32_elf32_linux_be_vec
fbaf61ad 14128#undef TARGET_BIG_NAME
35c08157 14129#define TARGET_BIG_NAME "elf32-nds32be-linux"
fbaf61ad 14130#undef TARGET_LITTLE_SYM
6d00b590 14131#define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
fbaf61ad 14132#undef TARGET_LITTLE_NAME
35c08157 14133#define TARGET_LITTLE_NAME "elf32-nds32le-linux"
fbaf61ad 14134#undef elf32_bed
35c08157
KLC
14135#define elf32_bed elf32_nds32_lin_bed
14136
14137#include "elf32-target.h"