Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-nds32.c
Line
Count
Source
1
/* NDS32-specific support for 32-bit ELF.
2
   Copyright (C) 2012-2026 Free Software Foundation, Inc.
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
20
   02110-1301, USA.  */
21
22
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#include "elf-bfd.h"
28
#include "libiberty.h"
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
35
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
36
22
#define OCTETS_PER_BYTE(ABFD, SEC) 1
37
38
/* Relocation HOWTO functions.  */
39
static bfd_reloc_status_type nds32_elf_ignore_reloc
40
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41
static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43
static bfd_reloc_status_type nds32_elf_hi20_reloc
44
  (bfd *, arelent *, asymbol *, void *,
45
   asection *, bfd *, char **);
46
static bfd_reloc_status_type nds32_elf_lo12_reloc
47
  (bfd *, arelent *, asymbol *, void *,
48
   asection *, bfd *, char **);
49
static bfd_reloc_status_type nds32_elf_generic_reloc
50
  (bfd *, arelent *, asymbol *, void *,
51
   asection *, bfd *, char **);
52
static bfd_reloc_status_type nds32_elf_sda15_reloc
53
  (bfd *, arelent *, asymbol *, void *,
54
   asection *, bfd *, char **);
55
56
/* Helper functions for HOWTO.  */
57
static 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);
60
61
/* Nds32 helper functions.  */
62
static bfd_vma calculate_memory_address
63
  (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
64
static int nds32_get_section_contents (bfd *, asection *,
65
               bfd_byte **, bool);
66
static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67
         Elf_Internal_Sym **);
68
static bool  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);
72
static bool nds32_fag_remove_unused_fpbase
73
  (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74
   Elf_Internal_Rela *irelend);
75
76
enum
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
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
90
91
0
#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92
0
          && (flags) & SEC_LOAD \
93
0
          && (flags) & SEC_READONLY)
94
95
/* The nop opcode we use.  */
96
0
#define NDS32_NOP32 0x40000009
97
0
#define NDS32_NOP16 0x9200
98
99
/* The size in bytes of an entry in the procedure linkage table.  */
100
0
#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.  */
106
0
#define PLT0_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(.got+4)    */
107
0
#define PLT0_ENTRY_WORD1  0x58f78000    /* ori     r15, r25, LO12(.got+4) */
108
0
#define PLT0_ENTRY_WORD2  0x05178000    /* lwi     r17, [r15+0]     */
109
0
#define PLT0_ENTRY_WORD3  0x04f78001    /* lwi     r15, [r15+4]     */
110
0
#define PLT0_ENTRY_WORD4  0x4a003c00    /* jr    r15        */
111
112
/* $ta is change to $r15 (from $r25).  */
113
0
#define PLT0_PIC_ENTRY_WORD0  0x46f00000  /* sethi   r15, HI20(got[1]@GOT)  */
114
0
#define PLT0_PIC_ENTRY_WORD1  0x58f78000  /* ori     r15, r15, LO12(got[1]@GOT) */
115
0
#define PLT0_PIC_ENTRY_WORD2  0x40f7f400  /* add     r15, gp, r15     */
116
0
#define PLT0_PIC_ENTRY_WORD3  0x05178000  /* lwi     r17, [r15+0]     */
117
0
#define PLT0_PIC_ENTRY_WORD4  0x04f78001  /* lwi     r15, [r15+4]     */
118
0
#define PLT0_PIC_ENTRY_WORD5  0x4a003c00  /* jr    r15        */
119
120
0
#define PLT_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(&got[n+3])      */
121
0
#define PLT_ENTRY_WORD1  0x04f78000    /* lwi     r15, r15, LO12(&got[n+3]) */
122
0
#define PLT_ENTRY_WORD2  0x4a003c00    /* jr    r15           */
123
0
#define PLT_ENTRY_WORD3  0x45000000    /* movi    r16, sizeof(RELA) * n     */
124
0
#define PLT_ENTRY_WORD4  0x48000000    /* j    .plt0.         */
125
126
0
#define PLT_PIC_ENTRY_WORD0  0x46f00000    /* sethi  r15, HI20(got[n+3]@GOT)    */
127
0
#define PLT_PIC_ENTRY_WORD1  0x58f78000    /* ori    r15, r15,    LO12(got[n+3]@GOT) */
128
0
#define PLT_PIC_ENTRY_WORD2  0x38febc02    /* lw   r15, [gp+r15]        */
129
0
#define PLT_PIC_ENTRY_WORD3  0x4a003c00    /* jr   r15          */
130
0
#define PLT_PIC_ENTRY_WORD4  0x45000000    /* movi   r16, sizeof(RELA) * n      */
131
0
#define PLT_PIC_ENTRY_WORD5  0x48000000    /* j    .plt0          */
132
133
/* These are macros used to get the relocation accurate value.  */
134
4
#define ACCURATE_8BIT_S1  (0x100)
135
0
#define ACCURATE_U9BIT_S1 (0x400)
136
#define ACCURATE_12BIT_S1 (0x2000)
137
0
#define ACCURATE_14BIT_S1 (0x4000)
138
#define ACCURATE_19BIT    (0x40000)
139
140
/* These are macros used to get the relocation conservative value.  */
141
0
#define CONSERVATIVE_8BIT_S1  (0x100 - 4)
142
0
#define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143
0
#define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144
0
#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
0
#define CONSERVATIVE_20BIT  (0x80000 - 0x1000)
152
153
/* Size of small data/bss sections, used to calculate SDA_BASE.  */
154
static long got_size = 0;
155
static int is_SDA_BASE_set = 0;
156
157
/* Convert ELF-VER in eflags to string for debugging purpose.  */
158
static 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
174
struct 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
184
enum 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
196
/* Nds32 ELF linker hash entry.  */
197
198
struct elf_nds32_link_hash_entry
199
{
200
  struct elf_link_hash_entry root;
201
202
  /* For checking relocation type.  */
203
  enum elf_nds32_tls_type tls_type;
204
205
  int offset_to_gp;
206
};
207
208
/* Get the nds32 ELF linker hash table from a link_info structure.  */
209
210
0
#define FP_BASE_NAME "_FP_BASE_"
211
static int check_start_export_sym = 0;
212
213
/* The offset for executable tls relaxation.  */
214
0
#define TP_OFFSET 0x0
215
216
typedef 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
225
struct 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;
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;
240
};
241
242
#define elf_nds32_tdata(bfd) \
243
0
  ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
244
245
#define elf32_nds32_local_got_tls_type(bfd) \
246
0
  (elf_nds32_tdata (bfd)->local_got_tls_type)
247
248
#define elf32_nds32_local_gp_offset(bfd) \
249
0
  (elf_nds32_tdata (bfd)->offset_to_gp)
250
251
0
#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
252
253
#define elf32_nds32_relax_group_ptr(bfd) \
254
0
  &(elf_nds32_tdata (bfd)->relax_group)
255
256
static bool
257
nds32_elf_mkobject (bfd *abfd)
258
95.7k
{
259
95.7k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata));
260
95.7k
}
261
262
/* Relocations used for relocation.  */
263
/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
264
   initialize array nds32_elf_howto_table in any order. The benefit
265
   is that we can add any new relocations with any numbers and don't
266
   need to fill the gap by lots of EMPTY_HOWTO. */
267
#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
268
  [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
269
270
static reloc_howto_type nds32_elf_howto_table[] =
271
{
272
  /* This reloc does nothing.  */
273
  HOWTO2 (R_NDS32_NONE,   /* type  */
274
   0,     /* rightshift  */
275
   4,     /* size  */
276
   32,      /* bitsize  */
277
   false,     /* pc_relative  */
278
   0,     /* bitpos  */
279
   complain_overflow_bitfield,/* complain_on_overflow  */
280
   bfd_elf_generic_reloc, /* special_function  */
281
   "R_NDS32_NONE",  /* name  */
282
   false,     /* partial_inplace  */
283
   0,     /* src_mask  */
284
   0,     /* dst_mask  */
285
   false),    /* pcrel_offset  */
286
287
  /* A 16 bit absolute relocation.  */
288
  HOWTO2 (R_NDS32_16,   /* type  */
289
   0,     /* rightshift  */
290
   2,     /* size  */
291
   16,      /* bitsize  */
292
   false,     /* pc_relative  */
293
   0,     /* bitpos  */
294
   complain_overflow_bitfield,/* complain_on_overflow  */
295
   nds32_elf_generic_reloc,/* special_function  */
296
   "R_NDS32_16",    /* name  */
297
   false,     /* partial_inplace  */
298
   0xffff,    /* src_mask  */
299
   0xffff,    /* dst_mask  */
300
   false),    /* pcrel_offset  */
301
302
  /* A 32 bit absolute relocation.  */
303
  HOWTO2 (R_NDS32_32,   /* type  */
304
   0,     /* rightshift  */
305
   4,     /* size  */
306
   32,      /* bitsize  */
307
   false,     /* pc_relative  */
308
   0,     /* bitpos  */
309
   complain_overflow_bitfield,/* complain_on_overflow  */
310
   nds32_elf_generic_reloc,/* special_function  */
311
   "R_NDS32_32",    /* name  */
312
   false,     /* partial_inplace  */
313
   0xffffffff,    /* src_mask  */
314
   0xffffffff,    /* dst_mask  */
315
   false),    /* pcrel_offset  */
316
317
  /* A 20 bit address.  */
318
  HOWTO2 (R_NDS32_20,   /* type  */
319
   0,     /* rightshift  */
320
   4,     /* size  */
321
   20,      /* bitsize  */
322
   false,     /* pc_relative  */
323
   0,     /* bitpos  */
324
   complain_overflow_unsigned,/* complain_on_overflow  */
325
   nds32_elf_generic_reloc,/* special_function  */
326
   "R_NDS32_20",    /* name  */
327
   false,     /* partial_inplace  */
328
   0xfffff,   /* src_mask  */
329
   0xfffff,   /* dst_mask  */
330
   false),    /* pcrel_offset  */
331
332
  /* An PC Relative 9-bit relocation, shifted by 2.
333
     This reloc is complicated because relocations are relative to pc & -4.
334
     i.e. branches in the right insn slot use the address of the left insn
335
     slot for pc.  */
336
  /* It's not clear whether this should have partial_inplace set or not.
337
     Branch relaxing in the assembler can store the addend in the insn,
338
     and if bfd_install_relocation gets called the addend may get added
339
     again.  */
340
  HOWTO2 (R_NDS32_9_PCREL,  /* type  */
341
   1,     /* rightshift  */
342
   2,     /* size  */
343
   8,     /* bitsize  */
344
   true,      /* pc_relative  */
345
   0,     /* bitpos  */
346
   complain_overflow_signed,/* complain_on_overflow  */
347
   nds32_elf_9_pcrel_reloc,/* special_function  */
348
   "R_NDS32_9_PCREL", /* name  */
349
   false,     /* partial_inplace  */
350
   0xff,      /* src_mask  */
351
   0xff,      /* dst_mask  */
352
   true),     /* pcrel_offset  */
353
354
  /* A relative 15 bit relocation, right shifted by 1.  */
355
  HOWTO2 (R_NDS32_15_PCREL, /* type  */
356
   1,     /* rightshift  */
357
   4,     /* size  */
358
   14,      /* bitsize  */
359
   true,      /* pc_relative  */
360
   0,     /* bitpos  */
361
   complain_overflow_signed,/* complain_on_overflow  */
362
   bfd_elf_generic_reloc, /* special_function  */
363
   "R_NDS32_15_PCREL",  /* name  */
364
   false,     /* partial_inplace  */
365
   0x3fff,    /* src_mask  */
366
   0x3fff,    /* dst_mask  */
367
   true),     /* pcrel_offset  */
368
369
  /* A relative 17 bit relocation, right shifted by 1.  */
370
  HOWTO2 (R_NDS32_17_PCREL, /* type  */
371
   1,     /* rightshift  */
372
   4,     /* size  */
373
   16,      /* bitsize  */
374
   true,      /* pc_relative  */
375
   0,     /* bitpos  */
376
   complain_overflow_signed,/* complain_on_overflow  */
377
   bfd_elf_generic_reloc, /* special_function  */
378
   "R_NDS32_17_PCREL",  /* name  */
379
   false,     /* partial_inplace  */
380
   0xffff,    /* src_mask  */
381
   0xffff,    /* dst_mask  */
382
   true),     /* pcrel_offset  */
383
384
  /* A relative 25 bit relocation, right shifted by 1.  */
385
  /* It's not clear whether this should have partial_inplace set or not.
386
     Branch relaxing in the assembler can store the addend in the insn,
387
     and if bfd_install_relocation gets called the addend may get added
388
     again.  */
389
  HOWTO2 (R_NDS32_25_PCREL, /* type  */
390
   1,     /* rightshift  */
391
   4,     /* size  */
392
   24,      /* bitsize  */
393
   true,      /* pc_relative  */
394
   0,     /* bitpos  */
395
   complain_overflow_signed,/* complain_on_overflow  */
396
   bfd_elf_generic_reloc, /* special_function  */
397
   "R_NDS32_25_PCREL",  /* name  */
398
   false,     /* partial_inplace  */
399
   0xffffff,    /* src_mask  */
400
   0xffffff,    /* dst_mask  */
401
   true),     /* pcrel_offset  */
402
403
  /* High 20 bits of address when lower 12 is or'd in.  */
404
  HOWTO2 (R_NDS32_HI20,   /* type  */
405
   12,      /* rightshift  */
406
   4,     /* size  */
407
   20,      /* bitsize  */
408
   false,     /* pc_relative  */
409
   0,     /* bitpos  */
410
   complain_overflow_dont,/* complain_on_overflow  */
411
   nds32_elf_hi20_reloc,  /* special_function  */
412
   "R_NDS32_HI20",  /* name  */
413
   false,     /* partial_inplace  */
414
   0x000fffff,    /* src_mask  */
415
   0x000fffff,    /* dst_mask  */
416
   false),    /* pcrel_offset  */
417
418
  /* Lower 12 bits of address.  */
419
  HOWTO2 (R_NDS32_LO12S3, /* type  */
420
   3,     /* rightshift  */
421
   4,     /* size  */
422
   9,     /* bitsize  */
423
   false,     /* pc_relative  */
424
   0,     /* bitpos  */
425
   complain_overflow_dont,/* complain_on_overflow  */
426
   nds32_elf_lo12_reloc,  /* special_function  */
427
   "R_NDS32_LO12S3",  /* name  */
428
   false,     /* partial_inplace  */
429
   0x000001ff,    /* src_mask  */
430
   0x000001ff,    /* dst_mask  */
431
   false),    /* pcrel_offset  */
432
433
  /* Lower 12 bits of address.  */
434
  HOWTO2 (R_NDS32_LO12S2, /* type  */
435
   2,     /* rightshift  */
436
   4,     /* size  */
437
   10,      /* bitsize  */
438
   false,     /* pc_relative  */
439
   0,     /* bitpos  */
440
   complain_overflow_dont,/* complain_on_overflow  */
441
   nds32_elf_lo12_reloc,  /* special_function  */
442
   "R_NDS32_LO12S2",  /* name  */
443
   false,     /* partial_inplace  */
444
   0x000003ff,    /* src_mask  */
445
   0x000003ff,    /* dst_mask  */
446
   false),    /* pcrel_offset  */
447
448
  /* Lower 12 bits of address.  */
449
  HOWTO2 (R_NDS32_LO12S1, /* type  */
450
   1,     /* rightshift  */
451
   4,     /* size  */
452
   11,      /* bitsize  */
453
   false,     /* pc_relative  */
454
   0,     /* bitpos  */
455
   complain_overflow_dont,/* complain_on_overflow  */
456
   nds32_elf_lo12_reloc,  /* special_function  */
457
   "R_NDS32_LO12S1",  /* name  */
458
   false,     /* partial_inplace  */
459
   0x000007ff,    /* src_mask  */
460
   0x000007ff,    /* dst_mask  */
461
   false),    /* pcrel_offset  */
462
463
  /* Lower 12 bits of address.  */
464
  HOWTO2 (R_NDS32_LO12S0, /* type  */
465
   0,     /* rightshift  */
466
   4,     /* size  */
467
   12,      /* bitsize  */
468
   false,     /* pc_relative  */
469
   0,     /* bitpos  */
470
   complain_overflow_dont,/* complain_on_overflow  */
471
   nds32_elf_lo12_reloc,  /* special_function  */
472
   "R_NDS32_LO12S0",  /* name  */
473
   false,     /* partial_inplace  */
474
   0x00000fff,    /* src_mask  */
475
   0x00000fff,    /* dst_mask  */
476
   false),    /* pcrel_offset  */
477
478
  /* Small data area 15 bits offset.  */
479
  HOWTO2 (R_NDS32_SDA15S3,  /* type  */
480
   3,     /* rightshift  */
481
   4,     /* size  */
482
   15,      /* bitsize  */
483
   false,     /* pc_relative  */
484
   0,     /* bitpos  */
485
   complain_overflow_signed,/* complain_on_overflow  */
486
   nds32_elf_sda15_reloc, /* special_function  */
487
   "R_NDS32_SDA15S3", /* name  */
488
   false,     /* partial_inplace  */
489
   0x00007fff,    /* src_mask  */
490
   0x00007fff,    /* dst_mask  */
491
   false),    /* pcrel_offset  */
492
493
  /* Small data area 15 bits offset.  */
494
  HOWTO2 (R_NDS32_SDA15S2,  /* type  */
495
   2,     /* rightshift  */
496
   4,     /* size  */
497
   15,      /* bitsize  */
498
   false,     /* pc_relative  */
499
   0,     /* bitpos  */
500
   complain_overflow_signed,/* complain_on_overflow  */
501
   nds32_elf_sda15_reloc, /* special_function  */
502
   "R_NDS32_SDA15S2", /* name  */
503
   false,     /* partial_inplace  */
504
   0x00007fff,    /* src_mask  */
505
   0x00007fff,    /* dst_mask  */
506
   false),    /* pcrel_offset  */
507
508
  /* Small data area 15 bits offset.  */
509
  HOWTO2 (R_NDS32_SDA15S1,  /* type  */
510
   1,     /* rightshift  */
511
   4,     /* size  */
512
   15,      /* bitsize  */
513
   false,     /* pc_relative  */
514
   0,     /* bitpos  */
515
   complain_overflow_signed,/* complain_on_overflow  */
516
   nds32_elf_sda15_reloc, /* special_function  */
517
   "R_NDS32_SDA15S1", /* name  */
518
   false,     /* partial_inplace  */
519
   0x00007fff,    /* src_mask  */
520
   0x00007fff,    /* dst_mask  */
521
   false),    /* pcrel_offset  */
522
523
  /* Small data area 15 bits offset.  */
524
  HOWTO2 (R_NDS32_SDA15S0,  /* type  */
525
   0,     /* rightshift  */
526
   4,     /* size  */
527
   15,      /* bitsize  */
528
   false,     /* pc_relative  */
529
   0,     /* bitpos  */
530
   complain_overflow_signed,/* complain_on_overflow  */
531
   nds32_elf_sda15_reloc, /* special_function  */
532
   "R_NDS32_SDA15S0", /* name  */
533
   false,     /* partial_inplace  */
534
   0x00007fff,    /* src_mask  */
535
   0x00007fff,    /* dst_mask  */
536
   false),    /* pcrel_offset  */
537
538
  /* GNU extension to record C++ vtable hierarchy  */
539
  HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type  */
540
   0,     /* rightshift  */
541
   4,     /* size  */
542
   0,     /* bitsize  */
543
   false,     /* pc_relative  */
544
   0,     /* bitpos  */
545
   complain_overflow_dont,/* complain_on_overflow  */
546
   NULL,      /* special_function  */
547
   "R_NDS32_GNU_VTINHERIT",/* name  */
548
   false,     /* partial_inplace  */
549
   0,     /* src_mask  */
550
   0,     /* dst_mask  */
551
   false),    /* pcrel_offset  */
552
553
  /* GNU extension to record C++ vtable member usage  */
554
  HOWTO2 (R_NDS32_GNU_VTENTRY,  /* type  */
555
   0,     /* rightshift  */
556
   4,     /* size  */
557
   0,     /* bitsize  */
558
   false,     /* pc_relative  */
559
   0,     /* bitpos  */
560
   complain_overflow_dont,/* complain_on_overflow  */
561
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
562
   "R_NDS32_GNU_VTENTRY", /* name  */
563
   false,     /* partial_inplace  */
564
   0,     /* src_mask  */
565
   0,     /* dst_mask  */
566
   false),    /* pcrel_offset  */
567
568
  /* A 16 bit absolute relocation.  */
569
  HOWTO2 (R_NDS32_16_RELA,  /* type  */
570
   0,     /* rightshift  */
571
   2,     /* size  */
572
   16,      /* bitsize  */
573
   false,     /* pc_relative  */
574
   0,     /* bitpos  */
575
   complain_overflow_bitfield,/* complain_on_overflow  */
576
   bfd_elf_generic_reloc, /* special_function  */
577
   "R_NDS32_16_RELA", /* name  */
578
   false,     /* partial_inplace  */
579
   0xffff,    /* src_mask  */
580
   0xffff,    /* dst_mask  */
581
   false),    /* pcrel_offset  */
582
583
  /* A 32 bit absolute relocation.  */
584
  HOWTO2 (R_NDS32_32_RELA,  /* type  */
585
   0,     /* rightshift  */
586
   4,     /* size  */
587
   32,      /* bitsize  */
588
   false,     /* pc_relative  */
589
   0,     /* bitpos  */
590
   complain_overflow_bitfield,/* complain_on_overflow  */
591
   bfd_elf_generic_reloc, /* special_function  */
592
   "R_NDS32_32_RELA", /* name  */
593
   false,     /* partial_inplace  */
594
   0xffffffff,    /* src_mask  */
595
   0xffffffff,    /* dst_mask  */
596
   false),    /* pcrel_offset  */
597
598
  /* A 20 bit address.  */
599
  HOWTO2 (R_NDS32_20_RELA,  /* type  */
600
   0,     /* rightshift  */
601
   4,     /* size  */
602
   20,      /* bitsize  */
603
   false,     /* pc_relative  */
604
   0,     /* bitpos  */
605
   complain_overflow_signed,/* complain_on_overflow  */
606
   bfd_elf_generic_reloc, /* special_function  */
607
   "R_NDS32_20_RELA", /* name  */
608
   false,     /* partial_inplace  */
609
   0xfffff,   /* src_mask  */
610
   0xfffff,   /* dst_mask  */
611
   false),    /* pcrel_offset  */
612
613
  HOWTO2 (R_NDS32_9_PCREL_RELA, /* type  */
614
   1,     /* rightshift  */
615
   2,     /* size  */
616
   8,     /* bitsize  */
617
   true,      /* pc_relative  */
618
   0,     /* bitpos  */
619
   complain_overflow_signed,/* complain_on_overflow  */
620
   bfd_elf_generic_reloc, /* special_function  */
621
   "R_NDS32_9_PCREL_RELA",/* name  */
622
   false,     /* partial_inplace  */
623
   0xff,      /* src_mask  */
624
   0xff,      /* dst_mask  */
625
   true),     /* pcrel_offset  */
626
627
  /* A relative 15 bit relocation, right shifted by 1.  */
628
  HOWTO2 (R_NDS32_15_PCREL_RELA,/* type  */
629
   1,     /* rightshift  */
630
   4,     /* size  */
631
   14,      /* bitsize  */
632
   true,      /* pc_relative  */
633
   0,     /* bitpos  */
634
   complain_overflow_signed,/* complain_on_overflow  */
635
   bfd_elf_generic_reloc, /* special_function  */
636
   "R_NDS32_15_PCREL_RELA",/* name  */
637
   false,     /* partial_inplace  */
638
   0x3fff,    /* src_mask  */
639
   0x3fff,    /* dst_mask  */
640
   true),     /* pcrel_offset  */
641
642
  /* A relative 17 bit relocation, right shifted by 1.  */
643
  HOWTO2 (R_NDS32_17_PCREL_RELA,/* type  */
644
   1,     /* rightshift  */
645
   4,     /* size  */
646
   16,      /* bitsize  */
647
   true,      /* pc_relative  */
648
   0,     /* bitpos  */
649
   complain_overflow_signed,/* complain_on_overflow  */
650
   bfd_elf_generic_reloc, /* special_function  */
651
   "R_NDS32_17_PCREL_RELA",/* name  */
652
   false,     /* partial_inplace  */
653
   0xffff,    /* src_mask  */
654
   0xffff,    /* dst_mask  */
655
   true),     /* pcrel_offset  */
656
657
  /* A relative 25 bit relocation, right shifted by 2.  */
658
  HOWTO2 (R_NDS32_25_PCREL_RELA,/* type  */
659
   1,     /* rightshift  */
660
   4,     /* size  */
661
   24,      /* bitsize  */
662
   true,      /* pc_relative  */
663
   0,     /* bitpos  */
664
   complain_overflow_signed,/* complain_on_overflow  */
665
   bfd_elf_generic_reloc, /* special_function  */
666
   "R_NDS32_25_PCREL_RELA",/* name  */
667
   false,     /* partial_inplace  */
668
   0xffffff,    /* src_mask  */
669
   0xffffff,    /* dst_mask  */
670
   true),     /* pcrel_offset  */
671
672
  /* High 20 bits of address when lower 16 is or'd in.  */
673
  HOWTO2 (R_NDS32_HI20_RELA,  /* type  */
674
   12,      /* rightshift  */
675
   4,     /* size  */
676
   20,      /* bitsize  */
677
   false,     /* pc_relative  */
678
   0,     /* bitpos  */
679
   complain_overflow_dont,/* complain_on_overflow  */
680
   bfd_elf_generic_reloc, /* special_function  */
681
   "R_NDS32_HI20_RELA", /* name  */
682
   false,     /* partial_inplace  */
683
   0x000fffff,    /* src_mask  */
684
   0x000fffff,    /* dst_mask  */
685
   false),    /* pcrel_offset  */
686
687
  /* Lower 12 bits of address.  */
688
  HOWTO2 (R_NDS32_LO12S3_RELA,  /* type  */
689
   3,     /* rightshift  */
690
   4,     /* size  */
691
   9,     /* bitsize  */
692
   false,     /* pc_relative  */
693
   0,     /* bitpos  */
694
   complain_overflow_dont,/* complain_on_overflow  */
695
   bfd_elf_generic_reloc, /* special_function  */
696
   "R_NDS32_LO12S3_RELA", /* name  */
697
   false,     /* partial_inplace  */
698
   0x000001ff,    /* src_mask  */
699
   0x000001ff,    /* dst_mask  */
700
   false),    /* pcrel_offset  */
701
702
  /* Lower 12 bits of address.  */
703
  HOWTO2 (R_NDS32_LO12S2_RELA,  /* type  */
704
   2,     /* rightshift  */
705
   4,     /* size  */
706
   10,      /* bitsize  */
707
   false,     /* pc_relative  */
708
   0,     /* bitpos  */
709
   complain_overflow_dont,/* complain_on_overflow  */
710
   bfd_elf_generic_reloc, /* special_function  */
711
   "R_NDS32_LO12S2_RELA", /* name  */
712
   false,     /* partial_inplace  */
713
   0x000003ff,    /* src_mask  */
714
   0x000003ff,    /* dst_mask  */
715
   false),    /* pcrel_offset  */
716
717
  /* Lower 12 bits of address.  */
718
  HOWTO2 (R_NDS32_LO12S1_RELA,  /* type  */
719
   1,     /* rightshift  */
720
   4,     /* size  */
721
   11,      /* bitsize  */
722
   false,     /* pc_relative  */
723
   0,     /* bitpos  */
724
   complain_overflow_dont,/* complain_on_overflow  */
725
   bfd_elf_generic_reloc, /* special_function  */
726
   "R_NDS32_LO12S1_RELA", /* name  */
727
   false,     /* partial_inplace  */
728
   0x000007ff,    /* src_mask  */
729
   0x000007ff,    /* dst_mask  */
730
   false),    /* pcrel_offset  */
731
732
  /* Lower 12 bits of address.  */
733
  HOWTO2 (R_NDS32_LO12S0_RELA,  /* type  */
734
   0,     /* rightshift  */
735
   4,     /* size  */
736
   12,      /* bitsize  */
737
   false,     /* pc_relative  */
738
   0,     /* bitpos  */
739
   complain_overflow_dont,/* complain_on_overflow  */
740
   bfd_elf_generic_reloc, /* special_function  */
741
   "R_NDS32_LO12S0_RELA", /* name  */
742
   false,     /* partial_inplace  */
743
   0x00000fff,    /* src_mask  */
744
   0x00000fff,    /* dst_mask  */
745
   false),    /* pcrel_offset  */
746
747
  /* Small data area 15 bits offset.  */
748
  HOWTO2 (R_NDS32_SDA15S3_RELA, /* type  */
749
   3,     /* rightshift  */
750
   4,     /* size  */
751
   15,      /* bitsize  */
752
   false,     /* pc_relative  */
753
   0,     /* bitpos  */
754
   complain_overflow_signed,/* complain_on_overflow  */
755
   bfd_elf_generic_reloc, /* special_function  */
756
   "R_NDS32_SDA15S3_RELA",/* name  */
757
   false,     /* partial_inplace  */
758
   0x00007fff,    /* src_mask  */
759
   0x00007fff,    /* dst_mask  */
760
   false),    /* pcrel_offset  */
761
762
  /* Small data area 15 bits offset.  */
763
  HOWTO2 (R_NDS32_SDA15S2_RELA, /* type  */
764
   2,     /* rightshift  */
765
   4,     /* size  */
766
   15,      /* bitsize  */
767
   false,     /* pc_relative  */
768
   0,     /* bitpos  */
769
   complain_overflow_signed,/* complain_on_overflow  */
770
   bfd_elf_generic_reloc, /* special_function  */
771
   "R_NDS32_SDA15S2_RELA",/* name  */
772
   false,     /* partial_inplace  */
773
   0x00007fff,    /* src_mask  */
774
   0x00007fff,    /* dst_mask  */
775
   false),    /* pcrel_offset  */
776
777
  HOWTO2 (R_NDS32_SDA15S1_RELA, /* type  */
778
   1,     /* rightshift  */
779
   4,     /* size  */
780
   15,      /* bitsize  */
781
   false,     /* pc_relative  */
782
   0,     /* bitpos  */
783
   complain_overflow_signed,/* complain_on_overflow  */
784
   bfd_elf_generic_reloc, /* special_function  */
785
   "R_NDS32_SDA15S1_RELA",/* name  */
786
   false,     /* partial_inplace  */
787
   0x00007fff,    /* src_mask  */
788
   0x00007fff,    /* dst_mask  */
789
   false),    /* pcrel_offset  */
790
791
  HOWTO2 (R_NDS32_SDA15S0_RELA, /* type  */
792
   0,     /* rightshift  */
793
   4,     /* size  */
794
   15,      /* bitsize  */
795
   false,     /* pc_relative  */
796
   0,     /* bitpos  */
797
   complain_overflow_signed,/* complain_on_overflow  */
798
   bfd_elf_generic_reloc, /* special_function  */
799
   "R_NDS32_SDA15S0_RELA",/* name  */
800
   false,     /* partial_inplace  */
801
   0x00007fff,    /* src_mask  */
802
   0x00007fff,    /* dst_mask  */
803
   false),    /* pcrel_offset  */
804
805
  /* GNU extension to record C++ vtable hierarchy  */
806
  HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type  */
807
   0,     /* rightshift  */
808
   4,     /* size  */
809
   0,     /* bitsize  */
810
   false,     /* pc_relative  */
811
   0,     /* bitpos  */
812
   complain_overflow_dont,/* complain_on_overflow  */
813
   NULL,      /* special_function  */
814
   "R_NDS32_RELA_GNU_VTINHERIT",/* name  */
815
   false,     /* partial_inplace  */
816
   0,     /* src_mask  */
817
   0,     /* dst_mask  */
818
   false),    /* pcrel_offset  */
819
820
  /* GNU extension to record C++ vtable member usage  */
821
  HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type  */
822
   0,     /* rightshift  */
823
   4,     /* size  */
824
   0,     /* bitsize  */
825
   false,     /* pc_relative  */
826
   0,     /* bitpos  */
827
   complain_overflow_dont,/* complain_on_overflow  */
828
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
829
   "R_NDS32_RELA_GNU_VTENTRY",/* name  */
830
   false,     /* partial_inplace  */
831
   0,     /* src_mask  */
832
   0,     /* dst_mask  */
833
   false),    /* pcrel_offset  */
834
835
  /* Like R_NDS32_20, but referring to the GOT table entry for
836
     the symbol.  */
837
  HOWTO2 (R_NDS32_GOT20,  /* type  */
838
   0,     /* rightshift  */
839
   4,     /* size  */
840
   20,      /* bitsize  */
841
   false,     /* pc_relative  */
842
   0,     /* bitpos  */
843
   complain_overflow_signed,/* complain_on_overflow  */
844
   bfd_elf_generic_reloc, /* special_function  */
845
   "R_NDS32_GOT20", /* name  */
846
   false,     /* partial_inplace  */
847
   0xfffff,   /* src_mask  */
848
   0xfffff,   /* dst_mask  */
849
   false),    /* pcrel_offset  */
850
851
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
852
     entry for the symbol.  */
853
  HOWTO2 (R_NDS32_25_PLTREL,  /* type  */
854
   1,     /* rightshift  */
855
   4,     /* size  */
856
   24,      /* bitsize  */
857
   true,      /* pc_relative  */
858
   0,     /* bitpos  */
859
   complain_overflow_signed,/* complain_on_overflow  */
860
   bfd_elf_generic_reloc, /* special_function  */
861
   "R_NDS32_25_PLTREL", /* name  */
862
   false,     /* partial_inplace  */
863
   0xffffff,    /* src_mask  */
864
   0xffffff,    /* dst_mask  */
865
   true),     /* pcrel_offset  */
866
867
  /* This is used only by the dynamic linker.  The symbol should exist
868
     both in the object being run and in some shared library.  The
869
     dynamic linker copies the data addressed by the symbol from the
870
     shared library into the object, because the object being
871
     run has to have the data at some particular address.  */
872
  HOWTO2 (R_NDS32_COPY,   /* type  */
873
   0,     /* rightshift  */
874
   4,     /* size  */
875
   32,      /* bitsize  */
876
   false,     /* pc_relative  */
877
   0,     /* bitpos  */
878
   complain_overflow_bitfield,/* complain_on_overflow  */
879
   bfd_elf_generic_reloc, /* special_function  */
880
   "R_NDS32_COPY",  /* name  */
881
   false,     /* partial_inplace  */
882
   0xffffffff,    /* src_mask  */
883
   0xffffffff,    /* dst_mask  */
884
   false),    /* pcrel_offset  */
885
886
  /* Like R_NDS32_20, but used when setting global offset table
887
     entries.  */
888
  HOWTO2 (R_NDS32_GLOB_DAT, /* type  */
889
   0,     /* rightshift  */
890
   4,     /* size  */
891
   32,      /* bitsize  */
892
   false,     /* pc_relative  */
893
   0,     /* bitpos  */
894
   complain_overflow_bitfield,/* complain_on_overflow  */
895
   bfd_elf_generic_reloc, /* special_function  */
896
   "R_NDS32_GLOB_DAT",  /* name  */
897
   false,     /* partial_inplace  */
898
   0xffffffff,    /* src_mask  */
899
   0xffffffff,    /* dst_mask  */
900
   false),    /* pcrel_offset  */
901
902
  /* Marks a procedure linkage table entry for a symbol.  */
903
  HOWTO2 (R_NDS32_JMP_SLOT, /* type  */
904
   0,     /* rightshift  */
905
   4,     /* size  */
906
   32,      /* bitsize  */
907
   false,     /* pc_relative  */
908
   0,     /* bitpos  */
909
   complain_overflow_bitfield,/* complain_on_overflow  */
910
   bfd_elf_generic_reloc, /* special_function  */
911
   "R_NDS32_JMP_SLOT",  /* name  */
912
   false,     /* partial_inplace  */
913
   0xffffffff,    /* src_mask  */
914
   0xffffffff,    /* dst_mask  */
915
   false),    /* pcrel_offset  */
916
917
  /* Used only by the dynamic linker.  When the object is run, this
918
     longword is set to the load address of the object, plus the
919
     addend.  */
920
  HOWTO2 (R_NDS32_RELATIVE, /* type  */
921
   0,     /* rightshift  */
922
   4,     /* size  */
923
   32,      /* bitsize  */
924
   false,     /* pc_relative  */
925
   0,     /* bitpos  */
926
   complain_overflow_bitfield,/* complain_on_overflow  */
927
   bfd_elf_generic_reloc, /* special_function  */
928
   "R_NDS32_RELATIVE",  /* name  */
929
   false,     /* partial_inplace  */
930
   0xffffffff,    /* src_mask  */
931
   0xffffffff,    /* dst_mask  */
932
   false),    /* pcrel_offset  */
933
934
  HOWTO2 (R_NDS32_GOTOFF, /* type  */
935
   0,     /* rightshift  */
936
   4,     /* size  */
937
   20,      /* bitsize  */
938
   false,     /* pc_relative  */
939
   0,     /* bitpos  */
940
   complain_overflow_signed,/* complain_on_overflow  */
941
   bfd_elf_generic_reloc, /* special_function  */
942
   "R_NDS32_GOTOFF",  /* name  */
943
   false,     /* partial_inplace  */
944
   0xfffff,   /* src_mask  */
945
   0xfffff,   /* dst_mask  */
946
   false),    /* pcrel_offset  */
947
948
  /* An PC Relative 20-bit relocation used when setting PIC offset
949
     table register.  */
950
  HOWTO2 (R_NDS32_GOTPC20,  /* type  */
951
   0,     /* rightshift  */
952
   4,     /* size  */
953
   20,      /* bitsize  */
954
   true,      /* pc_relative  */
955
   0,     /* bitpos  */
956
   complain_overflow_signed,/* complain_on_overflow  */
957
   bfd_elf_generic_reloc, /* special_function  */
958
   "R_NDS32_GOTPC20", /* name  */
959
   false,     /* partial_inplace  */
960
   0xfffff,   /* src_mask  */
961
   0xfffff,   /* dst_mask  */
962
   true),     /* pcrel_offset  */
963
964
  /* Like R_NDS32_HI20, but referring to the GOT table entry for
965
     the symbol.  */
966
  HOWTO2 (R_NDS32_GOT_HI20, /* type  */
967
   12,      /* rightshift  */
968
   4,     /* size  */
969
   20,      /* bitsize  */
970
   false,     /* pc_relative  */
971
   0,     /* bitpos  */
972
   complain_overflow_dont,/* complain_on_overflow  */
973
   bfd_elf_generic_reloc, /* special_function  */
974
   "R_NDS32_GOT_HI20",  /* name  */
975
   false,     /* partial_inplace  */
976
   0x000fffff,    /* src_mask  */
977
   0x000fffff,    /* dst_mask  */
978
   false),    /* pcrel_offset  */
979
980
  HOWTO2 (R_NDS32_GOT_LO12, /* type  */
981
   0,     /* rightshift  */
982
   4,     /* size  */
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  */
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.  */
997
  HOWTO2 (R_NDS32_GOTPC_HI20, /* type  */
998
   12,      /* rightshift  */
999
   4,     /* size  */
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
1011
  HOWTO2 (R_NDS32_GOTPC_LO12, /* type  */
1012
   0,     /* rightshift  */
1013
   4,     /* size  */
1014
   12,      /* bitsize  */
1015
   false,     /* pc_relative  */
1016
   0,     /* bitpos  */
1017
   complain_overflow_dont,/* complain_on_overflow  */
1018
   bfd_elf_generic_reloc, /* special_function  */
1019
   "R_NDS32_GOTPC_LO12",  /* name  */
1020
   false,     /* partial_inplace  */
1021
   0x00000fff,    /* src_mask  */
1022
   0x00000fff,    /* dst_mask  */
1023
   true),     /* pcrel_offset  */
1024
1025
  HOWTO2 (R_NDS32_GOTOFF_HI20,  /* type  */
1026
   12,      /* rightshift  */
1027
   4,     /* size  */
1028
   20,      /* bitsize  */
1029
   false,     /* pc_relative  */
1030
   0,     /* bitpos  */
1031
   complain_overflow_dont,/* complain_on_overflow  */
1032
   bfd_elf_generic_reloc, /* special_function  */
1033
   "R_NDS32_GOTOFF_HI20", /* name  */
1034
   false,     /* partial_inplace  */
1035
   0x000fffff,    /* src_mask  */
1036
   0x000fffff,    /* dst_mask  */
1037
   false),    /* pcrel_offset  */
1038
1039
  HOWTO2 (R_NDS32_GOTOFF_LO12,  /* type  */
1040
   0,     /* rightshift  */
1041
   4,     /* size  */
1042
   12,      /* bitsize  */
1043
   false,     /* pc_relative  */
1044
   0,     /* bitpos  */
1045
   complain_overflow_dont,/* complain_on_overflow  */
1046
   bfd_elf_generic_reloc, /* special_function  */
1047
   "R_NDS32_GOTOFF_LO12", /* name  */
1048
   false,     /* partial_inplace  */
1049
   0x00000fff,    /* src_mask  */
1050
   0x00000fff,    /* dst_mask  */
1051
   false),    /* pcrel_offset  */
1052
1053
  /* Alignment hint for relaxable instruction.  This is used with
1054
     R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
1055
     in order to make next label aligned on word boundary.  */
1056
  HOWTO2 (R_NDS32_INSN16, /* type  */
1057
   0,     /* rightshift  */
1058
   4,     /* size  */
1059
   32,      /* bitsize  */
1060
   false,     /* pc_relative  */
1061
   0,     /* bitpos  */
1062
   complain_overflow_dont,/* complain_on_overflow  */
1063
   nds32_elf_ignore_reloc,/* special_function  */
1064
   "R_NDS32_INSN16",  /* name  */
1065
   false,     /* partial_inplace  */
1066
   0x00000fff,    /* src_mask  */
1067
   0x00000fff,    /* dst_mask  */
1068
   false),    /* pcrel_offset  */
1069
1070
  /* Alignment hint for label.  */
1071
  HOWTO2 (R_NDS32_LABEL,  /* type  */
1072
   0,     /* rightshift  */
1073
   4,     /* size  */
1074
   32,      /* bitsize  */
1075
   false,     /* pc_relative  */
1076
   0,     /* bitpos  */
1077
   complain_overflow_dont,/* complain_on_overflow  */
1078
   nds32_elf_ignore_reloc,/* special_function  */
1079
   "R_NDS32_LABEL", /* name  */
1080
   false,     /* partial_inplace  */
1081
   0xffffffff,    /* src_mask  */
1082
   0xffffffff,    /* dst_mask  */
1083
   false),    /* pcrel_offset  */
1084
1085
  /* Relax hint for unconditional call sequence  */
1086
  HOWTO2 (R_NDS32_LONGCALL1,  /* type  */
1087
   0,     /* rightshift  */
1088
   4,     /* size  */
1089
   32,      /* bitsize  */
1090
   false,     /* pc_relative  */
1091
   0,     /* bitpos  */
1092
   complain_overflow_dont,/* complain_on_overflow  */
1093
   nds32_elf_ignore_reloc,/* special_function  */
1094
   "R_NDS32_LONGCALL1", /* name  */
1095
   false,     /* partial_inplace  */
1096
   0xffffffff,    /* src_mask  */
1097
   0xffffffff,    /* dst_mask  */
1098
   false),    /* pcrel_offset  */
1099
1100
  /* Relax hint for conditional call sequence.  */
1101
  HOWTO2 (R_NDS32_LONGCALL2,  /* type  */
1102
   0,     /* rightshift  */
1103
   4,     /* size  */
1104
   32,      /* bitsize  */
1105
   false,     /* pc_relative  */
1106
   0,     /* bitpos  */
1107
   complain_overflow_dont,/* complain_on_overflow  */
1108
   nds32_elf_ignore_reloc,/* special_function  */
1109
   "R_NDS32_LONGCALL2", /* name  */
1110
   false,     /* partial_inplace  */
1111
   0xffffffff,    /* src_mask  */
1112
   0xffffffff,    /* dst_mask  */
1113
   false),    /* pcrel_offset  */
1114
1115
  /* Relax hint for conditional call sequence.  */
1116
  HOWTO2 (R_NDS32_LONGCALL3,  /* type  */
1117
   0,     /* rightshift  */
1118
   4,     /* size  */
1119
   32,      /* bitsize  */
1120
   false,     /* pc_relative  */
1121
   0,     /* bitpos  */
1122
   complain_overflow_dont,/* complain_on_overflow  */
1123
   nds32_elf_ignore_reloc,/* special_function  */
1124
   "R_NDS32_LONGCALL3", /* name  */
1125
   false,     /* partial_inplace  */
1126
   0xffffffff,    /* src_mask  */
1127
   0xffffffff,    /* dst_mask  */
1128
   false),    /* pcrel_offset  */
1129
1130
  /* Relax hint for unconditional branch sequence.  */
1131
  HOWTO2 (R_NDS32_LONGJUMP1,  /* type  */
1132
   0,     /* rightshift  */
1133
   4,     /* size  */
1134
   32,      /* bitsize  */
1135
   false,     /* pc_relative  */
1136
   0,     /* bitpos  */
1137
   complain_overflow_dont,/* complain_on_overflow  */
1138
   nds32_elf_ignore_reloc,/* special_function  */
1139
   "R_NDS32_LONGJUMP1", /* name  */
1140
   false,     /* partial_inplace  */
1141
   0xffffffff,    /* src_mask  */
1142
   0xffffffff,    /* dst_mask  */
1143
   false),    /* pcrel_offset  */
1144
1145
  /* Relax hint for conditional branch sequence.  */
1146
  HOWTO2 (R_NDS32_LONGJUMP2,  /* type  */
1147
   0,     /* rightshift  */
1148
   4,     /* size  */
1149
   32,      /* bitsize  */
1150
   false,     /* pc_relative  */
1151
   0,     /* bitpos  */
1152
   complain_overflow_dont,/* complain_on_overflow  */
1153
   nds32_elf_ignore_reloc,/* special_function  */
1154
   "R_NDS32_LONGJUMP2", /* name  */
1155
   false,     /* partial_inplace  */
1156
   0xffffffff,    /* src_mask  */
1157
   0xffffffff,    /* dst_mask  */
1158
   false),    /* pcrel_offset  */
1159
1160
  /* Relax hint for conditional branch sequence.  */
1161
  HOWTO2 (R_NDS32_LONGJUMP3,  /* type  */
1162
   0,     /* rightshift  */
1163
   4,     /* size  */
1164
   32,      /* bitsize  */
1165
   false,     /* pc_relative  */
1166
   0,     /* bitpos  */
1167
   complain_overflow_dont,/* complain_on_overflow  */
1168
   nds32_elf_ignore_reloc,/* special_function  */
1169
   "R_NDS32_LONGJUMP3", /* name  */
1170
   false,     /* partial_inplace  */
1171
   0xffffffff,    /* src_mask  */
1172
   0xffffffff,    /* dst_mask  */
1173
   false),    /* pcrel_offset  */
1174
1175
  /* Relax hint for load/store sequence.   */
1176
  HOWTO2 (R_NDS32_LOADSTORE,  /* type  */
1177
   0,     /* rightshift  */
1178
   4,     /* size  */
1179
   32,      /* bitsize  */
1180
   false,     /* pc_relative  */
1181
   0,     /* bitpos  */
1182
   complain_overflow_dont,/* complain_on_overflow  */
1183
   nds32_elf_ignore_reloc,/* special_function  */
1184
   "R_NDS32_LOADSTORE", /* name  */
1185
   false,     /* partial_inplace  */
1186
   0xffffffff,    /* src_mask  */
1187
   0xffffffff,    /* dst_mask  */
1188
   false),    /* pcrel_offset  */
1189
1190
  /* Relax hint for load/store sequence.  */
1191
  HOWTO2 (R_NDS32_9_FIXED_RELA, /* type  */
1192
   0,     /* rightshift  */
1193
   2,     /* size  */
1194
   16,      /* bitsize  */
1195
   false,     /* pc_relative  */
1196
   0,     /* bitpos  */
1197
   complain_overflow_dont,/* complain_on_overflow  */
1198
   nds32_elf_ignore_reloc,/* special_function  */
1199
   "R_NDS32_9_FIXED_RELA",/* name  */
1200
   false,     /* partial_inplace  */
1201
   0x000000ff,    /* src_mask  */
1202
   0x000000ff,    /* dst_mask  */
1203
   false),    /* pcrel_offset  */
1204
1205
  /* Relax hint for load/store sequence.  */
1206
  HOWTO2 (R_NDS32_15_FIXED_RELA,/* type  */
1207
   0,     /* rightshift  */
1208
   4,     /* size  */
1209
   32,      /* bitsize  */
1210
   false,     /* pc_relative  */
1211
   0,     /* bitpos  */
1212
   complain_overflow_dont,/* complain_on_overflow  */
1213
   nds32_elf_ignore_reloc,/* special_function  */
1214
   "R_NDS32_15_FIXED_RELA",/* name  */
1215
   false,     /* partial_inplace  */
1216
   0x00003fff,    /* src_mask  */
1217
   0x00003fff,    /* dst_mask  */
1218
   false),    /* pcrel_offset  */
1219
1220
  /* Relax hint for load/store sequence.  */
1221
  HOWTO2 (R_NDS32_17_FIXED_RELA,/* type  */
1222
   0,     /* rightshift  */
1223
   4,     /* size  */
1224
   32,      /* bitsize  */
1225
   false,     /* pc_relative  */
1226
   0,     /* bitpos  */
1227
   complain_overflow_dont,/* complain_on_overflow  */
1228
   nds32_elf_ignore_reloc,/* special_function  */
1229
   "R_NDS32_17_FIXED_RELA",/* name  */
1230
   false,     /* partial_inplace  */
1231
   0x0000ffff,    /* src_mask  */
1232
   0x0000ffff,    /* dst_mask  */
1233
   false),    /* pcrel_offset  */
1234
1235
  /* Relax hint for load/store sequence.  */
1236
  HOWTO2 (R_NDS32_25_FIXED_RELA,/* type  */
1237
   0,     /* rightshift  */
1238
   4,     /* size  */
1239
   32,      /* bitsize  */
1240
   false,     /* pc_relative  */
1241
   0,     /* bitpos  */
1242
   complain_overflow_dont,/* complain_on_overflow  */
1243
   nds32_elf_ignore_reloc,/* special_function  */
1244
   "R_NDS32_25_FIXED_RELA",/* name  */
1245
   false,     /* partial_inplace  */
1246
   0x00ffffff,    /* src_mask  */
1247
   0x00ffffff,    /* dst_mask  */
1248
   false),    /* pcrel_offset  */
1249
1250
  /* High 20 bits of PLT symbol offset relative to PC.  */
1251
  HOWTO2 (R_NDS32_PLTREL_HI20,  /* type  */
1252
   12,      /* rightshift  */
1253
   4,     /* size  */
1254
   20,      /* bitsize  */
1255
   false,     /* pc_relative  */
1256
   0,     /* bitpos  */
1257
   complain_overflow_dont,/* complain_on_overflow  */
1258
   bfd_elf_generic_reloc, /* special_function  */
1259
   "R_NDS32_PLTREL_HI20", /* name  */
1260
   false,     /* partial_inplace  */
1261
   0x000fffff,    /* src_mask  */
1262
   0x000fffff,    /* dst_mask  */
1263
   false),    /* pcrel_offset  */
1264
1265
  /* Low 12 bits of PLT symbol offset relative to PC.  */
1266
  HOWTO2 (R_NDS32_PLTREL_LO12,  /* type  */
1267
   0,     /* rightshift  */
1268
   4,     /* size  */
1269
   12,      /* bitsize  */
1270
   false,     /* pc_relative  */
1271
   0,     /* bitpos  */
1272
   complain_overflow_dont,/* complain_on_overflow  */
1273
   bfd_elf_generic_reloc, /* special_function  */
1274
   "R_NDS32_PLTREL_LO12", /* name  */
1275
   false,     /* partial_inplace  */
1276
   0x00000fff,    /* src_mask  */
1277
   0x00000fff,    /* dst_mask  */
1278
   false),    /* pcrel_offset  */
1279
1280
  /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
1281
  HOWTO2 (R_NDS32_PLT_GOTREL_HI20,  /* type  */
1282
   12,      /* rightshift  */
1283
   4,     /* size  */
1284
   20,      /* bitsize  */
1285
   false,     /* pc_relative  */
1286
   0,     /* bitpos  */
1287
   complain_overflow_dont,/* complain_on_overflow  */
1288
   bfd_elf_generic_reloc, /* special_function  */
1289
   "R_NDS32_PLT_GOTREL_HI20",/* name  */
1290
   false,     /* partial_inplace  */
1291
   0x000fffff,    /* src_mask  */
1292
   0x000fffff,    /* dst_mask  */
1293
   false),    /* pcrel_offset  */
1294
1295
  /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
1296
  HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type  */
1297
   0,     /* rightshift  */
1298
   4,     /* size  */
1299
   12,      /* bitsize  */
1300
   false,     /* pc_relative  */
1301
   0,     /* bitpos  */
1302
   complain_overflow_dont,/* complain_on_overflow  */
1303
   bfd_elf_generic_reloc, /* special_function  */
1304
   "R_NDS32_PLT_GOTREL_LO12",/* name  */
1305
   false,     /* partial_inplace  */
1306
   0x00000fff,    /* src_mask  */
1307
   0x00000fff,    /* dst_mask  */
1308
   false),    /* pcrel_offset  */
1309
1310
  /* Small data area 12 bits offset.  */
1311
  HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type  */
1312
   2,     /* rightshift  */
1313
   4,     /* size  */
1314
   12,      /* bitsize  */
1315
   false,     /* pc_relative  */
1316
   0,     /* bitpos  */
1317
   complain_overflow_signed,/* complain_on_overflow  */
1318
   bfd_elf_generic_reloc, /* special_function  */
1319
   "R_NDS32_SDA12S2_DP_RELA",/* name  */
1320
   false,     /* partial_inplace  */
1321
   0x00000fff,    /* src_mask  */
1322
   0x00000fff,    /* dst_mask  */
1323
   false),    /* pcrel_offset  */
1324
1325
  /* Small data area 12 bits offset.  */
1326
  HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type  */
1327
   2,     /* rightshift  */
1328
   4,     /* size  */
1329
   12,      /* bitsize  */
1330
   false,     /* pc_relative  */
1331
   0,     /* bitpos  */
1332
   complain_overflow_signed,/* complain_on_overflow  */
1333
   bfd_elf_generic_reloc, /* special_function  */
1334
   "R_NDS32_SDA12S2_SP_RELA",/* name  */
1335
   false,     /* partial_inplace  */
1336
   0x00000fff,    /* src_mask  */
1337
   0x00000fff,    /* dst_mask  */
1338
   false),    /* pcrel_offset  */
1339
  /* Lower 12 bits of address.  */
1340
1341
  HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type  */
1342
   2,     /* rightshift  */
1343
   4,     /* size  */
1344
   10,      /* bitsize  */
1345
   false,     /* pc_relative  */
1346
   0,     /* bitpos  */
1347
   complain_overflow_dont,/* complain_on_overflow  */
1348
   bfd_elf_generic_reloc, /* special_function  */
1349
   "R_NDS32_LO12S2_DP_RELA",/* name  */
1350
   false,     /* partial_inplace  */
1351
   0x000003ff,    /* src_mask  */
1352
   0x000003ff,    /* dst_mask  */
1353
   false),    /* pcrel_offset  */
1354
1355
  /* Lower 12 bits of address.  */
1356
  HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type  */
1357
   2,     /* rightshift  */
1358
   4,     /* size  */
1359
   10,      /* bitsize  */
1360
   false,     /* pc_relative  */
1361
   0,     /* bitpos  */
1362
   complain_overflow_dont,/* complain_on_overflow  */
1363
   bfd_elf_generic_reloc, /* special_function  */
1364
   "R_NDS32_LO12S2_SP_RELA",/* name  */
1365
   false,     /* partial_inplace  */
1366
   0x000003ff,    /* src_mask  */
1367
   0x000003ff,    /* dst_mask  */
1368
   false),    /* pcrel_offset  */
1369
1370
  /* Lower 12 bits of address.  Special identity for or case.  */
1371
  HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type  */
1372
   0,     /* rightshift  */
1373
   4,     /* size  */
1374
   12,      /* bitsize  */
1375
   false,     /* pc_relative  */
1376
   0,     /* bitpos  */
1377
   complain_overflow_dont,/* complain_on_overflow  */
1378
   bfd_elf_generic_reloc, /* special_function  */
1379
   "R_NDS32_LO12S0_ORI_RELA",/* name  */
1380
   false,     /* partial_inplace  */
1381
   0x00000fff,    /* src_mask  */
1382
   0x00000fff,    /* dst_mask  */
1383
   false),    /* pcrel_offset  */
1384
1385
  /* Small data area 19 bits offset.  */
1386
  HOWTO2 (R_NDS32_SDA16S3_RELA, /* type  */
1387
   3,     /* rightshift  */
1388
   4,     /* size  */
1389
   16,      /* bitsize  */
1390
   false,     /* pc_relative  */
1391
   0,     /* bitpos  */
1392
   complain_overflow_signed,/* complain_on_overflow  */
1393
   bfd_elf_generic_reloc, /* special_function  */
1394
   "R_NDS32_SDA16S3_RELA",/* name  */
1395
   false,     /* partial_inplace  */
1396
   0x0000ffff,    /* src_mask  */
1397
   0x0000ffff,    /* dst_mask  */
1398
   false),    /* pcrel_offset  */
1399
1400
  /* Small data area 15 bits offset.  */
1401
  HOWTO2 (R_NDS32_SDA17S2_RELA, /* type  */
1402
   2,     /* rightshift  */
1403
   4,     /* size  */
1404
   17,      /* bitsize  */
1405
   false,     /* pc_relative  */
1406
   0,     /* bitpos  */
1407
   complain_overflow_signed,/* complain_on_overflow  */
1408
   bfd_elf_generic_reloc, /* special_function  */
1409
   "R_NDS32_SDA17S2_RELA",/* name  */
1410
   false,     /* partial_inplace  */
1411
   0x0001ffff,    /* src_mask  */
1412
   0x0001ffff,    /* dst_mask  */
1413
   false),    /* pcrel_offset  */
1414
1415
  HOWTO2 (R_NDS32_SDA18S1_RELA, /* type  */
1416
   1,     /* rightshift  */
1417
   4,     /* size  */
1418
   18,      /* bitsize  */
1419
   false,     /* pc_relative  */
1420
   0,     /* bitpos  */
1421
   complain_overflow_signed,/* complain_on_overflow  */
1422
   bfd_elf_generic_reloc, /* special_function  */
1423
   "R_NDS32_SDA18S1_RELA",/* name  */
1424
   false,     /* partial_inplace  */
1425
   0x0003ffff,    /* src_mask  */
1426
   0x0003ffff,    /* dst_mask  */
1427
   false),    /* pcrel_offset  */
1428
1429
  HOWTO2 (R_NDS32_SDA19S0_RELA, /* type  */
1430
   0,     /* rightshift  */
1431
   4,     /* size  */
1432
   19,      /* bitsize  */
1433
   false,     /* pc_relative  */
1434
   0,     /* bitpos  */
1435
   complain_overflow_signed,/* complain_on_overflow  */
1436
   bfd_elf_generic_reloc, /* special_function  */
1437
   "R_NDS32_SDA19S0_RELA",/* name  */
1438
   false,     /* partial_inplace  */
1439
   0x0007ffff,    /* src_mask  */
1440
   0x0007ffff,    /* dst_mask  */
1441
   false),    /* pcrel_offset  */
1442
  HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type  */
1443
   0,     /* rightshift  */
1444
   1,     /* size  */
1445
   8,     /* bitsize  */
1446
   false,     /* pc_relative  */
1447
   0,     /* bitpos  */
1448
   complain_overflow_dont,/* complain_on_overflow  */
1449
   nds32_elf_ignore_reloc,/* special_function  */
1450
   "R_NDS32_DWARF2_OP1_RELA",/* name  */
1451
   false,     /* partial_inplace  */
1452
   0xff,      /* src_mask  */
1453
   0xff,      /* dst_mask  */
1454
   false),    /* pcrel_offset  */
1455
1456
  HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type  */
1457
   0,     /* rightshift  */
1458
   2,     /* size  */
1459
   16,      /* bitsize  */
1460
   false,     /* pc_relative  */
1461
   0,     /* bitpos  */
1462
   complain_overflow_dont,/* complain_on_overflow  */
1463
   nds32_elf_ignore_reloc,/* special_function  */
1464
   "R_NDS32_DWARF2_OP2_RELA",/* name  */
1465
   false,     /* partial_inplace  */
1466
   0xffff,    /* src_mask  */
1467
   0xffff,    /* dst_mask  */
1468
   false),    /* pcrel_offset  */
1469
1470
  HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type  */
1471
   0,     /* rightshift  */
1472
   4,     /* size  */
1473
   32,      /* bitsize  */
1474
   false,     /* pc_relative  */
1475
   0,     /* bitpos  */
1476
   complain_overflow_dont,/* complain_on_overflow  */
1477
   nds32_elf_ignore_reloc,/* special_function  */
1478
   "R_NDS32_DWARF2_LEB_RELA",/* name  */
1479
   false,     /* partial_inplace  */
1480
   0xffffffff,    /* src_mask  */
1481
   0xffffffff,    /* dst_mask  */
1482
   false),    /* pcrel_offset  */
1483
1484
  HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type  */
1485
   0,     /* rightshift  */
1486
   2,     /* size  */
1487
   16,      /* bitsize  */
1488
   false,     /* pc_relative  */
1489
   0,     /* bitpos  */
1490
   complain_overflow_dont,/* complain_on_overflow  */
1491
   nds32_elf_ignore_reloc,/* special_function  */
1492
   "R_NDS32_UPDATE_TA_RELA",/* name  */
1493
   false,     /* partial_inplace  */
1494
   0xffff,    /* src_mask  */
1495
   0xffff,    /* dst_mask  */
1496
   false),    /* pcrel_offset  */
1497
1498
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1499
     entry for the symbol.  */
1500
  HOWTO2 (R_NDS32_9_PLTREL, /* type  */
1501
   1,     /* rightshift  */
1502
   2,     /* size  */
1503
   8,     /* bitsize  */
1504
   true,      /* pc_relative  */
1505
   0,     /* bitpos  */
1506
   complain_overflow_signed,/* complain_on_overflow  */
1507
   bfd_elf_generic_reloc, /* special_function  */
1508
   "R_NDS32_9_PLTREL",  /* name  */
1509
   false,     /* partial_inplace  */
1510
   0xff,      /* src_mask  */
1511
   0xff,      /* dst_mask  */
1512
   true),     /* pcrel_offset  */
1513
1514
  /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
1515
  HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type  */
1516
   0,     /* rightshift  */
1517
   4,     /* size  */
1518
   20,      /* bitsize  */
1519
   false,     /* pc_relative  */
1520
   0,     /* bitpos  */
1521
   complain_overflow_dont,/* complain_on_overflow  */
1522
   bfd_elf_generic_reloc, /* special_function  */
1523
   "R_NDS32_PLT_GOTREL_LO20",/* name  */
1524
   false,     /* partial_inplace  */
1525
   0x000fffff,    /* src_mask  */
1526
   0x000fffff,    /* dst_mask  */
1527
   false),    /* pcrel_offset  */
1528
1529
  /* low 15 bits of PLT symbol offset relative to GOT (GP)  */
1530
  HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type  */
1531
   0,     /* rightshift  */
1532
   4,     /* size  */
1533
   15,      /* bitsize  */
1534
   false,     /* pc_relative  */
1535
   0,     /* bitpos  */
1536
   complain_overflow_dont,/* complain_on_overflow  */
1537
   bfd_elf_generic_reloc, /* special_function  */
1538
   "R_NDS32_PLT_GOTREL_LO15",/* name  */
1539
   false,     /* partial_inplace  */
1540
   0x00007fff,    /* src_mask  */
1541
   0x00007fff,    /* dst_mask  */
1542
   false),    /* pcrel_offset  */
1543
1544
  /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
1545
  HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type  */
1546
   0,     /* rightshift  */
1547
   4,     /* size  */
1548
   19,      /* bitsize  */
1549
   false,     /* pc_relative  */
1550
   0,     /* bitpos  */
1551
   complain_overflow_dont,/* complain_on_overflow  */
1552
   bfd_elf_generic_reloc, /* special_function  */
1553
   "R_NDS32_PLT_GOTREL_LO19",/* name  */
1554
   false,     /* partial_inplace  */
1555
   0x0007ffff,    /* src_mask  */
1556
   0x0007ffff,    /* dst_mask  */
1557
   false),    /* pcrel_offset  */
1558
1559
  HOWTO2 (R_NDS32_GOT_LO15, /* type  */
1560
   0,     /* rightshift  */
1561
   4,     /* size  */
1562
   15,      /* bitsize  */
1563
   false,     /* pc_relative  */
1564
   0,     /* bitpos  */
1565
   complain_overflow_dont,/* complain_on_overflow  */
1566
   bfd_elf_generic_reloc, /* special_function  */
1567
   "R_NDS32_GOT_LO15",  /* name  */
1568
   false,     /* partial_inplace  */
1569
   0x00007fff,    /* src_mask  */
1570
   0x00007fff,    /* dst_mask  */
1571
   false),    /* pcrel_offset  */
1572
1573
  HOWTO2 (R_NDS32_GOT_LO19, /* type  */
1574
   0,     /* rightshift  */
1575
   4,     /* size  */
1576
   19,      /* 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_GOT_LO19",  /* name  */
1582
   false,     /* partial_inplace  */
1583
   0x0007ffff,    /* src_mask  */
1584
   0x0007ffff,    /* dst_mask  */
1585
   false),    /* pcrel_offset  */
1586
1587
  HOWTO2 (R_NDS32_GOTOFF_LO15,  /* type  */
1588
   0,     /* rightshift  */
1589
   4,     /* size  */
1590
   15,      /* bitsize  */
1591
   false,     /* pc_relative  */
1592
   0,     /* bitpos  */
1593
   complain_overflow_dont,/* complain_on_overflow  */
1594
   bfd_elf_generic_reloc, /* special_function  */
1595
   "R_NDS32_GOTOFF_LO15", /* name  */
1596
   false,     /* partial_inplace  */
1597
   0x00007fff,    /* src_mask  */
1598
   0x00007fff,    /* dst_mask  */
1599
   false),    /* pcrel_offset  */
1600
1601
  HOWTO2 (R_NDS32_GOTOFF_LO19,  /* type  */
1602
   0,     /* rightshift  */
1603
   4,     /* size  */
1604
   19,      /* bitsize  */
1605
   false,     /* pc_relative  */
1606
   0,     /* bitpos  */
1607
   complain_overflow_dont,/* complain_on_overflow  */
1608
   bfd_elf_generic_reloc, /* special_function  */
1609
   "R_NDS32_GOTOFF_LO19", /* name  */
1610
   false,     /* partial_inplace  */
1611
   0x0007ffff,    /* src_mask  */
1612
   0x0007ffff,    /* dst_mask  */
1613
   false),    /* pcrel_offset  */
1614
1615
  /* GOT 15 bits offset.  */
1616
  HOWTO2 (R_NDS32_GOT15S2_RELA, /* type  */
1617
   2,     /* rightshift  */
1618
   4,     /* size  */
1619
   15,      /* bitsize  */
1620
   false,     /* pc_relative  */
1621
   0,     /* bitpos  */
1622
   complain_overflow_signed,/* complain_on_overflow  */
1623
   bfd_elf_generic_reloc, /* special_function  */
1624
   "R_NDS32_GOT15S2_RELA",/* name  */
1625
   false,     /* partial_inplace  */
1626
   0x00007fff,    /* src_mask  */
1627
   0x00007fff,    /* dst_mask  */
1628
   false),    /* pcrel_offset  */
1629
1630
  /* GOT 17 bits offset.  */
1631
  HOWTO2 (R_NDS32_GOT17S2_RELA, /* type  */
1632
   2,     /* rightshift  */
1633
   4,     /* size  */
1634
   17,      /* bitsize  */
1635
   false,     /* pc_relative  */
1636
   0,     /* bitpos  */
1637
   complain_overflow_signed,/* complain_on_overflow  */
1638
   bfd_elf_generic_reloc, /* special_function  */
1639
   "R_NDS32_GOT17S2_RELA",/* name  */
1640
   false,     /* partial_inplace  */
1641
   0x0001ffff,    /* src_mask  */
1642
   0x0001ffff,    /* dst_mask  */
1643
   false),    /* pcrel_offset  */
1644
1645
  /* A 5 bit address.  */
1646
  HOWTO2 (R_NDS32_5_RELA, /* type  */
1647
   0,     /* rightshift  */
1648
   2,     /* size  */
1649
   5,     /* bitsize  */
1650
   false,     /* pc_relative  */
1651
   0,     /* bitpos  */
1652
   complain_overflow_signed,/* complain_on_overflow  */
1653
   bfd_elf_generic_reloc, /* special_function  */
1654
   "R_NDS32_5_RELA",  /* name  */
1655
   false,     /* partial_inplace  */
1656
   0x1f,      /* src_mask  */
1657
   0x1f,      /* dst_mask  */
1658
   false),    /* pcrel_offset  */
1659
1660
  HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type  */
1661
   1,     /* rightshift  */
1662
   2,     /* size  */
1663
   9,     /* bitsize  */
1664
   true,      /* pc_relative  */
1665
   0,     /* bitpos  */
1666
   complain_overflow_unsigned,/* complain_on_overflow  */
1667
   bfd_elf_generic_reloc, /* special_function  */
1668
   "R_NDS32_10_UPCREL_RELA",/* name  */
1669
   false,     /* partial_inplace  */
1670
   0x1ff,     /* src_mask  */
1671
   0x1ff,     /* dst_mask  */
1672
   true),     /* pcrel_offset  */
1673
1674
  HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type  */
1675
   2,     /* rightshift  */
1676
   2,     /* size  */
1677
   7,     /* bitsize  */
1678
   false,     /* pc_relative  */
1679
   0,     /* bitpos  */
1680
   complain_overflow_unsigned,/* complain_on_overflow  */
1681
   bfd_elf_generic_reloc, /* special_function  */
1682
   "R_NDS32_SDA_FP7U2_RELA",/* name  */
1683
   false,     /* partial_inplace  */
1684
   0x0000007f,    /* src_mask  */
1685
   0x0000007f,    /* dst_mask  */
1686
   false),    /* pcrel_offset  */
1687
1688
  HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type  */
1689
   1,     /* rightshift  */
1690
   4,     /* size  */
1691
   8,     /* bitsize  */
1692
   true,      /* pc_relative  */
1693
   0,     /* bitpos  */
1694
   complain_overflow_signed,/* complain_on_overflow  */
1695
   bfd_elf_generic_reloc, /* special_function  */
1696
   "R_NDS32_WORD_9_PCREL_RELA",/* name  */
1697
   false,     /* partial_inplace  */
1698
   0xff,      /* src_mask  */
1699
   0xff,      /* dst_mask  */
1700
   true),     /* pcrel_offset  */
1701
1702
  HOWTO2 (R_NDS32_25_ABS_RELA,  /* type  */
1703
   1,     /* rightshift  */
1704
   4,     /* size  */
1705
   24,      /* bitsize  */
1706
   false,     /* pc_relative  */
1707
   0,     /* bitpos  */
1708
   complain_overflow_dont,/* complain_on_overflow  */
1709
   bfd_elf_generic_reloc, /* special_function  */
1710
   "R_NDS32_25_ABS_RELA", /* name  */
1711
   false,     /* partial_inplace  */
1712
   0xffffff,    /* src_mask  */
1713
   0xffffff,    /* dst_mask  */
1714
   false),    /* pcrel_offset  */
1715
1716
  /* A relative 17 bit relocation for ifc, right shifted by 1.  */
1717
  HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type  */
1718
   1,     /* rightshift  */
1719
   4,     /* size  */
1720
   16,      /* bitsize  */
1721
   true,      /* pc_relative  */
1722
   0,     /* bitpos  */
1723
   complain_overflow_signed,/* complain_on_overflow  */
1724
   bfd_elf_generic_reloc, /* special_function  */
1725
   "R_NDS32_17IFC_PCREL_RELA",/* name  */
1726
   false,     /* partial_inplace  */
1727
   0xffff,    /* src_mask  */
1728
   0xffff,    /* dst_mask  */
1729
   true),     /* pcrel_offset  */
1730
1731
  /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
1732
  HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type  */
1733
   1,     /* rightshift  */
1734
   2,     /* size  */
1735
   9,     /* bitsize  */
1736
   true,      /* pc_relative  */
1737
   0,     /* bitpos  */
1738
   complain_overflow_unsigned,/* complain_on_overflow  */
1739
   bfd_elf_generic_reloc, /* special_function  */
1740
   "R_NDS32_10IFCU_PCREL_RELA",/* name  */
1741
   false,     /* partial_inplace  */
1742
   0x1ff,     /* src_mask  */
1743
   0x1ff,     /* dst_mask  */
1744
   true),     /* pcrel_offset  */
1745
1746
  /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol.  */
1747
  HOWTO2 (R_NDS32_TLS_LE_HI20,  /* type  */
1748
   12,      /* rightshift  */
1749
   4,     /* size  */
1750
   20,      /* bitsize  */
1751
   false,     /* pc_relative  */
1752
   0,     /* bitpos  */
1753
   complain_overflow_dont,/* complain_on_overflow  */
1754
   bfd_elf_generic_reloc, /* special_function  */
1755
   "R_NDS32_TLS_LE_HI20", /* name  */
1756
   false,     /* partial_inplace  */
1757
   0x000fffff,    /* src_mask  */
1758
   0x000fffff,    /* dst_mask  */
1759
   false),    /* pcrel_offset  */
1760
1761
  HOWTO2 (R_NDS32_TLS_LE_LO12,  /* type  */
1762
   0,     /* rightshift  */
1763
   4,     /* size  */
1764
   12,      /* bitsize  */
1765
   false,     /* pc_relative  */
1766
   0,     /* bitpos  */
1767
   complain_overflow_dont,/* complain_on_overflow  */
1768
   bfd_elf_generic_reloc, /* special_function  */
1769
   "R_NDS32_TLS_LE_LO12", /* name  */
1770
   false,     /* partial_inplace  */
1771
   0x00000fff,    /* src_mask  */
1772
   0x00000fff,    /* dst_mask  */
1773
   false),    /* pcrel_offset  */
1774
1775
  /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol.  */
1776
  HOWTO2 (R_NDS32_TLS_IE_HI20,  /* type  */
1777
   12,      /* rightshift  */
1778
   4,     /* size  */
1779
   20,      /* bitsize  */
1780
   false,     /* pc_relative  */
1781
   0,     /* bitpos  */
1782
   complain_overflow_dont,/* complain_on_overflow  */
1783
   bfd_elf_generic_reloc, /* special_function  */
1784
   "R_NDS32_TLS_IE_HI20", /* name  */
1785
   false,     /* partial_inplace  */
1786
   0x000fffff,    /* src_mask  */
1787
   0x000fffff,    /* dst_mask  */
1788
   false),    /* pcrel_offset  */
1789
1790
  HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type  */
1791
   2,     /* rightshift  */
1792
   4,     /* size  */
1793
   10,      /* bitsize  */
1794
   false,     /* pc_relative  */
1795
   0,     /* bitpos  */
1796
   complain_overflow_dont,/* complain_on_overflow  */
1797
   bfd_elf_generic_reloc, /* special_function  */
1798
   "R_NDS32_TLS_IE_LO12S2",/* name  */
1799
   false,     /* partial_inplace  */
1800
   0x000003ff,    /* src_mask  */
1801
   0x000003ff,    /* dst_mask  */
1802
   false),    /* pcrel_offset  */
1803
1804
  /* TLS LE TP offset relocation  */
1805
  HOWTO2 (R_NDS32_TLS_TPOFF,  /* type  */
1806
   0,     /* rightshift  */
1807
   4,     /* size  */
1808
   32,      /* bitsize  */
1809
   false,     /* pc_relative  */
1810
   0,     /* bitpos  */
1811
   complain_overflow_bitfield,/* complain_on_overflow  */
1812
   bfd_elf_generic_reloc, /* special_function  */
1813
   "R_NDS32_TLS_TPOFF", /* name  */
1814
   false,     /* partial_inplace  */
1815
   0xffffffff,    /* src_mask  */
1816
   0xffffffff,    /* dst_mask  */
1817
   false),    /* pcrel_offset  */
1818
1819
  /* A 20 bit address.  */
1820
  HOWTO2 (R_NDS32_TLS_LE_20,  /* type  */
1821
   0,     /* rightshift  */
1822
   4,     /* size  */
1823
   20,      /* bitsize  */
1824
   false,     /* pc_relative  */
1825
   0,     /* bitpos  */
1826
   complain_overflow_signed,/* complain_on_overflow  */
1827
   bfd_elf_generic_reloc, /* special_function  */
1828
   "R_NDS32_TLS_LE_20", /* name  */
1829
   false,     /* partial_inplace  */
1830
   0xfffff,   /* src_mask  */
1831
   0xfffff,   /* dst_mask  */
1832
   false),    /* pcrel_offset  */
1833
1834
  HOWTO2 (R_NDS32_TLS_LE_15S0,  /* type  */
1835
   0,     /* rightshift  */
1836
   4,     /* size  */
1837
   15,      /* bitsize  */
1838
   false,     /* pc_relative  */
1839
   0,     /* bitpos  */
1840
   complain_overflow_signed,/* complain_on_overflow  */
1841
   bfd_elf_generic_reloc, /* special_function  */
1842
   "R_NDS32_TLS_LE_15S0", /* name  */
1843
   false,     /* partial_inplace  */
1844
   0x7fff,    /* src_mask  */
1845
   0x7fff,    /* dst_mask  */
1846
   false),    /* pcrel_offset  */
1847
1848
  HOWTO2 (R_NDS32_TLS_LE_15S1,  /* type  */
1849
   1,     /* rightshift  */
1850
   4,     /* size  */
1851
   15,      /* bitsize  */
1852
   false,     /* pc_relative  */
1853
   0,     /* bitpos  */
1854
   complain_overflow_signed,/* complain_on_overflow  */
1855
   bfd_elf_generic_reloc, /* special_function  */
1856
   "R_NDS32_TLS_LE_15S1", /* name  */
1857
   false,     /* partial_inplace  */
1858
   0x7fff,    /* src_mask  */
1859
   0x7fff,    /* dst_mask  */
1860
   false),    /* pcrel_offset  */
1861
1862
  HOWTO2 (R_NDS32_TLS_LE_15S2,  /* type  */
1863
   2,     /* rightshift  */
1864
   4,     /* size  */
1865
   15,      /* bitsize  */
1866
   false,     /* pc_relative  */
1867
   0,     /* bitpos  */
1868
   complain_overflow_signed,/* complain_on_overflow  */
1869
   bfd_elf_generic_reloc, /* special_function  */
1870
   "R_NDS32_TLS_LE_15S2", /* name  */
1871
   false,     /* partial_inplace  */
1872
   0x7fff,    /* src_mask  */
1873
   0x7fff,    /* dst_mask  */
1874
   false),    /* pcrel_offset  */
1875
1876
  /* Relax hint for unconditional call sequence  */
1877
  HOWTO2 (R_NDS32_LONGCALL4,  /* type  */
1878
   0,     /* rightshift  */
1879
   4,     /* size  */
1880
   32,      /* bitsize  */
1881
   false,     /* pc_relative  */
1882
   0,     /* bitpos  */
1883
   complain_overflow_dont,/* complain_on_overflow  */
1884
   nds32_elf_ignore_reloc,/* special_function  */
1885
   "R_NDS32_LONGCALL4", /* name  */
1886
   false,     /* partial_inplace  */
1887
   0xffffffff,    /* src_mask  */
1888
   0xffffffff,    /* dst_mask  */
1889
   false),    /* pcrel_offset  */
1890
1891
  /* Relax hint for conditional call sequence.  */
1892
  HOWTO2 (R_NDS32_LONGCALL5,  /* type  */
1893
   0,     /* rightshift  */
1894
   4,     /* size  */
1895
   32,      /* bitsize  */
1896
   false,     /* pc_relative  */
1897
   0,     /* bitpos  */
1898
   complain_overflow_dont,/* complain_on_overflow  */
1899
   nds32_elf_ignore_reloc,/* special_function  */
1900
   "R_NDS32_LONGCALL5", /* name  */
1901
   false,     /* partial_inplace  */
1902
   0xffffffff,    /* src_mask  */
1903
   0xffffffff,    /* dst_mask  */
1904
   false),    /* pcrel_offset  */
1905
1906
  /* Relax hint for conditional call sequence.  */
1907
  HOWTO2 (R_NDS32_LONGCALL6,  /* type  */
1908
   0,     /* rightshift  */
1909
   4,     /* size  */
1910
   32,      /* bitsize  */
1911
   false,     /* pc_relative  */
1912
   0,     /* bitpos  */
1913
   complain_overflow_dont,/* complain_on_overflow  */
1914
   nds32_elf_ignore_reloc,/* special_function  */
1915
   "R_NDS32_LONGCALL6", /* name  */
1916
   false,     /* partial_inplace  */
1917
   0xffffffff,    /* src_mask  */
1918
   0xffffffff,    /* dst_mask  */
1919
   false),    /* pcrel_offset  */
1920
1921
  /* Relax hint for unconditional branch sequence.  */
1922
  HOWTO2 (R_NDS32_LONGJUMP4,  /* type  */
1923
   0,     /* rightshift  */
1924
   4,     /* size  */
1925
   32,      /* bitsize  */
1926
   false,     /* pc_relative  */
1927
   0,     /* bitpos  */
1928
   complain_overflow_dont,/* complain_on_overflow  */
1929
   nds32_elf_ignore_reloc,/* special_function  */
1930
   "R_NDS32_LONGJUMP4", /* name  */
1931
   false,     /* partial_inplace  */
1932
   0xffffffff,    /* src_mask  */
1933
   0xffffffff,    /* dst_mask  */
1934
   false),    /* pcrel_offset  */
1935
1936
  /* Relax hint for conditional branch sequence.  */
1937
  HOWTO2 (R_NDS32_LONGJUMP5,  /* type  */
1938
   0,     /* rightshift  */
1939
   4,     /* size  */
1940
   32,      /* bitsize  */
1941
   false,     /* pc_relative  */
1942
   0,     /* bitpos  */
1943
   complain_overflow_dont,/* complain_on_overflow  */
1944
   nds32_elf_ignore_reloc,/* special_function  */
1945
   "R_NDS32_LONGJUMP5", /* name  */
1946
   false,     /* partial_inplace  */
1947
   0xffffffff,    /* src_mask  */
1948
   0xffffffff,    /* dst_mask  */
1949
   false),    /* pcrel_offset  */
1950
1951
  /* Relax hint for conditional branch sequence.  */
1952
  HOWTO2 (R_NDS32_LONGJUMP6,  /* type  */
1953
   0,     /* rightshift  */
1954
   4,     /* size  */
1955
   32,      /* bitsize  */
1956
   false,     /* pc_relative  */
1957
   0,     /* bitpos  */
1958
   complain_overflow_dont,/* complain_on_overflow  */
1959
   nds32_elf_ignore_reloc,/* special_function  */
1960
   "R_NDS32_LONGJUMP6", /* name  */
1961
   false,     /* partial_inplace  */
1962
   0xffffffff,    /* src_mask  */
1963
   0xffffffff,    /* dst_mask  */
1964
   false),    /* pcrel_offset  */
1965
1966
  /* Relax hint for conditional branch sequence.  */
1967
  HOWTO2 (R_NDS32_LONGJUMP7,  /* type  */
1968
   0,     /* rightshift  */
1969
   4,     /* size  */
1970
   32,      /* bitsize  */
1971
   false,     /* pc_relative  */
1972
   0,     /* bitpos  */
1973
   complain_overflow_dont,/* complain_on_overflow  */
1974
   nds32_elf_ignore_reloc,/* special_function  */
1975
   "R_NDS32_LONGJUMP7", /* name  */
1976
   false,     /* partial_inplace  */
1977
   0xffffffff,    /* src_mask  */
1978
   0xffffffff,    /* dst_mask  */
1979
   false),    /* pcrel_offset  */
1980
1981
  EMPTY_HOWTO (114),
1982
1983
  HOWTO2 (R_NDS32_TLS_IE_LO12,  /* type  */
1984
   0,     /* rightshift  */
1985
   4,     /* size  */
1986
   12,      /* bitsize  */
1987
   false,     /* pc_relative  */
1988
   0,     /* bitpos  */
1989
   complain_overflow_dont,/* complain_on_overflow  */
1990
   bfd_elf_generic_reloc, /* special_function  */
1991
   "R_NDS32_TLS_IE_LO12", /* name  */
1992
   false,     /* partial_inplace  */
1993
   0x00000fff,    /* src_mask  */
1994
   0x00000fff,    /* dst_mask  */
1995
   false),    /* pcrel_offset  */
1996
1997
  /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1998
     entry for the symbol.  */
1999
  HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type  */
2000
   12,      /* rightshift  */
2001
   4,     /* size  */
2002
   20,      /* bitsize  */
2003
   false,     /* pc_relative  */
2004
   0,     /* bitpos  */
2005
   complain_overflow_dont,/* complain_on_overflow  */
2006
   bfd_elf_generic_reloc, /* special_function  */
2007
   "R_NDS32_TLS_IEGP_HI20",/* name  */
2008
   false,     /* partial_inplace  */
2009
   0x000fffff,    /* src_mask  */
2010
   0x000fffff,    /* dst_mask  */
2011
   false),    /* pcrel_offset  */
2012
2013
  HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type  */
2014
   0,     /* rightshift  */
2015
   4,     /* size  */
2016
   12,      /* bitsize  */
2017
   false,     /* pc_relative  */
2018
   0,     /* bitpos  */
2019
   complain_overflow_dont,/* complain_on_overflow  */
2020
   bfd_elf_generic_reloc, /* special_function  */
2021
   "R_NDS32_TLS_IEGP_LO12",/* name  */
2022
   false,     /* partial_inplace  */
2023
   0x00000fff,    /* src_mask  */
2024
   0x00000fff,    /* dst_mask  */
2025
   false),    /* pcrel_offset  */
2026
2027
  HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type  */
2028
   2,     /* rightshift  */
2029
   4,     /* size  */
2030
   10,      /* bitsize  */
2031
   false,     /* pc_relative  */
2032
   0,     /* bitpos  */
2033
   complain_overflow_dont,/* complain_on_overflow  */
2034
   bfd_elf_generic_reloc, /* special_function  */
2035
   "R_NDS32_TLS_IEGP_LO12S2",/* name  */
2036
   false,     /* partial_inplace  */
2037
   0x000003ff,    /* src_mask  */
2038
   0x000003ff,    /* dst_mask  */
2039
   false),    /* pcrel_offset  */
2040
2041
  /* TLS description relocation  */
2042
  HOWTO2 (R_NDS32_TLS_DESC, /* type  */
2043
   12,      /* rightshift  */
2044
   4,     /* size  */
2045
   20,      /* bitsize  */
2046
   false,     /* pc_relative  */
2047
   0,     /* bitpos  */
2048
   complain_overflow_dont,/* complain_on_overflow  */
2049
   nds32_elf_hi20_reloc,  /* special_function  */
2050
   "R_NDS32_TLS_DESC_HI20",/* name  */
2051
   false,     /* partial_inplace  */
2052
   0x000fffff,    /* src_mask  */
2053
   0x000fffff,    /* dst_mask  */
2054
   false),    /* pcrel_offset  */
2055
2056
  /* TLS GD/LD description offset high part.  */
2057
  HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type  */
2058
   12,      /* rightshift  */
2059
   4,     /* size  */
2060
   20,      /* bitsize  */
2061
   false,     /* pc_relative  */
2062
   0,     /* bitpos  */
2063
   complain_overflow_dont,/* complain_on_overflow  */
2064
   nds32_elf_hi20_reloc,  /* special_function  */
2065
   "R_NDS32_TLS_DESC_HI20",/* name  */
2066
   false,     /* partial_inplace  */
2067
   0x000fffff,    /* src_mask  */
2068
   0x000fffff,    /* dst_mask  */
2069
   false),    /* pcrel_offset  */
2070
2071
  /* TLS GD/LD description offset low part.  */
2072
  HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type  */
2073
   0,     /* rightshift  */
2074
   4,     /* size  */
2075
   12,      /* bitsize  */
2076
   false,     /* pc_relative  */
2077
   0,     /* bitpos  */
2078
   complain_overflow_dont,/* complain_on_overflow  */
2079
   nds32_elf_lo12_reloc,  /* special_function  */
2080
   "R_NDS32_TLS_DESC_LO12",/* name  */
2081
   false,     /* partial_inplace  */
2082
   0x00000fff,    /* src_mask  */
2083
   0x00000fff,    /* dst_mask  */
2084
   false),    /* pcrel_offset  */
2085
2086
  /* TLS GD/LD description offset set (movi).  */
2087
  HOWTO2 (R_NDS32_TLS_DESC_20,  /* type  */
2088
   0,     /* rightshift  */
2089
   4,     /* size  */
2090
   20,      /* bitsize  */
2091
   false,     /* pc_relative  */
2092
   0,     /* bitpos  */
2093
   complain_overflow_signed,/* complain_on_overflow  */
2094
   bfd_elf_generic_reloc, /* special_function  */
2095
   "R_NDS32_TLS_DESC_20", /* name  */
2096
   false,     /* partial_inplace  */
2097
   0x000fffff,    /* src_mask  */
2098
   0x000fffff,    /* dst_mask  */
2099
   false),    /* pcrel_offset  */
2100
2101
  /* TLS GD/LD description offset set (lwi.gp).  */
2102
  HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type  */
2103
   2,     /* rightshift  */
2104
   4,     /* size  */
2105
   17,      /* bitsize  */
2106
   false,     /* pc_relative  */
2107
   0,     /* bitpos  */
2108
   complain_overflow_signed,/* complain_on_overflow  */
2109
   bfd_elf_generic_reloc, /* special_function  */
2110
   "R_NDS32_TLS_DESC_SDA17S2",/* name  */
2111
   false,     /* partial_inplace  */
2112
   0x0001ffff,    /* src_mask  */
2113
   0x0001ffff,    /* dst_mask  */
2114
   false),    /* pcrel_offset  */
2115
};
2116
2117
/* Relocations used for relaxation.  */
2118
#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2119
  [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2120
2121
static reloc_howto_type nds32_elf_relax_howto_table[] = {
2122
  HOWTO3 (R_NDS32_RELAX_ENTRY,  /* type  */
2123
   0,     /* rightshift  */
2124
   4,     /* size  */
2125
   32,      /* bitsize  */
2126
   false,     /* pc_relative  */
2127
   0,     /* bitpos  */
2128
   complain_overflow_dont,/* complain_on_overflow  */
2129
   nds32_elf_ignore_reloc,/* special_function  */
2130
   "R_NDS32_RELAX_ENTRY", /* name  */
2131
   false,     /* partial_inplace  */
2132
   0xffffffff,    /* src_mask  */
2133
   0xffffffff,    /* dst_mask  */
2134
   false),    /* pcrel_offset  */
2135
2136
  HOWTO3 (R_NDS32_GOT_SUFF, /* type  */
2137
   0,     /* rightshift  */
2138
   4,     /* size  */
2139
   32,      /* bitsize  */
2140
   false,     /* pc_relative  */
2141
   0,     /* bitpos  */
2142
   complain_overflow_dont,/* complain_on_overflow  */
2143
   nds32_elf_ignore_reloc,/* special_function  */
2144
   "R_NDS32_GOT_SUFF",  /* name  */
2145
   false,     /* partial_inplace  */
2146
   0xffffffff,    /* src_mask  */
2147
   0xffffffff,    /* dst_mask  */
2148
   false),    /* pcrel_offset  */
2149
2150
  HOWTO3 (R_NDS32_GOTOFF_SUFF,  /* type  */
2151
   0,     /* rightshift  */
2152
   4,     /* size  */
2153
   32,      /* bitsize  */
2154
   false,     /* pc_relative  */
2155
   0,     /* bitpos  */
2156
   complain_overflow_bitfield,/* complain_on_overflow  */
2157
   nds32_elf_ignore_reloc,/* special_function  */
2158
   "R_NDS32_GOTOFF_SUFF", /* name  */
2159
   false,     /* partial_inplace  */
2160
   0xffffffff,    /* src_mask  */
2161
   0xffffffff,    /* dst_mask  */
2162
   false),    /* pcrel_offset  */
2163
2164
  HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type  */
2165
   0,     /* rightshift  */
2166
   4,     /* size  */
2167
   32,      /* bitsize  */
2168
   false,     /* pc_relative  */
2169
   0,     /* bitpos  */
2170
   complain_overflow_dont,/* complain_on_overflow  */
2171
   nds32_elf_ignore_reloc,/* special_function  */
2172
   "R_NDS32_PLT_GOT_SUFF",/* name  */
2173
   false,     /* partial_inplace  */
2174
   0xffffffff,    /* src_mask  */
2175
   0xffffffff,    /* dst_mask  */
2176
   false),    /* pcrel_offset  */
2177
2178
  HOWTO3 (R_NDS32_MULCALL_SUFF, /* type  */
2179
   0,     /* rightshift  */
2180
   4,     /* size  */
2181
   32,      /* bitsize  */
2182
   false,     /* pc_relative  */
2183
   0,     /* bitpos  */
2184
   complain_overflow_dont,/* complain_on_overflow  */
2185
   nds32_elf_ignore_reloc,/* special_function  */
2186
   "R_NDS32_MULCALL_SUFF",/* name  */
2187
   false,     /* partial_inplace  */
2188
   0xffffffff,    /* src_mask  */
2189
   0xffffffff,    /* dst_mask  */
2190
   false),    /* pcrel_offset  */
2191
2192
  HOWTO3 (R_NDS32_PTR,    /* type  */
2193
   0,     /* rightshift  */
2194
   4,     /* size  */
2195
   32,      /* bitsize  */
2196
   false,     /* pc_relative  */
2197
   0,     /* bitpos  */
2198
   complain_overflow_dont,/* complain_on_overflow  */
2199
   nds32_elf_ignore_reloc,/* special_function  */
2200
   "R_NDS32_PTR",   /* name  */
2201
   false,     /* partial_inplace  */
2202
   0xffffffff,    /* src_mask  */
2203
   0xffffffff,    /* dst_mask  */
2204
   false),    /* pcrel_offset  */
2205
2206
  HOWTO3 (R_NDS32_PTR_COUNT,  /* type  */
2207
   0,     /* rightshift  */
2208
   4,     /* size  */
2209
   32,      /* bitsize  */
2210
   false,     /* pc_relative  */
2211
   0,     /* bitpos  */
2212
   complain_overflow_dont,/* complain_on_overflow  */
2213
   nds32_elf_ignore_reloc,/* special_function  */
2214
   "R_NDS32_PTR_COUNT", /* name  */
2215
   false,     /* partial_inplace  */
2216
   0xffffffff,    /* src_mask  */
2217
   0xffffffff,    /* dst_mask  */
2218
   false),    /* pcrel_offset  */
2219
2220
  HOWTO3 (R_NDS32_PTR_RESOLVED, /* type  */
2221
   0,     /* rightshift  */
2222
   4,     /* size  */
2223
   32,      /* bitsize  */
2224
   false,     /* pc_relative  */
2225
   0,     /* bitpos  */
2226
   complain_overflow_dont,/* complain_on_overflow  */
2227
   nds32_elf_ignore_reloc,/* special_function  */
2228
   "R_NDS32_PTR_RESOLVED",/* name  */
2229
   false,     /* partial_inplace  */
2230
   0xffffffff,    /* src_mask  */
2231
   0xffffffff,    /* dst_mask  */
2232
   false),    /* pcrel_offset  */
2233
2234
  HOWTO3 (R_NDS32_PLTBLOCK, /* type  */
2235
   0,     /* rightshift  */
2236
   4,     /* size  */
2237
   32,      /* bitsize  */
2238
   false,     /* pc_relative  */
2239
   0,     /* bitpos  */
2240
   complain_overflow_dont,/* complain_on_overflow  */
2241
   nds32_elf_ignore_reloc,/* special_function  */
2242
   "R_NDS32_PLTBLOCK",  /* name  */
2243
   false,     /* partial_inplace  */
2244
   0xffffffff,    /* src_mask  */
2245
   0xffffffff,    /* dst_mask  */
2246
   false),    /* pcrel_offset  */
2247
2248
  HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type  */
2249
   0,     /* rightshift  */
2250
   4,     /* size  */
2251
   32,      /* bitsize  */
2252
   false,     /* pc_relative  */
2253
   0,     /* bitpos  */
2254
   complain_overflow_dont,/* complain_on_overflow  */
2255
   nds32_elf_ignore_reloc,/* special_function  */
2256
   "R_NDS32_RELAX_REGION_BEGIN",/* name  */
2257
   false,     /* partial_inplace  */
2258
   0xffffffff,    /* src_mask  */
2259
   0xffffffff,    /* dst_mask  */
2260
   false),    /* pcrel_offset  */
2261
2262
  HOWTO3 (R_NDS32_RELAX_REGION_END,/* type  */
2263
   0,     /* rightshift  */
2264
   4,     /* size  */
2265
   32,      /* bitsize  */
2266
   false,     /* pc_relative  */
2267
   0,     /* bitpos  */
2268
   complain_overflow_dont,/* complain_on_overflow  */
2269
   nds32_elf_ignore_reloc,/* special_function  */
2270
   "R_NDS32_RELAX_REGION_END",/* name  */
2271
   false,     /* partial_inplace  */
2272
   0xffffffff,    /* src_mask  */
2273
   0xffffffff,    /* dst_mask  */
2274
   false),    /* pcrel_offset  */
2275
2276
  HOWTO3 (R_NDS32_MINUEND,  /* type  */
2277
   0,     /* rightshift  */
2278
   4,     /* size  */
2279
   32,      /* bitsize  */
2280
   false,     /* pc_relative  */
2281
   0,     /* bitpos  */
2282
   complain_overflow_dont,/* complain_on_overflow  */
2283
   nds32_elf_ignore_reloc,/* special_function  */
2284
   "R_NDS32_MINUEND", /* name  */
2285
   false,     /* partial_inplace  */
2286
   0xffffffff,    /* src_mask  */
2287
   0xffffffff,    /* dst_mask  */
2288
   false),    /* pcrel_offset  */
2289
2290
  HOWTO3 (R_NDS32_SUBTRAHEND, /* type  */
2291
   0,     /* rightshift  */
2292
   4,     /* size  */
2293
   32,      /* bitsize  */
2294
   false,     /* pc_relative  */
2295
   0,     /* bitpos  */
2296
   complain_overflow_dont,/* complain_on_overflow  */
2297
   nds32_elf_ignore_reloc,/* special_function  */
2298
   "R_NDS32_SUBTRAHEND",  /* name  */
2299
   false,     /* partial_inplace  */
2300
   0xffffffff,    /* src_mask  */
2301
   0xffffffff,    /* dst_mask  */
2302
   false),    /* pcrel_offset  */
2303
2304
  HOWTO3 (R_NDS32_DIFF8,  /* type  */
2305
   0,     /* rightshift  */
2306
   1,     /* size  */
2307
   8,     /* 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_DIFF8", /* name  */
2313
   false,     /* partial_inplace  */
2314
   0x000000ff,    /* src_mask  */
2315
   0x000000ff,    /* dst_mask  */
2316
   false),    /* pcrel_offset  */
2317
2318
  HOWTO3 (R_NDS32_DIFF16, /* type  */
2319
   0,     /* rightshift  */
2320
   2,     /* size  */
2321
   16,      /* bitsize  */
2322
   false,     /* pc_relative  */
2323
   0,     /* bitpos  */
2324
   complain_overflow_dont,/* complain_on_overflow  */
2325
   nds32_elf_ignore_reloc,/* special_function  */
2326
   "R_NDS32_DIFF16",  /* name  */
2327
   false,     /* partial_inplace  */
2328
   0x0000ffff,    /* src_mask  */
2329
   0x0000ffff,    /* dst_mask  */
2330
   false),    /* pcrel_offset  */
2331
2332
  HOWTO3 (R_NDS32_DIFF32, /* type  */
2333
   0,     /* rightshift  */
2334
   4,     /* size  */
2335
   32,      /* bitsize  */
2336
   false,     /* pc_relative  */
2337
   0,     /* bitpos  */
2338
   complain_overflow_dont,/* complain_on_overflow  */
2339
   nds32_elf_ignore_reloc,/* special_function  */
2340
   "R_NDS32_DIFF32",  /* name  */
2341
   false,     /* partial_inplace  */
2342
   0xffffffff,    /* src_mask  */
2343
   0xffffffff,    /* dst_mask  */
2344
   false),    /* pcrel_offset  */
2345
2346
  HOWTO3 (R_NDS32_DIFF_ULEB128, /* type  */
2347
   0,     /* rightshift  */
2348
   1,     /* size  */
2349
   0,     /* bitsize  */
2350
   false,     /* pc_relative  */
2351
   0,     /* bitpos  */
2352
   complain_overflow_dont,/* complain_on_overflow  */
2353
   nds32_elf_ignore_reloc,/* special_function  */
2354
   "R_NDS32_DIFF_ULEB128",/* name  */
2355
   false,     /* partial_inplace  */
2356
   0xffffffff,    /* src_mask  */
2357
   0xffffffff,    /* dst_mask  */
2358
   false),    /* pcrel_offset  */
2359
2360
  HOWTO3 (R_NDS32_DATA,   /* type  */
2361
   0,     /* rightshift  */
2362
   4,     /* size  */
2363
   32,      /* bitsize  */
2364
   false,     /* pc_relative  */
2365
   0,     /* bitpos  */
2366
   complain_overflow_dont,/* complain_on_overflow  */
2367
   nds32_elf_ignore_reloc,/* special_function  */
2368
   "R_NDS32_DATA",  /* name  */
2369
   false,     /* partial_inplace  */
2370
   0xffffffff,    /* src_mask  */
2371
   0xffffffff,    /* dst_mask  */
2372
   false),    /* pcrel_offset  */
2373
2374
  HOWTO3 (R_NDS32_TRAN,   /* type  */
2375
   0,     /* rightshift  */
2376
   4,     /* size  */
2377
   32,      /* bitsize  */
2378
   false,     /* pc_relative  */
2379
   0,     /* bitpos  */
2380
   complain_overflow_dont,/* complain_on_overflow  */
2381
   nds32_elf_ignore_reloc,/* special_function  */
2382
   "R_NDS32_TRAN",  /* name  */
2383
   false,     /* partial_inplace  */
2384
   0xffffffff,    /* src_mask  */
2385
   0xffffffff,    /* dst_mask  */
2386
   false),    /* pcrel_offset  */
2387
2388
  HOWTO3 (R_NDS32_TLS_LE_ADD, /* type  */
2389
   0,     /* rightshift  */
2390
   4,     /* size  */
2391
   32,      /* bitsize  */
2392
   false,     /* pc_relative  */
2393
   0,     /* bitpos  */
2394
   complain_overflow_dont,/* complain_on_overflow  */
2395
   nds32_elf_ignore_reloc,/* special_function  */
2396
   "R_NDS32_TLS_LE_ADD",  /* name  */
2397
   false,     /* partial_inplace  */
2398
   0xffffffff,    /* src_mask  */
2399
   0xffffffff,    /* dst_mask  */
2400
   false),    /* pcrel_offset  */
2401
2402
  HOWTO3 (R_NDS32_TLS_LE_LS,  /* type  */
2403
   0,     /* rightshift  */
2404
   4,     /* size  */
2405
   32,      /* bitsize  */
2406
   false,     /* pc_relative  */
2407
   0,     /* bitpos  */
2408
   complain_overflow_dont,/* complain_on_overflow  */
2409
   nds32_elf_ignore_reloc,/* special_function  */
2410
   "R_NDS32_TLS_LE_LS", /* name  */
2411
   false,     /* partial_inplace  */
2412
   0xffffffff,    /* src_mask  */
2413
   0xffffffff,    /* dst_mask  */
2414
   false),    /* pcrel_offset  */
2415
2416
  HOWTO3 (R_NDS32_EMPTY,  /* type  */
2417
   0,     /* rightshift  */
2418
   4,     /* size  */
2419
   32,      /* bitsize  */
2420
   false,     /* pc_relative  */
2421
   0,     /* bitpos  */
2422
   complain_overflow_dont,/* complain_on_overflow  */
2423
   nds32_elf_ignore_reloc,/* special_function  */
2424
   "R_NDS32_EMPTY", /* name  */
2425
   false,     /* partial_inplace  */
2426
   0xffffffff,    /* src_mask  */
2427
   0xffffffff,    /* dst_mask  */
2428
   false),    /* pcrel_offset  */
2429
2430
  /* TLS GD/LD description address base addition.  */
2431
  HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type  */
2432
   0,     /* rightshift  */
2433
   4,     /* size  */
2434
   32,      /* bitsize  */
2435
   false,     /* pc_relative  */
2436
   0,     /* bitpos  */
2437
   complain_overflow_dont,/* complain_on_overflow  */
2438
   nds32_elf_ignore_reloc,/* special_function  */
2439
   "R_NDS32_TLS_DESC_ADD",/* name  */
2440
   false,     /* partial_inplace  */
2441
   0xffffffff,    /* src_mask  */
2442
   0xffffffff,    /* dst_mask  */
2443
   false),    /* pcrel_offset  */
2444
2445
  /* TLS GD/LD description function load.  */
2446
  HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type  */
2447
   0,     /* rightshift  */
2448
   4,     /* size  */
2449
   32,      /* bitsize  */
2450
   false,     /* pc_relative  */
2451
   0,     /* bitpos  */
2452
   complain_overflow_dont,/* complain_on_overflow  */
2453
   nds32_elf_ignore_reloc,/* special_function  */
2454
   "R_NDS32_TLS_DESC_FUNC",/* name  */
2455
   false,     /* partial_inplace  */
2456
   0xffffffff,    /* src_mask  */
2457
   0xffffffff,    /* dst_mask  */
2458
   false),    /* pcrel_offset  */
2459
2460
  /* TLS DESC resolve function call.  */
2461
  HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type  */
2462
   0,     /* rightshift  */
2463
   4,     /* size  */
2464
   32,      /* bitsize  */
2465
   false,     /* pc_relative  */
2466
   0,     /* bitpos  */
2467
   complain_overflow_dont,/* complain_on_overflow  */
2468
   nds32_elf_ignore_reloc,/* special_function  */
2469
   "R_NDS32_TLS_DESC_CALL",/* name  */
2470
   false,     /* partial_inplace  */
2471
   0xffffffff,    /* src_mask  */
2472
   0xffffffff,    /* dst_mask  */
2473
   false),    /* pcrel_offset  */
2474
2475
  /* TLS DESC variable access.  */
2476
  HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type  */
2477
   0,     /* rightshift  */
2478
   4,     /* size  */
2479
   32,      /* bitsize  */
2480
   false,     /* pc_relative  */
2481
   0,     /* bitpos  */
2482
   complain_overflow_dont,/* complain_on_overflow  */
2483
   nds32_elf_ignore_reloc,/* special_function  */
2484
   "R_NDS32_TLS_DESC_MEM",/* name  */
2485
   false,     /* partial_inplace  */
2486
   0xffffffff,    /* src_mask  */
2487
   0xffffffff,    /* dst_mask  */
2488
   false),    /* pcrel_offset  */
2489
2490
  /* TLS GD/LD description mark (@tlsdec).  */
2491
  HOWTO3 (R_NDS32_RELAX_REMOVE, /* type  */
2492
   0,     /* rightshift  */
2493
   4,     /* size  */
2494
   32,      /* bitsize  */
2495
   false,     /* pc_relative  */
2496
   0,     /* bitpos  */
2497
   complain_overflow_dont,/* complain_on_overflow  */
2498
   nds32_elf_ignore_reloc,/* special_function  */
2499
   "R_NDS32_REMOVE",  /* name  */
2500
   false,     /* partial_inplace  */
2501
   0xffffffff,    /* src_mask  */
2502
   0xffffffff,    /* dst_mask  */
2503
   false),    /* pcrel_offset  */
2504
2505
  /* TLS GD/LD description mark (@tlsdec).  */
2506
  HOWTO3 (R_NDS32_RELAX_GROUP,  /* type  */
2507
   0,     /* rightshift  */
2508
   4,     /* size  */
2509
   32,      /* bitsize  */
2510
   false,     /* pc_relative  */
2511
   0,     /* bitpos  */
2512
   complain_overflow_dont,/* complain_on_overflow  */
2513
   nds32_elf_ignore_reloc,/* special_function  */
2514
   "R_NDS32_GROUP", /* name  */
2515
   false,     /* partial_inplace  */
2516
   0xffffffff,    /* src_mask  */
2517
   0xffffffff,    /* dst_mask  */
2518
   false),    /* pcrel_offset  */
2519
2520
  HOWTO3 (R_NDS32_TLS_IEGP_LW,  /* type  */
2521
   0,     /* rightshift  */
2522
   4,     /* size  */
2523
   32,      /* bitsize  */
2524
   false,     /* pc_relative  */
2525
   0,     /* bitpos  */
2526
   complain_overflow_dont,/* complain_on_overflow  */
2527
   nds32_elf_ignore_reloc,/* special_function  */
2528
   "R_NDS32_TLS_IEGP_LW", /* name  */
2529
   false,     /* partial_inplace  */
2530
   0xffffffff,    /* src_mask  */
2531
   0xffffffff,    /* dst_mask  */
2532
   false),    /* pcrel_offset  */
2533
2534
  /* LA and FLSI relaxation.  */
2535
  HOWTO3 (R_NDS32_LSI,    /* type  */
2536
   0,     /* rightshift  */
2537
   4,     /* size  */
2538
   32,      /* bitsize  */
2539
   false,     /* pc_relative  */
2540
   0,     /* bitpos  */
2541
   complain_overflow_dont,/* complain_on_overflow  */
2542
   nds32_elf_ignore_reloc,/* special_function  */
2543
   "R_NDS32_LSI",   /* name  */
2544
   false,     /* partial_inplace  */
2545
   0xffffffff,    /* src_mask  */
2546
   0xffffffff,    /* dst_mask  */
2547
   false),
2548
};
2549
2550
static unsigned long dl_tlsdesc_lazy_trampoline[] =
2551
{
2552
  0x46200000,     /* sethi $r2,#0x0      */
2553
  0x58210000,     /* ori $r2,$r2,#0x0    */
2554
  0x40217400,     /* add $r2,$r2,$gp     */
2555
  0x04210000,     /* lwi $r2,[$r2+#0x0]  */
2556
  0x46300000,     /* sethi $r3,#0x0      */
2557
  0x58318000,     /* ori $r3,$r3,#0x0    */
2558
  0x4031f400,     /* add $r3,$r3,$gp     */
2559
  0x4a000800,     /* jr $r2              */
2560
};
2561
2562
static void
2563
nds32_put_trampoline (void *contents, const unsigned long *template,
2564
          unsigned count)
2565
0
{
2566
0
  unsigned ix;
2567
2568
0
  for (ix = 0; ix != count; ix++)
2569
0
    {
2570
0
      unsigned long insn = template[ix];
2571
0
      bfd_putb32 (insn, (char *) contents + ix * 4);
2572
0
    }
2573
0
}
2574
2575
/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2576
   This prototype is the same as qsort ().  */
2577
2578
static void
2579
nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2580
          int (*compar) (const void *lhs, const void *rhs))
2581
0
{
2582
0
  char *ptr = (char *) base;
2583
0
  int i, j;
2584
0
  char tmp[sizeof (Elf_Internal_Rela)];
2585
2586
0
  BFD_ASSERT (size <= sizeof (tmp));
2587
2588
  /* If i is less than j, i is inserted before j.
2589
2590
     |---- j ----- i --------------|
2591
      \    / \      /
2592
   sorted   unsorted
2593
   */
2594
2595
0
  for (i = 1; i < (int) nmemb; i++)
2596
0
    {
2597
0
      for (j = (i - 1); j >= 0; j--)
2598
0
  if (compar (ptr + i * size, ptr + j * size) >= 0)
2599
0
    break;
2600
2601
0
      j++;
2602
2603
0
      if (i == j)
2604
0
  continue; /* i is in order.  */
2605
2606
0
      memcpy (tmp, ptr + i * size, size);
2607
0
      memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2608
0
      memcpy (ptr + j * size, tmp, size);
2609
0
    }
2610
0
}
2611
2612
/* Sort relocation by r_offset.
2613
2614
   We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2615
   algorithm.  Relocations at the same r_offset must keep their order.
2616
   For example, RELAX_ENTRY must be the very first relocation entry.
2617
2618
   Currently, this function implements insertion-sort.
2619
2620
   FIXME: If we already sort them in assembler, why bother sort them
2621
    here again?  */
2622
2623
static int
2624
compar_reloc (const void *lhs, const void *rhs)
2625
0
{
2626
0
  const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2627
0
  const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2628
2629
0
  if (l->r_offset > r->r_offset)
2630
0
    return 1;
2631
0
  else if (l->r_offset == r->r_offset)
2632
0
    return 0;
2633
0
  else
2634
0
    return -1;
2635
0
}
2636
2637
/* Functions listed below are only used for old relocs.
2638
     nds32_elf_9_pcrel_reloc
2639
     nds32_elf_do_9_pcrel_reloc
2640
     nds32_elf_hi20_reloc
2641
     nds32_elf_relocate_hi20
2642
     nds32_elf_lo12_reloc
2643
     nds32_elf_sda15_reloc
2644
     nds32_elf_generic_reloc.  */
2645
2646
/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
2647
2648
static bfd_reloc_status_type
2649
nds32_elf_9_pcrel_reloc (bfd *       abfd,
2650
       arelent *   reloc_entry,
2651
       asymbol *   symbol,
2652
       void *      data,
2653
       asection *  input_section,
2654
       bfd *       output_bfd,
2655
       char **     error_message ATTRIBUTE_UNUSED)
2656
3
{
2657
  /* This part is from bfd_elf_generic_reloc.  */
2658
3
  if (output_bfd != (bfd *) NULL
2659
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
2660
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2661
0
    {
2662
0
      reloc_entry->address += input_section->output_offset;
2663
0
      return bfd_reloc_ok;
2664
0
    }
2665
2666
3
  if (output_bfd != NULL)
2667
0
    {
2668
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
2669
0
      return bfd_reloc_continue;
2670
0
    }
2671
2672
3
  return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2673
3
             input_section,
2674
3
             data, reloc_entry->address,
2675
3
             symbol->section,
2676
3
             (symbol->value
2677
3
              + symbol->section->output_section->vma
2678
3
              + symbol->section->output_offset),
2679
3
             reloc_entry->addend);
2680
3
}
2681
2682
/* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
2683
0
#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2684
2685
static bfd_reloc_status_type
2686
nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
2687
          reloc_howto_type *  howto,
2688
          asection *          input_section,
2689
          bfd_byte *          data,
2690
          bfd_vma             offset,
2691
          asection *          symbol_section ATTRIBUTE_UNUSED,
2692
          bfd_vma             symbol_value,
2693
          bfd_vma             addend)
2694
3
{
2695
3
  bfd_vma relocation;
2696
3
  unsigned short x;
2697
3
  bfd_reloc_status_type status;
2698
2699
  /* Sanity check the address (offset in section).  */
2700
3
  bfd_vma octet = offset * OCTETS_PER_BYTE (abfd, input_section);
2701
3
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octet))
2702
1
    return bfd_reloc_outofrange;
2703
2704
2
  relocation = symbol_value + addend;
2705
  /* Make it pc relative.  */
2706
2
  relocation -= (input_section->output_section->vma
2707
2
     + input_section->output_offset);
2708
  /* These jumps mask off the lower two bits of the current address
2709
     before doing pcrel calculations.  */
2710
2
  relocation -= (offset & -(bfd_vma) 2);
2711
2712
2
  if (relocation + ACCURATE_8BIT_S1 >= 2 * ACCURATE_8BIT_S1)
2713
1
    status = bfd_reloc_overflow;
2714
1
  else
2715
1
    status = bfd_reloc_ok;
2716
2717
2
  x = bfd_getb16 (data + offset);
2718
2719
2
  relocation >>= howto->rightshift;
2720
2
  relocation <<= howto->bitpos;
2721
2
  x = (x & ~howto->dst_mask)
2722
2
      | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2723
2724
2
  bfd_putb16 ((bfd_vma) x, data + offset);
2725
2726
2
  return status;
2727
3
}
2728
2729
/* Handle the R_NDS32_HI20_[SU]LO relocs.
2730
   HI20_SLO is for the add3 and load/store with displacement instructions.
2731
   HI20 is for the or3 instruction.
2732
   For R_NDS32_HI20_SLO, the lower 12 bits are sign extended when added to
2733
   the high 20 bits so if the lower 12 bits are negative (bit 11 set) then
2734
   we must add one to the high 20 bits (which will get subtracted off when
2735
   the low 12 bits are added).
2736
   These relocs have to be done in combination with an R_NDS32_LO12 reloc
2737
   because there is a carry from the LO12 to the HI20.  Here we just save
2738
   the information we need; we do the actual relocation when we see the LO12.
2739
   This code is copied from the elf32-mips.c.  We also support an arbitrary
2740
   number of HI20 relocs to be associated with a single LO12 reloc.  The
2741
   assembler sorts the relocs to ensure each HI20 immediately precedes its
2742
   LO12.  However if there are multiple copies, the assembler may not find
2743
   the real LO12 so it picks the first one it finds.  */
2744
2745
struct nds32_hi20
2746
{
2747
  struct nds32_hi20 *next;
2748
  bfd_byte *addr;
2749
  bfd_vma addend;
2750
};
2751
2752
struct _nds32_elf_section_data
2753
{
2754
  struct bfd_elf_section_data elf;
2755
  struct nds32_hi20 *nds32_hi20_list;
2756
};
2757
2758
#define nds32_elf_section_data(sec) \
2759
10.1k
  ((struct _nds32_elf_section_data *) elf_section_data (sec))
2760
2761
static bool
2762
nds32_elf_new_section_hook (bfd *abfd, asection *sec)
2763
30.6k
{
2764
30.6k
  struct _nds32_elf_section_data *sdata;
2765
2766
30.6k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
2767
30.6k
  if (sdata == NULL)
2768
0
    return false;
2769
30.6k
  sec->used_by_bfd = sdata;
2770
2771
30.6k
  return _bfd_elf_new_section_hook (abfd, sec);
2772
30.6k
}
2773
2774
static void
2775
nds32_elf_free_hi20_list (asection *sec)
2776
10.1k
{
2777
10.1k
  struct _nds32_elf_section_data *sdata = nds32_elf_section_data (sec);
2778
10.1k
  while (sdata->nds32_hi20_list != NULL)
2779
2
    {
2780
2
      struct nds32_hi20 *hi = sdata->nds32_hi20_list;
2781
2
      sdata->nds32_hi20_list = hi->next;
2782
2
      free (hi);
2783
2
    }
2784
10.1k
}
2785
2786
static bool
2787
nds32_elf_free_cached_info (bfd *abfd)
2788
190k
{
2789
190k
  if (bfd_get_format (abfd) == bfd_object
2790
190k
      || bfd_get_format (abfd) == bfd_core)
2791
670
    {
2792
10.7k
      for (asection *sec = abfd->sections; sec; sec = sec->next)
2793
10.1k
  nds32_elf_free_hi20_list (sec);
2794
670
    }
2795
190k
  return _bfd_elf_free_cached_info (abfd);
2796
190k
}
2797
2798
static bfd_reloc_status_type
2799
nds32_elf_hi20_reloc (bfd *abfd,
2800
          arelent *reloc_entry,
2801
          asymbol *symbol,
2802
          void *data,
2803
          asection *input_section,
2804
          bfd *output_bfd,
2805
          char **error_message ATTRIBUTE_UNUSED)
2806
5
{
2807
5
  bfd_reloc_status_type ret;
2808
5
  bfd_vma relocation;
2809
5
  struct nds32_hi20 *n;
2810
5
  struct _nds32_elf_section_data *sdata;
2811
2812
  /* This part is from bfd_elf_generic_reloc.
2813
     If we're relocating, and this an external symbol, we don't want
2814
     to change anything.  */
2815
5
  if (output_bfd != (bfd *) NULL
2816
0
      && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2817
0
    {
2818
0
      reloc_entry->address += input_section->output_offset;
2819
0
      return bfd_reloc_ok;
2820
0
    }
2821
2822
  /* Sanity check the address (offset in section).  */
2823
5
  bfd_vma octet = (reloc_entry->address
2824
5
       * OCTETS_PER_BYTE (abfd, input_section));
2825
5
  if (!bfd_reloc_offset_in_range (reloc_entry->howto,
2826
5
          abfd, input_section, octet))
2827
3
    return bfd_reloc_outofrange;
2828
2829
2
  ret = bfd_reloc_ok;
2830
2
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2831
0
    ret = bfd_reloc_undefined;
2832
2833
2
  if (bfd_is_com_section (symbol->section))
2834
0
    relocation = 0;
2835
2
  else
2836
2
    relocation = symbol->value;
2837
2838
2
  relocation += symbol->section->output_section->vma;
2839
2
  relocation += symbol->section->output_offset;
2840
2
  relocation += reloc_entry->addend;
2841
2842
  /* Save the information, and let LO12 do the actual relocation.  */
2843
2
  n = bfd_malloc (sizeof (*n));
2844
2
  if (n == NULL)
2845
0
    return bfd_reloc_outofrange;
2846
2
  sdata = nds32_elf_section_data (input_section);
2847
2848
2
  n->addr = (bfd_byte *) data + reloc_entry->address;
2849
2
  n->addend = relocation;
2850
2
  n->next = sdata->nds32_hi20_list;
2851
2
  sdata->nds32_hi20_list = n;
2852
2853
2
  if (output_bfd != (bfd *) NULL)
2854
0
    reloc_entry->address += input_section->output_offset;
2855
2856
2
  return ret;
2857
2
}
2858
2859
/* Handle an NDS32 ELF HI20 reloc.  */
2860
2861
static void
2862
nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2863
       int type ATTRIBUTE_UNUSED,
2864
       Elf_Internal_Rela *relhi,
2865
       Elf_Internal_Rela *rello,
2866
       bfd_byte *contents,
2867
       bfd_vma addend)
2868
0
{
2869
0
  unsigned long insn;
2870
0
  bfd_vma addlo;
2871
2872
0
  insn = bfd_getb32 (contents + relhi->r_offset);
2873
2874
0
  addlo = bfd_getb32 (contents + rello->r_offset);
2875
0
  addlo &= 0xfff;
2876
2877
0
  addend += ((insn & 0xfffff) << 20) + addlo;
2878
2879
0
  insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2880
0
  bfd_putb32 (insn, contents + relhi->r_offset);
2881
0
}
2882
2883
/* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
2884
   inplace relocation; this function exists in order to do the
2885
   R_NDS32_HI20_[SU]LO relocation described above.  */
2886
2887
static bfd_reloc_status_type
2888
nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2889
          void *data, asection *input_section, bfd *output_bfd,
2890
          char **error_message)
2891
5
{
2892
5
  struct _nds32_elf_section_data *sdata;
2893
2894
  /* This part is from bfd_elf_generic_reloc.
2895
     If we're relocating, and this an external symbol, we don't want
2896
     to change anything.  */
2897
5
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2898
0
      && reloc_entry->addend == 0)
2899
0
    {
2900
0
      reloc_entry->address += input_section->output_offset;
2901
0
      return bfd_reloc_ok;
2902
0
    }
2903
2904
5
  bfd_vma octet = (reloc_entry->address
2905
5
       * OCTETS_PER_BYTE (input_bfd, input_section));
2906
5
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, input_bfd, input_section,
2907
5
          octet))
2908
2
    return bfd_reloc_outofrange;
2909
2910
3
  sdata = nds32_elf_section_data (input_section);
2911
3
  if (sdata->nds32_hi20_list != NULL)
2912
0
    {
2913
0
      struct nds32_hi20 *l;
2914
2915
0
      l = sdata->nds32_hi20_list;
2916
0
      while (l != NULL)
2917
0
  {
2918
0
    unsigned long insn;
2919
0
    unsigned long val;
2920
0
    unsigned long vallo;
2921
0
    struct nds32_hi20 *next;
2922
2923
    /* Do the HI20 relocation.  Note that we actually don't need
2924
       to know anything about the LO12 itself, except where to
2925
       find the low 12 bits of the addend needed by the LO12.  */
2926
0
    insn = bfd_getb32 (l->addr);
2927
0
    vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2928
0
    vallo &= 0xfff;
2929
0
    switch (reloc_entry->howto->type)
2930
0
      {
2931
0
      case R_NDS32_LO12S3:
2932
0
        vallo <<= 3;
2933
0
        break;
2934
2935
0
      case R_NDS32_LO12S2:
2936
0
        vallo <<= 2;
2937
0
        break;
2938
2939
0
      case R_NDS32_LO12S1:
2940
0
        vallo <<= 1;
2941
0
        break;
2942
2943
0
      case R_NDS32_LO12S0:
2944
0
        vallo <<= 0;
2945
0
        break;
2946
0
      }
2947
2948
0
    val = ((insn & 0xfffff) << 12) + vallo;
2949
0
    val += l->addend;
2950
2951
0
    insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2952
0
    bfd_putb32 ((bfd_vma) insn, l->addr);
2953
2954
0
    next = l->next;
2955
0
    free (l);
2956
0
    l = next;
2957
0
  }
2958
2959
0
      sdata->nds32_hi20_list = NULL;
2960
0
    }
2961
2962
  /* Now do the LO12 reloc in the usual way.
2963
     ??? It would be nice to call bfd_elf_generic_reloc here,
2964
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2965
     pass the handling back to bfd_install_relocation which will install
2966
     a section relative addend which is wrong.  */
2967
3
  return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2968
3
          input_section, output_bfd, error_message);
2969
3
}
2970
2971
/* Do generic partial_inplace relocation.
2972
   This is a local replacement for bfd_elf_generic_reloc.  */
2973
2974
static bfd_reloc_status_type
2975
nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2976
       asymbol *symbol, void *data, asection *input_section,
2977
       bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2978
9
{
2979
9
  bfd_reloc_status_type ret;
2980
9
  bfd_vma relocation;
2981
9
  bfd_byte *inplace_address;
2982
2983
  /* This part is from bfd_elf_generic_reloc.
2984
     If we're relocating, and this an external symbol, we don't want
2985
     to change anything.  */
2986
9
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2987
0
      && reloc_entry->addend == 0)
2988
0
    {
2989
0
      reloc_entry->address += input_section->output_offset;
2990
0
      return bfd_reloc_ok;
2991
0
    }
2992
2993
  /* Now do the reloc in the usual way.
2994
     ??? It would be nice to call bfd_elf_generic_reloc here,
2995
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2996
     pass the handling back to bfd_install_relocation which will install
2997
     a section relative addend which is wrong.  */
2998
2999
  /* Sanity check the address (offset in section).  */
3000
9
  bfd_vma octet = (reloc_entry->address
3001
9
       * OCTETS_PER_BYTE (input_bfd, input_section));
3002
9
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, input_bfd, input_section,
3003
9
          octet))
3004
1
    return bfd_reloc_outofrange;
3005
3006
8
  ret = bfd_reloc_ok;
3007
8
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
3008
0
    ret = bfd_reloc_undefined;
3009
3010
8
  if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
3011
0
    relocation = 0;
3012
8
  else
3013
8
    relocation = symbol->value;
3014
3015
  /* Only do this for a final link.  */
3016
8
  if (output_bfd == (bfd *) NULL)
3017
8
    {
3018
8
      relocation += symbol->section->output_section->vma;
3019
8
      relocation += symbol->section->output_offset;
3020
8
    }
3021
3022
8
  relocation += reloc_entry->addend;
3023
8
  switch (reloc_entry->howto->type)
3024
8
    {
3025
1
    case R_NDS32_LO12S3:
3026
1
      relocation >>= 3;
3027
1
      break;
3028
3029
0
    case R_NDS32_LO12S2:
3030
0
      relocation >>= 2;
3031
0
      break;
3032
3033
0
    case R_NDS32_LO12S1:
3034
0
      relocation >>= 1;
3035
0
      break;
3036
3037
2
    case R_NDS32_LO12S0:
3038
7
    default:
3039
7
      relocation >>= 0;
3040
7
      break;
3041
8
    }
3042
3043
8
  inplace_address = (bfd_byte *) data + reloc_entry->address;
3044
3045
8
#define DOIT(x)           \
3046
8
  x = ((x & ~reloc_entry->howto->dst_mask) |    \
3047
8
  (((x & reloc_entry->howto->src_mask) +  relocation) & \
3048
8
  reloc_entry->howto->dst_mask))
3049
3050
8
  switch (bfd_get_reloc_size (reloc_entry->howto))
3051
8
    {
3052
3
    case 2:
3053
3
      {
3054
3
  short x = bfd_getb16 (inplace_address);
3055
3056
3
  DOIT (x);
3057
3
  bfd_putb16 ((bfd_vma) x, inplace_address);
3058
3
      }
3059
3
      break;
3060
5
    case 4:
3061
5
      {
3062
5
  unsigned long x = bfd_getb32 (inplace_address);
3063
3064
5
  DOIT (x);
3065
5
  bfd_putb32 ((bfd_vma) x, inplace_address);
3066
5
      }
3067
5
      break;
3068
0
    default:
3069
0
      BFD_ASSERT (0);
3070
8
    }
3071
3072
8
  if (output_bfd != (bfd *) NULL)
3073
0
    reloc_entry->address += input_section->output_offset;
3074
3075
8
  return ret;
3076
8
}
3077
3078
/* Handle the R_NDS32_SDA15 reloc.
3079
   This reloc is used to compute the address of objects in the small data area
3080
   and to perform loads and stores from that area.
3081
   The lower 15 bits are sign extended and added to the register specified
3082
   in the instruction, which is assumed to point to _SDA_BASE_.
3083
3084
   Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
3085
   the access size, this must be taken care of.  */
3086
3087
static bfd_reloc_status_type
3088
nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3089
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
3090
           asection *input_section, bfd *output_bfd,
3091
           char **error_message ATTRIBUTE_UNUSED)
3092
0
{
3093
  /* This part is from bfd_elf_generic_reloc.  */
3094
0
  if (output_bfd != (bfd *) NULL
3095
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3096
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3097
0
    {
3098
0
      reloc_entry->address += input_section->output_offset;
3099
0
      return bfd_reloc_ok;
3100
0
    }
3101
3102
0
  if (output_bfd != NULL)
3103
0
    {
3104
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
3105
0
      return bfd_reloc_continue;
3106
0
    }
3107
3108
  /* FIXME: not sure what to do here yet.  But then again, the linker
3109
     may never call us.  */
3110
0
  abort ();
3111
0
}
3112
3113
/* nds32_elf_ignore_reloc is the special function for
3114
   relocation types which don't need to be relocated
3115
   like relaxation relocation types.
3116
   This function simply return bfd_reloc_ok when it is
3117
   invoked.  */
3118
3119
static bfd_reloc_status_type
3120
nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3121
      asymbol *symbol ATTRIBUTE_UNUSED,
3122
      void *data ATTRIBUTE_UNUSED, asection *input_section,
3123
      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3124
5
{
3125
5
  if (output_bfd != NULL)
3126
0
    reloc_entry->address += input_section->output_offset;
3127
3128
5
  return bfd_reloc_ok;
3129
5
}
3130

3131
3132
/* Map BFD reloc types to NDS32 ELF reloc types.  */
3133
3134
struct nds32_reloc_map_entry
3135
{
3136
  bfd_reloc_code_real_type bfd_reloc_val;
3137
  unsigned char elf_reloc_val;
3138
};
3139
3140
static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3141
{
3142
  {BFD_RELOC_NONE, R_NDS32_NONE},
3143
  {BFD_RELOC_16, R_NDS32_16_RELA},
3144
  {BFD_RELOC_32, R_NDS32_32_RELA},
3145
  {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3146
  {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3147
3148
  {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3149
  {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3150
  {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3151
  {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3152
  {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3153
  {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3154
  {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3155
  {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3156
  {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3157
  {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3158
  {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3159
  {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3160
  {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3161
  {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3162
  {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3163
  {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3164
  {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3165
  {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3166
  {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3167
  {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3168
  {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3169
  {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3170
  {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3171
  {BFD_RELOC_COPY, R_NDS32_COPY},
3172
  {BFD_RELOC_GLOB_DAT, R_NDS32_GLOB_DAT},
3173
  {BFD_RELOC_JMP_SLOT, R_NDS32_JMP_SLOT},
3174
  {BFD_RELOC_RELATIVE, R_NDS32_RELATIVE},
3175
  {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3176
  {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3177
  {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3178
  {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3179
  {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3180
  {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3181
  {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3182
  {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3183
  {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3184
  {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3185
  {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3186
  {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3187
  {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3188
  {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3189
  {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3190
  {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3191
  {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3192
  {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3193
  {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3194
  {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3195
  {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3196
  {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3197
  {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3198
  {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3199
  {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3200
  {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3201
  {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3202
  {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3203
  {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3204
  {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3205
  {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3206
  {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3207
  {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3208
  {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3209
  {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3210
  {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3211
  {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3212
  {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3213
  {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3214
  {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3215
  {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3216
  {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3217
  {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3218
  {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3219
  {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3220
  {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3221
  {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3222
  {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3223
  {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3224
  {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3225
  {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3226
  {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3227
  {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3228
  {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3229
  {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3230
  {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3231
  {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3232
  {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3233
  {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3234
  {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3235
  {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3236
  {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3237
  {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3238
  {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3239
  {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3240
  {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3241
  {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3242
  {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3243
  {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3244
  {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3245
  {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3246
  {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3247
  {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3248
  {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3249
  {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3250
  /* Not sure.  */
3251
  {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3252
  /* Missing: BFD_RELOC_NDS32_GOTTPOFF.  */
3253
  {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3254
  {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3255
  {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3256
  {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3257
  {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3258
  {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3259
  {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3260
  {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3261
  {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3262
  {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3263
  {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3264
  {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3265
  {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3266
  {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3267
  {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3268
  {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3269
  {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3270
  {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3271
  {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3272
  {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3273
  {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3274
  {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3275
  {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3276
  {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3277
  {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3278
  {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3279
  {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3280
};
3281
3282
/* Patch tag.  */
3283
3284
static reloc_howto_type *
3285
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3286
         const char *r_name)
3287
0
{
3288
0
  unsigned int i;
3289
3290
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3291
0
    if (nds32_elf_howto_table[i].name != NULL
3292
0
  && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3293
0
      return &nds32_elf_howto_table[i];
3294
3295
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3296
0
    if (nds32_elf_relax_howto_table[i].name != NULL
3297
0
  && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3298
0
      return &nds32_elf_relax_howto_table[i];
3299
3300
0
  return NULL;
3301
0
}
3302
3303
static reloc_howto_type *
3304
bfd_elf32_bfd_reloc_type_table_lookup (unsigned int code)
3305
1.54k
{
3306
1.54k
  if (code < R_NDS32_RELAX_ENTRY)
3307
1.45k
    {
3308
1.45k
      if (code < ARRAY_SIZE (nds32_elf_howto_table))
3309
1.44k
  return &nds32_elf_howto_table[code];
3310
1.45k
    }
3311
95
  else
3312
95
    {
3313
95
      if (code - R_NDS32_RELAX_ENTRY < ARRAY_SIZE (nds32_elf_relax_howto_table))
3314
88
  return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3315
95
    }
3316
13
  return NULL;
3317
1.54k
}
3318
3319
static reloc_howto_type *
3320
bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3321
         bfd_reloc_code_real_type code)
3322
0
{
3323
0
  unsigned int i;
3324
3325
0
  for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3326
0
    {
3327
0
      if (nds32_reloc_map[i].bfd_reloc_val == code)
3328
0
  return bfd_elf32_bfd_reloc_type_table_lookup
3329
0
    (nds32_reloc_map[i].elf_reloc_val);
3330
0
    }
3331
3332
0
  return NULL;
3333
0
}
3334
3335
/* Set the howto pointer for an NDS32 ELF reloc.  */
3336
3337
static bool
3338
nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3339
       Elf_Internal_Rela *dst)
3340
116
{
3341
116
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3342
3343
116
  cache_ptr->howto = NULL;
3344
116
  if (r_type <= R_NDS32_GNU_VTENTRY)
3345
108
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3346
116
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3347
8
    {
3348
      /* xgettext:c-format */
3349
8
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3350
8
        abfd, r_type);
3351
8
      bfd_set_error (bfd_error_bad_value);
3352
8
      return false;
3353
8
    }
3354
108
  return true;
3355
116
}
3356
3357
static bool
3358
nds32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3359
         Elf_Internal_Rela *dst)
3360
1.46k
{
3361
1.46k
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3362
3363
1.46k
  cache_ptr->howto = NULL;
3364
1.46k
  if (r_type == R_NDS32_NONE
3365
343
      || r_type > R_NDS32_GNU_VTENTRY)
3366
1.44k
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3367
1.46k
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3368
40
    {
3369
      /* xgettext:c-format */
3370
40
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3371
40
        abfd, r_type);
3372
40
      bfd_set_error (bfd_error_bad_value);
3373
40
      return false;
3374
40
    }
3375
1.42k
  return true;
3376
1.46k
}
3377
3378
/* Support for core dump NOTE sections.
3379
   Reference to include/linux/elfcore.h in Linux.  */
3380
3381
static bool
3382
nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3383
0
{
3384
0
  int offset;
3385
0
  size_t size;
3386
3387
0
  switch (note->descsz)
3388
0
    {
3389
0
    case 0x114:
3390
      /* Linux/NDS32 32-bit, ABI1.  */
3391
3392
      /* pr_cursig */
3393
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3394
3395
      /* pr_pid */
3396
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3397
3398
      /* pr_reg */
3399
0
      offset = 72;
3400
0
      size = 200;
3401
0
      break;
3402
3403
0
    case 0xfc:
3404
      /* Linux/NDS32 32-bit.  */
3405
3406
      /* pr_cursig */
3407
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3408
3409
      /* pr_pid */
3410
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3411
3412
      /* pr_reg */
3413
0
      offset = 72;
3414
0
      size = 176;
3415
0
      break;
3416
3417
0
    default:
3418
0
      return false;
3419
0
    }
3420
3421
  /* Make a ".reg" section.  */
3422
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3423
0
            size, note->descpos + offset);
3424
0
}
3425
3426
static bool
3427
nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3428
0
{
3429
0
  switch (note->descsz)
3430
0
    {
3431
0
    case 124:
3432
      /* Linux/NDS32.  */
3433
3434
      /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
3435
0
      elf_tdata (abfd)->core->program =
3436
0
  _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3437
0
      elf_tdata (abfd)->core->command =
3438
0
  _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3439
0
      break;
3440
3441
0
    default:
3442
0
      return false;
3443
0
    }
3444
3445
  /* Note that for some reason, a spurious space is tacked
3446
     onto the end of the args in some (at least one anyway)
3447
     implementations, so strip it off if it exists.  */
3448
0
  {
3449
0
    char *command = elf_tdata (abfd)->core->command;
3450
0
    int n = strlen (command);
3451
3452
0
    if (0 < n && command[n - 1] == ' ')
3453
0
      command[n - 1] = '\0';
3454
0
  }
3455
3456
0
  return true;
3457
0
}
3458
3459
/* Hook called by the linker routine which adds symbols from an object
3460
   file.  We must handle the special NDS32 section numbers here.
3461
   We also keep watching for whether we need to create the sdata special
3462
   linker sections.  */
3463
3464
static bool
3465
nds32_elf_add_symbol_hook (bfd *abfd,
3466
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
3467
         Elf_Internal_Sym *sym,
3468
         const char **namep ATTRIBUTE_UNUSED,
3469
         flagword *flagsp ATTRIBUTE_UNUSED,
3470
         asection **secp, bfd_vma *valp)
3471
0
{
3472
0
  switch (sym->st_shndx)
3473
0
    {
3474
0
    case SHN_COMMON:
3475
      /* Common symbols less than the GP size are automatically
3476
   treated as SHN_MIPS_SCOMMON symbols.  */
3477
0
      if (sym->st_size > elf_gp_size (abfd)
3478
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3479
0
  break;
3480
3481
      /* st_value is the alignment constraint.
3482
   That might be its actual size if it is an array or structure.  */
3483
0
      switch (sym->st_value)
3484
0
  {
3485
0
  case 1:
3486
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3487
0
    break;
3488
0
  case 2:
3489
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3490
0
    break;
3491
0
  case 4:
3492
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3493
0
    break;
3494
0
  case 8:
3495
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3496
0
    break;
3497
0
  default:
3498
0
    return true;
3499
0
  }
3500
3501
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
3502
0
      *valp = sym->st_size;
3503
0
      break;
3504
0
    }
3505
3506
0
  return true;
3507
0
}
3508
3509
/* This function can figure out the best location for a base register to access
3510
   data relative to this base register
3511
   INPUT:
3512
   sda_d0: size of first DOUBLE WORD data section
3513
   sda_w0: size of first WORD data section
3514
   sda_h0: size of first HALF WORD data section
3515
   sda_b : size of BYTE data section
3516
   sda_hi: size of second HALF WORD data section
3517
   sda_w1: size of second WORD data section
3518
   sda_d1: size of second DOUBLE WORD data section
3519
   OUTPUT:
3520
   offset (always positive) from the beginning of sda_d0 if OK
3521
   a negative error value if fail
3522
   NOTE:
3523
   these 7 sections have to be located back to back if exist
3524
   a pass in 0 value for non-existing section   */
3525
3526
/* Due to the interpretation of simm15 field of load/store depending on
3527
   data accessing size, the organization of base register relative data shall
3528
   like the following figure
3529
   -------------------------------------------
3530
   |  DOUBLE WORD sized data (range +/- 128K)
3531
   -------------------------------------------
3532
   |  WORD sized data (range +/- 64K)
3533
   -------------------------------------------
3534
   |  HALF WORD sized data (range +/- 32K)
3535
   -------------------------------------------
3536
   |  BYTE sized data (range +/- 16K)
3537
   -------------------------------------------
3538
   |  HALF WORD sized data (range +/- 32K)
3539
   -------------------------------------------
3540
   |  WORD sized data (range +/- 64K)
3541
   -------------------------------------------
3542
   |  DOUBLE WORD sized data (range +/- 128K)
3543
   -------------------------------------------
3544
   Its base register shall be set to access these data freely.  */
3545
3546
/* We have to figure out the SDA_BASE value, so that we can adjust the
3547
   symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3548
   BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3549
   target data.  We don't need to adjust the symbol value for an
3550
   external symbol if we are producing relocatable output.  */
3551
3552
static asection *sda_rela_sec = NULL;
3553
3554
#define SDA_SECTION_NUM 10
3555
3556
static bfd_reloc_status_type
3557
nds32_elf_final_sda_base (bfd *output_bfd,
3558
        struct bfd_link_info *info,
3559
        bfd_vma *psb,
3560
        bool add_symbol)
3561
0
{
3562
0
  int relax_fp_as_gp;
3563
0
  struct elf_nds32_link_hash_table *table;
3564
0
  struct bfd_link_hash_entry *h, *h2;
3565
0
  long unsigned int total = 0;
3566
0
  asection *first = NULL, *final = NULL, *temp;
3567
0
  bfd_vma sda_base = 0;
3568
3569
0
  h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
3570
0
  if (!h || (h->type != bfd_link_hash_defined
3571
0
       && h->type != bfd_link_hash_defweak))
3572
0
    {
3573
      /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3574
   4 byte-aligned.  Therefore, it has to set the first section ".data"
3575
   4 byte-aligned.  */
3576
0
      static const char sec_name[SDA_SECTION_NUM][10] =
3577
0
  {
3578
0
    ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3579
0
    ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3580
0
  };
3581
0
      size_t i = 0;
3582
3583
0
      if (output_bfd->sections == NULL)
3584
0
  {
3585
0
    *psb = elf_gp (output_bfd);
3586
0
    return bfd_reloc_ok;
3587
0
  }
3588
3589
      /* Get the first and final section.  */
3590
0
      while (i < ARRAY_SIZE (sec_name))
3591
0
  {
3592
0
    temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3593
0
    if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3594
0
      first = temp;
3595
0
    if (temp && (temp->size != 0 || temp->rawsize != 0))
3596
0
      final = temp;
3597
3598
    /* Summarize the sections in order to check if joining .bss.  */
3599
0
    if (temp && temp->size != 0)
3600
0
      total += temp->size;
3601
0
    else if (temp && temp->rawsize != 0)
3602
0
      total += temp->rawsize;
3603
3604
0
    i++;
3605
0
  }
3606
3607
      /* Check .bss size.  */
3608
0
      temp = bfd_get_section_by_name (output_bfd, ".bss");
3609
0
      if (temp)
3610
0
  {
3611
0
    if (temp->size != 0)
3612
0
      total += temp->size;
3613
0
    else if (temp->rawsize != 0)
3614
0
      total += temp->rawsize;
3615
3616
0
    if (total < 0x80000)
3617
0
      {
3618
0
        if (!first && (temp->size != 0 || temp->rawsize != 0))
3619
0
    first = temp;
3620
0
        if ((temp->size != 0 || temp->rawsize != 0))
3621
0
    final = temp;
3622
0
      }
3623
0
  }
3624
3625
0
      if (first && final)
3626
0
  {
3627
    /* The middle of data region.  */
3628
0
    sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3629
3630
    /* Find the section sda_base located.  */
3631
0
    i = 0;
3632
0
    while (i < ARRAY_SIZE (sec_name))
3633
0
      {
3634
0
        final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3635
0
        if (final && (final->size != 0 || final->rawsize != 0)
3636
0
      && sda_base >= final->vma)
3637
0
    {
3638
0
      first = final;
3639
0
      i++;
3640
0
    }
3641
0
        else
3642
0
    break;
3643
0
      }
3644
0
  }
3645
0
      else
3646
0
  {
3647
    /* If there is not any default data section in output bfd, try to find
3648
       the first data section.  If no data section be found, just simplily
3649
       choose the first output section.  */
3650
0
    temp = output_bfd->sections;
3651
0
    while (temp)
3652
0
      {
3653
0
        if (temp->flags & SEC_ALLOC
3654
0
      && (((temp->flags & SEC_DATA)
3655
0
           && ((temp->flags & SEC_READONLY) == 0))
3656
0
          || (temp->flags & SEC_LOAD) == 0)
3657
0
      && (temp->size != 0 || temp->rawsize != 0))
3658
0
    {
3659
0
      if (!first)
3660
0
        first = temp;
3661
0
      final = temp;
3662
0
    }
3663
0
        temp = temp->next;
3664
0
      }
3665
3666
    /* There is no data or bss section.  */
3667
0
    if (!first || (first->size == 0 && first->rawsize == 0))
3668
0
      {
3669
0
        first = output_bfd->sections;
3670
0
        while (first && first->size == 0 && first->rawsize == 0)
3671
0
    first = first->next;
3672
0
      }
3673
3674
    /* There is no concrete section.  */
3675
0
    if (!first)
3676
0
      {
3677
0
        *psb = elf_gp (output_bfd);
3678
0
        return bfd_reloc_ok;
3679
0
      }
3680
3681
0
    if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3682
0
      sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3683
0
    else
3684
0
      sda_base = first->vma + 0x2000;
3685
0
  }
3686
3687
0
      sda_base -= first->vma;
3688
0
      sda_base = sda_base & (~7);
3689
3690
0
      if (!_bfd_generic_link_add_one_symbol
3691
0
    (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3692
0
     (bfd_vma) sda_base, (const char *) NULL, false,
3693
0
     get_elf_backend_data (output_bfd)->collect, &h))
3694
0
  return false;
3695
3696
0
      sda_rela_sec = first;
3697
0
    }
3698
3699
  /* Set _FP_BASE_ to _SDA_BASE_.  */
3700
0
  table = nds32_elf_hash_table (info);
3701
0
  relax_fp_as_gp = table->relax_fp_as_gp;
3702
0
  h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, false, false, false);
3703
  /* _SDA_BASE_ is difined in linker script.  */
3704
0
  if (!first)
3705
0
    {
3706
0
      first = h->u.def.section;
3707
0
      sda_base = h->u.def.value;
3708
0
    }
3709
3710
0
  if (relax_fp_as_gp && h2
3711
0
      && (h2->type == bfd_link_hash_undefweak
3712
0
    || h2->type == bfd_link_hash_undefined))
3713
0
    {
3714
      /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3715
   And set FP equal to SDA_BASE to do relaxation for
3716
   la $fp, _FP_BASE_.  */
3717
0
      if (!_bfd_generic_link_add_one_symbol
3718
0
    (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3719
0
     first, sda_base, (const char *) NULL,
3720
0
     false, get_elf_backend_data (output_bfd)->collect, &h2))
3721
0
  return false;
3722
0
    }
3723
3724
0
  if (add_symbol)
3725
0
    {
3726
0
      if (h)
3727
0
  {
3728
    /* Now set gp.  */
3729
0
    elf_gp (output_bfd) = (h->u.def.value
3730
0
         + h->u.def.section->output_section->vma
3731
0
         + h->u.def.section->output_offset);
3732
0
  }
3733
0
      else
3734
0
  {
3735
0
    _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3736
0
    return bfd_reloc_dangerous;
3737
0
  }
3738
0
    }
3739
3740
0
  *psb = h->u.def.value
3741
0
    + h->u.def.section->output_section->vma
3742
0
    + h->u.def.section->output_offset;
3743
0
  return bfd_reloc_ok;
3744
0
}
3745

3746
3747
/* Return size of a PLT entry.  */
3748
#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3749
3750
/* Create an entry in an nds32 ELF linker hash table.  */
3751
3752
static struct bfd_hash_entry *
3753
nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3754
           struct bfd_hash_table *table,
3755
           const char *string)
3756
0
{
3757
0
  struct elf_nds32_link_hash_entry *ret;
3758
3759
0
  ret = (struct elf_nds32_link_hash_entry *) entry;
3760
3761
  /* Allocate the structure if it has not already been allocated by a
3762
     subclass.  */
3763
0
  if (ret == NULL)
3764
0
    ret = (struct elf_nds32_link_hash_entry *)
3765
0
       bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3766
3767
0
  if (ret == NULL)
3768
0
    return (struct bfd_hash_entry *) ret;
3769
3770
  /* Call the allocation method of the superclass.  */
3771
0
  ret = (struct elf_nds32_link_hash_entry *)
3772
0
    _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3773
3774
0
  if (ret != NULL)
3775
0
    {
3776
0
      struct elf_nds32_link_hash_entry *eh;
3777
3778
0
      eh = (struct elf_nds32_link_hash_entry *) ret;
3779
0
      eh->tls_type = GOT_UNKNOWN;
3780
0
      eh->offset_to_gp = 0;
3781
0
    }
3782
3783
0
  return (struct bfd_hash_entry *) ret;
3784
0
}
3785
3786
/* Create an nds32 ELF linker hash table.  */
3787
3788
static struct bfd_link_hash_table *
3789
nds32_elf_link_hash_table_create (bfd *abfd)
3790
0
{
3791
0
  struct elf_nds32_link_hash_table *ret;
3792
3793
0
  size_t amt = sizeof (struct elf_nds32_link_hash_table);
3794
3795
0
  ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3796
0
  if (ret == NULL)
3797
0
    return NULL;
3798
3799
  /* Patch tag.  */
3800
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3801
0
              nds32_elf_link_hash_newfunc,
3802
0
              sizeof (struct elf_nds32_link_hash_entry)))
3803
0
    {
3804
0
      free (ret);
3805
0
      return NULL;
3806
0
    }
3807
3808
0
  ret->sym_ld_script = NULL;
3809
3810
0
  return &ret->root.root;
3811
0
}
3812
3813
/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3814
   shortcuts to them in our hash table.  */
3815
3816
static bool
3817
create_got_section (bfd *dynobj, struct bfd_link_info *info)
3818
0
{
3819
0
  struct elf_link_hash_table *ehtab;
3820
3821
0
  if (!_bfd_elf_create_got_section (dynobj, info))
3822
0
    return false;
3823
3824
0
  ehtab = elf_hash_table (info);
3825
0
  ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3826
0
  ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3827
0
  if (!ehtab->sgot || !ehtab->sgotplt)
3828
0
    abort ();
3829
3830
  /* _bfd_elf_create_got_section will create it for us.  */
3831
0
  ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3832
0
  if (ehtab->srelgot == NULL
3833
0
      || !bfd_set_section_flags (ehtab->srelgot,
3834
0
         (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3835
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED
3836
0
          | SEC_READONLY))
3837
0
      || !bfd_set_section_alignment (ehtab->srelgot, 2))
3838
0
    return false;
3839
3840
0
  return true;
3841
0
}
3842
3843
/* Create dynamic sections when linking against a dynamic object.  */
3844
3845
static bool
3846
nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3847
0
{
3848
0
  struct elf_link_hash_table *ehtab;
3849
0
  struct elf_nds32_link_hash_table *htab;
3850
0
  flagword flags, pltflags;
3851
0
  register asection *s;
3852
0
  elf_backend_data *bed;
3853
0
  int ptralign = 2;   /* 32-bit  */
3854
0
  const char *secname;
3855
0
  char *relname;
3856
0
  flagword secflags;
3857
0
  asection *sec;
3858
3859
0
  bed = get_elf_backend_data (abfd);
3860
0
  ehtab = elf_hash_table (info);
3861
0
  htab = nds32_elf_hash_table (info);
3862
3863
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3864
     .rel[a].bss sections.  */
3865
3866
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3867
0
     | SEC_LINKER_CREATED);
3868
3869
0
  pltflags = flags;
3870
0
  pltflags |= SEC_CODE;
3871
0
  if (bed->plt_not_loaded)
3872
0
    pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3873
0
  if (bed->plt_readonly)
3874
0
    pltflags |= SEC_READONLY;
3875
3876
0
  s = bfd_make_section (abfd, ".plt");
3877
0
  ehtab->splt = s;
3878
0
  if (s == NULL
3879
0
      || !bfd_set_section_flags (s, pltflags)
3880
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
3881
0
    return false;
3882
3883
0
  if (bed->want_plt_sym)
3884
0
    {
3885
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3886
   .plt section.  */
3887
0
      struct bfd_link_hash_entry *bh = NULL;
3888
0
      struct elf_link_hash_entry *h;
3889
3890
0
      if (!(_bfd_generic_link_add_one_symbol
3891
0
      (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3892
0
       (bfd_vma) 0, (const char *) NULL, false,
3893
0
       get_elf_backend_data (abfd)->collect, &bh)))
3894
0
  return false;
3895
3896
0
      h = (struct elf_link_hash_entry *) bh;
3897
0
      h->def_regular = 1;
3898
0
      h->type = STT_OBJECT;
3899
3900
0
      if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3901
0
  return false;
3902
0
    }
3903
3904
0
  s = bfd_make_section (abfd,
3905
0
      bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3906
0
  ehtab->srelplt = s;
3907
0
  if (s == NULL
3908
0
      || !bfd_set_section_flags (s, flags | SEC_READONLY)
3909
0
      || !bfd_set_section_alignment (s, ptralign))
3910
0
    return false;
3911
3912
0
  if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3913
0
    return false;
3914
3915
0
  for (sec = abfd->sections; sec; sec = sec->next)
3916
0
    {
3917
0
      secflags = bfd_section_flags (sec);
3918
0
      if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3919
0
    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3920
0
  continue;
3921
0
      secname = bfd_section_name (sec);
3922
0
      relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3923
0
      strcpy (relname, ".rela");
3924
0
      strcat (relname, secname);
3925
0
      if (bfd_get_section_by_name (abfd, secname))
3926
0
  continue;
3927
0
      s = bfd_make_section (abfd, relname);
3928
0
      if (s == NULL
3929
0
    || !bfd_set_section_flags (s, flags | SEC_READONLY)
3930
0
    || !bfd_set_section_alignment (s, ptralign))
3931
0
  return false;
3932
0
    }
3933
3934
0
  if (bed->want_dynbss)
3935
0
    {
3936
      /* The .dynbss section is a place to put symbols which are defined
3937
   by dynamic objects, are referenced by regular objects, and are
3938
   not functions.  We must allocate space for them in the process
3939
   image and use a R_*_COPY reloc to tell the dynamic linker to
3940
   initialize them at run time.  The linker script puts the .dynbss
3941
   section into the .bss section of the final image.  */
3942
0
      s = bfd_make_section (abfd, ".dynbss");
3943
0
      htab->root.sdynbss = s;
3944
0
      if (s == NULL
3945
0
    || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3946
0
  return false;
3947
      /* The .rel[a].bss section holds copy relocs.  This section is not
3948
   normally needed.  We need to create it here, though, so that the
3949
   linker will map it to an output section.  We can't just create it
3950
   only if we need it, because we will not know whether we need it
3951
   until we have seen all the input files, and the first time the
3952
   main linker code calls BFD after examining all the input files
3953
   (size_dynamic_sections) the input sections have already been
3954
   mapped to the output sections.  If the section turns out not to
3955
   be needed, we can discard it later.  We will never need this
3956
   section when generating a shared object, since they do not use
3957
   copy relocs.  */
3958
0
      if (!bfd_link_pic (info))
3959
0
  {
3960
0
    s = bfd_make_section (abfd, (bed->default_use_rela_p
3961
0
               ? ".rela.bss" : ".rel.bss"));
3962
0
    htab->root.srelbss = s;
3963
0
    if (s == NULL
3964
0
        || !bfd_set_section_flags (s, flags | SEC_READONLY)
3965
0
        || !bfd_set_section_alignment (s, ptralign))
3966
0
      return false;
3967
0
  }
3968
0
    }
3969
3970
0
  return true;
3971
0
}
3972
3973
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3974
static void
3975
nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3976
        struct elf_link_hash_entry *dir,
3977
        struct elf_link_hash_entry *ind)
3978
0
{
3979
0
  struct elf_nds32_link_hash_entry *edir, *eind;
3980
3981
0
  edir = (struct elf_nds32_link_hash_entry *) dir;
3982
0
  eind = (struct elf_nds32_link_hash_entry *) ind;
3983
3984
0
  if (ind->root.type == bfd_link_hash_indirect)
3985
0
    {
3986
0
      if (dir->got.refcount <= 0)
3987
0
  {
3988
0
    edir->tls_type = eind->tls_type;
3989
0
    eind->tls_type = GOT_UNKNOWN;
3990
0
  }
3991
0
    }
3992
3993
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3994
0
}
3995

3996
/* Adjust a symbol defined by a dynamic object and referenced by a
3997
   regular object.  The current definition is in some section of the
3998
   dynamic object, but we're not including those sections.  We have to
3999
   change the definition to something the rest of the link can
4000
   understand.  */
4001
4002
static bool
4003
nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4004
         struct elf_link_hash_entry *h)
4005
0
{
4006
0
  struct elf_nds32_link_hash_table *htab;
4007
0
  bfd *dynobj;
4008
0
  asection *s;
4009
0
  unsigned int power_of_two;
4010
4011
0
  dynobj = elf_hash_table (info)->dynobj;
4012
4013
  /* Make sure we know what is going on here.  */
4014
0
  BFD_ASSERT (dynobj != NULL
4015
0
        && (h->needs_plt
4016
0
      || h->is_weakalias
4017
0
      || (h->def_dynamic && h->ref_regular && !h->def_regular)));
4018
4019
4020
  /* If this is a function, put it in the procedure linkage table.  We
4021
     will fill in the contents of the procedure linkage table later,
4022
     when we know the address of the .got section.  */
4023
0
  if (h->type == STT_FUNC || h->needs_plt)
4024
0
    {
4025
0
      if (!bfd_link_pic (info)
4026
0
    && !h->def_dynamic
4027
0
    && !h->ref_dynamic
4028
0
    && h->root.type != bfd_link_hash_undefweak
4029
0
    && h->root.type != bfd_link_hash_undefined)
4030
0
  {
4031
    /* This case can occur if we saw a PLT reloc in an input
4032
       file, but the symbol was never referred to by a dynamic
4033
       object.  In such a case, we don't actually need to build
4034
       a procedure linkage table, and we can just do a PCREL
4035
       reloc instead.  */
4036
0
    h->plt.offset = (bfd_vma) - 1;
4037
0
    h->needs_plt = 0;
4038
0
  }
4039
4040
0
      return true;
4041
0
    }
4042
0
  else
4043
0
    h->plt.offset = (bfd_vma) - 1;
4044
4045
  /* If this is a weak symbol, and there is a real definition, the
4046
     processor independent code will have arranged for us to see the
4047
     real definition first, and we can just use the same value.  */
4048
0
  if (h->is_weakalias)
4049
0
    {
4050
0
      struct elf_link_hash_entry *def = weakdef (h);
4051
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4052
0
      h->root.u.def.section = def->root.u.def.section;
4053
0
      h->root.u.def.value = def->root.u.def.value;
4054
0
      return true;
4055
0
    }
4056
4057
  /* This is a reference to a symbol defined by a dynamic object which
4058
     is not a function.  */
4059
4060
  /* If we are creating a shared library, we must presume that the
4061
     only references to the symbol are via the global offset table.
4062
     For such cases we need not do anything here; the relocations will
4063
     be handled correctly by relocate_section.  */
4064
0
  if (bfd_link_pic (info))
4065
0
    return true;
4066
4067
  /* If there are no references to this symbol that do not use the
4068
     GOT, we don't need to generate a copy reloc.  */
4069
0
  if (!h->non_got_ref)
4070
0
    return true;
4071
4072
  /* If -z nocopyreloc was given, we won't generate them either.  */
4073
0
  if (0 && info->nocopyreloc)
4074
0
    {
4075
0
      h->non_got_ref = 0;
4076
0
      return true;
4077
0
    }
4078
4079
  /* If we don't find any dynamic relocs in read-only sections, then
4080
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4081
0
  if (!_bfd_elf_readonly_dynrelocs (h))
4082
0
    {
4083
0
      h->non_got_ref = 0;
4084
0
      return true;
4085
0
    }
4086
4087
  /* We must allocate the symbol in our .dynbss section, which will
4088
     become part of the .bss section of the executable.  There will be
4089
     an entry for this symbol in the .dynsym section.  The dynamic
4090
     object will contain position independent code, so all references
4091
     from the dynamic object to this symbol will go through the global
4092
     offset table.  The dynamic linker will use the .dynsym entry to
4093
     determine the address it must put in the global offset table, so
4094
     both the dynamic object and the regular object will refer to the
4095
     same memory location for the variable.  */
4096
4097
0
  htab = nds32_elf_hash_table (info);
4098
0
  s = htab->root.sdynbss;
4099
0
  BFD_ASSERT (s != NULL);
4100
4101
  /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4102
     to copy the initial value out of the dynamic object and into the
4103
     runtime process image.  We need to remember the offset into the
4104
     .rela.bss section we are going to use.  */
4105
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4106
0
    {
4107
0
      asection *srel;
4108
4109
0
      srel = htab->root.srelbss;
4110
0
      BFD_ASSERT (srel != NULL);
4111
0
      srel->size += sizeof (Elf32_External_Rela);
4112
0
      h->needs_copy = 1;
4113
0
    }
4114
4115
  /* We need to figure out the alignment required for this symbol.  I
4116
     have no idea how ELF linkers handle this.  */
4117
0
  power_of_two = bfd_log2 (h->size);
4118
0
  if (power_of_two > 3)
4119
0
    power_of_two = 3;
4120
4121
  /* Apply the required alignment.  */
4122
0
  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4123
0
  if (!bfd_link_align_section (s, power_of_two))
4124
0
    return false;
4125
4126
  /* Define the symbol as being at this point in the section.  */
4127
0
  h->root.u.def.section = s;
4128
0
  h->root.u.def.value = s->size;
4129
4130
  /* Increment the section size to make room for the symbol.  */
4131
0
  s->size += h->size;
4132
4133
0
  return true;
4134
0
}
4135
4136
/* Allocate space in .plt, .got and associated reloc sections for
4137
   dynamic relocs.  */
4138
4139
static bool
4140
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4141
0
{
4142
0
  struct bfd_link_info *info;
4143
0
  struct elf_link_hash_table *ehtab;
4144
0
  struct elf_nds32_link_hash_table *htab;
4145
0
  struct elf_dyn_relocs *p;
4146
4147
0
  if (h->root.type == bfd_link_hash_indirect)
4148
0
    return true;
4149
4150
  /* When warning symbols are created, they **replace** the "real"
4151
     entry in the hash table, thus we never get to see the real
4152
     symbol in a hash traversal. So look at it now.  */
4153
0
  if (h->root.type == bfd_link_hash_warning)
4154
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4155
4156
0
  info = (struct bfd_link_info *) inf;
4157
0
  ehtab = elf_hash_table (info);
4158
0
  htab = nds32_elf_hash_table (info);
4159
0
  if (htab == NULL)
4160
0
    return false;
4161
4162
0
  if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4163
0
      && h->plt.refcount > 0
4164
0
      && !(bfd_link_pie (info) && h->def_regular))
4165
0
    {
4166
      /* Make sure this symbol is output as a dynamic symbol.
4167
   Undefined weak syms won't yet be marked as dynamic.  */
4168
0
      if (h->dynindx == -1 && !h->forced_local)
4169
0
  {
4170
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4171
0
      return false;
4172
0
  }
4173
4174
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4175
0
  {
4176
0
    asection *s = ehtab->splt;
4177
4178
    /* If this is the first .plt entry, make room for the special
4179
       first entry.  */
4180
0
    if (s->size == 0)
4181
0
      s->size += PLT_ENTRY_SIZE;
4182
4183
0
    h->plt.offset = s->size;
4184
4185
    /* If this symbol is not defined in a regular file, and we are
4186
       not generating a shared library, then set the symbol to this
4187
       location in the .plt.  This is required to make function
4188
       pointers compare as equal between the normal executable and
4189
       the shared library.  */
4190
0
    if (!bfd_link_pic (info) && !h->def_regular)
4191
0
      {
4192
0
        h->root.u.def.section = s;
4193
0
        h->root.u.def.value = h->plt.offset;
4194
0
      }
4195
4196
    /* Make room for this entry.  */
4197
0
    s->size += PLT_ENTRY_SIZE;
4198
4199
    /* We also need to make an entry in the .got.plt section, which
4200
       will be placed in the .got section by the linker script.  */
4201
0
    ehtab->sgotplt->size += 4;
4202
4203
    /* We also need to make an entry in the .rel.plt section.  */
4204
0
    ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4205
0
    if (htab->tls_desc_trampoline)
4206
0
      htab->next_tls_desc_index++;
4207
0
  }
4208
0
      else
4209
0
  {
4210
0
    h->plt.offset = (bfd_vma) - 1;
4211
0
    h->needs_plt = 0;
4212
0
  }
4213
0
    }
4214
0
  else
4215
0
    {
4216
0
      h->plt.offset = (bfd_vma) - 1;
4217
0
      h->needs_plt = 0;
4218
0
    }
4219
4220
0
  if (h->got.refcount > 0)
4221
0
    {
4222
0
      asection *sgot;
4223
0
      bool dyn;
4224
0
      int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4225
4226
      /* Make sure this symbol is output as a dynamic symbol.
4227
   Undefined weak syms won't yet be marked as dynamic.  */
4228
0
      if (h->dynindx == -1 && !h->forced_local)
4229
0
  {
4230
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4231
0
      return false;
4232
0
  }
4233
4234
0
      sgot = elf_hash_table (info)->sgot;
4235
0
      h->got.offset = sgot->size;
4236
4237
0
      if (tls_type == GOT_UNKNOWN)
4238
0
  abort ();
4239
4240
      /* Non-TLS symbols, and TLS_IE need one GOT slot.  */
4241
0
      if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4242
0
  sgot->size += 4;
4243
0
      else
4244
0
  {
4245
    /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots.  */
4246
0
    if (tls_type & GOT_TLS_DESC)
4247
0
      sgot->size += 8;
4248
0
  }
4249
4250
0
      dyn = htab->root.dynamic_sections_created;
4251
4252
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4253
0
  {
4254
0
    if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4255
0
      {
4256
        /* TLS_DESC with trampoline needs a relocation slot
4257
     within .rela.plt.  */
4258
0
        htab->num_tls_desc++;
4259
0
        ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4260
0
        htab->tls_trampoline = -1;
4261
0
      }
4262
0
    else
4263
0
      {
4264
        /* other relocations, including TLS_DESC without trampoline, need
4265
     a relocation slot within .rela.got.  */
4266
0
        ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4267
0
      }
4268
0
  }
4269
0
    }
4270
0
  else
4271
0
    h->got.offset = (bfd_vma)-1;
4272
4273
0
  if (h->dyn_relocs == NULL)
4274
0
    return true;
4275
4276
  /* In the shared -Bsymbolic case, discard space allocated for
4277
     dynamic pc-relative relocs against symbols which turn out to be
4278
     defined in regular objects.  For the normal shared case, discard
4279
     space for pc-relative relocs that have become local due to symbol
4280
     visibility changes.  */
4281
4282
0
  if (bfd_link_pic (info))
4283
0
    {
4284
0
      if (h->def_regular && (h->forced_local || info->symbolic))
4285
0
  {
4286
0
    struct elf_dyn_relocs **pp;
4287
4288
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4289
0
      {
4290
0
        p->count -= p->pc_count;
4291
0
        p->pc_count = 0;
4292
0
        if (p->count == 0)
4293
0
    *pp = p->next;
4294
0
        else
4295
0
    pp = &p->next;
4296
0
      }
4297
0
  }
4298
0
    }
4299
0
  else
4300
0
    {
4301
      /* For the non-shared case, discard space for relocs against
4302
   symbols which turn out to need copy relocs or are not dynamic.  */
4303
4304
0
      if (!h->non_got_ref
4305
0
    && ((h->def_dynamic
4306
0
         && !h->def_regular)
4307
0
        || (htab->root.dynamic_sections_created
4308
0
      && (h->root.type == bfd_link_hash_undefweak
4309
0
          || h->root.type == bfd_link_hash_undefined))))
4310
0
  {
4311
    /* Make sure this symbol is output as a dynamic symbol.
4312
       Undefined weak syms won't yet be marked as dynamic.  */
4313
0
    if (h->dynindx == -1 && !h->forced_local)
4314
0
      {
4315
0
        if (!bfd_elf_link_record_dynamic_symbol (info, h))
4316
0
    return false;
4317
0
      }
4318
4319
    /* If that succeeded, we know we'll be keeping all the
4320
       relocs.  */
4321
0
    if (h->dynindx != -1)
4322
0
      goto keep;
4323
0
  }
4324
4325
0
      h->dyn_relocs = NULL;
4326
4327
0
    keep:;
4328
0
    }
4329
4330
  /* Finally, allocate space.  */
4331
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
4332
0
    {
4333
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4334
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4335
0
    }
4336
4337
0
  return true;
4338
0
}
4339
4340
/* Add relocation REL to the end of relocation section SRELOC.  */
4341
4342
static void
4343
elf32_nds32_add_dynreloc (bfd *output_bfd,
4344
        struct bfd_link_info *info ATTRIBUTE_UNUSED,
4345
        asection *sreloc, Elf_Internal_Rela *rel)
4346
0
{
4347
0
  bfd_byte *loc;
4348
0
  if (sreloc == NULL)
4349
0
    abort ();
4350
4351
0
  loc = sreloc->contents;
4352
0
  loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4353
0
  if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4354
0
    abort ();
4355
4356
0
  bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4357
0
}
4358
4359
/* Set the sizes of the dynamic sections.  */
4360
4361
static bool
4362
nds32_elf_late_size_sections (struct bfd_link_info *info)
4363
0
{
4364
0
  struct elf_nds32_link_hash_table *htab;
4365
0
  bfd *dynobj;
4366
0
  asection *s;
4367
0
  bool relocs;
4368
0
  bfd *ibfd;
4369
4370
0
  htab = nds32_elf_hash_table (info);
4371
0
  if (htab == NULL)
4372
0
    return false;
4373
4374
0
  dynobj = elf_hash_table (info)->dynobj;
4375
0
  if (dynobj == NULL)
4376
0
    return true;
4377
4378
0
  if (elf_hash_table (info)->dynamic_sections_created)
4379
0
    {
4380
      /* Set the contents of the .interp section to the interpreter.  */
4381
0
      if (bfd_link_executable (info) && !info->nointerp)
4382
0
  {
4383
0
    s = elf_hash_table (info)->interp;
4384
0
    BFD_ASSERT (s != NULL);
4385
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4386
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4387
0
    s->alloced = 1;
4388
0
  }
4389
0
    }
4390
4391
  /* Set up .got offsets for local syms, and space for local dynamic
4392
     relocs.  */
4393
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4394
0
    {
4395
0
      bfd_signed_vma *local_got;
4396
0
      bfd_signed_vma *end_local_got;
4397
0
      bfd_size_type locsymcount;
4398
0
      Elf_Internal_Shdr *symtab_hdr;
4399
0
      asection *sgot;
4400
0
      char *local_tls_type;
4401
0
      bfd_vma *local_tlsdesc_gotent;
4402
4403
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4404
0
  continue;
4405
4406
0
      for (s = ibfd->sections; s != NULL; s = s->next)
4407
0
  {
4408
0
    struct elf_dyn_relocs *p;
4409
4410
0
    for (p = ((struct elf_dyn_relocs *)
4411
0
        elf_section_data (s)->local_dynrel);
4412
0
         p != NULL; p = p->next)
4413
0
      {
4414
0
        if (!bfd_is_abs_section (p->sec)
4415
0
      && bfd_is_abs_section (p->sec->output_section))
4416
0
    {
4417
      /* Input section has been discarded, either because
4418
         it is a copy of a linkonce section or due to
4419
         linker script /DISCARD/, so we'll be discarding
4420
         the relocs too.  */
4421
0
    }
4422
0
        else if (p->count != 0)
4423
0
    {
4424
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4425
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4426
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4427
0
        info->flags |= DF_TEXTREL;
4428
0
    }
4429
0
      }
4430
0
  }
4431
4432
0
      local_got = elf_local_got_refcounts (ibfd);
4433
0
      if (!local_got)
4434
0
  continue;
4435
4436
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
4437
0
      locsymcount = symtab_hdr->sh_info;
4438
0
      end_local_got = local_got + locsymcount;
4439
0
      sgot = elf_hash_table (info)->sgot;
4440
0
      local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4441
0
      local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4442
0
      for (; local_got < end_local_got;
4443
0
     ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
4444
0
  {
4445
0
    if (*local_got > 0)
4446
0
      {
4447
0
        int num_of_got_entry_needed = 0;
4448
0
        *local_got = sgot->size;
4449
0
        *local_tlsdesc_gotent = sgot->size;
4450
4451
        /* TLS_NORMAL, and TLS_IE need one slot in .got.  */
4452
0
        if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4453
0
    num_of_got_entry_needed = 1;
4454
        /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT.  */
4455
0
        else if (*local_tls_type & GOT_TLS_DESC)
4456
0
    num_of_got_entry_needed = 2;
4457
4458
0
        sgot->size += (num_of_got_entry_needed << 2);
4459
4460
        /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4461
     others need a slot in .rela.got.  */
4462
0
        if (*local_tls_type == GOT_TLS_DESC)
4463
0
    {
4464
0
      if (bfd_link_pic (info))
4465
0
        {
4466
0
          if (htab->tls_desc_trampoline)
4467
0
      {
4468
0
        htab->num_tls_desc++;
4469
0
        htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4470
0
        htab->tls_trampoline = -1;
4471
0
      }
4472
0
          else
4473
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4474
0
        }
4475
0
      else
4476
0
        {
4477
          /* TLS_DESC -> TLS_LE  */
4478
0
        }
4479
0
    }
4480
0
        else
4481
0
    {
4482
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4483
0
    }
4484
0
      }
4485
0
    else
4486
0
      {
4487
0
        *local_got = (bfd_vma) -1;
4488
0
        *local_tlsdesc_gotent = (bfd_vma) -1;
4489
0
      }
4490
0
  }
4491
0
    }
4492
4493
  /* Allocate global sym .plt and .got entries, and space for global
4494
     sym dynamic relocs.  */
4495
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4496
4497
  /* For every jump slot reserved in the sgotplt, reloc_count is
4498
     incremented.  However, when we reserve space for TLS descriptors,
4499
     it's not incremented, so in order to compute the space reserved
4500
     for them, it suffices to multiply the reloc count by the jump
4501
     slot size.  */
4502
0
  if (htab->tls_desc_trampoline && htab->root.srelplt)
4503
0
    htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4504
4505
0
  if (htab->tls_trampoline)
4506
0
    {
4507
0
      htab->tls_trampoline = htab->root.splt->size;
4508
4509
      /* If we're not using lazy TLS relocations, don't generate the
4510
   PLT and GOT entries they require.  */
4511
0
      if ((info->flags & DF_BIND_NOW))
4512
0
  htab->root.tlsdesc_plt = 0;
4513
0
      else
4514
0
  {
4515
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
4516
0
    htab->root.sgot->size += 4;
4517
4518
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
4519
0
    htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4520
0
  }
4521
0
    }
4522
4523
  /* We now have determined the sizes of the various dynamic sections.
4524
     Allocate memory for them.  */
4525
  /* The check_relocs and adjust_dynamic_symbol entry points have
4526
     determined the sizes of the various dynamic sections.  Allocate
4527
     memory for them.  */
4528
0
  relocs = false;
4529
0
  for (s = dynobj->sections; s != NULL; s = s->next)
4530
0
    {
4531
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
4532
0
  continue;
4533
4534
0
      if (s == htab->root.splt)
4535
0
  {
4536
    /* Strip this section if we don't need it; see the
4537
       comment below.  */
4538
0
    ;
4539
0
  }
4540
0
      else if (s == elf_hash_table (info)->sgot)
4541
0
  {
4542
0
    got_size += s->size;
4543
0
  }
4544
0
      else if (s == elf_hash_table (info)->sgotplt)
4545
0
  {
4546
0
    got_size += s->size;
4547
0
  }
4548
0
      else if (startswith (bfd_section_name (s), ".rela"))
4549
0
  {
4550
0
    if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4551
0
      relocs = true;
4552
4553
    /* We use the reloc_count field as a counter if we need
4554
       to copy relocs into the output file.  */
4555
0
    s->reloc_count = 0;
4556
0
  }
4557
0
      else
4558
0
  {
4559
    /* It's not one of our sections, so don't allocate space.  */
4560
0
    continue;
4561
0
  }
4562
4563
0
      if (s->size == 0)
4564
0
  {
4565
    /* If we don't need this section, strip it from the
4566
       output file.  This is mostly to handle .rela.bss and
4567
       .rela.plt.  We must create both sections in
4568
       create_dynamic_sections, because they must be created
4569
       before the linker maps input sections to output
4570
       sections.  The linker does that before
4571
       adjust_dynamic_symbol is called, and it is that
4572
       function which decides whether anything needs to go
4573
       into these sections.  */
4574
0
    s->flags |= SEC_EXCLUDE;
4575
0
    continue;
4576
0
  }
4577
4578
      /* Allocate memory for the section contents.  We use bfd_zalloc
4579
   here in case unused entries are not reclaimed before the
4580
   section's contents are written out.  This should not happen,
4581
   but this way if it does, we get a R_NDS32_NONE reloc instead
4582
   of garbage.  */
4583
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4584
0
      if (s->contents == NULL)
4585
0
  return false;
4586
0
      s->alloced = 1;
4587
0
    }
4588
4589
0
  return _bfd_elf_add_dynamic_tags (info, relocs);
4590
0
}
4591
4592
static bfd_reloc_status_type
4593
nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4594
       bfd_vma relocation, bfd_byte *location)
4595
0
{
4596
0
  int size;
4597
0
  bfd_vma x = 0;
4598
0
  bfd_reloc_status_type flag;
4599
0
  unsigned int rightshift = howto->rightshift;
4600
0
  unsigned int bitpos = howto->bitpos;
4601
4602
0
  if (howto->negate)
4603
0
    relocation = -relocation;
4604
4605
  /* Get the value we are going to relocate.  */
4606
0
  size = bfd_get_reloc_size (howto);
4607
0
  switch (size)
4608
0
    {
4609
0
    default:
4610
0
      abort ();
4611
0
      break;
4612
0
    case 0:
4613
0
      return bfd_reloc_ok;
4614
0
    case 2:
4615
0
      x = bfd_getb16 (location);
4616
0
      break;
4617
0
    case 4:
4618
0
      x = bfd_getb32 (location);
4619
0
      break;
4620
0
    }
4621
4622
  /* Check for overflow.  FIXME: We may drop bits during the addition
4623
     which we don't check for.  We must either check at every single
4624
     operation, which would be tedious, or we must do the computations
4625
     in a type larger than bfd_vma, which would be inefficient.  */
4626
0
  flag = bfd_reloc_ok;
4627
0
  if (howto->complain_on_overflow != complain_overflow_dont)
4628
0
    {
4629
0
      bfd_vma addrmask, fieldmask, signmask, ss;
4630
0
      bfd_vma a, b, sum;
4631
4632
      /* Get the values to be added together.  For signed and unsigned
4633
   relocations, we assume that all values should be truncated to
4634
   the size of an address.  For bitfields, all the bits matter.
4635
   See also bfd_check_overflow.  */
4636
0
      fieldmask = N_ONES (howto->bitsize);
4637
0
      signmask = ~fieldmask;
4638
0
      addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4639
0
      a = (relocation & addrmask) >> rightshift;
4640
0
      b = (x & howto->src_mask & addrmask) >> bitpos;
4641
4642
0
      switch (howto->complain_on_overflow)
4643
0
  {
4644
0
  case complain_overflow_signed:
4645
    /* If any sign bits are set, all sign bits must be set.
4646
       That is, A must be a valid negative address after
4647
       shifting.  */
4648
0
    signmask = ~(fieldmask >> 1);
4649
    /* Fall through.  */
4650
4651
0
  case complain_overflow_bitfield:
4652
    /* Much like the signed check, but for a field one bit
4653
       wider.  We allow a bitfield to represent numbers in the
4654
       range -2**n to 2**n-1, where n is the number of bits in the
4655
       field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4656
       can't overflow, which is exactly what we want.  */
4657
0
    ss = a & signmask;
4658
0
    if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4659
0
      flag = bfd_reloc_overflow;
4660
4661
    /* We only need this next bit of code if the sign bit of B
4662
       is below the sign bit of A.  This would only happen if
4663
       SRC_MASK had fewer bits than BITSIZE.  Note that if
4664
       SRC_MASK has more bits than BITSIZE, we can get into
4665
       trouble; we would need to verify that B is in range, as
4666
       we do for A above.  */
4667
0
    ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4668
0
    ss >>= bitpos;
4669
4670
    /* Set all the bits above the sign bit.  */
4671
0
    b = (b ^ ss) - ss;
4672
4673
    /* Now we can do the addition.  */
4674
0
    sum = a + b;
4675
4676
    /* See if the result has the correct sign.  Bits above the
4677
       sign bit are junk now; ignore them.  If the sum is
4678
       positive, make sure we did not have all negative inputs;
4679
       if the sum is negative, make sure we did not have all
4680
       positive inputs.  The test below looks only at the sign
4681
       bits, and it really just
4682
       SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4683
4684
       We mask with addrmask here to explicitly allow an address
4685
       wrap-around.  The Linux kernel relies on it, and it is
4686
       the only way to write assembler code which can run when
4687
       loaded at a location 0x80000000 away from the location at
4688
       which it is linked.  */
4689
0
    if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4690
0
      flag = bfd_reloc_overflow;
4691
4692
0
    break;
4693
4694
0
  case complain_overflow_unsigned:
4695
    /* Checking for an unsigned overflow is relatively easy:
4696
       trim the addresses and add, and trim the result as well.
4697
       Overflow is normally indicated when the result does not
4698
       fit in the field.  However, we also need to consider the
4699
       case when, e.g., fieldmask is 0x7fffffff or smaller, an
4700
       input is 0x80000000, and bfd_vma is only 32 bits; then we
4701
       will get sum == 0, but there is an overflow, since the
4702
       inputs did not fit in the field.  Instead of doing a
4703
       separate test, we can check for this by or-ing in the
4704
       operands when testing for the sum overflowing its final
4705
       field.  */
4706
0
    sum = (a + b) & addrmask;
4707
0
    if ((a | b | sum) & signmask)
4708
0
      flag = bfd_reloc_overflow;
4709
0
    break;
4710
4711
0
  default:
4712
0
    abort ();
4713
0
  }
4714
0
    }
4715
4716
  /* Put RELOCATION in the right bits.  */
4717
0
  relocation >>= (bfd_vma) rightshift;
4718
0
  relocation <<= (bfd_vma) bitpos;
4719
4720
  /* Add RELOCATION to the right bits of X.  */
4721
  /* FIXME : 090616
4722
     Because the relaxation may generate duplicate relocation at one address,
4723
     an addition to immediate in the instruction may cause the relocation added
4724
     several times.
4725
     This bug should be fixed in assembler, but a check is also needed here.  */
4726
0
  if (howto->partial_inplace)
4727
0
    x = ((x & ~howto->dst_mask)
4728
0
   | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4729
0
  else
4730
0
    x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4731
4732
4733
  /* Put the relocated value back in the object file.  */
4734
0
  switch (size)
4735
0
    {
4736
0
    default:
4737
0
    case 0:
4738
0
    case 1:
4739
0
    case 8:
4740
0
      abort ();
4741
0
      break;
4742
0
    case 2:
4743
0
      bfd_putb16 (x, location);
4744
0
      break;
4745
0
    case 4:
4746
0
      bfd_putb32 (x, location);
4747
0
      break;
4748
0
    }
4749
4750
0
  return flag;
4751
0
}
4752
4753
static bfd_reloc_status_type
4754
nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4755
             asection *input_section, bfd_byte *contents,
4756
             bfd_vma address, bfd_vma value, bfd_vma addend)
4757
0
{
4758
0
  bfd_vma relocation;
4759
4760
  /* Sanity check the address.  */
4761
0
  bfd_vma octet = address * OCTETS_PER_BYTE (input_bfd, input_section);
4762
0
  if (!bfd_reloc_offset_in_range (howto, input_bfd, input_section, octet))
4763
0
    return bfd_reloc_outofrange;
4764
4765
  /* This function assumes that we are dealing with a basic relocation
4766
     against a symbol.  We want to compute the value of the symbol to
4767
     relocate to.  This is just VALUE, the value of the symbol, plus
4768
     ADDEND, any addend associated with the reloc.  */
4769
0
  relocation = value + addend;
4770
4771
  /* If the relocation is PC relative, we want to set RELOCATION to
4772
     the distance between the symbol (currently in RELOCATION) and the
4773
     location we are relocating.  If pcrel_offset is FALSE we do not
4774
     need to subtract out the offset of the location within the
4775
     section (which is just ADDRESS).  */
4776
0
  if (howto->pc_relative)
4777
0
    {
4778
0
      relocation -= (input_section->output_section->vma
4779
0
         + input_section->output_offset);
4780
0
      if (howto->pcrel_offset)
4781
0
  relocation -= address;
4782
0
    }
4783
4784
0
  return nds32_relocate_contents (howto, input_bfd, relocation,
4785
0
          contents + address);
4786
0
}
4787
4788
static int
4789
nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4790
            const char *name,
4791
            Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4792
            asection *input_sec,
4793
            struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4794
0
{
4795
0
  const char *source;
4796
0
  FILE *sym_ld_script = NULL;
4797
0
  struct elf_nds32_link_hash_table *table;
4798
4799
0
  table = nds32_elf_hash_table (info);
4800
0
  sym_ld_script = table->sym_ld_script;
4801
0
  if (!sym_ld_script)
4802
0
    return true;
4803
4804
0
  if (!h || !name || *name == '\0')
4805
0
    return true;
4806
4807
0
  if (input_sec->flags & SEC_EXCLUDE)
4808
0
    return true;
4809
4810
0
  if (!check_start_export_sym)
4811
0
    {
4812
0
      fprintf (sym_ld_script, "SECTIONS\n{\n");
4813
0
      check_start_export_sym = 1;
4814
0
    }
4815
4816
0
  if (h->root.type == bfd_link_hash_defined
4817
0
      || h->root.type == bfd_link_hash_defweak)
4818
0
    {
4819
0
      if (!h->root.u.def.section->output_section)
4820
0
  return true;
4821
4822
0
      if (bfd_is_const_section (input_sec))
4823
0
  source = input_sec->name;
4824
0
      else
4825
0
  source = bfd_get_filename (input_sec->owner);
4826
4827
0
      fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4828
0
         h->root.root.string,
4829
0
         (long) (h->root.u.def.value
4830
0
    + h->root.u.def.section->output_section->vma
4831
0
    + h->root.u.def.section->output_offset), source);
4832
0
    }
4833
4834
0
  return true;
4835
0
}
4836
4837
/* Relocate an NDS32/D ELF section.
4838
   There is some attempt to make this function usable for many architectures,
4839
   both for RELA and REL type relocs, if only to serve as a learning tool.
4840
4841
   The RELOCATE_SECTION function is called by the new ELF backend linker
4842
   to handle the relocations for a section.
4843
4844
   The relocs are always passed as Rela structures; if the section
4845
   actually uses Rel structures, the r_addend field will always be
4846
   zero.
4847
4848
   This function is responsible for adjust the section contents as
4849
   necessary, and (if using Rela relocs and generating a
4850
   relocatable output file) adjusting the reloc addend as
4851
   necessary.
4852
4853
   This function does not have to worry about setting the reloc
4854
   address or the reloc symbol index.
4855
4856
   LOCAL_SYMS is a pointer to the swapped in local symbols.
4857
4858
   LOCAL_SECTIONS is an array giving the section in the input file
4859
   corresponding to the st_shndx field of each local symbol.
4860
4861
   The global hash table entry for the global symbols can be found
4862
   via elf_sym_hashes (input_bfd).
4863
4864
   When generating relocatable output, this function must handle
4865
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4866
   going to be the section symbol corresponding to the output
4867
   section, which means that the addend must be adjusted
4868
   accordingly.  */
4869
4870
/* Return the base VMA address which should be subtracted from real addresses
4871
   when resolving @dtpoff relocation.
4872
   This is PT_TLS segment p_vaddr.  */
4873
4874
/* Return the relocation value for @tpoff relocation
4875
   if STT_TLS virtual address is ADDRESS.  */
4876
4877
/* Return the relocation value for @gottpoff relocation
4878
   if STT_TLS virtual address is ADDRESS.  */
4879
4880
static bfd_vma
4881
gottpoff (struct bfd_link_info *info, bfd_vma address)
4882
0
{
4883
0
  bfd_vma tp_base;
4884
0
  bfd_vma tp_offset;
4885
4886
  /* If tls_sec is NULL, we should have signalled an error already.  */
4887
0
  if (elf_hash_table (info)->tls_sec == NULL)
4888
0
    return 0;
4889
4890
0
  tp_base = elf_hash_table (info)->tls_sec->vma;
4891
0
  tp_offset = address - tp_base;
4892
4893
0
  return tp_offset;
4894
0
}
4895
4896
static bool
4897
patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4898
0
{
4899
  /* TLS_GD/TLS_LD model #1
4900
     46 00 00 00 sethi $r0,#0x0
4901
     58 00 00 00 ori $r0,$r0,#0x0
4902
     40 00 74 00 add $r0,$r0,$gp
4903
     04 10 00 00 lwi $r1,[$r0+#0x0]
4904
     4b e0 04 01 jral $lp,$r1  */
4905
4906
  /* TLS_GD/TLS_LD model #2
4907
     46 00 00 00 sethi $r0,#0x0
4908
     58 00 00 00 ori $r0,$r0,#0x0
4909
     38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4910
     40 00 74 00 add $r0,$r0,$gp
4911
     4b e0 04 01 jral $lp,$r1  */
4912
4913
  /* TLS_IE model (non-PIC)
4914
     46 00 00 00 sethi $r0,#0x0
4915
     04 00 00 00 lwi $r0,[$r0+#0x0]
4916
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4917
4918
  /* TLS_IE model (PIC)
4919
     46 00 00 00 sethi $r0,#0x0
4920
     58 00 00 00 ori $r0,$r0,#0x0
4921
     38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4922
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4923
4924
  /* TLS_GD_TO_IE model
4925
     46 00 00 00 sethi $r0,#0x0
4926
     58 00 00 00 ori $r0,$r0,#0x0
4927
     40 00 74 00 add $r0,$rM,$gp
4928
     04 00 00 01 lwi $r0,[$r0+#0x4]
4929
     40 00 64 00 add $r0,$r0,$r25  */
4930
4931
0
  bool rz = false;
4932
4933
0
  typedef struct
4934
0
    {
4935
0
      uint32_t opcode;
4936
0
      uint32_t mask;
4937
0
    } pat_t;
4938
4939
0
  uint32_t patch[3] =
4940
0
    {
4941
0
      0x40007400, /* add $r0,$rM,$gp     */
4942
0
      0x04000001, /* lwi $r0,[$r0+#0x4]  */
4943
0
      0x40006400, /* add $r0,$r0,$r25    */
4944
0
    };
4945
4946
0
  pat_t mode0[3] =
4947
0
    {
4948
0
  { 0x40000000, 0xfe0003ff },
4949
0
  { 0x04000000, 0xfe000000 },
4950
0
  { 0x4be00001, 0xffff83ff },
4951
0
    };
4952
4953
0
  pat_t mode1[3] =
4954
0
    {
4955
0
  { 0x38007402, 0xfe007fff },
4956
0
  { 0x40007400, 0xfe007fff },
4957
0
  { 0x4be00001, 0xffff83ff },
4958
0
    };
4959
4960
0
  unsigned char *p = contents + rel->r_offset;
4961
4962
0
  uint32_t insn;
4963
0
  uint32_t regidx = 0;
4964
0
  insn = bfd_getb32 (p);
4965
0
  if (INSN_SETHI == (0xfe0fffffu & insn))
4966
0
    {
4967
0
      regidx = 0x1f & (insn >> 20);
4968
0
      p += 4;
4969
0
    }
4970
4971
0
  insn = bfd_getb32 (p);
4972
0
  if (INSN_ORI == (0xfe007fffu & insn))
4973
0
    {
4974
0
      regidx = 0x1f & (insn >> 20);
4975
0
      p += 4;
4976
0
    }
4977
4978
0
  if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction.  */
4979
0
    {
4980
      /* already patched?  */
4981
0
      if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4982
0
    (patch[1] == bfd_getb32 (p + 4)))
4983
0
  rz = true;
4984
0
    }
4985
0
  else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4986
0
    {
4987
0
      if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4988
0
    (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4989
0
  {
4990
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4991
0
    bfd_putb32 (patch[1], p + 4);
4992
0
    bfd_putb32 (patch[2], p + 8);
4993
0
    rz = true;
4994
0
  }
4995
0
    }
4996
0
  else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4997
0
    {
4998
0
      if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4999
0
    (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5000
0
  {
5001
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5002
0
    bfd_putb32 (patch[1], p + 4);
5003
0
    bfd_putb32 (patch[2], p + 8);
5004
0
    rz = true;
5005
0
  }
5006
0
    }
5007
5008
0
  if (!rz)
5009
0
    {
5010
0
      printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
5011
0
        (int) rel->r_offset);
5012
0
      BFD_ASSERT(0); /* Unsupported pattern.  */
5013
0
    }
5014
5015
0
  return rz;
5016
0
}
5017
5018
static enum elf_nds32_tls_type
5019
get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5020
5021
static unsigned int
5022
ones32 (register unsigned int x)
5023
0
{
5024
  /* 32-bit recursive reduction using SWAR...
5025
     but first step is mapping 2-bit values
5026
     into sum of 2 1-bit values in sneaky way.  */
5027
0
  x -= ((x >> 1) & 0x55555555);
5028
0
  x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5029
0
  x = (((x >> 4) + x) & 0x0f0f0f0f);
5030
0
  x += (x >> 8);
5031
0
  x += (x >> 16);
5032
0
  return (x & 0x0000003f);
5033
0
}
5034
5035
#if !HAVE_FLS
5036
static unsigned int
5037
fls (register unsigned int x)
5038
0
{
5039
0
  return ffs (x & (-x));
5040
0
}
5041
#endif /* !HAVE_FLS */
5042
5043
#define nds32_elf_local_tlsdesc_gotent(bfd) \
5044
  (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5045
5046
static int
5047
nds32_elf_relocate_section (struct bfd_link_info * info,
5048
          bfd *      input_bfd,
5049
          asection *       input_section,
5050
          bfd_byte *       contents,
5051
          Elf_Internal_Rela *    relocs,
5052
          Elf_Internal_Sym *     local_syms,
5053
          asection **      local_sections)
5054
0
{
5055
0
  Elf_Internal_Shdr *symtab_hdr;
5056
0
  struct elf_link_hash_entry **sym_hashes;
5057
0
  Elf_Internal_Rela *rel, *relend;
5058
0
  bool ret = true;   /* Assume success.  */
5059
0
  int align = 0;
5060
0
  bfd_reloc_status_type r;
5061
0
  const char *errmsg = NULL;
5062
0
  bfd_vma gp;
5063
0
  struct elf_link_hash_table *ehtab;
5064
0
  struct elf_nds32_link_hash_table *htab;
5065
0
  bfd *dynobj;
5066
0
  bfd_vma *local_got_offsets;
5067
0
  asection *sgot, *splt, *sreloc;
5068
0
  bfd_vma high_address;
5069
0
  struct elf_nds32_link_hash_table *table;
5070
0
  int eliminate_gc_relocs;
5071
0
  bfd_vma fpbase_addr;
5072
5073
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
5074
0
  sym_hashes = elf_sym_hashes (input_bfd);
5075
0
  ehtab = elf_hash_table (info);
5076
0
  htab = nds32_elf_hash_table (info);
5077
0
  high_address = bfd_get_section_limit (input_bfd, input_section);
5078
5079
0
  dynobj = htab->root.dynobj;
5080
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
5081
5082
0
  sgot = ehtab->sgot;
5083
0
  splt = ehtab->splt;
5084
0
  sreloc = NULL;
5085
5086
0
  rel = relocs;
5087
0
  relend = relocs + input_section->reloc_count;
5088
5089
0
  table = nds32_elf_hash_table (info);
5090
0
  eliminate_gc_relocs = table->eliminate_gc_relocs;
5091
5092
  /* By this time, we can adjust the value of _SDA_BASE_.  */
5093
  /* Explain _SDA_BASE_  */
5094
0
  if ((!bfd_link_relocatable (info)))
5095
0
    {
5096
0
      is_SDA_BASE_set = 1;
5097
0
      r = nds32_elf_final_sda_base (info->output_bfd, info, &gp, true);
5098
0
      if (r != bfd_reloc_ok)
5099
0
  return false;
5100
0
    }
5101
5102
  /* Do TLS model conversion once at first.  */
5103
0
  nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5104
5105
  /* Use gp as fp to prevent truncated fit.  Because in relaxation time
5106
     the fp value is set as gp, and it has be reverted for instruction
5107
     setting fp.  */
5108
0
  fpbase_addr = elf_gp (info->output_bfd);
5109
5110
  /* Deal with (dynamic) relocations.  */
5111
0
  for (rel = relocs; rel < relend; rel++)
5112
0
    {
5113
0
      enum elf_nds32_reloc_type r_type;
5114
0
      reloc_howto_type *howto = NULL;
5115
0
      unsigned long r_symndx;
5116
0
      struct elf_link_hash_entry *h = NULL;
5117
0
      Elf_Internal_Sym *sym = NULL;
5118
0
      asection *sec;
5119
0
      bfd_vma relocation;
5120
0
      bfd_vma relocation_sym = 0xdeadbeef;
5121
0
      Elf_Internal_Rela *lorel;
5122
0
      bfd_vma off;
5123
5124
      /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5125
   ensure it's zero (we use REL relocs, not RELA).  Therefore this
5126
   should be assigning zero to `addend', but for clarity we use
5127
   `r_addend'.  */
5128
5129
0
      bfd_vma addend = rel->r_addend;
5130
0
      bfd_vma offset = rel->r_offset;
5131
5132
0
      r_type = ELF32_R_TYPE (rel->r_info);
5133
0
      if (r_type >= R_NDS32_max)
5134
0
  {
5135
    /* xgettext:c-format */
5136
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5137
0
            input_bfd, r_type);
5138
0
    bfd_set_error (bfd_error_bad_value);
5139
0
    ret = false;
5140
0
    continue;
5141
0
  }
5142
5143
0
      if (r_type == R_NDS32_GNU_VTENTRY
5144
0
    || r_type == R_NDS32_GNU_VTINHERIT
5145
0
    || r_type == R_NDS32_NONE
5146
0
    || r_type == R_NDS32_RELA_GNU_VTENTRY
5147
0
    || r_type == R_NDS32_RELA_GNU_VTINHERIT
5148
0
    || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5149
0
    || r_type == R_NDS32_DATA
5150
0
    || r_type == R_NDS32_TRAN)
5151
0
  continue;
5152
5153
      /* If we enter the fp-as-gp region.  Resolve the address
5154
   of best fp-base.  */
5155
0
      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5156
0
    && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5157
0
  {
5158
0
    int dist;
5159
5160
    /* Distance to relocation of best fp-base is encoded in R_SYM.  */
5161
0
    dist =  rel->r_addend >> 16;
5162
0
    fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5163
0
              local_syms, symtab_hdr);
5164
0
  }
5165
0
      else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5166
0
         && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5167
0
  {
5168
0
    fpbase_addr = elf_gp (info->output_bfd);
5169
0
  }
5170
5171
      /* Skip the relocations used for relaxation.  */
5172
      /* We have to update LONGCALL and LONGJUMP
5173
   relocations when generating the relocatable files.  */
5174
0
      if (!bfd_link_relocatable (info)
5175
0
    && (r_type >= R_NDS32_RELAX_ENTRY
5176
0
        || (r_type >= R_NDS32_LONGCALL4
5177
0
      && r_type <= R_NDS32_LONGJUMP7)))
5178
0
  continue;
5179
5180
0
      howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5181
0
      r_symndx = ELF32_R_SYM (rel->r_info);
5182
5183
      /* This is a final link.  */
5184
0
      sym = NULL;
5185
0
      sec = NULL;
5186
0
      h = NULL;
5187
5188
0
      if (r_symndx < symtab_hdr->sh_info)
5189
0
  {
5190
    /* Local symbol.  */
5191
0
    sym = local_syms + r_symndx;
5192
0
    sec = local_sections[r_symndx];
5193
5194
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
5195
0
            sym, &sec, rel);
5196
0
    addend = rel->r_addend;
5197
5198
    /* keep symbol location for static TLS_IE GOT entry  */
5199
0
    relocation_sym = relocation;
5200
0
    if (bfd_link_relocatable (info))
5201
0
      {
5202
        /* This is a relocatable link.  We don't have to change
5203
     anything, unless the reloc is against a section symbol,
5204
     in which case we have to adjust according to where the
5205
     section symbol winds up in the output section.  */
5206
0
        if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5207
0
    rel->r_addend += sec->output_offset + sym->st_value;
5208
5209
0
        continue;
5210
0
      }
5211
0
  }
5212
0
      else
5213
0
  {
5214
    /* External symbol.  */
5215
0
    if (bfd_link_relocatable (info))
5216
0
      continue;
5217
0
    bool warned, ignored, unresolved_reloc;
5218
0
    int symndx = r_symndx - symtab_hdr->sh_info;
5219
5220
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5221
0
           r_symndx, symtab_hdr, sym_hashes, h, sec,
5222
0
           relocation, unresolved_reloc, warned,
5223
0
           ignored);
5224
5225
    /* keep symbol location for static TLS_IE GOT entry  */
5226
0
    relocation_sym = relocation;
5227
5228
    /* la $fp, _FP_BASE_ is per-function (region).
5229
       Handle it specially.  */
5230
0
    switch ((int) r_type)
5231
0
      {
5232
0
      case R_NDS32_HI20_RELA:
5233
0
      case R_NDS32_LO12S0_RELA:
5234
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5235
0
        FP_BASE_NAME) == 0)
5236
0
    {
5237
0
      if (!bfd_link_pie (info))
5238
0
        {
5239
0
          _bfd_error_handler
5240
0
      ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5241
0
       input_bfd);
5242
0
        }
5243
0
      relocation = fpbase_addr;
5244
0
    }
5245
0
        break;
5246
0
      case R_NDS32_SDA19S0_RELA:
5247
0
      case R_NDS32_SDA15S0_RELA:
5248
0
      case R_NDS32_20_RELA:
5249
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5250
0
        FP_BASE_NAME) == 0)
5251
0
    {
5252
0
      relocation = fpbase_addr;
5253
0
      break;
5254
0
    }
5255
0
      }
5256
0
  }
5257
5258
      /* Sanity check the address.  */
5259
0
      if (offset > high_address)
5260
0
  {
5261
0
    r = bfd_reloc_outofrange;
5262
0
    goto check_reloc;
5263
0
  }
5264
5265
0
      if (r_type >= R_NDS32_RELAX_ENTRY)
5266
0
  continue;
5267
5268
0
      switch ((int) r_type)
5269
0
  {
5270
0
  case R_NDS32_GOTOFF:
5271
    /* Relocation is relative to the start of the global offset
5272
       table (for ld24 rx, #uimm24), e.g. access at label+addend
5273
5274
       ld24 rx. #label@GOTOFF + addend
5275
       sub  rx, r12.  */
5276
0
  case R_NDS32_GOTOFF_HI20:
5277
0
  case R_NDS32_GOTOFF_LO12:
5278
0
  case R_NDS32_GOTOFF_LO15:
5279
0
  case R_NDS32_GOTOFF_LO19:
5280
0
    BFD_ASSERT (sgot != NULL);
5281
5282
0
    relocation -= elf_gp (info->output_bfd);
5283
0
    break;
5284
5285
0
  case R_NDS32_9_PLTREL:
5286
0
  case R_NDS32_25_PLTREL:
5287
    /* Relocation is to the entry for this symbol in the
5288
       procedure linkage table.  */
5289
5290
    /* The native assembler will generate a 25_PLTREL reloc
5291
       for a local symbol if you assemble a call from one
5292
       section to another when using -K pic.  */
5293
0
    if (h == NULL)
5294
0
      break;
5295
5296
0
    if (h->forced_local)
5297
0
      break;
5298
5299
    /* We didn't make a PLT entry for this symbol.  This
5300
       happens when statically linking PIC code, or when
5301
       using -Bsymbolic.  */
5302
0
    if (h->plt.offset == (bfd_vma) - 1)
5303
0
      break;
5304
5305
0
    relocation = (splt->output_section->vma
5306
0
      + splt->output_offset + h->plt.offset);
5307
0
    break;
5308
5309
0
  case R_NDS32_PLT_GOTREL_HI20:
5310
0
  case R_NDS32_PLT_GOTREL_LO12:
5311
0
  case R_NDS32_PLT_GOTREL_LO15:
5312
0
  case R_NDS32_PLT_GOTREL_LO19:
5313
0
  case R_NDS32_PLT_GOTREL_LO20:
5314
0
    if (h == NULL
5315
0
        || h->forced_local
5316
0
        || h->plt.offset == (bfd_vma) -1
5317
0
        || (bfd_link_pie (info) && h->def_regular))
5318
0
      {
5319
        /* Maybe we should find better checking to optimize
5320
     PIE PLT relocations.  */
5321
        /* We didn't make a PLT entry for this symbol.  This
5322
     happens when statically linking PIC code, or when
5323
     using -Bsymbolic.  */
5324
0
        if (h)
5325
0
    h->plt.offset = (bfd_vma) -1;   /* Cancel PLT trampoline.  */
5326
0
        relocation -= elf_gp (info->output_bfd);
5327
0
        break;
5328
0
      }
5329
5330
0
    relocation = (splt->output_section->vma
5331
0
      + splt->output_offset + h->plt.offset);
5332
5333
0
    relocation -= elf_gp (info->output_bfd);
5334
0
    break;
5335
5336
0
  case R_NDS32_PLTREL_HI20:
5337
0
  case R_NDS32_PLTREL_LO12:
5338
5339
    /* Relocation is to the entry for this symbol in the
5340
       procedure linkage table.  */
5341
5342
    /* The native assembler will generate a 25_PLTREL reloc
5343
       for a local symbol if you assemble a call from one
5344
       section to another when using -K pic.  */
5345
0
    if (h == NULL)
5346
0
      break;
5347
5348
0
    if (h->forced_local)
5349
0
      break;
5350
5351
0
    if (h->plt.offset == (bfd_vma) - 1)
5352
      /* We didn't make a PLT entry for this symbol.  This
5353
         happens when statically linking PIC code, or when
5354
         using -Bsymbolic.  */
5355
0
      break;
5356
5357
0
    if (splt == NULL)
5358
0
      break;
5359
5360
0
    relocation = (splt->output_section->vma
5361
0
      + splt->output_offset
5362
0
      + h->plt.offset + 4)
5363
0
           - (input_section->output_section->vma
5364
0
        + input_section->output_offset
5365
0
        + rel->r_offset);
5366
5367
0
    break;
5368
5369
0
  case R_NDS32_GOTPC20:
5370
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5371
       ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
5372
0
    relocation = elf_gp (info->output_bfd);
5373
0
    break;
5374
5375
0
  case R_NDS32_GOTPC_HI20:
5376
0
  case R_NDS32_GOTPC_LO12:
5377
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5378
       bl .+4
5379
       seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5380
       or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5381
       or
5382
       bl .+4
5383
       seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5384
       add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)  */
5385
0
    relocation = elf_gp (info->output_bfd);
5386
0
    relocation -= (input_section->output_section->vma
5387
0
       + input_section->output_offset + rel->r_offset);
5388
0
    break;
5389
5390
0
  case R_NDS32_GOT20:
5391
    /* Fall through.  */
5392
0
  case R_NDS32_GOT_HI20:
5393
0
  case R_NDS32_GOT_LO12:
5394
0
  case R_NDS32_GOT_LO15:
5395
0
  case R_NDS32_GOT_LO19:
5396
    /* Relocation is to the entry for this symbol in the global
5397
       offset table.  */
5398
0
    BFD_ASSERT (sgot != NULL);
5399
5400
0
    if (h != NULL)
5401
0
      {
5402
        /* External symbol  */
5403
0
        bool dyn;
5404
5405
0
        off = h->got.offset;
5406
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5407
0
        dyn = htab->root.dynamic_sections_created;
5408
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5409
0
                bfd_link_pic (info),
5410
0
                h)
5411
0
      || (bfd_link_pic (info)
5412
0
          && (info->symbolic
5413
0
        || h->dynindx == -1
5414
0
        || h->forced_local) && h->def_regular))
5415
0
    {
5416
      /* This is actually a static link, or it is a
5417
         -Bsymbolic link and the symbol is defined
5418
         locally, or the symbol was forced to be local
5419
         because of a version file.  We must initialize
5420
         this entry in the global offset table.  Since the
5421
         offset must always be a multiple of 4, we use the
5422
         least significant bit to record whether we have
5423
         initialized it already.
5424
5425
         When doing a dynamic link, we create a .rela.got
5426
         relocation entry to initialize the value.  This
5427
         is done in the finish_dynamic_symbol routine.  */
5428
0
      if ((off & 1) != 0) /* clear LSB  */
5429
0
        off &= ~1;
5430
0
      else
5431
0
        {
5432
0
          bfd_put_32 (info->output_bfd, relocation,
5433
0
          sgot->contents + off);
5434
0
          h->got.offset |= 1;
5435
0
        }
5436
0
    }
5437
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5438
0
         - elf_gp (info->output_bfd);
5439
0
      }
5440
0
    else
5441
0
      {
5442
        /* Local symbol  */
5443
0
        bfd_byte *loc;
5444
5445
0
        BFD_ASSERT (local_got_offsets != NULL
5446
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5447
5448
0
        off = local_got_offsets[r_symndx];
5449
5450
        /* The offset must always be a multiple of 4.  We use
5451
     the least significant bit to record whether we have
5452
     already processed this entry.  */
5453
0
        if ((off & 1) != 0) /* clear LSB  */
5454
0
    off &= ~1;
5455
0
        else
5456
0
    {
5457
0
      bfd_put_32 (info->output_bfd, relocation,
5458
0
            sgot->contents + off);
5459
5460
0
      if (bfd_link_pic (info))
5461
0
        {
5462
0
          asection *srelgot;
5463
0
          Elf_Internal_Rela outrel;
5464
5465
          /* We need to generate a R_NDS32_RELATIVE reloc
5466
       for the dynamic linker.  */
5467
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5468
0
          BFD_ASSERT (srelgot != NULL);
5469
5470
0
          outrel.r_offset = (elf_gp (info->output_bfd)
5471
0
           + sgot->output_offset + off);
5472
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5473
0
          outrel.r_addend = relocation;
5474
0
          loc = srelgot->contents;
5475
0
          loc +=
5476
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5477
0
          bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5478
0
          ++srelgot->reloc_count;
5479
0
        }
5480
0
      local_got_offsets[r_symndx] |= 1;
5481
0
    }
5482
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5483
0
         - elf_gp (info->output_bfd);
5484
0
      }
5485
5486
0
    break;
5487
5488
0
  case R_NDS32_16_RELA:
5489
0
  case R_NDS32_20_RELA:
5490
0
  case R_NDS32_5_RELA:
5491
0
  case R_NDS32_32_RELA:
5492
0
  case R_NDS32_9_PCREL_RELA:
5493
0
  case R_NDS32_WORD_9_PCREL_RELA:
5494
0
  case R_NDS32_10_UPCREL_RELA:
5495
0
  case R_NDS32_15_PCREL_RELA:
5496
0
  case R_NDS32_17_PCREL_RELA:
5497
0
  case R_NDS32_25_PCREL_RELA:
5498
0
  case R_NDS32_HI20_RELA:
5499
0
  case R_NDS32_LO12S3_RELA:
5500
0
  case R_NDS32_LO12S2_RELA:
5501
0
  case R_NDS32_LO12S2_DP_RELA:
5502
0
  case R_NDS32_LO12S2_SP_RELA:
5503
0
  case R_NDS32_LO12S1_RELA:
5504
0
  case R_NDS32_LO12S0_RELA:
5505
0
  case R_NDS32_LO12S0_ORI_RELA:
5506
0
    if (bfd_link_pic (info) && r_symndx != 0
5507
0
        && (input_section->flags & SEC_ALLOC) != 0
5508
0
        && (eliminate_gc_relocs == 0
5509
0
      || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5510
0
        && ((r_type != R_NDS32_9_PCREL_RELA
5511
0
       && r_type != R_NDS32_WORD_9_PCREL_RELA
5512
0
       && r_type != R_NDS32_10_UPCREL_RELA
5513
0
       && r_type != R_NDS32_15_PCREL_RELA
5514
0
       && r_type != R_NDS32_17_PCREL_RELA
5515
0
       && r_type != R_NDS32_25_PCREL_RELA
5516
0
       && !(r_type == R_NDS32_32_RELA
5517
0
      && strcmp (input_section->name, ".eh_frame") == 0))
5518
0
      || (h != NULL && h->dynindx != -1
5519
0
          && (!info->symbolic || !h->def_regular))))
5520
0
      {
5521
0
        Elf_Internal_Rela outrel;
5522
0
        bool skip, relocate;
5523
0
        bfd_byte *loc;
5524
5525
        /* When generating a shared object, these relocations
5526
     are copied into the output file to be resolved at run
5527
     time.  */
5528
5529
0
        if (sreloc == NULL)
5530
0
    {
5531
0
      const char *name;
5532
5533
0
      name = bfd_elf_string_from_elf_section
5534
0
        (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5535
0
         elf_section_data (input_section)->rela.hdr->sh_name);
5536
0
      if (name == NULL)
5537
0
        return false;
5538
5539
0
      BFD_ASSERT (startswith (name, ".rela")
5540
0
            && strcmp (bfd_section_name (input_section),
5541
0
           name + 5) == 0);
5542
5543
0
      sreloc = bfd_get_section_by_name (dynobj, name);
5544
0
      BFD_ASSERT (sreloc != NULL);
5545
0
    }
5546
5547
0
        skip = false;
5548
0
        relocate = false;
5549
5550
0
        outrel.r_offset = _bfd_elf_section_offset (info->output_bfd,
5551
0
               info,
5552
0
               input_section,
5553
0
               rel->r_offset);
5554
0
        if (outrel.r_offset == (bfd_vma) - 1)
5555
0
    skip = true;
5556
0
        else if (outrel.r_offset == (bfd_vma) - 2)
5557
0
    skip = true, relocate = true;
5558
0
        outrel.r_offset += (input_section->output_section->vma
5559
0
          + input_section->output_offset);
5560
5561
0
        if (skip)
5562
0
    memset (&outrel, 0, sizeof outrel);
5563
0
        else if (r_type == R_NDS32_17_PCREL_RELA
5564
0
           || r_type == R_NDS32_15_PCREL_RELA
5565
0
           || r_type == R_NDS32_25_PCREL_RELA)
5566
0
    {
5567
0
      BFD_ASSERT (h != NULL && h->dynindx != -1);
5568
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5569
0
      outrel.r_addend = rel->r_addend;
5570
0
    }
5571
0
        else
5572
0
    {
5573
      /* h->dynindx may be -1 if this symbol was marked to
5574
         become local.  */
5575
0
      if (h == NULL
5576
0
          || ((info->symbolic || h->dynindx == -1)
5577
0
        && h->def_regular)
5578
0
          || (bfd_link_pie (info) && h->def_regular))
5579
0
        {
5580
0
          relocate = true;
5581
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5582
0
          outrel.r_addend = relocation + rel->r_addend;
5583
5584
0
          if (h)
5585
0
      {
5586
0
        h->plt.offset = (bfd_vma) -1;   /* cancel PLT trampoline.  */
5587
5588
0
        BFD_ASSERT (sgot != NULL);
5589
        /* If we did not allocate got entry for the symbol,
5590
           we can not fill the nonexistent got entry.  */
5591
0
        if (h->got.offset != (bfd_vma) -1
5592
0
            && (h->got.offset & 1) == 0)
5593
0
          {
5594
0
            bfd_put_32 (info->output_bfd, outrel.r_addend,
5595
0
            sgot->contents + h->got.offset);
5596
0
          }
5597
0
      }
5598
0
        }
5599
0
      else
5600
0
        {
5601
0
          if (h->dynindx == -1)
5602
0
      {
5603
0
        _bfd_error_handler
5604
0
          (_("%pB: relocation %s against `%s' can not be used when "
5605
0
             "making a shared object; recompile with -fPIC"),
5606
0
           input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5607
0
        bfd_set_error (bfd_error_bad_value);
5608
0
        return false;
5609
0
      }
5610
5611
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5612
0
          outrel.r_addend = rel->r_addend;
5613
0
        }
5614
0
    }
5615
5616
0
        loc = sreloc->contents;
5617
0
        loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5618
0
        bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5619
0
        ++sreloc->reloc_count;
5620
5621
        /* If this reloc is against an external symbol, we do
5622
     not want to fiddle with the addend.  Otherwise, we
5623
     need to include the symbol value so that it becomes
5624
     an addend for the dynamic reloc.  */
5625
0
        if (!relocate)
5626
0
    continue;
5627
0
      }
5628
0
    break;
5629
5630
0
  case R_NDS32_25_ABS_RELA:
5631
0
    if (bfd_link_pic (info))
5632
0
      {
5633
0
        _bfd_error_handler
5634
0
    (_("%pB: warning: %s unsupported in shared mode"),
5635
0
     input_bfd, "R_NDS32_25_ABS_RELA");
5636
0
        return false;
5637
0
      }
5638
0
    break;
5639
5640
0
  case R_NDS32_9_PCREL:
5641
0
    r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5642
0
            contents, offset,
5643
0
            sec, relocation, addend);
5644
0
    goto check_reloc;
5645
5646
0
  case R_NDS32_HI20:
5647
    /* We allow an arbitrary number of HI20 relocs before the
5648
       LO12 reloc.  This permits gcc to emit the HI and LO relocs
5649
       itself.  */
5650
0
    for (lorel = rel + 1;
5651
0
         (lorel < relend
5652
0
    && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5653
0
      continue;
5654
0
    if (lorel < relend
5655
0
        && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5656
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5657
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5658
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5659
0
      {
5660
0
        nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5661
0
               contents, relocation + addend);
5662
0
        r = bfd_reloc_ok;
5663
0
      }
5664
0
    else
5665
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5666
0
            contents, offset, relocation,
5667
0
            addend);
5668
0
    goto check_reloc;
5669
5670
0
  case R_NDS32_GOT17S2_RELA:
5671
0
  case R_NDS32_GOT15S2_RELA:
5672
0
    BFD_ASSERT (sgot != NULL);
5673
5674
0
    if (h != NULL)
5675
0
      {
5676
0
        bool dyn;
5677
5678
0
        off = h->got.offset;
5679
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5680
5681
0
        dyn = htab->root.dynamic_sections_created;
5682
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5683
0
      (dyn, bfd_link_pic (info), h)
5684
0
      || (bfd_link_pic (info)
5685
0
          && (info->symbolic
5686
0
        || h->dynindx == -1
5687
0
        || h->forced_local)
5688
0
          && h->def_regular))
5689
0
    {
5690
      /* This is actually a static link, or it is a
5691
         -Bsymbolic link and the symbol is defined
5692
         locally, or the symbol was forced to be local
5693
         because of a version file.  We must initialize
5694
         this entry in the global offset table.  Since the
5695
         offset must always be a multiple of 4, we use the
5696
         least significant bit to record whether we have
5697
         initialized it already.
5698
5699
         When doing a dynamic link, we create a .rela.got
5700
         relocation entry to initialize the value.  This
5701
         is done in the finish_dynamic_symbol routine.  */
5702
0
      if ((off & 1) != 0)
5703
0
        off &= ~1;
5704
0
      else
5705
0
        {
5706
0
          bfd_put_32 (info->output_bfd, relocation,
5707
0
          sgot->contents + off);
5708
0
          h->got.offset |= 1;
5709
0
        }
5710
0
    }
5711
0
      }
5712
0
    else
5713
0
      {
5714
0
        bfd_byte *loc;
5715
5716
0
        BFD_ASSERT (local_got_offsets != NULL
5717
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5718
5719
0
        off = local_got_offsets[r_symndx];
5720
5721
        /* The offset must always be a multiple of 4.  We use
5722
     the least significant bit to record whether we have
5723
     already processed this entry.  */
5724
0
        if ((off & 1) != 0)
5725
0
    off &= ~1;
5726
0
        else
5727
0
    {
5728
0
      bfd_put_32 (info->output_bfd, relocation,
5729
0
            sgot->contents + off);
5730
5731
0
      if (bfd_link_pic (info))
5732
0
        {
5733
0
          asection *srelgot;
5734
0
          Elf_Internal_Rela outrel;
5735
5736
          /* We need to generate a R_NDS32_RELATIVE reloc
5737
       for the dynamic linker.  */
5738
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5739
0
          BFD_ASSERT (srelgot != NULL);
5740
5741
0
          outrel.r_offset = (elf_gp (info->output_bfd)
5742
0
           + sgot->output_offset + off);
5743
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5744
0
          outrel.r_addend = relocation;
5745
0
          loc = srelgot->contents;
5746
0
          loc +=
5747
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5748
0
          bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5749
0
          ++srelgot->reloc_count;
5750
0
        }
5751
0
      local_got_offsets[r_symndx] |= 1;
5752
0
    }
5753
0
      }
5754
0
    relocation = sgot->output_section->vma + sgot->output_offset + off
5755
0
      - elf_gp (info->output_bfd);
5756
5757
0
    if (relocation & align)
5758
0
      {
5759
        /* Incorrect alignment.  */
5760
0
        _bfd_error_handler
5761
0
    (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5762
0
        ret = false;
5763
0
        r = bfd_reloc_dangerous;
5764
0
        goto check_reloc;
5765
0
      }
5766
0
    break;
5767
5768
0
  case R_NDS32_SDA16S3_RELA:
5769
0
  case R_NDS32_SDA15S3_RELA:
5770
0
  case R_NDS32_SDA15S3:
5771
0
    align = 0x7;
5772
0
    goto handle_sda;
5773
5774
0
  case R_NDS32_SDA17S2_RELA:
5775
0
  case R_NDS32_SDA15S2_RELA:
5776
0
  case R_NDS32_SDA12S2_SP_RELA:
5777
0
  case R_NDS32_SDA12S2_DP_RELA:
5778
0
  case R_NDS32_SDA15S2:
5779
0
  case R_NDS32_SDA_FP7U2_RELA:
5780
0
    align = 0x3;
5781
0
    goto handle_sda;
5782
5783
0
  case R_NDS32_SDA18S1_RELA:
5784
0
  case R_NDS32_SDA15S1_RELA:
5785
0
  case R_NDS32_SDA15S1:
5786
0
    align = 0x1;
5787
0
    goto handle_sda;
5788
5789
0
  case R_NDS32_SDA19S0_RELA:
5790
0
  case R_NDS32_SDA15S0_RELA:
5791
0
  case R_NDS32_SDA15S0:
5792
0
    align = 0x0;
5793
0
  handle_sda:
5794
0
    BFD_ASSERT (sec != NULL);
5795
5796
    /* If the symbol is in the abs section, the out_bfd will be null.
5797
       This happens when the relocation has a symbol@GOTOFF.  */
5798
0
    r = nds32_elf_final_sda_base (info->output_bfd, info, &gp, false);
5799
0
    if (r != bfd_reloc_ok)
5800
0
      {
5801
0
        _bfd_error_handler
5802
0
    (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5803
0
        ret = false;
5804
0
        goto check_reloc;
5805
0
      }
5806
5807
    /* At this point `relocation' contains the object's
5808
       address.  */
5809
0
    if (r_type == R_NDS32_SDA_FP7U2_RELA)
5810
0
      {
5811
0
        relocation -= fpbase_addr;
5812
0
      }
5813
0
    else
5814
0
      relocation -= gp;
5815
    /* Now it contains the offset from _SDA_BASE_.  */
5816
5817
    /* Make sure alignment is correct.  */
5818
5819
0
    if (relocation & align)
5820
0
      {
5821
        /* Incorrect alignment.  */
5822
0
        _bfd_error_handler
5823
    /* xgettext:c-format */
5824
0
    (_("%pB(%pA): warning: unaligned small data access"
5825
0
       " of type %d"),
5826
0
     input_bfd, input_section, r_type);
5827
0
        ret = false;
5828
0
        goto check_reloc;
5829
0
      }
5830
0
    break;
5831
5832
0
  case R_NDS32_17IFC_PCREL_RELA:
5833
0
  case R_NDS32_10IFCU_PCREL_RELA:
5834
    /* Do nothing.  */
5835
0
    break;
5836
5837
0
  case R_NDS32_TLS_LE_HI20:
5838
0
  case R_NDS32_TLS_LE_LO12:
5839
0
  case R_NDS32_TLS_LE_20:
5840
0
  case R_NDS32_TLS_LE_15S0:
5841
0
  case R_NDS32_TLS_LE_15S1:
5842
0
  case R_NDS32_TLS_LE_15S2:
5843
    /* We do not have garbage collection for got entries.
5844
       Therefore, IE to LE may have one empty entry, and DESC to
5845
       LE may have two.  */
5846
0
    if (elf_hash_table (info)->tls_sec != NULL)
5847
0
      relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5848
0
    break;
5849
5850
0
  case R_NDS32_TLS_IE_HI20:
5851
0
  case R_NDS32_TLS_IE_LO12S2:
5852
0
  case R_NDS32_TLS_DESC_HI20:
5853
0
  case R_NDS32_TLS_DESC_LO12:
5854
0
  case R_NDS32_TLS_IE_LO12:
5855
0
  case R_NDS32_TLS_IEGP_HI20:
5856
0
  case R_NDS32_TLS_IEGP_LO12:
5857
0
  case R_NDS32_TLS_IEGP_LO12S2:
5858
0
    {
5859
      /* Relocation is to the entry for this symbol in the global
5860
         offset table.  */
5861
0
      enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5862
0
      asection *srelgot;
5863
0
      Elf_Internal_Rela outrel;
5864
0
      bfd_byte *loc;
5865
0
      int indx = 0;
5866
5867
0
      eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5868
5869
0
      BFD_ASSERT (sgot != NULL);
5870
0
      if (h != NULL)
5871
0
        {
5872
0
    bool dyn;
5873
5874
0
    off = h->got.offset;
5875
0
    BFD_ASSERT (off != (bfd_vma) -1);
5876
0
    dyn = htab->root.dynamic_sections_created;
5877
0
    tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5878
0
    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5879
0
        && (!bfd_link_pic (info)
5880
0
      || !SYMBOL_REFERENCES_LOCAL (info, h)))
5881
0
      indx = h->dynindx;
5882
0
        }
5883
0
      else
5884
0
        {
5885
0
    BFD_ASSERT (local_got_offsets != NULL
5886
0
          && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5887
0
    off = local_got_offsets[r_symndx];
5888
0
    tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5889
0
        }
5890
5891
0
      relocation = sgot->output_section->vma + sgot->output_offset + off;
5892
5893
0
      if (1 < ones32 (tls_type))
5894
0
        {
5895
0
    eff_tls_type = 1 << (fls (tls_type) - 1);
5896
    /* TLS model shall be handled in nds32_elf_unify_tls_model ().  */
5897
5898
    /* TLS model X -> LE is not implement yet!
5899
       workaround here!  */
5900
0
    if (eff_tls_type == GOT_TLS_LE)
5901
0
      {
5902
0
        eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5903
0
      }
5904
0
        }
5905
5906
      /* The offset must always be a multiple of 4.  We use
5907
         the least significant bit to record whether we have
5908
         already processed this entry.  */
5909
0
      bool need_relocs = false;
5910
0
      srelgot = ehtab->srelgot;
5911
0
      if ((bfd_link_pic (info) || indx != 0)
5912
0
    && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5913
0
        || h->root.type != bfd_link_hash_undefweak))
5914
0
        {
5915
0
    need_relocs = true;
5916
0
    BFD_ASSERT (srelgot != NULL);
5917
0
        }
5918
5919
0
      if (off & 1)
5920
0
        {
5921
0
    off &= ~1;
5922
0
    relocation &= ~1;
5923
5924
0
    if (eff_tls_type & GOT_TLS_DESC)
5925
0
      {
5926
0
        relocation -= elf_gp (info->output_bfd);
5927
0
        if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5928
0
          {
5929
      /* TLS model shall be converted.  */
5930
0
      BFD_ASSERT(0);
5931
0
          }
5932
0
      }
5933
0
    else if (eff_tls_type & GOT_TLS_IEGP)
5934
0
      {
5935
0
        relocation -= elf_gp (info->output_bfd);
5936
0
      }
5937
0
        }
5938
0
      else
5939
0
        {
5940
0
    if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5941
0
      {
5942
        /* TLS model workaround shall be applied.  */
5943
0
        BFD_ASSERT(0);
5944
0
      }
5945
0
    else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5946
0
      {
5947
0
        if (eff_tls_type & GOT_TLS_IEGP)
5948
0
          relocation -= elf_gp(info->output_bfd);
5949
5950
0
        if (need_relocs)
5951
0
          {
5952
0
      if (indx == 0)
5953
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5954
0
      else
5955
0
        outrel.r_addend = 0;
5956
0
      outrel.r_offset = (sgot->output_section->vma
5957
0
             + sgot->output_offset + off);
5958
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5959
5960
0
      elf32_nds32_add_dynreloc (info->output_bfd, info,
5961
0
              srelgot, &outrel);
5962
0
          }
5963
0
        else
5964
0
          {
5965
0
      bfd_put_32 (info->output_bfd,
5966
0
            gottpoff (info, relocation_sym),
5967
0
            sgot->contents + off);
5968
0
          }
5969
0
      }
5970
0
    else if (eff_tls_type & GOT_TLS_DESC)
5971
0
      {
5972
0
        relocation -= elf_gp (info->output_bfd);
5973
0
        if (need_relocs)
5974
0
          {
5975
0
      if (indx == 0)
5976
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5977
0
      else
5978
0
        outrel.r_addend = 0;
5979
0
      outrel.r_offset = (sgot->output_section->vma
5980
0
             + sgot->output_offset + off);
5981
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5982
5983
0
      if (htab->tls_desc_trampoline)
5984
0
        {
5985
0
          asection *srelplt;
5986
0
          srelplt = ehtab->srelplt;
5987
0
          loc = srelplt->contents;
5988
0
          loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5989
0
          BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5990
0
          <= srelplt->contents + srelplt->size);
5991
5992
0
          bfd_elf32_swap_reloca_out (info->output_bfd,
5993
0
                   &outrel, loc);
5994
0
        }
5995
0
      else
5996
0
        {
5997
0
          loc = srelgot->contents;
5998
0
          loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5999
0
          bfd_elf32_swap_reloca_out (info->output_bfd,
6000
0
                   &outrel, loc);
6001
0
          ++srelgot->reloc_count;
6002
0
        }
6003
0
          }
6004
0
        else
6005
0
          {
6006
      /* feed me!  */
6007
0
      bfd_put_32 (info->output_bfd, 0xdeadbeef,
6008
0
            sgot->contents + off);
6009
0
      bfd_put_32 (info->output_bfd,
6010
0
            gottpoff (info, relocation_sym),
6011
0
            sgot->contents + off + 4);
6012
0
      patch_tls_desc_to_ie (contents, rel, input_bfd);
6013
0
      BFD_ASSERT(0);
6014
0
          }
6015
0
      }
6016
0
    else
6017
0
      {
6018
        /* TLS model workaround shall be applied.  */
6019
0
        BFD_ASSERT(0);
6020
0
      }
6021
6022
0
    if (h != NULL)
6023
0
      h->got.offset |= 1;
6024
0
    else
6025
0
      local_got_offsets[r_symndx] |= 1;
6026
0
        }
6027
0
    }
6028
0
  break;
6029
    /* DON'T fall through.  */
6030
6031
0
  default:
6032
    /* OLD_NDS32_RELOC.  */
6033
6034
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6035
0
          contents, offset, relocation, addend);
6036
0
    goto check_reloc;
6037
0
  }
6038
6039
0
      switch ((int) r_type)
6040
0
  {
6041
0
  case R_NDS32_20_RELA:
6042
0
  case R_NDS32_5_RELA:
6043
0
  case R_NDS32_9_PCREL_RELA:
6044
0
  case R_NDS32_WORD_9_PCREL_RELA:
6045
0
  case R_NDS32_10_UPCREL_RELA:
6046
0
  case R_NDS32_15_PCREL_RELA:
6047
0
  case R_NDS32_17_PCREL_RELA:
6048
0
  case R_NDS32_25_PCREL_RELA:
6049
0
  case R_NDS32_25_ABS_RELA:
6050
0
  case R_NDS32_HI20_RELA:
6051
0
  case R_NDS32_LO12S3_RELA:
6052
0
  case R_NDS32_LO12S2_RELA:
6053
0
  case R_NDS32_LO12S2_DP_RELA:
6054
0
  case R_NDS32_LO12S2_SP_RELA:
6055
0
  case R_NDS32_LO12S1_RELA:
6056
0
  case R_NDS32_LO12S0_RELA:
6057
0
  case R_NDS32_LO12S0_ORI_RELA:
6058
0
  case R_NDS32_SDA16S3_RELA:
6059
0
  case R_NDS32_SDA17S2_RELA:
6060
0
  case R_NDS32_SDA18S1_RELA:
6061
0
  case R_NDS32_SDA19S0_RELA:
6062
0
  case R_NDS32_SDA15S3_RELA:
6063
0
  case R_NDS32_SDA15S2_RELA:
6064
0
  case R_NDS32_SDA12S2_DP_RELA:
6065
0
  case R_NDS32_SDA12S2_SP_RELA:
6066
0
  case R_NDS32_SDA15S1_RELA:
6067
0
  case R_NDS32_SDA15S0_RELA:
6068
0
  case R_NDS32_SDA_FP7U2_RELA:
6069
0
  case R_NDS32_9_PLTREL:
6070
0
  case R_NDS32_25_PLTREL:
6071
0
  case R_NDS32_GOT20:
6072
0
  case R_NDS32_GOT_HI20:
6073
0
  case R_NDS32_GOT_LO12:
6074
0
  case R_NDS32_GOT_LO15:
6075
0
  case R_NDS32_GOT_LO19:
6076
0
  case R_NDS32_GOT15S2_RELA:
6077
0
  case R_NDS32_GOT17S2_RELA:
6078
0
  case R_NDS32_GOTPC20:
6079
0
  case R_NDS32_GOTPC_HI20:
6080
0
  case R_NDS32_GOTPC_LO12:
6081
0
  case R_NDS32_GOTOFF:
6082
0
  case R_NDS32_GOTOFF_HI20:
6083
0
  case R_NDS32_GOTOFF_LO12:
6084
0
  case R_NDS32_GOTOFF_LO15:
6085
0
  case R_NDS32_GOTOFF_LO19:
6086
0
  case R_NDS32_PLTREL_HI20:
6087
0
  case R_NDS32_PLTREL_LO12:
6088
0
  case R_NDS32_PLT_GOTREL_HI20:
6089
0
  case R_NDS32_PLT_GOTREL_LO12:
6090
0
  case R_NDS32_PLT_GOTREL_LO15:
6091
0
  case R_NDS32_PLT_GOTREL_LO19:
6092
0
  case R_NDS32_PLT_GOTREL_LO20:
6093
0
  case R_NDS32_17IFC_PCREL_RELA:
6094
0
  case R_NDS32_10IFCU_PCREL_RELA:
6095
0
  case R_NDS32_TLS_LE_HI20:
6096
0
  case R_NDS32_TLS_LE_LO12:
6097
0
  case R_NDS32_TLS_IE_HI20:
6098
0
  case R_NDS32_TLS_IE_LO12S2:
6099
0
  case R_NDS32_TLS_LE_20:
6100
0
  case R_NDS32_TLS_LE_15S0:
6101
0
  case R_NDS32_TLS_LE_15S1:
6102
0
  case R_NDS32_TLS_LE_15S2:
6103
0
  case R_NDS32_TLS_DESC_HI20:
6104
0
  case R_NDS32_TLS_DESC_LO12:
6105
0
  case R_NDS32_TLS_IE_LO12:
6106
0
  case R_NDS32_TLS_IEGP_HI20:
6107
0
  case R_NDS32_TLS_IEGP_LO12:
6108
0
  case R_NDS32_TLS_IEGP_LO12S2:
6109
    /* Instruction related relocs must handle endian properly.  */
6110
    /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
6111
0
    r = nds32_elf_final_link_relocate (howto, input_bfd,
6112
0
               input_section, contents,
6113
0
               rel->r_offset, relocation,
6114
0
               rel->r_addend);
6115
0
    break;
6116
6117
0
  default:
6118
    /* All other relocs can use default handler.  */
6119
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6120
0
          contents, rel->r_offset,
6121
0
          relocation, rel->r_addend);
6122
0
    break;
6123
0
  }
6124
6125
0
    check_reloc:
6126
6127
0
      if (r != bfd_reloc_ok)
6128
0
  {
6129
    /* FIXME: This should be generic enough to go in a utility.  */
6130
0
    const char *name;
6131
6132
0
    if (h != NULL)
6133
0
      name = h->root.root.string;
6134
0
    else
6135
0
      {
6136
0
        name = bfd_elf_string_from_elf_section
6137
0
          (input_bfd, symtab_hdr->sh_link, sym->st_name);
6138
0
        if (name == NULL || *name == '\0')
6139
0
    name = bfd_section_name (sec);
6140
0
      }
6141
6142
0
    if (errmsg != NULL)
6143
0
      goto common_error;
6144
6145
0
    switch (r)
6146
0
      {
6147
0
      case bfd_reloc_overflow:
6148
0
        (*info->callbacks->reloc_overflow)
6149
0
    (info, (h ? &h->root : NULL), name, howto->name,
6150
0
     (bfd_vma) 0, input_bfd, input_section, offset);
6151
0
        break;
6152
6153
0
      case bfd_reloc_undefined:
6154
0
        (*info->callbacks->undefined_symbol)
6155
0
    (info, name, input_bfd, input_section, offset, true);
6156
0
        break;
6157
6158
0
      case bfd_reloc_outofrange:
6159
0
        errmsg = _("internal error: out of range error");
6160
0
        goto common_error;
6161
6162
0
      case bfd_reloc_notsupported:
6163
0
        errmsg = _("internal error: unsupported relocation error");
6164
0
        goto common_error;
6165
6166
0
      case bfd_reloc_dangerous:
6167
0
        errmsg = _("internal error: dangerous error");
6168
0
        goto common_error;
6169
6170
0
      default:
6171
0
        errmsg = _("internal error: unknown error");
6172
        /* Fall through.  */
6173
6174
0
      common_error:
6175
0
        (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6176
0
             input_section, offset);
6177
0
        break;
6178
0
      }
6179
0
  }
6180
0
    }
6181
6182
  /* Resotre header size to avoid overflow load.  */
6183
0
  if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6184
0
    symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6185
6186
0
  return ret;
6187
0
}
6188
6189
/* Finish up dynamic symbol handling.  We set the contents of various
6190
   dynamic sections here.  */
6191
6192
static bool
6193
nds32_elf_finish_dynamic_symbol (struct bfd_link_info *info,
6194
         struct elf_link_hash_entry *h,
6195
         Elf_Internal_Sym *sym)
6196
0
{
6197
0
  struct elf_link_hash_table *ehtab;
6198
0
  struct elf_nds32_link_hash_entry *hent;
6199
0
  bfd_byte *loc;
6200
6201
0
  ehtab = elf_hash_table (info);
6202
0
  hent = (struct elf_nds32_link_hash_entry *) h;
6203
6204
0
  if (h->plt.offset != (bfd_vma) - 1)
6205
0
    {
6206
0
      asection *splt;
6207
0
      asection *sgot;
6208
0
      asection *srela;
6209
6210
0
      bfd_vma plt_index;
6211
0
      bfd_vma got_offset;
6212
0
      bfd_vma local_plt_offset;
6213
0
      Elf_Internal_Rela rela;
6214
6215
      /* This symbol has an entry in the procedure linkage table.  Set
6216
   it up.  */
6217
6218
0
      BFD_ASSERT (h->dynindx != -1);
6219
6220
0
      splt = ehtab->splt;
6221
0
      sgot = ehtab->sgotplt;
6222
0
      srela = ehtab->srelplt;
6223
0
      BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6224
6225
      /* Get the index in the procedure linkage table which
6226
   corresponds to this symbol.  This is the index of this symbol
6227
   in all the symbols for which we are making plt entries.  The
6228
   first entry in the procedure linkage table is reserved.  */
6229
0
      plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6230
6231
      /* Get the offset into the .got table of the entry that
6232
   corresponds to this function.  Each .got entry is 4 bytes.
6233
   The first three are reserved.  */
6234
0
      got_offset = (plt_index + 3) * 4;
6235
6236
      /* Fill in the entry in the procedure linkage table.  */
6237
0
      if (!bfd_link_pic (info))
6238
0
  {
6239
0
    unsigned long insn;
6240
6241
0
    insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6242
0
              + sgot->output_offset + got_offset) >> 12)
6243
0
            & 0xfffff);
6244
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6245
6246
0
    insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6247
0
              + sgot->output_offset + got_offset) & 0x0fff)
6248
0
            >> 2);
6249
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6250
6251
0
    insn = PLT_ENTRY_WORD2;
6252
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6253
6254
0
    insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6255
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6256
6257
0
    insn = PLT_ENTRY_WORD4
6258
0
     + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6259
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6260
0
    local_plt_offset = 12;
6261
0
  }
6262
0
      else
6263
0
  {
6264
    /* sda_base must be set at this time.  */
6265
0
    unsigned long insn;
6266
0
    long offset;
6267
6268
0
    offset = sgot->output_section->vma + sgot->output_offset + got_offset
6269
0
       - elf_gp (info->output_bfd);
6270
0
    insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6271
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6272
6273
0
    insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6274
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6275
6276
0
    insn = PLT_PIC_ENTRY_WORD2;
6277
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6278
6279
0
    insn = PLT_PIC_ENTRY_WORD3;
6280
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6281
6282
0
    insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6283
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6284
6285
0
    insn = PLT_PIC_ENTRY_WORD5
6286
0
      + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6287
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6288
6289
0
    local_plt_offset = 16;
6290
0
  }
6291
6292
      /* Fill in the entry in the global offset table,
6293
   so it will fall through to the next instruction for the first time.  */
6294
0
      bfd_put_32 (info->output_bfd,
6295
0
      (splt->output_section->vma + splt->output_offset
6296
0
       + h->plt.offset + local_plt_offset),
6297
0
      sgot->contents + got_offset);
6298
6299
      /* Fill in the entry in the .rela.plt section.  */
6300
0
      rela.r_offset = (sgot->output_section->vma
6301
0
           + sgot->output_offset + got_offset);
6302
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6303
0
      rela.r_addend = 0;
6304
0
      loc = srela->contents;
6305
0
      loc += plt_index * sizeof (Elf32_External_Rela);
6306
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6307
6308
0
      if (!h->def_regular)
6309
0
  {
6310
    /* Mark the symbol as undefined, rather than as defined in
6311
       the .plt section.  Leave the value alone.  */
6312
0
    sym->st_shndx = SHN_UNDEF;
6313
0
    if (!h->ref_regular_nonweak)
6314
0
      sym->st_value = 0;
6315
0
  }
6316
0
    }
6317
6318
0
  if (h->got.offset != (bfd_vma) - 1
6319
0
      && hent->tls_type == GOT_NORMAL)
6320
0
    {
6321
0
      asection *sgot;
6322
0
      asection *srelagot;
6323
0
      Elf_Internal_Rela rela;
6324
6325
      /* This symbol has an entry in the global offset table.
6326
   Set it up.  */
6327
6328
0
      sgot = ehtab->sgot;
6329
0
      srelagot = ehtab->srelgot;
6330
0
      BFD_ASSERT (sgot != NULL && srelagot != NULL);
6331
6332
0
      rela.r_offset = (sgot->output_section->vma
6333
0
           + sgot->output_offset + (h->got.offset & ~1));
6334
6335
      /* If this is a -Bsymbolic link, and the symbol is defined
6336
   locally, we just want to emit a RELATIVE reloc.  Likewise if
6337
   the symbol was forced to be local because of a version file.
6338
   The entry in the global offset table will already have been
6339
   initialized in the relocate_section function.  */
6340
0
      if ((bfd_link_pic (info)
6341
0
     && (info->symbolic || h->dynindx == -1 || h->forced_local)
6342
0
     && h->def_regular)
6343
0
    || (bfd_link_pie (info) && h->def_regular))
6344
0
  {
6345
0
    rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6346
0
    rela.r_addend = (h->root.u.def.value
6347
0
         + h->root.u.def.section->output_section->vma
6348
0
         + h->root.u.def.section->output_offset);
6349
6350
0
    if ((h->got.offset & 1) == 0)
6351
0
      {
6352
0
        bfd_put_32 (info->output_bfd, rela.r_addend,
6353
0
        sgot->contents + h->got.offset);
6354
0
      }
6355
0
  }
6356
0
      else
6357
0
  {
6358
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
6359
0
    bfd_put_32 (info->output_bfd, (bfd_vma) 0,
6360
0
          sgot->contents + h->got.offset);
6361
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6362
0
    rela.r_addend = 0;
6363
0
  }
6364
6365
0
      loc = srelagot->contents;
6366
0
      loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6367
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6368
0
      ++srelagot->reloc_count;
6369
0
      BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6370
0
    }
6371
6372
0
  if (h->needs_copy)
6373
0
    {
6374
0
      asection *s;
6375
0
      Elf_Internal_Rela rela;
6376
6377
      /* This symbols needs a copy reloc.  Set it up.  */
6378
6379
0
      BFD_ASSERT (h->dynindx != -1
6380
0
      && (h->root.type == bfd_link_hash_defined
6381
0
          || h->root.type == bfd_link_hash_defweak));
6382
6383
0
      s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6384
0
      BFD_ASSERT (s != NULL);
6385
6386
0
      rela.r_offset = (h->root.u.def.value
6387
0
           + h->root.u.def.section->output_section->vma
6388
0
           + h->root.u.def.section->output_offset);
6389
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6390
0
      rela.r_addend = 0;
6391
0
      loc = s->contents;
6392
0
      loc += s->reloc_count * sizeof (Elf32_External_Rela);
6393
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6394
0
      ++s->reloc_count;
6395
0
    }
6396
6397
  /* Mark some specially defined symbols as absolute.  */
6398
0
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6399
0
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6400
0
    sym->st_shndx = SHN_ABS;
6401
6402
0
  return true;
6403
0
}
6404
6405
6406
/* Finish up the dynamic sections.  */
6407
6408
static bool
6409
nds32_elf_finish_dynamic_sections (struct bfd_link_info *info,
6410
           bfd_byte *buf ATTRIBUTE_UNUSED)
6411
0
{
6412
0
  bfd *dynobj;
6413
0
  asection *sdyn;
6414
0
  asection *sgotplt;
6415
0
  struct elf_link_hash_table *ehtab;
6416
0
  struct elf_nds32_link_hash_table *htab;
6417
6418
0
  ehtab = elf_hash_table (info);
6419
0
  htab = nds32_elf_hash_table (info);
6420
0
  if (htab == NULL)
6421
0
    return false;
6422
6423
0
  dynobj = elf_hash_table (info)->dynobj;
6424
6425
0
  sgotplt = ehtab->sgotplt;
6426
  /* A broken linker script might have discarded the dynamic sections.
6427
     Catch this here so that we do not seg-fault later on.  */
6428
0
  if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6429
0
    return false;
6430
0
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6431
6432
0
  if (elf_hash_table (info)->dynamic_sections_created)
6433
0
    {
6434
0
      asection *splt;
6435
0
      Elf32_External_Dyn *dyncon, *dynconend;
6436
6437
0
      BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6438
6439
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
6440
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6441
6442
0
      for (; dyncon < dynconend; dyncon++)
6443
0
  {
6444
0
    Elf_Internal_Dyn dyn;
6445
0
    asection *s;
6446
6447
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6448
6449
0
    switch (dyn.d_tag)
6450
0
      {
6451
0
      default:
6452
0
        break;
6453
6454
0
      case DT_PLTGOT:
6455
        /* name = ".got";  */
6456
0
        s = ehtab->sgot;
6457
0
        goto get_vma;
6458
0
      case DT_JMPREL:
6459
0
        s = ehtab->srelplt;
6460
0
      get_vma:
6461
0
        BFD_ASSERT (s != NULL);
6462
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6463
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6464
0
        break;
6465
6466
0
      case DT_PLTRELSZ:
6467
0
        s = ehtab->srelplt;
6468
0
        BFD_ASSERT (s != NULL);
6469
0
        dyn.d_un.d_val = s->size;
6470
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6471
0
        break;
6472
6473
0
      case DT_RELASZ:
6474
        /* My reading of the SVR4 ABI indicates that the
6475
     procedure linkage table relocs (DT_JMPREL) should be
6476
     included in the overall relocs (DT_RELA).  This is
6477
     what Solaris does.  However, UnixWare can not handle
6478
     that case.  Therefore, we override the DT_RELASZ entry
6479
     here to make it not include the JMPREL relocs.  Since
6480
     the linker script arranges for .rela.plt to follow all
6481
     other relocation sections, we don't have to worry
6482
     about changing the DT_RELA entry.  */
6483
0
        if (ehtab->srelplt != NULL)
6484
0
    {
6485
0
      s = ehtab->srelplt;
6486
0
      dyn.d_un.d_val -= s->size;
6487
0
    }
6488
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6489
0
        break;
6490
6491
0
      case DT_TLSDESC_PLT:
6492
0
        s = htab->root.splt;
6493
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6494
0
        + htab->root.tlsdesc_plt);
6495
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6496
0
        break;
6497
6498
0
      case DT_TLSDESC_GOT:
6499
0
        s = htab->root.sgot;
6500
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6501
0
        + htab->root.tlsdesc_got);
6502
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6503
0
        break;
6504
0
      }
6505
0
  }
6506
6507
      /* Fill in the first entry in the procedure linkage table.  */
6508
0
      splt = ehtab->splt;
6509
0
      if (splt && splt->size > 0)
6510
0
  {
6511
0
    if (bfd_link_pic (info))
6512
0
      {
6513
0
        unsigned long insn;
6514
0
        long offset;
6515
6516
0
        offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6517
0
    - elf_gp (info->output_bfd);
6518
0
        insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6519
0
        bfd_putb32 (insn, splt->contents);
6520
6521
        /* here has a typo?  */
6522
0
        insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6523
0
        bfd_putb32 (insn, splt->contents + 4);
6524
6525
0
        insn = PLT0_PIC_ENTRY_WORD2;
6526
0
        bfd_putb32 (insn, splt->contents + 8);
6527
6528
0
        insn = PLT0_PIC_ENTRY_WORD3;
6529
0
        bfd_putb32 (insn, splt->contents + 12);
6530
6531
0
        insn = PLT0_PIC_ENTRY_WORD4;
6532
0
        bfd_putb32 (insn, splt->contents + 16);
6533
6534
0
        insn = PLT0_PIC_ENTRY_WORD5;
6535
0
        bfd_putb32 (insn, splt->contents + 20);
6536
0
      }
6537
0
    else
6538
0
      {
6539
0
        unsigned long insn;
6540
0
        unsigned long addr;
6541
6542
        /* addr = .got + 4 */
6543
0
        addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6544
0
        insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6545
0
        bfd_putb32 (insn, splt->contents);
6546
6547
0
        insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6548
0
        bfd_putb32 (insn, splt->contents + 4);
6549
6550
0
        insn = PLT0_ENTRY_WORD2;
6551
0
        bfd_putb32 (insn, splt->contents + 8);
6552
6553
0
        insn = PLT0_ENTRY_WORD3;
6554
0
        bfd_putb32 (insn, splt->contents + 12);
6555
6556
0
        insn = PLT0_ENTRY_WORD4;
6557
0
        bfd_putb32 (insn, splt->contents + 16);
6558
0
      }
6559
6560
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6561
0
      PLT_ENTRY_SIZE;
6562
0
  }
6563
6564
0
      if (htab->root.tlsdesc_plt)
6565
0
  {
6566
    /* Calculate addresses.  */
6567
0
    asection *sgot = sgot = ehtab->sgot;
6568
0
    bfd_vma pltgot = sgotplt->output_section->vma
6569
0
      + sgotplt->output_offset;
6570
0
    bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6571
0
      + htab->root.tlsdesc_got;
6572
6573
    /* Get GP offset.  */
6574
0
    pltgot -= elf_gp (info->output_bfd) - 4; /* PLTGOT[1]  */
6575
0
    tlsdesc_got -= elf_gp (info->output_bfd);
6576
6577
    /* Do relocation.  */
6578
0
    dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6579
0
    dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6580
0
    dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6581
0
    dl_tlsdesc_lazy_trampoline[5] +=  0xfff & pltgot;
6582
6583
    /* Insert .plt.  */
6584
0
    nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
6585
0
        dl_tlsdesc_lazy_trampoline,
6586
0
        ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6587
0
  }
6588
0
    }
6589
6590
  /* Fill in the first three entries in the global offset table.  */
6591
0
  if (sgotplt && sgotplt->size > 0)
6592
0
    {
6593
0
      if (sdyn == NULL)
6594
0
  bfd_put_32 (info->output_bfd, 0, sgotplt->contents);
6595
0
      else
6596
0
  bfd_put_32 (info->output_bfd,
6597
0
        sdyn->output_section->vma + sdyn->output_offset,
6598
0
        sgotplt->contents);
6599
0
      bfd_put_32 (info->output_bfd, 0, sgotplt->contents + 4);
6600
0
      bfd_put_32 (info->output_bfd, 0, sgotplt->contents + 8);
6601
6602
0
      elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6603
0
    }
6604
6605
0
  return true;
6606
0
}
6607

6608
6609
/* Set the right machine number.  */
6610
6611
static bool
6612
nds32_elf_object_p (bfd *abfd)
6613
1.76k
{
6614
1.76k
  static unsigned int cur_arch = 0;
6615
6616
1.76k
  if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6617
1.73k
    {
6618
      /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
6619
1.73k
      cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6620
1.73k
    }
6621
6622
1.76k
  switch (cur_arch)
6623
1.76k
    {
6624
1.62k
    default:
6625
1.62k
    case E_N1_ARCH:
6626
1.62k
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6627
1.62k
      break;
6628
9
    case E_N1H_ARCH:
6629
9
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6630
9
      break;
6631
114
    case E_NDS_ARCH_STAR_V2_0:
6632
114
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6633
114
      break;
6634
3
    case E_NDS_ARCH_STAR_V3_0:
6635
3
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6636
3
      break;
6637
12
    case E_NDS_ARCH_STAR_V3_M:
6638
12
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6639
12
      break;
6640
1.76k
    }
6641
6642
1.76k
  return true;
6643
1.76k
}
6644
6645
/* Store the machine number in the flags field.  */
6646
6647
static bool
6648
nds32_elf_final_write_processing (bfd *abfd)
6649
7
{
6650
7
  unsigned long val;
6651
7
  static unsigned int cur_mach = 0;
6652
6653
7
  if (bfd_mach_n1 != bfd_get_mach (abfd))
6654
1
    {
6655
1
      cur_mach = bfd_get_mach (abfd);
6656
1
    }
6657
6658
7
  switch (cur_mach)
6659
7
    {
6660
0
    case bfd_mach_n1:
6661
      /* Only happen when object is empty, since the case is abandon.  */
6662
0
      val = E_N1_ARCH;
6663
0
      val |= E_NDS_ABI_AABI;
6664
0
      val |= E_NDS32_ELF_VER_1_4;
6665
0
      break;
6666
0
    case bfd_mach_n1h:
6667
0
      val = E_N1H_ARCH;
6668
0
      break;
6669
0
    case bfd_mach_n1h_v2:
6670
0
      val = E_NDS_ARCH_STAR_V2_0;
6671
0
      break;
6672
0
    case bfd_mach_n1h_v3:
6673
0
      val = E_NDS_ARCH_STAR_V3_0;
6674
0
      break;
6675
6
    case bfd_mach_n1h_v3m:
6676
6
      val = E_NDS_ARCH_STAR_V3_M;
6677
6
      break;
6678
1
    default:
6679
1
      val = 0;
6680
1
      break;
6681
7
    }
6682
6683
7
  elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6684
7
  elf_elfheader (abfd)->e_flags |= val;
6685
7
  return _bfd_elf_final_write_processing (abfd);
6686
7
}
6687
6688
/* Function to keep NDS32 specific file flags.  */
6689
6690
static bool
6691
nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6692
0
{
6693
0
  BFD_ASSERT (!elf_flags_init (abfd)
6694
0
        || elf_elfheader (abfd)->e_flags == flags);
6695
6696
0
  elf_elfheader (abfd)->e_flags = flags;
6697
0
  elf_flags_init (abfd) = true;
6698
0
  return true;
6699
0
}
6700
6701
static unsigned int
6702
convert_e_flags (unsigned int e_flags, unsigned int arch)
6703
0
{
6704
0
  if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6705
0
    {
6706
      /* From 0.9 to 1.0.  */
6707
0
      e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6708
6709
      /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6710
0
      e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6711
0
      if (arch == E_NDS_ARCH_STAR_V1_0)
6712
0
  {
6713
    /* Done.  */
6714
0
    return e_flags;
6715
0
  }
6716
0
    }
6717
6718
  /* From 1.0 to 2.0.  */
6719
0
  e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6720
6721
  /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
6722
0
  e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6723
6724
  /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6725
0
  e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6726
0
  return e_flags;
6727
0
}
6728
6729
static bool
6730
nds32_check_vec_size (bfd *ibfd)
6731
0
{
6732
0
  static unsigned int nds32_vec_size = 0;
6733
6734
0
  asection *sec_t = NULL;
6735
0
  bfd_byte *contents = NULL;
6736
6737
0
  sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6738
6739
0
  if (sec_t && sec_t->size >= 4)
6740
0
    {
6741
      /* Get vec_size in file.  */
6742
0
      unsigned int flag_t;
6743
6744
0
      nds32_get_section_contents (ibfd, sec_t, &contents, true);
6745
0
      flag_t = bfd_get_32 (ibfd, contents);
6746
6747
      /* The value could only be 4 or 16.  */
6748
6749
0
      if (!nds32_vec_size)
6750
  /* Set if not set yet.  */
6751
0
  nds32_vec_size = (flag_t & 0x3);
6752
0
      else if (nds32_vec_size != (flag_t & 0x3))
6753
0
  {
6754
0
    _bfd_error_handler
6755
      /* xgettext:c-format */
6756
0
      (_("%pB: ISR vector size mismatch"
6757
0
         " with previous modules, previous %u-byte, current %u-byte"),
6758
0
       ibfd,
6759
0
       nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6760
0
       (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6761
0
    return false;
6762
0
  }
6763
0
      else
6764
  /* Only keep the first vec_size section.  */
6765
0
  sec_t->flags |= SEC_EXCLUDE;
6766
0
    }
6767
6768
0
  return true;
6769
0
}
6770
6771
/* Merge backend specific data from an object file to the output
6772
   object file when linking.  */
6773
6774
static bool
6775
nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6776
0
{
6777
0
  bfd *obfd = info->output_bfd;
6778
0
  flagword out_flags;
6779
0
  flagword in_flags;
6780
0
  flagword out_16regs;
6781
0
  flagword in_no_mac;
6782
0
  flagword out_no_mac;
6783
0
  flagword in_16regs;
6784
0
  flagword out_version;
6785
0
  flagword in_version;
6786
0
  flagword out_fpu_config;
6787
0
  flagword in_fpu_config;
6788
6789
  /* FIXME: What should be checked when linking shared libraries?  */
6790
0
  if ((ibfd->flags & DYNAMIC) != 0)
6791
0
    return true;
6792
6793
  /* TODO: Revise to use object-attributes instead.  */
6794
0
  if (!nds32_check_vec_size (ibfd))
6795
0
    return false;
6796
6797
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
6798
0
    return true;
6799
6800
0
  if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6801
0
    {
6802
0
      _bfd_error_handler
6803
0
  (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6804
6805
0
      bfd_set_error (bfd_error_bad_value);
6806
0
      return false;
6807
0
    }
6808
6809
  /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6810
     treat as generic one without checking and merging.  */
6811
0
  if (elf_elfheader (ibfd)->e_flags)
6812
0
    {
6813
0
      in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6814
0
      if (in_version == E_NDS32_ELF_VER_1_2)
6815
0
  {
6816
0
    _bfd_error_handler
6817
0
      (_("%pB: warning: older version of object file encountered, "
6818
0
         "please recompile with current tool chain"), ibfd);
6819
0
  }
6820
6821
      /* We may need to merge V1 and V2 arch object files to V2.  */
6822
0
      if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6823
0
    != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6824
0
  {
6825
    /* Need to convert version.  */
6826
0
    if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6827
0
        == E_NDS_ARCH_STAR_RESERVED)
6828
0
      {
6829
0
        elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6830
0
      }
6831
0
    else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6832
0
       == E_NDS_ARCH_STAR_V3_M
6833
0
       && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6834
0
       == E_NDS_ARCH_STAR_V3_0)
6835
0
      {
6836
0
        elf_elfheader (ibfd)->e_flags =
6837
0
    (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6838
0
    | E_NDS_ARCH_STAR_V3_0;
6839
0
      }
6840
0
    else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6841
0
       == E_NDS_ARCH_STAR_V0_9
6842
0
       || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6843
0
       > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6844
0
      {
6845
0
        elf_elfheader (obfd)->e_flags =
6846
0
    convert_e_flags (elf_elfheader (obfd)->e_flags,
6847
0
         (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6848
0
      }
6849
0
    else
6850
0
      {
6851
0
        elf_elfheader (ibfd)->e_flags =
6852
0
    convert_e_flags (elf_elfheader (ibfd)->e_flags,
6853
0
         (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6854
0
      }
6855
0
  }
6856
6857
      /* Extract some flags.  */
6858
0
      in_flags = elf_elfheader (ibfd)->e_flags
6859
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6860
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6861
6862
      /* The following flags need special treatment.  */
6863
0
      in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6864
0
      in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6865
0
      in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6866
6867
      /* Extract some flags.  */
6868
0
      out_flags = elf_elfheader (obfd)->e_flags
6869
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6870
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6871
6872
      /* The following flags need special treatment.  */
6873
0
      out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6874
0
      out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6875
0
      out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6876
0
      out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6877
0
      if (!elf_flags_init (obfd))
6878
0
  {
6879
    /* If the input is the default architecture then do not
6880
       bother setting the flags for the output architecture,
6881
       instead allow future merges to do this.  If no future
6882
       merges ever set these flags then they will retain their
6883
       unitialised values, which surprise surprise, correspond
6884
       to the default values.  */
6885
0
    if (bfd_get_arch_info (ibfd)->the_default)
6886
0
      return true;
6887
6888
0
    elf_flags_init (obfd) = true;
6889
0
    elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6890
6891
0
    if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6892
0
        && bfd_get_arch_info (obfd)->the_default)
6893
0
      {
6894
0
        return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6895
0
          bfd_get_mach (ibfd));
6896
0
      }
6897
6898
0
    return true;
6899
0
  }
6900
6901
      /* Check flag compatibility.  */
6902
0
      if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6903
0
  {
6904
0
    _bfd_error_handler
6905
0
      (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6906
0
    bfd_set_error (bfd_error_bad_value);
6907
0
    return false;
6908
0
  }
6909
6910
0
      if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6911
0
  {
6912
0
    if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6913
0
      {
6914
0
        _bfd_error_handler
6915
0
    (_("%pB: error: instruction set mismatch with previous modules"),
6916
0
     ibfd);
6917
6918
0
        bfd_set_error (bfd_error_bad_value);
6919
0
        return false;
6920
0
      }
6921
0
  }
6922
6923
      /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6924
   and perf ext1 and DIV are mergerd to perf ext1.  */
6925
0
      if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6926
0
  {
6927
0
    elf_elfheader (obfd)->e_flags =
6928
0
      (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6929
0
      | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6930
0
      | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6931
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6932
0
      | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6933
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6934
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6935
0
      | ((in_version > out_version) ? out_version : in_version);
6936
0
  }
6937
0
      else
6938
0
  {
6939
0
    if (in_version != out_version)
6940
0
      _bfd_error_handler
6941
        /* xgettext:c-format */
6942
0
        (_("%pB: warning: incompatible elf-versions %s and %s"),
6943
0
         ibfd, nds32_elfver_strtab[out_version],
6944
0
         nds32_elfver_strtab[in_version]);
6945
6946
0
    elf_elfheader (obfd)->e_flags = in_flags | out_flags
6947
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6948
0
      | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6949
0
      | (in_version > out_version ?  out_version : in_version);
6950
0
  }
6951
0
    }
6952
6953
0
  return true;
6954
0
}
6955
6956
/* Display the flags field.  */
6957
6958
static bool
6959
nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6960
409
{
6961
409
  FILE *file = (FILE *) ptr;
6962
6963
409
  BFD_ASSERT (abfd != NULL && ptr != NULL);
6964
6965
409
  _bfd_elf_print_private_bfd_data (abfd, ptr);
6966
6967
409
  fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6968
6969
409
  switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6970
409
    {
6971
409
    default:
6972
409
    case E_N1_ARCH:
6973
409
      fprintf (file, _(": n1 instructions"));
6974
409
      break;
6975
0
    case E_N1H_ARCH:
6976
0
      fprintf (file, _(": n1h instructions"));
6977
0
      break;
6978
409
    }
6979
6980
409
  fputc ('\n', file);
6981
6982
409
  return true;
6983
409
}
6984
6985
static unsigned int
6986
nds32_elf_action_discarded (asection *sec)
6987
0
{
6988
6989
0
  if (startswith (sec->name, ".gcc_except_table"))
6990
0
    return 0;
6991
6992
0
  return _bfd_elf_default_action_discarded (sec);
6993
0
}
6994
6995
static asection *
6996
nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6997
      struct elf_reloc_cookie *cookie,
6998
      struct elf_link_hash_entry *h,
6999
      unsigned int symndx)
7000
0
{
7001
0
  if (h != NULL)
7002
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
7003
0
      {
7004
0
      case R_NDS32_GNU_VTINHERIT:
7005
0
      case R_NDS32_GNU_VTENTRY:
7006
0
      case R_NDS32_RELA_GNU_VTINHERIT:
7007
0
      case R_NDS32_RELA_GNU_VTENTRY:
7008
0
  return NULL;
7009
0
      }
7010
7011
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
7012
0
}
7013
7014
static enum elf_nds32_tls_type
7015
get_tls_type (enum elf_nds32_reloc_type r_type,
7016
        struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7017
0
{
7018
0
  enum elf_nds32_tls_type tls_type;
7019
7020
0
  switch (r_type)
7021
0
    {
7022
0
    case R_NDS32_TLS_LE_HI20:
7023
0
    case R_NDS32_TLS_LE_LO12:
7024
0
      tls_type = GOT_TLS_LE;
7025
0
      break;
7026
0
    case R_NDS32_TLS_IE_HI20:
7027
0
    case R_NDS32_TLS_IE_LO12S2:
7028
0
    case R_NDS32_TLS_IE_LO12:
7029
0
      tls_type = GOT_TLS_IE;
7030
0
      break;
7031
0
    case R_NDS32_TLS_IEGP_HI20:
7032
0
    case R_NDS32_TLS_IEGP_LO12:
7033
0
    case R_NDS32_TLS_IEGP_LO12S2:
7034
0
      tls_type = GOT_TLS_IEGP;
7035
0
      break;
7036
0
    case R_NDS32_TLS_DESC_HI20:
7037
0
    case R_NDS32_TLS_DESC_LO12:
7038
0
    case R_NDS32_TLS_DESC_ADD:
7039
0
    case R_NDS32_TLS_DESC_FUNC:
7040
0
    case R_NDS32_TLS_DESC_CALL:
7041
0
      tls_type = GOT_TLS_DESC;
7042
0
      break;
7043
0
    default:
7044
0
      tls_type = GOT_NORMAL;
7045
0
      break;
7046
0
    }
7047
7048
0
  return tls_type;
7049
0
}
7050
7051
/* Ensure that we have allocated bookkeeping structures for ABFD's local
7052
   symbols.  */
7053
7054
static bool
7055
elf32_nds32_allocate_local_sym_info (bfd *abfd)
7056
0
{
7057
0
  if (elf_local_got_refcounts (abfd) == NULL)
7058
0
    {
7059
0
      bfd_size_type num_syms;
7060
0
      bfd_size_type size;
7061
0
      char *data;
7062
7063
0
      num_syms = elf_symtab_hdr (abfd).sh_info;
7064
      /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7065
   gp_offset.  The details can refer to struct elf_nds32_obj_tdata.  */
7066
0
      size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7067
0
       + sizeof (bfd_vma) + sizeof (int)
7068
0
       + sizeof (bool) + sizeof (bfd_vma));
7069
0
      data = bfd_zalloc (abfd, size);
7070
0
      if (data == NULL)
7071
0
  return false;
7072
7073
0
      elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7074
0
      data += num_syms * sizeof (bfd_signed_vma);
7075
7076
0
      elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7077
0
      data += num_syms * sizeof (char);
7078
7079
0
      elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7080
0
      data += num_syms * sizeof (bfd_vma);
7081
7082
0
      elf32_nds32_local_gp_offset (abfd) = (int *) data;
7083
0
      data += num_syms * sizeof (int);
7084
0
    }
7085
7086
0
  return true;
7087
0
}
7088
7089
/* Look through the relocs for a section during the first phase.
7090
   Since we don't do .gots or .plts, we just need to consider the
7091
   virtual table relocs for gc.  */
7092
7093
static bool
7094
nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7095
      asection *sec, const Elf_Internal_Rela *relocs)
7096
0
{
7097
0
  Elf_Internal_Shdr *symtab_hdr;
7098
0
  struct elf_link_hash_entry **sym_hashes;
7099
0
  const Elf_Internal_Rela *rel;
7100
0
  const Elf_Internal_Rela *rel_end;
7101
0
  struct elf_link_hash_table *ehtab;
7102
0
  struct elf_nds32_link_hash_table *htab;
7103
0
  bfd *dynobj;
7104
0
  asection *sreloc = NULL;
7105
7106
  /* No need for relocation if relocatable already.  */
7107
0
  if (bfd_link_relocatable (info))
7108
0
    {
7109
0
      elf32_nds32_check_relax_group (abfd, sec);
7110
0
      return true;
7111
0
    }
7112
7113
0
  symtab_hdr = &elf_symtab_hdr (abfd);
7114
0
  sym_hashes = elf_sym_hashes (abfd);
7115
7116
0
  ehtab = elf_hash_table (info);
7117
0
  htab = nds32_elf_hash_table (info);
7118
0
  dynobj = htab->root.dynobj;
7119
7120
0
  rel_end = relocs + sec->reloc_count;
7121
0
  for (rel = relocs; rel < rel_end; rel++)
7122
0
    {
7123
0
      enum elf_nds32_reloc_type r_type;
7124
0
      struct elf_link_hash_entry *h;
7125
0
      unsigned long r_symndx;
7126
0
      enum elf_nds32_tls_type tls_type, old_tls_type;
7127
7128
0
      r_symndx = ELF32_R_SYM (rel->r_info);
7129
0
      r_type = ELF32_R_TYPE (rel->r_info);
7130
0
      if (r_symndx < symtab_hdr->sh_info)
7131
0
  h = NULL;
7132
0
      else
7133
0
  {
7134
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7135
0
    while (h->root.type == bfd_link_hash_indirect
7136
0
     || h->root.type == bfd_link_hash_warning)
7137
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
7138
0
  }
7139
7140
      /* Create .got section if necessary.
7141
   Some relocs require a global offset table.  We create
7142
   got section here, since these relocation need a got section
7143
   and if it is not created yet.  */
7144
0
      if (ehtab->sgot == NULL)
7145
0
  {
7146
0
    switch (r_type)
7147
0
      {
7148
0
      case R_NDS32_GOT_HI20:
7149
0
      case R_NDS32_GOT_LO12:
7150
0
      case R_NDS32_GOT_LO15:
7151
0
      case R_NDS32_GOT_LO19:
7152
0
      case R_NDS32_GOT17S2_RELA:
7153
0
      case R_NDS32_GOT15S2_RELA:
7154
0
      case R_NDS32_GOTOFF:
7155
0
      case R_NDS32_GOTOFF_HI20:
7156
0
      case R_NDS32_GOTOFF_LO12:
7157
0
      case R_NDS32_GOTOFF_LO15:
7158
0
      case R_NDS32_GOTOFF_LO19:
7159
0
      case R_NDS32_GOTPC20:
7160
0
      case R_NDS32_GOTPC_HI20:
7161
0
      case R_NDS32_GOTPC_LO12:
7162
0
      case R_NDS32_GOT20:
7163
0
      case R_NDS32_TLS_IE_HI20:
7164
0
      case R_NDS32_TLS_IE_LO12:
7165
0
      case R_NDS32_TLS_IE_LO12S2:
7166
0
      case R_NDS32_TLS_IEGP_HI20:
7167
0
      case R_NDS32_TLS_IEGP_LO12:
7168
0
      case R_NDS32_TLS_IEGP_LO12S2:
7169
0
      case R_NDS32_TLS_DESC_HI20:
7170
0
      case R_NDS32_TLS_DESC_LO12:
7171
0
        if (dynobj == NULL)
7172
0
    htab->root.dynobj = dynobj = abfd;
7173
0
        if (!create_got_section (dynobj, info))
7174
0
    return false;
7175
0
        break;
7176
7177
0
      default:
7178
0
        break;
7179
0
      }
7180
0
  }
7181
7182
      /* Check relocation type.  */
7183
0
      switch ((int) r_type)
7184
0
  {
7185
0
  case R_NDS32_GOT_HI20:
7186
0
  case R_NDS32_GOT_LO12:
7187
0
  case R_NDS32_GOT_LO15:
7188
0
  case R_NDS32_GOT_LO19:
7189
0
  case R_NDS32_GOT20:
7190
0
  case R_NDS32_TLS_LE_HI20:
7191
0
  case R_NDS32_TLS_LE_LO12:
7192
0
  case R_NDS32_TLS_IE_HI20:
7193
0
  case R_NDS32_TLS_IE_LO12:
7194
0
  case R_NDS32_TLS_IE_LO12S2:
7195
0
  case R_NDS32_TLS_IEGP_HI20:
7196
0
  case R_NDS32_TLS_IEGP_LO12:
7197
0
  case R_NDS32_TLS_IEGP_LO12S2:
7198
0
  case R_NDS32_TLS_DESC_HI20:
7199
0
  case R_NDS32_TLS_DESC_LO12:
7200
0
    tls_type = get_tls_type (r_type, h);
7201
0
    if (h)
7202
0
      {
7203
0
        if (tls_type != GOT_TLS_LE)
7204
0
    h->got.refcount += 1;
7205
0
        old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7206
0
      }
7207
0
    else
7208
0
      {
7209
        /* This is a global offset table entry for a local symbol.  */
7210
0
        if (!elf32_nds32_allocate_local_sym_info (abfd))
7211
0
    return false;
7212
7213
0
        BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7214
0
        if (tls_type != GOT_TLS_LE)
7215
0
    elf_local_got_refcounts (abfd)[r_symndx] += 1;
7216
0
        old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7217
0
      }
7218
7219
    /* We would already have issued an error message if there
7220
       is a TLS/non-TLS mismatch, based on the symbol
7221
       type.  So just combine any TLS types needed.  */
7222
0
    if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7223
0
        && tls_type != GOT_NORMAL)
7224
0
      tls_type |= old_tls_type;
7225
7226
    /* DESC to IE/IEGP if link to executable.  */
7227
0
    if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7228
0
        && (bfd_link_executable (info)))
7229
0
      tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7230
7231
0
    if (old_tls_type != tls_type)
7232
0
      {
7233
0
        if (h != NULL)
7234
0
    elf32_nds32_hash_entry (h)->tls_type = tls_type;
7235
0
        else
7236
0
    elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7237
0
      }
7238
0
    break;
7239
0
  case R_NDS32_9_PLTREL:
7240
0
  case R_NDS32_25_PLTREL:
7241
0
  case R_NDS32_PLTREL_HI20:
7242
0
  case R_NDS32_PLTREL_LO12:
7243
0
  case R_NDS32_PLT_GOTREL_HI20:
7244
0
  case R_NDS32_PLT_GOTREL_LO12:
7245
0
  case R_NDS32_PLT_GOTREL_LO15:
7246
0
  case R_NDS32_PLT_GOTREL_LO19:
7247
0
  case R_NDS32_PLT_GOTREL_LO20:
7248
7249
    /* This symbol requires a procedure linkage table entry.  We
7250
       actually build the entry in adjust_dynamic_symbol,
7251
       because this might be a case of linking PIC code without
7252
       linking in any dynamic objects, in which case we don't
7253
       need to generate a procedure linkage table after all.  */
7254
7255
    /* If this is a local symbol, we resolve it directly without
7256
       creating a procedure linkage table entry.  */
7257
0
    if (h == NULL)
7258
0
      continue;
7259
7260
0
    if (h->forced_local
7261
0
        || (bfd_link_pie (info) && h->def_regular))
7262
0
      break;
7263
7264
0
    elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7265
0
    h->needs_plt = 1;
7266
0
    h->plt.refcount += 1;
7267
0
    break;
7268
7269
0
  case R_NDS32_16_RELA:
7270
0
  case R_NDS32_20_RELA:
7271
0
  case R_NDS32_5_RELA:
7272
0
  case R_NDS32_32_RELA:
7273
0
  case R_NDS32_HI20_RELA:
7274
0
  case R_NDS32_LO12S3_RELA:
7275
0
  case R_NDS32_LO12S2_RELA:
7276
0
  case R_NDS32_LO12S2_DP_RELA:
7277
0
  case R_NDS32_LO12S2_SP_RELA:
7278
0
  case R_NDS32_LO12S1_RELA:
7279
0
  case R_NDS32_LO12S0_RELA:
7280
0
  case R_NDS32_LO12S0_ORI_RELA:
7281
0
  case R_NDS32_SDA16S3_RELA:
7282
0
  case R_NDS32_SDA17S2_RELA:
7283
0
  case R_NDS32_SDA18S1_RELA:
7284
0
  case R_NDS32_SDA19S0_RELA:
7285
0
  case R_NDS32_SDA15S3_RELA:
7286
0
  case R_NDS32_SDA15S2_RELA:
7287
0
  case R_NDS32_SDA12S2_DP_RELA:
7288
0
  case R_NDS32_SDA12S2_SP_RELA:
7289
0
  case R_NDS32_SDA15S1_RELA:
7290
0
  case R_NDS32_SDA15S0_RELA:
7291
0
  case R_NDS32_SDA_FP7U2_RELA:
7292
0
  case R_NDS32_15_PCREL_RELA:
7293
0
  case R_NDS32_17_PCREL_RELA:
7294
0
  case R_NDS32_25_PCREL_RELA:
7295
7296
0
    if (h != NULL && !bfd_link_pic (info))
7297
0
      {
7298
0
        h->non_got_ref = 1;
7299
0
        h->plt.refcount += 1;
7300
0
      }
7301
7302
    /* If we are creating a shared library, and this is a reloc against
7303
       a global symbol, or a non PC relative reloc against a local
7304
       symbol, then we need to copy the reloc into the shared library.
7305
       However, if we are linking with -Bsymbolic, we do not need to
7306
       copy a reloc against a global symbol which is defined in an
7307
       object we are including in the link (i.e., DEF_REGULAR is set).
7308
       At this point we have not seen all the input files, so it is
7309
       possible that DEF_REGULAR is not set now but will be set later
7310
       (it is never cleared).  We account for that possibility below by
7311
       storing information in the dyn_relocs field of the hash table
7312
       entry.  A similar situation occurs when creating shared libraries
7313
       and symbol visibility changes render the symbol local.
7314
7315
       If on the other hand, we are creating an executable, we may need
7316
       to keep relocations for symbols satisfied by a dynamic library
7317
       if we manage to avoid copy relocs for the symbol.  */
7318
0
    if ((bfd_link_pic (info)
7319
0
         && (sec->flags & SEC_ALLOC) != 0
7320
0
         && ((r_type != R_NDS32_25_PCREL_RELA
7321
0
        && r_type != R_NDS32_15_PCREL_RELA
7322
0
        && r_type != R_NDS32_17_PCREL_RELA
7323
0
        && !(r_type == R_NDS32_32_RELA
7324
0
       && strcmp (sec->name, ".eh_frame") == 0))
7325
0
       || (h != NULL
7326
0
           && (!info->symbolic
7327
0
         || h->root.type == bfd_link_hash_defweak
7328
0
         || !h->def_regular))))
7329
0
        || (!bfd_link_pic (info)
7330
0
      && (sec->flags & SEC_ALLOC) != 0
7331
0
      && h != NULL
7332
0
      && (h->root.type == bfd_link_hash_defweak
7333
0
          || !h->def_regular)))
7334
0
      {
7335
0
        struct elf_dyn_relocs *p;
7336
0
        struct elf_dyn_relocs **head;
7337
7338
0
        if (dynobj == NULL)
7339
0
    htab->root.dynobj = dynobj = abfd;
7340
7341
        /* When creating a shared object, we must copy these
7342
     relocs into the output file.  We create a reloc
7343
     section in dynobj and make room for the reloc.  */
7344
0
        if (sreloc == NULL)
7345
0
    {
7346
0
      const char *name;
7347
7348
0
      name = bfd_elf_string_from_elf_section
7349
0
        (abfd, elf_elfheader (abfd)->e_shstrndx,
7350
0
         elf_section_data (sec)->rela.hdr->sh_name);
7351
0
      if (name == NULL)
7352
0
        return false;
7353
7354
0
      BFD_ASSERT (startswith (name, ".rela")
7355
0
            && strcmp (bfd_section_name (sec),
7356
0
           name + 5) == 0);
7357
7358
0
      sreloc = bfd_get_section_by_name (dynobj, name);
7359
0
      if (sreloc == NULL)
7360
0
        {
7361
0
          flagword flags;
7362
7363
0
          sreloc = bfd_make_section (dynobj, name);
7364
0
          flags = (SEC_HAS_CONTENTS | SEC_READONLY
7365
0
             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7366
0
          if ((sec->flags & SEC_ALLOC) != 0)
7367
0
      flags |= SEC_ALLOC | SEC_LOAD;
7368
0
          if (sreloc == NULL
7369
0
        || !bfd_set_section_flags (sreloc, flags)
7370
0
        || !bfd_set_section_alignment (sreloc, 2))
7371
0
      return false;
7372
7373
0
          elf_section_type (sreloc) = SHT_RELA;
7374
0
        }
7375
0
      elf_section_data (sec)->sreloc = sreloc;
7376
0
    }
7377
7378
        /* If this is a global symbol, we count the number of
7379
     relocations we need for this symbol.  */
7380
0
        if (h != NULL)
7381
0
    head = &h->dyn_relocs;
7382
0
        else
7383
0
    {
7384
0
      asection *s;
7385
0
      void *vpp;
7386
7387
0
      Elf_Internal_Sym *isym;
7388
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
7389
0
            abfd, r_symndx);
7390
0
      if (isym == NULL)
7391
0
        return false;
7392
7393
      /* Track dynamic relocs needed for local syms too.  */
7394
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7395
0
      if (s == NULL)
7396
0
        return false;
7397
7398
0
      vpp = &elf_section_data (s)->local_dynrel;
7399
0
      head = (struct elf_dyn_relocs **) vpp;
7400
0
    }
7401
7402
0
        p = *head;
7403
0
        if (p == NULL || p->sec != sec)
7404
0
    {
7405
0
      size_t amt = sizeof (*p);
7406
0
      p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7407
0
      if (p == NULL)
7408
0
        return false;
7409
0
      p->next = *head;
7410
0
      *head = p;
7411
0
      p->sec = sec;
7412
0
      p->count = 0;
7413
0
      p->pc_count = 0;
7414
0
    }
7415
7416
0
        p->count += 1;
7417
7418
        /* Since eh_frame is readonly, R_NDS32_32_RELA
7419
     reloc for eh_frame will cause shared library has
7420
     TEXTREL entry in the dynamic section. This lead glibc
7421
     testsuites to failure (bug-13092) and cause kernel fail
7422
     (bug-11819).  I think the best solution is to replace
7423
     absolute reloc with pc relative reloc in the eh_frame.
7424
     To do that, we need to support the following issues:
7425
7426
     === For GCC ===
7427
     * gcc/config/nds32/nds32.h: Define
7428
     ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7429
     and DW_EH_PE_sdata4 into DWARF exception header when
7430
     option have '-fpic'.
7431
7432
     === For binutils ===
7433
     * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7434
     * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7435
     may break our nds DIFF mechanism, therefore, we
7436
     must disable all linker relaxations to ensure
7437
     correctness.
7438
     * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7439
     R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7440
     do the necessary modification.
7441
7442
     Unfortunately, it still have some problems for nds32
7443
     to support pc relative reloc in the eh_frame. So I use
7444
     another solution to fix this issue.
7445
7446
     However, I find that ld always emit TEXTREL marker for
7447
     R_NDS32_NONE relocs in rel.dyn. These none relocs are
7448
     correspond to R_NDS32_32_RELA for .eh_frame section.
7449
     It means that we always reserve redundant entries of rel.dyn
7450
     for these relocs which actually do nothing in dynamic linker.
7451
7452
     Therefore, we regard these relocs as pc relative relocs
7453
     here and increase the pc_count.  */
7454
0
        if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7455
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7456
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7457
0
      || (r_type == R_NDS32_32_RELA
7458
0
          && strcmp (sec->name, ".eh_frame") == 0))
7459
0
    p->pc_count += 1;
7460
0
      }
7461
0
    break;
7462
7463
    /* This relocation describes the C++ object vtable hierarchy.
7464
       Reconstruct it for later use during GC.  */
7465
0
  case R_NDS32_RELA_GNU_VTINHERIT:
7466
0
  case R_NDS32_GNU_VTINHERIT:
7467
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7468
0
      return false;
7469
0
    break;
7470
7471
    /* This relocation describes which C++ vtable entries are actually
7472
       used.  Record for later use during GC.  */
7473
0
  case R_NDS32_GNU_VTENTRY:
7474
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7475
0
      return false;
7476
0
    break;
7477
0
  case R_NDS32_RELA_GNU_VTENTRY:
7478
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7479
0
      return false;
7480
0
    break;
7481
0
  }
7482
0
    }
7483
7484
0
  return true;
7485
0
}
7486
7487
/* Write VAL in uleb128 format to P, returning a pointer to the
7488
   following byte.
7489
   This code is copied from elf-attr.c.  */
7490
7491
static bfd_byte *
7492
write_uleb128 (bfd_byte *p, unsigned int val)
7493
0
{
7494
0
  bfd_byte c;
7495
0
  do
7496
0
    {
7497
0
      c = val & 0x7f;
7498
0
      val >>= 7;
7499
0
      if (val)
7500
0
  c |= 0x80;
7501
0
      *(p++) = c;
7502
0
    }
7503
0
  while (val);
7504
0
  return p;
7505
0
}
7506
7507
static bfd_signed_vma
7508
calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7509
      Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7510
0
{
7511
0
  bfd_signed_vma foff;
7512
0
  bfd_vma symval, addend;
7513
0
  asection *sym_sec;
7514
7515
  /* Get the value of the symbol referred to by the reloc.  */
7516
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7517
0
    {
7518
0
      Elf_Internal_Sym *isym;
7519
7520
      /* A local symbol.  */
7521
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
7522
7523
0
      if (isym->st_shndx == SHN_UNDEF)
7524
0
  sym_sec = bfd_und_section_ptr;
7525
0
      else if (isym->st_shndx == SHN_ABS)
7526
0
  sym_sec = bfd_abs_section_ptr;
7527
0
      else if (isym->st_shndx == SHN_COMMON)
7528
0
  sym_sec = bfd_com_section_ptr;
7529
0
      else
7530
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7531
0
      symval = isym->st_value + sym_sec->output_section->vma
7532
0
         + sym_sec->output_offset;
7533
0
    }
7534
0
  else
7535
0
    {
7536
0
      unsigned long indx;
7537
0
      struct elf_link_hash_entry *h;
7538
7539
      /* An external symbol.  */
7540
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7541
0
      h = elf_sym_hashes (abfd)[indx];
7542
0
      BFD_ASSERT (h != NULL);
7543
7544
0
      if (h->root.type != bfd_link_hash_defined
7545
0
    && h->root.type != bfd_link_hash_defweak)
7546
  /* This appears to be a reference to an undefined
7547
     symbol.  Just ignore it--it will be caught by the
7548
     regular reloc processing.  */
7549
0
  return 0;
7550
7551
0
      if (h->root.u.def.section->flags & SEC_MERGE)
7552
0
  {
7553
0
    sym_sec = h->root.u.def.section;
7554
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
7555
0
                 h->root.u.def.value);
7556
0
    symval = symval + sym_sec->output_section->vma
7557
0
       + sym_sec->output_offset;
7558
0
  }
7559
0
      else
7560
0
  symval = (h->root.u.def.value
7561
0
      + h->root.u.def.section->output_section->vma
7562
0
      + h->root.u.def.section->output_offset);
7563
0
    }
7564
7565
0
  addend = irel->r_addend;
7566
7567
0
  foff = (symval + addend
7568
0
    - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7569
0
  return foff;
7570
0
}
7571

7572
7573
/* Convert a 32-bit instruction to 16-bit one.
7574
   INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7575
   instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
7576
   type of INSN16.  Return 1 if successful.  */
7577
7578
static int
7579
nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7580
           int *pinsn_type)
7581
0
{
7582
0
  uint16_t insn16 = 0;
7583
0
  int insn_type = 0;
7584
0
  unsigned long mach = bfd_get_mach (abfd);
7585
7586
0
  if (N32_SH5 (insn) != 0)
7587
0
    return 0;
7588
7589
0
  switch (N32_SUB5 (insn))
7590
0
    {
7591
0
    case N32_ALU1_ADD_SLLI:
7592
0
    case N32_ALU1_ADD_SRLI:
7593
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7594
0
  {
7595
0
    insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7596
0
        N32_RB5 (insn));
7597
0
    insn_type = NDS32_INSN_ADD333;
7598
0
  }
7599
0
      else if (N32_IS_RT4 (insn))
7600
0
  {
7601
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7602
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7603
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7604
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7605
0
    insn_type = NDS32_INSN_ADD45;
7606
0
  }
7607
0
      break;
7608
7609
0
    case N32_ALU1_SUB_SLLI:
7610
0
    case N32_ALU1_SUB_SRLI:
7611
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7612
0
  {
7613
0
    insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7614
0
        N32_RB5 (insn));
7615
0
    insn_type = NDS32_INSN_SUB333;
7616
0
  }
7617
0
      else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7618
0
  {
7619
0
    insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7620
0
    insn_type = NDS32_INSN_SUB45;
7621
0
  }
7622
0
      break;
7623
7624
0
    case N32_ALU1_AND_SLLI:
7625
0
    case N32_ALU1_AND_SRLI:
7626
      /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
7627
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7628
0
    && N32_IS_RB3 (insn))
7629
0
  {
7630
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7631
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7632
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7633
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7634
0
    if (insn16)
7635
0
      insn_type = NDS32_INSN_AND33;
7636
0
  }
7637
0
      break;
7638
7639
0
    case N32_ALU1_XOR_SLLI:
7640
0
    case N32_ALU1_XOR_SRLI:
7641
      /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
7642
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7643
0
    && N32_IS_RB3 (insn))
7644
0
  {
7645
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7646
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7647
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7648
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7649
0
    if (insn16)
7650
0
      insn_type = NDS32_INSN_XOR33;
7651
0
  }
7652
0
      break;
7653
7654
0
    case N32_ALU1_OR_SLLI:
7655
0
    case N32_ALU1_OR_SRLI:
7656
      /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
7657
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7658
0
    && N32_IS_RB3 (insn))
7659
0
  {
7660
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7661
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7662
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7663
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7664
0
    if (insn16)
7665
0
      insn_type = NDS32_INSN_OR33;
7666
0
  }
7667
0
      break;
7668
0
    case N32_ALU1_NOR:
7669
      /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
7670
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7671
0
    && N32_RA5 (insn) == N32_RB5 (insn))
7672
0
  {
7673
0
    insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7674
0
    insn_type = NDS32_INSN_NOT33;
7675
0
  }
7676
0
      break;
7677
0
    case N32_ALU1_SRAI:
7678
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7679
0
  {
7680
0
    insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7681
0
    insn_type = NDS32_INSN_SRAI45;
7682
0
  }
7683
0
      break;
7684
7685
0
    case N32_ALU1_SRLI:
7686
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7687
0
  {
7688
0
    insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7689
0
    insn_type = NDS32_INSN_SRLI45;
7690
0
  }
7691
0
      break;
7692
7693
0
    case N32_ALU1_SLLI:
7694
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7695
0
  {
7696
0
    insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7697
0
        N32_UB5 (insn));
7698
0
    insn_type = NDS32_INSN_SLLI333;
7699
0
  }
7700
0
      break;
7701
7702
0
    case N32_ALU1_ZEH:
7703
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7704
0
  {
7705
0
    insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7706
0
    insn_type = NDS32_INSN_ZEH33;
7707
0
  }
7708
0
      break;
7709
7710
0
    case N32_ALU1_SEB:
7711
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7712
0
  {
7713
0
    insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7714
0
    insn_type = NDS32_INSN_SEB33;
7715
0
  }
7716
0
      break;
7717
7718
0
    case N32_ALU1_SEH:
7719
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7720
0
  {
7721
0
    insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7722
0
    insn_type = NDS32_INSN_SEH33;
7723
0
  }
7724
0
      break;
7725
7726
0
    case N32_ALU1_SLT:
7727
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7728
0
  {
7729
    /* Implicit r15.  */
7730
0
    insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7731
0
    insn_type = NDS32_INSN_SLT45;
7732
0
  }
7733
0
      break;
7734
7735
0
    case N32_ALU1_SLTS:
7736
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7737
0
  {
7738
    /* Implicit r15.  */
7739
0
    insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7740
0
    insn_type = NDS32_INSN_SLTS45;
7741
0
  }
7742
0
      break;
7743
0
    }
7744
7745
0
  if ((insn16 & 0x8000) == 0)
7746
0
    return 0;
7747
7748
0
  if (pinsn16)
7749
0
    *pinsn16 = insn16;
7750
0
  if (pinsn_type)
7751
0
    *pinsn_type = insn_type;
7752
0
  return 1;
7753
0
}
7754
7755
static int
7756
nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7757
           int *pinsn_type)
7758
0
{
7759
0
  uint16_t insn16 = 0;
7760
0
  int insn_type;
7761
0
  unsigned long mach = bfd_get_mach (abfd);
7762
7763
  /* TODO: bset, bclr, btgl, btst.  */
7764
0
  if (__GF (insn, 6, 4) != 0)
7765
0
    return 0;
7766
7767
0
  switch (N32_IMMU (insn, 6))
7768
0
    {
7769
0
    case N32_ALU2_MUL:
7770
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7771
0
    && N32_IS_RB3 (insn))
7772
0
  {
7773
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7774
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7775
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7776
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7777
0
    if (insn16)
7778
0
      insn_type = NDS32_INSN_MUL33;
7779
0
  }
7780
0
    }
7781
7782
0
  if ((insn16 & 0x8000) == 0)
7783
0
    return 0;
7784
7785
0
  if (pinsn16)
7786
0
    *pinsn16 = insn16;
7787
0
  if (pinsn_type)
7788
0
    *pinsn_type = insn_type;
7789
0
  return 1;
7790
0
}
7791
7792
int
7793
bfd_elf_nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7794
        int *pinsn_type)
7795
0
{
7796
0
  int op6;
7797
0
  uint16_t insn16 = 0;
7798
0
  int insn_type = 0;
7799
0
  unsigned long mach = bfd_get_mach (abfd);
7800
7801
  /* Decode 32-bit instruction.  */
7802
0
  if (insn & 0x80000000)
7803
0
    {
7804
      /* Not 32-bit insn.  */
7805
0
      return 0;
7806
0
    }
7807
7808
0
  op6 = N32_OP6 (insn);
7809
7810
  /* Convert it to 16-bit instruction.  */
7811
0
  switch (op6)
7812
0
    {
7813
0
    case N32_OP6_MOVI:
7814
0
      if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7815
0
  {
7816
0
    insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7817
0
    insn_type = NDS32_INSN_MOVI55;
7818
0
  }
7819
0
      else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7820
0
         && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7821
0
  {
7822
0
    insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7823
0
             N32_IMM20S (insn) - 16);
7824
0
    insn_type = NDS32_INSN_MOVPI45;
7825
0
  }
7826
0
      break;
7827
7828
0
    case N32_OP6_ADDI:
7829
0
      if (N32_IMM15S (insn) == 0)
7830
0
  {
7831
    /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7832
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7833
0
    if (mach <= MACH_V2
7834
0
        || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7835
0
      {
7836
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7837
0
        insn_type = NDS32_INSN_MOV55;
7838
0
      }
7839
0
  }
7840
0
      else if (N32_IMM15S (insn) > 0)
7841
0
  {
7842
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7843
0
      {
7844
0
        insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7845
0
            N32_IMM15S (insn));
7846
0
        insn_type = NDS32_INSN_ADDI333;
7847
0
      }
7848
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7849
0
       && N32_IMM15S (insn) < 32)
7850
0
      {
7851
0
        insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7852
0
        insn_type = NDS32_INSN_ADDI45;
7853
0
      }
7854
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7855
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7856
0
       && N32_IMM15S (insn) < 512)
7857
0
      {
7858
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7859
0
        insn_type = NDS32_INSN_ADDI10_SP;
7860
0
      }
7861
0
    else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7862
0
       && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7863
0
       && (N32_IMM15S (insn) % 4 == 0))
7864
0
      {
7865
0
        insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7866
0
           N32_IMM15S (insn) >> 2);
7867
0
        insn_type = NDS32_INSN_ADDRI36_SP;
7868
0
      }
7869
0
  }
7870
0
      else
7871
0
  {
7872
    /* Less than 0.  */
7873
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7874
0
      {
7875
0
        insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7876
0
            0 - N32_IMM15S (insn));
7877
0
        insn_type = NDS32_INSN_SUBI333;
7878
0
      }
7879
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7880
0
       && N32_IMM15S (insn) > -32)
7881
0
      {
7882
0
        insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7883
0
           0 - N32_IMM15S (insn));
7884
0
        insn_type = NDS32_INSN_SUBI45;
7885
0
      }
7886
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7887
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7888
0
       && N32_IMM15S (insn) >= -512)
7889
0
      {
7890
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7891
0
        insn_type = NDS32_INSN_ADDI10_SP;
7892
0
      }
7893
0
  }
7894
0
      break;
7895
7896
0
    case N32_OP6_ORI:
7897
0
      if (N32_IMM15S (insn) == 0)
7898
0
  {
7899
    /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7900
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7901
0
    if (mach <= MACH_V2
7902
0
        || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7903
0
      {
7904
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7905
0
        insn_type = NDS32_INSN_MOV55;
7906
0
      }
7907
0
  }
7908
0
      break;
7909
7910
0
    case N32_OP6_SUBRI:
7911
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7912
0
    && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7913
0
  {
7914
0
    insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7915
0
    insn_type = NDS32_INSN_NEG33;
7916
0
  }
7917
0
      break;
7918
7919
0
    case N32_OP6_ANDI:
7920
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7921
0
  {
7922
0
    if (N32_IMM15U (insn) == 1)
7923
0
      {
7924
0
        insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7925
0
        insn_type = NDS32_INSN_XLSB33;
7926
0
      }
7927
0
    else if (N32_IMM15U (insn) == 0x7ff)
7928
0
      {
7929
0
        insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7930
0
        insn_type = NDS32_INSN_X11B33;
7931
0
      }
7932
0
    else if (N32_IMM15U (insn) == 0xff)
7933
0
      {
7934
0
        insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7935
0
        insn_type = NDS32_INSN_ZEB33;
7936
0
      }
7937
0
    else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7938
0
       && N32_IMM15U (insn) < 256)
7939
0
      {
7940
0
        int imm15u = N32_IMM15U (insn);
7941
7942
0
        if (__builtin_popcount (imm15u) == 1)
7943
0
    {
7944
      /* BMSKI33 */
7945
0
      int imm3u = __builtin_ctz (imm15u);
7946
7947
0
      insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7948
0
      insn_type = NDS32_INSN_BMSKI33;
7949
0
    }
7950
0
        else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7951
0
    {
7952
      /* FEXTI33 */
7953
0
      int imm3u = __builtin_ctz (imm15u + 1) - 1;
7954
7955
0
      insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7956
0
      insn_type = NDS32_INSN_FEXTI33;
7957
0
    }
7958
0
      }
7959
0
  }
7960
0
      break;
7961
7962
0
    case N32_OP6_SLTI:
7963
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7964
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7965
0
  {
7966
0
    insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7967
0
    insn_type = NDS32_INSN_SLTI45;
7968
0
  }
7969
0
      break;
7970
7971
0
    case N32_OP6_SLTSI:
7972
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7973
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7974
0
  {
7975
0
    insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7976
0
    insn_type = NDS32_INSN_SLTSI45;
7977
0
  }
7978
0
      break;
7979
7980
0
    case N32_OP6_LWI:
7981
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7982
0
  {
7983
0
    insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7984
0
    insn_type = NDS32_INSN_LWI450;
7985
0
  }
7986
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7987
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
7988
0
  {
7989
0
    insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7990
0
        N32_IMM15S (insn));
7991
0
    insn_type = NDS32_INSN_LWI333;
7992
0
  }
7993
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7994
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7995
0
  {
7996
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7997
0
    insn_type = NDS32_INSN_LWI37;
7998
0
  }
7999
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8000
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8001
0
  {
8002
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8003
0
    insn_type = NDS32_INSN_LWI37_SP;
8004
0
  }
8005
0
      else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
8006
0
         && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8007
0
  {
8008
0
    insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8009
0
             N32_IMM15S (insn) + 32);
8010
0
    insn_type = NDS32_INSN_LWI45_FE;
8011
0
  }
8012
0
      break;
8013
8014
0
    case N32_OP6_SWI:
8015
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8016
0
  {
8017
0
    insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8018
0
    insn_type = NDS32_INSN_SWI450;
8019
0
  }
8020
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8021
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
8022
0
  {
8023
0
    insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8024
0
        N32_IMM15S (insn));
8025
0
    insn_type = NDS32_INSN_SWI333;
8026
0
  }
8027
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
8028
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8029
0
  {
8030
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8031
0
    insn_type = NDS32_INSN_SWI37;
8032
0
  }
8033
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
8034
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8035
0
  {
8036
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8037
0
    insn_type = NDS32_INSN_SWI37_SP;
8038
0
  }
8039
0
      break;
8040
8041
0
    case N32_OP6_LWI_BI:
8042
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8043
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8044
0
  {
8045
0
    insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8046
0
        N32_IMM15S (insn));
8047
0
    insn_type = NDS32_INSN_LWI333_BI;
8048
0
  }
8049
0
      break;
8050
8051
0
    case N32_OP6_SWI_BI:
8052
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8053
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8054
0
  {
8055
0
    insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8056
0
        N32_IMM15S (insn));
8057
0
    insn_type = NDS32_INSN_SWI333_BI;
8058
0
  }
8059
0
      break;
8060
8061
0
    case N32_OP6_LHI:
8062
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8063
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8064
0
  {
8065
0
    insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8066
0
        N32_IMM15S (insn));
8067
0
    insn_type = NDS32_INSN_LHI333;
8068
0
  }
8069
0
      break;
8070
8071
0
    case N32_OP6_SHI:
8072
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8073
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8074
0
  {
8075
0
    insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8076
0
        N32_IMM15S (insn));
8077
0
    insn_type = NDS32_INSN_SHI333;
8078
0
  }
8079
0
      break;
8080
8081
0
    case N32_OP6_LBI:
8082
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8083
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8084
0
  {
8085
0
    insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8086
0
        N32_IMM15S (insn));
8087
0
    insn_type = NDS32_INSN_LBI333;
8088
0
  }
8089
0
      break;
8090
8091
0
    case N32_OP6_SBI:
8092
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8093
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8094
0
  {
8095
0
    insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8096
0
        N32_IMM15S (insn));
8097
0
    insn_type = NDS32_INSN_SBI333;
8098
0
  }
8099
0
      break;
8100
8101
0
    case N32_OP6_ALU1:
8102
0
      return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8103
8104
0
    case N32_OP6_ALU2:
8105
0
      return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8106
8107
0
    case N32_OP6_BR1:
8108
0
      if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8109
0
  goto done;
8110
8111
0
      if ((insn & N32_BIT (14)) == 0)
8112
0
  {
8113
    /* N32_BR1_BEQ */
8114
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8115
0
        && N32_RT5 (insn) != REG_R5)
8116
0
      insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8117
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8118
0
       && N32_RA5 (insn) != REG_R5)
8119
0
      insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8120
0
    insn_type = NDS32_INSN_BEQS38;
8121
0
    break;
8122
0
  }
8123
0
      else
8124
0
  {
8125
    /* N32_BR1_BNE */
8126
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8127
0
        && N32_RT5 (insn) != REG_R5)
8128
0
      insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8129
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8130
0
       && N32_RA5 (insn) != REG_R5)
8131
0
      insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8132
0
    insn_type = NDS32_INSN_BNES38;
8133
0
    break;
8134
0
  }
8135
0
      break;
8136
8137
0
    case N32_OP6_BR2:
8138
0
      switch (N32_BR2_SUB (insn))
8139
0
  {
8140
0
  case N32_BR2_BEQZ:
8141
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8142
0
      {
8143
0
        insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8144
0
        insn_type = NDS32_INSN_BEQZ38;
8145
0
      }
8146
0
    else if (N32_RT5 (insn) == REG_R15
8147
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8148
0
      {
8149
0
        insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8150
0
        insn_type = NDS32_INSN_BEQZS8;
8151
0
      }
8152
0
    break;
8153
8154
0
  case N32_BR2_BNEZ:
8155
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8156
0
      {
8157
0
        insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8158
0
        insn_type = NDS32_INSN_BNEZ38;
8159
0
      }
8160
0
    else if (N32_RT5 (insn) == REG_R15
8161
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8162
0
      {
8163
0
        insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8164
0
        insn_type = NDS32_INSN_BNEZS8;
8165
0
      }
8166
0
    break;
8167
8168
0
  case N32_BR2_SOP0:
8169
0
    if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8170
0
      {
8171
0
        insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8172
0
        insn_type = NDS32_INSN_IFCALL9;
8173
0
      }
8174
0
    break;
8175
0
  }
8176
0
      break;
8177
8178
0
    case N32_OP6_JI:
8179
0
      if ((insn & N32_BIT (24)) == 0)
8180
0
  {
8181
    /* N32_JI_J */
8182
0
    if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8183
0
      {
8184
0
        insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8185
0
        insn_type = NDS32_INSN_J8;
8186
0
      }
8187
0
  }
8188
0
      break;
8189
8190
0
    case N32_OP6_JREG:
8191
0
      if (__GF (insn, 8, 2) != 0)
8192
0
  goto done;
8193
8194
0
      switch (N32_IMMU (insn, 5))
8195
0
  {
8196
0
  case N32_JREG_JR:
8197
0
    if (N32_JREG_HINT (insn) == 0)
8198
0
      {
8199
        /* jr */
8200
0
        insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8201
0
        insn_type = NDS32_INSN_JR5;
8202
0
      }
8203
0
    else if (N32_JREG_HINT (insn) == 1)
8204
0
      {
8205
        /* ret */
8206
0
        insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8207
0
        insn_type = NDS32_INSN_RET5;
8208
0
      }
8209
0
    else if (N32_JREG_HINT (insn) == 3)
8210
0
      {
8211
        /* ifret = mov55 $sp, $sp */
8212
0
        insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8213
0
        insn_type = NDS32_INSN_IFRET;
8214
0
      }
8215
0
    break;
8216
8217
0
  case N32_JREG_JRAL:
8218
    /* It's convertible when return rt5 is $lp and address
8219
       translation is kept.  */
8220
0
    if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8221
0
      {
8222
0
        insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8223
0
        insn_type = NDS32_INSN_JRAL5;
8224
0
      }
8225
0
    break;
8226
0
  }
8227
0
      break;
8228
8229
0
    case N32_OP6_MISC:
8230
0
      if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8231
0
  {
8232
    /* For v3, swid above 31 are used for ex9.it.  */
8233
0
    insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8234
0
    insn_type = NDS32_INSN_BREAK16;
8235
0
  }
8236
0
      break;
8237
8238
0
    default:
8239
      /* This instruction has no 16-bit variant.  */
8240
0
      goto done;
8241
0
    }
8242
8243
0
 done:
8244
  /* Bit-15 of insn16 should be set for a valid instruction.  */
8245
0
  if ((insn16 & 0x8000) == 0)
8246
0
    return 0;
8247
8248
0
  if (pinsn16)
8249
0
    *pinsn16 = insn16;
8250
0
  if (pinsn_type)
8251
0
    *pinsn_type = insn_type;
8252
0
  return 1;
8253
0
}
8254
8255
static int
8256
special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8257
        Elf_Internal_Rela *reloc)
8258
0
{
8259
0
  uint16_t insn16 = 0;
8260
8261
0
  if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8262
0
      || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8263
0
    return 0;
8264
8265
0
  if (!N32_IS_RT3 (insn))
8266
0
    return 0;
8267
8268
0
  switch (N32_OP6 (insn))
8269
0
    {
8270
0
    case N32_OP6_LWI:
8271
0
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8272
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8273
0
      break;
8274
0
    case N32_OP6_SWI:
8275
0
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8276
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8277
0
      break;
8278
0
    case N32_OP6_HWGP:
8279
0
      if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8280
0
  break;
8281
8282
0
      if (__GF (insn, 17, 3) == 6)
8283
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8284
0
      else if (__GF (insn, 17, 3) == 7)
8285
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8286
0
      break;
8287
0
    }
8288
8289
0
  if ((insn16 & 0x8000) == 0)
8290
0
    return 0;
8291
8292
0
  *pinsn16 = insn16;
8293
0
  return 1;
8294
0
}
8295
8296
/* Convert a 16-bit instruction to 32-bit one.
8297
   INSN16 it the input and PINSN it the point to output.
8298
   Return non-zero on successful.  Otherwise 0 is returned.  */
8299
8300
int
8301
bfd_elf_nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8302
0
{
8303
0
  uint32_t insn = 0xffffffff;
8304
0
  unsigned long mach = bfd_get_mach (abfd);
8305
8306
  /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
8307
8308
0
  switch (__GF (insn16, 9, 6))
8309
0
    {
8310
0
    case 0x4:     /* add45 */
8311
0
      insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8312
0
           N16_RA5 (insn16));
8313
0
      goto done;
8314
0
    case 0x5:     /* sub45 */
8315
0
      insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8316
0
           N16_RA5 (insn16));
8317
0
      goto done;
8318
0
    case 0x6:     /* addi45 */
8319
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8320
0
      N16_IMM5U (insn16));
8321
0
      goto done;
8322
0
    case 0x7:     /* subi45 */
8323
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8324
0
      -N16_IMM5U (insn16));
8325
0
      goto done;
8326
0
    case 0x8:     /* srai45 */
8327
0
      insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8328
0
           N16_IMM5U (insn16));
8329
0
      goto done;
8330
0
    case 0x9:     /* srli45 */
8331
0
      insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8332
0
           N16_IMM5U (insn16));
8333
0
      goto done;
8334
0
    case 0xa:     /* slli333 */
8335
0
      insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8336
0
           N16_IMM3U (insn16));
8337
0
      goto done;
8338
0
    case 0xc:     /* add333 */
8339
0
      insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8340
0
           N16_RB3 (insn16));
8341
0
      goto done;
8342
0
    case 0xd:     /* sub333 */
8343
0
      insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8344
0
           N16_RB3 (insn16));
8345
0
      goto done;
8346
0
    case 0xe:     /* addi333 */
8347
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8348
0
      N16_IMM3U (insn16));
8349
0
      goto done;
8350
0
    case 0xf:     /* subi333 */
8351
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8352
0
      -N16_IMM3U (insn16));
8353
0
      goto done;
8354
0
    case 0x10:      /* lwi333 */
8355
0
      insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8356
0
      N16_IMM3U (insn16));
8357
0
      goto done;
8358
0
    case 0x12:      /* lhi333 */
8359
0
      insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8360
0
      N16_IMM3U (insn16));
8361
0
      goto done;
8362
0
    case 0x13:      /* lbi333 */
8363
0
      insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8364
0
      N16_IMM3U (insn16));
8365
0
      goto done;
8366
0
    case 0x11:      /* lwi333.bi */
8367
0
      insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8368
0
      N16_IMM3U (insn16));
8369
0
      goto done;
8370
0
    case 0x14:      /* swi333 */
8371
0
      insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8372
0
      N16_IMM3U (insn16));
8373
0
      goto done;
8374
0
    case 0x16:      /* shi333 */
8375
0
      insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8376
0
      N16_IMM3U (insn16));
8377
0
      goto done;
8378
0
    case 0x17:      /* sbi333 */
8379
0
      insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8380
0
      N16_IMM3U (insn16));
8381
0
      goto done;
8382
0
    case 0x15:      /* swi333.bi */
8383
0
      insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8384
0
      N16_IMM3U (insn16));
8385
0
      goto done;
8386
0
    case 0x18:      /* addri36.sp */
8387
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8388
0
      N16_IMM6U (insn16) << 2);
8389
0
      goto done;
8390
0
    case 0x19:      /* lwi45.fe */
8391
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8392
0
      (N16_IMM5U (insn16) - 32));
8393
0
      goto done;
8394
0
    case 0x1a:      /* lwi450 */
8395
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8396
0
      goto done;
8397
0
    case 0x1b:      /* swi450 */
8398
0
      insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8399
0
      goto done;
8400
8401
      /* These are r15 implied instructions.  */
8402
0
    case 0x30:      /* slts45 */
8403
0
      insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8404
0
      goto done;
8405
0
    case 0x31:      /* slt45 */
8406
0
      insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8407
0
      goto done;
8408
0
    case 0x32:      /* sltsi45 */
8409
0
      insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8410
0
      goto done;
8411
0
    case 0x33:      /* slti45 */
8412
0
      insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8413
0
      goto done;
8414
0
    case 0x34:      /* beqzs8, bnezs8 */
8415
0
      if (insn16 & N32_BIT (8))
8416
0
  insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8417
0
      else
8418
0
  insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8419
0
      goto done;
8420
8421
0
    case 0x35:      /* break16, ex9.it */
8422
      /* Only consider range of v3 break16.  */
8423
0
      insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8424
0
      goto done;
8425
8426
0
    case 0x3c:      /* ifcall9 */
8427
0
      insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8428
0
      goto done;
8429
0
    case 0x3d:      /* movpi45 */
8430
0
      insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8431
0
      goto done;
8432
8433
0
    case 0x3f:      /* MISC33 */
8434
0
      switch (insn16 & 0x7)
8435
0
  {
8436
0
  case 2:     /* neg33 */
8437
0
    insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8438
0
    break;
8439
0
  case 3:     /* not33 */
8440
0
    insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8441
0
         N16_RA3 (insn16));
8442
0
    break;
8443
0
  case 4:     /* mul33 */
8444
0
    insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8445
0
         N16_RA3 (insn16));
8446
0
    break;
8447
0
  case 5:     /* xor33 */
8448
0
    insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8449
0
         N16_RA3 (insn16));
8450
0
    break;
8451
0
  case 6:     /* and33 */
8452
0
    insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8453
0
         N16_RA3 (insn16));
8454
0
    break;
8455
0
  case 7:     /* or33 */
8456
0
    insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8457
0
         N16_RA3 (insn16));
8458
0
    break;
8459
0
  }
8460
0
      goto done;
8461
8462
0
    case 0xb:
8463
0
      switch (insn16 & 0x7)
8464
0
  {
8465
0
  case 0:     /* zeb33 */
8466
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8467
0
    break;
8468
0
  case 1:     /* zeh33 */
8469
0
    insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8470
0
    break;
8471
0
  case 2:     /* seb33 */
8472
0
    insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8473
0
    break;
8474
0
  case 3:     /* seh33 */
8475
0
    insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8476
0
    break;
8477
0
  case 4:     /* xlsb33 */
8478
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8479
0
    break;
8480
0
  case 5:     /* x11b33 */
8481
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8482
0
    break;
8483
0
  case 6:     /* bmski33 */
8484
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8485
0
          1 << __GF (insn16, 3, 3));
8486
0
    break;
8487
0
  case 7:     /* fexti33 */
8488
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8489
0
          (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8490
0
    break;
8491
0
  }
8492
0
      goto done;
8493
0
    }
8494
8495
0
  switch (__GF (insn16, 10, 5))
8496
0
    {
8497
0
    case 0x0:     /* mov55 or ifret16 */
8498
0
      if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8499
0
    && N16_RT5 (insn16) == N16_RA5 (insn16))
8500
0
  insn = N32_JREG (JR, 0, 0, 0, 3);
8501
0
      else
8502
0
  insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8503
0
      goto done;
8504
0
    case 0x1:     /* movi55 */
8505
0
      insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8506
0
      goto done;
8507
0
    case 0x1b:      /* addi10s (V2) */
8508
0
      insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8509
0
      goto done;
8510
0
    }
8511
8512
0
  switch (__GF (insn16, 11, 4))
8513
0
    {
8514
0
    case 0x7:     /* lwi37.fp/swi37.fp */
8515
0
      if (insn16 & N32_BIT (7)) /* swi37.fp */
8516
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8517
0
      else      /* lwi37.fp */
8518
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8519
0
      goto done;
8520
0
    case 0x8:     /* beqz38 */
8521
0
      insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8522
0
      goto done;
8523
0
    case 0x9:     /* bnez38 */
8524
0
      insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8525
0
      goto done;
8526
0
    case 0xa:     /* beqs38/j8, implied r5 */
8527
0
      if (N16_RT38 (insn16) == 5)
8528
0
  insn = N32_JI (J, N16_IMM8S (insn16));
8529
0
      else
8530
0
  insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8531
0
      goto done;
8532
0
    case 0xb:     /* bnes38 and others.  */
8533
0
      if (N16_RT38 (insn16) == 5)
8534
0
  {
8535
0
    switch (__GF (insn16, 5, 3))
8536
0
      {
8537
0
      case 0:   /* jr5 */
8538
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8539
0
        break;
8540
0
      case 4:   /* ret5 */
8541
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8542
0
        break;
8543
0
      case 1:   /* jral5 */
8544
0
        insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8545
0
        break;
8546
0
      case 2:   /* ex9.it imm5 */
8547
        /* ex9.it had no 32-bit variantl.  */
8548
0
        break;
8549
0
      case 5:   /* add5.pc */
8550
        /* add5.pc had no 32-bit variantl.  */
8551
0
        break;
8552
0
      }
8553
0
  }
8554
0
      else      /* bnes38 */
8555
0
  insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8556
0
      goto done;
8557
0
    case 0xe:     /* lwi37/swi37 */
8558
0
      if (insn16 & (1 << 7)) /* swi37.sp */
8559
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8560
0
      else      /* lwi37.sp */
8561
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8562
0
      goto done;
8563
0
    }
8564
8565
0
 done:
8566
0
  if (insn & 0x80000000)
8567
0
    return 0;
8568
8569
0
  if (pinsn)
8570
0
    *pinsn = insn;
8571
0
  return 1;
8572
0
}
8573

8574
8575
static bool
8576
is_sda_access_insn (unsigned long insn)
8577
0
{
8578
0
  switch (N32_OP6 (insn))
8579
0
    {
8580
0
    case N32_OP6_LWI:
8581
0
    case N32_OP6_LHI:
8582
0
    case N32_OP6_LHSI:
8583
0
    case N32_OP6_LBI:
8584
0
    case N32_OP6_LBSI:
8585
0
    case N32_OP6_SWI:
8586
0
    case N32_OP6_SHI:
8587
0
    case N32_OP6_SBI:
8588
0
    case N32_OP6_LWC:
8589
0
    case N32_OP6_LDC:
8590
0
    case N32_OP6_SWC:
8591
0
    case N32_OP6_SDC:
8592
0
      return true;
8593
0
    default:
8594
0
      ;
8595
0
    }
8596
0
  return false;
8597
0
}
8598
8599
static unsigned long
8600
turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8601
0
{
8602
0
  uint32_t oinsn = 0;
8603
8604
0
  switch (type)
8605
0
    {
8606
0
    case R_NDS32_GOT_LO12:
8607
0
    case R_NDS32_GOTOFF_LO12:
8608
0
    case R_NDS32_PLTREL_LO12:
8609
0
    case R_NDS32_PLT_GOTREL_LO12:
8610
0
    case R_NDS32_LO12S0_RELA:
8611
0
      switch (N32_OP6 (insn))
8612
0
  {
8613
0
  case N32_OP6_LBI:
8614
    /* lbi.gp */
8615
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8616
0
    break;
8617
0
  case N32_OP6_LBSI:
8618
    /* lbsi.gp */
8619
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8620
0
    break;
8621
0
  case N32_OP6_SBI:
8622
    /* sbi.gp */
8623
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8624
0
    break;
8625
0
  case N32_OP6_ORI:
8626
    /* addi.gp */
8627
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8628
0
    break;
8629
0
  }
8630
0
      break;
8631
8632
0
    case R_NDS32_LO12S1_RELA:
8633
0
      switch (N32_OP6 (insn))
8634
0
  {
8635
0
  case N32_OP6_LHI:
8636
    /* lhi.gp */
8637
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8638
0
    break;
8639
0
  case N32_OP6_LHSI:
8640
    /* lhsi.gp */
8641
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8642
0
    break;
8643
0
  case N32_OP6_SHI:
8644
    /* shi.gp */
8645
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8646
0
    break;
8647
0
  }
8648
0
      break;
8649
8650
0
    case R_NDS32_LO12S2_RELA:
8651
0
      switch (N32_OP6 (insn))
8652
0
  {
8653
0
  case N32_OP6_LWI:
8654
    /* lwi.gp */
8655
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8656
0
    break;
8657
0
  case N32_OP6_SWI:
8658
    /* swi.gp */
8659
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8660
0
    break;
8661
0
  }
8662
0
      break;
8663
8664
0
    case R_NDS32_LO12S2_DP_RELA:
8665
0
    case R_NDS32_LO12S2_SP_RELA:
8666
0
      oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8667
0
      break;
8668
0
    }
8669
8670
0
  if (oinsn)
8671
0
    *pinsn = oinsn;
8672
8673
0
  return oinsn != 0;
8674
0
}
8675
8676
/* Linker hasn't found the correct merge section for non-section symbol
8677
   in relax time, this work is left to the function elf_link_input_bfd().
8678
   So for non-section symbol, _bfd_merged_section_offset is also needed
8679
   to find the correct symbol address.  */
8680
8681
static bfd_vma
8682
nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8683
        asection **psec, Elf_Internal_Rela *rel)
8684
0
{
8685
0
  asection *sec = *psec;
8686
0
  bfd_vma relocation;
8687
8688
0
  relocation = (sec->output_section->vma
8689
0
    + sec->output_offset + sym->st_value);
8690
0
  if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8691
0
    {
8692
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8693
0
  rel->r_addend =
8694
0
    _bfd_merged_section_offset (abfd, psec,
8695
0
              sym->st_value + rel->r_addend);
8696
0
      else
8697
0
  rel->r_addend =
8698
0
    _bfd_merged_section_offset (abfd, psec,
8699
0
              sym->st_value) + rel->r_addend;
8700
8701
0
      if (sec != *psec)
8702
0
  {
8703
    /* If we have changed the section, and our original section is
8704
       marked with SEC_EXCLUDE, it means that the original
8705
       SEC_MERGE section has been completely subsumed in some
8706
       other SEC_MERGE section.  In this case, we need to leave
8707
       some info around for --emit-relocs.  */
8708
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
8709
0
      sec->kept_section = *psec;
8710
0
    sec = *psec;
8711
0
  }
8712
0
      rel->r_addend -= relocation;
8713
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
8714
0
    }
8715
0
  return relocation;
8716
0
}
8717
8718
static bfd_vma
8719
calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8720
        Elf_Internal_Sym *isymbuf,
8721
        Elf_Internal_Shdr *symtab_hdr)
8722
0
{
8723
0
  bfd_signed_vma foff;
8724
0
  bfd_vma symval, addend;
8725
0
  Elf_Internal_Rela irel_fn;
8726
0
  Elf_Internal_Sym *isym;
8727
0
  asection *sym_sec;
8728
8729
  /* Get the value of the symbol referred to by the reloc.  */
8730
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8731
0
    {
8732
      /* A local symbol.  */
8733
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
8734
8735
0
      if (isym->st_shndx == SHN_UNDEF)
8736
0
  sym_sec = bfd_und_section_ptr;
8737
0
      else if (isym->st_shndx == SHN_ABS)
8738
0
  sym_sec = bfd_abs_section_ptr;
8739
0
      else if (isym->st_shndx == SHN_COMMON)
8740
0
  sym_sec = bfd_com_section_ptr;
8741
0
      else
8742
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8743
0
      memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8744
0
      symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8745
0
      addend = irel_fn.r_addend;
8746
0
    }
8747
0
  else
8748
0
    {
8749
0
      unsigned long indx;
8750
0
      struct elf_link_hash_entry *h;
8751
8752
      /* An external symbol.  */
8753
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8754
0
      h = elf_sym_hashes (abfd)[indx];
8755
0
      BFD_ASSERT (h != NULL);
8756
8757
0
      while (h->root.type == bfd_link_hash_indirect
8758
0
       || h->root.type == bfd_link_hash_warning)
8759
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
8760
8761
0
      if (h->root.type != bfd_link_hash_defined
8762
0
    && h->root.type != bfd_link_hash_defweak)
8763
  /* This appears to be a reference to an undefined
8764
     symbol.  Just ignore it--it will be caught by the
8765
     regular reloc processing.  */
8766
0
  return 0;
8767
8768
0
      if (h->root.u.def.section->flags & SEC_MERGE)
8769
0
  {
8770
0
    sym_sec = h->root.u.def.section;
8771
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
8772
0
                 h->root.u.def.value);
8773
0
    symval = symval + sym_sec->output_section->vma
8774
0
       + sym_sec->output_offset;
8775
0
  }
8776
0
      else
8777
0
  symval = (h->root.u.def.value
8778
0
      + h->root.u.def.section->output_section->vma
8779
0
      + h->root.u.def.section->output_offset);
8780
0
      addend = irel->r_addend;
8781
0
    }
8782
8783
0
  foff = symval + addend;
8784
8785
0
  return foff;
8786
0
}
8787
8788
static int
8789
is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8790
        asection *sec, Elf_Internal_Rela *rel)
8791
0
{
8792
0
  bfd_byte *contents;
8793
0
  unsigned short insn16;
8794
8795
0
  if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8796
0
    return false;
8797
0
  contents = elf_section_data (sec)->this_hdr.contents;
8798
0
  insn16 = bfd_getb16 (contents + rel->r_offset);
8799
0
  if (insn16 == NDS32_NOP16)
8800
0
    return true;
8801
0
  return false;
8802
0
}
8803
8804
/* It checks whether the instruction could be converted to
8805
   16-bit form and returns the converted one.
8806
8807
   `internal_relocs' is supposed to be sorted.  */
8808
8809
static int
8810
is_convert_32_to_16 (bfd *abfd, asection *sec,
8811
         Elf_Internal_Rela *reloc,
8812
         Elf_Internal_Rela *internal_relocs,
8813
         Elf_Internal_Rela *irelend,
8814
         uint16_t *insn16)
8815
0
{
8816
0
#define NORMAL_32_TO_16 (1 << 0)
8817
0
#define SPECIAL_32_TO_16 (1 << 1)
8818
0
  bfd_byte *contents = NULL;
8819
0
  bfd_signed_vma off;
8820
0
  bfd_vma mem_addr;
8821
0
  uint32_t insn = 0;
8822
0
  Elf_Internal_Rela *pc_rel;
8823
0
  Elf_Internal_Shdr *symtab_hdr;
8824
0
  Elf_Internal_Sym *isymbuf = NULL;
8825
0
  int convert_type;
8826
0
  bfd_vma offset;
8827
8828
0
  if (reloc->r_offset + 4 > sec->size)
8829
0
    return false;
8830
8831
0
  offset = reloc->r_offset;
8832
8833
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true))
8834
0
    return false;
8835
0
  insn = bfd_getb32 (contents + offset);
8836
8837
0
  if (bfd_elf_nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8838
0
    convert_type = NORMAL_32_TO_16;
8839
0
  else if (special_convert_32_to_16 (insn, insn16, reloc))
8840
0
    convert_type = SPECIAL_32_TO_16;
8841
0
  else
8842
0
    return false;
8843
8844
0
  symtab_hdr = &elf_symtab_hdr (abfd);
8845
0
  if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8846
0
    return false;
8847
8848
  /* Find the first relocation of the same relocation-type,
8849
     so we iteratie them forward.  */
8850
0
  pc_rel = reloc;
8851
0
  while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8852
0
    pc_rel--;
8853
8854
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8855
0
    {
8856
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8857
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8858
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8859
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8860
0
  {
8861
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8862
0
    if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8863
0
        || off == 0)
8864
0
      return false;
8865
0
    break;
8866
0
  }
8867
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8868
0
  {
8869
    /* movi => movi55  */
8870
0
    mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8871
0
                 symtab_hdr);
8872
    /* mem_addr is unsigned, but the value should
8873
       be between [-16, 15].  */
8874
0
    if ((mem_addr + 0x10) >> 5)
8875
0
      return false;
8876
0
    break;
8877
0
  }
8878
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8879
0
         || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8880
0
  {
8881
    /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8882
       because it can be relaxed to addi for TLS_LE_ADD.  */
8883
0
    return false;
8884
0
  }
8885
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8886
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8887
0
         && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8888
0
         && convert_type == SPECIAL_32_TO_16)
8889
0
  {
8890
    /* fp-as-gp
8891
       We've selected a best fp-base for this access, so we can
8892
       always resolve it anyway.  Do nothing.  */
8893
0
    break;
8894
0
  }
8895
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8896
0
    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8897
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8898
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8899
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8900
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8901
0
  {
8902
    /* Prevent unresolved addi instruction translate
8903
       to addi45 or addi333.  */
8904
0
    return false;
8905
0
  }
8906
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8907
0
  {
8908
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8909
0
    if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8910
0
      return false;
8911
0
    break;
8912
0
  }
8913
0
    }
8914
8915
0
  return true;
8916
0
}
8917
8918
static void
8919
nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8920
        Elf_Internal_Rela *reloc,
8921
        Elf_Internal_Rela *internal_relocs,
8922
        Elf_Internal_Rela *irelend,
8923
        unsigned short insn16)
8924
0
{
8925
0
  Elf_Internal_Rela *pc_rel;
8926
0
  bfd_vma offset;
8927
8928
0
  offset = reloc->r_offset;
8929
0
  bfd_putb16 (insn16, contents + offset);
8930
  /* Find the first relocation of the same relocation-type,
8931
     so we iteratie them forward.  */
8932
0
  pc_rel = reloc;
8933
0
  while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8934
0
    pc_rel--;
8935
8936
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8937
0
    {
8938
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8939
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8940
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8941
0
  {
8942
0
    pc_rel->r_info =
8943
0
      ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8944
0
  }
8945
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8946
0
  pc_rel->r_info =
8947
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8948
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8949
0
  pc_rel->r_info =
8950
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8951
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8952
0
         || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8953
0
  pc_rel->r_info =
8954
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8955
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8956
0
  pc_rel->r_info =
8957
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8958
0
    }
8959
0
}
8960
8961
/* Find a relocation of type specified by `reloc_type'
8962
   of the same r_offset with reloc.
8963
   If not found, return irelend.
8964
8965
   Assuming relocations are sorted by r_offset,
8966
   we find the relocation from `reloc' backward untill relocs,
8967
   or find it from `reloc' forward untill irelend.  */
8968
8969
static Elf_Internal_Rela *
8970
find_relocs_at_address (Elf_Internal_Rela *reloc,
8971
      Elf_Internal_Rela *relocs,
8972
      Elf_Internal_Rela *irelend,
8973
      enum elf_nds32_reloc_type reloc_type)
8974
0
{
8975
0
  Elf_Internal_Rela *rel_t;
8976
8977
  /* Find backward.  */
8978
0
  for (rel_t = reloc;
8979
0
       rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8980
0
       rel_t--)
8981
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8982
0
      return rel_t;
8983
8984
  /* We didn't find it backward.  Try find it forward.  */
8985
0
  for (rel_t = reloc;
8986
0
       rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8987
0
       rel_t++)
8988
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8989
0
      return rel_t;
8990
8991
0
  return irelend;
8992
0
}
8993
8994
/* Find a relocation of specified type and offset.
8995
   `reloc' is just a refence point to find a relocation at specified offset.
8996
   If not found, return irelend.
8997
8998
   Assuming relocations are sorted by r_offset,
8999
   we find the relocation from `reloc' backward untill relocs,
9000
   or find it from `reloc' forward untill irelend.  */
9001
9002
static Elf_Internal_Rela *
9003
find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9004
           Elf_Internal_Rela *relocs,
9005
           Elf_Internal_Rela *irelend,
9006
           enum elf_nds32_reloc_type reloc_type,
9007
           bfd_vma offset_p)
9008
0
{
9009
0
  Elf_Internal_Rela *rel_t = NULL;
9010
9011
  /* First, we try to find a relocation of offset `offset_p',
9012
     and then we use find_relocs_at_address to find specific type.  */
9013
9014
0
  if (reloc->r_offset > offset_p)
9015
0
    {
9016
      /* Find backward.  */
9017
0
      for (rel_t = reloc;
9018
0
     rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9019
0
  /* Do nothing.  */;
9020
0
    }
9021
0
  else if (reloc->r_offset < offset_p)
9022
0
    {
9023
      /* Find forward.  */
9024
0
      for (rel_t = reloc;
9025
0
     rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9026
0
  /* Do nothing.  */;
9027
0
    }
9028
0
  else
9029
0
    rel_t = reloc;
9030
9031
  /* Not found?  */
9032
0
  if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9033
0
    return irelend;
9034
9035
0
  return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9036
0
}
9037
9038
typedef struct nds32_elf_blank nds32_elf_blank_t;
9039
struct nds32_elf_blank
9040
{
9041
  /* Where the blank begins.  */
9042
  bfd_vma offset;
9043
  /* The size of the blank.  */
9044
  bfd_vma size;
9045
  /* The accumulative size before this blank.  */
9046
  bfd_vma total_size;
9047
  nds32_elf_blank_t *next;
9048
  nds32_elf_blank_t *prev;
9049
};
9050
9051
static nds32_elf_blank_t *blank_free_list = NULL;
9052
9053
static nds32_elf_blank_t *
9054
create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9055
0
{
9056
0
  nds32_elf_blank_t *blank_t;
9057
9058
0
  if (blank_free_list)
9059
0
    {
9060
0
      blank_t = blank_free_list;
9061
0
      blank_free_list = blank_free_list->next;
9062
0
    }
9063
0
  else
9064
0
    blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9065
9066
0
  if (blank_t == NULL)
9067
0
    return NULL;
9068
9069
0
  blank_t->offset = offset_p;
9070
0
  blank_t->size = size_p;
9071
0
  blank_t->total_size = 0;
9072
0
  blank_t->next = NULL;
9073
0
  blank_t->prev = NULL;
9074
9075
0
  return blank_t;
9076
0
}
9077
9078
static void
9079
remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9080
0
{
9081
0
  if (blank_free_list)
9082
0
    {
9083
0
      blank_free_list->prev = blank_p;
9084
0
      blank_p->next = blank_free_list;
9085
0
    }
9086
0
  else
9087
0
    blank_p->next = NULL;
9088
9089
0
  blank_p->prev = NULL;
9090
0
  blank_free_list = blank_p;
9091
0
}
9092
9093
static void
9094
clean_nds32_elf_blank (void)
9095
0
{
9096
0
  nds32_elf_blank_t *blank_t;
9097
9098
0
  while (blank_free_list)
9099
0
    {
9100
0
      blank_t = blank_free_list;
9101
0
      blank_free_list = blank_free_list->next;
9102
0
      free (blank_t);
9103
0
    }
9104
0
}
9105
9106
static nds32_elf_blank_t *
9107
search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9108
0
{
9109
0
  nds32_elf_blank_t *blank_t;
9110
9111
0
  if (!blank_p)
9112
0
    return NULL;
9113
0
  blank_t = blank_p;
9114
9115
0
  while (blank_t && addr < blank_t->offset)
9116
0
    blank_t = blank_t->prev;
9117
0
  while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9118
0
    blank_t = blank_t->next;
9119
9120
0
  return blank_t;
9121
0
}
9122
9123
static bfd_vma
9124
get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9125
         int overwrite)
9126
0
{
9127
0
  nds32_elf_blank_t *blank_t;
9128
9129
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9130
0
  if (!blank_t)
9131
0
    return 0;
9132
9133
0
  if (overwrite)
9134
0
    *blank_p = blank_t;
9135
9136
0
  if (addr < blank_t->offset + blank_t->size)
9137
0
    return blank_t->total_size + (addr - blank_t->offset);
9138
0
  else
9139
0
    return blank_t->total_size + blank_t->size;
9140
0
}
9141
9142
static bool
9143
insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9144
0
{
9145
0
  nds32_elf_blank_t *blank_t, *blank_t2;
9146
9147
0
  if (!*blank_p)
9148
0
    {
9149
0
      *blank_p = create_nds32_elf_blank (addr, len);
9150
0
      return *blank_p != NULL;
9151
0
    }
9152
9153
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9154
9155
0
  if (blank_t == NULL)
9156
0
    {
9157
0
      blank_t = create_nds32_elf_blank (addr, len);
9158
0
      if (!blank_t)
9159
0
  return false;
9160
0
      while ((*blank_p)->prev != NULL)
9161
0
  *blank_p = (*blank_p)->prev;
9162
0
      blank_t->next = *blank_p;
9163
0
      (*blank_p)->prev = blank_t;
9164
0
      (*blank_p) = blank_t;
9165
0
      return true;
9166
0
    }
9167
9168
0
  if (addr < blank_t->offset + blank_t->size)
9169
0
    {
9170
      /* Extend the origin blank.  */
9171
0
      if (addr + len > blank_t->offset + blank_t->size)
9172
0
  blank_t->size = addr + len - blank_t->offset;
9173
0
    }
9174
0
  else
9175
0
    {
9176
0
      blank_t2 = create_nds32_elf_blank (addr, len);
9177
0
      if (!blank_t2)
9178
0
  return false;
9179
0
      if (blank_t->next)
9180
0
  {
9181
0
    blank_t->next->prev = blank_t2;
9182
0
    blank_t2->next = blank_t->next;
9183
0
  }
9184
0
      blank_t2->prev = blank_t;
9185
0
      blank_t->next = blank_t2;
9186
0
      *blank_p = blank_t2;
9187
0
    }
9188
9189
0
  return true;
9190
0
}
9191
9192
static bool
9193
insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9194
             bfd_vma len)
9195
0
{
9196
0
  nds32_elf_blank_t *blank_t;
9197
9198
0
  if (!insert_nds32_elf_blank (blank_p, addr, len))
9199
0
    return false;
9200
9201
0
  blank_t = *blank_p;
9202
9203
0
  if (!blank_t->prev)
9204
0
    {
9205
0
      blank_t->total_size = 0;
9206
0
      blank_t = blank_t->next;
9207
0
    }
9208
9209
0
  while (blank_t)
9210
0
    {
9211
0
      blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9212
0
      blank_t = blank_t->next;
9213
0
    }
9214
9215
0
  return true;
9216
0
}
9217
9218
static void
9219
calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9220
0
{
9221
0
  nds32_elf_blank_t *blank_t;
9222
0
  bfd_vma total_size = 0;
9223
9224
0
  if (!blank_p)
9225
0
    return;
9226
9227
0
  blank_t = blank_p;
9228
0
  while (blank_t->prev)
9229
0
    blank_t = blank_t->prev;
9230
0
  while (blank_t)
9231
0
    {
9232
0
      blank_t->total_size = total_size;
9233
0
      total_size += blank_t->size;
9234
0
      blank_t = blank_t->next;
9235
0
    }
9236
0
}
9237
9238
static bool
9239
nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9240
             nds32_elf_blank_t *blank_p)
9241
0
{
9242
0
  Elf_Internal_Shdr *symtab_hdr;  /* Symbol table header of this bfd.  */
9243
0
  Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd.  */
9244
0
  Elf_Internal_Sym *isymend;    /* Symbol entry iterator.  */
9245
0
  unsigned int sec_shndx;   /* The section the be relaxed.  */
9246
0
  bfd_byte *contents;     /* Contents data of iterating section.  */
9247
0
  Elf_Internal_Rela *internal_relocs;
9248
0
  Elf_Internal_Rela *irel;
9249
0
  Elf_Internal_Rela *irelend;
9250
0
  struct elf_link_hash_entry **sym_hashes;
9251
0
  struct elf_link_hash_entry **end_hashes;
9252
0
  unsigned int symcount;
9253
0
  asection *sect;
9254
0
  nds32_elf_blank_t *blank_t;
9255
0
  nds32_elf_blank_t *blank_t2;
9256
0
  nds32_elf_blank_t *blank_head;
9257
9258
0
  blank_head = blank_t = blank_p;
9259
0
  while (blank_head->prev != NULL)
9260
0
    blank_head = blank_head->prev;
9261
0
  while (blank_t->next != NULL)
9262
0
    blank_t = blank_t->next;
9263
9264
0
  if (blank_t->offset + blank_t->size <= sec->size)
9265
0
    {
9266
0
      blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9267
0
      blank_t->next->prev = blank_t;
9268
0
    }
9269
0
  if (blank_head->offset > 0)
9270
0
    {
9271
0
      blank_head->prev = create_nds32_elf_blank (0, 0);
9272
0
      blank_head->prev->next = blank_head;
9273
0
      blank_head = blank_head->prev;
9274
0
    }
9275
9276
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9277
9278
  /* The deletion must stop at the next ALIGN reloc for an alignment
9279
     power larger than the number of bytes we are deleting.  */
9280
9281
0
  symtab_hdr = &elf_symtab_hdr (abfd);
9282
0
  if (!nds32_get_local_syms (abfd, sec, &isym))
9283
0
    return false;
9284
9285
0
  if (isym == NULL)
9286
0
    {
9287
0
      isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9288
0
           symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9289
0
      symtab_hdr->contents = (bfd_byte *) isym;
9290
0
    }
9291
9292
0
  if (isym == NULL || symtab_hdr->sh_info == 0)
9293
0
    return false;
9294
9295
0
  blank_t = blank_head;
9296
0
  calc_nds32_blank_total (blank_head);
9297
9298
0
  for (sect = abfd->sections; sect != NULL; sect = sect->next)
9299
0
    {
9300
      /* Adjust all the relocs.  */
9301
9302
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
9303
0
      internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9304
0
               true /* keep_memory */);
9305
0
      irelend = internal_relocs + sect->reloc_count;
9306
9307
0
      blank_t = blank_head;
9308
0
      blank_t2 = blank_head;
9309
9310
0
      if (!(sect->flags & SEC_RELOC))
9311
0
  continue;
9312
9313
0
      contents = NULL;
9314
0
      nds32_get_section_contents (abfd, sect, &contents, true);
9315
9316
0
      for (irel = internal_relocs; irel < irelend; irel++)
9317
0
  {
9318
0
    bfd_vma raddr;
9319
9320
0
    if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9321
0
        && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9322
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9323
0
      {
9324
0
        unsigned long val = 0;
9325
0
        unsigned long mask;
9326
0
        long before, between;
9327
0
        long offset = 0;
9328
9329
0
        switch (ELF32_R_TYPE (irel->r_info))
9330
0
    {
9331
0
    case R_NDS32_DIFF8:
9332
0
      offset = bfd_get_8 (abfd, contents + irel->r_offset);
9333
0
      break;
9334
0
    case R_NDS32_DIFF16:
9335
0
      offset = bfd_get_16 (abfd, contents + irel->r_offset);
9336
0
      break;
9337
0
    case R_NDS32_DIFF32:
9338
0
      val = bfd_get_32 (abfd, contents + irel->r_offset);
9339
      /* Get the signed bit and mask for the high part.  The
9340
         gcc will alarm when right shift 32-bit since the
9341
         type size of long may be 32-bit.  */
9342
0
      mask = 0 - (val >> 31);
9343
0
      if (mask)
9344
0
        offset = (val | (mask - 0xffffffff));
9345
0
      else
9346
0
        offset = val;
9347
0
      break;
9348
0
    default:
9349
0
      BFD_ASSERT (0);
9350
0
    }
9351
9352
        /*      DIFF value
9353
    0      |encoded in location|
9354
    |------------|-------------------|---------
9355
          sym+off(addend)
9356
    -- before ---| *****************
9357
    --------------------- between ---|
9358
9359
    We only care how much data are relax between DIFF,
9360
    marked as ***.  */
9361
9362
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9363
0
        between = get_nds32_elf_blank_total (&blank_t,
9364
0
               irel->r_addend + offset, 0);
9365
0
        if (between == before)
9366
0
    goto done_adjust_diff;
9367
9368
0
        switch (ELF32_R_TYPE (irel->r_info))
9369
0
    {
9370
0
    case R_NDS32_DIFF8:
9371
0
      bfd_put_8 (abfd, offset - (between - before),
9372
0
           contents + irel->r_offset);
9373
0
      break;
9374
0
    case R_NDS32_DIFF16:
9375
0
      bfd_put_16 (abfd, offset - (between - before),
9376
0
            contents + irel->r_offset);
9377
0
      break;
9378
0
    case R_NDS32_DIFF32:
9379
0
      bfd_put_32 (abfd, offset - (between - before),
9380
0
            contents + irel->r_offset);
9381
0
      break;
9382
0
    }
9383
0
      }
9384
0
    else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9385
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9386
0
      {
9387
0
        bfd_vma val;
9388
0
        unsigned long before, between;
9389
0
        bfd_byte *endp, *p;
9390
9391
0
        p = contents + irel->r_offset;
9392
0
        endp = contents + sec->size;
9393
0
        val = _bfd_safe_read_leb128 (abfd, &p, false, endp);
9394
9395
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9396
0
        between = get_nds32_elf_blank_total (&blank_t,
9397
0
               irel->r_addend + val, 0);
9398
0
        if (between == before)
9399
0
    goto done_adjust_diff;
9400
9401
0
        endp = p - 1;
9402
0
        p = contents + irel->r_offset;
9403
0
        memset (p, 0x80, endp - p);
9404
0
        *endp = 0;
9405
0
        p = write_uleb128 (p, val - (between - before)) - 1;
9406
0
        if (p < endp)
9407
0
    *p |= 0x80;
9408
0
      }
9409
0
  done_adjust_diff:
9410
9411
0
    if (sec == sect)
9412
0
      {
9413
0
        raddr = irel->r_offset;
9414
0
        irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9415
0
                 irel->r_offset, 1);
9416
9417
0
        if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9418
0
    continue;
9419
0
        if (blank_t2 && blank_t2->next
9420
0
      && (blank_t2->offset > raddr
9421
0
          || blank_t2->next->offset <= raddr))
9422
0
    _bfd_error_handler
9423
0
      (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9424
0
       abfd);
9425
9426
        /* Mark reloc in deleted portion as NONE.
9427
     For some relocs like R_NDS32_LABEL that doesn't modify the
9428
     content in the section.  R_NDS32_LABEL doesn't belong to the
9429
     instruction in the section, so we should preserve it.  */
9430
0
        if (raddr >= blank_t2->offset
9431
0
      && raddr < blank_t2->offset + blank_t2->size
9432
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9433
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9434
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9435
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9436
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9437
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9438
0
    {
9439
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9440
0
                 R_NDS32_NONE);
9441
0
      continue;
9442
0
    }
9443
0
      }
9444
9445
0
    if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9446
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9447
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9448
0
      continue;
9449
9450
0
    if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9451
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9452
0
        && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9453
0
      {
9454
0
        if (irel->r_addend <= sec->size)
9455
0
    irel->r_addend -=
9456
0
      get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9457
0
      }
9458
0
  }
9459
0
    }
9460
9461
  /* Adjust the local symbols defined in this section.  */
9462
0
  blank_t = blank_head;
9463
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9464
0
    {
9465
0
      if (isym->st_shndx == sec_shndx)
9466
0
  {
9467
0
    if (isym->st_value <= sec->size)
9468
0
      {
9469
0
        bfd_vma ahead;
9470
0
        bfd_vma orig_addr = isym->st_value;
9471
9472
0
        ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9473
0
        isym->st_value -= ahead;
9474
9475
        /* Adjust function size.  */
9476
0
        if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9477
0
      && isym->st_size > 0)
9478
0
    isym->st_size -=
9479
0
      get_nds32_elf_blank_total
9480
0
      (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9481
0
      }
9482
0
  }
9483
0
    }
9484
9485
  /* Now adjust the global symbols defined in this section.  */
9486
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9487
0
        - symtab_hdr->sh_info);
9488
0
  sym_hashes = elf_sym_hashes (abfd);
9489
0
  end_hashes = sym_hashes + symcount;
9490
0
  blank_t = blank_head;
9491
0
  for (; sym_hashes < end_hashes; sym_hashes++)
9492
0
    {
9493
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
9494
9495
0
      if ((sym_hash->root.type == bfd_link_hash_defined
9496
0
     || sym_hash->root.type == bfd_link_hash_defweak)
9497
0
    && sym_hash->root.u.def.section == sec)
9498
0
  {
9499
0
    if (sym_hash->root.u.def.value <= sec->size)
9500
0
      {
9501
0
        bfd_vma ahead;
9502
0
        bfd_vma orig_addr = sym_hash->root.u.def.value;
9503
9504
0
        ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9505
0
        sym_hash->root.u.def.value -= ahead;
9506
9507
        /* Adjust function size.  */
9508
0
        if (sym_hash->type == STT_FUNC)
9509
0
    sym_hash->size -=
9510
0
      get_nds32_elf_blank_total
9511
0
      (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9512
9513
0
      }
9514
0
  }
9515
0
    }
9516
9517
0
  contents = elf_section_data (sec)->this_hdr.contents;
9518
0
  blank_t = blank_head;
9519
0
  while (blank_t->next)
9520
0
    {
9521
      /* Actually delete the bytes.  */
9522
9523
      /* If current blank is the last blank overlap with current section,
9524
   go to finish process.  */
9525
0
      if (sec->size <= (blank_t->next->offset))
9526
0
  break;
9527
9528
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9529
0
         contents + blank_t->offset + blank_t->size,
9530
0
         blank_t->next->offset - (blank_t->offset + blank_t->size));
9531
9532
0
      blank_t = blank_t->next;
9533
0
    }
9534
9535
0
  if (sec->size > (blank_t->offset + blank_t->size))
9536
0
    {
9537
      /* There are remaining code between blank and section boundary.
9538
   Move the remaining code to appropriate location.  */
9539
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9540
0
         contents + blank_t->offset + blank_t->size,
9541
0
         sec->size - (blank_t->offset + blank_t->size));
9542
0
      sec->size -= blank_t->total_size + blank_t->size;
9543
0
    }
9544
0
  else
9545
    /* This blank is not entirely included in the section,
9546
       reduce the section size by only part of the blank size.  */
9547
0
    sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9548
9549
0
  while (blank_head)
9550
0
    {
9551
0
      blank_t = blank_head;
9552
0
      blank_head = blank_head->next;
9553
0
      remove_nds32_elf_blank (blank_t);
9554
0
    }
9555
9556
0
  return true;
9557
0
}
9558
9559
/* Get the contents of a section.  */
9560
9561
static int
9562
nds32_get_section_contents (bfd *abfd, asection *sec,
9563
          bfd_byte **contents_p, bool cache)
9564
65
{
9565
  /* Get the section contents.  */
9566
65
  if (elf_section_data (sec)->this_hdr.contents != NULL)
9567
0
    *contents_p = elf_section_data (sec)->this_hdr.contents;
9568
65
  else
9569
65
    {
9570
65
      if (!bfd_get_full_section_contents (abfd, sec, contents_p))
9571
1
  return false;
9572
64
      if (cache)
9573
0
  elf_section_data (sec)->this_hdr.contents = *contents_p;
9574
64
    }
9575
9576
64
  return true;
9577
65
}
9578
9579
/* Get the contents of the internal symbol of abfd.  */
9580
9581
static int
9582
nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9583
          Elf_Internal_Sym **isymbuf_p)
9584
0
{
9585
0
  Elf_Internal_Shdr *symtab_hdr;
9586
0
  symtab_hdr = &elf_symtab_hdr (abfd);
9587
9588
  /* Read this BFD's local symbols if we haven't done so already.  */
9589
0
  if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9590
0
    {
9591
0
      *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9592
0
      if (*isymbuf_p == NULL)
9593
0
  {
9594
0
    *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9595
0
               symtab_hdr->sh_info, 0,
9596
0
               NULL, NULL, NULL);
9597
0
    if (*isymbuf_p == NULL)
9598
0
      return false;
9599
0
  }
9600
0
    }
9601
0
  symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9602
9603
0
  return true;
9604
0
}
9605
9606
/* Range of small data.  */
9607
static bfd_vma sdata_range[2][2];
9608
static bfd_vma const sdata_init_range[2] =
9609
{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
9610
9611
static int
9612
nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9613
         bfd_byte *contents, bfd_vma addr)
9614
0
{
9615
0
  unsigned long insn = bfd_getb32 (contents + addr);
9616
9617
0
  if (insn & 0x80000000)
9618
0
    return 2;
9619
9620
0
  return 4;
9621
0
}
9622
9623
/* Set the gp relax range.  We have to measure the safe range
9624
   to do gp relaxation.  */
9625
9626
static void
9627
relax_range_measurement (bfd *abfd, struct bfd_link_info *link_info)
9628
0
{
9629
0
  asection *sec_f, *sec_b;
9630
  /* For upper bound.   */
9631
0
  bfd_vma maxpgsz;
9632
0
  bfd_vma align;
9633
0
  static int decide_relax_range = 0;
9634
0
  int i;
9635
0
  int range_number = ARRAY_SIZE (sdata_init_range);
9636
9637
0
  if (decide_relax_range)
9638
0
    return;
9639
0
  decide_relax_range = 1;
9640
9641
0
  if (sda_rela_sec == NULL)
9642
0
    {
9643
      /* Since there is no data sections, we assume the range is page size.  */
9644
0
      for (i = 0; i < range_number; i++)
9645
0
  {
9646
0
    sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9647
0
    sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9648
0
  }
9649
0
      return;
9650
0
    }
9651
9652
  /* Get the biggest alignment power after the gp located section.  */
9653
0
  sec_f = sda_rela_sec->output_section;
9654
0
  sec_b = sec_f->next;
9655
0
  align = 0;
9656
0
  while (sec_b != NULL)
9657
0
    {
9658
0
      if ((unsigned)(1 << sec_b->alignment_power) > align)
9659
0
  align = (1 << sec_b->alignment_power);
9660
0
      sec_b = sec_b->next;
9661
0
    }
9662
9663
0
  if (link_info != NULL)
9664
0
    maxpgsz = link_info->maxpagesize;
9665
0
  else
9666
0
    maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9667
  /* I guess we can not determine the section before
9668
     gp located section, so we assume the align is max page size.  */
9669
0
  for (i = 0; i < range_number; i++)
9670
0
    {
9671
0
      sdata_range[i][1] = sdata_init_range[i] - align;
9672
0
      BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9673
0
      sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9674
0
      BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9675
0
    }
9676
0
}
9677
9678
/* These are macros used to check flags encoded in r_addend.
9679
   They are only used by nds32_elf_relax_section ().  */
9680
0
#define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
9681
0
#define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
9682
#define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
9683
0
#define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
9684
9685
static const char * unrecognized_reloc_msg =
9686
  /* xgettext:c-format */
9687
  N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9688
9689
/* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
9690
9691
static bool
9692
nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9693
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9694
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9695
         Elf_Internal_Shdr *symtab_hdr)
9696
0
{
9697
  /* There are 3 variations for LONGCALL1
9698
     case 4-4-2; 16-bit on, optimize off or optimize for space
9699
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9700
     ori   ta, ta, lo12(symbol) ; LO12S0
9701
     jral5 ta     ;
9702
9703
     case 4-4-4; 16-bit off, optimize don't care
9704
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9705
     ori   ta, ta, lo12(symbol) ; LO12S0
9706
     jral  ta     ;
9707
9708
     case 4-4-4; 16-bit on, optimize for speed
9709
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9710
     ori   ta, ta, lo12(symbol) ; LO12S0
9711
     jral  ta     ;
9712
     Check code for -mlong-calls output.  */
9713
9714
  /* Get the reloc for the address from which the register is
9715
     being loaded.  This reloc will tell us which function is
9716
     actually being called.  */
9717
9718
0
  bfd_vma laddr;
9719
0
  int seq_len;  /* Original length of instruction sequence.  */
9720
0
  uint32_t insn;
9721
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9722
0
  bfd_signed_vma foff;
9723
0
  uint16_t insn16;
9724
9725
0
  irelend = internal_relocs + sec->reloc_count;
9726
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9727
0
  laddr = irel->r_offset;
9728
0
  *insn_len = seq_len;
9729
9730
0
  hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9731
0
             R_NDS32_HI20_RELA, laddr);
9732
0
  lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9733
0
             R_NDS32_LO12S0_ORI_RELA,
9734
0
             laddr + 4);
9735
9736
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9737
0
    {
9738
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9739
0
        (uint64_t) irel->r_offset);
9740
0
      return false;
9741
0
    }
9742
9743
  /* Get the value of the symbol referred to by the reloc.  */
9744
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9745
9746
  /* This condition only happened when symbol is undefined.  */
9747
0
  if (foff == 0
9748
0
      || foff < -CONSERVATIVE_24BIT_S1
9749
0
      || foff >= CONSERVATIVE_24BIT_S1)
9750
0
    return false;
9751
9752
  /* Relax to: jal symbol; 25_PCREL.  */
9753
  /* For simplicity of coding, we are going to modify the section
9754
     contents, the section relocs, and the BFD symbol table.  We
9755
     must tell the rest of the code not to free up this
9756
     information.  It would be possible to instead create a table
9757
     of changes which have to be made, as is done in coff-mips.c;
9758
     that would be more work, but would require less memory when
9759
     the linker is run.  */
9760
9761
  /* Replace the long call with a jal.  */
9762
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9763
0
             R_NDS32_25_PCREL_RELA);
9764
0
  irel->r_addend = hi_irelfn->r_addend;
9765
9766
  /* We don't resolve this here but resolve it in relocate_section.  */
9767
0
  insn = INSN_JAL;
9768
0
  bfd_putb32 (insn, contents + irel->r_offset);
9769
9770
0
  hi_irelfn->r_info =
9771
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9772
0
  lo_irelfn->r_info =
9773
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9774
0
  *insn_len = 4;
9775
9776
0
  if (seq_len & 0x2)
9777
0
    {
9778
0
      insn16 = NDS32_NOP16;
9779
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9780
0
      lo_irelfn->r_info =
9781
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9782
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9783
0
      *insn_len += 2;
9784
0
    }
9785
0
  return true;
9786
0
}
9787
9788
0
#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9789
/* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
9790
9791
static bool
9792
nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9793
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9794
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9795
         Elf_Internal_Shdr *symtab_hdr)
9796
0
{
9797
  /* bltz  rt, .L1   ; LONGCALL2
9798
     jal   symbol   ; 25_PCREL
9799
     .L1: */
9800
9801
  /* Get the reloc for the address from which the register is
9802
     being loaded.  This reloc will tell us which function is
9803
     actually being called.  */
9804
9805
0
  bfd_vma laddr;
9806
0
  uint32_t insn;
9807
0
  Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9808
0
  bfd_signed_vma foff;
9809
9810
0
  irelend = internal_relocs + sec->reloc_count;
9811
0
  laddr = irel->r_offset;
9812
0
  i1_irelfn =
9813
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9814
0
         R_NDS32_25_PCREL_RELA, laddr + 4);
9815
9816
0
  if (i1_irelfn == irelend)
9817
0
    {
9818
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9819
0
        (uint64_t) irel->r_offset);
9820
0
      return false;
9821
0
    }
9822
9823
0
  insn = bfd_getb32 (contents + laddr);
9824
9825
  /* Get the value of the symbol referred to by the reloc.  */
9826
0
  foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9827
9828
0
  if (foff == 0
9829
0
      || foff < -CONSERVATIVE_16BIT_S1
9830
0
      || foff >= CONSERVATIVE_16BIT_S1)
9831
0
    return false;
9832
9833
  /* Relax to bgezal   rt, label ; 17_PCREL
9834
     or   bltzal   rt, label ; 17_PCREL */
9835
9836
  /* Convert to complimentary conditional call.  */
9837
0
  insn = CONVERT_CONDITION_CALL (insn);
9838
9839
  /* For simplicity of coding, we are going to modify the section
9840
     contents, the section relocs, and the BFD symbol table.  We
9841
     must tell the rest of the code not to free up this
9842
     information.  It would be possible to instead create a table
9843
     of changes which have to be made, as is done in coff-mips.c;
9844
     that would be more work, but would require less memory when
9845
     the linker is run.  */
9846
9847
  /* Clean unnessary relocations.  */
9848
0
  i1_irelfn->r_info =
9849
0
    ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9850
0
  cond_irelfn =
9851
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9852
0
         R_NDS32_17_PCREL_RELA, laddr);
9853
0
  if (cond_irelfn != irelend)
9854
0
    cond_irelfn->r_info =
9855
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9856
9857
  /* Replace the long call with a bgezal.  */
9858
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9859
0
             R_NDS32_17_PCREL_RELA);
9860
0
  irel->r_addend = i1_irelfn->r_addend;
9861
9862
0
  bfd_putb32 (insn, contents + irel->r_offset);
9863
9864
0
  *insn_len = 4;
9865
0
  return true;
9866
0
}
9867
9868
/* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9869
9870
static bool
9871
nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9872
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9873
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9874
         Elf_Internal_Shdr *symtab_hdr)
9875
0
{
9876
  /* There are 3 variations for LONGCALL3
9877
     case 4-4-4-2; 16-bit on, optimize off or optimize for space
9878
     bltz  rt,   $1      ; LONGCALL3
9879
     sethi ta,   hi20(symbol)    ; HI20
9880
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9881
     jral5 ta        ;
9882
     $1
9883
9884
     case 4-4-4-4; 16-bit off, optimize don't care
9885
     bltz  rt,   $1      ; LONGCALL3
9886
     sethi ta,   hi20(symbol)    ; HI20
9887
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9888
     jral  ta        ;
9889
     $1
9890
9891
     case 4-4-4-4; 16-bit on, optimize for speed
9892
     bltz  rt,   $1      ; LONGCALL3
9893
     sethi ta,   hi20(symbol)    ; HI20
9894
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9895
     jral  ta        ;
9896
     $1 */
9897
9898
  /* Get the reloc for the address from which the register is
9899
     being loaded.  This reloc will tell us which function is
9900
     actually being called.  */
9901
9902
0
  bfd_vma laddr;
9903
0
  int seq_len;  /* Original length of instruction sequence.  */
9904
0
  uint32_t insn;
9905
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9906
0
  bfd_signed_vma foff;
9907
0
  uint16_t insn16;
9908
9909
0
  irelend = internal_relocs + sec->reloc_count;
9910
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9911
0
  laddr = irel->r_offset;
9912
0
  *insn_len = seq_len;
9913
9914
0
  hi_irelfn =
9915
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9916
0
         R_NDS32_HI20_RELA, laddr + 4);
9917
0
  lo_irelfn =
9918
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9919
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9920
9921
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9922
0
    {
9923
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9924
0
        (uint64_t) irel->r_offset);
9925
0
      return false;
9926
0
    }
9927
9928
  /* Get the value of the symbol referred to by the reloc.  */
9929
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9930
9931
0
  if (foff == 0
9932
0
      || foff < -CONSERVATIVE_24BIT_S1
9933
0
      || foff >= CONSERVATIVE_24BIT_S1)
9934
0
    return false;
9935
9936
0
  insn = bfd_getb32 (contents + laddr);
9937
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9938
0
    {
9939
      /* Relax to  bgezal   rt, label ; 17_PCREL
9940
   or    bltzal   rt, label ; 17_PCREL */
9941
9942
      /* Convert to complimentary conditional call.  */
9943
0
      insn = CONVERT_CONDITION_CALL (insn);
9944
0
      bfd_putb32 (insn, contents + irel->r_offset);
9945
9946
0
      *insn_len = 4;
9947
0
      irel->r_info =
9948
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9949
0
      hi_irelfn->r_info =
9950
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9951
0
      lo_irelfn->r_info =
9952
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9953
9954
0
      cond_irelfn =
9955
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
9956
0
             R_NDS32_17_PCREL_RELA, laddr);
9957
0
      if (cond_irelfn != irelend)
9958
0
  {
9959
0
    cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9960
0
                R_NDS32_17_PCREL_RELA);
9961
0
    cond_irelfn->r_addend = hi_irelfn->r_addend;
9962
0
  }
9963
9964
0
      if (seq_len & 0x2)
9965
0
  {
9966
0
    insn16 = NDS32_NOP16;
9967
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9968
0
    hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9969
0
              R_NDS32_INSN16);
9970
0
    hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9971
0
    insn_len += 2;
9972
0
  }
9973
0
    }
9974
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9975
0
    {
9976
      /* Relax to the following instruction sequence
9977
   bltz  rt,   $1 ; LONGCALL2
9978
   jal   symbol   ; 25_PCREL
9979
   $1 */
9980
0
      *insn_len = 8;
9981
0
      insn = INSN_JAL;
9982
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9983
9984
0
      hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9985
0
          R_NDS32_25_PCREL_RELA);
9986
0
      irel->r_info =
9987
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9988
9989
0
      lo_irelfn->r_info =
9990
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9991
9992
0
      if (seq_len & 0x2)
9993
0
  {
9994
0
    insn16 = NDS32_NOP16;
9995
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9996
0
    lo_irelfn->r_info =
9997
0
      ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9998
0
    lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9999
0
    insn_len += 2;
10000
0
  }
10001
0
    }
10002
0
  return true;
10003
0
}
10004
10005
/* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
10006
10007
static bool
10008
nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10009
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10010
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10011
         Elf_Internal_Shdr *symtab_hdr)
10012
0
{
10013
  /* There are 3 variations for LONGJUMP1
10014
     case 4-4-2; 16-bit bit on, optimize off or optimize for space
10015
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10016
     ori   ta, ta, lo12(symbol)  ; LO12S0
10017
     jr5   ta      ;
10018
10019
     case 4-4-4; 16-bit off, optimize don't care
10020
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10021
     ori   ta, ta, lo12(symbol)  ; LO12S0
10022
     jr    ta      ;
10023
10024
     case 4-4-4; 16-bit on, optimize for speed
10025
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10026
     ori   ta, ta, lo12(symbol)  ; LO12S0
10027
     jr    ta      ;  */
10028
10029
  /* Get the reloc for the address from which the register is
10030
     being loaded.  This reloc will tell us which function is
10031
     actually being called.  */
10032
10033
0
  bfd_vma laddr;
10034
0
  int seq_len;  /* Original length of instruction sequence.  */
10035
0
  int insn16_on;  /* 16-bit on/off.  */
10036
0
  uint32_t insn;
10037
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10038
0
  bfd_signed_vma foff;
10039
0
  uint16_t insn16;
10040
0
  unsigned long reloc;
10041
10042
0
  irelend = internal_relocs + sec->reloc_count;
10043
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10044
0
  laddr = irel->r_offset;
10045
0
  *insn_len = seq_len;
10046
0
  insn16_on = IS_16BIT_ON (irel->r_addend);
10047
10048
0
  hi_irelfn =
10049
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10050
0
         R_NDS32_HI20_RELA, laddr);
10051
0
  lo_irelfn =
10052
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10053
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10054
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
10055
0
    {
10056
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10057
0
        (uint64_t) irel->r_offset);
10058
0
      return false;
10059
0
    }
10060
10061
  /* Get the value of the symbol referred to by the reloc.  */
10062
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10063
10064
0
  if (foff == 0
10065
0
      || foff >= CONSERVATIVE_24BIT_S1
10066
0
      || foff < -CONSERVATIVE_24BIT_S1)
10067
0
    return false;
10068
10069
0
  if (insn16_on
10070
0
      && foff >= -ACCURATE_8BIT_S1
10071
0
      && foff < ACCURATE_8BIT_S1
10072
0
      && (seq_len & 0x2))
10073
0
    {
10074
      /* j8 label */
10075
      /* 16-bit on, but not optimized for speed.  */
10076
0
      reloc = R_NDS32_9_PCREL_RELA;
10077
0
      insn16 = INSN_J8;
10078
0
      bfd_putb16 (insn16, contents + irel->r_offset);
10079
0
      *insn_len = 2;
10080
0
      irel->r_info =
10081
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10082
0
    }
10083
0
  else
10084
0
    {
10085
      /* j     label */
10086
0
      reloc = R_NDS32_25_PCREL_RELA;
10087
0
      insn = INSN_J;
10088
0
      bfd_putb32 (insn, contents + irel->r_offset);
10089
0
      *insn_len = 4;
10090
0
      irel->r_info =
10091
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10092
0
      irel->r_addend = 0;
10093
0
    }
10094
10095
0
  hi_irelfn->r_info =
10096
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10097
0
  lo_irelfn->r_info =
10098
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10099
10100
0
  if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10101
0
    {
10102
0
      insn16 = NDS32_NOP16;
10103
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10104
0
      lo_irelfn->r_info =
10105
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10106
0
          R_NDS32_INSN16);
10107
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10108
0
      *insn_len += 2;
10109
0
    }
10110
0
  return true;
10111
0
}
10112
10113
/* Revert condition branch.  This function does not check if the input
10114
   instruction is condition branch or not.  */
10115
10116
static void
10117
nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10118
         uint16_t *re_insn16, uint32_t *re_insn)
10119
0
{
10120
0
  uint32_t comp_insn = 0;
10121
0
  uint16_t comp_insn16 = 0;
10122
10123
0
  if (insn)
10124
0
    {
10125
0
      if (N32_OP6 (insn) == N32_OP6_BR1)
10126
0
  {
10127
    /* beqs label.  */
10128
0
    comp_insn = (insn ^ 0x4000) & 0xffffc000;
10129
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10130
0
      {
10131
        /* Insn can be contracted to 16-bit implied r5.  */
10132
0
        comp_insn16 =
10133
0
    (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10134
0
        comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10135
0
      }
10136
0
  }
10137
0
      else if (N32_OP6 (insn) == N32_OP6_BR3)
10138
0
  {
10139
    /* bnec $ta, imm11, label.  */
10140
0
    comp_insn = (insn ^ 0x80000) & 0xffffff00;
10141
0
  }
10142
0
      else
10143
0
  {
10144
0
    comp_insn = (insn ^ 0x10000) & 0xffffc000;
10145
0
    if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10146
0
        || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10147
0
      {
10148
0
        if (N32_IS_RT3 (insn))
10149
0
    {
10150
      /* Insn can be contracted to 16-bit.  */
10151
0
      comp_insn16 =
10152
0
        (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10153
0
      comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10154
0
    }
10155
0
        else if (N32_RT5 (insn) == REG_R15)
10156
0
    {
10157
      /* Insn can be contracted to 16-bit.  */
10158
0
      comp_insn16 =
10159
0
        (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10160
0
    }
10161
0
      }
10162
0
  }
10163
0
    }
10164
0
  else
10165
0
    {
10166
0
      switch ((insn16 & 0xf000) >> 12)
10167
0
  {
10168
0
  case 0xc:
10169
    /* beqz38 or bnez38 */
10170
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10171
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10172
0
    comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10173
0
    break;
10174
10175
0
  case 0xd:
10176
    /* beqs38 or bnes38 */
10177
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10178
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10179
0
    comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10180
0
      | (REG_R5 << 15);
10181
0
    break;
10182
10183
0
  case 0xe:
10184
    /* beqzS8 or bnezS8 */
10185
0
    comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10186
0
    comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10187
0
    comp_insn |= REG_R15 << 20;
10188
0
    break;
10189
10190
0
  default:
10191
0
    break;
10192
0
  }
10193
0
    }
10194
0
  if (comp_insn && re_insn)
10195
0
    *re_insn = comp_insn;
10196
0
  if (comp_insn16 && re_insn16)
10197
0
    *re_insn16 = comp_insn16;
10198
0
}
10199
10200
/* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
10201
10202
static bool
10203
nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10204
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10205
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10206
         Elf_Internal_Shdr *symtab_hdr)
10207
0
{
10208
  /* There are 3 variations for LONGJUMP2
10209
     case 2-4;  1st insn convertible, 16-bit on,
10210
     optimize off or optimize for space
10211
     bnes38  rt, ra, $1 ; LONGJUMP2
10212
     j       label      ; 25_PCREL
10213
     $1:
10214
10215
     case 4-4; 1st insn not convertible
10216
     bne  rt, ra, $1 ; LONGJUMP2
10217
     j    label      ; 25_PCREL
10218
     $1:
10219
10220
     case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10221
     bne  rt, ra, $1 ; LONGJUMP2
10222
     j    label      ; 25_PCREL
10223
     $1: */
10224
10225
  /* Get the reloc for the address from which the register is
10226
     being loaded.  This reloc will tell us which function is
10227
     actually being called.  */
10228
10229
0
  bfd_vma laddr;
10230
0
  int seq_len;  /* Original length of instruction sequence.  */
10231
0
  Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10232
0
  int first_size;
10233
0
  unsigned int i;
10234
0
  bfd_signed_vma foff;
10235
0
  uint32_t insn, re_insn = 0;
10236
0
  uint16_t insn16, re_insn16 = 0;
10237
0
  unsigned long reloc, cond_reloc;
10238
10239
0
  enum elf_nds32_reloc_type checked_types[] =
10240
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10241
10242
0
  irelend = internal_relocs + sec->reloc_count;
10243
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10244
0
  laddr = irel->r_offset;
10245
0
  *insn_len = seq_len;
10246
0
  first_size = (seq_len == 6) ? 2 : 4;
10247
10248
0
  i2_irelfn =
10249
0
    find_relocs_at_address_addr (irel, internal_relocs,
10250
0
         irelend, R_NDS32_25_PCREL_RELA,
10251
0
         laddr + first_size);
10252
10253
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10254
0
    {
10255
0
      cond_irelfn =
10256
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10257
0
             checked_types[i], laddr);
10258
0
      if (cond_irelfn != irelend)
10259
0
  break;
10260
0
    }
10261
10262
0
  if (i2_irelfn == irelend || cond_irelfn == irelend)
10263
0
    {
10264
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10265
0
        (uint64_t) irel->r_offset);
10266
0
      return false;
10267
0
    }
10268
10269
  /* Get the value of the symbol referred to by the reloc.  */
10270
0
  foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10271
0
  if (foff == 0
10272
0
      || foff < -CONSERVATIVE_16BIT_S1
10273
0
      || foff >= CONSERVATIVE_16BIT_S1)
10274
0
    return false;
10275
10276
  /* Get the all corresponding instructions.  */
10277
0
  if (first_size == 4)
10278
0
    {
10279
0
      insn = bfd_getb32 (contents + laddr);
10280
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10281
0
    }
10282
0
  else
10283
0
    {
10284
0
      insn16 = bfd_getb16 (contents + laddr);
10285
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10286
0
    }
10287
10288
0
  if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10289
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10290
0
    {
10291
0
      if (first_size == 4)
10292
0
  {
10293
    /* Don't convert it to 16-bit now, keep this as relaxable for
10294
       ``label reloc; INSN16''.  */
10295
10296
    /* Save comp_insn32 to buffer.  */
10297
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10298
0
    *insn_len = 4;
10299
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10300
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10301
0
    cond_reloc = R_NDS32_INSN16;
10302
0
  }
10303
0
      else
10304
0
  {
10305
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10306
0
    *insn_len = 2;
10307
0
    reloc = R_NDS32_9_PCREL_RELA;
10308
0
    cond_reloc = R_NDS32_NONE;
10309
0
  }
10310
0
    }
10311
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10312
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10313
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10314
0
    {
10315
      /* beqs     label    ; 15_PCREL */
10316
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10317
0
      *insn_len = 4;
10318
0
      reloc = R_NDS32_15_PCREL_RELA;
10319
0
      cond_reloc = R_NDS32_NONE;
10320
0
    }
10321
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10322
0
     && foff >= -CONSERVATIVE_16BIT_S1
10323
0
     && foff < CONSERVATIVE_16BIT_S1)
10324
0
    {
10325
      /* beqz     label ; 17_PCREL */
10326
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10327
0
      *insn_len = 4;
10328
0
      reloc = R_NDS32_17_PCREL_RELA;
10329
0
      cond_reloc = R_NDS32_NONE;
10330
0
    }
10331
0
  else
10332
0
    return false;
10333
10334
  /* Set all relocations.  */
10335
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10336
0
  irel->r_addend = i2_irelfn->r_addend;
10337
10338
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10339
0
              cond_reloc);
10340
0
  cond_irelfn->r_addend = 0;
10341
10342
0
  if ((seq_len ^ *insn_len ) & 0x2)
10343
0
    {
10344
0
      insn16 = NDS32_NOP16;
10345
0
      bfd_putb16 (insn16, contents + irel->r_offset + 4);
10346
0
      i2_irelfn->r_offset = 4;
10347
0
      i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10348
0
          R_NDS32_INSN16);
10349
0
      i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10350
0
      *insn_len += 2;
10351
0
    }
10352
0
  else
10353
0
    i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10354
0
              R_NDS32_NONE);
10355
0
  return true;
10356
0
}
10357
10358
/* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
10359
10360
static bool
10361
nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10362
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10363
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10364
         Elf_Internal_Shdr *symtab_hdr)
10365
0
{
10366
  /* There are 5 variations for LONGJUMP3
10367
     case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10368
     optimize off or optimize for space
10369
     bnes38   rt, ra, $1      ; LONGJUMP3
10370
     sethi    ta, hi20(symbol)      ; HI20
10371
     ori      ta, ta, lo12(symbol)  ; LO12S0
10372
     jr5      ta        ;
10373
     $1:          ;
10374
10375
     case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10376
     bnes38   rt, ra, $1     ; LONGJUMP3
10377
     sethi    ta, hi20(symbol)     ; HI20
10378
     ori      ta, ta, lo12(symbol) ; LO12S0
10379
     jr5      ta       ;
10380
     $1:         ; LABEL
10381
10382
     case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10383
     optimize off or optimize for space
10384
     bne   rt, ra, $1   ; LONGJUMP3
10385
     sethi ta, hi20(symbol) ; HI20
10386
     ori   ta, ta, lo12(symbol) ; LO12S0
10387
     jr5   ta     ;
10388
     $1:      ;
10389
10390
     case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10391
     16-bit off if no INSN16
10392
     bne   rt, ra, $1   ; LONGJUMP3
10393
     sethi ta, hi20(symbol) ; HI20
10394
     ori   ta, ta, lo12(symbol) ; LO12S0
10395
     jr    ta     ;
10396
     $1:      ;
10397
10398
     case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10399
     16-bit off if no INSN16
10400
     bne   rt, ra, $1   ; LONGJUMP3
10401
     sethi ta, hi20(symbol) ; HI20
10402
     ori   ta, ta, lo12(symbol) ; LO12S0
10403
     jr    ta     ;
10404
     $1:      ; LABEL */
10405
10406
  /* Get the reloc for the address from which the register is
10407
     being loaded.  This reloc will tell us which function is
10408
     actually being called.  */
10409
0
  enum elf_nds32_reloc_type checked_types[] =
10410
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10411
10412
0
  int reloc_off = 0, cond_removed = 0, convertible;
10413
0
  bfd_vma laddr;
10414
0
  int seq_len;  /* Original length of instruction sequence.  */
10415
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10416
0
  int first_size;
10417
0
  unsigned int i;
10418
0
  bfd_signed_vma foff;
10419
0
  uint32_t insn, re_insn = 0;
10420
0
  uint16_t insn16, re_insn16 = 0;
10421
0
  unsigned long reloc, cond_reloc;
10422
10423
0
  irelend = internal_relocs + sec->reloc_count;
10424
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10425
0
  laddr = irel->r_offset;
10426
0
  *insn_len = seq_len;
10427
10428
0
  convertible = IS_1ST_CONVERT (irel->r_addend);
10429
10430
0
  if (convertible)
10431
0
    first_size = 2;
10432
0
  else
10433
0
    first_size = 4;
10434
10435
  /* Get all needed relocations.  */
10436
0
  hi_irelfn =
10437
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10438
0
         R_NDS32_HI20_RELA, laddr + first_size);
10439
0
  lo_irelfn =
10440
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10441
0
         R_NDS32_LO12S0_ORI_RELA,
10442
0
         laddr + first_size + 4);
10443
10444
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10445
0
    {
10446
0
      cond_irelfn =
10447
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10448
0
             checked_types[i], laddr);
10449
0
      if (cond_irelfn != irelend)
10450
0
  break;
10451
0
    }
10452
10453
0
  if (hi_irelfn == irelend
10454
0
      || lo_irelfn == irelend
10455
0
      || cond_irelfn == irelend)
10456
0
    {
10457
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10458
0
        (uint64_t) irel->r_offset);
10459
0
      return false;
10460
0
    }
10461
10462
  /* Get the value of the symbol referred to by the reloc.  */
10463
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10464
10465
0
  if (foff == 0
10466
0
      || foff < -CONSERVATIVE_24BIT_S1
10467
0
      || foff >= CONSERVATIVE_24BIT_S1)
10468
0
    return false;
10469
10470
  /* Get the all corresponding instructions.  */
10471
0
  if (first_size == 4)
10472
0
    {
10473
0
      insn = bfd_getb32 (contents + laddr);
10474
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10475
0
    }
10476
0
  else
10477
0
    {
10478
0
      insn16 = bfd_getb16 (contents + laddr);
10479
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10480
0
    }
10481
10482
  /* For simplicity of coding, we are going to modify the section
10483
     contents, the section relocs, and the BFD symbol table.  We
10484
     must tell the rest of the code not to free up this
10485
     information.  It would be possible to instead create a table
10486
     of changes which have to be made, as is done in coff-mips.c;
10487
     that would be more work, but would require less memory when
10488
     the linker is run.  */
10489
10490
0
  if (re_insn16
10491
0
      && foff >= -ACCURATE_8BIT_S1 - first_size
10492
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10493
0
    {
10494
0
      if (!(seq_len & 0x2))
10495
0
  {
10496
    /* Don't convert it to 16-bit now, keep this as relaxable
10497
       for ``label reloc; INSN1a''6.  */
10498
    /* Save comp_insn32 to buffer.  */
10499
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10500
0
    *insn_len = 4;
10501
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10502
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10503
0
    cond_reloc = R_NDS32_INSN16;
10504
0
  }
10505
0
      else
10506
0
  {
10507
    /* Not optimize for speed; convert sequence to 16-bit.  */
10508
    /* Save comp_insn16 to buffer.  */
10509
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10510
0
    *insn_len = 2;
10511
0
    reloc = R_NDS32_9_PCREL_RELA;
10512
0
    cond_reloc = R_NDS32_NONE;
10513
0
  }
10514
0
      cond_removed = 1;
10515
0
    }
10516
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10517
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10518
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10519
0
    {
10520
      /* beqs     label    ; 15_PCREL */
10521
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10522
0
      *insn_len = 4;
10523
0
      reloc = R_NDS32_15_PCREL_RELA;
10524
0
      cond_reloc = R_NDS32_NONE;
10525
0
      cond_removed = 1;
10526
0
    }
10527
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10528
0
     && foff >= -CONSERVATIVE_16BIT_S1
10529
0
     && foff < CONSERVATIVE_16BIT_S1)
10530
0
    {
10531
      /* beqz     label ; 17_PCREL */
10532
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10533
0
      *insn_len = 4;
10534
0
      reloc = R_NDS32_17_PCREL_RELA;
10535
0
      cond_reloc = R_NDS32_NONE;
10536
0
      cond_removed = 1;
10537
0
    }
10538
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10539
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10540
0
    {
10541
      /* Relax to one of the following 3 variations
10542
10543
   case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10544
   for space
10545
   bnes38  rt, $1 ; LONGJUMP2
10546
   j       label  ; 25_PCREL
10547
   $1
10548
10549
   case 4-4; 1st insn not convertible, others don't care
10550
   bne   rt, ra, $1 ; LONGJUMP2
10551
   j     label      ; 25_PCREL
10552
   $1
10553
10554
   case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10555
   bne   rt, ra, $1 ; LONGJUMP2
10556
   j     label      ; 25_PCREL
10557
   $1 */
10558
10559
      /* Offset for first instruction.  */
10560
10561
      /* Use j label as second instruction.  */
10562
0
      *insn_len = 4 + first_size;
10563
0
      insn = INSN_J;
10564
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10565
0
      reloc = R_NDS32_LONGJUMP2;
10566
0
      cond_reloc = R_NDS32_25_PLTREL;
10567
0
    }
10568
0
    else
10569
0
      return false;
10570
10571
0
    if (cond_removed == 1)
10572
0
      {
10573
  /* Set all relocations.  */
10574
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10575
0
  irel->r_addend = hi_irelfn->r_addend;
10576
10577
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10578
0
              cond_reloc);
10579
0
  cond_irelfn->r_addend = 0;
10580
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10581
0
            R_NDS32_NONE);
10582
0
      }
10583
0
    else
10584
0
      {
10585
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10586
0
  irel->r_addend = irel->r_addend;
10587
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10588
0
            cond_reloc);
10589
0
      }
10590
10591
0
  if ((seq_len ^ *insn_len ) & 0x2)
10592
0
    {
10593
0
      insn16 = NDS32_NOP16;
10594
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10595
0
      lo_irelfn->r_offset = *insn_len;
10596
0
      lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10597
0
          R_NDS32_INSN16);
10598
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10599
0
      *insn_len += 2;
10600
0
    }
10601
0
  else
10602
0
    lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10603
0
              R_NDS32_NONE);
10604
0
  return true;
10605
0
}
10606
10607
/* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
10608
10609
static bool
10610
nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10611
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10612
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10613
         Elf_Internal_Shdr *symtab_hdr)
10614
0
{
10615
  /* The pattern for LONGCALL4.  Support for function cse.
10616
     sethi ta, hi20(symbol) ; LONGCALL4/HI20
10617
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10618
     jral  ta     ; PTR_RES/EMPTY/INSN16  */
10619
10620
0
  bfd_vma laddr;
10621
0
  uint32_t insn;
10622
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10623
0
  Elf_Internal_Rela *irelend;
10624
0
  bfd_signed_vma foff;
10625
10626
0
  irelend = internal_relocs + sec->reloc_count;
10627
0
  laddr = irel->r_offset;
10628
10629
  /* Get the reloc for the address from which the register is
10630
     being loaded.  This reloc will tell us which function is
10631
     actually being called.  */
10632
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10633
0
           R_NDS32_HI20_RELA, laddr);
10634
10635
0
  if (hi_irel == irelend)
10636
0
    {
10637
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10638
0
        (uint64_t) irel->r_offset);
10639
0
      return false;
10640
0
    }
10641
10642
  /* Get the value of the symbol referred to by the reloc.  */
10643
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10644
10645
  /* This condition only happened when symbol is undefined.  */
10646
0
  if (foff == 0
10647
0
      || foff < -CONSERVATIVE_24BIT_S1
10648
0
      || foff >= CONSERVATIVE_24BIT_S1)
10649
0
    return false;
10650
10651
  /* Relax to: jal symbol; 25_PCREL.  */
10652
  /* For simplicity of coding, we are going to modify the section
10653
     contents, the section relocs, and the BFD symbol table.  We
10654
     must tell the rest of the code not to free up this
10655
     information.  It would be possible to instead create a table
10656
     of changes which have to be made, as is done in coff-mips.c;
10657
     that would be more work, but would require less memory when
10658
     the linker is run.  */
10659
10660
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10661
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10662
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10663
0
            R_NDS32_EMPTY, irel->r_addend);
10664
10665
0
  if (ptr_irel == irelend || em_irel == irelend)
10666
0
    {
10667
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10668
0
        (uint64_t) irel->r_offset);
10669
0
      return false;
10670
0
    }
10671
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10672
0
  insn = bfd_getb32 (contents + irel->r_addend);
10673
0
  if (insn & 0x80000000)
10674
0
    return false;
10675
10676
  /* Replace the long call with a jal.  */
10677
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10678
0
          R_NDS32_25_PCREL_RELA);
10679
0
  ptr_irel->r_addend = 1;
10680
10681
  /* We don't resolve this here but resolve it in relocate_section.  */
10682
0
  insn = INSN_JAL;
10683
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10684
10685
0
  irel->r_info =
10686
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10687
10688
  /* If there is function cse, HI20 can not remove now.  */
10689
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10690
0
             R_NDS32_LONGCALL4, laddr);
10691
0
  if (call_irel == irelend)
10692
0
    {
10693
0
      *insn_len = 0;
10694
0
      hi_irel->r_info =
10695
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10696
0
    }
10697
10698
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10699
0
            R_NDS32_INSN16, irel->r_addend);
10700
0
  if (insn_irel != irelend)
10701
0
    insn_irel->r_info =
10702
0
      ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10703
10704
0
  return true;
10705
0
}
10706
10707
/* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
10708
10709
static bool
10710
nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10711
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10712
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10713
         Elf_Internal_Shdr *symtab_hdr)
10714
0
{
10715
  /* The pattern for LONGCALL5.
10716
     bltz  rt, .L1  ; LONGCALL5/17_PCREL
10717
     jal   symbol ; 25_PCREL
10718
     .L1:  */
10719
10720
0
  bfd_vma laddr;
10721
0
  uint32_t insn;
10722
0
  Elf_Internal_Rela *cond_irel, *irelend;
10723
0
  bfd_signed_vma foff;
10724
10725
0
  irelend = internal_relocs + sec->reloc_count;
10726
0
  laddr = irel->r_offset;
10727
0
  insn = bfd_getb32 (contents + laddr);
10728
10729
  /* Get the reloc for the address from which the register is
10730
     being loaded.  This reloc will tell us which function is
10731
     actually being called.  */
10732
0
  cond_irel =
10733
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10734
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
10735
0
  if (cond_irel == irelend)
10736
0
    {
10737
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10738
0
        (uint64_t) irel->r_offset);
10739
0
      return false;
10740
0
    }
10741
10742
  /* Get the value of the symbol referred to by the reloc.  */
10743
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10744
10745
0
  if (foff == 0
10746
0
      || foff < -CONSERVATIVE_16BIT_S1
10747
0
      || foff >= CONSERVATIVE_16BIT_S1)
10748
0
    return false;
10749
10750
  /* Relax to bgezal   rt, label ; 17_PCREL
10751
     or   bltzal   rt, label ; 17_PCREL.  */
10752
10753
  /* Convert to complimentary conditional call.  */
10754
0
  insn = CONVERT_CONDITION_CALL (insn);
10755
10756
  /* For simplicity of coding, we are going to modify the section
10757
     contents, the section relocs, and the BFD symbol table.  We
10758
     must tell the rest of the code not to free up this
10759
     information.  It would be possible to instead create a table
10760
     of changes which have to be made, as is done in coff-mips.c;
10761
     that would be more work, but would require less memory when
10762
     the linker is run.  */
10763
10764
  /* Modify relocation and contents.  */
10765
0
  cond_irel->r_info =
10766
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10767
10768
  /* Replace the long call with a bgezal.  */
10769
0
  bfd_putb32 (insn, contents + cond_irel->r_offset);
10770
0
  *insn_len = 0;
10771
10772
  /* Clean unnessary relocations.  */
10773
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10774
10775
0
  cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10776
0
             R_NDS32_17_PCREL_RELA, laddr);
10777
0
  cond_irel->r_info =
10778
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10779
10780
0
  return true;
10781
0
}
10782
10783
/* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
10784
10785
static bool
10786
nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10787
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10788
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10789
         Elf_Internal_Shdr *symtab_hdr)
10790
0
{
10791
  /* The pattern for LONGCALL6.
10792
     bltz  rt,   .L1      ; LONGCALL6/17_PCREL
10793
     sethi ta,   hi20(symbol)   ; HI20/PTR
10794
     ori   ta, ta,  lo12(symbol)  ; LO12S0_ORI/PTR
10795
     jral  ta       ; PTR_RES/EMPTY/INSN16
10796
     .L1  */
10797
10798
0
  bfd_vma laddr;
10799
0
  uint32_t insn;
10800
0
  Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10801
0
  bfd_signed_vma foff;
10802
10803
0
  irelend = internal_relocs + sec->reloc_count;
10804
0
  laddr = irel->r_offset;
10805
10806
  /* Get the reloc for the address from which the register is
10807
     being loaded.  This reloc will tell us which function is
10808
     actually being called.  */
10809
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10810
0
           R_NDS32_EMPTY, irel->r_addend);
10811
10812
0
  if (em_irel == irelend)
10813
0
    {
10814
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10815
0
        (uint64_t) irel->r_offset);
10816
0
      return false;
10817
0
    }
10818
10819
  /* Get the value of the symbol referred to by the reloc.  */
10820
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10821
10822
0
  if (foff == 0
10823
0
      || foff < -CONSERVATIVE_24BIT_S1
10824
0
      || foff >= CONSERVATIVE_24BIT_S1)
10825
0
    return false;
10826
10827
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10828
0
  insn = bfd_getb32 (contents + irel->r_addend);
10829
0
  if (insn & 0x80000000)
10830
0
    return false;
10831
10832
0
  insn = bfd_getb32 (contents + laddr);
10833
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10834
0
    {
10835
      /* Relax to  bgezal   rt, label ; 17_PCREL
10836
   or    bltzal   rt, label ; 17_PCREL.  */
10837
10838
      /* Convert to complimentary conditional call.  */
10839
0
      *insn_len = 0;
10840
0
      insn = CONVERT_CONDITION_CALL (insn);
10841
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10842
10843
0
      em_irel->r_info =
10844
0
  ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10845
10846
      /* Set resolved relocation.  */
10847
0
      cond_irel =
10848
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10849
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10850
0
      if (cond_irel == irelend)
10851
0
  {
10852
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10853
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10854
0
    return false;
10855
0
  }
10856
0
      cond_irel->r_addend = 1;
10857
10858
      /* Clear relocations.  */
10859
10860
0
      irel->r_info =
10861
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10862
10863
0
      cond_irel =
10864
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10865
0
             R_NDS32_17_PCREL_RELA, laddr);
10866
0
      if (cond_irel != irelend)
10867
0
  cond_irel->r_info =
10868
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10869
10870
0
      cond_irel =
10871
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10872
0
             R_NDS32_INSN16, irel->r_addend);
10873
0
      if (cond_irel != irelend)
10874
0
  cond_irel->r_info =
10875
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10876
10877
0
    }
10878
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10879
0
    {
10880
      /* Relax to the following instruction sequence
10881
   bltz  rt, .L1  ; LONGCALL2/17_PCREL
10882
   jal   symbol ; 25_PCREL/PTR_RES
10883
   .L1  */
10884
0
      *insn_len = 4;
10885
      /* Convert instruction.  */
10886
0
      insn = INSN_JAL;
10887
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10888
10889
      /* Convert relocations.  */
10890
0
      em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10891
0
              R_NDS32_25_PCREL_RELA);
10892
0
      irel->r_info =
10893
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10894
10895
      /* Set resolved relocation.  */
10896
0
      cond_irel =
10897
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10898
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10899
0
      if (cond_irel == irelend)
10900
0
  {
10901
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10902
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10903
0
    return false;
10904
0
  }
10905
0
      cond_irel->r_addend = 1;
10906
10907
0
      cond_irel =
10908
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10909
0
             R_NDS32_INSN16, irel->r_addend);
10910
0
      if (cond_irel != irelend)
10911
0
  cond_irel->r_info =
10912
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10913
0
    }
10914
0
  return true;
10915
0
}
10916
10917
/* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10918
10919
static bool
10920
nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10921
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10922
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10923
         Elf_Internal_Shdr *symtab_hdr)
10924
0
{
10925
  /* The pattern for LONGJUMP4.
10926
     sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10927
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10928
     jr    ta     ; PTR_RES/INSN16/EMPTY  */
10929
10930
0
  bfd_vma laddr;
10931
0
  int seq_len;  /* Original length of instruction sequence.  */
10932
0
  uint32_t insn;
10933
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10934
0
  bfd_signed_vma foff;
10935
10936
0
  irelend = internal_relocs + sec->reloc_count;
10937
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10938
0
  laddr = irel->r_offset;
10939
0
  *insn_len = seq_len;
10940
10941
  /* Get the reloc for the address from which the register is
10942
     being loaded.  This reloc will tell us which function is
10943
     actually being called.  */
10944
10945
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10946
0
           R_NDS32_HI20_RELA, laddr);
10947
10948
0
  if (hi_irel == irelend)
10949
0
    {
10950
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10951
0
        (uint64_t) irel->r_offset);
10952
0
      return false;
10953
0
    }
10954
10955
  /* Get the value of the symbol referred to by the reloc.  */
10956
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10957
10958
0
  if (foff == 0
10959
0
      || foff >= CONSERVATIVE_24BIT_S1
10960
0
      || foff < -CONSERVATIVE_24BIT_S1)
10961
0
    return false;
10962
10963
  /* Convert it to "j label", it may be converted to j8 in the final
10964
     pass of relaxation.  Therefore, we do not consider this currently.  */
10965
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10966
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10967
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10968
0
           R_NDS32_EMPTY, irel->r_addend);
10969
10970
0
  if (ptr_irel == irelend || em_irel == irelend)
10971
0
    {
10972
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10973
0
        (uint64_t) irel->r_offset);
10974
0
      return false;
10975
0
    }
10976
10977
0
  em_irel->r_info =
10978
0
    ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10979
0
  ptr_irel->r_addend = 1;
10980
10981
  /* Write instruction.  */
10982
0
  insn = INSN_J;
10983
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10984
10985
  /* Clear relocations.  */
10986
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10987
10988
  /* If there is function cse, HI20 can not remove now.  */
10989
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10990
0
             R_NDS32_LONGJUMP4, laddr);
10991
0
  if (call_irel == irelend)
10992
0
    {
10993
0
      *insn_len = 0;
10994
0
      hi_irel->r_info =
10995
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10996
0
    }
10997
10998
0
  return true;
10999
0
}
11000
11001
/* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
11002
11003
static bool
11004
nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11005
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11006
         int *seq_len, bfd_byte *contents,
11007
         Elf_Internal_Sym *isymbuf,
11008
         Elf_Internal_Shdr *symtab_hdr)
11009
0
{
11010
  /* There are 2 variations for LONGJUMP5
11011
     case 2-4;  1st insn convertible, 16-bit on.
11012
     bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11013
     j       label    ; 25_PCREL/INSN16
11014
     $1:
11015
11016
     case 4-4; 1st insn not convertible
11017
     bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11018
     j    label   ; 25_PCREL/INSN16
11019
     .L1:  */
11020
11021
0
  bfd_vma laddr;
11022
0
  Elf_Internal_Rela *cond_irel,  *irelend;
11023
0
  unsigned int i;
11024
0
  bfd_signed_vma foff;
11025
0
  uint32_t insn, re_insn = 0;
11026
0
  uint16_t insn16, re_insn16 = 0;
11027
0
  unsigned long reloc;
11028
11029
0
  enum elf_nds32_reloc_type checked_types[] =
11030
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11031
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11032
11033
0
  irelend = internal_relocs + sec->reloc_count;
11034
0
  laddr = irel->r_offset;
11035
11036
  /* Get the reloc for the address from which the register is
11037
     being loaded.  This reloc will tell us which function is
11038
     actually being called.  */
11039
11040
0
  cond_irel =
11041
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11042
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
11043
0
  if (cond_irel == irelend)
11044
0
    {
11045
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11046
0
        (uint64_t) irel->r_offset);
11047
0
      return false;
11048
0
    }
11049
11050
  /* Get the value of the symbol referred to by the reloc.  */
11051
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11052
11053
0
  if (foff == 0
11054
0
      || foff < -CONSERVATIVE_16BIT_S1
11055
0
      || foff >= CONSERVATIVE_16BIT_S1)
11056
0
    return false;
11057
11058
  /* Get the all corresponding instructions.  */
11059
0
  insn = bfd_getb32 (contents + laddr);
11060
  /* Check instruction size.  */
11061
0
  if (insn & 0x80000000)
11062
0
    {
11063
0
      *seq_len = 0;
11064
0
      insn16 = insn >> 16;
11065
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11066
0
    }
11067
0
  else
11068
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11069
11070
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11071
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11072
0
    {
11073
      /* beqs label ; 15_PCREL.  */
11074
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11075
0
      reloc = R_NDS32_15_PCREL_RELA;
11076
0
    }
11077
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11078
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11079
0
    {
11080
      /* beqz label ; 17_PCREL.  */
11081
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11082
0
      reloc = R_NDS32_17_PCREL_RELA;
11083
0
    }
11084
0
  else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11085
0
     && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11086
0
    {
11087
      /* beqc label ; 9_PCREL.  */
11088
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11089
0
      reloc = R_NDS32_WORD_9_PCREL_RELA;
11090
0
    }
11091
0
  else
11092
0
    return false;
11093
11094
  /* Set all relocations.  */
11095
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11096
11097
  /* Clean relocations.  */
11098
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11099
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11100
0
    {
11101
0
      cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11102
0
                 checked_types[i], laddr);
11103
0
      if (cond_irel != irelend)
11104
0
  {
11105
0
    if (*seq_len == 0
11106
0
        && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11107
0
      {
11108
        /* If the branch instruction is 2 byte, it cannot remove
11109
     directly.  Only convert it to nop16 and remove it after
11110
     checking alignment issue.  */
11111
0
        insn16 = NDS32_NOP16;
11112
0
        bfd_putb16 (insn16, contents + laddr);
11113
0
        cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11114
0
      }
11115
0
    else
11116
0
      cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11117
0
                R_NDS32_NONE);
11118
0
  }
11119
0
    }
11120
0
  *insn_len = 0;
11121
11122
0
  return true;
11123
0
}
11124
11125
/* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
11126
11127
static bool
11128
nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11129
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11130
         int *seq_len, bfd_byte *contents,
11131
         Elf_Internal_Sym *isymbuf,
11132
         Elf_Internal_Shdr *symtab_hdr)
11133
0
{
11134
  /* There are 5 variations for LONGJUMP6
11135
     case : 2-4-4-4; 1st insn convertible, 16-bit on.
11136
     bnes38   rt, ra, .L1   ; LONGJUMP6/15_PCREL/INSN16
11137
     sethi    ta, hi20(symbol)    ; HI20/PTR
11138
     ori      ta, ta, lo12(symbol)  ; LO12S0_ORI/PTR
11139
     jr       ta      ; PTR_RES/INSN16/EMPTY
11140
     .L1:
11141
11142
     case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11143
     bne   rt, ra, .L1    ; LONGJUMP6/15_PCREL/INSN16
11144
     sethi ta, hi20(symbol) ; HI20/PTR
11145
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11146
     jr    ta     ; PTR_RES/INSN16/EMPTY
11147
     .L1:  */
11148
11149
0
  enum elf_nds32_reloc_type checked_types[] =
11150
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11151
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11152
11153
0
  int reloc_off = 0, cond_removed = 0;
11154
0
  bfd_vma laddr;
11155
0
  Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11156
0
  unsigned int i;
11157
0
  bfd_signed_vma foff;
11158
0
  uint32_t insn, re_insn = 0;
11159
0
  uint16_t insn16, re_insn16 = 0;
11160
0
  unsigned long reloc;
11161
11162
0
  irelend = internal_relocs + sec->reloc_count;
11163
0
  laddr = irel->r_offset;
11164
11165
  /* Get the reloc for the address from which the register is
11166
     being loaded.  This reloc will tell us which function is
11167
     actually being called.  */
11168
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11169
0
           R_NDS32_EMPTY, irel->r_addend);
11170
11171
0
  if (em_irel == irelend)
11172
0
    {
11173
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11174
0
        (uint64_t) irel->r_offset);
11175
0
      return false;
11176
0
    }
11177
11178
  /* Get the value of the symbol referred to by the reloc.  */
11179
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11180
11181
0
  if (foff == 0
11182
0
      || foff < -CONSERVATIVE_24BIT_S1
11183
0
      || foff >= CONSERVATIVE_24BIT_S1)
11184
0
    return false;
11185
11186
0
  insn = bfd_getb32 (contents + laddr);
11187
  /* Check instruction size.  */
11188
0
  if (insn & 0x80000000)
11189
0
    {
11190
0
      *seq_len = 0;
11191
0
      insn16 = insn >> 16;
11192
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11193
0
    }
11194
0
  else
11195
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11196
11197
  /* For simplicity of coding, we are going to modify the section
11198
     contents, the section relocs, and the BFD symbol table.  We
11199
     must tell the rest of the code not to free up this
11200
     information.  It would be possible to instead create a table
11201
     of changes which have to be made, as is done in coff-mips.c;
11202
     that would be more work, but would require less memory when
11203
     the linker is run.  */
11204
11205
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11206
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11207
0
    {
11208
      /* beqs     label    ; 15_PCREL.  */
11209
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11210
0
      reloc = R_NDS32_15_PCREL_RELA;
11211
0
      cond_removed = 1;
11212
0
    }
11213
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11214
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11215
0
    {
11216
      /* beqz     label ; 17_PCREL.  */
11217
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11218
0
      reloc = R_NDS32_17_PCREL_RELA;
11219
0
      cond_removed = 1;
11220
0
    }
11221
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11222
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11223
0
    {
11224
      /* Relax to one of the following 2 variations
11225
11226
   case 2-4;  1st insn convertible, 16-bit on.
11227
   bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11228
   j       label    ; 25_PCREL/INSN16
11229
   $1:
11230
11231
   case 4-4; 1st insn not convertible
11232
   bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11233
   j    label   ; 25_PCREL/INSN16
11234
   .L1:  */
11235
11236
      /* Use j label as second instruction.  */
11237
0
      insn = INSN_J;
11238
0
      reloc = R_NDS32_25_PCREL_RELA;
11239
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
11240
0
    }
11241
0
  else
11242
0
    return false;
11243
11244
  /* Set all relocations.  */
11245
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11246
11247
0
  cond_irel =
11248
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11249
0
         R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11250
0
  cond_irel->r_addend = 1;
11251
11252
  /* Use INSN16 of first branch instruction to distinguish if keeping
11253
     INSN16 of final instruction or not.  */
11254
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11255
0
             R_NDS32_INSN16, irel->r_offset);
11256
0
  if (insn_irel == irelend)
11257
0
    {
11258
      /* Clean the final INSN16.  */
11259
0
      insn_irel =
11260
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
11261
0
             R_NDS32_INSN16, em_irel->r_offset);
11262
0
      insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11263
0
          R_NDS32_NONE);
11264
0
    }
11265
11266
0
  if (cond_removed == 1)
11267
0
    {
11268
0
      *insn_len = 0;
11269
11270
      /* Clear relocations.  */
11271
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11272
11273
0
      for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11274
0
  {
11275
0
    cond_irel =
11276
0
      find_relocs_at_address_addr (irel, internal_relocs, irelend,
11277
0
           checked_types[i], laddr);
11278
0
    if (cond_irel != irelend)
11279
0
      {
11280
0
        if (*seq_len == 0
11281
0
      && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11282
0
    {
11283
      /* If the branch instruction is 2 byte, it cannot remove
11284
         directly.  Only convert it to nop16 and remove it after
11285
         checking alignment issue.  */
11286
0
      insn16 = NDS32_NOP16;
11287
0
      bfd_putb16 (insn16, contents + laddr);
11288
0
      cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11289
0
    }
11290
0
        else
11291
0
    cond_irel->r_info =
11292
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11293
0
      }
11294
0
  }
11295
0
    }
11296
0
  else
11297
0
    {
11298
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11299
0
           R_NDS32_LONGJUMP5);
11300
0
    }
11301
11302
0
  return true;
11303
0
}
11304
11305
/* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
11306
11307
static bool
11308
nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11309
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11310
         int *seq_len, bfd_byte *contents,
11311
         Elf_Internal_Sym *isymbuf,
11312
         Elf_Internal_Shdr *symtab_hdr)
11313
0
{
11314
  /* There are 2 variations for LONGJUMP5
11315
     case 2-4;  1st insn convertible, 16-bit on.
11316
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11317
     beq     rt, ta, label  ; 15_PCREL
11318
11319
     case 4-4; 1st insn not convertible
11320
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11321
     beq     rt, ta, label  ; 15_PCREL  */
11322
11323
0
  bfd_vma laddr;
11324
0
  Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
11325
0
  bfd_signed_vma foff;
11326
0
  uint32_t insn, re_insn = 0;
11327
0
  uint16_t insn16;
11328
0
  uint32_t imm11;
11329
11330
0
  irelend = internal_relocs + sec->reloc_count;
11331
0
  laddr = irel->r_offset;
11332
11333
  /* Get the reloc for the address from which the register is
11334
     being loaded.  This reloc will tell us which function is
11335
     actually being called.  */
11336
11337
0
  cond_irel =
11338
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11339
0
         R_NDS32_15_PCREL_RELA, irel->r_addend);
11340
0
  if (cond_irel == irelend)
11341
0
    {
11342
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11343
0
        (uint64_t) irel->r_offset);
11344
0
      return false;
11345
0
    }
11346
11347
  /* Get the value of the symbol referred to by the reloc.  */
11348
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11349
11350
0
  if (foff == 0
11351
0
      || foff < -CONSERVATIVE_8BIT_S1
11352
0
      || foff >= CONSERVATIVE_8BIT_S1)
11353
0
    return false;
11354
11355
  /* Get the first instruction for its size.  */
11356
0
  insn = bfd_getb32 (contents + laddr);
11357
0
  if (insn & 0x80000000)
11358
0
    {
11359
0
      *seq_len = 0;
11360
      /* Get the immediate from movi55.  */
11361
0
      imm11 = N16_IMM5S (insn >> 16);
11362
0
    }
11363
0
  else
11364
0
    {
11365
      /* Get the immediate from movi.  */
11366
0
      imm11 = N32_IMM20S (insn);
11367
0
    }
11368
11369
  /* Get the branch instruction.  */
11370
0
  insn = bfd_getb32 (contents + irel->r_addend);
11371
  /* Convert instruction to BR3.  */
11372
0
  if ((insn >> 14) & 0x1)
11373
0
    re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11374
0
  else
11375
0
    re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11376
11377
0
  bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11378
11379
  /* Set all relocations.  */
11380
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11381
0
            R_NDS32_WORD_9_PCREL_RELA);
11382
11383
  /* Clean relocations.  */
11384
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11385
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11386
0
             R_NDS32_INSN16, irel->r_offset);
11387
0
  if (insn_irel != irelend)
11388
0
    {
11389
0
      if (*seq_len == 0)
11390
0
  {
11391
    /* If the first insntruction is 16bit, convert it to nop16.  */
11392
0
    insn16 = NDS32_NOP16;
11393
0
    bfd_putb16 (insn16, contents + laddr);
11394
0
    insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11395
0
  }
11396
0
      else
11397
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11398
0
            R_NDS32_NONE);
11399
0
    }
11400
0
  *insn_len = 0;
11401
11402
0
  return true;
11403
0
}
11404
11405
/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11406
   for each relax round. But the gp may changed dramatically and then cause
11407
   the truncated to fit errors for the the converted gp instructions.
11408
   Therefore, we must reserve the minimum but safe enough size to prevent it.  */
11409
11410
static bool
11411
nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11412
           Elf_Internal_Rela *irel, bool *again,
11413
           bool init,
11414
           struct elf_nds32_link_hash_table *table,
11415
           Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11416
11417
0
{
11418
0
  int offset_to_gp;
11419
0
  static bool sec_pass = false;
11420
0
  static asection *first_sec = NULL, *sym_sec;
11421
  /* Record the number of instructions which may be removed.  */
11422
0
  static int count = 0, record_count;
11423
0
  Elf_Internal_Sym *isym;
11424
0
  struct elf_link_hash_entry *h = NULL;
11425
0
  int indx;
11426
0
  unsigned long r_symndx;
11427
0
  bfd *abfd = sec->owner;
11428
0
  static bfd_vma record_sda = 0;
11429
0
  int sda_offset = 0;
11430
11431
  /* Force doing relaxation when hyper-relax is high.  */
11432
0
  if (table->hyper_relax == 2)
11433
0
    return true;
11434
11435
  /* Do not relax the load/store patterns for the first
11436
     relax round.  */
11437
0
  if (init)
11438
0
    {
11439
0
      if (!first_sec)
11440
0
  first_sec = sec;
11441
0
      else if (first_sec == sec)
11442
0
  {
11443
0
    record_count = count;
11444
0
    count = 0;
11445
0
    sec_pass = true;
11446
0
  }
11447
11448
0
      if (!sec_pass)
11449
0
  *again = true;
11450
11451
0
      return true;
11452
0
    }
11453
11454
  /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11455
     DATA_SEGMENT_ALIGN size in the linker script may make it
11456
     get even bigger.  */
11457
0
  if (record_sda == 0)
11458
0
    record_sda = local_sda;
11459
0
  else if (local_sda > record_sda)
11460
0
    sda_offset = local_sda - record_sda;
11461
11462
  /* Assume the instruction will be removed in the best case.  */
11463
0
  count++;
11464
11465
  /* We record the offset to gp for each symbol, and then check
11466
     if it is changed dramatically after relaxing.
11467
     (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11468
     (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx].  */
11469
0
  r_symndx = ELF32_R_SYM (irel->r_info);
11470
0
  if (r_symndx >= symtab_hdr->sh_info)
11471
0
    {
11472
      /* Global symbols.  */
11473
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11474
0
      h = elf_sym_hashes (abfd)[indx];
11475
0
      sym_sec = h->root.u.def.section;
11476
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags)
11477
0
    || bfd_is_abs_section (sym_sec))
11478
0
  {
11479
    /* Forbid doing relaxation when hyper-relax is low.  */
11480
0
    if (table->hyper_relax == 0)
11481
0
      return false;
11482
11483
0
    offset_to_gp = *access_addr - local_sda;
11484
0
    if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11485
0
      elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11486
0
    else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11487
0
       < abs (offset_to_gp) - sda_offset)
11488
0
      {
11489
        /* This may cause the error, so we reserve the
11490
     safe enough size for relaxing.  */
11491
0
        if (*access_addr >= local_sda)
11492
0
    *access_addr += (record_count * 4);
11493
0
        else
11494
0
    *access_addr -= (record_count * 4);
11495
0
      }
11496
0
    return sec_pass;
11497
0
  }
11498
0
    }
11499
0
  else
11500
0
    {
11501
      /* Local symbols.  */
11502
0
      if (!elf32_nds32_allocate_local_sym_info (abfd))
11503
0
  return false;
11504
0
      isym = isymbuf + r_symndx;
11505
11506
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11507
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags))
11508
0
  {
11509
    /* Forbid doing relaxation when hyper-relax is low.  */
11510
0
    if (table->hyper_relax == 0)
11511
0
      return false;
11512
11513
0
    offset_to_gp = *access_addr - local_sda;
11514
0
    if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11515
0
      elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11516
0
    else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11517
0
       < abs (offset_to_gp) - sda_offset)
11518
0
      {
11519
        /* This may cause the error, so we reserve the
11520
     safe enough size for relaxing.  */
11521
0
        if (*access_addr >= local_sda)
11522
0
    *access_addr += (record_count * 4);
11523
0
        else
11524
0
    *access_addr -= (record_count * 4);
11525
0
      }
11526
0
    return sec_pass;
11527
0
  }
11528
0
    }
11529
11530
0
  return true;
11531
0
}
11532
11533
0
#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11534
11535
/* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
11536
11537
static bool
11538
nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11539
         asection *sec, Elf_Internal_Rela *irel,
11540
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11541
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11542
         Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11543
         struct elf_nds32_link_hash_table *table)
11544
0
{
11545
0
  int eliminate_sethi = 0, range_type;
11546
0
  unsigned int i;
11547
0
  bfd_vma local_sda, laddr;
11548
0
  int seq_len;  /* Original length of instruction sequence.  */
11549
0
  uint32_t insn;
11550
0
  Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11551
0
  bfd_vma access_addr = 0;
11552
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11553
0
  struct elf_link_hash_entry *h = NULL;
11554
0
  int indx;
11555
0
  enum elf_nds32_reloc_type checked_types[] =
11556
0
    { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11557
0
      R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11558
0
      R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11559
0
      R_NDS32_TLS_LE_HI20
11560
0
    };
11561
11562
0
  irelend = internal_relocs + sec->reloc_count;
11563
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
11564
0
  laddr = irel->r_offset;
11565
0
  *insn_len = seq_len;
11566
11567
  /* Get the high part relocation.  */
11568
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11569
0
    {
11570
0
      hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11571
0
                 checked_types[i], laddr);
11572
0
      if (hi_irelfn != irelend)
11573
0
  break;
11574
0
    }
11575
11576
0
  if (hi_irelfn == irelend)
11577
0
    {
11578
      /* Not R_NDS32_HI20_RELA.  */
11579
0
      if (i != 0)
11580
0
  _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11581
0
          (uint64_t) irel->r_offset);
11582
0
      return false;
11583
0
    }
11584
11585
0
  range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11586
0
  nds32_elf_final_sda_base (sec->output_section->owner,
11587
0
          link_info, &local_sda, false);
11588
11589
0
  switch (ELF32_R_TYPE (hi_irelfn->r_info))
11590
0
    {
11591
0
    case R_NDS32_HI20_RELA:
11592
0
      insn = bfd_getb32 (contents + laddr);
11593
0
      access_addr =
11594
0
  calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11595
11596
0
      if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11597
0
  {
11598
0
    indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11599
0
    h = elf_sym_hashes (abfd)[indx];
11600
0
  }
11601
11602
      /* Try movi.  */
11603
0
      if (range_type == NDS32_LOADSTORE_IMM
11604
0
    && access_addr < CONSERVATIVE_20BIT
11605
0
    && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11606
0
  {
11607
0
    eliminate_sethi = 1;
11608
0
    break;
11609
0
  }
11610
11611
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11612
0
  {
11613
0
    eliminate_sethi = 1;
11614
0
    break;
11615
0
  }
11616
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11617
0
               NULL, false, table, isymbuf, symtab_hdr))
11618
0
  return false;
11619
11620
0
      if (!load_store_relax)
11621
0
  return false;
11622
11623
      /* Case for set gp register.  */
11624
0
      if (N32_RT5 (insn) == REG_GP)
11625
0
  return false;
11626
11627
0
      if (range_type == NDS32_LOADSTORE_FLOAT_S
11628
0
    || range_type == NDS32_LOADSTORE_FLOAT_D)
11629
0
  {
11630
0
    range_l = sdata_range[0][0];
11631
0
    range_h = sdata_range[0][1];
11632
0
  }
11633
0
      else
11634
0
  {
11635
0
    range_l = sdata_range[1][0];
11636
0
    range_h = sdata_range[1][1];
11637
0
  }
11638
0
      break;
11639
11640
0
    default:
11641
0
      return false;
11642
0
    }
11643
11644
  /* Delete sethi instruction.  */
11645
0
  if (eliminate_sethi == 1
11646
0
      || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11647
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11648
0
    {
11649
0
      hi_irelfn->r_info =
11650
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11651
0
      irel->r_info =
11652
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11653
0
      *insn_len = 0;
11654
0
      return true;
11655
0
    }
11656
11657
0
  return false;
11658
0
}
11659
11660
/* Relax LO12 relocation for nds32_elf_relax_section.  */
11661
11662
static void
11663
nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11664
          asection *sec, Elf_Internal_Rela *irel,
11665
          Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11666
          Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11667
          struct elf_nds32_link_hash_table *table)
11668
0
{
11669
0
  uint32_t insn;
11670
0
  bfd_vma local_sda, laddr;
11671
0
  unsigned long reloc;
11672
0
  bfd_vma access_addr;
11673
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11674
0
  Elf_Internal_Rela *irelfn = NULL, *irelend;
11675
0
  struct elf_link_hash_entry *h = NULL;
11676
0
  int indx;
11677
11678
  /* For SDA base relative relaxation.  */
11679
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11680
0
          &local_sda, false);
11681
11682
0
  irelend = internal_relocs + sec->reloc_count;
11683
0
  laddr = irel->r_offset;
11684
0
  insn = bfd_getb32 (contents + laddr);
11685
11686
0
  if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11687
0
    return;
11688
11689
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11690
11691
0
  if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11692
0
    {
11693
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11694
0
      h = elf_sym_hashes (abfd)[indx];
11695
0
    }
11696
11697
  /* Try movi.  */
11698
0
  if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11699
0
      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11700
0
    {
11701
0
      reloc = R_NDS32_20_RELA;
11702
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11703
0
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11704
0
      bfd_putb32 (insn, contents + laddr);
11705
0
    }
11706
0
  else
11707
0
    {
11708
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11709
0
  {
11710
    /* Fall through.  */
11711
0
  }
11712
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11713
0
               false, table, isymbuf, symtab_hdr))
11714
0
  return;
11715
11716
0
      range_l = sdata_range[1][0];
11717
0
      range_h = sdata_range[1][1];
11718
0
      switch (ELF32_R_TYPE (irel->r_info))
11719
0
  {
11720
0
  case R_NDS32_LO12S0_RELA:
11721
0
    reloc = R_NDS32_SDA19S0_RELA;
11722
0
    break;
11723
0
  case R_NDS32_LO12S1_RELA:
11724
0
    reloc = R_NDS32_SDA18S1_RELA;
11725
0
    break;
11726
0
  case R_NDS32_LO12S2_RELA:
11727
0
    reloc = R_NDS32_SDA17S2_RELA;
11728
0
    break;
11729
0
  case R_NDS32_LO12S2_DP_RELA:
11730
0
    range_l = sdata_range[0][0];
11731
0
    range_h = sdata_range[0][1];
11732
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11733
0
    break;
11734
0
  case R_NDS32_LO12S2_SP_RELA:
11735
0
    range_l = sdata_range[0][0];
11736
0
    range_h = sdata_range[0][1];
11737
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11738
0
    break;
11739
0
  default:
11740
0
    return;
11741
0
  }
11742
11743
      /* There are range_h and range_l because linker has to promise
11744
   all sections move cross one page together.  */
11745
0
      if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11746
0
    || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11747
0
    || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11748
0
  {
11749
0
    if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11750
0
      {
11751
        /* Maybe we should add R_NDS32_INSN16 reloc type here
11752
     or manually do some optimization.  sethi can't be
11753
     eliminated when updating $gp so the relative ori
11754
     needs to be preserved.  */
11755
0
        return;
11756
0
      }
11757
0
    if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11758
0
          &insn))
11759
0
      return;
11760
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11761
0
    bfd_putb32 (insn, contents + laddr);
11762
11763
0
    irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11764
0
             R_NDS32_INSN16);
11765
    /* SDA17 must keep INSN16 for converting fp_as_gp.  */
11766
0
    if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11767
0
      irelfn->r_info =
11768
0
        ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11769
11770
0
  }
11771
0
    }
11772
0
  return;
11773
0
}
11774
11775
/* Relax PTR relocation for nds32_elf_relax_section.  */
11776
11777
static bool
11778
nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11779
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11780
         int *seq_len, bfd_byte *contents)
11781
0
{
11782
0
  Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11783
11784
0
  irelend = internal_relocs + sec->reloc_count;
11785
11786
0
  re_irel =
11787
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11788
0
         R_NDS32_PTR_RESOLVED, irel->r_addend);
11789
11790
0
  if (re_irel == irelend)
11791
0
    {
11792
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11793
0
        (uint64_t) irel->r_offset);
11794
0
      return false;
11795
0
    }
11796
11797
0
  if (re_irel->r_addend != 1)
11798
0
    return false;
11799
11800
  /* Pointed target is relaxed and no longer needs this void *,
11801
     change the type to NONE.  */
11802
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11803
11804
  /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11805
     not exist, it means only count 1 and remove it directly.  */
11806
  /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11807
0
  count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11808
0
               R_NDS32_PTR_COUNT);
11809
0
  ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11810
0
             R_NDS32_PTR);
11811
0
  if (count_irel != irelend)
11812
0
    {
11813
0
      if (--count_irel->r_addend > 0)
11814
0
  return false;
11815
0
    }
11816
11817
0
  if (ptr_irel != irelend)
11818
0
    return false;
11819
11820
  /* If the PTR_COUNT is already 0, remove current instruction.  */
11821
0
  *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11822
0
  *insn_len = 0;
11823
0
  return true;
11824
0
}
11825
11826
/* Relax LWC relocation for nds32_elf_relax_section.  */
11827
11828
static void
11829
nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11830
          asection *sec, Elf_Internal_Rela *irel,
11831
          Elf_Internal_Rela *internal_relocs,
11832
          bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11833
          Elf_Internal_Shdr *symtab_hdr, bool *again)
11834
0
{
11835
  /* Pattern:
11836
     sethi    ra, hi20(symbol)      ; HI20/LOADSTORE
11837
     ori      ra, ra, lo12(symbol)  ; LO12S0/PTR/PTR/.../INSN16
11838
     flsi     fsa, [ra + offset1]   ; LSI/PTR_RESOLVED/INSN16
11839
     flsi     fsb, [ra + offset2]   ; LSI/PTR_RESOLVED/INSN16
11840
     ...  */
11841
11842
0
  uint32_t insn;
11843
0
  bfd_vma local_sda, laddr;
11844
0
  unsigned long reloc;
11845
0
  bfd_vma access_addr, flsi_offset;
11846
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11847
0
  Elf_Internal_Rela *irelend, *re_irel;
11848
0
  unsigned int opcode;
11849
11850
0
  irelend = internal_relocs + sec->reloc_count;
11851
0
  laddr = irel->r_offset;
11852
0
  insn = bfd_getb32 (contents + laddr);
11853
11854
0
  if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11855
0
    return;
11856
11857
  /* Can not do relaxation for bi format.  */
11858
0
  if ((insn & 0x1000))
11859
0
    return;
11860
11861
  /* Only deal with flsi, fssi, fldi, fsdi, so far.  */
11862
0
  opcode = N32_OP6 (insn);
11863
0
  if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11864
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11865
0
  else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11866
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11867
0
  else
11868
0
    return;
11869
11870
0
  re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11871
0
            R_NDS32_PTR_RESOLVED);
11872
0
  if (re_irel == irelend)
11873
0
    {
11874
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11875
0
        (uint64_t) irel->r_offset);
11876
0
      return;
11877
0
    }
11878
11879
  /* For SDA base relative relaxation.  */
11880
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11881
0
          &local_sda, false);
11882
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11883
0
  flsi_offset = (insn & 0xfff) << 2;
11884
0
  access_addr += flsi_offset;
11885
0
  range_l = sdata_range[0][0];
11886
0
  range_h = sdata_range[0][1];
11887
11888
0
  if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11889
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11890
0
    {
11891
      /* Turn flsi instruction into sda access format.  */
11892
0
      insn = (insn & 0x7ff07000) | (REG_GP << 15);
11893
11894
      /* Add relocation type to flsi.  */
11895
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11896
0
      irel->r_addend += flsi_offset;
11897
0
      bfd_putb32 (insn, contents + re_irel->r_offset);
11898
11899
0
      re_irel->r_addend |= 1;
11900
0
      *again = true;
11901
0
    }
11902
0
}
11903
11904
static bool
11905
nds32_relax_adjust_label (bfd *abfd, asection *sec,
11906
        Elf_Internal_Rela *internal_relocs,
11907
        bfd_byte *contents,
11908
        nds32_elf_blank_t **relax_blank_list,
11909
        int optimize, int opt_size)
11910
0
{
11911
  /* This code block is used to adjust 4-byte alignment by relax a pair
11912
     of instruction a time.
11913
11914
     It recognizes three types of relocations.
11915
     1. R_NDS32_LABEL - a alignment.
11916
     2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11917
     3. is_16bit_NOP () - remove a 16-bit instruction.  */
11918
11919
  /* TODO: It seems currently implementation only support 4-byte alignment.
11920
     We should handle any-alignment.  */
11921
11922
0
  Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11923
0
  Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11924
0
  Elf_Internal_Rela rel_temp;
11925
0
  Elf_Internal_Rela *irelend;
11926
0
  bfd_vma address;
11927
0
  uint16_t insn16;
11928
11929
  /* Checking for branch relaxation relies on the relocations to
11930
     be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11931
0
  nds32_insertion_sort (internal_relocs, sec->reloc_count,
11932
0
      sizeof (Elf_Internal_Rela), compar_reloc);
11933
11934
0
  irelend = internal_relocs + sec->reloc_count;
11935
11936
  /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11937
  /* FIXME: Can we generate the right order in assembler?
11938
     So we don't have to swapping them here.  */
11939
11940
0
  for (label_rel = internal_relocs, insn_rel = internal_relocs;
11941
0
       label_rel < irelend; label_rel++)
11942
0
    {
11943
0
      if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11944
0
  continue;
11945
11946
      /* Find the first reloc has the same offset with label_rel.  */
11947
0
      while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11948
0
  insn_rel++;
11949
11950
0
      for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11951
0
     insn_rel++)
11952
  /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11953
     address.  */
11954
0
  if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11955
0
    break;
11956
11957
0
      if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11958
0
    && insn_rel < label_rel)
11959
0
  {
11960
    /* Swap the two reloc if the R_NDS32_INSN16 is
11961
       before R_NDS32_LABEL.  */
11962
0
    memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11963
0
    memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11964
0
    memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11965
0
  }
11966
0
    }
11967
11968
0
  label_rel = NULL;
11969
0
  insn_rel = NULL;
11970
  /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11971
     or higher, remove other R_NDS32_LABEL with lower alignment.
11972
     If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11973
     then the R_NDS32_LABEL sequence is broke.  */
11974
0
  for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11975
0
    {
11976
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11977
0
  {
11978
0
    if (label_rel == NULL)
11979
0
      {
11980
0
        if (tmp_rel->r_addend < 2)
11981
0
    label_rel = tmp_rel;
11982
0
        continue;
11983
0
      }
11984
0
    else if (tmp_rel->r_addend > 1)
11985
0
      {
11986
        /* Remove all LABEL relocation from label_rel to tmp_rel
11987
     including relocations with same offset as tmp_rel.  */
11988
0
        for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
11989
0
    {
11990
0
      if (tmp2_rel->r_offset == tmp_rel->r_offset)
11991
0
        break;
11992
11993
0
      if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11994
0
          && tmp2_rel->r_addend < 2)
11995
0
        tmp2_rel->r_info =
11996
0
          ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11997
0
            R_NDS32_NONE);
11998
0
    }
11999
0
        label_rel = NULL;
12000
0
      }
12001
0
  }
12002
0
      else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12003
0
  {
12004
    /* A new INSN16 which can be converted, so clear label_rel.  */
12005
0
    if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12006
0
           irelend, &insn16)
12007
0
        || is_16bit_NOP (abfd, sec, tmp_rel))
12008
0
      label_rel = NULL;
12009
0
  }
12010
0
    }
12011
12012
0
  label_rel = NULL;
12013
0
  insn_rel = NULL;
12014
  /* Optimized for speed and nothing has not been relaxed.
12015
     It's time to align labels.
12016
     We may convert a 16-bit instruction right before a label to
12017
     32-bit, in order to align the label if necessary
12018
     all reloc entries has been sorted by r_offset.  */
12019
0
  for (irel = internal_relocs;
12020
0
       irel < irelend && irel->r_offset < sec->size; irel++)
12021
0
    {
12022
0
      if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12023
0
    && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12024
0
  continue;
12025
12026
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12027
0
  {
12028
    /* A new INSN16 found, resize the old one.  */
12029
0
    if (is_convert_32_to_16
12030
0
        (abfd, sec, irel, internal_relocs, irelend, &insn16)
12031
0
        || is_16bit_NOP (abfd, sec, irel))
12032
0
      {
12033
0
        if (insn_rel)
12034
0
    {
12035
      /* Previous INSN16 reloc exists, reduce its
12036
         size to 16-bit.  */
12037
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12038
0
             irelend, &insn16))
12039
0
        {
12040
0
          nds32_elf_write_16 (abfd, contents, insn_rel,
12041
0
            internal_relocs, irelend, insn16);
12042
12043
0
          if (!insert_nds32_elf_blank_recalc_total
12044
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12045
0
      return false;
12046
0
        }
12047
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12048
0
        {
12049
0
          if (!insert_nds32_elf_blank_recalc_total
12050
0
        (relax_blank_list, insn_rel->r_offset, 2))
12051
0
      return false;
12052
0
        }
12053
0
      insn_rel->r_info =
12054
0
        ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12055
0
    }
12056
        /* Save the new one for later use.  */
12057
0
        insn_rel = irel;
12058
0
      }
12059
0
    else
12060
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12061
0
           R_NDS32_NONE);
12062
0
  }
12063
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12064
0
  {
12065
    /* Search for label.  */
12066
0
    int force_relax = 0;
12067
12068
    /* Label on 16-bit instruction or optimization
12069
       needless, just reset this reloc.  */
12070
0
    insn16 = bfd_getb16 (contents + irel->r_offset);
12071
0
    if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12072
0
      {
12073
0
        irel->r_info =
12074
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12075
0
        continue;
12076
0
      }
12077
12078
0
    address =
12079
0
      irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12080
0
              irel->r_offset, 1);
12081
12082
0
    if (!insn_rel)
12083
0
      {
12084
        /* Check if there is case which can not be aligned.  */
12085
0
        if (irel->r_addend == 2 && address & 0x2)
12086
0
    return false;
12087
0
        continue;
12088
0
      }
12089
12090
    /* Try to align this label.  */
12091
12092
0
    if ((irel->r_addend & 0x1f) < 2)
12093
0
      {
12094
        /* Check if there is a INSN16 at the same address.
12095
     Label_rel always seats before insn_rel after
12096
     our sort.  */
12097
12098
        /* Search for INSN16 at LABEL location.  If INSN16 is at
12099
     same location and this LABEL alignment is lower than 2,
12100
     the INSN16 can be converted to 2-byte.  */
12101
0
        for (tmp_rel = irel;
12102
0
       tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12103
0
       tmp_rel++)
12104
0
    {
12105
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12106
0
          && (is_convert_32_to_16
12107
0
        (abfd, sec, tmp_rel, internal_relocs,
12108
0
         irelend, &insn16)
12109
0
        || is_16bit_NOP (abfd, sec, tmp_rel)))
12110
0
        {
12111
0
          force_relax = 1;
12112
0
          break;
12113
0
        }
12114
0
    }
12115
0
      }
12116
12117
0
    if (force_relax || irel->r_addend == 1 || address & 0x2)
12118
0
      {
12119
        /* Label not aligned.  */
12120
        /* Previous reloc exists, reduce its size to 16-bit.  */
12121
0
        if (is_convert_32_to_16 (abfd, sec, insn_rel,
12122
0
               internal_relocs, irelend, &insn16))
12123
0
    {
12124
0
      nds32_elf_write_16 (abfd, contents, insn_rel,
12125
0
              internal_relocs, irelend, insn16);
12126
12127
0
      if (!insert_nds32_elf_blank_recalc_total
12128
0
          (relax_blank_list, insn_rel->r_offset + 2, 2))
12129
0
        return false;
12130
0
    }
12131
0
        else if (is_16bit_NOP (abfd, sec, insn_rel))
12132
0
    {
12133
0
      if (!insert_nds32_elf_blank_recalc_total
12134
0
          (relax_blank_list, insn_rel->r_offset, 2))
12135
0
        return false;
12136
0
    }
12137
12138
0
      }
12139
    /* INSN16 reloc is used.  */
12140
0
    insn_rel = NULL;
12141
0
  }
12142
0
    }
12143
12144
0
  address =
12145
0
    sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12146
0
  if (insn_rel && (address & 0x2 || opt_size))
12147
0
    {
12148
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12149
0
             irelend, &insn16))
12150
0
  {
12151
0
    nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12152
0
            irelend, insn16);
12153
0
    if (!insert_nds32_elf_blank_recalc_total
12154
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12155
0
      return false;
12156
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12157
0
             R_NDS32_NONE);
12158
0
  }
12159
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12160
0
  {
12161
0
    if (!insert_nds32_elf_blank_recalc_total
12162
0
        (relax_blank_list, insn_rel->r_offset, 2))
12163
0
      return false;
12164
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12165
0
             R_NDS32_NONE);
12166
0
  }
12167
0
    }
12168
0
  insn_rel = NULL;
12169
0
  return true;
12170
0
}
12171
12172
static bool
12173
nds32_elf_relax_section (bfd *abfd, asection *sec,
12174
       struct bfd_link_info *link_info, bool *again)
12175
0
{
12176
0
  nds32_elf_blank_t *relax_blank_list = NULL;
12177
0
  Elf_Internal_Shdr *symtab_hdr;
12178
0
  Elf_Internal_Rela *internal_relocs;
12179
0
  Elf_Internal_Rela *irel;
12180
0
  Elf_Internal_Rela *irelend;
12181
0
  Elf_Internal_Sym *isymbuf = NULL;
12182
0
  bfd_byte *contents = NULL;
12183
0
  bool result = true;
12184
0
  int optimize = 0;
12185
0
  int opt_size = 0;
12186
0
  uint32_t insn;
12187
0
  uint16_t insn16;
12188
12189
  /* Target dependnet option.  */
12190
0
  struct elf_nds32_link_hash_table *table;
12191
0
  int load_store_relax;
12192
12193
0
  relax_blank_list = NULL;
12194
12195
0
  *again = false;
12196
12197
  /* Nothing to do for
12198
   * relocatable link or
12199
   * non-relocatable section or
12200
   * non-code section or
12201
   * empty content or
12202
   * no reloc entry.  */
12203
0
  if (bfd_link_relocatable (link_info)
12204
0
      || (sec->flags & SEC_RELOC) == 0
12205
0
      || (sec->flags & SEC_EXCLUDE) != 0
12206
0
      || (sec->flags & SEC_CODE) == 0
12207
0
      || sec->size == 0
12208
0
      || sec->reloc_count == 0)
12209
0
    return true;
12210
12211
  /* 09.12.11 Workaround.  */
12212
  /*  We have to adjust align for R_NDS32_LABEL if needed.
12213
      The adjust approach only can fix 2-byte align once.  */
12214
0
  if (sec->alignment_power > 2)
12215
0
    return true;
12216
12217
  /* Do TLS model conversion once at first.  */
12218
0
  nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12219
12220
  /* The optimization type to do.  */
12221
12222
0
  table = nds32_elf_hash_table (link_info);
12223
12224
  /* Save the first section for abs symbol relaxation.
12225
     This is used for checking gp relaxation in the
12226
     nds32_elf_relax_loadstore and nds32_elf_relax_lo12.  */
12227
0
  nds32_elf_relax_guard (NULL, 0, sec, NULL, again, true,
12228
0
       table, NULL, NULL);
12229
12230
  /* The begining of general relaxation.  */
12231
12232
0
  if (is_SDA_BASE_set == 0)
12233
0
    {
12234
0
      bfd_vma gp;
12235
0
      is_SDA_BASE_set = 1;
12236
0
      nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12237
0
        &gp, false);
12238
0
      relax_range_measurement (abfd, link_info);
12239
0
    }
12240
12241
0
  symtab_hdr = &elf_symtab_hdr (abfd);
12242
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
12243
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12244
0
                 true /* keep_memory */);
12245
0
  if (internal_relocs == NULL)
12246
0
    goto error_return;
12247
12248
0
  irelend = internal_relocs + sec->reloc_count;
12249
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
12250
0
         irelend, R_NDS32_RELAX_ENTRY);
12251
12252
0
  if (irel == irelend)
12253
0
    return true;
12254
12255
0
  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12256
0
    {
12257
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12258
0
  return true;
12259
12260
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12261
0
  optimize = 1;
12262
12263
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12264
0
  opt_size = 1;
12265
0
    }
12266
12267
0
  load_store_relax = table->load_store_relax;
12268
12269
  /* Get symbol table and section content.  */
12270
0
  contents = NULL;
12271
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12272
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12273
0
    goto error_return;
12274
12275
  /* Do relax loop only when finalize is not done.
12276
     Take care of relaxable relocs except INSN16.  */
12277
0
  for (irel = internal_relocs; irel < irelend; irel++)
12278
0
    {
12279
0
      int seq_len;    /* Original length of instruction sequence.  */
12280
0
      int insn_len = 0;   /* Final length of instruction sequence.  */
12281
0
      bool removed;
12282
12283
0
      insn = 0;
12284
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12285
0
    && (irel->r_addend & 0x1f) >= 2)
12286
0
  optimize = 1;
12287
12288
      /* Relocation Types
12289
   R_NDS32_LONGCALL1  53
12290
   R_NDS32_LONGCALL2  54
12291
   R_NDS32_LONGCALL3  55
12292
   R_NDS32_LONGJUMP1  56
12293
   R_NDS32_LONGJUMP2  57
12294
   R_NDS32_LONGJUMP3  58
12295
   R_NDS32_LOADSTORE  59  */
12296
0
      if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12297
0
    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12298
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
12299
12300
      /* Relocation Types
12301
   R_NDS32_LONGCALL4  107
12302
   R_NDS32_LONGCALL5  108
12303
   R_NDS32_LONGCALL6  109
12304
   R_NDS32_LONGJUMP4  110
12305
   R_NDS32_LONGJUMP5  111
12306
   R_NDS32_LONGJUMP6  112
12307
   R_NDS32_LONGJUMP7  113  */
12308
0
      else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12309
0
         && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12310
0
  seq_len = 4;
12311
12312
  /* Relocation Types
12313
   R_NDS32_LO12S0_RELA    30
12314
   R_NDS32_LO12S1_RELA    29
12315
   R_NDS32_LO12S2_RELA    28
12316
   R_NDS32_LO12S2_SP_RELA   71
12317
   R_NDS32_LO12S2_DP_RELA   70
12318
   R_NDS32_GOT_LO12   46
12319
   R_NDS32_GOTOFF_LO12    50
12320
   R_NDS32_PLTREL_LO12    65
12321
   R_NDS32_PLT_GOTREL_LO12  67
12322
   R_NDS32_17IFC_PCREL_RELA 96
12323
   R_NDS32_GOT_SUFF   193
12324
   R_NDS32_GOTOFF_SUFF    194
12325
   R_NDS32_PLT_GOT_SUFF   195
12326
   R_NDS32_MULCALL_SUFF   196
12327
   R_NDS32_PTR      197  */
12328
0
      else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12329
0
    && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12330
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12331
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12332
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12333
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12334
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12335
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12336
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12337
0
         || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12338
0
       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12339
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12340
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12341
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12342
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12343
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12344
0
  seq_len = 0;
12345
0
      else
12346
0
  continue;
12347
12348
0
      insn_len = seq_len;
12349
0
      removed = false;
12350
12351
0
      switch (ELF32_R_TYPE (irel->r_info))
12352
0
  {
12353
0
  case R_NDS32_LONGCALL1:
12354
0
    removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12355
0
                 &insn_len, contents, isymbuf,
12356
0
                 symtab_hdr);
12357
0
    break;
12358
0
  case R_NDS32_LONGCALL2:
12359
0
    removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12360
0
                 &insn_len, contents, isymbuf,
12361
0
                 symtab_hdr);
12362
0
    break;
12363
0
  case R_NDS32_LONGCALL3:
12364
0
    removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12365
0
                 &insn_len, contents, isymbuf,
12366
0
                 symtab_hdr);
12367
0
    break;
12368
0
  case R_NDS32_LONGJUMP1:
12369
0
    removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12370
0
                 &insn_len, contents, isymbuf,
12371
0
                 symtab_hdr);
12372
0
    break;
12373
0
  case R_NDS32_LONGJUMP2:
12374
0
    removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12375
0
                 &insn_len, contents, isymbuf,
12376
0
                 symtab_hdr);
12377
0
    break;
12378
0
  case R_NDS32_LONGJUMP3:
12379
0
    removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12380
0
                 &insn_len, contents, isymbuf,
12381
0
                 symtab_hdr);
12382
0
    break;
12383
0
  case R_NDS32_LONGCALL4:
12384
0
    removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12385
0
                 &insn_len, contents, isymbuf,
12386
0
                 symtab_hdr);
12387
0
    break;
12388
0
  case R_NDS32_LONGCALL5:
12389
0
    removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12390
0
                 &insn_len, contents, isymbuf,
12391
0
                 symtab_hdr);
12392
0
    break;
12393
0
  case R_NDS32_LONGCALL6:
12394
0
    removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12395
0
                 &insn_len, contents, isymbuf,
12396
0
                 symtab_hdr);
12397
0
    break;
12398
0
  case R_NDS32_LONGJUMP4:
12399
0
    removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12400
0
                 &insn_len, contents, isymbuf,
12401
0
                 symtab_hdr);
12402
0
    break;
12403
0
  case R_NDS32_LONGJUMP5:
12404
0
    removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12405
0
                 &insn_len, &seq_len, contents,
12406
0
                 isymbuf, symtab_hdr);
12407
0
    break;
12408
0
  case R_NDS32_LONGJUMP6:
12409
0
    removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12410
0
                 &insn_len, &seq_len, contents,
12411
0
                 isymbuf, symtab_hdr);
12412
0
    break;
12413
0
  case R_NDS32_LONGJUMP7:
12414
0
    removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12415
0
                 &insn_len, &seq_len, contents,
12416
0
                 isymbuf, symtab_hdr);
12417
0
    break;
12418
0
  case R_NDS32_LOADSTORE:
12419
0
    removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12420
0
                 internal_relocs, &insn_len,
12421
0
                 contents, isymbuf, symtab_hdr,
12422
0
                 load_store_relax, table);
12423
0
    break;
12424
0
  case R_NDS32_LO12S0_RELA:
12425
0
  case R_NDS32_LO12S1_RELA:
12426
0
  case R_NDS32_LO12S2_RELA:
12427
0
  case R_NDS32_LO12S2_DP_RELA:
12428
0
  case R_NDS32_LO12S2_SP_RELA:
12429
    /* Relax for low part.  */
12430
0
    nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12431
0
        contents, isymbuf, symtab_hdr, table);
12432
12433
    /* It is impossible to delete blank, so just continue.  */
12434
0
    continue;
12435
0
  case R_NDS32_PTR:
12436
0
    removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12437
0
           &insn_len, &seq_len, contents);
12438
0
    break;
12439
0
  case R_NDS32_LSI:
12440
0
    nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12441
0
        contents, isymbuf, symtab_hdr, again);
12442
0
    continue;
12443
0
  case R_NDS32_GOT_LO12:
12444
0
  case R_NDS32_GOTOFF_LO12:
12445
0
  case R_NDS32_PLTREL_LO12:
12446
0
  case R_NDS32_PLT_GOTREL_LO12:
12447
0
  case R_NDS32_GOTPC_LO12:
12448
0
  case R_NDS32_TLS_LE_LO12:
12449
0
  case R_NDS32_TLS_LE_ADD:
12450
0
  case R_NDS32_TLS_LE_LS:
12451
0
  case R_NDS32_PLT_GOT_SUFF:
12452
0
  case R_NDS32_GOT_SUFF:
12453
0
  case R_NDS32_GOTOFF_SUFF:
12454
0
    continue;
12455
0
  default:
12456
0
    continue;
12457
0
  }
12458
12459
0
      if (removed && seq_len - insn_len > 0)
12460
0
  {
12461
0
    if (!insert_nds32_elf_blank
12462
0
        (&relax_blank_list, irel->r_offset + insn_len,
12463
0
         seq_len - insn_len))
12464
0
      goto error_return;
12465
0
    *again = true;
12466
0
  }
12467
0
    }
12468
12469
0
  calc_nds32_blank_total (relax_blank_list);
12470
12471
0
  if (table->relax_fp_as_gp)
12472
0
    {
12473
0
      if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12474
0
         irelend, isymbuf))
12475
0
  goto error_return;
12476
12477
0
      if (!*again)
12478
0
  {
12479
0
    if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12480
0
                 irelend))
12481
0
      goto error_return;
12482
0
  }
12483
0
    }
12484
12485
0
  if (!*again)
12486
0
    {
12487
0
      if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12488
0
             &relax_blank_list, optimize, opt_size))
12489
0
  goto error_return;
12490
0
    }
12491
12492
  /* It doesn't matter optimize_for_space_no_align anymore.
12493
       If object file is assembled with flag '-Os',
12494
       the we don't adjust jump-destination on 4-byte boundary.  */
12495
12496
0
  if (relax_blank_list)
12497
0
    {
12498
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12499
0
      relax_blank_list = NULL;
12500
0
    }
12501
12502
0
  if (!*again)
12503
0
    {
12504
      /* Closing the section, so we don't relax it anymore.  */
12505
0
      bfd_vma sec_size_align;
12506
0
      Elf_Internal_Rela *tmp_rel;
12507
12508
      /* Pad to alignment boundary.  Only handle current section alignment.  */
12509
0
      sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12510
0
           & ((-1U) << sec->alignment_power);
12511
0
      if ((sec_size_align - sec->size) & 0x2)
12512
0
  {
12513
0
    insn16 = NDS32_NOP16;
12514
0
    bfd_putb16 (insn16, contents + sec->size);
12515
0
    sec->size += 2;
12516
0
  }
12517
12518
0
      while (sec_size_align != sec->size)
12519
0
  {
12520
0
    insn = NDS32_NOP32;
12521
0
    bfd_putb32 (insn, contents + sec->size);
12522
0
    sec->size += 4;
12523
0
  }
12524
12525
0
      tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12526
0
          irelend, R_NDS32_RELAX_ENTRY);
12527
0
      if (tmp_rel != irelend)
12528
0
  tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12529
12530
0
      clean_nds32_elf_blank ();
12531
0
    }
12532
12533
0
 finish:
12534
0
  if (elf_section_data (sec)->relocs != internal_relocs)
12535
0
    free (internal_relocs);
12536
12537
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
12538
0
    free (contents);
12539
12540
0
  if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12541
0
    free (isymbuf);
12542
12543
0
  return result;
12544
12545
0
 error_return:
12546
0
  result = false;
12547
0
  goto finish;
12548
0
}
12549
12550
static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12551
{
12552
  {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12553
  {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12554
  {NULL, 0, 0, 0, 0}
12555
};
12556
12557
static bool
12558
nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12559
3.99k
{
12560
3.99k
  const char *name = hdr->bfd_section->name;
12561
12562
3.99k
  if (startswith (name, ".sbss")
12563
3.92k
      || startswith (name, ".sdata"))
12564
139
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
12565
12566
3.99k
  return true;
12567
3.99k
}
12568
12569
static bool
12570
nds32_elf_output_arch_syms (struct bfd_link_info *info,
12571
          void *finfo ATTRIBUTE_UNUSED,
12572
          int (*func) (void *, const char *,
12573
           Elf_Internal_Sym *,
12574
           asection *,
12575
           struct elf_link_hash_entry *)
12576
          ATTRIBUTE_UNUSED)
12577
0
{
12578
0
  FILE *sym_ld_script = NULL;
12579
0
  struct elf_nds32_link_hash_table *table;
12580
12581
0
  table = nds32_elf_hash_table (info);
12582
0
  sym_ld_script = table->sym_ld_script;
12583
12584
0
  if (check_start_export_sym)
12585
0
    fprintf (sym_ld_script, "}\n");
12586
12587
0
  return true;
12588
0
}
12589
12590
static enum elf_reloc_type_class
12591
nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12592
          const asection *rel_sec ATTRIBUTE_UNUSED,
12593
          const Elf_Internal_Rela *rela)
12594
0
{
12595
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
12596
0
    {
12597
0
    case R_NDS32_RELATIVE:
12598
0
      return reloc_class_relative;
12599
0
    case R_NDS32_JMP_SLOT:
12600
0
      return reloc_class_plt;
12601
0
    case R_NDS32_COPY:
12602
0
      return reloc_class_copy;
12603
0
    default:
12604
0
      return reloc_class_normal;
12605
0
    }
12606
0
}
12607
12608
/* Put target dependent option into info hash table.  */
12609
void
12610
bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12611
           int relax_fp_as_gp,
12612
           int eliminate_gc_relocs,
12613
           FILE * sym_ld_script,
12614
           int hyper_relax,
12615
           int tls_desc_trampoline,
12616
           int load_store_relax)
12617
0
{
12618
0
  struct elf_nds32_link_hash_table *table;
12619
12620
0
  table = nds32_elf_hash_table (link_info);
12621
0
  if (table == NULL)
12622
0
    return;
12623
12624
0
  table->relax_fp_as_gp = relax_fp_as_gp;
12625
0
  table->eliminate_gc_relocs = eliminate_gc_relocs;
12626
0
  table->sym_ld_script = sym_ld_script;
12627
0
  table->hyper_relax = hyper_relax;
12628
0
  table->tls_desc_trampoline = tls_desc_trampoline;
12629
0
  table ->load_store_relax = load_store_relax;
12630
0
}
12631

12632
12633
/* These functions and data-structures are used for fp-as-gp
12634
   optimization.  */
12635
12636
0
#define FAG_THRESHOLD 3  /* At least 3 gp-access.  */
12637
/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12638
   the read-only section and read-write section.  */
12639
0
#define FAG_WINDOW  (508 - 32)
12640
12641
/* An nds32_fag represent a gp-relative access.
12642
   We find best fp-base by using a sliding window
12643
   to find a base address which can cover most gp-access.  */
12644
struct nds32_fag
12645
{
12646
  struct nds32_fag *next; /* NULL-teminated linked list.  */
12647
  bfd_vma addr;     /* The address of this fag.  */
12648
  Elf_Internal_Rela **relas;  /* The relocations associated with this fag.
12649
           It is used for applying FP7U2_FLAG.  */
12650
  int count;      /* How many times this address is referred.
12651
           There should be exactly `count' relocations
12652
           in relas.  */
12653
  int relas_capcity;    /* The buffer size of relas.
12654
           We use an array instead of linked-list,
12655
           and realloc is used to adjust buffer size.  */
12656
};
12657
12658
static void
12659
nds32_fag_init (struct nds32_fag *head)
12660
0
{
12661
0
  memset (head, 0, sizeof (struct nds32_fag));
12662
0
}
12663
12664
static void
12665
nds32_fag_verify (struct nds32_fag *head)
12666
0
{
12667
0
  struct nds32_fag *iter;
12668
0
  struct nds32_fag *prev;
12669
12670
0
  prev = NULL;
12671
0
  iter = head->next;
12672
0
  while (iter)
12673
0
    {
12674
0
      if (prev && prev->addr >= iter->addr)
12675
0
  puts ("Bug in fp-as-gp insertion.");
12676
0
      prev = iter;
12677
0
      iter = iter->next;
12678
0
    }
12679
0
}
12680
12681
/* Insert a fag in ascending order.
12682
   If a fag of the same address already exists,
12683
   they are chained by relas array.  */
12684
12685
static void
12686
nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12687
      Elf_Internal_Rela * rel)
12688
0
{
12689
0
  struct nds32_fag *iter;
12690
0
  struct nds32_fag *new_fag;
12691
0
  const int INIT_RELAS_CAP = 4;
12692
12693
0
  for (iter = head;
12694
0
       iter->next && iter->next->addr <= addr;
12695
0
       iter = iter->next)
12696
0
    /* Find somewhere to insert.  */ ;
12697
12698
  /* `iter' will be equal to `head' if the list is empty.  */
12699
0
  if (iter != head && iter->addr == addr)
12700
0
    {
12701
      /* The address exists in the list.
12702
   Insert `rel' into relocation list, relas.  */
12703
12704
      /* Check whether relas is big enough.  */
12705
0
      if (iter->count >= iter->relas_capcity)
12706
0
  {
12707
0
    iter->relas_capcity *= 2;
12708
0
    iter->relas = bfd_realloc
12709
0
      (iter->relas, iter->relas_capcity * sizeof (void *));
12710
0
  }
12711
0
      iter->relas[iter->count++] = rel;
12712
0
      return;
12713
0
    }
12714
12715
  /* This is a new address.  Create a fag node for it.  */
12716
0
  new_fag = bfd_malloc (sizeof (struct nds32_fag));
12717
0
  memset (new_fag, 0, sizeof (*new_fag));
12718
0
  new_fag->addr = addr;
12719
0
  new_fag->count = 1;
12720
0
  new_fag->next = iter->next;
12721
0
  new_fag->relas_capcity = INIT_RELAS_CAP;
12722
0
  new_fag->relas = (Elf_Internal_Rela **)
12723
0
    bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12724
0
  new_fag->relas[0] = rel;
12725
0
  iter->next = new_fag;
12726
12727
0
  nds32_fag_verify (head);
12728
0
}
12729
12730
static void
12731
nds32_fag_free_list (struct nds32_fag *head)
12732
0
{
12733
0
  struct nds32_fag *iter;
12734
12735
0
  iter = head->next;
12736
0
  while (iter)
12737
0
    {
12738
0
      struct nds32_fag *tmp = iter;
12739
0
      iter = iter->next;
12740
0
      free (tmp->relas);
12741
0
      tmp->relas = NULL;
12742
0
      free (tmp);
12743
0
    }
12744
0
}
12745
12746
/* Find the best fp-base address.
12747
   The relocation associated with that address is returned,
12748
   so we can track the symbol instead of a fixed address.
12749
12750
   When relaxation, the address of an datum may change,
12751
   because a text section is shrinked, so the data section
12752
   moves forward.  If the aligments of text and data section
12753
   are different, their distance may change too.
12754
   Therefore, tracking a fixed address is not appriate.  */
12755
12756
static int
12757
nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12758
0
{
12759
0
  struct nds32_fag *base; /* First fag in the window.  */
12760
0
  struct nds32_fag *last; /* First fag outside the window.  */
12761
0
  int accu = 0;     /* Usage accumulation.  */
12762
0
  struct nds32_fag *best; /* Best fag.  */
12763
0
  int baccu = 0;    /* Best accumulation.  */
12764
12765
  /* Use first fag for initial, and find the last fag in the window.
12766
12767
     In each iteration, we could simply subtract previous fag
12768
     and accumulate following fags which are inside the window,
12769
     untill we each the end.  */
12770
12771
0
  if (head->next == NULL)
12772
0
    {
12773
0
      *bestpp = NULL;
12774
0
      return 0;
12775
0
    }
12776
12777
  /* Initialize base.  */
12778
0
  base = head->next;
12779
0
  best = base;
12780
0
  for (last = base;
12781
0
       last && last->addr < base->addr + FAG_WINDOW;
12782
0
       last = last->next)
12783
0
    accu += last->count;
12784
12785
0
  baccu = accu;
12786
12787
  /* Record the best base in each iteration.  */
12788
0
  while (base->next)
12789
0
    {
12790
0
      accu -= base->count;
12791
0
      base = base->next;
12792
      /* Account fags in window.  */
12793
0
      for (/* Nothing.  */;
12794
0
     last && last->addr < base->addr + FAG_WINDOW;
12795
0
     last = last->next)
12796
0
  accu += last->count;
12797
12798
      /* A better fp-base?  */
12799
0
      if (accu > baccu)
12800
0
  {
12801
0
    best = base;
12802
0
    baccu = accu;
12803
0
  }
12804
0
    }
12805
12806
0
  if (bestpp)
12807
0
    *bestpp = best;
12808
0
  return baccu;
12809
0
}
12810
12811
/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12812
   so we can convert it fo fp-relative access later.
12813
   `best_fag' is the best fp-base.  Only those inside the window
12814
   of best_fag is applied the flag.  */
12815
12816
static bool
12817
nds32_fag_mark_relax (struct bfd_link_info *link_info,
12818
          asection *sec, struct nds32_fag *best_fag,
12819
          Elf_Internal_Rela *internal_relocs,
12820
          Elf_Internal_Rela *irelend)
12821
0
{
12822
0
  struct nds32_fag *ifag;
12823
0
  bfd_vma best_fpbase, gp;
12824
0
  bfd *output_bfd;
12825
12826
0
  output_bfd = sec->output_section->owner;
12827
0
  nds32_elf_final_sda_base (output_bfd, link_info, &gp, false);
12828
0
  best_fpbase = best_fag->addr;
12829
12830
0
  if (best_fpbase > gp + sdata_range[1][1]
12831
0
      || best_fpbase < gp - sdata_range[1][0])
12832
0
    return false;
12833
12834
  /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12835
     so we know they can be converted to lwi37.fp.   */
12836
0
  for (ifag = best_fag;
12837
0
       ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12838
0
    {
12839
0
      int i;
12840
12841
0
      for (i = 0; i < ifag->count; i++)
12842
0
  {
12843
0
    Elf_Internal_Rela *insn16_rel;
12844
0
    Elf_Internal_Rela *fag_rel;
12845
12846
0
    fag_rel = ifag->relas[i];
12847
12848
    /* Only if this is within the WINDOWS, FP7U2_FLAG
12849
       is applied.  */
12850
12851
0
    insn16_rel = find_relocs_at_address
12852
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12853
12854
0
    if (insn16_rel != irelend)
12855
0
      insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12856
0
  }
12857
0
    }
12858
0
  return true;
12859
0
}
12860
12861
/* Reset INSN16 to clean fp as gp.  */
12862
12863
static void
12864
nds32_fag_unmark_relax (struct nds32_fag *fag,
12865
      Elf_Internal_Rela *internal_relocs,
12866
      Elf_Internal_Rela *irelend)
12867
0
{
12868
0
  struct nds32_fag *ifag;
12869
0
  int i;
12870
0
  Elf_Internal_Rela *insn16_rel;
12871
0
  Elf_Internal_Rela *fag_rel;
12872
12873
0
  for (ifag = fag; ifag; ifag = ifag->next)
12874
0
    {
12875
0
      for (i = 0; i < ifag->count; i++)
12876
0
  {
12877
0
    fag_rel = ifag->relas[i];
12878
12879
    /* Restore the INSN16 relocation.  */
12880
0
    insn16_rel = find_relocs_at_address
12881
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12882
12883
0
    if (insn16_rel != irelend)
12884
0
      insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12885
0
  }
12886
0
    }
12887
0
}
12888
12889
/* This is the main function of fp-as-gp optimization.
12890
   It should be called by relax_section.  */
12891
12892
static bool
12893
nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12894
          bfd *abfd, asection *sec,
12895
          Elf_Internal_Rela *internal_relocs,
12896
          Elf_Internal_Rela *irelend,
12897
          Elf_Internal_Sym *isymbuf)
12898
0
{
12899
0
  Elf_Internal_Rela *begin_rel = NULL;
12900
0
  Elf_Internal_Rela *irel;
12901
0
  struct nds32_fag fag_head;
12902
0
  Elf_Internal_Shdr *symtab_hdr;
12903
0
  bfd_byte *contents;
12904
0
  bool ifc_inside = false;
12905
12906
  /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12907
12908
  /* Per-function fp-base selection.
12909
     1. Create a list for all the gp-relative access.
12910
     2. Base on those gp-relative address,
12911
  find a fp-base which can cover most access.
12912
     3. Use the fp-base for fp-as-gp relaxation.
12913
12914
     NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12915
     we should
12916
     1. delete the `la $fp, _FP_BASE_' instruction and
12917
     2. not convert lwi.gp to lwi37.fp.
12918
12919
     To delete the _FP_BASE_ instruction, we simply apply
12920
     R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12921
12922
     To suppress the conversion, we simply NOT to apply
12923
     R_NDS32_INSN16_FP7U2_FLAG flag.  */
12924
12925
0
  symtab_hdr = &elf_symtab_hdr (abfd);
12926
12927
0
  contents = NULL;
12928
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12929
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12930
0
    return false;
12931
12932
  /* Check whether it is worth for fp-as-gp optimization,
12933
     i.e., at least 3 gp-load.
12934
12935
     Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12936
     apply this optimization.  */
12937
12938
0
  for (irel = internal_relocs; irel < irelend; irel++)
12939
0
    {
12940
      /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12941
   One we enter the begin of the region, we track all the LW/ST
12942
   instructions, so when we leave the region, we try to find
12943
   the best fp-base address for those LW/ST instructions.  */
12944
12945
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12946
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12947
0
  {
12948
    /* Begin of the region.  */
12949
0
    if (begin_rel)
12950
      /* xgettext:c-format */
12951
0
      _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12952
12953
0
    begin_rel = irel;
12954
0
    nds32_fag_init (&fag_head);
12955
0
    ifc_inside = false;
12956
0
  }
12957
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12958
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12959
0
  {
12960
0
    int accu;
12961
0
    struct nds32_fag *best_fag, *tmp_fag;
12962
0
    int dist;
12963
12964
    /* End of the region.
12965
       Check whether it is worth to do fp-as-gp.  */
12966
12967
0
    if (begin_rel == NULL)
12968
0
      {
12969
        /* xgettext:c-format */
12970
0
        _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12971
0
          abfd, sec);
12972
0
        continue;
12973
0
      }
12974
12975
0
    accu = nds32_fag_find_base (&fag_head, &best_fag);
12976
12977
    /* Clean FP7U2_FLAG because they may set ever.  */
12978
0
    tmp_fag = fag_head.next;
12979
0
    nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12980
12981
    /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
12982
0
    if (accu < FAG_THRESHOLD
12983
0
        || !nds32_fag_mark_relax (link_info, sec, best_fag,
12984
0
          internal_relocs, irelend))
12985
0
      {
12986
        /* Not worth to do fp-as-gp.  */
12987
0
        begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12988
0
        begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12989
0
        irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12990
0
        irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12991
0
        nds32_fag_free_list (&fag_head);
12992
0
        begin_rel = NULL;
12993
0
        continue;
12994
0
      }
12995
12996
    /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12997
       so we use it to record the distance to the reloction of best
12998
       fp-base.  */
12999
0
    dist = best_fag->relas[0] - begin_rel;
13000
0
    BFD_ASSERT (dist > 0 && dist < 0xffffff);
13001
    /* Use high 16 bits of addend to record the _FP_BASE_ matched
13002
       relocation.  And get the base value when relocating.  */
13003
0
    begin_rel->r_addend &= (0x1 << 16) - 1;
13004
0
    begin_rel->r_addend |= dist << 16;
13005
13006
0
    nds32_fag_free_list (&fag_head);
13007
0
    begin_rel = NULL;
13008
0
  }
13009
13010
0
      if (begin_rel == NULL || ifc_inside)
13011
  /* Skip if we are not in the region of fp-as-gp.  */
13012
0
  continue;
13013
13014
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13015
0
    || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13016
0
  {
13017
0
    bfd_vma addr;
13018
0
    uint32_t insn;
13019
13020
    /* A gp-relative access is found.  Insert it to the fag-list.  */
13021
13022
    /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
13023
0
    insn = bfd_getb32 (contents + irel->r_offset);
13024
0
    if (!N32_IS_RT3 (insn))
13025
0
      continue;
13026
13027
0
    addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13028
0
    nds32_fag_insert (&fag_head, addr, irel);
13029
0
  }
13030
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13031
0
  {
13032
0
    begin_rel = NULL;
13033
0
  }
13034
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13035
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13036
0
  {
13037
    /* Suppress fp as gp when encounter ifc.  */
13038
0
    ifc_inside = true;
13039
0
  }
13040
0
    }
13041
13042
0
  return true;
13043
0
}
13044
13045
/* Remove unused `la $fp, _FD_BASE_' instruction.  */
13046
13047
static bool
13048
nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13049
        Elf_Internal_Rela *internal_relocs,
13050
        Elf_Internal_Rela *irelend)
13051
0
{
13052
0
  Elf_Internal_Rela *irel;
13053
0
  Elf_Internal_Shdr *symtab_hdr;
13054
0
  bfd_byte *contents = NULL;
13055
0
  nds32_elf_blank_t *relax_blank_list = NULL;
13056
0
  bool result = true;
13057
0
  bool unused_region = false;
13058
13059
  /*
13060
     NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13061
       R_NDS32_17IFC_PCREL_RELA
13062
       R_NDS32_10IFCU_PCREL_RELA.  */
13063
13064
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13065
0
  nds32_get_section_contents (abfd, sec, &contents, true);
13066
13067
0
  for (irel = internal_relocs; irel < irelend; irel++)
13068
0
    {
13069
      /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13070
   we marked to in previous pass.
13071
   DO NOT scan relocations again, since we've alreadly decided it
13072
   and set the flag.  */
13073
0
      const char *syname;
13074
0
      int syndx;
13075
0
      uint32_t insn;
13076
13077
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13078
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13079
0
  unused_region = true;
13080
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13081
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13082
0
  unused_region = false;
13083
13084
      /* We're not in the region.  */
13085
0
      if (!unused_region)
13086
0
  continue;
13087
13088
      /* _FP_BASE_ must be a GLOBAL symbol.  */
13089
0
      syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13090
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13091
0
  continue;
13092
13093
      /* The symbol name must be _FP_BASE_.  */
13094
0
      syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13095
0
      if (strcmp (syname, FP_BASE_NAME) != 0)
13096
0
  continue;
13097
13098
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13099
0
  {
13100
    /* addi.gp  $fp, -256  */
13101
0
    insn = bfd_getb32 (contents + irel->r_offset);
13102
0
    if (insn != INSN_ADDIGP_TO_FP)
13103
0
      continue;
13104
0
  }
13105
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13106
0
  {
13107
    /* addi  $fp, $gp, -256  */
13108
0
    insn = bfd_getb32 (contents + irel->r_offset);
13109
0
    if (insn != INSN_ADDI_GP_TO_FP)
13110
0
      continue;
13111
0
  }
13112
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13113
0
  {
13114
    /* movi  $fp, FP_BASE  */
13115
0
    insn = bfd_getb32 (contents + irel->r_offset);
13116
0
    if (insn != INSN_MOVI_TO_FP)
13117
0
      continue;
13118
0
  }
13119
0
      else
13120
0
  continue;
13121
13122
      /* We got here because a FP_BASE instruction is found.  */
13123
0
      if (!insert_nds32_elf_blank_recalc_total
13124
0
    (&relax_blank_list, irel->r_offset, 4))
13125
0
  goto error_return;
13126
0
    }
13127
13128
0
 finish:
13129
0
  if (relax_blank_list)
13130
0
    {
13131
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13132
0
      relax_blank_list = NULL;
13133
0
    }
13134
0
  return result;
13135
13136
0
 error_return:
13137
0
  result = false;
13138
0
  goto finish;
13139
0
}
13140
13141
/* This is a version of bfd_generic_get_relocated_section_contents.
13142
   We need this variety because relaxation will modify the dwarf
13143
   infomation.  When there is undefined symbol reference error mesage,
13144
   linker need to dump line number where the symbol be used.  However
13145
   the address is be relaxed, it can not get the original dwarf contents.
13146
   The variety only modify function call for reading in the section.  */
13147
13148
static bfd_byte *
13149
nds32_elf_get_relocated_section_contents (bfd *abfd,
13150
            struct bfd_link_info *link_info,
13151
            struct bfd_link_order *link_order,
13152
            bfd_byte *data,
13153
            bool relocatable,
13154
            asymbol **symbols)
13155
67
{
13156
67
  bfd *input_bfd = link_order->u.indirect.section->owner;
13157
67
  asection *input_section = link_order->u.indirect.section;
13158
67
  long reloc_size;
13159
67
  arelent **reloc_vector;
13160
67
  long reloc_count;
13161
13162
67
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13163
67
  if (reloc_size < 0)
13164
2
    return NULL;
13165
13166
  /* Read in the section.  */
13167
65
  bfd_byte *orig_data = data;
13168
65
  if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
13169
1
    return NULL;
13170
13171
64
  if (reloc_size == 0)
13172
0
    return data;
13173
13174
64
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13175
64
  if (reloc_vector == NULL)
13176
0
    goto error_return;
13177
13178
64
  reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13179
64
          reloc_vector, symbols);
13180
64
  if (reloc_count < 0)
13181
14
    goto error_return;
13182
13183
50
  if (reloc_count > 0)
13184
33
    {
13185
33
      arelent **parent;
13186
270
      for (parent = reloc_vector; *parent != NULL; parent++)
13187
262
  {
13188
262
    char *error_message = NULL;
13189
262
    asymbol *symbol;
13190
262
    bfd_reloc_status_type r;
13191
13192
262
    symbol = *(*parent)->sym_ptr_ptr;
13193
262
    if (symbol->section && discarded_section (symbol->section))
13194
0
      {
13195
0
        bfd_vma off;
13196
0
        static reloc_howto_type none_howto
13197
0
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13198
0
       "unused", false, 0, 0, false);
13199
13200
0
        off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13201
0
                input_section);
13202
0
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13203
0
           input_section, data, off);
13204
0
        (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13205
0
        (*parent)->addend = 0;
13206
0
        (*parent)->howto = &none_howto;
13207
0
        r = bfd_reloc_ok;
13208
0
      }
13209
262
    else
13210
262
      r = bfd_perform_relocation (input_bfd, *parent, data,
13211
262
          input_section,
13212
262
          relocatable ? abfd : NULL,
13213
262
          &error_message);
13214
13215
262
    if (relocatable)
13216
0
      {
13217
0
        asection *os = input_section->output_section;
13218
13219
        /* A partial link, so keep the relocs.  */
13220
0
        os->orelocation[os->reloc_count] = *parent;
13221
0
        os->reloc_count++;
13222
0
      }
13223
13224
262
    if (r != bfd_reloc_ok)
13225
36
      {
13226
36
        _bfd_link_reloc_status_error (abfd, link_info, input_section,
13227
36
              *parent, error_message, r);
13228
36
        if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
13229
25
    goto error_return;
13230
36
      }
13231
262
  }
13232
33
    }
13233
13234
25
  free (reloc_vector);
13235
25
  return data;
13236
13237
39
 error_return:
13238
39
  free (reloc_vector);
13239
39
  if (orig_data == NULL)
13240
18
    free (data);
13241
39
  return NULL;
13242
50
}
13243
13244
/* Check target symbol.  */
13245
13246
static bool
13247
nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13248
1.39k
{
13249
1.39k
  if (!sym || !sym->name || sym->name[0] != '$')
13250
1.37k
    return false;
13251
23
  return true;
13252
1.39k
}
13253
13254
/* nds32 find maybe function sym.  Ignore target special symbol
13255
   first, and then go the general function.  */
13256
13257
static bfd_size_type
13258
nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13259
            bfd_vma *code_off)
13260
1.09k
{
13261
1.09k
  if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13262
17
    return 0;
13263
13264
1.08k
  return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13265
1.09k
}
13266

13267
13268
/* Do TLS model conversion.  */
13269
13270
typedef struct relax_group_list_t
13271
{
13272
  Elf_Internal_Rela *relo;
13273
  struct relax_group_list_t *next;
13274
  struct relax_group_list_t *next_sibling;
13275
  int id;
13276
} relax_group_list_t;
13277
13278
int
13279
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13280
13281
int
13282
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13283
13284
void
13285
dump_chain (relax_group_list_t *pHead);
13286
13287
int
13288
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13289
0
{
13290
0
  relax_group_list_t *pNext = pHead;
13291
13292
  /* Find place.  */
13293
0
  while (pNext->next)
13294
0
    {
13295
0
      if (pNext->next->id > (int) pElem->r_addend)
13296
0
  break;
13297
13298
0
      pNext = pNext->next;
13299
0
    }
13300
13301
  /* Insert node.  */
13302
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13303
0
  if (!pNew)
13304
0
    return false;
13305
13306
0
  relax_group_list_t *tmp = pNext->next;
13307
0
  pNext->next = pNew;
13308
13309
0
  pNew->id = pElem->r_addend;
13310
0
  pNew->relo = pElem;
13311
0
  pNew->next = tmp;
13312
0
  pNew->next_sibling = NULL;
13313
13314
0
  return true;
13315
0
}
13316
13317
int
13318
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13319
0
{
13320
0
  relax_group_list_t *pNext = pNode;
13321
13322
  /* Find place.  */
13323
0
  while (pNext->next_sibling)
13324
0
    {
13325
0
      pNext = pNext->next_sibling;
13326
0
    }
13327
13328
  /* Insert node.  */
13329
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13330
0
  if (!pNew)
13331
0
    return false;
13332
13333
0
  relax_group_list_t *tmp = pNext->next_sibling;
13334
0
  pNext->next_sibling = pNew;
13335
13336
0
  pNew->id = -1;
13337
0
  pNew->relo = pElem;
13338
0
  pNew->next = NULL;
13339
0
  pNew->next_sibling = tmp;
13340
13341
0
  return true;
13342
0
}
13343
13344
void
13345
dump_chain (relax_group_list_t *pHead)
13346
0
{
13347
0
  relax_group_list_t *pNext = pHead->next;
13348
0
  while (pNext)
13349
0
    {
13350
0
      printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13351
0
      relax_group_list_t *pNextSib = pNext->next_sibling;
13352
0
      while (pNextSib)
13353
0
  {
13354
0
    printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13355
0
    pNextSib = pNextSib->next_sibling;
13356
0
  }
13357
0
      pNext = pNext->next;
13358
0
      printf("\n");
13359
0
    }
13360
0
}
13361
13362
/* Check R_NDS32_RELAX_GROUP of each section.
13363
   There might be multiple sections in one object file.  */
13364
13365
int
13366
elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13367
0
{
13368
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13369
0
    elf32_nds32_relax_group_ptr (abfd);
13370
13371
0
  int min_id = relax_group_ptr->min_id;
13372
0
  int max_id = relax_group_ptr->max_id;
13373
13374
0
  Elf_Internal_Rela *rel;
13375
0
  Elf_Internal_Rela *relend;
13376
0
  Elf_Internal_Rela *relocs;
13377
0
  enum elf_nds32_reloc_type rtype;
13378
13379
0
  do
13380
0
    {
13381
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13382
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13383
0
            true /* keep_memory  */);
13384
0
      if (relocs == NULL)
13385
0
  break;
13386
13387
      /* Check R_NDS32_RELAX_GROUP.  */
13388
0
      relend = relocs + asec->reloc_count;
13389
0
      for (rel = relocs; rel < relend; rel++)
13390
0
  {
13391
0
    int id;
13392
0
    rtype = ELF32_R_TYPE (rel->r_info);
13393
0
    if (rtype != R_NDS32_RELAX_GROUP)
13394
0
      continue;
13395
13396
0
    id = rel->r_addend;
13397
0
    if (id < min_id)
13398
0
      min_id = id;
13399
0
    else if (id > max_id)
13400
0
      max_id = id;
13401
0
  }
13402
0
    }
13403
0
  while (false);
13404
13405
0
  if (elf_section_data (asec)->relocs != relocs)
13406
0
    free (relocs);
13407
13408
0
  if ((min_id != relax_group_ptr->min_id)
13409
0
      || (max_id != relax_group_ptr->max_id))
13410
0
    {
13411
0
      relax_group_ptr->count = max_id - min_id + 1;
13412
0
      BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13413
0
      relax_group_ptr->min_id = min_id;
13414
0
      BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13415
0
      relax_group_ptr->max_id = max_id;
13416
0
    }
13417
13418
0
  return relax_group_ptr->count;
13419
0
}
13420
13421
/* Reorder RELAX_GROUP ID when command line option '-r' is applied.  */
13422
static struct section_id_list_t *relax_group_section_id_list = NULL;
13423
13424
struct section_id_list_t *
13425
elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13426
0
{
13427
0
  struct section_id_list_t *result = NULL;
13428
0
  struct section_id_list_t *lst = *lst_ptr;
13429
13430
0
  if (NULL == lst)
13431
0
    {
13432
0
      result = (struct section_id_list_t *) calloc
13433
0
  (1, sizeof (struct section_id_list_t));
13434
0
      BFD_ASSERT (result); /* Feed me.  */
13435
0
      result->id = id;
13436
0
      *lst_ptr = result;
13437
0
    }
13438
0
  else
13439
0
    {
13440
0
      struct section_id_list_t *cur = lst;
13441
0
      struct section_id_list_t *prv = NULL;
13442
0
      struct section_id_list_t *sec = NULL;
13443
13444
0
      while (cur)
13445
0
  {
13446
0
    if (cur->id < id)
13447
0
      {
13448
0
        prv = cur;
13449
0
        cur = cur->next;
13450
0
        continue;
13451
0
      }
13452
13453
0
    if (cur->id > id)
13454
0
      {
13455
0
        cur = NULL; /* To insert after prv.  */
13456
0
        sec = cur;  /* In case prv == NULL.  */
13457
0
      }
13458
13459
0
    break;
13460
0
  }
13461
13462
0
      if (NULL == cur)
13463
0
  {
13464
    /* Insert after prv.  */
13465
0
    result = (struct section_id_list_t *) calloc
13466
0
      (1, sizeof (struct section_id_list_t));
13467
0
    BFD_ASSERT (result); /* Feed me.  */
13468
0
    result->id = id;
13469
0
    if (NULL != prv)
13470
0
      {
13471
0
        result->next = prv->next;
13472
0
        prv->next = result;
13473
0
      }
13474
0
    else
13475
0
      {
13476
0
        *lst_ptr = result;
13477
0
        result->next = sec;
13478
0
      }
13479
0
  }
13480
0
    }
13481
13482
0
  return result;
13483
0
}
13484
13485
int
13486
elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13487
0
{
13488
0
  static int next_relax_group_bias = 0;
13489
13490
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13491
0
    elf32_nds32_relax_group_ptr (abfd);
13492
13493
0
  bool result = true;
13494
0
  Elf_Internal_Rela *rel;
13495
0
  Elf_Internal_Rela *relend;
13496
0
  Elf_Internal_Rela *relocs = NULL;
13497
0
  enum elf_nds32_reloc_type rtype;
13498
0
  struct section_id_list_t *node = NULL;
13499
13500
0
  do
13501
0
    {
13502
0
      if (0 == relax_group_ptr->count)
13503
0
  break;
13504
13505
      /* Check if this section has been handled.  */
13506
0
      node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13507
0
      if (NULL == node)
13508
0
  break; /* Hit, the section id has handled.  */
13509
13510
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13511
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13512
0
            true /* keep_memory  */);
13513
0
      if (relocs == NULL)
13514
0
  {
13515
0
    BFD_ASSERT (0); /* feed me */
13516
0
    break;
13517
0
  }
13518
13519
      /* Allocate group id bias for this bfd!  */
13520
0
      if (0 == relax_group_ptr->init)
13521
0
  {
13522
0
    relax_group_ptr->bias = next_relax_group_bias;
13523
0
    next_relax_group_bias += relax_group_ptr->count;
13524
0
    relax_group_ptr->init = 1;
13525
0
  }
13526
13527
      /* Reorder relax group groups.  */
13528
0
      relend = relocs + asec->reloc_count;
13529
0
      for (rel = relocs; rel < relend; rel++)
13530
0
  {
13531
0
    rtype = ELF32_R_TYPE(rel->r_info);
13532
0
    if (rtype != R_NDS32_RELAX_GROUP)
13533
0
      continue;
13534
13535
    /* Change it.  */
13536
0
    rel->r_addend += relax_group_ptr->bias;
13537
0
  }
13538
0
    }
13539
0
  while (false);
13540
13541
0
  if (elf_section_data (asec)->relocs != relocs)
13542
0
    free (relocs);
13543
13544
0
  return result;
13545
0
}
13546
13547
int
13548
nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13549
         struct bfd_link_info *lnkinfo)
13550
0
{
13551
0
  bool result = true;
13552
0
  Elf_Internal_Rela *irel;
13553
0
  Elf_Internal_Rela *irelend;
13554
0
  Elf_Internal_Rela *internal_relocs;
13555
0
  unsigned long r_symndx;
13556
0
  enum elf_nds32_reloc_type r_type;
13557
13558
0
  Elf_Internal_Sym *local_syms = NULL;
13559
0
  bfd_byte *contents = NULL;
13560
13561
0
  relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13562
13563
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (inbfd);
13564
0
  struct elf_link_hash_entry **sym_hashes;
13565
0
  sym_hashes = elf_sym_hashes (inbfd);
13566
13567
  /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
13568
0
  if (bfd_link_relocatable (lnkinfo))
13569
0
    {
13570
0
      elf32_nds32_unify_relax_group (inbfd, insec);
13571
0
      return result;
13572
0
    }
13573
13574
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13575
0
  internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13576
0
                 true /* keep_memory  */);
13577
0
  if (internal_relocs == NULL)
13578
0
    goto error_return;
13579
13580
0
  irelend = internal_relocs + insec->reloc_count;
13581
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
13582
0
         irelend, R_NDS32_RELAX_ENTRY);
13583
0
  if (irel == irelend)
13584
0
    goto finish;
13585
13586
  /* Chain/remove groups.  */
13587
0
  for (irel = internal_relocs; irel < irelend; irel++)
13588
0
    {
13589
0
      r_symndx = ELF32_R_SYM (irel->r_info);
13590
0
      r_type = ELF32_R_TYPE (irel->r_info);
13591
0
      if (r_type != R_NDS32_RELAX_GROUP)
13592
0
  continue;
13593
13594
      /* Remove it.  */
13595
0
      irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13596
      /* Chain it now.  */
13597
0
      if (!list_insert (&chain, irel))
13598
0
  goto error_return;
13599
0
    }
13600
13601
  /* Collect group relocations.  */
13602
  /* Presume relocations are sorted.  */
13603
0
  relax_group_list_t *pNext = chain.next;
13604
0
  while (pNext)
13605
0
    {
13606
0
      for (irel = internal_relocs; irel < irelend; irel++)
13607
0
  {
13608
0
    if (irel->r_offset == pNext->relo->r_offset)
13609
0
      {
13610
        /* Ignore Non-TLS relocation types.  */
13611
0
        r_type = ELF32_R_TYPE (irel->r_info);
13612
0
        if ((R_NDS32_TLS_LE_HI20 > r_type)
13613
0
      || (R_NDS32_RELAX_ENTRY == r_type))
13614
0
    continue;
13615
13616
0
        if (!list_insert_sibling (pNext, irel))
13617
0
    goto error_return;
13618
0
      }
13619
0
    else if (irel->r_offset > pNext->relo->r_offset)
13620
0
      {
13621
0
        pNext = pNext->next;
13622
0
        if (!pNext)
13623
0
    break;
13624
13625
0
        bfd_vma current_offset = pNext->relo->r_offset;
13626
0
        if (irel->r_offset > current_offset)
13627
0
    irel = internal_relocs; /* restart from head */
13628
0
        else
13629
0
    --irel; /* Check current irel again.  */
13630
0
        continue;
13631
0
      }
13632
0
    else
13633
0
      {
13634
        /* This shouldn't be reached.  */
13635
0
      }
13636
0
  }
13637
0
      if (pNext)
13638
0
  pNext = pNext->next;
13639
0
    }
13640
13641
#ifdef DUBUG_VERBOSE
13642
  dump_chain(&chain);
13643
#endif
13644
13645
  /* Get symbol table and section content.  */
13646
0
  if (incontents)
13647
0
    contents = incontents;
13648
0
  else if (!nds32_get_section_contents (inbfd, insec, &contents, true)
13649
0
     || !nds32_get_local_syms (inbfd, insec, &local_syms))
13650
0
    goto error_return;
13651
13652
0
  char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13653
13654
  /* Convert TLS model each group if necessary.  */
13655
0
  pNext = chain.next;
13656
13657
0
  int cur_grp_id = -1;
13658
0
  int sethi_rt = -1;
13659
0
  int add_rt = -1;
13660
0
  enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13661
13662
0
  tls_type = org_tls_type = eff_tls_type = 0;
13663
13664
0
  while (pNext)
13665
0
    {
13666
0
      relax_group_list_t *pNextSig = pNext->next_sibling;
13667
0
      while (pNextSig)
13668
0
  {
13669
0
    struct elf_link_hash_entry *h = NULL;
13670
13671
0
    irel = pNextSig->relo;
13672
0
    r_symndx = ELF32_R_SYM(irel->r_info);
13673
0
    r_type = ELF32_R_TYPE(irel->r_info);
13674
13675
0
    if (pNext->id != cur_grp_id)
13676
0
      {
13677
0
        cur_grp_id = pNext->id;
13678
0
        org_tls_type = get_tls_type (r_type, NULL);
13679
0
        if (r_symndx >= symtab_hdr->sh_info)
13680
0
    {
13681
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13682
0
      while (h->root.type == bfd_link_hash_indirect
13683
0
       || h->root.type == bfd_link_hash_warning)
13684
0
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
13685
0
      tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13686
0
    }
13687
0
        else
13688
0
    {
13689
0
      tls_type = local_got_tls_type
13690
0
        ? local_got_tls_type[r_symndx]
13691
0
        : GOT_NORMAL;
13692
0
    }
13693
13694
0
        eff_tls_type = 1 << (fls (tls_type) - 1);
13695
0
        sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13696
0
      }
13697
13698
0
    if (eff_tls_type != org_tls_type)
13699
0
      {
13700
0
        switch (org_tls_type)
13701
0
    {
13702
      /* DESC to IEGP/IE/LE.  */
13703
0
    case GOT_TLS_DESC:
13704
0
      switch (eff_tls_type)
13705
0
        {
13706
0
        case GOT_TLS_IE:
13707
0
          switch (r_type)
13708
0
      {
13709
0
      case R_NDS32_TLS_DESC_HI20:
13710
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13711
0
                  R_NDS32_TLS_IE_HI20);
13712
0
        break;
13713
0
      case R_NDS32_TLS_DESC_LO12:
13714
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13715
0
                  R_NDS32_TLS_IE_LO12);
13716
0
        break;
13717
0
      case R_NDS32_TLS_DESC_ADD:
13718
0
        {
13719
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13720
0
          add_rt = N32_RT5 (insn);
13721
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13722
0
          bfd_putb32 (insn, contents + irel->r_offset);
13723
13724
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13725
0
        }
13726
0
        break;
13727
0
      case R_NDS32_TLS_DESC_FUNC:
13728
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13729
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13730
0
                  R_NDS32_RELAX_REMOVE);
13731
0
        break;
13732
0
      case R_NDS32_TLS_DESC_CALL:
13733
0
        {
13734
0
          uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13735
0
                 REG_TP);
13736
0
          bfd_putb32 (insn, contents + irel->r_offset);
13737
13738
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13739
0
        }
13740
0
        break;
13741
0
      case R_NDS32_LOADSTORE:
13742
0
      case R_NDS32_PTR:
13743
0
      case R_NDS32_PTR_RESOLVED:
13744
0
      case R_NDS32_NONE:
13745
0
      case R_NDS32_LABEL:
13746
0
        break;
13747
0
      default:
13748
0
        BFD_ASSERT(0);
13749
0
        break;
13750
0
      }
13751
0
          break;
13752
0
        case GOT_TLS_IEGP:
13753
0
          switch (r_type)
13754
0
      {
13755
0
      case R_NDS32_TLS_DESC_HI20:
13756
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13757
0
                  R_NDS32_TLS_IEGP_HI20);
13758
0
        break;
13759
0
      case R_NDS32_TLS_DESC_LO12:
13760
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13761
0
                  R_NDS32_TLS_IEGP_LO12);
13762
0
        break;
13763
0
      case R_NDS32_TLS_DESC_ADD:
13764
0
        {
13765
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13766
0
          add_rt = N32_RT5 (insn);
13767
0
          insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13768
0
          bfd_putb32 (insn, contents + irel->r_offset);
13769
13770
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13771
0
        }
13772
0
        break;
13773
0
      case R_NDS32_TLS_DESC_FUNC:
13774
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13775
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13776
0
                  R_NDS32_RELAX_REMOVE);
13777
0
        break;
13778
0
      case R_NDS32_TLS_DESC_CALL:
13779
0
        {
13780
0
          uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13781
0
                 REG_TP);
13782
0
          bfd_putb32 (insn, contents + irel->r_offset);
13783
13784
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13785
0
        }
13786
0
        break;
13787
0
      case R_NDS32_LOADSTORE:
13788
0
      case R_NDS32_PTR:
13789
0
      case R_NDS32_PTR_RESOLVED:
13790
0
      case R_NDS32_NONE:
13791
0
      case R_NDS32_LABEL:
13792
0
        break;
13793
0
      default:
13794
0
        BFD_ASSERT(0);
13795
0
        break;
13796
0
      }
13797
0
          break;
13798
0
        case GOT_TLS_LE:
13799
0
          switch (r_type)
13800
0
      {
13801
0
      case R_NDS32_TLS_DESC_HI20:
13802
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13803
0
        break;
13804
0
      case R_NDS32_TLS_DESC_LO12:
13805
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13806
0
        break;
13807
0
      case R_NDS32_TLS_DESC_ADD:
13808
0
        {
13809
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13810
13811
0
          add_rt = N32_RT5 (insn);
13812
0
          insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13813
0
          bfd_putb32 (insn, contents + irel->r_offset);
13814
13815
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13816
0
        }
13817
0
        break;
13818
0
      case R_NDS32_TLS_DESC_FUNC:
13819
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13820
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13821
0
        break;
13822
0
      case R_NDS32_TLS_DESC_CALL:
13823
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13824
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13825
0
        break;
13826
0
      case R_NDS32_LOADSTORE:
13827
0
      case R_NDS32_PTR:
13828
0
      case R_NDS32_PTR_RESOLVED:
13829
0
      case R_NDS32_NONE:
13830
0
      case R_NDS32_LABEL:
13831
0
        break;
13832
0
      default:
13833
0
        BFD_ASSERT(0);
13834
0
        break;
13835
0
      }
13836
0
          break;
13837
0
        default:
13838
0
          break;
13839
0
        }
13840
0
      break;
13841
      /* IEGP to IE/LE.  */
13842
0
    case GOT_TLS_IEGP:
13843
0
      switch (eff_tls_type)
13844
0
        {
13845
0
        case GOT_TLS_IE:
13846
0
          switch (r_type)
13847
0
      {
13848
0
      case R_NDS32_TLS_IEGP_HI20:
13849
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13850
0
                  R_NDS32_TLS_IE_HI20);
13851
0
        break;
13852
0
      case R_NDS32_TLS_IEGP_LO12:
13853
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13854
0
                  R_NDS32_TLS_IE_LO12);
13855
0
        break;
13856
0
      case R_NDS32_PTR_RESOLVED:
13857
0
        {
13858
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13859
13860
0
          add_rt = N32_RT5 (insn);
13861
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13862
0
          bfd_putb32 (insn, contents + irel->r_offset);
13863
0
        }
13864
0
        break;
13865
0
      case R_NDS32_TLS_IEGP_LW:
13866
0
        break;
13867
0
      case R_NDS32_LOADSTORE:
13868
0
      case R_NDS32_PTR:
13869
0
      case R_NDS32_NONE:
13870
0
      case R_NDS32_LABEL:
13871
0
        break;
13872
0
      default:
13873
0
        BFD_ASSERT(0);
13874
0
        break;
13875
0
      }
13876
0
          break;
13877
0
        case GOT_TLS_LE:
13878
0
          switch (r_type)
13879
0
      {
13880
0
      case R_NDS32_TLS_IEGP_HI20:
13881
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13882
0
        break;
13883
0
      case R_NDS32_TLS_IEGP_LO12:
13884
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13885
0
        break;
13886
0
      case R_NDS32_TLS_IEGP_LW:
13887
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13888
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13889
0
        break;
13890
0
      case R_NDS32_LOADSTORE:
13891
0
      case R_NDS32_PTR:
13892
0
      case R_NDS32_NONE:
13893
0
      case R_NDS32_LABEL:
13894
0
      case R_NDS32_PTR_RESOLVED:
13895
0
        break;
13896
0
      default:
13897
0
        BFD_ASSERT(0);
13898
0
        break;
13899
0
      }
13900
0
          break;
13901
0
        default:
13902
0
          break;
13903
0
        }
13904
0
      break;
13905
      /* IE to LE. */
13906
0
    case GOT_TLS_IE:
13907
0
      switch (eff_tls_type)
13908
0
        {
13909
0
        case GOT_TLS_LE:
13910
0
          switch (r_type)
13911
0
      {
13912
0
      case R_NDS32_TLS_IE_HI20:
13913
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13914
0
        break;
13915
0
      case R_NDS32_TLS_IE_LO12S2:
13916
0
        {
13917
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13918
13919
0
          add_rt = N32_RT5 (insn);
13920
0
          insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13921
0
          bfd_putb32 (insn, contents + irel->r_offset);
13922
13923
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13924
0
        }
13925
0
        break;
13926
0
      case R_NDS32_LOADSTORE:
13927
0
      case R_NDS32_PTR:
13928
0
      case R_NDS32_NONE:
13929
0
      case R_NDS32_LABEL:
13930
0
        break;
13931
0
      default:
13932
0
        BFD_ASSERT(0);
13933
0
        break;
13934
0
      }
13935
0
          break;
13936
0
        default:
13937
0
          break;
13938
0
        }
13939
0
      break;
13940
0
    default:
13941
0
      break;
13942
0
    }
13943
0
      }
13944
0
    pNextSig = pNextSig->next_sibling;
13945
0
  }
13946
13947
0
#if 1
13948
0
      pNext = pNext->next;
13949
#else
13950
      while (pNext)
13951
  {
13952
    if (pNext->id != cur_grp_id)
13953
      break;
13954
    pNext = pNext->next;
13955
  }
13956
#endif
13957
0
    }
13958
13959
0
 finish:
13960
0
  if (incontents)
13961
0
    contents = NULL;
13962
13963
0
  if (elf_section_data (insec)->relocs != internal_relocs)
13964
0
    free (internal_relocs);
13965
13966
0
  if (elf_section_data (insec)->this_hdr.contents != contents)
13967
0
    free (contents);
13968
13969
0
  if (symtab_hdr->contents != (bfd_byte *) local_syms)
13970
0
    free (local_syms);
13971
13972
0
  if (chain.next)
13973
0
    {
13974
0
      pNext = chain.next;
13975
0
      relax_group_list_t *pDel;
13976
0
      while (pNext)
13977
0
  {
13978
0
    pDel = pNext;
13979
0
    pNext = pNext->next;
13980
0
    free (pDel);
13981
0
  }
13982
0
    }
13983
13984
0
  return result;
13985
13986
0
 error_return:
13987
  result = false;
13988
0
  goto finish;
13989
0
}
13990
13991
/* End TLS model conversion.  */
13992
13993
#define ELF_ARCH        bfd_arch_nds32
13994
#define ELF_MACHINE_CODE      EM_NDS32
13995
#define ELF_MAXPAGESIZE       0x1000
13996
#define ELF_TARGET_ID       NDS32_ELF_DATA
13997
13998
#define TARGET_BIG_SYM        nds32_elf32_be_vec
13999
#define TARGET_BIG_NAME       "elf32-nds32be"
14000
#define TARGET_LITTLE_SYM     nds32_elf32_le_vec
14001
#define TARGET_LITTLE_NAME      "elf32-nds32le"
14002
14003
#define elf_info_to_howto     nds32_info_to_howto
14004
#define elf_info_to_howto_rel     nds32_info_to_howto_rel
14005
14006
#define bfd_elf32_bfd_link_hash_table_create  nds32_elf_link_hash_table_create
14007
#define bfd_elf32_bfd_merge_private_bfd_data  nds32_elf_merge_private_bfd_data
14008
#define bfd_elf32_bfd_print_private_bfd_data  nds32_elf_print_private_bfd_data
14009
#define bfd_elf32_bfd_relax_section   nds32_elf_relax_section
14010
#define bfd_elf32_bfd_set_private_flags   nds32_elf_set_private_flags
14011
14012
#define bfd_elf32_mkobject      nds32_elf_mkobject
14013
#define bfd_elf32_new_section_hook    nds32_elf_new_section_hook
14014
#define bfd_elf32_bfd_free_cached_info    nds32_elf_free_cached_info
14015
#define elf_backend_action_discarded    nds32_elf_action_discarded
14016
#define elf_backend_add_symbol_hook   nds32_elf_add_symbol_hook
14017
#define elf_backend_check_relocs    nds32_elf_check_relocs
14018
#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14019
#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14020
#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14021
#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14022
#define elf_backend_late_size_sections    nds32_elf_late_size_sections
14023
#define elf_backend_relocate_section    nds32_elf_relocate_section
14024
#define elf_backend_gc_mark_hook    nds32_elf_gc_mark_hook
14025
#define elf_backend_grok_prstatus   nds32_elf_grok_prstatus
14026
#define elf_backend_grok_psinfo     nds32_elf_grok_psinfo
14027
#define elf_backend_reloc_type_class    nds32_elf_reloc_type_class
14028
#define elf_backend_copy_indirect_symbol  nds32_elf_copy_indirect_symbol
14029
#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14030
#define elf_backend_output_arch_syms    nds32_elf_output_arch_syms
14031
#define elf_backend_object_p      nds32_elf_object_p
14032
#define elf_backend_final_write_processing  nds32_elf_final_write_processing
14033
#define elf_backend_special_sections    nds32_elf_special_sections
14034
#define elf_backend_section_flags   nds32_elf_section_flags
14035
#define bfd_elf32_bfd_get_relocated_section_contents \
14036
        nds32_elf_get_relocated_section_contents
14037
#define bfd_elf32_bfd_is_target_special_symbol  nds32_elf_is_target_special_symbol
14038
#define elf_backend_maybe_function_sym    nds32_elf_maybe_function_sym
14039
14040
#define elf_backend_can_gc_sections   1
14041
#define elf_backend_can_refcount    1
14042
#define elf_backend_want_got_plt    1
14043
#define elf_backend_plt_readonly    1
14044
#define elf_backend_want_plt_sym    0
14045
#define elf_backend_got_header_size   12
14046
#define elf_backend_may_use_rel_p   1
14047
#define elf_backend_default_use_rela_p    1
14048
#define elf_backend_may_use_rela_p    1
14049
#define elf_backend_dtrel_excludes_plt    0
14050
14051
#include "elf32-target.h"
14052
14053
#undef ELF_MAXPAGESIZE
14054
#define ELF_MAXPAGESIZE       0x2000
14055
14056
#undef  TARGET_BIG_SYM
14057
#define TARGET_BIG_SYM        nds32_elf32_linux_be_vec
14058
#undef  TARGET_BIG_NAME
14059
#define TARGET_BIG_NAME       "elf32-nds32be-linux"
14060
#undef  TARGET_LITTLE_SYM
14061
#define TARGET_LITTLE_SYM     nds32_elf32_linux_le_vec
14062
#undef  TARGET_LITTLE_NAME
14063
#define TARGET_LITTLE_NAME      "elf32-nds32le-linux"
14064
#undef  elf32_bed
14065
#define elf32_bed       elf32_nds32_lin_bed
14066
14067
#include "elf32-target.h"