Automatic date update in version.in
[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
fbaf61ad
NC
3877 if (ind->root.type == bfd_link_hash_indirect)
3878 {
3879 if (dir->got.refcount <= 0)
3880 {
3881 edir->tls_type = eind->tls_type;
3882 eind->tls_type = GOT_UNKNOWN;
3883 }
3884 }
3885
35c08157
KLC
3886 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3887}
3888\f
35c08157
KLC
3889/* Adjust a symbol defined by a dynamic object and referenced by a
3890 regular object. The current definition is in some section of the
3891 dynamic object, but we're not including those sections. We have to
3892 change the definition to something the rest of the link can
3893 understand. */
3894
3895static bfd_boolean
3896nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3897 struct elf_link_hash_entry *h)
3898{
3899 struct elf_nds32_link_hash_table *htab;
35c08157
KLC
3900 bfd *dynobj;
3901 asection *s;
3902 unsigned int power_of_two;
3903
3904 dynobj = elf_hash_table (info)->dynobj;
3905
3906 /* Make sure we know what is going on here. */
3907 BFD_ASSERT (dynobj != NULL
3908 && (h->needs_plt
60d67dc8 3909 || h->is_weakalias
35c08157
KLC
3910 || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3911
3912
3913 /* If this is a function, put it in the procedure linkage table. We
3914 will fill in the contents of the procedure linkage table later,
3915 when we know the address of the .got section. */
3916 if (h->type == STT_FUNC || h->needs_plt)
3917 {
0e1862bb 3918 if (!bfd_link_pic (info)
35c08157
KLC
3919 && !h->def_dynamic
3920 && !h->ref_dynamic
3921 && h->root.type != bfd_link_hash_undefweak
3922 && h->root.type != bfd_link_hash_undefined)
3923 {
3924 /* This case can occur if we saw a PLT reloc in an input
3925 file, but the symbol was never referred to by a dynamic
3926 object. In such a case, we don't actually need to build
3927 a procedure linkage table, and we can just do a PCREL
3928 reloc instead. */
3929 h->plt.offset = (bfd_vma) - 1;
3930 h->needs_plt = 0;
3931 }
3932
3933 return TRUE;
3934 }
3935 else
3936 h->plt.offset = (bfd_vma) - 1;
3937
3938 /* If this is a weak symbol, and there is a real definition, the
3939 processor independent code will have arranged for us to see the
3940 real definition first, and we can just use the same value. */
60d67dc8 3941 if (h->is_weakalias)
35c08157 3942 {
60d67dc8
AM
3943 struct elf_link_hash_entry *def = weakdef (h);
3944 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3945 h->root.u.def.section = def->root.u.def.section;
3946 h->root.u.def.value = def->root.u.def.value;
35c08157
KLC
3947 return TRUE;
3948 }
3949
3950 /* This is a reference to a symbol defined by a dynamic object which
3951 is not a function. */
3952
3953 /* If we are creating a shared library, we must presume that the
3954 only references to the symbol are via the global offset table.
3955 For such cases we need not do anything here; the relocations will
3956 be handled correctly by relocate_section. */
0e1862bb 3957 if (bfd_link_pic (info))
35c08157
KLC
3958 return TRUE;
3959
3960 /* If there are no references to this symbol that do not use the
3961 GOT, we don't need to generate a copy reloc. */
3962 if (!h->non_got_ref)
3963 return TRUE;
3964
3965 /* If -z nocopyreloc was given, we won't generate them either. */
3bf083ed 3966 if (0 && info->nocopyreloc)
35c08157
KLC
3967 {
3968 h->non_got_ref = 0;
3969 return TRUE;
3970 }
3971
3bf083ed
AM
3972 /* If we don't find any dynamic relocs in read-only sections, then
3973 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5dbc8b37 3974 if (!_bfd_elf_readonly_dynrelocs (h))
35c08157
KLC
3975 {
3976 h->non_got_ref = 0;
3977 return TRUE;
3978 }
3979
3980 /* We must allocate the symbol in our .dynbss section, which will
3981 become part of the .bss section of the executable. There will be
3982 an entry for this symbol in the .dynsym section. The dynamic
3983 object will contain position independent code, so all references
3984 from the dynamic object to this symbol will go through the global
3985 offset table. The dynamic linker will use the .dynsym entry to
3986 determine the address it must put in the global offset table, so
3987 both the dynamic object and the regular object will refer to the
3988 same memory location for the variable. */
3989
3990 htab = nds32_elf_hash_table (info);
3991 s = htab->sdynbss;
3992 BFD_ASSERT (s != NULL);
3993
3994 /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
3995 to copy the initial value out of the dynamic object and into the
3996 runtime process image. We need to remember the offset into the
3997 .rela.bss section we are going to use. */
3998 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
3999 {
4000 asection *srel;
4001
4002 srel = htab->srelbss;
4003 BFD_ASSERT (srel != NULL);
4004 srel->size += sizeof (Elf32_External_Rela);
4005 h->needs_copy = 1;
4006 }
4007
4008 /* We need to figure out the alignment required for this symbol. I
4009 have no idea how ELF linkers handle this. */
4010 power_of_two = bfd_log2 (h->size);
4011 if (power_of_two > 3)
4012 power_of_two = 3;
4013
4014 /* Apply the required alignment. */
4015 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
fd361982 4016 if (power_of_two > bfd_section_alignment (s))
35c08157 4017 {
fd361982 4018 if (!bfd_set_section_alignment (s, power_of_two))
35c08157
KLC
4019 return FALSE;
4020 }
4021
4022 /* Define the symbol as being at this point in the section. */
4023 h->root.u.def.section = s;
4024 h->root.u.def.value = s->size;
4025
4026 /* Increment the section size to make room for the symbol. */
4027 s->size += h->size;
4028
4029 return TRUE;
4030}
4031
4032/* Allocate space in .plt, .got and associated reloc sections for
4033 dynamic relocs. */
4034
4035static bfd_boolean
4036allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4037{
4038 struct bfd_link_info *info;
fbaf61ad 4039 struct elf_link_hash_table *ehtab;
35c08157 4040 struct elf_nds32_link_hash_table *htab;
3bf083ed 4041 struct elf_dyn_relocs *p;
35c08157
KLC
4042
4043 if (h->root.type == bfd_link_hash_indirect)
4044 return TRUE;
4045
fbaf61ad
NC
4046 /* When warning symbols are created, they **replace** the "real"
4047 entry in the hash table, thus we never get to see the real
4048 symbol in a hash traversal. So look at it now. */
35c08157 4049 if (h->root.type == bfd_link_hash_warning)
35c08157
KLC
4050 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4051
4052 info = (struct bfd_link_info *) inf;
fbaf61ad 4053 ehtab = elf_hash_table (info);
35c08157 4054 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4055 if (htab == NULL)
4056 return FALSE;
35c08157 4057
fbaf61ad
NC
4058 if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4059 && h->plt.refcount > 0
4060 && !(bfd_link_pie (info) && h->def_regular))
35c08157
KLC
4061 {
4062 /* Make sure this symbol is output as a dynamic symbol.
4063 Undefined weak syms won't yet be marked as dynamic. */
4064 if (h->dynindx == -1 && !h->forced_local)
4065 {
4066 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4067 return FALSE;
4068 }
4069
0e1862bb 4070 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
35c08157 4071 {
fbaf61ad 4072 asection *s = ehtab->splt;
35c08157
KLC
4073
4074 /* If this is the first .plt entry, make room for the special
4075 first entry. */
4076 if (s->size == 0)
4077 s->size += PLT_ENTRY_SIZE;
4078
4079 h->plt.offset = s->size;
4080
4081 /* If this symbol is not defined in a regular file, and we are
4082 not generating a shared library, then set the symbol to this
4083 location in the .plt. This is required to make function
4084 pointers compare as equal between the normal executable and
4085 the shared library. */
0e1862bb 4086 if (!bfd_link_pic (info) && !h->def_regular)
35c08157
KLC
4087 {
4088 h->root.u.def.section = s;
4089 h->root.u.def.value = h->plt.offset;
4090 }
4091
4092 /* Make room for this entry. */
4093 s->size += PLT_ENTRY_SIZE;
4094
4095 /* We also need to make an entry in the .got.plt section, which
4096 will be placed in the .got section by the linker script. */
fbaf61ad 4097 ehtab->sgotplt->size += 4;
35c08157
KLC
4098
4099 /* We also need to make an entry in the .rel.plt section. */
fbaf61ad
NC
4100 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4101 if (htab->tls_desc_trampoline)
4102 htab->next_tls_desc_index++;
35c08157
KLC
4103 }
4104 else
4105 {
4106 h->plt.offset = (bfd_vma) - 1;
4107 h->needs_plt = 0;
4108 }
4109 }
4110 else
4111 {
4112 h->plt.offset = (bfd_vma) - 1;
4113 h->needs_plt = 0;
4114 }
4115
4116 if (h->got.refcount > 0)
4117 {
fbaf61ad 4118 asection *sgot;
35c08157 4119 bfd_boolean dyn;
1c8f6a4d 4120 int tls_type = elf32_nds32_hash_entry (h)->tls_type;
35c08157
KLC
4121
4122 /* Make sure this symbol is output as a dynamic symbol.
4123 Undefined weak syms won't yet be marked as dynamic. */
4124 if (h->dynindx == -1 && !h->forced_local)
4125 {
4126 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4127 return FALSE;
4128 }
4129
fbaf61ad
NC
4130 sgot = elf_hash_table (info)->sgot;
4131 h->got.offset = sgot->size;
1c8f6a4d
KLC
4132
4133 if (tls_type == GOT_UNKNOWN)
4134 abort ();
fbaf61ad
NC
4135
4136 /* Non-TLS symbols, and TLS_IE need one GOT slot. */
4137 if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4138 sgot->size += 4;
4139 else
4140 {
4141 /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots. */
4142 if (tls_type & GOT_TLS_DESC)
4143 sgot->size += 8;
4144 }
1c8f6a4d 4145
35c08157 4146 dyn = htab->root.dynamic_sections_created;
fbaf61ad 4147
0e1862bb 4148 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
fbaf61ad
NC
4149 {
4150 if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4151 {
4152 /* TLS_DESC with trampoline needs a relocation slot
4153 within .rela.plt. */
4154 htab->num_tls_desc++;
4155 ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4156 htab->tls_trampoline = -1;
4157 }
4158 else
4159 {
4160 /* other relocations, including TLS_DESC without trampoline, need
4161 a relocation slot within .rela.got. */
4162 ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4163 }
4164 }
35c08157
KLC
4165 }
4166 else
fbaf61ad 4167 h->got.offset = (bfd_vma)-1;
35c08157 4168
190eb1dd 4169 if (h->dyn_relocs == NULL)
35c08157
KLC
4170 return TRUE;
4171
4172 /* In the shared -Bsymbolic case, discard space allocated for
4173 dynamic pc-relative relocs against symbols which turn out to be
4174 defined in regular objects. For the normal shared case, discard
4175 space for pc-relative relocs that have become local due to symbol
4176 visibility changes. */
4177
0e1862bb 4178 if (bfd_link_pic (info))
35c08157
KLC
4179 {
4180 if (h->def_regular && (h->forced_local || info->symbolic))
4181 {
3bf083ed 4182 struct elf_dyn_relocs **pp;
35c08157 4183
190eb1dd 4184 for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
35c08157
KLC
4185 {
4186 p->count -= p->pc_count;
4187 p->pc_count = 0;
4188 if (p->count == 0)
4189 *pp = p->next;
4190 else
4191 pp = &p->next;
4192 }
4193 }
4194 }
4195 else
4196 {
4197 /* For the non-shared case, discard space for relocs against
4198 symbols which turn out to need copy relocs or are not dynamic. */
4199
4200 if (!h->non_got_ref
4201 && ((h->def_dynamic
4202 && !h->def_regular)
4203 || (htab->root.dynamic_sections_created
4204 && (h->root.type == bfd_link_hash_undefweak
4205 || h->root.type == bfd_link_hash_undefined))))
4206 {
4207 /* Make sure this symbol is output as a dynamic symbol.
4208 Undefined weak syms won't yet be marked as dynamic. */
4209 if (h->dynindx == -1 && !h->forced_local)
4210 {
4211 if (!bfd_elf_link_record_dynamic_symbol (info, h))
4212 return FALSE;
4213 }
4214
4215 /* If that succeeded, we know we'll be keeping all the
4216 relocs. */
4217 if (h->dynindx != -1)
4218 goto keep;
4219 }
4220
190eb1dd 4221 h->dyn_relocs = NULL;
35c08157 4222
dc1e8a47 4223 keep:;
35c08157
KLC
4224 }
4225
4226 /* Finally, allocate space. */
190eb1dd 4227 for (p = h->dyn_relocs; p != NULL; p = p->next)
35c08157
KLC
4228 {
4229 asection *sreloc = elf_section_data (p->sec)->sreloc;
4230 sreloc->size += p->count * sizeof (Elf32_External_Rela);
4231 }
4232
4233 return TRUE;
4234}
4235
fbaf61ad
NC
4236/* Add relocation REL to the end of relocation section SRELOC. */
4237
4238static void
4239elf32_nds32_add_dynreloc (bfd *output_bfd,
4240 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4241 asection *sreloc, Elf_Internal_Rela *rel)
4242{
4243 bfd_byte *loc;
4244 if (sreloc == NULL)
4245 abort ();
4246
4247 loc = sreloc->contents;
4248 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4249 if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4250 abort ();
4251
4252 bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4253}
4254
35c08157
KLC
4255/* Set the sizes of the dynamic sections. */
4256
4257static bfd_boolean
4258nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4259 struct bfd_link_info *info)
4260{
4261 struct elf_nds32_link_hash_table *htab;
4262 bfd *dynobj;
4263 asection *s;
4264 bfd_boolean relocs;
4265 bfd *ibfd;
4266
4267 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
4268 if (htab == NULL)
4269 return FALSE;
4270
4271 dynobj = elf_hash_table (info)->dynobj;
35c08157
KLC
4272 BFD_ASSERT (dynobj != NULL);
4273
fbaf61ad 4274 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
4275 {
4276 /* Set the contents of the .interp section to the interpreter. */
f5233a16 4277 if (bfd_link_executable (info) && !info->nointerp)
35c08157
KLC
4278 {
4279 s = bfd_get_section_by_name (dynobj, ".interp");
4280 BFD_ASSERT (s != NULL);
4281 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4282 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4283 }
4284 }
4285
4286 /* Set up .got offsets for local syms, and space for local dynamic
4287 relocs. */
c72f2fb2 4288 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
35c08157
KLC
4289 {
4290 bfd_signed_vma *local_got;
4291 bfd_signed_vma *end_local_got;
4292 bfd_size_type locsymcount;
4293 Elf_Internal_Shdr *symtab_hdr;
fbaf61ad
NC
4294 asection *sgot;
4295 char *local_tls_type;
4296 unsigned long symndx;
4297 bfd_vma *local_tlsdesc_gotent;
35c08157
KLC
4298
4299 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4300 continue;
4301
4302 for (s = ibfd->sections; s != NULL; s = s->next)
4303 {
3bf083ed 4304 struct elf_dyn_relocs *p;
35c08157 4305
3bf083ed 4306 for (p = ((struct elf_dyn_relocs *)
35c08157
KLC
4307 elf_section_data (s)->local_dynrel);
4308 p != NULL; p = p->next)
4309 {
4310 if (!bfd_is_abs_section (p->sec)
4311 && bfd_is_abs_section (p->sec->output_section))
4312 {
4313 /* Input section has been discarded, either because
4314 it is a copy of a linkonce section or due to
4315 linker script /DISCARD/, so we'll be discarding
4316 the relocs too. */
4317 }
4318 else if (p->count != 0)
4319 {
fbaf61ad
NC
4320 asection *sreloc = elf_section_data (p->sec)->sreloc;
4321 sreloc->size += p->count * sizeof (Elf32_External_Rela);
35c08157
KLC
4322 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4323 info->flags |= DF_TEXTREL;
4324 }
4325 }
4326 }
4327
4328 local_got = elf_local_got_refcounts (ibfd);
4329 if (!local_got)
4330 continue;
4331
4332 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4333 locsymcount = symtab_hdr->sh_info;
4334 end_local_got = local_got + locsymcount;
fbaf61ad
NC
4335 sgot = elf_hash_table (info)->sgot;
4336 local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4337 local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4338 for (symndx = 0; local_got < end_local_got;
4339 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
35c08157
KLC
4340 {
4341 if (*local_got > 0)
4342 {
fbaf61ad
NC
4343 int num_of_got_entry_needed = 0;
4344 *local_got = sgot->size;
4345 *local_tlsdesc_gotent = sgot->size;
4346
4347 /* TLS_NORMAL, and TLS_IE need one slot in .got. */
4348 if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4349 num_of_got_entry_needed = 1;
4350 /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT. */
4351 else if (*local_tls_type & GOT_TLS_DESC)
4352 num_of_got_entry_needed = 2;
4353
4354 sgot->size += (num_of_got_entry_needed << 2);
4355
4356 /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4357 others need a slot in .rela.got. */
4358 if (*local_tls_type == GOT_TLS_DESC)
4359 {
4360 if (bfd_link_pic (info))
4361 {
4362 if (htab->tls_desc_trampoline)
4363 {
4364 htab->num_tls_desc++;
4365 htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4366 htab->tls_trampoline = -1;
4367 }
4368 else
4369 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4370 }
4371 else
4372 {
4373 /* TLS_DESC -> TLS_LE */
4374 }
4375 }
4376 else
4377 {
4378 htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4379 }
35c08157
KLC
4380 }
4381 else
fbaf61ad
NC
4382 {
4383 *local_got = (bfd_vma) -1;
4384 *local_tlsdesc_gotent = (bfd_vma) -1;
4385 }
35c08157
KLC
4386 }
4387 }
4388
4389 /* Allocate global sym .plt and .got entries, and space for global
4390 sym dynamic relocs. */
4391 elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4392
fbaf61ad
NC
4393 /* For every jump slot reserved in the sgotplt, reloc_count is
4394 incremented. However, when we reserve space for TLS descriptors,
4395 it's not incremented, so in order to compute the space reserved
4396 for them, it suffices to multiply the reloc count by the jump
4397 slot size. */
4398 if (htab->tls_desc_trampoline && htab->root.srelplt)
4399 htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4400
4401 if (htab->tls_trampoline)
4402 {
4403 htab->tls_trampoline = htab->root.splt->size;
4404
4405 /* If we're not using lazy TLS relocations, don't generate the
4406 PLT and GOT entries they require. */
9bcc30e4
L
4407 if ((info->flags & DF_BIND_NOW))
4408 htab->root.tlsdesc_plt = 0;
4409 else
fbaf61ad 4410 {
9bcc30e4 4411 htab->root.tlsdesc_got = htab->root.sgot->size;
fbaf61ad
NC
4412 htab->root.sgot->size += 4;
4413
9bcc30e4 4414 htab->root.tlsdesc_plt = htab->root.splt->size;
fbaf61ad
NC
4415 htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4416 }
4417 }
4418
35c08157
KLC
4419 /* We now have determined the sizes of the various dynamic sections.
4420 Allocate memory for them. */
fbaf61ad
NC
4421 /* The check_relocs and adjust_dynamic_symbol entry points have
4422 determined the sizes of the various dynamic sections. Allocate
4423 memory for them. */
35c08157
KLC
4424 relocs = FALSE;
4425 for (s = dynobj->sections; s != NULL; s = s->next)
4426 {
4427 if ((s->flags & SEC_LINKER_CREATED) == 0)
4428 continue;
4429
ce558b89 4430 if (s == htab->root.splt)
35c08157
KLC
4431 {
4432 /* Strip this section if we don't need it; see the
4433 comment below. */
3084d7a2 4434 ;
35c08157 4435 }
fbaf61ad 4436 else if (s == elf_hash_table (info)->sgot)
35c08157
KLC
4437 {
4438 got_size += s->size;
4439 }
fbaf61ad 4440 else if (s == elf_hash_table (info)->sgotplt)
35c08157
KLC
4441 {
4442 got_size += s->size;
4443 }
fd361982 4444 else if (strncmp (bfd_section_name (s), ".rela", 5) == 0)
35c08157 4445 {
fbaf61ad 4446 if (s->size != 0 && s != elf_hash_table (info)->srelplt)
35c08157
KLC
4447 relocs = TRUE;
4448
4449 /* We use the reloc_count field as a counter if we need
4450 to copy relocs into the output file. */
4451 s->reloc_count = 0;
4452 }
4453 else
4454 {
4455 /* It's not one of our sections, so don't allocate space. */
4456 continue;
4457 }
4458
4459 if (s->size == 0)
4460 {
4461 /* If we don't need this section, strip it from the
4462 output file. This is mostly to handle .rela.bss and
4463 .rela.plt. We must create both sections in
4464 create_dynamic_sections, because they must be created
4465 before the linker maps input sections to output
4466 sections. The linker does that before
4467 adjust_dynamic_symbol is called, and it is that
4468 function which decides whether anything needs to go
4469 into these sections. */
4470 s->flags |= SEC_EXCLUDE;
4471 continue;
4472 }
4473
4474 /* Allocate memory for the section contents. We use bfd_zalloc
4475 here in case unused entries are not reclaimed before the
4476 section's contents are written out. This should not happen,
4477 but this way if it does, we get a R_NDS32_NONE reloc instead
4478 of garbage. */
4479 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4480 if (s->contents == NULL)
4481 return FALSE;
4482 }
4483
3084d7a2 4484 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
35c08157
KLC
4485}
4486
4487static bfd_reloc_status_type
4488nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4489 bfd_vma relocation, bfd_byte *location)
4490{
4491 int size;
4492 bfd_vma x = 0;
4493 bfd_reloc_status_type flag;
4494 unsigned int rightshift = howto->rightshift;
4495 unsigned int bitpos = howto->bitpos;
4496
4497 /* If the size is negative, negate RELOCATION. This isn't very
4498 general. */
4499 if (howto->size < 0)
4500 relocation = -relocation;
4501
4502 /* Get the value we are going to relocate. */
4503 size = bfd_get_reloc_size (howto);
4504 switch (size)
4505 {
4506 default:
35c08157
KLC
4507 abort ();
4508 break;
6346d5ca
AM
4509 case 0:
4510 return bfd_reloc_ok;
35c08157
KLC
4511 case 2:
4512 x = bfd_getb16 (location);
4513 break;
4514 case 4:
4515 x = bfd_getb32 (location);
4516 break;
4517 }
4518
4519 /* Check for overflow. FIXME: We may drop bits during the addition
4520 which we don't check for. We must either check at every single
4521 operation, which would be tedious, or we must do the computations
4522 in a type larger than bfd_vma, which would be inefficient. */
4523 flag = bfd_reloc_ok;
4524 if (howto->complain_on_overflow != complain_overflow_dont)
4525 {
4526 bfd_vma addrmask, fieldmask, signmask, ss;
4527 bfd_vma a, b, sum;
4528
4529 /* Get the values to be added together. For signed and unsigned
4530 relocations, we assume that all values should be truncated to
4531 the size of an address. For bitfields, all the bits matter.
4532 See also bfd_check_overflow. */
4533 fieldmask = N_ONES (howto->bitsize);
4534 signmask = ~fieldmask;
4535 addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4536 a = (relocation & addrmask) >> rightshift;
4537 b = (x & howto->src_mask & addrmask) >> bitpos;
4538
4539 switch (howto->complain_on_overflow)
4540 {
4541 case complain_overflow_signed:
4542 /* If any sign bits are set, all sign bits must be set.
4543 That is, A must be a valid negative address after
4544 shifting. */
4545 signmask = ~(fieldmask >> 1);
4546 /* Fall through. */
4547
4548 case complain_overflow_bitfield:
4549 /* Much like the signed check, but for a field one bit
4550 wider. We allow a bitfield to represent numbers in the
4551 range -2**n to 2**n-1, where n is the number of bits in the
4552 field. Note that when bfd_vma is 32 bits, a 32-bit reloc
4553 can't overflow, which is exactly what we want. */
4554 ss = a & signmask;
4555 if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4556 flag = bfd_reloc_overflow;
4557
4558 /* We only need this next bit of code if the sign bit of B
4559 is below the sign bit of A. This would only happen if
4560 SRC_MASK had fewer bits than BITSIZE. Note that if
4561 SRC_MASK has more bits than BITSIZE, we can get into
4562 trouble; we would need to verify that B is in range, as
4563 we do for A above. */
4564 ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4565 ss >>= bitpos;
4566
4567 /* Set all the bits above the sign bit. */
4568 b = (b ^ ss) - ss;
4569
4570 /* Now we can do the addition. */
4571 sum = a + b;
4572
4573 /* See if the result has the correct sign. Bits above the
4574 sign bit are junk now; ignore them. If the sum is
4575 positive, make sure we did not have all negative inputs;
4576 if the sum is negative, make sure we did not have all
4577 positive inputs. The test below looks only at the sign
4578 bits, and it really just
4579 SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4580
4581 We mask with addrmask here to explicitly allow an address
4582 wrap-around. The Linux kernel relies on it, and it is
4583 the only way to write assembler code which can run when
4584 loaded at a location 0x80000000 away from the location at
4585 which it is linked. */
4586 if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4587 flag = bfd_reloc_overflow;
4588
4589 break;
4590
4591 case complain_overflow_unsigned:
4592 /* Checking for an unsigned overflow is relatively easy:
4593 trim the addresses and add, and trim the result as well.
4594 Overflow is normally indicated when the result does not
4595 fit in the field. However, we also need to consider the
4596 case when, e.g., fieldmask is 0x7fffffff or smaller, an
4597 input is 0x80000000, and bfd_vma is only 32 bits; then we
4598 will get sum == 0, but there is an overflow, since the
4599 inputs did not fit in the field. Instead of doing a
4600 separate test, we can check for this by or-ing in the
4601 operands when testing for the sum overflowing its final
4602 field. */
4603 sum = (a + b) & addrmask;
4604 if ((a | b | sum) & signmask)
4605 flag = bfd_reloc_overflow;
4606 break;
4607
4608 default:
4609 abort ();
4610 }
4611 }
4612
4613 /* Put RELOCATION in the right bits. */
4614 relocation >>= (bfd_vma) rightshift;
4615 relocation <<= (bfd_vma) bitpos;
4616
4617 /* Add RELOCATION to the right bits of X. */
4618 /* FIXME : 090616
4619 Because the relaxation may generate duplicate relocation at one address,
4620 an addition to immediate in the instruction may cause the relocation added
4621 several times.
4622 This bug should be fixed in assembler, but a check is also needed here. */
4623 if (howto->partial_inplace)
4624 x = ((x & ~howto->dst_mask)
4625 | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4626 else
4627 x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4628
4629
4630 /* Put the relocated value back in the object file. */
4631 switch (size)
4632 {
4633 default:
4634 case 0:
4635 case 1:
4636 case 8:
4637 abort ();
4638 break;
4639 case 2:
4640 bfd_putb16 (x, location);
4641 break;
4642 case 4:
4643 bfd_putb32 (x, location);
4644 break;
4645 }
4646
4647 return flag;
4648}
4649
4650static bfd_reloc_status_type
4651nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4652 asection *input_section, bfd_byte *contents,
4653 bfd_vma address, bfd_vma value, bfd_vma addend)
4654{
4655 bfd_vma relocation;
4656
4657 /* Sanity check the address. */
4658 if (address > bfd_get_section_limit (input_bfd, input_section))
4659 return bfd_reloc_outofrange;
4660
4661 /* This function assumes that we are dealing with a basic relocation
4662 against a symbol. We want to compute the value of the symbol to
4663 relocate to. This is just VALUE, the value of the symbol, plus
4664 ADDEND, any addend associated with the reloc. */
4665 relocation = value + addend;
4666
4667 /* If the relocation is PC relative, we want to set RELOCATION to
4668 the distance between the symbol (currently in RELOCATION) and the
c2bf1eec 4669 location we are relocating. If pcrel_offset is FALSE we do not
35c08157
KLC
4670 need to subtract out the offset of the location within the
4671 section (which is just ADDRESS). */
4672 if (howto->pc_relative)
4673 {
4674 relocation -= (input_section->output_section->vma
4675 + input_section->output_offset);
4676 if (howto->pcrel_offset)
4677 relocation -= address;
4678 }
4679
4680 return nds32_relocate_contents (howto, input_bfd, relocation,
4681 contents + address);
4682}
4683
4684static bfd_boolean
4685nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4686 const char *name,
4687 Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4688 asection *input_sec,
4689 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4690{
4691 const char *source;
4692 FILE *sym_ld_script = NULL;
4693 struct elf_nds32_link_hash_table *table;
4694
4695 table = nds32_elf_hash_table (info);
4696 sym_ld_script = table->sym_ld_script;
4697 if (!sym_ld_script)
4698 return TRUE;
4699
4700 if (!h || !name || *name == '\0')
4701 return TRUE;
4702
4703 if (input_sec->flags & SEC_EXCLUDE)
4704 return TRUE;
4705
4706 if (!check_start_export_sym)
4707 {
4708 fprintf (sym_ld_script, "SECTIONS\n{\n");
4709 check_start_export_sym = 1;
4710 }
4711
4712 if (h->root.type == bfd_link_hash_defined
4713 || h->root.type == bfd_link_hash_defweak)
4714 {
4715 if (!h->root.u.def.section->output_section)
4716 return TRUE;
4717
4718 if (bfd_is_const_section (input_sec))
4719 source = input_sec->name;
4720 else
765cf5f6 4721 source = bfd_get_filename (input_sec->owner);
35c08157
KLC
4722
4723 fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4724 h->root.root.string,
4725 (long) (h->root.u.def.value
4726 + h->root.u.def.section->output_section->vma
4727 + h->root.u.def.section->output_offset), source);
4728 }
4729
4730 return TRUE;
4731}
4732
4733/* Relocate an NDS32/D ELF section.
4734 There is some attempt to make this function usable for many architectures,
4735 both for RELA and REL type relocs, if only to serve as a learning tool.
4736
4737 The RELOCATE_SECTION function is called by the new ELF backend linker
4738 to handle the relocations for a section.
4739
4740 The relocs are always passed as Rela structures; if the section
4741 actually uses Rel structures, the r_addend field will always be
4742 zero.
4743
4744 This function is responsible for adjust the section contents as
4745 necessary, and (if using Rela relocs and generating a
4746 relocatable output file) adjusting the reloc addend as
4747 necessary.
4748
4749 This function does not have to worry about setting the reloc
4750 address or the reloc symbol index.
4751
4752 LOCAL_SYMS is a pointer to the swapped in local symbols.
4753
4754 LOCAL_SECTIONS is an array giving the section in the input file
4755 corresponding to the st_shndx field of each local symbol.
4756
4757 The global hash table entry for the global symbols can be found
4758 via elf_sym_hashes (input_bfd).
4759
4760 When generating relocatable output, this function must handle
4761 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
4762 going to be the section symbol corresponding to the output
4763 section, which means that the addend must be adjusted
4764 accordingly. */
4765
fbaf61ad
NC
4766/* Return the base VMA address which should be subtracted from real addresses
4767 when resolving @dtpoff relocation.
4768 This is PT_TLS segment p_vaddr. */
4769
4770/* Return the relocation value for @tpoff relocation
4771 if STT_TLS virtual address is ADDRESS. */
4772
4773/* Return the relocation value for @gottpoff relocation
4774 if STT_TLS virtual address is ADDRESS. */
4775
1c8f6a4d 4776static bfd_vma
fbaf61ad 4777gottpoff (struct bfd_link_info *info, bfd_vma address)
1c8f6a4d 4778{
fbaf61ad
NC
4779 bfd_vma tp_base;
4780 bfd_vma tp_offset;
4781
1c8f6a4d
KLC
4782 /* If tls_sec is NULL, we should have signalled an error already. */
4783 if (elf_hash_table (info)->tls_sec == NULL)
4784 return 0;
fbaf61ad
NC
4785
4786 tp_base = elf_hash_table (info)->tls_sec->vma;
4787 tp_offset = address - tp_base;
4788
4789 return tp_offset;
1c8f6a4d
KLC
4790}
4791
35c08157 4792static bfd_boolean
fbaf61ad
NC
4793patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4794{
4795 /* TLS_GD/TLS_LD model #1
4796 46 00 00 00 sethi $r0,#0x0
4797 58 00 00 00 ori $r0,$r0,#0x0
4798 40 00 74 00 add $r0,$r0,$gp
4799 04 10 00 00 lwi $r1,[$r0+#0x0]
4800 4b e0 04 01 jral $lp,$r1 */
4801
4802 /* TLS_GD/TLS_LD model #2
4803 46 00 00 00 sethi $r0,#0x0
4804 58 00 00 00 ori $r0,$r0,#0x0
4805 38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4806 40 00 74 00 add $r0,$r0,$gp
4807 4b e0 04 01 jral $lp,$r1 */
4808
4809 /* TLS_IE model (non-PIC)
4810 46 00 00 00 sethi $r0,#0x0
4811 04 00 00 00 lwi $r0,[$r0+#0x0]
4812 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4813
4814 /* TLS_IE model (PIC)
4815 46 00 00 00 sethi $r0,#0x0
4816 58 00 00 00 ori $r0,$r0,#0x0
4817 38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4818 38 00 64 02 lw $r0,[$r0+($r25<<#0x0)] */
4819
4820 /* TLS_GD_TO_IE model
4821 46 00 00 00 sethi $r0,#0x0
4822 58 00 00 00 ori $r0,$r0,#0x0
4823 40 00 74 00 add $r0,$rM,$gp
4824 04 00 00 01 lwi $r0,[$r0+#0x4]
4825 40 00 64 00 add $r0,$r0,$r25 */
4826
4827 bfd_boolean rz = FALSE;
4828
4829 typedef struct
4830 {
4831 uint32_t opcode;
4832 uint32_t mask;
4833 } pat_t;
4834
4835 uint32_t patch[3] =
4836 {
4837 0x40007400, /* add $r0,$rM,$gp */
4838 0x04000001, /* lwi $r0,[$r0+#0x4] */
4839 0x40006400, /* add $r0,$r0,$r25 */
4840 };
4841
4842 pat_t mode0[3] =
4843 {
4844 { 0x40000000, 0xfe0003ff },
4845 { 0x04000000, 0xfe000000 },
4846 { 0x4be00001, 0xffff83ff },
4847 };
4848
4849 pat_t mode1[3] =
4850 {
4851 { 0x38007402, 0xfe007fff },
4852 { 0x40007400, 0xfe007fff },
4853 { 0x4be00001, 0xffff83ff },
4854 };
4855
4856 unsigned char *p = contents + rel->r_offset;
4857
4858 uint32_t insn;
4859 uint32_t regidx = 0;
4860 insn = bfd_getb32 (p);
4861 if (INSN_SETHI == (0xfe0fffffu & insn))
4862 {
4863 regidx = 0x1f & (insn >> 20);
4864 p += 4;
4865 }
4866
4867 insn = bfd_getb32 (p);
4868 if (INSN_ORI == (0xfe007fffu & insn))
4869 {
4870 regidx = 0x1f & (insn >> 20);
4871 p += 4;
4872 }
4873
4874 if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction. */
4875 {
4876 /* already patched? */
4877 if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4878 (patch[1] == bfd_getb32 (p + 4)))
4879 rz = TRUE;
4880 }
4881 else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4882 {
4883 if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4884 (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4885 {
4886 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4887 bfd_putb32 (patch[1], p + 4);
4888 bfd_putb32 (patch[2], p + 8);
4889 rz = TRUE;
4890 }
4891 }
4892 else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4893 {
4894 if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4895 (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
4896 {
4897 bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4898 bfd_putb32 (patch[1], p + 4);
4899 bfd_putb32 (patch[2], p + 8);
4900 rz = TRUE;
4901 }
4902 }
4903
4904 if (!rz)
4905 {
765cf5f6 4906 printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
fbaf61ad
NC
4907 (int) rel->r_offset);
4908 BFD_ASSERT(0); /* Unsupported pattern. */
4909 }
4910
4911 return rz;
4912}
4913
4914static enum elf_nds32_tls_type
4915get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
4916
4917static unsigned int
4918ones32 (register unsigned int x)
4919{
4920 /* 32-bit recursive reduction using SWAR...
4921 but first step is mapping 2-bit values
4922 into sum of 2 1-bit values in sneaky way. */
4923 x -= ((x >> 1) & 0x55555555);
4924 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
4925 x = (((x >> 4) + x) & 0x0f0f0f0f);
4926 x += (x >> 8);
4927 x += (x >> 16);
4928 return (x & 0x0000003f);
4929}
4930
a767a1c4 4931#if !HAVE_FLS
fbaf61ad
NC
4932static unsigned int
4933fls (register unsigned int x)
4934{
4935 return ffs (x & (-x));
4936}
a767a1c4 4937#endif /* !HAVE_FLS */
fbaf61ad
NC
4938
4939#define nds32_elf_local_tlsdesc_gotent(bfd) \
4940 (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
4941
4942static bfd_boolean
4943nds32_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
4944 struct bfd_link_info * info,
4945 bfd * input_bfd,
4946 asection * input_section,
4947 bfd_byte * contents,
4948 Elf_Internal_Rela * relocs,
4949 Elf_Internal_Sym * local_syms,
4950 asection ** local_sections)
4951{
4952 Elf_Internal_Shdr *symtab_hdr;
4953 struct elf_link_hash_entry **sym_hashes;
4954 Elf_Internal_Rela *rel, *relend;
4955 bfd_boolean ret = TRUE; /* Assume success. */
4956 int align = 0;
4957 bfd_reloc_status_type r;
4958 const char *errmsg = NULL;
4959 bfd_vma gp;
4960 struct elf_link_hash_table *ehtab;
4961 struct elf_nds32_link_hash_table *htab;
4962 bfd *dynobj;
4963 bfd_vma *local_got_offsets;
4964 asection *sgot, *splt, *sreloc;
4965 bfd_vma high_address;
4966 struct elf_nds32_link_hash_table *table;
4967 int eliminate_gc_relocs;
35c08157
KLC
4968 bfd_vma fpbase_addr;
4969
4970 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4971 sym_hashes = elf_sym_hashes (input_bfd);
fbaf61ad 4972 ehtab = elf_hash_table (info);
35c08157
KLC
4973 htab = nds32_elf_hash_table (info);
4974 high_address = bfd_get_section_limit (input_bfd, input_section);
4975
4976 dynobj = htab->root.dynobj;
4977 local_got_offsets = elf_local_got_offsets (input_bfd);
4978
fbaf61ad
NC
4979 sgot = ehtab->sgot;
4980 splt = ehtab->splt;
35c08157
KLC
4981 sreloc = NULL;
4982
4983 rel = relocs;
4984 relend = relocs + input_section->reloc_count;
4985
4986 table = nds32_elf_hash_table (info);
4987 eliminate_gc_relocs = table->eliminate_gc_relocs;
fbaf61ad 4988
35c08157 4989 /* By this time, we can adjust the value of _SDA_BASE_. */
fbaf61ad 4990 /* Explain _SDA_BASE_ */
0e1862bb 4991 if ((!bfd_link_relocatable (info)))
35c08157
KLC
4992 {
4993 is_SDA_BASE_set = 1;
4994 r = nds32_elf_final_sda_base (output_bfd, info, &gp, TRUE);
4995 if (r != bfd_reloc_ok)
4996 return FALSE;
4997 }
4998
fbaf61ad
NC
4999 /* Do TLS model conversion once at first. */
5000 nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5001
35c08157
KLC
5002 /* Use gp as fp to prevent truncated fit. Because in relaxation time
5003 the fp value is set as gp, and it has be reverted for instruction
5004 setting fp. */
5005 fpbase_addr = elf_gp (output_bfd);
5006
fbaf61ad 5007 /* Deal with (dynamic) relocations. */
35c08157
KLC
5008 for (rel = relocs; rel < relend; rel++)
5009 {
5010 enum elf_nds32_reloc_type r_type;
5011 reloc_howto_type *howto = NULL;
5012 unsigned long r_symndx;
5013 struct elf_link_hash_entry *h = NULL;
5014 Elf_Internal_Sym *sym = NULL;
5015 asection *sec;
5016 bfd_vma relocation;
fbaf61ad
NC
5017 bfd_vma relocation_sym = 0xdeadbeef;
5018 Elf_Internal_Rela *lorel;
5019 bfd_vma off;
35c08157
KLC
5020
5021 /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5022 ensure it's zero (we use REL relocs, not RELA). Therefore this
5023 should be assigning zero to `addend', but for clarity we use
5024 `r_addend'. */
5025
5026 bfd_vma addend = rel->r_addend;
5027 bfd_vma offset = rel->r_offset;
5028
5029 r_type = ELF32_R_TYPE (rel->r_info);
5030 if (r_type >= R_NDS32_max)
5031 {
695344c0 5032 /* xgettext:c-format */
0aa13fee 5033 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
4eca0228 5034 input_bfd, r_type);
35c08157
KLC
5035 bfd_set_error (bfd_error_bad_value);
5036 ret = FALSE;
5037 continue;
5038 }
5039
5040 if (r_type == R_NDS32_GNU_VTENTRY
5041 || r_type == R_NDS32_GNU_VTINHERIT
5042 || r_type == R_NDS32_NONE
5043 || r_type == R_NDS32_RELA_GNU_VTENTRY
5044 || r_type == R_NDS32_RELA_GNU_VTINHERIT
5045 || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5046 || r_type == R_NDS32_DATA
fbaf61ad 5047 || r_type == R_NDS32_TRAN)
35c08157
KLC
5048 continue;
5049
0c4bd9d9
KLC
5050 /* If we enter the fp-as-gp region. Resolve the address
5051 of best fp-base. */
35c08157
KLC
5052 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5053 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5054 {
5055 int dist;
5056
5057 /* Distance to relocation of best fp-base is encoded in R_SYM. */
5058 dist = rel->r_addend >> 16;
5059 fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5060 local_syms, symtab_hdr);
5061 }
5062 else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5063 && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5064 {
5065 fpbase_addr = elf_gp (output_bfd);
5066 }
5067
fbaf61ad
NC
5068 /* Skip the relocations used for relaxation. */
5069 /* We have to update LONGCALL and LONGJUMP
5070 relocations when generating the relocatable files. */
5071 if (!bfd_link_relocatable (info)
5072 && (r_type >= R_NDS32_RELAX_ENTRY
5073 || (r_type >= R_NDS32_LONGCALL4
5074 && r_type <= R_NDS32_LONGJUMP7)))
35c08157
KLC
5075 continue;
5076
5077 howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5078 r_symndx = ELF32_R_SYM (rel->r_info);
5079
5080 /* This is a final link. */
5081 sym = NULL;
5082 sec = NULL;
5083 h = NULL;
5084
5085 if (r_symndx < symtab_hdr->sh_info)
5086 {
5087 /* Local symbol. */
5088 sym = local_syms + r_symndx;
5089 sec = local_sections[r_symndx];
5090
5091 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5092 addend = rel->r_addend;
fbaf61ad
NC
5093
5094 /* keep symbol location for static TLS_IE GOT entry */
5095 relocation_sym = relocation;
5096 if (bfd_link_relocatable (info))
5097 {
5098 /* This is a relocatable link. We don't have to change
5099 anything, unless the reloc is against a section symbol,
5100 in which case we have to adjust according to where the
5101 section symbol winds up in the output section. */
5102 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5103 rel->r_addend += sec->output_offset + sym->st_value;
5104
5105 continue;
5106 }
35c08157
KLC
5107 }
5108 else
5109 {
5110 /* External symbol. */
fbaf61ad
NC
5111 if (bfd_link_relocatable (info))
5112 continue;
35c08157
KLC
5113 bfd_boolean warned, ignored, unresolved_reloc;
5114 int symndx = r_symndx - symtab_hdr->sh_info;
5115
5116 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5117 r_symndx, symtab_hdr, sym_hashes, h, sec,
5118 relocation, unresolved_reloc, warned,
5119 ignored);
5120
fbaf61ad
NC
5121 /* keep symbol location for static TLS_IE GOT entry */
5122 relocation_sym = relocation;
5123
35c08157
KLC
5124 /* la $fp, _FP_BASE_ is per-function (region).
5125 Handle it specially. */
5126 switch ((int) r_type)
5127 {
fbaf61ad
NC
5128 case R_NDS32_HI20_RELA:
5129 case R_NDS32_LO12S0_RELA:
5130 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5131 FP_BASE_NAME) == 0)
5132 {
5133 if (!bfd_link_pie (info))
5134 {
5135 _bfd_error_handler
5136 ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5137 input_bfd);
5138 }
5139 relocation = fpbase_addr;
5140 }
5141 break;
35c08157
KLC
5142 case R_NDS32_SDA19S0_RELA:
5143 case R_NDS32_SDA15S0_RELA:
5144 case R_NDS32_20_RELA:
5145 if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5146 FP_BASE_NAME) == 0)
5147 {
5148 relocation = fpbase_addr;
5149 break;
5150 }
5151 }
35c08157
KLC
5152 }
5153
5154 /* Sanity check the address. */
5155 if (offset > high_address)
5156 {
5157 r = bfd_reloc_outofrange;
5158 goto check_reloc;
5159 }
5160
fbaf61ad 5161 if (r_type >= R_NDS32_RELAX_ENTRY)
35c08157
KLC
5162 continue;
5163
5164 switch ((int) r_type)
5165 {
5166 case R_NDS32_GOTOFF:
5167 /* Relocation is relative to the start of the global offset
5168 table (for ld24 rx, #uimm24), e.g. access at label+addend
5169
5170 ld24 rx. #label@GOTOFF + addend
5171 sub rx, r12. */
5172 case R_NDS32_GOTOFF_HI20:
5173 case R_NDS32_GOTOFF_LO12:
5174 case R_NDS32_GOTOFF_LO15:
5175 case R_NDS32_GOTOFF_LO19:
5176 BFD_ASSERT (sgot != NULL);
5177
5178 relocation -= elf_gp (output_bfd);
5179 break;
5180
5181 case R_NDS32_9_PLTREL:
5182 case R_NDS32_25_PLTREL:
5183 /* Relocation is to the entry for this symbol in the
5184 procedure linkage table. */
5185
5186 /* The native assembler will generate a 25_PLTREL reloc
5187 for a local symbol if you assemble a call from one
5188 section to another when using -K pic. */
5189 if (h == NULL)
5190 break;
5191
5192 if (h->forced_local)
5193 break;
5194
5195 /* We didn't make a PLT entry for this symbol. This
5196 happens when statically linking PIC code, or when
5197 using -Bsymbolic. */
5198 if (h->plt.offset == (bfd_vma) - 1)
5199 break;
5200
5201 relocation = (splt->output_section->vma
5202 + splt->output_offset + h->plt.offset);
5203 break;
5204
5205 case R_NDS32_PLT_GOTREL_HI20:
5206 case R_NDS32_PLT_GOTREL_LO12:
5207 case R_NDS32_PLT_GOTREL_LO15:
5208 case R_NDS32_PLT_GOTREL_LO19:
5209 case R_NDS32_PLT_GOTREL_LO20:
fbaf61ad
NC
5210 if (h == NULL
5211 || h->forced_local
5212 || h->plt.offset == (bfd_vma) -1
5213 || (bfd_link_pie (info) && h->def_regular))
35c08157 5214 {
fbaf61ad
NC
5215 /* Maybe we should find better checking to optimize
5216 PIE PLT relocations. */
35c08157
KLC
5217 /* We didn't make a PLT entry for this symbol. This
5218 happens when statically linking PIC code, or when
5219 using -Bsymbolic. */
fbaf61ad
NC
5220 if (h)
5221 h->plt.offset = (bfd_vma) -1; /* Cancel PLT trampoline. */
35c08157
KLC
5222 relocation -= elf_gp (output_bfd);
5223 break;
5224 }
5225
5226 relocation = (splt->output_section->vma
5227 + splt->output_offset + h->plt.offset);
5228
5229 relocation -= elf_gp (output_bfd);
5230 break;
5231
5232 case R_NDS32_PLTREL_HI20:
5233 case R_NDS32_PLTREL_LO12:
5234
5235 /* Relocation is to the entry for this symbol in the
5236 procedure linkage table. */
5237
5238 /* The native assembler will generate a 25_PLTREL reloc
5239 for a local symbol if you assemble a call from one
5240 section to another when using -K pic. */
5241 if (h == NULL)
5242 break;
5243
5244 if (h->forced_local)
5245 break;
5246
5247 if (h->plt.offset == (bfd_vma) - 1)
5248 /* We didn't make a PLT entry for this symbol. This
5249 happens when statically linking PIC code, or when
5250 using -Bsymbolic. */
5251 break;
5252
5253 if (splt == NULL)
5254 break;
5255
5256 relocation = (splt->output_section->vma
5257 + splt->output_offset
5258 + h->plt.offset + 4)
5259 - (input_section->output_section->vma
5260 + input_section->output_offset
5261 + rel->r_offset);
5262
5263 break;
5264
5265 case R_NDS32_GOTPC20:
5266 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5267 ld24 rx,#_GLOBAL_OFFSET_TABLE_ */
5268 relocation = elf_gp (output_bfd);
5269 break;
5270
5271 case R_NDS32_GOTPC_HI20:
5272 case R_NDS32_GOTPC_LO12:
fbaf61ad
NC
5273 /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5274 bl .+4
5275 seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5276 or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5277 or
5278 bl .+4
5279 seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5280 add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4) */
5281 relocation = elf_gp (output_bfd);
5282 relocation -= (input_section->output_section->vma
5283 + input_section->output_offset + rel->r_offset);
5284 break;
35c08157
KLC
5285
5286 case R_NDS32_GOT20:
5287 /* Fall through. */
5288 case R_NDS32_GOT_HI20:
5289 case R_NDS32_GOT_LO12:
5290 case R_NDS32_GOT_LO15:
5291 case R_NDS32_GOT_LO19:
5292 /* Relocation is to the entry for this symbol in the global
5293 offset table. */
5294 BFD_ASSERT (sgot != NULL);
5295
5296 if (h != NULL)
5297 {
fbaf61ad 5298 /* External symbol */
35c08157 5299 bfd_boolean dyn;
35c08157
KLC
5300
5301 off = h->got.offset;
5302 BFD_ASSERT (off != (bfd_vma) - 1);
5303 dyn = htab->root.dynamic_sections_created;
0e1862bb
L
5304 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5305 bfd_link_pic (info),
5306 h)
5307 || (bfd_link_pic (info)
35c08157
KLC
5308 && (info->symbolic
5309 || h->dynindx == -1
5310 || h->forced_local) && h->def_regular))
5311 {
5312 /* This is actually a static link, or it is a
5313 -Bsymbolic link and the symbol is defined
5314 locally, or the symbol was forced to be local
5315 because of a version file. We must initialize
5316 this entry in the global offset table. Since the
5317 offset must always be a multiple of 4, we use the
5318 least significant bit to record whether we have
5319 initialized it already.
5320
5321 When doing a dynamic link, we create a .rela.got
5322 relocation entry to initialize the value. This
5323 is done in the finish_dynamic_symbol routine. */
fbaf61ad 5324 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5325 off &= ~1;
5326 else
5327 {
5328 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5329 h->got.offset |= 1;
5330 }
5331 }
5332 relocation = sgot->output_section->vma + sgot->output_offset + off
5333 - elf_gp (output_bfd);
5334 }
5335 else
5336 {
fbaf61ad 5337 /* Local symbol */
35c08157
KLC
5338 bfd_byte *loc;
5339
5340 BFD_ASSERT (local_got_offsets != NULL
5341 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5342
5343 off = local_got_offsets[r_symndx];
5344
5345 /* The offset must always be a multiple of 4. We use
5346 the least significant bit to record whether we have
5347 already processed this entry. */
fbaf61ad 5348 if ((off & 1) != 0) /* clear LSB */
35c08157
KLC
5349 off &= ~1;
5350 else
5351 {
5352 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5353
0e1862bb 5354 if (bfd_link_pic (info))
35c08157
KLC
5355 {
5356 asection *srelgot;
5357 Elf_Internal_Rela outrel;
5358
5359 /* We need to generate a R_NDS32_RELATIVE reloc
5360 for the dynamic linker. */
fbaf61ad 5361 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
35c08157
KLC
5362 BFD_ASSERT (srelgot != NULL);
5363
5364 outrel.r_offset = (elf_gp (output_bfd)
5365 + sgot->output_offset + off);
5366 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5367 outrel.r_addend = relocation;
5368 loc = srelgot->contents;
5369 loc +=
5370 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5371 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5372 ++srelgot->reloc_count;
5373 }
5374 local_got_offsets[r_symndx] |= 1;
5375 }
5376 relocation = sgot->output_section->vma + sgot->output_offset + off
5377 - elf_gp (output_bfd);
5378 }
5379
5380 break;
5381
5382 case R_NDS32_16_RELA:
5383 case R_NDS32_20_RELA:
5384 case R_NDS32_5_RELA:
5385 case R_NDS32_32_RELA:
5386 case R_NDS32_9_PCREL_RELA:
5387 case R_NDS32_WORD_9_PCREL_RELA:
5388 case R_NDS32_10_UPCREL_RELA:
5389 case R_NDS32_15_PCREL_RELA:
5390 case R_NDS32_17_PCREL_RELA:
5391 case R_NDS32_25_PCREL_RELA:
5392 case R_NDS32_HI20_RELA:
5393 case R_NDS32_LO12S3_RELA:
5394 case R_NDS32_LO12S2_RELA:
5395 case R_NDS32_LO12S2_DP_RELA:
5396 case R_NDS32_LO12S2_SP_RELA:
5397 case R_NDS32_LO12S1_RELA:
5398 case R_NDS32_LO12S0_RELA:
5399 case R_NDS32_LO12S0_ORI_RELA:
0e1862bb 5400 if (bfd_link_pic (info) && r_symndx != 0
35c08157
KLC
5401 && (input_section->flags & SEC_ALLOC) != 0
5402 && (eliminate_gc_relocs == 0
5403 || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5404 && ((r_type != R_NDS32_9_PCREL_RELA
5405 && r_type != R_NDS32_WORD_9_PCREL_RELA
5406 && r_type != R_NDS32_10_UPCREL_RELA
5407 && r_type != R_NDS32_15_PCREL_RELA
5408 && r_type != R_NDS32_17_PCREL_RELA
5409 && r_type != R_NDS32_25_PCREL_RELA
5410 && !(r_type == R_NDS32_32_RELA
5411 && strcmp (input_section->name, ".eh_frame") == 0))
5412 || (h != NULL && h->dynindx != -1
5413 && (!info->symbolic || !h->def_regular))))
5414 {
5415 Elf_Internal_Rela outrel;
5416 bfd_boolean skip, relocate;
5417 bfd_byte *loc;
5418
5419 /* When generating a shared object, these relocations
5420 are copied into the output file to be resolved at run
5421 time. */
5422
5423 if (sreloc == NULL)
5424 {
5425 const char *name;
5426
5427 name = bfd_elf_string_from_elf_section
5428 (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5429 elf_section_data (input_section)->rela.hdr->sh_name);
5430 if (name == NULL)
5431 return FALSE;
5432
5433 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
fd361982 5434 && strcmp (bfd_section_name (input_section),
35c08157
KLC
5435 name + 5) == 0);
5436
5437 sreloc = bfd_get_section_by_name (dynobj, name);
5438 BFD_ASSERT (sreloc != NULL);
5439 }
5440
5441 skip = FALSE;
5442 relocate = FALSE;
5443
5444 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5445 info,
5446 input_section,
5447 rel->r_offset);
5448 if (outrel.r_offset == (bfd_vma) - 1)
5449 skip = TRUE;
5450 else if (outrel.r_offset == (bfd_vma) - 2)
5451 skip = TRUE, relocate = TRUE;
5452 outrel.r_offset += (input_section->output_section->vma
5453 + input_section->output_offset);
5454
5455 if (skip)
5456 memset (&outrel, 0, sizeof outrel);
5457 else if (r_type == R_NDS32_17_PCREL_RELA
5458 || r_type == R_NDS32_15_PCREL_RELA
5459 || r_type == R_NDS32_25_PCREL_RELA)
5460 {
5461 BFD_ASSERT (h != NULL && h->dynindx != -1);
5462 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5463 outrel.r_addend = rel->r_addend;
5464 }
5465 else
5466 {
5467 /* h->dynindx may be -1 if this symbol was marked to
5468 become local. */
5469 if (h == NULL
5470 || ((info->symbolic || h->dynindx == -1)
fbaf61ad
NC
5471 && h->def_regular)
5472 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
5473 {
5474 relocate = TRUE;
5475 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5476 outrel.r_addend = relocation + rel->r_addend;
fbaf61ad
NC
5477
5478 if (h)
5479 {
5480 h->plt.offset = (bfd_vma) -1; /* cancel PLT trampoline. */
5481
5482 BFD_ASSERT (sgot != NULL);
5483 /* If we did not allocate got entry for the symbol,
5484 we can not fill the nonexistent got entry. */
5485 if (h->got.offset != (bfd_vma) -1
5486 && (h->got.offset & 1) == 0)
5487 {
5488 bfd_put_32 (output_bfd, outrel.r_addend,
5489 sgot->contents + h->got.offset);
5490 }
5491 }
35c08157
KLC
5492 }
5493 else
5494 {
fbaf61ad
NC
5495 if (h->dynindx == -1)
5496 {
5497 _bfd_error_handler
acef8081 5498 (_("%pB: relocation %s against `%s' can not be used when "
fbaf61ad
NC
5499 "making a shared object; recompile with -fPIC"),
5500 input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5501 bfd_set_error (bfd_error_bad_value);
5502 return FALSE;
5503 }
5504
35c08157
KLC
5505 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5506 outrel.r_addend = rel->r_addend;
5507 }
5508 }
5509
5510 loc = sreloc->contents;
5511 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5512 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5513 ++sreloc->reloc_count;
5514
5515 /* If this reloc is against an external symbol, we do
5516 not want to fiddle with the addend. Otherwise, we
5517 need to include the symbol value so that it becomes
5518 an addend for the dynamic reloc. */
5519 if (!relocate)
5520 continue;
5521 }
5522 break;
5523
5524 case R_NDS32_25_ABS_RELA:
0e1862bb 5525 if (bfd_link_pic (info))
35c08157 5526 {
4eca0228 5527 _bfd_error_handler
38f14ab8
AM
5528 (_("%pB: warning: %s unsupported in shared mode"),
5529 input_bfd, "R_NDS32_25_ABS_RELA");
35c08157
KLC
5530 return FALSE;
5531 }
5532 break;
5533
5534 case R_NDS32_9_PCREL:
5535 r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5536 contents, offset,
5537 sec, relocation, addend);
5538 goto check_reloc;
5539
5540 case R_NDS32_HI20:
fbaf61ad
NC
5541 /* We allow an arbitrary number of HI20 relocs before the
5542 LO12 reloc. This permits gcc to emit the HI and LO relocs
5543 itself. */
5544 for (lorel = rel + 1;
5545 (lorel < relend
5546 && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5547 continue;
5548 if (lorel < relend
5549 && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5550 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5551 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5552 || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
35c08157 5553 {
fbaf61ad
NC
5554 nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5555 contents, relocation + addend);
5556 r = bfd_reloc_ok;
35c08157 5557 }
fbaf61ad
NC
5558 else
5559 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5560 contents, offset, relocation,
5561 addend);
35c08157
KLC
5562 goto check_reloc;
5563
5564 case R_NDS32_GOT17S2_RELA:
5565 case R_NDS32_GOT15S2_RELA:
fbaf61ad
NC
5566 BFD_ASSERT (sgot != NULL);
5567
5568 if (h != NULL)
35c08157 5569 {
fbaf61ad 5570 bfd_boolean dyn;
35c08157 5571
fbaf61ad
NC
5572 off = h->got.offset;
5573 BFD_ASSERT (off != (bfd_vma) - 1);
35c08157 5574
fbaf61ad
NC
5575 dyn = htab->root.dynamic_sections_created;
5576 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5577 (dyn, bfd_link_pic (info), h)
5578 || (bfd_link_pic (info)
5579 && (info->symbolic
5580 || h->dynindx == -1
5581 || h->forced_local)
5582 && h->def_regular))
35c08157 5583 {
fbaf61ad
NC
5584 /* This is actually a static link, or it is a
5585 -Bsymbolic link and the symbol is defined
5586 locally, or the symbol was forced to be local
5587 because of a version file. We must initialize
5588 this entry in the global offset table. Since the
5589 offset must always be a multiple of 4, we use the
5590 least significant bit to record whether we have
5591 initialized it already.
5592
5593 When doing a dynamic link, we create a .rela.got
5594 relocation entry to initialize the value. This
5595 is done in the finish_dynamic_symbol routine. */
5596 if ((off & 1) != 0)
5597 off &= ~1;
5598 else
35c08157 5599 {
fbaf61ad
NC
5600 bfd_put_32 (output_bfd, relocation,
5601 sgot->contents + off);
5602 h->got.offset |= 1;
35c08157
KLC
5603 }
5604 }
fbaf61ad
NC
5605 }
5606 else
5607 {
5608 bfd_byte *loc;
35c08157 5609
fbaf61ad
NC
5610 BFD_ASSERT (local_got_offsets != NULL
5611 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
35c08157 5612
fbaf61ad 5613 off = local_got_offsets[r_symndx];
35c08157 5614
fbaf61ad
NC
5615 /* The offset must always be a multiple of 4. We use
5616 the least significant bit to record whether we have
5617 already processed this entry. */
5618 if ((off & 1) != 0)
5619 off &= ~1;
5620 else
5621 {
5622 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5623
5624 if (bfd_link_pic (info))
35c08157 5625 {
fbaf61ad
NC
5626 asection *srelgot;
5627 Elf_Internal_Rela outrel;
35c08157 5628
fbaf61ad
NC
5629 /* We need to generate a R_NDS32_RELATIVE reloc
5630 for the dynamic linker. */
5631 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5632 BFD_ASSERT (srelgot != NULL);
5633
5634 outrel.r_offset = (elf_gp (output_bfd)
5635 + sgot->output_offset + off);
5636 outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5637 outrel.r_addend = relocation;
5638 loc = srelgot->contents;
5639 loc +=
5640 srelgot->reloc_count * sizeof (Elf32_External_Rela);
5641 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5642 ++srelgot->reloc_count;
35c08157 5643 }
fbaf61ad 5644 local_got_offsets[r_symndx] |= 1;
35c08157 5645 }
35c08157 5646 }
fbaf61ad
NC
5647 relocation = sgot->output_section->vma + sgot->output_offset + off
5648 - elf_gp (output_bfd);
5649
35c08157
KLC
5650 if (relocation & align)
5651 {
5652 /* Incorrect alignment. */
4eca0228 5653 _bfd_error_handler
38f14ab8 5654 (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
35c08157
KLC
5655 ret = FALSE;
5656 r = bfd_reloc_dangerous;
5657 goto check_reloc;
5658 }
5659 break;
5660
5661 case R_NDS32_SDA16S3_RELA:
5662 case R_NDS32_SDA15S3_RELA:
5663 case R_NDS32_SDA15S3:
5664 align = 0x7;
5665 goto handle_sda;
5666
5667 case R_NDS32_SDA17S2_RELA:
5668 case R_NDS32_SDA15S2_RELA:
5669 case R_NDS32_SDA12S2_SP_RELA:
5670 case R_NDS32_SDA12S2_DP_RELA:
5671 case R_NDS32_SDA15S2:
5672 case R_NDS32_SDA_FP7U2_RELA:
5673 align = 0x3;
5674 goto handle_sda;
5675
5676 case R_NDS32_SDA18S1_RELA:
5677 case R_NDS32_SDA15S1_RELA:
5678 case R_NDS32_SDA15S1:
5679 align = 0x1;
5680 goto handle_sda;
5681
5682 case R_NDS32_SDA19S0_RELA:
5683 case R_NDS32_SDA15S0_RELA:
5684 case R_NDS32_SDA15S0:
fbaf61ad 5685 align = 0x0;
dc1e8a47 5686 handle_sda:
fbaf61ad 5687 BFD_ASSERT (sec != NULL);
35c08157 5688
fbaf61ad
NC
5689 /* If the symbol is in the abs section, the out_bfd will be null.
5690 This happens when the relocation has a symbol@GOTOFF. */
5691 r = nds32_elf_final_sda_base (output_bfd, info, &gp, FALSE);
5692 if (r != bfd_reloc_ok)
5693 {
5694 _bfd_error_handler
5695 (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5696 ret = FALSE;
5697 goto check_reloc;
5698 }
35c08157 5699
fbaf61ad
NC
5700 /* At this point `relocation' contains the object's
5701 address. */
5702 if (r_type == R_NDS32_SDA_FP7U2_RELA)
5703 {
5704 relocation -= fpbase_addr;
5705 }
5706 else
5707 relocation -= gp;
5708 /* Now it contains the offset from _SDA_BASE_. */
35c08157 5709
fbaf61ad 5710 /* Make sure alignment is correct. */
35c08157 5711
fbaf61ad
NC
5712 if (relocation & align)
5713 {
5714 /* Incorrect alignment. */
5715 _bfd_error_handler
5716 /* xgettext:c-format */
5717 (_("%pB(%pA): warning: unaligned small data access"
5718 " of type %d"),
5719 input_bfd, input_section, r_type);
5720 ret = FALSE;
5721 goto check_reloc;
35c08157 5722 }
35c08157 5723 break;
fbaf61ad 5724
35c08157
KLC
5725 case R_NDS32_17IFC_PCREL_RELA:
5726 case R_NDS32_10IFCU_PCREL_RELA:
fbaf61ad 5727 /* Do nothing. */
35c08157
KLC
5728 break;
5729
1c8f6a4d
KLC
5730 case R_NDS32_TLS_LE_HI20:
5731 case R_NDS32_TLS_LE_LO12:
5732 case R_NDS32_TLS_LE_20:
5733 case R_NDS32_TLS_LE_15S0:
5734 case R_NDS32_TLS_LE_15S1:
5735 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
5736 /* We do not have garbage collection for got entries.
5737 Therefore, IE to LE may have one empty entry, and DESC to
5738 LE may have two. */
1c8f6a4d
KLC
5739 if (elf_hash_table (info)->tls_sec != NULL)
5740 relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5741 break;
fbaf61ad 5742
1c8f6a4d
KLC
5743 case R_NDS32_TLS_IE_HI20:
5744 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
5745 case R_NDS32_TLS_DESC_HI20:
5746 case R_NDS32_TLS_DESC_LO12:
5747 case R_NDS32_TLS_IE_LO12:
5748 case R_NDS32_TLS_IEGP_HI20:
5749 case R_NDS32_TLS_IEGP_LO12:
5750 case R_NDS32_TLS_IEGP_LO12S2:
1c8f6a4d
KLC
5751 {
5752 /* Relocation is to the entry for this symbol in the global
5753 offset table. */
fbaf61ad 5754 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
1c8f6a4d
KLC
5755 asection *srelgot;
5756 Elf_Internal_Rela outrel;
1c8f6a4d
KLC
5757 bfd_byte *loc;
5758 int indx = 0;
5759
fbaf61ad
NC
5760 eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5761
1c8f6a4d
KLC
5762 BFD_ASSERT (sgot != NULL);
5763 if (h != NULL)
5764 {
5765 bfd_boolean dyn;
5766
5767 off = h->got.offset;
fbaf61ad 5768 BFD_ASSERT (off != (bfd_vma) -1);
1c8f6a4d
KLC
5769 dyn = htab->root.dynamic_sections_created;
5770 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
0e1862bb
L
5771 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5772 && (!bfd_link_pic (info)
1c8f6a4d
KLC
5773 || !SYMBOL_REFERENCES_LOCAL (info, h)))
5774 indx = h->dynindx;
5775 }
5776 else
5777 {
1c8f6a4d
KLC
5778 BFD_ASSERT (local_got_offsets != NULL
5779 && local_got_offsets[r_symndx] != (bfd_vma) - 1);
1c8f6a4d 5780 off = local_got_offsets[r_symndx];
1c8f6a4d
KLC
5781 tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5782 }
fbaf61ad 5783
1c8f6a4d
KLC
5784 relocation = sgot->output_section->vma + sgot->output_offset + off;
5785
fbaf61ad
NC
5786 if (1 < ones32 (tls_type))
5787 {
5788 eff_tls_type = 1 << (fls (tls_type) - 1);
5789 /* TLS model shall be handled in nds32_elf_unify_tls_model (). */
5790
5791 /* TLS model X -> LE is not implement yet!
5792 workaround here! */
5793 if (eff_tls_type == GOT_TLS_LE)
5794 {
5795 eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5796 }
5797 }
1c8f6a4d
KLC
5798
5799 /* The offset must always be a multiple of 4. We use
5800 the least significant bit to record whether we have
5801 already processed this entry. */
fbaf61ad
NC
5802 bfd_boolean need_relocs = FALSE;
5803 srelgot = ehtab->srelgot;
5804 if ((bfd_link_pic (info) || indx != 0)
5805 && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5806 || h->root.type != bfd_link_hash_undefweak))
5807 {
5808 need_relocs = TRUE;
5809 BFD_ASSERT (srelgot != NULL);
5810 }
5811
5812 if (off & 1)
5813 {
5814 off &= ~1;
5815 relocation &= ~1;
5816
5817 if (eff_tls_type & GOT_TLS_DESC)
5818 {
5819 relocation -= elf_gp (output_bfd);
5820 if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5821 {
5822 /* TLS model shall be converted. */
5823 BFD_ASSERT(0);
5824 }
5825 }
5826 else if (eff_tls_type & GOT_TLS_IEGP)
5827 {
5828 relocation -= elf_gp (output_bfd);
5829 }
5830 }
1c8f6a4d
KLC
5831 else
5832 {
fbaf61ad 5833 if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
1c8f6a4d 5834 {
fbaf61ad
NC
5835 /* TLS model workaround shall be applied. */
5836 BFD_ASSERT(0);
1c8f6a4d 5837 }
fbaf61ad 5838 else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
1c8f6a4d 5839 {
fbaf61ad
NC
5840 if (eff_tls_type & GOT_TLS_IEGP)
5841 relocation -= elf_gp(output_bfd);
5842
1c8f6a4d
KLC
5843 if (need_relocs)
5844 {
fbaf61ad
NC
5845 if (indx == 0)
5846 outrel.r_addend = gottpoff (info, relocation_sym);
1c8f6a4d
KLC
5847 else
5848 outrel.r_addend = 0;
5849 outrel.r_offset = (sgot->output_section->vma
fbaf61ad
NC
5850 + sgot->output_offset + off);
5851 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5852
5853 elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5854 &outrel);
1c8f6a4d
KLC
5855 }
5856 else
fbaf61ad
NC
5857 {
5858 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5859 sgot->contents + off);
5860 }
1c8f6a4d 5861 }
fbaf61ad
NC
5862 else if (eff_tls_type & GOT_TLS_DESC)
5863 {
5864 relocation -= elf_gp (output_bfd);
5865 if (need_relocs)
5866 {
5867 if (indx == 0)
5868 outrel.r_addend = gottpoff (info, relocation_sym);
5869 else
5870 outrel.r_addend = 0;
5871 outrel.r_offset = (sgot->output_section->vma
5872 + sgot->output_offset + off);
5873 outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5874
5875 if (htab->tls_desc_trampoline)
5876 {
5877 asection *srelplt;
5878 srelplt = ehtab->srelplt;
5879 loc = srelplt->contents;
5880 loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5881 BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5882 <= srelplt->contents + srelplt->size);
5883
5884 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5885 }
5886 else
5887 {
5888 loc = srelgot->contents;
5889 loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5890 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5891 ++srelgot->reloc_count;
5892 }
5893 }
5894 else
5895 {
5896 /* feed me! */
5897 bfd_put_32 (output_bfd, 0xdeadbeef,
5898 sgot->contents + off);
5899 bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5900 sgot->contents + off + 4);
5901 patch_tls_desc_to_ie (contents, rel, input_bfd);
5902 BFD_ASSERT(0);
5903 }
5904 }
5905 else
5906 {
5907 /* TLS model workaround shall be applied. */
5908 BFD_ASSERT(0);
5909 }
5910
5911 if (h != NULL)
5912 h->got.offset |= 1;
5913 else
5914 local_got_offsets[r_symndx] |= 1;
5915 }
5916 }
5917 break;
5918 /* DON'T fall through. */
5919
5920 default:
5921 /* OLD_NDS32_RELOC. */
5922
5923 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5924 contents, offset, relocation, addend);
5925 goto check_reloc;
5926 }
5927
5928 switch ((int) r_type)
5929 {
35c08157
KLC
5930 case R_NDS32_20_RELA:
5931 case R_NDS32_5_RELA:
5932 case R_NDS32_9_PCREL_RELA:
5933 case R_NDS32_WORD_9_PCREL_RELA:
5934 case R_NDS32_10_UPCREL_RELA:
5935 case R_NDS32_15_PCREL_RELA:
5936 case R_NDS32_17_PCREL_RELA:
5937 case R_NDS32_25_PCREL_RELA:
5938 case R_NDS32_25_ABS_RELA:
5939 case R_NDS32_HI20_RELA:
5940 case R_NDS32_LO12S3_RELA:
5941 case R_NDS32_LO12S2_RELA:
5942 case R_NDS32_LO12S2_DP_RELA:
5943 case R_NDS32_LO12S2_SP_RELA:
5944 case R_NDS32_LO12S1_RELA:
5945 case R_NDS32_LO12S0_RELA:
5946 case R_NDS32_LO12S0_ORI_RELA:
5947 case R_NDS32_SDA16S3_RELA:
5948 case R_NDS32_SDA17S2_RELA:
5949 case R_NDS32_SDA18S1_RELA:
5950 case R_NDS32_SDA19S0_RELA:
5951 case R_NDS32_SDA15S3_RELA:
5952 case R_NDS32_SDA15S2_RELA:
5953 case R_NDS32_SDA12S2_DP_RELA:
5954 case R_NDS32_SDA12S2_SP_RELA:
5955 case R_NDS32_SDA15S1_RELA:
5956 case R_NDS32_SDA15S0_RELA:
5957 case R_NDS32_SDA_FP7U2_RELA:
5958 case R_NDS32_9_PLTREL:
5959 case R_NDS32_25_PLTREL:
5960 case R_NDS32_GOT20:
5961 case R_NDS32_GOT_HI20:
5962 case R_NDS32_GOT_LO12:
5963 case R_NDS32_GOT_LO15:
5964 case R_NDS32_GOT_LO19:
5965 case R_NDS32_GOT15S2_RELA:
5966 case R_NDS32_GOT17S2_RELA:
5967 case R_NDS32_GOTPC20:
5968 case R_NDS32_GOTPC_HI20:
5969 case R_NDS32_GOTPC_LO12:
5970 case R_NDS32_GOTOFF:
5971 case R_NDS32_GOTOFF_HI20:
5972 case R_NDS32_GOTOFF_LO12:
5973 case R_NDS32_GOTOFF_LO15:
5974 case R_NDS32_GOTOFF_LO19:
5975 case R_NDS32_PLTREL_HI20:
5976 case R_NDS32_PLTREL_LO12:
5977 case R_NDS32_PLT_GOTREL_HI20:
5978 case R_NDS32_PLT_GOTREL_LO12:
5979 case R_NDS32_PLT_GOTREL_LO15:
5980 case R_NDS32_PLT_GOTREL_LO19:
5981 case R_NDS32_PLT_GOTREL_LO20:
5982 case R_NDS32_17IFC_PCREL_RELA:
5983 case R_NDS32_10IFCU_PCREL_RELA:
1c8f6a4d
KLC
5984 case R_NDS32_TLS_LE_HI20:
5985 case R_NDS32_TLS_LE_LO12:
5986 case R_NDS32_TLS_IE_HI20:
5987 case R_NDS32_TLS_IE_LO12S2:
5988 case R_NDS32_TLS_LE_20:
5989 case R_NDS32_TLS_LE_15S0:
5990 case R_NDS32_TLS_LE_15S1:
5991 case R_NDS32_TLS_LE_15S2:
fbaf61ad
NC
5992 case R_NDS32_TLS_DESC_HI20:
5993 case R_NDS32_TLS_DESC_LO12:
5994 case R_NDS32_TLS_IE_LO12:
5995 case R_NDS32_TLS_IEGP_HI20:
5996 case R_NDS32_TLS_IEGP_LO12:
5997 case R_NDS32_TLS_IEGP_LO12S2:
35c08157 5998 /* Instruction related relocs must handle endian properly. */
1c8f6a4d 5999 /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER. */
35c08157
KLC
6000 r = nds32_elf_final_link_relocate (howto, input_bfd,
6001 input_section, contents,
6002 rel->r_offset, relocation,
6003 rel->r_addend);
6004 break;
6005
6006 default:
6007 /* All other relocs can use default handler. */
6008 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6009 contents, rel->r_offset,
6010 relocation, rel->r_addend);
6011 break;
6012 }
6013
dc1e8a47 6014 check_reloc:
35c08157
KLC
6015
6016 if (r != bfd_reloc_ok)
6017 {
6018 /* FIXME: This should be generic enough to go in a utility. */
6019 const char *name;
6020
6021 if (h != NULL)
6022 name = h->root.root.string;
6023 else
6024 {
6025 name = bfd_elf_string_from_elf_section
6026 (input_bfd, symtab_hdr->sh_link, sym->st_name);
6027 if (name == NULL || *name == '\0')
fd361982 6028 name = bfd_section_name (sec);
35c08157
KLC
6029 }
6030
6031 if (errmsg != NULL)
6032 goto common_error;
6033
6034 switch (r)
6035 {
6036 case bfd_reloc_overflow:
1a72702b
AM
6037 (*info->callbacks->reloc_overflow)
6038 (info, (h ? &h->root : NULL), name, howto->name,
6039 (bfd_vma) 0, input_bfd, input_section, offset);
35c08157
KLC
6040 break;
6041
6042 case bfd_reloc_undefined:
1a72702b
AM
6043 (*info->callbacks->undefined_symbol)
6044 (info, name, input_bfd, input_section, offset, TRUE);
35c08157
KLC
6045 break;
6046
6047 case bfd_reloc_outofrange:
6048 errmsg = _("internal error: out of range error");
6049 goto common_error;
6050
6051 case bfd_reloc_notsupported:
6052 errmsg = _("internal error: unsupported relocation error");
6053 goto common_error;
6054
6055 case bfd_reloc_dangerous:
6056 errmsg = _("internal error: dangerous error");
6057 goto common_error;
6058
6059 default:
6060 errmsg = _("internal error: unknown error");
6061 /* Fall through. */
6062
dc1e8a47 6063 common_error:
1a72702b
AM
6064 (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6065 input_section, offset);
35c08157
KLC
6066 break;
6067 }
6068 }
6069 }
6070
fbaf61ad
NC
6071 /* Resotre header size to avoid overflow load. */
6072 if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6073 symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6074
35c08157
KLC
6075 return ret;
6076}
6077
6078/* Finish up dynamic symbol handling. We set the contents of various
6079 dynamic sections here. */
6080
6081static bfd_boolean
6082nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6083 struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6084{
fbaf61ad
NC
6085 struct elf_link_hash_table *ehtab;
6086 struct elf_nds32_link_hash_entry *hent;
35c08157
KLC
6087 bfd_byte *loc;
6088
fbaf61ad
NC
6089 ehtab = elf_hash_table (info);
6090 hent = (struct elf_nds32_link_hash_entry *) h;
35c08157
KLC
6091
6092 if (h->plt.offset != (bfd_vma) - 1)
6093 {
6094 asection *splt;
6095 asection *sgot;
6096 asection *srela;
6097
6098 bfd_vma plt_index;
6099 bfd_vma got_offset;
6100 bfd_vma local_plt_offset;
6101 Elf_Internal_Rela rela;
6102
6103 /* This symbol has an entry in the procedure linkage table. Set
6104 it up. */
6105
6106 BFD_ASSERT (h->dynindx != -1);
6107
fbaf61ad
NC
6108 splt = ehtab->splt;
6109 sgot = ehtab->sgotplt;
6110 srela = ehtab->srelplt;
35c08157
KLC
6111 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6112
6113 /* Get the index in the procedure linkage table which
6114 corresponds to this symbol. This is the index of this symbol
6115 in all the symbols for which we are making plt entries. The
6116 first entry in the procedure linkage table is reserved. */
6117 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6118
6119 /* Get the offset into the .got table of the entry that
6120 corresponds to this function. Each .got entry is 4 bytes.
6121 The first three are reserved. */
6122 got_offset = (plt_index + 3) * 4;
6123
6124 /* Fill in the entry in the procedure linkage table. */
0e1862bb 6125 if (!bfd_link_pic (info))
35c08157
KLC
6126 {
6127 unsigned long insn;
6128
6129 insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6130 + sgot->output_offset + got_offset) >> 12)
6131 & 0xfffff);
6132 bfd_putb32 (insn, splt->contents + h->plt.offset);
6133
6134 insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6135 + sgot->output_offset + got_offset) & 0x0fff)
6136 >> 2);
6137 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6138
6139 insn = PLT_ENTRY_WORD2;
6140 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6141
6142 insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6143 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6144
6145 insn = PLT_ENTRY_WORD4
6146 + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6147 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6148 local_plt_offset = 12;
6149 }
6150 else
6151 {
6152 /* sda_base must be set at this time. */
6153 unsigned long insn;
6154 long offset;
6155
35c08157
KLC
6156 offset = sgot->output_section->vma + sgot->output_offset + got_offset
6157 - elf_gp (output_bfd);
6158 insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6159 bfd_putb32 (insn, splt->contents + h->plt.offset);
6160
6161 insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6162 bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6163
6164 insn = PLT_PIC_ENTRY_WORD2;
6165 bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6166
6167 insn = PLT_PIC_ENTRY_WORD3;
6168 bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6169
6170 insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6171 bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6172
6173 insn = PLT_PIC_ENTRY_WORD5
6174 + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6175 bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6176
6177 local_plt_offset = 16;
6178 }
6179
6180 /* Fill in the entry in the global offset table,
6181 so it will fall through to the next instruction for the first time. */
6182 bfd_put_32 (output_bfd,
6183 (splt->output_section->vma + splt->output_offset
6184 + h->plt.offset + local_plt_offset),
6185 sgot->contents + got_offset);
6186
6187 /* Fill in the entry in the .rela.plt section. */
6188 rela.r_offset = (sgot->output_section->vma
6189 + sgot->output_offset + got_offset);
6190 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6191 rela.r_addend = 0;
6192 loc = srela->contents;
6193 loc += plt_index * sizeof (Elf32_External_Rela);
6194 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6195
6196 if (!h->def_regular)
6197 {
6198 /* Mark the symbol as undefined, rather than as defined in
6199 the .plt section. Leave the value alone. */
6200 sym->st_shndx = SHN_UNDEF;
6201 if (!h->ref_regular_nonweak)
6202 sym->st_value = 0;
6203 }
6204 }
6205
fbaf61ad
NC
6206 if (h->got.offset != (bfd_vma) - 1
6207 && hent->tls_type == GOT_NORMAL)
35c08157
KLC
6208 {
6209 asection *sgot;
fbaf61ad 6210 asection *srelagot;
35c08157
KLC
6211 Elf_Internal_Rela rela;
6212
6213 /* This symbol has an entry in the global offset table.
6214 Set it up. */
6215
fbaf61ad
NC
6216 sgot = ehtab->sgot;
6217 srelagot = ehtab->srelgot;
6218 BFD_ASSERT (sgot != NULL && srelagot != NULL);
35c08157
KLC
6219
6220 rela.r_offset = (sgot->output_section->vma
6221 + sgot->output_offset + (h->got.offset & ~1));
6222
6223 /* If this is a -Bsymbolic link, and the symbol is defined
6224 locally, we just want to emit a RELATIVE reloc. Likewise if
6225 the symbol was forced to be local because of a version file.
6226 The entry in the global offset table will already have been
6227 initialized in the relocate_section function. */
fbaf61ad
NC
6228 if ((bfd_link_pic (info)
6229 && (info->symbolic || h->dynindx == -1 || h->forced_local)
6230 && h->def_regular)
6231 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
6232 {
6233 rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6234 rela.r_addend = (h->root.u.def.value
6235 + h->root.u.def.section->output_section->vma
6236 + h->root.u.def.section->output_offset);
fbaf61ad
NC
6237
6238 if ((h->got.offset & 1) == 0)
6239 {
6240 bfd_put_32 (output_bfd, rela.r_addend,
6241 sgot->contents + h->got.offset);
6242 }
35c08157
KLC
6243 }
6244 else
6245 {
6246 BFD_ASSERT ((h->got.offset & 1) == 0);
6247 bfd_put_32 (output_bfd, (bfd_vma) 0,
6248 sgot->contents + h->got.offset);
6249 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6250 rela.r_addend = 0;
6251 }
6252
fbaf61ad
NC
6253 loc = srelagot->contents;
6254 loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
35c08157 6255 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
fbaf61ad
NC
6256 ++srelagot->reloc_count;
6257 BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
35c08157
KLC
6258 }
6259
6260 if (h->needs_copy)
6261 {
6262 asection *s;
6263 Elf_Internal_Rela rela;
6264
6265 /* This symbols needs a copy reloc. Set it up. */
6266
6267 BFD_ASSERT (h->dynindx != -1
6268 && (h->root.type == bfd_link_hash_defined
6269 || h->root.type == bfd_link_hash_defweak));
6270
6271 s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6272 BFD_ASSERT (s != NULL);
6273
6274 rela.r_offset = (h->root.u.def.value
6275 + h->root.u.def.section->output_section->vma
6276 + h->root.u.def.section->output_offset);
6277 rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6278 rela.r_addend = 0;
6279 loc = s->contents;
6280 loc += s->reloc_count * sizeof (Elf32_External_Rela);
6281 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6282 ++s->reloc_count;
6283 }
6284
6285 /* Mark some specially defined symbols as absolute. */
6286 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6287 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6288 sym->st_shndx = SHN_ABS;
6289
6290 return TRUE;
6291}
6292
6293
6294/* Finish up the dynamic sections. */
6295
6296static bfd_boolean
6297nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6298{
35c08157
KLC
6299 bfd *dynobj;
6300 asection *sdyn;
fbaf61ad
NC
6301 asection *sgotplt;
6302 struct elf_link_hash_table *ehtab;
6303 struct elf_nds32_link_hash_table *htab;
35c08157 6304
fbaf61ad 6305 ehtab = elf_hash_table (info);
35c08157 6306 htab = nds32_elf_hash_table (info);
fbaf61ad
NC
6307 if (htab == NULL)
6308 return FALSE;
6309
6310 dynobj = elf_hash_table (info)->dynobj;
35c08157 6311
fbaf61ad
NC
6312 sgotplt = ehtab->sgotplt;
6313 /* A broken linker script might have discarded the dynamic sections.
6314 Catch this here so that we do not seg-fault later on. */
6315 if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6316 return FALSE;
35c08157
KLC
6317 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6318
fbaf61ad 6319 if (elf_hash_table (info)->dynamic_sections_created)
35c08157
KLC
6320 {
6321 asection *splt;
6322 Elf32_External_Dyn *dyncon, *dynconend;
6323
fbaf61ad 6324 BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
35c08157
KLC
6325
6326 dyncon = (Elf32_External_Dyn *) sdyn->contents;
6327 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6328
6329 for (; dyncon < dynconend; dyncon++)
6330 {
6331 Elf_Internal_Dyn dyn;
6332 asection *s;
6333
6334 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6335
6336 switch (dyn.d_tag)
6337 {
6338 default:
6339 break;
6340
6341 case DT_PLTGOT:
fbaf61ad
NC
6342 /* name = ".got"; */
6343 s = ehtab->sgot->output_section;
35c08157
KLC
6344 goto get_vma;
6345 case DT_JMPREL:
fbaf61ad 6346 s = ehtab->srelplt->output_section;
dc1e8a47 6347 get_vma:
fbaf61ad
NC
6348 BFD_ASSERT (s != NULL);
6349 dyn.d_un.d_ptr = s->vma;
35c08157
KLC
6350 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6351 break;
6352
6353 case DT_PLTRELSZ:
fbaf61ad
NC
6354 s = ehtab->srelplt->output_section;
6355 BFD_ASSERT (s != NULL);
35c08157
KLC
6356 dyn.d_un.d_val = s->size;
6357 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6358 break;
fbaf61ad
NC
6359
6360 case DT_RELASZ:
6361 /* My reading of the SVR4 ABI indicates that the
6362 procedure linkage table relocs (DT_JMPREL) should be
6363 included in the overall relocs (DT_RELA). This is
6364 what Solaris does. However, UnixWare can not handle
6365 that case. Therefore, we override the DT_RELASZ entry
6366 here to make it not include the JMPREL relocs. Since
6367 the linker script arranges for .rela.plt to follow all
6368 other relocation sections, we don't have to worry
6369 about changing the DT_RELA entry. */
6370 if (ehtab->srelplt != NULL)
6371 {
6372 s = ehtab->srelplt->output_section;
6373 dyn.d_un.d_val -= s->size;
6374 }
6375 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6376 break;
6377
6378 case DT_TLSDESC_PLT:
6379 s = htab->root.splt;
6380 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
9bcc30e4 6381 + htab->root.tlsdesc_plt);
fbaf61ad
NC
6382 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6383 break;
6384
6385 case DT_TLSDESC_GOT:
6386 s = htab->root.sgot;
6387 dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
9bcc30e4 6388 + htab->root.tlsdesc_got);
fbaf61ad
NC
6389 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6390 break;
35c08157
KLC
6391 }
6392 }
6393
6394 /* Fill in the first entry in the procedure linkage table. */
fbaf61ad 6395 splt = ehtab->splt;
35c08157
KLC
6396 if (splt && splt->size > 0)
6397 {
0e1862bb 6398 if (bfd_link_pic (info))
35c08157
KLC
6399 {
6400 unsigned long insn;
6401 long offset;
6402
fbaf61ad
NC
6403 offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6404 - elf_gp (output_bfd);
35c08157
KLC
6405 insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6406 bfd_putb32 (insn, splt->contents);
6407
35c08157
KLC
6408 /* here has a typo? */
6409 insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6410 bfd_putb32 (insn, splt->contents + 4);
6411
6412 insn = PLT0_PIC_ENTRY_WORD2;
6413 bfd_putb32 (insn, splt->contents + 8);
6414
6415 insn = PLT0_PIC_ENTRY_WORD3;
6416 bfd_putb32 (insn, splt->contents + 12);
6417
6418 insn = PLT0_PIC_ENTRY_WORD4;
6419 bfd_putb32 (insn, splt->contents + 16);
6420
6421 insn = PLT0_PIC_ENTRY_WORD5;
6422 bfd_putb32 (insn, splt->contents + 20);
6423 }
6424 else
6425 {
6426 unsigned long insn;
6427 unsigned long addr;
6428
6429 /* addr = .got + 4 */
fbaf61ad 6430 addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
35c08157
KLC
6431 insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6432 bfd_putb32 (insn, splt->contents);
6433
6434 insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6435 bfd_putb32 (insn, splt->contents + 4);
6436
6437 insn = PLT0_ENTRY_WORD2;
6438 bfd_putb32 (insn, splt->contents + 8);
6439
6440 insn = PLT0_ENTRY_WORD3;
6441 bfd_putb32 (insn, splt->contents + 12);
6442
6443 insn = PLT0_ENTRY_WORD4;
6444 bfd_putb32 (insn, splt->contents + 16);
6445 }
6446
6447 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6448 PLT_ENTRY_SIZE;
6449 }
fbaf61ad 6450
9bcc30e4 6451 if (htab->root.tlsdesc_plt)
fbaf61ad
NC
6452 {
6453 /* Calculate addresses. */
6454 asection *sgot = sgot = ehtab->sgot;
6455 bfd_vma pltgot = sgotplt->output_section->vma
6456 + sgotplt->output_offset;
6457 bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
9bcc30e4 6458 + htab->root.tlsdesc_got;
fbaf61ad
NC
6459
6460 /* Get GP offset. */
6461 pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1] */
6462 tlsdesc_got -= elf_gp (output_bfd);
6463
6464 /* Do relocation. */
6465 dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6466 dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6467 dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6468 dl_tlsdesc_lazy_trampoline[5] += 0xfff & pltgot;
6469
6470 /* Insert .plt. */
9bcc30e4 6471 nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
fbaf61ad
NC
6472 dl_tlsdesc_lazy_trampoline,
6473 ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6474 }
35c08157
KLC
6475 }
6476
6477 /* Fill in the first three entries in the global offset table. */
fbaf61ad 6478 if (sgotplt && sgotplt->size > 0)
35c08157
KLC
6479 {
6480 if (sdyn == NULL)
fbaf61ad 6481 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
35c08157
KLC
6482 else
6483 bfd_put_32 (output_bfd,
6484 sdyn->output_section->vma + sdyn->output_offset,
fbaf61ad
NC
6485 sgotplt->contents);
6486 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6487 bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
35c08157 6488
fbaf61ad 6489 elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
35c08157
KLC
6490 }
6491
6492 return TRUE;
6493}
6494\f
6495
6496/* Set the right machine number. */
6497
6498static bfd_boolean
6499nds32_elf_object_p (bfd *abfd)
6500{
6501 static unsigned int cur_arch = 0;
6502
6503 if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6504 {
6505 /* E_N1_ARCH is a wild card, so it is set only when no others exist. */
6506 cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6507 }
6508
6509 switch (cur_arch)
6510 {
6511 default:
6512 case E_N1_ARCH:
6513 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6514 break;
6515 case E_N1H_ARCH:
6516 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6517 break;
6518 case E_NDS_ARCH_STAR_V2_0:
6519 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6520 break;
6521 case E_NDS_ARCH_STAR_V3_0:
6522 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6523 break;
6524 case E_NDS_ARCH_STAR_V3_M:
6525 bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6526 break;
6527 }
6528
6529 return TRUE;
6530}
6531
6532/* Store the machine number in the flags field. */
6533
cc364be6
AM
6534static bfd_boolean
6535nds32_elf_final_write_processing (bfd *abfd)
35c08157
KLC
6536{
6537 unsigned long val;
6538 static unsigned int cur_mach = 0;
6539
6540 if (bfd_mach_n1 != bfd_get_mach (abfd))
6541 {
6542 cur_mach = bfd_get_mach (abfd);
6543 }
6544
6545 switch (cur_mach)
6546 {
6547 case bfd_mach_n1:
6548 /* Only happen when object is empty, since the case is abandon. */
6549 val = E_N1_ARCH;
6550 val |= E_NDS_ABI_AABI;
6551 val |= E_NDS32_ELF_VER_1_4;
6552 break;
6553 case bfd_mach_n1h:
6554 val = E_N1H_ARCH;
6555 break;
6556 case bfd_mach_n1h_v2:
6557 val = E_NDS_ARCH_STAR_V2_0;
6558 break;
6559 case bfd_mach_n1h_v3:
6560 val = E_NDS_ARCH_STAR_V3_0;
6561 break;
6562 case bfd_mach_n1h_v3m:
6563 val = E_NDS_ARCH_STAR_V3_M;
6564 break;
6565 default:
6566 val = 0;
6567 break;
6568 }
6569
6570 elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6571 elf_elfheader (abfd)->e_flags |= val;
cc364be6 6572 return _bfd_elf_final_write_processing (abfd);
35c08157
KLC
6573}
6574
6575/* Function to keep NDS32 specific file flags. */
6576
6577static bfd_boolean
6578nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6579{
6580 BFD_ASSERT (!elf_flags_init (abfd)
6581 || elf_elfheader (abfd)->e_flags == flags);
6582
6583 elf_elfheader (abfd)->e_flags = flags;
6584 elf_flags_init (abfd) = TRUE;
6585 return TRUE;
6586}
6587
6588static unsigned int
6589convert_e_flags (unsigned int e_flags, unsigned int arch)
6590{
6591 if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6592 {
6593 /* From 0.9 to 1.0. */
6594 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6595
6596 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6597 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6598 if (arch == E_NDS_ARCH_STAR_V1_0)
6599 {
6600 /* Done. */
6601 return e_flags;
6602 }
6603 }
6604
6605 /* From 1.0 to 2.0. */
6606 e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6607
6608 /* Clear E_NDS32_HAS_MFUSR_PC_INST. */
6609 e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6610
6611 /* Invert E_NDS32_HAS_NO_MAC_INST. */
6612 e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6613 return e_flags;
6614}
6615
6616static bfd_boolean
6617nds32_check_vec_size (bfd *ibfd)
6618{
6619 static unsigned int nds32_vec_size = 0;
6620
6621 asection *sec_t = NULL;
6622 bfd_byte *contents = NULL;
6623
6624 sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6625
6626 if (sec_t && sec_t->size >= 4)
6627 {
6628 /* Get vec_size in file. */
6629 unsigned int flag_t;
6630
0c4bd9d9 6631 nds32_get_section_contents (ibfd, sec_t, &contents, TRUE);
35c08157
KLC
6632 flag_t = bfd_get_32 (ibfd, contents);
6633
6634 /* The value could only be 4 or 16. */
6635
6636 if (!nds32_vec_size)
6637 /* Set if not set yet. */
6638 nds32_vec_size = (flag_t & 0x3);
6639 else if (nds32_vec_size != (flag_t & 0x3))
6640 {
4eca0228 6641 _bfd_error_handler
695344c0 6642 /* xgettext:c-format */
871b3ab2 6643 (_("%pB: ISR vector size mismatch"
4eca0228
AM
6644 " with previous modules, previous %u-byte, current %u-byte"),
6645 ibfd,
6646 nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6647 (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
35c08157
KLC
6648 return FALSE;
6649 }
6650 else
6651 /* Only keep the first vec_size section. */
6652 sec_t->flags |= SEC_EXCLUDE;
6653 }
6654
6655 return TRUE;
6656}
6657
6658/* Merge backend specific data from an object file to the output
6659 object file when linking. */
6660
6661static bfd_boolean
50e03d47 6662nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
35c08157 6663{
50e03d47 6664 bfd *obfd = info->output_bfd;
35c08157
KLC
6665 flagword out_flags;
6666 flagword in_flags;
6667 flagword out_16regs;
6668 flagword in_no_mac;
6669 flagword out_no_mac;
6670 flagword in_16regs;
6671 flagword out_version;
6672 flagword in_version;
6673 flagword out_fpu_config;
6674 flagword in_fpu_config;
6675
6b728d32
AM
6676 /* FIXME: What should be checked when linking shared libraries? */
6677 if ((ibfd->flags & DYNAMIC) != 0)
6678 return TRUE;
6679
35c08157
KLC
6680 /* TODO: Revise to use object-attributes instead. */
6681 if (!nds32_check_vec_size (ibfd))
6682 return FALSE;
6683
6684 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6685 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6686 return TRUE;
6687
6688 if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6689 {
4eca0228 6690 _bfd_error_handler
38f14ab8 6691 (_("%pB: warning: endian mismatch with previous modules"), ibfd);
35c08157
KLC
6692
6693 bfd_set_error (bfd_error_bad_value);
6694 return FALSE;
6695 }
6696
fbaf61ad
NC
6697 /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6698 treat as generic one without checking and merging. */
6699 if (elf_elfheader (ibfd)->e_flags)
35c08157 6700 {
fbaf61ad
NC
6701 in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6702 if (in_version == E_NDS32_ELF_VER_1_2)
35c08157 6703 {
fbaf61ad
NC
6704 _bfd_error_handler
6705 (_("%pB: warning: older version of object file encountered, "
6706 "please recompile with current tool chain"), ibfd);
35c08157 6707 }
35c08157 6708
fbaf61ad
NC
6709 /* We may need to merge V1 and V2 arch object files to V2. */
6710 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6711 != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
35c08157 6712 {
fbaf61ad
NC
6713 /* Need to convert version. */
6714 if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6715 == E_NDS_ARCH_STAR_RESERVED)
6716 {
6717 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6718 }
6719 else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6720 == E_NDS_ARCH_STAR_V3_M
6721 && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6722 == E_NDS_ARCH_STAR_V3_0)
6723 {
6724 elf_elfheader (ibfd)->e_flags =
6725 (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6726 | E_NDS_ARCH_STAR_V3_0;
6727 }
6728 else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6729 == E_NDS_ARCH_STAR_V0_9
6730 || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6731 > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6732 {
6733 elf_elfheader (obfd)->e_flags =
6734 convert_e_flags (elf_elfheader (obfd)->e_flags,
6735 (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6736 }
6737 else
6738 {
6739 elf_elfheader (ibfd)->e_flags =
6740 convert_e_flags (elf_elfheader (ibfd)->e_flags,
6741 (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6742 }
35c08157
KLC
6743 }
6744
fbaf61ad
NC
6745 /* Extract some flags. */
6746 in_flags = elf_elfheader (ibfd)->e_flags
6747 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6748 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6749
6750 /* The following flags need special treatment. */
6751 in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6752 in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6753 in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6754
6755 /* Extract some flags. */
6756 out_flags = elf_elfheader (obfd)->e_flags
6757 & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6758 | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6759
6760 /* The following flags need special treatment. */
6761 out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6762 out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6763 out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6764 out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6765 if (!elf_flags_init (obfd))
6766 {
6767 /* If the input is the default architecture then do not
6768 bother setting the flags for the output architecture,
6769 instead allow future merges to do this. If no future
6770 merges ever set these flags then they will retain their
6771 unitialised values, which surprise surprise, correspond
6772 to the default values. */
6773 if (bfd_get_arch_info (ibfd)->the_default)
6774 return TRUE;
6775
6776 elf_flags_init (obfd) = TRUE;
6777 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
35c08157 6778
fbaf61ad
NC
6779 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6780 && bfd_get_arch_info (obfd)->the_default)
6781 {
6782 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6783 bfd_get_mach (ibfd));
6784 }
35c08157 6785
fbaf61ad
NC
6786 return TRUE;
6787 }
35c08157 6788
fbaf61ad
NC
6789 /* Check flag compatibility. */
6790 if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
35c08157 6791 {
4eca0228 6792 _bfd_error_handler
fbaf61ad 6793 (_("%pB: error: ABI mismatch with previous modules"), ibfd);
35c08157
KLC
6794 bfd_set_error (bfd_error_bad_value);
6795 return FALSE;
6796 }
35c08157 6797
fbaf61ad
NC
6798 if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6799 {
6800 if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6801 {
6802 _bfd_error_handler
6803 (_("%pB: error: instruction set mismatch with previous modules"),
6804 ibfd);
6805
6806 bfd_set_error (bfd_error_bad_value);
6807 return FALSE;
6808 }
6809 }
6810
6811 /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6812 and perf ext1 and DIV are mergerd to perf ext1. */
6813 if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6814 {
6815 elf_elfheader (obfd)->e_flags =
6816 (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6817 | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6818 | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6819 ? E_NDS32_HAS_EXT_INST : 0)
6820 | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6821 ? E_NDS32_HAS_EXT_INST : 0)
6822 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6823 | ((in_version > out_version) ? out_version : in_version);
6824 }
6825 else
6826 {
6827 if (in_version != out_version)
6828 _bfd_error_handler
6829 /* xgettext:c-format */
6830 (_("%pB: warning: incompatible elf-versions %s and %s"),
6831 ibfd, nds32_elfver_strtab[out_version],
6832 nds32_elfver_strtab[in_version]);
35c08157 6833
fbaf61ad
NC
6834 elf_elfheader (obfd)->e_flags = in_flags | out_flags
6835 | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6836 | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6837 | (in_version > out_version ? out_version : in_version);
6838 }
35c08157
KLC
6839 }
6840
6841 return TRUE;
6842}
6843
6844/* Display the flags field. */
6845
6846static bfd_boolean
6847nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6848{
6849 FILE *file = (FILE *) ptr;
6850
6851 BFD_ASSERT (abfd != NULL && ptr != NULL);
6852
6853 _bfd_elf_print_private_bfd_data (abfd, ptr);
6854
6855 fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6856
6857 switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6858 {
6859 default:
6860 case E_N1_ARCH:
6861 fprintf (file, _(": n1 instructions"));
6862 break;
6863 case E_N1H_ARCH:
6864 fprintf (file, _(": n1h instructions"));
6865 break;
6866 }
6867
6868 fputc ('\n', file);
6869
6870 return TRUE;
6871}
6872
6873static unsigned int
6874nds32_elf_action_discarded (asection *sec)
6875{
6876
6877 if (strncmp
6878 (".gcc_except_table", sec->name, sizeof (".gcc_except_table") - 1) == 0)
6879 return 0;
6880
6881 return _bfd_elf_default_action_discarded (sec);
6882}
6883
6884static asection *
6885nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6886 Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6887 Elf_Internal_Sym *sym)
6888{
6889 if (h != NULL)
6890 switch (ELF32_R_TYPE (rel->r_info))
6891 {
6892 case R_NDS32_GNU_VTINHERIT:
6893 case R_NDS32_GNU_VTENTRY:
6894 case R_NDS32_RELA_GNU_VTINHERIT:
6895 case R_NDS32_RELA_GNU_VTENTRY:
6896 return NULL;
6897 }
6898
6899 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6900}
6901
fbaf61ad
NC
6902static enum elf_nds32_tls_type
6903get_tls_type (enum elf_nds32_reloc_type r_type,
6904 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6905{
6906 enum elf_nds32_tls_type tls_type;
6907
6908 switch (r_type)
6909 {
6910 case R_NDS32_TLS_LE_HI20:
6911 case R_NDS32_TLS_LE_LO12:
6912 tls_type = GOT_TLS_LE;
6913 break;
6914 case R_NDS32_TLS_IE_HI20:
6915 case R_NDS32_TLS_IE_LO12S2:
6916 case R_NDS32_TLS_IE_LO12:
6917 tls_type = GOT_TLS_IE;
6918 break;
6919 case R_NDS32_TLS_IEGP_HI20:
6920 case R_NDS32_TLS_IEGP_LO12:
6921 case R_NDS32_TLS_IEGP_LO12S2:
6922 tls_type = GOT_TLS_IEGP;
6923 break;
6924 case R_NDS32_TLS_DESC_HI20:
6925 case R_NDS32_TLS_DESC_LO12:
6926 case R_NDS32_TLS_DESC_ADD:
6927 case R_NDS32_TLS_DESC_FUNC:
6928 case R_NDS32_TLS_DESC_CALL:
6929 tls_type = GOT_TLS_DESC;
6930 break;
6931 default:
6932 tls_type = GOT_NORMAL;
6933 break;
6934 }
6935
6936 return tls_type;
6937}
6938
6939/* Ensure that we have allocated bookkeeping structures for ABFD's local
6940 symbols. */
6941
6942static bfd_boolean
6943elf32_nds32_allocate_local_sym_info (bfd *abfd)
6944{
6945 if (elf_local_got_refcounts (abfd) == NULL)
6946 {
6947 bfd_size_type num_syms;
6948 bfd_size_type size;
6949 char *data;
6950
6951 num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
6952 /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
6953 gp_offset. The details can refer to struct elf_nds32_obj_tdata. */
6954 size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
6955 + sizeof (bfd_vma) + sizeof (int)
6956 + sizeof (bfd_boolean) + sizeof (bfd_vma));
6957 data = bfd_zalloc (abfd, size);
6958 if (data == NULL)
6959 return FALSE;
6960
6961 elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
6962 data += num_syms * sizeof (bfd_signed_vma);
6963
6964 elf32_nds32_local_got_tls_type (abfd) = (char *) data;
6965 data += num_syms * sizeof (char);
6966
6967 elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
6968 data += num_syms * sizeof (bfd_vma);
6969
6970 elf32_nds32_local_gp_offset (abfd) = (int *) data;
6971 data += num_syms * sizeof (int);
6972 }
6973
6974 return TRUE;
6975}
6976
35c08157
KLC
6977/* Look through the relocs for a section during the first phase.
6978 Since we don't do .gots or .plts, we just need to consider the
6979 virtual table relocs for gc. */
6980
6981static bfd_boolean
6982nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
6983 asection *sec, const Elf_Internal_Rela *relocs)
6984{
6985 Elf_Internal_Shdr *symtab_hdr;
6986 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
6987 const Elf_Internal_Rela *rel;
6988 const Elf_Internal_Rela *rel_end;
fbaf61ad 6989 struct elf_link_hash_table *ehtab;
35c08157
KLC
6990 struct elf_nds32_link_hash_table *htab;
6991 bfd *dynobj;
6992 asection *sreloc = NULL;
6993
fbaf61ad 6994 /* No need for relocation if relocatable already. */
0e1862bb 6995 if (bfd_link_relocatable (info))
fbaf61ad
NC
6996 {
6997 elf32_nds32_check_relax_group (abfd, sec);
6998 return TRUE;
6999 }
35c08157
KLC
7000
7001 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7002 sym_hashes = elf_sym_hashes (abfd);
7003 sym_hashes_end =
7004 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
7005 if (!elf_bad_symtab (abfd))
7006 sym_hashes_end -= symtab_hdr->sh_info;
7007
fbaf61ad 7008 ehtab = elf_hash_table (info);
35c08157
KLC
7009 htab = nds32_elf_hash_table (info);
7010 dynobj = htab->root.dynobj;
7011
7012 rel_end = relocs + sec->reloc_count;
7013 for (rel = relocs; rel < rel_end; rel++)
7014 {
7015 enum elf_nds32_reloc_type r_type;
7016 struct elf_link_hash_entry *h;
7017 unsigned long r_symndx;
fbaf61ad 7018 enum elf_nds32_tls_type tls_type, old_tls_type;
35c08157
KLC
7019
7020 r_symndx = ELF32_R_SYM (rel->r_info);
7021 r_type = ELF32_R_TYPE (rel->r_info);
7022 if (r_symndx < symtab_hdr->sh_info)
7023 h = NULL;
7024 else
7025 {
7026 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7027 while (h->root.type == bfd_link_hash_indirect
7028 || h->root.type == bfd_link_hash_warning)
7029 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7030 }
7031
fbaf61ad
NC
7032 /* Create .got section if necessary.
7033 Some relocs require a global offset table. We create
7034 got section here, since these relocation need a got section
7035 and if it is not created yet. */
7036 if (ehtab->sgot == NULL)
35c08157
KLC
7037 {
7038 switch (r_type)
7039 {
7040 case R_NDS32_GOT_HI20:
7041 case R_NDS32_GOT_LO12:
7042 case R_NDS32_GOT_LO15:
7043 case R_NDS32_GOT_LO19:
7044 case R_NDS32_GOT17S2_RELA:
7045 case R_NDS32_GOT15S2_RELA:
7046 case R_NDS32_GOTOFF:
7047 case R_NDS32_GOTOFF_HI20:
7048 case R_NDS32_GOTOFF_LO12:
7049 case R_NDS32_GOTOFF_LO15:
7050 case R_NDS32_GOTOFF_LO19:
7051 case R_NDS32_GOTPC20:
7052 case R_NDS32_GOTPC_HI20:
7053 case R_NDS32_GOTPC_LO12:
7054 case R_NDS32_GOT20:
1c8f6a4d 7055 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7056 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7057 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7058 case R_NDS32_TLS_IEGP_HI20:
7059 case R_NDS32_TLS_IEGP_LO12:
7060 case R_NDS32_TLS_IEGP_LO12S2:
7061 case R_NDS32_TLS_DESC_HI20:
7062 case R_NDS32_TLS_DESC_LO12:
35c08157
KLC
7063 if (dynobj == NULL)
7064 htab->root.dynobj = dynobj = abfd;
fbaf61ad 7065 if (!create_got_section (dynobj, info))
35c08157
KLC
7066 return FALSE;
7067 break;
7068
7069 default:
7070 break;
7071 }
7072 }
7073
fbaf61ad 7074 /* Check relocation type. */
35c08157
KLC
7075 switch ((int) r_type)
7076 {
7077 case R_NDS32_GOT_HI20:
7078 case R_NDS32_GOT_LO12:
7079 case R_NDS32_GOT_LO15:
7080 case R_NDS32_GOT_LO19:
7081 case R_NDS32_GOT20:
fbaf61ad
NC
7082 case R_NDS32_TLS_LE_HI20:
7083 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 7084 case R_NDS32_TLS_IE_HI20:
fbaf61ad 7085 case R_NDS32_TLS_IE_LO12:
1c8f6a4d 7086 case R_NDS32_TLS_IE_LO12S2:
fbaf61ad
NC
7087 case R_NDS32_TLS_IEGP_HI20:
7088 case R_NDS32_TLS_IEGP_LO12:
7089 case R_NDS32_TLS_IEGP_LO12S2:
7090 case R_NDS32_TLS_DESC_HI20:
7091 case R_NDS32_TLS_DESC_LO12:
7092 tls_type = get_tls_type (r_type, h);
7093 if (h)
1c8f6a4d 7094 {
fbaf61ad
NC
7095 if (tls_type != GOT_TLS_LE)
7096 h->got.refcount += 1;
1c8f6a4d 7097 old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
1c8f6a4d 7098 }
35c08157
KLC
7099 else
7100 {
fbaf61ad
NC
7101 /* This is a global offset table entry for a local symbol. */
7102 if (!elf32_nds32_allocate_local_sym_info (abfd))
7103 return FALSE;
35c08157 7104
fbaf61ad
NC
7105 BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7106 if (tls_type != GOT_TLS_LE)
7107 elf_local_got_refcounts (abfd)[r_symndx] += 1;
1c8f6a4d 7108 old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
35c08157 7109 }
35c08157 7110
fbaf61ad 7111 /* We would already have issued an error message if there
1c8f6a4d
KLC
7112 is a TLS/non-TLS mismatch, based on the symbol
7113 type. So just combine any TLS types needed. */
7114 if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7115 && tls_type != GOT_NORMAL)
7116 tls_type |= old_tls_type;
7117
fbaf61ad
NC
7118 /* DESC to IE/IEGP if link to executable. */
7119 if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7120 && (bfd_link_executable (info)))
7121 tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7122
1c8f6a4d
KLC
7123 if (old_tls_type != tls_type)
7124 {
7125 if (h != NULL)
7126 elf32_nds32_hash_entry (h)->tls_type = tls_type;
7127 else
7128 elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7129 }
7130 break;
7131 case R_NDS32_9_PLTREL:
35c08157
KLC
7132 case R_NDS32_25_PLTREL:
7133 case R_NDS32_PLTREL_HI20:
7134 case R_NDS32_PLTREL_LO12:
7135 case R_NDS32_PLT_GOTREL_HI20:
7136 case R_NDS32_PLT_GOTREL_LO12:
7137 case R_NDS32_PLT_GOTREL_LO15:
7138 case R_NDS32_PLT_GOTREL_LO19:
7139 case R_NDS32_PLT_GOTREL_LO20:
7140
7141 /* This symbol requires a procedure linkage table entry. We
7142 actually build the entry in adjust_dynamic_symbol,
7143 because this might be a case of linking PIC code without
7144 linking in any dynamic objects, in which case we don't
7145 need to generate a procedure linkage table after all. */
7146
7147 /* If this is a local symbol, we resolve it directly without
7148 creating a procedure linkage table entry. */
7149 if (h == NULL)
7150 continue;
7151
fbaf61ad
NC
7152 if (h->forced_local
7153 || (bfd_link_pie (info) && h->def_regular))
35c08157
KLC
7154 break;
7155
1c8f6a4d 7156 elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
35c08157
KLC
7157 h->needs_plt = 1;
7158 h->plt.refcount += 1;
7159 break;
7160
7161 case R_NDS32_16_RELA:
7162 case R_NDS32_20_RELA:
7163 case R_NDS32_5_RELA:
7164 case R_NDS32_32_RELA:
7165 case R_NDS32_HI20_RELA:
7166 case R_NDS32_LO12S3_RELA:
7167 case R_NDS32_LO12S2_RELA:
7168 case R_NDS32_LO12S2_DP_RELA:
7169 case R_NDS32_LO12S2_SP_RELA:
7170 case R_NDS32_LO12S1_RELA:
7171 case R_NDS32_LO12S0_RELA:
7172 case R_NDS32_LO12S0_ORI_RELA:
7173 case R_NDS32_SDA16S3_RELA:
7174 case R_NDS32_SDA17S2_RELA:
7175 case R_NDS32_SDA18S1_RELA:
7176 case R_NDS32_SDA19S0_RELA:
7177 case R_NDS32_SDA15S3_RELA:
7178 case R_NDS32_SDA15S2_RELA:
7179 case R_NDS32_SDA12S2_DP_RELA:
7180 case R_NDS32_SDA12S2_SP_RELA:
7181 case R_NDS32_SDA15S1_RELA:
7182 case R_NDS32_SDA15S0_RELA:
7183 case R_NDS32_SDA_FP7U2_RELA:
7184 case R_NDS32_15_PCREL_RELA:
7185 case R_NDS32_17_PCREL_RELA:
7186 case R_NDS32_25_PCREL_RELA:
7187
0e1862bb 7188 if (h != NULL && !bfd_link_pic (info))
35c08157
KLC
7189 {
7190 h->non_got_ref = 1;
7191 h->plt.refcount += 1;
7192 }
7193
7194 /* If we are creating a shared library, and this is a reloc against
7195 a global symbol, or a non PC relative reloc against a local
7196 symbol, then we need to copy the reloc into the shared library.
7197 However, if we are linking with -Bsymbolic, we do not need to
7198 copy a reloc against a global symbol which is defined in an
7199 object we are including in the link (i.e., DEF_REGULAR is set).
7200 At this point we have not seen all the input files, so it is
7201 possible that DEF_REGULAR is not set now but will be set later
7202 (it is never cleared). We account for that possibility below by
7203 storing information in the dyn_relocs field of the hash table
7204 entry. A similar situation occurs when creating shared libraries
7205 and symbol visibility changes render the symbol local.
7206
7207 If on the other hand, we are creating an executable, we may need
7208 to keep relocations for symbols satisfied by a dynamic library
7209 if we manage to avoid copy relocs for the symbol. */
0e1862bb 7210 if ((bfd_link_pic (info)
35c08157
KLC
7211 && (sec->flags & SEC_ALLOC) != 0
7212 && ((r_type != R_NDS32_25_PCREL_RELA
7213 && r_type != R_NDS32_15_PCREL_RELA
7214 && r_type != R_NDS32_17_PCREL_RELA
7215 && !(r_type == R_NDS32_32_RELA
7216 && strcmp (sec->name, ".eh_frame") == 0))
7217 || (h != NULL
7218 && (!info->symbolic
7219 || h->root.type == bfd_link_hash_defweak
7220 || !h->def_regular))))
0e1862bb 7221 || (!bfd_link_pic (info)
35c08157
KLC
7222 && (sec->flags & SEC_ALLOC) != 0
7223 && h != NULL
7224 && (h->root.type == bfd_link_hash_defweak
7225 || !h->def_regular)))
7226 {
3bf083ed
AM
7227 struct elf_dyn_relocs *p;
7228 struct elf_dyn_relocs **head;
35c08157
KLC
7229
7230 if (dynobj == NULL)
7231 htab->root.dynobj = dynobj = abfd;
7232
7233 /* When creating a shared object, we must copy these
7234 relocs into the output file. We create a reloc
7235 section in dynobj and make room for the reloc. */
7236 if (sreloc == NULL)
7237 {
7238 const char *name;
7239
7240 name = bfd_elf_string_from_elf_section
7241 (abfd, elf_elfheader (abfd)->e_shstrndx,
7242 elf_section_data (sec)->rela.hdr->sh_name);
7243 if (name == NULL)
7244 return FALSE;
7245
7246 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
fd361982 7247 && strcmp (bfd_section_name (sec),
35c08157
KLC
7248 name + 5) == 0);
7249
7250 sreloc = bfd_get_section_by_name (dynobj, name);
7251 if (sreloc == NULL)
7252 {
7253 flagword flags;
7254
7255 sreloc = bfd_make_section (dynobj, name);
7256 flags = (SEC_HAS_CONTENTS | SEC_READONLY
7257 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7258 if ((sec->flags & SEC_ALLOC) != 0)
7259 flags |= SEC_ALLOC | SEC_LOAD;
7260 if (sreloc == NULL
fd361982
AM
7261 || !bfd_set_section_flags (sreloc, flags)
7262 || !bfd_set_section_alignment (sreloc, 2))
35c08157
KLC
7263 return FALSE;
7264
7265 elf_section_type (sreloc) = SHT_RELA;
7266 }
7267 elf_section_data (sec)->sreloc = sreloc;
7268 }
7269
7270 /* If this is a global symbol, we count the number of
7271 relocations we need for this symbol. */
7272 if (h != NULL)
190eb1dd 7273 head = &h->dyn_relocs;
35c08157
KLC
7274 else
7275 {
7276 asection *s;
61034b0b 7277 void *vpp;
35c08157
KLC
7278
7279 Elf_Internal_Sym *isym;
7280 isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
7281 if (isym == NULL)
7282 return FALSE;
7283
7284 /* Track dynamic relocs needed for local syms too. */
7285 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7286 if (s == NULL)
7287 return FALSE;
7288
61034b0b 7289 vpp = &elf_section_data (s)->local_dynrel;
3bf083ed 7290 head = (struct elf_dyn_relocs **) vpp;
35c08157
KLC
7291 }
7292
7293 p = *head;
7294 if (p == NULL || p->sec != sec)
7295 {
986f0783 7296 size_t amt = sizeof (*p);
3bf083ed 7297 p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
35c08157
KLC
7298 if (p == NULL)
7299 return FALSE;
7300 p->next = *head;
7301 *head = p;
7302 p->sec = sec;
7303 p->count = 0;
7304 p->pc_count = 0;
7305 }
7306
7307 p->count += 1;
fbaf61ad
NC
7308
7309 /* Since eh_frame is readonly, R_NDS32_32_RELA
7310 reloc for eh_frame will cause shared library has
7311 TEXTREL entry in the dynamic section. This lead glibc
7312 testsuites to failure (bug-13092) and cause kernel fail
7313 (bug-11819). I think the best solution is to replace
7314 absolute reloc with pc relative reloc in the eh_frame.
7315 To do that, we need to support the following issues:
7316
7317 === For GCC ===
7318 * gcc/config/nds32/nds32.h: Define
7319 ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7320 and DW_EH_PE_sdata4 into DWARF exception header when
7321 option have '-fpic'.
7322
7323 === For binutils ===
7324 * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7325 * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7326 may break our nds DIFF mechanism, therefore, we
7327 must disable all linker relaxations to ensure
7328 correctness.
7329 * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7330 R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7331 do the necessary modification.
7332
7333 Unfortunately, it still have some problems for nds32
7334 to support pc relative reloc in the eh_frame. So I use
7335 another solution to fix this issue.
7336
7337 However, I find that ld always emit TEXTREL marker for
7338 R_NDS32_NONE relocs in rel.dyn. These none relocs are
7339 correspond to R_NDS32_32_RELA for .eh_frame section.
7340 It means that we always reserve redundant entries of rel.dyn
7341 for these relocs which actually do nothing in dynamic linker.
7342
7343 Therefore, we regard these relocs as pc relative relocs
7344 here and increase the pc_count. */
35c08157
KLC
7345 if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7346 || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
fbaf61ad
NC
7347 || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7348 || (r_type == R_NDS32_32_RELA
7349 && strcmp (sec->name, ".eh_frame") == 0))
35c08157
KLC
7350 p->pc_count += 1;
7351 }
7352 break;
7353
7354 /* This relocation describes the C++ object vtable hierarchy.
7355 Reconstruct it for later use during GC. */
7356 case R_NDS32_RELA_GNU_VTINHERIT:
7357 case R_NDS32_GNU_VTINHERIT:
7358 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7359 return FALSE;
7360 break;
7361
7362 /* This relocation describes which C++ vtable entries are actually
7363 used. Record for later use during GC. */
7364 case R_NDS32_GNU_VTENTRY:
7365 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7366 return FALSE;
7367 break;
7368 case R_NDS32_RELA_GNU_VTENTRY:
7369 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7370 return FALSE;
7371 break;
7372 }
7373 }
7374
7375 return TRUE;
7376}
7377
7378/* Write VAL in uleb128 format to P, returning a pointer to the
7379 following byte.
7380 This code is copied from elf-attr.c. */
7381
7382static bfd_byte *
7383write_uleb128 (bfd_byte *p, unsigned int val)
7384{
7385 bfd_byte c;
7386 do
7387 {
7388 c = val & 0x7f;
7389 val >>= 7;
7390 if (val)
7391 c |= 0x80;
7392 *(p++) = c;
7393 }
7394 while (val);
7395 return p;
7396}
7397
7398static bfd_signed_vma
7399calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
fbaf61ad 7400 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
35c08157
KLC
7401{
7402 bfd_signed_vma foff;
7403 bfd_vma symval, addend;
7404 asection *sym_sec;
7405
7406 /* Get the value of the symbol referred to by the reloc. */
7407 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7408 {
7409 Elf_Internal_Sym *isym;
7410
7411 /* A local symbol. */
7412 isym = isymbuf + ELF32_R_SYM (irel->r_info);
7413
7414 if (isym->st_shndx == SHN_UNDEF)
7415 sym_sec = bfd_und_section_ptr;
7416 else if (isym->st_shndx == SHN_ABS)
7417 sym_sec = bfd_abs_section_ptr;
7418 else if (isym->st_shndx == SHN_COMMON)
7419 sym_sec = bfd_com_section_ptr;
7420 else
7421 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7422 symval = isym->st_value + sym_sec->output_section->vma
7423 + sym_sec->output_offset;
7424 }
7425 else
7426 {
7427 unsigned long indx;
7428 struct elf_link_hash_entry *h;
35c08157
KLC
7429
7430 /* An external symbol. */
7431 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7432 h = elf_sym_hashes (abfd)[indx];
7433 BFD_ASSERT (h != NULL);
7434
7435 if (h->root.type != bfd_link_hash_defined
7436 && h->root.type != bfd_link_hash_defweak)
7437 /* This appears to be a reference to an undefined
7438 symbol. Just ignore it--it will be caught by the
7439 regular reloc processing. */
7440 return 0;
35c08157
KLC
7441
7442 if (h->root.u.def.section->flags & SEC_MERGE)
7443 {
7444 sym_sec = h->root.u.def.section;
7445 symval = _bfd_merged_section_offset (abfd, &sym_sec,
7446 elf_section_data (sym_sec)->sec_info,
7447 h->root.u.def.value);
7448 symval = symval + sym_sec->output_section->vma
7449 + sym_sec->output_offset;
7450 }
7451 else
7452 symval = (h->root.u.def.value
7453 + h->root.u.def.section->output_section->vma
7454 + h->root.u.def.section->output_offset);
7455 }
7456
7457 addend = irel->r_addend;
7458
7459 foff = (symval + addend
7460 - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7461 return foff;
7462}
35c08157 7463\f
fbaf61ad 7464
35c08157
KLC
7465/* Convert a 32-bit instruction to 16-bit one.
7466 INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7467 instruction. If INSN_TYPE is not NULL, it the CGEN instruction
7468 type of INSN16. Return 1 if successful. */
7469
7470static int
7471nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7472 int *pinsn_type)
7473{
7474 uint16_t insn16 = 0;
6cae483a 7475 int insn_type = 0;
35c08157
KLC
7476 unsigned long mach = bfd_get_mach (abfd);
7477
7478 if (N32_SH5 (insn) != 0)
7479 return 0;
7480
7481 switch (N32_SUB5 (insn))
7482 {
7483 case N32_ALU1_ADD_SLLI:
7484 case N32_ALU1_ADD_SRLI:
7485 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7486 {
7487 insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7488 N32_RB5 (insn));
7489 insn_type = NDS32_INSN_ADD333;
7490 }
7491 else if (N32_IS_RT4 (insn))
7492 {
7493 if (N32_RT5 (insn) == N32_RA5 (insn))
7494 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7495 else if (N32_RT5 (insn) == N32_RB5 (insn))
7496 insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7497 insn_type = NDS32_INSN_ADD45;
7498 }
7499 break;
7500
7501 case N32_ALU1_SUB_SLLI:
7502 case N32_ALU1_SUB_SRLI:
7503 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7504 {
7505 insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7506 N32_RB5 (insn));
7507 insn_type = NDS32_INSN_SUB333;
7508 }
7509 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7510 {
7511 insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7512 insn_type = NDS32_INSN_SUB45;
7513 }
7514 break;
7515
7516 case N32_ALU1_AND_SLLI:
7517 case N32_ALU1_AND_SRLI:
7518 /* and $rt, $rt, $rb -> and33 for v3, v3m. */
7519 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7520 && N32_IS_RB3 (insn))
7521 {
7522 if (N32_RT5 (insn) == N32_RA5 (insn))
7523 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7524 else if (N32_RT5 (insn) == N32_RB5 (insn))
7525 insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7526 if (insn16)
7527 insn_type = NDS32_INSN_AND33;
7528 }
7529 break;
7530
7531 case N32_ALU1_XOR_SLLI:
7532 case N32_ALU1_XOR_SRLI:
7533 /* xor $rt, $rt, $rb -> xor33 for v3, v3m. */
7534 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7535 && N32_IS_RB3 (insn))
7536 {
7537 if (N32_RT5 (insn) == N32_RA5 (insn))
7538 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7539 else if (N32_RT5 (insn) == N32_RB5 (insn))
7540 insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7541 if (insn16)
7542 insn_type = NDS32_INSN_XOR33;
7543 }
7544 break;
7545
7546 case N32_ALU1_OR_SLLI:
7547 case N32_ALU1_OR_SRLI:
7548 /* or $rt, $rt, $rb -> or33 for v3, v3m. */
7549 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7550 && N32_IS_RB3 (insn))
7551 {
7552 if (N32_RT5 (insn) == N32_RA5 (insn))
7553 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7554 else if (N32_RT5 (insn) == N32_RB5 (insn))
7555 insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7556 if (insn16)
7557 insn_type = NDS32_INSN_OR33;
7558 }
7559 break;
7560 case N32_ALU1_NOR:
7561 /* nor $rt, $ra, $ra -> not33 for v3, v3m. */
7562 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7563 && N32_RA5 (insn) == N32_RB5 (insn))
7564 {
7565 insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7566 insn_type = NDS32_INSN_NOT33;
7567 }
7568 break;
7569 case N32_ALU1_SRAI:
7570 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7571 {
7572 insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7573 insn_type = NDS32_INSN_SRAI45;
7574 }
7575 break;
7576
7577 case N32_ALU1_SRLI:
7578 if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7579 {
7580 insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7581 insn_type = NDS32_INSN_SRLI45;
7582 }
7583 break;
7584
7585 case N32_ALU1_SLLI:
7586 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7587 {
7588 insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7589 N32_UB5 (insn));
7590 insn_type = NDS32_INSN_SLLI333;
7591 }
7592 break;
7593
7594 case N32_ALU1_ZEH:
7595 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7596 {
7597 insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7598 insn_type = NDS32_INSN_ZEH33;
7599 }
7600 break;
7601
7602 case N32_ALU1_SEB:
7603 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7604 {
7605 insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7606 insn_type = NDS32_INSN_SEB33;
7607 }
7608 break;
7609
7610 case N32_ALU1_SEH:
7611 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7612 {
7613 insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7614 insn_type = NDS32_INSN_SEH33;
7615 }
7616 break;
7617
7618 case N32_ALU1_SLT:
7619 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7620 {
7621 /* Implicit r15. */
7622 insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7623 insn_type = NDS32_INSN_SLT45;
7624 }
7625 break;
7626
7627 case N32_ALU1_SLTS:
7628 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7629 {
7630 /* Implicit r15. */
7631 insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7632 insn_type = NDS32_INSN_SLTS45;
7633 }
7634 break;
7635 }
7636
7637 if ((insn16 & 0x8000) == 0)
7638 return 0;
7639
7640 if (pinsn16)
7641 *pinsn16 = insn16;
7642 if (pinsn_type)
7643 *pinsn_type = insn_type;
7644 return 1;
7645}
7646
7647static int
7648nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7649 int *pinsn_type)
7650{
7651 uint16_t insn16 = 0;
7652 int insn_type;
7653 unsigned long mach = bfd_get_mach (abfd);
7654
7655 /* TODO: bset, bclr, btgl, btst. */
7656 if (__GF (insn, 6, 4) != 0)
7657 return 0;
7658
7659 switch (N32_IMMU (insn, 6))
7660 {
7661 case N32_ALU2_MUL:
7662 if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7663 && N32_IS_RB3 (insn))
7664 {
7665 if (N32_RT5 (insn) == N32_RA5 (insn))
7666 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7667 else if (N32_RT5 (insn) == N32_RB5 (insn))
7668 insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7669 if (insn16)
7670 insn_type = NDS32_INSN_MUL33;
7671 }
7672 }
7673
7674 if ((insn16 & 0x8000) == 0)
7675 return 0;
7676
7677 if (pinsn16)
7678 *pinsn16 = insn16;
7679 if (pinsn_type)
7680 *pinsn_type = insn_type;
7681 return 1;
7682}
7683
7684int
7685nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7686 int *pinsn_type)
7687{
7688 int op6;
7689 uint16_t insn16 = 0;
1624c9ca 7690 int insn_type = 0;
35c08157
KLC
7691 unsigned long mach = bfd_get_mach (abfd);
7692
7693 /* Decode 32-bit instruction. */
7694 if (insn & 0x80000000)
7695 {
7696 /* Not 32-bit insn. */
7697 return 0;
7698 }
7699
7700 op6 = N32_OP6 (insn);
7701
7702 /* Convert it to 16-bit instruction. */
7703 switch (op6)
7704 {
7705 case N32_OP6_MOVI:
7706 if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7707 {
7708 insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7709 insn_type = NDS32_INSN_MOVI55;
7710 }
7711 else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7712 && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7713 {
7714 insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7715 N32_IMM20S (insn) - 16);
7716 insn_type = NDS32_INSN_MOVPI45;
7717 }
7718 break;
7719
7720 case N32_OP6_ADDI:
7721 if (N32_IMM15S (insn) == 0)
7722 {
7723 /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7724 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7725 if (mach <= MACH_V2
7726 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7727 {
7728 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7729 insn_type = NDS32_INSN_MOV55;
7730 }
7731 }
7732 else if (N32_IMM15S (insn) > 0)
7733 {
7734 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7735 {
7736 insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7737 N32_IMM15S (insn));
7738 insn_type = NDS32_INSN_ADDI333;
7739 }
7740 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7741 && N32_IMM15S (insn) < 32)
7742 {
7743 insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7744 insn_type = NDS32_INSN_ADDI45;
7745 }
7746 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7747 && N32_RT5 (insn) == N32_RA5 (insn)
7748 && N32_IMM15S (insn) < 512)
7749 {
7750 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7751 insn_type = NDS32_INSN_ADDI10_SP;
7752 }
7753 else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7754 && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7755 && (N32_IMM15S (insn) % 4 == 0))
7756 {
7757 insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7758 N32_IMM15S (insn) >> 2);
7759 insn_type = NDS32_INSN_ADDRI36_SP;
7760 }
7761 }
7762 else
7763 {
7764 /* Less than 0. */
7765 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7766 {
7767 insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7768 0 - N32_IMM15S (insn));
7769 insn_type = NDS32_INSN_SUBI333;
7770 }
7771 else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7772 && N32_IMM15S (insn) > -32)
7773 {
1c8f6a4d
KLC
7774 insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7775 0 - N32_IMM15S (insn));
35c08157
KLC
7776 insn_type = NDS32_INSN_SUBI45;
7777 }
7778 else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7779 && N32_RT5 (insn) == N32_RA5 (insn)
7780 && N32_IMM15S (insn) >= -512)
7781 {
7782 insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7783 insn_type = NDS32_INSN_ADDI10_SP;
7784 }
7785 }
7786 break;
7787
7788 case N32_OP6_ORI:
7789 if (N32_IMM15S (insn) == 0)
7790 {
7791 /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7792 because `mov55 $sp, $sp' is ifret16 in V3 ISA. */
7793 if (mach <= MACH_V2
7794 || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7795 {
7796 insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7797 insn_type = NDS32_INSN_MOV55;
7798 }
7799 }
7800 break;
7801
7802 case N32_OP6_SUBRI:
7803 if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7804 && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7805 {
7806 insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7807 insn_type = NDS32_INSN_NEG33;
7808 }
7809 break;
7810
7811 case N32_OP6_ANDI:
7812 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7813 {
7814 if (N32_IMM15U (insn) == 1)
7815 {
7816 insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7817 insn_type = NDS32_INSN_XLSB33;
7818 }
7819 else if (N32_IMM15U (insn) == 0x7ff)
7820 {
7821 insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7822 insn_type = NDS32_INSN_X11B33;
7823 }
7824 else if (N32_IMM15U (insn) == 0xff)
7825 {
7826 insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7827 insn_type = NDS32_INSN_ZEB33;
7828 }
7829 else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7830 && N32_IMM15U (insn) < 256)
7831 {
7832 int imm15u = N32_IMM15U (insn);
7833
7834 if (__builtin_popcount (imm15u) == 1)
7835 {
7836 /* BMSKI33 */
7837 int imm3u = __builtin_ctz (imm15u);
7838
7839 insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7840 insn_type = NDS32_INSN_BMSKI33;
7841 }
7842 else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7843 {
7844 /* FEXTI33 */
7845 int imm3u = __builtin_ctz (imm15u + 1) - 1;
7846
7847 insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7848 insn_type = NDS32_INSN_FEXTI33;
7849 }
7850 }
7851 }
7852 break;
7853
7854 case N32_OP6_SLTI:
7855 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7856 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7857 {
7858 insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7859 insn_type = NDS32_INSN_SLTI45;
7860 }
7861 break;
7862
7863 case N32_OP6_SLTSI:
7864 if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7865 && IS_WITHIN_U (N32_IMM15S (insn), 5))
7866 {
7867 insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7868 insn_type = NDS32_INSN_SLTSI45;
7869 }
7870 break;
7871
7872 case N32_OP6_LWI:
7873 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7874 {
7875 insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7876 insn_type = NDS32_INSN_LWI450;
7877 }
7878 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7879 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7880 {
7881 insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7882 N32_IMM15S (insn));
7883 insn_type = NDS32_INSN_LWI333;
7884 }
7885 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7886 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7887 {
7888 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7889 insn_type = NDS32_INSN_LWI37;
7890 }
7891 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7892 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7893 {
7894 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7895 insn_type = NDS32_INSN_LWI37_SP;
7896 }
7897 else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7898 && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7899 {
1c8f6a4d
KLC
7900 insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7901 N32_IMM15S (insn) + 32);
35c08157
KLC
7902 insn_type = NDS32_INSN_LWI45_FE;
7903 }
7904 break;
7905
7906 case N32_OP6_SWI:
7907 if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7908 {
7909 insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7910 insn_type = NDS32_INSN_SWI450;
7911 }
7912 else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7913 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7914 {
1c8f6a4d
KLC
7915 insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7916 N32_IMM15S (insn));
35c08157
KLC
7917 insn_type = NDS32_INSN_SWI333;
7918 }
7919 else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7920 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7921 {
7922 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7923 insn_type = NDS32_INSN_SWI37;
7924 }
7925 else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7926 && IS_WITHIN_U (N32_IMM15S (insn), 7))
7927 {
7928 insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7929 insn_type = NDS32_INSN_SWI37_SP;
7930 }
7931 break;
7932
7933 case N32_OP6_LWI_BI:
7934 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7935 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7936 {
7937 insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7938 N32_IMM15S (insn));
7939 insn_type = NDS32_INSN_LWI333_BI;
7940 }
7941 break;
7942
7943 case N32_OP6_SWI_BI:
7944 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7945 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7946 {
7947 insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7948 N32_IMM15S (insn));
7949 insn_type = NDS32_INSN_SWI333_BI;
7950 }
7951 break;
7952
7953 case N32_OP6_LHI:
7954 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7955 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7956 {
7957 insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7958 N32_IMM15S (insn));
7959 insn_type = NDS32_INSN_LHI333;
7960 }
7961 break;
7962
7963 case N32_OP6_SHI:
7964 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7965 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7966 {
7967 insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
7968 N32_IMM15S (insn));
7969 insn_type = NDS32_INSN_SHI333;
7970 }
7971 break;
7972
7973 case N32_OP6_LBI:
7974 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7975 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7976 {
7977 insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
7978 N32_IMM15S (insn));
7979 insn_type = NDS32_INSN_LBI333;
7980 }
7981 break;
7982
7983 case N32_OP6_SBI:
7984 if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7985 && IS_WITHIN_U (N32_IMM15S (insn), 3))
7986 {
7987 insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
7988 N32_IMM15S (insn));
7989 insn_type = NDS32_INSN_SBI333;
7990 }
7991 break;
7992
7993 case N32_OP6_ALU1:
7994 return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
7995
7996 case N32_OP6_ALU2:
7997 return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
7998
7999 case N32_OP6_BR1:
8000 if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8001 goto done;
8002
4ec521f2 8003 if ((insn & N32_BIT (14)) == 0)
35c08157
KLC
8004 {
8005 /* N32_BR1_BEQ */
8006 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8007 && N32_RT5 (insn) != REG_R5)
8008 insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8009 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8010 && N32_RA5 (insn) != REG_R5)
8011 insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8012 insn_type = NDS32_INSN_BEQS38;
8013 break;
8014 }
8015 else
8016 {
8017 /* N32_BR1_BNE */
8018 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8019 && N32_RT5 (insn) != REG_R5)
8020 insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8021 else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8022 && N32_RA5 (insn) != REG_R5)
8023 insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8024 insn_type = NDS32_INSN_BNES38;
8025 break;
8026 }
8027 break;
8028
8029 case N32_OP6_BR2:
8030 switch (N32_BR2_SUB (insn))
8031 {
8032 case N32_BR2_BEQZ:
8033 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8034 {
8035 insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8036 insn_type = NDS32_INSN_BEQZ38;
8037 }
1c8f6a4d
KLC
8038 else if (N32_RT5 (insn) == REG_R15
8039 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8040 {
8041 insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8042 insn_type = NDS32_INSN_BEQZS8;
8043 }
8044 break;
8045
8046 case N32_BR2_BNEZ:
8047 if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8048 {
8049 insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8050 insn_type = NDS32_INSN_BNEZ38;
8051 }
1c8f6a4d
KLC
8052 else if (N32_RT5 (insn) == REG_R15
8053 && IS_WITHIN_S (N32_IMM16S (insn), 8))
35c08157
KLC
8054 {
8055 insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8056 insn_type = NDS32_INSN_BNEZS8;
8057 }
8058 break;
8059
fbaf61ad
NC
8060 case N32_BR2_SOP0:
8061 if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
35c08157
KLC
8062 {
8063 insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8064 insn_type = NDS32_INSN_IFCALL9;
8065 }
8066 break;
8067 }
8068 break;
8069
8070 case N32_OP6_JI:
4ec521f2 8071 if ((insn & N32_BIT (24)) == 0)
35c08157
KLC
8072 {
8073 /* N32_JI_J */
8074 if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8075 {
8076 insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8077 insn_type = NDS32_INSN_J8;
8078 }
8079 }
8080 break;
8081
8082 case N32_OP6_JREG:
8083 if (__GF (insn, 8, 2) != 0)
8084 goto done;
8085
8086 switch (N32_IMMU (insn, 5))
8087 {
8088 case N32_JREG_JR:
8089 if (N32_JREG_HINT (insn) == 0)
8090 {
8091 /* jr */
8092 insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8093 insn_type = NDS32_INSN_JR5;
8094 }
8095 else if (N32_JREG_HINT (insn) == 1)
8096 {
8097 /* ret */
8098 insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8099 insn_type = NDS32_INSN_RET5;
8100 }
8101 else if (N32_JREG_HINT (insn) == 3)
8102 {
8103 /* ifret = mov55 $sp, $sp */
8104 insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8105 insn_type = NDS32_INSN_IFRET;
8106 }
8107 break;
8108
8109 case N32_JREG_JRAL:
8110 /* It's convertible when return rt5 is $lp and address
8111 translation is kept. */
8112 if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8113 {
8114 insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8115 insn_type = NDS32_INSN_JRAL5;
8116 }
8117 break;
8118 }
8119 break;
8120
8121 case N32_OP6_MISC:
8122 if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8123 {
8124 /* For v3, swid above 31 are used for ex9.it. */
8125 insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8126 insn_type = NDS32_INSN_BREAK16;
8127 }
8128 break;
8129
8130 default:
8131 /* This instruction has no 16-bit variant. */
8132 goto done;
8133 }
8134
dc1e8a47 8135 done:
35c08157
KLC
8136 /* Bit-15 of insn16 should be set for a valid instruction. */
8137 if ((insn16 & 0x8000) == 0)
8138 return 0;
8139
8140 if (pinsn16)
8141 *pinsn16 = insn16;
8142 if (pinsn_type)
8143 *pinsn_type = insn_type;
8144 return 1;
8145}
8146
8147static int
8148special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8149 Elf_Internal_Rela *reloc)
8150{
8151 uint16_t insn16 = 0;
8152
8153 if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8154 || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8155 return 0;
8156
8157 if (!N32_IS_RT3 (insn))
8158 return 0;
8159
8160 switch (N32_OP6 (insn))
8161 {
8162 case N32_OP6_LWI:
8163 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8164 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8165 break;
8166 case N32_OP6_SWI:
8167 if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8168 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8169 break;
8170 case N32_OP6_HWGP:
8171 if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8172 break;
8173
8174 if (__GF (insn, 17, 3) == 6)
8175 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8176 else if (__GF (insn, 17, 3) == 7)
8177 insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8178 break;
8179 }
8180
8181 if ((insn16 & 0x8000) == 0)
8182 return 0;
8183
8184 *pinsn16 = insn16;
8185 return 1;
8186}
8187
8188/* Convert a 16-bit instruction to 32-bit one.
8189 INSN16 it the input and PINSN it the point to output.
8190 Return non-zero on successful. Otherwise 0 is returned. */
8191
8192int
8193nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8194{
8195 uint32_t insn = 0xffffffff;
8196 unsigned long mach = bfd_get_mach (abfd);
8197
8198 /* NOTE: push25, pop25 and movd44 do not have 32-bit variants. */
8199
8200 switch (__GF (insn16, 9, 6))
8201 {
8202 case 0x4: /* add45 */
1c8f6a4d
KLC
8203 insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8204 N16_RA5 (insn16));
35c08157
KLC
8205 goto done;
8206 case 0x5: /* sub45 */
1c8f6a4d
KLC
8207 insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8208 N16_RA5 (insn16));
35c08157
KLC
8209 goto done;
8210 case 0x6: /* addi45 */
1c8f6a4d
KLC
8211 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8212 N16_IMM5U (insn16));
35c08157
KLC
8213 goto done;
8214 case 0x7: /* subi45 */
1c8f6a4d
KLC
8215 insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8216 -N16_IMM5U (insn16));
35c08157
KLC
8217 goto done;
8218 case 0x8: /* srai45 */
1c8f6a4d
KLC
8219 insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8220 N16_IMM5U (insn16));
35c08157
KLC
8221 goto done;
8222 case 0x9: /* srli45 */
1c8f6a4d
KLC
8223 insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8224 N16_IMM5U (insn16));
35c08157 8225 goto done;
35c08157 8226 case 0xa: /* slli333 */
1c8f6a4d
KLC
8227 insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8228 N16_IMM3U (insn16));
35c08157
KLC
8229 goto done;
8230 case 0xc: /* add333 */
1c8f6a4d
KLC
8231 insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8232 N16_RB3 (insn16));
35c08157
KLC
8233 goto done;
8234 case 0xd: /* sub333 */
1c8f6a4d
KLC
8235 insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8236 N16_RB3 (insn16));
35c08157
KLC
8237 goto done;
8238 case 0xe: /* addi333 */
1c8f6a4d
KLC
8239 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8240 N16_IMM3U (insn16));
35c08157
KLC
8241 goto done;
8242 case 0xf: /* subi333 */
1c8f6a4d
KLC
8243 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8244 -N16_IMM3U (insn16));
35c08157 8245 goto done;
35c08157 8246 case 0x10: /* lwi333 */
1c8f6a4d
KLC
8247 insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8248 N16_IMM3U (insn16));
35c08157
KLC
8249 goto done;
8250 case 0x12: /* lhi333 */
1c8f6a4d
KLC
8251 insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8252 N16_IMM3U (insn16));
35c08157
KLC
8253 goto done;
8254 case 0x13: /* lbi333 */
1c8f6a4d
KLC
8255 insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8256 N16_IMM3U (insn16));
35c08157
KLC
8257 goto done;
8258 case 0x11: /* lwi333.bi */
1c8f6a4d
KLC
8259 insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8260 N16_IMM3U (insn16));
35c08157
KLC
8261 goto done;
8262 case 0x14: /* swi333 */
1c8f6a4d
KLC
8263 insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8264 N16_IMM3U (insn16));
35c08157
KLC
8265 goto done;
8266 case 0x16: /* shi333 */
1c8f6a4d
KLC
8267 insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8268 N16_IMM3U (insn16));
35c08157
KLC
8269 goto done;
8270 case 0x17: /* sbi333 */
1c8f6a4d
KLC
8271 insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8272 N16_IMM3U (insn16));
35c08157
KLC
8273 goto done;
8274 case 0x15: /* swi333.bi */
1c8f6a4d
KLC
8275 insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8276 N16_IMM3U (insn16));
35c08157 8277 goto done;
35c08157 8278 case 0x18: /* addri36.sp */
1c8f6a4d
KLC
8279 insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8280 N16_IMM6U (insn16) << 2);
35c08157 8281 goto done;
35c08157 8282 case 0x19: /* lwi45.fe */
1c8f6a4d
KLC
8283 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8284 (N16_IMM5U (insn16) - 32));
35c08157
KLC
8285 goto done;
8286 case 0x1a: /* lwi450 */
8287 insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8288 goto done;
8289 case 0x1b: /* swi450 */
8290 insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8291 goto done;
8292
1c8f6a4d 8293 /* These are r15 implied instructions. */
35c08157
KLC
8294 case 0x30: /* slts45 */
8295 insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8296 goto done;
8297 case 0x31: /* slt45 */
8298 insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8299 goto done;
8300 case 0x32: /* sltsi45 */
8301 insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8302 goto done;
8303 case 0x33: /* slti45 */
8304 insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8305 goto done;
8306 case 0x34: /* beqzs8, bnezs8 */
4ec521f2 8307 if (insn16 & N32_BIT (8))
35c08157
KLC
8308 insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8309 else
8310 insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8311 goto done;
8312
8313 case 0x35: /* break16, ex9.it */
8314 /* Only consider range of v3 break16. */
8315 insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8316 goto done;
8317
8318 case 0x3c: /* ifcall9 */
fbaf61ad 8319 insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
35c08157
KLC
8320 goto done;
8321 case 0x3d: /* movpi45 */
8322 insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8323 goto done;
8324
8325 case 0x3f: /* MISC33 */
1c8f6a4d 8326 switch (insn16 & 0x7)
35c08157
KLC
8327 {
8328 case 2: /* neg33 */
8329 insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8330 break;
8331 case 3: /* not33 */
1c8f6a4d
KLC
8332 insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8333 N16_RA3 (insn16));
35c08157
KLC
8334 break;
8335 case 4: /* mul33 */
1c8f6a4d
KLC
8336 insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8337 N16_RA3 (insn16));
35c08157
KLC
8338 break;
8339 case 5: /* xor33 */
1c8f6a4d
KLC
8340 insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8341 N16_RA3 (insn16));
35c08157
KLC
8342 break;
8343 case 6: /* and33 */
1c8f6a4d
KLC
8344 insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8345 N16_RA3 (insn16));
35c08157
KLC
8346 break;
8347 case 7: /* or33 */
1c8f6a4d
KLC
8348 insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8349 N16_RA3 (insn16));
35c08157
KLC
8350 break;
8351 }
8352 goto done;
8353
1c8f6a4d 8354 case 0xb:
35c08157
KLC
8355 switch (insn16 & 0x7)
8356 {
8357 case 0: /* zeb33 */
8358 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8359 break;
8360 case 1: /* zeh33 */
8361 insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8362 break;
8363 case 2: /* seb33 */
8364 insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8365 break;
8366 case 3: /* seh33 */
8367 insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8368 break;
8369 case 4: /* xlsb33 */
8370 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8371 break;
8372 case 5: /* x11b33 */
8373 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8374 break;
8375 case 6: /* bmski33 */
8376 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8377 1 << __GF (insn16, 3, 3));
35c08157
KLC
8378 break;
8379 case 7: /* fexti33 */
8380 insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
1c8f6a4d 8381 (1 << (__GF (insn16, 3, 3) + 1)) - 1);
35c08157
KLC
8382 break;
8383 }
8384 goto done;
8385 }
8386
8387 switch (__GF (insn16, 10, 5))
8388 {
8389 case 0x0: /* mov55 or ifret16 */
8390 if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8391 && N16_RT5 (insn16) == N16_RA5 (insn16))
1c8f6a4d 8392 insn = N32_JREG (JR, 0, 0, 0, 3);
35c08157 8393 else
1c8f6a4d 8394 insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
35c08157
KLC
8395 goto done;
8396 case 0x1: /* movi55 */
8397 insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8398 goto done;
8399 case 0x1b: /* addi10s (V2) */
8400 insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8401 goto done;
8402 }
8403
8404 switch (__GF (insn16, 11, 4))
8405 {
8406 case 0x7: /* lwi37.fp/swi37.fp */
4ec521f2 8407 if (insn16 & N32_BIT (7)) /* swi37.fp */
35c08157
KLC
8408 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8409 else /* lwi37.fp */
8410 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8411 goto done;
8412 case 0x8: /* beqz38 */
8413 insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8414 goto done;
8415 case 0x9: /* bnez38 */
8416 insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8417 goto done;
8418 case 0xa: /* beqs38/j8, implied r5 */
8419 if (N16_RT38 (insn16) == 5)
8420 insn = N32_JI (J, N16_IMM8S (insn16));
8421 else
8422 insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8423 goto done;
fbaf61ad 8424 case 0xb: /* bnes38 and others. */
35c08157
KLC
8425 if (N16_RT38 (insn16) == 5)
8426 {
8427 switch (__GF (insn16, 5, 3))
8428 {
8429 case 0: /* jr5 */
8430 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8431 break;
8432 case 4: /* ret5 */
8433 insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8434 break;
8435 case 1: /* jral5 */
8436 insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8437 break;
8438 case 2: /* ex9.it imm5 */
8439 /* ex9.it had no 32-bit variantl. */
8440 break;
8441 case 5: /* add5.pc */
8442 /* add5.pc had no 32-bit variantl. */
8443 break;
8444 }
8445 }
8446 else /* bnes38 */
8447 insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8448 goto done;
8449 case 0xe: /* lwi37/swi37 */
8450 if (insn16 & (1 << 7)) /* swi37.sp */
8451 insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8452 else /* lwi37.sp */
8453 insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8454 goto done;
8455 }
8456
dc1e8a47 8457 done:
35c08157
KLC
8458 if (insn & 0x80000000)
8459 return 0;
8460
8461 if (pinsn)
8462 *pinsn = insn;
8463 return 1;
8464}
8465\f
fbaf61ad 8466
35c08157
KLC
8467static bfd_boolean
8468is_sda_access_insn (unsigned long insn)
8469{
8470 switch (N32_OP6 (insn))
8471 {
8472 case N32_OP6_LWI:
8473 case N32_OP6_LHI:
8474 case N32_OP6_LHSI:
8475 case N32_OP6_LBI:
8476 case N32_OP6_LBSI:
8477 case N32_OP6_SWI:
8478 case N32_OP6_SHI:
8479 case N32_OP6_SBI:
8480 case N32_OP6_LWC:
8481 case N32_OP6_LDC:
8482 case N32_OP6_SWC:
8483 case N32_OP6_SDC:
8484 return TRUE;
8485 default:
8486 ;
8487 }
8488 return FALSE;
8489}
8490
8491static unsigned long
8492turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8493{
8494 uint32_t oinsn = 0;
8495
8496 switch (type)
8497 {
8498 case R_NDS32_GOT_LO12:
8499 case R_NDS32_GOTOFF_LO12:
8500 case R_NDS32_PLTREL_LO12:
8501 case R_NDS32_PLT_GOTREL_LO12:
8502 case R_NDS32_LO12S0_RELA:
8503 switch (N32_OP6 (insn))
8504 {
8505 case N32_OP6_LBI:
8506 /* lbi.gp */
8507 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8508 break;
8509 case N32_OP6_LBSI:
8510 /* lbsi.gp */
4ec521f2 8511 oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8512 break;
8513 case N32_OP6_SBI:
8514 /* sbi.gp */
8515 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8516 break;
8517 case N32_OP6_ORI:
8518 /* addi.gp */
4ec521f2 8519 oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8520 break;
8521 }
8522 break;
8523
8524 case R_NDS32_LO12S1_RELA:
8525 switch (N32_OP6 (insn))
8526 {
8527 case N32_OP6_LHI:
8528 /* lhi.gp */
8529 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8530 break;
8531 case N32_OP6_LHSI:
8532 /* lhsi.gp */
4ec521f2 8533 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
35c08157
KLC
8534 break;
8535 case N32_OP6_SHI:
8536 /* shi.gp */
4ec521f2 8537 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
35c08157
KLC
8538 break;
8539 }
8540 break;
8541
8542 case R_NDS32_LO12S2_RELA:
8543 switch (N32_OP6 (insn))
8544 {
8545 case N32_OP6_LWI:
8546 /* lwi.gp */
8547 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8548 break;
8549 case N32_OP6_SWI:
8550 /* swi.gp */
8551 oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8552 break;
8553 }
8554 break;
8555
8556 case R_NDS32_LO12S2_DP_RELA:
8557 case R_NDS32_LO12S2_SP_RELA:
8558 oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8559 break;
8560 }
8561
8562 if (oinsn)
8563 *pinsn = oinsn;
8564
8565 return oinsn != 0;
8566}
8567
8568/* Linker hasn't found the correct merge section for non-section symbol
8569 in relax time, this work is left to the function elf_link_input_bfd().
8570 So for non-section symbol, _bfd_merged_section_offset is also needed
8571 to find the correct symbol address. */
8572
8573static bfd_vma
8574nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8575 asection **psec, Elf_Internal_Rela *rel)
8576{
8577 asection *sec = *psec;
8578 bfd_vma relocation;
8579
8580 relocation = (sec->output_section->vma
8581 + sec->output_offset + sym->st_value);
8582 if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8583 {
8584 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8585 rel->r_addend =
8586 _bfd_merged_section_offset (abfd, psec,
8587 elf_section_data (sec)->sec_info,
8588 sym->st_value + rel->r_addend);
8589 else
8590 rel->r_addend =
8591 _bfd_merged_section_offset (abfd, psec,
8592 elf_section_data (sec)->sec_info,
8593 sym->st_value) + rel->r_addend;
8594
8595 if (sec != *psec)
8596 {
8597 /* If we have changed the section, and our original section is
8598 marked with SEC_EXCLUDE, it means that the original
8599 SEC_MERGE section has been completely subsumed in some
8600 other SEC_MERGE section. In this case, we need to leave
8601 some info around for --emit-relocs. */
8602 if ((sec->flags & SEC_EXCLUDE) != 0)
8603 sec->kept_section = *psec;
8604 sec = *psec;
8605 }
8606 rel->r_addend -= relocation;
8607 rel->r_addend += sec->output_section->vma + sec->output_offset;
8608 }
8609 return relocation;
8610}
8611
8612static bfd_vma
8613calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8614 Elf_Internal_Sym *isymbuf,
8615 Elf_Internal_Shdr *symtab_hdr)
8616{
8617 bfd_signed_vma foff;
8618 bfd_vma symval, addend;
8619 Elf_Internal_Rela irel_fn;
8620 Elf_Internal_Sym *isym;
8621 asection *sym_sec;
8622
8623 /* Get the value of the symbol referred to by the reloc. */
8624 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8625 {
8626 /* A local symbol. */
8627 isym = isymbuf + ELF32_R_SYM (irel->r_info);
8628
8629 if (isym->st_shndx == SHN_UNDEF)
8630 sym_sec = bfd_und_section_ptr;
8631 else if (isym->st_shndx == SHN_ABS)
8632 sym_sec = bfd_abs_section_ptr;
8633 else if (isym->st_shndx == SHN_COMMON)
8634 sym_sec = bfd_com_section_ptr;
8635 else
8636 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8637 memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8638 symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8639 addend = irel_fn.r_addend;
8640 }
8641 else
8642 {
8643 unsigned long indx;
8644 struct elf_link_hash_entry *h;
8645
8646 /* An external symbol. */
8647 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8648 h = elf_sym_hashes (abfd)[indx];
8649 BFD_ASSERT (h != NULL);
8650
8651 while (h->root.type == bfd_link_hash_indirect
8652 || h->root.type == bfd_link_hash_warning)
8653 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8654
8655 if (h->root.type != bfd_link_hash_defined
8656 && h->root.type != bfd_link_hash_defweak)
8657 /* This appears to be a reference to an undefined
8658 symbol. Just ignore it--it will be caught by the
8659 regular reloc processing. */
8660 return 0;
8661
8662 if (h->root.u.def.section->flags & SEC_MERGE)
8663 {
8664 sym_sec = h->root.u.def.section;
8665 symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8666 (sym_sec)->sec_info, h->root.u.def.value);
8667 symval = symval + sym_sec->output_section->vma
8668 + sym_sec->output_offset;
8669 }
8670 else
8671 symval = (h->root.u.def.value
8672 + h->root.u.def.section->output_section->vma
8673 + h->root.u.def.section->output_offset);
8674 addend = irel->r_addend;
8675 }
8676
8677 foff = symval + addend;
8678
8679 return foff;
8680}
8681
35c08157
KLC
8682static int
8683is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8684 asection *sec, Elf_Internal_Rela *rel)
8685{
8686 bfd_byte *contents;
8687 unsigned short insn16;
8688
8689 if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8690 return FALSE;
8691 contents = elf_section_data (sec)->this_hdr.contents;
8692 insn16 = bfd_getb16 (contents + rel->r_offset);
8693 if (insn16 == NDS32_NOP16)
8694 return TRUE;
8695 return FALSE;
8696}
8697
8698/* It checks whether the instruction could be converted to
8699 16-bit form and returns the converted one.
8700
8701 `internal_relocs' is supposed to be sorted. */
8702
8703static int
8704is_convert_32_to_16 (bfd *abfd, asection *sec,
8705 Elf_Internal_Rela *reloc,
8706 Elf_Internal_Rela *internal_relocs,
8707 Elf_Internal_Rela *irelend,
8708 uint16_t *insn16)
8709{
8710#define NORMAL_32_TO_16 (1 << 0)
8711#define SPECIAL_32_TO_16 (1 << 1)
8712 bfd_byte *contents = NULL;
8713 bfd_signed_vma off;
8714 bfd_vma mem_addr;
8715 uint32_t insn = 0;
8716 Elf_Internal_Rela *pc_rel;
35c08157
KLC
8717 Elf_Internal_Shdr *symtab_hdr;
8718 Elf_Internal_Sym *isymbuf = NULL;
8719 int convert_type;
8720 bfd_vma offset;
8721
8722 if (reloc->r_offset + 4 > sec->size)
8723 return FALSE;
8724
8725 offset = reloc->r_offset;
8726
0c4bd9d9 8727 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE))
35c08157
KLC
8728 return FALSE;
8729 insn = bfd_getb32 (contents + offset);
8730
8731 if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8732 convert_type = NORMAL_32_TO_16;
8733 else if (special_convert_32_to_16 (insn, insn16, reloc))
8734 convert_type = SPECIAL_32_TO_16;
8735 else
8736 return FALSE;
8737
8738 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8739 if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8740 return FALSE;
8741
8742 /* Find the first relocation of the same relocation-type,
8743 so we iteratie them forward. */
8744 pc_rel = reloc;
1c8f6a4d 8745 while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
35c08157
KLC
8746 pc_rel--;
8747
8748 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8749 {
8750 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8751 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8752 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8753 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8754 {
fbaf61ad 8755 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8756 if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8757 || off == 0)
35c08157
KLC
8758 return FALSE;
8759 break;
8760 }
8761 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8762 {
8763 /* movi => movi55 */
1c8f6a4d
KLC
8764 mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8765 symtab_hdr);
8766 /* mem_addr is unsigned, but the value should
8767 be between [-16, 15]. */
35c08157
KLC
8768 if ((mem_addr + 0x10) >> 5)
8769 return FALSE;
8770 break;
8771 }
1c8f6a4d
KLC
8772 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8773 || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8774 {
8775 /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8776 because it can be relaxed to addi for TLS_LE_ADD. */
8777 return FALSE;
8778 }
35c08157
KLC
8779 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8780 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8781 && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8782 && convert_type == SPECIAL_32_TO_16)
8783 {
8784 /* fp-as-gp
8785 We've selected a best fp-base for this access, so we can
8786 always resolve it anyway. Do nothing. */
8787 break;
8788 }
8789 else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8790 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8791 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8792 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8793 || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8794 && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8795 {
1c8f6a4d
KLC
8796 /* Prevent unresolved addi instruction translate
8797 to addi45 or addi333. */
35c08157
KLC
8798 return FALSE;
8799 }
1c8f6a4d
KLC
8800 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8801 {
fbaf61ad 8802 off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
1c8f6a4d
KLC
8803 if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8804 return FALSE;
8805 break;
8806 }
35c08157
KLC
8807 }
8808
8809 return TRUE;
8810}
8811
8812static void
8813nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8814 Elf_Internal_Rela *reloc,
8815 Elf_Internal_Rela *internal_relocs,
8816 Elf_Internal_Rela *irelend,
8817 unsigned short insn16)
8818{
8819 Elf_Internal_Rela *pc_rel;
8820 bfd_vma offset;
8821
8822 offset = reloc->r_offset;
8823 bfd_putb16 (insn16, contents + offset);
8824 /* Find the first relocation of the same relocation-type,
8825 so we iteratie them forward. */
8826 pc_rel = reloc;
8827 while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8828 pc_rel--;
8829
8830 for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8831 {
8832 if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8833 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8834 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8835 {
8836 pc_rel->r_info =
8837 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8838 }
8839 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8840 pc_rel->r_info =
8841 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8842 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8843 pc_rel->r_info =
8844 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8845 else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8846 || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8847 pc_rel->r_info =
8848 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
1c8f6a4d
KLC
8849 else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8850 pc_rel->r_info =
8851 ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
35c08157
KLC
8852 }
8853}
8854
8855/* Find a relocation of type specified by `reloc_type'
8856 of the same r_offset with reloc.
8857 If not found, return irelend.
8858
8859 Assuming relocations are sorted by r_offset,
8860 we find the relocation from `reloc' backward untill relocs,
8861 or find it from `reloc' forward untill irelend. */
8862
8863static Elf_Internal_Rela *
8864find_relocs_at_address (Elf_Internal_Rela *reloc,
8865 Elf_Internal_Rela *relocs,
8866 Elf_Internal_Rela *irelend,
8867 enum elf_nds32_reloc_type reloc_type)
8868{
8869 Elf_Internal_Rela *rel_t;
8870
8871 /* Find backward. */
8872 for (rel_t = reloc;
8873 rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8874 rel_t--)
8875 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8876 return rel_t;
8877
1c8f6a4d 8878 /* We didn't find it backward. Try find it forward. */
35c08157
KLC
8879 for (rel_t = reloc;
8880 rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8881 rel_t++)
8882 if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8883 return rel_t;
8884
8885 return irelend;
8886}
8887
8888/* Find a relocation of specified type and offset.
8889 `reloc' is just a refence point to find a relocation at specified offset.
8890 If not found, return irelend.
8891
8892 Assuming relocations are sorted by r_offset,
8893 we find the relocation from `reloc' backward untill relocs,
8894 or find it from `reloc' forward untill irelend. */
8895
8896static Elf_Internal_Rela *
8897find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8898 Elf_Internal_Rela *relocs,
8899 Elf_Internal_Rela *irelend,
6cae483a 8900 enum elf_nds32_reloc_type reloc_type,
35c08157
KLC
8901 bfd_vma offset_p)
8902{
8903 Elf_Internal_Rela *rel_t = NULL;
8904
8905 /* First, we try to find a relocation of offset `offset_p',
8906 and then we use find_relocs_at_address to find specific type. */
8907
8908 if (reloc->r_offset > offset_p)
8909 {
8910 /* Find backward. */
8911 for (rel_t = reloc;
8912 rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8913 /* Do nothing. */;
8914 }
8915 else if (reloc->r_offset < offset_p)
8916 {
8917 /* Find forward. */
8918 for (rel_t = reloc;
8919 rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8920 /* Do nothing. */;
8921 }
8922 else
8923 rel_t = reloc;
8924
8925 /* Not found? */
8926 if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8927 return irelend;
8928
8929 return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8930}
8931
fbaf61ad
NC
8932typedef struct nds32_elf_blank nds32_elf_blank_t;
8933struct nds32_elf_blank
35c08157 8934{
fbaf61ad
NC
8935 /* Where the blank begins. */
8936 bfd_vma offset;
8937 /* The size of the blank. */
8938 bfd_vma size;
8939 /* The accumulative size before this blank. */
8940 bfd_vma total_size;
8941 nds32_elf_blank_t *next;
8942 nds32_elf_blank_t *prev;
8943};
35c08157
KLC
8944
8945static nds32_elf_blank_t *blank_free_list = NULL;
8946
8947static nds32_elf_blank_t *
8948create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8949{
8950 nds32_elf_blank_t *blank_t;
8951
8952 if (blank_free_list)
8953 {
8954 blank_t = blank_free_list;
8955 blank_free_list = blank_free_list->next;
8956 }
8957 else
8958 blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
8959
8960 if (blank_t == NULL)
8961 return NULL;
8962
8963 blank_t->offset = offset_p;
8964 blank_t->size = size_p;
8965 blank_t->total_size = 0;
8966 blank_t->next = NULL;
8967 blank_t->prev = NULL;
8968
8969 return blank_t;
8970}
8971
8972static void
8973remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
8974{
8975 if (blank_free_list)
8976 {
8977 blank_free_list->prev = blank_p;
8978 blank_p->next = blank_free_list;
8979 }
8980 else
8981 blank_p->next = NULL;
8982
8983 blank_p->prev = NULL;
8984 blank_free_list = blank_p;
8985}
8986
8987static void
8988clean_nds32_elf_blank (void)
8989{
8990 nds32_elf_blank_t *blank_t;
8991
8992 while (blank_free_list)
8993 {
8994 blank_t = blank_free_list;
8995 blank_free_list = blank_free_list->next;
8996 free (blank_t);
8997 }
8998}
8999
9000static nds32_elf_blank_t *
9001search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9002{
9003 nds32_elf_blank_t *blank_t;
9004
9005 if (!blank_p)
9006 return NULL;
9007 blank_t = blank_p;
9008
9009 while (blank_t && addr < blank_t->offset)
9010 blank_t = blank_t->prev;
9011 while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9012 blank_t = blank_t->next;
9013
9014 return blank_t;
9015}
9016
9017static bfd_vma
9018get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9019 int overwrite)
9020{
9021 nds32_elf_blank_t *blank_t;
9022
9023 blank_t = search_nds32_elf_blank (*blank_p, addr);
9024 if (!blank_t)
9025 return 0;
9026
9027 if (overwrite)
9028 *blank_p = blank_t;
9029
9030 if (addr < blank_t->offset + blank_t->size)
9031 return blank_t->total_size + (addr - blank_t->offset);
9032 else
9033 return blank_t->total_size + blank_t->size;
9034}
9035
9036static bfd_boolean
9037insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9038{
9039 nds32_elf_blank_t *blank_t, *blank_t2;
9040
9041 if (!*blank_p)
9042 {
9043 *blank_p = create_nds32_elf_blank (addr, len);
9044 return *blank_p ? TRUE : FALSE;
9045 }
9046
9047 blank_t = search_nds32_elf_blank (*blank_p, addr);
9048
9049 if (blank_t == NULL)
9050 {
9051 blank_t = create_nds32_elf_blank (addr, len);
9052 if (!blank_t)
9053 return FALSE;
9054 while ((*blank_p)->prev != NULL)
9055 *blank_p = (*blank_p)->prev;
9056 blank_t->next = *blank_p;
9057 (*blank_p)->prev = blank_t;
9058 (*blank_p) = blank_t;
9059 return TRUE;
9060 }
9061
9062 if (addr < blank_t->offset + blank_t->size)
9063 {
fbaf61ad
NC
9064 /* Extend the origin blank. */
9065 if (addr + len > blank_t->offset + blank_t->size)
9066 blank_t->size = addr + len - blank_t->offset;
35c08157
KLC
9067 }
9068 else
9069 {
9070 blank_t2 = create_nds32_elf_blank (addr, len);
9071 if (!blank_t2)
9072 return FALSE;
9073 if (blank_t->next)
9074 {
9075 blank_t->next->prev = blank_t2;
9076 blank_t2->next = blank_t->next;
9077 }
9078 blank_t2->prev = blank_t;
9079 blank_t->next = blank_t2;
9080 *blank_p = blank_t2;
9081 }
9082
9083 return TRUE;
9084}
9085
9086static bfd_boolean
9087insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9088 bfd_vma len)
9089{
9090 nds32_elf_blank_t *blank_t;
9091
9092 if (!insert_nds32_elf_blank (blank_p, addr, len))
9093 return FALSE;
9094
9095 blank_t = *blank_p;
9096
9097 if (!blank_t->prev)
9098 {
9099 blank_t->total_size = 0;
9100 blank_t = blank_t->next;
9101 }
9102
9103 while (blank_t)
9104 {
9105 blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9106 blank_t = blank_t->next;
9107 }
9108
9109 return TRUE;
9110}
9111
9112static void
9113calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9114{
9115 nds32_elf_blank_t *blank_t;
9116 bfd_vma total_size = 0;
9117
9118 if (!blank_p)
9119 return;
9120
9121 blank_t = blank_p;
9122 while (blank_t->prev)
9123 blank_t = blank_t->prev;
9124 while (blank_t)
9125 {
9126 blank_t->total_size = total_size;
9127 total_size += blank_t->size;
9128 blank_t = blank_t->next;
9129 }
9130}
9131
9132static bfd_boolean
9133nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9134 nds32_elf_blank_t *blank_p)
9135{
9136 Elf_Internal_Shdr *symtab_hdr; /* Symbol table header of this bfd. */
1c8f6a4d 9137 Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd. */
35c08157
KLC
9138 Elf_Internal_Sym *isymend; /* Symbol entry iterator. */
9139 unsigned int sec_shndx; /* The section the be relaxed. */
9140 bfd_byte *contents; /* Contents data of iterating section. */
9141 Elf_Internal_Rela *internal_relocs;
9142 Elf_Internal_Rela *irel;
9143 Elf_Internal_Rela *irelend;
9144 struct elf_link_hash_entry **sym_hashes;
9145 struct elf_link_hash_entry **end_hashes;
9146 unsigned int symcount;
9147 asection *sect;
9148 nds32_elf_blank_t *blank_t;
9149 nds32_elf_blank_t *blank_t2;
9150 nds32_elf_blank_t *blank_head;
9151
9152 blank_head = blank_t = blank_p;
9153 while (blank_head->prev != NULL)
9154 blank_head = blank_head->prev;
9155 while (blank_t->next != NULL)
9156 blank_t = blank_t->next;
9157
9158 if (blank_t->offset + blank_t->size <= sec->size)
9159 {
9160 blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9161 blank_t->next->prev = blank_t;
9162 }
9163 if (blank_head->offset > 0)
9164 {
9165 blank_head->prev = create_nds32_elf_blank (0, 0);
9166 blank_head->prev->next = blank_head;
9167 blank_head = blank_head->prev;
9168 }
9169
9170 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9171
9172 /* The deletion must stop at the next ALIGN reloc for an alignment
9173 power larger than the number of bytes we are deleting. */
9174
9175 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9176 if (!nds32_get_local_syms (abfd, sec, &isym))
9177 return FALSE;
9178
9179 if (isym == NULL)
9180 {
9181 isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9182 symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9183 symtab_hdr->contents = (bfd_byte *) isym;
9184 }
9185
9186 if (isym == NULL || symtab_hdr->sh_info == 0)
9187 return FALSE;
9188
9189 blank_t = blank_head;
9190 calc_nds32_blank_total (blank_head);
9191
9192 for (sect = abfd->sections; sect != NULL; sect = sect->next)
9193 {
9194 /* Adjust all the relocs. */
9195
9196 /* Relocations MUST be kept in memory, because relaxation adjust them. */
9197 internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9198 TRUE /* keep_memory */);
9199 irelend = internal_relocs + sect->reloc_count;
9200
9201 blank_t = blank_head;
9202 blank_t2 = blank_head;
9203
9204 if (!(sect->flags & SEC_RELOC))
9205 continue;
9206
0c4bd9d9 9207 nds32_get_section_contents (abfd, sect, &contents, TRUE);
35c08157
KLC
9208
9209 for (irel = internal_relocs; irel < irelend; irel++)
9210 {
9211 bfd_vma raddr;
9212
9213 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9214 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9215 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9216 {
9217 unsigned long val = 0;
1c8f6a4d
KLC
9218 unsigned long mask;
9219 long before, between;
6cae483a 9220 long offset = 0;
35c08157
KLC
9221
9222 switch (ELF32_R_TYPE (irel->r_info))
9223 {
9224 case R_NDS32_DIFF8:
1c8f6a4d 9225 offset = bfd_get_8 (abfd, contents + irel->r_offset);
35c08157
KLC
9226 break;
9227 case R_NDS32_DIFF16:
1c8f6a4d 9228 offset = bfd_get_16 (abfd, contents + irel->r_offset);
35c08157
KLC
9229 break;
9230 case R_NDS32_DIFF32:
9231 val = bfd_get_32 (abfd, contents + irel->r_offset);
1c8f6a4d
KLC
9232 /* Get the signed bit and mask for the high part. The
9233 gcc will alarm when right shift 32-bit since the
9234 type size of long may be 32-bit. */
9235 mask = 0 - (val >> 31);
9236 if (mask)
9237 offset = (val | (mask - 0xffffffff));
9238 else
9239 offset = val;
35c08157
KLC
9240 break;
9241 default:
9242 BFD_ASSERT (0);
9243 }
9244
9245 /* DIFF value
9246 0 |encoded in location|
9247 |------------|-------------------|---------
9248 sym+off(addend)
9249 -- before ---| *****************
9250 --------------------- between ---|
9251
1c8f6a4d
KLC
9252 We only care how much data are relax between DIFF,
9253 marked as ***. */
35c08157
KLC
9254
9255 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9256 between = get_nds32_elf_blank_total (&blank_t,
9257 irel->r_addend + offset, 0);
35c08157
KLC
9258 if (between == before)
9259 goto done_adjust_diff;
9260
9261 switch (ELF32_R_TYPE (irel->r_info))
9262 {
9263 case R_NDS32_DIFF8:
1c8f6a4d
KLC
9264 bfd_put_8 (abfd, offset - (between - before),
9265 contents + irel->r_offset);
35c08157
KLC
9266 break;
9267 case R_NDS32_DIFF16:
1c8f6a4d
KLC
9268 bfd_put_16 (abfd, offset - (between - before),
9269 contents + irel->r_offset);
35c08157
KLC
9270 break;
9271 case R_NDS32_DIFF32:
1c8f6a4d
KLC
9272 bfd_put_32 (abfd, offset - (between - before),
9273 contents + irel->r_offset);
35c08157
KLC
9274 break;
9275 }
9276 }
9277 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9278 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9279 {
9280 bfd_vma val = 0;
9281 unsigned int len = 0;
9282 unsigned long before, between;
9283 bfd_byte *endp, *p;
9284
4265548c
PA
9285 val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9286 &len);
35c08157
KLC
9287
9288 before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
1c8f6a4d
KLC
9289 between = get_nds32_elf_blank_total (&blank_t,
9290 irel->r_addend + val, 0);
35c08157
KLC
9291 if (between == before)
9292 goto done_adjust_diff;
9293
9294 p = contents + irel->r_offset;
9295 endp = p + len -1;
9296 memset (p, 0x80, len);
9297 *(endp) = 0;
9298 p = write_uleb128 (p, val - (between - before)) - 1;
9299 if (p < endp)
9300 *p |= 0x80;
9301 }
dc1e8a47 9302 done_adjust_diff:
35c08157
KLC
9303
9304 if (sec == sect)
9305 {
9306 raddr = irel->r_offset;
1c8f6a4d
KLC
9307 irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9308 irel->r_offset, 1);
35c08157
KLC
9309
9310 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9311 continue;
9312 if (blank_t2 && blank_t2->next
1c8f6a4d
KLC
9313 && (blank_t2->offset > raddr
9314 || blank_t2->next->offset <= raddr))
4eca0228 9315 _bfd_error_handler
38f14ab8
AM
9316 (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9317 abfd);
35c08157
KLC
9318
9319 /* Mark reloc in deleted portion as NONE.
9320 For some relocs like R_NDS32_LABEL that doesn't modify the
9321 content in the section. R_NDS32_LABEL doesn't belong to the
9322 instruction in the section, so we should preserve it. */
9323 if (raddr >= blank_t2->offset
9324 && raddr < blank_t2->offset + blank_t2->size
9325 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9326 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9327 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9328 && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9329 && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9330 && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9331 {
9332 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9333 R_NDS32_NONE);
9334 continue;
9335 }
9336 }
9337
9338 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9339 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9340 || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9341 continue;
9342
9343 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9344 && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9345 && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9346 {
9347 if (irel->r_addend <= sec->size)
9348 irel->r_addend -=
9349 get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9350 }
9351 }
9352 }
9353
9354 /* Adjust the local symbols defined in this section. */
9355 blank_t = blank_head;
9356 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9357 {
9358 if (isym->st_shndx == sec_shndx)
9359 {
9360 if (isym->st_value <= sec->size)
9361 {
9362 bfd_vma ahead;
9363 bfd_vma orig_addr = isym->st_value;
9364
9365 ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9366 isym->st_value -= ahead;
9367
9368 /* Adjust function size. */
1c8f6a4d
KLC
9369 if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9370 && isym->st_size > 0)
9371 isym->st_size -=
9372 get_nds32_elf_blank_total
9373 (&blank_t, orig_addr + isym->st_size, 0) - ahead;
35c08157
KLC
9374 }
9375 }
9376 }
9377
9378 /* Now adjust the global symbols defined in this section. */
9379 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9380 - symtab_hdr->sh_info);
9381 sym_hashes = elf_sym_hashes (abfd);
9382 end_hashes = sym_hashes + symcount;
9383 blank_t = blank_head;
9384 for (; sym_hashes < end_hashes; sym_hashes++)
9385 {
9386 struct elf_link_hash_entry *sym_hash = *sym_hashes;
9387
9388 if ((sym_hash->root.type == bfd_link_hash_defined
9389 || sym_hash->root.type == bfd_link_hash_defweak)
9390 && sym_hash->root.u.def.section == sec)
9391 {
9392 if (sym_hash->root.u.def.value <= sec->size)
9393 {
9394 bfd_vma ahead;
9395 bfd_vma orig_addr = sym_hash->root.u.def.value;
9396
9397 ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9398 sym_hash->root.u.def.value -= ahead;
9399
9400 /* Adjust function size. */
9401 if (sym_hash->type == STT_FUNC)
1c8f6a4d
KLC
9402 sym_hash->size -=
9403 get_nds32_elf_blank_total
9404 (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
35c08157
KLC
9405
9406 }
9407 }
9408 }
9409
9410 contents = elf_section_data (sec)->this_hdr.contents;
9411 blank_t = blank_head;
9412 while (blank_t->next)
9413 {
9414 /* Actually delete the bytes. */
9415
9416 /* If current blank is the last blank overlap with current section,
9417 go to finish process. */
9418 if (sec->size <= (blank_t->next->offset))
9419 break;
9420
9421 memmove (contents + blank_t->offset - blank_t->total_size,
9422 contents + blank_t->offset + blank_t->size,
9423 blank_t->next->offset - (blank_t->offset + blank_t->size));
9424
9425 blank_t = blank_t->next;
9426 }
9427
9428 if (sec->size > (blank_t->offset + blank_t->size))
9429 {
9430 /* There are remaining code between blank and section boundary.
9431 Move the remaining code to appropriate location. */
9432 memmove (contents + blank_t->offset - blank_t->total_size,
9433 contents + blank_t->offset + blank_t->size,
9434 sec->size - (blank_t->offset + blank_t->size));
9435 sec->size -= blank_t->total_size + blank_t->size;
9436 }
9437 else
9438 /* This blank is not entirely included in the section,
9439 reduce the section size by only part of the blank size. */
9440 sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9441
9442 while (blank_head)
9443 {
9444 blank_t = blank_head;
9445 blank_head = blank_head->next;
9446 remove_nds32_elf_blank (blank_t);
9447 }
9448
9449 return TRUE;
9450}
9451
9452/* Get the contents of a section. */
9453
9454static int
0c4bd9d9
KLC
9455nds32_get_section_contents (bfd *abfd, asection *sec,
9456 bfd_byte **contents_p, bfd_boolean cache)
35c08157
KLC
9457{
9458 /* Get the section contents. */
9459 if (elf_section_data (sec)->this_hdr.contents != NULL)
9460 *contents_p = elf_section_data (sec)->this_hdr.contents;
9461 else
9462 {
9463 if (!bfd_malloc_and_get_section (abfd, sec, contents_p))
9464 return FALSE;
0c4bd9d9
KLC
9465 if (cache)
9466 elf_section_data (sec)->this_hdr.contents = *contents_p;
35c08157
KLC
9467 }
9468
9469 return TRUE;
9470}
9471
9472/* Get the contents of the internal symbol of abfd. */
9473
9474static int
9475nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9476 Elf_Internal_Sym **isymbuf_p)
9477{
9478 Elf_Internal_Shdr *symtab_hdr;
9479 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9480
9481 /* Read this BFD's local symbols if we haven't done so already. */
9482 if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9483 {
9484 *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9485 if (*isymbuf_p == NULL)
9486 {
9487 *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9488 symtab_hdr->sh_info, 0,
9489 NULL, NULL, NULL);
9490 if (*isymbuf_p == NULL)
9491 return FALSE;
9492 }
9493 }
9494 symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9495
9496 return TRUE;
9497}
9498
9499/* Range of small data. */
1c8f6a4d
KLC
9500static bfd_vma sdata_range[2][2];
9501static bfd_vma const sdata_init_range[2] =
9502{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
35c08157
KLC
9503
9504static int
9505nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9506 bfd_byte *contents, bfd_vma addr)
9507{
9508 unsigned long insn = bfd_getb32 (contents + addr);
9509
9510 if (insn & 0x80000000)
9511 return 2;
9512
9513 return 4;
9514}
9515
9516/* Set the gp relax range. We have to measure the safe range
9517 to do gp relaxation. */
9518
9519static void
9520relax_range_measurement (bfd *abfd)
9521{
9522 asection *sec_f, *sec_b;
9523 /* For upper bound. */
9524 bfd_vma maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9525 bfd_vma align;
35c08157
KLC
9526 static int decide_relax_range = 0;
9527 int i;
fbaf61ad 9528 int range_number = ARRAY_SIZE (sdata_init_range);
35c08157
KLC
9529
9530 if (decide_relax_range)
9531 return;
9532 decide_relax_range = 1;
9533
9534 if (sda_rela_sec == NULL)
9535 {
9536 /* Since there is no data sections, we assume the range is page size. */
1c8f6a4d 9537 for (i = 0; i < range_number; i++)
35c08157
KLC
9538 {
9539 sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9540 sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9541 }
9542 return;
9543 }
9544
9545 /* Get the biggest alignment power after the gp located section. */
9546 sec_f = sda_rela_sec->output_section;
9547 sec_b = sec_f->next;
9548 align = 0;
9549 while (sec_b != NULL)
9550 {
9551 if ((unsigned)(1 << sec_b->alignment_power) > align)
9552 align = (1 << sec_b->alignment_power);
9553 sec_b = sec_b->next;
9554 }
9555
9556 /* I guess we can not determine the section before
9557 gp located section, so we assume the align is max page size. */
1c8f6a4d 9558 for (i = 0; i < range_number; i++)
35c08157 9559 {
1c8f6a4d 9560 sdata_range[i][1] = sdata_init_range[i] - align;
35c08157 9561 BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
1c8f6a4d 9562 sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
35c08157
KLC
9563 BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9564 }
9565}
9566
9567/* These are macros used to check flags encoded in r_addend.
9568 They are only used by nds32_elf_relax_section (). */
9569#define GET_SEQ_LEN(addend) ((addend) & 0x000000ff)
9570#define IS_1ST_CONVERT(addend) ((addend) & 0x80000000)
9571#define IS_OPTIMIZE(addend) ((addend) & 0x40000000)
9572#define IS_16BIT_ON(addend) ((addend) & 0x20000000)
9573
695344c0
NC
9574static const char * unrecognized_reloc_msg =
9575 /* xgettext:c-format */
2dcf00ce 9576 N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
695344c0 9577
1c8f6a4d
KLC
9578/* Relax LONGCALL1 relocation for nds32_elf_relax_section. */
9579
35c08157 9580static bfd_boolean
1c8f6a4d
KLC
9581nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9582 Elf_Internal_Rela *internal_relocs, int *insn_len,
9583 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9584 Elf_Internal_Shdr *symtab_hdr)
9585{
9586 /* There are 3 variations for LONGCALL1
9587 case 4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8 9588 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9589 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9590 jral5 ta ;
1c8f6a4d
KLC
9591
9592 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8 9593 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9594 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9595 jral ta ;
1c8f6a4d
KLC
9596
9597 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8 9598 sethi ta, hi20(symbol) ; LONGCALL1/HI20
1c8f6a4d 9599 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9600 jral ta ;
1c8f6a4d
KLC
9601 Check code for -mlong-calls output. */
9602
9603 /* Get the reloc for the address from which the register is
9604 being loaded. This reloc will tell us which function is
9605 actually being called. */
9606
9607 bfd_vma laddr;
9608 int seq_len; /* Original length of instruction sequence. */
35c08157 9609 uint32_t insn;
1c8f6a4d 9610 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d 9611 bfd_signed_vma foff;
35c08157 9612 uint16_t insn16;
35c08157 9613
1c8f6a4d
KLC
9614 irelend = internal_relocs + sec->reloc_count;
9615 seq_len = GET_SEQ_LEN (irel->r_addend);
9616 laddr = irel->r_offset;
9617 *insn_len = seq_len;
35c08157 9618
1c8f6a4d
KLC
9619 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9620 R_NDS32_HI20_RELA, laddr);
9621 lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9622 R_NDS32_LO12S0_ORI_RELA,
9623 laddr + 4);
35c08157 9624
1c8f6a4d 9625 if (hi_irelfn == irelend || lo_irelfn == irelend)
35c08157 9626 {
695344c0 9627 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
2dcf00ce 9628 (uint64_t) irel->r_offset);
1c8f6a4d 9629 return FALSE;
35c08157
KLC
9630 }
9631
1c8f6a4d 9632 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9633 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9634
1c8f6a4d 9635 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
9636 if (foff == 0
9637 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9638 || foff >= CONSERVATIVE_24BIT_S1)
9639 return FALSE;
35c08157 9640
fbaf61ad 9641 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
9642 /* For simplicity of coding, we are going to modify the section
9643 contents, the section relocs, and the BFD symbol table. We
9644 must tell the rest of the code not to free up this
9645 information. It would be possible to instead create a table
9646 of changes which have to be made, as is done in coff-mips.c;
9647 that would be more work, but would require less memory when
9648 the linker is run. */
9649
9650 /* Replace the long call with a jal. */
9651 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9652 R_NDS32_25_PCREL_RELA);
9653 irel->r_addend = hi_irelfn->r_addend;
9654
9655 /* We don't resolve this here but resolve it in relocate_section. */
9656 insn = INSN_JAL;
9657 bfd_putb32 (insn, contents + irel->r_offset);
9658
9659 hi_irelfn->r_info =
9660 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9661 lo_irelfn->r_info =
9662 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9663 *insn_len = 4;
9664
9665 if (seq_len & 0x2)
9666 {
9667 insn16 = NDS32_NOP16;
9668 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9669 lo_irelfn->r_info =
9670 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9671 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9672 *insn_len += 2;
9673 }
9674 return TRUE;
9675}
35c08157 9676
1c8f6a4d
KLC
9677#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9678/* Relax LONGCALL2 relocation for nds32_elf_relax_section. */
35c08157 9679
1c8f6a4d
KLC
9680static bfd_boolean
9681nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9682 Elf_Internal_Rela *internal_relocs, int *insn_len,
9683 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9684 Elf_Internal_Shdr *symtab_hdr)
9685{
9686 /* bltz rt, .L1 ; LONGCALL2
9687 jal symbol ; 25_PCREL
9688 .L1: */
35c08157 9689
1c8f6a4d
KLC
9690 /* Get the reloc for the address from which the register is
9691 being loaded. This reloc will tell us which function is
9692 actually being called. */
35c08157 9693
1c8f6a4d
KLC
9694 bfd_vma laddr;
9695 uint32_t insn;
9696 Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
1c8f6a4d 9697 bfd_signed_vma foff;
35c08157 9698
1c8f6a4d
KLC
9699 irelend = internal_relocs + sec->reloc_count;
9700 laddr = irel->r_offset;
9701 i1_irelfn =
9702 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9703 R_NDS32_25_PCREL_RELA, laddr + 4);
9704
9705 if (i1_irelfn == irelend)
35c08157 9706 {
695344c0 9707 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
2dcf00ce 9708 (uint64_t) irel->r_offset);
1c8f6a4d 9709 return FALSE;
35c08157
KLC
9710 }
9711
1c8f6a4d 9712 insn = bfd_getb32 (contents + laddr);
35c08157 9713
1c8f6a4d 9714 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9715 foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
35c08157 9716
fbaf61ad
NC
9717 if (foff == 0
9718 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
9719 || foff >= CONSERVATIVE_16BIT_S1)
9720 return FALSE;
35c08157 9721
1c8f6a4d
KLC
9722 /* Relax to bgezal rt, label ; 17_PCREL
9723 or bltzal rt, label ; 17_PCREL */
9724
9725 /* Convert to complimentary conditional call. */
9726 insn = CONVERT_CONDITION_CALL (insn);
9727
9728 /* For simplicity of coding, we are going to modify the section
9729 contents, the section relocs, and the BFD symbol table. We
9730 must tell the rest of the code not to free up this
9731 information. It would be possible to instead create a table
9732 of changes which have to be made, as is done in coff-mips.c;
9733 that would be more work, but would require less memory when
9734 the linker is run. */
9735
9736 /* Clean unnessary relocations. */
9737 i1_irelfn->r_info =
9738 ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9739 cond_irelfn =
9740 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9741 R_NDS32_17_PCREL_RELA, laddr);
9742 if (cond_irelfn != irelend)
9743 cond_irelfn->r_info =
9744 ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9745
9746 /* Replace the long call with a bgezal. */
9747 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9748 R_NDS32_17_PCREL_RELA);
9749 irel->r_addend = i1_irelfn->r_addend;
9750
9751 bfd_putb32 (insn, contents + irel->r_offset);
9752
9753 *insn_len = 4;
9754 return TRUE;
9755}
35c08157 9756
1c8f6a4d
KLC
9757/* Relax LONGCALL3 relocation for nds32_elf_relax_section. */
9758
9759static bfd_boolean
9760nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9761 Elf_Internal_Rela *internal_relocs, int *insn_len,
9762 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9763 Elf_Internal_Shdr *symtab_hdr)
9764{
9765 /* There are 3 variations for LONGCALL3
9766 case 4-4-4-2; 16-bit on, optimize off or optimize for space
07d6d2b8
AM
9767 bltz rt, $1 ; LONGCALL3
9768 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9769 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9770 jral5 ta ;
1c8f6a4d
KLC
9771 $1
9772
9773 case 4-4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
9774 bltz rt, $1 ; LONGCALL3
9775 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9776 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9777 jral ta ;
1c8f6a4d
KLC
9778 $1
9779
9780 case 4-4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
9781 bltz rt, $1 ; LONGCALL3
9782 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 9783 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8 9784 jral ta ;
1c8f6a4d
KLC
9785 $1 */
9786
9787 /* Get the reloc for the address from which the register is
9788 being loaded. This reloc will tell us which function is
9789 actually being called. */
9790
9791 bfd_vma laddr;
9792 int seq_len; /* Original length of instruction sequence. */
9793 uint32_t insn;
9794 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
1c8f6a4d
KLC
9795 bfd_signed_vma foff;
9796 uint16_t insn16;
9797
9798 irelend = internal_relocs + sec->reloc_count;
9799 seq_len = GET_SEQ_LEN (irel->r_addend);
9800 laddr = irel->r_offset;
9801 *insn_len = seq_len;
9802
9803 hi_irelfn =
9804 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9805 R_NDS32_HI20_RELA, laddr + 4);
9806 lo_irelfn =
9807 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9808 R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9809
9810 if (hi_irelfn == irelend || lo_irelfn == irelend)
9811 {
695344c0 9812 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
2dcf00ce 9813 (uint64_t) irel->r_offset);
1c8f6a4d 9814 return FALSE;
35c08157
KLC
9815 }
9816
1c8f6a4d 9817 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9818 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9819
fbaf61ad
NC
9820 if (foff == 0
9821 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9822 || foff >= CONSERVATIVE_24BIT_S1)
9823 return FALSE;
35c08157 9824
1c8f6a4d
KLC
9825 insn = bfd_getb32 (contents + laddr);
9826 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
35c08157 9827 {
1c8f6a4d
KLC
9828 /* Relax to bgezal rt, label ; 17_PCREL
9829 or bltzal rt, label ; 17_PCREL */
35c08157 9830
1c8f6a4d
KLC
9831 /* Convert to complimentary conditional call. */
9832 insn = CONVERT_CONDITION_CALL (insn);
9833 bfd_putb32 (insn, contents + irel->r_offset);
35c08157 9834
1c8f6a4d
KLC
9835 *insn_len = 4;
9836 irel->r_info =
9837 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9838 hi_irelfn->r_info =
9839 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9840 lo_irelfn->r_info =
9841 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9842
9843 cond_irelfn =
9844 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9845 R_NDS32_17_PCREL_RELA, laddr);
9846 if (cond_irelfn != irelend)
35c08157 9847 {
1c8f6a4d
KLC
9848 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9849 R_NDS32_17_PCREL_RELA);
9850 cond_irelfn->r_addend = hi_irelfn->r_addend;
35c08157 9851 }
1c8f6a4d
KLC
9852
9853 if (seq_len & 0x2)
35c08157 9854 {
1c8f6a4d
KLC
9855 insn16 = NDS32_NOP16;
9856 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9857 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9858 R_NDS32_INSN16);
9859 hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9860 insn_len += 2;
35c08157 9861 }
1c8f6a4d
KLC
9862 }
9863 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9864 {
9865 /* Relax to the following instruction sequence
9866 bltz rt, $1 ; LONGCALL2
9867 jal symbol ; 25_PCREL
9868 $1 */
9869 *insn_len = 8;
9870 insn = INSN_JAL;
9871 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
35c08157 9872
1c8f6a4d
KLC
9873 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9874 R_NDS32_25_PCREL_RELA);
9875 irel->r_info =
9876 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9877
9878 lo_irelfn->r_info =
9879 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 9880
1c8f6a4d 9881 if (seq_len & 0x2)
35c08157 9882 {
1c8f6a4d
KLC
9883 insn16 = NDS32_NOP16;
9884 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9885 lo_irelfn->r_info =
9886 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9887 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9888 insn_len += 2;
9889 }
9890 }
9891 return TRUE;
9892}
35c08157 9893
1c8f6a4d 9894/* Relax LONGJUMP1 relocation for nds32_elf_relax_section. */
35c08157 9895
1c8f6a4d
KLC
9896static bfd_boolean
9897nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9898 Elf_Internal_Rela *internal_relocs, int *insn_len,
9899 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9900 Elf_Internal_Shdr *symtab_hdr)
9901{
9902 /* There are 3 variations for LONGJUMP1
9903 case 4-4-2; 16-bit bit on, optimize off or optimize for space
07d6d2b8
AM
9904 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9905 ori ta, ta, lo12(symbol) ; LO12S0
9906 jr5 ta ;
1c8f6a4d
KLC
9907
9908 case 4-4-4; 16-bit off, optimize don't care
07d6d2b8
AM
9909 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9910 ori ta, ta, lo12(symbol) ; LO12S0
9911 jr ta ;
1c8f6a4d
KLC
9912
9913 case 4-4-4; 16-bit on, optimize for speed
07d6d2b8
AM
9914 sethi ta, hi20(symbol) ; LONGJUMP1/HI20
9915 ori ta, ta, lo12(symbol) ; LO12S0
9916 jr ta ; */
1c8f6a4d
KLC
9917
9918 /* Get the reloc for the address from which the register is
9919 being loaded. This reloc will tell us which function is
9920 actually being called. */
9921
9922 bfd_vma laddr;
9923 int seq_len; /* Original length of instruction sequence. */
9924 int insn16_on; /* 16-bit on/off. */
9925 uint32_t insn;
9926 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
1c8f6a4d
KLC
9927 bfd_signed_vma foff;
9928 uint16_t insn16;
9929 unsigned long reloc;
35c08157 9930
1c8f6a4d
KLC
9931 irelend = internal_relocs + sec->reloc_count;
9932 seq_len = GET_SEQ_LEN (irel->r_addend);
9933 laddr = irel->r_offset;
9934 *insn_len = seq_len;
9935 insn16_on = IS_16BIT_ON (irel->r_addend);
9936
9937 hi_irelfn =
9938 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9939 R_NDS32_HI20_RELA, laddr);
9940 lo_irelfn =
9941 find_relocs_at_address_addr (irel, internal_relocs, irelend,
9942 R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9943 if (hi_irelfn == irelend || lo_irelfn == irelend)
9944 {
695344c0 9945 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
2dcf00ce 9946 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
9947 return FALSE;
9948 }
35c08157 9949
1c8f6a4d 9950 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 9951 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 9952
fbaf61ad
NC
9953 if (foff == 0
9954 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
9955 || foff < -CONSERVATIVE_24BIT_S1)
9956 return FALSE;
35c08157 9957
fbaf61ad
NC
9958 if (insn16_on
9959 && foff >= -ACCURATE_8BIT_S1
9960 && foff < ACCURATE_8BIT_S1
9961 && (seq_len & 0x2))
1c8f6a4d
KLC
9962 {
9963 /* j8 label */
9964 /* 16-bit on, but not optimized for speed. */
9965 reloc = R_NDS32_9_PCREL_RELA;
9966 insn16 = INSN_J8;
9967 bfd_putb16 (insn16, contents + irel->r_offset);
9968 *insn_len = 2;
9969 irel->r_info =
9970 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
9971 }
9972 else
9973 {
9974 /* j label */
9975 reloc = R_NDS32_25_PCREL_RELA;
9976 insn = INSN_J;
9977 bfd_putb32 (insn, contents + irel->r_offset);
9978 *insn_len = 4;
9979 irel->r_info =
9980 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
9981 irel->r_addend = 0;
9982 }
35c08157 9983
1c8f6a4d
KLC
9984 hi_irelfn->r_info =
9985 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
9986 lo_irelfn->r_info =
9987 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
35c08157 9988
1c8f6a4d
KLC
9989 if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
9990 {
9991 insn16 = NDS32_NOP16;
9992 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9993 lo_irelfn->r_info =
9994 ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
9995 R_NDS32_INSN16);
9996 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9997 *insn_len += 2;
9998 }
9999 return TRUE;
10000}
10001
10002/* Revert condition branch. This function does not check if the input
10003 instruction is condition branch or not. */
10004
10005static void
10006nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10007 uint16_t *re_insn16, uint32_t *re_insn)
10008{
10009 uint32_t comp_insn = 0;
10010 uint16_t comp_insn16 = 0;
10011
10012 if (insn)
10013 {
10014 if (N32_OP6 (insn) == N32_OP6_BR1)
10015 {
10016 /* beqs label. */
10017 comp_insn = (insn ^ 0x4000) & 0xffffc000;
10018 if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
35c08157 10019 {
1c8f6a4d
KLC
10020 /* Insn can be contracted to 16-bit implied r5. */
10021 comp_insn16 =
10022 (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10023 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
35c08157 10024 }
1c8f6a4d
KLC
10025 }
10026 else if (N32_OP6 (insn) == N32_OP6_BR3)
10027 {
10028 /* bnec $ta, imm11, label. */
10029 comp_insn = (insn ^ 0x80000) & 0xffffff00;
10030 }
10031 else
10032 {
10033 comp_insn = (insn ^ 0x10000) & 0xffffc000;
10034 if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10035 || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
35c08157 10036 {
1c8f6a4d 10037 if (N32_IS_RT3 (insn))
35c08157 10038 {
1c8f6a4d
KLC
10039 /* Insn can be contracted to 16-bit. */
10040 comp_insn16 =
10041 (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10042 comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10043 }
10044 else if (N32_RT5 (insn) == REG_R15)
10045 {
10046 /* Insn can be contracted to 16-bit. */
10047 comp_insn16 =
10048 (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
35c08157 10049 }
35c08157
KLC
10050 }
10051 }
1c8f6a4d
KLC
10052 }
10053 else
10054 {
10055 switch ((insn16 & 0xf000) >> 12)
35c08157 10056 {
1c8f6a4d
KLC
10057 case 0xc:
10058 /* beqz38 or bnez38 */
10059 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10060 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10061 comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10062 break;
35c08157 10063
1c8f6a4d
KLC
10064 case 0xd:
10065 /* beqs38 or bnes38 */
10066 comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10067 comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10068 comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10069 | (REG_R5 << 15);
10070 break;
35c08157 10071
1c8f6a4d
KLC
10072 case 0xe:
10073 /* beqzS8 or bnezS8 */
10074 comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10075 comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10076 comp_insn |= REG_R15 << 20;
10077 break;
35c08157 10078
1c8f6a4d
KLC
10079 default:
10080 break;
10081 }
10082 }
10083 if (comp_insn && re_insn)
10084 *re_insn = comp_insn;
10085 if (comp_insn16 && re_insn16)
10086 *re_insn16 = comp_insn16;
10087}
35c08157 10088
1c8f6a4d 10089/* Relax LONGJUMP2 relocation for nds32_elf_relax_section. */
35c08157 10090
1c8f6a4d
KLC
10091static bfd_boolean
10092nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10093 Elf_Internal_Rela *internal_relocs, int *insn_len,
10094 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10095 Elf_Internal_Shdr *symtab_hdr)
10096{
10097 /* There are 3 variations for LONGJUMP2
10098 case 2-4; 1st insn convertible, 16-bit on,
10099 optimize off or optimize for space
10100 bnes38 rt, ra, $1 ; LONGJUMP2
10101 j label ; 25_PCREL
10102 $1:
10103
10104 case 4-4; 1st insn not convertible
10105 bne rt, ra, $1 ; LONGJUMP2
10106 j label ; 25_PCREL
10107 $1:
10108
10109 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10110 bne rt, ra, $1 ; LONGJUMP2
10111 j label ; 25_PCREL
10112 $1: */
10113
10114 /* Get the reloc for the address from which the register is
10115 being loaded. This reloc will tell us which function is
10116 actually being called. */
10117
10118 bfd_vma laddr;
10119 int seq_len; /* Original length of instruction sequence. */
10120 Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10121 int first_size;
1c8f6a4d
KLC
10122 unsigned int i;
10123 bfd_signed_vma foff;
10124 uint32_t insn, re_insn = 0;
10125 uint16_t insn16, re_insn16 = 0;
10126 unsigned long reloc, cond_reloc;
35c08157 10127
1c8f6a4d
KLC
10128 enum elf_nds32_reloc_type checked_types[] =
10129 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
35c08157 10130
1c8f6a4d
KLC
10131 irelend = internal_relocs + sec->reloc_count;
10132 seq_len = GET_SEQ_LEN (irel->r_addend);
10133 laddr = irel->r_offset;
10134 *insn_len = seq_len;
10135 first_size = (seq_len == 6) ? 2 : 4;
10136
10137 i2_irelfn =
10138 find_relocs_at_address_addr (irel, internal_relocs,
10139 irelend, R_NDS32_25_PCREL_RELA,
10140 laddr + first_size);
10141
fbaf61ad 10142 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10143 {
10144 cond_irelfn =
10145 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10146 checked_types[i], laddr);
10147 if (cond_irelfn != irelend)
10148 break;
10149 }
35c08157 10150
1c8f6a4d
KLC
10151 if (i2_irelfn == irelend || cond_irelfn == irelend)
10152 {
695344c0 10153 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
2dcf00ce 10154 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10155 return FALSE;
10156 }
35c08157 10157
1c8f6a4d 10158 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad
NC
10159 foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10160 if (foff == 0
10161 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10162 || foff >= CONSERVATIVE_16BIT_S1)
10163 return FALSE;
35c08157 10164
1c8f6a4d
KLC
10165 /* Get the all corresponding instructions. */
10166 if (first_size == 4)
10167 {
10168 insn = bfd_getb32 (contents + laddr);
10169 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10170 }
10171 else
10172 {
10173 insn16 = bfd_getb16 (contents + laddr);
10174 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10175 }
35c08157 10176
1c8f6a4d
KLC
10177 if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10178 && foff < ACCURATE_8BIT_S1 - first_size)
10179 {
10180 if (first_size == 4)
10181 {
10182 /* Don't convert it to 16-bit now, keep this as relaxable for
10183 ``label reloc; INSN16''. */
35c08157 10184
1c8f6a4d
KLC
10185 /* Save comp_insn32 to buffer. */
10186 bfd_putb32 (re_insn, contents + irel->r_offset);
10187 *insn_len = 4;
10188 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10189 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10190 cond_reloc = R_NDS32_INSN16;
10191 }
10192 else
10193 {
10194 bfd_putb16 (re_insn16, contents + irel->r_offset);
10195 *insn_len = 2;
10196 reloc = R_NDS32_9_PCREL_RELA;
10197 cond_reloc = R_NDS32_NONE;
10198 }
10199 }
10200 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10201 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10202 && foff < ACCURATE_14BIT_S1 - first_size))
10203 {
10204 /* beqs label ; 15_PCREL */
10205 bfd_putb32 (re_insn, contents + irel->r_offset);
10206 *insn_len = 4;
10207 reloc = R_NDS32_15_PCREL_RELA;
10208 cond_reloc = R_NDS32_NONE;
10209 }
10210 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10211 && foff >= -CONSERVATIVE_16BIT_S1
10212 && foff < CONSERVATIVE_16BIT_S1)
10213 {
10214 /* beqz label ; 17_PCREL */
10215 bfd_putb32 (re_insn, contents + irel->r_offset);
10216 *insn_len = 4;
10217 reloc = R_NDS32_17_PCREL_RELA;
10218 cond_reloc = R_NDS32_NONE;
10219 }
10220 else
10221 return FALSE;
35c08157 10222
1c8f6a4d
KLC
10223 /* Set all relocations. */
10224 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10225 irel->r_addend = i2_irelfn->r_addend;
35c08157 10226
1c8f6a4d
KLC
10227 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10228 cond_reloc);
10229 cond_irelfn->r_addend = 0;
35c08157 10230
1c8f6a4d
KLC
10231 if ((seq_len ^ *insn_len ) & 0x2)
10232 {
10233 insn16 = NDS32_NOP16;
10234 bfd_putb16 (insn16, contents + irel->r_offset + 4);
10235 i2_irelfn->r_offset = 4;
10236 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10237 R_NDS32_INSN16);
10238 i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10239 *insn_len += 2;
10240 }
10241 else
10242 i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10243 R_NDS32_NONE);
10244 return TRUE;
10245}
35c08157 10246
1c8f6a4d 10247/* Relax LONGJUMP3 relocation for nds32_elf_relax_section. */
35c08157 10248
1c8f6a4d
KLC
10249static bfd_boolean
10250nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10251 Elf_Internal_Rela *internal_relocs, int *insn_len,
10252 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10253 Elf_Internal_Shdr *symtab_hdr)
10254{
10255 /* There are 5 variations for LONGJUMP3
10256 case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10257 optimize off or optimize for space
07d6d2b8
AM
10258 bnes38 rt, ra, $1 ; LONGJUMP3
10259 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10260 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10261 jr5 ta ;
10262 $1: ;
1c8f6a4d
KLC
10263
10264 case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
07d6d2b8
AM
10265 bnes38 rt, ra, $1 ; LONGJUMP3
10266 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10267 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10268 jr5 ta ;
10269 $1: ; LABEL
1c8f6a4d
KLC
10270
10271 case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10272 optimize off or optimize for space
07d6d2b8
AM
10273 bne rt, ra, $1 ; LONGJUMP3
10274 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10275 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10276 jr5 ta ;
10277 $1: ;
1c8f6a4d
KLC
10278
10279 case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10280 16-bit off if no INSN16
07d6d2b8
AM
10281 bne rt, ra, $1 ; LONGJUMP3
10282 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10283 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10284 jr ta ;
10285 $1: ;
1c8f6a4d
KLC
10286
10287 case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10288 16-bit off if no INSN16
07d6d2b8
AM
10289 bne rt, ra, $1 ; LONGJUMP3
10290 sethi ta, hi20(symbol) ; HI20
1c8f6a4d 10291 ori ta, ta, lo12(symbol) ; LO12S0
07d6d2b8
AM
10292 jr ta ;
10293 $1: ; LABEL */
1c8f6a4d
KLC
10294
10295 /* Get the reloc for the address from which the register is
10296 being loaded. This reloc will tell us which function is
10297 actually being called. */
10298 enum elf_nds32_reloc_type checked_types[] =
10299 { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10300
10301 int reloc_off = 0, cond_removed = 0, convertible;
10302 bfd_vma laddr;
10303 int seq_len; /* Original length of instruction sequence. */
10304 Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
fbaf61ad 10305 int first_size;
1c8f6a4d
KLC
10306 unsigned int i;
10307 bfd_signed_vma foff;
10308 uint32_t insn, re_insn = 0;
10309 uint16_t insn16, re_insn16 = 0;
10310 unsigned long reloc, cond_reloc;
35c08157 10311
1c8f6a4d
KLC
10312 irelend = internal_relocs + sec->reloc_count;
10313 seq_len = GET_SEQ_LEN (irel->r_addend);
10314 laddr = irel->r_offset;
10315 *insn_len = seq_len;
35c08157 10316
1c8f6a4d 10317 convertible = IS_1ST_CONVERT (irel->r_addend);
35c08157 10318
1c8f6a4d
KLC
10319 if (convertible)
10320 first_size = 2;
10321 else
10322 first_size = 4;
10323
10324 /* Get all needed relocations. */
10325 hi_irelfn =
10326 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10327 R_NDS32_HI20_RELA, laddr + first_size);
10328 lo_irelfn =
10329 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10330 R_NDS32_LO12S0_ORI_RELA,
10331 laddr + first_size + 4);
10332
fbaf61ad 10333 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10334 {
10335 cond_irelfn =
10336 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10337 checked_types[i], laddr);
10338 if (cond_irelfn != irelend)
10339 break;
10340 }
35c08157 10341
fbaf61ad
NC
10342 if (hi_irelfn == irelend
10343 || lo_irelfn == irelend
10344 || cond_irelfn == irelend)
1c8f6a4d 10345 {
695344c0 10346 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
2dcf00ce 10347 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10348 return FALSE;
10349 }
35c08157 10350
1c8f6a4d 10351 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10352 foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
35c08157 10353
fbaf61ad
NC
10354 if (foff == 0
10355 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10356 || foff >= CONSERVATIVE_24BIT_S1)
10357 return FALSE;
35c08157 10358
1c8f6a4d
KLC
10359 /* Get the all corresponding instructions. */
10360 if (first_size == 4)
10361 {
10362 insn = bfd_getb32 (contents + laddr);
10363 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10364 }
10365 else
10366 {
10367 insn16 = bfd_getb16 (contents + laddr);
10368 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10369 }
35c08157 10370
1c8f6a4d
KLC
10371 /* For simplicity of coding, we are going to modify the section
10372 contents, the section relocs, and the BFD symbol table. We
10373 must tell the rest of the code not to free up this
10374 information. It would be possible to instead create a table
10375 of changes which have to be made, as is done in coff-mips.c;
10376 that would be more work, but would require less memory when
10377 the linker is run. */
35c08157 10378
fbaf61ad
NC
10379 if (re_insn16
10380 && foff >= -ACCURATE_8BIT_S1 - first_size
1c8f6a4d
KLC
10381 && foff < ACCURATE_8BIT_S1 - first_size)
10382 {
10383 if (!(seq_len & 0x2))
10384 {
10385 /* Don't convert it to 16-bit now, keep this as relaxable
10386 for ``label reloc; INSN1a''6. */
10387 /* Save comp_insn32 to buffer. */
10388 bfd_putb32 (re_insn, contents + irel->r_offset);
10389 *insn_len = 4;
10390 reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10391 R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10392 cond_reloc = R_NDS32_INSN16;
10393 }
10394 else
10395 {
10396 /* Not optimize for speed; convert sequence to 16-bit. */
10397 /* Save comp_insn16 to buffer. */
10398 bfd_putb16 (re_insn16, contents + irel->r_offset);
10399 *insn_len = 2;
10400 reloc = R_NDS32_9_PCREL_RELA;
10401 cond_reloc = R_NDS32_NONE;
10402 }
10403 cond_removed = 1;
10404 }
10405 else if (N32_OP6 (re_insn) == N32_OP6_BR1
10406 && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10407 && foff < ACCURATE_14BIT_S1 - first_size))
10408 {
10409 /* beqs label ; 15_PCREL */
10410 bfd_putb32 (re_insn, contents + irel->r_offset);
10411 *insn_len = 4;
10412 reloc = R_NDS32_15_PCREL_RELA;
10413 cond_reloc = R_NDS32_NONE;
10414 cond_removed = 1;
10415 }
10416 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10417 && foff >= -CONSERVATIVE_16BIT_S1
10418 && foff < CONSERVATIVE_16BIT_S1)
10419 {
10420 /* beqz label ; 17_PCREL */
10421 bfd_putb32 (re_insn, contents + irel->r_offset);
10422 *insn_len = 4;
10423 reloc = R_NDS32_17_PCREL_RELA;
10424 cond_reloc = R_NDS32_NONE;
10425 cond_removed = 1;
10426 }
10427 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10428 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10429 {
10430 /* Relax to one of the following 3 variations
10431
10432 case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10433 for space
10434 bnes38 rt, $1 ; LONGJUMP2
10435 j label ; 25_PCREL
10436 $1
10437
10438 case 4-4; 1st insn not convertible, others don't care
10439 bne rt, ra, $1 ; LONGJUMP2
10440 j label ; 25_PCREL
10441 $1
10442
10443 case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10444 bne rt, ra, $1 ; LONGJUMP2
10445 j label ; 25_PCREL
10446 $1 */
10447
10448 /* Offset for first instruction. */
10449
10450 /* Use j label as second instruction. */
10451 *insn_len = 4 + first_size;
10452 insn = INSN_J;
10453 bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10454 reloc = R_NDS32_LONGJUMP2;
10455 cond_reloc = R_NDS32_25_PLTREL;
10456 }
10457 else
10458 return FALSE;
35c08157 10459
1c8f6a4d
KLC
10460 if (cond_removed == 1)
10461 {
10462 /* Set all relocations. */
10463 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10464 irel->r_addend = hi_irelfn->r_addend;
10465
10466 cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10467 cond_reloc);
10468 cond_irelfn->r_addend = 0;
10469 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10470 R_NDS32_NONE);
10471 }
10472 else
10473 {
10474 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
fbaf61ad 10475 irel->r_addend = irel->r_addend;
1c8f6a4d
KLC
10476 hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10477 cond_reloc);
10478 }
35c08157 10479
1c8f6a4d
KLC
10480 if ((seq_len ^ *insn_len ) & 0x2)
10481 {
10482 insn16 = NDS32_NOP16;
10483 bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10484 lo_irelfn->r_offset = *insn_len;
10485 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10486 R_NDS32_INSN16);
10487 lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10488 *insn_len += 2;
10489 }
10490 else
10491 lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10492 R_NDS32_NONE);
10493 return TRUE;
10494}
35c08157 10495
1c8f6a4d 10496/* Relax LONGCALL4 relocation for nds32_elf_relax_section. */
35c08157 10497
1c8f6a4d
KLC
10498static bfd_boolean
10499nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10500 Elf_Internal_Rela *internal_relocs, int *insn_len,
10501 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10502 Elf_Internal_Shdr *symtab_hdr)
10503{
10504 /* The pattern for LONGCALL4. Support for function cse.
10505 sethi ta, hi20(symbol) ; LONGCALL4/HI20
10506 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10507 jral ta ; PTR_RES/EMPTY/INSN16 */
35c08157 10508
1c8f6a4d
KLC
10509 bfd_vma laddr;
10510 uint32_t insn;
10511 Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10512 Elf_Internal_Rela *irelend;
1c8f6a4d 10513 bfd_signed_vma foff;
35c08157 10514
1c8f6a4d
KLC
10515 irelend = internal_relocs + sec->reloc_count;
10516 laddr = irel->r_offset;
35c08157 10517
1c8f6a4d
KLC
10518 /* Get the reloc for the address from which the register is
10519 being loaded. This reloc will tell us which function is
10520 actually being called. */
10521 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10522 R_NDS32_HI20_RELA, laddr);
35c08157 10523
1c8f6a4d
KLC
10524 if (hi_irel == irelend)
10525 {
695344c0 10526 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10527 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10528 return FALSE;
10529 }
35c08157 10530
1c8f6a4d 10531 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10532 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10533
1c8f6a4d 10534 /* This condition only happened when symbol is undefined. */
fbaf61ad
NC
10535 if (foff == 0
10536 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10537 || foff >= CONSERVATIVE_24BIT_S1)
10538 return FALSE;
35c08157 10539
fbaf61ad 10540 /* Relax to: jal symbol; 25_PCREL. */
1c8f6a4d
KLC
10541 /* For simplicity of coding, we are going to modify the section
10542 contents, the section relocs, and the BFD symbol table. We
10543 must tell the rest of the code not to free up this
10544 information. It would be possible to instead create a table
10545 of changes which have to be made, as is done in coff-mips.c;
10546 that would be more work, but would require less memory when
10547 the linker is run. */
35c08157 10548
1c8f6a4d
KLC
10549 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10550 R_NDS32_PTR_RESOLVED, irel->r_addend);
10551 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10552 R_NDS32_EMPTY, irel->r_addend);
35c08157 10553
1c8f6a4d
KLC
10554 if (ptr_irel == irelend || em_irel == irelend)
10555 {
695344c0 10556 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
2dcf00ce 10557 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10558 return FALSE;
10559 }
10560 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10561 insn = bfd_getb32 (contents + irel->r_addend);
10562 if (insn & 0x80000000)
10563 return FALSE;
35c08157 10564
1c8f6a4d
KLC
10565 /* Replace the long call with a jal. */
10566 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10567 R_NDS32_25_PCREL_RELA);
10568 ptr_irel->r_addend = 1;
35c08157 10569
1c8f6a4d
KLC
10570 /* We don't resolve this here but resolve it in relocate_section. */
10571 insn = INSN_JAL;
10572 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10573
1c8f6a4d
KLC
10574 irel->r_info =
10575 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10576
1c8f6a4d
KLC
10577 /* If there is function cse, HI20 can not remove now. */
10578 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10579 R_NDS32_LONGCALL4, laddr);
10580 if (call_irel == irelend)
10581 {
10582 *insn_len = 0;
10583 hi_irel->r_info =
10584 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10585 }
35c08157 10586
1c8f6a4d
KLC
10587 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10588 R_NDS32_INSN16, irel->r_addend);
10589 if (insn_irel != irelend)
10590 insn_irel->r_info =
10591 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10592
1c8f6a4d
KLC
10593 return TRUE;
10594}
35c08157 10595
1c8f6a4d 10596/* Relax LONGCALL5 relocation for nds32_elf_relax_section. */
35c08157 10597
1c8f6a4d
KLC
10598static bfd_boolean
10599nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10600 Elf_Internal_Rela *internal_relocs, int *insn_len,
10601 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10602 Elf_Internal_Shdr *symtab_hdr)
10603{
10604 /* The pattern for LONGCALL5.
10605 bltz rt, .L1 ; LONGCALL5/17_PCREL
10606 jal symbol ; 25_PCREL
10607 .L1: */
35c08157 10608
1c8f6a4d
KLC
10609 bfd_vma laddr;
10610 uint32_t insn;
10611 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d 10612 bfd_signed_vma foff;
35c08157 10613
1c8f6a4d
KLC
10614 irelend = internal_relocs + sec->reloc_count;
10615 laddr = irel->r_offset;
10616 insn = bfd_getb32 (contents + laddr);
35c08157 10617
1c8f6a4d
KLC
10618 /* Get the reloc for the address from which the register is
10619 being loaded. This reloc will tell us which function is
10620 actually being called. */
10621 cond_irel =
10622 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10623 R_NDS32_25_PCREL_RELA, irel->r_addend);
10624 if (cond_irel == irelend)
10625 {
695344c0 10626 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
2dcf00ce 10627 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10628 return FALSE;
10629 }
35c08157 10630
1c8f6a4d 10631 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10632 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 10633
fbaf61ad
NC
10634 if (foff == 0
10635 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10636 || foff >= CONSERVATIVE_16BIT_S1)
10637 return FALSE;
35c08157 10638
1c8f6a4d 10639 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10640 or bltzal rt, label ; 17_PCREL. */
35c08157 10641
1c8f6a4d
KLC
10642 /* Convert to complimentary conditional call. */
10643 insn = CONVERT_CONDITION_CALL (insn);
35c08157 10644
1c8f6a4d
KLC
10645 /* For simplicity of coding, we are going to modify the section
10646 contents, the section relocs, and the BFD symbol table. We
10647 must tell the rest of the code not to free up this
10648 information. It would be possible to instead create a table
10649 of changes which have to be made, as is done in coff-mips.c;
10650 that would be more work, but would require less memory when
10651 the linker is run. */
35c08157 10652
1c8f6a4d
KLC
10653 /* Modify relocation and contents. */
10654 cond_irel->r_info =
10655 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
35c08157 10656
1c8f6a4d
KLC
10657 /* Replace the long call with a bgezal. */
10658 bfd_putb32 (insn, contents + cond_irel->r_offset);
10659 *insn_len = 0;
35c08157 10660
1c8f6a4d
KLC
10661 /* Clean unnessary relocations. */
10662 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10663
1c8f6a4d
KLC
10664 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10665 R_NDS32_17_PCREL_RELA, laddr);
10666 cond_irel->r_info =
10667 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10668
1c8f6a4d
KLC
10669 return TRUE;
10670}
35c08157 10671
1c8f6a4d 10672/* Relax LONGCALL6 relocation for nds32_elf_relax_section. */
35c08157 10673
1c8f6a4d
KLC
10674static bfd_boolean
10675nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10676 Elf_Internal_Rela *internal_relocs, int *insn_len,
10677 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10678 Elf_Internal_Shdr *symtab_hdr)
10679{
10680 /* The pattern for LONGCALL6.
10681 bltz rt, .L1 ; LONGCALL6/17_PCREL
10682 sethi ta, hi20(symbol) ; HI20/PTR
10683 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10684 jral ta ; PTR_RES/EMPTY/INSN16
10685 .L1 */
10686
10687 bfd_vma laddr;
10688 uint32_t insn;
10689 Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
1c8f6a4d 10690 bfd_signed_vma foff;
35c08157 10691
1c8f6a4d
KLC
10692 irelend = internal_relocs + sec->reloc_count;
10693 laddr = irel->r_offset;
35c08157 10694
1c8f6a4d
KLC
10695 /* Get the reloc for the address from which the register is
10696 being loaded. This reloc will tell us which function is
10697 actually being called. */
10698 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10699 R_NDS32_EMPTY, irel->r_addend);
35c08157 10700
1c8f6a4d
KLC
10701 if (em_irel == irelend)
10702 {
695344c0 10703 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
2dcf00ce 10704 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10705 return FALSE;
10706 }
35c08157 10707
1c8f6a4d 10708 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10709 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 10710
fbaf61ad
NC
10711 if (foff == 0
10712 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10713 || foff >= CONSERVATIVE_24BIT_S1)
10714 return FALSE;
35c08157 10715
1c8f6a4d
KLC
10716 /* Check these is enough space to insert jal in R_NDS32_EMPTY. */
10717 insn = bfd_getb32 (contents + irel->r_addend);
10718 if (insn & 0x80000000)
10719 return FALSE;
35c08157 10720
1c8f6a4d
KLC
10721 insn = bfd_getb32 (contents + laddr);
10722 if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10723 {
10724 /* Relax to bgezal rt, label ; 17_PCREL
fbaf61ad 10725 or bltzal rt, label ; 17_PCREL. */
35c08157 10726
1c8f6a4d
KLC
10727 /* Convert to complimentary conditional call. */
10728 *insn_len = 0;
10729 insn = CONVERT_CONDITION_CALL (insn);
10730 bfd_putb32 (insn, contents + em_irel->r_offset);
10731
10732 em_irel->r_info =
10733 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10734
10735 /* Set resolved relocation. */
10736 cond_irel =
10737 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10738 R_NDS32_PTR_RESOLVED, irel->r_addend);
10739 if (cond_irel == irelend)
35c08157 10740 {
695344c0 10741 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10742 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10743 return FALSE;
10744 }
10745 cond_irel->r_addend = 1;
35c08157 10746
1c8f6a4d 10747 /* Clear relocations. */
35c08157 10748
1c8f6a4d
KLC
10749 irel->r_info =
10750 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10751
1c8f6a4d
KLC
10752 cond_irel =
10753 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10754 R_NDS32_17_PCREL_RELA, laddr);
10755 if (cond_irel != irelend)
10756 cond_irel->r_info =
10757 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10758
1c8f6a4d
KLC
10759 cond_irel =
10760 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10761 R_NDS32_INSN16, irel->r_addend);
10762 if (cond_irel != irelend)
10763 cond_irel->r_info =
10764 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 10765
1c8f6a4d
KLC
10766 }
10767 else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10768 {
10769 /* Relax to the following instruction sequence
10770 bltz rt, .L1 ; LONGCALL2/17_PCREL
10771 jal symbol ; 25_PCREL/PTR_RES
10772 .L1 */
10773 *insn_len = 4;
10774 /* Convert instruction. */
10775 insn = INSN_JAL;
10776 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10777
1c8f6a4d
KLC
10778 /* Convert relocations. */
10779 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10780 R_NDS32_25_PCREL_RELA);
10781 irel->r_info =
10782 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
35c08157 10783
1c8f6a4d
KLC
10784 /* Set resolved relocation. */
10785 cond_irel =
10786 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10787 R_NDS32_PTR_RESOLVED, irel->r_addend);
10788 if (cond_irel == irelend)
10789 {
695344c0 10790 _bfd_error_handler (unrecognized_reloc_msg, abfd,
2dcf00ce 10791 "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10792 return FALSE;
10793 }
10794 cond_irel->r_addend = 1;
35c08157 10795
1c8f6a4d
KLC
10796 cond_irel =
10797 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10798 R_NDS32_INSN16, irel->r_addend);
10799 if (cond_irel != irelend)
10800 cond_irel->r_info =
10801 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10802 }
10803 return TRUE;
10804}
35c08157 10805
1c8f6a4d 10806/* Relax LONGJUMP4 relocation for nds32_elf_relax_section. */
35c08157 10807
1c8f6a4d
KLC
10808static bfd_boolean
10809nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10810 Elf_Internal_Rela *internal_relocs, int *insn_len,
10811 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10812 Elf_Internal_Shdr *symtab_hdr)
10813{
10814 /* The pattern for LONGJUMP4.
10815 sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10816 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10817 jr ta ; PTR_RES/INSN16/EMPTY */
10818
10819 bfd_vma laddr;
10820 int seq_len; /* Original length of instruction sequence. */
10821 uint32_t insn;
10822 Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
1c8f6a4d 10823 bfd_signed_vma foff;
35c08157 10824
1c8f6a4d
KLC
10825 irelend = internal_relocs + sec->reloc_count;
10826 seq_len = GET_SEQ_LEN (irel->r_addend);
10827 laddr = irel->r_offset;
10828 *insn_len = seq_len;
35c08157 10829
1c8f6a4d
KLC
10830 /* Get the reloc for the address from which the register is
10831 being loaded. This reloc will tell us which function is
10832 actually being called. */
35c08157 10833
1c8f6a4d
KLC
10834 hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10835 R_NDS32_HI20_RELA, laddr);
35c08157 10836
1c8f6a4d
KLC
10837 if (hi_irel == irelend)
10838 {
695344c0 10839 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 10840 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10841 return FALSE;
10842 }
35c08157 10843
1c8f6a4d 10844 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10845 foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
35c08157 10846
fbaf61ad
NC
10847 if (foff == 0
10848 || foff >= CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
10849 || foff < -CONSERVATIVE_24BIT_S1)
10850 return FALSE;
35c08157 10851
1c8f6a4d
KLC
10852 /* Convert it to "j label", it may be converted to j8 in the final
10853 pass of relaxation. Therefore, we do not consider this currently. */
10854 ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10855 R_NDS32_PTR_RESOLVED, irel->r_addend);
10856 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10857 R_NDS32_EMPTY, irel->r_addend);
35c08157 10858
1c8f6a4d
KLC
10859 if (ptr_irel == irelend || em_irel == irelend)
10860 {
695344c0 10861 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
2dcf00ce 10862 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10863 return FALSE;
10864 }
35c08157 10865
1c8f6a4d
KLC
10866 em_irel->r_info =
10867 ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10868 ptr_irel->r_addend = 1;
35c08157 10869
1c8f6a4d
KLC
10870 /* Write instruction. */
10871 insn = INSN_J;
10872 bfd_putb32 (insn, contents + em_irel->r_offset);
35c08157 10873
1c8f6a4d
KLC
10874 /* Clear relocations. */
10875 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 10876
1c8f6a4d
KLC
10877 /* If there is function cse, HI20 can not remove now. */
10878 call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10879 R_NDS32_LONGJUMP4, laddr);
10880 if (call_irel == irelend)
10881 {
10882 *insn_len = 0;
10883 hi_irel->r_info =
10884 ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10885 }
35c08157 10886
1c8f6a4d
KLC
10887 return TRUE;
10888}
35c08157 10889
1c8f6a4d 10890/* Relax LONGJUMP5 relocation for nds32_elf_relax_section. */
35c08157 10891
1c8f6a4d
KLC
10892static bfd_boolean
10893nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10894 Elf_Internal_Rela *internal_relocs, int *insn_len,
10895 int *seq_len, bfd_byte *contents,
10896 Elf_Internal_Sym *isymbuf,
10897 Elf_Internal_Shdr *symtab_hdr)
10898{
10899 /* There are 2 variations for LONGJUMP5
10900 case 2-4; 1st insn convertible, 16-bit on.
10901 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
10902 j label ; 25_PCREL/INSN16
10903 $1:
10904
10905 case 4-4; 1st insn not convertible
10906 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10907 j label ; 25_PCREL/INSN16
10908 .L1: */
10909
10910 bfd_vma laddr;
10911 Elf_Internal_Rela *cond_irel, *irelend;
1c8f6a4d
KLC
10912 unsigned int i;
10913 bfd_signed_vma foff;
10914 uint32_t insn, re_insn = 0;
10915 uint16_t insn16, re_insn16 = 0;
10916 unsigned long reloc;
35c08157 10917
1c8f6a4d
KLC
10918 enum elf_nds32_reloc_type checked_types[] =
10919 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10920 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
35c08157 10921
1c8f6a4d
KLC
10922 irelend = internal_relocs + sec->reloc_count;
10923 laddr = irel->r_offset;
35c08157 10924
1c8f6a4d
KLC
10925 /* Get the reloc for the address from which the register is
10926 being loaded. This reloc will tell us which function is
10927 actually being called. */
35c08157 10928
1c8f6a4d
KLC
10929 cond_irel =
10930 find_relocs_at_address_addr (irel, internal_relocs, irelend,
10931 R_NDS32_25_PCREL_RELA, irel->r_addend);
10932 if (cond_irel == irelend)
10933 {
695344c0 10934 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
2dcf00ce 10935 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
10936 return FALSE;
10937 }
35c08157 10938
1c8f6a4d 10939 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 10940 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 10941
fbaf61ad
NC
10942 if (foff == 0
10943 || foff < -CONSERVATIVE_16BIT_S1
1c8f6a4d
KLC
10944 || foff >= CONSERVATIVE_16BIT_S1)
10945 return FALSE;
35c08157 10946
1c8f6a4d
KLC
10947 /* Get the all corresponding instructions. */
10948 insn = bfd_getb32 (contents + laddr);
10949 /* Check instruction size. */
10950 if (insn & 0x80000000)
10951 {
10952 *seq_len = 0;
10953 insn16 = insn >> 16;
10954 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10955 }
10956 else
10957 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 10958
1c8f6a4d
KLC
10959 if (N32_OP6 (re_insn) == N32_OP6_BR1
10960 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
10961 {
10962 /* beqs label ; 15_PCREL. */
10963 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10964 reloc = R_NDS32_15_PCREL_RELA;
10965 }
10966 else if (N32_OP6 (re_insn) == N32_OP6_BR2
10967 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10968 {
10969 /* beqz label ; 17_PCREL. */
10970 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10971 reloc = R_NDS32_17_PCREL_RELA;
10972 }
10973 else if ( N32_OP6 (re_insn) == N32_OP6_BR3
10974 && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
10975 {
10976 /* beqc label ; 9_PCREL. */
10977 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
10978 reloc = R_NDS32_WORD_9_PCREL_RELA;
10979 }
10980 else
10981 return FALSE;
35c08157 10982
1c8f6a4d
KLC
10983 /* Set all relocations. */
10984 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
35c08157 10985
1c8f6a4d
KLC
10986 /* Clean relocations. */
10987 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
fbaf61ad 10988 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
10989 {
10990 cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10991 checked_types[i], laddr);
10992 if (cond_irel != irelend)
10993 {
10994 if (*seq_len == 0
10995 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
35c08157 10996 {
1c8f6a4d
KLC
10997 /* If the branch instruction is 2 byte, it cannot remove
10998 directly. Only convert it to nop16 and remove it after
10999 checking alignment issue. */
11000 insn16 = NDS32_NOP16;
11001 bfd_putb16 (insn16, contents + laddr);
11002 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157
KLC
11003 }
11004 else
1c8f6a4d
KLC
11005 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11006 R_NDS32_NONE);
35c08157 11007 }
1c8f6a4d
KLC
11008 }
11009 *insn_len = 0;
35c08157 11010
1c8f6a4d
KLC
11011 return TRUE;
11012}
35c08157 11013
1c8f6a4d 11014/* Relax LONGJUMP6 relocation for nds32_elf_relax_section. */
35c08157 11015
1c8f6a4d
KLC
11016static bfd_boolean
11017nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11018 Elf_Internal_Rela *internal_relocs, int *insn_len,
11019 int *seq_len, bfd_byte *contents,
11020 Elf_Internal_Sym *isymbuf,
11021 Elf_Internal_Shdr *symtab_hdr)
11022{
11023 /* There are 5 variations for LONGJUMP6
11024 case : 2-4-4-4; 1st insn convertible, 16-bit on.
11025 bnes38 rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11026 sethi ta, hi20(symbol) ; HI20/PTR
11027 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11028 jr ta ; PTR_RES/INSN16/EMPTY
11029 .L1:
11030
11031 case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11032 bne rt, ra, .L1 ; LONGJUMP6/15_PCREL/INSN16
11033 sethi ta, hi20(symbol) ; HI20/PTR
11034 ori ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11035 jr ta ; PTR_RES/INSN16/EMPTY
11036 .L1: */
11037
11038 enum elf_nds32_reloc_type checked_types[] =
11039 { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11040 R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11041
11042 int reloc_off = 0, cond_removed = 0;
11043 bfd_vma laddr;
11044 Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11045 unsigned int i;
11046 bfd_signed_vma foff;
11047 uint32_t insn, re_insn = 0;
11048 uint16_t insn16, re_insn16 = 0;
11049 unsigned long reloc;
35c08157 11050
1c8f6a4d
KLC
11051 irelend = internal_relocs + sec->reloc_count;
11052 laddr = irel->r_offset;
35c08157 11053
1c8f6a4d
KLC
11054 /* Get the reloc for the address from which the register is
11055 being loaded. This reloc will tell us which function is
11056 actually being called. */
11057 em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11058 R_NDS32_EMPTY, irel->r_addend);
35c08157 11059
1c8f6a4d
KLC
11060 if (em_irel == irelend)
11061 {
695344c0 11062 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
2dcf00ce 11063 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11064 return FALSE;
11065 }
35c08157 11066
1c8f6a4d 11067 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11068 foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
35c08157 11069
fbaf61ad
NC
11070 if (foff == 0
11071 || foff < -CONSERVATIVE_24BIT_S1
1c8f6a4d
KLC
11072 || foff >= CONSERVATIVE_24BIT_S1)
11073 return FALSE;
35c08157 11074
1c8f6a4d
KLC
11075 insn = bfd_getb32 (contents + laddr);
11076 /* Check instruction size. */
11077 if (insn & 0x80000000)
11078 {
11079 *seq_len = 0;
11080 insn16 = insn >> 16;
11081 nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11082 }
11083 else
11084 nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
35c08157 11085
1c8f6a4d
KLC
11086 /* For simplicity of coding, we are going to modify the section
11087 contents, the section relocs, and the BFD symbol table. We
11088 must tell the rest of the code not to free up this
11089 information. It would be possible to instead create a table
11090 of changes which have to be made, as is done in coff-mips.c;
11091 that would be more work, but would require less memory when
11092 the linker is run. */
35c08157 11093
1c8f6a4d
KLC
11094 if (N32_OP6 (re_insn) == N32_OP6_BR1
11095 && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11096 {
fbaf61ad 11097 /* beqs label ; 15_PCREL. */
1c8f6a4d
KLC
11098 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11099 reloc = R_NDS32_15_PCREL_RELA;
11100 cond_removed = 1;
11101 }
11102 else if (N32_OP6 (re_insn) == N32_OP6_BR2
11103 && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11104 {
fbaf61ad 11105 /* beqz label ; 17_PCREL. */
1c8f6a4d
KLC
11106 bfd_putb32 (re_insn, contents + em_irel->r_offset);
11107 reloc = R_NDS32_17_PCREL_RELA;
11108 cond_removed = 1;
11109 }
11110 else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11111 && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11112 {
11113 /* Relax to one of the following 2 variations
35c08157 11114
1c8f6a4d
KLC
11115 case 2-4; 1st insn convertible, 16-bit on.
11116 bnes38 rt, ra, .L1 ; LONGJUMP5/9_PCREL/INSN16
11117 j label ; 25_PCREL/INSN16
11118 $1:
35c08157 11119
1c8f6a4d
KLC
11120 case 4-4; 1st insn not convertible
11121 bne rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11122 j label ; 25_PCREL/INSN16
11123 .L1: */
35c08157 11124
1c8f6a4d
KLC
11125 /* Use j label as second instruction. */
11126 insn = INSN_J;
11127 reloc = R_NDS32_25_PCREL_RELA;
11128 bfd_putb32 (insn, contents + em_irel->r_offset);
11129 }
11130 else
11131 return FALSE;
35c08157 11132
1c8f6a4d
KLC
11133 /* Set all relocations. */
11134 em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
35c08157 11135
1c8f6a4d
KLC
11136 cond_irel =
11137 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11138 R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11139 cond_irel->r_addend = 1;
35c08157 11140
1c8f6a4d
KLC
11141 /* Use INSN16 of first branch instruction to distinguish if keeping
11142 INSN16 of final instruction or not. */
11143 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11144 R_NDS32_INSN16, irel->r_offset);
11145 if (insn_irel == irelend)
11146 {
11147 /* Clean the final INSN16. */
11148 insn_irel =
11149 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11150 R_NDS32_INSN16, em_irel->r_offset);
11151 insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11152 R_NDS32_NONE);
11153 }
11154
11155 if (cond_removed == 1)
11156 {
11157 *insn_len = 0;
11158
11159 /* Clear relocations. */
11160 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11161
fbaf61ad 11162 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11163 {
11164 cond_irel =
11165 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11166 checked_types[i], laddr);
11167 if (cond_irel != irelend)
35c08157 11168 {
1c8f6a4d
KLC
11169 if (*seq_len == 0
11170 && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11171 {
11172 /* If the branch instruction is 2 byte, it cannot remove
11173 directly. Only convert it to nop16 and remove it after
11174 checking alignment issue. */
11175 insn16 = NDS32_NOP16;
11176 bfd_putb16 (insn16, contents + laddr);
11177 cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11178 }
11179 else
11180 cond_irel->r_info =
11181 ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
35c08157 11182 }
35c08157 11183 }
1c8f6a4d
KLC
11184 }
11185 else
11186 {
11187 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11188 R_NDS32_LONGJUMP5);
11189 }
35c08157 11190
1c8f6a4d
KLC
11191 return TRUE;
11192}
35c08157 11193
1c8f6a4d 11194/* Relax LONGJUMP7 relocation for nds32_elf_relax_section. */
35c08157 11195
1c8f6a4d
KLC
11196static bfd_boolean
11197nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11198 Elf_Internal_Rela *internal_relocs, int *insn_len,
11199 int *seq_len, bfd_byte *contents,
11200 Elf_Internal_Sym *isymbuf,
11201 Elf_Internal_Shdr *symtab_hdr)
11202{
11203 /* There are 2 variations for LONGJUMP5
11204 case 2-4; 1st insn convertible, 16-bit on.
11205 movi55 ta, imm11 ; LONGJUMP7/INSN16
11206 beq rt, ta, label ; 15_PCREL
11207
11208 case 4-4; 1st insn not convertible
11209 movi55 ta, imm11 ; LONGJUMP7/INSN16
11210 beq rt, ta, label ; 15_PCREL */
11211
11212 bfd_vma laddr;
11213 Elf_Internal_Rela *cond_irel, *irelend, *insn_irel;
1c8f6a4d
KLC
11214 bfd_signed_vma foff;
11215 uint32_t insn, re_insn = 0;
11216 uint16_t insn16;
11217 uint32_t imm11;
35c08157 11218
1c8f6a4d
KLC
11219 irelend = internal_relocs + sec->reloc_count;
11220 laddr = irel->r_offset;
35c08157 11221
1c8f6a4d
KLC
11222 /* Get the reloc for the address from which the register is
11223 being loaded. This reloc will tell us which function is
11224 actually being called. */
35c08157 11225
1c8f6a4d
KLC
11226 cond_irel =
11227 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11228 R_NDS32_15_PCREL_RELA, irel->r_addend);
11229 if (cond_irel == irelend)
11230 {
695344c0 11231 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
2dcf00ce 11232 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11233 return FALSE;
11234 }
35c08157 11235
1c8f6a4d 11236 /* Get the value of the symbol referred to by the reloc. */
fbaf61ad 11237 foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
35c08157 11238
fbaf61ad
NC
11239 if (foff == 0
11240 || foff < -CONSERVATIVE_8BIT_S1
1c8f6a4d
KLC
11241 || foff >= CONSERVATIVE_8BIT_S1)
11242 return FALSE;
35c08157 11243
1c8f6a4d
KLC
11244 /* Get the first instruction for its size. */
11245 insn = bfd_getb32 (contents + laddr);
11246 if (insn & 0x80000000)
11247 {
11248 *seq_len = 0;
11249 /* Get the immediate from movi55. */
11250 imm11 = N16_IMM5S (insn >> 16);
11251 }
11252 else
11253 {
11254 /* Get the immediate from movi. */
11255 imm11 = N32_IMM20S (insn);
35c08157
KLC
11256 }
11257
1c8f6a4d
KLC
11258 /* Get the branch instruction. */
11259 insn = bfd_getb32 (contents + irel->r_addend);
11260 /* Convert instruction to BR3. */
11261 if ((insn >> 14) & 0x1)
11262 re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11263 else
11264 re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
35c08157 11265
1c8f6a4d 11266 bfd_putb32 (re_insn, contents + cond_irel->r_offset);
35c08157 11267
1c8f6a4d
KLC
11268 /* Set all relocations. */
11269 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11270 R_NDS32_WORD_9_PCREL_RELA);
11271
11272 /* Clean relocations. */
11273 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11274 insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11275 R_NDS32_INSN16, irel->r_offset);
11276 if (insn_irel != irelend)
11277 {
11278 if (*seq_len == 0)
35c08157 11279 {
1c8f6a4d
KLC
11280 /* If the first insntruction is 16bit, convert it to nop16. */
11281 insn16 = NDS32_NOP16;
11282 bfd_putb16 (insn16, contents + laddr);
11283 insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
35c08157 11284 }
1c8f6a4d
KLC
11285 else
11286 cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11287 R_NDS32_NONE);
35c08157 11288 }
1c8f6a4d 11289 *insn_len = 0;
35c08157 11290
1c8f6a4d
KLC
11291 return TRUE;
11292}
35c08157 11293
fbaf61ad
NC
11294/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11295 for each relax round. But the gp may changed dramatically and then cause
11296 the truncated to fit errors for the the converted gp instructions.
11297 Therefore, we must reserve the minimum but safe enough size to prevent it. */
11298
11299static bfd_boolean
11300nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11301 Elf_Internal_Rela *irel, bfd_boolean *again,
11302 bfd_boolean init,
11303 struct elf_nds32_link_hash_table *table,
11304 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11305
11306{
11307 int offset_to_gp;
11308 static bfd_boolean sec_pass = FALSE;
11309 static asection *first_sec = NULL, *sym_sec;
11310 /* Record the number of instructions which may be removed. */
11311 static int count = 0, record_count;
11312 Elf_Internal_Sym *isym;
11313 struct elf_link_hash_entry *h = NULL;
11314 int indx;
11315 unsigned long r_symndx;
11316 bfd *abfd = sec->owner;
11317 static bfd_vma record_sda = 0;
11318 int sda_offset = 0;
11319
11320 /* Force doing relaxation when hyper-relax is high. */
11321 if (table->hyper_relax == 2)
11322 return TRUE;
11323
11324 /* Do not relax the load/store patterns for the first
11325 relax round. */
11326 if (init)
11327 {
11328 if (!first_sec)
11329 first_sec = sec;
11330 else if (first_sec == sec)
11331 {
11332 record_count = count;
11333 count = 0;
11334 sec_pass = TRUE;
11335 }
11336
11337 if (!sec_pass)
11338 *again = TRUE;
11339
11340 return TRUE;
11341 }
11342
11343 /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11344 DATA_SEGMENT_ALIGN size in the linker script may make it
11345 get even bigger. */
11346 if (record_sda == 0)
11347 record_sda = local_sda;
11348 else if (local_sda > record_sda)
11349 sda_offset = local_sda - record_sda;
11350
11351 /* Assume the instruction will be removed in the best case. */
11352 count++;
11353
11354 /* We record the offset to gp for each symbol, and then check
11355 if it is changed dramatically after relaxing.
11356 (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11357 (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx]. */
11358 r_symndx = ELF32_R_SYM (irel->r_info);
11359 if (r_symndx >= symtab_hdr->sh_info)
11360 {
11361 /* Global symbols. */
11362 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11363 h = elf_sym_hashes (abfd)[indx];
11364 sym_sec = h->root.u.def.section;
11365 if (NDS32_GUARD_SEC_P (sym_sec->flags)
11366 || bfd_is_abs_section (sym_sec))
11367 {
11368 /* Forbid doing relaxation when hyper-relax is low. */
11369 if (table->hyper_relax == 0)
11370 return FALSE;
11371
11372 offset_to_gp = *access_addr - local_sda;
11373 if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11374 elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11375 else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11376 < abs (offset_to_gp) - sda_offset)
11377 {
11378 /* This may cause the error, so we reserve the
11379 safe enough size for relaxing. */
11380 if (*access_addr >= local_sda)
11381 *access_addr += (record_count * 4);
11382 else
11383 *access_addr -= (record_count * 4);
11384 }
11385 return sec_pass;
11386 }
11387 }
11388 else
11389 {
11390 /* Local symbols. */
11391 if (!elf32_nds32_allocate_local_sym_info (abfd))
11392 return FALSE;
11393 isym = isymbuf + r_symndx;
11394
11395 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11396 if (NDS32_GUARD_SEC_P (sym_sec->flags))
11397 {
11398 /* Forbid doing relaxation when hyper-relax is low. */
11399 if (table->hyper_relax == 0)
11400 return FALSE;
11401
11402 offset_to_gp = *access_addr - local_sda;
11403 if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11404 elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11405 else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11406 < abs (offset_to_gp) - sda_offset)
11407 {
11408 /* This may cause the error, so we reserve the
11409 safe enough size for relaxing. */
11410 if (*access_addr >= local_sda)
11411 *access_addr += (record_count * 4);
11412 else
11413 *access_addr -= (record_count * 4);
11414 }
11415 return sec_pass;
11416 }
11417 }
11418
11419 return TRUE;
11420}
11421
1c8f6a4d 11422#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
35c08157 11423
1c8f6a4d 11424/* Relax LOADSTORE relocation for nds32_elf_relax_section. */
35c08157 11425
1c8f6a4d
KLC
11426static bfd_boolean
11427nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11428 asection *sec, Elf_Internal_Rela *irel,
11429 Elf_Internal_Rela *internal_relocs, int *insn_len,
11430 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
fbaf61ad
NC
11431 Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11432 struct elf_nds32_link_hash_table *table)
1c8f6a4d 11433{
6cae483a
AM
11434 int eliminate_sethi = 0, range_type;
11435 unsigned int i;
1c8f6a4d
KLC
11436 bfd_vma local_sda, laddr;
11437 int seq_len; /* Original length of instruction sequence. */
11438 uint32_t insn;
11439 Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11440 bfd_vma access_addr = 0;
11441 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
fbaf61ad
NC
11442 struct elf_link_hash_entry *h = NULL;
11443 int indx;
1c8f6a4d
KLC
11444 enum elf_nds32_reloc_type checked_types[] =
11445 { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11446 R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11447 R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11448 R_NDS32_TLS_LE_HI20
11449 };
35c08157 11450
1c8f6a4d
KLC
11451 irelend = internal_relocs + sec->reloc_count;
11452 seq_len = GET_SEQ_LEN (irel->r_addend);
11453 laddr = irel->r_offset;
11454 *insn_len = seq_len;
35c08157 11455
1c8f6a4d 11456 /* Get the high part relocation. */
6cae483a 11457 for (i = 0; i < ARRAY_SIZE (checked_types); i++)
1c8f6a4d
KLC
11458 {
11459 hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11460 checked_types[i], laddr);
11461 if (hi_irelfn != irelend)
11462 break;
11463 }
35c08157 11464
1c8f6a4d
KLC
11465 if (hi_irelfn == irelend)
11466 {
fbaf61ad
NC
11467 /* Not R_NDS32_HI20_RELA. */
11468 if (i != 0)
11469 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11470 (uint64_t) irel->r_offset);
11471 return FALSE;
1c8f6a4d 11472 }
35c08157 11473
1c8f6a4d
KLC
11474 range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11475 nds32_elf_final_sda_base (sec->output_section->owner,
11476 link_info, &local_sda, FALSE);
35c08157 11477
1c8f6a4d
KLC
11478 switch (ELF32_R_TYPE (hi_irelfn->r_info))
11479 {
11480 case R_NDS32_HI20_RELA:
11481 insn = bfd_getb32 (contents + laddr);
11482 access_addr =
11483 calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11484
fbaf61ad 11485 if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
1c8f6a4d 11486 {
fbaf61ad
NC
11487 indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11488 h = elf_sym_hashes (abfd)[indx];
11489 }
35c08157 11490
fbaf61ad
NC
11491 /* Try movi. */
11492 if (range_type == NDS32_LOADSTORE_IMM
11493 && access_addr < CONSERVATIVE_20BIT
11494 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11495 {
11496 eliminate_sethi = 1;
11497 break;
11498 }
1c8f6a4d 11499
fbaf61ad
NC
11500 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11501 {
11502 eliminate_sethi = 1;
11503 break;
1c8f6a4d 11504 }
fbaf61ad
NC
11505 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11506 NULL, FALSE, table, isymbuf, symtab_hdr))
11507 return FALSE;
1c8f6a4d
KLC
11508
11509 if (!load_store_relax)
11510 return FALSE;
11511
11512 /* Case for set gp register. */
11513 if (N32_RT5 (insn) == REG_GP)
fbaf61ad 11514 return FALSE;
1c8f6a4d
KLC
11515
11516 if (range_type == NDS32_LOADSTORE_FLOAT_S
6cef73f9 11517 || range_type == NDS32_LOADSTORE_FLOAT_D)
1c8f6a4d
KLC
11518 {
11519 range_l = sdata_range[0][0];
11520 range_h = sdata_range[0][1];
11521 }
11522 else
11523 {
11524 range_l = sdata_range[1][0];
11525 range_h = sdata_range[1][1];
11526 }
11527 break;
11528
1c8f6a4d
KLC
11529 default:
11530 return FALSE;
11531 }
11532
11533 /* Delete sethi instruction. */
11534 if (eliminate_sethi == 1
11535 || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11536 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11537 {
11538 hi_irelfn->r_info =
11539 ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11540 irel->r_info =
11541 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11542 *insn_len = 0;
fbaf61ad 11543 return TRUE;
1c8f6a4d 11544 }
fbaf61ad
NC
11545
11546 return FALSE;
1c8f6a4d
KLC
11547}
11548
11549/* Relax LO12 relocation for nds32_elf_relax_section. */
11550
11551static void
11552nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11553 asection *sec, Elf_Internal_Rela *irel,
11554 Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
fbaf61ad
NC
11555 Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11556 struct elf_nds32_link_hash_table *table)
1c8f6a4d
KLC
11557{
11558 uint32_t insn;
11559 bfd_vma local_sda, laddr;
11560 unsigned long reloc;
11561 bfd_vma access_addr;
11562 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11563 Elf_Internal_Rela *irelfn = NULL, *irelend;
11564 struct elf_link_hash_entry *h = NULL;
11565 int indx;
11566
11567 /* For SDA base relative relaxation. */
11568 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11569 &local_sda, FALSE);
11570
11571 irelend = internal_relocs + sec->reloc_count;
11572 laddr = irel->r_offset;
11573 insn = bfd_getb32 (contents + laddr);
11574
11575 if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11576 return;
11577
11578 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11579
11580 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11581 {
11582 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11583 h = elf_sym_hashes (abfd)[indx];
11584 }
11585
fbaf61ad 11586 /* Try movi. */
1c8f6a4d
KLC
11587 if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11588 && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11589 {
11590 reloc = R_NDS32_20_RELA;
11591 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11592 insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11593 bfd_putb32 (insn, contents + laddr);
11594 }
1c8f6a4d
KLC
11595 else
11596 {
fbaf61ad
NC
11597 if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11598 {
11599 /* Fall through. */
11600 }
11601 else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11602 FALSE, table, isymbuf, symtab_hdr))
11603 return;
11604
1c8f6a4d
KLC
11605 range_l = sdata_range[1][0];
11606 range_h = sdata_range[1][1];
11607 switch (ELF32_R_TYPE (irel->r_info))
11608 {
11609 case R_NDS32_LO12S0_RELA:
11610 reloc = R_NDS32_SDA19S0_RELA;
11611 break;
11612 case R_NDS32_LO12S1_RELA:
11613 reloc = R_NDS32_SDA18S1_RELA;
11614 break;
11615 case R_NDS32_LO12S2_RELA:
11616 reloc = R_NDS32_SDA17S2_RELA;
11617 break;
11618 case R_NDS32_LO12S2_DP_RELA:
11619 range_l = sdata_range[0][0];
11620 range_h = sdata_range[0][1];
11621 reloc = R_NDS32_SDA12S2_DP_RELA;
11622 break;
11623 case R_NDS32_LO12S2_SP_RELA:
11624 range_l = sdata_range[0][0];
11625 range_h = sdata_range[0][1];
11626 reloc = R_NDS32_SDA12S2_SP_RELA;
11627 break;
11628 default:
11629 return;
11630 }
11631
11632 /* There are range_h and range_l because linker has to promise
11633 all sections move cross one page together. */
11634 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
fbaf61ad
NC
11635 || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11636 || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
1c8f6a4d
KLC
11637 {
11638 if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
35c08157 11639 {
1c8f6a4d
KLC
11640 /* Maybe we should add R_NDS32_INSN16 reloc type here
11641 or manually do some optimization. sethi can't be
11642 eliminated when updating $gp so the relative ori
11643 needs to be preserved. */
11644 return;
35c08157 11645 }
1c8f6a4d
KLC
11646 if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11647 &insn))
11648 return;
11649 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11650 bfd_putb32 (insn, contents + laddr);
11651
11652 irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11653 R_NDS32_INSN16);
11654 /* SDA17 must keep INSN16 for converting fp_as_gp. */
11655 if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11656 irelfn->r_info =
11657 ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11658
35c08157 11659 }
1c8f6a4d
KLC
11660 }
11661 return;
11662}
35c08157 11663
1c8f6a4d 11664/* Relax PTR relocation for nds32_elf_relax_section. */
35c08157 11665
1c8f6a4d
KLC
11666static bfd_boolean
11667nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11668 Elf_Internal_Rela *internal_relocs, int *insn_len,
11669 int *seq_len, bfd_byte *contents)
11670{
11671 Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
35c08157 11672
1c8f6a4d 11673 irelend = internal_relocs + sec->reloc_count;
35c08157 11674
1c8f6a4d
KLC
11675 re_irel =
11676 find_relocs_at_address_addr (irel, internal_relocs, irelend,
11677 R_NDS32_PTR_RESOLVED, irel->r_addend);
35c08157 11678
1c8f6a4d
KLC
11679 if (re_irel == irelend)
11680 {
695344c0 11681 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
2dcf00ce 11682 (uint64_t) irel->r_offset);
1c8f6a4d
KLC
11683 return FALSE;
11684 }
35c08157 11685
1c8f6a4d
KLC
11686 if (re_irel->r_addend != 1)
11687 return FALSE;
35c08157 11688
1c8f6a4d
KLC
11689 /* Pointed target is relaxed and no longer needs this void *,
11690 change the type to NONE. */
11691 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
35c08157 11692
1c8f6a4d
KLC
11693 /* Find PTR_COUNT to decide remove it or not. If PTR_COUNT does
11694 not exist, it means only count 1 and remove it directly. */
11695 /* TODO: I hope we can obsolate R_NDS32_COUNT in the future. */
11696 count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11697 R_NDS32_PTR_COUNT);
11698 ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11699 R_NDS32_PTR);
11700 if (count_irel != irelend)
11701 {
11702 if (--count_irel->r_addend > 0)
11703 return FALSE;
11704 }
11705
11706 if (ptr_irel != irelend)
11707 return FALSE;
11708
11709 /* If the PTR_COUNT is already 0, remove current instruction. */
11710 *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11711 *insn_len = 0;
11712 return TRUE;
11713}
11714
fbaf61ad 11715/* Relax LWC relocation for nds32_elf_relax_section. */
1c8f6a4d
KLC
11716
11717static void
fbaf61ad
NC
11718nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11719 asection *sec, Elf_Internal_Rela *irel,
11720 Elf_Internal_Rela *internal_relocs,
11721 bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11722 Elf_Internal_Shdr *symtab_hdr, bfd_boolean *again)
1c8f6a4d 11723{
fbaf61ad
NC
11724 /* Pattern:
11725 sethi ra, hi20(symbol) ; HI20/LOADSTORE
11726 ori ra, ra, lo12(symbol) ; LO12S0/PTR/PTR/.../INSN16
11727 flsi fsa, [ra + offset1] ; LSI/PTR_RESOLVED/INSN16
11728 flsi fsb, [ra + offset2] ; LSI/PTR_RESOLVED/INSN16
11729 ... */
11730
1c8f6a4d 11731 uint32_t insn;
1c8f6a4d 11732 bfd_vma local_sda, laddr;
fbaf61ad
NC
11733 unsigned long reloc;
11734 bfd_vma access_addr, flsi_offset;
11735 bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound. */
11736 Elf_Internal_Rela *irelend, *re_irel;
11737 unsigned int opcode;
1c8f6a4d
KLC
11738
11739 irelend = internal_relocs + sec->reloc_count;
11740 laddr = irel->r_offset;
11741 insn = bfd_getb32 (contents + laddr);
11742
fbaf61ad 11743 if ((insn & 0x80000000) || !is_sda_access_insn (insn))
1c8f6a4d
KLC
11744 return;
11745
fbaf61ad
NC
11746 /* Can not do relaxation for bi format. */
11747 if ((insn & 0x1000))
1c8f6a4d
KLC
11748 return;
11749
fbaf61ad
NC
11750 /* Only deal with flsi, fssi, fldi, fsdi, so far. */
11751 opcode = N32_OP6 (insn);
11752 if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11753 reloc = R_NDS32_SDA12S2_SP_RELA;
11754 else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11755 reloc = R_NDS32_SDA12S2_DP_RELA;
1c8f6a4d
KLC
11756 else
11757 return;
11758
fbaf61ad
NC
11759 re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11760 R_NDS32_PTR_RESOLVED);
11761 if (re_irel == irelend)
1c8f6a4d 11762 {
fbaf61ad
NC
11763 _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11764 (uint64_t) irel->r_offset);
11765 return;
1c8f6a4d 11766 }
1c8f6a4d 11767
fbaf61ad 11768 /* For SDA base relative relaxation. */
1c8f6a4d
KLC
11769 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11770 &local_sda, FALSE);
fbaf61ad
NC
11771 access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11772 flsi_offset = (insn & 0xfff) << 2;
11773 access_addr += flsi_offset;
11774 range_l = sdata_range[0][0];
11775 range_h = sdata_range[0][1];
1c8f6a4d 11776
fbaf61ad
NC
11777 if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11778 || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
1c8f6a4d 11779 {
fbaf61ad
NC
11780 /* Turn flsi instruction into sda access format. */
11781 insn = (insn & 0x7ff07000) | (REG_GP << 15);
1c8f6a4d 11782
fbaf61ad
NC
11783 /* Add relocation type to flsi. */
11784 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11785 irel->r_addend += flsi_offset;
11786 bfd_putb32 (insn, contents + re_irel->r_offset);
1c8f6a4d 11787
fbaf61ad 11788 re_irel->r_addend |= 1;
1c8f6a4d
KLC
11789 *again = TRUE;
11790 }
1c8f6a4d
KLC
11791}
11792
11793static bfd_boolean
11794nds32_relax_adjust_label (bfd *abfd, asection *sec,
11795 Elf_Internal_Rela *internal_relocs,
11796 bfd_byte *contents,
11797 nds32_elf_blank_t **relax_blank_list,
11798 int optimize, int opt_size)
11799{
11800 /* This code block is used to adjust 4-byte alignment by relax a pair
11801 of instruction a time.
11802
11803 It recognizes three types of relocations.
de194d85 11804 1. R_NDS32_LABEL - a alignment.
1c8f6a4d
KLC
11805 2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11806 3. is_16bit_NOP () - remove a 16-bit instruction. */
11807
de194d85
YC
11808 /* TODO: It seems currently implementation only support 4-byte alignment.
11809 We should handle any-alignment. */
1c8f6a4d
KLC
11810
11811 Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11812 Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11813 Elf_Internal_Rela rel_temp;
11814 Elf_Internal_Rela *irelend;
11815 bfd_vma address;
11816 uint16_t insn16;
11817
11818 /* Checking for branch relaxation relies on the relocations to
11819 be sorted on 'r_offset'. This is not guaranteed so we must sort. */
11820 nds32_insertion_sort (internal_relocs, sec->reloc_count,
11821 sizeof (Elf_Internal_Rela), compar_reloc);
11822
11823 irelend = internal_relocs + sec->reloc_count;
11824
11825 /* Force R_NDS32_LABEL before R_NDS32_INSN16. */
11826 /* FIXME: Can we generate the right order in assembler?
11827 So we don't have to swapping them here. */
11828
11829 for (label_rel = internal_relocs, insn_rel = internal_relocs;
11830 label_rel < irelend; label_rel++)
11831 {
11832 if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11833 continue;
11834
11835 /* Find the first reloc has the same offset with label_rel. */
11836 while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11837 insn_rel++;
11838
11839 for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11840 insn_rel++)
11841 /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11842 address. */
11843 if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11844 break;
11845
11846 if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11847 && insn_rel < label_rel)
11848 {
11849 /* Swap the two reloc if the R_NDS32_INSN16 is
11850 before R_NDS32_LABEL. */
11851 memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11852 memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11853 memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11854 }
11855 }
11856
11857 label_rel = NULL;
11858 insn_rel = NULL;
11859 /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11860 or higher, remove other R_NDS32_LABEL with lower alignment.
11861 If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11862 then the R_NDS32_LABEL sequence is broke. */
11863 for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11864 {
11865 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11866 {
11867 if (label_rel == NULL)
11868 {
11869 if (tmp_rel->r_addend < 2)
11870 label_rel = tmp_rel;
11871 continue;
11872 }
11873 else if (tmp_rel->r_addend > 1)
11874 {
11875 /* Remove all LABEL relocation from label_rel to tmp_rel
11876 including relocations with same offset as tmp_rel. */
fbaf61ad 11877 for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
1c8f6a4d 11878 {
fbaf61ad
NC
11879 if (tmp2_rel->r_offset == tmp_rel->r_offset)
11880 break;
11881
1c8f6a4d
KLC
11882 if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11883 && tmp2_rel->r_addend < 2)
11884 tmp2_rel->r_info =
11885 ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11886 R_NDS32_NONE);
11887 }
11888 label_rel = NULL;
11889 }
11890 }
11891 else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11892 {
11893 /* A new INSN16 which can be converted, so clear label_rel. */
11894 if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11895 irelend, &insn16)
11896 || is_16bit_NOP (abfd, sec, tmp_rel))
11897 label_rel = NULL;
11898 }
11899 }
11900
11901 label_rel = NULL;
11902 insn_rel = NULL;
11903 /* Optimized for speed and nothing has not been relaxed.
11904 It's time to align labels.
11905 We may convert a 16-bit instruction right before a label to
11906 32-bit, in order to align the label if necessary
11907 all reloc entries has been sorted by r_offset. */
fbaf61ad
NC
11908 for (irel = internal_relocs;
11909 irel < irelend && irel->r_offset < sec->size; irel++)
1c8f6a4d
KLC
11910 {
11911 if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11912 && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11913 continue;
11914
11915 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11916 {
11917 /* A new INSN16 found, resize the old one. */
11918 if (is_convert_32_to_16
11919 (abfd, sec, irel, internal_relocs, irelend, &insn16)
11920 || is_16bit_NOP (abfd, sec, irel))
11921 {
11922 if (insn_rel)
11923 {
11924 /* Previous INSN16 reloc exists, reduce its
11925 size to 16-bit. */
11926 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11927 irelend, &insn16))
35c08157 11928 {
1c8f6a4d
KLC
11929 nds32_elf_write_16 (abfd, contents, insn_rel,
11930 internal_relocs, irelend, insn16);
11931
11932 if (!insert_nds32_elf_blank_recalc_total
11933 (relax_blank_list, insn_rel->r_offset + 2, 2))
11934 return FALSE;
11935 }
11936 else if (is_16bit_NOP (abfd, sec, insn_rel))
11937 {
11938 if (!insert_nds32_elf_blank_recalc_total
11939 (relax_blank_list, insn_rel->r_offset, 2))
11940 return FALSE;
11941 }
11942 insn_rel->r_info =
11943 ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11944 }
11945 /* Save the new one for later use. */
11946 insn_rel = irel;
11947 }
11948 else
11949 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11950 R_NDS32_NONE);
11951 }
11952 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11953 {
11954 /* Search for label. */
11955 int force_relax = 0;
11956
11957 /* Label on 16-bit instruction or optimization
11958 needless, just reset this reloc. */
11959 insn16 = bfd_getb16 (contents + irel->r_offset);
11960 if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
11961 {
11962 irel->r_info =
11963 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11964 continue;
11965 }
11966
11967 address =
11968 irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
11969 irel->r_offset, 1);
11970
11971 if (!insn_rel)
11972 {
11973 /* Check if there is case which can not be aligned. */
11974 if (irel->r_addend == 2 && address & 0x2)
11975 return FALSE;
11976 continue;
11977 }
11978
11979 /* Try to align this label. */
11980
11981 if ((irel->r_addend & 0x1f) < 2)
11982 {
11983 /* Check if there is a INSN16 at the same address.
11984 Label_rel always seats before insn_rel after
11985 our sort. */
11986
11987 /* Search for INSN16 at LABEL location. If INSN16 is at
11988 same location and this LABEL alignment is lower than 2,
11989 the INSN16 can be converted to 2-byte. */
11990 for (tmp_rel = irel;
11991 tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
11992 tmp_rel++)
11993 {
11994 if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
11995 && (is_convert_32_to_16
11996 (abfd, sec, tmp_rel, internal_relocs,
11997 irelend, &insn16)
11998 || is_16bit_NOP (abfd, sec, tmp_rel)))
11999 {
12000 force_relax = 1;
12001 break;
12002 }
12003 }
12004 }
12005
12006 if (force_relax || irel->r_addend == 1 || address & 0x2)
12007 {
12008 /* Label not aligned. */
12009 /* Previous reloc exists, reduce its size to 16-bit. */
12010 if (is_convert_32_to_16 (abfd, sec, insn_rel,
12011 internal_relocs, irelend, &insn16))
12012 {
12013 nds32_elf_write_16 (abfd, contents, insn_rel,
12014 internal_relocs, irelend, insn16);
12015
12016 if (!insert_nds32_elf_blank_recalc_total
12017 (relax_blank_list, insn_rel->r_offset + 2, 2))
12018 return FALSE;
12019 }
12020 else if (is_16bit_NOP (abfd, sec, insn_rel))
12021 {
12022 if (!insert_nds32_elf_blank_recalc_total
12023 (relax_blank_list, insn_rel->r_offset, 2))
12024 return FALSE;
12025 }
12026
12027 }
12028 /* INSN16 reloc is used. */
12029 insn_rel = NULL;
12030 }
12031 }
12032
12033 address =
12034 sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12035 if (insn_rel && (address & 0x2 || opt_size))
12036 {
12037 if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12038 irelend, &insn16))
12039 {
12040 nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12041 irelend, insn16);
12042 if (!insert_nds32_elf_blank_recalc_total
12043 (relax_blank_list, insn_rel->r_offset + 2, 2))
12044 return FALSE;
12045 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12046 R_NDS32_NONE);
12047 }
12048 else if (is_16bit_NOP (abfd, sec, insn_rel))
12049 {
12050 if (!insert_nds32_elf_blank_recalc_total
12051 (relax_blank_list, insn_rel->r_offset, 2))
12052 return FALSE;
12053 insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12054 R_NDS32_NONE);
12055 }
12056 }
12057 insn_rel = NULL;
12058 return TRUE;
12059}
12060
1c8f6a4d
KLC
12061static bfd_boolean
12062nds32_elf_relax_section (bfd *abfd, asection *sec,
12063 struct bfd_link_info *link_info, bfd_boolean *again)
12064{
12065 nds32_elf_blank_t *relax_blank_list = NULL;
12066 Elf_Internal_Shdr *symtab_hdr;
12067 Elf_Internal_Rela *internal_relocs;
12068 Elf_Internal_Rela *irel;
12069 Elf_Internal_Rela *irelend;
12070 Elf_Internal_Sym *isymbuf = NULL;
12071 bfd_byte *contents = NULL;
12072 bfd_boolean result = TRUE;
12073 int optimize = 0;
12074 int opt_size = 0;
12075 uint32_t insn;
12076 uint16_t insn16;
12077
12078 /* Target dependnet option. */
12079 struct elf_nds32_link_hash_table *table;
12080 int load_store_relax;
1c8f6a4d
KLC
12081
12082 relax_blank_list = NULL;
12083
12084 *again = FALSE;
12085
12086 /* Nothing to do for
12087 * relocatable link or
12088 * non-relocatable section or
12089 * non-code section or
12090 * empty content or
12091 * no reloc entry. */
0e1862bb 12092 if (bfd_link_relocatable (link_info)
1c8f6a4d 12093 || (sec->flags & SEC_RELOC) == 0
cd28e7aa 12094 || (sec->flags & SEC_EXCLUDE) != 0
1c8f6a4d 12095 || (sec->flags & SEC_CODE) == 0
fbaf61ad
NC
12096 || sec->size == 0
12097 || sec->reloc_count == 0)
1c8f6a4d
KLC
12098 return TRUE;
12099
12100 /* 09.12.11 Workaround. */
12101 /* We have to adjust align for R_NDS32_LABEL if needed.
12102 The adjust approach only can fix 2-byte align once. */
12103 if (sec->alignment_power > 2)
12104 return TRUE;
12105
fbaf61ad
NC
12106 /* Do TLS model conversion once at first. */
12107 nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12108
1c8f6a4d
KLC
12109 /* The optimization type to do. */
12110
12111 table = nds32_elf_hash_table (link_info);
1c8f6a4d 12112
fbaf61ad
NC
12113 /* Save the first section for abs symbol relaxation.
12114 This is used for checking gp relaxation in the
12115 nds32_elf_relax_loadstore and nds32_elf_relax_lo12. */
12116 nds32_elf_relax_guard (NULL, 0, sec, NULL, again, TRUE,
12117 table, NULL, NULL);
12118
1c8f6a4d
KLC
12119 /* The begining of general relaxation. */
12120
12121 if (is_SDA_BASE_set == 0)
12122 {
12123 bfd_vma gp;
12124 is_SDA_BASE_set = 1;
12125 nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12126 &gp, FALSE);
12127 relax_range_measurement (abfd);
12128 }
12129
1c8f6a4d
KLC
12130 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12131 /* Relocations MUST be kept in memory, because relaxation adjust them. */
12132 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12133 TRUE /* keep_memory */);
12134 if (internal_relocs == NULL)
12135 goto error_return;
12136
12137 irelend = internal_relocs + sec->reloc_count;
12138 irel = find_relocs_at_address (internal_relocs, internal_relocs,
12139 irelend, R_NDS32_RELAX_ENTRY);
12140
12141 if (irel == irelend)
12142 return TRUE;
12143
12144 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12145 {
12146 if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
f9671640 12147 return TRUE;
1c8f6a4d
KLC
12148
12149 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12150 optimize = 1;
12151
12152 if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12153 opt_size = 1;
12154 }
12155
12156 load_store_relax = table->load_store_relax;
12157
12158 /* Get symbol table and section content. */
0c4bd9d9 12159 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
1c8f6a4d
KLC
12160 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12161 goto error_return;
12162
12163 /* Do relax loop only when finalize is not done.
12164 Take care of relaxable relocs except INSN16. */
12165 for (irel = internal_relocs; irel < irelend; irel++)
12166 {
12167 int seq_len; /* Original length of instruction sequence. */
12168 int insn_len = 0; /* Final length of instruction sequence. */
12169 bfd_boolean removed;
12170
12171 insn = 0;
12172 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12173 && (irel->r_addend & 0x1f) >= 2)
12174 optimize = 1;
12175
12176 /* Relocation Types
12177 R_NDS32_LONGCALL1 53
12178 R_NDS32_LONGCALL2 54
12179 R_NDS32_LONGCALL3 55
12180 R_NDS32_LONGJUMP1 56
12181 R_NDS32_LONGJUMP2 57
12182 R_NDS32_LONGJUMP3 58
12183 R_NDS32_LOADSTORE 59 */
12184 if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12185 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12186 seq_len = GET_SEQ_LEN (irel->r_addend);
12187
12188 /* Relocation Types
12189 R_NDS32_LONGCALL4 107
12190 R_NDS32_LONGCALL5 108
12191 R_NDS32_LONGCALL6 109
12192 R_NDS32_LONGJUMP4 110
12193 R_NDS32_LONGJUMP5 111
12194 R_NDS32_LONGJUMP6 112
12195 R_NDS32_LONGJUMP7 113 */
12196 else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12197 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12198 seq_len = 4;
12199
12200 /* Relocation Types
12201 R_NDS32_LO12S0_RELA 30
12202 R_NDS32_LO12S1_RELA 29
12203 R_NDS32_LO12S2_RELA 28
12204 R_NDS32_LO12S2_SP_RELA 71
12205 R_NDS32_LO12S2_DP_RELA 70
12206 R_NDS32_GOT_LO12 46
12207 R_NDS32_GOTOFF_LO12 50
12208 R_NDS32_PLTREL_LO12 65
12209 R_NDS32_PLT_GOTREL_LO12 67
12210 R_NDS32_17IFC_PCREL_RELA 96
12211 R_NDS32_GOT_SUFF 193
12212 R_NDS32_GOTOFF_SUFF 194
12213 R_NDS32_PLT_GOT_SUFF 195
12214 R_NDS32_MULCALL_SUFF 196
12215 R_NDS32_PTR 197 */
12216 else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12217 && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12218 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12219 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12220 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12221 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12222 || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12223 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12224 || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12225 || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12226 && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12227 || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12228 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12229 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
fbaf61ad
NC
12230 || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12231 || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
1c8f6a4d
KLC
12232 seq_len = 0;
12233 else
12234 continue;
12235
12236 insn_len = seq_len;
12237 removed = FALSE;
12238
12239 switch (ELF32_R_TYPE (irel->r_info))
12240 {
12241 case R_NDS32_LONGCALL1:
12242 removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12243 &insn_len, contents, isymbuf,
12244 symtab_hdr);
12245 break;
12246 case R_NDS32_LONGCALL2:
12247 removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12248 &insn_len, contents, isymbuf,
12249 symtab_hdr);
12250 break;
12251 case R_NDS32_LONGCALL3:
12252 removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12253 &insn_len, contents, isymbuf,
12254 symtab_hdr);
12255 break;
12256 case R_NDS32_LONGJUMP1:
12257 removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12258 &insn_len, contents, isymbuf,
12259 symtab_hdr);
12260 break;
12261 case R_NDS32_LONGJUMP2:
12262 removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12263 &insn_len, contents, isymbuf,
12264 symtab_hdr);
12265 break;
12266 case R_NDS32_LONGJUMP3:
12267 removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12268 &insn_len, contents, isymbuf,
12269 symtab_hdr);
12270 break;
12271 case R_NDS32_LONGCALL4:
12272 removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12273 &insn_len, contents, isymbuf,
12274 symtab_hdr);
12275 break;
12276 case R_NDS32_LONGCALL5:
12277 removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12278 &insn_len, contents, isymbuf,
12279 symtab_hdr);
12280 break;
12281 case R_NDS32_LONGCALL6:
12282 removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12283 &insn_len, contents, isymbuf,
12284 symtab_hdr);
12285 break;
12286 case R_NDS32_LONGJUMP4:
12287 removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12288 &insn_len, contents, isymbuf,
12289 symtab_hdr);
12290 break;
12291 case R_NDS32_LONGJUMP5:
12292 removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12293 &insn_len, &seq_len, contents,
12294 isymbuf, symtab_hdr);
12295 break;
12296 case R_NDS32_LONGJUMP6:
12297 removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12298 &insn_len, &seq_len, contents,
12299 isymbuf, symtab_hdr);
12300 break;
12301 case R_NDS32_LONGJUMP7:
12302 removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12303 &insn_len, &seq_len, contents,
12304 isymbuf, symtab_hdr);
12305 break;
12306 case R_NDS32_LOADSTORE:
12307 removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12308 internal_relocs, &insn_len,
12309 contents, isymbuf, symtab_hdr,
fbaf61ad 12310 load_store_relax, table);
1c8f6a4d
KLC
12311 break;
12312 case R_NDS32_LO12S0_RELA:
12313 case R_NDS32_LO12S1_RELA:
fbaf61ad 12314 case R_NDS32_LO12S2_RELA:
1c8f6a4d
KLC
12315 case R_NDS32_LO12S2_DP_RELA:
12316 case R_NDS32_LO12S2_SP_RELA:
1c8f6a4d
KLC
12317 /* Relax for low part. */
12318 nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
fbaf61ad 12319 contents, isymbuf, symtab_hdr, table);
1c8f6a4d
KLC
12320
12321 /* It is impossible to delete blank, so just continue. */
12322 continue;
fbaf61ad
NC
12323 case R_NDS32_PTR:
12324 removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12325 &insn_len, &seq_len, contents);
12326 break;
12327 case R_NDS32_LSI:
12328 nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12329 contents, isymbuf, symtab_hdr, again);
12330 continue;
1c8f6a4d
KLC
12331 case R_NDS32_GOT_LO12:
12332 case R_NDS32_GOTOFF_LO12:
12333 case R_NDS32_PLTREL_LO12:
12334 case R_NDS32_PLT_GOTREL_LO12:
12335 case R_NDS32_GOTPC_LO12:
1c8f6a4d 12336 case R_NDS32_TLS_LE_LO12:
1c8f6a4d 12337 case R_NDS32_TLS_LE_ADD:
1c8f6a4d 12338 case R_NDS32_TLS_LE_LS:
1c8f6a4d 12339 case R_NDS32_PLT_GOT_SUFF:
1c8f6a4d 12340 case R_NDS32_GOT_SUFF:
1c8f6a4d 12341 case R_NDS32_GOTOFF_SUFF:
1c8f6a4d
KLC
12342 continue;
12343 default:
12344 continue;
1c8f6a4d 12345 }
fbaf61ad 12346
1c8f6a4d
KLC
12347 if (removed && seq_len - insn_len > 0)
12348 {
12349 if (!insert_nds32_elf_blank
12350 (&relax_blank_list, irel->r_offset + insn_len,
12351 seq_len - insn_len))
12352 goto error_return;
12353 *again = TRUE;
35c08157 12354 }
1c8f6a4d
KLC
12355 }
12356
12357 calc_nds32_blank_total (relax_blank_list);
12358
12359 if (table->relax_fp_as_gp)
12360 {
12361 if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12362 irelend, isymbuf))
12363 goto error_return;
35c08157 12364
535b785f 12365 if (!*again)
35c08157 12366 {
1c8f6a4d
KLC
12367 if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12368 irelend))
12369 goto error_return;
35c08157
KLC
12370 }
12371 }
1c8f6a4d 12372
535b785f 12373 if (!*again)
1c8f6a4d
KLC
12374 {
12375 if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12376 &relax_blank_list, optimize, opt_size))
12377 goto error_return;
12378 }
12379
12380 /* It doesn't matter optimize_for_space_no_align anymore.
35c08157
KLC
12381 If object file is assembled with flag '-Os',
12382 the we don't adjust jump-destination on 4-byte boundary. */
12383
12384 if (relax_blank_list)
12385 {
12386 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12387 relax_blank_list = NULL;
12388 }
12389
535b785f 12390 if (!*again)
35c08157
KLC
12391 {
12392 /* Closing the section, so we don't relax it anymore. */
12393 bfd_vma sec_size_align;
12394 Elf_Internal_Rela *tmp_rel;
12395
12396 /* Pad to alignment boundary. Only handle current section alignment. */
609332f1
NC
12397 sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12398 & ((-1U) << sec->alignment_power);
35c08157
KLC
12399 if ((sec_size_align - sec->size) & 0x2)
12400 {
12401 insn16 = NDS32_NOP16;
12402 bfd_putb16 (insn16, contents + sec->size);
12403 sec->size += 2;
12404 }
12405
12406 while (sec_size_align != sec->size)
12407 {
12408 insn = NDS32_NOP32;
12409 bfd_putb32 (insn, contents + sec->size);
12410 sec->size += 4;
12411 }
12412
1c8f6a4d
KLC
12413 tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12414 irelend, R_NDS32_RELAX_ENTRY);
35c08157
KLC
12415 if (tmp_rel != irelend)
12416 tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12417
12418 clean_nds32_elf_blank ();
12419 }
12420
dc1e8a47 12421 finish:
c9594989 12422 if (elf_section_data (sec)->relocs != internal_relocs)
35c08157
KLC
12423 free (internal_relocs);
12424
c9594989 12425 if (elf_section_data (sec)->this_hdr.contents != contents)
35c08157
KLC
12426 free (contents);
12427
c9594989 12428 if (symtab_hdr->contents != (bfd_byte *) isymbuf)
35c08157
KLC
12429 free (isymbuf);
12430
12431 return result;
12432
dc1e8a47 12433 error_return:
35c08157
KLC
12434 result = FALSE;
12435 goto finish;
12436}
12437
12438static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12439{
12440 {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12441 {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12442 {NULL, 0, 0, 0, 0}
12443};
12444
bf577467
AM
12445static bfd_boolean
12446nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12447{
12448 const char *name = hdr->bfd_section->name;
12449
12450 if (strncmp (name, ".sbss", 5) == 0
12451 || strncmp (name, ".sdata", 6) == 0)
12452 hdr->bfd_section->flags |= SEC_SMALL_DATA;
12453
12454 return TRUE;
12455}
12456
35c08157
KLC
12457static bfd_boolean
12458nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12459 struct bfd_link_info *info,
12460 void *finfo ATTRIBUTE_UNUSED,
12461 bfd_boolean (*func) (void *, const char *,
12462 Elf_Internal_Sym *,
12463 asection *,
12464 struct elf_link_hash_entry *)
12465 ATTRIBUTE_UNUSED)
12466{
12467 FILE *sym_ld_script = NULL;
12468 struct elf_nds32_link_hash_table *table;
12469
12470 table = nds32_elf_hash_table (info);
12471 sym_ld_script = table->sym_ld_script;
12472
12473 if (check_start_export_sym)
12474 fprintf (sym_ld_script, "}\n");
12475
12476 return TRUE;
12477}
12478
12479static enum elf_reloc_type_class
12480nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12481 const asection *rel_sec ATTRIBUTE_UNUSED,
12482 const Elf_Internal_Rela *rela)
12483{
12484 switch ((int) ELF32_R_TYPE (rela->r_info))
12485 {
12486 case R_NDS32_RELATIVE:
12487 return reloc_class_relative;
12488 case R_NDS32_JMP_SLOT:
12489 return reloc_class_plt;
12490 case R_NDS32_COPY:
12491 return reloc_class_copy;
12492 default:
12493 return reloc_class_normal;
12494 }
12495}
12496
12497/* Put target dependent option into info hash table. */
12498void
12499bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12500 int relax_fp_as_gp,
12501 int eliminate_gc_relocs,
fbaf61ad
NC
12502 FILE * sym_ld_script,
12503 int hyper_relax,
12504 int tls_desc_trampoline,
12505 int load_store_relax)
35c08157
KLC
12506{
12507 struct elf_nds32_link_hash_table *table;
12508
12509 table = nds32_elf_hash_table (link_info);
12510 if (table == NULL)
12511 return;
12512
12513 table->relax_fp_as_gp = relax_fp_as_gp;
12514 table->eliminate_gc_relocs = eliminate_gc_relocs;
12515 table->sym_ld_script = sym_ld_script;
fbaf61ad
NC
12516 table->hyper_relax = hyper_relax;
12517 table->tls_desc_trampoline = tls_desc_trampoline;
35c08157 12518 table ->load_store_relax = load_store_relax;
35c08157
KLC
12519}
12520\f
fbaf61ad 12521
35c08157
KLC
12522/* These functions and data-structures are used for fp-as-gp
12523 optimization. */
12524
12525#define FAG_THRESHOLD 3 /* At least 3 gp-access. */
1c8f6a4d
KLC
12526/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12527 the read-only section and read-write section. */
12528#define FAG_WINDOW (508 - 32)
35c08157
KLC
12529
12530/* An nds32_fag represent a gp-relative access.
12531 We find best fp-base by using a sliding window
12532 to find a base address which can cover most gp-access. */
12533struct nds32_fag
12534{
12535 struct nds32_fag *next; /* NULL-teminated linked list. */
12536 bfd_vma addr; /* The address of this fag. */
12537 Elf_Internal_Rela **relas; /* The relocations associated with this fag.
12538 It is used for applying FP7U2_FLAG. */
12539 int count; /* How many times this address is referred.
12540 There should be exactly `count' relocations
12541 in relas. */
12542 int relas_capcity; /* The buffer size of relas.
12543 We use an array instead of linked-list,
12544 and realloc is used to adjust buffer size. */
12545};
12546
12547static void
12548nds32_fag_init (struct nds32_fag *head)
12549{
12550 memset (head, 0, sizeof (struct nds32_fag));
12551}
12552
12553static void
12554nds32_fag_verify (struct nds32_fag *head)
12555{
12556 struct nds32_fag *iter;
12557 struct nds32_fag *prev;
12558
12559 prev = NULL;
12560 iter = head->next;
12561 while (iter)
12562 {
12563 if (prev && prev->addr >= iter->addr)
12564 puts ("Bug in fp-as-gp insertion.");
12565 prev = iter;
12566 iter = iter->next;
12567 }
12568}
12569
12570/* Insert a fag in ascending order.
12571 If a fag of the same address already exists,
12572 they are chained by relas array. */
12573
12574static void
12575nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12576 Elf_Internal_Rela * rel)
12577{
12578 struct nds32_fag *iter;
12579 struct nds32_fag *new_fag;
12580 const int INIT_RELAS_CAP = 4;
12581
12582 for (iter = head;
12583 iter->next && iter->next->addr <= addr;
12584 iter = iter->next)
12585 /* Find somewhere to insert. */ ;
12586
12587 /* `iter' will be equal to `head' if the list is empty. */
12588 if (iter != head && iter->addr == addr)
12589 {
12590 /* The address exists in the list.
12591 Insert `rel' into relocation list, relas. */
12592
12593 /* Check whether relas is big enough. */
12594 if (iter->count >= iter->relas_capcity)
12595 {
12596 iter->relas_capcity *= 2;
12597 iter->relas = bfd_realloc
12598 (iter->relas, iter->relas_capcity * sizeof (void *));
12599 }
12600 iter->relas[iter->count++] = rel;
12601 return;
12602 }
12603
12604 /* This is a new address. Create a fag node for it. */
12605 new_fag = bfd_malloc (sizeof (struct nds32_fag));
12606 memset (new_fag, 0, sizeof (*new_fag));
12607 new_fag->addr = addr;
12608 new_fag->count = 1;
12609 new_fag->next = iter->next;
12610 new_fag->relas_capcity = INIT_RELAS_CAP;
12611 new_fag->relas = (Elf_Internal_Rela **)
12612 bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12613 new_fag->relas[0] = rel;
12614 iter->next = new_fag;
12615
12616 nds32_fag_verify (head);
12617}
12618
12619static void
12620nds32_fag_free_list (struct nds32_fag *head)
12621{
12622 struct nds32_fag *iter;
12623
12624 iter = head->next;
12625 while (iter)
12626 {
12627 struct nds32_fag *tmp = iter;
12628 iter = iter->next;
12629 free (tmp->relas);
12630 tmp->relas = NULL;
12631 free (tmp);
12632 }
12633}
12634
35c08157
KLC
12635/* Find the best fp-base address.
12636 The relocation associated with that address is returned,
12637 so we can track the symbol instead of a fixed address.
12638
12639 When relaxation, the address of an datum may change,
12640 because a text section is shrinked, so the data section
1c8f6a4d 12641 moves forward. If the aligments of text and data section
35c08157
KLC
12642 are different, their distance may change too.
12643 Therefore, tracking a fixed address is not appriate. */
12644
12645static int
12646nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12647{
12648 struct nds32_fag *base; /* First fag in the window. */
12649 struct nds32_fag *last; /* First fag outside the window. */
12650 int accu = 0; /* Usage accumulation. */
12651 struct nds32_fag *best; /* Best fag. */
12652 int baccu = 0; /* Best accumulation. */
12653
12654 /* Use first fag for initial, and find the last fag in the window.
12655
12656 In each iteration, we could simply subtract previous fag
12657 and accumulate following fags which are inside the window,
12658 untill we each the end. */
12659
1c8f6a4d
KLC
12660 if (head->next == NULL)
12661 {
12662 *bestpp = NULL;
12663 return 0;
12664 }
35c08157
KLC
12665
12666 /* Initialize base. */
12667 base = head->next;
12668 best = base;
12669 for (last = base;
12670 last && last->addr < base->addr + FAG_WINDOW;
12671 last = last->next)
12672 accu += last->count;
12673
12674 baccu = accu;
12675
12676 /* Record the best base in each iteration. */
12677 while (base->next)
1c8f6a4d
KLC
12678 {
12679 accu -= base->count;
12680 base = base->next;
12681 /* Account fags in window. */
12682 for (/* Nothing. */;
12683 last && last->addr < base->addr + FAG_WINDOW;
12684 last = last->next)
12685 accu += last->count;
12686
12687 /* A better fp-base? */
12688 if (accu > baccu)
12689 {
12690 best = base;
12691 baccu = accu;
12692 }
12693 }
35c08157
KLC
12694
12695 if (bestpp)
12696 *bestpp = best;
12697 return baccu;
12698}
12699
12700/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12701 so we can convert it fo fp-relative access later.
12702 `best_fag' is the best fp-base. Only those inside the window
12703 of best_fag is applied the flag. */
12704
12705static bfd_boolean
12706nds32_fag_mark_relax (struct bfd_link_info *link_info,
fbaf61ad 12707 asection *sec, struct nds32_fag *best_fag,
35c08157
KLC
12708 Elf_Internal_Rela *internal_relocs,
12709 Elf_Internal_Rela *irelend)
12710{
12711 struct nds32_fag *ifag;
12712 bfd_vma best_fpbase, gp;
12713 bfd *output_bfd;
12714
fbaf61ad 12715 output_bfd = sec->output_section->owner;
35c08157
KLC
12716 nds32_elf_final_sda_base (output_bfd, link_info, &gp, FALSE);
12717 best_fpbase = best_fag->addr;
12718
1c8f6a4d
KLC
12719 if (best_fpbase > gp + sdata_range[1][1]
12720 || best_fpbase < gp - sdata_range[1][0])
35c08157
KLC
12721 return FALSE;
12722
12723 /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12724 so we know they can be converted to lwi37.fp. */
12725 for (ifag = best_fag;
12726 ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12727 {
12728 int i;
12729
12730 for (i = 0; i < ifag->count; i++)
12731 {
12732 Elf_Internal_Rela *insn16_rel;
12733 Elf_Internal_Rela *fag_rel;
12734
12735 fag_rel = ifag->relas[i];
12736
12737 /* Only if this is within the WINDOWS, FP7U2_FLAG
12738 is applied. */
12739
12740 insn16_rel = find_relocs_at_address
12741 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12742
12743 if (insn16_rel != irelend)
12744 insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12745 }
12746 }
12747 return TRUE;
12748}
12749
1c8f6a4d
KLC
12750/* Reset INSN16 to clean fp as gp. */
12751
12752static void
12753nds32_fag_unmark_relax (struct nds32_fag *fag,
12754 Elf_Internal_Rela *internal_relocs,
12755 Elf_Internal_Rela *irelend)
12756{
12757 struct nds32_fag *ifag;
12758 int i;
12759 Elf_Internal_Rela *insn16_rel;
12760 Elf_Internal_Rela *fag_rel;
12761
12762 for (ifag = fag; ifag; ifag = ifag->next)
12763 {
12764 for (i = 0; i < ifag->count; i++)
12765 {
12766 fag_rel = ifag->relas[i];
12767
12768 /* Restore the INSN16 relocation. */
12769 insn16_rel = find_relocs_at_address
12770 (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12771
12772 if (insn16_rel != irelend)
12773 insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12774 }
12775 }
12776}
12777
35c08157
KLC
12778/* This is the main function of fp-as-gp optimization.
12779 It should be called by relax_section. */
12780
12781static bfd_boolean
12782nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12783 bfd *abfd, asection *sec,
12784 Elf_Internal_Rela *internal_relocs,
12785 Elf_Internal_Rela *irelend,
12786 Elf_Internal_Sym *isymbuf)
12787{
12788 Elf_Internal_Rela *begin_rel = NULL;
12789 Elf_Internal_Rela *irel;
12790 struct nds32_fag fag_head;
12791 Elf_Internal_Shdr *symtab_hdr;
12792 bfd_byte *contents;
1c8f6a4d 12793 bfd_boolean ifc_inside = FALSE;
35c08157
KLC
12794
12795 /* FIXME: Can we bfd_elf_link_read_relocs for the relocs? */
12796
12797 /* Per-function fp-base selection.
12798 1. Create a list for all the gp-relative access.
12799 2. Base on those gp-relative address,
12800 find a fp-base which can cover most access.
12801 3. Use the fp-base for fp-as-gp relaxation.
12802
12803 NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12804 we should
12805 1. delete the `la $fp, _FP_BASE_' instruction and
12806 2. not convert lwi.gp to lwi37.fp.
12807
12808 To delete the _FP_BASE_ instruction, we simply apply
12809 R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12810
12811 To suppress the conversion, we simply NOT to apply
12812 R_NDS32_INSN16_FP7U2_FLAG flag. */
12813
12814 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12815
0c4bd9d9 12816 if (!nds32_get_section_contents (abfd, sec, &contents, TRUE)
35c08157
KLC
12817 || !nds32_get_local_syms (abfd, sec, &isymbuf))
12818 return FALSE;
12819
12820 /* Check whether it is worth for fp-as-gp optimization,
12821 i.e., at least 3 gp-load.
12822
12823 Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12824 apply this optimization. */
12825
12826 for (irel = internal_relocs; irel < irelend; irel++)
12827 {
12828 /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12829 One we enter the begin of the region, we track all the LW/ST
12830 instructions, so when we leave the region, we try to find
12831 the best fp-base address for those LW/ST instructions. */
12832
12833 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12834 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12835 {
12836 /* Begin of the region. */
12837 if (begin_rel)
695344c0 12838 /* xgettext:c-format */
38f14ab8 12839 _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
35c08157
KLC
12840
12841 begin_rel = irel;
12842 nds32_fag_init (&fag_head);
1c8f6a4d 12843 ifc_inside = FALSE;
35c08157
KLC
12844 }
12845 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12846 && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12847 {
12848 int accu;
1c8f6a4d 12849 struct nds32_fag *best_fag, *tmp_fag;
35c08157
KLC
12850 int dist;
12851
12852 /* End of the region.
12853 Check whether it is worth to do fp-as-gp. */
12854
12855 if (begin_rel == NULL)
12856 {
695344c0 12857 /* xgettext:c-format */
38f14ab8
AM
12858 _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12859 abfd, sec);
35c08157
KLC
12860 continue;
12861 }
12862
12863 accu = nds32_fag_find_base (&fag_head, &best_fag);
12864
1c8f6a4d
KLC
12865 /* Clean FP7U2_FLAG because they may set ever. */
12866 tmp_fag = fag_head.next;
12867 nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12868
35c08157
KLC
12869 /* Check if it is worth, and FP_BASE is near enough to SDA_BASE. */
12870 if (accu < FAG_THRESHOLD
fbaf61ad 12871 || !nds32_fag_mark_relax (link_info, sec, best_fag,
35c08157
KLC
12872 internal_relocs, irelend))
12873 {
12874 /* Not worth to do fp-as-gp. */
12875 begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12876 begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12877 irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12878 irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12879 nds32_fag_free_list (&fag_head);
12880 begin_rel = NULL;
12881 continue;
12882 }
12883
12884 /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12885 so we use it to record the distance to the reloction of best
12886 fp-base. */
12887 dist = best_fag->relas[0] - begin_rel;
12888 BFD_ASSERT (dist > 0 && dist < 0xffffff);
12889 /* Use high 16 bits of addend to record the _FP_BASE_ matched
12890 relocation. And get the base value when relocating. */
1c8f6a4d 12891 begin_rel->r_addend &= (0x1 << 16) - 1;
35c08157
KLC
12892 begin_rel->r_addend |= dist << 16;
12893
12894 nds32_fag_free_list (&fag_head);
12895 begin_rel = NULL;
12896 }
12897
1c8f6a4d 12898 if (begin_rel == NULL || ifc_inside)
35c08157
KLC
12899 /* Skip if we are not in the region of fp-as-gp. */
12900 continue;
12901
12902 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12903 || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12904 {
12905 bfd_vma addr;
12906 uint32_t insn;
12907
12908 /* A gp-relative access is found. Insert it to the fag-list. */
12909
12910 /* Rt is necessary an RT3, so it can be converted to lwi37.fp. */
12911 insn = bfd_getb32 (contents + irel->r_offset);
12912 if (!N32_IS_RT3 (insn))
12913 continue;
12914
12915 addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12916 nds32_fag_insert (&fag_head, addr, irel);
12917 }
12918 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12919 {
12920 begin_rel = NULL;
12921 }
1c8f6a4d
KLC
12922 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12923 || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12924 {
12925 /* Suppress fp as gp when encounter ifc. */
12926 ifc_inside = TRUE;
12927 }
35c08157
KLC
12928 }
12929
12930 return TRUE;
12931}
12932
12933/* Remove unused `la $fp, _FD_BASE_' instruction. */
12934
12935static bfd_boolean
12936nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12937 Elf_Internal_Rela *internal_relocs,
12938 Elf_Internal_Rela *irelend)
12939{
12940 Elf_Internal_Rela *irel;
12941 Elf_Internal_Shdr *symtab_hdr;
12942 bfd_byte *contents = NULL;
12943 nds32_elf_blank_t *relax_blank_list = NULL;
12944 bfd_boolean result = TRUE;
12945 bfd_boolean unused_region = FALSE;
12946
12947 /*
fbaf61ad
NC
12948 NOTE: Disable fp-as-gp if we encounter ifcall relocations:
12949 R_NDS32_17IFC_PCREL_RELA
12950 R_NDS32_10IFCU_PCREL_RELA. */
35c08157
KLC
12951
12952 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
0c4bd9d9 12953 nds32_get_section_contents (abfd, sec, &contents, TRUE);
35c08157
KLC
12954
12955 for (irel = internal_relocs; irel < irelend; irel++)
12956 {
12957 /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
12958 we marked to in previous pass.
12959 DO NOT scan relocations again, since we've alreadly decided it
12960 and set the flag. */
12961 const char *syname;
12962 int syndx;
12963 uint32_t insn;
12964
12965 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12966 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12967 unused_region = TRUE;
12968 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12969 && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
12970 unused_region = FALSE;
12971
12972 /* We're not in the region. */
12973 if (!unused_region)
12974 continue;
12975
12976 /* _FP_BASE_ must be a GLOBAL symbol. */
12977 syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
12978 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
12979 continue;
12980
12981 /* The symbol name must be _FP_BASE_. */
12982 syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
12983 if (strcmp (syname, FP_BASE_NAME) != 0)
12984 continue;
12985
12986 if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
12987 {
12988 /* addi.gp $fp, -256 */
12989 insn = bfd_getb32 (contents + irel->r_offset);
12990 if (insn != INSN_ADDIGP_TO_FP)
12991 continue;
12992 }
12993 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
12994 {
12995 /* addi $fp, $gp, -256 */
12996 insn = bfd_getb32 (contents + irel->r_offset);
12997 if (insn != INSN_ADDI_GP_TO_FP)
12998 continue;
12999 }
13000 else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13001 {
13002 /* movi $fp, FP_BASE */
13003 insn = bfd_getb32 (contents + irel->r_offset);
13004 if (insn != INSN_MOVI_TO_FP)
13005 continue;
13006 }
13007 else
13008 continue;
13009
13010 /* We got here because a FP_BASE instruction is found. */
13011 if (!insert_nds32_elf_blank_recalc_total
13012 (&relax_blank_list, irel->r_offset, 4))
13013 goto error_return;
13014 }
13015
dc1e8a47 13016 finish:
35c08157
KLC
13017 if (relax_blank_list)
13018 {
13019 nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13020 relax_blank_list = NULL;
13021 }
13022 return result;
13023
dc1e8a47 13024 error_return:
35c08157
KLC
13025 result = FALSE;
13026 goto finish;
13027}
1c8f6a4d
KLC
13028
13029/* This is a version of bfd_generic_get_relocated_section_contents.
13030 We need this variety because relaxation will modify the dwarf
13031 infomation. When there is undefined symbol reference error mesage,
13032 linker need to dump line number where the symbol be used. However
13033 the address is be relaxed, it can not get the original dwarf contents.
13034 The variety only modify function call for reading in the section. */
13035
13036static bfd_byte *
13037nds32_elf_get_relocated_section_contents (bfd *abfd,
13038 struct bfd_link_info *link_info,
13039 struct bfd_link_order *link_order,
13040 bfd_byte *data,
13041 bfd_boolean relocatable,
13042 asymbol **symbols)
13043{
13044 bfd *input_bfd = link_order->u.indirect.section->owner;
13045 asection *input_section = link_order->u.indirect.section;
13046 long reloc_size;
13047 arelent **reloc_vector;
13048 long reloc_count;
13049
13050 reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13051 if (reloc_size < 0)
13052 return NULL;
13053
13054 /* Read in the section. */
0c4bd9d9 13055 if (!nds32_get_section_contents (input_bfd, input_section, &data, FALSE))
1c8f6a4d
KLC
13056 return NULL;
13057
13058 if (reloc_size == 0)
13059 return data;
13060
13061 reloc_vector = (arelent **) bfd_malloc (reloc_size);
13062 if (reloc_vector == NULL)
13063 return NULL;
13064
13065 reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13066 reloc_vector, symbols);
13067 if (reloc_count < 0)
13068 goto error_return;
13069
13070 if (reloc_count > 0)
13071 {
13072 arelent **parent;
13073 for (parent = reloc_vector; *parent != NULL; parent++)
13074 {
13075 char *error_message = NULL;
13076 asymbol *symbol;
13077 bfd_reloc_status_type r;
13078
13079 symbol = *(*parent)->sym_ptr_ptr;
13080 if (symbol->section && discarded_section (symbol->section))
13081 {
0930cb30 13082 bfd_vma off;
1c8f6a4d
KLC
13083 static reloc_howto_type none_howto
13084 = HOWTO (0, 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL,
13085 "unused", FALSE, 0, 0, FALSE);
13086
bb294208
AM
13087 off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13088 input_section);
0930cb30
AM
13089 _bfd_clear_contents ((*parent)->howto, input_bfd,
13090 input_section, data, off);
1c8f6a4d
KLC
13091 (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13092 (*parent)->addend = 0;
13093 (*parent)->howto = &none_howto;
13094 r = bfd_reloc_ok;
13095 }
13096 else
13097 r = bfd_perform_relocation (input_bfd, *parent, data,
13098 input_section,
13099 relocatable ? abfd : NULL,
13100 &error_message);
13101
13102 if (relocatable)
13103 {
13104 asection *os = input_section->output_section;
13105
13106 /* A partial link, so keep the relocs. */
13107 os->orelocation[os->reloc_count] = *parent;
13108 os->reloc_count++;
13109 }
13110
13111 if (r != bfd_reloc_ok)
13112 {
13113 switch (r)
13114 {
13115 case bfd_reloc_undefined:
1a72702b
AM
13116 (*link_info->callbacks->undefined_symbol)
13117 (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13118 input_bfd, input_section, (*parent)->address, TRUE);
1c8f6a4d
KLC
13119 break;
13120 case bfd_reloc_dangerous:
13121 BFD_ASSERT (error_message != NULL);
1a72702b
AM
13122 (*link_info->callbacks->reloc_dangerous)
13123 (link_info, error_message,
13124 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13125 break;
13126 case bfd_reloc_overflow:
1a72702b
AM
13127 (*link_info->callbacks->reloc_overflow)
13128 (link_info, NULL,
13129 bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13130 (*parent)->howto->name, (*parent)->addend,
13131 input_bfd, input_section, (*parent)->address);
1c8f6a4d
KLC
13132 break;
13133 case bfd_reloc_outofrange:
13134 /* PR ld/13730:
13135 This error can result when processing some partially
13136 complete binaries. Do not abort, but issue an error
13137 message instead. */
13138 link_info->callbacks->einfo
695344c0 13139 /* xgettext:c-format */
c1c8c1ef 13140 (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
1c8f6a4d
KLC
13141 abfd, input_section, * parent);
13142 goto error_return;
13143
13144 default:
13145 abort ();
13146 break;
13147 }
13148 }
13149 }
13150 }
13151
13152 free (reloc_vector);
13153 return data;
13154
dc1e8a47 13155 error_return:
1c8f6a4d
KLC
13156 free (reloc_vector);
13157 return NULL;
13158}
fbaf61ad
NC
13159
13160/* Check target symbol. */
13161
13162static bfd_boolean
13163nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13164{
13165 if (!sym || !sym->name || sym->name[0] != '$')
13166 return FALSE;
13167 return TRUE;
13168}
13169
13170/* nds32 find maybe function sym. Ignore target special symbol
13171 first, and then go the general function. */
13172
13173static bfd_size_type
13174nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13175 bfd_vma *code_off)
13176{
13177 if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13178 return 0;
13179
13180 return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13181}
35c08157 13182\f
35c08157 13183
fbaf61ad
NC
13184/* Do TLS model conversion. */
13185
13186typedef struct relax_group_list_t
13187{
13188 Elf_Internal_Rela *relo;
13189 struct relax_group_list_t *next;
13190 struct relax_group_list_t *next_sibling;
13191 int id;
13192} relax_group_list_t;
13193
13194int
13195list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13196
13197int
13198list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13199
13200void
13201dump_chain (relax_group_list_t *pHead);
13202
13203int
13204list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13205{
13206 relax_group_list_t *pNext = pHead;
13207
13208 /* Find place. */
13209 while (pNext->next)
13210 {
13211 if (pNext->next->id > (int) pElem->r_addend)
13212 break;
13213
13214 pNext = pNext->next;
13215 }
13216
13217 /* Insert node. */
13218 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13219 if (!pNew)
13220 return FALSE;
13221
13222 relax_group_list_t *tmp = pNext->next;
13223 pNext->next = pNew;
13224
13225 pNew->id = pElem->r_addend;
13226 pNew->relo = pElem;
13227 pNew->next = tmp;
13228 pNew->next_sibling = NULL;
13229
13230 return TRUE;
13231}
13232
13233int
13234list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13235{
13236 relax_group_list_t *pNext = pNode;
13237
13238 /* Find place. */
13239 while (pNext->next_sibling)
13240 {
13241 pNext = pNext->next_sibling;
13242 }
13243
13244 /* Insert node. */
13245 relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13246 if (!pNew)
13247 return FALSE;
13248
13249 relax_group_list_t *tmp = pNext->next_sibling;
13250 pNext->next_sibling = pNew;
13251
13252 pNew->id = -1;
13253 pNew->relo = pElem;
13254 pNew->next = NULL;
13255 pNew->next_sibling = tmp;
13256
13257 return TRUE;
13258}
13259
13260void
13261dump_chain (relax_group_list_t *pHead)
13262{
13263 relax_group_list_t *pNext = pHead->next;
13264 while (pNext)
13265 {
13266 printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13267 relax_group_list_t *pNextSib = pNext->next_sibling;
13268 while (pNextSib)
13269 {
13270 printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13271 pNextSib = pNextSib->next_sibling;
13272 }
13273 pNext = pNext->next;
13274 printf("\n");
13275 }
13276}
13277
13278/* Check R_NDS32_RELAX_GROUP of each section.
13279 There might be multiple sections in one object file. */
13280
13281int
13282elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13283{
13284 elf32_nds32_relax_group_t *relax_group_ptr =
13285 elf32_nds32_relax_group_ptr (abfd);
13286
13287 int min_id = relax_group_ptr->min_id;
13288 int max_id = relax_group_ptr->max_id;
13289
13290 Elf_Internal_Rela *rel;
13291 Elf_Internal_Rela *relend;
13292 Elf_Internal_Rela *relocs;
13293 enum elf_nds32_reloc_type rtype;
13294
13295 do
13296 {
13297 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13298 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13299 TRUE /* keep_memory */);
13300 if (relocs == NULL)
13301 break;
13302
13303 /* Check R_NDS32_RELAX_GROUP. */
13304 relend = relocs + asec->reloc_count;
13305 for (rel = relocs; rel < relend; rel++)
13306 {
13307 int id;
13308 rtype = ELF32_R_TYPE (rel->r_info);
13309 if (rtype != R_NDS32_RELAX_GROUP)
13310 continue;
13311
13312 id = rel->r_addend;
13313 if (id < min_id)
13314 min_id = id;
13315 else if (id > max_id)
13316 max_id = id;
13317 }
13318 }
13319 while (FALSE);
13320
c9594989 13321 if (elf_section_data (asec)->relocs != relocs)
fbaf61ad
NC
13322 free (relocs);
13323
13324 if ((min_id != relax_group_ptr->min_id)
13325 || (max_id != relax_group_ptr->max_id))
13326 {
13327 relax_group_ptr->count = max_id - min_id + 1;
13328 BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13329 relax_group_ptr->min_id = min_id;
13330 BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13331 relax_group_ptr->max_id = max_id;
13332 }
13333
13334 return relax_group_ptr->count;
13335}
13336
13337/* Reorder RELAX_GROUP ID when command line option '-r' is applied. */
13338struct section_id_list_t *relax_group_section_id_list = NULL;
13339
13340struct section_id_list_t *
13341elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13342{
13343 struct section_id_list_t *result = NULL;
13344 struct section_id_list_t *lst = *lst_ptr;
13345
13346 if (NULL == lst)
13347 {
13348 result = (struct section_id_list_t *) calloc
13349 (1, sizeof (struct section_id_list_t));
13350 BFD_ASSERT (result); /* Feed me. */
13351 result->id = id;
13352 *lst_ptr = result;
13353 }
13354 else
13355 {
13356 struct section_id_list_t *cur = lst;
13357 struct section_id_list_t *prv = NULL;
13358 struct section_id_list_t *sec = NULL;
13359
13360 while (cur)
13361 {
13362 if (cur->id < id)
13363 {
13364 prv = cur;
13365 cur = cur->next;
13366 continue;
13367 }
13368
13369 if (cur->id > id)
13370 {
13371 cur = NULL; /* To insert after prv. */
13372 sec = cur; /* In case prv == NULL. */
13373 }
13374
13375 break;
13376 }
13377
13378 if (NULL == cur)
13379 {
13380 /* Insert after prv. */
13381 result = (struct section_id_list_t *) calloc
13382 (1, sizeof (struct section_id_list_t));
13383 BFD_ASSERT (result); /* Feed me. */
13384 result->id = id;
13385 if (NULL != prv)
13386 {
13387 result->next = prv->next;
13388 prv->next = result;
13389 }
13390 else
13391 {
13392 *lst_ptr = result;
13393 result->next = sec;
13394 }
13395 }
13396 }
13397
13398 return result;
13399}
13400
13401int
13402elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13403{
13404 static int next_relax_group_bias = 0;
13405
13406 elf32_nds32_relax_group_t *relax_group_ptr =
13407 elf32_nds32_relax_group_ptr (abfd);
13408
13409 bfd_boolean result = TRUE;
13410 Elf_Internal_Rela *rel;
13411 Elf_Internal_Rela *relend;
13412 Elf_Internal_Rela *relocs = NULL;
13413 enum elf_nds32_reloc_type rtype;
13414 struct section_id_list_t *node = NULL;
13415 int count = 0;
13416
13417 do
13418 {
13419 if (0 == relax_group_ptr->count)
13420 break;
13421
13422 /* Check if this section has been handled. */
13423 node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13424 if (NULL == node)
13425 break; /* Hit, the section id has handled. */
13426
13427 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13428 relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13429 TRUE /* keep_memory */);
13430 if (relocs == NULL)
13431 {
13432 BFD_ASSERT (0); /* feed me */
13433 break;
13434 }
13435
13436 /* Allocate group id bias for this bfd! */
13437 if (0 == relax_group_ptr->init)
13438 {
13439 relax_group_ptr->bias = next_relax_group_bias;
13440 next_relax_group_bias += relax_group_ptr->count;
13441 relax_group_ptr->init = 1;
13442 }
13443
13444 /* Reorder relax group groups. */
13445 relend = relocs + asec->reloc_count;
13446 for (rel = relocs; rel < relend; rel++)
13447 {
13448 rtype = ELF32_R_TYPE(rel->r_info);
13449 if (rtype != R_NDS32_RELAX_GROUP)
13450 continue;
13451
13452 /* Change it. */
13453 rel->r_addend += relax_group_ptr->bias;
13454 /* Debugging count. */
13455 count++;
13456 }
13457 }
13458 while (FALSE);
13459
c9594989 13460 if (elf_section_data (asec)->relocs != relocs)
fbaf61ad
NC
13461 free (relocs);
13462
13463 return result;
13464}
13465
13466int
13467nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13468 struct bfd_link_info *lnkinfo)
13469{
13470 bfd_boolean result = TRUE;
13471 Elf_Internal_Rela *irel;
13472 Elf_Internal_Rela *irelend;
13473 Elf_Internal_Rela *internal_relocs;
13474 unsigned long r_symndx;
13475 enum elf_nds32_reloc_type r_type;
13476
13477 Elf_Internal_Sym *local_syms = NULL;
13478 bfd_byte *contents = NULL;
13479
13480 relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13481
13482 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13483 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
13484 sym_hashes = elf_sym_hashes (inbfd);
13485 sym_hashes_end =
13486 sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
13487 if (!elf_bad_symtab (inbfd))
13488 sym_hashes_end -= symtab_hdr->sh_info;
13489
13490 /* Reorder RELAX_GROUP when command line option '-r' is applied. */
13491 if (bfd_link_relocatable (lnkinfo))
13492 {
13493 elf32_nds32_unify_relax_group (inbfd, insec);
13494 return result;
13495 }
13496
13497 /* Relocations MUST be kept in memory, because relaxation adjust them. */
13498 internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13499 TRUE /* keep_memory */);
13500 if (internal_relocs == NULL)
13501 goto error_return;
13502
13503 irelend = internal_relocs + insec->reloc_count;
13504 irel = find_relocs_at_address (internal_relocs, internal_relocs,
13505 irelend, R_NDS32_RELAX_ENTRY);
13506 if (irel == irelend)
13507 goto finish;
13508
13509 /* Chain/remove groups. */
13510 for (irel = internal_relocs; irel < irelend; irel++)
13511 {
13512 r_symndx = ELF32_R_SYM (irel->r_info);
13513 r_type = ELF32_R_TYPE (irel->r_info);
13514 if (r_type != R_NDS32_RELAX_GROUP)
13515 continue;
13516
13517 /* Remove it. */
13518 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13519 /* Chain it now. */
13520 if (!list_insert (&chain, irel))
13521 goto error_return;
13522 }
13523
13524 /* Collect group relocations. */
13525 /* Presume relocations are sorted. */
13526 relax_group_list_t *pNext = chain.next;
13527 while (pNext)
13528 {
13529 for (irel = internal_relocs; irel < irelend; irel++)
13530 {
13531 if (irel->r_offset == pNext->relo->r_offset)
13532 {
13533 /* Ignore Non-TLS relocation types. */
13534 r_type = ELF32_R_TYPE (irel->r_info);
13535 if ((R_NDS32_TLS_LE_HI20 > r_type)
13536 || (R_NDS32_RELAX_ENTRY == r_type))
13537 continue;
13538
13539 if (!list_insert_sibling (pNext, irel))
13540 goto error_return;
13541 }
13542 else if (irel->r_offset > pNext->relo->r_offset)
13543 {
13544 pNext = pNext->next;
13545 if (!pNext)
13546 break;
13547
13548 bfd_vma current_offset = pNext->relo->r_offset;
13549 if (irel->r_offset > current_offset)
13550 irel = internal_relocs; /* restart from head */
13551 else
13552 --irel; /* Check current irel again. */
13553 continue;
13554 }
13555 else
13556 {
13557 /* This shouldn't be reached. */
13558 }
13559 }
13560 if (pNext)
13561 pNext = pNext->next;
13562 }
13563
13564#ifdef DUBUG_VERBOSE
13565 dump_chain(&chain);
13566#endif
13567
13568 /* Get symbol table and section content. */
13569 if (incontents)
13570 contents = incontents;
13571 else if (!nds32_get_section_contents (inbfd, insec, &contents, TRUE)
13572 || !nds32_get_local_syms (inbfd, insec, &local_syms))
13573 goto error_return;
13574
13575 char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13576
13577 /* Convert TLS model each group if necessary. */
13578 pNext = chain.next;
13579
13580 int cur_grp_id = -1;
13581 int sethi_rt = -1;
13582 int add_rt = -1;
13583 enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13584
13585 tls_type = org_tls_type = eff_tls_type = 0;
13586
13587 while (pNext)
13588 {
13589 relax_group_list_t *pNextSig = pNext->next_sibling;
13590 while (pNextSig)
13591 {
13592 struct elf_link_hash_entry *h = NULL;
13593
13594 irel = pNextSig->relo;
13595 r_symndx = ELF32_R_SYM(irel->r_info);
13596 r_type = ELF32_R_TYPE(irel->r_info);
13597
13598 if (pNext->id != cur_grp_id)
13599 {
13600 cur_grp_id = pNext->id;
13601 org_tls_type = get_tls_type (r_type, NULL);
13602 if (r_symndx >= symtab_hdr->sh_info)
13603 {
13604 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13605 while (h->root.type == bfd_link_hash_indirect
13606 || h->root.type == bfd_link_hash_warning)
13607 h = (struct elf_link_hash_entry *) h->root.u.i.link;
13608 tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13609 }
13610 else
13611 {
13612 tls_type = local_got_tls_type
13613 ? local_got_tls_type[r_symndx]
13614 : GOT_NORMAL;
13615 }
13616
13617 eff_tls_type = 1 << (fls (tls_type) - 1);
13618 sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13619 }
13620
13621 if (eff_tls_type != org_tls_type)
13622 {
13623 switch (org_tls_type)
13624 {
13625 /* DESC to IEGP/IE/LE. */
13626 case GOT_TLS_DESC:
13627 switch (eff_tls_type)
13628 {
13629 case GOT_TLS_IE:
13630 switch (r_type)
13631 {
13632 case R_NDS32_TLS_DESC_HI20:
13633 irel->r_info = ELF32_R_INFO(r_symndx,
13634 R_NDS32_TLS_IE_HI20);
13635 break;
13636 case R_NDS32_TLS_DESC_LO12:
13637 irel->r_info = ELF32_R_INFO(r_symndx,
13638 R_NDS32_TLS_IE_LO12);
13639 break;
13640 case R_NDS32_TLS_DESC_ADD:
13641 {
13642 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13643 add_rt = N32_RT5 (insn);
13644 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13645 bfd_putb32 (insn, contents + irel->r_offset);
13646
13647 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13648 }
13649 break;
13650 case R_NDS32_TLS_DESC_FUNC:
13651 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13652 irel->r_info = ELF32_R_INFO(r_symndx,
13653 R_NDS32_RELAX_REMOVE);
13654 break;
13655 case R_NDS32_TLS_DESC_CALL:
13656 {
13657 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13658 REG_TP);
13659 bfd_putb32 (insn, contents + irel->r_offset);
13660
13661 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13662 }
13663 break;
13664 case R_NDS32_LOADSTORE:
13665 case R_NDS32_PTR:
13666 case R_NDS32_PTR_RESOLVED:
13667 case R_NDS32_NONE:
13668 case R_NDS32_LABEL:
13669 break;
13670 default:
13671 BFD_ASSERT(0);
13672 break;
13673 }
13674 break;
13675 case GOT_TLS_IEGP:
13676 switch (r_type)
13677 {
13678 case R_NDS32_TLS_DESC_HI20:
13679 irel->r_info = ELF32_R_INFO(r_symndx,
13680 R_NDS32_TLS_IEGP_HI20);
13681 break;
13682 case R_NDS32_TLS_DESC_LO12:
13683 irel->r_info = ELF32_R_INFO(r_symndx,
13684 R_NDS32_TLS_IEGP_LO12);
13685 break;
13686 case R_NDS32_TLS_DESC_ADD:
13687 {
13688 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13689 add_rt = N32_RT5 (insn);
13690 insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13691 bfd_putb32 (insn, contents + irel->r_offset);
13692
13693 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13694 }
13695 break;
13696 case R_NDS32_TLS_DESC_FUNC:
13697 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13698 irel->r_info = ELF32_R_INFO(r_symndx,
13699 R_NDS32_RELAX_REMOVE);
13700 break;
13701 case R_NDS32_TLS_DESC_CALL:
13702 {
13703 uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13704 REG_TP);
13705 bfd_putb32 (insn, contents + irel->r_offset);
13706
13707 irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13708 }
13709 break;
13710 case R_NDS32_LOADSTORE:
13711 case R_NDS32_PTR:
13712 case R_NDS32_PTR_RESOLVED:
13713 case R_NDS32_NONE:
13714 case R_NDS32_LABEL:
13715 break;
13716 default:
13717 BFD_ASSERT(0);
13718 break;
13719 }
13720 break;
13721 case GOT_TLS_LE:
13722 switch (r_type)
13723 {
13724 case R_NDS32_TLS_DESC_HI20:
13725 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13726 break;
13727 case R_NDS32_TLS_DESC_LO12:
13728 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13729 break;
13730 case R_NDS32_TLS_DESC_ADD:
13731 {
13732 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13733
13734 add_rt = N32_RT5 (insn);
13735 insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13736 bfd_putb32 (insn, contents + irel->r_offset);
13737
13738 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13739 }
13740 break;
13741 case R_NDS32_TLS_DESC_FUNC:
13742 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13743 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13744 break;
13745 case R_NDS32_TLS_DESC_CALL:
13746 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13747 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13748 break;
13749 case R_NDS32_LOADSTORE:
13750 case R_NDS32_PTR:
13751 case R_NDS32_PTR_RESOLVED:
13752 case R_NDS32_NONE:
13753 case R_NDS32_LABEL:
13754 break;
13755 default:
13756 BFD_ASSERT(0);
13757 break;
13758 }
13759 break;
13760 default:
13761 break;
13762 }
13763 break;
13764 /* IEGP to IE/LE. */
13765 case GOT_TLS_IEGP:
13766 switch (eff_tls_type)
13767 {
13768 case GOT_TLS_IE:
13769 switch (r_type)
13770 {
13771 case R_NDS32_TLS_IEGP_HI20:
13772 irel->r_info = ELF32_R_INFO(r_symndx,
13773 R_NDS32_TLS_IE_HI20);
13774 break;
13775 case R_NDS32_TLS_IEGP_LO12:
13776 irel->r_info = ELF32_R_INFO(r_symndx,
13777 R_NDS32_TLS_IE_LO12);
13778 break;
13779 case R_NDS32_PTR_RESOLVED:
13780 {
13781 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13782
13783 add_rt = N32_RT5 (insn);
13784 insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13785 bfd_putb32 (insn, contents + irel->r_offset);
13786 }
13787 break;
13788 case R_NDS32_TLS_IEGP_LW:
13789 break;
13790 case R_NDS32_LOADSTORE:
13791 case R_NDS32_PTR:
13792 case R_NDS32_NONE:
13793 case R_NDS32_LABEL:
13794 break;
13795 default:
13796 BFD_ASSERT(0);
13797 break;
13798 }
13799 break;
13800 case GOT_TLS_LE:
13801 switch (r_type)
13802 {
13803 case R_NDS32_TLS_IEGP_HI20:
13804 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13805 break;
13806 case R_NDS32_TLS_IEGP_LO12:
13807 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13808 break;
13809 case R_NDS32_TLS_IEGP_LW:
13810 bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13811 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13812 break;
13813 case R_NDS32_LOADSTORE:
13814 case R_NDS32_PTR:
13815 case R_NDS32_NONE:
13816 case R_NDS32_LABEL:
13817 case R_NDS32_PTR_RESOLVED:
13818 break;
13819 default:
13820 BFD_ASSERT(0);
13821 break;
13822 }
13823 break;
13824 default:
13825 break;
13826 }
13827 break;
13828 /* IE to LE. */
13829 case GOT_TLS_IE:
13830 switch (eff_tls_type)
13831 {
13832 case GOT_TLS_LE:
13833 switch (r_type)
13834 {
13835 case R_NDS32_TLS_IE_HI20:
13836 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13837 break;
13838 case R_NDS32_TLS_IE_LO12S2:
13839 {
13840 uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13841
13842 add_rt = N32_RT5 (insn);
13843 insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13844 bfd_putb32 (insn, contents + irel->r_offset);
13845
13846 irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13847 }
13848 break;
13849 case R_NDS32_LOADSTORE:
13850 case R_NDS32_PTR:
13851 case R_NDS32_NONE:
13852 case R_NDS32_LABEL:
13853 break;
13854 default:
13855 BFD_ASSERT(0);
13856 break;
13857 }
13858 break;
13859 default:
13860 break;
13861 }
13862 break;
13863 default:
13864 break;
13865 }
13866 }
13867 pNextSig = pNextSig->next_sibling;
13868 }
13869
13870#if 1
13871 pNext = pNext->next;
13872#else
13873 while (pNext)
13874 {
13875 if (pNext->id != cur_grp_id)
13876 break;
13877 pNext = pNext->next;
13878 }
13879#endif
13880 }
13881
dc1e8a47 13882 finish:
fbaf61ad
NC
13883 if (incontents)
13884 contents = NULL;
13885
c9594989 13886 if (elf_section_data (insec)->relocs != internal_relocs)
fbaf61ad
NC
13887 free (internal_relocs);
13888
c9594989 13889 if (elf_section_data (insec)->this_hdr.contents != contents)
fbaf61ad
NC
13890 free (contents);
13891
c9594989 13892 if (symtab_hdr->contents != (bfd_byte *) local_syms)
fbaf61ad
NC
13893 free (local_syms);
13894
13895 if (chain.next)
13896 {
13897 pNext = chain.next;
13898 relax_group_list_t *pDel;
13899 while (pNext)
13900 {
13901 pDel = pNext;
13902 pNext = pNext->next;
13903 free (pDel);
13904 }
13905 }
13906
13907 return result;
13908
dc1e8a47 13909 error_return:
fbaf61ad
NC
13910 result = FALSE;
13911 goto finish;
13912}
13913
13914/* End TLS model conversion. */
13915
35c08157
KLC
13916#define ELF_ARCH bfd_arch_nds32
13917#define ELF_MACHINE_CODE EM_NDS32
13918#define ELF_MAXPAGESIZE 0x1000
07d6d2b8 13919#define ELF_TARGET_ID NDS32_ELF_DATA
35c08157 13920
6d00b590 13921#define TARGET_BIG_SYM nds32_elf32_be_vec
35c08157 13922#define TARGET_BIG_NAME "elf32-nds32be"
6d00b590 13923#define TARGET_LITTLE_SYM nds32_elf32_le_vec
35c08157
KLC
13924#define TARGET_LITTLE_NAME "elf32-nds32le"
13925
13926#define elf_info_to_howto nds32_info_to_howto
13927#define elf_info_to_howto_rel nds32_info_to_howto_rel
13928
13929#define bfd_elf32_bfd_link_hash_table_create nds32_elf_link_hash_table_create
13930#define bfd_elf32_bfd_merge_private_bfd_data nds32_elf_merge_private_bfd_data
13931#define bfd_elf32_bfd_print_private_bfd_data nds32_elf_print_private_bfd_data
13932#define bfd_elf32_bfd_relax_section nds32_elf_relax_section
13933#define bfd_elf32_bfd_set_private_flags nds32_elf_set_private_flags
13934
07d6d2b8 13935#define bfd_elf32_mkobject nds32_elf_mkobject
35c08157
KLC
13936#define elf_backend_action_discarded nds32_elf_action_discarded
13937#define elf_backend_add_symbol_hook nds32_elf_add_symbol_hook
13938#define elf_backend_check_relocs nds32_elf_check_relocs
13939#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
13940#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
13941#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
13942#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
13943#define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
13944#define elf_backend_relocate_section nds32_elf_relocate_section
13945#define elf_backend_gc_mark_hook nds32_elf_gc_mark_hook
35c08157
KLC
13946#define elf_backend_grok_prstatus nds32_elf_grok_prstatus
13947#define elf_backend_grok_psinfo nds32_elf_grok_psinfo
13948#define elf_backend_reloc_type_class nds32_elf_reloc_type_class
13949#define elf_backend_copy_indirect_symbol nds32_elf_copy_indirect_symbol
13950#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
13951#define elf_backend_output_arch_syms nds32_elf_output_arch_syms
13952#define elf_backend_object_p nds32_elf_object_p
13953#define elf_backend_final_write_processing nds32_elf_final_write_processing
13954#define elf_backend_special_sections nds32_elf_special_sections
bf577467 13955#define elf_backend_section_flags nds32_elf_section_flags
1c8f6a4d 13956#define bfd_elf32_bfd_get_relocated_section_contents \
07d6d2b8 13957 nds32_elf_get_relocated_section_contents
fbaf61ad
NC
13958#define bfd_elf32_bfd_is_target_special_symbol nds32_elf_is_target_special_symbol
13959#define elf_backend_maybe_function_sym nds32_elf_maybe_function_sym
35c08157
KLC
13960
13961#define elf_backend_can_gc_sections 1
13962#define elf_backend_can_refcount 1
13963#define elf_backend_want_got_plt 1
13964#define elf_backend_plt_readonly 1
13965#define elf_backend_want_plt_sym 0
13966#define elf_backend_got_header_size 12
13967#define elf_backend_may_use_rel_p 1
13968#define elf_backend_default_use_rela_p 1
13969#define elf_backend_may_use_rela_p 1
fbaf61ad 13970#define elf_backend_dtrel_excludes_plt 0
35c08157
KLC
13971
13972#include "elf32-target.h"
13973
13974#undef ELF_MAXPAGESIZE
13975#define ELF_MAXPAGESIZE 0x2000
13976
fbaf61ad 13977#undef TARGET_BIG_SYM
6d00b590 13978#define TARGET_BIG_SYM nds32_elf32_linux_be_vec
fbaf61ad 13979#undef TARGET_BIG_NAME
35c08157 13980#define TARGET_BIG_NAME "elf32-nds32be-linux"
fbaf61ad 13981#undef TARGET_LITTLE_SYM
6d00b590 13982#define TARGET_LITTLE_SYM nds32_elf32_linux_le_vec
fbaf61ad 13983#undef TARGET_LITTLE_NAME
35c08157 13984#define TARGET_LITTLE_NAME "elf32-nds32le-linux"
fbaf61ad 13985#undef elf32_bed
35c08157
KLC
13986#define elf32_bed elf32_nds32_lin_bed
13987
13988#include "elf32-target.h"
This page took 1.098564 seconds and 4 git commands to generate.