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