Coverage Report

Created: 2026-04-04 08:16

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