ld: Pass -fno-sanitize=all to tests with linker
[deliverable/binutils-gdb.git] / bfd / elf32-nds32.c
CommitLineData
35c08157 1/* NDS32-specific support for 32-bit ELF.
b3adc24a 2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
35c08157
KLC
3 Contributed by Andes Technology Corporation.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
1c8f6a4d 20 02110-1301, USA. */
35c08157
KLC
21
22
23#include "sysdep.h"
24#include "bfd.h"
35c08157
KLC
25#include "bfdlink.h"
26#include "libbfd.h"
27#include "elf-bfd.h"
28#include "libiberty.h"
35c08157
KLC
29#include "elf/nds32.h"
30#include "opcode/nds32.h"
31#include "elf32-nds32.h"
32#include "opcode/cgen.h"
33#include "../opcodes/nds32-opc.h"
34
bb294208
AM
35/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
36#define OCTETS_PER_BYTE(ABFD, SEC) 1
37
35c08157
KLC
38/* Relocation HOWTO functions. */
39static bfd_reloc_status_type nds32_elf_ignore_reloc
40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43static bfd_reloc_status_type nds32_elf_hi20_reloc
44 (bfd *, arelent *, asymbol *, void *,
45 asection *, bfd *, char **);
46static bfd_reloc_status_type nds32_elf_lo12_reloc
47 (bfd *, arelent *, asymbol *, void *,
48 asection *, bfd *, char **);
49static bfd_reloc_status_type nds32_elf_generic_reloc
50 (bfd *, arelent *, asymbol *, void *,
51 asection *, bfd *, char **);
52static bfd_reloc_status_type nds32_elf_sda15_reloc
53 (bfd *, arelent *, asymbol *, void *,
54 asection *, bfd *, char **);
55
56/* Helper functions for HOWTO. */
57static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
58 (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
59 asection *, bfd_vma, bfd_vma);
35c08157
KLC
60
61/* Nds32 helper functions. */
35c08157 62static bfd_vma calculate_memory_address
fbaf61ad 63 (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
0c4bd9d9
KLC
64static int nds32_get_section_contents (bfd *, asection *,
65 bfd_byte **, bfd_boolean);
35c08157
KLC
66static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67 Elf_Internal_Sym **);
35c08157
KLC
68static bfd_boolean nds32_relax_fp_as_gp
69 (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
70 Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
71 Elf_Internal_Sym *isymbuf);
72static bfd_boolean nds32_fag_remove_unused_fpbase
73 (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74 Elf_Internal_Rela *irelend);
75
76enum
77{
78 MACH_V1 = bfd_mach_n1h,
79 MACH_V2 = bfd_mach_n1h_v2,
80 MACH_V3 = bfd_mach_n1h_v3,
81 MACH_V3M = bfd_mach_n1h_v3m
82};
83
84#define MIN(a, b) ((a) > (b) ? (b) : (a))
85#define MAX(a, b) ((a) > (b) ? (a) : (b))
86
87/* The name of the dynamic interpreter. This is put in the .interp
88 section. */
89#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
90
fbaf61ad
NC
91#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92 && (flags) & SEC_LOAD \
93 && (flags) & SEC_READONLY)
94
35c08157
KLC
95/* The nop opcode we use. */
96#define NDS32_NOP32 0x40000009
97#define NDS32_NOP16 0x9200
98
99/* The size in bytes of an entry in the procedure linkage table. */
100#define PLT_ENTRY_SIZE 24
101#define PLT_HEADER_SIZE 24
102
103/* The first entry in a procedure linkage table are reserved,
104 and the initial contents are unimportant (we zero them out).
105 Subsequent entries look like this. */
07d6d2b8
AM
106#define PLT0_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(.got+4) */
107#define PLT0_ENTRY_WORD1 0x58f78000 /* ori r15, r25, LO12(.got+4) */
108#define PLT0_ENTRY_WORD2 0x05178000 /* lwi r17, [r15+0] */
109#define PLT0_ENTRY_WORD3 0x04f78001 /* lwi r15, [r15+4] */
110#define PLT0_ENTRY_WORD4 0x4a003c00 /* jr r15 */
35c08157
KLC
111
112/* $ta is change to $r15 (from $r25). */
113#define PLT0_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[1]@GOT) */
07d6d2b8
AM
114#define PLT0_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[1]@GOT) */
115#define PLT0_PIC_ENTRY_WORD2 0x40f7f400 /* add r15, gp, r15 */
116#define PLT0_PIC_ENTRY_WORD3 0x05178000 /* lwi r17, [r15+0] */
117#define PLT0_PIC_ENTRY_WORD4 0x04f78001 /* lwi r15, [r15+4] */
118#define PLT0_PIC_ENTRY_WORD5 0x4a003c00 /* jr r15 */
119
120#define PLT_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(&got[n+3]) */
121#define PLT_ENTRY_WORD1 0x04f78000 /* lwi r15, r15, LO12(&got[n+3]) */
122#define PLT_ENTRY_WORD2 0x4a003c00 /* jr r15 */
123#define PLT_ENTRY_WORD3 0x45000000 /* movi r16, sizeof(RELA) * n */
124#define PLT_ENTRY_WORD4 0x48000000 /* j .plt0. */
35c08157
KLC
125
126#define PLT_PIC_ENTRY_WORD0 0x46f00000 /* sethi r15, HI20(got[n+3]@GOT) */
07d6d2b8
AM
127#define PLT_PIC_ENTRY_WORD1 0x58f78000 /* ori r15, r15, LO12(got[n+3]@GOT) */
128#define PLT_PIC_ENTRY_WORD2 0x38febc02 /* lw r15, [gp+r15] */
129#define PLT_PIC_ENTRY_WORD3 0x4a003c00 /* jr r15 */
130#define PLT_PIC_ENTRY_WORD4 0x45000000 /* movi r16, sizeof(RELA) * n */
131#define PLT_PIC_ENTRY_WORD5 0x48000000 /* j .plt0 */
35c08157 132
1c8f6a4d
KLC
133/* These are macros used to get the relocation accurate value. */
134#define ACCURATE_8BIT_S1 (0x100)
135#define ACCURATE_U9BIT_S1 (0x400)
136#define ACCURATE_12BIT_S1 (0x2000)
137#define ACCURATE_14BIT_S1 (0x4000)
138#define ACCURATE_19BIT (0x40000)
139
140/* These are macros used to get the relocation conservative value. */
141#define CONSERVATIVE_8BIT_S1 (0x100 - 4)
142#define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143#define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144#define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
145/* These must be more conservative because the address may be in
146 different segment. */
147#define CONSERVATIVE_15BIT (0x4000 - 0x1000)
148#define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
149#define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
150#define CONSERVATIVE_19BIT (0x40000 - 0x1000)
151#define CONSERVATIVE_20BIT (0x80000 - 0x1000)
152
35c08157
KLC
153/* Size of small data/bss sections, used to calculate SDA_BASE. */
154static long got_size = 0;
155static int is_SDA_BASE_set = 0;
35c08157 156
35c08157
KLC
157/* Convert ELF-VER in eflags to string for debugging purpose. */
158static const char *const nds32_elfver_strtab[] =
159{
160 "ELF-1.2",
161 "ELF-1.3",
162 "ELF-1.4",
163};
164
165/* The nds32 linker needs to keep track of the number of relocs that it
166 decides to copy in check_relocs for each symbol. This is so that
167 it can discard PC relative relocs if it doesn't need them when
168 linking with -Bsymbolic. We store the information in a field
169 extending the regular ELF linker hash table. */
170
171/* This structure keeps track of the number of PC relative relocs we
172 have copied for a given symbol. */
173
174struct elf_nds32_pcrel_relocs_copied
175{
176 /* Next section. */
177 struct elf_nds32_pcrel_relocs_copied *next;
178 /* A section in dynobj. */
179 asection *section;
180 /* Number of relocs copied in this section. */
181 bfd_size_type count;
182};
183
fbaf61ad
NC
184enum elf_nds32_tls_type
185{
186 GOT_UNKNOWN = (0),
187 GOT_NORMAL = (1 << 0),
188 GOT_TLS_LE = (1 << 1),
189 GOT_TLS_IE = (1 << 2),
190 GOT_TLS_IEGP = (1 << 3),
191 GOT_TLS_LD = (1 << 4),
192 GOT_TLS_GD = (1 << 5),
193 GOT_TLS_DESC = (1 << 6),
194};
195
35c08157
KLC
196/* Nds32 ELF linker hash entry. */
197
198struct elf_nds32_link_hash_entry
199{
200 struct elf_link_hash_entry root;
201
1c8f6a4d 202 /* For checking relocation type. */
fbaf61ad
NC
203 enum elf_nds32_tls_type tls_type;
204
205 int offset_to_gp;
35c08157
KLC
206};
207
208/* Get the nds32 ELF linker hash table from a link_info structure. */
209
210#define FP_BASE_NAME "_FP_BASE_"
211static int check_start_export_sym = 0;
35c08157 212
1c8f6a4d
KLC
213/* The offset for executable tls relaxation. */
214#define TP_OFFSET 0x0
215
fbaf61ad
NC
216typedef struct
217{
218 int min_id;
219 int max_id;
220 int count;
221 int bias;
222 int init;
223} elf32_nds32_relax_group_t;
224
1c8f6a4d
KLC
225struct elf_nds32_obj_tdata
226{
227 struct elf_obj_tdata root;
228
229 /* tls_type for each local got entry. */
230 char *local_got_tls_type;
fbaf61ad
NC
231
232 /* GOTPLT entries for TLS descriptors. */
233 bfd_vma *local_tlsdesc_gotent;
234
235 /* for R_NDS32_RELAX_GROUP handling. */
236 elf32_nds32_relax_group_t relax_group;
237
238 unsigned int hdr_size;
239 int* offset_to_gp;
1c8f6a4d
KLC
240};
241
242#define elf_nds32_tdata(bfd) \
243 ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
244
245#define elf32_nds32_local_got_tls_type(bfd) \
246 (elf_nds32_tdata (bfd)->local_got_tls_type)
247
fbaf61ad
NC
248#define elf32_nds32_local_gp_offset(bfd) \
249 (elf_nds32_tdata (bfd)->offset_to_gp)
250
1c8f6a4d
KLC
251#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
252
fbaf61ad
NC
253#define elf32_nds32_relax_group_ptr(bfd) \
254 &(elf_nds32_tdata (bfd)->relax_group)
255
1c8f6a4d
KLC
256static bfd_boolean
257nds32_elf_mkobject (bfd *abfd)
258{
259 return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
260 NDS32_ELF_DATA);
261}
262
35c08157 263/* Relocations used for relocation. */
fbaf61ad
NC
264/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
265 initialize array nds32_elf_howto_table in any order. The benefit
266 is that we can add any new relocations with any numbers and don't
267 need to fill the gap by lots of EMPTY_HOWTO. */
268#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
269 [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
270
35c08157
KLC
271static reloc_howto_type nds32_elf_howto_table[] =
272{
273 /* This reloc does nothing. */
fbaf61ad
NC
274 HOWTO2 (R_NDS32_NONE, /* type */
275 0, /* rightshift */
276 2, /* size (0 = byte, 1 = short, 2 = long) */
277 32, /* bitsize */
278 FALSE, /* pc_relative */
279 0, /* bitpos */
280 complain_overflow_bitfield,/* complain_on_overflow */
281 bfd_elf_generic_reloc, /* special_function */
282 "R_NDS32_NONE", /* name */
283 FALSE, /* partial_inplace */
284 0, /* src_mask */
285 0, /* dst_mask */
286 FALSE), /* pcrel_offset */
35c08157
KLC
287
288 /* A 16 bit absolute relocation. */
fbaf61ad
NC
289 HOWTO2 (R_NDS32_16, /* type */
290 0, /* rightshift */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
292 16, /* bitsize */
293 FALSE, /* pc_relative */
294 0, /* bitpos */
295 complain_overflow_bitfield,/* complain_on_overflow */
296 nds32_elf_generic_reloc,/* special_function */
297 "R_NDS32_16", /* name */
298 FALSE, /* partial_inplace */
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
301 FALSE), /* pcrel_offset */
35c08157
KLC
302
303 /* A 32 bit absolute relocation. */
fbaf61ad
NC
304 HOWTO2 (R_NDS32_32, /* type */
305 0, /* rightshift */
306 2, /* size (0 = byte, 1 = short, 2 = long) */
307 32, /* bitsize */
308 FALSE, /* pc_relative */
309 0, /* bitpos */
310 complain_overflow_bitfield,/* complain_on_overflow */
311 nds32_elf_generic_reloc,/* special_function */
312 "R_NDS32_32", /* name */
313 FALSE, /* partial_inplace */
314 0xffffffff, /* src_mask */
315 0xffffffff, /* dst_mask */
316 FALSE), /* pcrel_offset */
35c08157
KLC
317
318 /* A 20 bit address. */
fbaf61ad
NC
319 HOWTO2 (R_NDS32_20, /* type */
320 0, /* rightshift */
321 2, /* size (0 = byte, 1 = short, 2 = long) */
322 20, /* bitsize */
323 FALSE, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_unsigned,/* complain_on_overflow */
326 nds32_elf_generic_reloc,/* special_function */
327 "R_NDS32_20", /* name */
328 FALSE, /* partial_inplace */
329 0xfffff, /* src_mask */
330 0xfffff, /* dst_mask */
331 FALSE), /* pcrel_offset */
35c08157
KLC
332
333 /* An PC Relative 9-bit relocation, shifted by 2.
334 This reloc is complicated because relocations are relative to pc & -4.
335 i.e. branches in the right insn slot use the address of the left insn
336 slot for pc. */
fbaf61ad 337 /* It's not clear whether this should have partial_inplace set or not.
35c08157
KLC
338 Branch relaxing in the assembler can store the addend in the insn,
339 and if bfd_install_relocation gets called the addend may get added
340 again. */
fbaf61ad
NC
341 HOWTO2 (R_NDS32_9_PCREL, /* type */
342 1, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 8, /* bitsize */
345 TRUE, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_signed,/* complain_on_overflow */
348 nds32_elf_9_pcrel_reloc,/* special_function */
349 "R_NDS32_9_PCREL", /* name */
350 FALSE, /* partial_inplace */
351 0xff, /* src_mask */
352 0xff, /* dst_mask */
353 TRUE), /* pcrel_offset */
35c08157
KLC
354
355 /* A relative 15 bit relocation, right shifted by 1. */
fbaf61ad
NC
356 HOWTO2 (R_NDS32_15_PCREL, /* type */
357 1, /* rightshift */
358 2, /* size (0 = byte, 1 = short, 2 = long) */
359 14, /* bitsize */
360 TRUE, /* pc_relative */
361 0, /* bitpos */
362 complain_overflow_signed,/* complain_on_overflow */
363 bfd_elf_generic_reloc, /* special_function */
364 "R_NDS32_15_PCREL", /* name */
365 FALSE, /* partial_inplace */
366 0x3fff, /* src_mask */
367 0x3fff, /* dst_mask */
368 TRUE), /* pcrel_offset */
35c08157
KLC
369
370 /* A relative 17 bit relocation, right shifted by 1. */
fbaf61ad
NC
371 HOWTO2 (R_NDS32_17_PCREL, /* type */
372 1, /* rightshift */
373 2, /* size (0 = byte, 1 = short, 2 = long) */
374 16, /* bitsize */
375 TRUE, /* pc_relative */
376 0, /* bitpos */
377 complain_overflow_signed,/* complain_on_overflow */
378 bfd_elf_generic_reloc, /* special_function */
379 "R_NDS32_17_PCREL", /* name */
380 FALSE, /* partial_inplace */
381 0xffff, /* src_mask */
382 0xffff, /* dst_mask */
383 TRUE), /* pcrel_offset */
35c08157
KLC
384
385 /* A relative 25 bit relocation, right shifted by 1. */
fbaf61ad 386 /* It's not clear whether this should have partial_inplace set or not.
35c08157
KLC
387 Branch relaxing in the assembler can store the addend in the insn,
388 and if bfd_install_relocation gets called the addend may get added
389 again. */
fbaf61ad
NC
390 HOWTO2 (R_NDS32_25_PCREL, /* type */
391 1, /* rightshift */
392 2, /* size (0 = byte, 1 = short, 2 = long) */
393 24, /* bitsize */
394 TRUE, /* pc_relative */
395 0, /* bitpos */
396 complain_overflow_signed,/* complain_on_overflow */
397 bfd_elf_generic_reloc, /* special_function */
398 "R_NDS32_25_PCREL", /* name */
399 FALSE, /* partial_inplace */
400 0xffffff, /* src_mask */
401 0xffffff, /* dst_mask */
402 TRUE), /* pcrel_offset */
35c08157
KLC
403
404 /* High 20 bits of address when lower 12 is or'd in. */
fbaf61ad
NC
405 HOWTO2 (R_NDS32_HI20, /* type */
406 12, /* rightshift */
407 2, /* size (0 = byte, 1 = short, 2 = long) */
408 20, /* bitsize */
409 FALSE, /* pc_relative */
410 0, /* bitpos */
411 complain_overflow_dont,/* complain_on_overflow */
412 nds32_elf_hi20_reloc, /* special_function */
413 "R_NDS32_HI20", /* name */
414 FALSE, /* partial_inplace */
415 0x000fffff, /* src_mask */
416 0x000fffff, /* dst_mask */
417 FALSE), /* pcrel_offset */
35c08157
KLC
418
419 /* Lower 12 bits of address. */
fbaf61ad
NC
420 HOWTO2 (R_NDS32_LO12S3, /* type */
421 3, /* rightshift */
422 2, /* size (0 = byte, 1 = short, 2 = long) */
423 9, /* bitsize */
424 FALSE, /* pc_relative */
425 0, /* bitpos */
426 complain_overflow_dont,/* complain_on_overflow */
427 nds32_elf_lo12_reloc, /* special_function */
428 "R_NDS32_LO12S3", /* name */
429 FALSE, /* partial_inplace */
430 0x000001ff, /* src_mask */
431 0x000001ff, /* dst_mask */
432 FALSE), /* pcrel_offset */
35c08157
KLC
433
434 /* Lower 12 bits of address. */
fbaf61ad
NC
435 HOWTO2 (R_NDS32_LO12S2, /* type */
436 2, /* rightshift */
437 2, /* size (0 = byte, 1 = short, 2 = long) */
438 10, /* bitsize */
439 FALSE, /* pc_relative */
440 0, /* bitpos */
441 complain_overflow_dont,/* complain_on_overflow */
442 nds32_elf_lo12_reloc, /* special_function */
443 "R_NDS32_LO12S2", /* name */
444 FALSE, /* partial_inplace */
445 0x000003ff, /* src_mask */
446 0x000003ff, /* dst_mask */
447 FALSE), /* pcrel_offset */
35c08157
KLC
448
449 /* Lower 12 bits of address. */
fbaf61ad
NC
450 HOWTO2 (R_NDS32_LO12S1, /* type */
451 1, /* rightshift */
452 2, /* size (0 = byte, 1 = short, 2 = long) */
453 11, /* bitsize */
454 FALSE, /* pc_relative */
455 0, /* bitpos */
456 complain_overflow_dont,/* complain_on_overflow */
457 nds32_elf_lo12_reloc, /* special_function */
458 "R_NDS32_LO12S1", /* name */
459 FALSE, /* partial_inplace */
460 0x000007ff, /* src_mask */
461 0x000007ff, /* dst_mask */
462 FALSE), /* pcrel_offset */
35c08157
KLC
463
464 /* Lower 12 bits of address. */
fbaf61ad
NC
465 HOWTO2 (R_NDS32_LO12S0, /* type */
466 0, /* rightshift */
467 2, /* size (0 = byte, 1 = short, 2 = long) */
468 12, /* bitsize */
469 FALSE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_dont,/* complain_on_overflow */
472 nds32_elf_lo12_reloc, /* special_function */
473 "R_NDS32_LO12S0", /* name */
474 FALSE, /* partial_inplace */
475 0x00000fff, /* src_mask */
476 0x00000fff, /* dst_mask */
477 FALSE), /* pcrel_offset */
35c08157
KLC
478
479 /* Small data area 15 bits offset. */
fbaf61ad
NC
480 HOWTO2 (R_NDS32_SDA15S3, /* type */
481 3, /* rightshift */
482 2, /* size (0 = byte, 1 = short, 2 = long) */
483 15, /* bitsize */
484 FALSE, /* pc_relative */
485 0, /* bitpos */
486 complain_overflow_signed,/* complain_on_overflow */
487 nds32_elf_sda15_reloc, /* special_function */
488 "R_NDS32_SDA15S3", /* name */
489 FALSE, /* partial_inplace */
490 0x00007fff, /* src_mask */
491 0x00007fff, /* dst_mask */
492 FALSE), /* pcrel_offset */
35c08157
KLC
493
494 /* Small data area 15 bits offset. */
fbaf61ad
NC
495 HOWTO2 (R_NDS32_SDA15S2, /* type */
496 2, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 15, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_signed,/* complain_on_overflow */
502 nds32_elf_sda15_reloc, /* special_function */
503 "R_NDS32_SDA15S2", /* name */
504 FALSE, /* partial_inplace */
505 0x00007fff, /* src_mask */
506 0x00007fff, /* dst_mask */
507 FALSE), /* pcrel_offset */
35c08157
KLC
508
509 /* Small data area 15 bits offset. */
fbaf61ad
NC
510 HOWTO2 (R_NDS32_SDA15S1, /* type */
511 1, /* rightshift */
512 2, /* size (0 = byte, 1 = short, 2 = long) */
513 15, /* bitsize */
514 FALSE, /* pc_relative */
515 0, /* bitpos */
516 complain_overflow_signed,/* complain_on_overflow */
517 nds32_elf_sda15_reloc, /* special_function */
518 "R_NDS32_SDA15S1", /* name */
519 FALSE, /* partial_inplace */
520 0x00007fff, /* src_mask */
521 0x00007fff, /* dst_mask */
522 FALSE), /* pcrel_offset */
35c08157
KLC
523
524 /* Small data area 15 bits offset. */
fbaf61ad
NC
525 HOWTO2 (R_NDS32_SDA15S0, /* type */
526 0, /* rightshift */
527 2, /* size (0 = byte, 1 = short, 2 = long) */
528 15, /* bitsize */
529 FALSE, /* pc_relative */
530 0, /* bitpos */
531 complain_overflow_signed,/* complain_on_overflow */
532 nds32_elf_sda15_reloc, /* special_function */
533 "R_NDS32_SDA15S0", /* name */
534 FALSE, /* partial_inplace */
535 0x00007fff, /* src_mask */
536 0x00007fff, /* dst_mask */
537 FALSE), /* pcrel_offset */
538
539 /* GNU extension to record C++ vtable hierarchy */
540 HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type */
541 0, /* rightshift */
542 2, /* size (0 = byte, 1 = short, 2 = long) */
543 0, /* bitsize */
544 FALSE, /* pc_relative */
545 0, /* bitpos */
546 complain_overflow_dont,/* complain_on_overflow */
547 NULL, /* special_function */
548 "R_NDS32_GNU_VTINHERIT",/* name */
549 FALSE, /* partial_inplace */
550 0, /* src_mask */
551 0, /* dst_mask */
552 FALSE), /* pcrel_offset */
553
554 /* GNU extension to record C++ vtable member usage */
555 HOWTO2 (R_NDS32_GNU_VTENTRY, /* type */
556 0, /* rightshift */
557 2, /* size (0 = byte, 1 = short, 2 = long) */
558 0, /* bitsize */
559 FALSE, /* pc_relative */
560 0, /* bitpos */
561 complain_overflow_dont,/* complain_on_overflow */
562 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
563 "R_NDS32_GNU_VTENTRY", /* name */
564 FALSE, /* partial_inplace */
565 0, /* src_mask */
566 0, /* dst_mask */
567 FALSE), /* pcrel_offset */
35c08157
KLC
568
569 /* A 16 bit absolute relocation. */
fbaf61ad
NC
570 HOWTO2 (R_NDS32_16_RELA, /* type */
571 0, /* rightshift */
572 1, /* size (0 = byte, 1 = short, 2 = long) */
573 16, /* bitsize */
574 FALSE, /* pc_relative */
575 0, /* bitpos */
576 complain_overflow_bitfield,/* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_NDS32_16_RELA", /* name */
579 FALSE, /* partial_inplace */
580 0xffff, /* src_mask */
581 0xffff, /* dst_mask */
582 FALSE), /* pcrel_offset */
35c08157
KLC
583
584 /* A 32 bit absolute relocation. */
fbaf61ad
NC
585 HOWTO2 (R_NDS32_32_RELA, /* type */
586 0, /* rightshift */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
588 32, /* bitsize */
589 FALSE, /* pc_relative */
590 0, /* bitpos */
591 complain_overflow_bitfield,/* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_NDS32_32_RELA", /* name */
594 FALSE, /* partial_inplace */
595 0xffffffff, /* src_mask */
596 0xffffffff, /* dst_mask */
597 FALSE), /* pcrel_offset */
35c08157
KLC
598
599 /* A 20 bit address. */
fbaf61ad
NC
600 HOWTO2 (R_NDS32_20_RELA, /* type */
601 0, /* rightshift */
602 2, /* size (0 = byte, 1 = short, 2 = long) */
603 20, /* bitsize */
604 FALSE, /* pc_relative */
605 0, /* bitpos */
606 complain_overflow_signed,/* complain_on_overflow */
607 bfd_elf_generic_reloc, /* special_function */
608 "R_NDS32_20_RELA", /* name */
609 FALSE, /* partial_inplace */
610 0xfffff, /* src_mask */
611 0xfffff, /* dst_mask */
612 FALSE), /* pcrel_offset */
613
614 HOWTO2 (R_NDS32_9_PCREL_RELA, /* type */
615 1, /* rightshift */
616 1, /* size (0 = byte, 1 = short, 2 = long) */
617 8, /* bitsize */
618 TRUE, /* pc_relative */
619 0, /* bitpos */
620 complain_overflow_signed,/* complain_on_overflow */
621 bfd_elf_generic_reloc, /* special_function */
622 "R_NDS32_9_PCREL_RELA",/* name */
623 FALSE, /* partial_inplace */
624 0xff, /* src_mask */
625 0xff, /* dst_mask */
626 TRUE), /* pcrel_offset */
35c08157
KLC
627
628 /* A relative 15 bit relocation, right shifted by 1. */
fbaf61ad
NC
629 HOWTO2 (R_NDS32_15_PCREL_RELA,/* type */
630 1, /* rightshift */
631 2, /* size (0 = byte, 1 = short, 2 = long) */
632 14, /* bitsize */
633 TRUE, /* pc_relative */
634 0, /* bitpos */
635 complain_overflow_signed,/* complain_on_overflow */
636 bfd_elf_generic_reloc, /* special_function */
637 "R_NDS32_15_PCREL_RELA",/* name */
638 FALSE, /* partial_inplace */
639 0x3fff, /* src_mask */
640 0x3fff, /* dst_mask */
641 TRUE), /* pcrel_offset */
35c08157
KLC
642
643 /* A relative 17 bit relocation, right shifted by 1. */
fbaf61ad
NC
644 HOWTO2 (R_NDS32_17_PCREL_RELA,/* type */
645 1, /* rightshift */
646 2, /* size (0 = byte, 1 = short, 2 = long) */
647 16, /* bitsize */
648 TRUE, /* pc_relative */
649 0, /* bitpos */
650 complain_overflow_signed,/* complain_on_overflow */
651 bfd_elf_generic_reloc, /* special_function */
652 "R_NDS32_17_PCREL_RELA",/* name */
653 FALSE, /* partial_inplace */
654 0xffff, /* src_mask */
655 0xffff, /* dst_mask */
656 TRUE), /* pcrel_offset */
35c08157
KLC
657
658 /* A relative 25 bit relocation, right shifted by 2. */
fbaf61ad
NC
659 HOWTO2 (R_NDS32_25_PCREL_RELA,/* type */
660 1, /* rightshift */
661 2, /* size (0 = byte, 1 = short, 2 = long) */
662 24, /* bitsize */
663 TRUE, /* pc_relative */
664 0, /* bitpos */
665 complain_overflow_signed,/* complain_on_overflow */
666 bfd_elf_generic_reloc, /* special_function */
667 "R_NDS32_25_PCREL_RELA",/* name */
668 FALSE, /* partial_inplace */
669 0xffffff, /* src_mask */
670 0xffffff, /* dst_mask */
671 TRUE), /* pcrel_offset */
35c08157
KLC
672
673 /* High 20 bits of address when lower 16 is or'd in. */
fbaf61ad
NC
674 HOWTO2 (R_NDS32_HI20_RELA, /* type */
675 12, /* rightshift */
676 2, /* size (0 = byte, 1 = short, 2 = long) */
677 20, /* bitsize */
678 FALSE, /* pc_relative */
679 0, /* bitpos */
680 complain_overflow_dont,/* complain_on_overflow */
681 bfd_elf_generic_reloc, /* special_function */
682 "R_NDS32_HI20_RELA", /* name */
683 FALSE, /* partial_inplace */
684 0x000fffff, /* src_mask */
685 0x000fffff, /* dst_mask */
686 FALSE), /* pcrel_offset */
35c08157
KLC
687
688 /* Lower 12 bits of address. */
fbaf61ad
NC
689 HOWTO2 (R_NDS32_LO12S3_RELA, /* type */
690 3, /* rightshift */
691 2, /* size (0 = byte, 1 = short, 2 = long) */
692 9, /* bitsize */
693 FALSE, /* pc_relative */
694 0, /* bitpos */
695 complain_overflow_dont,/* complain_on_overflow */
696 bfd_elf_generic_reloc, /* special_function */
697 "R_NDS32_LO12S3_RELA", /* name */
698 FALSE, /* partial_inplace */
699 0x000001ff, /* src_mask */
700 0x000001ff, /* dst_mask */
701 FALSE), /* pcrel_offset */
35c08157
KLC
702
703 /* Lower 12 bits of address. */
fbaf61ad
NC
704 HOWTO2 (R_NDS32_LO12S2_RELA, /* type */
705 2, /* rightshift */
706 2, /* size (0 = byte, 1 = short, 2 = long) */
707 10, /* bitsize */
708 FALSE, /* pc_relative */
709 0, /* bitpos */
710 complain_overflow_dont,/* complain_on_overflow */
711 bfd_elf_generic_reloc, /* special_function */
712 "R_NDS32_LO12S2_RELA", /* name */
713 FALSE, /* partial_inplace */
714 0x000003ff, /* src_mask */
715 0x000003ff, /* dst_mask */
716 FALSE), /* pcrel_offset */
35c08157
KLC
717
718 /* Lower 12 bits of address. */
fbaf61ad
NC
719 HOWTO2 (R_NDS32_LO12S1_RELA, /* type */
720 1, /* rightshift */
721 2, /* size (0 = byte, 1 = short, 2 = long) */
722 11, /* bitsize */
723 FALSE, /* pc_relative */
724 0, /* bitpos */
725 complain_overflow_dont,/* complain_on_overflow */
726 bfd_elf_generic_reloc, /* special_function */
727 "R_NDS32_LO12S1_RELA", /* name */
728 FALSE, /* partial_inplace */
729 0x000007ff, /* src_mask */
730 0x000007ff, /* dst_mask */
731 FALSE), /* pcrel_offset */
35c08157
KLC
732
733 /* Lower 12 bits of address. */
fbaf61ad
NC
734 HOWTO2 (R_NDS32_LO12S0_RELA, /* type */
735 0, /* rightshift */
736 2, /* size (0 = byte, 1 = short, 2 = long) */
737 12, /* bitsize */
738 FALSE, /* pc_relative */
739 0, /* bitpos */
740 complain_overflow_dont,/* complain_on_overflow */
741 bfd_elf_generic_reloc, /* special_function */
742 "R_NDS32_LO12S0_RELA", /* name */
743 FALSE, /* partial_inplace */
744 0x00000fff, /* src_mask */
745 0x00000fff, /* dst_mask */
746 FALSE), /* pcrel_offset */
35c08157
KLC
747
748 /* Small data area 15 bits offset. */
fbaf61ad
NC
749 HOWTO2 (R_NDS32_SDA15S3_RELA, /* type */
750 3, /* rightshift */
751 2, /* size (0 = byte, 1 = short, 2 = long) */
752 15, /* bitsize */
753 FALSE, /* pc_relative */
754 0, /* bitpos */
755 complain_overflow_signed,/* complain_on_overflow */
756 bfd_elf_generic_reloc, /* special_function */
757 "R_NDS32_SDA15S3_RELA",/* name */
758 FALSE, /* partial_inplace */
759 0x00007fff, /* src_mask */
760 0x00007fff, /* dst_mask */
761 FALSE), /* pcrel_offset */
35c08157
KLC
762
763 /* Small data area 15 bits offset. */
fbaf61ad
NC
764 HOWTO2 (R_NDS32_SDA15S2_RELA, /* type */
765 2, /* rightshift */
766 2, /* size (0 = byte, 1 = short, 2 = long) */
767 15, /* bitsize */
768 FALSE, /* pc_relative */
769 0, /* bitpos */
770 complain_overflow_signed,/* complain_on_overflow */
771 bfd_elf_generic_reloc, /* special_function */
772 "R_NDS32_SDA15S2_RELA",/* name */
773 FALSE, /* partial_inplace */
774 0x00007fff, /* src_mask */
775 0x00007fff, /* dst_mask */
776 FALSE), /* pcrel_offset */
777
778 HOWTO2 (R_NDS32_SDA15S1_RELA, /* type */
779 1, /* rightshift */
780 2, /* size (0 = byte, 1 = short, 2 = long) */
781 15, /* bitsize */
782 FALSE, /* pc_relative */
783 0, /* bitpos */
784 complain_overflow_signed,/* complain_on_overflow */
785 bfd_elf_generic_reloc, /* special_function */
786 "R_NDS32_SDA15S1_RELA",/* name */
787 FALSE, /* partial_inplace */
788 0x00007fff, /* src_mask */
789 0x00007fff, /* dst_mask */
790 FALSE), /* pcrel_offset */
791
792 HOWTO2 (R_NDS32_SDA15S0_RELA, /* type */
793 0, /* rightshift */
794 2, /* size (0 = byte, 1 = short, 2 = long) */
795 15, /* bitsize */
796 FALSE, /* pc_relative */
797 0, /* bitpos */
798 complain_overflow_signed,/* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_NDS32_SDA15S0_RELA",/* name */
801 FALSE, /* partial_inplace */
802 0x00007fff, /* src_mask */
803 0x00007fff, /* dst_mask */
804 FALSE), /* pcrel_offset */
805
806 /* GNU extension to record C++ vtable hierarchy */
807 HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type */
808 0, /* rightshift */
809 2, /* size (0 = byte, 1 = short, 2 = long) */
810 0, /* bitsize */
811 FALSE, /* pc_relative */
812 0, /* bitpos */
813 complain_overflow_dont,/* complain_on_overflow */
814 NULL, /* special_function */
815 "R_NDS32_RELA_GNU_VTINHERIT",/* name */
816 FALSE, /* partial_inplace */
817 0, /* src_mask */
818 0, /* dst_mask */
819 FALSE), /* pcrel_offset */
820
821 /* GNU extension to record C++ vtable member usage */
822 HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type */
823 0, /* rightshift */
824 2, /* size (0 = byte, 1 = short, 2 = long) */
825 0, /* bitsize */
826 FALSE, /* pc_relative */
827 0, /* bitpos */
828 complain_overflow_dont,/* complain_on_overflow */
829 _bfd_elf_rel_vtable_reloc_fn,/* special_function */
830 "R_NDS32_RELA_GNU_VTENTRY",/* name */
831 FALSE, /* partial_inplace */
832 0, /* src_mask */
833 0, /* dst_mask */
834 FALSE), /* pcrel_offset */
35c08157
KLC
835
836 /* Like R_NDS32_20, but referring to the GOT table entry for
837 the symbol. */
fbaf61ad
NC
838 HOWTO2 (R_NDS32_GOT20, /* type */
839 0, /* rightshift */
840 2, /* size (0 = byte, 1 = short, 2 = long) */
841 20, /* bitsize */
842 FALSE, /* pc_relative */
843 0, /* bitpos */
844 complain_overflow_signed,/* complain_on_overflow */
845 bfd_elf_generic_reloc, /* special_function */
846 "R_NDS32_GOT20", /* name */
847 FALSE, /* partial_inplace */
848 0xfffff, /* src_mask */
849 0xfffff, /* dst_mask */
850 FALSE), /* pcrel_offset */
35c08157
KLC
851
852 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
853 entry for the symbol. */
fbaf61ad
NC
854 HOWTO2 (R_NDS32_25_PLTREL, /* type */
855 1, /* rightshift */
856 2, /* size (0 = byte, 1 = short, 2 = long) */
857 24, /* bitsize */
858 TRUE, /* pc_relative */
859 0, /* bitpos */
860 complain_overflow_signed,/* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_NDS32_25_PLTREL", /* name */
863 FALSE, /* partial_inplace */
864 0xffffff, /* src_mask */
865 0xffffff, /* dst_mask */
866 TRUE), /* pcrel_offset */
35c08157
KLC
867
868 /* This is used only by the dynamic linker. The symbol should exist
869 both in the object being run and in some shared library. The
870 dynamic linker copies the data addressed by the symbol from the
871 shared library into the object, because the object being
872 run has to have the data at some particular address. */
fbaf61ad
NC
873 HOWTO2 (R_NDS32_COPY, /* type */
874 0, /* rightshift */
875 2, /* size (0 = byte, 1 = short, 2 = long) */
876 32, /* bitsize */
877 FALSE, /* pc_relative */
878 0, /* bitpos */
879 complain_overflow_bitfield,/* complain_on_overflow */
880 bfd_elf_generic_reloc, /* special_function */
881 "R_NDS32_COPY", /* name */
882 FALSE, /* partial_inplace */
883 0xffffffff, /* src_mask */
884 0xffffffff, /* dst_mask */
885 FALSE), /* pcrel_offset */
35c08157
KLC
886
887 /* Like R_NDS32_20, but used when setting global offset table
888 entries. */
fbaf61ad
NC
889 HOWTO2 (R_NDS32_GLOB_DAT, /* type */
890 0, /* rightshift */
891 2, /* size (0 = byte, 1 = short, 2 = long) */
892 32, /* bitsize */
893 FALSE, /* pc_relative */
894 0, /* bitpos */
895 complain_overflow_bitfield,/* complain_on_overflow */
896 bfd_elf_generic_reloc, /* special_function */
897 "R_NDS32_GLOB_DAT", /* name */
898 FALSE, /* partial_inplace */
899 0xffffffff, /* src_mask */
900 0xffffffff, /* dst_mask */
901 FALSE), /* pcrel_offset */
35c08157
KLC
902
903 /* Marks a procedure linkage table entry for a symbol. */
fbaf61ad
NC
904 HOWTO2 (R_NDS32_JMP_SLOT, /* type */
905 0, /* rightshift */
906 2, /* size (0 = byte, 1 = short, 2 = long) */
907 32, /* bitsize */
908 FALSE, /* pc_relative */
909 0, /* bitpos */
910 complain_overflow_bitfield,/* complain_on_overflow */
911 bfd_elf_generic_reloc, /* special_function */
912 "R_NDS32_JMP_SLOT", /* name */
913 FALSE, /* partial_inplace */
914 0xffffffff, /* src_mask */
915 0xffffffff, /* dst_mask */
916 FALSE), /* pcrel_offset */
35c08157
KLC
917
918 /* Used only by the dynamic linker. When the object is run, this
919 longword is set to the load address of the object, plus the
920 addend. */
fbaf61ad
NC
921 HOWTO2 (R_NDS32_RELATIVE, /* type */
922 0, /* rightshift */
923 2, /* size (0 = byte, 1 = short, 2 = long) */
924 32, /* bitsize */
925 FALSE, /* pc_relative */
926 0, /* bitpos */
927 complain_overflow_bitfield,/* complain_on_overflow */
928 bfd_elf_generic_reloc, /* special_function */
929 "R_NDS32_RELATIVE", /* name */
930 FALSE, /* partial_inplace */
931 0xffffffff, /* src_mask */
932 0xffffffff, /* dst_mask */
933 FALSE), /* pcrel_offset */
934
935 HOWTO2 (R_NDS32_GOTOFF, /* type */
936 0, /* rightshift */
937 2, /* size (0 = byte, 1 = short, 2 = long) */
938 20, /* bitsize */
939 FALSE, /* pc_relative */
940 0, /* bitpos */
941 complain_overflow_signed,/* complain_on_overflow */
942 bfd_elf_generic_reloc, /* special_function */
943 "R_NDS32_GOTOFF", /* name */
944 FALSE, /* partial_inplace */
945 0xfffff, /* src_mask */
946 0xfffff, /* dst_mask */
947 FALSE), /* pcrel_offset */
35c08157
KLC
948
949 /* An PC Relative 20-bit relocation used when setting PIC offset
950 table register. */
fbaf61ad
NC
951 HOWTO2 (R_NDS32_GOTPC20, /* type */
952 0, /* rightshift */
953 2, /* size (0 = byte, 1 = short, 2 = long) */
954 20, /* bitsize */
955 TRUE, /* pc_relative */
956 0, /* bitpos */
957 complain_overflow_signed,/* complain_on_overflow */
958 bfd_elf_generic_reloc, /* special_function */
959 "R_NDS32_GOTPC20", /* name */
960 FALSE, /* partial_inplace */
961 0xfffff, /* src_mask */
962 0xfffff, /* dst_mask */
963 TRUE), /* pcrel_offset */
35c08157
KLC
964
965 /* Like R_NDS32_HI20, but referring to the GOT table entry for
966 the symbol. */
fbaf61ad
NC
967 HOWTO2 (R_NDS32_GOT_HI20, /* type */
968 12, /* rightshift */
969 2, /* size (0 = byte, 1 = short, 2 = long) */
970 20, /* bitsize */
971 FALSE, /* pc_relative */
972 0, /* bitpos */
973 complain_overflow_dont,/* complain_on_overflow */
974 bfd_elf_generic_reloc, /* special_function */
975 "R_NDS32_GOT_HI20", /* name */
976 FALSE, /* partial_inplace */
977 0x000fffff, /* src_mask */
978 0x000fffff, /* dst_mask */
979 FALSE), /* pcrel_offset */
980 HOWTO2 (R_NDS32_GOT_LO12, /* type */
981 0, /* rightshift */
982 2, /* size (0 = byte, 1 = short, 2 = long) */
983 12, /* bitsize */
984 FALSE, /* pc_relative */
985 0, /* bitpos */
986 complain_overflow_dont,/* complain_on_overflow */
987 bfd_elf_generic_reloc, /* special_function */
988 "R_NDS32_GOT_LO12", /* name */
989 FALSE, /* partial_inplace */
990 0x00000fff, /* src_mask */
991 0x00000fff, /* dst_mask */
992 FALSE), /* pcrel_offset */
35c08157
KLC
993
994 /* An PC Relative relocation used when setting PIC offset table register.
995 Like R_NDS32_HI20, but referring to the GOT table entry for
996 the symbol. */
fbaf61ad
NC
997 HOWTO2 (R_NDS32_GOTPC_HI20, /* type */
998 12, /* rightshift */
999 2, /* size (0 = byte, 1 = short, 2 = long) */
1000 20, /* bitsize */
1001 FALSE, /* pc_relative */
1002 0, /* bitpos */
1003 complain_overflow_dont,/* complain_on_overflow */
1004 bfd_elf_generic_reloc, /* special_function */
1005 "R_NDS32_GOTPC_HI20", /* name */
1006 FALSE, /* partial_inplace */
1007 0x000fffff, /* src_mask */
1008 0x000fffff, /* dst_mask */
1009 TRUE), /* pcrel_offset */
1010 HOWTO2 (R_NDS32_GOTPC_LO12, /* type */
1011 0, /* rightshift */
1012 2, /* size (0 = byte, 1 = short, 2 = long) */
1013 12, /* bitsize */
1014 FALSE, /* pc_relative */
1015 0, /* bitpos */
1016 complain_overflow_dont,/* complain_on_overflow */
1017 bfd_elf_generic_reloc, /* special_function */
1018 "R_NDS32_GOTPC_LO12", /* name */
1019 FALSE, /* partial_inplace */
1020 0x00000fff, /* src_mask */
1021 0x00000fff, /* dst_mask */
1022 TRUE), /* pcrel_offset */
1023
1024 HOWTO2 (R_NDS32_GOTOFF_HI20, /* type */
1025 12, /* rightshift */
1026 2, /* size (0 = byte, 1 = short, 2 = long) */
1027 20, /* bitsize */
1028 FALSE, /* pc_relative */
1029 0, /* bitpos */
1030 complain_overflow_dont,/* complain_on_overflow */
1031 bfd_elf_generic_reloc, /* special_function */
1032 "R_NDS32_GOTOFF_HI20", /* name */
1033 FALSE, /* partial_inplace */
1034 0x000fffff, /* src_mask */
1035 0x000fffff, /* dst_mask */
1036 FALSE), /* pcrel_offset */
1037 HOWTO2 (R_NDS32_GOTOFF_LO12, /* type */
1038 0, /* rightshift */
1039 2, /* size (0 = byte, 1 = short, 2 = long) */
1040 12, /* bitsize */
1041 FALSE, /* pc_relative */
1042 0, /* bitpos */
1043 complain_overflow_dont,/* complain_on_overflow */
1044 bfd_elf_generic_reloc, /* special_function */
1045 "R_NDS32_GOTOFF_LO12", /* name */
1046 FALSE, /* partial_inplace */
1047 0x00000fff, /* src_mask */
1048 0x00000fff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
35c08157
KLC
1050
1051 /* Alignment hint for relaxable instruction. This is used with
1052 R_NDS32_LABEL as a pair. Relax this instruction from 4 bytes to 2
1053 in order to make next label aligned on word boundary. */
fbaf61ad
NC
1054 HOWTO2 (R_NDS32_INSN16, /* type */
1055 0, /* rightshift */
1056 2, /* size (0 = byte, 1 = short, 2 = long) */
1057 32, /* bitsize */
1058 FALSE, /* pc_relative */
1059 0, /* bitpos */
1060 complain_overflow_dont,/* complain_on_overflow */
1061 nds32_elf_ignore_reloc,/* special_function */
1062 "R_NDS32_INSN16", /* name */
1063 FALSE, /* partial_inplace */
1064 0x00000fff, /* src_mask */
1065 0x00000fff, /* dst_mask */
1066 FALSE), /* pcrel_offset */
35c08157
KLC
1067
1068 /* Alignment hint for label. */
fbaf61ad
NC
1069 HOWTO2 (R_NDS32_LABEL, /* type */
1070 0, /* rightshift */
1071 2, /* size (0 = byte, 1 = short, 2 = long) */
1072 32, /* bitsize */
1073 FALSE, /* pc_relative */
1074 0, /* bitpos */
1075 complain_overflow_dont,/* complain_on_overflow */
1076 nds32_elf_ignore_reloc,/* special_function */
1077 "R_NDS32_LABEL", /* name */
1078 FALSE, /* partial_inplace */
1079 0xffffffff, /* src_mask */
1080 0xffffffff, /* dst_mask */
1081 FALSE), /* pcrel_offset */
35c08157
KLC
1082
1083 /* Relax hint for unconditional call sequence */
fbaf61ad
NC
1084 HOWTO2 (R_NDS32_LONGCALL1, /* type */
1085 0, /* rightshift */
1086 2, /* size (0 = byte, 1 = short, 2 = long) */
1087 32, /* bitsize */
1088 FALSE, /* pc_relative */
1089 0, /* bitpos */
1090 complain_overflow_dont,/* complain_on_overflow */
1091 nds32_elf_ignore_reloc,/* special_function */
1092 "R_NDS32_LONGCALL1", /* name */
1093 FALSE, /* partial_inplace */
1094 0xffffffff, /* src_mask */
1095 0xffffffff, /* dst_mask */
1096 FALSE), /* pcrel_offset */
35c08157
KLC
1097
1098 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1099 HOWTO2 (R_NDS32_LONGCALL2, /* type */
1100 0, /* rightshift */
1101 2, /* size (0 = byte, 1 = short, 2 = long) */
1102 32, /* bitsize */
1103 FALSE, /* pc_relative */
1104 0, /* bitpos */
1105 complain_overflow_dont,/* complain_on_overflow */
1106 nds32_elf_ignore_reloc,/* special_function */
1107 "R_NDS32_LONGCALL2", /* name */
1108 FALSE, /* partial_inplace */
1109 0xffffffff, /* src_mask */
1110 0xffffffff, /* dst_mask */
1111 FALSE), /* pcrel_offset */
35c08157
KLC
1112
1113 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1114 HOWTO2 (R_NDS32_LONGCALL3, /* type */
1115 0, /* rightshift */
1116 2, /* size (0 = byte, 1 = short, 2 = long) */
1117 32, /* bitsize */
1118 FALSE, /* pc_relative */
1119 0, /* bitpos */
1120 complain_overflow_dont,/* complain_on_overflow */
1121 nds32_elf_ignore_reloc,/* special_function */
1122 "R_NDS32_LONGCALL3", /* name */
1123 FALSE, /* partial_inplace */
1124 0xffffffff, /* src_mask */
1125 0xffffffff, /* dst_mask */
1126 FALSE), /* pcrel_offset */
35c08157
KLC
1127
1128 /* Relax hint for unconditional branch sequence. */
fbaf61ad
NC
1129 HOWTO2 (R_NDS32_LONGJUMP1, /* type */
1130 0, /* rightshift */
1131 2, /* size (0 = byte, 1 = short, 2 = long) */
1132 32, /* bitsize */
1133 FALSE, /* pc_relative */
1134 0, /* bitpos */
1135 complain_overflow_dont,/* complain_on_overflow */
1136 nds32_elf_ignore_reloc,/* special_function */
1137 "R_NDS32_LONGJUMP1", /* name */
1138 FALSE, /* partial_inplace */
1139 0xffffffff, /* src_mask */
1140 0xffffffff, /* dst_mask */
1141 FALSE), /* pcrel_offset */
35c08157
KLC
1142
1143 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1144 HOWTO2 (R_NDS32_LONGJUMP2, /* type */
1145 0, /* rightshift */
1146 2, /* size (0 = byte, 1 = short, 2 = long) */
1147 32, /* bitsize */
1148 FALSE, /* pc_relative */
1149 0, /* bitpos */
1150 complain_overflow_dont,/* complain_on_overflow */
1151 nds32_elf_ignore_reloc,/* special_function */
1152 "R_NDS32_LONGJUMP2", /* name */
1153 FALSE, /* partial_inplace */
1154 0xffffffff, /* src_mask */
1155 0xffffffff, /* dst_mask */
1156 FALSE), /* pcrel_offset */
35c08157
KLC
1157
1158 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1159 HOWTO2 (R_NDS32_LONGJUMP3, /* type */
1160 0, /* rightshift */
1161 2, /* size (0 = byte, 1 = short, 2 = long) */
1162 32, /* bitsize */
1163 FALSE, /* pc_relative */
1164 0, /* bitpos */
1165 complain_overflow_dont,/* complain_on_overflow */
1166 nds32_elf_ignore_reloc,/* special_function */
1167 "R_NDS32_LONGJUMP3", /* name */
1168 FALSE, /* partial_inplace */
1169 0xffffffff, /* src_mask */
1170 0xffffffff, /* dst_mask */
1171 FALSE), /* pcrel_offset */
35c08157
KLC
1172
1173 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1174 HOWTO2 (R_NDS32_LOADSTORE, /* type */
1175 0, /* rightshift */
1176 2, /* size (0 = byte, 1 = short, 2 = long) */
1177 32, /* bitsize */
1178 FALSE, /* pc_relative */
1179 0, /* bitpos */
1180 complain_overflow_dont,/* complain_on_overflow */
1181 nds32_elf_ignore_reloc,/* special_function */
1182 "R_NDS32_LOADSTORE", /* name */
1183 FALSE, /* partial_inplace */
1184 0xffffffff, /* src_mask */
1185 0xffffffff, /* dst_mask */
1186 FALSE), /* pcrel_offset */
35c08157
KLC
1187
1188 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1189 HOWTO2 (R_NDS32_9_FIXED_RELA, /* type */
1190 0, /* rightshift */
1191 1, /* size (0 = byte, 1 = short, 2 = long) */
1192 16, /* bitsize */
1193 FALSE, /* pc_relative */
1194 0, /* bitpos */
1195 complain_overflow_dont,/* complain_on_overflow */
1196 nds32_elf_ignore_reloc,/* special_function */
1197 "R_NDS32_9_FIXED_RELA",/* name */
1198 FALSE, /* partial_inplace */
1199 0x000000ff, /* src_mask */
1200 0x000000ff, /* dst_mask */
1201 FALSE), /* pcrel_offset */
35c08157
KLC
1202
1203 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1204 HOWTO2 (R_NDS32_15_FIXED_RELA,/* type */
1205 0, /* rightshift */
1206 2, /* size (0 = byte, 1 = short, 2 = long) */
1207 32, /* bitsize */
1208 FALSE, /* pc_relative */
1209 0, /* bitpos */
1210 complain_overflow_dont,/* complain_on_overflow */
1211 nds32_elf_ignore_reloc,/* special_function */
1212 "R_NDS32_15_FIXED_RELA",/* name */
1213 FALSE, /* partial_inplace */
1214 0x00003fff, /* src_mask */
1215 0x00003fff, /* dst_mask */
1216 FALSE), /* pcrel_offset */
35c08157
KLC
1217
1218 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1219 HOWTO2 (R_NDS32_17_FIXED_RELA,/* type */
1220 0, /* rightshift */
1221 2, /* size (0 = byte, 1 = short, 2 = long) */
1222 32, /* bitsize */
1223 FALSE, /* pc_relative */
1224 0, /* bitpos */
1225 complain_overflow_dont,/* complain_on_overflow */
1226 nds32_elf_ignore_reloc,/* special_function */
1227 "R_NDS32_17_FIXED_RELA",/* name */
1228 FALSE, /* partial_inplace */
1229 0x0000ffff, /* src_mask */
1230 0x0000ffff, /* dst_mask */
1231 FALSE), /* pcrel_offset */
35c08157
KLC
1232
1233 /* Relax hint for load/store sequence. */
fbaf61ad
NC
1234 HOWTO2 (R_NDS32_25_FIXED_RELA,/* type */
1235 0, /* rightshift */
1236 2, /* size (0 = byte, 1 = short, 2 = long) */
1237 32, /* bitsize */
1238 FALSE, /* pc_relative */
1239 0, /* bitpos */
1240 complain_overflow_dont,/* complain_on_overflow */
1241 nds32_elf_ignore_reloc,/* special_function */
1242 "R_NDS32_25_FIXED_RELA",/* name */
1243 FALSE, /* partial_inplace */
1244 0x00ffffff, /* src_mask */
1245 0x00ffffff, /* dst_mask */
1246 FALSE), /* pcrel_offset */
35c08157
KLC
1247
1248 /* High 20 bits of PLT symbol offset relative to PC. */
fbaf61ad
NC
1249 HOWTO2 (R_NDS32_PLTREL_HI20, /* type */
1250 12, /* rightshift */
1251 2, /* size (0 = byte, 1 = short, 2 = long) */
1252 20, /* bitsize */
1253 FALSE, /* pc_relative */
1254 0, /* bitpos */
1255 complain_overflow_dont,/* complain_on_overflow */
1256 bfd_elf_generic_reloc, /* special_function */
1257 "R_NDS32_PLTREL_HI20", /* name */
1258 FALSE, /* partial_inplace */
1259 0x000fffff, /* src_mask */
1260 0x000fffff, /* dst_mask */
1261 FALSE), /* pcrel_offset */
35c08157
KLC
1262
1263 /* Low 12 bits of PLT symbol offset relative to PC. */
fbaf61ad
NC
1264 HOWTO2 (R_NDS32_PLTREL_LO12, /* type */
1265 0, /* rightshift */
1266 2, /* size (0 = byte, 1 = short, 2 = long) */
1267 12, /* bitsize */
1268 FALSE, /* pc_relative */
1269 0, /* bitpos */
1270 complain_overflow_dont,/* complain_on_overflow */
1271 bfd_elf_generic_reloc, /* special_function */
1272 "R_NDS32_PLTREL_LO12", /* name */
1273 FALSE, /* partial_inplace */
1274 0x00000fff, /* src_mask */
1275 0x00000fff, /* dst_mask */
1276 FALSE), /* pcrel_offset */
35c08157
KLC
1277
1278 /* High 20 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1279 HOWTO2 (R_NDS32_PLT_GOTREL_HI20, /* type */
1280 12, /* rightshift */
1281 2, /* size (0 = byte, 1 = short, 2 = long) */
1282 20, /* bitsize */
1283 FALSE, /* pc_relative */
1284 0, /* bitpos */
1285 complain_overflow_dont,/* complain_on_overflow */
1286 bfd_elf_generic_reloc, /* special_function */
1287 "R_NDS32_PLT_GOTREL_HI20",/* name */
1288 FALSE, /* partial_inplace */
1289 0x000fffff, /* src_mask */
1290 0x000fffff, /* dst_mask */
1291 FALSE), /* pcrel_offset */
35c08157
KLC
1292
1293 /* Low 12 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1294 HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type */
1295 0, /* rightshift */
1296 2, /* size (0 = byte, 1 = short, 2 = long) */
1297 12, /* bitsize */
1298 FALSE, /* pc_relative */
1299 0, /* bitpos */
1300 complain_overflow_dont,/* complain_on_overflow */
1301 bfd_elf_generic_reloc, /* special_function */
1302 "R_NDS32_PLT_GOTREL_LO12",/* name */
1303 FALSE, /* partial_inplace */
1304 0x00000fff, /* src_mask */
1305 0x00000fff, /* dst_mask */
1306 FALSE), /* pcrel_offset */
35c08157
KLC
1307
1308 /* Small data area 12 bits offset. */
fbaf61ad
NC
1309 HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type */
1310 2, /* rightshift */
1311 2, /* size (0 = byte, 1 = short, 2 = long) */
1312 12, /* bitsize */
1313 FALSE, /* pc_relative */
1314 0, /* bitpos */
1315 complain_overflow_signed,/* complain_on_overflow */
1316 bfd_elf_generic_reloc, /* special_function */
1317 "R_NDS32_SDA12S2_DP_RELA",/* name */
1318 FALSE, /* partial_inplace */
1319 0x00000fff, /* src_mask */
1320 0x00000fff, /* dst_mask */
1321 FALSE), /* pcrel_offset */
35c08157
KLC
1322
1323 /* Small data area 12 bits offset. */
fbaf61ad
NC
1324 HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type */
1325 2, /* rightshift */
1326 2, /* size (0 = byte, 1 = short, 2 = long) */
1327 12, /* bitsize */
1328 FALSE, /* pc_relative */
1329 0, /* bitpos */
1330 complain_overflow_signed,/* complain_on_overflow */
1331 bfd_elf_generic_reloc, /* special_function */
1332 "R_NDS32_SDA12S2_SP_RELA",/* name */
1333 FALSE, /* partial_inplace */
1334 0x00000fff, /* src_mask */
1335 0x00000fff, /* dst_mask */
1336 FALSE), /* pcrel_offset */
35c08157
KLC
1337 /* Lower 12 bits of address. */
1338
fbaf61ad
NC
1339 HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type */
1340 2, /* rightshift */
1341 2, /* size (0 = byte, 1 = short, 2 = long) */
1342 10, /* bitsize */
1343 FALSE, /* pc_relative */
1344 0, /* bitpos */
1345 complain_overflow_dont,/* complain_on_overflow */
1346 bfd_elf_generic_reloc, /* special_function */
1347 "R_NDS32_LO12S2_DP_RELA",/* name */
1348 FALSE, /* partial_inplace */
1349 0x000003ff, /* src_mask */
1350 0x000003ff, /* dst_mask */
1351 FALSE), /* pcrel_offset */
35c08157
KLC
1352
1353 /* Lower 12 bits of address. */
fbaf61ad
NC
1354 HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type */
1355 2, /* rightshift */
1356 2, /* size (0 = byte, 1 = short, 2 = long) */
1357 10, /* bitsize */
1358 FALSE, /* pc_relative */
1359 0, /* bitpos */
1360 complain_overflow_dont,/* complain_on_overflow */
1361 bfd_elf_generic_reloc, /* special_function */
1362 "R_NDS32_LO12S2_SP_RELA",/* name */
1363 FALSE, /* partial_inplace */
1364 0x000003ff, /* src_mask */
1365 0x000003ff, /* dst_mask */
1366 FALSE), /* pcrel_offset */
35c08157 1367 /* Lower 12 bits of address. Special identity for or case. */
fbaf61ad
NC
1368 HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type */
1369 0, /* rightshift */
1370 2, /* size (0 = byte, 1 = short, 2 = long) */
1371 12, /* bitsize */
1372 FALSE, /* pc_relative */
1373 0, /* bitpos */
1374 complain_overflow_dont,/* complain_on_overflow */
1375 bfd_elf_generic_reloc, /* special_function */
1376 "R_NDS32_LO12S0_ORI_RELA",/* name */
1377 FALSE, /* partial_inplace */
1378 0x00000fff, /* src_mask */
1379 0x00000fff, /* dst_mask */
1380 FALSE), /* pcrel_offset */
35c08157 1381 /* Small data area 19 bits offset. */
fbaf61ad
NC
1382 HOWTO2 (R_NDS32_SDA16S3_RELA, /* type */
1383 3, /* rightshift */
1384 2, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_signed,/* complain_on_overflow */
1389 bfd_elf_generic_reloc, /* special_function */
1390 "R_NDS32_SDA16S3_RELA",/* name */
1391 FALSE, /* partial_inplace */
1392 0x0000ffff, /* src_mask */
1393 0x0000ffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
35c08157
KLC
1395
1396 /* Small data area 15 bits offset. */
fbaf61ad
NC
1397 HOWTO2 (R_NDS32_SDA17S2_RELA, /* type */
1398 2, /* rightshift */
1399 2, /* size (0 = byte, 1 = short, 2 = long) */
1400 17, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_signed,/* complain_on_overflow */
1404 bfd_elf_generic_reloc, /* special_function */
1405 "R_NDS32_SDA17S2_RELA",/* name */
1406 FALSE, /* partial_inplace */
1407 0x0001ffff, /* src_mask */
1408 0x0001ffff, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1410
1411 HOWTO2 (R_NDS32_SDA18S1_RELA, /* type */
1412 1, /* rightshift */
1413 2, /* size (0 = byte, 1 = short, 2 = long) */
1414 18, /* bitsize */
1415 FALSE, /* pc_relative */
1416 0, /* bitpos */
1417 complain_overflow_signed,/* complain_on_overflow */
1418 bfd_elf_generic_reloc, /* special_function */
1419 "R_NDS32_SDA18S1_RELA",/* name */
1420 FALSE, /* partial_inplace */
1421 0x0003ffff, /* src_mask */
1422 0x0003ffff, /* dst_mask */
1423 FALSE), /* pcrel_offset */
1424
1425 HOWTO2 (R_NDS32_SDA19S0_RELA, /* type */
1426 0, /* rightshift */
1427 2, /* size (0 = byte, 1 = short, 2 = long) */
1428 19, /* bitsize */
1429 FALSE, /* pc_relative */
1430 0, /* bitpos */
1431 complain_overflow_signed,/* complain_on_overflow */
1432 bfd_elf_generic_reloc, /* special_function */
1433 "R_NDS32_SDA19S0_RELA",/* name */
1434 FALSE, /* partial_inplace */
1435 0x0007ffff, /* src_mask */
1436 0x0007ffff, /* dst_mask */
1437 FALSE), /* pcrel_offset */
1438 HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type */
1439 0, /* rightshift */
1440 0, /* size (0 = byte, 1 = short, 2 = long) */
1441 8, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont,/* complain_on_overflow */
1445 nds32_elf_ignore_reloc,/* special_function */
1446 "R_NDS32_DWARF2_OP1_RELA",/* name */
1447 FALSE, /* partial_inplace */
1448 0xff, /* src_mask */
1449 0xff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451 HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type */
1452 0, /* rightshift */
1453 1, /* size (0 = byte, 1 = short, 2 = long) */
1454 16, /* bitsize */
1455 FALSE, /* pc_relative */
1456 0, /* bitpos */
1457 complain_overflow_dont,/* complain_on_overflow */
1458 nds32_elf_ignore_reloc,/* special_function */
1459 "R_NDS32_DWARF2_OP2_RELA",/* name */
1460 FALSE, /* partial_inplace */
1461 0xffff, /* src_mask */
1462 0xffff, /* dst_mask */
1463 FALSE), /* pcrel_offset */
1464 HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type */
1465 0, /* rightshift */
1466 2, /* size (0 = byte, 1 = short, 2 = long) */
1467 32, /* bitsize */
1468 FALSE, /* pc_relative */
1469 0, /* bitpos */
1470 complain_overflow_dont,/* complain_on_overflow */
1471 nds32_elf_ignore_reloc,/* special_function */
1472 "R_NDS32_DWARF2_LEB_RELA",/* name */
1473 FALSE, /* partial_inplace */
1474 0xffffffff, /* src_mask */
1475 0xffffffff, /* dst_mask */
1476 FALSE), /* pcrel_offset */
1477 HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type */
1478 0, /* rightshift */
1479 1, /* size (0 = byte, 1 = short, 2 = long) */
1480 16, /* bitsize */
1481 FALSE, /* pc_relative */
1482 0, /* bitpos */
1483 complain_overflow_dont,/* complain_on_overflow */
1484 nds32_elf_ignore_reloc,/* special_function */
1485 "R_NDS32_UPDATE_TA_RELA",/* name */
1486 FALSE, /* partial_inplace */
1487 0xffff, /* src_mask */
1488 0xffff, /* dst_mask */
1489 FALSE), /* pcrel_offset */
35c08157
KLC
1490 /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1491 entry for the symbol. */
fbaf61ad
NC
1492 HOWTO2 (R_NDS32_9_PLTREL, /* type */
1493 1, /* rightshift */
1494 1, /* size (0 = byte, 1 = short, 2 = long) */
1495 8, /* bitsize */
1496 TRUE, /* pc_relative */
1497 0, /* bitpos */
1498 complain_overflow_signed,/* complain_on_overflow */
1499 bfd_elf_generic_reloc, /* special_function */
1500 "R_NDS32_9_PLTREL", /* name */
1501 FALSE, /* partial_inplace */
1502 0xff, /* src_mask */
1503 0xff, /* dst_mask */
1504 TRUE), /* pcrel_offset */
35c08157 1505 /* Low 20 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1506 HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type */
1507 0, /* rightshift */
1508 2, /* size (0 = byte, 1 = short, 2 = long) */
1509 20, /* bitsize */
1510 FALSE, /* pc_relative */
1511 0, /* bitpos */
1512 complain_overflow_dont,/* complain_on_overflow */
1513 bfd_elf_generic_reloc, /* special_function */
1514 "R_NDS32_PLT_GOTREL_LO20",/* name */
1515 FALSE, /* partial_inplace */
1516 0x000fffff, /* src_mask */
1517 0x000fffff, /* dst_mask */
1518 FALSE), /* pcrel_offset */
1519 /* low 15 bits of PLT symbol offset relative to GOT (GP) */
1520 HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type */
1521 0, /* rightshift */
1522 2, /* size (0 = byte, 1 = short, 2 = long) */
1523 15, /* bitsize */
1524 FALSE, /* pc_relative */
1525 0, /* bitpos */
1526 complain_overflow_dont,/* complain_on_overflow */
1527 bfd_elf_generic_reloc, /* special_function */
1528 "R_NDS32_PLT_GOTREL_LO15",/* name */
1529 FALSE, /* partial_inplace */
1530 0x00007fff, /* src_mask */
1531 0x00007fff, /* dst_mask */
1532 FALSE), /* pcrel_offset */
35c08157 1533 /* Low 19 bits of PLT symbol offset relative to GOT (GP). */
fbaf61ad
NC
1534 HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type */
1535 0, /* rightshift */
1536 2, /* size (0 = byte, 1 = short, 2 = long) */
1537 19, /* bitsize */
1538 FALSE, /* pc_relative */
1539 0, /* bitpos */
1540 complain_overflow_dont,/* complain_on_overflow */
1541 bfd_elf_generic_reloc, /* special_function */
1542 "R_NDS32_PLT_GOTREL_LO19",/* name */
1543 FALSE, /* partial_inplace */
1544 0x0007ffff, /* src_mask */
1545 0x0007ffff, /* dst_mask */
1546 FALSE), /* pcrel_offset */
1547 HOWTO2 (R_NDS32_GOT_LO15, /* type */
1548 0, /* rightshift */
1549 2, /* size (0 = byte, 1 = short, 2 = long) */
1550 15, /* bitsize */
1551 FALSE, /* pc_relative */
1552 0, /* bitpos */
1553 complain_overflow_dont,/* complain_on_overflow */
1554 bfd_elf_generic_reloc, /* special_function */
1555 "R_NDS32_GOT_LO15", /* name */
1556 FALSE, /* partial_inplace */
1557 0x00007fff, /* src_mask */
1558 0x00007fff, /* dst_mask */
1559 FALSE), /* pcrel_offset */
1560 HOWTO2 (R_NDS32_GOT_LO19, /* type */
1561 0, /* rightshift */
1562 2, /* size (0 = byte, 1 = short, 2 = long) */
1563 19, /* bitsize */
1564 FALSE, /* pc_relative */
1565 0, /* bitpos */
1566 complain_overflow_dont,/* complain_on_overflow */
1567 bfd_elf_generic_reloc, /* special_function */
1568 "R_NDS32_GOT_LO19", /* name */
1569 FALSE, /* partial_inplace */
1570 0x0007ffff, /* src_mask */
1571 0x0007ffff, /* dst_mask */
1572 FALSE), /* pcrel_offset */
1573 HOWTO2 (R_NDS32_GOTOFF_LO15, /* type */
1574 0, /* rightshift */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1576 15, /* bitsize */
1577 FALSE, /* pc_relative */
1578 0, /* bitpos */
1579 complain_overflow_dont,/* complain_on_overflow */
1580 bfd_elf_generic_reloc, /* special_function */
1581 "R_NDS32_GOTOFF_LO15", /* name */
1582 FALSE, /* partial_inplace */
1583 0x00007fff, /* src_mask */
1584 0x00007fff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586 HOWTO2 (R_NDS32_GOTOFF_LO19, /* type */
1587 0, /* rightshift */
1588 2, /* size (0 = byte, 1 = short, 2 = long) */
1589 19, /* bitsize */
1590 FALSE, /* pc_relative */
1591 0, /* bitpos */
1592 complain_overflow_dont,/* complain_on_overflow */
1593 bfd_elf_generic_reloc, /* special_function */
1594 "R_NDS32_GOTOFF_LO19", /* name */
1595 FALSE, /* partial_inplace */
1596 0x0007ffff, /* src_mask */
1597 0x0007ffff, /* dst_mask */
1598 FALSE), /* pcrel_offset */
35c08157 1599 /* GOT 15 bits offset. */
fbaf61ad
NC
1600 HOWTO2 (R_NDS32_GOT15S2_RELA, /* type */
1601 2, /* rightshift */
1602 2, /* size (0 = byte, 1 = short, 2 = long) */
1603 15, /* bitsize */
1604 FALSE, /* pc_relative */
1605 0, /* bitpos */
1606 complain_overflow_signed,/* complain_on_overflow */
1607 bfd_elf_generic_reloc, /* special_function */
1608 "R_NDS32_GOT15S2_RELA",/* name */
1609 FALSE, /* partial_inplace */
1610 0x00007fff, /* src_mask */
1611 0x00007fff, /* dst_mask */
1612 FALSE), /* pcrel_offset */
35c08157 1613 /* GOT 17 bits offset. */
fbaf61ad
NC
1614 HOWTO2 (R_NDS32_GOT17S2_RELA, /* type */
1615 2, /* rightshift */
1616 2, /* size (0 = byte, 1 = short, 2 = long) */
1617 17, /* bitsize */
1618 FALSE, /* pc_relative */
1619 0, /* bitpos */
1620 complain_overflow_signed,/* complain_on_overflow */
1621 bfd_elf_generic_reloc, /* special_function */
1622 "R_NDS32_GOT17S2_RELA",/* name */
1623 FALSE, /* partial_inplace */
1624 0x0001ffff, /* src_mask */
1625 0x0001ffff, /* dst_mask */
1626 FALSE), /* pcrel_offset */
35c08157 1627 /* A 5 bit address. */
fbaf61ad
NC
1628 HOWTO2 (R_NDS32_5_RELA, /* type */
1629 0, /* rightshift */
1630 1, /* size (0 = byte, 1 = short, 2 = long) */
1631 5, /* bitsize */
1632 FALSE, /* pc_relative */
1633 0, /* bitpos */
1634 complain_overflow_signed,/* complain_on_overflow */
1635 bfd_elf_generic_reloc, /* special_function */
1636 "R_NDS32_5_RELA", /* name */
1637 FALSE, /* partial_inplace */
1638 0x1f, /* src_mask */
1639 0x1f, /* dst_mask */
1640 FALSE), /* pcrel_offset */
1641 HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type */
1642 1, /* rightshift */
1643 1, /* size (0 = byte, 1 = short, 2 = long) */
1644 9, /* bitsize */
1645 TRUE, /* pc_relative */
1646 0, /* bitpos */
1647 complain_overflow_unsigned,/* complain_on_overflow */
1648 bfd_elf_generic_reloc, /* special_function */
1649 "R_NDS32_10_UPCREL_RELA",/* name */
1650 FALSE, /* partial_inplace */
1651 0x1ff, /* src_mask */
1652 0x1ff, /* dst_mask */
1653 TRUE), /* pcrel_offset */
1654 HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type */
1655 2, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1657 7, /* bitsize */
1658 FALSE, /* pc_relative */
1659 0, /* bitpos */
1660 complain_overflow_unsigned,/* complain_on_overflow */
1661 bfd_elf_generic_reloc, /* special_function */
1662 "R_NDS32_SDA_FP7U2_RELA",/* name */
1663 FALSE, /* partial_inplace */
1664 0x0000007f, /* src_mask */
1665 0x0000007f, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1667 HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type */
1668 1, /* rightshift */
1669 2, /* size (0 = byte, 1 = short, 2 = long) */
1670 8, /* bitsize */
1671 TRUE, /* pc_relative */
1672 0, /* bitpos */
1673 complain_overflow_signed,/* complain_on_overflow */
1674 bfd_elf_generic_reloc, /* special_function */
1675 "R_NDS32_WORD_9_PCREL_RELA",/* name */
1676 FALSE, /* partial_inplace */
1677 0xff, /* src_mask */
1678 0xff, /* dst_mask */
1679 TRUE), /* pcrel_offset */
1680 HOWTO2 (R_NDS32_25_ABS_RELA, /* type */
1681 1, /* rightshift */
1682 2, /* size (0 = byte, 1 = short, 2 = long) */
1683 24, /* bitsize */
1684 FALSE, /* pc_relative */
1685 0, /* bitpos */
1686 complain_overflow_dont,/* complain_on_overflow */
1687 bfd_elf_generic_reloc, /* special_function */
1688 "R_NDS32_25_ABS_RELA", /* name */
1689 FALSE, /* partial_inplace */
1690 0xffffff, /* src_mask */
1691 0xffffff, /* dst_mask */
1692 FALSE), /* pcrel_offset */
35c08157
KLC
1693
1694 /* A relative 17 bit relocation for ifc, right shifted by 1. */
fbaf61ad
NC
1695 HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type */
1696 1, /* rightshift */
1697 2, /* size (0 = byte, 1 = short, 2 = long) */
1698 16, /* bitsize */
1699 TRUE, /* pc_relative */
1700 0, /* bitpos */
1701 complain_overflow_signed,/* complain_on_overflow */
1702 bfd_elf_generic_reloc, /* special_function */
1703 "R_NDS32_17IFC_PCREL_RELA",/* name */
1704 FALSE, /* partial_inplace */
1705 0xffff, /* src_mask */
1706 0xffff, /* dst_mask */
1707 TRUE), /* pcrel_offset */
35c08157
KLC
1708
1709 /* A relative unsigned 10 bit relocation for ifc, right shifted by 1. */
fbaf61ad
NC
1710 HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type */
1711 1, /* rightshift */
1712 1, /* size (0 = byte, 1 = short, 2 = long) */
1713 9, /* bitsize */
1714 TRUE, /* pc_relative */
1715 0, /* bitpos */
1716 complain_overflow_unsigned,/* complain_on_overflow */
1717 bfd_elf_generic_reloc, /* special_function */
1718 "R_NDS32_10IFCU_PCREL_RELA",/* name */
1719 FALSE, /* partial_inplace */
1720 0x1ff, /* src_mask */
1721 0x1ff, /* dst_mask */
1722 TRUE), /* pcrel_offset */
1723
1724 /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol. */
1725 HOWTO2 (R_NDS32_TLS_LE_HI20, /* type */
1726 12, /* rightshift */
1727 2, /* size (0 = byte, 1 = short, 2 = long) */
1728 20, /* bitsize */
1729 FALSE, /* pc_relative */
1730 0, /* bitpos */
1731 complain_overflow_dont,/* complain_on_overflow */
1732 bfd_elf_generic_reloc, /* special_function */
1733 "R_NDS32_TLS_LE_HI20", /* name */
1734 FALSE, /* partial_inplace */
1735 0x000fffff, /* src_mask */
1736 0x000fffff, /* dst_mask */
1737 FALSE), /* pcrel_offset */
1738
1739 HOWTO2 (R_NDS32_TLS_LE_LO12, /* type */
1740 0, /* rightshift */
1741 2, /* size (0 = byte, 1 = short, 2 = long) */
1742 12, /* bitsize */
1743 FALSE, /* pc_relative */
1744 0, /* bitpos */
1745 complain_overflow_dont,/* complain_on_overflow */
1746 bfd_elf_generic_reloc, /* special_function */
1747 "R_NDS32_TLS_LE_LO12", /* name */
1748 FALSE, /* partial_inplace */
1749 0x00000fff, /* src_mask */
1750 0x00000fff, /* dst_mask */
1751 FALSE), /* pcrel_offset */
1752
1753 /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol. */
1754 HOWTO2 (R_NDS32_TLS_IE_HI20, /* type */
1755 12, /* rightshift */
1756 2, /* size (0 = byte, 1 = short, 2 = long) */
1757 20, /* bitsize */
1758 FALSE, /* pc_relative */
1759 0, /* bitpos */
1760 complain_overflow_dont,/* complain_on_overflow */
1761 bfd_elf_generic_reloc, /* special_function */
1762 "R_NDS32_TLS_IE_HI20", /* name */
1763 FALSE, /* partial_inplace */
1764 0x000fffff, /* src_mask */
1765 0x000fffff, /* dst_mask */
1766 FALSE), /* pcrel_offset */
1767
1768 HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type */
1769 2, /* rightshift */
1770 2, /* size (0 = byte, 1 = short, 2 = long) */
1771 10, /* bitsize */
1772 FALSE, /* pc_relative */
1773 0, /* bitpos */
1774 complain_overflow_dont,/* complain_on_overflow */
1775 bfd_elf_generic_reloc, /* special_function */
1776 "R_NDS32_TLS_IE_LO12S2",/* name */
1777 FALSE, /* partial_inplace */
1778 0x000003ff, /* src_mask */
1779 0x000003ff, /* dst_mask */
1780 FALSE), /* pcrel_offset */
1781
1782 /* TLS LE TP offset relocation */
1783 HOWTO2 (R_NDS32_TLS_TPOFF, /* type */
1784 0, /* rightshift */
1785 2, /* size (0 = byte, 1 = short, 2 = long) */
1786 32, /* bitsize */
1787 FALSE, /* pc_relative */
1788 0, /* bitpos */
1789 complain_overflow_bitfield,/* complain_on_overflow */
1790 bfd_elf_generic_reloc, /* special_function */
1791 "R_NDS32_TLS_TPOFF", /* name */
1792 FALSE, /* partial_inplace */
1793 0xffffffff, /* src_mask */
1794 0xffffffff, /* dst_mask */
1795 FALSE), /* pcrel_offset */
1796
1c8f6a4d 1797 /* A 20 bit address. */
fbaf61ad
NC
1798 HOWTO2 (R_NDS32_TLS_LE_20, /* type */
1799 0, /* rightshift */
1800 2, /* size (0 = byte, 1 = short, 2 = long) */
1801 20, /* bitsize */
1802 FALSE, /* pc_relative */
1803 0, /* bitpos */
1804 complain_overflow_signed,/* complain_on_overflow */
1805 bfd_elf_generic_reloc, /* special_function */
1806 "R_NDS32_TLS_LE_20", /* name */
1807 FALSE, /* partial_inplace */
1808 0xfffff, /* src_mask */
1809 0xfffff, /* dst_mask */
1810 FALSE), /* pcrel_offset */
1811
1812 HOWTO2 (R_NDS32_TLS_LE_15S0, /* type */
1813 0, /* rightshift */
1814 2, /* size (0 = byte, 1 = short, 2 = long) */
1815 15, /* bitsize */
1816 FALSE, /* pc_relative */
1817 0, /* bitpos */
1818 complain_overflow_signed,/* complain_on_overflow */
1819 bfd_elf_generic_reloc, /* special_function */
1820 "R_NDS32_TLS_LE_15S0", /* name */
1821 FALSE, /* partial_inplace */
1822 0x7fff, /* src_mask */
1823 0x7fff, /* dst_mask */
1824 FALSE), /* pcrel_offset */
1825 HOWTO2 (R_NDS32_TLS_LE_15S1, /* type */
1826 1, /* rightshift */
1827 2, /* size (0 = byte, 1 = short, 2 = long) */
1828 15, /* bitsize */
1829 FALSE, /* pc_relative */
1830 0, /* bitpos */
1831 complain_overflow_signed,/* complain_on_overflow */
1832 bfd_elf_generic_reloc, /* special_function */
1833 "R_NDS32_TLS_LE_15S1", /* name */
1834 FALSE, /* partial_inplace */
1835 0x7fff, /* src_mask */
1836 0x7fff, /* dst_mask */
1837 FALSE), /* pcrel_offset */
1838 HOWTO2 (R_NDS32_TLS_LE_15S2, /* type */
1839 2, /* rightshift */
1840 2, /* size (0 = byte, 1 = short, 2 = long) */
1841 15, /* bitsize */
1842 FALSE, /* pc_relative */
1843 0, /* bitpos */
1844 complain_overflow_signed,/* complain_on_overflow */
1845 bfd_elf_generic_reloc, /* special_function */
1846 "R_NDS32_TLS_LE_15S2", /* name */
1847 FALSE, /* partial_inplace */
1848 0x7fff, /* src_mask */
1849 0x7fff, /* dst_mask */
1850 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1851
1852 /* Relax hint for unconditional call sequence */
fbaf61ad
NC
1853 HOWTO2 (R_NDS32_LONGCALL4, /* type */
1854 0, /* rightshift */
1855 2, /* size (0 = byte, 1 = short, 2 = long) */
1856 32, /* bitsize */
1857 FALSE, /* pc_relative */
1858 0, /* bitpos */
1859 complain_overflow_dont,/* complain_on_overflow */
1860 nds32_elf_ignore_reloc,/* special_function */
1861 "R_NDS32_LONGCALL4", /* name */
1862 FALSE, /* partial_inplace */
1863 0xffffffff, /* src_mask */
1864 0xffffffff, /* dst_mask */
1865 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1866
1867 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1868 HOWTO2 (R_NDS32_LONGCALL5, /* type */
1869 0, /* rightshift */
1870 2, /* size (0 = byte, 1 = short, 2 = long) */
1871 32, /* bitsize */
1872 FALSE, /* pc_relative */
1873 0, /* bitpos */
1874 complain_overflow_dont,/* complain_on_overflow */
1875 nds32_elf_ignore_reloc,/* special_function */
1876 "R_NDS32_LONGCALL5", /* name */
1877 FALSE, /* partial_inplace */
1878 0xffffffff, /* src_mask */
1879 0xffffffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1881
1882 /* Relax hint for conditional call sequence. */
fbaf61ad
NC
1883 HOWTO2 (R_NDS32_LONGCALL6, /* type */
1884 0, /* rightshift */
1885 2, /* size (0 = byte, 1 = short, 2 = long) */
1886 32, /* bitsize */
1887 FALSE, /* pc_relative */
1888 0, /* bitpos */
1889 complain_overflow_dont,/* complain_on_overflow */
1890 nds32_elf_ignore_reloc,/* special_function */
1891 "R_NDS32_LONGCALL6", /* name */
1892 FALSE, /* partial_inplace */
1893 0xffffffff, /* src_mask */
1894 0xffffffff, /* dst_mask */
1895 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1896
1897 /* Relax hint for unconditional branch sequence. */
fbaf61ad
NC
1898 HOWTO2 (R_NDS32_LONGJUMP4, /* type */
1899 0, /* rightshift */
1900 2, /* size (0 = byte, 1 = short, 2 = long) */
1901 32, /* bitsize */
1902 FALSE, /* pc_relative */
1903 0, /* bitpos */
1904 complain_overflow_dont,/* complain_on_overflow */
1905 nds32_elf_ignore_reloc,/* special_function */
1906 "R_NDS32_LONGJUMP4", /* name */
1907 FALSE, /* partial_inplace */
1908 0xffffffff, /* src_mask */
1909 0xffffffff, /* dst_mask */
1910 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1911
1912 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1913 HOWTO2 (R_NDS32_LONGJUMP5, /* type */
1914 0, /* rightshift */
1915 2, /* size (0 = byte, 1 = short, 2 = long) */
1916 32, /* bitsize */
1917 FALSE, /* pc_relative */
1918 0, /* bitpos */
1919 complain_overflow_dont,/* complain_on_overflow */
1920 nds32_elf_ignore_reloc,/* special_function */
1921 "R_NDS32_LONGJUMP5", /* name */
1922 FALSE, /* partial_inplace */
1923 0xffffffff, /* src_mask */
1924 0xffffffff, /* dst_mask */
1925 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1926
1927 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1928 HOWTO2 (R_NDS32_LONGJUMP6, /* type */
1929 0, /* rightshift */
1930 2, /* size (0 = byte, 1 = short, 2 = long) */
1931 32, /* bitsize */
1932 FALSE, /* pc_relative */
1933 0, /* bitpos */
1934 complain_overflow_dont,/* complain_on_overflow */
1935 nds32_elf_ignore_reloc,/* special_function */
1936 "R_NDS32_LONGJUMP6", /* name */
1937 FALSE, /* partial_inplace */
1938 0xffffffff, /* src_mask */
1939 0xffffffff, /* dst_mask */
1940 FALSE), /* pcrel_offset */
1c8f6a4d
KLC
1941
1942 /* Relax hint for conditional branch sequence. */
fbaf61ad
NC
1943 HOWTO2 (R_NDS32_LONGJUMP7, /* type */
1944 0, /* rightshift */
1945 2, /* size (0 = byte, 1 = short, 2 = long) */
1946 32, /* bitsize */
1947 FALSE, /* pc_relative */
1948 0, /* bitpos */
1949 complain_overflow_dont,/* complain_on_overflow */
1950 nds32_elf_ignore_reloc,/* special_function */
1951 "R_NDS32_LONGJUMP7", /* name */
1952 FALSE, /* partial_inplace */
1953 0xffffffff, /* src_mask */
1954 0xffffffff, /* dst_mask */
1955 FALSE), /* pcrel_offset */
1956
1957 HOWTO2 (R_NDS32_TLS_IE_LO12, /* type */
1958 0, /* rightshift */
1959 2, /* size (0 = byte, 1 = short, 2 = long) */
1960 12, /* bitsize */
1961 FALSE, /* pc_relative */
1962 0, /* bitpos */
1963 complain_overflow_dont,/* complain_on_overflow */
1964 bfd_elf_generic_reloc, /* special_function */
1965 "R_NDS32_TLS_IE_LO12", /* name */
1966 FALSE, /* partial_inplace */
1967 0x00000fff, /* src_mask */
1968 0x00000fff, /* dst_mask */
1969 FALSE), /* pcrel_offset */
1970
1971 /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1972 entry for the symbol. */
1973 HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type */
1974 12, /* rightshift */
1975 2, /* size (0 = byte, 1 = short, 2 = long) */
1976 20, /* bitsize */
1977 FALSE, /* pc_relative */
1978 0, /* bitpos */
1979 complain_overflow_dont,/* complain_on_overflow */
1980 bfd_elf_generic_reloc, /* special_function */
1981 "R_NDS32_TLS_IEGP_HI20",/* name */
1982 FALSE, /* partial_inplace */
1983 0x000fffff, /* src_mask */
1984 0x000fffff, /* dst_mask */
1985 FALSE), /* pcrel_offset */
1986
1987 HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type */
1988 0, /* rightshift */
1989 2, /* size (0 = byte, 1 = short, 2 = long) */
1990 12, /* bitsize */
1991 FALSE, /* pc_relative */
1992 0, /* bitpos */
1993 complain_overflow_dont,/* complain_on_overflow */
1994 bfd_elf_generic_reloc, /* special_function */
1995 "R_NDS32_TLS_IEGP_LO12",/* name */
1996 FALSE, /* partial_inplace */
1997 0x00000fff, /* src_mask */
1998 0x00000fff, /* dst_mask */
1999 FALSE), /* pcrel_offset */
2000
2001 HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type */
2002 2, /* rightshift */
2003 2, /* size (0 = byte, 1 = short, 2 = long) */
2004 10, /* bitsize */
2005 FALSE, /* pc_relative */
2006 0, /* bitpos */
2007 complain_overflow_dont,/* complain_on_overflow */
2008 bfd_elf_generic_reloc, /* special_function */
2009 "R_NDS32_TLS_IEGP_LO12S2",/* name */
2010 FALSE, /* partial_inplace */
2011 0x000003ff, /* src_mask */
2012 0x000003ff, /* dst_mask */
2013 FALSE), /* pcrel_offset */
2014
2015 /* TLS description relocation */
2016 HOWTO2 (R_NDS32_TLS_DESC, /* type */
2017 12, /* rightshift */
2018 2, /* size (0 = byte, 1 = short, 2 = long) */
2019 20, /* bitsize */
2020 FALSE, /* pc_relative */
2021 0, /* bitpos */
2022 complain_overflow_dont,/* complain_on_overflow */
2023 nds32_elf_hi20_reloc, /* special_function */
2024 "R_NDS32_TLS_DESC_HI20",/* name */
2025 FALSE, /* partial_inplace */
2026 0x000fffff, /* src_mask */
2027 0x000fffff, /* dst_mask */
2028 FALSE), /* pcrel_offset */
2029
2030 /* TLS GD/LD description offset high part. */
2031 HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type */
2032 12, /* rightshift */
2033 2, /* size (0 = byte, 1 = short, 2 = long) */
2034 20, /* bitsize */
2035 FALSE, /* pc_relative */
2036 0, /* bitpos */
2037 complain_overflow_dont,/* complain_on_overflow */
2038 nds32_elf_hi20_reloc, /* special_function */
2039 "R_NDS32_TLS_DESC_HI20",/* name */
2040 FALSE, /* partial_inplace */
2041 0x000fffff, /* src_mask */
2042 0x000fffff, /* dst_mask */
2043 FALSE), /* pcrel_offset */
2044
2045 /* TLS GD/LD description offset low part. */
2046 HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type */
2047 0, /* rightshift */
2048 2, /* size (0 = byte, 1 = short, 2 = long) */
2049 12, /* bitsize */
2050 FALSE, /* pc_relative */
2051 0, /* bitpos */
2052 complain_overflow_dont,/* complain_on_overflow */
2053 nds32_elf_lo12_reloc, /* special_function */
2054 "R_NDS32_TLS_DESC_LO12",/* name */
2055 FALSE, /* partial_inplace */
2056 0x00000fff, /* src_mask */
2057 0x00000fff, /* dst_mask */
2058 FALSE), /* pcrel_offset */
2059
2060 /* TLS GD/LD description offset set (movi). */
2061 HOWTO2 (R_NDS32_TLS_DESC_20, /* type */
2062 0, /* rightshift */
2063 2, /* size (0 = byte, 1 = short, 2 = long) */
2064 20, /* bitsize */
2065 FALSE, /* pc_relative */
2066 0, /* bitpos */
2067 complain_overflow_signed,/* complain_on_overflow */
2068 bfd_elf_generic_reloc, /* special_function */
2069 "R_NDS32_TLS_DESC_20", /* name */
2070 FALSE, /* partial_inplace */
2071 0x000fffff, /* src_mask */
2072 0x000fffff, /* dst_mask */
2073 FALSE), /* pcrel_offset */
2074
2075 /* TLS GD/LD description offset set (lwi.gp). */
2076 HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type */
2077 2, /* rightshift */
2078 2, /* size (0 = byte, 1 = short, 2 = long) */
2079 17, /* bitsize */
2080 FALSE, /* pc_relative */
2081 0, /* bitpos */
2082 complain_overflow_signed,/* complain_on_overflow */
2083 bfd_elf_generic_reloc, /* special_function */
2084 "R_NDS32_TLS_DESC_SDA17S2",/* name */
2085 FALSE, /* partial_inplace */
2086 0x0001ffff, /* src_mask */
2087 0x0001ffff, /* dst_mask */
2088 FALSE), /* pcrel_offset */
35c08157
KLC
2089};
2090
2091/* Relocations used for relaxation. */
fbaf61ad
NC
2092#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2093 [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2094
2095static reloc_howto_type nds32_elf_relax_howto_table[] = {
2096 HOWTO3 (R_NDS32_RELAX_ENTRY, /* type */
2097 0, /* rightshift */
2098 2, /* size (0 = byte, 1 = short, 2 = long) */
2099 32, /* bitsize */
2100 FALSE, /* pc_relative */
2101 0, /* bitpos */
2102 complain_overflow_dont,/* complain_on_overflow */
2103 nds32_elf_ignore_reloc,/* special_function */
2104 "R_NDS32_RELAX_ENTRY", /* name */
2105 FALSE, /* partial_inplace */
2106 0xffffffff, /* src_mask */
2107 0xffffffff, /* dst_mask */
2108 FALSE), /* pcrel_offset */
2109 HOWTO3 (R_NDS32_GOT_SUFF, /* type */
2110 0, /* rightshift */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2112 32, /* bitsize */
2113 FALSE, /* pc_relative */
2114 0, /* bitpos */
2115 complain_overflow_dont,/* complain_on_overflow */
2116 nds32_elf_ignore_reloc,/* special_function */
2117 "R_NDS32_GOT_SUFF", /* name */
2118 FALSE, /* partial_inplace */
2119 0xffffffff, /* src_mask */
2120 0xffffffff, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2122 HOWTO3 (R_NDS32_GOTOFF_SUFF, /* type */
2123 0, /* rightshift */
2124 2, /* size (0 = byte, 1 = short, 2 = long) */
2125 32, /* bitsize */
2126 FALSE, /* pc_relative */
2127 0, /* bitpos */
2128 complain_overflow_bitfield,/* complain_on_overflow */
2129 nds32_elf_ignore_reloc,/* special_function */
2130 "R_NDS32_GOTOFF_SUFF", /* name */
2131 FALSE, /* partial_inplace */
2132 0xffffffff, /* src_mask */
2133 0xffffffff, /* dst_mask */
2134 FALSE), /* pcrel_offset */
2135 HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type */
2136 0, /* rightshift */
2137 2, /* size (0 = byte, 1 = short, 2 = long) */
2138 32, /* bitsize */
2139 FALSE, /* pc_relative */
2140 0, /* bitpos */
2141 complain_overflow_dont,/* complain_on_overflow */
2142 nds32_elf_ignore_reloc,/* special_function */
2143 "R_NDS32_PLT_GOT_SUFF",/* name */
2144 FALSE, /* partial_inplace */
2145 0xffffffff, /* src_mask */
2146 0xffffffff, /* dst_mask */
2147 FALSE), /* pcrel_offset */
2148 HOWTO3 (R_NDS32_MULCALL_SUFF, /* type */
2149 0, /* rightshift */
2150 2, /* size (0 = byte, 1 = short, 2 = long) */
2151 32, /* bitsize */
2152 FALSE, /* pc_relative */
2153 0, /* bitpos */
2154 complain_overflow_dont,/* complain_on_overflow */
2155 nds32_elf_ignore_reloc,/* special_function */
2156 "R_NDS32_MULCALL_SUFF",/* name */
2157 FALSE, /* partial_inplace */
2158 0xffffffff, /* src_mask */
2159 0xffffffff, /* dst_mask */
2160 FALSE), /* pcrel_offset */
2161 HOWTO3 (R_NDS32_PTR, /* type */
2162 0, /* rightshift */
2163 2, /* size (0 = byte, 1 = short, 2 = long) */
2164 32, /* bitsize */
2165 FALSE, /* pc_relative */
2166 0, /* bitpos */
2167 complain_overflow_dont,/* complain_on_overflow */
2168 nds32_elf_ignore_reloc,/* special_function */
2169 "R_NDS32_PTR", /* name */
2170 FALSE, /* partial_inplace */
2171 0xffffffff, /* src_mask */
2172 0xffffffff, /* dst_mask */
2173 FALSE), /* pcrel_offset */
2174 HOWTO3 (R_NDS32_PTR_COUNT, /* type */
2175 0, /* rightshift */
2176 2, /* size (0 = byte, 1 = short, 2 = long) */
2177 32, /* bitsize */
2178 FALSE, /* pc_relative */
2179 0, /* bitpos */
2180 complain_overflow_dont,/* complain_on_overflow */
2181 nds32_elf_ignore_reloc,/* special_function */
2182 "R_NDS32_PTR_COUNT", /* name */
2183 FALSE, /* partial_inplace */
2184 0xffffffff, /* src_mask */
2185 0xffffffff, /* dst_mask */
2186 FALSE), /* pcrel_offset */
2187 HOWTO3 (R_NDS32_PTR_RESOLVED, /* type */
2188 0, /* rightshift */
2189 2, /* size (0 = byte, 1 = short, 2 = long) */
2190 32, /* bitsize */
2191 FALSE, /* pc_relative */
2192 0, /* bitpos */
2193 complain_overflow_dont,/* complain_on_overflow */
2194 nds32_elf_ignore_reloc,/* special_function */
2195 "R_NDS32_PTR_RESOLVED",/* name */
2196 FALSE, /* partial_inplace */
2197 0xffffffff, /* src_mask */
2198 0xffffffff, /* dst_mask */
2199 FALSE), /* pcrel_offset */
2200 HOWTO3 (R_NDS32_PLTBLOCK, /* type */
2201 0, /* rightshift */
2202 2, /* size (0 = byte, 1 = short, 2 = long) */
2203 32, /* bitsize */
2204 FALSE, /* pc_relative */
2205 0, /* bitpos */
2206 complain_overflow_dont,/* complain_on_overflow */
2207 nds32_elf_ignore_reloc,/* special_function */
2208 "R_NDS32_PLTBLOCK", /* name */
2209 FALSE, /* partial_inplace */
2210 0xffffffff, /* src_mask */
2211 0xffffffff, /* dst_mask */
2212 FALSE), /* pcrel_offset */
2213 HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type */
2214 0, /* rightshift */
2215 2, /* size (0 = byte, 1 = short, 2 = long) */
2216 32, /* bitsize */
2217 FALSE, /* pc_relative */
2218 0, /* bitpos */
2219 complain_overflow_dont,/* complain_on_overflow */
2220 nds32_elf_ignore_reloc,/* special_function */
2221 "R_NDS32_RELAX_REGION_BEGIN",/* name */
2222 FALSE, /* partial_inplace */
2223 0xffffffff, /* src_mask */
2224 0xffffffff, /* dst_mask */
2225 FALSE), /* pcrel_offset */
2226 HOWTO3 (R_NDS32_RELAX_REGION_END,/* type */
2227 0, /* rightshift */
2228 2, /* size (0 = byte, 1 = short, 2 = long) */
2229 32, /* bitsize */
2230 FALSE, /* pc_relative */
2231 0, /* bitpos */
2232 complain_overflow_dont,/* complain_on_overflow */
2233 nds32_elf_ignore_reloc,/* special_function */
2234 "R_NDS32_RELAX_REGION_END",/* name */
2235 FALSE, /* partial_inplace */
2236 0xffffffff, /* src_mask */
2237 0xffffffff, /* dst_mask */
2238 FALSE), /* pcrel_offset */
2239 HOWTO3 (R_NDS32_MINUEND, /* type */
2240 0, /* rightshift */
2241 2, /* size (0 = byte, 1 = short, 2 = long) */
2242 32, /* bitsize */
2243 FALSE, /* pc_relative */
2244 0, /* bitpos */
2245 complain_overflow_dont,/* complain_on_overflow */
2246 nds32_elf_ignore_reloc,/* special_function */
2247 "R_NDS32_MINUEND", /* name */
2248 FALSE, /* partial_inplace */
2249 0xffffffff, /* src_mask */
2250 0xffffffff, /* dst_mask */
2251 FALSE), /* pcrel_offset */
2252 HOWTO3 (R_NDS32_SUBTRAHEND, /* type */
2253 0, /* rightshift */
2254 2, /* size (0 = byte, 1 = short, 2 = long) */
2255 32, /* bitsize */
2256 FALSE, /* pc_relative */
2257 0, /* bitpos */
2258 complain_overflow_dont,/* complain_on_overflow */
2259 nds32_elf_ignore_reloc,/* special_function */
2260 "R_NDS32_SUBTRAHEND", /* name */
2261 FALSE, /* partial_inplace */
2262 0xffffffff, /* src_mask */
2263 0xffffffff, /* dst_mask */
2264 FALSE), /* pcrel_offset */
2265 HOWTO3 (R_NDS32_DIFF8, /* type */
2266 0, /* rightshift */
2267 0, /* size (0 = byte, 1 = short, 2 = long) */
2268 8, /* bitsize */
2269 FALSE, /* pc_relative */
2270 0, /* bitpos */
2271 complain_overflow_dont,/* complain_on_overflow */
2272 nds32_elf_ignore_reloc,/* special_function */
2273 "R_NDS32_DIFF8", /* name */
2274 FALSE, /* partial_inplace */
2275 0x000000ff, /* src_mask */
2276 0x000000ff, /* dst_mask */
2277 FALSE), /* pcrel_offset */
2278 HOWTO3 (R_NDS32_DIFF16, /* type */
2279 0, /* rightshift */
2280 1, /* size (0 = byte, 1 = short, 2 = long) */
2281 16, /* bitsize */
2282 FALSE, /* pc_relative */
2283 0, /* bitpos */
2284 complain_overflow_dont,/* complain_on_overflow */
2285 nds32_elf_ignore_reloc,/* special_function */
2286 "R_NDS32_DIFF16", /* name */
2287 FALSE, /* partial_inplace */
2288 0x0000ffff, /* src_mask */
2289 0x0000ffff, /* dst_mask */
2290 FALSE), /* pcrel_offset */
2291 HOWTO3 (R_NDS32_DIFF32, /* type */
2292 0, /* rightshift */
2293 2, /* size (0 = byte, 1 = short, 2 = long) */
2294 32, /* bitsize */
2295 FALSE, /* pc_relative */
2296 0, /* bitpos */
2297 complain_overflow_dont,/* complain_on_overflow */
2298 nds32_elf_ignore_reloc,/* special_function */
2299 "R_NDS32_DIFF32", /* name */
2300 FALSE, /* partial_inplace */
2301 0xffffffff, /* src_mask */
2302 0xffffffff, /* dst_mask */
2303 FALSE), /* pcrel_offset */
2304 HOWTO3 (R_NDS32_DIFF_ULEB128, /* type */
2305 0, /* rightshift */
2306 0, /* size (0 = byte, 1 = short, 2 = long) */
2307 0, /* bitsize */
2308 FALSE, /* pc_relative */
2309 0, /* bitpos */
2310 complain_overflow_dont,/* complain_on_overflow */
2311 nds32_elf_ignore_reloc,/* special_function */
2312 "R_NDS32_DIFF_ULEB128",/* name */
2313 FALSE, /* partial_inplace */
2314 0xffffffff, /* src_mask */
2315 0xffffffff, /* dst_mask */
2316 FALSE), /* pcrel_offset */
2317 HOWTO3 (R_NDS32_DATA, /* type */
2318 0, /* rightshift */
2319 2, /* size (0 = byte, 1 = short, 2 = long) */
2320 32, /* bitsize */
2321 FALSE, /* pc_relative */
2322 0, /* bitpos */
2323 complain_overflow_dont,/* complain_on_overflow */
2324 nds32_elf_ignore_reloc,/* special_function */
2325 "R_NDS32_DATA", /* name */
2326 FALSE, /* partial_inplace */
2327 0xffffffff, /* src_mask */
2328 0xffffffff, /* dst_mask */
2329 FALSE), /* pcrel_offset */
2330 HOWTO3 (R_NDS32_TRAN, /* type */
2331 0, /* rightshift */
2332 2, /* size (0 = byte, 1 = short, 2 = long) */
2333 32, /* bitsize */
2334 FALSE, /* pc_relative */
2335 0, /* bitpos */
2336 complain_overflow_dont,/* complain_on_overflow */
2337 nds32_elf_ignore_reloc,/* special_function */
2338 "R_NDS32_TRAN", /* name */
2339 FALSE, /* partial_inplace */
2340 0xffffffff, /* src_mask */
2341 0xffffffff, /* dst_mask */
2342 FALSE), /* pcrel_offset */
2343 HOWTO3 (R_NDS32_TLS_LE_ADD, /* type */
2344 0, /* rightshift */
2345 2, /* size (0 = byte, 1 = short, 2 = long) */
2346 32, /* bitsize */
2347 FALSE, /* pc_relative */
2348 0, /* bitpos */
2349 complain_overflow_dont,/* complain_on_overflow */
2350 nds32_elf_ignore_reloc,/* special_function */
2351 "R_NDS32_TLS_LE_ADD", /* name */
2352 FALSE, /* partial_inplace */
2353 0xffffffff, /* src_mask */
2354 0xffffffff, /* dst_mask */
2355 FALSE), /* pcrel_offset */
2356 HOWTO3 (R_NDS32_TLS_LE_LS, /* type */
2357 0, /* rightshift */
2358 2, /* size (0 = byte, 1 = short, 2 = long) */
2359 32, /* bitsize */
2360 FALSE, /* pc_relative */
2361 0, /* bitpos */
2362 complain_overflow_dont,/* complain_on_overflow */
2363 nds32_elf_ignore_reloc,/* special_function */
2364 "R_NDS32_TLS_LE_LS", /* name */
2365 FALSE, /* partial_inplace */
2366 0xffffffff, /* src_mask */
2367 0xffffffff, /* dst_mask */
2368 FALSE), /* pcrel_offset */
2369 HOWTO3 (R_NDS32_EMPTY, /* type */
2370 0, /* rightshift */
2371 2, /* size (0 = byte, 1 = short, 2 = long) */
2372 32, /* bitsize */
2373 FALSE, /* pc_relative */
2374 0, /* bitpos */
2375 complain_overflow_dont,/* complain_on_overflow */
2376 nds32_elf_ignore_reloc,/* special_function */
2377 "R_NDS32_EMPTY", /* name */
2378 FALSE, /* partial_inplace */
2379 0xffffffff, /* src_mask */
2380 0xffffffff, /* dst_mask */
2381 FALSE), /* pcrel_offset */
2382 /* TLS GD/LD description address base addition. */
2383 HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type */
2384 0, /* rightshift */
2385 2, /* size (0 = byte, 1 = short, 2 = long) */
2386 32, /* bitsize */
2387 FALSE, /* pc_relative */
2388 0, /* bitpos */
2389 complain_overflow_dont,/* complain_on_overflow */
2390 nds32_elf_ignore_reloc,/* special_function */
2391 "R_NDS32_TLS_DESC_ADD",/* name */
2392 FALSE, /* partial_inplace */
2393 0xffffffff, /* src_mask */
2394 0xffffffff, /* dst_mask */
2395 FALSE), /* pcrel_offset */
2396 /* TLS GD/LD description function load. */
2397 HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type */
2398 0, /* rightshift */
2399 2, /* size (0 = byte, 1 = short, 2 = long) */
2400 32, /* bitsize */
2401 FALSE, /* pc_relative */
2402 0, /* bitpos */
2403 complain_overflow_dont,/* complain_on_overflow */
2404 nds32_elf_ignore_reloc,/* special_function */
2405 "R_NDS32_TLS_DESC_FUNC",/* name */
2406 FALSE, /* partial_inplace */
2407 0xffffffff, /* src_mask */
2408 0xffffffff, /* dst_mask */
2409 FALSE), /* pcrel_offset */
2410 /* TLS DESC resolve function call. */
2411 HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type */
2412 0, /* rightshift */
2413 2, /* size (0 = byte, 1 = short, 2 = long) */
2414 32, /* bitsize */
2415 FALSE, /* pc_relative */
2416 0, /* bitpos */
2417 complain_overflow_dont,/* complain_on_overflow */
2418 nds32_elf_ignore_reloc,/* special_function */
2419 "R_NDS32_TLS_DESC_CALL",/* name */
2420 FALSE, /* partial_inplace */
2421 0xffffffff, /* src_mask */
2422 0xffffffff, /* dst_mask */
2423 FALSE), /* pcrel_offset */
2424 /* TLS DESC variable access. */
2425 HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type */
2426 0, /* rightshift */
2427 2, /* size (0 = byte, 1 = short, 2 = long) */
2428 32, /* bitsize */
2429 FALSE, /* pc_relative */
2430 0, /* bitpos */
2431 complain_overflow_dont,/* complain_on_overflow */
2432 nds32_elf_ignore_reloc,/* special_function */
2433 "R_NDS32_TLS_DESC_MEM",/* name */
2434 FALSE, /* partial_inplace */
2435 0xffffffff, /* src_mask */
2436 0xffffffff, /* dst_mask */
2437 FALSE), /* pcrel_offset */
2438 /* TLS GD/LD description mark (@tlsdec). */
2439 HOWTO3 (R_NDS32_RELAX_REMOVE, /* type */
2440 0, /* rightshift */
2441 2, /* size (0 = byte, 1 = short, 2 = long) */
2442 32, /* bitsize */
2443 FALSE, /* pc_relative */
2444 0, /* bitpos */
2445 complain_overflow_dont,/* complain_on_overflow */
2446 nds32_elf_ignore_reloc,/* special_function */
2447 "R_NDS32_REMOVE", /* name */
2448 FALSE, /* partial_inplace */
2449 0xffffffff, /* src_mask */
2450 0xffffffff, /* dst_mask */
2451 FALSE), /* pcrel_offset */
2452 /* TLS GD/LD description mark (@tlsdec). */
2453 HOWTO3 (R_NDS32_RELAX_GROUP, /* type */
2454 0, /* rightshift */
2455 2, /* size (0 = byte, 1 = short, 2 = long) */
2456 32, /* bitsize */
2457 FALSE, /* pc_relative */
2458 0, /* bitpos */
2459 complain_overflow_dont,/* complain_on_overflow */
2460 nds32_elf_ignore_reloc,/* special_function */
2461 "R_NDS32_GROUP", /* name */
2462 FALSE, /* partial_inplace */
2463 0xffffffff, /* src_mask */
2464 0xffffffff, /* dst_mask */
2465 FALSE), /* pcrel_offset */
2466 HOWTO3 (R_NDS32_TLS_IEGP_LW, /* type */
2467 0, /* rightshift */
2468 2, /* size (0 = byte, 1 = short, 2 = long) */
2469 32, /* bitsize */
2470 FALSE, /* pc_relative */
2471 0, /* bitpos */
2472 complain_overflow_dont,/* complain_on_overflow */
2473 nds32_elf_ignore_reloc,/* special_function */
2474 "R_NDS32_TLS_IEGP_LW", /* name */
2475 FALSE, /* partial_inplace */
2476 0xffffffff, /* src_mask */
2477 0xffffffff, /* dst_mask */
2478 FALSE), /* pcrel_offset */
2479 /* LA and FLSI relaxation. */
2480 HOWTO3 (R_NDS32_LSI, /* type */
2481 0, /* rightshift */
2482 2, /* size (0 = byte, 1 = short, 2 = long) */
2483 32, /* bitsize */
2484 FALSE, /* pc_relative */
2485 0, /* bitpos */
2486 complain_overflow_dont,/* complain_on_overflow */
2487 nds32_elf_ignore_reloc,/* special_function */
2488 "R_NDS32_LSI", /* name */
2489 FALSE, /* partial_inplace */
2490 0xffffffff, /* src_mask */
2491 0xffffffff, /* dst_mask */
2492 FALSE),
35c08157
KLC
2493};
2494
fbaf61ad
NC
2495static unsigned long dl_tlsdesc_lazy_trampoline[] =
2496{
2497 0x46200000, /* sethi $r2,#0x0 */
2498 0x58210000, /* ori $r2,$r2,#0x0 */
2499 0x40217400, /* add $r2,$r2,$gp */
2500 0x04210000, /* lwi $r2,[$r2+#0x0] */
2501 0x46300000, /* sethi $r3,#0x0 */
2502 0x58318000, /* ori $r3,$r3,#0x0 */
2503 0x4031f400, /* add $r3,$r3,$gp */
2504 0x4a000800, /* jr $r2 */
2505};
2506
2507static void
2508nds32_put_trampoline (void *contents, const unsigned long *template,
2509 unsigned count)
2510{
2511 unsigned ix;
2512
2513 for (ix = 0; ix != count; ix++)
2514 {
2515 unsigned long insn = template[ix];
2516 bfd_putb32 (insn, (char *) contents + ix * 4);
2517 }
2518}
2519
35c08157
KLC
2520/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2521 This prototype is the same as qsort (). */
2522
2523void
2524nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2525 int (*compar) (const void *lhs, const void *rhs))
2526{
2527 char *ptr = (char *) base;
1c8f6a4d 2528 int i, j;
9a733151
AM
2529 char tmp[sizeof (Elf_Internal_Rela)];
2530
2531 BFD_ASSERT (size <= sizeof (tmp));
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 }
2555}
2556
2557/* Sort relocation by r_offset.
2558
2559 We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2560 algorithm. Relocations at the same r_offset must keep their order.
2561 For example, RELAX_ENTRY must be the very first relocation entry.
2562
2563 Currently, this function implements insertion-sort.
2564
2565 FIXME: If we already sort them in assembler, why bother sort them
2566 here again? */
2567
2568static int
2569compar_reloc (const void *lhs, const void *rhs)
2570{
2571 const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2572 const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2573
2574 if (l->r_offset > r->r_offset)
2575 return 1;
2576 else if (l->r_offset == r->r_offset)
2577 return 0;
2578 else
2579 return -1;
2580}
2581
2582/* Functions listed below are only used for old relocs.
fbaf61ad
NC
2583 nds32_elf_9_pcrel_reloc
2584 nds32_elf_do_9_pcrel_reloc
2585 nds32_elf_hi20_reloc
2586 nds32_elf_relocate_hi20
2587 nds32_elf_lo12_reloc
2588 nds32_elf_sda15_reloc
2589 nds32_elf_generic_reloc. */
35c08157
KLC
2590
2591/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc. */
2592
2593static bfd_reloc_status_type
fbaf61ad
NC
2594nds32_elf_9_pcrel_reloc (bfd * abfd,
2595 arelent * reloc_entry,
2596 asymbol * symbol,
2597 void * data,
2598 asection * input_section,
2599 bfd * output_bfd,
2600 char ** error_message ATTRIBUTE_UNUSED)
35c08157
KLC
2601{
2602 /* This part is from bfd_elf_generic_reloc. */
2603 if (output_bfd != (bfd *) NULL
2604 && (symbol->flags & BSF_SECTION_SYM) == 0
2605 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2606 {
2607 reloc_entry->address += input_section->output_offset;
2608 return bfd_reloc_ok;
2609 }
2610
2611 if (output_bfd != NULL)
2612 {
2613 /* FIXME: See bfd_perform_relocation. Is this right? */
2614 return bfd_reloc_continue;
2615 }
2616
2617 return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2618 input_section,
2619 data, reloc_entry->address,
2620 symbol->section,
2621 (symbol->value
2622 + symbol->section->output_section->vma
2623 + symbol->section->output_offset),
2624 reloc_entry->addend);
2625}
2626
2627/* Utility to actually perform an R_NDS32_9_PCREL reloc. */
2628#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2629
2630static bfd_reloc_status_type
fbaf61ad
NC
2631nds32_elf_do_9_pcrel_reloc (bfd * abfd,
2632 reloc_howto_type * howto,
2633 asection * input_section,
2634 bfd_byte * data,
2635 bfd_vma offset,
2636 asection * symbol_section ATTRIBUTE_UNUSED,
2637 bfd_vma symbol_value,
2638 bfd_vma addend)
35c08157
KLC
2639{
2640 bfd_signed_vma relocation;
2641 unsigned short x;
2642 bfd_reloc_status_type status;
2643
2644 /* Sanity check the address (offset in section). */
2645 if (offset > bfd_get_section_limit (abfd, input_section))
2646 return bfd_reloc_outofrange;
2647
2648 relocation = symbol_value + addend;
2649 /* Make it pc relative. */
2650 relocation -= (input_section->output_section->vma
2651 + input_section->output_offset);
2652 /* These jumps mask off the lower two bits of the current address
2653 before doing pcrel calculations. */
2654 relocation -= (offset & -(bfd_vma) 2);
2655
1c8f6a4d 2656 if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
35c08157
KLC
2657 status = bfd_reloc_overflow;
2658 else
2659 status = bfd_reloc_ok;
2660
2661 x = bfd_getb16 (data + offset);
2662
2663 relocation >>= howto->rightshift;
2664 relocation <<= howto->bitpos;
2665 x = (x & ~howto->dst_mask)
2666 | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2667
2668 bfd_putb16 ((bfd_vma) x, data + offset);
2669
2670 return status;
2671}
2672
2673/* Handle the R_NDS32_HI20_[SU]LO relocs.
2674 HI20_SLO is for the add3 and load/store with displacement instructions.
2675 HI20 is for the or3 instruction.
2676 For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2677 the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2678 we must add one to the high 16 bytes (which will get subtracted off when
2679 the low 16 bits are added).
2680 These relocs have to be done in combination with an R_NDS32_LO12 reloc
2681 because there is a carry from the LO12 to the HI20. Here we just save
2682 the information we need; we do the actual relocation when we see the LO12.
2683 This code is copied from the elf32-mips.c. We also support an arbitrary
2684 number of HI20 relocs to be associated with a single LO12 reloc. The
2685 assembler sorts the relocs to ensure each HI20 immediately precedes its
2686 LO12. However if there are multiple copies, the assembler may not find
2687 the real LO12 so it picks the first one it finds. */
2688
2689struct nds32_hi20
2690{
2691 struct nds32_hi20 *next;
2692 bfd_byte *addr;
2693 bfd_vma addend;
2694};
2695
2696static struct nds32_hi20 *nds32_hi20_list;
2697
2698static bfd_reloc_status_type
fbaf61ad
NC
2699nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2700 arelent *reloc_entry,
2701 asymbol *symbol,
2702 void *data,
2703 asection *input_section,
2704 bfd *output_bfd,
2705 char **error_message ATTRIBUTE_UNUSED)
35c08157
KLC
2706{
2707 bfd_reloc_status_type ret;
2708 bfd_vma relocation;
2709 struct nds32_hi20 *n;
2710
2711 /* This part is from bfd_elf_generic_reloc.
2712 If we're relocating, and this an external symbol, we don't want
2713 to change anything. */
2714 if (output_bfd != (bfd *) NULL
2715 && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2716 {
2717 reloc_entry->address += input_section->output_offset;
2718 return bfd_reloc_ok;
2719 }
2720
2721 /* Sanity check the address (offset in section). */
2722 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2723 return bfd_reloc_outofrange;
2724
2725 ret = bfd_reloc_ok;
2726 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2727 ret = bfd_reloc_undefined;
2728
2729 if (bfd_is_com_section (symbol->section))
2730 relocation = 0;
2731 else
2732 relocation = symbol->value;
2733
2734 relocation += symbol->section->output_section->vma;
2735 relocation += symbol->section->output_offset;
2736 relocation += reloc_entry->addend;
2737
2738 /* Save the information, and let LO12 do the actual relocation. */
2739 n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2740 if (n == NULL)
2741 return bfd_reloc_outofrange;
2742
2743 n->addr = (bfd_byte *) data + reloc_entry->address;
2744 n->addend = relocation;
2745 n->next = nds32_hi20_list;
2746 nds32_hi20_list = n;
2747
2748 if (output_bfd != (bfd *) NULL)
2749 reloc_entry->address += input_section->output_offset;
2750
2751 return ret;
2752}
2753
2754/* Handle an NDS32 ELF HI20 reloc. */
2755
2756static void
2757nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
fbaf61ad
NC
2758 int type ATTRIBUTE_UNUSED,
2759 Elf_Internal_Rela *relhi,
2760 Elf_Internal_Rela *rello,
2761 bfd_byte *contents,
35c08157
KLC
2762 bfd_vma addend)
2763{
2764 unsigned long insn;
2765 bfd_vma addlo;
2766
2767 insn = bfd_getb32 (contents + relhi->r_offset);
2768
2769 addlo = bfd_getb32 (contents + rello->r_offset);
2770 addlo &= 0xfff;
2771
2772 addend += ((insn & 0xfffff) << 20) + addlo;
2773
2774 insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2775 bfd_putb32 (insn, contents + relhi->r_offset);
2776}
2777
2778/* Do an R_NDS32_LO12 relocation. This is a straightforward 12 bit
2779 inplace relocation; this function exists in order to do the
2780 R_NDS32_HI20_[SU]LO relocation described above. */
2781
2782static bfd_reloc_status_type
2783nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2784 void *data, asection *input_section, bfd *output_bfd,
2785 char **error_message)
2786{
2787 /* This part is from bfd_elf_generic_reloc.
2788 If we're relocating, and this an external symbol, we don't want
2789 to change anything. */
2790 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2791 && reloc_entry->addend == 0)
2792 {
2793 reloc_entry->address += input_section->output_offset;
2794 return bfd_reloc_ok;
2795 }
2796
2797 if (nds32_hi20_list != NULL)
2798 {
2799 struct nds32_hi20 *l;
2800
2801 l = nds32_hi20_list;
2802 while (l != NULL)
2803 {
2804 unsigned long insn;
2805 unsigned long val;
2806 unsigned long vallo;
2807 struct nds32_hi20 *next;
2808
2809 /* Do the HI20 relocation. Note that we actually don't need
2810 to know anything about the LO12 itself, except where to
2811 find the low 12 bits of the addend needed by the LO12. */
2812 insn = bfd_getb32 (l->addr);
2813 vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2814 vallo &= 0xfff;
2815 switch (reloc_entry->howto->type)
2816 {
2817 case R_NDS32_LO12S3:
2818 vallo <<= 3;
2819 break;
2820
2821 case R_NDS32_LO12S2:
2822 vallo <<= 2;
2823 break;
2824
2825 case R_NDS32_LO12S1:
2826 vallo <<= 1;
2827 break;
2828
2829 case R_NDS32_LO12S0:
2830 vallo <<= 0;
2831 break;
2832 }
2833
2834 val = ((insn & 0xfffff) << 12) + vallo;
2835 val += l->addend;
2836
2837 insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2838 bfd_putb32 ((bfd_vma) insn, l->addr);
2839
2840 next = l->next;
2841 free (l);
2842 l = next;
2843 }
2844
2845 nds32_hi20_list = NULL;
2846 }
2847
2848 /* Now do the LO12 reloc in the usual way.
2849 ??? It would be nice to call bfd_elf_generic_reloc here,
2850 but we have partial_inplace set. bfd_elf_generic_reloc will
2851 pass the handling back to bfd_install_relocation which will install
2852 a section relative addend which is wrong. */
2853 return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2854 input_section, output_bfd, error_message);
2855}
2856
2857/* Do generic partial_inplace relocation.
2858 This is a local replacement for bfd_elf_generic_reloc. */
2859
2860static bfd_reloc_status_type
2861nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2862 asymbol *symbol, void *data, asection *input_section,
2863 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2864{
2865 bfd_reloc_status_type ret;
2866 bfd_vma relocation;
2867 bfd_byte *inplace_address;
2868
2869 /* This part is from bfd_elf_generic_reloc.
2870 If we're relocating, and this an external symbol, we don't want
2871 to change anything. */
2872 if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2873 && reloc_entry->addend == 0)
2874 {
2875 reloc_entry->address += input_section->output_offset;
2876 return bfd_reloc_ok;
2877 }
2878
2879 /* Now do the reloc in the usual way.
2880 ??? It would be nice to call bfd_elf_generic_reloc here,
2881 but we have partial_inplace set. bfd_elf_generic_reloc will
2882 pass the handling back to bfd_install_relocation which will install
2883 a section relative addend which is wrong. */
2884
2885 /* Sanity check the address (offset in section). */
2886 if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2887 return bfd_reloc_outofrange;
2888
2889 ret = bfd_reloc_ok;
2890 if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2891 ret = bfd_reloc_undefined;
2892
2893 if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2894 relocation = 0;
2895 else
2896 relocation = symbol->value;
2897
2898 /* Only do this for a final link. */
2899 if (output_bfd == (bfd *) NULL)
2900 {
2901 relocation += symbol->section->output_section->vma;
2902 relocation += symbol->section->output_offset;
2903 }
2904
2905 relocation += reloc_entry->addend;
2906 switch (reloc_entry->howto->type)
2907 {
2908 case R_NDS32_LO12S3:
2909 relocation >>= 3;
2910 break;
2911
2912 case R_NDS32_LO12S2:
2913 relocation >>= 2;
2914 break;
2915
2916 case R_NDS32_LO12S1:
2917 relocation >>= 1;
2918 break;
2919
2920 case R_NDS32_LO12S0:
2921 default:
2922 relocation >>= 0;
2923 break;
2924 }
2925
2926 inplace_address = (bfd_byte *) data + reloc_entry->address;
2927
2928#define DOIT(x) \
2929 x = ((x & ~reloc_entry->howto->dst_mask) | \
2930 (((x & reloc_entry->howto->src_mask) + relocation) & \
2931 reloc_entry->howto->dst_mask))
2932
2933 switch (reloc_entry->howto->size)
2934 {
2935 case 1:
2936 {
2937 short x = bfd_getb16 (inplace_address);
2938
2939 DOIT (x);
2940 bfd_putb16 ((bfd_vma) x, inplace_address);
2941 }
2942 break;
2943 case 2:
2944 {
2945 unsigned long x = bfd_getb32 (inplace_address);
2946
2947 DOIT (x);
2948 bfd_putb32 ((bfd_vma) x, inplace_address);
2949 }
2950 break;
2951 default:
2952 BFD_ASSERT (0);
2953 }
2954
2955 if (output_bfd != (bfd *) NULL)
2956 reloc_entry->address += input_section->output_offset;
2957
2958 return ret;
2959}
2960
2961/* Handle the R_NDS32_SDA15 reloc.
2962 This reloc is used to compute the address of objects in the small data area
2963 and to perform loads and stores from that area.
2964 The lower 15 bits are sign extended and added to the register specified
2965 in the instruction, which is assumed to point to _SDA_BASE_.
2966
2967 Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
2968 the access size, this must be taken care of. */
2969
2970static bfd_reloc_status_type
2971nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
2972 asymbol *symbol, void *data ATTRIBUTE_UNUSED,
2973 asection *input_section, bfd *output_bfd,
2974 char **error_message ATTRIBUTE_UNUSED)
2975{
2976 /* This part is from bfd_elf_generic_reloc. */
2977 if (output_bfd != (bfd *) NULL
2978 && (symbol->flags & BSF_SECTION_SYM) == 0
2979 && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2980 {
2981 reloc_entry->address += input_section->output_offset;
2982 return bfd_reloc_ok;
2983 }
2984
2985 if (output_bfd != NULL)
2986 {
2987 /* FIXME: See bfd_perform_relocation. Is this right? */
2988 return bfd_reloc_continue;
2989 }
2990
2991 /* FIXME: not sure what to do here yet. But then again, the linker
2992 may never call us. */
2993 abort ();
2994}
2995
2996/* nds32_elf_ignore_reloc is the special function for
2997 relocation types which don't need to be relocated
2998 like relaxation relocation types.
2999 This function simply return bfd_reloc_ok when it is
3000 invoked. */
3001
3002static bfd_reloc_status_type
3003nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3004 asymbol *symbol ATTRIBUTE_UNUSED,
3005 void *data ATTRIBUTE_UNUSED, asection *input_section,
3006 bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3007{
3008 if (output_bfd != NULL)
3009 reloc_entry->address += input_section->output_offset;
3010
3011 return bfd_reloc_ok;
3012}
3013\f
3014
3015/* Map BFD reloc types to NDS32 ELF reloc types. */
3016
3017struct nds32_reloc_map_entry
3018{
3019 bfd_reloc_code_real_type bfd_reloc_val;
3020 unsigned char elf_reloc_val;
3021};
3022
3023static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3024{
3025 {BFD_RELOC_NONE, R_NDS32_NONE},
3026 {BFD_RELOC_16, R_NDS32_16_RELA},
3027 {BFD_RELOC_32, R_NDS32_32_RELA},
fbaf61ad
NC
3028 {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3029 {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3030
35c08157 3031 {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
35c08157
KLC
3032 {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3033 {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3034 {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3035 {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3036 {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
35c08157
KLC
3037 {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3038 {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3039 {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3040 {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3041 {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3042 {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3043 {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3044 {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3045 {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3046 {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
fbaf61ad
NC
3047 {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3048 {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3049 {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3050 {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
35c08157
KLC
3051 {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3052 {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3053 {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3054 {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3055 {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3056 {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3057 {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3058 {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
fbaf61ad
NC
3059 {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3060 {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
35c08157
KLC
3061 {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3062 {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3063 {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
35c08157
KLC
3064 {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3065 {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
35c08157
KLC
3066 {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3067 {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3068 {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3069 {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3070 {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3071 {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3072 {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3073 {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3074 {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3075 {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3076 {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3077 {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3078 {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
fbaf61ad
NC
3079 {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3080 {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3081 {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3082 {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3083 {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3084 {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3085 {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
35c08157
KLC
3086 {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3087 {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3088 {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3089 {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
35c08157
KLC
3090 {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3091 {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3092 {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3093 {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
35c08157
KLC
3094 {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3095 {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3096 {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3097 {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
fbaf61ad
NC
3098 {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3099 {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3100 {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3101 {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3102 {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3103 {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3104 {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
35c08157
KLC
3105 {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3106 {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
fbaf61ad
NC
3107 {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3108 {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3109 {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3110 {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3111 {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3112 {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3113 {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3114 {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
35c08157
KLC
3115 {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3116 {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
35c08157 3117 {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
35c08157
KLC
3118 {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3119 {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3120 {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3121 {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3122 {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
35c08157
KLC
3123 {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3124 {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3125 {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3126 {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
fbaf61ad 3127 {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
35c08157
KLC
3128 {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3129 {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3130 {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3131 {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3132 {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
fbaf61ad
NC
3133 /* Not sure. */
3134 {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3135 /* Missing: BFD_RELOC_NDS32_GOTTPOFF. */
1c8f6a4d
KLC
3136 {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3137 {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
1c8f6a4d
KLC
3138 {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3139 {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3140 {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3141 {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
fbaf61ad
NC
3142 {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3143 {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3144 {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3145 {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3146 {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3147 {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3148 {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3149 {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3150 {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3151 {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3152 {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3153 {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3154 {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3155 {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3156 {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3157 {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3158 {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3159 {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3160 {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3161 {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3162 {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
35c08157
KLC
3163};
3164
3165/* Patch tag. */
3166
3167static reloc_howto_type *
3168bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3169 const char *r_name)
3170{
3171 unsigned int i;
3172
3173 for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3174 if (nds32_elf_howto_table[i].name != NULL
3175 && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3176 return &nds32_elf_howto_table[i];
3177
3178 for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3179 if (nds32_elf_relax_howto_table[i].name != NULL
3180 && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3181 return &nds32_elf_relax_howto_table[i];
3182
3183 return NULL;
3184}
3185
3186static reloc_howto_type *
3187bfd_elf32_bfd_reloc_type_table_lookup (enum elf_nds32_reloc_type code)
3188{
3189 if (code < R_NDS32_RELAX_ENTRY)
3190 {
3191 BFD_ASSERT (code < ARRAY_SIZE (nds32_elf_howto_table));
3192 return &nds32_elf_howto_table[code];
3193 }
3194 else
3195 {
fbaf61ad
NC
3196 if ((size_t) (code - R_NDS32_RELAX_ENTRY)
3197 >= ARRAY_SIZE (nds32_elf_relax_howto_table))
3198 {
3199 int i = code;
3200 i += 1;
3201 }
3202
35c08157
KLC
3203 BFD_ASSERT ((size_t) (code - R_NDS32_RELAX_ENTRY)
3204 < ARRAY_SIZE (nds32_elf_relax_howto_table));
3205 return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3206 }
3207}
3208
3209static reloc_howto_type *
3210bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3211 bfd_reloc_code_real_type code)
3212{
3213 unsigned int i;
3214
3215 for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3216 {
3217 if (nds32_reloc_map[i].bfd_reloc_val == code)
3218 return bfd_elf32_bfd_reloc_type_table_lookup
fbaf61ad 3219 (nds32_reloc_map[i].elf_reloc_val);
35c08157
KLC
3220 }
3221
3222 return NULL;
3223}
3224
3225/* Set the howto pointer for an NDS32 ELF reloc. */
3226
f3185997
NC
3227static bfd_boolean
3228nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
35c08157
KLC
3229 Elf_Internal_Rela *dst)
3230{
3231 enum elf_nds32_reloc_type r_type;
3232
3233 r_type = ELF32_R_TYPE (dst->r_info);
5860e3f8
NC
3234 if (r_type > R_NDS32_GNU_VTENTRY)
3235 {
695344c0 3236 /* xgettext:c-format */
0aa13fee
AM
3237 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3238 abfd, r_type);
f3185997
NC
3239 bfd_set_error (bfd_error_bad_value);
3240 return FALSE;
5860e3f8 3241 }
fbaf61ad
NC
3242
3243 BFD_ASSERT (ELF32_R_TYPE (dst->r_info) <= R_NDS32_GNU_VTENTRY);
35c08157 3244 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
f3185997 3245 return TRUE;
35c08157
KLC
3246}
3247
f3185997 3248static bfd_boolean
35c08157
KLC
3249nds32_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
3250 Elf_Internal_Rela *dst)
3251{
f3185997
NC
3252 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3253
3254 if ((r_type == R_NDS32_NONE)
3255 || ((r_type > R_NDS32_GNU_VTENTRY)
3256 && (r_type < R_NDS32_max)))
3257 {
3258 cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3259 return TRUE;
3260 }
3261
3262 /* xgettext:c-format */
3263 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r_type);
3264 bfd_set_error (bfd_error_bad_value);
fbaf61ad 3265 return FALSE;
35c08157
KLC
3266}
3267
3268/* Support for core dump NOTE sections.
3269 Reference to include/linux/elfcore.h in Linux. */
3270
3271static bfd_boolean
3272nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3273{
3274 int offset;
3275 size_t size;
3276
3277 switch (note->descsz)
3278 {
3279 case 0x114:
fbaf61ad 3280 /* Linux/NDS32 32-bit, ABI1. */
35c08157
KLC
3281
3282 /* pr_cursig */
3283 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3284
3285 /* pr_pid */
3286 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3287
3288 /* pr_reg */
3289 offset = 72;
3290 size = 200;
3291 break;
3292
3293 case 0xfc:
fbaf61ad 3294 /* Linux/NDS32 32-bit. */
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 = 176;
3305 break;
3306
3307 default:
3308 return FALSE;
3309 }
3310
3311 /* Make a ".reg" section. */
3312 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3313 size, note->descpos + offset);
3314}
3315
3316static bfd_boolean
3317nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3318{
3319 switch (note->descsz)
3320 {
3321 case 124:
fbaf61ad 3322 /* Linux/NDS32. */
35c08157
KLC
3323
3324 /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform. */
3325 elf_tdata (abfd)->core->program =
3326 _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3327 elf_tdata (abfd)->core->command =
3328 _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
2b804145 3329 break;
35c08157
KLC
3330
3331 default:
3332 return FALSE;
3333 }
3334
3335 /* Note that for some reason, a spurious space is tacked
3336 onto the end of the args in some (at least one anyway)
3337 implementations, so strip it off if it exists. */
3338 {
3339 char *command = elf_tdata (abfd)->core->command;
3340 int n = strlen (command);
3341
3342 if (0 < n && command[n - 1] == ' ')
3343 command[n - 1] = '\0';
3344 }
3345
3346 return TRUE;
3347}
3348
3349/* Hook called by the linker routine which adds symbols from an object
3350 file. We must handle the special NDS32 section numbers here.
3351 We also keep watching for whether we need to create the sdata special
3352 linker sections. */
3353
3354static bfd_boolean
3355nds32_elf_add_symbol_hook (bfd *abfd,
3356 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3357 Elf_Internal_Sym *sym,
3358 const char **namep ATTRIBUTE_UNUSED,
3359 flagword *flagsp ATTRIBUTE_UNUSED,
3360 asection **secp, bfd_vma *valp)
3361{
3362 switch (sym->st_shndx)
3363 {
3364 case SHN_COMMON:
3365 /* Common symbols less than the GP size are automatically
3366 treated as SHN_MIPS_SCOMMON symbols. */
3367 if (sym->st_size > elf_gp_size (abfd)
3368 || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3369 break;
3370
3371 /* st_value is the alignemnt constraint.
3372 That might be its actual size if it is an array or structure. */
3373 switch (sym->st_value)
3374 {
3375 case 1:
3376 *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3377 break;
3378 case 2:
3379 *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3380 break;
3381 case 4:
3382 *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3383 break;
3384 case 8:
3385 *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3386 break;
3387 default:
3388 return TRUE;
3389 }
3390
3391 (*secp)->flags |= SEC_IS_COMMON;
3392 *valp = sym->st_size;
3393 break;
3394 }
3395
3396 return TRUE;
3397}
3398
35c08157
KLC
3399/* This function can figure out the best location for a base register to access
3400 data relative to this base register
3401 INPUT:
3402 sda_d0: size of first DOUBLE WORD data section
3403 sda_w0: size of first WORD data section
3404 sda_h0: size of first HALF WORD data section
3405 sda_b : size of BYTE data section
3406 sda_hi: size of second HALF WORD data section
3407 sda_w1: size of second WORD data section
3408 sda_d1: size of second DOUBLE WORD data section
3409 OUTPUT:
3410 offset (always positive) from the beginning of sda_d0 if OK
3411 a negative error value if fail
3412 NOTE:
3413 these 7 sections have to be located back to back if exist
3414 a pass in 0 value for non-existing section */
3415
3416/* Due to the interpretation of simm15 field of load/store depending on
3417 data accessing size, the organization of base register relative data shall
3418 like the following figure
3419 -------------------------------------------
3420 | DOUBLE WORD sized data (range +/- 128K)
3421 -------------------------------------------
3422 | WORD sized data (range +/- 64K)
3423 -------------------------------------------
3424 | HALF WORD sized data (range +/- 32K)
3425 -------------------------------------------
3426 | BYTE sized data (range +/- 16K)
3427 -------------------------------------------
3428 | HALF WORD sized data (range +/- 32K)
3429 -------------------------------------------
3430 | WORD sized data (range +/- 64K)
3431 -------------------------------------------
3432 | DOUBLE WORD sized data (range +/- 128K)
3433 -------------------------------------------
3434 Its base register shall be set to access these data freely. */
3435
3436/* We have to figure out the SDA_BASE value, so that we can adjust the
3437 symbol value correctly. We look up the symbol _SDA_BASE_ in the output
3438 BFD. If we can't find it, we're stuck. We cache it in the ELF
3439 target data. We don't need to adjust the symbol value for an
3440 external symbol if we are producing relocatable output. */
3441
3442static asection *sda_rela_sec = NULL;
3443
1c8f6a4d 3444#define SDA_SECTION_NUM 10
35c08157
KLC
3445
3446static bfd_reloc_status_type
fbaf61ad
NC
3447nds32_elf_final_sda_base (bfd * output_bfd,
3448 struct bfd_link_info * info,
3449 bfd_vma * psb,
3450 bfd_boolean add_symbol)
35c08157
KLC
3451{
3452 int relax_fp_as_gp;
3453 struct elf_nds32_link_hash_table *table;
3454 struct bfd_link_hash_entry *h, *h2;
1c8f6a4d 3455 long unsigned int total = 0;
fbaf61ad
NC
3456 asection *first = NULL, *final = NULL, *temp;
3457 bfd_vma sda_base = 0;
35c08157
KLC
3458
3459 h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", FALSE, FALSE, TRUE);
fbaf61ad
NC
3460 if (!h || (h->type != bfd_link_hash_defined
3461 && h->type != bfd_link_hash_defweak))
35c08157 3462 {
35c08157
KLC
3463 /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3464 4 byte-aligned. Therefore, it has to set the first section ".data"
3465 4 byte-aligned. */
3466 static const char sec_name[SDA_SECTION_NUM][10] =
3467 {
3468 ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
1c8f6a4d 3469 ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
35c08157
KLC
3470 };
3471 size_t i = 0;
3472
3473 if (output_bfd->sections == NULL)
3474 {
3475 *psb = elf_gp (output_bfd);
3476 return bfd_reloc_ok;
3477 }
3478
3479 /* Get the first and final section. */
fbaf61ad 3480 while (i < ARRAY_SIZE (sec_name))
35c08157
KLC
3481 {
3482 temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3483 if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3484 first = temp;
3485 if (temp && (temp->size != 0 || temp->rawsize != 0))
3486 final = temp;
1c8f6a4d
KLC
3487
3488 /* Summarize the sections in order to check if joining .bss. */
3489 if (temp && temp->size != 0)
3490 total += temp->size;
3491 else if (temp && temp->rawsize != 0)
3492 total += temp->rawsize;
3493
35c08157
KLC
3494 i++;
3495 }
3496
1c8f6a4d
KLC
3497 /* Check .bss size. */
3498 temp = bfd_get_section_by_name (output_bfd, ".bss");
3499 if (temp)
3500 {
3501 if (temp->size != 0)
3502 total += temp->size;
3503 else if (temp->rawsize != 0)
3504 total += temp->rawsize;
3505
3506 if (total < 0x80000)
3507 {
3508 if (!first && (temp->size != 0 || temp->rawsize != 0))
3509 first = temp;
3510 if ((temp->size != 0 || temp->rawsize != 0))
3511 final = temp;
3512 }
3513 }
3514
35c08157
KLC
3515 if (first && final)
3516 {
3517 /* The middle of data region. */
1c8f6a4d 3518 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
35c08157
KLC
3519
3520 /* Find the section sda_base located. */
3521 i = 0;
fbaf61ad 3522 while (i < ARRAY_SIZE (sec_name))
35c08157
KLC
3523 {
3524 final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3525 if (final && (final->size != 0 || final->rawsize != 0)
3526 && sda_base >= final->vma)
3527 {
3528 first = final;
3529 i++;
3530 }
3531 else
3532 break;
3533 }
3534 }
3535 else
3536 {
fbaf61ad
NC
3537 /* If there is not any default data section in output bfd, try to find
3538 the first data section. If no data section be found, just simplily
3539 choose the first output section. */
3540 temp = output_bfd->sections;
3541 while (temp)
3542 {
3543 if (temp->flags & SEC_ALLOC
3544 && (((temp->flags & SEC_DATA)
3545 && ((temp->flags & SEC_READONLY) == 0))
3546 || (temp->flags & SEC_LOAD) == 0)
3547 && (temp->size != 0 || temp->rawsize != 0))
3548 {
3549 if (!first)
3550 first = temp;
3551 final = temp;
3552 }
3553 temp = temp->next;
3554 }
3555
3556 /* There is no data or bss section. */
3557 if (!first || (first->size == 0 && first->rawsize == 0))
3558 {
3559 first = output_bfd->sections;
3560 while (first && first->size == 0 && first->rawsize == 0)
3561 first = first->next;
3562 }
3563
3564 /* There is no concrete section. */
35c08157
KLC
3565 if (!first)
3566 {
3567 *psb = elf_gp (output_bfd);
3568 return bfd_reloc_ok;
3569 }
fbaf61ad
NC
3570
3571 if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3572 sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3573 else
3574 sda_base = first->vma + 0x2000;
35c08157
KLC
3575 }
3576
3577 sda_base -= first->vma;
3578 sda_base = sda_base & (~7);
3579
3580 if (!_bfd_generic_link_add_one_symbol
fbaf61ad
NC
3581 (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3582 (bfd_vma) sda_base, (const char *) NULL, FALSE,
3583 get_elf_backend_data (output_bfd)->collect, &h))
35c08157
KLC
3584 return FALSE;
3585
3586 sda_rela_sec = first;
fbaf61ad 3587 }
35c08157 3588
fbaf61ad
NC
3589 /* Set _FP_BASE_ to _SDA_BASE_. */
3590 table = nds32_elf_hash_table (info);
3591 relax_fp_as_gp = table->relax_fp_as_gp;
3592 h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, FALSE, FALSE, FALSE);
3593 /* _SDA_BASE_ is difined in linker script. */
3594 if (!first)
3595 {
3596 first = h->u.def.section;
3597 sda_base = h->u.def.value;
3598 }
3599
3600 if (relax_fp_as_gp && h2
3601 && (h2->type == bfd_link_hash_undefweak
3602 || h2->type == bfd_link_hash_undefined))
3603 {
3604 /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3605 And set FP equal to SDA_BASE to do relaxation for
3606 la $fp, _FP_BASE_. */
3607 if (!_bfd_generic_link_add_one_symbol
3608 (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3609 first, sda_base, (const char *) NULL,
3610 FALSE, get_elf_backend_data (output_bfd)->collect, &h2))
3611 return FALSE;
35c08157
KLC
3612 }
3613
535b785f 3614 if (add_symbol)
35c08157
KLC
3615 {
3616 if (h)
3617 {
3618 /* Now set gp. */
3619 elf_gp (output_bfd) = (h->u.def.value
3620 + h->u.def.section->output_section->vma
3621 + h->u.def.section->output_offset);
3622 }
3623 else
3624 {
38f14ab8 3625 _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
35c08157
KLC
3626 return bfd_reloc_dangerous;
3627 }
3628 }
3629
fbaf61ad
NC
3630 *psb = h->u.def.value
3631 + h->u.def.section->output_section->vma
3632 + h->u.def.section->output_offset;
35c08157
KLC
3633 return bfd_reloc_ok;
3634}
3635\f
3636
3637/* Return size of a PLT entry. */
3638#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3639
35c08157
KLC
3640/* Create an entry in an nds32 ELF linker hash table. */
3641
3642static struct bfd_hash_entry *
3643nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3644 struct bfd_hash_table *table,
3645 const char *string)
3646{
3647 struct elf_nds32_link_hash_entry *ret;
3648
3649 ret = (struct elf_nds32_link_hash_entry *) entry;
3650
3651 /* Allocate the structure if it has not already been allocated by a
3652 subclass. */
3653 if (ret == NULL)
3654 ret = (struct elf_nds32_link_hash_entry *)
3655 bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3656
3657 if (ret == NULL)
3658 return (struct bfd_hash_entry *) ret;
3659
3660 /* Call the allocation method of the superclass. */
3661 ret = (struct elf_nds32_link_hash_entry *)
3662 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3663
3664 if (ret != NULL)
3665 {
3666 struct elf_nds32_link_hash_entry *eh;
3667
3668 eh = (struct elf_nds32_link_hash_entry *) ret;
1c8f6a4d 3669 eh->tls_type = GOT_UNKNOWN;
fbaf61ad 3670 eh->offset_to_gp = 0;
35c08157
KLC
3671 }
3672
3673 return (struct bfd_hash_entry *) ret;
3674}
3675
3676/* Create an nds32 ELF linker hash table. */
3677
3678static struct bfd_link_hash_table *
3679nds32_elf_link_hash_table_create (bfd *abfd)
3680{
3681 struct elf_nds32_link_hash_table *ret;
3682
986f0783 3683 size_t amt = sizeof (struct elf_nds32_link_hash_table);
35c08157
KLC
3684
3685 ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3686 if (ret == NULL)
3687 return NULL;
3688
fbaf61ad 3689 /* Patch tag. */
35c08157
KLC
3690 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3691 nds32_elf_link_hash_newfunc,
3692 sizeof (struct elf_nds32_link_hash_entry),
3693 NDS32_ELF_DATA))
3694 {
3695 free (ret);
3696 return NULL;
3697 }
3698
fbaf61ad
NC
3699 ret->sdynbss = NULL;
3700 ret->srelbss = NULL;
3701 ret->sym_ld_script = NULL;
3702
35c08157
KLC
3703 return &ret->root.root;
3704}
3705
fbaf61ad
NC
3706/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3707 shortcuts to them in our hash table. */
3708
3709static bfd_boolean
3710create_got_section (bfd *dynobj, struct bfd_link_info *info)
3711{
3712 struct elf_link_hash_table *ehtab;
3713
3714 if (!_bfd_elf_create_got_section (dynobj, info))
3715 return FALSE;
3716
3717 ehtab = elf_hash_table (info);
3718 ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3719 ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3720 if (!ehtab->sgot || !ehtab->sgotplt)
3721 abort ();
3722
3723 /* _bfd_elf_create_got_section will create it for us. */
3724 ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3725 if (ehtab->srelgot == NULL
fd361982 3726 || !bfd_set_section_flags (ehtab->srelgot,
fbaf61ad
NC
3727 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3728 | SEC_IN_MEMORY | SEC_LINKER_CREATED
3729 | SEC_READONLY))
fd361982 3730 || !bfd_set_section_alignment (ehtab->srelgot, 2))
fbaf61ad
NC
3731 return FALSE;
3732
3733 return TRUE;
3734}
3735
35c08157
KLC
3736/* Create dynamic sections when linking against a dynamic object. */
3737
3738static bfd_boolean
3739nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3740{
fbaf61ad 3741 struct elf_link_hash_table *ehtab;
35c08157
KLC
3742 struct elf_nds32_link_hash_table *htab;
3743 flagword flags, pltflags;
3744 register asection *s;
3745 const struct elf_backend_data *bed;
3746 int ptralign = 2; /* 32-bit */
fbaf61ad
NC
3747 const char *secname;
3748 char *relname;
3749 flagword secflags;
3750 asection *sec;
35c08157
KLC
3751
3752 bed = get_elf_backend_data (abfd);
fbaf61ad 3753 ehtab = elf_hash_table (info);
35c08157
KLC
3754 htab = nds32_elf_hash_table (info);
3755
3756 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3757 .rel[a].bss sections. */
3758
3759 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3760 | SEC_LINKER_CREATED);
3761
3762 pltflags = flags;
3763 pltflags |= SEC_CODE;
3764 if (bed->plt_not_loaded)
3765 pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3766 if (bed->plt_readonly)
3767 pltflags |= SEC_READONLY;
3768
3769 s = bfd_make_section (abfd, ".plt");
fbaf61ad 3770 ehtab->splt = s;
35c08157 3771 if (s == NULL
fd361982
AM
3772 || !bfd_set_section_flags (s, pltflags)
3773 || !bfd_set_section_alignment (s, bed->plt_alignment))
35c08157
KLC
3774 return FALSE;
3775
3776 if (bed->want_plt_sym)
3777 {
3778 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3779 .plt section. */
3780 struct bfd_link_hash_entry *bh = NULL;
3781 struct elf_link_hash_entry *h;
3782
3783 if (!(_bfd_generic_link_add_one_symbol
3784 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3785 (bfd_vma) 0, (const char *) NULL, FALSE,
3786 get_elf_backend_data (abfd)->collect, &bh)))
3787 return FALSE;
3788
3789 h = (struct elf_link_hash_entry *) bh;
3790 h->def_regular = 1;
3791 h->type = STT_OBJECT;
3792
0e1862bb 3793 if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
35c08157
KLC
3794 return FALSE;
3795 }
3796
3797 s = bfd_make_section (abfd,
3798 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
fbaf61ad 3799 ehtab->srelplt = s;
35c08157 3800 if (s == NULL
fd361982
AM
3801 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3802 || !bfd_set_section_alignment (s, ptralign))
35c08157
KLC
3803 return FALSE;
3804
fbaf61ad 3805 if (ehtab->sgot == NULL && !create_got_section (abfd, info))
35c08157
KLC
3806 return FALSE;
3807
fbaf61ad
NC
3808 for (sec = abfd->sections; sec; sec = sec->next)
3809 {
fd361982 3810 secflags = bfd_section_flags (sec);
fbaf61ad
NC
3811 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3812 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3813 continue;
fd361982 3814 secname = bfd_section_name (sec);
fbaf61ad
NC
3815 relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3816 strcpy (relname, ".rela");
3817 strcat (relname, secname);
3818 if (bfd_get_section_by_name (abfd, secname))
3819 continue;
3820 s = bfd_make_section (abfd, relname);
3821 if (s == NULL
fd361982
AM
3822 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3823 || !bfd_set_section_alignment (s, ptralign))
fbaf61ad
NC
3824 return FALSE;
3825 }
35c08157
KLC
3826
3827 if (bed->want_dynbss)
3828 {
3829 /* The .dynbss section is a place to put symbols which are defined
3830 by dynamic objects, are referenced by regular objects, and are
3831 not functions. We must allocate space for them in the process
3832 image and use a R_*_COPY reloc to tell the dynamic linker to
3833 initialize them at run time. The linker script puts the .dynbss
3834 section into the .bss section of the final image. */
3835 s = bfd_make_section (abfd, ".dynbss");
3836 htab->sdynbss = s;
3837 if (s == NULL
fd361982 3838 || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
35c08157
KLC
3839 return FALSE;
3840 /* The .rel[a].bss section holds copy relocs. This section is not
3841 normally needed. We need to create it here, though, so that the
3842 linker will map it to an output section. We can't just create it
3843 only if we need it, because we will not know whether we need it
3844 until we have seen all the input files, and the first time the
3845 main linker code calls BFD after examining all the input files
3846 (size_dynamic_sections) the input sections have already been
3847 mapped to the output sections. If the section turns out not to
3848 be needed, we can discard it later. We will never need this
3849 section when generating a shared object, since they do not use
3850 copy relocs. */
0e1862bb 3851 if (!bfd_link_pic (info))
35c08157
KLC
3852 {
3853 s = bfd_make_section (abfd, (bed->default_use_rela_p
3854 ? ".rela.bss" : ".rel.bss"));
3855 htab->srelbss = s;
3856 if (s == NULL
fd361982
AM
3857 || !bfd_set_section_flags (s, flags | SEC_READONLY)
3858 || !bfd_set_section_alignment (s, ptralign))
35c08157
KLC
3859 return FALSE;
3860 }
3861 }
3862
3863 return TRUE;
3864}
3865
3866/* Copy the extra info we tack onto an elf_link_hash_entry. */
3867static void
3868nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3869 struct elf_link_hash_entry *dir,
3870 struct elf_link_hash_entry *ind)
3871{
3872 struct elf_nds32_link_hash_entry *edir, *eind;
3873
3874 edir = (struct elf_nds32_link_hash_entry *) dir;
3875 eind = (struct elf_nds32_link_hash_entry *) ind;
3876
190eb1dd 3877 if (ind->dyn_relocs != NULL)
35c08157 3878 {
190eb1dd 3879 if (dir->dyn_relocs != NULL)
35c08157 3880 {
3bf083ed
AM
3881 struct elf_dyn_relocs **pp;
3882 struct elf_dyn_relocs *p;
35c08157
KLC
3883
3884 if (ind->root.type == bfd_link_hash_indirect)
3885 abort ();
3886
3887 /* Add reloc counts against the weak sym to the strong sym
3888 list. Merge any entries against the same section. */
190eb1dd 3889 for (pp = &ind->dyn_relocs; (p = *pp) != NULL;)
35c08157 3890 {
3bf083ed 3891 struct elf_dyn_relocs *q;
35c08157 3892
190eb1dd 3893 for (q = dir->dyn_relocs; q != NULL; q = q->next)
35c08157
KLC
3894 if (q->sec == p->sec)
3895 {
3896 q->pc_count += p->pc_count;
3897 q->count += p->count;
3898 *pp = p->next;
3899 break;
3900 }
3901 if (q == NULL)
3902 pp = &p->next;
3903 }
190eb1dd 3904 *pp = dir->dyn_relocs;
35c08157
KLC
3905 }
3906
190eb1dd
L
3907 dir->dyn_relocs = ind->dyn_relocs;
3908 ind->dyn_relocs = NULL;
35c08157
KLC
3909 }
3910
fbaf61ad
NC
3911 if (ind->root.type == bfd_link_hash_indirect)
3912 {
3913 if (dir->got.refcount <= 0)
3914 {
3915 edir->tls_type = eind->tls_type;
3916 eind->tls_type = GOT_UNKNOWN;
3917 }
3918 }
3919
35c08157
KLC
3920 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3921}
3922\f
fbaf61ad 3923
63c1f59d
AM
3924/* Find dynamic relocs for H that apply to read-only sections. */
3925
3926static asection *
3927readonly_dynrelocs (struct elf_link_hash_entry *h)
3928{
3bf083ed 3929 struct elf_dyn_relocs *p;
63c1f59d 3930
190eb1dd 3931 for (p = h->dyn_relocs; p != NULL; p = p->next)
63c1f59d
AM
3932 {
3933 asection *s = p->sec->output_section;
3934
3935 if (s != NULL && (s->flags & SEC_READONLY) != 0)
3936 return p->sec;
3937 }
3938 return NULL;
3939}
35c08157
KLC
3940
3941/* Adjust a symbol defined by a dynamic object and referenced by a
3942 regular object. The current definition is in some section of the
3943 dynamic object, but we're not including those sections. We have to
3944 change the definition to something the rest of the link can
3945 understand. */
3946
3947static bfd_boolean
3948nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3949 struct elf_link_hash_entry *h)
3950{
3951 struct elf_nds32_link_hash_table *htab;
35c08157
KLC
3952 bfd *dynobj;
3953 asection *s;
3954 unsigned int power_of_two;
3955
3956 dynobj = elf_hash_table (info)->dynobj;
3957
3958 /* Make sure we know what is going on here. */
3959 BFD_ASSERT (dynobj != NULL
3960 && (h->needs_plt
60d67dc8 3961 || h->is_weakalias
35c08157
KLC
3962 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3963
3964
3965 /* If this is a function, put it in the procedure linkage table. We
3966 will fill in the contents of the procedure linkage table later,
3967 when we know the address of the .got section. */
3968 if (h->type == STT_FUNC || h->needs_plt)
3969 {
0e1862bb 3970 if (!bfd_link_pic (info)
35c08157
KLC
3971 && !h->def_dynamic
3972 && !h->ref_dynamic
3973 && h->root.type != bfd_link_hash_undefweak
3974 && h->root.type != bfd_link_hash_undefined)
3975 {
3976 /* This case can occur if we saw a PLT reloc in an input
3977 file, but the symbol was never referred to by a dynamic
3978 object. In such a case, we don't actually need to build
3979 a procedure linkage table, and we can just do a PCREL
3980 reloc instead. */
3981 h->plt.offset = (bfd_vma) - 1;
3982 h->needs_plt = 0;
3983 }
3984
3985 return TRUE;
3986 }
3987 else
3988 h->plt.offset = (bfd_vma) - 1;
3989
3990 /* If this is a weak symbol, and there is a real definition, the
3991 processor independent code will have arranged for us to see the
3992 real definition first, and we can just use the same value. */
60d67dc8 3993 if (h->is_weakalias)
35c08157 3994 {
60d67dc8
AM
3995 struct elf_link_hash_entry *def = weakdef (h);
3996 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3997 h->root.u.def.section = def->root.u.def.section;
3998 h->root.u.def.value = def->root.u.def.value;
35c08157
KLC
3999 return TRUE;
4000 }
4001
4002 /* This is a reference to a symbol defined by a dynamic object which
4003 is not a function. */
4004
4005 /* If we are creating a shared library, we must presume that the
4006 only references to the symbol are via the global offset table.
4007 For such cases we need not do anything here; the relocations will
4008 be handled correctly by relocate_section. */
0e1862bb 4009 if (bfd_link_pic (info))
35c08157
KLC
4010 return TRUE;
4011
4012 /* If there are no references to this symbol that do not use the
4013 GOT, we don't need to generate a copy reloc. */
4014 if (!h->non_got_ref)
4015 return TRUE;
4016
4017 /* If -z nocopyreloc was given, we won't generate them either. */
3bf083ed 4018 if (0 && info->nocopyreloc)
35c08157
KLC
4019 {
4020 h->non_got_ref = 0;
4021 return TRUE;
4022 }
4023
3bf083ed
AM
4024 /* If we don't find any dynamic relocs in read-only sections, then
4025 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
fbaf61ad 4026 if (!readonly_dynrelocs (h))
35c08157
KLC
4027 {
4028 h->non_got_ref = 0;
4029 return TRUE;
4030 }
4031
4032 /* We must allocate the symbol in our .dynbss section, which will
4033 become part of the .bss section of the executable. There will be
4034 an entry for this symbol in the .dynsym section. The dynamic
4035 object will contain position independent code, so all references
4036 from the dynamic object to this symbol will go through the global
4037 offset table. The dynamic linker will use the .dynsym entry to
4038 determine the address it must put in the global offset table, so
4039 both the dynamic object and the regular object will refer to the
4040 same memory location for the variable. */
4041
4042 htab = nds32_elf_hash_table (info);
4043 s = htab->sdynbss;
4044 BFD_ASSERT (s != NULL);
4045
4046 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4047 to copy the initial value out of the dynamic object and into the
4048 runtime process image. We need to remember the offset into the
4049 .rela.bss section we are going to use. */
4050 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4051 {
4052 asection *srel;
4053
4054 srel = htab->srelbss;
4055 BFD_ASSERT (srel != NULL);
4056 srel->size += sizeof (Elf32_External_Rela);
4057 h->needs_copy = 1;
4058 }
4059
4060 /* We need to figure out the alignment required for this symbol. I
4061 have no idea how ELF linkers handle this. */
4062 power_of_two = bfd_log2 (h->size);
4063 if (power_of_two > 3)
4064 power_of_two = 3;
4065
4066 /* Apply the required alignment. */
4067 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
fd361982 4068 if (power_of_two > bfd_section_alignment (s))
35c08157 4069 {
fd361982 4070 if (!bfd_set_section_alignment (s, power_of_two))
35c08157
KLC
4071 return FALSE;
4072 }
4073
4074 /* Define the symbol as being at this point in the section. */
4075 h->root.u.def.section = s;
4076 h->root.u.def.value = s->size;
4077
4078 /* Increment the section size to make room for the symbol. */
4079 s->size += h->size;
4080
4081 return TRUE;
4082}
4083
4084/* Allocate space in .plt, .got and associated reloc sections for
4085 dynamic relocs. */
4086
4087static bfd_boolean
4088allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4089{
4090 struct bfd_link_info *info;
fbaf61ad 4091 struct elf_link_hash_table *ehtab;
35c08157 4092 struct elf_nds32_link_hash_table *htab;
3bf083ed 4093 struct elf_dyn_relocs *p;
35c08157
KLC
4094
4095 if (h->root.type == bfd_link_hash_indirect)
4096 return TRUE;
4097
fbaf61ad
NC
4098 /* When warning symbols are created, they **replace** the "real"
4099 entry in the hash table, thus we never get to see the real
4100 symbol in a hash traversal. So look at it now. */
35c08157 4101 if (h->root.type == bfd_link_hash_warning)
35c08157
KLC
4102 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4103
4104 info = (struct bfd_link_info *) inf;
fbaf61ad 4105 ehtab = elf_hash_table (info);
35c08157 4106 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4107 if (htab == NULL)
4108 return FALSE;
35c08157 4109
fbaf61ad
NC
4110 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4111 && h->plt.refcount > 0
4112 && !(bfd_link_pie (info) && h->def_regular))
35c08157
KLC
4113 {
4114 /* Make sure this symbol is output as a dynamic symbol.
4115 Undefined weak syms won't yet be marked as dynamic. */
4116 if (h->dynindx == -1 && !h->forced_local)
4117 {
4118 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4119 return FALSE;
4120 }
4121
0e1862bb 4122 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
35c08157 4123 {
fbaf61ad 4124 asection *s = ehtab->splt;
35c08157
KLC
4125
4126 /* If this is the first .plt entry, make room for the special
4127 first entry. */
4128 if (s->size == 0)
4129 s->size += PLT_ENTRY_SIZE;
4130
4131 h->plt.offset = s->size;
4132
4133 /* If this symbol is not defined in a regular file, and we are
4134 not generating a shared library, then set the symbol to this
4135 location in the .plt. This is required to make function
4136 pointers compare as equal between the normal executable and
4137 the shared library. */
0e1862bb 4138 if (!bfd_link_pic (info) && !h->def_regular)
35c08157
KLC
4139 {
4140 h->root.u.def.section = s;
4141 h->root.u.def.value = h->plt.offset;
4142 }
4143
4144 /* Make room for this entry. */
4145 s->size += PLT_ENTRY_SIZE;
4146
4147 /* We also need to make an entry in the .got.plt section, which
4148 will be placed in the .got section by the linker script. */
fbaf61ad 4149 ehtab->sgotplt->size += 4;
35c08157
KLC
4150
4151 /* We also need to make an entry in the .rel.plt section. */
fbaf61ad
NC
4152 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4153 if (htab->tls_desc_trampoline)
4154 htab->next_tls_desc_index++;
35c08157
KLC
4155 }
4156 else
4157 {
4158 h->plt.offset = (bfd_vma) - 1;
4159 h->needs_plt = 0;
4160 }
4161 }
4162 else
4163 {
4164 h->plt.offset = (bfd_vma) - 1;
4165 h->needs_plt = 0;
4166 }
4167
4168 if (h->got.refcount > 0)
4169 {
fbaf61ad 4170 asection *sgot;
35c08157 4171 bfd_boolean dyn;
1c8f6a4d 4172 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
35c08157
KLC
4173
4174 /* Make sure this symbol is output as a dynamic symbol.
4175 Undefined weak syms won't yet be marked as dynamic. */
4176 if (h->dynindx == -1 && !h->forced_local)
4177 {
4178 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4179 return FALSE;
4180 }
4181
fbaf61ad
NC
4182 sgot = elf_hash_table (info)->sgot;
4183 h->got.offset = sgot->size;
1c8f6a4d
KLC
4184
4185 if (tls_type == GOT_UNKNOWN)
4186 abort ();
fbaf61ad
NC
4187
4188 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4189 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4190 sgot->size += 4;
4191 else
4192 {
4193 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4194 if (tls_type & GOT_TLS_DESC)
4195 sgot->size += 8;
4196 }
1c8f6a4d 4197
35c08157 4198 dyn = htab->root.dynamic_sections_created;
fbaf61ad 4199
0e1862bb 4200 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
fbaf61ad
NC
4201 {
4202 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4203 {
4204 /* TLS_DESC with trampoline needs a relocation slot
4205 within .rela.plt. */
4206 htab->num_tls_desc++;
4207 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4208 htab->tls_trampoline = -1;
4209 }
4210 else
4211 {
4212 /* other relocations, including TLS_DESC without trampoline, need
4213 a relocation slot within .rela.got. */
4214 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4215 }
4216 }
35c08157
KLC
4217 }
4218 else
fbaf61ad 4219 h->got.offset = (bfd_vma)-1;
35c08157 4220
190eb1dd 4221 if (h->dyn_relocs == NULL)
35c08157
KLC
4222 return TRUE;
4223
4224 /* In the shared -Bsymbolic case, discard space allocated for
4225 dynamic pc-relative relocs against symbols which turn out to be
4226 defined in regular objects. For the normal shared case, discard
4227 space for pc-relative relocs that have become local due to symbol
4228 visibility changes. */
4229
0e1862bb 4230 if (bfd_link_pic (info))
35c08157
KLC
4231 {
4232 if (h->def_regular && (h->forced_local || info->symbolic))
4233 {
3bf083ed 4234 struct elf_dyn_relocs **pp;
35c08157 4235
190eb1dd 4236 for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
35c08157
KLC
4237 {
4238 p->count -= p->pc_count;
4239 p->pc_count = 0;
4240 if (p->count == 0)
4241 *pp = p->next;
4242 else
4243 pp = &p->next;
4244 }
4245 }
4246 }
4247 else
4248 {
4249 /* For the non-shared case, discard space for relocs against
4250 symbols which turn out to need copy relocs or are not dynamic. */
4251
4252 if (!h->non_got_ref
4253 && ((h->def_dynamic
4254 && !h->def_regular)
4255 || (htab->root.dynamic_sections_created
4256 && (h->root.type == bfd_link_hash_undefweak
4257 || h->root.type == bfd_link_hash_undefined))))
4258 {
4259 /* Make sure this symbol is output as a dynamic symbol.
4260 Undefined weak syms won't yet be marked as dynamic. */
4261 if (h->dynindx == -1 && !h->forced_local)
4262 {
4263 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4264 return FALSE;
4265 }
4266
4267 /* If that succeeded, we know we'll be keeping all the
4268 relocs. */
4269 if (h->dynindx != -1)
4270 goto keep;
4271 }
4272
190eb1dd 4273 h->dyn_relocs = NULL;
35c08157 4274
dc1e8a47 4275 keep:;
35c08157
KLC
4276 }
4277
4278 /* Finally, allocate space. */
190eb1dd 4279 for (p = h->dyn_relocs; p != NULL; p = p->next)
35c08157
KLC
4280 {
4281 asection *sreloc = elf_section_data (p->sec)->sreloc;
4282 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4283 }
4284
4285 return TRUE;
4286}
4287
fbaf61ad
NC
4288/* Add relocation REL to the end of relocation section SRELOC. */
4289
4290static void
4291elf32_nds32_add_dynreloc (bfd *output_bfd,
4292 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4293 asection *sreloc, Elf_Internal_Rela *rel)
4294{
4295 bfd_byte *loc;
4296 if (sreloc == NULL)
4297 abort ();
4298
4299 loc = sreloc->contents;
4300 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4301 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4302 abort ();
4303
4304 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4305}
4306
63c1f59d
AM
4307/* Set DF_TEXTREL if we find any dynamic relocs that apply to
4308 read-only sections. */
35c08157
KLC
4309
4310static bfd_boolean
63c1f59d 4311maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
35c08157 4312{
63c1f59d 4313 asection *sec;
35c08157 4314
63c1f59d
AM
4315 if (h->root.type == bfd_link_hash_indirect)
4316 return TRUE;
35c08157 4317
63c1f59d
AM
4318 sec = readonly_dynrelocs (h);
4319 if (sec != NULL)
35c08157 4320 {
63c1f59d 4321 struct bfd_link_info *info = (struct bfd_link_info *) info_p;
35c08157 4322
63c1f59d
AM
4323 info->flags |= DF_TEXTREL;
4324 info->callbacks->minfo
c1c8c1ef 4325 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
63c1f59d 4326 sec->owner, h->root.root.string, sec);
35c08157 4327
63c1f59d
AM
4328 /* Not an error, just cut short the traversal. */
4329 return FALSE;
35c08157
KLC
4330 }
4331 return TRUE;
4332}
4333
4334/* Set the sizes of the dynamic sections. */
4335
4336static bfd_boolean
4337nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4338 struct bfd_link_info *info)
4339{
4340 struct elf_nds32_link_hash_table *htab;
4341 bfd *dynobj;
4342 asection *s;
4343 bfd_boolean relocs;
fbaf61ad 4344 bfd_boolean plt;
35c08157
KLC
4345 bfd *ibfd;
4346
4347 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4348 if (htab == NULL)
4349 return FALSE;
4350
4351 dynobj = elf_hash_table (info)->dynobj;
35c08157
KLC
4352 BFD_ASSERT (dynobj != NULL);
4353
fbaf61ad 4354 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
4355 {
4356 /* Set the contents of the .interp section to the interpreter. */
f5233a16 4357 if (bfd_link_executable (info) && !info->nointerp)
35c08157
KLC
4358 {
4359 s = bfd_get_section_by_name (dynobj, ".interp");
4360 BFD_ASSERT (s != NULL);
4361 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4362 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4363 }
4364 }
4365
4366 /* Set up .got offsets for local syms, and space for local dynamic
4367 relocs. */
c72f2fb2 4368 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
35c08157
KLC
4369 {
4370 bfd_signed_vma *local_got;
4371 bfd_signed_vma *end_local_got;
4372 bfd_size_type locsymcount;
4373 Elf_Internal_Shdr *symtab_hdr;
fbaf61ad
NC
4374 asection *sgot;
4375 char *local_tls_type;
4376 unsigned long symndx;
4377 bfd_vma *local_tlsdesc_gotent;
35c08157
KLC
4378
4379 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4380 continue;
4381
4382 for (s = ibfd->sections; s != NULL; s = s->next)
4383 {
3bf083ed 4384 struct elf_dyn_relocs *p;
35c08157 4385
3bf083ed 4386 for (p = ((struct elf_dyn_relocs *)
35c08157
KLC
4387 elf_section_data (s)->local_dynrel);
4388 p != NULL; p = p->next)
4389 {
4390 if (!bfd_is_abs_section (p->sec)
4391 && bfd_is_abs_section (p->sec->output_section))
4392 {
4393 /* Input section has been discarded, either because
4394 it is a copy of a linkonce section or due to
4395 linker script /DISCARD/, so we'll be discarding
4396 the relocs too. */
4397 }
4398 else if (p->count != 0)
4399 {
fbaf61ad
NC
4400 asection *sreloc = elf_section_data (p->sec)->sreloc;
4401 sreloc->size += p->count * sizeof (Elf32_External_Rela);
35c08157
KLC
4402 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4403 info->flags |= DF_TEXTREL;
4404 }
4405 }
4406 }
4407
4408 local_got = elf_local_got_refcounts (ibfd);
4409 if (!local_got)
4410 continue;
4411
4412 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4413 locsymcount = symtab_hdr->sh_info;
4414 end_local_got = local_got + locsymcount;
fbaf61ad
NC
4415 sgot = elf_hash_table (info)->sgot;
4416 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4417 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4418 for (symndx = 0; local_got < end_local_got;
4419 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
35c08157
KLC
4420 {
4421 if (*local_got > 0)
4422 {
fbaf61ad
NC
4423 int num_of_got_entry_needed = 0;
4424 *local_got = sgot->size;
4425 *local_tlsdesc_gotent = sgot->size;
4426
4427 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4428 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4429 num_of_got_entry_needed = 1;
4430 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4431 else if (*local_tls_type & GOT_TLS_DESC)
4432 num_of_got_entry_needed = 2;
4433
4434 sgot->size += (num_of_got_entry_needed << 2);
4435
4436 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4437 others need a slot in .rela.got. */
4438 if (*local_tls_type == GOT_TLS_DESC)
4439 {
4440 if (bfd_link_pic (info))
4441 {
4442 if (htab->tls_desc_trampoline)
4443 {
4444 htab->num_tls_desc++;
4445 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4446 htab->tls_trampoline = -1;
4447 }
4448 else
4449 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4450 }
4451 else
4452 {
4453 /* TLS_DESC -> TLS_LE */
4454 }
4455 }
4456 else
4457 {
4458 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4459 }
35c08157
KLC
4460 }
4461 else
fbaf61ad
NC
4462 {
4463 *local_got = (bfd_vma) -1;
4464 *local_tlsdesc_gotent = (bfd_vma) -1;
4465 }
35c08157
KLC
4466 }
4467 }
4468
4469 /* Allocate global sym .plt and .got entries, and space for global
4470 sym dynamic relocs. */
4471 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4472
fbaf61ad
NC
4473 /* For every jump slot reserved in the sgotplt, reloc_count is
4474 incremented. However, when we reserve space for TLS descriptors,
4475 it's not incremented, so in order to compute the space reserved
4476 for them, it suffices to multiply the reloc count by the jump
4477 slot size. */
4478 if (htab->tls_desc_trampoline && htab->root.srelplt)
4479 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4480
4481 if (htab->tls_trampoline)
4482 {
4483 htab->tls_trampoline = htab->root.splt->size;
4484
4485 /* If we're not using lazy TLS relocations, don't generate the
4486 PLT and GOT entries they require. */
4487 if (!(info->flags & DF_BIND_NOW))
4488 {
4489 htab->dt_tlsdesc_got = htab->root.sgot->size;
4490 htab->root.sgot->size += 4;
4491
4492 htab->dt_tlsdesc_plt = htab->root.splt->size;
4493 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4494 }
4495 }
4496
35c08157
KLC
4497 /* We now have determined the sizes of the various dynamic sections.
4498 Allocate memory for them. */
fbaf61ad
NC
4499 /* The check_relocs and adjust_dynamic_symbol entry points have
4500 determined the sizes of the various dynamic sections. Allocate
4501 memory for them. */
4502 plt = FALSE;
35c08157
KLC
4503 relocs = FALSE;
4504 for (s = dynobj->sections; s != NULL; s = s->next)
4505 {
4506 if ((s->flags & SEC_LINKER_CREATED) == 0)
4507 continue;
4508
ce558b89 4509 if (s == htab->root.splt)
35c08157
KLC
4510 {
4511 /* Strip this section if we don't need it; see the
4512 comment below. */
fbaf61ad 4513 plt = s->size != 0;
35c08157 4514 }
fbaf61ad 4515 else if (s == elf_hash_table (info)->sgot)
35c08157
KLC
4516 {
4517 got_size += s->size;
4518 }
fbaf61ad 4519 else if (s == elf_hash_table (info)->sgotplt)
35c08157
KLC
4520 {
4521 got_size += s->size;
4522 }
fd361982 4523 else if (strncmp (bfd_section_name (s), ".rela", 5) == 0)
35c08157 4524 {
fbaf61ad 4525 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
35c08157
KLC
4526 relocs = TRUE;
4527
4528 /* We use the reloc_count field as a counter if we need
4529 to copy relocs into the output file. */
4530 s->reloc_count = 0;
4531 }
4532 else
4533 {
4534 /* It's not one of our sections, so don't allocate space. */
4535 continue;
4536 }
4537
4538 if (s->size == 0)
4539 {
4540 /* If we don't need this section, strip it from the
4541 output file. This is mostly to handle .rela.bss and
4542 .rela.plt. We must create both sections in
4543 create_dynamic_sections, because they must be created
4544 before the linker maps input sections to output
4545 sections. The linker does that before
4546 adjust_dynamic_symbol is called, and it is that
4547 function which decides whether anything needs to go
4548 into these sections. */
4549 s->flags |= SEC_EXCLUDE;
4550 continue;
4551 }
4552
4553 /* Allocate memory for the section contents. We use bfd_zalloc
4554 here in case unused entries are not reclaimed before the
4555 section's contents are written out. This should not happen,
4556 but this way if it does, we get a R_NDS32_NONE reloc instead
4557 of garbage. */
4558 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4559 if (s->contents == NULL)
4560 return FALSE;
4561 }
4562
4563
4564 if (htab->root.dynamic_sections_created)
4565 {
4566 /* Add some entries to the .dynamic section. We fill in the
4567 values later, in nds32_elf_finish_dynamic_sections, but we
4568 must add the entries now so that we get the correct size for
4569 the .dynamic section. The DT_DEBUG entry is filled in by the
4570 dynamic linker and used by the debugger. */
4571#define add_dynamic_entry(TAG, VAL) \
4572 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4573
fbaf61ad 4574 if (bfd_link_executable (info))
35c08157
KLC
4575 {
4576 if (!add_dynamic_entry (DT_DEBUG, 0))
4577 return FALSE;
4578 }
4579
fbaf61ad 4580 if (elf_hash_table (info)->splt->size != 0)
35c08157
KLC
4581 {
4582 if (!add_dynamic_entry (DT_PLTGOT, 0)
4583 || !add_dynamic_entry (DT_PLTRELSZ, 0)
4584 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4585 || !add_dynamic_entry (DT_JMPREL, 0))
4586 return FALSE;
4587 }
4588
fbaf61ad
NC
4589 if (htab->tls_desc_trampoline && plt)
4590 {
4591 if (htab->dt_tlsdesc_plt
4592 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
4593 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
4594 return FALSE;
4595 }
4596
35c08157
KLC
4597 if (relocs)
4598 {
4599 if (!add_dynamic_entry (DT_RELA, 0)
4600 || !add_dynamic_entry (DT_RELASZ, 0)
4601 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
4602 return FALSE;
4603
4604 /* If any dynamic relocs apply to a read-only section,
4605 then we need a DT_TEXTREL entry. */
4606 if ((info->flags & DF_TEXTREL) == 0)
63c1f59d 4607 elf_link_hash_traverse (&htab->root, maybe_set_textrel,
35c08157
KLC
4608 (void *) info);
4609
4610 if ((info->flags & DF_TEXTREL) != 0)
4611 {
4612 if (!add_dynamic_entry (DT_TEXTREL, 0))
4613 return FALSE;
4614 }
4615 }
4616 }
4617#undef add_dynamic_entry
4618
4619 return TRUE;
4620}
4621
4622static bfd_reloc_status_type
4623nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4624 bfd_vma relocation, bfd_byte *location)
4625{
4626 int size;
4627 bfd_vma x = 0;
4628 bfd_reloc_status_type flag;
4629 unsigned int rightshift = howto->rightshift;
4630 unsigned int bitpos = howto->bitpos;
4631
4632 /* If the size is negative, negate RELOCATION. This isn't very
4633 general. */
4634 if (howto->size < 0)
4635 relocation = -relocation;
4636
4637 /* Get the value we are going to relocate. */
4638 size = bfd_get_reloc_size (howto);
4639 switch (size)
4640 {
4641 default:
35c08157
KLC
4642 abort ();
4643 break;
6346d5ca
AM
4644 case 0:
4645 return bfd_reloc_ok;
35c08157
KLC
4646 case 2:
4647 x = bfd_getb16 (location);
4648 break;
4649 case 4:
4650 x = bfd_getb32 (location);
4651 break;
4652 }
4653
4654 /* Check for overflow. FIXME: We may drop bits during the addition
4655 which we don't check for. We must either check at every single
4656 operation, which would be tedious, or we must do the computations
4657 in a type larger than bfd_vma, which would be inefficient. */
4658 flag = bfd_reloc_ok;
4659 if (howto->complain_on_overflow != complain_overflow_dont)
4660 {
4661 bfd_vma addrmask, fieldmask, signmask, ss;
4662 bfd_vma a, b, sum;
4663
4664 /* Get the values to be added together. For signed and unsigned
4665 relocations, we assume that all values should be truncated to
4666 the size of an address. For bitfields, all the bits matter.
4667 See also bfd_check_overflow. */
4668 fieldmask = N_ONES (howto->bitsize);
4669 signmask = ~fieldmask;
4670 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4671 a = (relocation & addrmask) >> rightshift;
4672 b = (x & howto->src_mask & addrmask) >> bitpos;
4673
4674 switch (howto->complain_on_overflow)
4675 {
4676 case complain_overflow_signed:
4677 /* If any sign bits are set, all sign bits must be set.
4678 That is, A must be a valid negative address after
4679 shifting. */
4680 signmask = ~(fieldmask >> 1);
4681 /* Fall through. */
4682
4683 case complain_overflow_bitfield:
4684 /* Much like the signed check, but for a field one bit
4685 wider. We allow a bitfield to represent numbers in the
4686 range -2**n to 2**n-1, where n is the number of bits in the
4687 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4688 can't overflow, which is exactly what we want. */
4689 ss = a & signmask;
4690 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4691 flag = bfd_reloc_overflow;
4692
4693 /* We only need this next bit of code if the sign bit of B
4694 is below the sign bit of A. This would only happen if
4695 SRC_MASK had fewer bits than BITSIZE. Note that if
4696 SRC_MASK has more bits than BITSIZE, we can get into
4697 trouble; we would need to verify that B is in range, as
4698 we do for A above. */
4699 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4700 ss >>= bitpos;
4701
4702 /* Set all the bits above the sign bit. */
4703 b = (b ^ ss) - ss;
4704
4705 /* Now we can do the addition. */
4706 sum = a + b;
4707
4708 /* See if the result has the correct sign. Bits above the
4709 sign bit are junk now; ignore them. If the sum is
4710 positive, make sure we did not have all negative inputs;
4711 if the sum is negative, make sure we did not have all
4712 positive inputs. The test below looks only at the sign
4713 bits, and it really just
4714 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4715
4716 We mask with addrmask here to explicitly allow an address
4717 wrap-around. The Linux kernel relies on it, and it is
4718 the only way to write assembler code which can run when
4719 loaded at a location 0x80000000 away from the location at
4720 which it is linked. */
4721 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4722 flag = bfd_reloc_overflow;
4723
4724 break;
4725
4726 case complain_overflow_unsigned:
4727 /* Checking for an unsigned overflow is relatively easy:
4728 trim the addresses and add, and trim the result as well.
4729 Overflow is normally indicated when the result does not
4730 fit in the field. However, we also need to consider the
4731 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4732 input is 0x80000000, and bfd_vma is only 32 bits; then we
4733 will get sum == 0, but there is an overflow, since the
4734 inputs did not fit in the field. Instead of doing a
4735 separate test, we can check for this by or-ing in the
4736 operands when testing for the sum overflowing its final
4737 field. */
4738 sum = (a + b) & addrmask;
4739 if ((a | b | sum) & signmask)
4740 flag = bfd_reloc_overflow;
4741 break;
4742
4743 default:
4744 abort ();
4745 }
4746 }
4747
4748 /* Put RELOCATION in the right bits. */
4749 relocation >>= (bfd_vma) rightshift;
4750 relocation <<= (bfd_vma) bitpos;
4751
4752 /* Add RELOCATION to the right bits of X. */
4753 /* FIXME : 090616
4754 Because the relaxation may generate duplicate relocation at one address,
4755 an addition to immediate in the instruction may cause the relocation added
4756 several times.
4757 This bug should be fixed in assembler, but a check is also needed here. */
4758 if (howto->partial_inplace)
4759 x = ((x & ~howto->dst_mask)
4760 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4761 else
4762 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4763
4764
4765 /* Put the relocated value back in the object file. */
4766 switch (size)
4767 {
4768 default:
4769 case 0:
4770 case 1:
4771 case 8:
4772 abort ();
4773 break;
4774 case 2:
4775 bfd_putb16 (x, location);
4776 break;
4777 case 4:
4778 bfd_putb32 (x, location);
4779 break;
4780 }
4781
4782 return flag;
4783}
4784
4785static bfd_reloc_status_type
4786nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4787 asection *input_section, bfd_byte *contents,
4788 bfd_vma address, bfd_vma value, bfd_vma addend)
4789{
4790 bfd_vma relocation;
4791
4792 /* Sanity check the address. */
4793 if (address > bfd_get_section_limit (input_bfd, input_section))
4794 return bfd_reloc_outofrange;
4795
4796 /* This function assumes that we are dealing with a basic relocation
4797 against a symbol. We want to compute the value of the symbol to
4798 relocate to. This is just VALUE, the value of the symbol, plus
4799 ADDEND, any addend associated with the reloc. */
4800 relocation = value + addend;
4801
4802 /* If the relocation is PC relative, we want to set RELOCATION to
4803 the distance between the symbol (currently in RELOCATION) and the
c2bf1eec 4804 location we are relocating. If pcrel_offset is FALSE we do not
35c08157
KLC
4805 need to subtract out the offset of the location within the
4806 section (which is just ADDRESS). */
4807 if (howto->pc_relative)
4808 {
4809 relocation -= (input_section->output_section->vma
4810 + input_section->output_offset);
4811 if (howto->pcrel_offset)
4812 relocation -= address;
4813 }
4814
4815 return nds32_relocate_contents (howto, input_bfd, relocation,
4816 contents + address);
4817}
4818
4819static bfd_boolean
4820nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4821 const char *name,
4822 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4823 asection *input_sec,
4824 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4825{
4826 const char *source;
4827 FILE *sym_ld_script = NULL;
4828 struct elf_nds32_link_hash_table *table;
4829
4830 table = nds32_elf_hash_table (info);
4831 sym_ld_script = table->sym_ld_script;
4832 if (!sym_ld_script)
4833 return TRUE;
4834
4835 if (!h || !name || *name == '\0')
4836 return TRUE;
4837
4838 if (input_sec->flags & SEC_EXCLUDE)
4839 return TRUE;
4840
4841 if (!check_start_export_sym)
4842 {
4843 fprintf (sym_ld_script, "SECTIONS\n{\n");
4844 check_start_export_sym = 1;
4845 }
4846
4847 if (h->root.type == bfd_link_hash_defined
4848 || h->root.type == bfd_link_hash_defweak)
4849 {
4850 if (!h->root.u.def.section->output_section)
4851 return TRUE;
4852
4853 if (bfd_is_const_section (input_sec))
4854 source = input_sec->name;
4855 else
765cf5f6 4856 source = bfd_get_filename (input_sec->owner);
35c08157
KLC
4857
4858 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4859 h->root.root.string,
4860 (long) (h->root.u.def.value
4861 + h->root.u.def.section->output_section->vma
4862 + h->root.u.def.section->output_offset), source);
4863 }
4864
4865 return TRUE;
4866}
4867
4868/* Relocate an NDS32/D ELF section.
4869 There is some attempt to make this function usable for many architectures,
4870 both for RELA and REL type relocs, if only to serve as a learning tool.
4871
4872 The RELOCATE_SECTION function is called by the new ELF backend linker
4873 to handle the relocations for a section.
4874
4875 The relocs are always passed as Rela structures; if the section
4876 actually uses Rel structures, the r_addend field will always be
4877 zero.
4878
4879 This function is responsible for adjust the section contents as
4880 necessary, and (if using Rela relocs and generating a
4881 relocatable output file) adjusting the reloc addend as
4882 necessary.
4883
4884 This function does not have to worry about setting the reloc
4885 address or the reloc symbol index.
4886
4887 LOCAL_SYMS is a pointer to the swapped in local symbols.
4888
4889 LOCAL_SECTIONS is an array giving the section in the input file
4890 corresponding to the st_shndx field of each local symbol.
4891
4892 The global hash table entry for the global symbols can be found
4893 via elf_sym_hashes (input_bfd).
4894
4895 When generating relocatable output, this function must handle
4896 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4897 going to be the section symbol corresponding to the output
4898 section, which means that the addend must be adjusted
4899 accordingly. */
4900
fbaf61ad
NC
4901/* Return the base VMA address which should be subtracted from real addresses
4902 when resolving @dtpoff relocation.
4903 This is PT_TLS segment p_vaddr. */
4904
4905/* Return the relocation value for @tpoff relocation
4906 if STT_TLS virtual address is ADDRESS. */
4907
4908/* Return the relocation value for @gottpoff relocation
4909 if STT_TLS virtual address is ADDRESS. */
4910
1c8f6a4d 4911static bfd_vma
fbaf61ad 4912gottpoff (struct bfd_link_info *info, bfd_vma address)
1c8f6a4d 4913{
fbaf61ad
NC
4914 bfd_vma tp_base;
4915 bfd_vma tp_offset;
4916
1c8f6a4d
KLC
4917 /* If tls_sec is NULL, we should have signalled an error already. */
4918 if (elf_hash_table (info)->tls_sec == NULL)
4919 return 0;
fbaf61ad
NC
4920
4921 tp_base = elf_hash_table (info)->tls_sec->vma;
4922 tp_offset = address - tp_base;
4923
4924 return tp_offset;
1c8f6a4d
KLC
4925}
4926
35c08157 4927static bfd_boolean
fbaf61ad
NC
4928patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4929{
4930 /* TLS_GD/TLS_LD model #1
4931 46 00 00 00 sethi $r0,#0x0
4932 58 00 00 00 ori $r0,$r0,#0x0
4933 40 00 74 00 add $r0,$r0,$gp
4934 04 10 00 00 lwi $r1,[$r0+#0x0]
4935 4b e0 04 01 jral $lp,$r1 */
4936
4937 /* TLS_GD/TLS_LD model #2
4938 46 00 00 00 sethi $r0,#0x0
4939 58 00 00 00 ori $r0,$r0,#0x0
4940 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4941 40 00 74 00 add $r0,$r0,$gp
4942 4b e0 04 01 jral $lp,$r1 */
4943
4944 /* TLS_IE model (non-PIC)
4945 46 00 00 00 sethi $r0,#0x0
4946 04 00 00 00 lwi $r0,[$r0+#0x0]
4947 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4948
4949 /* TLS_IE model (PIC)
4950 46 00 00 00 sethi $r0,#0x0
4951 58 00 00 00 ori $r0,$r0,#0x0
4952 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4953 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4954
4955 /* TLS_GD_TO_IE model
4956 46 00 00 00 sethi $r0,#0x0
4957 58 00 00 00 ori $r0,$r0,#0x0
4958 40 00 74 00 add $r0,$rM,$gp
4959 04 00 00 01 lwi $r0,[$r0+#0x4]
4960 40 00 64 00 add $r0,$r0,$r25 */
4961
4962 bfd_boolean rz = FALSE;
4963
4964 typedef struct
4965 {
4966 uint32_t opcode;
4967 uint32_t mask;
4968 } pat_t;
4969
4970 uint32_t patch[3] =
4971 {
4972 0x40007400, /* add $r0,$rM,$gp */
4973 0x04000001, /* lwi $r0,[$r0+#0x4] */
4974 0x40006400, /* add $r0,$r0,$r25 */
4975 };
4976
4977 pat_t mode0[3] =
4978 {
4979 { 0x40000000, 0xfe0003ff },
4980 { 0x04000000, 0xfe000000 },
4981 { 0x4be00001, 0xffff83ff },
4982 };
4983
4984 pat_t mode1[3] =
4985 {
4986 { 0x38007402, 0xfe007fff },
4987 { 0x40007400, 0xfe007fff },
4988 { 0x4be00001, 0xffff83ff },
4989 };
4990
4991 unsigned char *p = contents + rel->r_offset;
4992
4993 uint32_t insn;
4994 uint32_t regidx = 0;
4995 insn = bfd_getb32 (p);
4996 if (INSN_SETHI == (0xfe0fffffu & insn))
4997 {
4998 regidx = 0x1f & (insn >> 20);
4999 p += 4;
5000 }
5001
5002 insn = bfd_getb32 (p);
5003 if (INSN_ORI == (0xfe007fffu & insn))
5004 {
5005 regidx = 0x1f & (insn >> 20);
5006 p += 4;
5007 }
5008
5009 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
5010 {
5011 /* already patched? */
5012 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
5013 (patch[1] == bfd_getb32 (p + 4)))
5014 rz = TRUE;
5015 }
5016 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
5017 {
5018 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
5019 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
5020 {
5021 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5022 bfd_putb32 (patch[1], p + 4);
5023 bfd_putb32 (patch[2], p + 8);
5024 rz = TRUE;
5025 }
5026 }
5027 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
5028 {
5029 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
5030 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5031 {
5032 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5033 bfd_putb32 (patch[1], p + 4);
5034 bfd_putb32 (patch[2], p + 8);
5035 rz = TRUE;
5036 }
5037 }
5038
5039 if (!rz)
5040 {
765cf5f6 5041 printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
fbaf61ad
NC
5042 (int) rel->r_offset);
5043 BFD_ASSERT(0); /* Unsupported pattern. */
5044 }
5045
5046 return rz;
5047}
5048
5049static enum elf_nds32_tls_type
5050get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5051
5052static unsigned int
5053ones32 (register unsigned int x)
5054{
5055 /* 32-bit recursive reduction using SWAR...
5056 but first step is mapping 2-bit values
5057 into sum of 2 1-bit values in sneaky way. */
5058 x -= ((x >> 1) & 0x55555555);
5059 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5060 x = (((x >> 4) + x) & 0x0f0f0f0f);
5061 x += (x >> 8);
5062 x += (x >> 16);
5063 return (x & 0x0000003f);
5064}
5065
a767a1c4 5066#if !HAVE_FLS
fbaf61ad
NC
5067static unsigned int
5068fls (register unsigned int x)
5069{
5070 return ffs (x & (-x));
5071}
a767a1c4 5072#endif /* !HAVE_FLS */
fbaf61ad
NC
5073
5074#define nds32_elf_local_tlsdesc_gotent(bfd) \
5075 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5076
5077static bfd_boolean
5078nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
5079 struct bfd_link_info * info,
5080 bfd * input_bfd,
5081 asection * input_section,
5082 bfd_byte * contents,
5083 Elf_Internal_Rela * relocs,
5084 Elf_Internal_Sym * local_syms,
5085 asection ** local_sections)
5086{
5087 Elf_Internal_Shdr *symtab_hdr;
5088 struct elf_link_hash_entry **sym_hashes;
5089 Elf_Internal_Rela *rel, *relend;
5090 bfd_boolean ret = TRUE; /* Assume success. */
5091 int align = 0;
5092 bfd_reloc_status_type r;
5093 const char *errmsg = NULL;
5094 bfd_vma gp;
5095 struct elf_link_hash_table *ehtab;
5096 struct elf_nds32_link_hash_table *htab;
5097 bfd *dynobj;
5098 bfd_vma *local_got_offsets;
5099 asection *sgot, *splt, *sreloc;
5100 bfd_vma high_address;
5101 struct elf_nds32_link_hash_table *table;
5102 int eliminate_gc_relocs;
35c08157
KLC
5103 bfd_vma fpbase_addr;
5104
5105 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5106 sym_hashes = elf_sym_hashes (input_bfd);
fbaf61ad 5107 ehtab = elf_hash_table (info);
35c08157
KLC
5108 htab = nds32_elf_hash_table (info);
5109 high_address = bfd_get_section_limit (input_bfd, input_section);
5110
5111 dynobj = htab->root.dynobj;
5112 local_got_offsets = elf_local_got_offsets (input_bfd);
5113
fbaf61ad
NC
5114 sgot = ehtab->sgot;
5115 splt = ehtab->splt;
35c08157
KLC
5116 sreloc = NULL;
5117
5118 rel = relocs;
5119 relend = relocs + input_section->reloc_count;
5120
5121 table = nds32_elf_hash_table (info);
5122 eliminate_gc_relocs = table->eliminate_gc_relocs;
fbaf61ad 5123
35c08157 5124 /* By this time, we can adjust the value of _SDA_BASE_. */
fbaf61ad 5125 /* Explain _SDA_BASE_ */
0e1862bb 5126 if ((!bfd_link_relocatable (info)))
35c08157
KLC
5127 {
5128 is_SDA_BASE_set = 1;
5129 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
5130 if (r != bfd_reloc_ok)
5131 return FALSE;
5132 }
5133
fbaf61ad
NC
5134 /* Do TLS model conversion once at first. */
5135 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5136
35c08157
KLC
5137 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5138 the fp value is set as gp, and it has be reverted for instruction
5139 setting fp. */
5140 fpbase_addr = elf_gp (output_bfd);
5141
fbaf61ad 5142 /* Deal with (dynamic) relocations. */
35c08157
KLC
5143 for (rel = relocs; rel < relend; rel++)
5144 {
5145 enum elf_nds32_reloc_type r_type;
5146 reloc_howto_type *howto = NULL;
5147 unsigned long r_symndx;
5148 struct elf_link_hash_entry *h = NULL;
5149 Elf_Internal_Sym *sym = NULL;
5150 asection *sec;
5151 bfd_vma relocation;
fbaf61ad
NC
5152 bfd_vma relocation_sym = 0xdeadbeef;
5153 Elf_Internal_Rela *lorel;
5154 bfd_vma off;
35c08157
KLC
5155
5156 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5157 ensure it's zero (we use REL relocs, not RELA). Therefore this
5158 should be assigning zero to `addend', but for clarity we use
5159 `r_addend'. */
5160
5161 bfd_vma addend = rel->r_addend;
5162 bfd_vma offset = rel->r_offset;
5163
5164 r_type = ELF32_R_TYPE (rel->r_info);
5165 if (r_type >= R_NDS32_max)
5166 {
695344c0 5167 /* xgettext:c-format */
0aa13fee 5168 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4eca0228 5169 input_bfd, r_type);
35c08157
KLC
5170 bfd_set_error (bfd_error_bad_value);
5171 ret = FALSE;
5172 continue;
5173 }
5174
5175 if (r_type == R_NDS32_GNU_VTENTRY
5176 || r_type == R_NDS32_GNU_VTINHERIT
5177 || r_type == R_NDS32_NONE
5178 || r_type == R_NDS32_RELA_GNU_VTENTRY
5179 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5180 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5181 || r_type == R_NDS32_DATA
fbaf61ad 5182 || r_type == R_NDS32_TRAN)
35c08157
KLC
5183 continue;
5184
0c4bd9d9
KLC
5185 /* If we enter the fp-as-gp region. Resolve the address
5186 of best fp-base. */
35c08157
KLC
5187 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5188 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5189 {
5190 int dist;
5191
5192 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5193 dist = rel->r_addend >> 16;
5194 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5195 local_syms, symtab_hdr);
5196 }
5197 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5198 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5199 {
5200 fpbase_addr = elf_gp (output_bfd);
5201 }
5202
fbaf61ad
NC
5203 /* Skip the relocations used for relaxation. */
5204 /* We have to update LONGCALL and LONGJUMP
5205 relocations when generating the relocatable files. */
5206 if (!bfd_link_relocatable (info)
5207 && (r_type >= R_NDS32_RELAX_ENTRY
5208 || (r_type >= R_NDS32_LONGCALL4
5209 && r_type <= R_NDS32_LONGJUMP7)))
35c08157
KLC
5210 continue;
5211
5212 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5213 r_symndx = ELF32_R_SYM (rel->r_info);
5214
5215 /* This is a final link. */
5216 sym = NULL;
5217 sec = NULL;
5218 h = NULL;
5219
5220 if (r_symndx < symtab_hdr->sh_info)
5221 {
5222 /* Local symbol. */
5223 sym = local_syms + r_symndx;
5224 sec = local_sections[r_symndx];
5225
5226 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5227 addend = rel->r_addend;
fbaf61ad
NC
5228
5229 /* keep symbol location for static TLS_IE GOT entry */
5230 relocation_sym = relocation;
5231 if (bfd_link_relocatable (info))
5232 {
5233 /* This is a relocatable link. We don't have to change
5234 anything, unless the reloc is against a section symbol,
5235 in which case we have to adjust according to where the
5236 section symbol winds up in the output section. */
5237 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5238 rel->r_addend += sec->output_offset + sym->st_value;
5239
5240 continue;
5241 }
35c08157
KLC
5242 }
5243 else
5244 {
5245 /* External symbol. */
fbaf61ad
NC
5246 if (bfd_link_relocatable (info))
5247 continue;
35c08157
KLC
5248 bfd_boolean warned, ignored, unresolved_reloc;
5249 int symndx = r_symndx - symtab_hdr->sh_info;
5250
5251 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5252 r_symndx, symtab_hdr, sym_hashes, h, sec,
5253 relocation, unresolved_reloc, warned,
5254 ignored);
5255
fbaf61ad
NC
5256 /* keep symbol location for static TLS_IE GOT entry */
5257 relocation_sym = relocation;
5258
35c08157
KLC
5259 /* la $fp, _FP_BASE_ is per-function (region).
5260 Handle it specially. */
5261 switch ((int) r_type)
5262 {
fbaf61ad
NC
5263 case R_NDS32_HI20_RELA:
5264 case R_NDS32_LO12S0_RELA:
5265 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5266 FP_BASE_NAME) == 0)
5267 {
5268 if (!bfd_link_pie (info))
5269 {
5270 _bfd_error_handler
5271 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5272 input_bfd);
5273 }
5274 relocation = fpbase_addr;
5275 }
5276 break;
35c08157
KLC
5277 case R_NDS32_SDA19S0_RELA:
5278 case R_NDS32_SDA15S0_RELA:
5279 case R_NDS32_20_RELA:
5280 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5281 FP_BASE_NAME) == 0)
5282 {
5283 relocation = fpbase_addr;
5284 break;
5285 }
5286 }
35c08157
KLC
5287 }
5288
5289 /* Sanity check the address. */
5290 if (offset > high_address)
5291 {
5292 r = bfd_reloc_outofrange;
5293 goto check_reloc;
5294 }
5295
fbaf61ad 5296 if (r_type >= R_NDS32_RELAX_ENTRY)
35c08157
KLC
5297 continue;
5298
5299 switch ((int) r_type)
5300 {
5301 case R_NDS32_GOTOFF:
5302 /* Relocation is relative to the start of the global offset
5303 table (for ld24 rx, #uimm24), e.g. access at label+addend
5304
5305 ld24 rx. #label@GOTOFF + addend
5306 sub rx, r12. */
5307 case R_NDS32_GOTOFF_HI20:
5308 case R_NDS32_GOTOFF_LO12:
5309 case R_NDS32_GOTOFF_LO15:
5310 case R_NDS32_GOTOFF_LO19:
5311 BFD_ASSERT (sgot != NULL);
5312
5313 relocation -= elf_gp (output_bfd);
5314 break;
5315
5316 case R_NDS32_9_PLTREL:
5317 case R_NDS32_25_PLTREL:
5318 /* Relocation is to the entry for this symbol in the
5319 procedure linkage table. */
5320
5321 /* The native assembler will generate a 25_PLTREL reloc
5322 for a local symbol if you assemble a call from one
5323 section to another when using -K pic. */
5324 if (h == NULL)
5325 break;
5326
5327 if (h->forced_local)
5328 break;
5329
5330 /* We didn't make a PLT entry for this symbol. This
5331 happens when statically linking PIC code, or when
5332 using -Bsymbolic. */
5333 if (h->plt.offset == (bfd_vma) - 1)
5334 break;
5335
5336 relocation = (splt->output_section->vma
5337 + splt->output_offset + h->plt.offset);
5338 break;
5339
5340 case R_NDS32_PLT_GOTREL_HI20:
5341 case R_NDS32_PLT_GOTREL_LO12:
5342 case R_NDS32_PLT_GOTREL_LO15:
5343 case R_NDS32_PLT_GOTREL_LO19:
5344 case R_NDS32_PLT_GOTREL_LO20:
fbaf61ad
NC
5345 if (h == NULL
5346 || h->forced_local
5347 || h->plt.offset == (bfd_vma) -1
5348 || (bfd_link_pie (info) && h->def_regular))
35c08157 5349 {
fbaf61ad
NC
5350 /* Maybe we should find better checking to optimize
5351 PIE PLT relocations. */
35c08157
KLC
5352 /* We didn't make a PLT entry for this symbol. This
5353 happens when statically linking PIC code, or when
5354 using -Bsymbolic. */
fbaf61ad
NC
5355 if (h)
5356 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
35c08157
KLC
5357 relocation -= elf_gp (output_bfd);
5358 break;
5359 }
5360
5361 relocation = (splt->output_section->vma
5362 + splt->output_offset + h->plt.offset);
5363
5364 relocation -= elf_gp (output_bfd);
5365 break;
5366
5367 case R_NDS32_PLTREL_HI20:
5368 case R_NDS32_PLTREL_LO12:
5369
5370 /* Relocation is to the entry for this symbol in the
5371 procedure linkage table. */
5372
5373 /* The native assembler will generate a 25_PLTREL reloc
5374 for a local symbol if you assemble a call from one
5375 section to another when using -K pic. */
5376 if (h == NULL)
5377 break;
5378
5379 if (h->forced_local)
5380 break;
5381
5382 if (h->plt.offset == (bfd_vma) - 1)
5383 /* We didn't make a PLT entry for this symbol. This
5384 happens when statically linking PIC code, or when
5385 using -Bsymbolic. */
5386 break;
5387
5388 if (splt == NULL)
5389 break;
5390
5391 relocation = (splt->output_section->vma
5392 + splt->output_offset
5393 + h->plt.offset + 4)
5394 - (input_section->output_section->vma
5395 + input_section->output_offset
5396 + rel->r_offset);
5397
5398 break;
5399
5400 case R_NDS32_GOTPC20:
5401 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5402 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5403 relocation = elf_gp (output_bfd);
5404 break;
5405
5406 case R_NDS32_GOTPC_HI20:
5407 case R_NDS32_GOTPC_LO12:
fbaf61ad
NC
5408 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5409 bl .+4
5410 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5411 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5412 or
5413 bl .+4
5414 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5415 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5416 relocation = elf_gp (output_bfd);
5417 relocation -= (input_section->output_section->vma
5418 + input_section->output_offset + rel->r_offset);
5419 break;
35c08157
KLC
5420
5421 case R_NDS32_GOT20:
5422 /* Fall through. */
5423 case R_NDS32_GOT_HI20:
5424 case R_NDS32_GOT_LO12:
5425 case R_NDS32_GOT_LO15:
5426 case R_NDS32_GOT_LO19:
5427 /* Relocation is to the entry for this symbol in the global
5428 offset table. */
5429 BFD_ASSERT (sgot != NULL);
5430
5431 if (h != NULL)
5432 {
fbaf61ad 5433 /* External symbol */
35c08157 5434 bfd_boolean dyn;
35c08157
KLC
5435
5436 off = h->got.offset;
5437 BFD_ASSERT (off != (bfd_vma) - 1);
5438 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
5439 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5440 bfd_link_pic (info),
5441 h)
5442 || (bfd_link_pic (info)
35c08157
KLC
5443 && (info->symbolic
5444 || h->dynindx == -1
5445 || h->forced_local) && h->def_regular))
5446 {
5447 /* This is actually a static link, or it is a
5448 -Bsymbolic link and the symbol is defined
5449 locally, or the symbol was forced to be local
5450 because of a version file. We must initialize
5451 this entry in the global offset table. Since the
5452 offset must always be a multiple of 4, we use the
5453 least significant bit to record whether we have
5454 initialized it already.
5455
5456 When doing a dynamic link, we create a .rela.got
5457 relocation entry to initialize the value. This
5458 is done in the finish_dynamic_symbol routine. */
fbaf61ad 5459 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5460 off &= ~1;
5461 else
5462 {
5463 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5464 h->got.offset |= 1;
5465 }
5466 }
5467 relocation = sgot->output_section->vma + sgot->output_offset + off
5468 - elf_gp (output_bfd);
5469 }
5470 else
5471 {
fbaf61ad 5472 /* Local symbol */
35c08157
KLC
5473 bfd_byte *loc;
5474
5475 BFD_ASSERT (local_got_offsets != NULL
5476 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5477
5478 off = local_got_offsets[r_symndx];
5479
5480 /* The offset must always be a multiple of 4. We use
5481 the least significant bit to record whether we have
5482 already processed this entry. */
fbaf61ad 5483 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5484 off &= ~1;
5485 else
5486 {
5487 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5488
0e1862bb 5489 if (bfd_link_pic (info))
35c08157
KLC
5490 {
5491 asection *srelgot;
5492 Elf_Internal_Rela outrel;
5493
5494 /* We need to generate a R_NDS32_RELATIVE reloc
5495 for the dynamic linker. */
fbaf61ad 5496 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
35c08157
KLC
5497 BFD_ASSERT (srelgot != NULL);
5498
5499 outrel.r_offset = (elf_gp (output_bfd)
5500 + sgot->output_offset + off);
5501 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5502 outrel.r_addend = relocation;
5503 loc = srelgot->contents;
5504 loc +=
5505 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5506 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5507 ++srelgot->reloc_count;
5508 }
5509 local_got_offsets[r_symndx] |= 1;
5510 }
5511 relocation = sgot->output_section->vma + sgot->output_offset + off
5512 - elf_gp (output_bfd);
5513 }
5514
5515 break;
5516
5517 case R_NDS32_16_RELA:
5518 case R_NDS32_20_RELA:
5519 case R_NDS32_5_RELA:
5520 case R_NDS32_32_RELA:
5521 case R_NDS32_9_PCREL_RELA:
5522 case R_NDS32_WORD_9_PCREL_RELA:
5523 case R_NDS32_10_UPCREL_RELA:
5524 case R_NDS32_15_PCREL_RELA:
5525 case R_NDS32_17_PCREL_RELA:
5526 case R_NDS32_25_PCREL_RELA:
5527 case R_NDS32_HI20_RELA:
5528 case R_NDS32_LO12S3_RELA:
5529 case R_NDS32_LO12S2_RELA:
5530 case R_NDS32_LO12S2_DP_RELA:
5531 case R_NDS32_LO12S2_SP_RELA:
5532 case R_NDS32_LO12S1_RELA:
5533 case R_NDS32_LO12S0_RELA:
5534 case R_NDS32_LO12S0_ORI_RELA:
0e1862bb 5535 if (bfd_link_pic (info) && r_symndx != 0
35c08157
KLC
5536 && (input_section->flags & SEC_ALLOC) != 0
5537 && (eliminate_gc_relocs == 0
5538 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5539 && ((r_type != R_NDS32_9_PCREL_RELA
5540 && r_type != R_NDS32_WORD_9_PCREL_RELA
5541 && r_type != R_NDS32_10_UPCREL_RELA
5542 && r_type != R_NDS32_15_PCREL_RELA
5543 && r_type != R_NDS32_17_PCREL_RELA
5544 && r_type != R_NDS32_25_PCREL_RELA
5545 && !(r_type == R_NDS32_32_RELA
5546 && strcmp (input_section->name, ".eh_frame") == 0))
5547 || (h != NULL && h->dynindx != -1
5548 && (!info->symbolic || !h->def_regular))))
5549 {
5550 Elf_Internal_Rela outrel;
5551 bfd_boolean skip, relocate;
5552 bfd_byte *loc;
5553
5554 /* When generating a shared object, these relocations
5555 are copied into the output file to be resolved at run
5556 time. */
5557
5558 if (sreloc == NULL)
5559 {
5560 const char *name;
5561
5562 name = bfd_elf_string_from_elf_section
5563 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5564 elf_section_data (input_section)->rela.hdr->sh_name);
5565 if (name == NULL)
5566 return FALSE;
5567
5568 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
fd361982 5569 && strcmp (bfd_section_name (input_section),
35c08157
KLC
5570 name + 5) == 0);
5571
5572 sreloc = bfd_get_section_by_name (dynobj, name);
5573 BFD_ASSERT (sreloc != NULL);
5574 }
5575
5576 skip = FALSE;
5577 relocate = FALSE;
5578
5579 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5580 info,
5581 input_section,
5582 rel->r_offset);
5583 if (outrel.r_offset == (bfd_vma) - 1)
5584 skip = TRUE;
5585 else if (outrel.r_offset == (bfd_vma) - 2)
5586 skip = TRUE, relocate = TRUE;
5587 outrel.r_offset += (input_section->output_section->vma
5588 + input_section->output_offset);
5589
5590 if (skip)
5591 memset (&outrel, 0, sizeof outrel);
5592 else if (r_type == R_NDS32_17_PCREL_RELA
5593 || r_type == R_NDS32_15_PCREL_RELA
5594 || r_type == R_NDS32_25_PCREL_RELA)
5595 {
5596 BFD_ASSERT (h != NULL && h->dynindx != -1);
5597 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5598 outrel.r_addend = rel->r_addend;
5599 }
5600 else
5601 {
5602 /* h->dynindx may be -1 if this symbol was marked to
5603 become local. */
5604 if (h == NULL
5605 || ((info->symbolic || h->dynindx == -1)
fbaf61ad
NC
5606 && h->def_regular)
5607 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
5608 {
5609 relocate = TRUE;
5610 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5611 outrel.r_addend = relocation + rel->r_addend;
fbaf61ad
NC
5612
5613 if (h)
5614 {
5615 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5616
5617 BFD_ASSERT (sgot != NULL);
5618 /* If we did not allocate got entry for the symbol,
5619 we can not fill the nonexistent got entry. */
5620 if (h->got.offset != (bfd_vma) -1
5621 && (h->got.offset & 1) == 0)
5622 {
5623 bfd_put_32 (output_bfd, outrel.r_addend,
5624 sgot->contents + h->got.offset);
5625 }
5626 }
35c08157
KLC
5627 }
5628 else
5629 {
fbaf61ad
NC
5630 if (h->dynindx == -1)
5631 {
5632 _bfd_error_handler
acef8081 5633 (_("%pB: relocation %s against `%s' can not be used when "
fbaf61ad
NC
5634 "making a shared object; recompile with -fPIC"),
5635 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5636 bfd_set_error (bfd_error_bad_value);
5637 return FALSE;
5638 }
5639
35c08157
KLC
5640 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5641 outrel.r_addend = rel->r_addend;
5642 }
5643 }
5644
5645 loc = sreloc->contents;
5646 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5647 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5648 ++sreloc->reloc_count;
5649
5650 /* If this reloc is against an external symbol, we do
5651 not want to fiddle with the addend. Otherwise, we
5652 need to include the symbol value so that it becomes
5653 an addend for the dynamic reloc. */
5654 if (!relocate)
5655 continue;
5656 }
5657 break;
5658
5659 case R_NDS32_25_ABS_RELA:
0e1862bb 5660 if (bfd_link_pic (info))
35c08157 5661 {
4eca0228 5662 _bfd_error_handler
38f14ab8
AM
5663 (_("%pB: warning: %s unsupported in shared mode"),
5664 input_bfd, "R_NDS32_25_ABS_RELA");
35c08157
KLC
5665 return FALSE;
5666 }
5667 break;
5668
5669 case R_NDS32_9_PCREL:
5670 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5671 contents, offset,
5672 sec, relocation, addend);
5673 goto check_reloc;
5674
5675 case R_NDS32_HI20:
fbaf61ad
NC
5676 /* We allow an arbitrary number of HI20 relocs before the
5677 LO12 reloc. This permits gcc to emit the HI and LO relocs
5678 itself. */
5679 for (lorel = rel + 1;
5680 (lorel < relend
5681 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5682 continue;
5683 if (lorel < relend
5684 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5685 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5686 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5687 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
35c08157 5688 {
fbaf61ad
NC
5689 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5690 contents, relocation + addend);
5691 r = bfd_reloc_ok;
35c08157 5692 }
fbaf61ad
NC
5693 else
5694 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5695 contents, offset, relocation,
5696 addend);
35c08157
KLC
5697 goto check_reloc;
5698
5699 case R_NDS32_GOT17S2_RELA:
5700 case R_NDS32_GOT15S2_RELA:
fbaf61ad
NC
5701 BFD_ASSERT (sgot != NULL);
5702
5703 if (h != NULL)
35c08157 5704 {
fbaf61ad 5705 bfd_boolean dyn;
35c08157 5706
fbaf61ad
NC
5707 off = h->got.offset;
5708 BFD_ASSERT (off != (bfd_vma) - 1);
35c08157 5709
fbaf61ad
NC
5710 dyn = htab->root.dynamic_sections_created;
5711 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5712 (dyn, bfd_link_pic (info), h)
5713 || (bfd_link_pic (info)
5714 && (info->symbolic
5715 || h->dynindx == -1
5716 || h->forced_local)
5717 && h->def_regular))
35c08157 5718 {
fbaf61ad
NC
5719 /* This is actually a static link, or it is a
5720 -Bsymbolic link and the symbol is defined
5721 locally, or the symbol was forced to be local
5722 because of a version file. We must initialize
5723 this entry in the global offset table. Since the
5724 offset must always be a multiple of 4, we use the
5725 least significant bit to record whether we have
5726 initialized it already.
5727
5728 When doing a dynamic link, we create a .rela.got
5729 relocation entry to initialize the value. This
5730 is done in the finish_dynamic_symbol routine. */
5731 if ((off & 1) != 0)
5732 off &= ~1;
5733 else
35c08157 5734 {
fbaf61ad
NC
5735 bfd_put_32 (output_bfd, relocation,
5736 sgot->contents + off);
5737 h->got.offset |= 1;
35c08157
KLC
5738 }
5739 }
fbaf61ad
NC
5740 }
5741 else
5742 {
5743 bfd_byte *loc;
35c08157 5744
fbaf61ad
NC
5745 BFD_ASSERT (local_got_offsets != NULL
5746 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
35c08157 5747
fbaf61ad 5748 off = local_got_offsets[r_symndx];
35c08157 5749
fbaf61ad
NC
5750 /* The offset must always be a multiple of 4. We use
5751 the least significant bit to record whether we have
5752 already processed this entry. */
5753 if ((off & 1) != 0)
5754 off &= ~1;
5755 else
5756 {
5757 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5758
5759 if (bfd_link_pic (info))
35c08157 5760 {
fbaf61ad
NC
5761 asection *srelgot;
5762 Elf_Internal_Rela outrel;
35c08157 5763
fbaf61ad
NC
5764 /* We need to generate a R_NDS32_RELATIVE reloc
5765 for the dynamic linker. */
5766 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5767 BFD_ASSERT (srelgot != NULL);
5768
5769 outrel.r_offset = (elf_gp (output_bfd)
5770 + sgot->output_offset + off);
5771 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5772 outrel.r_addend = relocation;
5773 loc = srelgot->contents;
5774 loc +=
5775 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5776 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5777 ++srelgot->reloc_count;
35c08157 5778 }
fbaf61ad 5779 local_got_offsets[r_symndx] |= 1;
35c08157 5780 }
35c08157 5781 }
fbaf61ad
NC
5782 relocation = sgot->output_section->vma + sgot->output_offset + off
5783 - elf_gp (output_bfd);
5784
35c08157
KLC
5785 if (relocation & align)
5786 {
5787 /* Incorrect alignment. */
4eca0228 5788 _bfd_error_handler
38f14ab8 5789 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
35c08157
KLC
5790 ret = FALSE;
5791 r = bfd_reloc_dangerous;
5792 goto check_reloc;
5793 }
5794 break;
5795
5796 case R_NDS32_SDA16S3_RELA:
5797 case R_NDS32_SDA15S3_RELA:
5798 case R_NDS32_SDA15S3:
5799 align = 0x7;
5800 goto handle_sda;
5801
5802 case R_NDS32_SDA17S2_RELA:
5803 case R_NDS32_SDA15S2_RELA:
5804 case R_NDS32_SDA12S2_SP_RELA:
5805 case R_NDS32_SDA12S2_DP_RELA:
5806 case R_NDS32_SDA15S2:
5807 case R_NDS32_SDA_FP7U2_RELA:
5808 align = 0x3;
5809 goto handle_sda;
5810
5811 case R_NDS32_SDA18S1_RELA:
5812 case R_NDS32_SDA15S1_RELA:
5813 case R_NDS32_SDA15S1:
5814 align = 0x1;
5815 goto handle_sda;
5816
5817 case R_NDS32_SDA19S0_RELA:
5818 case R_NDS32_SDA15S0_RELA:
5819 case R_NDS32_SDA15S0:
fbaf61ad 5820 align = 0x0;
dc1e8a47 5821 handle_sda:
fbaf61ad 5822 BFD_ASSERT (sec != NULL);
35c08157 5823
fbaf61ad
NC
5824 /* If the symbol is in the abs section, the out_bfd will be null.
5825 This happens when the relocation has a symbol@GOTOFF. */
5826 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5827 if (r != bfd_reloc_ok)
5828 {
5829 _bfd_error_handler
5830 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5831 ret = FALSE;
5832 goto check_reloc;
5833 }
35c08157 5834
fbaf61ad
NC
5835 /* At this point `relocation' contains the object's
5836 address. */
5837 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5838 {
5839 relocation -= fpbase_addr;
5840 }
5841 else
5842 relocation -= gp;
5843 /* Now it contains the offset from _SDA_BASE_. */
35c08157 5844
fbaf61ad 5845 /* Make sure alignment is correct. */
35c08157 5846
fbaf61ad
NC
5847 if (relocation & align)
5848 {
5849 /* Incorrect alignment. */
5850 _bfd_error_handler
5851 /* xgettext:c-format */
5852 (_("%pB(%pA): warning: unaligned small data access"
5853 " of type %d"),
5854 input_bfd, input_section, r_type);
5855 ret = FALSE;
5856 goto check_reloc;
35c08157 5857 }
35c08157 5858 break;
fbaf61ad 5859
35c08157
KLC
5860 case R_NDS32_17IFC_PCREL_RELA:
5861 case R_NDS32_10IFCU_PCREL_RELA:
fbaf61ad 5862 /* Do nothing. */
35c08157
KLC
5863 break;
5864
1c8f6a4d
KLC
5865 case R_NDS32_TLS_LE_HI20:
5866 case R_NDS32_TLS_LE_LO12:
5867 case R_NDS32_TLS_LE_20:
5868 case R_NDS32_TLS_LE_15S0:
5869 case R_NDS32_TLS_LE_15S1:
5870 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
5871 /* We do not have garbage collection for got entries.
5872 Therefore, IE to LE may have one empty entry, and DESC to
5873 LE may have two. */
1c8f6a4d
KLC
5874 if (elf_hash_table (info)->tls_sec != NULL)
5875 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5876 break;
fbaf61ad 5877
1c8f6a4d
KLC
5878 case R_NDS32_TLS_IE_HI20:
5879 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
5880 case R_NDS32_TLS_DESC_HI20:
5881 case R_NDS32_TLS_DESC_LO12:
5882 case R_NDS32_TLS_IE_LO12:
5883 case R_NDS32_TLS_IEGP_HI20:
5884 case R_NDS32_TLS_IEGP_LO12:
5885 case R_NDS32_TLS_IEGP_LO12S2:
1c8f6a4d
KLC
5886 {
5887 /* Relocation is to the entry for this symbol in the global
5888 offset table. */
fbaf61ad 5889 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
1c8f6a4d
KLC
5890 asection *srelgot;
5891 Elf_Internal_Rela outrel;
1c8f6a4d
KLC
5892 bfd_byte *loc;
5893 int indx = 0;
5894
fbaf61ad
NC
5895 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5896
1c8f6a4d
KLC
5897 BFD_ASSERT (sgot != NULL);
5898 if (h != NULL)
5899 {
5900 bfd_boolean dyn;
5901
5902 off = h->got.offset;
fbaf61ad 5903 BFD_ASSERT (off != (bfd_vma) -1);
1c8f6a4d
KLC
5904 dyn = htab->root.dynamic_sections_created;
5905 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
0e1862bb
L
5906 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5907 && (!bfd_link_pic (info)
1c8f6a4d
KLC
5908 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5909 indx = h->dynindx;
5910 }
5911 else
5912 {
1c8f6a4d
KLC
5913 BFD_ASSERT (local_got_offsets != NULL
5914 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
1c8f6a4d 5915 off = local_got_offsets[r_symndx];
1c8f6a4d
KLC
5916 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5917 }
fbaf61ad 5918
1c8f6a4d
KLC
5919 relocation = sgot->output_section->vma + sgot->output_offset + off;
5920
fbaf61ad
NC
5921 if (1 < ones32 (tls_type))
5922 {
5923 eff_tls_type = 1 << (fls (tls_type) - 1);
5924 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5925
5926 /* TLS model X -> LE is not implement yet!
5927 workaround here! */
5928 if (eff_tls_type == GOT_TLS_LE)
5929 {
5930 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5931 }
5932 }
1c8f6a4d
KLC
5933
5934 /* The offset must always be a multiple of 4. We use
5935 the least significant bit to record whether we have
5936 already processed this entry. */
fbaf61ad
NC
5937 bfd_boolean need_relocs = FALSE;
5938 srelgot = ehtab->srelgot;
5939 if ((bfd_link_pic (info) || indx != 0)
5940 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5941 || h->root.type != bfd_link_hash_undefweak))
5942 {
5943 need_relocs = TRUE;
5944 BFD_ASSERT (srelgot != NULL);
5945 }
5946
5947 if (off & 1)
5948 {
5949 off &= ~1;
5950 relocation &= ~1;
5951
5952 if (eff_tls_type & GOT_TLS_DESC)
5953 {
5954 relocation -= elf_gp (output_bfd);
5955 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5956 {
5957 /* TLS model shall be converted. */
5958 BFD_ASSERT(0);
5959 }
5960 }
5961 else if (eff_tls_type & GOT_TLS_IEGP)
5962 {
5963 relocation -= elf_gp (output_bfd);
5964 }
5965 }
1c8f6a4d
KLC
5966 else
5967 {
fbaf61ad 5968 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
1c8f6a4d 5969 {
fbaf61ad
NC
5970 /* TLS model workaround shall be applied. */
5971 BFD_ASSERT(0);
1c8f6a4d 5972 }
fbaf61ad 5973 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
1c8f6a4d 5974 {
fbaf61ad
NC
5975 if (eff_tls_type & GOT_TLS_IEGP)
5976 relocation -= elf_gp(output_bfd);
5977
1c8f6a4d
KLC
5978 if (need_relocs)
5979 {
fbaf61ad
NC
5980 if (indx == 0)
5981 outrel.r_addend = gottpoff (info, relocation_sym);
1c8f6a4d
KLC
5982 else
5983 outrel.r_addend = 0;
5984 outrel.r_offset = (sgot->output_section->vma
fbaf61ad
NC
5985 + sgot->output_offset + off);
5986 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5987
5988 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5989 &outrel);
1c8f6a4d
KLC
5990 }
5991 else
fbaf61ad
NC
5992 {
5993 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5994 sgot->contents + off);
5995 }
1c8f6a4d 5996 }
fbaf61ad
NC
5997 else if (eff_tls_type & GOT_TLS_DESC)
5998 {
5999 relocation -= elf_gp (output_bfd);
6000 if (need_relocs)
6001 {
6002 if (indx == 0)
6003 outrel.r_addend = gottpoff (info, relocation_sym);
6004 else
6005 outrel.r_addend = 0;
6006 outrel.r_offset = (sgot->output_section->vma
6007 + sgot->output_offset + off);
6008 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
6009
6010 if (htab->tls_desc_trampoline)
6011 {
6012 asection *srelplt;
6013 srelplt = ehtab->srelplt;
6014 loc = srelplt->contents;
6015 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
6016 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
6017 <= srelplt->contents + srelplt->size);
6018
6019 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6020 }
6021 else
6022 {
6023 loc = srelgot->contents;
6024 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
6025 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
6026 ++srelgot->reloc_count;
6027 }
6028 }
6029 else
6030 {
6031 /* feed me! */
6032 bfd_put_32 (output_bfd, 0xdeadbeef,
6033 sgot->contents + off);
6034 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
6035 sgot->contents + off + 4);
6036 patch_tls_desc_to_ie (contents, rel, input_bfd);
6037 BFD_ASSERT(0);
6038 }
6039 }
6040 else
6041 {
6042 /* TLS model workaround shall be applied. */
6043 BFD_ASSERT(0);
6044 }
6045
6046 if (h != NULL)
6047 h->got.offset |= 1;
6048 else
6049 local_got_offsets[r_symndx] |= 1;
6050 }
6051 }
6052 break;
6053 /* DON'T fall through. */
6054
6055 default:
6056 /* OLD_NDS32_RELOC. */
6057
6058 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6059 contents, offset, relocation, addend);
6060 goto check_reloc;
6061 }
6062
6063 switch ((int) r_type)
6064 {
35c08157
KLC
6065 case R_NDS32_20_RELA:
6066 case R_NDS32_5_RELA:
6067 case R_NDS32_9_PCREL_RELA:
6068 case R_NDS32_WORD_9_PCREL_RELA:
6069 case R_NDS32_10_UPCREL_RELA:
6070 case R_NDS32_15_PCREL_RELA:
6071 case R_NDS32_17_PCREL_RELA:
6072 case R_NDS32_25_PCREL_RELA:
6073 case R_NDS32_25_ABS_RELA:
6074 case R_NDS32_HI20_RELA:
6075 case R_NDS32_LO12S3_RELA:
6076 case R_NDS32_LO12S2_RELA:
6077 case R_NDS32_LO12S2_DP_RELA:
6078 case R_NDS32_LO12S2_SP_RELA:
6079 case R_NDS32_LO12S1_RELA:
6080 case R_NDS32_LO12S0_RELA:
6081 case R_NDS32_LO12S0_ORI_RELA:
6082 case R_NDS32_SDA16S3_RELA:
6083 case R_NDS32_SDA17S2_RELA:
6084 case R_NDS32_SDA18S1_RELA:
6085 case R_NDS32_SDA19S0_RELA:
6086 case R_NDS32_SDA15S3_RELA:
6087 case R_NDS32_SDA15S2_RELA:
6088 case R_NDS32_SDA12S2_DP_RELA:
6089 case R_NDS32_SDA12S2_SP_RELA:
6090 case R_NDS32_SDA15S1_RELA:
6091 case R_NDS32_SDA15S0_RELA:
6092 case R_NDS32_SDA_FP7U2_RELA:
6093 case R_NDS32_9_PLTREL:
6094 case R_NDS32_25_PLTREL:
6095 case R_NDS32_GOT20:
6096 case R_NDS32_GOT_HI20:
6097 case R_NDS32_GOT_LO12:
6098 case R_NDS32_GOT_LO15:
6099 case R_NDS32_GOT_LO19:
6100 case R_NDS32_GOT15S2_RELA:
6101 case R_NDS32_GOT17S2_RELA:
6102 case R_NDS32_GOTPC20:
6103 case R_NDS32_GOTPC_HI20:
6104 case R_NDS32_GOTPC_LO12:
6105 case R_NDS32_GOTOFF:
6106 case R_NDS32_GOTOFF_HI20:
6107 case R_NDS32_GOTOFF_LO12:
6108 case R_NDS32_GOTOFF_LO15:
6109 case R_NDS32_GOTOFF_LO19:
6110 case R_NDS32_PLTREL_HI20:
6111 case R_NDS32_PLTREL_LO12:
6112 case R_NDS32_PLT_GOTREL_HI20:
6113 case R_NDS32_PLT_GOTREL_LO12:
6114 case R_NDS32_PLT_GOTREL_LO15:
6115 case R_NDS32_PLT_GOTREL_LO19:
6116 case R_NDS32_PLT_GOTREL_LO20:
6117 case R_NDS32_17IFC_PCREL_RELA:
6118 case R_NDS32_10IFCU_PCREL_RELA:
1c8f6a4d
KLC
6119 case R_NDS32_TLS_LE_HI20:
6120 case R_NDS32_TLS_LE_LO12:
6121 case R_NDS32_TLS_IE_HI20:
6122 case R_NDS32_TLS_IE_LO12S2:
6123 case R_NDS32_TLS_LE_20:
6124 case R_NDS32_TLS_LE_15S0:
6125 case R_NDS32_TLS_LE_15S1:
6126 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
6127 case R_NDS32_TLS_DESC_HI20:
6128 case R_NDS32_TLS_DESC_LO12:
6129 case R_NDS32_TLS_IE_LO12:
6130 case R_NDS32_TLS_IEGP_HI20:
6131 case R_NDS32_TLS_IEGP_LO12:
6132 case R_NDS32_TLS_IEGP_LO12S2:
35c08157 6133 /* Instruction related relocs must handle endian properly. */
1c8f6a4d 6134 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
35c08157
KLC
6135 r = nds32_elf_final_link_relocate (howto, input_bfd,
6136 input_section, contents,
6137 rel->r_offset, relocation,
6138 rel->r_addend);
6139 break;
6140
6141 default:
6142 /* All other relocs can use default handler. */
6143 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6144 contents, rel->r_offset,
6145 relocation, rel->r_addend);
6146 break;
6147 }
6148
dc1e8a47 6149 check_reloc:
35c08157
KLC
6150
6151 if (r != bfd_reloc_ok)
6152 {
6153 /* FIXME: This should be generic enough to go in a utility. */
6154 const char *name;
6155
6156 if (h != NULL)
6157 name = h->root.root.string;
6158 else
6159 {
6160 name = bfd_elf_string_from_elf_section
6161 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6162 if (name == NULL || *name == '\0')
fd361982 6163 name = bfd_section_name (sec);
35c08157
KLC
6164 }
6165
6166 if (errmsg != NULL)
6167 goto common_error;
6168
6169 switch (r)
6170 {
6171 case bfd_reloc_overflow:
1a72702b
AM
6172 (*info->callbacks->reloc_overflow)
6173 (info, (h ? &h->root : NULL), name, howto->name,
6174 (bfd_vma) 0, input_bfd, input_section, offset);
35c08157
KLC
6175 break;
6176
6177 case bfd_reloc_undefined:
1a72702b
AM
6178 (*info->callbacks->undefined_symbol)
6179 (info, name, input_bfd, input_section, offset, TRUE);
35c08157
KLC
6180 break;
6181
6182 case bfd_reloc_outofrange:
6183 errmsg = _("internal error: out of range error");
6184 goto common_error;
6185
6186 case bfd_reloc_notsupported:
6187 errmsg = _("internal error: unsupported relocation error");
6188 goto common_error;
6189
6190 case bfd_reloc_dangerous:
6191 errmsg = _("internal error: dangerous error");
6192 goto common_error;
6193
6194 default:
6195 errmsg = _("internal error: unknown error");
6196 /* Fall through. */
6197
dc1e8a47 6198 common_error:
1a72702b
AM
6199 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6200 input_section, offset);
35c08157
KLC
6201 break;
6202 }
6203 }
6204 }
6205
fbaf61ad
NC
6206 /* Resotre header size to avoid overflow load. */
6207 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6208 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6209
35c08157
KLC
6210 return ret;
6211}
6212
6213/* Finish up dynamic symbol handling. We set the contents of various
6214 dynamic sections here. */
6215
6216static bfd_boolean
6217nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6218 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6219{
fbaf61ad
NC
6220 struct elf_link_hash_table *ehtab;
6221 struct elf_nds32_link_hash_entry *hent;
35c08157
KLC
6222 bfd_byte *loc;
6223
fbaf61ad
NC
6224 ehtab = elf_hash_table (info);
6225 hent = (struct elf_nds32_link_hash_entry *) h;
35c08157
KLC
6226
6227 if (h->plt.offset != (bfd_vma) - 1)
6228 {
6229 asection *splt;
6230 asection *sgot;
6231 asection *srela;
6232
6233 bfd_vma plt_index;
6234 bfd_vma got_offset;
6235 bfd_vma local_plt_offset;
6236 Elf_Internal_Rela rela;
6237
6238 /* This symbol has an entry in the procedure linkage table. Set
6239 it up. */
6240
6241 BFD_ASSERT (h->dynindx != -1);
6242
fbaf61ad
NC
6243 splt = ehtab->splt;
6244 sgot = ehtab->sgotplt;
6245 srela = ehtab->srelplt;
35c08157
KLC
6246 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6247
6248 /* Get the index in the procedure linkage table which
6249 corresponds to this symbol. This is the index of this symbol
6250 in all the symbols for which we are making plt entries. The
6251 first entry in the procedure linkage table is reserved. */
6252 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6253
6254 /* Get the offset into the .got table of the entry that
6255 corresponds to this function. Each .got entry is 4 bytes.
6256 The first three are reserved. */
6257 got_offset = (plt_index + 3) * 4;
6258
6259 /* Fill in the entry in the procedure linkage table. */
0e1862bb 6260 if (!bfd_link_pic (info))
35c08157
KLC
6261 {
6262 unsigned long insn;
6263
6264 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6265 + sgot->output_offset + got_offset) >> 12)
6266 & 0xfffff);
6267 bfd_putb32 (insn, splt->contents + h->plt.offset);
6268
6269 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6270 + sgot->output_offset + got_offset) & 0x0fff)
6271 >> 2);
6272 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6273
6274 insn = PLT_ENTRY_WORD2;
6275 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6276
6277 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6278 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6279
6280 insn = PLT_ENTRY_WORD4
6281 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6282 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6283 local_plt_offset = 12;
6284 }
6285 else
6286 {
6287 /* sda_base must be set at this time. */
6288 unsigned long insn;
6289 long offset;
6290
35c08157
KLC
6291 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6292 - elf_gp (output_bfd);
6293 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6294 bfd_putb32 (insn, splt->contents + h->plt.offset);
6295
6296 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6297 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6298
6299 insn = PLT_PIC_ENTRY_WORD2;
6300 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6301
6302 insn = PLT_PIC_ENTRY_WORD3;
6303 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6304
6305 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6306 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6307
6308 insn = PLT_PIC_ENTRY_WORD5
6309 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6310 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6311
6312 local_plt_offset = 16;
6313 }
6314
6315 /* Fill in the entry in the global offset table,
6316 so it will fall through to the next instruction for the first time. */
6317 bfd_put_32 (output_bfd,
6318 (splt->output_section->vma + splt->output_offset
6319 + h->plt.offset + local_plt_offset),
6320 sgot->contents + got_offset);
6321
6322 /* Fill in the entry in the .rela.plt section. */
6323 rela.r_offset = (sgot->output_section->vma
6324 + sgot->output_offset + got_offset);
6325 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6326 rela.r_addend = 0;
6327 loc = srela->contents;
6328 loc += plt_index * sizeof (Elf32_External_Rela);
6329 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6330
6331 if (!h->def_regular)
6332 {
6333 /* Mark the symbol as undefined, rather than as defined in
6334 the .plt section. Leave the value alone. */
6335 sym->st_shndx = SHN_UNDEF;
6336 if (!h->ref_regular_nonweak)
6337 sym->st_value = 0;
6338 }
6339 }
6340
fbaf61ad
NC
6341 if (h->got.offset != (bfd_vma) - 1
6342 && hent->tls_type == GOT_NORMAL)
35c08157
KLC
6343 {
6344 asection *sgot;
fbaf61ad 6345 asection *srelagot;
35c08157
KLC
6346 Elf_Internal_Rela rela;
6347
6348 /* This symbol has an entry in the global offset table.
6349 Set it up. */
6350
fbaf61ad
NC
6351 sgot = ehtab->sgot;
6352 srelagot = ehtab->srelgot;
6353 BFD_ASSERT (sgot != NULL && srelagot != NULL);
35c08157
KLC
6354
6355 rela.r_offset = (sgot->output_section->vma
6356 + sgot->output_offset + (h->got.offset & ~1));
6357
6358 /* If this is a -Bsymbolic link, and the symbol is defined
6359 locally, we just want to emit a RELATIVE reloc. Likewise if
6360 the symbol was forced to be local because of a version file.
6361 The entry in the global offset table will already have been
6362 initialized in the relocate_section function. */
fbaf61ad
NC
6363 if ((bfd_link_pic (info)
6364 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6365 && h->def_regular)
6366 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
6367 {
6368 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6369 rela.r_addend = (h->root.u.def.value
6370 + h->root.u.def.section->output_section->vma
6371 + h->root.u.def.section->output_offset);
fbaf61ad
NC
6372
6373 if ((h->got.offset & 1) == 0)
6374 {
6375 bfd_put_32 (output_bfd, rela.r_addend,
6376 sgot->contents + h->got.offset);
6377 }
35c08157
KLC
6378 }
6379 else
6380 {
6381 BFD_ASSERT ((h->got.offset & 1) == 0);
6382 bfd_put_32 (output_bfd, (bfd_vma) 0,
6383 sgot->contents + h->got.offset);
6384 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6385 rela.r_addend = 0;
6386 }
6387
fbaf61ad
NC
6388 loc = srelagot->contents;
6389 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
35c08157 6390 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
fbaf61ad
NC
6391 ++srelagot->reloc_count;
6392 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
35c08157
KLC
6393 }
6394
6395 if (h->needs_copy)
6396 {
6397 asection *s;
6398 Elf_Internal_Rela rela;
6399
6400 /* This symbols needs a copy reloc. Set it up. */
6401
6402 BFD_ASSERT (h->dynindx != -1
6403 && (h->root.type == bfd_link_hash_defined
6404 || h->root.type == bfd_link_hash_defweak));
6405
6406 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6407 BFD_ASSERT (s != NULL);
6408
6409 rela.r_offset = (h->root.u.def.value
6410 + h->root.u.def.section->output_section->vma
6411 + h->root.u.def.section->output_offset);
6412 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6413 rela.r_addend = 0;
6414 loc = s->contents;
6415 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6416 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6417 ++s->reloc_count;
6418 }
6419
6420 /* Mark some specially defined symbols as absolute. */
6421 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6422 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6423 sym->st_shndx = SHN_ABS;
6424
6425 return TRUE;
6426}
6427
6428
6429/* Finish up the dynamic sections. */
6430
6431static bfd_boolean
6432nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6433{
35c08157
KLC
6434 bfd *dynobj;
6435 asection *sdyn;
fbaf61ad
NC
6436 asection *sgotplt;
6437 struct elf_link_hash_table *ehtab;
6438 struct elf_nds32_link_hash_table *htab;
35c08157 6439
fbaf61ad 6440 ehtab = elf_hash_table (info);
35c08157 6441 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
6442 if (htab == NULL)
6443 return FALSE;
6444
6445 dynobj = elf_hash_table (info)->dynobj;
35c08157 6446
fbaf61ad
NC
6447 sgotplt = ehtab->sgotplt;
6448 /* A broken linker script might have discarded the dynamic sections.
6449 Catch this here so that we do not seg-fault later on. */
6450 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6451 return FALSE;
35c08157
KLC
6452 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6453
fbaf61ad 6454 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
6455 {
6456 asection *splt;
6457 Elf32_External_Dyn *dyncon, *dynconend;
6458
fbaf61ad 6459 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
35c08157
KLC
6460
6461 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6462 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6463
6464 for (; dyncon < dynconend; dyncon++)
6465 {
6466 Elf_Internal_Dyn dyn;
6467 asection *s;
6468
6469 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6470
6471 switch (dyn.d_tag)
6472 {
6473 default:
6474 break;
6475
6476 case DT_PLTGOT:
fbaf61ad
NC
6477 /* name = ".got"; */
6478 s = ehtab->sgot->output_section;
35c08157
KLC
6479 goto get_vma;
6480 case DT_JMPREL:
fbaf61ad 6481 s = ehtab->srelplt->output_section;
dc1e8a47 6482 get_vma:
fbaf61ad
NC
6483 BFD_ASSERT (s != NULL);
6484 dyn.d_un.d_ptr = s->vma;
35c08157
KLC
6485 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6486 break;
6487
6488 case DT_PLTRELSZ:
fbaf61ad
NC
6489 s = ehtab->srelplt->output_section;
6490 BFD_ASSERT (s != NULL);
35c08157
KLC
6491 dyn.d_un.d_val = s->size;
6492 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6493 break;
fbaf61ad
NC
6494
6495 case DT_RELASZ:
6496 /* My reading of the SVR4 ABI indicates that the
6497 procedure linkage table relocs (DT_JMPREL) should be
6498 included in the overall relocs (DT_RELA). This is
6499 what Solaris does. However, UnixWare can not handle
6500 that case. Therefore, we override the DT_RELASZ entry
6501 here to make it not include the JMPREL relocs. Since
6502 the linker script arranges for .rela.plt to follow all
6503 other relocation sections, we don't have to worry
6504 about changing the DT_RELA entry. */
6505 if (ehtab->srelplt != NULL)
6506 {
6507 s = ehtab->srelplt->output_section;
6508 dyn.d_un.d_val -= s->size;
6509 }
6510 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6511 break;
6512
6513 case DT_TLSDESC_PLT:
6514 s = htab->root.splt;
6515 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6516 + htab->dt_tlsdesc_plt);
6517 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6518 break;
6519
6520 case DT_TLSDESC_GOT:
6521 s = htab->root.sgot;
6522 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6523 + htab->dt_tlsdesc_got);
6524 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6525 break;
35c08157
KLC
6526 }
6527 }
6528
6529 /* Fill in the first entry in the procedure linkage table. */
fbaf61ad 6530 splt = ehtab->splt;
35c08157
KLC
6531 if (splt && splt->size > 0)
6532 {
0e1862bb 6533 if (bfd_link_pic (info))
35c08157
KLC
6534 {
6535 unsigned long insn;
6536 long offset;
6537
fbaf61ad
NC
6538 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6539 - elf_gp (output_bfd);
35c08157
KLC
6540 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6541 bfd_putb32 (insn, splt->contents);
6542
35c08157
KLC
6543 /* here has a typo? */
6544 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6545 bfd_putb32 (insn, splt->contents + 4);
6546
6547 insn = PLT0_PIC_ENTRY_WORD2;
6548 bfd_putb32 (insn, splt->contents + 8);
6549
6550 insn = PLT0_PIC_ENTRY_WORD3;
6551 bfd_putb32 (insn, splt->contents + 12);
6552
6553 insn = PLT0_PIC_ENTRY_WORD4;
6554 bfd_putb32 (insn, splt->contents + 16);
6555
6556 insn = PLT0_PIC_ENTRY_WORD5;
6557 bfd_putb32 (insn, splt->contents + 20);
6558 }
6559 else
6560 {
6561 unsigned long insn;
6562 unsigned long addr;
6563
6564 /* addr = .got + 4 */
fbaf61ad 6565 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
35c08157
KLC
6566 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6567 bfd_putb32 (insn, splt->contents);
6568
6569 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6570 bfd_putb32 (insn, splt->contents + 4);
6571
6572 insn = PLT0_ENTRY_WORD2;
6573 bfd_putb32 (insn, splt->contents + 8);
6574
6575 insn = PLT0_ENTRY_WORD3;
6576 bfd_putb32 (insn, splt->contents + 12);
6577
6578 insn = PLT0_ENTRY_WORD4;
6579 bfd_putb32 (insn, splt->contents + 16);
6580 }
6581
6582 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6583 PLT_ENTRY_SIZE;
6584 }
fbaf61ad
NC
6585
6586 if (htab->dt_tlsdesc_plt)
6587 {
6588 /* Calculate addresses. */
6589 asection *sgot = sgot = ehtab->sgot;
6590 bfd_vma pltgot = sgotplt->output_section->vma
6591 + sgotplt->output_offset;
6592 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6593 + htab->dt_tlsdesc_got;
6594
6595 /* Get GP offset. */
6596 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6597 tlsdesc_got -= elf_gp (output_bfd);
6598
6599 /* Do relocation. */
6600 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6601 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6602 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6603 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6604
6605 /* Insert .plt. */
6606 nds32_put_trampoline (splt->contents + htab->dt_tlsdesc_plt,
6607 dl_tlsdesc_lazy_trampoline,
6608 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6609 }
35c08157
KLC
6610 }
6611
6612 /* Fill in the first three entries in the global offset table. */
fbaf61ad 6613 if (sgotplt && sgotplt->size > 0)
35c08157
KLC
6614 {
6615 if (sdyn == NULL)
fbaf61ad 6616 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
35c08157
KLC
6617 else
6618 bfd_put_32 (output_bfd,
6619 sdyn->output_section->vma + sdyn->output_offset,
fbaf61ad
NC
6620 sgotplt->contents);
6621 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6622 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
35c08157 6623
fbaf61ad 6624 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
35c08157
KLC
6625 }
6626
6627 return TRUE;
6628}
6629\f
6630
6631/* Set the right machine number. */
6632
6633static bfd_boolean
6634nds32_elf_object_p (bfd *abfd)
6635{
6636 static unsigned int cur_arch = 0;
6637
6638 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6639 {
6640 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6641 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6642 }
6643
6644 switch (cur_arch)
6645 {
6646 default:
6647 case E_N1_ARCH:
6648 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6649 break;
6650 case E_N1H_ARCH:
6651 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6652 break;
6653 case E_NDS_ARCH_STAR_V2_0:
6654 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6655 break;
6656 case E_NDS_ARCH_STAR_V3_0:
6657 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6658 break;
6659 case E_NDS_ARCH_STAR_V3_M:
6660 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6661 break;
6662 }
6663
6664 return TRUE;
6665}
6666
6667/* Store the machine number in the flags field. */
6668
cc364be6
AM
6669static bfd_boolean
6670nds32_elf_final_write_processing (bfd *abfd)
35c08157
KLC
6671{
6672 unsigned long val;
6673 static unsigned int cur_mach = 0;
6674
6675 if (bfd_mach_n1 != bfd_get_mach (abfd))
6676 {
6677 cur_mach = bfd_get_mach (abfd);
6678 }
6679
6680 switch (cur_mach)
6681 {
6682 case bfd_mach_n1:
6683 /* Only happen when object is empty, since the case is abandon. */
6684 val = E_N1_ARCH;
6685 val |= E_NDS_ABI_AABI;
6686 val |= E_NDS32_ELF_VER_1_4;
6687 break;
6688 case bfd_mach_n1h:
6689 val = E_N1H_ARCH;
6690 break;
6691 case bfd_mach_n1h_v2:
6692 val = E_NDS_ARCH_STAR_V2_0;
6693 break;
6694 case bfd_mach_n1h_v3:
6695 val = E_NDS_ARCH_STAR_V3_0;
6696 break;
6697 case bfd_mach_n1h_v3m:
6698 val = E_NDS_ARCH_STAR_V3_M;
6699 break;
6700 default:
6701 val = 0;
6702 break;
6703 }
6704
6705 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6706 elf_elfheader (abfd)->e_flags |= val;
cc364be6 6707 return _bfd_elf_final_write_processing (abfd);
35c08157
KLC
6708}
6709
6710/* Function to keep NDS32 specific file flags. */
6711
6712static bfd_boolean
6713nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6714{
6715 BFD_ASSERT (!elf_flags_init (abfd)
6716 || elf_elfheader (abfd)->e_flags == flags);
6717
6718 elf_elfheader (abfd)->e_flags = flags;
6719 elf_flags_init (abfd) = TRUE;
6720 return TRUE;
6721}
6722
6723static unsigned int
6724convert_e_flags (unsigned int e_flags, unsigned int arch)
6725{
6726 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6727 {
6728 /* From 0.9 to 1.0. */
6729 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6730
6731 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6732 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6733 if (arch == E_NDS_ARCH_STAR_V1_0)
6734 {
6735 /* Done. */
6736 return e_flags;
6737 }
6738 }
6739
6740 /* From 1.0 to 2.0. */
6741 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6742
6743 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6744 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6745
6746 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6747 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6748 return e_flags;
6749}
6750
6751static bfd_boolean
6752nds32_check_vec_size (bfd *ibfd)
6753{
6754 static unsigned int nds32_vec_size = 0;
6755
6756 asection *sec_t = NULL;
6757 bfd_byte *contents = NULL;
6758
6759 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6760
6761 if (sec_t && sec_t->size >= 4)
6762 {
6763 /* Get vec_size in file. */
6764 unsigned int flag_t;
6765
0c4bd9d9 6766 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
35c08157
KLC
6767 flag_t = bfd_get_32 (ibfd, contents);
6768
6769 /* The value could only be 4 or 16. */
6770
6771 if (!nds32_vec_size)
6772 /* Set if not set yet. */
6773 nds32_vec_size = (flag_t & 0x3);
6774 else if (nds32_vec_size != (flag_t & 0x3))
6775 {
4eca0228 6776 _bfd_error_handler
695344c0 6777 /* xgettext:c-format */
871b3ab2 6778 (_("%pB: ISR vector size mismatch"
4eca0228
AM
6779 " with previous modules, previous %u-byte, current %u-byte"),
6780 ibfd,
6781 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6782 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
35c08157
KLC
6783 return FALSE;
6784 }
6785 else
6786 /* Only keep the first vec_size section. */
6787 sec_t->flags |= SEC_EXCLUDE;
6788 }
6789
6790 return TRUE;
6791}
6792
6793/* Merge backend specific data from an object file to the output
6794 object file when linking. */
6795
6796static bfd_boolean
50e03d47 6797nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
35c08157 6798{
50e03d47 6799 bfd *obfd = info->output_bfd;
35c08157
KLC
6800 flagword out_flags;
6801 flagword in_flags;
6802 flagword out_16regs;
6803 flagword in_no_mac;
6804 flagword out_no_mac;
6805 flagword in_16regs;
6806 flagword out_version;
6807 flagword in_version;
6808 flagword out_fpu_config;
6809 flagword in_fpu_config;
6810
6b728d32
AM
6811 /* FIXME: What should be checked when linking shared libraries? */
6812 if ((ibfd->flags & DYNAMIC) != 0)
6813 return TRUE;
6814
35c08157
KLC
6815 /* TODO: Revise to use object-attributes instead. */
6816 if (!nds32_check_vec_size (ibfd))
6817 return FALSE;
6818
6819 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6820 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6821 return TRUE;
6822
6823 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6824 {
4eca0228 6825 _bfd_error_handler
38f14ab8 6826 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
35c08157
KLC
6827
6828 bfd_set_error (bfd_error_bad_value);
6829 return FALSE;
6830 }
6831
fbaf61ad
NC
6832 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6833 treat as generic one without checking and merging. */
6834 if (elf_elfheader (ibfd)->e_flags)
35c08157 6835 {
fbaf61ad
NC
6836 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6837 if (in_version == E_NDS32_ELF_VER_1_2)
35c08157 6838 {
fbaf61ad
NC
6839 _bfd_error_handler
6840 (_("%pB: warning: older version of object file encountered, "
6841 "please recompile with current tool chain"), ibfd);
35c08157 6842 }
35c08157 6843
fbaf61ad
NC
6844 /* We may need to merge V1 and V2 arch object files to V2. */
6845 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6846 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
35c08157 6847 {
fbaf61ad
NC
6848 /* Need to convert version. */
6849 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6850 == E_NDS_ARCH_STAR_RESERVED)
6851 {
6852 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6853 }
6854 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6855 == E_NDS_ARCH_STAR_V3_M
6856 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6857 == E_NDS_ARCH_STAR_V3_0)
6858 {
6859 elf_elfheader (ibfd)->e_flags =
6860 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6861 | E_NDS_ARCH_STAR_V3_0;
6862 }
6863 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6864 == E_NDS_ARCH_STAR_V0_9
6865 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6866 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6867 {
6868 elf_elfheader (obfd)->e_flags =
6869 convert_e_flags (elf_elfheader (obfd)->e_flags,
6870 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6871 }
6872 else
6873 {
6874 elf_elfheader (ibfd)->e_flags =
6875 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6876 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6877 }
35c08157
KLC
6878 }
6879
fbaf61ad
NC
6880 /* Extract some flags. */
6881 in_flags = elf_elfheader (ibfd)->e_flags
6882 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6883 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6884
6885 /* The following flags need special treatment. */
6886 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6887 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6888 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6889
6890 /* Extract some flags. */
6891 out_flags = elf_elfheader (obfd)->e_flags
6892 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6893 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6894
6895 /* The following flags need special treatment. */
6896 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6897 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6898 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6899 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6900 if (!elf_flags_init (obfd))
6901 {
6902 /* If the input is the default architecture then do not
6903 bother setting the flags for the output architecture,
6904 instead allow future merges to do this. If no future
6905 merges ever set these flags then they will retain their
6906 unitialised values, which surprise surprise, correspond
6907 to the default values. */
6908 if (bfd_get_arch_info (ibfd)->the_default)
6909 return TRUE;
6910
6911 elf_flags_init (obfd) = TRUE;
6912 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
35c08157 6913
fbaf61ad
NC
6914 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6915 && bfd_get_arch_info (obfd)->the_default)
6916 {
6917 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6918 bfd_get_mach (ibfd));
6919 }
35c08157 6920
fbaf61ad
NC
6921 return TRUE;
6922 }
35c08157 6923
fbaf61ad
NC
6924 /* Check flag compatibility. */
6925 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
35c08157 6926 {
4eca0228 6927 _bfd_error_handler
fbaf61ad 6928 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
35c08157
KLC
6929 bfd_set_error (bfd_error_bad_value);
6930 return FALSE;
6931 }
35c08157 6932
fbaf61ad
NC
6933 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6934 {
6935 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6936 {
6937 _bfd_error_handler
6938 (_("%pB: error: instruction set mismatch with previous modules"),
6939 ibfd);
6940
6941 bfd_set_error (bfd_error_bad_value);
6942 return FALSE;
6943 }
6944 }
6945
6946 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6947 and perf ext1 and DIV are mergerd to perf ext1. */
6948 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6949 {
6950 elf_elfheader (obfd)->e_flags =
6951 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6952 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6953 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6954 ? E_NDS32_HAS_EXT_INST : 0)
6955 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6956 ? E_NDS32_HAS_EXT_INST : 0)
6957 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6958 | ((in_version > out_version) ? out_version : in_version);
6959 }
6960 else
6961 {
6962 if (in_version != out_version)
6963 _bfd_error_handler
6964 /* xgettext:c-format */
6965 (_("%pB: warning: incompatible elf-versions %s and %s"),
6966 ibfd, nds32_elfver_strtab[out_version],
6967 nds32_elfver_strtab[in_version]);
35c08157 6968
fbaf61ad
NC
6969 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6970 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6971 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6972 | (in_version > out_version ? out_version : in_version);
6973 }
35c08157
KLC
6974 }
6975
6976 return TRUE;
6977}
6978
6979/* Display the flags field. */
6980
6981static bfd_boolean
6982nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6983{
6984 FILE *file = (FILE *) ptr;
6985
6986 BFD_ASSERT (abfd != NULL && ptr != NULL);
6987
6988 _bfd_elf_print_private_bfd_data (abfd, ptr);
6989
6990 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6991
6992 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6993 {
6994 default:
6995 case E_N1_ARCH:
6996 fprintf (file, _(": n1 instructions"));
6997 break;
6998 case E_N1H_ARCH:
6999 fprintf (file, _(": n1h instructions"));
7000 break;
7001 }
7002
7003 fputc ('\n', file);
7004
7005 return TRUE;
7006}
7007
7008static unsigned int
7009nds32_elf_action_discarded (asection *sec)
7010{
7011
7012 if (strncmp
7013 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
7014 return 0;
7015
7016 return _bfd_elf_default_action_discarded (sec);
7017}
7018
7019static asection *
7020nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
7021 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
7022 Elf_Internal_Sym *sym)
7023{
7024 if (h != NULL)
7025 switch (ELF32_R_TYPE (rel->r_info))
7026 {
7027 case R_NDS32_GNU_VTINHERIT:
7028 case R_NDS32_GNU_VTENTRY:
7029 case R_NDS32_RELA_GNU_VTINHERIT:
7030 case R_NDS32_RELA_GNU_VTENTRY:
7031 return NULL;
7032 }
7033
7034 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
7035}
7036
fbaf61ad
NC
7037static enum elf_nds32_tls_type
7038get_tls_type (enum elf_nds32_reloc_type r_type,
7039 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7040{
7041 enum elf_nds32_tls_type tls_type;
7042
7043 switch (r_type)
7044 {
7045 case R_NDS32_TLS_LE_HI20:
7046 case R_NDS32_TLS_LE_LO12:
7047 tls_type = GOT_TLS_LE;
7048 break;
7049 case R_NDS32_TLS_IE_HI20:
7050 case R_NDS32_TLS_IE_LO12S2:
7051 case R_NDS32_TLS_IE_LO12:
7052 tls_type = GOT_TLS_IE;
7053 break;
7054 case R_NDS32_TLS_IEGP_HI20:
7055 case R_NDS32_TLS_IEGP_LO12:
7056 case R_NDS32_TLS_IEGP_LO12S2:
7057 tls_type = GOT_TLS_IEGP;
7058 break;
7059 case R_NDS32_TLS_DESC_HI20:
7060 case R_NDS32_TLS_DESC_LO12:
7061 case R_NDS32_TLS_DESC_ADD:
7062 case R_NDS32_TLS_DESC_FUNC:
7063 case R_NDS32_TLS_DESC_CALL:
7064 tls_type = GOT_TLS_DESC;
7065 break;
7066 default:
7067 tls_type = GOT_NORMAL;
7068 break;
7069 }
7070
7071 return tls_type;
7072}
7073
7074/* Ensure that we have allocated bookkeeping structures for ABFD's local
7075 symbols. */
7076
7077static bfd_boolean
7078elf32_nds32_allocate_local_sym_info (bfd *abfd)
7079{
7080 if (elf_local_got_refcounts (abfd) == NULL)
7081 {
7082 bfd_size_type num_syms;
7083 bfd_size_type size;
7084 char *data;
7085
7086 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7087 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7088 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
7089 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7090 + sizeof (bfd_vma) + sizeof (int)
7091 + sizeof (bfd_boolean) + sizeof (bfd_vma));
7092 data = bfd_zalloc (abfd, size);
7093 if (data == NULL)
7094 return FALSE;
7095
7096 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7097 data += num_syms * sizeof (bfd_signed_vma);
7098
7099 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7100 data += num_syms * sizeof (char);
7101
7102 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7103 data += num_syms * sizeof (bfd_vma);
7104
7105 elf32_nds32_local_gp_offset (abfd) = (int *) data;
7106 data += num_syms * sizeof (int);
7107 }
7108
7109 return TRUE;
7110}
7111
35c08157
KLC
7112/* Look through the relocs for a section during the first phase.
7113 Since we don't do .gots or .plts, we just need to consider the
7114 virtual table relocs for gc. */
7115
7116static bfd_boolean
7117nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7118 asection *sec, const Elf_Internal_Rela *relocs)
7119{
7120 Elf_Internal_Shdr *symtab_hdr;
7121 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
7122 const Elf_Internal_Rela *rel;
7123 const Elf_Internal_Rela *rel_end;
fbaf61ad 7124 struct elf_link_hash_table *ehtab;
35c08157
KLC
7125 struct elf_nds32_link_hash_table *htab;
7126 bfd *dynobj;
7127 asection *sreloc = NULL;
7128
fbaf61ad 7129 /* No need for relocation if relocatable already. */
0e1862bb 7130 if (bfd_link_relocatable (info))
fbaf61ad
NC
7131 {
7132 elf32_nds32_check_relax_group (abfd, sec);
7133 return TRUE;
7134 }
35c08157 7135
65281396
AM
7136 /* Don't do anything special with non-loaded, non-alloced sections.
7137 In particular, any relocs in such sections should not affect GOT
7138 and PLT reference counting (ie. we don't allow them to create GOT
7139 or PLT entries), there's no possibility or desire to optimize TLS
7140 relocs, and there's not much point in propagating relocs to shared
7141 libs that the dynamic linker won't relocate. */
7142 if ((sec->flags & SEC_ALLOC) == 0)
7143 return TRUE;
7144
35c08157
KLC
7145 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7146 sym_hashes = elf_sym_hashes (abfd);
7147 sym_hashes_end =
7148 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7149 if (!elf_bad_symtab (abfd))
7150 sym_hashes_end -= symtab_hdr->sh_info;
7151
fbaf61ad 7152 ehtab = elf_hash_table (info);
35c08157
KLC
7153 htab = nds32_elf_hash_table (info);
7154 dynobj = htab->root.dynobj;
7155
7156 rel_end = relocs + sec->reloc_count;
7157 for (rel = relocs; rel < rel_end; rel++)
7158 {
7159 enum elf_nds32_reloc_type r_type;
7160 struct elf_link_hash_entry *h;
7161 unsigned long r_symndx;
fbaf61ad 7162 enum elf_nds32_tls_type tls_type, old_tls_type;
35c08157
KLC
7163
7164 r_symndx = ELF32_R_SYM (rel->r_info);
7165 r_type = ELF32_R_TYPE (rel->r_info);
7166 if (r_symndx < symtab_hdr->sh_info)
7167 h = NULL;
7168 else
7169 {
7170 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7171 while (h->root.type == bfd_link_hash_indirect
7172 || h->root.type == bfd_link_hash_warning)
7173 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7174 }
7175
fbaf61ad
NC
7176 /* Create .got section if necessary.
7177 Some relocs require a global offset table. We create
7178 got section here, since these relocation need a got section
7179 and if it is not created yet. */
7180 if (ehtab->sgot == NULL)
35c08157
KLC
7181 {
7182 switch (r_type)
7183 {
7184 case R_NDS32_GOT_HI20:
7185 case R_NDS32_GOT_LO12:
7186 case R_NDS32_GOT_LO15:
7187 case R_NDS32_GOT_LO19:
7188 case R_NDS32_GOT17S2_RELA:
7189 case R_NDS32_GOT15S2_RELA:
7190 case R_NDS32_GOTOFF:
7191 case R_NDS32_GOTOFF_HI20:
7192 case R_NDS32_GOTOFF_LO12:
7193 case R_NDS32_GOTOFF_LO15:
7194 case R_NDS32_GOTOFF_LO19:
7195 case R_NDS32_GOTPC20:
7196 case R_NDS32_GOTPC_HI20:
7197 case R_NDS32_GOTPC_LO12:
7198 case R_NDS32_GOT20:
1c8f6a4d 7199 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7200 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7201 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7202 case R_NDS32_TLS_IEGP_HI20:
7203 case R_NDS32_TLS_IEGP_LO12:
7204 case R_NDS32_TLS_IEGP_LO12S2:
7205 case R_NDS32_TLS_DESC_HI20:
7206 case R_NDS32_TLS_DESC_LO12:
35c08157
KLC
7207 if (dynobj == NULL)
7208 htab->root.dynobj = dynobj = abfd;
fbaf61ad 7209 if (!create_got_section (dynobj, info))
35c08157
KLC
7210 return FALSE;
7211 break;
7212
7213 default:
7214 break;
7215 }
7216 }
7217
fbaf61ad 7218 /* Check relocation type. */
35c08157
KLC
7219 switch ((int) r_type)
7220 {
7221 case R_NDS32_GOT_HI20:
7222 case R_NDS32_GOT_LO12:
7223 case R_NDS32_GOT_LO15:
7224 case R_NDS32_GOT_LO19:
7225 case R_NDS32_GOT20:
fbaf61ad
NC
7226 case R_NDS32_TLS_LE_HI20:
7227 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 7228 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7229 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7230 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7231 case R_NDS32_TLS_IEGP_HI20:
7232 case R_NDS32_TLS_IEGP_LO12:
7233 case R_NDS32_TLS_IEGP_LO12S2:
7234 case R_NDS32_TLS_DESC_HI20:
7235 case R_NDS32_TLS_DESC_LO12:
7236 tls_type = get_tls_type (r_type, h);
7237 if (h)
1c8f6a4d 7238 {
fbaf61ad
NC
7239 if (tls_type != GOT_TLS_LE)
7240 h->got.refcount += 1;
1c8f6a4d 7241 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
1c8f6a4d 7242 }
35c08157
KLC
7243 else
7244 {
fbaf61ad
NC
7245 /* This is a global offset table entry for a local symbol. */
7246 if (!elf32_nds32_allocate_local_sym_info (abfd))
7247 return FALSE;
35c08157 7248
fbaf61ad
NC
7249 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7250 if (tls_type != GOT_TLS_LE)
7251 elf_local_got_refcounts (abfd)[r_symndx] += 1;
1c8f6a4d 7252 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
35c08157 7253 }
35c08157 7254
fbaf61ad 7255 /* We would already have issued an error message if there
1c8f6a4d
KLC
7256 is a TLS/non-TLS mismatch, based on the symbol
7257 type. So just combine any TLS types needed. */
7258 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7259 && tls_type != GOT_NORMAL)
7260 tls_type |= old_tls_type;
7261
fbaf61ad
NC
7262 /* DESC to IE/IEGP if link to executable. */
7263 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7264 && (bfd_link_executable (info)))
7265 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7266
1c8f6a4d
KLC
7267 if (old_tls_type != tls_type)
7268 {
7269 if (h != NULL)
7270 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7271 else
7272 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7273 }
7274 break;
7275 case R_NDS32_9_PLTREL:
35c08157
KLC
7276 case R_NDS32_25_PLTREL:
7277 case R_NDS32_PLTREL_HI20:
7278 case R_NDS32_PLTREL_LO12:
7279 case R_NDS32_PLT_GOTREL_HI20:
7280 case R_NDS32_PLT_GOTREL_LO12:
7281 case R_NDS32_PLT_GOTREL_LO15:
7282 case R_NDS32_PLT_GOTREL_LO19:
7283 case R_NDS32_PLT_GOTREL_LO20:
7284
7285 /* This symbol requires a procedure linkage table entry. We
7286 actually build the entry in adjust_dynamic_symbol,
7287 because this might be a case of linking PIC code without
7288 linking in any dynamic objects, in which case we don't
7289 need to generate a procedure linkage table after all. */
7290
7291 /* If this is a local symbol, we resolve it directly without
7292 creating a procedure linkage table entry. */
7293 if (h == NULL)
7294 continue;
7295
fbaf61ad
NC
7296 if (h->forced_local
7297 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
7298 break;
7299
1c8f6a4d 7300 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
35c08157
KLC
7301 h->needs_plt = 1;
7302 h->plt.refcount += 1;
7303 break;
7304
7305 case R_NDS32_16_RELA:
7306 case R_NDS32_20_RELA:
7307 case R_NDS32_5_RELA:
7308 case R_NDS32_32_RELA:
7309 case R_NDS32_HI20_RELA:
7310 case R_NDS32_LO12S3_RELA:
7311 case R_NDS32_LO12S2_RELA:
7312 case R_NDS32_LO12S2_DP_RELA:
7313 case R_NDS32_LO12S2_SP_RELA:
7314 case R_NDS32_LO12S1_RELA:
7315 case R_NDS32_LO12S0_RELA:
7316 case R_NDS32_LO12S0_ORI_RELA:
7317 case R_NDS32_SDA16S3_RELA:
7318 case R_NDS32_SDA17S2_RELA:
7319 case R_NDS32_SDA18S1_RELA:
7320 case R_NDS32_SDA19S0_RELA:
7321 case R_NDS32_SDA15S3_RELA:
7322 case R_NDS32_SDA15S2_RELA:
7323 case R_NDS32_SDA12S2_DP_RELA:
7324 case R_NDS32_SDA12S2_SP_RELA:
7325 case R_NDS32_SDA15S1_RELA:
7326 case R_NDS32_SDA15S0_RELA:
7327 case R_NDS32_SDA_FP7U2_RELA:
7328 case R_NDS32_15_PCREL_RELA:
7329 case R_NDS32_17_PCREL_RELA:
7330 case R_NDS32_25_PCREL_RELA:
7331
0e1862bb 7332 if (h != NULL && !bfd_link_pic (info))
35c08157
KLC
7333 {
7334 h->non_got_ref = 1;
7335 h->plt.refcount += 1;
7336 }
7337
7338 /* If we are creating a shared library, and this is a reloc against
7339 a global symbol, or a non PC relative reloc against a local
7340 symbol, then we need to copy the reloc into the shared library.
7341 However, if we are linking with -Bsymbolic, we do not need to
7342 copy a reloc against a global symbol which is defined in an
7343 object we are including in the link (i.e., DEF_REGULAR is set).
7344 At this point we have not seen all the input files, so it is
7345 possible that DEF_REGULAR is not set now but will be set later
7346 (it is never cleared). We account for that possibility below by
7347 storing information in the dyn_relocs field of the hash table
7348 entry. A similar situation occurs when creating shared libraries
7349 and symbol visibility changes render the symbol local.
7350
7351 If on the other hand, we are creating an executable, we may need
7352 to keep relocations for symbols satisfied by a dynamic library
7353 if we manage to avoid copy relocs for the symbol. */
0e1862bb 7354 if ((bfd_link_pic (info)
35c08157
KLC
7355 && (sec->flags & SEC_ALLOC) != 0
7356 && ((r_type != R_NDS32_25_PCREL_RELA
7357 && r_type != R_NDS32_15_PCREL_RELA
7358 && r_type != R_NDS32_17_PCREL_RELA
7359 && !(r_type == R_NDS32_32_RELA
7360 && strcmp (sec->name, ".eh_frame") == 0))
7361 || (h != NULL
7362 && (!info->symbolic
7363 || h->root.type == bfd_link_hash_defweak
7364 || !h->def_regular))))
0e1862bb 7365 || (!bfd_link_pic (info)
35c08157
KLC
7366 && (sec->flags & SEC_ALLOC) != 0
7367 && h != NULL
7368 && (h->root.type == bfd_link_hash_defweak
7369 || !h->def_regular)))
7370 {
3bf083ed
AM
7371 struct elf_dyn_relocs *p;
7372 struct elf_dyn_relocs **head;
35c08157
KLC
7373
7374 if (dynobj == NULL)
7375 htab->root.dynobj = dynobj = abfd;
7376
7377 /* When creating a shared object, we must copy these
7378 relocs into the output file. We create a reloc
7379 section in dynobj and make room for the reloc. */
7380 if (sreloc == NULL)
7381 {
7382 const char *name;
7383
7384 name = bfd_elf_string_from_elf_section
7385 (abfd, elf_elfheader (abfd)->e_shstrndx,
7386 elf_section_data (sec)->rela.hdr->sh_name);
7387 if (name == NULL)
7388 return FALSE;
7389
7390 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
fd361982 7391 && strcmp (bfd_section_name (sec),
35c08157
KLC
7392 name + 5) == 0);
7393
7394 sreloc = bfd_get_section_by_name (dynobj, name);
7395 if (sreloc == NULL)
7396 {
7397 flagword flags;
7398
7399 sreloc = bfd_make_section (dynobj, name);
7400 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7401 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7402 if ((sec->flags & SEC_ALLOC) != 0)
7403 flags |= SEC_ALLOC | SEC_LOAD;
7404 if (sreloc == NULL
fd361982
AM
7405 || !bfd_set_section_flags (sreloc, flags)
7406 || !bfd_set_section_alignment (sreloc, 2))
35c08157
KLC
7407 return FALSE;
7408
7409 elf_section_type (sreloc) = SHT_RELA;
7410 }
7411 elf_section_data (sec)->sreloc = sreloc;
7412 }
7413
7414 /* If this is a global symbol, we count the number of
7415 relocations we need for this symbol. */
7416 if (h != NULL)
190eb1dd 7417 head = &h->dyn_relocs;
35c08157
KLC
7418 else
7419 {
7420 asection *s;
61034b0b 7421 void *vpp;
35c08157
KLC
7422
7423 Elf_Internal_Sym *isym;
7424 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7425 if (isym == NULL)
7426 return FALSE;
7427
7428 /* Track dynamic relocs needed for local syms too. */
7429 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7430 if (s == NULL)
7431 return FALSE;
7432
61034b0b 7433 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 7434 head = (struct elf_dyn_relocs **) vpp;
35c08157
KLC
7435 }
7436
7437 p = *head;
7438 if (p == NULL || p->sec != sec)
7439 {
986f0783 7440 size_t amt = sizeof (*p);
3bf083ed 7441 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
35c08157
KLC
7442 if (p == NULL)
7443 return FALSE;
7444 p->next = *head;
7445 *head = p;
7446 p->sec = sec;
7447 p->count = 0;
7448 p->pc_count = 0;
7449 }
7450
7451 p->count += 1;
fbaf61ad
NC
7452
7453 /* Since eh_frame is readonly, R_NDS32_32_RELA
7454 reloc for eh_frame will cause shared library has
7455 TEXTREL entry in the dynamic section. This lead glibc
7456 testsuites to failure (bug-13092) and cause kernel fail
7457 (bug-11819). I think the best solution is to replace
7458 absolute reloc with pc relative reloc in the eh_frame.
7459 To do that, we need to support the following issues:
7460
7461 === For GCC ===
7462 * gcc/config/nds32/nds32.h: Define
7463 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7464 and DW_EH_PE_sdata4 into DWARF exception header when
7465 option have '-fpic'.
7466
7467 === For binutils ===
7468 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7469 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7470 may break our nds DIFF mechanism, therefore, we
7471 must disable all linker relaxations to ensure
7472 correctness.
7473 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7474 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7475 do the necessary modification.
7476
7477 Unfortunately, it still have some problems for nds32
7478 to support pc relative reloc in the eh_frame. So I use
7479 another solution to fix this issue.
7480
7481 However, I find that ld always emit TEXTREL marker for
7482 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7483 correspond to R_NDS32_32_RELA for .eh_frame section.
7484 It means that we always reserve redundant entries of rel.dyn
7485 for these relocs which actually do nothing in dynamic linker.
7486
7487 Therefore, we regard these relocs as pc relative relocs
7488 here and increase the pc_count. */
35c08157
KLC
7489 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7490 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
fbaf61ad
NC
7491 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7492 || (r_type == R_NDS32_32_RELA
7493 && strcmp (sec->name, ".eh_frame") == 0))
35c08157
KLC
7494 p->pc_count += 1;
7495 }
7496 break;
7497
7498 /* This relocation describes the C++ object vtable hierarchy.
7499 Reconstruct it for later use during GC. */
7500 case R_NDS32_RELA_GNU_VTINHERIT:
7501 case R_NDS32_GNU_VTINHERIT:
7502 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7503 return FALSE;
7504 break;
7505
7506 /* This relocation describes which C++ vtable entries are actually
7507 used. Record for later use during GC. */
7508 case R_NDS32_GNU_VTENTRY:
7509 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7510 return FALSE;
7511 break;
7512 case R_NDS32_RELA_GNU_VTENTRY:
7513 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7514 return FALSE;
7515 break;
7516 }
7517 }
7518
7519 return TRUE;
7520}
7521
7522/* Write VAL in uleb128 format to P, returning a pointer to the
7523 following byte.
7524 This code is copied from elf-attr.c. */
7525
7526static bfd_byte *
7527write_uleb128 (bfd_byte *p, unsigned int val)
7528{
7529 bfd_byte c;
7530 do
7531 {
7532 c = val & 0x7f;
7533 val >>= 7;
7534 if (val)
7535 c |= 0x80;
7536 *(p++) = c;
7537 }
7538 while (val);
7539 return p;
7540}
7541
7542static bfd_signed_vma
7543calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
fbaf61ad 7544 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
35c08157
KLC
7545{
7546 bfd_signed_vma foff;
7547 bfd_vma symval, addend;
7548 asection *sym_sec;
7549
7550 /* Get the value of the symbol referred to by the reloc. */
7551 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7552 {
7553 Elf_Internal_Sym *isym;
7554
7555 /* A local symbol. */
7556 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7557
7558 if (isym->st_shndx == SHN_UNDEF)
7559 sym_sec = bfd_und_section_ptr;
7560 else if (isym->st_shndx == SHN_ABS)
7561 sym_sec = bfd_abs_section_ptr;
7562 else if (isym->st_shndx == SHN_COMMON)
7563 sym_sec = bfd_com_section_ptr;
7564 else
7565 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7566 symval = isym->st_value + sym_sec->output_section->vma
7567 + sym_sec->output_offset;
7568 }
7569 else
7570 {
7571 unsigned long indx;
7572 struct elf_link_hash_entry *h;
35c08157
KLC
7573
7574 /* An external symbol. */
7575 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7576 h = elf_sym_hashes (abfd)[indx];
7577 BFD_ASSERT (h != NULL);
7578
7579 if (h->root.type != bfd_link_hash_defined
7580 && h->root.type != bfd_link_hash_defweak)
7581 /* This appears to be a reference to an undefined
7582 symbol. Just ignore it--it will be caught by the
7583 regular reloc processing. */
7584 return 0;
35c08157
KLC
7585
7586 if (h->root.u.def.section->flags & SEC_MERGE)
7587 {
7588 sym_sec = h->root.u.def.section;
7589 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7590 elf_section_data (sym_sec)->sec_info,
7591 h->root.u.def.value);
7592 symval = symval + sym_sec->output_section->vma
7593 + sym_sec->output_offset;
7594 }
7595 else
7596 symval = (h->root.u.def.value
7597 + h->root.u.def.section->output_section->vma
7598 + h->root.u.def.section->output_offset);
7599 }
7600
7601 addend = irel->r_addend;
7602
7603 foff = (symval + addend
7604 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7605 return foff;
7606}
35c08157 7607\f
fbaf61ad 7608
35c08157
KLC
7609/* Convert a 32-bit instruction to 16-bit one.
7610 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7611 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7612 type of INSN16. Return 1 if successful. */
7613
7614static int
7615nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7616 int *pinsn_type)
7617{
7618 uint16_t insn16 = 0;
6cae483a 7619 int insn_type = 0;
35c08157
KLC
7620 unsigned long mach = bfd_get_mach (abfd);
7621
7622 if (N32_SH5 (insn) != 0)
7623 return 0;
7624
7625 switch (N32_SUB5 (insn))
7626 {
7627 case N32_ALU1_ADD_SLLI:
7628 case N32_ALU1_ADD_SRLI:
7629 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7630 {
7631 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7632 N32_RB5 (insn));
7633 insn_type = NDS32_INSN_ADD333;
7634 }
7635 else if (N32_IS_RT4 (insn))
7636 {
7637 if (N32_RT5 (insn) == N32_RA5 (insn))
7638 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7639 else if (N32_RT5 (insn) == N32_RB5 (insn))
7640 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7641 insn_type = NDS32_INSN_ADD45;
7642 }
7643 break;
7644
7645 case N32_ALU1_SUB_SLLI:
7646 case N32_ALU1_SUB_SRLI:
7647 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7648 {
7649 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7650 N32_RB5 (insn));
7651 insn_type = NDS32_INSN_SUB333;
7652 }
7653 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7654 {
7655 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7656 insn_type = NDS32_INSN_SUB45;
7657 }
7658 break;
7659
7660 case N32_ALU1_AND_SLLI:
7661 case N32_ALU1_AND_SRLI:
7662 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7663 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7664 && N32_IS_RB3 (insn))
7665 {
7666 if (N32_RT5 (insn) == N32_RA5 (insn))
7667 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7668 else if (N32_RT5 (insn) == N32_RB5 (insn))
7669 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7670 if (insn16)
7671 insn_type = NDS32_INSN_AND33;
7672 }
7673 break;
7674
7675 case N32_ALU1_XOR_SLLI:
7676 case N32_ALU1_XOR_SRLI:
7677 /* xor $rt, $rt, $rb -> xor33 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 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7683 else if (N32_RT5 (insn) == N32_RB5 (insn))
7684 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7685 if (insn16)
7686 insn_type = NDS32_INSN_XOR33;
7687 }
7688 break;
7689
7690 case N32_ALU1_OR_SLLI:
7691 case N32_ALU1_OR_SRLI:
7692 /* or $rt, $rt, $rb -> or33 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 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7698 else if (N32_RT5 (insn) == N32_RB5 (insn))
7699 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7700 if (insn16)
7701 insn_type = NDS32_INSN_OR33;
7702 }
7703 break;
7704 case N32_ALU1_NOR:
7705 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7706 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7707 && N32_RA5 (insn) == N32_RB5 (insn))
7708 {
7709 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7710 insn_type = NDS32_INSN_NOT33;
7711 }
7712 break;
7713 case N32_ALU1_SRAI:
7714 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7715 {
7716 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7717 insn_type = NDS32_INSN_SRAI45;
7718 }
7719 break;
7720
7721 case N32_ALU1_SRLI:
7722 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7723 {
7724 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7725 insn_type = NDS32_INSN_SRLI45;
7726 }
7727 break;
7728
7729 case N32_ALU1_SLLI:
7730 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7731 {
7732 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7733 N32_UB5 (insn));
7734 insn_type = NDS32_INSN_SLLI333;
7735 }
7736 break;
7737
7738 case N32_ALU1_ZEH:
7739 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7740 {
7741 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7742 insn_type = NDS32_INSN_ZEH33;
7743 }
7744 break;
7745
7746 case N32_ALU1_SEB:
7747 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7748 {
7749 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7750 insn_type = NDS32_INSN_SEB33;
7751 }
7752 break;
7753
7754 case N32_ALU1_SEH:
7755 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7756 {
7757 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7758 insn_type = NDS32_INSN_SEH33;
7759 }
7760 break;
7761
7762 case N32_ALU1_SLT:
7763 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7764 {
7765 /* Implicit r15. */
7766 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7767 insn_type = NDS32_INSN_SLT45;
7768 }
7769 break;
7770
7771 case N32_ALU1_SLTS:
7772 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7773 {
7774 /* Implicit r15. */
7775 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7776 insn_type = NDS32_INSN_SLTS45;
7777 }
7778 break;
7779 }
7780
7781 if ((insn16 & 0x8000) == 0)
7782 return 0;
7783
7784 if (pinsn16)
7785 *pinsn16 = insn16;
7786 if (pinsn_type)
7787 *pinsn_type = insn_type;
7788 return 1;
7789}
7790
7791static int
7792nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7793 int *pinsn_type)
7794{
7795 uint16_t insn16 = 0;
7796 int insn_type;
7797 unsigned long mach = bfd_get_mach (abfd);
7798
7799 /* TODO: bset, bclr, btgl, btst. */
7800 if (__GF (insn, 6, 4) != 0)
7801 return 0;
7802
7803 switch (N32_IMMU (insn, 6))
7804 {
7805 case N32_ALU2_MUL:
7806 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7807 && N32_IS_RB3 (insn))
7808 {
7809 if (N32_RT5 (insn) == N32_RA5 (insn))
7810 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7811 else if (N32_RT5 (insn) == N32_RB5 (insn))
7812 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7813 if (insn16)
7814 insn_type = NDS32_INSN_MUL33;
7815 }
7816 }
7817
7818 if ((insn16 & 0x8000) == 0)
7819 return 0;
7820
7821 if (pinsn16)
7822 *pinsn16 = insn16;
7823 if (pinsn_type)
7824 *pinsn_type = insn_type;
7825 return 1;
7826}
7827
7828int
7829nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7830 int *pinsn_type)
7831{
7832 int op6;
7833 uint16_t insn16 = 0;
1624c9ca 7834 int insn_type = 0;
35c08157
KLC
7835 unsigned long mach = bfd_get_mach (abfd);
7836
7837 /* Decode 32-bit instruction. */
7838 if (insn & 0x80000000)
7839 {
7840 /* Not 32-bit insn. */
7841 return 0;
7842 }
7843
7844 op6 = N32_OP6 (insn);
7845
7846 /* Convert it to 16-bit instruction. */
7847 switch (op6)
7848 {
7849 case N32_OP6_MOVI:
7850 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7851 {
7852 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7853 insn_type = NDS32_INSN_MOVI55;
7854 }
7855 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7856 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7857 {
7858 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7859 N32_IMM20S (insn) - 16);
7860 insn_type = NDS32_INSN_MOVPI45;
7861 }
7862 break;
7863
7864 case N32_OP6_ADDI:
7865 if (N32_IMM15S (insn) == 0)
7866 {
7867 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7868 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7869 if (mach <= MACH_V2
7870 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7871 {
7872 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7873 insn_type = NDS32_INSN_MOV55;
7874 }
7875 }
7876 else if (N32_IMM15S (insn) > 0)
7877 {
7878 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7879 {
7880 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7881 N32_IMM15S (insn));
7882 insn_type = NDS32_INSN_ADDI333;
7883 }
7884 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7885 && N32_IMM15S (insn) < 32)
7886 {
7887 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7888 insn_type = NDS32_INSN_ADDI45;
7889 }
7890 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7891 && N32_RT5 (insn) == N32_RA5 (insn)
7892 && N32_IMM15S (insn) < 512)
7893 {
7894 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7895 insn_type = NDS32_INSN_ADDI10_SP;
7896 }
7897 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7898 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7899 && (N32_IMM15S (insn) % 4 == 0))
7900 {
7901 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7902 N32_IMM15S (insn) >> 2);
7903 insn_type = NDS32_INSN_ADDRI36_SP;
7904 }
7905 }
7906 else
7907 {
7908 /* Less than 0. */
7909 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7910 {
7911 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7912 0 - N32_IMM15S (insn));
7913 insn_type = NDS32_INSN_SUBI333;
7914 }
7915 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7916 && N32_IMM15S (insn) > -32)
7917 {
1c8f6a4d
KLC
7918 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7919 0 - N32_IMM15S (insn));
35c08157
KLC
7920 insn_type = NDS32_INSN_SUBI45;
7921 }
7922 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7923 && N32_RT5 (insn) == N32_RA5 (insn)
7924 && N32_IMM15S (insn) >= -512)
7925 {
7926 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7927 insn_type = NDS32_INSN_ADDI10_SP;
7928 }
7929 }
7930 break;
7931
7932 case N32_OP6_ORI:
7933 if (N32_IMM15S (insn) == 0)
7934 {
7935 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7936 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7937 if (mach <= MACH_V2
7938 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7939 {
7940 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7941 insn_type = NDS32_INSN_MOV55;
7942 }
7943 }
7944 break;
7945
7946 case N32_OP6_SUBRI:
7947 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7948 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7949 {
7950 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7951 insn_type = NDS32_INSN_NEG33;
7952 }
7953 break;
7954
7955 case N32_OP6_ANDI:
7956 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7957 {
7958 if (N32_IMM15U (insn) == 1)
7959 {
7960 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7961 insn_type = NDS32_INSN_XLSB33;
7962 }
7963 else if (N32_IMM15U (insn) == 0x7ff)
7964 {
7965 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7966 insn_type = NDS32_INSN_X11B33;
7967 }
7968 else if (N32_IMM15U (insn) == 0xff)
7969 {
7970 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7971 insn_type = NDS32_INSN_ZEB33;
7972 }
7973 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7974 && N32_IMM15U (insn) < 256)
7975 {
7976 int imm15u = N32_IMM15U (insn);
7977
7978 if (__builtin_popcount (imm15u) == 1)
7979 {
7980 /* BMSKI33 */
7981 int imm3u = __builtin_ctz (imm15u);
7982
7983 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7984 insn_type = NDS32_INSN_BMSKI33;
7985 }
7986 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7987 {
7988 /* FEXTI33 */
7989 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7990
7991 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7992 insn_type = NDS32_INSN_FEXTI33;
7993 }
7994 }
7995 }
7996 break;
7997
7998 case N32_OP6_SLTI:
7999 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8000 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8001 {
8002 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
8003 insn_type = NDS32_INSN_SLTI45;
8004 }
8005 break;
8006
8007 case N32_OP6_SLTSI:
8008 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
8009 && IS_WITHIN_U (N32_IMM15S (insn), 5))
8010 {
8011 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
8012 insn_type = NDS32_INSN_SLTSI45;
8013 }
8014 break;
8015
8016 case N32_OP6_LWI:
8017 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8018 {
8019 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
8020 insn_type = NDS32_INSN_LWI450;
8021 }
8022 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8023 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8024 {
8025 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
8026 N32_IMM15S (insn));
8027 insn_type = NDS32_INSN_LWI333;
8028 }
8029 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8030 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8031 {
8032 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8033 insn_type = NDS32_INSN_LWI37;
8034 }
8035 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8036 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8037 {
8038 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8039 insn_type = NDS32_INSN_LWI37_SP;
8040 }
8041 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8042 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8043 {
1c8f6a4d
KLC
8044 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8045 N32_IMM15S (insn) + 32);
35c08157
KLC
8046 insn_type = NDS32_INSN_LWI45_FE;
8047 }
8048 break;
8049
8050 case N32_OP6_SWI:
8051 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8052 {
8053 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8054 insn_type = NDS32_INSN_SWI450;
8055 }
8056 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8057 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8058 {
1c8f6a4d
KLC
8059 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8060 N32_IMM15S (insn));
35c08157
KLC
8061 insn_type = NDS32_INSN_SWI333;
8062 }
8063 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8064 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8065 {
8066 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8067 insn_type = NDS32_INSN_SWI37;
8068 }
8069 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8070 && IS_WITHIN_U (N32_IMM15S (insn), 7))
8071 {
8072 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8073 insn_type = NDS32_INSN_SWI37_SP;
8074 }
8075 break;
8076
8077 case N32_OP6_LWI_BI:
8078 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8079 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8080 {
8081 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8082 N32_IMM15S (insn));
8083 insn_type = NDS32_INSN_LWI333_BI;
8084 }
8085 break;
8086
8087 case N32_OP6_SWI_BI:
8088 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8089 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8090 {
8091 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8092 N32_IMM15S (insn));
8093 insn_type = NDS32_INSN_SWI333_BI;
8094 }
8095 break;
8096
8097 case N32_OP6_LHI:
8098 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8099 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8100 {
8101 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8102 N32_IMM15S (insn));
8103 insn_type = NDS32_INSN_LHI333;
8104 }
8105 break;
8106
8107 case N32_OP6_SHI:
8108 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8109 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8110 {
8111 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8112 N32_IMM15S (insn));
8113 insn_type = NDS32_INSN_SHI333;
8114 }
8115 break;
8116
8117 case N32_OP6_LBI:
8118 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8119 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8120 {
8121 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8122 N32_IMM15S (insn));
8123 insn_type = NDS32_INSN_LBI333;
8124 }
8125 break;
8126
8127 case N32_OP6_SBI:
8128 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8129 && IS_WITHIN_U (N32_IMM15S (insn), 3))
8130 {
8131 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8132 N32_IMM15S (insn));
8133 insn_type = NDS32_INSN_SBI333;
8134 }
8135 break;
8136
8137 case N32_OP6_ALU1:
8138 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8139
8140 case N32_OP6_ALU2:
8141 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8142
8143 case N32_OP6_BR1:
8144 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8145 goto done;
8146
4ec521f2 8147 if ((insn & N32_BIT (14)) == 0)
35c08157
KLC
8148 {
8149 /* N32_BR1_BEQ */
8150 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8151 && N32_RT5 (insn) != REG_R5)
8152 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8153 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8154 && N32_RA5 (insn) != REG_R5)
8155 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8156 insn_type = NDS32_INSN_BEQS38;
8157 break;
8158 }
8159 else
8160 {
8161 /* N32_BR1_BNE */
8162 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8163 && N32_RT5 (insn) != REG_R5)
8164 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8165 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8166 && N32_RA5 (insn) != REG_R5)
8167 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8168 insn_type = NDS32_INSN_BNES38;
8169 break;
8170 }
8171 break;
8172
8173 case N32_OP6_BR2:
8174 switch (N32_BR2_SUB (insn))
8175 {
8176 case N32_BR2_BEQZ:
8177 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8178 {
8179 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8180 insn_type = NDS32_INSN_BEQZ38;
8181 }
1c8f6a4d
KLC
8182 else if (N32_RT5 (insn) == REG_R15
8183 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8184 {
8185 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8186 insn_type = NDS32_INSN_BEQZS8;
8187 }
8188 break;
8189
8190 case N32_BR2_BNEZ:
8191 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8192 {
8193 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8194 insn_type = NDS32_INSN_BNEZ38;
8195 }
1c8f6a4d
KLC
8196 else if (N32_RT5 (insn) == REG_R15
8197 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8198 {
8199 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8200 insn_type = NDS32_INSN_BNEZS8;
8201 }
8202 break;
8203
fbaf61ad
NC
8204 case N32_BR2_SOP0:
8205 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
35c08157
KLC
8206 {
8207 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8208 insn_type = NDS32_INSN_IFCALL9;
8209 }
8210 break;
8211 }
8212 break;
8213
8214 case N32_OP6_JI:
4ec521f2 8215 if ((insn & N32_BIT (24)) == 0)
35c08157
KLC
8216 {
8217 /* N32_JI_J */
8218 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8219 {
8220 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8221 insn_type = NDS32_INSN_J8;
8222 }
8223 }
8224 break;
8225
8226 case N32_OP6_JREG:
8227 if (__GF (insn, 8, 2) != 0)
8228 goto done;
8229
8230 switch (N32_IMMU (insn, 5))
8231 {
8232 case N32_JREG_JR:
8233 if (N32_JREG_HINT (insn) == 0)
8234 {
8235 /* jr */
8236 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8237 insn_type = NDS32_INSN_JR5;
8238 }
8239 else if (N32_JREG_HINT (insn) == 1)
8240 {
8241 /* ret */
8242 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8243 insn_type = NDS32_INSN_RET5;
8244 }
8245 else if (N32_JREG_HINT (insn) == 3)
8246 {
8247 /* ifret = mov55 $sp, $sp */
8248 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8249 insn_type = NDS32_INSN_IFRET;
8250 }
8251 break;
8252
8253 case N32_JREG_JRAL:
8254 /* It's convertible when return rt5 is $lp and address
8255 translation is kept. */
8256 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8257 {
8258 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8259 insn_type = NDS32_INSN_JRAL5;
8260 }
8261 break;
8262 }
8263 break;
8264
8265 case N32_OP6_MISC:
8266 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8267 {
8268 /* For v3, swid above 31 are used for ex9.it. */
8269 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8270 insn_type = NDS32_INSN_BREAK16;
8271 }
8272 break;
8273
8274 default:
8275 /* This instruction has no 16-bit variant. */
8276 goto done;
8277 }
8278
dc1e8a47 8279 done:
35c08157
KLC
8280 /* Bit-15 of insn16 should be set for a valid instruction. */
8281 if ((insn16 & 0x8000) == 0)
8282 return 0;
8283
8284 if (pinsn16)
8285 *pinsn16 = insn16;
8286 if (pinsn_type)
8287 *pinsn_type = insn_type;
8288 return 1;
8289}
8290
8291static int
8292special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8293 Elf_Internal_Rela *reloc)
8294{
8295 uint16_t insn16 = 0;
8296
8297 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8298 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8299 return 0;
8300
8301 if (!N32_IS_RT3 (insn))
8302 return 0;
8303
8304 switch (N32_OP6 (insn))
8305 {
8306 case N32_OP6_LWI:
8307 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8308 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8309 break;
8310 case N32_OP6_SWI:
8311 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8312 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8313 break;
8314 case N32_OP6_HWGP:
8315 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8316 break;
8317
8318 if (__GF (insn, 17, 3) == 6)
8319 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8320 else if (__GF (insn, 17, 3) == 7)
8321 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8322 break;
8323 }
8324
8325 if ((insn16 & 0x8000) == 0)
8326 return 0;
8327
8328 *pinsn16 = insn16;
8329 return 1;
8330}
8331
8332/* Convert a 16-bit instruction to 32-bit one.
8333 INSN16 it the input and PINSN it the point to output.
8334 Return non-zero on successful. Otherwise 0 is returned. */
8335
8336int
8337nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8338{
8339 uint32_t insn = 0xffffffff;
8340 unsigned long mach = bfd_get_mach (abfd);
8341
8342 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8343
8344 switch (__GF (insn16, 9, 6))
8345 {
8346 case 0x4: /* add45 */
1c8f6a4d
KLC
8347 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8348 N16_RA5 (insn16));
35c08157
KLC
8349 goto done;
8350 case 0x5: /* sub45 */
1c8f6a4d
KLC
8351 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8352 N16_RA5 (insn16));
35c08157
KLC
8353 goto done;
8354 case 0x6: /* addi45 */
1c8f6a4d
KLC
8355 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8356 N16_IMM5U (insn16));
35c08157
KLC
8357 goto done;
8358 case 0x7: /* subi45 */
1c8f6a4d
KLC
8359 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8360 -N16_IMM5U (insn16));
35c08157
KLC
8361 goto done;
8362 case 0x8: /* srai45 */
1c8f6a4d
KLC
8363 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8364 N16_IMM5U (insn16));
35c08157
KLC
8365 goto done;
8366 case 0x9: /* srli45 */
1c8f6a4d
KLC
8367 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8368 N16_IMM5U (insn16));
35c08157 8369 goto done;
35c08157 8370 case 0xa: /* slli333 */
1c8f6a4d
KLC
8371 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8372 N16_IMM3U (insn16));
35c08157
KLC
8373 goto done;
8374 case 0xc: /* add333 */
1c8f6a4d
KLC
8375 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8376 N16_RB3 (insn16));
35c08157
KLC
8377 goto done;
8378 case 0xd: /* sub333 */
1c8f6a4d
KLC
8379 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8380 N16_RB3 (insn16));
35c08157
KLC
8381 goto done;
8382 case 0xe: /* addi333 */
1c8f6a4d
KLC
8383 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8384 N16_IMM3U (insn16));
35c08157
KLC
8385 goto done;
8386 case 0xf: /* subi333 */
1c8f6a4d
KLC
8387 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8388 -N16_IMM3U (insn16));
35c08157 8389 goto done;
35c08157 8390 case 0x10: /* lwi333 */
1c8f6a4d
KLC
8391 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8392 N16_IMM3U (insn16));
35c08157
KLC
8393 goto done;
8394 case 0x12: /* lhi333 */
1c8f6a4d
KLC
8395 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8396 N16_IMM3U (insn16));
35c08157
KLC
8397 goto done;
8398 case 0x13: /* lbi333 */
1c8f6a4d
KLC
8399 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8400 N16_IMM3U (insn16));
35c08157
KLC
8401 goto done;
8402 case 0x11: /* lwi333.bi */
1c8f6a4d
KLC
8403 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8404 N16_IMM3U (insn16));
35c08157
KLC
8405 goto done;
8406 case 0x14: /* swi333 */
1c8f6a4d
KLC
8407 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8408 N16_IMM3U (insn16));
35c08157
KLC
8409 goto done;
8410 case 0x16: /* shi333 */
1c8f6a4d
KLC
8411 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8412 N16_IMM3U (insn16));
35c08157
KLC
8413 goto done;
8414 case 0x17: /* sbi333 */
1c8f6a4d
KLC
8415 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8416 N16_IMM3U (insn16));
35c08157
KLC
8417 goto done;
8418 case 0x15: /* swi333.bi */
1c8f6a4d
KLC
8419 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8420 N16_IMM3U (insn16));
35c08157 8421 goto done;
35c08157 8422 case 0x18: /* addri36.sp */
1c8f6a4d
KLC
8423 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8424 N16_IMM6U (insn16) << 2);
35c08157 8425 goto done;
35c08157 8426 case 0x19: /* lwi45.fe */
1c8f6a4d
KLC
8427 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8428 (N16_IMM5U (insn16) - 32));
35c08157
KLC
8429 goto done;
8430 case 0x1a: /* lwi450 */
8431 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8432 goto done;
8433 case 0x1b: /* swi450 */
8434 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8435 goto done;
8436
1c8f6a4d 8437 /* These are r15 implied instructions. */
35c08157
KLC
8438 case 0x30: /* slts45 */
8439 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8440 goto done;
8441 case 0x31: /* slt45 */
8442 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8443 goto done;
8444 case 0x32: /* sltsi45 */
8445 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8446 goto done;
8447 case 0x33: /* slti45 */
8448 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8449 goto done;
8450 case 0x34: /* beqzs8, bnezs8 */
4ec521f2 8451 if (insn16 & N32_BIT (8))
35c08157
KLC
8452 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8453 else
8454 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8455 goto done;
8456
8457 case 0x35: /* break16, ex9.it */
8458 /* Only consider range of v3 break16. */
8459 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8460 goto done;
8461
8462 case 0x3c: /* ifcall9 */
fbaf61ad 8463 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
35c08157
KLC
8464 goto done;
8465 case 0x3d: /* movpi45 */
8466 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8467 goto done;
8468
8469 case 0x3f: /* MISC33 */
1c8f6a4d 8470 switch (insn16 & 0x7)
35c08157
KLC
8471 {
8472 case 2: /* neg33 */
8473 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8474 break;
8475 case 3: /* not33 */
1c8f6a4d
KLC
8476 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8477 N16_RA3 (insn16));
35c08157
KLC
8478 break;
8479 case 4: /* mul33 */
1c8f6a4d
KLC
8480 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8481 N16_RA3 (insn16));
35c08157
KLC
8482 break;
8483 case 5: /* xor33 */
1c8f6a4d
KLC
8484 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8485 N16_RA3 (insn16));
35c08157
KLC
8486 break;
8487 case 6: /* and33 */
1c8f6a4d
KLC
8488 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8489 N16_RA3 (insn16));
35c08157
KLC
8490 break;
8491 case 7: /* or33 */
1c8f6a4d
KLC
8492 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8493 N16_RA3 (insn16));
35c08157
KLC
8494 break;
8495 }
8496 goto done;
8497
1c8f6a4d 8498 case 0xb:
35c08157
KLC
8499 switch (insn16 & 0x7)
8500 {
8501 case 0: /* zeb33 */
8502 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8503 break;
8504 case 1: /* zeh33 */
8505 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8506 break;
8507 case 2: /* seb33 */
8508 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8509 break;
8510 case 3: /* seh33 */
8511 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8512 break;
8513 case 4: /* xlsb33 */
8514 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8515 break;
8516 case 5: /* x11b33 */
8517 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8518 break;
8519 case 6: /* bmski33 */
8520 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8521 1 << __GF (insn16, 3, 3));
35c08157
KLC
8522 break;
8523 case 7: /* fexti33 */
8524 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8525 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
35c08157
KLC
8526 break;
8527 }
8528 goto done;
8529 }
8530
8531 switch (__GF (insn16, 10, 5))
8532 {
8533 case 0x0: /* mov55 or ifret16 */
8534 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8535 && N16_RT5 (insn16) == N16_RA5 (insn16))
1c8f6a4d 8536 insn = N32_JREG (JR, 0, 0, 0, 3);
35c08157 8537 else
1c8f6a4d 8538 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
35c08157
KLC
8539 goto done;
8540 case 0x1: /* movi55 */
8541 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8542 goto done;
8543 case 0x1b: /* addi10s (V2) */
8544 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8545 goto done;
8546 }
8547
8548 switch (__GF (insn16, 11, 4))
8549 {
8550 case 0x7: /* lwi37.fp/swi37.fp */
4ec521f2 8551 if (insn16 & N32_BIT (7)) /* swi37.fp */
35c08157
KLC
8552 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8553 else /* lwi37.fp */
8554 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8555 goto done;
8556 case 0x8: /* beqz38 */
8557 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8558 goto done;
8559 case 0x9: /* bnez38 */
8560 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8561 goto done;
8562 case 0xa: /* beqs38/j8, implied r5 */
8563 if (N16_RT38 (insn16) == 5)
8564 insn = N32_JI (J, N16_IMM8S (insn16));
8565 else
8566 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8567 goto done;
fbaf61ad 8568 case 0xb: /* bnes38 and others. */
35c08157
KLC
8569 if (N16_RT38 (insn16) == 5)
8570 {
8571 switch (__GF (insn16, 5, 3))
8572 {
8573 case 0: /* jr5 */
8574 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8575 break;
8576 case 4: /* ret5 */
8577 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8578 break;
8579 case 1: /* jral5 */
8580 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8581 break;
8582 case 2: /* ex9.it imm5 */
8583 /* ex9.it had no 32-bit variantl. */
8584 break;
8585 case 5: /* add5.pc */
8586 /* add5.pc had no 32-bit variantl. */
8587 break;
8588 }
8589 }
8590 else /* bnes38 */
8591 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8592 goto done;
8593 case 0xe: /* lwi37/swi37 */
8594 if (insn16 & (1 << 7)) /* swi37.sp */
8595 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8596 else /* lwi37.sp */
8597 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8598 goto done;
8599 }
8600
dc1e8a47 8601 done:
35c08157
KLC
8602 if (insn & 0x80000000)
8603 return 0;
8604
8605 if (pinsn)
8606 *pinsn = insn;
8607 return 1;
8608}
8609\f
fbaf61ad 8610
35c08157
KLC
8611static bfd_boolean
8612is_sda_access_insn (unsigned long insn)
8613{
8614 switch (N32_OP6 (insn))
8615 {
8616 case N32_OP6_LWI:
8617 case N32_OP6_LHI:
8618 case N32_OP6_LHSI:
8619 case N32_OP6_LBI:
8620 case N32_OP6_LBSI:
8621 case N32_OP6_SWI:
8622 case N32_OP6_SHI:
8623 case N32_OP6_SBI:
8624 case N32_OP6_LWC:
8625 case N32_OP6_LDC:
8626 case N32_OP6_SWC:
8627 case N32_OP6_SDC:
8628 return TRUE;
8629 default:
8630 ;
8631 }
8632 return FALSE;
8633}
8634
8635static unsigned long
8636turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8637{
8638 uint32_t oinsn = 0;
8639
8640 switch (type)
8641 {
8642 case R_NDS32_GOT_LO12:
8643 case R_NDS32_GOTOFF_LO12:
8644 case R_NDS32_PLTREL_LO12:
8645 case R_NDS32_PLT_GOTREL_LO12:
8646 case R_NDS32_LO12S0_RELA:
8647 switch (N32_OP6 (insn))
8648 {
8649 case N32_OP6_LBI:
8650 /* lbi.gp */
8651 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8652 break;
8653 case N32_OP6_LBSI:
8654 /* lbsi.gp */
4ec521f2 8655 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8656 break;
8657 case N32_OP6_SBI:
8658 /* sbi.gp */
8659 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8660 break;
8661 case N32_OP6_ORI:
8662 /* addi.gp */
4ec521f2 8663 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8664 break;
8665 }
8666 break;
8667
8668 case R_NDS32_LO12S1_RELA:
8669 switch (N32_OP6 (insn))
8670 {
8671 case N32_OP6_LHI:
8672 /* lhi.gp */
8673 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8674 break;
8675 case N32_OP6_LHSI:
8676 /* lhsi.gp */
4ec521f2 8677 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
35c08157
KLC
8678 break;
8679 case N32_OP6_SHI:
8680 /* shi.gp */
4ec521f2 8681 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8682 break;
8683 }
8684 break;
8685
8686 case R_NDS32_LO12S2_RELA:
8687 switch (N32_OP6 (insn))
8688 {
8689 case N32_OP6_LWI:
8690 /* lwi.gp */
8691 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8692 break;
8693 case N32_OP6_SWI:
8694 /* swi.gp */
8695 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8696 break;
8697 }
8698 break;
8699
8700 case R_NDS32_LO12S2_DP_RELA:
8701 case R_NDS32_LO12S2_SP_RELA:
8702 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8703 break;
8704 }
8705
8706 if (oinsn)
8707 *pinsn = oinsn;
8708
8709 return oinsn != 0;
8710}
8711
8712/* Linker hasn't found the correct merge section for non-section symbol
8713 in relax time, this work is left to the function elf_link_input_bfd().
8714 So for non-section symbol, _bfd_merged_section_offset is also needed
8715 to find the correct symbol address. */
8716
8717static bfd_vma
8718nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8719 asection **psec, Elf_Internal_Rela *rel)
8720{
8721 asection *sec = *psec;
8722 bfd_vma relocation;
8723
8724 relocation = (sec->output_section->vma
8725 + sec->output_offset + sym->st_value);
8726 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8727 {
8728 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8729 rel->r_addend =
8730 _bfd_merged_section_offset (abfd, psec,
8731 elf_section_data (sec)->sec_info,
8732 sym->st_value + rel->r_addend);
8733 else
8734 rel->r_addend =
8735 _bfd_merged_section_offset (abfd, psec,
8736 elf_section_data (sec)->sec_info,
8737 sym->st_value) + rel->r_addend;
8738
8739 if (sec != *psec)
8740 {
8741 /* If we have changed the section, and our original section is
8742 marked with SEC_EXCLUDE, it means that the original
8743 SEC_MERGE section has been completely subsumed in some
8744 other SEC_MERGE section. In this case, we need to leave
8745 some info around for --emit-relocs. */
8746 if ((sec->flags & SEC_EXCLUDE) != 0)
8747 sec->kept_section = *psec;
8748 sec = *psec;
8749 }
8750 rel->r_addend -= relocation;
8751 rel->r_addend += sec->output_section->vma + sec->output_offset;
8752 }
8753 return relocation;
8754}
8755
8756static bfd_vma
8757calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8758 Elf_Internal_Sym *isymbuf,
8759 Elf_Internal_Shdr *symtab_hdr)
8760{
8761 bfd_signed_vma foff;
8762 bfd_vma symval, addend;
8763 Elf_Internal_Rela irel_fn;
8764 Elf_Internal_Sym *isym;
8765 asection *sym_sec;
8766
8767 /* Get the value of the symbol referred to by the reloc. */
8768 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8769 {
8770 /* A local symbol. */
8771 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8772
8773 if (isym->st_shndx == SHN_UNDEF)
8774 sym_sec = bfd_und_section_ptr;
8775 else if (isym->st_shndx == SHN_ABS)
8776 sym_sec = bfd_abs_section_ptr;
8777 else if (isym->st_shndx == SHN_COMMON)
8778 sym_sec = bfd_com_section_ptr;
8779 else
8780 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8781 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8782 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8783 addend = irel_fn.r_addend;
8784 }
8785 else
8786 {
8787 unsigned long indx;
8788 struct elf_link_hash_entry *h;
8789
8790 /* An external symbol. */
8791 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8792 h = elf_sym_hashes (abfd)[indx];
8793 BFD_ASSERT (h != NULL);
8794
8795 while (h->root.type == bfd_link_hash_indirect
8796 || h->root.type == bfd_link_hash_warning)
8797 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8798
8799 if (h->root.type != bfd_link_hash_defined
8800 && h->root.type != bfd_link_hash_defweak)
8801 /* This appears to be a reference to an undefined
8802 symbol. Just ignore it--it will be caught by the
8803 regular reloc processing. */
8804 return 0;
8805
8806 if (h->root.u.def.section->flags & SEC_MERGE)
8807 {
8808 sym_sec = h->root.u.def.section;
8809 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8810 (sym_sec)->sec_info, h->root.u.def.value);
8811 symval = symval + sym_sec->output_section->vma
8812 + sym_sec->output_offset;
8813 }
8814 else
8815 symval = (h->root.u.def.value
8816 + h->root.u.def.section->output_section->vma
8817 + h->root.u.def.section->output_offset);
8818 addend = irel->r_addend;
8819 }
8820
8821 foff = symval + addend;
8822
8823 return foff;
8824}
8825
35c08157
KLC
8826static int
8827is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8828 asection *sec, Elf_Internal_Rela *rel)
8829{
8830 bfd_byte *contents;
8831 unsigned short insn16;
8832
8833 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8834 return FALSE;
8835 contents = elf_section_data (sec)->this_hdr.contents;
8836 insn16 = bfd_getb16 (contents + rel->r_offset);
8837 if (insn16 == NDS32_NOP16)
8838 return TRUE;
8839 return FALSE;
8840}
8841
8842/* It checks whether the instruction could be converted to
8843 16-bit form and returns the converted one.
8844
8845 `internal_relocs' is supposed to be sorted. */
8846
8847static int
8848is_convert_32_to_16 (bfd *abfd, asection *sec,
8849 Elf_Internal_Rela *reloc,
8850 Elf_Internal_Rela *internal_relocs,
8851 Elf_Internal_Rela *irelend,
8852 uint16_t *insn16)
8853{
8854#define NORMAL_32_TO_16 (1 << 0)
8855#define SPECIAL_32_TO_16 (1 << 1)
8856 bfd_byte *contents = NULL;
8857 bfd_signed_vma off;
8858 bfd_vma mem_addr;
8859 uint32_t insn = 0;
8860 Elf_Internal_Rela *pc_rel;
35c08157
KLC
8861 Elf_Internal_Shdr *symtab_hdr;
8862 Elf_Internal_Sym *isymbuf = NULL;
8863 int convert_type;
8864 bfd_vma offset;
8865
8866 if (reloc->r_offset + 4 > sec->size)
8867 return FALSE;
8868
8869 offset = reloc->r_offset;
8870
0c4bd9d9 8871 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
35c08157
KLC
8872 return FALSE;
8873 insn = bfd_getb32 (contents + offset);
8874
8875 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8876 convert_type = NORMAL_32_TO_16;
8877 else if (special_convert_32_to_16 (insn, insn16, reloc))
8878 convert_type = SPECIAL_32_TO_16;
8879 else
8880 return FALSE;
8881
8882 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8883 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8884 return FALSE;
8885
8886 /* Find the first relocation of the same relocation-type,
8887 so we iteratie them forward. */
8888 pc_rel = reloc;
1c8f6a4d 8889 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
35c08157
KLC
8890 pc_rel--;
8891
8892 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8893 {
8894 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8895 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8896 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8897 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8898 {
fbaf61ad 8899 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8900 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8901 || off == 0)
35c08157
KLC
8902 return FALSE;
8903 break;
8904 }
8905 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8906 {
8907 /* movi => movi55 */
1c8f6a4d
KLC
8908 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8909 symtab_hdr);
8910 /* mem_addr is unsigned, but the value should
8911 be between [-16, 15]. */
35c08157
KLC
8912 if ((mem_addr + 0x10) >> 5)
8913 return FALSE;
8914 break;
8915 }
1c8f6a4d
KLC
8916 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8917 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8918 {
8919 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8920 because it can be relaxed to addi for TLS_LE_ADD. */
8921 return FALSE;
8922 }
35c08157
KLC
8923 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8924 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8925 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8926 && convert_type == SPECIAL_32_TO_16)
8927 {
8928 /* fp-as-gp
8929 We've selected a best fp-base for this access, so we can
8930 always resolve it anyway. Do nothing. */
8931 break;
8932 }
8933 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8934 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8935 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8936 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8937 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8938 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8939 {
1c8f6a4d
KLC
8940 /* Prevent unresolved addi instruction translate
8941 to addi45 or addi333. */
35c08157
KLC
8942 return FALSE;
8943 }
1c8f6a4d
KLC
8944 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8945 {
fbaf61ad 8946 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8947 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8948 return FALSE;
8949 break;
8950 }
35c08157
KLC
8951 }
8952
8953 return TRUE;
8954}
8955
8956static void
8957nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8958 Elf_Internal_Rela *reloc,
8959 Elf_Internal_Rela *internal_relocs,
8960 Elf_Internal_Rela *irelend,
8961 unsigned short insn16)
8962{
8963 Elf_Internal_Rela *pc_rel;
8964 bfd_vma offset;
8965
8966 offset = reloc->r_offset;
8967 bfd_putb16 (insn16, contents + offset);
8968 /* Find the first relocation of the same relocation-type,
8969 so we iteratie them forward. */
8970 pc_rel = reloc;
8971 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8972 pc_rel--;
8973
8974 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8975 {
8976 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8977 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8978 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8979 {
8980 pc_rel->r_info =
8981 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8982 }
8983 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8984 pc_rel->r_info =
8985 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8986 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8987 pc_rel->r_info =
8988 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8989 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8990 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8991 pc_rel->r_info =
8992 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
1c8f6a4d
KLC
8993 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8994 pc_rel->r_info =
8995 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
35c08157
KLC
8996 }
8997}
8998
8999/* Find a relocation of type specified by `reloc_type'
9000 of the same r_offset with reloc.
9001 If not found, return irelend.
9002
9003 Assuming relocations are sorted by r_offset,
9004 we find the relocation from `reloc' backward untill relocs,
9005 or find it from `reloc' forward untill irelend. */
9006
9007static Elf_Internal_Rela *
9008find_relocs_at_address (Elf_Internal_Rela *reloc,
9009 Elf_Internal_Rela *relocs,
9010 Elf_Internal_Rela *irelend,
9011 enum elf_nds32_reloc_type reloc_type)
9012{
9013 Elf_Internal_Rela *rel_t;
9014
9015 /* Find backward. */
9016 for (rel_t = reloc;
9017 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
9018 rel_t--)
9019 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9020 return rel_t;
9021
1c8f6a4d 9022 /* We didn't find it backward. Try find it forward. */
35c08157
KLC
9023 for (rel_t = reloc;
9024 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9025 rel_t++)
9026 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9027 return rel_t;
9028
9029 return irelend;
9030}
9031
9032/* Find a relocation of specified type and offset.
9033 `reloc' is just a refence point to find a relocation at specified offset.
9034 If not found, return irelend.
9035
9036 Assuming relocations are sorted by r_offset,
9037 we find the relocation from `reloc' backward untill relocs,
9038 or find it from `reloc' forward untill irelend. */
9039
9040static Elf_Internal_Rela *
9041find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9042 Elf_Internal_Rela *relocs,
9043 Elf_Internal_Rela *irelend,
6cae483a 9044 enum elf_nds32_reloc_type reloc_type,
35c08157
KLC
9045 bfd_vma offset_p)
9046{
9047 Elf_Internal_Rela *rel_t = NULL;
9048
9049 /* First, we try to find a relocation of offset `offset_p',
9050 and then we use find_relocs_at_address to find specific type. */
9051
9052 if (reloc->r_offset > offset_p)
9053 {
9054 /* Find backward. */
9055 for (rel_t = reloc;
9056 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9057 /* Do nothing. */;
9058 }
9059 else if (reloc->r_offset < offset_p)
9060 {
9061 /* Find forward. */
9062 for (rel_t = reloc;
9063 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9064 /* Do nothing. */;
9065 }
9066 else
9067 rel_t = reloc;
9068
9069 /* Not found? */
9070 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9071 return irelend;
9072
9073 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9074}
9075
fbaf61ad
NC
9076typedef struct nds32_elf_blank nds32_elf_blank_t;
9077struct nds32_elf_blank
35c08157 9078{
fbaf61ad
NC
9079 /* Where the blank begins. */
9080 bfd_vma offset;
9081 /* The size of the blank. */
9082 bfd_vma size;
9083 /* The accumulative size before this blank. */
9084 bfd_vma total_size;
9085 nds32_elf_blank_t *next;
9086 nds32_elf_blank_t *prev;
9087};
35c08157
KLC
9088
9089static nds32_elf_blank_t *blank_free_list = NULL;
9090
9091static nds32_elf_blank_t *
9092create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9093{
9094 nds32_elf_blank_t *blank_t;
9095
9096 if (blank_free_list)
9097 {
9098 blank_t = blank_free_list;
9099 blank_free_list = blank_free_list->next;
9100 }
9101 else
9102 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9103
9104 if (blank_t == NULL)
9105 return NULL;
9106
9107 blank_t->offset = offset_p;
9108 blank_t->size = size_p;
9109 blank_t->total_size = 0;
9110 blank_t->next = NULL;
9111 blank_t->prev = NULL;
9112
9113 return blank_t;
9114}
9115
9116static void
9117remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9118{
9119 if (blank_free_list)
9120 {
9121 blank_free_list->prev = blank_p;
9122 blank_p->next = blank_free_list;
9123 }
9124 else
9125 blank_p->next = NULL;
9126
9127 blank_p->prev = NULL;
9128 blank_free_list = blank_p;
9129}
9130
9131static void
9132clean_nds32_elf_blank (void)
9133{
9134 nds32_elf_blank_t *blank_t;
9135
9136 while (blank_free_list)
9137 {
9138 blank_t = blank_free_list;
9139 blank_free_list = blank_free_list->next;
9140 free (blank_t);
9141 }
9142}
9143
9144static nds32_elf_blank_t *
9145search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9146{
9147 nds32_elf_blank_t *blank_t;
9148
9149 if (!blank_p)
9150 return NULL;
9151 blank_t = blank_p;
9152
9153 while (blank_t && addr < blank_t->offset)
9154 blank_t = blank_t->prev;
9155 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9156 blank_t = blank_t->next;
9157
9158 return blank_t;
9159}
9160
9161static bfd_vma
9162get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9163 int overwrite)
9164{
9165 nds32_elf_blank_t *blank_t;
9166
9167 blank_t = search_nds32_elf_blank (*blank_p, addr);
9168 if (!blank_t)
9169 return 0;
9170
9171 if (overwrite)
9172 *blank_p = blank_t;
9173
9174 if (addr < blank_t->offset + blank_t->size)
9175 return blank_t->total_size + (addr - blank_t->offset);
9176 else
9177 return blank_t->total_size + blank_t->size;
9178}
9179
9180static bfd_boolean
9181insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9182{
9183 nds32_elf_blank_t *blank_t, *blank_t2;
9184
9185 if (!*blank_p)
9186 {
9187 *blank_p = create_nds32_elf_blank (addr, len);
9188 return *blank_p ? TRUE : FALSE;
9189 }
9190
9191 blank_t = search_nds32_elf_blank (*blank_p, addr);
9192
9193 if (blank_t == NULL)
9194 {
9195 blank_t = create_nds32_elf_blank (addr, len);
9196 if (!blank_t)
9197 return FALSE;
9198 while ((*blank_p)->prev != NULL)
9199 *blank_p = (*blank_p)->prev;
9200 blank_t->next = *blank_p;
9201 (*blank_p)->prev = blank_t;
9202 (*blank_p) = blank_t;
9203 return TRUE;
9204 }
9205
9206 if (addr < blank_t->offset + blank_t->size)
9207 {
fbaf61ad
NC
9208 /* Extend the origin blank. */
9209 if (addr + len > blank_t->offset + blank_t->size)
9210 blank_t->size = addr + len - blank_t->offset;
35c08157
KLC
9211 }
9212 else
9213 {
9214 blank_t2 = create_nds32_elf_blank (addr, len);
9215 if (!blank_t2)
9216 return FALSE;
9217 if (blank_t->next)
9218 {
9219 blank_t->next->prev = blank_t2;
9220 blank_t2->next = blank_t->next;
9221 }
9222 blank_t2->prev = blank_t;
9223 blank_t->next = blank_t2;
9224 *blank_p = blank_t2;
9225 }
9226
9227 return TRUE;
9228}
9229
9230static bfd_boolean
9231insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9232 bfd_vma len)
9233{
9234 nds32_elf_blank_t *blank_t;
9235
9236 if (!insert_nds32_elf_blank (blank_p, addr, len))
9237 return FALSE;
9238
9239 blank_t = *blank_p;
9240
9241 if (!blank_t->prev)
9242 {
9243 blank_t->total_size = 0;
9244 blank_t = blank_t->next;
9245 }
9246
9247 while (blank_t)
9248 {
9249 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9250 blank_t = blank_t->next;
9251 }
9252
9253 return TRUE;
9254}
9255
9256static void
9257calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9258{
9259 nds32_elf_blank_t *blank_t;
9260 bfd_vma total_size = 0;
9261
9262 if (!blank_p)
9263 return;
9264
9265 blank_t = blank_p;
9266 while (blank_t->prev)
9267 blank_t = blank_t->prev;
9268 while (blank_t)
9269 {
9270 blank_t->total_size = total_size;
9271 total_size += blank_t->size;
9272 blank_t = blank_t->next;
9273 }
9274}
9275
9276static bfd_boolean
9277nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9278 nds32_elf_blank_t *blank_p)
9279{
9280 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
1c8f6a4d 9281 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
35c08157
KLC
9282 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9283 unsigned int sec_shndx; /* The section the be relaxed. */
9284 bfd_byte *contents; /* Contents data of iterating section. */
9285 Elf_Internal_Rela *internal_relocs;
9286 Elf_Internal_Rela *irel;
9287 Elf_Internal_Rela *irelend;
9288 struct elf_link_hash_entry **sym_hashes;
9289 struct elf_link_hash_entry **end_hashes;
9290 unsigned int symcount;
9291 asection *sect;
9292 nds32_elf_blank_t *blank_t;
9293 nds32_elf_blank_t *blank_t2;
9294 nds32_elf_blank_t *blank_head;
9295
9296 blank_head = blank_t = blank_p;
9297 while (blank_head->prev != NULL)
9298 blank_head = blank_head->prev;
9299 while (blank_t->next != NULL)
9300 blank_t = blank_t->next;
9301
9302 if (blank_t->offset + blank_t->size <= sec->size)
9303 {
9304 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9305 blank_t->next->prev = blank_t;
9306 }
9307 if (blank_head->offset > 0)
9308 {
9309 blank_head->prev = create_nds32_elf_blank (0, 0);
9310 blank_head->prev->next = blank_head;
9311 blank_head = blank_head->prev;
9312 }
9313
9314 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9315
9316 /* The deletion must stop at the next ALIGN reloc for an alignment
9317 power larger than the number of bytes we are deleting. */
9318
9319 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9320 if (!nds32_get_local_syms (abfd, sec, &isym))
9321 return FALSE;
9322
9323 if (isym == NULL)
9324 {
9325 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9326 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9327 symtab_hdr->contents = (bfd_byte *) isym;
9328 }
9329
9330 if (isym == NULL || symtab_hdr->sh_info == 0)
9331 return FALSE;
9332
9333 blank_t = blank_head;
9334 calc_nds32_blank_total (blank_head);
9335
9336 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9337 {
9338 /* Adjust all the relocs. */
9339
9340 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9341 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9342 TRUE /* keep_memory */);
9343 irelend = internal_relocs + sect->reloc_count;
9344
9345 blank_t = blank_head;
9346 blank_t2 = blank_head;
9347
9348 if (!(sect->flags & SEC_RELOC))
9349 continue;
9350
0c4bd9d9 9351 nds32_get_section_contents (abfd, sect, &contents, TRUE);
35c08157
KLC
9352
9353 for (irel = internal_relocs; irel < irelend; irel++)
9354 {
9355 bfd_vma raddr;
9356
9357 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9358 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9359 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9360 {
9361 unsigned long val = 0;
1c8f6a4d
KLC
9362 unsigned long mask;
9363 long before, between;
6cae483a 9364 long offset = 0;
35c08157
KLC
9365
9366 switch (ELF32_R_TYPE (irel->r_info))
9367 {
9368 case R_NDS32_DIFF8:
1c8f6a4d 9369 offset = bfd_get_8 (abfd, contents + irel->r_offset);
35c08157
KLC
9370 break;
9371 case R_NDS32_DIFF16:
1c8f6a4d 9372 offset = bfd_get_16 (abfd, contents + irel->r_offset);
35c08157
KLC
9373 break;
9374 case R_NDS32_DIFF32:
9375 val = bfd_get_32 (abfd, contents + irel->r_offset);
1c8f6a4d
KLC
9376 /* Get the signed bit and mask for the high part. The
9377 gcc will alarm when right shift 32-bit since the
9378 type size of long may be 32-bit. */
9379 mask = 0 - (val >> 31);
9380 if (mask)
9381 offset = (val | (mask - 0xffffffff));
9382 else
9383 offset = val;
35c08157
KLC
9384 break;
9385 default:
9386 BFD_ASSERT (0);
9387 }
9388
9389 /* DIFF value
9390 0 |encoded in location|
9391 |------------|-------------------|---------
9392 sym+off(addend)
9393 -- before ---| *****************
9394 --------------------- between ---|
9395
1c8f6a4d
KLC
9396 We only care how much data are relax between DIFF,
9397 marked as ***. */
35c08157
KLC
9398
9399 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9400 between = get_nds32_elf_blank_total (&blank_t,
9401 irel->r_addend + offset, 0);
35c08157
KLC
9402 if (between == before)
9403 goto done_adjust_diff;
9404
9405 switch (ELF32_R_TYPE (irel->r_info))
9406 {
9407 case R_NDS32_DIFF8:
1c8f6a4d
KLC
9408 bfd_put_8 (abfd, offset - (between - before),
9409 contents + irel->r_offset);
35c08157
KLC
9410 break;
9411 case R_NDS32_DIFF16:
1c8f6a4d
KLC
9412 bfd_put_16 (abfd, offset - (between - before),
9413 contents + irel->r_offset);
35c08157
KLC
9414 break;
9415 case R_NDS32_DIFF32:
1c8f6a4d
KLC
9416 bfd_put_32 (abfd, offset - (between - before),
9417 contents + irel->r_offset);
35c08157
KLC
9418 break;
9419 }
9420 }
9421 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9422 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9423 {
9424 bfd_vma val = 0;
9425 unsigned int len = 0;
9426 unsigned long before, between;
9427 bfd_byte *endp, *p;
9428
4265548c
PA
9429 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9430 &len);
35c08157
KLC
9431
9432 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9433 between = get_nds32_elf_blank_total (&blank_t,
9434 irel->r_addend + val, 0);
35c08157
KLC
9435 if (between == before)
9436 goto done_adjust_diff;
9437
9438 p = contents + irel->r_offset;
9439 endp = p + len -1;
9440 memset (p, 0x80, len);
9441 *(endp) = 0;
9442 p = write_uleb128 (p, val - (between - before)) - 1;
9443 if (p < endp)
9444 *p |= 0x80;
9445 }
dc1e8a47 9446 done_adjust_diff:
35c08157
KLC
9447
9448 if (sec == sect)
9449 {
9450 raddr = irel->r_offset;
1c8f6a4d
KLC
9451 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9452 irel->r_offset, 1);
35c08157
KLC
9453
9454 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9455 continue;
9456 if (blank_t2 && blank_t2->next
1c8f6a4d
KLC
9457 && (blank_t2->offset > raddr
9458 || blank_t2->next->offset <= raddr))
4eca0228 9459 _bfd_error_handler
38f14ab8
AM
9460 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9461 abfd);
35c08157
KLC
9462
9463 /* Mark reloc in deleted portion as NONE.
9464 For some relocs like R_NDS32_LABEL that doesn't modify the
9465 content in the section. R_NDS32_LABEL doesn't belong to the
9466 instruction in the section, so we should preserve it. */
9467 if (raddr >= blank_t2->offset
9468 && raddr < blank_t2->offset + blank_t2->size
9469 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9470 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9471 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9472 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9473 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9474 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9475 {
9476 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9477 R_NDS32_NONE);
9478 continue;
9479 }
9480 }
9481
9482 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9483 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9484 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9485 continue;
9486
9487 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9488 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9489 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9490 {
9491 if (irel->r_addend <= sec->size)
9492 irel->r_addend -=
9493 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9494 }
9495 }
9496 }
9497
9498 /* Adjust the local symbols defined in this section. */
9499 blank_t = blank_head;
9500 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9501 {
9502 if (isym->st_shndx == sec_shndx)
9503 {
9504 if (isym->st_value <= sec->size)
9505 {
9506 bfd_vma ahead;
9507 bfd_vma orig_addr = isym->st_value;
9508
9509 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9510 isym->st_value -= ahead;
9511
9512 /* Adjust function size. */
1c8f6a4d
KLC
9513 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9514 && isym->st_size > 0)
9515 isym->st_size -=
9516 get_nds32_elf_blank_total
9517 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
35c08157
KLC
9518 }
9519 }
9520 }
9521
9522 /* Now adjust the global symbols defined in this section. */
9523 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9524 - symtab_hdr->sh_info);
9525 sym_hashes = elf_sym_hashes (abfd);
9526 end_hashes = sym_hashes + symcount;
9527 blank_t = blank_head;
9528 for (; sym_hashes < end_hashes; sym_hashes++)
9529 {
9530 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9531
9532 if ((sym_hash->root.type == bfd_link_hash_defined
9533 || sym_hash->root.type == bfd_link_hash_defweak)
9534 && sym_hash->root.u.def.section == sec)
9535 {
9536 if (sym_hash->root.u.def.value <= sec->size)
9537 {
9538 bfd_vma ahead;
9539 bfd_vma orig_addr = sym_hash->root.u.def.value;
9540
9541 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9542 sym_hash->root.u.def.value -= ahead;
9543
9544 /* Adjust function size. */
9545 if (sym_hash->type == STT_FUNC)
1c8f6a4d
KLC
9546 sym_hash->size -=
9547 get_nds32_elf_blank_total
9548 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
35c08157
KLC
9549
9550 }
9551 }
9552 }
9553
9554 contents = elf_section_data (sec)->this_hdr.contents;
9555 blank_t = blank_head;
9556 while (blank_t->next)
9557 {
9558 /* Actually delete the bytes. */
9559
9560 /* If current blank is the last blank overlap with current section,
9561 go to finish process. */
9562 if (sec->size <= (blank_t->next->offset))
9563 break;
9564
9565 memmove (contents + blank_t->offset - blank_t->total_size,
9566 contents + blank_t->offset + blank_t->size,
9567 blank_t->next->offset - (blank_t->offset + blank_t->size));
9568
9569 blank_t = blank_t->next;
9570 }
9571
9572 if (sec->size > (blank_t->offset + blank_t->size))
9573 {
9574 /* There are remaining code between blank and section boundary.
9575 Move the remaining code to appropriate location. */
9576 memmove (contents + blank_t->offset - blank_t->total_size,
9577 contents + blank_t->offset + blank_t->size,
9578 sec->size - (blank_t->offset + blank_t->size));
9579 sec->size -= blank_t->total_size + blank_t->size;
9580 }
9581 else
9582 /* This blank is not entirely included in the section,
9583 reduce the section size by only part of the blank size. */
9584 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9585
9586 while (blank_head)
9587 {
9588 blank_t = blank_head;
9589 blank_head = blank_head->next;
9590 remove_nds32_elf_blank (blank_t);
9591 }
9592
9593 return TRUE;
9594}
9595
9596/* Get the contents of a section. */
9597
9598static int
0c4bd9d9
KLC
9599nds32_get_section_contents (bfd *abfd, asection *sec,
9600 bfd_byte **contents_p, bfd_boolean cache)
35c08157
KLC
9601{
9602 /* Get the section contents. */
9603 if (elf_section_data (sec)->this_hdr.contents != NULL)
9604 *contents_p = elf_section_data (sec)->this_hdr.contents;
9605 else
9606 {
9607 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9608 return FALSE;
0c4bd9d9
KLC
9609 if (cache)
9610 elf_section_data (sec)->this_hdr.contents = *contents_p;
35c08157
KLC
9611 }
9612
9613 return TRUE;
9614}
9615
9616/* Get the contents of the internal symbol of abfd. */
9617
9618static int
9619nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9620 Elf_Internal_Sym **isymbuf_p)
9621{
9622 Elf_Internal_Shdr *symtab_hdr;
9623 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9624
9625 /* Read this BFD's local symbols if we haven't done so already. */
9626 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9627 {
9628 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9629 if (*isymbuf_p == NULL)
9630 {
9631 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9632 symtab_hdr->sh_info, 0,
9633 NULL, NULL, NULL);
9634 if (*isymbuf_p == NULL)
9635 return FALSE;
9636 }
9637 }
9638 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9639
9640 return TRUE;
9641}
9642
9643/* Range of small data. */
1c8f6a4d
KLC
9644static bfd_vma sdata_range[2][2];
9645static bfd_vma const sdata_init_range[2] =
9646{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
35c08157
KLC
9647
9648static int
9649nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9650 bfd_byte *contents, bfd_vma addr)
9651{
9652 unsigned long insn = bfd_getb32 (contents + addr);
9653
9654 if (insn & 0x80000000)
9655 return 2;
9656
9657 return 4;
9658}
9659
9660/* Set the gp relax range. We have to measure the safe range
9661 to do gp relaxation. */
9662
9663static void
9664relax_range_measurement (bfd *abfd)
9665{
9666 asection *sec_f, *sec_b;
9667 /* For upper bound. */
9668 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9669 bfd_vma align;
35c08157
KLC
9670 static int decide_relax_range = 0;
9671 int i;
fbaf61ad 9672 int range_number = ARRAY_SIZE (sdata_init_range);
35c08157
KLC
9673
9674 if (decide_relax_range)
9675 return;
9676 decide_relax_range = 1;
9677
9678 if (sda_rela_sec == NULL)
9679 {
9680 /* Since there is no data sections, we assume the range is page size. */
1c8f6a4d 9681 for (i = 0; i < range_number; i++)
35c08157
KLC
9682 {
9683 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9684 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9685 }
9686 return;
9687 }
9688
9689 /* Get the biggest alignment power after the gp located section. */
9690 sec_f = sda_rela_sec->output_section;
9691 sec_b = sec_f->next;
9692 align = 0;
9693 while (sec_b != NULL)
9694 {
9695 if ((unsigned)(1 << sec_b->alignment_power) > align)
9696 align = (1 << sec_b->alignment_power);
9697 sec_b = sec_b->next;
9698 }
9699
9700 /* I guess we can not determine the section before
9701 gp located section, so we assume the align is max page size. */
1c8f6a4d 9702 for (i = 0; i < range_number; i++)
35c08157 9703 {
1c8f6a4d 9704 sdata_range[i][1] = sdata_init_range[i] - align;
35c08157 9705 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
1c8f6a4d 9706 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
35c08157
KLC
9707 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9708 }
9709}
9710
9711/* These are macros used to check flags encoded in r_addend.
9712 They are only used by nds32_elf_relax_section (). */
9713#define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9714#define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9715#define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9716#define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9717
695344c0
NC
9718static const char * unrecognized_reloc_msg =
9719 /* xgettext:c-format */
2dcf00ce 9720 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
695344c0 9721
1c8f6a4d
KLC
9722/* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9723
35c08157 9724static bfd_boolean
1c8f6a4d
KLC
9725nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9726 Elf_Internal_Rela *internal_relocs, int *insn_len,
9727 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9728 Elf_Internal_Shdr *symtab_hdr)
9729{
9730 /* There are 3 variations for LONGCALL1
9731 case 4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8 9732 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9733 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9734 jral5 ta ;
1c8f6a4d
KLC
9735
9736 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8 9737 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9738 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9739 jral ta ;
1c8f6a4d
KLC
9740
9741 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8 9742 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9743 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9744 jral ta ;
1c8f6a4d
KLC
9745 Check code for -mlong-calls output. */
9746
9747 /* Get the reloc for the address from which the register is
9748 being loaded. This reloc will tell us which function is
9749 actually being called. */
9750
9751 bfd_vma laddr;
9752 int seq_len; /* Original length of instruction sequence. */
35c08157 9753 uint32_t insn;
1c8f6a4d 9754 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d 9755 bfd_signed_vma foff;
35c08157 9756 uint16_t insn16;
35c08157 9757
1c8f6a4d
KLC
9758 irelend = internal_relocs + sec->reloc_count;
9759 seq_len = GET_SEQ_LEN (irel->r_addend);
9760 laddr = irel->r_offset;
9761 *insn_len = seq_len;
35c08157 9762
1c8f6a4d
KLC
9763 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9764 R_NDS32_HI20_RELA, laddr);
9765 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9766 R_NDS32_LO12S0_ORI_RELA,
9767 laddr + 4);
35c08157 9768
1c8f6a4d 9769 if (hi_irelfn == irelend || lo_irelfn == irelend)
35c08157 9770 {
695344c0 9771 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
2dcf00ce 9772 (uint64_t) irel->r_offset);
1c8f6a4d 9773 return FALSE;
35c08157
KLC
9774 }
9775
1c8f6a4d 9776 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9777 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9778
1c8f6a4d 9779 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
9780 if (foff == 0
9781 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9782 || foff >= CONSERVATIVE_24BIT_S1)
9783 return FALSE;
35c08157 9784
fbaf61ad 9785 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
9786 /* For simplicity of coding, we are going to modify the section
9787 contents, the section relocs, and the BFD symbol table. We
9788 must tell the rest of the code not to free up this
9789 information. It would be possible to instead create a table
9790 of changes which have to be made, as is done in coff-mips.c;
9791 that would be more work, but would require less memory when
9792 the linker is run. */
9793
9794 /* Replace the long call with a jal. */
9795 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9796 R_NDS32_25_PCREL_RELA);
9797 irel->r_addend = hi_irelfn->r_addend;
9798
9799 /* We don't resolve this here but resolve it in relocate_section. */
9800 insn = INSN_JAL;
9801 bfd_putb32 (insn, contents + irel->r_offset);
9802
9803 hi_irelfn->r_info =
9804 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9805 lo_irelfn->r_info =
9806 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9807 *insn_len = 4;
9808
9809 if (seq_len & 0x2)
9810 {
9811 insn16 = NDS32_NOP16;
9812 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9813 lo_irelfn->r_info =
9814 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9815 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9816 *insn_len += 2;
9817 }
9818 return TRUE;
9819}
35c08157 9820
1c8f6a4d
KLC
9821#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9822/* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
35c08157 9823
1c8f6a4d
KLC
9824static bfd_boolean
9825nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9826 Elf_Internal_Rela *internal_relocs, int *insn_len,
9827 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9828 Elf_Internal_Shdr *symtab_hdr)
9829{
9830 /* bltz rt, .L1 ; LONGCALL2
9831 jal symbol ; 25_PCREL
9832 .L1: */
35c08157 9833
1c8f6a4d
KLC
9834 /* Get the reloc for the address from which the register is
9835 being loaded. This reloc will tell us which function is
9836 actually being called. */
35c08157 9837
1c8f6a4d
KLC
9838 bfd_vma laddr;
9839 uint32_t insn;
9840 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
1c8f6a4d 9841 bfd_signed_vma foff;
35c08157 9842
1c8f6a4d
KLC
9843 irelend = internal_relocs + sec->reloc_count;
9844 laddr = irel->r_offset;
9845 i1_irelfn =
9846 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9847 R_NDS32_25_PCREL_RELA, laddr + 4);
9848
9849 if (i1_irelfn == irelend)
35c08157 9850 {
695344c0 9851 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
2dcf00ce 9852 (uint64_t) irel->r_offset);
1c8f6a4d 9853 return FALSE;
35c08157
KLC
9854 }
9855
1c8f6a4d 9856 insn = bfd_getb32 (contents + laddr);
35c08157 9857
1c8f6a4d 9858 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9859 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
35c08157 9860
fbaf61ad
NC
9861 if (foff == 0
9862 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
9863 || foff >= CONSERVATIVE_16BIT_S1)
9864 return FALSE;
35c08157 9865
1c8f6a4d
KLC
9866 /* Relax to bgezal rt, label ; 17_PCREL
9867 or bltzal rt, label ; 17_PCREL */
9868
9869 /* Convert to complimentary conditional call. */
9870 insn = CONVERT_CONDITION_CALL (insn);
9871
9872 /* For simplicity of coding, we are going to modify the section
9873 contents, the section relocs, and the BFD symbol table. We
9874 must tell the rest of the code not to free up this
9875 information. It would be possible to instead create a table
9876 of changes which have to be made, as is done in coff-mips.c;
9877 that would be more work, but would require less memory when
9878 the linker is run. */
9879
9880 /* Clean unnessary relocations. */
9881 i1_irelfn->r_info =
9882 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9883 cond_irelfn =
9884 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9885 R_NDS32_17_PCREL_RELA, laddr);
9886 if (cond_irelfn != irelend)
9887 cond_irelfn->r_info =
9888 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9889
9890 /* Replace the long call with a bgezal. */
9891 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9892 R_NDS32_17_PCREL_RELA);
9893 irel->r_addend = i1_irelfn->r_addend;
9894
9895 bfd_putb32 (insn, contents + irel->r_offset);
9896
9897 *insn_len = 4;
9898 return TRUE;
9899}
35c08157 9900
1c8f6a4d
KLC
9901/* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9902
9903static bfd_boolean
9904nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9905 Elf_Internal_Rela *internal_relocs, int *insn_len,
9906 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9907 Elf_Internal_Shdr *symtab_hdr)
9908{
9909 /* There are 3 variations for LONGCALL3
9910 case 4-4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8
AM
9911 bltz rt, $1 ; LONGCALL3
9912 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9913 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9914 jral5 ta ;
1c8f6a4d
KLC
9915 $1
9916
9917 case 4-4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
9918 bltz rt, $1 ; LONGCALL3
9919 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9920 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9921 jral ta ;
1c8f6a4d
KLC
9922 $1
9923
9924 case 4-4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
9925 bltz rt, $1 ; LONGCALL3
9926 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9927 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9928 jral ta ;
1c8f6a4d
KLC
9929 $1 */
9930
9931 /* Get the reloc for the address from which the register is
9932 being loaded. This reloc will tell us which function is
9933 actually being called. */
9934
9935 bfd_vma laddr;
9936 int seq_len; /* Original length of instruction sequence. */
9937 uint32_t insn;
9938 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
1c8f6a4d
KLC
9939 bfd_signed_vma foff;
9940 uint16_t insn16;
9941
9942 irelend = internal_relocs + sec->reloc_count;
9943 seq_len = GET_SEQ_LEN (irel->r_addend);
9944 laddr = irel->r_offset;
9945 *insn_len = seq_len;
9946
9947 hi_irelfn =
9948 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9949 R_NDS32_HI20_RELA, laddr + 4);
9950 lo_irelfn =
9951 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9952 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9953
9954 if (hi_irelfn == irelend || lo_irelfn == irelend)
9955 {
695344c0 9956 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
2dcf00ce 9957 (uint64_t) irel->r_offset);
1c8f6a4d 9958 return FALSE;
35c08157
KLC
9959 }
9960
1c8f6a4d 9961 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9962 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9963
fbaf61ad
NC
9964 if (foff == 0
9965 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9966 || foff >= CONSERVATIVE_24BIT_S1)
9967 return FALSE;
35c08157 9968
1c8f6a4d
KLC
9969 insn = bfd_getb32 (contents + laddr);
9970 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
35c08157 9971 {
1c8f6a4d
KLC
9972 /* Relax to bgezal rt, label ; 17_PCREL
9973 or bltzal rt, label ; 17_PCREL */
35c08157 9974
1c8f6a4d
KLC
9975 /* Convert to complimentary conditional call. */
9976 insn = CONVERT_CONDITION_CALL (insn);
9977 bfd_putb32 (insn, contents + irel->r_offset);
35c08157 9978
1c8f6a4d
KLC
9979 *insn_len = 4;
9980 irel->r_info =
9981 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9982 hi_irelfn->r_info =
9983 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9984 lo_irelfn->r_info =
9985 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9986
9987 cond_irelfn =
9988 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9989 R_NDS32_17_PCREL_RELA, laddr);
9990 if (cond_irelfn != irelend)
35c08157 9991 {
1c8f6a4d
KLC
9992 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9993 R_NDS32_17_PCREL_RELA);
9994 cond_irelfn->r_addend = hi_irelfn->r_addend;
35c08157 9995 }
1c8f6a4d
KLC
9996
9997 if (seq_len & 0x2)
35c08157 9998 {
1c8f6a4d
KLC
9999 insn16 = NDS32_NOP16;
10000 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10001 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10002 R_NDS32_INSN16);
10003 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10004 insn_len += 2;
35c08157 10005 }
1c8f6a4d
KLC
10006 }
10007 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10008 {
10009 /* Relax to the following instruction sequence
10010 bltz rt, $1 ; LONGCALL2
10011 jal symbol ; 25_PCREL
10012 $1 */
10013 *insn_len = 8;
10014 insn = INSN_JAL;
10015 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
35c08157 10016
1c8f6a4d
KLC
10017 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10018 R_NDS32_25_PCREL_RELA);
10019 irel->r_info =
10020 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10021
10022 lo_irelfn->r_info =
10023 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10024
1c8f6a4d 10025 if (seq_len & 0x2)
35c08157 10026 {
1c8f6a4d
KLC
10027 insn16 = NDS32_NOP16;
10028 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10029 lo_irelfn->r_info =
10030 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10031 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10032 insn_len += 2;
10033 }
10034 }
10035 return TRUE;
10036}
35c08157 10037
1c8f6a4d 10038/* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
35c08157 10039
1c8f6a4d
KLC
10040static bfd_boolean
10041nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10042 Elf_Internal_Rela *internal_relocs, int *insn_len,
10043 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10044 Elf_Internal_Shdr *symtab_hdr)
10045{
10046 /* There are 3 variations for LONGJUMP1
10047 case 4-4-2; 16-bit bit on, optimize off or optimize for space
07d6d2b8
AM
10048 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10049 ori ta, ta, lo12(symbol) ; LO12S0
10050 jr5 ta ;
1c8f6a4d
KLC
10051
10052 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
10053 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10054 ori ta, ta, lo12(symbol) ; LO12S0
10055 jr ta ;
1c8f6a4d
KLC
10056
10057 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
10058 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
10059 ori ta, ta, lo12(symbol) ; LO12S0
10060 jr ta ; */
1c8f6a4d
KLC
10061
10062 /* Get the reloc for the address from which the register is
10063 being loaded. This reloc will tell us which function is
10064 actually being called. */
10065
10066 bfd_vma laddr;
10067 int seq_len; /* Original length of instruction sequence. */
10068 int insn16_on; /* 16-bit on/off. */
10069 uint32_t insn;
10070 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d
KLC
10071 bfd_signed_vma foff;
10072 uint16_t insn16;
10073 unsigned long reloc;
35c08157 10074
1c8f6a4d
KLC
10075 irelend = internal_relocs + sec->reloc_count;
10076 seq_len = GET_SEQ_LEN (irel->r_addend);
10077 laddr = irel->r_offset;
10078 *insn_len = seq_len;
10079 insn16_on = IS_16BIT_ON (irel->r_addend);
10080
10081 hi_irelfn =
10082 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10083 R_NDS32_HI20_RELA, laddr);
10084 lo_irelfn =
10085 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10086 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10087 if (hi_irelfn == irelend || lo_irelfn == irelend)
10088 {
695344c0 10089 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
2dcf00ce 10090 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10091 return FALSE;
10092 }
35c08157 10093
1c8f6a4d 10094 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10095 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10096
fbaf61ad
NC
10097 if (foff == 0
10098 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10099 || foff < -CONSERVATIVE_24BIT_S1)
10100 return FALSE;
35c08157 10101
fbaf61ad
NC
10102 if (insn16_on
10103 && foff >= -ACCURATE_8BIT_S1
10104 && foff < ACCURATE_8BIT_S1
10105 && (seq_len & 0x2))
1c8f6a4d
KLC
10106 {
10107 /* j8 label */
10108 /* 16-bit on, but not optimized for speed. */
10109 reloc = R_NDS32_9_PCREL_RELA;
10110 insn16 = INSN_J8;
10111 bfd_putb16 (insn16, contents + irel->r_offset);
10112 *insn_len = 2;
10113 irel->r_info =
10114 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10115 }
10116 else
10117 {
10118 /* j label */
10119 reloc = R_NDS32_25_PCREL_RELA;
10120 insn = INSN_J;
10121 bfd_putb32 (insn, contents + irel->r_offset);
10122 *insn_len = 4;
10123 irel->r_info =
10124 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10125 irel->r_addend = 0;
10126 }
35c08157 10127
1c8f6a4d
KLC
10128 hi_irelfn->r_info =
10129 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10130 lo_irelfn->r_info =
10131 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 10132
1c8f6a4d
KLC
10133 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10134 {
10135 insn16 = NDS32_NOP16;
10136 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10137 lo_irelfn->r_info =
10138 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10139 R_NDS32_INSN16);
10140 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10141 *insn_len += 2;
10142 }
10143 return TRUE;
10144}
10145
10146/* Revert condition branch. This function does not check if the input
10147 instruction is condition branch or not. */
10148
10149static void
10150nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10151 uint16_t *re_insn16, uint32_t *re_insn)
10152{
10153 uint32_t comp_insn = 0;
10154 uint16_t comp_insn16 = 0;
10155
10156 if (insn)
10157 {
10158 if (N32_OP6 (insn) == N32_OP6_BR1)
10159 {
10160 /* beqs label. */
10161 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10162 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
35c08157 10163 {
1c8f6a4d
KLC
10164 /* Insn can be contracted to 16-bit implied r5. */
10165 comp_insn16 =
10166 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10167 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
35c08157 10168 }
1c8f6a4d
KLC
10169 }
10170 else if (N32_OP6 (insn) == N32_OP6_BR3)
10171 {
10172 /* bnec $ta, imm11, label. */
10173 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10174 }
10175 else
10176 {
10177 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10178 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10179 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
35c08157 10180 {
1c8f6a4d 10181 if (N32_IS_RT3 (insn))
35c08157 10182 {
1c8f6a4d
KLC
10183 /* Insn can be contracted to 16-bit. */
10184 comp_insn16 =
10185 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10186 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10187 }
10188 else if (N32_RT5 (insn) == REG_R15)
10189 {
10190 /* Insn can be contracted to 16-bit. */
10191 comp_insn16 =
10192 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
35c08157 10193 }
35c08157
KLC
10194 }
10195 }
1c8f6a4d
KLC
10196 }
10197 else
10198 {
10199 switch ((insn16 & 0xf000) >> 12)
35c08157 10200 {
1c8f6a4d
KLC
10201 case 0xc:
10202 /* beqz38 or bnez38 */
10203 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10204 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10205 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10206 break;
35c08157 10207
1c8f6a4d
KLC
10208 case 0xd:
10209 /* beqs38 or bnes38 */
10210 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10211 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10212 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10213 | (REG_R5 << 15);
10214 break;
35c08157 10215
1c8f6a4d
KLC
10216 case 0xe:
10217 /* beqzS8 or bnezS8 */
10218 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10219 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10220 comp_insn |= REG_R15 << 20;
10221 break;
35c08157 10222
1c8f6a4d
KLC
10223 default:
10224 break;
10225 }
10226 }
10227 if (comp_insn && re_insn)
10228 *re_insn = comp_insn;
10229 if (comp_insn16 && re_insn16)
10230 *re_insn16 = comp_insn16;
10231}
35c08157 10232
1c8f6a4d 10233/* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
35c08157 10234
1c8f6a4d
KLC
10235static bfd_boolean
10236nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10237 Elf_Internal_Rela *internal_relocs, int *insn_len,
10238 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10239 Elf_Internal_Shdr *symtab_hdr)
10240{
10241 /* There are 3 variations for LONGJUMP2
10242 case 2-4; 1st insn convertible, 16-bit on,
10243 optimize off or optimize for space
10244 bnes38 rt, ra, $1 ; LONGJUMP2
10245 j label ; 25_PCREL
10246 $1:
10247
10248 case 4-4; 1st insn not convertible
10249 bne rt, ra, $1 ; LONGJUMP2
10250 j label ; 25_PCREL
10251 $1:
10252
10253 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10254 bne rt, ra, $1 ; LONGJUMP2
10255 j label ; 25_PCREL
10256 $1: */
10257
10258 /* Get the reloc for the address from which the register is
10259 being loaded. This reloc will tell us which function is
10260 actually being called. */
10261
10262 bfd_vma laddr;
10263 int seq_len; /* Original length of instruction sequence. */
10264 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10265 int first_size;
1c8f6a4d
KLC
10266 unsigned int i;
10267 bfd_signed_vma foff;
10268 uint32_t insn, re_insn = 0;
10269 uint16_t insn16, re_insn16 = 0;
10270 unsigned long reloc, cond_reloc;
35c08157 10271
1c8f6a4d
KLC
10272 enum elf_nds32_reloc_type checked_types[] =
10273 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
35c08157 10274
1c8f6a4d
KLC
10275 irelend = internal_relocs + sec->reloc_count;
10276 seq_len = GET_SEQ_LEN (irel->r_addend);
10277 laddr = irel->r_offset;
10278 *insn_len = seq_len;
10279 first_size = (seq_len == 6) ? 2 : 4;
10280
10281 i2_irelfn =
10282 find_relocs_at_address_addr (irel, internal_relocs,
10283 irelend, R_NDS32_25_PCREL_RELA,
10284 laddr + first_size);
10285
fbaf61ad 10286 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10287 {
10288 cond_irelfn =
10289 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10290 checked_types[i], laddr);
10291 if (cond_irelfn != irelend)
10292 break;
10293 }
35c08157 10294
1c8f6a4d
KLC
10295 if (i2_irelfn == irelend || cond_irelfn == irelend)
10296 {
695344c0 10297 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
2dcf00ce 10298 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10299 return FALSE;
10300 }
35c08157 10301
1c8f6a4d 10302 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad
NC
10303 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10304 if (foff == 0
10305 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10306 || foff >= CONSERVATIVE_16BIT_S1)
10307 return FALSE;
35c08157 10308
1c8f6a4d
KLC
10309 /* Get the all corresponding instructions. */
10310 if (first_size == 4)
10311 {
10312 insn = bfd_getb32 (contents + laddr);
10313 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10314 }
10315 else
10316 {
10317 insn16 = bfd_getb16 (contents + laddr);
10318 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10319 }
35c08157 10320
1c8f6a4d
KLC
10321 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10322 && foff < ACCURATE_8BIT_S1 - first_size)
10323 {
10324 if (first_size == 4)
10325 {
10326 /* Don't convert it to 16-bit now, keep this as relaxable for
10327 ``label reloc; INSN16''. */
35c08157 10328
1c8f6a4d
KLC
10329 /* Save comp_insn32 to buffer. */
10330 bfd_putb32 (re_insn, contents + irel->r_offset);
10331 *insn_len = 4;
10332 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10333 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10334 cond_reloc = R_NDS32_INSN16;
10335 }
10336 else
10337 {
10338 bfd_putb16 (re_insn16, contents + irel->r_offset);
10339 *insn_len = 2;
10340 reloc = R_NDS32_9_PCREL_RELA;
10341 cond_reloc = R_NDS32_NONE;
10342 }
10343 }
10344 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10345 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10346 && foff < ACCURATE_14BIT_S1 - first_size))
10347 {
10348 /* beqs label ; 15_PCREL */
10349 bfd_putb32 (re_insn, contents + irel->r_offset);
10350 *insn_len = 4;
10351 reloc = R_NDS32_15_PCREL_RELA;
10352 cond_reloc = R_NDS32_NONE;
10353 }
10354 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10355 && foff >= -CONSERVATIVE_16BIT_S1
10356 && foff < CONSERVATIVE_16BIT_S1)
10357 {
10358 /* beqz label ; 17_PCREL */
10359 bfd_putb32 (re_insn, contents + irel->r_offset);
10360 *insn_len = 4;
10361 reloc = R_NDS32_17_PCREL_RELA;
10362 cond_reloc = R_NDS32_NONE;
10363 }
10364 else
10365 return FALSE;
35c08157 10366
1c8f6a4d
KLC
10367 /* Set all relocations. */
10368 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10369 irel->r_addend = i2_irelfn->r_addend;
35c08157 10370
1c8f6a4d
KLC
10371 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10372 cond_reloc);
10373 cond_irelfn->r_addend = 0;
35c08157 10374
1c8f6a4d
KLC
10375 if ((seq_len ^ *insn_len ) & 0x2)
10376 {
10377 insn16 = NDS32_NOP16;
10378 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10379 i2_irelfn->r_offset = 4;
10380 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10381 R_NDS32_INSN16);
10382 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10383 *insn_len += 2;
10384 }
10385 else
10386 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10387 R_NDS32_NONE);
10388 return TRUE;
10389}
35c08157 10390
1c8f6a4d 10391/* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
35c08157 10392
1c8f6a4d
KLC
10393static bfd_boolean
10394nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10395 Elf_Internal_Rela *internal_relocs, int *insn_len,
10396 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10397 Elf_Internal_Shdr *symtab_hdr)
10398{
10399 /* There are 5 variations for LONGJUMP3
10400 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10401 optimize off or optimize for space
07d6d2b8
AM
10402 bnes38 rt, ra, $1 ; LONGJUMP3
10403 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10404 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10405 jr5 ta ;
10406 $1: ;
1c8f6a4d
KLC
10407
10408 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
07d6d2b8
AM
10409 bnes38 rt, ra, $1 ; LONGJUMP3
10410 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10411 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10412 jr5 ta ;
10413 $1: ; LABEL
1c8f6a4d
KLC
10414
10415 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10416 optimize off or optimize for space
07d6d2b8
AM
10417 bne 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 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10424 16-bit off if no INSN16
07d6d2b8
AM
10425 bne rt, ra, $1 ; LONGJUMP3
10426 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10427 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10428 jr ta ;
10429 $1: ;
1c8f6a4d
KLC
10430
10431 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10432 16-bit off if no INSN16
07d6d2b8
AM
10433 bne rt, ra, $1 ; LONGJUMP3
10434 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10435 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10436 jr ta ;
10437 $1: ; LABEL */
1c8f6a4d
KLC
10438
10439 /* Get the reloc for the address from which the register is
10440 being loaded. This reloc will tell us which function is
10441 actually being called. */
10442 enum elf_nds32_reloc_type checked_types[] =
10443 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10444
10445 int reloc_off = 0, cond_removed = 0, convertible;
10446 bfd_vma laddr;
10447 int seq_len; /* Original length of instruction sequence. */
10448 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10449 int first_size;
1c8f6a4d
KLC
10450 unsigned int i;
10451 bfd_signed_vma foff;
10452 uint32_t insn, re_insn = 0;
10453 uint16_t insn16, re_insn16 = 0;
10454 unsigned long reloc, cond_reloc;
35c08157 10455
1c8f6a4d
KLC
10456 irelend = internal_relocs + sec->reloc_count;
10457 seq_len = GET_SEQ_LEN (irel->r_addend);
10458 laddr = irel->r_offset;
10459 *insn_len = seq_len;
35c08157 10460
1c8f6a4d 10461 convertible = IS_1ST_CONVERT (irel->r_addend);
35c08157 10462
1c8f6a4d
KLC
10463 if (convertible)
10464 first_size = 2;
10465 else
10466 first_size = 4;
10467
10468 /* Get all needed relocations. */
10469 hi_irelfn =
10470 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10471 R_NDS32_HI20_RELA, laddr + first_size);
10472 lo_irelfn =
10473 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10474 R_NDS32_LO12S0_ORI_RELA,
10475 laddr + first_size + 4);
10476
fbaf61ad 10477 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10478 {
10479 cond_irelfn =
10480 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10481 checked_types[i], laddr);
10482 if (cond_irelfn != irelend)
10483 break;
10484 }
35c08157 10485
fbaf61ad
NC
10486 if (hi_irelfn == irelend
10487 || lo_irelfn == irelend
10488 || cond_irelfn == irelend)
1c8f6a4d 10489 {
695344c0 10490 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
2dcf00ce 10491 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10492 return FALSE;
10493 }
35c08157 10494
1c8f6a4d 10495 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10496 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10497
fbaf61ad
NC
10498 if (foff == 0
10499 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10500 || foff >= CONSERVATIVE_24BIT_S1)
10501 return FALSE;
35c08157 10502
1c8f6a4d
KLC
10503 /* Get the all corresponding instructions. */
10504 if (first_size == 4)
10505 {
10506 insn = bfd_getb32 (contents + laddr);
10507 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10508 }
10509 else
10510 {
10511 insn16 = bfd_getb16 (contents + laddr);
10512 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10513 }
35c08157 10514
1c8f6a4d
KLC
10515 /* For simplicity of coding, we are going to modify the section
10516 contents, the section relocs, and the BFD symbol table. We
10517 must tell the rest of the code not to free up this
10518 information. It would be possible to instead create a table
10519 of changes which have to be made, as is done in coff-mips.c;
10520 that would be more work, but would require less memory when
10521 the linker is run. */
35c08157 10522
fbaf61ad
NC
10523 if (re_insn16
10524 && foff >= -ACCURATE_8BIT_S1 - first_size
1c8f6a4d
KLC
10525 && foff < ACCURATE_8BIT_S1 - first_size)
10526 {
10527 if (!(seq_len & 0x2))
10528 {
10529 /* Don't convert it to 16-bit now, keep this as relaxable
10530 for ``label reloc; INSN1a''6. */
10531 /* Save comp_insn32 to buffer. */
10532 bfd_putb32 (re_insn, contents + irel->r_offset);
10533 *insn_len = 4;
10534 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10535 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10536 cond_reloc = R_NDS32_INSN16;
10537 }
10538 else
10539 {
10540 /* Not optimize for speed; convert sequence to 16-bit. */
10541 /* Save comp_insn16 to buffer. */
10542 bfd_putb16 (re_insn16, contents + irel->r_offset);
10543 *insn_len = 2;
10544 reloc = R_NDS32_9_PCREL_RELA;
10545 cond_reloc = R_NDS32_NONE;
10546 }
10547 cond_removed = 1;
10548 }
10549 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10550 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10551 && foff < ACCURATE_14BIT_S1 - first_size))
10552 {
10553 /* beqs label ; 15_PCREL */
10554 bfd_putb32 (re_insn, contents + irel->r_offset);
10555 *insn_len = 4;
10556 reloc = R_NDS32_15_PCREL_RELA;
10557 cond_reloc = R_NDS32_NONE;
10558 cond_removed = 1;
10559 }
10560 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10561 && foff >= -CONSERVATIVE_16BIT_S1
10562 && foff < CONSERVATIVE_16BIT_S1)
10563 {
10564 /* beqz label ; 17_PCREL */
10565 bfd_putb32 (re_insn, contents + irel->r_offset);
10566 *insn_len = 4;
10567 reloc = R_NDS32_17_PCREL_RELA;
10568 cond_reloc = R_NDS32_NONE;
10569 cond_removed = 1;
10570 }
10571 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10572 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10573 {
10574 /* Relax to one of the following 3 variations
10575
10576 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10577 for space
10578 bnes38 rt, $1 ; LONGJUMP2
10579 j label ; 25_PCREL
10580 $1
10581
10582 case 4-4; 1st insn not convertible, others don't care
10583 bne rt, ra, $1 ; LONGJUMP2
10584 j label ; 25_PCREL
10585 $1
10586
10587 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10588 bne rt, ra, $1 ; LONGJUMP2
10589 j label ; 25_PCREL
10590 $1 */
10591
10592 /* Offset for first instruction. */
10593
10594 /* Use j label as second instruction. */
10595 *insn_len = 4 + first_size;
10596 insn = INSN_J;
10597 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10598 reloc = R_NDS32_LONGJUMP2;
10599 cond_reloc = R_NDS32_25_PLTREL;
10600 }
10601 else
10602 return FALSE;
35c08157 10603
1c8f6a4d
KLC
10604 if (cond_removed == 1)
10605 {
10606 /* Set all relocations. */
10607 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10608 irel->r_addend = hi_irelfn->r_addend;
10609
10610 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10611 cond_reloc);
10612 cond_irelfn->r_addend = 0;
10613 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10614 R_NDS32_NONE);
10615 }
10616 else
10617 {
10618 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
fbaf61ad 10619 irel->r_addend = irel->r_addend;
1c8f6a4d
KLC
10620 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10621 cond_reloc);
10622 }
35c08157 10623
1c8f6a4d
KLC
10624 if ((seq_len ^ *insn_len ) & 0x2)
10625 {
10626 insn16 = NDS32_NOP16;
10627 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10628 lo_irelfn->r_offset = *insn_len;
10629 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10630 R_NDS32_INSN16);
10631 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10632 *insn_len += 2;
10633 }
10634 else
10635 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10636 R_NDS32_NONE);
10637 return TRUE;
10638}
35c08157 10639
1c8f6a4d 10640/* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
35c08157 10641
1c8f6a4d
KLC
10642static bfd_boolean
10643nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10644 Elf_Internal_Rela *internal_relocs, int *insn_len,
10645 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10646 Elf_Internal_Shdr *symtab_hdr)
10647{
10648 /* The pattern for LONGCALL4. Support for function cse.
10649 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10650 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10651 jral ta ; PTR_RES/EMPTY/INSN16 */
35c08157 10652
1c8f6a4d
KLC
10653 bfd_vma laddr;
10654 uint32_t insn;
10655 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10656 Elf_Internal_Rela *irelend;
1c8f6a4d 10657 bfd_signed_vma foff;
35c08157 10658
1c8f6a4d
KLC
10659 irelend = internal_relocs + sec->reloc_count;
10660 laddr = irel->r_offset;
35c08157 10661
1c8f6a4d
KLC
10662 /* Get the reloc for the address from which the register is
10663 being loaded. This reloc will tell us which function is
10664 actually being called. */
10665 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10666 R_NDS32_HI20_RELA, laddr);
35c08157 10667
1c8f6a4d
KLC
10668 if (hi_irel == irelend)
10669 {
695344c0 10670 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10671 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10672 return FALSE;
10673 }
35c08157 10674
1c8f6a4d 10675 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10676 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10677
1c8f6a4d 10678 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
10679 if (foff == 0
10680 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10681 || foff >= CONSERVATIVE_24BIT_S1)
10682 return FALSE;
35c08157 10683
fbaf61ad 10684 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
10685 /* For simplicity of coding, we are going to modify the section
10686 contents, the section relocs, and the BFD symbol table. We
10687 must tell the rest of the code not to free up this
10688 information. It would be possible to instead create a table
10689 of changes which have to be made, as is done in coff-mips.c;
10690 that would be more work, but would require less memory when
10691 the linker is run. */
35c08157 10692
1c8f6a4d
KLC
10693 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10694 R_NDS32_PTR_RESOLVED, irel->r_addend);
10695 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10696 R_NDS32_EMPTY, irel->r_addend);
35c08157 10697
1c8f6a4d
KLC
10698 if (ptr_irel == irelend || em_irel == irelend)
10699 {
695344c0 10700 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10701 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10702 return FALSE;
10703 }
10704 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10705 insn = bfd_getb32 (contents + irel->r_addend);
10706 if (insn & 0x80000000)
10707 return FALSE;
35c08157 10708
1c8f6a4d
KLC
10709 /* Replace the long call with a jal. */
10710 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10711 R_NDS32_25_PCREL_RELA);
10712 ptr_irel->r_addend = 1;
35c08157 10713
1c8f6a4d
KLC
10714 /* We don't resolve this here but resolve it in relocate_section. */
10715 insn = INSN_JAL;
10716 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10717
1c8f6a4d
KLC
10718 irel->r_info =
10719 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10720
1c8f6a4d
KLC
10721 /* If there is function cse, HI20 can not remove now. */
10722 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10723 R_NDS32_LONGCALL4, laddr);
10724 if (call_irel == irelend)
10725 {
10726 *insn_len = 0;
10727 hi_irel->r_info =
10728 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10729 }
35c08157 10730
1c8f6a4d
KLC
10731 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10732 R_NDS32_INSN16, irel->r_addend);
10733 if (insn_irel != irelend)
10734 insn_irel->r_info =
10735 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10736
1c8f6a4d
KLC
10737 return TRUE;
10738}
35c08157 10739
1c8f6a4d 10740/* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
35c08157 10741
1c8f6a4d
KLC
10742static bfd_boolean
10743nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10744 Elf_Internal_Rela *internal_relocs, int *insn_len,
10745 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10746 Elf_Internal_Shdr *symtab_hdr)
10747{
10748 /* The pattern for LONGCALL5.
10749 bltz rt, .L1 ; LONGCALL5/17_PCREL
10750 jal symbol ; 25_PCREL
10751 .L1: */
35c08157 10752
1c8f6a4d
KLC
10753 bfd_vma laddr;
10754 uint32_t insn;
10755 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d 10756 bfd_signed_vma foff;
35c08157 10757
1c8f6a4d
KLC
10758 irelend = internal_relocs + sec->reloc_count;
10759 laddr = irel->r_offset;
10760 insn = bfd_getb32 (contents + laddr);
35c08157 10761
1c8f6a4d
KLC
10762 /* Get the reloc for the address from which the register is
10763 being loaded. This reloc will tell us which function is
10764 actually being called. */
10765 cond_irel =
10766 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10767 R_NDS32_25_PCREL_RELA, irel->r_addend);
10768 if (cond_irel == irelend)
10769 {
695344c0 10770 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
2dcf00ce 10771 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10772 return FALSE;
10773 }
35c08157 10774
1c8f6a4d 10775 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10776 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 10777
fbaf61ad
NC
10778 if (foff == 0
10779 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10780 || foff >= CONSERVATIVE_16BIT_S1)
10781 return FALSE;
35c08157 10782
1c8f6a4d 10783 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10784 or bltzal rt, label ; 17_PCREL. */
35c08157 10785
1c8f6a4d
KLC
10786 /* Convert to complimentary conditional call. */
10787 insn = CONVERT_CONDITION_CALL (insn);
35c08157 10788
1c8f6a4d
KLC
10789 /* For simplicity of coding, we are going to modify the section
10790 contents, the section relocs, and the BFD symbol table. We
10791 must tell the rest of the code not to free up this
10792 information. It would be possible to instead create a table
10793 of changes which have to be made, as is done in coff-mips.c;
10794 that would be more work, but would require less memory when
10795 the linker is run. */
35c08157 10796
1c8f6a4d
KLC
10797 /* Modify relocation and contents. */
10798 cond_irel->r_info =
10799 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
35c08157 10800
1c8f6a4d
KLC
10801 /* Replace the long call with a bgezal. */
10802 bfd_putb32 (insn, contents + cond_irel->r_offset);
10803 *insn_len = 0;
35c08157 10804
1c8f6a4d
KLC
10805 /* Clean unnessary relocations. */
10806 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10807
1c8f6a4d
KLC
10808 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10809 R_NDS32_17_PCREL_RELA, laddr);
10810 cond_irel->r_info =
10811 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10812
1c8f6a4d
KLC
10813 return TRUE;
10814}
35c08157 10815
1c8f6a4d 10816/* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
35c08157 10817
1c8f6a4d
KLC
10818static bfd_boolean
10819nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10820 Elf_Internal_Rela *internal_relocs, int *insn_len,
10821 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10822 Elf_Internal_Shdr *symtab_hdr)
10823{
10824 /* The pattern for LONGCALL6.
10825 bltz rt, .L1 ; LONGCALL6/17_PCREL
10826 sethi ta, hi20(symbol) ; HI20/PTR
10827 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10828 jral ta ; PTR_RES/EMPTY/INSN16
10829 .L1 */
10830
10831 bfd_vma laddr;
10832 uint32_t insn;
10833 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
1c8f6a4d 10834 bfd_signed_vma foff;
35c08157 10835
1c8f6a4d
KLC
10836 irelend = internal_relocs + sec->reloc_count;
10837 laddr = irel->r_offset;
35c08157 10838
1c8f6a4d
KLC
10839 /* Get the reloc for the address from which the register is
10840 being loaded. This reloc will tell us which function is
10841 actually being called. */
10842 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10843 R_NDS32_EMPTY, irel->r_addend);
35c08157 10844
1c8f6a4d
KLC
10845 if (em_irel == irelend)
10846 {
695344c0 10847 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
2dcf00ce 10848 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10849 return FALSE;
10850 }
35c08157 10851
1c8f6a4d 10852 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10853 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 10854
fbaf61ad
NC
10855 if (foff == 0
10856 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10857 || foff >= CONSERVATIVE_24BIT_S1)
10858 return FALSE;
35c08157 10859
1c8f6a4d
KLC
10860 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10861 insn = bfd_getb32 (contents + irel->r_addend);
10862 if (insn & 0x80000000)
10863 return FALSE;
35c08157 10864
1c8f6a4d
KLC
10865 insn = bfd_getb32 (contents + laddr);
10866 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10867 {
10868 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10869 or bltzal rt, label ; 17_PCREL. */
35c08157 10870
1c8f6a4d
KLC
10871 /* Convert to complimentary conditional call. */
10872 *insn_len = 0;
10873 insn = CONVERT_CONDITION_CALL (insn);
10874 bfd_putb32 (insn, contents + em_irel->r_offset);
10875
10876 em_irel->r_info =
10877 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10878
10879 /* Set resolved relocation. */
10880 cond_irel =
10881 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10882 R_NDS32_PTR_RESOLVED, irel->r_addend);
10883 if (cond_irel == irelend)
35c08157 10884 {
695344c0 10885 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10886 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10887 return FALSE;
10888 }
10889 cond_irel->r_addend = 1;
35c08157 10890
1c8f6a4d 10891 /* Clear relocations. */
35c08157 10892
1c8f6a4d
KLC
10893 irel->r_info =
10894 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10895
1c8f6a4d
KLC
10896 cond_irel =
10897 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10898 R_NDS32_17_PCREL_RELA, laddr);
10899 if (cond_irel != irelend)
10900 cond_irel->r_info =
10901 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10902
1c8f6a4d
KLC
10903 cond_irel =
10904 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10905 R_NDS32_INSN16, irel->r_addend);
10906 if (cond_irel != irelend)
10907 cond_irel->r_info =
10908 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10909
1c8f6a4d
KLC
10910 }
10911 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10912 {
10913 /* Relax to the following instruction sequence
10914 bltz rt, .L1 ; LONGCALL2/17_PCREL
10915 jal symbol ; 25_PCREL/PTR_RES
10916 .L1 */
10917 *insn_len = 4;
10918 /* Convert instruction. */
10919 insn = INSN_JAL;
10920 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10921
1c8f6a4d
KLC
10922 /* Convert relocations. */
10923 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10924 R_NDS32_25_PCREL_RELA);
10925 irel->r_info =
10926 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
35c08157 10927
1c8f6a4d
KLC
10928 /* Set resolved relocation. */
10929 cond_irel =
10930 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10931 R_NDS32_PTR_RESOLVED, irel->r_addend);
10932 if (cond_irel == irelend)
10933 {
695344c0 10934 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10935 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10936 return FALSE;
10937 }
10938 cond_irel->r_addend = 1;
35c08157 10939
1c8f6a4d
KLC
10940 cond_irel =
10941 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10942 R_NDS32_INSN16, irel->r_addend);
10943 if (cond_irel != irelend)
10944 cond_irel->r_info =
10945 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10946 }
10947 return TRUE;
10948}
35c08157 10949
1c8f6a4d 10950/* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
35c08157 10951
1c8f6a4d
KLC
10952static bfd_boolean
10953nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10954 Elf_Internal_Rela *internal_relocs, int *insn_len,
10955 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10956 Elf_Internal_Shdr *symtab_hdr)
10957{
10958 /* The pattern for LONGJUMP4.
10959 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10960 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10961 jr ta ; PTR_RES/INSN16/EMPTY */
10962
10963 bfd_vma laddr;
10964 int seq_len; /* Original length of instruction sequence. */
10965 uint32_t insn;
10966 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
1c8f6a4d 10967 bfd_signed_vma foff;
35c08157 10968
1c8f6a4d
KLC
10969 irelend = internal_relocs + sec->reloc_count;
10970 seq_len = GET_SEQ_LEN (irel->r_addend);
10971 laddr = irel->r_offset;
10972 *insn_len = seq_len;
35c08157 10973
1c8f6a4d
KLC
10974 /* Get the reloc for the address from which the register is
10975 being loaded. This reloc will tell us which function is
10976 actually being called. */
35c08157 10977
1c8f6a4d
KLC
10978 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10979 R_NDS32_HI20_RELA, laddr);
35c08157 10980
1c8f6a4d
KLC
10981 if (hi_irel == irelend)
10982 {
695344c0 10983 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 10984 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10985 return FALSE;
10986 }
35c08157 10987
1c8f6a4d 10988 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10989 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10990
fbaf61ad
NC
10991 if (foff == 0
10992 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10993 || foff < -CONSERVATIVE_24BIT_S1)
10994 return FALSE;
35c08157 10995
1c8f6a4d
KLC
10996 /* Convert it to "j label", it may be converted to j8 in the final
10997 pass of relaxation. Therefore, we do not consider this currently. */
10998 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10999 R_NDS32_PTR_RESOLVED, irel->r_addend);
11000 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11001 R_NDS32_EMPTY, irel->r_addend);
35c08157 11002
1c8f6a4d
KLC
11003 if (ptr_irel == irelend || em_irel == irelend)
11004 {
695344c0 11005 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 11006 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11007 return FALSE;
11008 }
35c08157 11009
1c8f6a4d
KLC
11010 em_irel->r_info =
11011 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
11012 ptr_irel->r_addend = 1;
35c08157 11013
1c8f6a4d
KLC
11014 /* Write instruction. */
11015 insn = INSN_J;
11016 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 11017
1c8f6a4d
KLC
11018 /* Clear relocations. */
11019 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11020
1c8f6a4d
KLC
11021 /* If there is function cse, HI20 can not remove now. */
11022 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11023 R_NDS32_LONGJUMP4, laddr);
11024 if (call_irel == irelend)
11025 {
11026 *insn_len = 0;
11027 hi_irel->r_info =
11028 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11029 }
35c08157 11030
1c8f6a4d
KLC
11031 return TRUE;
11032}
35c08157 11033
1c8f6a4d 11034/* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
35c08157 11035
1c8f6a4d
KLC
11036static bfd_boolean
11037nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11038 Elf_Internal_Rela *internal_relocs, int *insn_len,
11039 int *seq_len, bfd_byte *contents,
11040 Elf_Internal_Sym *isymbuf,
11041 Elf_Internal_Shdr *symtab_hdr)
11042{
11043 /* There are 2 variations for LONGJUMP5
11044 case 2-4; 1st insn convertible, 16-bit on.
11045 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11046 j label ; 25_PCREL/INSN16
11047 $1:
11048
11049 case 4-4; 1st insn not convertible
11050 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11051 j label ; 25_PCREL/INSN16
11052 .L1: */
11053
11054 bfd_vma laddr;
11055 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d
KLC
11056 unsigned int i;
11057 bfd_signed_vma foff;
11058 uint32_t insn, re_insn = 0;
11059 uint16_t insn16, re_insn16 = 0;
11060 unsigned long reloc;
35c08157 11061
1c8f6a4d
KLC
11062 enum elf_nds32_reloc_type checked_types[] =
11063 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11064 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
35c08157 11065
1c8f6a4d
KLC
11066 irelend = internal_relocs + sec->reloc_count;
11067 laddr = irel->r_offset;
35c08157 11068
1c8f6a4d
KLC
11069 /* Get the reloc for the address from which the register is
11070 being loaded. This reloc will tell us which function is
11071 actually being called. */
35c08157 11072
1c8f6a4d
KLC
11073 cond_irel =
11074 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11075 R_NDS32_25_PCREL_RELA, irel->r_addend);
11076 if (cond_irel == irelend)
11077 {
695344c0 11078 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
2dcf00ce 11079 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11080 return FALSE;
11081 }
35c08157 11082
1c8f6a4d 11083 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11084 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11085
fbaf61ad
NC
11086 if (foff == 0
11087 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
11088 || foff >= CONSERVATIVE_16BIT_S1)
11089 return FALSE;
35c08157 11090
1c8f6a4d
KLC
11091 /* Get the all corresponding instructions. */
11092 insn = bfd_getb32 (contents + laddr);
11093 /* Check instruction size. */
11094 if (insn & 0x80000000)
11095 {
11096 *seq_len = 0;
11097 insn16 = insn >> 16;
11098 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11099 }
11100 else
11101 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11102
1c8f6a4d
KLC
11103 if (N32_OP6 (re_insn) == N32_OP6_BR1
11104 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11105 {
11106 /* beqs label ; 15_PCREL. */
11107 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11108 reloc = R_NDS32_15_PCREL_RELA;
11109 }
11110 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11111 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11112 {
11113 /* beqz label ; 17_PCREL. */
11114 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11115 reloc = R_NDS32_17_PCREL_RELA;
11116 }
11117 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11118 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11119 {
11120 /* beqc label ; 9_PCREL. */
11121 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11122 reloc = R_NDS32_WORD_9_PCREL_RELA;
11123 }
11124 else
11125 return FALSE;
35c08157 11126
1c8f6a4d
KLC
11127 /* Set all relocations. */
11128 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
35c08157 11129
1c8f6a4d
KLC
11130 /* Clean relocations. */
11131 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
fbaf61ad 11132 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11133 {
11134 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11135 checked_types[i], laddr);
11136 if (cond_irel != irelend)
11137 {
11138 if (*seq_len == 0
11139 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
35c08157 11140 {
1c8f6a4d
KLC
11141 /* If the branch instruction is 2 byte, it cannot remove
11142 directly. Only convert it to nop16 and remove it after
11143 checking alignment issue. */
11144 insn16 = NDS32_NOP16;
11145 bfd_putb16 (insn16, contents + laddr);
11146 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157
KLC
11147 }
11148 else
1c8f6a4d
KLC
11149 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11150 R_NDS32_NONE);
35c08157 11151 }
1c8f6a4d
KLC
11152 }
11153 *insn_len = 0;
35c08157 11154
1c8f6a4d
KLC
11155 return TRUE;
11156}
35c08157 11157
1c8f6a4d 11158/* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
35c08157 11159
1c8f6a4d
KLC
11160static bfd_boolean
11161nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11162 Elf_Internal_Rela *internal_relocs, int *insn_len,
11163 int *seq_len, bfd_byte *contents,
11164 Elf_Internal_Sym *isymbuf,
11165 Elf_Internal_Shdr *symtab_hdr)
11166{
11167 /* There are 5 variations for LONGJUMP6
11168 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11169 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11170 sethi ta, hi20(symbol) ; HI20/PTR
11171 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11172 jr ta ; PTR_RES/INSN16/EMPTY
11173 .L1:
11174
11175 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11176 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11177 sethi ta, hi20(symbol) ; HI20/PTR
11178 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11179 jr ta ; PTR_RES/INSN16/EMPTY
11180 .L1: */
11181
11182 enum elf_nds32_reloc_type checked_types[] =
11183 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11184 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11185
11186 int reloc_off = 0, cond_removed = 0;
11187 bfd_vma laddr;
11188 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11189 unsigned int i;
11190 bfd_signed_vma foff;
11191 uint32_t insn, re_insn = 0;
11192 uint16_t insn16, re_insn16 = 0;
11193 unsigned long reloc;
35c08157 11194
1c8f6a4d
KLC
11195 irelend = internal_relocs + sec->reloc_count;
11196 laddr = irel->r_offset;
35c08157 11197
1c8f6a4d
KLC
11198 /* Get the reloc for the address from which the register is
11199 being loaded. This reloc will tell us which function is
11200 actually being called. */
11201 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11202 R_NDS32_EMPTY, irel->r_addend);
35c08157 11203
1c8f6a4d
KLC
11204 if (em_irel == irelend)
11205 {
695344c0 11206 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
2dcf00ce 11207 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11208 return FALSE;
11209 }
35c08157 11210
1c8f6a4d 11211 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11212 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 11213
fbaf61ad
NC
11214 if (foff == 0
11215 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
11216 || foff >= CONSERVATIVE_24BIT_S1)
11217 return FALSE;
35c08157 11218
1c8f6a4d
KLC
11219 insn = bfd_getb32 (contents + laddr);
11220 /* Check instruction size. */
11221 if (insn & 0x80000000)
11222 {
11223 *seq_len = 0;
11224 insn16 = insn >> 16;
11225 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11226 }
11227 else
11228 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11229
1c8f6a4d
KLC
11230 /* For simplicity of coding, we are going to modify the section
11231 contents, the section relocs, and the BFD symbol table. We
11232 must tell the rest of the code not to free up this
11233 information. It would be possible to instead create a table
11234 of changes which have to be made, as is done in coff-mips.c;
11235 that would be more work, but would require less memory when
11236 the linker is run. */
35c08157 11237
1c8f6a4d
KLC
11238 if (N32_OP6 (re_insn) == N32_OP6_BR1
11239 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11240 {
fbaf61ad 11241 /* beqs label ; 15_PCREL. */
1c8f6a4d
KLC
11242 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11243 reloc = R_NDS32_15_PCREL_RELA;
11244 cond_removed = 1;
11245 }
11246 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11247 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11248 {
fbaf61ad 11249 /* beqz label ; 17_PCREL. */
1c8f6a4d
KLC
11250 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11251 reloc = R_NDS32_17_PCREL_RELA;
11252 cond_removed = 1;
11253 }
11254 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11255 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11256 {
11257 /* Relax to one of the following 2 variations
35c08157 11258
1c8f6a4d
KLC
11259 case 2-4; 1st insn convertible, 16-bit on.
11260 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11261 j label ; 25_PCREL/INSN16
11262 $1:
35c08157 11263
1c8f6a4d
KLC
11264 case 4-4; 1st insn not convertible
11265 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11266 j label ; 25_PCREL/INSN16
11267 .L1: */
35c08157 11268
1c8f6a4d
KLC
11269 /* Use j label as second instruction. */
11270 insn = INSN_J;
11271 reloc = R_NDS32_25_PCREL_RELA;
11272 bfd_putb32 (insn, contents + em_irel->r_offset);
11273 }
11274 else
11275 return FALSE;
35c08157 11276
1c8f6a4d
KLC
11277 /* Set all relocations. */
11278 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
35c08157 11279
1c8f6a4d
KLC
11280 cond_irel =
11281 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11282 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11283 cond_irel->r_addend = 1;
35c08157 11284
1c8f6a4d
KLC
11285 /* Use INSN16 of first branch instruction to distinguish if keeping
11286 INSN16 of final instruction or not. */
11287 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11288 R_NDS32_INSN16, irel->r_offset);
11289 if (insn_irel == irelend)
11290 {
11291 /* Clean the final INSN16. */
11292 insn_irel =
11293 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11294 R_NDS32_INSN16, em_irel->r_offset);
11295 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11296 R_NDS32_NONE);
11297 }
11298
11299 if (cond_removed == 1)
11300 {
11301 *insn_len = 0;
11302
11303 /* Clear relocations. */
11304 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11305
fbaf61ad 11306 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11307 {
11308 cond_irel =
11309 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11310 checked_types[i], laddr);
11311 if (cond_irel != irelend)
35c08157 11312 {
1c8f6a4d
KLC
11313 if (*seq_len == 0
11314 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11315 {
11316 /* If the branch instruction is 2 byte, it cannot remove
11317 directly. Only convert it to nop16 and remove it after
11318 checking alignment issue. */
11319 insn16 = NDS32_NOP16;
11320 bfd_putb16 (insn16, contents + laddr);
11321 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11322 }
11323 else
11324 cond_irel->r_info =
11325 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 11326 }
35c08157 11327 }
1c8f6a4d
KLC
11328 }
11329 else
11330 {
11331 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11332 R_NDS32_LONGJUMP5);
11333 }
35c08157 11334
1c8f6a4d
KLC
11335 return TRUE;
11336}
35c08157 11337
1c8f6a4d 11338/* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
35c08157 11339
1c8f6a4d
KLC
11340static bfd_boolean
11341nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11342 Elf_Internal_Rela *internal_relocs, int *insn_len,
11343 int *seq_len, bfd_byte *contents,
11344 Elf_Internal_Sym *isymbuf,
11345 Elf_Internal_Shdr *symtab_hdr)
11346{
11347 /* There are 2 variations for LONGJUMP5
11348 case 2-4; 1st insn convertible, 16-bit on.
11349 movi55 ta, imm11 ; LONGJUMP7/INSN16
11350 beq rt, ta, label ; 15_PCREL
11351
11352 case 4-4; 1st insn not convertible
11353 movi55 ta, imm11 ; LONGJUMP7/INSN16
11354 beq rt, ta, label ; 15_PCREL */
11355
11356 bfd_vma laddr;
11357 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11358 bfd_signed_vma foff;
11359 uint32_t insn, re_insn = 0;
11360 uint16_t insn16;
11361 uint32_t imm11;
35c08157 11362
1c8f6a4d
KLC
11363 irelend = internal_relocs + sec->reloc_count;
11364 laddr = irel->r_offset;
35c08157 11365
1c8f6a4d
KLC
11366 /* Get the reloc for the address from which the register is
11367 being loaded. This reloc will tell us which function is
11368 actually being called. */
35c08157 11369
1c8f6a4d
KLC
11370 cond_irel =
11371 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11372 R_NDS32_15_PCREL_RELA, irel->r_addend);
11373 if (cond_irel == irelend)
11374 {
695344c0 11375 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
2dcf00ce 11376 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11377 return FALSE;
11378 }
35c08157 11379
1c8f6a4d 11380 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11381 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11382
fbaf61ad
NC
11383 if (foff == 0
11384 || foff < -CONSERVATIVE_8BIT_S1
1c8f6a4d
KLC
11385 || foff >= CONSERVATIVE_8BIT_S1)
11386 return FALSE;
35c08157 11387
1c8f6a4d
KLC
11388 /* Get the first instruction for its size. */
11389 insn = bfd_getb32 (contents + laddr);
11390 if (insn & 0x80000000)
11391 {
11392 *seq_len = 0;
11393 /* Get the immediate from movi55. */
11394 imm11 = N16_IMM5S (insn >> 16);
11395 }
11396 else
11397 {
11398 /* Get the immediate from movi. */
11399 imm11 = N32_IMM20S (insn);
35c08157
KLC
11400 }
11401
1c8f6a4d
KLC
11402 /* Get the branch instruction. */
11403 insn = bfd_getb32 (contents + irel->r_addend);
11404 /* Convert instruction to BR3. */
11405 if ((insn >> 14) & 0x1)
11406 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11407 else
11408 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
35c08157 11409
1c8f6a4d 11410 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
35c08157 11411
1c8f6a4d
KLC
11412 /* Set all relocations. */
11413 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11414 R_NDS32_WORD_9_PCREL_RELA);
11415
11416 /* Clean relocations. */
11417 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11418 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11419 R_NDS32_INSN16, irel->r_offset);
11420 if (insn_irel != irelend)
11421 {
11422 if (*seq_len == 0)
35c08157 11423 {
1c8f6a4d
KLC
11424 /* If the first insntruction is 16bit, convert it to nop16. */
11425 insn16 = NDS32_NOP16;
11426 bfd_putb16 (insn16, contents + laddr);
11427 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157 11428 }
1c8f6a4d
KLC
11429 else
11430 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11431 R_NDS32_NONE);
35c08157 11432 }
1c8f6a4d 11433 *insn_len = 0;
35c08157 11434
1c8f6a4d
KLC
11435 return TRUE;
11436}
35c08157 11437
fbaf61ad
NC
11438/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11439 for each relax round. But the gp may changed dramatically and then cause
11440 the truncated to fit errors for the the converted gp instructions.
11441 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11442
11443static bfd_boolean
11444nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11445 Elf_Internal_Rela *irel, bfd_boolean *again,
11446 bfd_boolean init,
11447 struct elf_nds32_link_hash_table *table,
11448 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11449
11450{
11451 int offset_to_gp;
11452 static bfd_boolean sec_pass = FALSE;
11453 static asection *first_sec = NULL, *sym_sec;
11454 /* Record the number of instructions which may be removed. */
11455 static int count = 0, record_count;
11456 Elf_Internal_Sym *isym;
11457 struct elf_link_hash_entry *h = NULL;
11458 int indx;
11459 unsigned long r_symndx;
11460 bfd *abfd = sec->owner;
11461 static bfd_vma record_sda = 0;
11462 int sda_offset = 0;
11463
11464 /* Force doing relaxation when hyper-relax is high. */
11465 if (table->hyper_relax == 2)
11466 return TRUE;
11467
11468 /* Do not relax the load/store patterns for the first
11469 relax round. */
11470 if (init)
11471 {
11472 if (!first_sec)
11473 first_sec = sec;
11474 else if (first_sec == sec)
11475 {
11476 record_count = count;
11477 count = 0;
11478 sec_pass = TRUE;
11479 }
11480
11481 if (!sec_pass)
11482 *again = TRUE;
11483
11484 return TRUE;
11485 }
11486
11487 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11488 DATA_SEGMENT_ALIGN size in the linker script may make it
11489 get even bigger. */
11490 if (record_sda == 0)
11491 record_sda = local_sda;
11492 else if (local_sda > record_sda)
11493 sda_offset = local_sda - record_sda;
11494
11495 /* Assume the instruction will be removed in the best case. */
11496 count++;
11497
11498 /* We record the offset to gp for each symbol, and then check
11499 if it is changed dramatically after relaxing.
11500 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11501 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11502 r_symndx = ELF32_R_SYM (irel->r_info);
11503 if (r_symndx >= symtab_hdr->sh_info)
11504 {
11505 /* Global symbols. */
11506 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11507 h = elf_sym_hashes (abfd)[indx];
11508 sym_sec = h->root.u.def.section;
11509 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11510 || bfd_is_abs_section (sym_sec))
11511 {
11512 /* Forbid doing relaxation when hyper-relax is low. */
11513 if (table->hyper_relax == 0)
11514 return FALSE;
11515
11516 offset_to_gp = *access_addr - local_sda;
11517 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11518 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11519 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11520 < abs (offset_to_gp) - sda_offset)
11521 {
11522 /* This may cause the error, so we reserve the
11523 safe enough size for relaxing. */
11524 if (*access_addr >= local_sda)
11525 *access_addr += (record_count * 4);
11526 else
11527 *access_addr -= (record_count * 4);
11528 }
11529 return sec_pass;
11530 }
11531 }
11532 else
11533 {
11534 /* Local symbols. */
11535 if (!elf32_nds32_allocate_local_sym_info (abfd))
11536 return FALSE;
11537 isym = isymbuf + r_symndx;
11538
11539 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11540 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11541 {
11542 /* Forbid doing relaxation when hyper-relax is low. */
11543 if (table->hyper_relax == 0)
11544 return FALSE;
11545
11546 offset_to_gp = *access_addr - local_sda;
11547 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11548 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11549 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11550 < abs (offset_to_gp) - sda_offset)
11551 {
11552 /* This may cause the error, so we reserve the
11553 safe enough size for relaxing. */
11554 if (*access_addr >= local_sda)
11555 *access_addr += (record_count * 4);
11556 else
11557 *access_addr -= (record_count * 4);
11558 }
11559 return sec_pass;
11560 }
11561 }
11562
11563 return TRUE;
11564}
11565
1c8f6a4d 11566#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
35c08157 11567
1c8f6a4d 11568/* Relax LOADSTORE relocation for nds32_elf_relax_section. */
35c08157 11569
1c8f6a4d
KLC
11570static bfd_boolean
11571nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11572 asection *sec, Elf_Internal_Rela *irel,
11573 Elf_Internal_Rela *internal_relocs, int *insn_len,
11574 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
fbaf61ad
NC
11575 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11576 struct elf_nds32_link_hash_table *table)
1c8f6a4d 11577{
6cae483a
AM
11578 int eliminate_sethi = 0, range_type;
11579 unsigned int i;
1c8f6a4d
KLC
11580 bfd_vma local_sda, laddr;
11581 int seq_len; /* Original length of instruction sequence. */
11582 uint32_t insn;
11583 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11584 bfd_vma access_addr = 0;
11585 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
fbaf61ad
NC
11586 struct elf_link_hash_entry *h = NULL;
11587 int indx;
1c8f6a4d
KLC
11588 enum elf_nds32_reloc_type checked_types[] =
11589 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11590 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11591 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11592 R_NDS32_TLS_LE_HI20
11593 };
35c08157 11594
1c8f6a4d
KLC
11595 irelend = internal_relocs + sec->reloc_count;
11596 seq_len = GET_SEQ_LEN (irel->r_addend);
11597 laddr = irel->r_offset;
11598 *insn_len = seq_len;
35c08157 11599
1c8f6a4d 11600 /* Get the high part relocation. */
6cae483a 11601 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11602 {
11603 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11604 checked_types[i], laddr);
11605 if (hi_irelfn != irelend)
11606 break;
11607 }
35c08157 11608
1c8f6a4d
KLC
11609 if (hi_irelfn == irelend)
11610 {
fbaf61ad
NC
11611 /* Not R_NDS32_HI20_RELA. */
11612 if (i != 0)
11613 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11614 (uint64_t) irel->r_offset);
11615 return FALSE;
1c8f6a4d 11616 }
35c08157 11617
1c8f6a4d
KLC
11618 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11619 nds32_elf_final_sda_base (sec->output_section->owner,
11620 link_info, &local_sda, FALSE);
35c08157 11621
1c8f6a4d
KLC
11622 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11623 {
11624 case R_NDS32_HI20_RELA:
11625 insn = bfd_getb32 (contents + laddr);
11626 access_addr =
11627 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11628
fbaf61ad 11629 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
1c8f6a4d 11630 {
fbaf61ad
NC
11631 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11632 h = elf_sym_hashes (abfd)[indx];
11633 }
35c08157 11634
fbaf61ad
NC
11635 /* Try movi. */
11636 if (range_type == NDS32_LOADSTORE_IMM
11637 && access_addr < CONSERVATIVE_20BIT
11638 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11639 {
11640 eliminate_sethi = 1;
11641 break;
11642 }
1c8f6a4d 11643
fbaf61ad
NC
11644 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11645 {
11646 eliminate_sethi = 1;
11647 break;
1c8f6a4d 11648 }
fbaf61ad
NC
11649 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11650 NULL, FALSE, table, isymbuf, symtab_hdr))
11651 return FALSE;
1c8f6a4d
KLC
11652
11653 if (!load_store_relax)
11654 return FALSE;
11655
11656 /* Case for set gp register. */
11657 if (N32_RT5 (insn) == REG_GP)
fbaf61ad 11658 return FALSE;
1c8f6a4d
KLC
11659
11660 if (range_type == NDS32_LOADSTORE_FLOAT_S
6cef73f9 11661 || range_type == NDS32_LOADSTORE_FLOAT_D)
1c8f6a4d
KLC
11662 {
11663 range_l = sdata_range[0][0];
11664 range_h = sdata_range[0][1];
11665 }
11666 else
11667 {
11668 range_l = sdata_range[1][0];
11669 range_h = sdata_range[1][1];
11670 }
11671 break;
11672
1c8f6a4d
KLC
11673 default:
11674 return FALSE;
11675 }
11676
11677 /* Delete sethi instruction. */
11678 if (eliminate_sethi == 1
11679 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11680 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11681 {
11682 hi_irelfn->r_info =
11683 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11684 irel->r_info =
11685 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11686 *insn_len = 0;
fbaf61ad 11687 return TRUE;
1c8f6a4d 11688 }
fbaf61ad
NC
11689
11690 return FALSE;
1c8f6a4d
KLC
11691}
11692
11693/* Relax LO12 relocation for nds32_elf_relax_section. */
11694
11695static void
11696nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11697 asection *sec, Elf_Internal_Rela *irel,
11698 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
fbaf61ad
NC
11699 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11700 struct elf_nds32_link_hash_table *table)
1c8f6a4d
KLC
11701{
11702 uint32_t insn;
11703 bfd_vma local_sda, laddr;
11704 unsigned long reloc;
11705 bfd_vma access_addr;
11706 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11707 Elf_Internal_Rela *irelfn = NULL, *irelend;
11708 struct elf_link_hash_entry *h = NULL;
11709 int indx;
11710
11711 /* For SDA base relative relaxation. */
11712 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11713 &local_sda, FALSE);
11714
11715 irelend = internal_relocs + sec->reloc_count;
11716 laddr = irel->r_offset;
11717 insn = bfd_getb32 (contents + laddr);
11718
11719 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11720 return;
11721
11722 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11723
11724 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11725 {
11726 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11727 h = elf_sym_hashes (abfd)[indx];
11728 }
11729
fbaf61ad 11730 /* Try movi. */
1c8f6a4d
KLC
11731 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11732 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11733 {
11734 reloc = R_NDS32_20_RELA;
11735 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11736 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11737 bfd_putb32 (insn, contents + laddr);
11738 }
1c8f6a4d
KLC
11739 else
11740 {
fbaf61ad
NC
11741 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11742 {
11743 /* Fall through. */
11744 }
11745 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11746 FALSE, table, isymbuf, symtab_hdr))
11747 return;
11748
1c8f6a4d
KLC
11749 range_l = sdata_range[1][0];
11750 range_h = sdata_range[1][1];
11751 switch (ELF32_R_TYPE (irel->r_info))
11752 {
11753 case R_NDS32_LO12S0_RELA:
11754 reloc = R_NDS32_SDA19S0_RELA;
11755 break;
11756 case R_NDS32_LO12S1_RELA:
11757 reloc = R_NDS32_SDA18S1_RELA;
11758 break;
11759 case R_NDS32_LO12S2_RELA:
11760 reloc = R_NDS32_SDA17S2_RELA;
11761 break;
11762 case R_NDS32_LO12S2_DP_RELA:
11763 range_l = sdata_range[0][0];
11764 range_h = sdata_range[0][1];
11765 reloc = R_NDS32_SDA12S2_DP_RELA;
11766 break;
11767 case R_NDS32_LO12S2_SP_RELA:
11768 range_l = sdata_range[0][0];
11769 range_h = sdata_range[0][1];
11770 reloc = R_NDS32_SDA12S2_SP_RELA;
11771 break;
11772 default:
11773 return;
11774 }
11775
11776 /* There are range_h and range_l because linker has to promise
11777 all sections move cross one page together. */
11778 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
fbaf61ad
NC
11779 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11780 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
1c8f6a4d
KLC
11781 {
11782 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
35c08157 11783 {
1c8f6a4d
KLC
11784 /* Maybe we should add R_NDS32_INSN16 reloc type here
11785 or manually do some optimization. sethi can't be
11786 eliminated when updating $gp so the relative ori
11787 needs to be preserved. */
11788 return;
35c08157 11789 }
1c8f6a4d
KLC
11790 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11791 &insn))
11792 return;
11793 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11794 bfd_putb32 (insn, contents + laddr);
11795
11796 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11797 R_NDS32_INSN16);
11798 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11799 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11800 irelfn->r_info =
11801 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11802
35c08157 11803 }
1c8f6a4d
KLC
11804 }
11805 return;
11806}
35c08157 11807
1c8f6a4d 11808/* Relax PTR relocation for nds32_elf_relax_section. */
35c08157 11809
1c8f6a4d
KLC
11810static bfd_boolean
11811nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11812 Elf_Internal_Rela *internal_relocs, int *insn_len,
11813 int *seq_len, bfd_byte *contents)
11814{
11815 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
35c08157 11816
1c8f6a4d 11817 irelend = internal_relocs + sec->reloc_count;
35c08157 11818
1c8f6a4d
KLC
11819 re_irel =
11820 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11821 R_NDS32_PTR_RESOLVED, irel->r_addend);
35c08157 11822
1c8f6a4d
KLC
11823 if (re_irel == irelend)
11824 {
695344c0 11825 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
2dcf00ce 11826 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11827 return FALSE;
11828 }
35c08157 11829
1c8f6a4d
KLC
11830 if (re_irel->r_addend != 1)
11831 return FALSE;
35c08157 11832
1c8f6a4d
KLC
11833 /* Pointed target is relaxed and no longer needs this void *,
11834 change the type to NONE. */
11835 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11836
1c8f6a4d
KLC
11837 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11838 not exist, it means only count 1 and remove it directly. */
11839 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11840 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11841 R_NDS32_PTR_COUNT);
11842 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11843 R_NDS32_PTR);
11844 if (count_irel != irelend)
11845 {
11846 if (--count_irel->r_addend > 0)
11847 return FALSE;
11848 }
11849
11850 if (ptr_irel != irelend)
11851 return FALSE;
11852
11853 /* If the PTR_COUNT is already 0, remove current instruction. */
11854 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11855 *insn_len = 0;
11856 return TRUE;
11857}
11858
fbaf61ad 11859/* Relax LWC relocation for nds32_elf_relax_section. */
1c8f6a4d
KLC
11860
11861static void
fbaf61ad
NC
11862nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11863 asection *sec, Elf_Internal_Rela *irel,
11864 Elf_Internal_Rela *internal_relocs,
11865 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11866 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
1c8f6a4d 11867{
fbaf61ad
NC
11868 /* Pattern:
11869 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11870 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11871 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11872 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11873 ... */
11874
1c8f6a4d 11875 uint32_t insn;
1c8f6a4d 11876 bfd_vma local_sda, laddr;
fbaf61ad
NC
11877 unsigned long reloc;
11878 bfd_vma access_addr, flsi_offset;
11879 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11880 Elf_Internal_Rela *irelend, *re_irel;
11881 unsigned int opcode;
1c8f6a4d
KLC
11882
11883 irelend = internal_relocs + sec->reloc_count;
11884 laddr = irel->r_offset;
11885 insn = bfd_getb32 (contents + laddr);
11886
fbaf61ad 11887 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
1c8f6a4d
KLC
11888 return;
11889
fbaf61ad
NC
11890 /* Can not do relaxation for bi format. */
11891 if ((insn & 0x1000))
1c8f6a4d
KLC
11892 return;
11893
fbaf61ad
NC
11894 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11895 opcode = N32_OP6 (insn);
11896 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11897 reloc = R_NDS32_SDA12S2_SP_RELA;
11898 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11899 reloc = R_NDS32_SDA12S2_DP_RELA;
1c8f6a4d
KLC
11900 else
11901 return;
11902
fbaf61ad
NC
11903 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11904 R_NDS32_PTR_RESOLVED);
11905 if (re_irel == irelend)
1c8f6a4d 11906 {
fbaf61ad
NC
11907 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11908 (uint64_t) irel->r_offset);
11909 return;
1c8f6a4d 11910 }
1c8f6a4d 11911
fbaf61ad 11912 /* For SDA base relative relaxation. */
1c8f6a4d
KLC
11913 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11914 &local_sda, FALSE);
fbaf61ad
NC
11915 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11916 flsi_offset = (insn & 0xfff) << 2;
11917 access_addr += flsi_offset;
11918 range_l = sdata_range[0][0];
11919 range_h = sdata_range[0][1];
1c8f6a4d 11920
fbaf61ad
NC
11921 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11922 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
1c8f6a4d 11923 {
fbaf61ad
NC
11924 /* Turn flsi instruction into sda access format. */
11925 insn = (insn & 0x7ff07000) | (REG_GP << 15);
1c8f6a4d 11926
fbaf61ad
NC
11927 /* Add relocation type to flsi. */
11928 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11929 irel->r_addend += flsi_offset;
11930 bfd_putb32 (insn, contents + re_irel->r_offset);
1c8f6a4d 11931
fbaf61ad 11932 re_irel->r_addend |= 1;
1c8f6a4d
KLC
11933 *again = TRUE;
11934 }
1c8f6a4d
KLC
11935}
11936
11937static bfd_boolean
11938nds32_relax_adjust_label (bfd *abfd, asection *sec,
11939 Elf_Internal_Rela *internal_relocs,
11940 bfd_byte *contents,
11941 nds32_elf_blank_t **relax_blank_list,
11942 int optimize, int opt_size)
11943{
11944 /* This code block is used to adjust 4-byte alignment by relax a pair
11945 of instruction a time.
11946
11947 It recognizes three types of relocations.
de194d85 11948 1. R_NDS32_LABEL - a alignment.
1c8f6a4d
KLC
11949 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11950 3. is_16bit_NOP () - remove a 16-bit instruction. */
11951
de194d85
YC
11952 /* TODO: It seems currently implementation only support 4-byte alignment.
11953 We should handle any-alignment. */
1c8f6a4d
KLC
11954
11955 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11956 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11957 Elf_Internal_Rela rel_temp;
11958 Elf_Internal_Rela *irelend;
11959 bfd_vma address;
11960 uint16_t insn16;
11961
11962 /* Checking for branch relaxation relies on the relocations to
11963 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11964 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11965 sizeof (Elf_Internal_Rela), compar_reloc);
11966
11967 irelend = internal_relocs + sec->reloc_count;
11968
11969 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11970 /* FIXME: Can we generate the right order in assembler?
11971 So we don't have to swapping them here. */
11972
11973 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11974 label_rel < irelend; label_rel++)
11975 {
11976 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11977 continue;
11978
11979 /* Find the first reloc has the same offset with label_rel. */
11980 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11981 insn_rel++;
11982
11983 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11984 insn_rel++)
11985 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11986 address. */
11987 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11988 break;
11989
11990 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11991 && insn_rel < label_rel)
11992 {
11993 /* Swap the two reloc if the R_NDS32_INSN16 is
11994 before R_NDS32_LABEL. */
11995 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11996 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11997 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11998 }
11999 }
12000
12001 label_rel = NULL;
12002 insn_rel = NULL;
12003 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
12004 or higher, remove other R_NDS32_LABEL with lower alignment.
12005 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
12006 then the R_NDS32_LABEL sequence is broke. */
12007 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
12008 {
12009 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
12010 {
12011 if (label_rel == NULL)
12012 {
12013 if (tmp_rel->r_addend < 2)
12014 label_rel = tmp_rel;
12015 continue;
12016 }
12017 else if (tmp_rel->r_addend > 1)
12018 {
12019 /* Remove all LABEL relocation from label_rel to tmp_rel
12020 including relocations with same offset as tmp_rel. */
fbaf61ad 12021 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
1c8f6a4d 12022 {
fbaf61ad
NC
12023 if (tmp2_rel->r_offset == tmp_rel->r_offset)
12024 break;
12025
1c8f6a4d
KLC
12026 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12027 && tmp2_rel->r_addend < 2)
12028 tmp2_rel->r_info =
12029 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12030 R_NDS32_NONE);
12031 }
12032 label_rel = NULL;
12033 }
12034 }
12035 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12036 {
12037 /* A new INSN16 which can be converted, so clear label_rel. */
12038 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12039 irelend, &insn16)
12040 || is_16bit_NOP (abfd, sec, tmp_rel))
12041 label_rel = NULL;
12042 }
12043 }
12044
12045 label_rel = NULL;
12046 insn_rel = NULL;
12047 /* Optimized for speed and nothing has not been relaxed.
12048 It's time to align labels.
12049 We may convert a 16-bit instruction right before a label to
12050 32-bit, in order to align the label if necessary
12051 all reloc entries has been sorted by r_offset. */
fbaf61ad
NC
12052 for (irel = internal_relocs;
12053 irel < irelend && irel->r_offset < sec->size; irel++)
1c8f6a4d
KLC
12054 {
12055 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12056 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12057 continue;
12058
12059 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12060 {
12061 /* A new INSN16 found, resize the old one. */
12062 if (is_convert_32_to_16
12063 (abfd, sec, irel, internal_relocs, irelend, &insn16)
12064 || is_16bit_NOP (abfd, sec, irel))
12065 {
12066 if (insn_rel)
12067 {
12068 /* Previous INSN16 reloc exists, reduce its
12069 size to 16-bit. */
12070 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12071 irelend, &insn16))
35c08157 12072 {
1c8f6a4d
KLC
12073 nds32_elf_write_16 (abfd, contents, insn_rel,
12074 internal_relocs, irelend, insn16);
12075
12076 if (!insert_nds32_elf_blank_recalc_total
12077 (relax_blank_list, insn_rel->r_offset + 2, 2))
12078 return FALSE;
12079 }
12080 else if (is_16bit_NOP (abfd, sec, insn_rel))
12081 {
12082 if (!insert_nds32_elf_blank_recalc_total
12083 (relax_blank_list, insn_rel->r_offset, 2))
12084 return FALSE;
12085 }
12086 insn_rel->r_info =
12087 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12088 }
12089 /* Save the new one for later use. */
12090 insn_rel = irel;
12091 }
12092 else
12093 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12094 R_NDS32_NONE);
12095 }
12096 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12097 {
12098 /* Search for label. */
12099 int force_relax = 0;
12100
12101 /* Label on 16-bit instruction or optimization
12102 needless, just reset this reloc. */
12103 insn16 = bfd_getb16 (contents + irel->r_offset);
12104 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12105 {
12106 irel->r_info =
12107 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12108 continue;
12109 }
12110
12111 address =
12112 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12113 irel->r_offset, 1);
12114
12115 if (!insn_rel)
12116 {
12117 /* Check if there is case which can not be aligned. */
12118 if (irel->r_addend == 2 && address & 0x2)
12119 return FALSE;
12120 continue;
12121 }
12122
12123 /* Try to align this label. */
12124
12125 if ((irel->r_addend & 0x1f) < 2)
12126 {
12127 /* Check if there is a INSN16 at the same address.
12128 Label_rel always seats before insn_rel after
12129 our sort. */
12130
12131 /* Search for INSN16 at LABEL location. If INSN16 is at
12132 same location and this LABEL alignment is lower than 2,
12133 the INSN16 can be converted to 2-byte. */
12134 for (tmp_rel = irel;
12135 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12136 tmp_rel++)
12137 {
12138 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12139 && (is_convert_32_to_16
12140 (abfd, sec, tmp_rel, internal_relocs,
12141 irelend, &insn16)
12142 || is_16bit_NOP (abfd, sec, tmp_rel)))
12143 {
12144 force_relax = 1;
12145 break;
12146 }
12147 }
12148 }
12149
12150 if (force_relax || irel->r_addend == 1 || address & 0x2)
12151 {
12152 /* Label not aligned. */
12153 /* Previous reloc exists, reduce its size to 16-bit. */
12154 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12155 internal_relocs, irelend, &insn16))
12156 {
12157 nds32_elf_write_16 (abfd, contents, insn_rel,
12158 internal_relocs, irelend, insn16);
12159
12160 if (!insert_nds32_elf_blank_recalc_total
12161 (relax_blank_list, insn_rel->r_offset + 2, 2))
12162 return FALSE;
12163 }
12164 else if (is_16bit_NOP (abfd, sec, insn_rel))
12165 {
12166 if (!insert_nds32_elf_blank_recalc_total
12167 (relax_blank_list, insn_rel->r_offset, 2))
12168 return FALSE;
12169 }
12170
12171 }
12172 /* INSN16 reloc is used. */
12173 insn_rel = NULL;
12174 }
12175 }
12176
12177 address =
12178 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12179 if (insn_rel && (address & 0x2 || opt_size))
12180 {
12181 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12182 irelend, &insn16))
12183 {
12184 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12185 irelend, insn16);
12186 if (!insert_nds32_elf_blank_recalc_total
12187 (relax_blank_list, insn_rel->r_offset + 2, 2))
12188 return FALSE;
12189 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12190 R_NDS32_NONE);
12191 }
12192 else if (is_16bit_NOP (abfd, sec, insn_rel))
12193 {
12194 if (!insert_nds32_elf_blank_recalc_total
12195 (relax_blank_list, insn_rel->r_offset, 2))
12196 return FALSE;
12197 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12198 R_NDS32_NONE);
12199 }
12200 }
12201 insn_rel = NULL;
12202 return TRUE;
12203}
12204
1c8f6a4d
KLC
12205static bfd_boolean
12206nds32_elf_relax_section (bfd *abfd, asection *sec,
12207 struct bfd_link_info *link_info, bfd_boolean *again)
12208{
12209 nds32_elf_blank_t *relax_blank_list = NULL;
12210 Elf_Internal_Shdr *symtab_hdr;
12211 Elf_Internal_Rela *internal_relocs;
12212 Elf_Internal_Rela *irel;
12213 Elf_Internal_Rela *irelend;
12214 Elf_Internal_Sym *isymbuf = NULL;
12215 bfd_byte *contents = NULL;
12216 bfd_boolean result = TRUE;
12217 int optimize = 0;
12218 int opt_size = 0;
12219 uint32_t insn;
12220 uint16_t insn16;
12221
12222 /* Target dependnet option. */
12223 struct elf_nds32_link_hash_table *table;
12224 int load_store_relax;
1c8f6a4d
KLC
12225
12226 relax_blank_list = NULL;
12227
12228 *again = FALSE;
12229
12230 /* Nothing to do for
12231 * relocatable link or
12232 * non-relocatable section or
12233 * non-code section or
12234 * empty content or
12235 * no reloc entry. */
0e1862bb 12236 if (bfd_link_relocatable (link_info)
1c8f6a4d 12237 || (sec->flags & SEC_RELOC) == 0
cd28e7aa 12238 || (sec->flags & SEC_EXCLUDE) != 0
1c8f6a4d 12239 || (sec->flags & SEC_CODE) == 0
fbaf61ad
NC
12240 || sec->size == 0
12241 || sec->reloc_count == 0)
1c8f6a4d
KLC
12242 return TRUE;
12243
12244 /* 09.12.11 Workaround. */
12245 /* We have to adjust align for R_NDS32_LABEL if needed.
12246 The adjust approach only can fix 2-byte align once. */
12247 if (sec->alignment_power > 2)
12248 return TRUE;
12249
fbaf61ad
NC
12250 /* Do TLS model conversion once at first. */
12251 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12252
1c8f6a4d
KLC
12253 /* The optimization type to do. */
12254
12255 table = nds32_elf_hash_table (link_info);
1c8f6a4d 12256
fbaf61ad
NC
12257 /* Save the first section for abs symbol relaxation.
12258 This is used for checking gp relaxation in the
12259 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12260 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12261 table, NULL, NULL);
12262
1c8f6a4d
KLC
12263 /* The begining of general relaxation. */
12264
12265 if (is_SDA_BASE_set == 0)
12266 {
12267 bfd_vma gp;
12268 is_SDA_BASE_set = 1;
12269 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12270 &gp, FALSE);
12271 relax_range_measurement (abfd);
12272 }
12273
1c8f6a4d
KLC
12274 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12275 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12276 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12277 TRUE /* keep_memory */);
12278 if (internal_relocs == NULL)
12279 goto error_return;
12280
12281 irelend = internal_relocs + sec->reloc_count;
12282 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12283 irelend, R_NDS32_RELAX_ENTRY);
12284
12285 if (irel == irelend)
12286 return TRUE;
12287
12288 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12289 {
12290 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
f9671640 12291 return TRUE;
1c8f6a4d
KLC
12292
12293 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12294 optimize = 1;
12295
12296 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12297 opt_size = 1;
12298 }
12299
12300 load_store_relax = table->load_store_relax;
12301
12302 /* Get symbol table and section content. */
0c4bd9d9 12303 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
1c8f6a4d
KLC
12304 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12305 goto error_return;
12306
12307 /* Do relax loop only when finalize is not done.
12308 Take care of relaxable relocs except INSN16. */
12309 for (irel = internal_relocs; irel < irelend; irel++)
12310 {
12311 int seq_len; /* Original length of instruction sequence. */
12312 int insn_len = 0; /* Final length of instruction sequence. */
12313 bfd_boolean removed;
12314
12315 insn = 0;
12316 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12317 && (irel->r_addend & 0x1f) >= 2)
12318 optimize = 1;
12319
12320 /* Relocation Types
12321 R_NDS32_LONGCALL1 53
12322 R_NDS32_LONGCALL2 54
12323 R_NDS32_LONGCALL3 55
12324 R_NDS32_LONGJUMP1 56
12325 R_NDS32_LONGJUMP2 57
12326 R_NDS32_LONGJUMP3 58
12327 R_NDS32_LOADSTORE 59 */
12328 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12329 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12330 seq_len = GET_SEQ_LEN (irel->r_addend);
12331
12332 /* Relocation Types
12333 R_NDS32_LONGCALL4 107
12334 R_NDS32_LONGCALL5 108
12335 R_NDS32_LONGCALL6 109
12336 R_NDS32_LONGJUMP4 110
12337 R_NDS32_LONGJUMP5 111
12338 R_NDS32_LONGJUMP6 112
12339 R_NDS32_LONGJUMP7 113 */
12340 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12341 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12342 seq_len = 4;
12343
12344 /* Relocation Types
12345 R_NDS32_LO12S0_RELA 30
12346 R_NDS32_LO12S1_RELA 29
12347 R_NDS32_LO12S2_RELA 28
12348 R_NDS32_LO12S2_SP_RELA 71
12349 R_NDS32_LO12S2_DP_RELA 70
12350 R_NDS32_GOT_LO12 46
12351 R_NDS32_GOTOFF_LO12 50
12352 R_NDS32_PLTREL_LO12 65
12353 R_NDS32_PLT_GOTREL_LO12 67
12354 R_NDS32_17IFC_PCREL_RELA 96
12355 R_NDS32_GOT_SUFF 193
12356 R_NDS32_GOTOFF_SUFF 194
12357 R_NDS32_PLT_GOT_SUFF 195
12358 R_NDS32_MULCALL_SUFF 196
12359 R_NDS32_PTR 197 */
12360 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12361 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12362 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12363 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12364 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12365 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12366 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12367 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12368 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12369 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12370 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12371 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12372 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12373 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
fbaf61ad
NC
12374 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12375 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
1c8f6a4d
KLC
12376 seq_len = 0;
12377 else
12378 continue;
12379
12380 insn_len = seq_len;
12381 removed = FALSE;
12382
12383 switch (ELF32_R_TYPE (irel->r_info))
12384 {
12385 case R_NDS32_LONGCALL1:
12386 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12387 &insn_len, contents, isymbuf,
12388 symtab_hdr);
12389 break;
12390 case R_NDS32_LONGCALL2:
12391 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12392 &insn_len, contents, isymbuf,
12393 symtab_hdr);
12394 break;
12395 case R_NDS32_LONGCALL3:
12396 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12397 &insn_len, contents, isymbuf,
12398 symtab_hdr);
12399 break;
12400 case R_NDS32_LONGJUMP1:
12401 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12402 &insn_len, contents, isymbuf,
12403 symtab_hdr);
12404 break;
12405 case R_NDS32_LONGJUMP2:
12406 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12407 &insn_len, contents, isymbuf,
12408 symtab_hdr);
12409 break;
12410 case R_NDS32_LONGJUMP3:
12411 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12412 &insn_len, contents, isymbuf,
12413 symtab_hdr);
12414 break;
12415 case R_NDS32_LONGCALL4:
12416 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12417 &insn_len, contents, isymbuf,
12418 symtab_hdr);
12419 break;
12420 case R_NDS32_LONGCALL5:
12421 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12422 &insn_len, contents, isymbuf,
12423 symtab_hdr);
12424 break;
12425 case R_NDS32_LONGCALL6:
12426 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12427 &insn_len, contents, isymbuf,
12428 symtab_hdr);
12429 break;
12430 case R_NDS32_LONGJUMP4:
12431 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12432 &insn_len, contents, isymbuf,
12433 symtab_hdr);
12434 break;
12435 case R_NDS32_LONGJUMP5:
12436 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12437 &insn_len, &seq_len, contents,
12438 isymbuf, symtab_hdr);
12439 break;
12440 case R_NDS32_LONGJUMP6:
12441 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12442 &insn_len, &seq_len, contents,
12443 isymbuf, symtab_hdr);
12444 break;
12445 case R_NDS32_LONGJUMP7:
12446 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12447 &insn_len, &seq_len, contents,
12448 isymbuf, symtab_hdr);
12449 break;
12450 case R_NDS32_LOADSTORE:
12451 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12452 internal_relocs, &insn_len,
12453 contents, isymbuf, symtab_hdr,
fbaf61ad 12454 load_store_relax, table);
1c8f6a4d
KLC
12455 break;
12456 case R_NDS32_LO12S0_RELA:
12457 case R_NDS32_LO12S1_RELA:
fbaf61ad 12458 case R_NDS32_LO12S2_RELA:
1c8f6a4d
KLC
12459 case R_NDS32_LO12S2_DP_RELA:
12460 case R_NDS32_LO12S2_SP_RELA:
1c8f6a4d
KLC
12461 /* Relax for low part. */
12462 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
fbaf61ad 12463 contents, isymbuf, symtab_hdr, table);
1c8f6a4d
KLC
12464
12465 /* It is impossible to delete blank, so just continue. */
12466 continue;
fbaf61ad
NC
12467 case R_NDS32_PTR:
12468 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12469 &insn_len, &seq_len, contents);
12470 break;
12471 case R_NDS32_LSI:
12472 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12473 contents, isymbuf, symtab_hdr, again);
12474 continue;
1c8f6a4d
KLC
12475 case R_NDS32_GOT_LO12:
12476 case R_NDS32_GOTOFF_LO12:
12477 case R_NDS32_PLTREL_LO12:
12478 case R_NDS32_PLT_GOTREL_LO12:
12479 case R_NDS32_GOTPC_LO12:
1c8f6a4d 12480 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 12481 case R_NDS32_TLS_LE_ADD:
1c8f6a4d 12482 case R_NDS32_TLS_LE_LS:
1c8f6a4d 12483 case R_NDS32_PLT_GOT_SUFF:
1c8f6a4d 12484 case R_NDS32_GOT_SUFF:
1c8f6a4d 12485 case R_NDS32_GOTOFF_SUFF:
1c8f6a4d
KLC
12486 continue;
12487 default:
12488 continue;
1c8f6a4d 12489 }
fbaf61ad 12490
1c8f6a4d
KLC
12491 if (removed && seq_len - insn_len > 0)
12492 {
12493 if (!insert_nds32_elf_blank
12494 (&relax_blank_list, irel->r_offset + insn_len,
12495 seq_len - insn_len))
12496 goto error_return;
12497 *again = TRUE;
35c08157 12498 }
1c8f6a4d
KLC
12499 }
12500
12501 calc_nds32_blank_total (relax_blank_list);
12502
12503 if (table->relax_fp_as_gp)
12504 {
12505 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12506 irelend, isymbuf))
12507 goto error_return;
35c08157 12508
535b785f 12509 if (!*again)
35c08157 12510 {
1c8f6a4d
KLC
12511 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12512 irelend))
12513 goto error_return;
35c08157
KLC
12514 }
12515 }
1c8f6a4d 12516
535b785f 12517 if (!*again)
1c8f6a4d
KLC
12518 {
12519 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12520 &relax_blank_list, optimize, opt_size))
12521 goto error_return;
12522 }
12523
12524 /* It doesn't matter optimize_for_space_no_align anymore.
35c08157
KLC
12525 If object file is assembled with flag '-Os',
12526 the we don't adjust jump-destination on 4-byte boundary. */
12527
12528 if (relax_blank_list)
12529 {
12530 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12531 relax_blank_list = NULL;
12532 }
12533
535b785f 12534 if (!*again)
35c08157
KLC
12535 {
12536 /* Closing the section, so we don't relax it anymore. */
12537 bfd_vma sec_size_align;
12538 Elf_Internal_Rela *tmp_rel;
12539
12540 /* Pad to alignment boundary. Only handle current section alignment. */
609332f1
NC
12541 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12542 & ((-1U) << sec->alignment_power);
35c08157
KLC
12543 if ((sec_size_align - sec->size) & 0x2)
12544 {
12545 insn16 = NDS32_NOP16;
12546 bfd_putb16 (insn16, contents + sec->size);
12547 sec->size += 2;
12548 }
12549
12550 while (sec_size_align != sec->size)
12551 {
12552 insn = NDS32_NOP32;
12553 bfd_putb32 (insn, contents + sec->size);
12554 sec->size += 4;
12555 }
12556
1c8f6a4d
KLC
12557 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12558 irelend, R_NDS32_RELAX_ENTRY);
35c08157
KLC
12559 if (tmp_rel != irelend)
12560 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12561
12562 clean_nds32_elf_blank ();
12563 }
12564
dc1e8a47 12565 finish:
c9594989 12566 if (elf_section_data (sec)->relocs != internal_relocs)
35c08157
KLC
12567 free (internal_relocs);
12568
c9594989 12569 if (elf_section_data (sec)->this_hdr.contents != contents)
35c08157
KLC
12570 free (contents);
12571
c9594989 12572 if (symtab_hdr->contents != (bfd_byte *) isymbuf)
35c08157
KLC
12573 free (isymbuf);
12574
12575 return result;
12576
dc1e8a47 12577 error_return:
35c08157
KLC
12578 result = FALSE;
12579 goto finish;
12580}
12581
12582static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12583{
12584 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12585 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12586 {NULL, 0, 0, 0, 0}
12587};
12588
bf577467
AM
12589static bfd_boolean
12590nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12591{
12592 const char *name = hdr->bfd_section->name;
12593
12594 if (strncmp (name, ".sbss", 5) == 0
12595 || strncmp (name, ".sdata", 6) == 0)
12596 hdr->bfd_section->flags |= SEC_SMALL_DATA;
12597
12598 return TRUE;
12599}
12600
35c08157
KLC
12601static bfd_boolean
12602nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12603 struct bfd_link_info *info,
12604 void *finfo ATTRIBUTE_UNUSED,
12605 bfd_boolean (*func) (void *, const char *,
12606 Elf_Internal_Sym *,
12607 asection *,
12608 struct elf_link_hash_entry *)
12609 ATTRIBUTE_UNUSED)
12610{
12611 FILE *sym_ld_script = NULL;
12612 struct elf_nds32_link_hash_table *table;
12613
12614 table = nds32_elf_hash_table (info);
12615 sym_ld_script = table->sym_ld_script;
12616
12617 if (check_start_export_sym)
12618 fprintf (sym_ld_script, "}\n");
12619
12620 return TRUE;
12621}
12622
12623static enum elf_reloc_type_class
12624nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12625 const asection *rel_sec ATTRIBUTE_UNUSED,
12626 const Elf_Internal_Rela *rela)
12627{
12628 switch ((int) ELF32_R_TYPE (rela->r_info))
12629 {
12630 case R_NDS32_RELATIVE:
12631 return reloc_class_relative;
12632 case R_NDS32_JMP_SLOT:
12633 return reloc_class_plt;
12634 case R_NDS32_COPY:
12635 return reloc_class_copy;
12636 default:
12637 return reloc_class_normal;
12638 }
12639}
12640
12641/* Put target dependent option into info hash table. */
12642void
12643bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12644 int relax_fp_as_gp,
12645 int eliminate_gc_relocs,
fbaf61ad
NC
12646 FILE * sym_ld_script,
12647 int hyper_relax,
12648 int tls_desc_trampoline,
12649 int load_store_relax)
35c08157
KLC
12650{
12651 struct elf_nds32_link_hash_table *table;
12652
12653 table = nds32_elf_hash_table (link_info);
12654 if (table == NULL)
12655 return;
12656
12657 table->relax_fp_as_gp = relax_fp_as_gp;
12658 table->eliminate_gc_relocs = eliminate_gc_relocs;
12659 table->sym_ld_script = sym_ld_script;
fbaf61ad
NC
12660 table->hyper_relax = hyper_relax;
12661 table->tls_desc_trampoline = tls_desc_trampoline;
35c08157 12662 table ->load_store_relax = load_store_relax;
35c08157
KLC
12663}
12664\f
fbaf61ad 12665
35c08157
KLC
12666/* These functions and data-structures are used for fp-as-gp
12667 optimization. */
12668
12669#define FAG_THRESHOLD 3 /* At least 3 gp-access. */
1c8f6a4d
KLC
12670/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12671 the read-only section and read-write section. */
12672#define FAG_WINDOW (508 - 32)
35c08157
KLC
12673
12674/* An nds32_fag represent a gp-relative access.
12675 We find best fp-base by using a sliding window
12676 to find a base address which can cover most gp-access. */
12677struct nds32_fag
12678{
12679 struct nds32_fag *next; /* NULL-teminated linked list. */
12680 bfd_vma addr; /* The address of this fag. */
12681 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12682 It is used for applying FP7U2_FLAG. */
12683 int count; /* How many times this address is referred.
12684 There should be exactly `count' relocations
12685 in relas. */
12686 int relas_capcity; /* The buffer size of relas.
12687 We use an array instead of linked-list,
12688 and realloc is used to adjust buffer size. */
12689};
12690
12691static void
12692nds32_fag_init (struct nds32_fag *head)
12693{
12694 memset (head, 0, sizeof (struct nds32_fag));
12695}
12696
12697static void
12698nds32_fag_verify (struct nds32_fag *head)
12699{
12700 struct nds32_fag *iter;
12701 struct nds32_fag *prev;
12702
12703 prev = NULL;
12704 iter = head->next;
12705 while (iter)
12706 {
12707 if (prev && prev->addr >= iter->addr)
12708 puts ("Bug in fp-as-gp insertion.");
12709 prev = iter;
12710 iter = iter->next;
12711 }
12712}
12713
12714/* Insert a fag in ascending order.
12715 If a fag of the same address already exists,
12716 they are chained by relas array. */
12717
12718static void
12719nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12720 Elf_Internal_Rela * rel)
12721{
12722 struct nds32_fag *iter;
12723 struct nds32_fag *new_fag;
12724 const int INIT_RELAS_CAP = 4;
12725
12726 for (iter = head;
12727 iter->next && iter->next->addr <= addr;
12728 iter = iter->next)
12729 /* Find somewhere to insert. */ ;
12730
12731 /* `iter' will be equal to `head' if the list is empty. */
12732 if (iter != head && iter->addr == addr)
12733 {
12734 /* The address exists in the list.
12735 Insert `rel' into relocation list, relas. */
12736
12737 /* Check whether relas is big enough. */
12738 if (iter->count >= iter->relas_capcity)
12739 {
12740 iter->relas_capcity *= 2;
12741 iter->relas = bfd_realloc
12742 (iter->relas, iter->relas_capcity * sizeof (void *));
12743 }
12744 iter->relas[iter->count++] = rel;
12745 return;
12746 }
12747
12748 /* This is a new address. Create a fag node for it. */
12749 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12750 memset (new_fag, 0, sizeof (*new_fag));
12751 new_fag->addr = addr;
12752 new_fag->count = 1;
12753 new_fag->next = iter->next;
12754 new_fag->relas_capcity = INIT_RELAS_CAP;
12755 new_fag->relas = (Elf_Internal_Rela **)
12756 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12757 new_fag->relas[0] = rel;
12758 iter->next = new_fag;
12759
12760 nds32_fag_verify (head);
12761}
12762
12763static void
12764nds32_fag_free_list (struct nds32_fag *head)
12765{
12766 struct nds32_fag *iter;
12767
12768 iter = head->next;
12769 while (iter)
12770 {
12771 struct nds32_fag *tmp = iter;
12772 iter = iter->next;
12773 free (tmp->relas);
12774 tmp->relas = NULL;
12775 free (tmp);
12776 }
12777}
12778
35c08157
KLC
12779/* Find the best fp-base address.
12780 The relocation associated with that address is returned,
12781 so we can track the symbol instead of a fixed address.
12782
12783 When relaxation, the address of an datum may change,
12784 because a text section is shrinked, so the data section
1c8f6a4d 12785 moves forward. If the aligments of text and data section
35c08157
KLC
12786 are different, their distance may change too.
12787 Therefore, tracking a fixed address is not appriate. */
12788
12789static int
12790nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12791{
12792 struct nds32_fag *base; /* First fag in the window. */
12793 struct nds32_fag *last; /* First fag outside the window. */
12794 int accu = 0; /* Usage accumulation. */
12795 struct nds32_fag *best; /* Best fag. */
12796 int baccu = 0; /* Best accumulation. */
12797
12798 /* Use first fag for initial, and find the last fag in the window.
12799
12800 In each iteration, we could simply subtract previous fag
12801 and accumulate following fags which are inside the window,
12802 untill we each the end. */
12803
1c8f6a4d
KLC
12804 if (head->next == NULL)
12805 {
12806 *bestpp = NULL;
12807 return 0;
12808 }
35c08157
KLC
12809
12810 /* Initialize base. */
12811 base = head->next;
12812 best = base;
12813 for (last = base;
12814 last && last->addr < base->addr + FAG_WINDOW;
12815 last = last->next)
12816 accu += last->count;
12817
12818 baccu = accu;
12819
12820 /* Record the best base in each iteration. */
12821 while (base->next)
1c8f6a4d
KLC
12822 {
12823 accu -= base->count;
12824 base = base->next;
12825 /* Account fags in window. */
12826 for (/* Nothing. */;
12827 last && last->addr < base->addr + FAG_WINDOW;
12828 last = last->next)
12829 accu += last->count;
12830
12831 /* A better fp-base? */
12832 if (accu > baccu)
12833 {
12834 best = base;
12835 baccu = accu;
12836 }
12837 }
35c08157
KLC
12838
12839 if (bestpp)
12840 *bestpp = best;
12841 return baccu;
12842}
12843
12844/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12845 so we can convert it fo fp-relative access later.
12846 `best_fag' is the best fp-base. Only those inside the window
12847 of best_fag is applied the flag. */
12848
12849static bfd_boolean
12850nds32_fag_mark_relax (struct bfd_link_info *link_info,
fbaf61ad 12851 asection *sec, struct nds32_fag *best_fag,
35c08157
KLC
12852 Elf_Internal_Rela *internal_relocs,
12853 Elf_Internal_Rela *irelend)
12854{
12855 struct nds32_fag *ifag;
12856 bfd_vma best_fpbase, gp;
12857 bfd *output_bfd;
12858
fbaf61ad 12859 output_bfd = sec->output_section->owner;
35c08157
KLC
12860 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12861 best_fpbase = best_fag->addr;
12862
1c8f6a4d
KLC
12863 if (best_fpbase > gp + sdata_range[1][1]
12864 || best_fpbase < gp - sdata_range[1][0])
35c08157
KLC
12865 return FALSE;
12866
12867 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12868 so we know they can be converted to lwi37.fp. */
12869 for (ifag = best_fag;
12870 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12871 {
12872 int i;
12873
12874 for (i = 0; i < ifag->count; i++)
12875 {
12876 Elf_Internal_Rela *insn16_rel;
12877 Elf_Internal_Rela *fag_rel;
12878
12879 fag_rel = ifag->relas[i];
12880
12881 /* Only if this is within the WINDOWS, FP7U2_FLAG
12882 is applied. */
12883
12884 insn16_rel = find_relocs_at_address
12885 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12886
12887 if (insn16_rel != irelend)
12888 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12889 }
12890 }
12891 return TRUE;
12892}
12893
1c8f6a4d
KLC
12894/* Reset INSN16 to clean fp as gp. */
12895
12896static void
12897nds32_fag_unmark_relax (struct nds32_fag *fag,
12898 Elf_Internal_Rela *internal_relocs,
12899 Elf_Internal_Rela *irelend)
12900{
12901 struct nds32_fag *ifag;
12902 int i;
12903 Elf_Internal_Rela *insn16_rel;
12904 Elf_Internal_Rela *fag_rel;
12905
12906 for (ifag = fag; ifag; ifag = ifag->next)
12907 {
12908 for (i = 0; i < ifag->count; i++)
12909 {
12910 fag_rel = ifag->relas[i];
12911
12912 /* Restore the INSN16 relocation. */
12913 insn16_rel = find_relocs_at_address
12914 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12915
12916 if (insn16_rel != irelend)
12917 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12918 }
12919 }
12920}
12921
35c08157
KLC
12922/* This is the main function of fp-as-gp optimization.
12923 It should be called by relax_section. */
12924
12925static bfd_boolean
12926nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12927 bfd *abfd, asection *sec,
12928 Elf_Internal_Rela *internal_relocs,
12929 Elf_Internal_Rela *irelend,
12930 Elf_Internal_Sym *isymbuf)
12931{
12932 Elf_Internal_Rela *begin_rel = NULL;
12933 Elf_Internal_Rela *irel;
12934 struct nds32_fag fag_head;
12935 Elf_Internal_Shdr *symtab_hdr;
12936 bfd_byte *contents;
1c8f6a4d 12937 bfd_boolean ifc_inside = FALSE;
35c08157
KLC
12938
12939 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12940
12941 /* Per-function fp-base selection.
12942 1. Create a list for all the gp-relative access.
12943 2. Base on those gp-relative address,
12944 find a fp-base which can cover most access.
12945 3. Use the fp-base for fp-as-gp relaxation.
12946
12947 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12948 we should
12949 1. delete the `la $fp, _FP_BASE_' instruction and
12950 2. not convert lwi.gp to lwi37.fp.
12951
12952 To delete the _FP_BASE_ instruction, we simply apply
12953 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12954
12955 To suppress the conversion, we simply NOT to apply
12956 R_NDS32_INSN16_FP7U2_FLAG flag. */
12957
12958 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12959
0c4bd9d9 12960 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
35c08157
KLC
12961 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12962 return FALSE;
12963
12964 /* Check whether it is worth for fp-as-gp optimization,
12965 i.e., at least 3 gp-load.
12966
12967 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12968 apply this optimization. */
12969
12970 for (irel = internal_relocs; irel < irelend; irel++)
12971 {
12972 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12973 One we enter the begin of the region, we track all the LW/ST
12974 instructions, so when we leave the region, we try to find
12975 the best fp-base address for those LW/ST instructions. */
12976
12977 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12978 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12979 {
12980 /* Begin of the region. */
12981 if (begin_rel)
695344c0 12982 /* xgettext:c-format */
38f14ab8 12983 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
35c08157
KLC
12984
12985 begin_rel = irel;
12986 nds32_fag_init (&fag_head);
1c8f6a4d 12987 ifc_inside = FALSE;
35c08157
KLC
12988 }
12989 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12990 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12991 {
12992 int accu;
1c8f6a4d 12993 struct nds32_fag *best_fag, *tmp_fag;
35c08157
KLC
12994 int dist;
12995
12996 /* End of the region.
12997 Check whether it is worth to do fp-as-gp. */
12998
12999 if (begin_rel == NULL)
13000 {
695344c0 13001 /* xgettext:c-format */
38f14ab8
AM
13002 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
13003 abfd, sec);
35c08157
KLC
13004 continue;
13005 }
13006
13007 accu = nds32_fag_find_base (&fag_head, &best_fag);
13008
1c8f6a4d
KLC
13009 /* Clean FP7U2_FLAG because they may set ever. */
13010 tmp_fag = fag_head.next;
13011 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
13012
35c08157
KLC
13013 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
13014 if (accu < FAG_THRESHOLD
fbaf61ad 13015 || !nds32_fag_mark_relax (link_info, sec, best_fag,
35c08157
KLC
13016 internal_relocs, irelend))
13017 {
13018 /* Not worth to do fp-as-gp. */
13019 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13020 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13021 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13022 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13023 nds32_fag_free_list (&fag_head);
13024 begin_rel = NULL;
13025 continue;
13026 }
13027
13028 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13029 so we use it to record the distance to the reloction of best
13030 fp-base. */
13031 dist = best_fag->relas[0] - begin_rel;
13032 BFD_ASSERT (dist > 0 && dist < 0xffffff);
13033 /* Use high 16 bits of addend to record the _FP_BASE_ matched
13034 relocation. And get the base value when relocating. */
1c8f6a4d 13035 begin_rel->r_addend &= (0x1 << 16) - 1;
35c08157
KLC
13036 begin_rel->r_addend |= dist << 16;
13037
13038 nds32_fag_free_list (&fag_head);
13039 begin_rel = NULL;
13040 }
13041
1c8f6a4d 13042 if (begin_rel == NULL || ifc_inside)
35c08157
KLC
13043 /* Skip if we are not in the region of fp-as-gp. */
13044 continue;
13045
13046 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13047 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13048 {
13049 bfd_vma addr;
13050 uint32_t insn;
13051
13052 /* A gp-relative access is found. Insert it to the fag-list. */
13053
13054 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
13055 insn = bfd_getb32 (contents + irel->r_offset);
13056 if (!N32_IS_RT3 (insn))
13057 continue;
13058
13059 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13060 nds32_fag_insert (&fag_head, addr, irel);
13061 }
13062 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13063 {
13064 begin_rel = NULL;
13065 }
1c8f6a4d
KLC
13066 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13067 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13068 {
13069 /* Suppress fp as gp when encounter ifc. */
13070 ifc_inside = TRUE;
13071 }
35c08157
KLC
13072 }
13073
13074 return TRUE;
13075}
13076
13077/* Remove unused `la $fp, _FD_BASE_' instruction. */
13078
13079static bfd_boolean
13080nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13081 Elf_Internal_Rela *internal_relocs,
13082 Elf_Internal_Rela *irelend)
13083{
13084 Elf_Internal_Rela *irel;
13085 Elf_Internal_Shdr *symtab_hdr;
13086 bfd_byte *contents = NULL;
13087 nds32_elf_blank_t *relax_blank_list = NULL;
13088 bfd_boolean result = TRUE;
13089 bfd_boolean unused_region = FALSE;
13090
13091 /*
fbaf61ad
NC
13092 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13093 R_NDS32_17IFC_PCREL_RELA
13094 R_NDS32_10IFCU_PCREL_RELA. */
35c08157
KLC
13095
13096 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
0c4bd9d9 13097 nds32_get_section_contents (abfd, sec, &contents, TRUE);
35c08157
KLC
13098
13099 for (irel = internal_relocs; irel < irelend; irel++)
13100 {
13101 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13102 we marked to in previous pass.
13103 DO NOT scan relocations again, since we've alreadly decided it
13104 and set the flag. */
13105 const char *syname;
13106 int syndx;
13107 uint32_t insn;
13108
13109 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13110 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13111 unused_region = TRUE;
13112 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13113 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13114 unused_region = FALSE;
13115
13116 /* We're not in the region. */
13117 if (!unused_region)
13118 continue;
13119
13120 /* _FP_BASE_ must be a GLOBAL symbol. */
13121 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13122 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13123 continue;
13124
13125 /* The symbol name must be _FP_BASE_. */
13126 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13127 if (strcmp (syname, FP_BASE_NAME) != 0)
13128 continue;
13129
13130 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13131 {
13132 /* addi.gp $fp, -256 */
13133 insn = bfd_getb32 (contents + irel->r_offset);
13134 if (insn != INSN_ADDIGP_TO_FP)
13135 continue;
13136 }
13137 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13138 {
13139 /* addi $fp, $gp, -256 */
13140 insn = bfd_getb32 (contents + irel->r_offset);
13141 if (insn != INSN_ADDI_GP_TO_FP)
13142 continue;
13143 }
13144 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13145 {
13146 /* movi $fp, FP_BASE */
13147 insn = bfd_getb32 (contents + irel->r_offset);
13148 if (insn != INSN_MOVI_TO_FP)
13149 continue;
13150 }
13151 else
13152 continue;
13153
13154 /* We got here because a FP_BASE instruction is found. */
13155 if (!insert_nds32_elf_blank_recalc_total
13156 (&relax_blank_list, irel->r_offset, 4))
13157 goto error_return;
13158 }
13159
dc1e8a47 13160 finish:
35c08157
KLC
13161 if (relax_blank_list)
13162 {
13163 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13164 relax_blank_list = NULL;
13165 }
13166 return result;
13167
dc1e8a47 13168 error_return:
35c08157
KLC
13169 result = FALSE;
13170 goto finish;
13171}
1c8f6a4d
KLC
13172
13173/* This is a version of bfd_generic_get_relocated_section_contents.
13174 We need this variety because relaxation will modify the dwarf
13175 infomation. When there is undefined symbol reference error mesage,
13176 linker need to dump line number where the symbol be used. However
13177 the address is be relaxed, it can not get the original dwarf contents.
13178 The variety only modify function call for reading in the section. */
13179
13180static bfd_byte *
13181nds32_elf_get_relocated_section_contents (bfd *abfd,
13182 struct bfd_link_info *link_info,
13183 struct bfd_link_order *link_order,
13184 bfd_byte *data,
13185 bfd_boolean relocatable,
13186 asymbol **symbols)
13187{
13188 bfd *input_bfd = link_order->u.indirect.section->owner;
13189 asection *input_section = link_order->u.indirect.section;
13190 long reloc_size;
13191 arelent **reloc_vector;
13192 long reloc_count;
13193
13194 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13195 if (reloc_size < 0)
13196 return NULL;
13197
13198 /* Read in the section. */
0c4bd9d9 13199 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
1c8f6a4d
KLC
13200 return NULL;
13201
13202 if (reloc_size == 0)
13203 return data;
13204
13205 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13206 if (reloc_vector == NULL)
13207 return NULL;
13208
13209 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13210 reloc_vector, symbols);
13211 if (reloc_count < 0)
13212 goto error_return;
13213
13214 if (reloc_count > 0)
13215 {
13216 arelent **parent;
13217 for (parent = reloc_vector; *parent != NULL; parent++)
13218 {
13219 char *error_message = NULL;
13220 asymbol *symbol;
13221 bfd_reloc_status_type r;
13222
13223 symbol = *(*parent)->sym_ptr_ptr;
13224 if (symbol->section && discarded_section (symbol->section))
13225 {
0930cb30 13226 bfd_vma off;
1c8f6a4d
KLC
13227 static reloc_howto_type none_howto
13228 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13229 "unused", FALSE, 0, 0, FALSE);
13230
bb294208
AM
13231 off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13232 input_section);
0930cb30
AM
13233 _bfd_clear_contents ((*parent)->howto, input_bfd,
13234 input_section, data, off);
1c8f6a4d
KLC
13235 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13236 (*parent)->addend = 0;
13237 (*parent)->howto = &none_howto;
13238 r = bfd_reloc_ok;
13239 }
13240 else
13241 r = bfd_perform_relocation (input_bfd, *parent, data,
13242 input_section,
13243 relocatable ? abfd : NULL,
13244 &error_message);
13245
13246 if (relocatable)
13247 {
13248 asection *os = input_section->output_section;
13249
13250 /* A partial link, so keep the relocs. */
13251 os->orelocation[os->reloc_count] = *parent;
13252 os->reloc_count++;
13253 }
13254
13255 if (r != bfd_reloc_ok)
13256 {
13257 switch (r)
13258 {
13259 case bfd_reloc_undefined:
1a72702b
AM
13260 (*link_info->callbacks->undefined_symbol)
13261 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13262 input_bfd, input_section, (*parent)->address, TRUE);
1c8f6a4d
KLC
13263 break;
13264 case bfd_reloc_dangerous:
13265 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13266 (*link_info->callbacks->reloc_dangerous)
13267 (link_info, error_message,
13268 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13269 break;
13270 case bfd_reloc_overflow:
1a72702b
AM
13271 (*link_info->callbacks->reloc_overflow)
13272 (link_info, NULL,
13273 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13274 (*parent)->howto->name, (*parent)->addend,
13275 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13276 break;
13277 case bfd_reloc_outofrange:
13278 /* PR ld/13730:
13279 This error can result when processing some partially
13280 complete binaries. Do not abort, but issue an error
13281 message instead. */
13282 link_info->callbacks->einfo
695344c0 13283 /* xgettext:c-format */
c1c8c1ef 13284 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
1c8f6a4d
KLC
13285 abfd, input_section, * parent);
13286 goto error_return;
13287
13288 default:
13289 abort ();
13290 break;
13291 }
13292 }
13293 }
13294 }
13295
13296 free (reloc_vector);
13297 return data;
13298
dc1e8a47 13299 error_return:
1c8f6a4d
KLC
13300 free (reloc_vector);
13301 return NULL;
13302}
fbaf61ad
NC
13303
13304/* Check target symbol. */
13305
13306static bfd_boolean
13307nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13308{
13309 if (!sym || !sym->name || sym->name[0] != '$')
13310 return FALSE;
13311 return TRUE;
13312}
13313
13314/* nds32 find maybe function sym. Ignore target special symbol
13315 first, and then go the general function. */
13316
13317static bfd_size_type
13318nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13319 bfd_vma *code_off)
13320{
13321 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13322 return 0;
13323
13324 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13325}
35c08157 13326\f
35c08157 13327
fbaf61ad
NC
13328/* Do TLS model conversion. */
13329
13330typedef struct relax_group_list_t
13331{
13332 Elf_Internal_Rela *relo;
13333 struct relax_group_list_t *next;
13334 struct relax_group_list_t *next_sibling;
13335 int id;
13336} relax_group_list_t;
13337
13338int
13339list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13340
13341int
13342list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13343
13344void
13345dump_chain (relax_group_list_t *pHead);
13346
13347int
13348list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13349{
13350 relax_group_list_t *pNext = pHead;
13351
13352 /* Find place. */
13353 while (pNext->next)
13354 {
13355 if (pNext->next->id > (int) pElem->r_addend)
13356 break;
13357
13358 pNext = pNext->next;
13359 }
13360
13361 /* Insert node. */
13362 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13363 if (!pNew)
13364 return FALSE;
13365
13366 relax_group_list_t *tmp = pNext->next;
13367 pNext->next = pNew;
13368
13369 pNew->id = pElem->r_addend;
13370 pNew->relo = pElem;
13371 pNew->next = tmp;
13372 pNew->next_sibling = NULL;
13373
13374 return TRUE;
13375}
13376
13377int
13378list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13379{
13380 relax_group_list_t *pNext = pNode;
13381
13382 /* Find place. */
13383 while (pNext->next_sibling)
13384 {
13385 pNext = pNext->next_sibling;
13386 }
13387
13388 /* Insert node. */
13389 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13390 if (!pNew)
13391 return FALSE;
13392
13393 relax_group_list_t *tmp = pNext->next_sibling;
13394 pNext->next_sibling = pNew;
13395
13396 pNew->id = -1;
13397 pNew->relo = pElem;
13398 pNew->next = NULL;
13399 pNew->next_sibling = tmp;
13400
13401 return TRUE;
13402}
13403
13404void
13405dump_chain (relax_group_list_t *pHead)
13406{
13407 relax_group_list_t *pNext = pHead->next;
13408 while (pNext)
13409 {
13410 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13411 relax_group_list_t *pNextSib = pNext->next_sibling;
13412 while (pNextSib)
13413 {
13414 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13415 pNextSib = pNextSib->next_sibling;
13416 }
13417 pNext = pNext->next;
13418 printf("\n");
13419 }
13420}
13421
13422/* Check R_NDS32_RELAX_GROUP of each section.
13423 There might be multiple sections in one object file. */
13424
13425int
13426elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13427{
13428 elf32_nds32_relax_group_t *relax_group_ptr =
13429 elf32_nds32_relax_group_ptr (abfd);
13430
13431 int min_id = relax_group_ptr->min_id;
13432 int max_id = relax_group_ptr->max_id;
13433
13434 Elf_Internal_Rela *rel;
13435 Elf_Internal_Rela *relend;
13436 Elf_Internal_Rela *relocs;
13437 enum elf_nds32_reloc_type rtype;
13438
13439 do
13440 {
13441 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13442 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13443 TRUE /* keep_memory */);
13444 if (relocs == NULL)
13445 break;
13446
13447 /* Check R_NDS32_RELAX_GROUP. */
13448 relend = relocs + asec->reloc_count;
13449 for (rel = relocs; rel < relend; rel++)
13450 {
13451 int id;
13452 rtype = ELF32_R_TYPE (rel->r_info);
13453 if (rtype != R_NDS32_RELAX_GROUP)
13454 continue;
13455
13456 id = rel->r_addend;
13457 if (id < min_id)
13458 min_id = id;
13459 else if (id > max_id)
13460 max_id = id;
13461 }
13462 }
13463 while (FALSE);
13464
c9594989 13465 if (elf_section_data (asec)->relocs != relocs)
fbaf61ad
NC
13466 free (relocs);
13467
13468 if ((min_id != relax_group_ptr->min_id)
13469 || (max_id != relax_group_ptr->max_id))
13470 {
13471 relax_group_ptr->count = max_id - min_id + 1;
13472 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13473 relax_group_ptr->min_id = min_id;
13474 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13475 relax_group_ptr->max_id = max_id;
13476 }
13477
13478 return relax_group_ptr->count;
13479}
13480
13481/* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13482struct section_id_list_t *relax_group_section_id_list = NULL;
13483
13484struct section_id_list_t *
13485elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13486{
13487 struct section_id_list_t *result = NULL;
13488 struct section_id_list_t *lst = *lst_ptr;
13489
13490 if (NULL == lst)
13491 {
13492 result = (struct section_id_list_t *) calloc
13493 (1, sizeof (struct section_id_list_t));
13494 BFD_ASSERT (result); /* Feed me. */
13495 result->id = id;
13496 *lst_ptr = result;
13497 }
13498 else
13499 {
13500 struct section_id_list_t *cur = lst;
13501 struct section_id_list_t *prv = NULL;
13502 struct section_id_list_t *sec = NULL;
13503
13504 while (cur)
13505 {
13506 if (cur->id < id)
13507 {
13508 prv = cur;
13509 cur = cur->next;
13510 continue;
13511 }
13512
13513 if (cur->id > id)
13514 {
13515 cur = NULL; /* To insert after prv. */
13516 sec = cur; /* In case prv == NULL. */
13517 }
13518
13519 break;
13520 }
13521
13522 if (NULL == cur)
13523 {
13524 /* Insert after prv. */
13525 result = (struct section_id_list_t *) calloc
13526 (1, sizeof (struct section_id_list_t));
13527 BFD_ASSERT (result); /* Feed me. */
13528 result->id = id;
13529 if (NULL != prv)
13530 {
13531 result->next = prv->next;
13532 prv->next = result;
13533 }
13534 else
13535 {
13536 *lst_ptr = result;
13537 result->next = sec;
13538 }
13539 }
13540 }
13541
13542 return result;
13543}
13544
13545int
13546elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13547{
13548 static int next_relax_group_bias = 0;
13549
13550 elf32_nds32_relax_group_t *relax_group_ptr =
13551 elf32_nds32_relax_group_ptr (abfd);
13552
13553 bfd_boolean result = TRUE;
13554 Elf_Internal_Rela *rel;
13555 Elf_Internal_Rela *relend;
13556 Elf_Internal_Rela *relocs = NULL;
13557 enum elf_nds32_reloc_type rtype;
13558 struct section_id_list_t *node = NULL;
13559 int count = 0;
13560
13561 do
13562 {
13563 if (0 == relax_group_ptr->count)
13564 break;
13565
13566 /* Check if this section has been handled. */
13567 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13568 if (NULL == node)
13569 break; /* Hit, the section id has handled. */
13570
13571 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13572 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13573 TRUE /* keep_memory */);
13574 if (relocs == NULL)
13575 {
13576 BFD_ASSERT (0); /* feed me */
13577 break;
13578 }
13579
13580 /* Allocate group id bias for this bfd! */
13581 if (0 == relax_group_ptr->init)
13582 {
13583 relax_group_ptr->bias = next_relax_group_bias;
13584 next_relax_group_bias += relax_group_ptr->count;
13585 relax_group_ptr->init = 1;
13586 }
13587
13588 /* Reorder relax group groups. */
13589 relend = relocs + asec->reloc_count;
13590 for (rel = relocs; rel < relend; rel++)
13591 {
13592 rtype = ELF32_R_TYPE(rel->r_info);
13593 if (rtype != R_NDS32_RELAX_GROUP)
13594 continue;
13595
13596 /* Change it. */
13597 rel->r_addend += relax_group_ptr->bias;
13598 /* Debugging count. */
13599 count++;
13600 }
13601 }
13602 while (FALSE);
13603
c9594989 13604 if (elf_section_data (asec)->relocs != relocs)
fbaf61ad
NC
13605 free (relocs);
13606
13607 return result;
13608}
13609
13610int
13611nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13612 struct bfd_link_info *lnkinfo)
13613{
13614 bfd_boolean result = TRUE;
13615 Elf_Internal_Rela *irel;
13616 Elf_Internal_Rela *irelend;
13617 Elf_Internal_Rela *internal_relocs;
13618 unsigned long r_symndx;
13619 enum elf_nds32_reloc_type r_type;
13620
13621 Elf_Internal_Sym *local_syms = NULL;
13622 bfd_byte *contents = NULL;
13623
13624 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13625
13626 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13627 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13628 sym_hashes = elf_sym_hashes (inbfd);
13629 sym_hashes_end =
13630 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13631 if (!elf_bad_symtab (inbfd))
13632 sym_hashes_end -= symtab_hdr->sh_info;
13633
13634 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13635 if (bfd_link_relocatable (lnkinfo))
13636 {
13637 elf32_nds32_unify_relax_group (inbfd, insec);
13638 return result;
13639 }
13640
13641 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13642 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13643 TRUE /* keep_memory */);
13644 if (internal_relocs == NULL)
13645 goto error_return;
13646
13647 irelend = internal_relocs + insec->reloc_count;
13648 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13649 irelend, R_NDS32_RELAX_ENTRY);
13650 if (irel == irelend)
13651 goto finish;
13652
13653 /* Chain/remove groups. */
13654 for (irel = internal_relocs; irel < irelend; irel++)
13655 {
13656 r_symndx = ELF32_R_SYM (irel->r_info);
13657 r_type = ELF32_R_TYPE (irel->r_info);
13658 if (r_type != R_NDS32_RELAX_GROUP)
13659 continue;
13660
13661 /* Remove it. */
13662 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13663 /* Chain it now. */
13664 if (!list_insert (&chain, irel))
13665 goto error_return;
13666 }
13667
13668 /* Collect group relocations. */
13669 /* Presume relocations are sorted. */
13670 relax_group_list_t *pNext = chain.next;
13671 while (pNext)
13672 {
13673 for (irel = internal_relocs; irel < irelend; irel++)
13674 {
13675 if (irel->r_offset == pNext->relo->r_offset)
13676 {
13677 /* Ignore Non-TLS relocation types. */
13678 r_type = ELF32_R_TYPE (irel->r_info);
13679 if ((R_NDS32_TLS_LE_HI20 > r_type)
13680 || (R_NDS32_RELAX_ENTRY == r_type))
13681 continue;
13682
13683 if (!list_insert_sibling (pNext, irel))
13684 goto error_return;
13685 }
13686 else if (irel->r_offset > pNext->relo->r_offset)
13687 {
13688 pNext = pNext->next;
13689 if (!pNext)
13690 break;
13691
13692 bfd_vma current_offset = pNext->relo->r_offset;
13693 if (irel->r_offset > current_offset)
13694 irel = internal_relocs; /* restart from head */
13695 else
13696 --irel; /* Check current irel again. */
13697 continue;
13698 }
13699 else
13700 {
13701 /* This shouldn't be reached. */
13702 }
13703 }
13704 if (pNext)
13705 pNext = pNext->next;
13706 }
13707
13708#ifdef DUBUG_VERBOSE
13709 dump_chain(&chain);
13710#endif
13711
13712 /* Get symbol table and section content. */
13713 if (incontents)
13714 contents = incontents;
13715 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13716 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13717 goto error_return;
13718
13719 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13720
13721 /* Convert TLS model each group if necessary. */
13722 pNext = chain.next;
13723
13724 int cur_grp_id = -1;
13725 int sethi_rt = -1;
13726 int add_rt = -1;
13727 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13728
13729 tls_type = org_tls_type = eff_tls_type = 0;
13730
13731 while (pNext)
13732 {
13733 relax_group_list_t *pNextSig = pNext->next_sibling;
13734 while (pNextSig)
13735 {
13736 struct elf_link_hash_entry *h = NULL;
13737
13738 irel = pNextSig->relo;
13739 r_symndx = ELF32_R_SYM(irel->r_info);
13740 r_type = ELF32_R_TYPE(irel->r_info);
13741
13742 if (pNext->id != cur_grp_id)
13743 {
13744 cur_grp_id = pNext->id;
13745 org_tls_type = get_tls_type (r_type, NULL);
13746 if (r_symndx >= symtab_hdr->sh_info)
13747 {
13748 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13749 while (h->root.type == bfd_link_hash_indirect
13750 || h->root.type == bfd_link_hash_warning)
13751 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13752 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13753 }
13754 else
13755 {
13756 tls_type = local_got_tls_type
13757 ? local_got_tls_type[r_symndx]
13758 : GOT_NORMAL;
13759 }
13760
13761 eff_tls_type = 1 << (fls (tls_type) - 1);
13762 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13763 }
13764
13765 if (eff_tls_type != org_tls_type)
13766 {
13767 switch (org_tls_type)
13768 {
13769 /* DESC to IEGP/IE/LE. */
13770 case GOT_TLS_DESC:
13771 switch (eff_tls_type)
13772 {
13773 case GOT_TLS_IE:
13774 switch (r_type)
13775 {
13776 case R_NDS32_TLS_DESC_HI20:
13777 irel->r_info = ELF32_R_INFO(r_symndx,
13778 R_NDS32_TLS_IE_HI20);
13779 break;
13780 case R_NDS32_TLS_DESC_LO12:
13781 irel->r_info = ELF32_R_INFO(r_symndx,
13782 R_NDS32_TLS_IE_LO12);
13783 break;
13784 case R_NDS32_TLS_DESC_ADD:
13785 {
13786 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13787 add_rt = N32_RT5 (insn);
13788 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13789 bfd_putb32 (insn, contents + irel->r_offset);
13790
13791 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13792 }
13793 break;
13794 case R_NDS32_TLS_DESC_FUNC:
13795 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13796 irel->r_info = ELF32_R_INFO(r_symndx,
13797 R_NDS32_RELAX_REMOVE);
13798 break;
13799 case R_NDS32_TLS_DESC_CALL:
13800 {
13801 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13802 REG_TP);
13803 bfd_putb32 (insn, contents + irel->r_offset);
13804
13805 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13806 }
13807 break;
13808 case R_NDS32_LOADSTORE:
13809 case R_NDS32_PTR:
13810 case R_NDS32_PTR_RESOLVED:
13811 case R_NDS32_NONE:
13812 case R_NDS32_LABEL:
13813 break;
13814 default:
13815 BFD_ASSERT(0);
13816 break;
13817 }
13818 break;
13819 case GOT_TLS_IEGP:
13820 switch (r_type)
13821 {
13822 case R_NDS32_TLS_DESC_HI20:
13823 irel->r_info = ELF32_R_INFO(r_symndx,
13824 R_NDS32_TLS_IEGP_HI20);
13825 break;
13826 case R_NDS32_TLS_DESC_LO12:
13827 irel->r_info = ELF32_R_INFO(r_symndx,
13828 R_NDS32_TLS_IEGP_LO12);
13829 break;
13830 case R_NDS32_TLS_DESC_ADD:
13831 {
13832 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13833 add_rt = N32_RT5 (insn);
13834 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13835 bfd_putb32 (insn, contents + irel->r_offset);
13836
13837 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13838 }
13839 break;
13840 case R_NDS32_TLS_DESC_FUNC:
13841 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13842 irel->r_info = ELF32_R_INFO(r_symndx,
13843 R_NDS32_RELAX_REMOVE);
13844 break;
13845 case R_NDS32_TLS_DESC_CALL:
13846 {
13847 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13848 REG_TP);
13849 bfd_putb32 (insn, contents + irel->r_offset);
13850
13851 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13852 }
13853 break;
13854 case R_NDS32_LOADSTORE:
13855 case R_NDS32_PTR:
13856 case R_NDS32_PTR_RESOLVED:
13857 case R_NDS32_NONE:
13858 case R_NDS32_LABEL:
13859 break;
13860 default:
13861 BFD_ASSERT(0);
13862 break;
13863 }
13864 break;
13865 case GOT_TLS_LE:
13866 switch (r_type)
13867 {
13868 case R_NDS32_TLS_DESC_HI20:
13869 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13870 break;
13871 case R_NDS32_TLS_DESC_LO12:
13872 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13873 break;
13874 case R_NDS32_TLS_DESC_ADD:
13875 {
13876 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13877
13878 add_rt = N32_RT5 (insn);
13879 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13880 bfd_putb32 (insn, contents + irel->r_offset);
13881
13882 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13883 }
13884 break;
13885 case R_NDS32_TLS_DESC_FUNC:
13886 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13887 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13888 break;
13889 case R_NDS32_TLS_DESC_CALL:
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_LOADSTORE:
13894 case R_NDS32_PTR:
13895 case R_NDS32_PTR_RESOLVED:
13896 case R_NDS32_NONE:
13897 case R_NDS32_LABEL:
13898 break;
13899 default:
13900 BFD_ASSERT(0);
13901 break;
13902 }
13903 break;
13904 default:
13905 break;
13906 }
13907 break;
13908 /* IEGP to IE/LE. */
13909 case GOT_TLS_IEGP:
13910 switch (eff_tls_type)
13911 {
13912 case GOT_TLS_IE:
13913 switch (r_type)
13914 {
13915 case R_NDS32_TLS_IEGP_HI20:
13916 irel->r_info = ELF32_R_INFO(r_symndx,
13917 R_NDS32_TLS_IE_HI20);
13918 break;
13919 case R_NDS32_TLS_IEGP_LO12:
13920 irel->r_info = ELF32_R_INFO(r_symndx,
13921 R_NDS32_TLS_IE_LO12);
13922 break;
13923 case R_NDS32_PTR_RESOLVED:
13924 {
13925 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13926
13927 add_rt = N32_RT5 (insn);
13928 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13929 bfd_putb32 (insn, contents + irel->r_offset);
13930 }
13931 break;
13932 case R_NDS32_TLS_IEGP_LW:
13933 break;
13934 case R_NDS32_LOADSTORE:
13935 case R_NDS32_PTR:
13936 case R_NDS32_NONE:
13937 case R_NDS32_LABEL:
13938 break;
13939 default:
13940 BFD_ASSERT(0);
13941 break;
13942 }
13943 break;
13944 case GOT_TLS_LE:
13945 switch (r_type)
13946 {
13947 case R_NDS32_TLS_IEGP_HI20:
13948 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13949 break;
13950 case R_NDS32_TLS_IEGP_LO12:
13951 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13952 break;
13953 case R_NDS32_TLS_IEGP_LW:
13954 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13955 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13956 break;
13957 case R_NDS32_LOADSTORE:
13958 case R_NDS32_PTR:
13959 case R_NDS32_NONE:
13960 case R_NDS32_LABEL:
13961 case R_NDS32_PTR_RESOLVED:
13962 break;
13963 default:
13964 BFD_ASSERT(0);
13965 break;
13966 }
13967 break;
13968 default:
13969 break;
13970 }
13971 break;
13972 /* IE to LE. */
13973 case GOT_TLS_IE:
13974 switch (eff_tls_type)
13975 {
13976 case GOT_TLS_LE:
13977 switch (r_type)
13978 {
13979 case R_NDS32_TLS_IE_HI20:
13980 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13981 break;
13982 case R_NDS32_TLS_IE_LO12S2:
13983 {
13984 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13985
13986 add_rt = N32_RT5 (insn);
13987 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13988 bfd_putb32 (insn, contents + irel->r_offset);
13989
13990 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13991 }
13992 break;
13993 case R_NDS32_LOADSTORE:
13994 case R_NDS32_PTR:
13995 case R_NDS32_NONE:
13996 case R_NDS32_LABEL:
13997 break;
13998 default:
13999 BFD_ASSERT(0);
14000 break;
14001 }
14002 break;
14003 default:
14004 break;
14005 }
14006 break;
14007 default:
14008 break;
14009 }
14010 }
14011 pNextSig = pNextSig->next_sibling;
14012 }
14013
14014#if 1
14015 pNext = pNext->next;
14016#else
14017 while (pNext)
14018 {
14019 if (pNext->id != cur_grp_id)
14020 break;
14021 pNext = pNext->next;
14022 }
14023#endif
14024 }
14025
dc1e8a47 14026 finish:
fbaf61ad
NC
14027 if (incontents)
14028 contents = NULL;
14029
c9594989 14030 if (elf_section_data (insec)->relocs != internal_relocs)
fbaf61ad
NC
14031 free (internal_relocs);
14032
c9594989 14033 if (elf_section_data (insec)->this_hdr.contents != contents)
fbaf61ad
NC
14034 free (contents);
14035
c9594989 14036 if (symtab_hdr->contents != (bfd_byte *) local_syms)
fbaf61ad
NC
14037 free (local_syms);
14038
14039 if (chain.next)
14040 {
14041 pNext = chain.next;
14042 relax_group_list_t *pDel;
14043 while (pNext)
14044 {
14045 pDel = pNext;
14046 pNext = pNext->next;
14047 free (pDel);
14048 }
14049 }
14050
14051 return result;
14052
dc1e8a47 14053 error_return:
fbaf61ad
NC
14054 result = FALSE;
14055 goto finish;
14056}
14057
14058/* End TLS model conversion. */
14059
35c08157
KLC
14060#define ELF_ARCH bfd_arch_nds32
14061#define ELF_MACHINE_CODE EM_NDS32
14062#define ELF_MAXPAGESIZE 0x1000
07d6d2b8 14063#define ELF_TARGET_ID NDS32_ELF_DATA
35c08157 14064
6d00b590 14065#define TARGET_BIG_SYM nds32_elf32_be_vec
35c08157 14066#define TARGET_BIG_NAME "elf32-nds32be"
6d00b590 14067#define TARGET_LITTLE_SYM nds32_elf32_le_vec
35c08157
KLC
14068#define TARGET_LITTLE_NAME "elf32-nds32le"
14069
14070#define elf_info_to_howto nds32_info_to_howto
14071#define elf_info_to_howto_rel nds32_info_to_howto_rel
14072
14073#define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
14074#define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
14075#define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
14076#define bfd_elf32_bfd_relax_section nds32_elf_relax_section
14077#define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
14078
07d6d2b8 14079#define bfd_elf32_mkobject nds32_elf_mkobject
35c08157
KLC
14080#define elf_backend_action_discarded nds32_elf_action_discarded
14081#define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
14082#define elf_backend_check_relocs nds32_elf_check_relocs
14083#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14084#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14085#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14086#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14087#define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
14088#define elf_backend_relocate_section nds32_elf_relocate_section
14089#define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
35c08157
KLC
14090#define elf_backend_grok_prstatus nds32_elf_grok_prstatus
14091#define elf_backend_grok_psinfo nds32_elf_grok_psinfo
14092#define elf_backend_reloc_type_class nds32_elf_reloc_type_class
14093#define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
14094#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14095#define elf_backend_output_arch_syms nds32_elf_output_arch_syms
14096#define elf_backend_object_p nds32_elf_object_p
14097#define elf_backend_final_write_processing nds32_elf_final_write_processing
14098#define elf_backend_special_sections nds32_elf_special_sections
bf577467 14099#define elf_backend_section_flags nds32_elf_section_flags
1c8f6a4d 14100#define bfd_elf32_bfd_get_relocated_section_contents \
07d6d2b8 14101 nds32_elf_get_relocated_section_contents
fbaf61ad
NC
14102#define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
14103#define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
35c08157
KLC
14104
14105#define elf_backend_can_gc_sections 1
14106#define elf_backend_can_refcount 1
14107#define elf_backend_want_got_plt 1
14108#define elf_backend_plt_readonly 1
14109#define elf_backend_want_plt_sym 0
14110#define elf_backend_got_header_size 12
14111#define elf_backend_may_use_rel_p 1
14112#define elf_backend_default_use_rela_p 1
14113#define elf_backend_may_use_rela_p 1
fbaf61ad 14114#define elf_backend_dtrel_excludes_plt 0
35c08157
KLC
14115
14116#include "elf32-target.h"
14117
14118#undef ELF_MAXPAGESIZE
14119#define ELF_MAXPAGESIZE 0x2000
14120
fbaf61ad 14121#undef TARGET_BIG_SYM
6d00b590 14122#define TARGET_BIG_SYM nds32_elf32_linux_be_vec
fbaf61ad 14123#undef TARGET_BIG_NAME
35c08157 14124#define TARGET_BIG_NAME "elf32-nds32be-linux"
fbaf61ad 14125#undef TARGET_LITTLE_SYM
6d00b590 14126#define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
fbaf61ad 14127#undef TARGET_LITTLE_NAME
35c08157 14128#define TARGET_LITTLE_NAME "elf32-nds32le-linux"
fbaf61ad 14129#undef elf32_bed
35c08157
KLC
14130#define elf32_bed elf32_nds32_lin_bed
14131
14132#include "elf32-target.h"
This page took 1.079591 seconds and 4 git commands to generate.