Coverage Report

Created: 2026-09-14 08:07

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
21
#define OCTETS_PER_BYTE(ABFD, SEC) 1
37
38
/* Relocation HOWTO functions.  */
39
static bfd_reloc_status_type nds32_elf_ignore_reloc
40
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41
static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43
static bfd_reloc_status_type nds32_elf_hi20_reloc
44
  (bfd *, arelent *, asymbol *, void *,
45
   asection *, bfd *, char **);
46
static bfd_reloc_status_type nds32_elf_lo12_reloc
47
  (bfd *, arelent *, asymbol *, void *,
48
   asection *, bfd *, char **);
49
static bfd_reloc_status_type nds32_elf_generic_reloc
50
  (bfd *, arelent *, asymbol *, void *,
51
   asection *, bfd *, char **);
52
static bfd_reloc_status_type nds32_elf_sda15_reloc
53
  (bfd *, arelent *, asymbol *, void *,
54
   asection *, bfd *, char **);
55
56
/* Helper functions for HOWTO.  */
57
static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
58
  (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
59
   asection *, bfd_vma, bfd_vma);
60
61
/* Nds32 helper functions.  */
62
static bfd_vma calculate_memory_address
63
  (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
64
static int nds32_get_section_contents (bfd *, asection *,
65
               bfd_byte **, bool);
66
static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67
         Elf_Internal_Sym **);
68
static bool  nds32_relax_fp_as_gp
69
  (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
70
   Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
71
   Elf_Internal_Sym *isymbuf);
72
static bool nds32_fag_remove_unused_fpbase
73
  (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74
   Elf_Internal_Rela *irelend);
75
76
enum
77
{
78
  MACH_V1 = bfd_mach_n1h,
79
  MACH_V2 = bfd_mach_n1h_v2,
80
  MACH_V3 = bfd_mach_n1h_v3,
81
  MACH_V3M = bfd_mach_n1h_v3m
82
};
83
84
#define MIN(a, b) ((a) > (b) ? (b) : (a))
85
#define MAX(a, b) ((a) > (b) ? (a) : (b))
86
87
/* The name of the dynamic interpreter.  This is put in the .interp
88
   section.  */
89
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
90
91
0
#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92
0
          && (flags) & SEC_LOAD \
93
0
          && (flags) & SEC_READONLY)
94
95
/* The nop opcode we use.  */
96
0
#define NDS32_NOP32 0x40000009
97
0
#define NDS32_NOP16 0x9200
98
99
/* The size in bytes of an entry in the procedure linkage table.  */
100
0
#define PLT_ENTRY_SIZE 24
101
#define PLT_HEADER_SIZE 24
102
103
/* The first entry in a procedure linkage table are reserved,
104
   and the initial contents are unimportant (we zero them out).
105
   Subsequent entries look like this.  */
106
0
#define PLT0_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(.got+4)    */
107
0
#define PLT0_ENTRY_WORD1  0x58f78000    /* ori     r15, r25, LO12(.got+4) */
108
0
#define PLT0_ENTRY_WORD2  0x05178000    /* lwi     r17, [r15+0]     */
109
0
#define PLT0_ENTRY_WORD3  0x04f78001    /* lwi     r15, [r15+4]     */
110
0
#define PLT0_ENTRY_WORD4  0x4a003c00    /* jr    r15        */
111
112
/* $ta is change to $r15 (from $r25).  */
113
0
#define PLT0_PIC_ENTRY_WORD0  0x46f00000  /* sethi   r15, HI20(got[1]@GOT)  */
114
0
#define PLT0_PIC_ENTRY_WORD1  0x58f78000  /* ori     r15, r15, LO12(got[1]@GOT) */
115
0
#define PLT0_PIC_ENTRY_WORD2  0x40f7f400  /* add     r15, gp, r15     */
116
0
#define PLT0_PIC_ENTRY_WORD3  0x05178000  /* lwi     r17, [r15+0]     */
117
0
#define PLT0_PIC_ENTRY_WORD4  0x04f78001  /* lwi     r15, [r15+4]     */
118
0
#define PLT0_PIC_ENTRY_WORD5  0x4a003c00  /* jr    r15        */
119
120
0
#define PLT_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(&got[n+3])      */
121
0
#define PLT_ENTRY_WORD1  0x04f78000    /* lwi     r15, r15, LO12(&got[n+3]) */
122
0
#define PLT_ENTRY_WORD2  0x4a003c00    /* jr    r15           */
123
0
#define PLT_ENTRY_WORD3  0x45000000    /* movi    r16, sizeof(RELA) * n     */
124
0
#define PLT_ENTRY_WORD4  0x48000000    /* j    .plt0.         */
125
126
0
#define PLT_PIC_ENTRY_WORD0  0x46f00000    /* sethi  r15, HI20(got[n+3]@GOT)    */
127
0
#define PLT_PIC_ENTRY_WORD1  0x58f78000    /* ori    r15, r15,    LO12(got[n+3]@GOT) */
128
0
#define PLT_PIC_ENTRY_WORD2  0x38febc02    /* lw   r15, [gp+r15]        */
129
0
#define PLT_PIC_ENTRY_WORD3  0x4a003c00    /* jr   r15          */
130
0
#define PLT_PIC_ENTRY_WORD4  0x45000000    /* movi   r16, sizeof(RELA) * n      */
131
0
#define PLT_PIC_ENTRY_WORD5  0x48000000    /* j    .plt0          */
132
133
/* These are macros used to get the relocation accurate value.  */
134
4
#define ACCURATE_8BIT_S1  (0x100)
135
0
#define ACCURATE_U9BIT_S1 (0x400)
136
#define ACCURATE_12BIT_S1 (0x2000)
137
0
#define ACCURATE_14BIT_S1 (0x4000)
138
#define ACCURATE_19BIT    (0x40000)
139
140
/* These are macros used to get the relocation conservative value.  */
141
0
#define CONSERVATIVE_8BIT_S1  (0x100 - 4)
142
0
#define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143
0
#define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144
0
#define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
145
/* These must be more conservative because the address may be in
146
   different segment.  */
147
#define CONSERVATIVE_15BIT  (0x4000 - 0x1000)
148
#define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
149
#define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
150
#define CONSERVATIVE_19BIT  (0x40000 - 0x1000)
151
0
#define CONSERVATIVE_20BIT  (0x80000 - 0x1000)
152
153
/* Size of small data/bss sections, used to calculate SDA_BASE.  */
154
static long got_size = 0;
155
static int is_SDA_BASE_set = 0;
156
157
/* Convert ELF-VER in eflags to string for debugging purpose.  */
158
static const char *const nds32_elfver_strtab[] =
159
{
160
  "ELF-1.2",
161
  "ELF-1.3",
162
  "ELF-1.4",
163
};
164
165
/* The nds32 linker needs to keep track of the number of relocs that it
166
   decides to copy in check_relocs for each symbol.  This is so that
167
   it can discard PC relative relocs if it doesn't need them when
168
   linking with -Bsymbolic.  We store the information in a field
169
   extending the regular ELF linker hash table.  */
170
171
/* This structure keeps track of the number of PC relative relocs we
172
   have copied for a given symbol.  */
173
174
struct elf_nds32_pcrel_relocs_copied
175
{
176
  /* Next section.  */
177
  struct elf_nds32_pcrel_relocs_copied *next;
178
  /* A section in dynobj.  */
179
  asection *section;
180
  /* Number of relocs copied in this section.  */
181
  bfd_size_type count;
182
};
183
184
enum elf_nds32_tls_type
185
{
186
  GOT_UNKNOWN = (0),
187
  GOT_NORMAL = (1 << 0),
188
  GOT_TLS_LE = (1 << 1),
189
  GOT_TLS_IE = (1 << 2),
190
  GOT_TLS_IEGP = (1 << 3),
191
  GOT_TLS_LD = (1 << 4),
192
  GOT_TLS_GD = (1 << 5),
193
  GOT_TLS_DESC = (1 << 6),
194
};
195
196
/* Nds32 ELF linker hash entry.  */
197
198
struct elf_nds32_link_hash_entry
199
{
200
  struct elf_link_hash_entry root;
201
202
  /* For checking relocation type.  */
203
  enum elf_nds32_tls_type tls_type;
204
205
  int offset_to_gp;
206
};
207
208
/* Get the nds32 ELF linker hash table from a link_info structure.  */
209
210
0
#define FP_BASE_NAME "_FP_BASE_"
211
static int check_start_export_sym = 0;
212
213
/* The offset for executable tls relaxation.  */
214
0
#define TP_OFFSET 0x0
215
216
typedef struct
217
{
218
  int min_id;
219
  int max_id;
220
  int count;
221
  int bias;
222
  int init;
223
} elf32_nds32_relax_group_t;
224
225
struct elf_nds32_obj_tdata
226
{
227
  struct elf_obj_tdata root;
228
229
  /* tls_type for each local got entry.  */
230
  char *local_got_tls_type;
231
232
  /* GOTPLT entries for TLS descriptors.  */
233
  bfd_vma *local_tlsdesc_gotent;
234
235
  /* for R_NDS32_RELAX_GROUP handling.  */
236
  elf32_nds32_relax_group_t relax_group;
237
238
  unsigned int hdr_size;
239
  int* offset_to_gp;
240
};
241
242
#define elf_nds32_tdata(bfd) \
243
0
  ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
244
245
#define elf32_nds32_local_got_tls_type(bfd) \
246
0
  (elf_nds32_tdata (bfd)->local_got_tls_type)
247
248
#define elf32_nds32_local_gp_offset(bfd) \
249
0
  (elf_nds32_tdata (bfd)->offset_to_gp)
250
251
0
#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
252
253
#define elf32_nds32_relax_group_ptr(bfd) \
254
0
  &(elf_nds32_tdata (bfd)->relax_group)
255
256
static bool
257
nds32_elf_mkobject (bfd *abfd)
258
92.7k
{
259
92.7k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata));
260
92.7k
}
261
262
/* Relocations used for relocation.  */
263
/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
264
   initialize array nds32_elf_howto_table in any order. The benefit
265
   is that we can add any new relocations with any numbers and don't
266
   need to fill the gap by lots of EMPTY_HOWTO. */
267
#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
268
  [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
269
270
static reloc_howto_type nds32_elf_howto_table[] =
271
{
272
  /* This reloc does nothing.  */
273
  HOWTO2 (R_NDS32_NONE,   /* type  */
274
   0,     /* rightshift  */
275
   4,     /* size  */
276
   32,      /* bitsize  */
277
   false,     /* pc_relative  */
278
   0,     /* bitpos  */
279
   complain_overflow_bitfield,/* complain_on_overflow  */
280
   bfd_elf_generic_reloc, /* special_function  */
281
   "R_NDS32_NONE",  /* name  */
282
   false,     /* partial_inplace  */
283
   0,     /* src_mask  */
284
   0,     /* dst_mask  */
285
   false),    /* pcrel_offset  */
286
287
  /* A 16 bit absolute relocation.  */
288
  HOWTO2 (R_NDS32_16,   /* type  */
289
   0,     /* rightshift  */
290
   2,     /* size  */
291
   16,      /* bitsize  */
292
   false,     /* pc_relative  */
293
   0,     /* bitpos  */
294
   complain_overflow_bitfield,/* complain_on_overflow  */
295
   nds32_elf_generic_reloc,/* special_function  */
296
   "R_NDS32_16",    /* name  */
297
   false,     /* partial_inplace  */
298
   0xffff,    /* src_mask  */
299
   0xffff,    /* dst_mask  */
300
   false),    /* pcrel_offset  */
301
302
  /* A 32 bit absolute relocation.  */
303
  HOWTO2 (R_NDS32_32,   /* type  */
304
   0,     /* rightshift  */
305
   4,     /* size  */
306
   32,      /* bitsize  */
307
   false,     /* pc_relative  */
308
   0,     /* bitpos  */
309
   complain_overflow_bitfield,/* complain_on_overflow  */
310
   nds32_elf_generic_reloc,/* special_function  */
311
   "R_NDS32_32",    /* name  */
312
   false,     /* partial_inplace  */
313
   0xffffffff,    /* src_mask  */
314
   0xffffffff,    /* dst_mask  */
315
   false),    /* pcrel_offset  */
316
317
  /* A 20 bit address.  */
318
  HOWTO2 (R_NDS32_20,   /* type  */
319
   0,     /* rightshift  */
320
   4,     /* size  */
321
   20,      /* bitsize  */
322
   false,     /* pc_relative  */
323
   0,     /* bitpos  */
324
   complain_overflow_unsigned,/* complain_on_overflow  */
325
   nds32_elf_generic_reloc,/* special_function  */
326
   "R_NDS32_20",    /* name  */
327
   false,     /* partial_inplace  */
328
   0xfffff,   /* src_mask  */
329
   0xfffff,   /* dst_mask  */
330
   false),    /* pcrel_offset  */
331
332
  /* An PC Relative 9-bit relocation, shifted by 2.
333
     This reloc is complicated because relocations are relative to pc & -4.
334
     i.e. branches in the right insn slot use the address of the left insn
335
     slot for pc.  */
336
  /* It's not clear whether this should have partial_inplace set or not.
337
     Branch relaxing in the assembler can store the addend in the insn,
338
     and if bfd_install_relocation gets called the addend may get added
339
     again.  */
340
  HOWTO2 (R_NDS32_9_PCREL,  /* type  */
341
   1,     /* rightshift  */
342
   2,     /* size  */
343
   8,     /* bitsize  */
344
   true,      /* pc_relative  */
345
   0,     /* bitpos  */
346
   complain_overflow_signed,/* complain_on_overflow  */
347
   nds32_elf_9_pcrel_reloc,/* special_function  */
348
   "R_NDS32_9_PCREL", /* name  */
349
   false,     /* partial_inplace  */
350
   0xff,      /* src_mask  */
351
   0xff,      /* dst_mask  */
352
   true),     /* pcrel_offset  */
353
354
  /* A relative 15 bit relocation, right shifted by 1.  */
355
  HOWTO2 (R_NDS32_15_PCREL, /* type  */
356
   1,     /* rightshift  */
357
   4,     /* size  */
358
   14,      /* bitsize  */
359
   true,      /* pc_relative  */
360
   0,     /* bitpos  */
361
   complain_overflow_signed,/* complain_on_overflow  */
362
   bfd_elf_generic_reloc, /* special_function  */
363
   "R_NDS32_15_PCREL",  /* name  */
364
   false,     /* partial_inplace  */
365
   0x3fff,    /* src_mask  */
366
   0x3fff,    /* dst_mask  */
367
   true),     /* pcrel_offset  */
368
369
  /* A relative 17 bit relocation, right shifted by 1.  */
370
  HOWTO2 (R_NDS32_17_PCREL, /* type  */
371
   1,     /* rightshift  */
372
   4,     /* size  */
373
   16,      /* bitsize  */
374
   true,      /* pc_relative  */
375
   0,     /* bitpos  */
376
   complain_overflow_signed,/* complain_on_overflow  */
377
   bfd_elf_generic_reloc, /* special_function  */
378
   "R_NDS32_17_PCREL",  /* name  */
379
   false,     /* partial_inplace  */
380
   0xffff,    /* src_mask  */
381
   0xffff,    /* dst_mask  */
382
   true),     /* pcrel_offset  */
383
384
  /* A relative 25 bit relocation, right shifted by 1.  */
385
  /* It's not clear whether this should have partial_inplace set or not.
386
     Branch relaxing in the assembler can store the addend in the insn,
387
     and if bfd_install_relocation gets called the addend may get added
388
     again.  */
389
  HOWTO2 (R_NDS32_25_PCREL, /* type  */
390
   1,     /* rightshift  */
391
   4,     /* size  */
392
   24,      /* bitsize  */
393
   true,      /* pc_relative  */
394
   0,     /* bitpos  */
395
   complain_overflow_signed,/* complain_on_overflow  */
396
   bfd_elf_generic_reloc, /* special_function  */
397
   "R_NDS32_25_PCREL",  /* name  */
398
   false,     /* partial_inplace  */
399
   0xffffff,    /* src_mask  */
400
   0xffffff,    /* dst_mask  */
401
   true),     /* pcrel_offset  */
402
403
  /* High 20 bits of address when lower 12 is or'd in.  */
404
  HOWTO2 (R_NDS32_HI20,   /* type  */
405
   12,      /* rightshift  */
406
   4,     /* size  */
407
   20,      /* bitsize  */
408
   false,     /* pc_relative  */
409
   0,     /* bitpos  */
410
   complain_overflow_dont,/* complain_on_overflow  */
411
   nds32_elf_hi20_reloc,  /* special_function  */
412
   "R_NDS32_HI20",  /* name  */
413
   false,     /* partial_inplace  */
414
   0x000fffff,    /* src_mask  */
415
   0x000fffff,    /* dst_mask  */
416
   false),    /* pcrel_offset  */
417
418
  /* Lower 12 bits of address.  */
419
  HOWTO2 (R_NDS32_LO12S3, /* type  */
420
   3,     /* rightshift  */
421
   4,     /* size  */
422
   9,     /* bitsize  */
423
   false,     /* pc_relative  */
424
   0,     /* bitpos  */
425
   complain_overflow_dont,/* complain_on_overflow  */
426
   nds32_elf_lo12_reloc,  /* special_function  */
427
   "R_NDS32_LO12S3",  /* name  */
428
   false,     /* partial_inplace  */
429
   0x000001ff,    /* src_mask  */
430
   0x000001ff,    /* dst_mask  */
431
   false),    /* pcrel_offset  */
432
433
  /* Lower 12 bits of address.  */
434
  HOWTO2 (R_NDS32_LO12S2, /* type  */
435
   2,     /* rightshift  */
436
   4,     /* size  */
437
   10,      /* bitsize  */
438
   false,     /* pc_relative  */
439
   0,     /* bitpos  */
440
   complain_overflow_dont,/* complain_on_overflow  */
441
   nds32_elf_lo12_reloc,  /* special_function  */
442
   "R_NDS32_LO12S2",  /* name  */
443
   false,     /* partial_inplace  */
444
   0x000003ff,    /* src_mask  */
445
   0x000003ff,    /* dst_mask  */
446
   false),    /* pcrel_offset  */
447
448
  /* Lower 12 bits of address.  */
449
  HOWTO2 (R_NDS32_LO12S1, /* type  */
450
   1,     /* rightshift  */
451
   4,     /* size  */
452
   11,      /* bitsize  */
453
   false,     /* pc_relative  */
454
   0,     /* bitpos  */
455
   complain_overflow_dont,/* complain_on_overflow  */
456
   nds32_elf_lo12_reloc,  /* special_function  */
457
   "R_NDS32_LO12S1",  /* name  */
458
   false,     /* partial_inplace  */
459
   0x000007ff,    /* src_mask  */
460
   0x000007ff,    /* dst_mask  */
461
   false),    /* pcrel_offset  */
462
463
  /* Lower 12 bits of address.  */
464
  HOWTO2 (R_NDS32_LO12S0, /* type  */
465
   0,     /* rightshift  */
466
   4,     /* size  */
467
   12,      /* bitsize  */
468
   false,     /* pc_relative  */
469
   0,     /* bitpos  */
470
   complain_overflow_dont,/* complain_on_overflow  */
471
   nds32_elf_lo12_reloc,  /* special_function  */
472
   "R_NDS32_LO12S0",  /* name  */
473
   false,     /* partial_inplace  */
474
   0x00000fff,    /* src_mask  */
475
   0x00000fff,    /* dst_mask  */
476
   false),    /* pcrel_offset  */
477
478
  /* Small data area 15 bits offset.  */
479
  HOWTO2 (R_NDS32_SDA15S3,  /* type  */
480
   3,     /* rightshift  */
481
   4,     /* size  */
482
   15,      /* bitsize  */
483
   false,     /* pc_relative  */
484
   0,     /* bitpos  */
485
   complain_overflow_signed,/* complain_on_overflow  */
486
   nds32_elf_sda15_reloc, /* special_function  */
487
   "R_NDS32_SDA15S3", /* name  */
488
   false,     /* partial_inplace  */
489
   0x00007fff,    /* src_mask  */
490
   0x00007fff,    /* dst_mask  */
491
   false),    /* pcrel_offset  */
492
493
  /* Small data area 15 bits offset.  */
494
  HOWTO2 (R_NDS32_SDA15S2,  /* type  */
495
   2,     /* rightshift  */
496
   4,     /* size  */
497
   15,      /* bitsize  */
498
   false,     /* pc_relative  */
499
   0,     /* bitpos  */
500
   complain_overflow_signed,/* complain_on_overflow  */
501
   nds32_elf_sda15_reloc, /* special_function  */
502
   "R_NDS32_SDA15S2", /* name  */
503
   false,     /* partial_inplace  */
504
   0x00007fff,    /* src_mask  */
505
   0x00007fff,    /* dst_mask  */
506
   false),    /* pcrel_offset  */
507
508
  /* Small data area 15 bits offset.  */
509
  HOWTO2 (R_NDS32_SDA15S1,  /* type  */
510
   1,     /* rightshift  */
511
   4,     /* size  */
512
   15,      /* bitsize  */
513
   false,     /* pc_relative  */
514
   0,     /* bitpos  */
515
   complain_overflow_signed,/* complain_on_overflow  */
516
   nds32_elf_sda15_reloc, /* special_function  */
517
   "R_NDS32_SDA15S1", /* name  */
518
   false,     /* partial_inplace  */
519
   0x00007fff,    /* src_mask  */
520
   0x00007fff,    /* dst_mask  */
521
   false),    /* pcrel_offset  */
522
523
  /* Small data area 15 bits offset.  */
524
  HOWTO2 (R_NDS32_SDA15S0,  /* type  */
525
   0,     /* rightshift  */
526
   4,     /* size  */
527
   15,      /* bitsize  */
528
   false,     /* pc_relative  */
529
   0,     /* bitpos  */
530
   complain_overflow_signed,/* complain_on_overflow  */
531
   nds32_elf_sda15_reloc, /* special_function  */
532
   "R_NDS32_SDA15S0", /* name  */
533
   false,     /* partial_inplace  */
534
   0x00007fff,    /* src_mask  */
535
   0x00007fff,    /* dst_mask  */
536
   false),    /* pcrel_offset  */
537
538
  /* GNU extension to record C++ vtable hierarchy  */
539
  HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type  */
540
   0,     /* rightshift  */
541
   4,     /* size  */
542
   0,     /* bitsize  */
543
   false,     /* pc_relative  */
544
   0,     /* bitpos  */
545
   complain_overflow_dont,/* complain_on_overflow  */
546
   NULL,      /* special_function  */
547
   "R_NDS32_GNU_VTINHERIT",/* name  */
548
   false,     /* partial_inplace  */
549
   0,     /* src_mask  */
550
   0,     /* dst_mask  */
551
   false),    /* pcrel_offset  */
552
553
  /* GNU extension to record C++ vtable member usage  */
554
  HOWTO2 (R_NDS32_GNU_VTENTRY,  /* type  */
555
   0,     /* rightshift  */
556
   4,     /* size  */
557
   0,     /* bitsize  */
558
   false,     /* pc_relative  */
559
   0,     /* bitpos  */
560
   complain_overflow_dont,/* complain_on_overflow  */
561
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
562
   "R_NDS32_GNU_VTENTRY", /* name  */
563
   false,     /* partial_inplace  */
564
   0,     /* src_mask  */
565
   0,     /* dst_mask  */
566
   false),    /* pcrel_offset  */
567
568
  /* A 16 bit absolute relocation.  */
569
  HOWTO2 (R_NDS32_16_RELA,  /* type  */
570
   0,     /* rightshift  */
571
   2,     /* size  */
572
   16,      /* bitsize  */
573
   false,     /* pc_relative  */
574
   0,     /* bitpos  */
575
   complain_overflow_bitfield,/* complain_on_overflow  */
576
   bfd_elf_generic_reloc, /* special_function  */
577
   "R_NDS32_16_RELA", /* name  */
578
   false,     /* partial_inplace  */
579
   0xffff,    /* src_mask  */
580
   0xffff,    /* dst_mask  */
581
   false),    /* pcrel_offset  */
582
583
  /* A 32 bit absolute relocation.  */
584
  HOWTO2 (R_NDS32_32_RELA,  /* type  */
585
   0,     /* rightshift  */
586
   4,     /* size  */
587
   32,      /* bitsize  */
588
   false,     /* pc_relative  */
589
   0,     /* bitpos  */
590
   complain_overflow_bitfield,/* complain_on_overflow  */
591
   bfd_elf_generic_reloc, /* special_function  */
592
   "R_NDS32_32_RELA", /* name  */
593
   false,     /* partial_inplace  */
594
   0xffffffff,    /* src_mask  */
595
   0xffffffff,    /* dst_mask  */
596
   false),    /* pcrel_offset  */
597
598
  /* A 20 bit address.  */
599
  HOWTO2 (R_NDS32_20_RELA,  /* type  */
600
   0,     /* rightshift  */
601
   4,     /* size  */
602
   20,      /* bitsize  */
603
   false,     /* pc_relative  */
604
   0,     /* bitpos  */
605
   complain_overflow_signed,/* complain_on_overflow  */
606
   bfd_elf_generic_reloc, /* special_function  */
607
   "R_NDS32_20_RELA", /* name  */
608
   false,     /* partial_inplace  */
609
   0xfffff,   /* src_mask  */
610
   0xfffff,   /* dst_mask  */
611
   false),    /* pcrel_offset  */
612
613
  HOWTO2 (R_NDS32_9_PCREL_RELA, /* type  */
614
   1,     /* rightshift  */
615
   2,     /* size  */
616
   8,     /* bitsize  */
617
   true,      /* pc_relative  */
618
   0,     /* bitpos  */
619
   complain_overflow_signed,/* complain_on_overflow  */
620
   bfd_elf_generic_reloc, /* special_function  */
621
   "R_NDS32_9_PCREL_RELA",/* name  */
622
   false,     /* partial_inplace  */
623
   0xff,      /* src_mask  */
624
   0xff,      /* dst_mask  */
625
   true),     /* pcrel_offset  */
626
627
  /* A relative 15 bit relocation, right shifted by 1.  */
628
  HOWTO2 (R_NDS32_15_PCREL_RELA,/* type  */
629
   1,     /* rightshift  */
630
   4,     /* size  */
631
   14,      /* bitsize  */
632
   true,      /* pc_relative  */
633
   0,     /* bitpos  */
634
   complain_overflow_signed,/* complain_on_overflow  */
635
   bfd_elf_generic_reloc, /* special_function  */
636
   "R_NDS32_15_PCREL_RELA",/* name  */
637
   false,     /* partial_inplace  */
638
   0x3fff,    /* src_mask  */
639
   0x3fff,    /* dst_mask  */
640
   true),     /* pcrel_offset  */
641
642
  /* A relative 17 bit relocation, right shifted by 1.  */
643
  HOWTO2 (R_NDS32_17_PCREL_RELA,/* type  */
644
   1,     /* rightshift  */
645
   4,     /* size  */
646
   16,      /* bitsize  */
647
   true,      /* pc_relative  */
648
   0,     /* bitpos  */
649
   complain_overflow_signed,/* complain_on_overflow  */
650
   bfd_elf_generic_reloc, /* special_function  */
651
   "R_NDS32_17_PCREL_RELA",/* name  */
652
   false,     /* partial_inplace  */
653
   0xffff,    /* src_mask  */
654
   0xffff,    /* dst_mask  */
655
   true),     /* pcrel_offset  */
656
657
  /* A relative 25 bit relocation, right shifted by 2.  */
658
  HOWTO2 (R_NDS32_25_PCREL_RELA,/* type  */
659
   1,     /* rightshift  */
660
   4,     /* size  */
661
   24,      /* bitsize  */
662
   true,      /* pc_relative  */
663
   0,     /* bitpos  */
664
   complain_overflow_signed,/* complain_on_overflow  */
665
   bfd_elf_generic_reloc, /* special_function  */
666
   "R_NDS32_25_PCREL_RELA",/* name  */
667
   false,     /* partial_inplace  */
668
   0xffffff,    /* src_mask  */
669
   0xffffff,    /* dst_mask  */
670
   true),     /* pcrel_offset  */
671
672
  /* High 20 bits of address when lower 16 is or'd in.  */
673
  HOWTO2 (R_NDS32_HI20_RELA,  /* type  */
674
   12,      /* rightshift  */
675
   4,     /* size  */
676
   20,      /* bitsize  */
677
   false,     /* pc_relative  */
678
   0,     /* bitpos  */
679
   complain_overflow_dont,/* complain_on_overflow  */
680
   bfd_elf_generic_reloc, /* special_function  */
681
   "R_NDS32_HI20_RELA", /* name  */
682
   false,     /* partial_inplace  */
683
   0x000fffff,    /* src_mask  */
684
   0x000fffff,    /* dst_mask  */
685
   false),    /* pcrel_offset  */
686
687
  /* Lower 12 bits of address.  */
688
  HOWTO2 (R_NDS32_LO12S3_RELA,  /* type  */
689
   3,     /* rightshift  */
690
   4,     /* size  */
691
   9,     /* bitsize  */
692
   false,     /* pc_relative  */
693
   0,     /* bitpos  */
694
   complain_overflow_dont,/* complain_on_overflow  */
695
   bfd_elf_generic_reloc, /* special_function  */
696
   "R_NDS32_LO12S3_RELA", /* name  */
697
   false,     /* partial_inplace  */
698
   0x000001ff,    /* src_mask  */
699
   0x000001ff,    /* dst_mask  */
700
   false),    /* pcrel_offset  */
701
702
  /* Lower 12 bits of address.  */
703
  HOWTO2 (R_NDS32_LO12S2_RELA,  /* type  */
704
   2,     /* rightshift  */
705
   4,     /* size  */
706
   10,      /* bitsize  */
707
   false,     /* pc_relative  */
708
   0,     /* bitpos  */
709
   complain_overflow_dont,/* complain_on_overflow  */
710
   bfd_elf_generic_reloc, /* special_function  */
711
   "R_NDS32_LO12S2_RELA", /* name  */
712
   false,     /* partial_inplace  */
713
   0x000003ff,    /* src_mask  */
714
   0x000003ff,    /* dst_mask  */
715
   false),    /* pcrel_offset  */
716
717
  /* Lower 12 bits of address.  */
718
  HOWTO2 (R_NDS32_LO12S1_RELA,  /* type  */
719
   1,     /* rightshift  */
720
   4,     /* size  */
721
   11,      /* bitsize  */
722
   false,     /* pc_relative  */
723
   0,     /* bitpos  */
724
   complain_overflow_dont,/* complain_on_overflow  */
725
   bfd_elf_generic_reloc, /* special_function  */
726
   "R_NDS32_LO12S1_RELA", /* name  */
727
   false,     /* partial_inplace  */
728
   0x000007ff,    /* src_mask  */
729
   0x000007ff,    /* dst_mask  */
730
   false),    /* pcrel_offset  */
731
732
  /* Lower 12 bits of address.  */
733
  HOWTO2 (R_NDS32_LO12S0_RELA,  /* type  */
734
   0,     /* rightshift  */
735
   4,     /* size  */
736
   12,      /* bitsize  */
737
   false,     /* pc_relative  */
738
   0,     /* bitpos  */
739
   complain_overflow_dont,/* complain_on_overflow  */
740
   bfd_elf_generic_reloc, /* special_function  */
741
   "R_NDS32_LO12S0_RELA", /* name  */
742
   false,     /* partial_inplace  */
743
   0x00000fff,    /* src_mask  */
744
   0x00000fff,    /* dst_mask  */
745
   false),    /* pcrel_offset  */
746
747
  /* Small data area 15 bits offset.  */
748
  HOWTO2 (R_NDS32_SDA15S3_RELA, /* type  */
749
   3,     /* rightshift  */
750
   4,     /* size  */
751
   15,      /* bitsize  */
752
   false,     /* pc_relative  */
753
   0,     /* bitpos  */
754
   complain_overflow_signed,/* complain_on_overflow  */
755
   bfd_elf_generic_reloc, /* special_function  */
756
   "R_NDS32_SDA15S3_RELA",/* name  */
757
   false,     /* partial_inplace  */
758
   0x00007fff,    /* src_mask  */
759
   0x00007fff,    /* dst_mask  */
760
   false),    /* pcrel_offset  */
761
762
  /* Small data area 15 bits offset.  */
763
  HOWTO2 (R_NDS32_SDA15S2_RELA, /* type  */
764
   2,     /* rightshift  */
765
   4,     /* size  */
766
   15,      /* bitsize  */
767
   false,     /* pc_relative  */
768
   0,     /* bitpos  */
769
   complain_overflow_signed,/* complain_on_overflow  */
770
   bfd_elf_generic_reloc, /* special_function  */
771
   "R_NDS32_SDA15S2_RELA",/* name  */
772
   false,     /* partial_inplace  */
773
   0x00007fff,    /* src_mask  */
774
   0x00007fff,    /* dst_mask  */
775
   false),    /* pcrel_offset  */
776
777
  HOWTO2 (R_NDS32_SDA15S1_RELA, /* type  */
778
   1,     /* rightshift  */
779
   4,     /* size  */
780
   15,      /* bitsize  */
781
   false,     /* pc_relative  */
782
   0,     /* bitpos  */
783
   complain_overflow_signed,/* complain_on_overflow  */
784
   bfd_elf_generic_reloc, /* special_function  */
785
   "R_NDS32_SDA15S1_RELA",/* name  */
786
   false,     /* partial_inplace  */
787
   0x00007fff,    /* src_mask  */
788
   0x00007fff,    /* dst_mask  */
789
   false),    /* pcrel_offset  */
790
791
  HOWTO2 (R_NDS32_SDA15S0_RELA, /* type  */
792
   0,     /* rightshift  */
793
   4,     /* size  */
794
   15,      /* bitsize  */
795
   false,     /* pc_relative  */
796
   0,     /* bitpos  */
797
   complain_overflow_signed,/* complain_on_overflow  */
798
   bfd_elf_generic_reloc, /* special_function  */
799
   "R_NDS32_SDA15S0_RELA",/* name  */
800
   false,     /* partial_inplace  */
801
   0x00007fff,    /* src_mask  */
802
   0x00007fff,    /* dst_mask  */
803
   false),    /* pcrel_offset  */
804
805
  /* GNU extension to record C++ vtable hierarchy  */
806
  HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type  */
807
   0,     /* rightshift  */
808
   4,     /* size  */
809
   0,     /* bitsize  */
810
   false,     /* pc_relative  */
811
   0,     /* bitpos  */
812
   complain_overflow_dont,/* complain_on_overflow  */
813
   NULL,      /* special_function  */
814
   "R_NDS32_RELA_GNU_VTINHERIT",/* name  */
815
   false,     /* partial_inplace  */
816
   0,     /* src_mask  */
817
   0,     /* dst_mask  */
818
   false),    /* pcrel_offset  */
819
820
  /* GNU extension to record C++ vtable member usage  */
821
  HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type  */
822
   0,     /* rightshift  */
823
   4,     /* size  */
824
   0,     /* bitsize  */
825
   false,     /* pc_relative  */
826
   0,     /* bitpos  */
827
   complain_overflow_dont,/* complain_on_overflow  */
828
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
829
   "R_NDS32_RELA_GNU_VTENTRY",/* name  */
830
   false,     /* partial_inplace  */
831
   0,     /* src_mask  */
832
   0,     /* dst_mask  */
833
   false),    /* pcrel_offset  */
834
835
  /* Like R_NDS32_20, but referring to the GOT table entry for
836
     the symbol.  */
837
  HOWTO2 (R_NDS32_GOT20,  /* type  */
838
   0,     /* rightshift  */
839
   4,     /* size  */
840
   20,      /* bitsize  */
841
   false,     /* pc_relative  */
842
   0,     /* bitpos  */
843
   complain_overflow_signed,/* complain_on_overflow  */
844
   bfd_elf_generic_reloc, /* special_function  */
845
   "R_NDS32_GOT20", /* name  */
846
   false,     /* partial_inplace  */
847
   0xfffff,   /* src_mask  */
848
   0xfffff,   /* dst_mask  */
849
   false),    /* pcrel_offset  */
850
851
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
852
     entry for the symbol.  */
853
  HOWTO2 (R_NDS32_25_PLTREL,  /* type  */
854
   1,     /* rightshift  */
855
   4,     /* size  */
856
   24,      /* bitsize  */
857
   true,      /* pc_relative  */
858
   0,     /* bitpos  */
859
   complain_overflow_signed,/* complain_on_overflow  */
860
   bfd_elf_generic_reloc, /* special_function  */
861
   "R_NDS32_25_PLTREL", /* name  */
862
   false,     /* partial_inplace  */
863
   0xffffff,    /* src_mask  */
864
   0xffffff,    /* dst_mask  */
865
   true),     /* pcrel_offset  */
866
867
  /* This is used only by the dynamic linker.  The symbol should exist
868
     both in the object being run and in some shared library.  The
869
     dynamic linker copies the data addressed by the symbol from the
870
     shared library into the object, because the object being
871
     run has to have the data at some particular address.  */
872
  HOWTO2 (R_NDS32_COPY,   /* type  */
873
   0,     /* rightshift  */
874
   4,     /* size  */
875
   32,      /* bitsize  */
876
   false,     /* pc_relative  */
877
   0,     /* bitpos  */
878
   complain_overflow_bitfield,/* complain_on_overflow  */
879
   bfd_elf_generic_reloc, /* special_function  */
880
   "R_NDS32_COPY",  /* name  */
881
   false,     /* partial_inplace  */
882
   0xffffffff,    /* src_mask  */
883
   0xffffffff,    /* dst_mask  */
884
   false),    /* pcrel_offset  */
885
886
  /* Like R_NDS32_20, but used when setting global offset table
887
     entries.  */
888
  HOWTO2 (R_NDS32_GLOB_DAT, /* type  */
889
   0,     /* rightshift  */
890
   4,     /* size  */
891
   32,      /* bitsize  */
892
   false,     /* pc_relative  */
893
   0,     /* bitpos  */
894
   complain_overflow_bitfield,/* complain_on_overflow  */
895
   bfd_elf_generic_reloc, /* special_function  */
896
   "R_NDS32_GLOB_DAT",  /* name  */
897
   false,     /* partial_inplace  */
898
   0xffffffff,    /* src_mask  */
899
   0xffffffff,    /* dst_mask  */
900
   false),    /* pcrel_offset  */
901
902
  /* Marks a procedure linkage table entry for a symbol.  */
903
  HOWTO2 (R_NDS32_JMP_SLOT, /* type  */
904
   0,     /* rightshift  */
905
   4,     /* size  */
906
   32,      /* bitsize  */
907
   false,     /* pc_relative  */
908
   0,     /* bitpos  */
909
   complain_overflow_bitfield,/* complain_on_overflow  */
910
   bfd_elf_generic_reloc, /* special_function  */
911
   "R_NDS32_JMP_SLOT",  /* name  */
912
   false,     /* partial_inplace  */
913
   0xffffffff,    /* src_mask  */
914
   0xffffffff,    /* dst_mask  */
915
   false),    /* pcrel_offset  */
916
917
  /* Used only by the dynamic linker.  When the object is run, this
918
     longword is set to the load address of the object, plus the
919
     addend.  */
920
  HOWTO2 (R_NDS32_RELATIVE, /* type  */
921
   0,     /* rightshift  */
922
   4,     /* size  */
923
   32,      /* bitsize  */
924
   false,     /* pc_relative  */
925
   0,     /* bitpos  */
926
   complain_overflow_bitfield,/* complain_on_overflow  */
927
   bfd_elf_generic_reloc, /* special_function  */
928
   "R_NDS32_RELATIVE",  /* name  */
929
   false,     /* partial_inplace  */
930
   0xffffffff,    /* src_mask  */
931
   0xffffffff,    /* dst_mask  */
932
   false),    /* pcrel_offset  */
933
934
  HOWTO2 (R_NDS32_GOTOFF, /* type  */
935
   0,     /* rightshift  */
936
   4,     /* size  */
937
   20,      /* bitsize  */
938
   false,     /* pc_relative  */
939
   0,     /* bitpos  */
940
   complain_overflow_signed,/* complain_on_overflow  */
941
   bfd_elf_generic_reloc, /* special_function  */
942
   "R_NDS32_GOTOFF",  /* name  */
943
   false,     /* partial_inplace  */
944
   0xfffff,   /* src_mask  */
945
   0xfffff,   /* dst_mask  */
946
   false),    /* pcrel_offset  */
947
948
  /* An PC Relative 20-bit relocation used when setting PIC offset
949
     table register.  */
950
  HOWTO2 (R_NDS32_GOTPC20,  /* type  */
951
   0,     /* rightshift  */
952
   4,     /* size  */
953
   20,      /* bitsize  */
954
   true,      /* pc_relative  */
955
   0,     /* bitpos  */
956
   complain_overflow_signed,/* complain_on_overflow  */
957
   bfd_elf_generic_reloc, /* special_function  */
958
   "R_NDS32_GOTPC20", /* name  */
959
   false,     /* partial_inplace  */
960
   0xfffff,   /* src_mask  */
961
   0xfffff,   /* dst_mask  */
962
   true),     /* pcrel_offset  */
963
964
  /* Like R_NDS32_HI20, but referring to the GOT table entry for
965
     the symbol.  */
966
  HOWTO2 (R_NDS32_GOT_HI20, /* type  */
967
   12,      /* rightshift  */
968
   4,     /* size  */
969
   20,      /* bitsize  */
970
   false,     /* pc_relative  */
971
   0,     /* bitpos  */
972
   complain_overflow_dont,/* complain_on_overflow  */
973
   bfd_elf_generic_reloc, /* special_function  */
974
   "R_NDS32_GOT_HI20",  /* name  */
975
   false,     /* partial_inplace  */
976
   0x000fffff,    /* src_mask  */
977
   0x000fffff,    /* dst_mask  */
978
   false),    /* pcrel_offset  */
979
980
  HOWTO2 (R_NDS32_GOT_LO12, /* type  */
981
   0,     /* rightshift  */
982
   4,     /* size  */
983
   12,      /* bitsize  */
984
   false,     /* pc_relative  */
985
   0,     /* bitpos  */
986
   complain_overflow_dont,/* complain_on_overflow  */
987
   bfd_elf_generic_reloc, /* special_function  */
988
   "R_NDS32_GOT_LO12",  /* name  */
989
   false,     /* partial_inplace  */
990
   0x00000fff,    /* src_mask  */
991
   0x00000fff,    /* dst_mask  */
992
   false),    /* pcrel_offset  */
993
994
  /* An PC Relative relocation used when setting PIC offset table register.
995
     Like R_NDS32_HI20, but referring to the GOT table entry for
996
     the symbol.  */
997
  HOWTO2 (R_NDS32_GOTPC_HI20, /* type  */
998
   12,      /* rightshift  */
999
   4,     /* size  */
1000
   20,      /* bitsize  */
1001
   false,     /* pc_relative  */
1002
   0,     /* bitpos  */
1003
   complain_overflow_dont,/* complain_on_overflow  */
1004
   bfd_elf_generic_reloc, /* special_function  */
1005
   "R_NDS32_GOTPC_HI20",  /* name  */
1006
   false,     /* partial_inplace  */
1007
   0x000fffff,    /* src_mask  */
1008
   0x000fffff,    /* dst_mask  */
1009
   true),     /* pcrel_offset  */
1010
1011
  HOWTO2 (R_NDS32_GOTPC_LO12, /* type  */
1012
   0,     /* rightshift  */
1013
   4,     /* size  */
1014
   12,      /* bitsize  */
1015
   false,     /* pc_relative  */
1016
   0,     /* bitpos  */
1017
   complain_overflow_dont,/* complain_on_overflow  */
1018
   bfd_elf_generic_reloc, /* special_function  */
1019
   "R_NDS32_GOTPC_LO12",  /* name  */
1020
   false,     /* partial_inplace  */
1021
   0x00000fff,    /* src_mask  */
1022
   0x00000fff,    /* dst_mask  */
1023
   true),     /* pcrel_offset  */
1024
1025
  HOWTO2 (R_NDS32_GOTOFF_HI20,  /* type  */
1026
   12,      /* rightshift  */
1027
   4,     /* size  */
1028
   20,      /* bitsize  */
1029
   false,     /* pc_relative  */
1030
   0,     /* bitpos  */
1031
   complain_overflow_dont,/* complain_on_overflow  */
1032
   bfd_elf_generic_reloc, /* special_function  */
1033
   "R_NDS32_GOTOFF_HI20", /* name  */
1034
   false,     /* partial_inplace  */
1035
   0x000fffff,    /* src_mask  */
1036
   0x000fffff,    /* dst_mask  */
1037
   false),    /* pcrel_offset  */
1038
1039
  HOWTO2 (R_NDS32_GOTOFF_LO12,  /* type  */
1040
   0,     /* rightshift  */
1041
   4,     /* size  */
1042
   12,      /* bitsize  */
1043
   false,     /* pc_relative  */
1044
   0,     /* bitpos  */
1045
   complain_overflow_dont,/* complain_on_overflow  */
1046
   bfd_elf_generic_reloc, /* special_function  */
1047
   "R_NDS32_GOTOFF_LO12", /* name  */
1048
   false,     /* partial_inplace  */
1049
   0x00000fff,    /* src_mask  */
1050
   0x00000fff,    /* dst_mask  */
1051
   false),    /* pcrel_offset  */
1052
1053
  /* Alignment hint for relaxable instruction.  This is used with
1054
     R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
1055
     in order to make next label aligned on word boundary.  */
1056
  HOWTO2 (R_NDS32_INSN16, /* type  */
1057
   0,     /* rightshift  */
1058
   4,     /* size  */
1059
   32,      /* bitsize  */
1060
   false,     /* pc_relative  */
1061
   0,     /* bitpos  */
1062
   complain_overflow_dont,/* complain_on_overflow  */
1063
   nds32_elf_ignore_reloc,/* special_function  */
1064
   "R_NDS32_INSN16",  /* name  */
1065
   false,     /* partial_inplace  */
1066
   0x00000fff,    /* src_mask  */
1067
   0x00000fff,    /* dst_mask  */
1068
   false),    /* pcrel_offset  */
1069
1070
  /* Alignment hint for label.  */
1071
  HOWTO2 (R_NDS32_LABEL,  /* type  */
1072
   0,     /* rightshift  */
1073
   4,     /* size  */
1074
   32,      /* bitsize  */
1075
   false,     /* pc_relative  */
1076
   0,     /* bitpos  */
1077
   complain_overflow_dont,/* complain_on_overflow  */
1078
   nds32_elf_ignore_reloc,/* special_function  */
1079
   "R_NDS32_LABEL", /* name  */
1080
   false,     /* partial_inplace  */
1081
   0xffffffff,    /* src_mask  */
1082
   0xffffffff,    /* dst_mask  */
1083
   false),    /* pcrel_offset  */
1084
1085
  /* Relax hint for unconditional call sequence  */
1086
  HOWTO2 (R_NDS32_LONGCALL1,  /* type  */
1087
   0,     /* rightshift  */
1088
   4,     /* size  */
1089
   32,      /* bitsize  */
1090
   false,     /* pc_relative  */
1091
   0,     /* bitpos  */
1092
   complain_overflow_dont,/* complain_on_overflow  */
1093
   nds32_elf_ignore_reloc,/* special_function  */
1094
   "R_NDS32_LONGCALL1", /* name  */
1095
   false,     /* partial_inplace  */
1096
   0xffffffff,    /* src_mask  */
1097
   0xffffffff,    /* dst_mask  */
1098
   false),    /* pcrel_offset  */
1099
1100
  /* Relax hint for conditional call sequence.  */
1101
  HOWTO2 (R_NDS32_LONGCALL2,  /* type  */
1102
   0,     /* rightshift  */
1103
   4,     /* size  */
1104
   32,      /* bitsize  */
1105
   false,     /* pc_relative  */
1106
   0,     /* bitpos  */
1107
   complain_overflow_dont,/* complain_on_overflow  */
1108
   nds32_elf_ignore_reloc,/* special_function  */
1109
   "R_NDS32_LONGCALL2", /* name  */
1110
   false,     /* partial_inplace  */
1111
   0xffffffff,    /* src_mask  */
1112
   0xffffffff,    /* dst_mask  */
1113
   false),    /* pcrel_offset  */
1114
1115
  /* Relax hint for conditional call sequence.  */
1116
  HOWTO2 (R_NDS32_LONGCALL3,  /* type  */
1117
   0,     /* rightshift  */
1118
   4,     /* size  */
1119
   32,      /* bitsize  */
1120
   false,     /* pc_relative  */
1121
   0,     /* bitpos  */
1122
   complain_overflow_dont,/* complain_on_overflow  */
1123
   nds32_elf_ignore_reloc,/* special_function  */
1124
   "R_NDS32_LONGCALL3", /* name  */
1125
   false,     /* partial_inplace  */
1126
   0xffffffff,    /* src_mask  */
1127
   0xffffffff,    /* dst_mask  */
1128
   false),    /* pcrel_offset  */
1129
1130
  /* Relax hint for unconditional branch sequence.  */
1131
  HOWTO2 (R_NDS32_LONGJUMP1,  /* type  */
1132
   0,     /* rightshift  */
1133
   4,     /* size  */
1134
   32,      /* bitsize  */
1135
   false,     /* pc_relative  */
1136
   0,     /* bitpos  */
1137
   complain_overflow_dont,/* complain_on_overflow  */
1138
   nds32_elf_ignore_reloc,/* special_function  */
1139
   "R_NDS32_LONGJUMP1", /* name  */
1140
   false,     /* partial_inplace  */
1141
   0xffffffff,    /* src_mask  */
1142
   0xffffffff,    /* dst_mask  */
1143
   false),    /* pcrel_offset  */
1144
1145
  /* Relax hint for conditional branch sequence.  */
1146
  HOWTO2 (R_NDS32_LONGJUMP2,  /* type  */
1147
   0,     /* rightshift  */
1148
   4,     /* size  */
1149
   32,      /* bitsize  */
1150
   false,     /* pc_relative  */
1151
   0,     /* bitpos  */
1152
   complain_overflow_dont,/* complain_on_overflow  */
1153
   nds32_elf_ignore_reloc,/* special_function  */
1154
   "R_NDS32_LONGJUMP2", /* name  */
1155
   false,     /* partial_inplace  */
1156
   0xffffffff,    /* src_mask  */
1157
   0xffffffff,    /* dst_mask  */
1158
   false),    /* pcrel_offset  */
1159
1160
  /* Relax hint for conditional branch sequence.  */
1161
  HOWTO2 (R_NDS32_LONGJUMP3,  /* type  */
1162
   0,     /* rightshift  */
1163
   4,     /* size  */
1164
   32,      /* bitsize  */
1165
   false,     /* pc_relative  */
1166
   0,     /* bitpos  */
1167
   complain_overflow_dont,/* complain_on_overflow  */
1168
   nds32_elf_ignore_reloc,/* special_function  */
1169
   "R_NDS32_LONGJUMP3", /* name  */
1170
   false,     /* partial_inplace  */
1171
   0xffffffff,    /* src_mask  */
1172
   0xffffffff,    /* dst_mask  */
1173
   false),    /* pcrel_offset  */
1174
1175
  /* Relax hint for load/store sequence.   */
1176
  HOWTO2 (R_NDS32_LOADSTORE,  /* type  */
1177
   0,     /* rightshift  */
1178
   4,     /* size  */
1179
   32,      /* bitsize  */
1180
   false,     /* pc_relative  */
1181
   0,     /* bitpos  */
1182
   complain_overflow_dont,/* complain_on_overflow  */
1183
   nds32_elf_ignore_reloc,/* special_function  */
1184
   "R_NDS32_LOADSTORE", /* name  */
1185
   false,     /* partial_inplace  */
1186
   0xffffffff,    /* src_mask  */
1187
   0xffffffff,    /* dst_mask  */
1188
   false),    /* pcrel_offset  */
1189
1190
  /* Relax hint for load/store sequence.  */
1191
  HOWTO2 (R_NDS32_9_FIXED_RELA, /* type  */
1192
   0,     /* rightshift  */
1193
   2,     /* size  */
1194
   16,      /* bitsize  */
1195
   false,     /* pc_relative  */
1196
   0,     /* bitpos  */
1197
   complain_overflow_dont,/* complain_on_overflow  */
1198
   nds32_elf_ignore_reloc,/* special_function  */
1199
   "R_NDS32_9_FIXED_RELA",/* name  */
1200
   false,     /* partial_inplace  */
1201
   0x000000ff,    /* src_mask  */
1202
   0x000000ff,    /* dst_mask  */
1203
   false),    /* pcrel_offset  */
1204
1205
  /* Relax hint for load/store sequence.  */
1206
  HOWTO2 (R_NDS32_15_FIXED_RELA,/* type  */
1207
   0,     /* rightshift  */
1208
   4,     /* size  */
1209
   32,      /* bitsize  */
1210
   false,     /* pc_relative  */
1211
   0,     /* bitpos  */
1212
   complain_overflow_dont,/* complain_on_overflow  */
1213
   nds32_elf_ignore_reloc,/* special_function  */
1214
   "R_NDS32_15_FIXED_RELA",/* name  */
1215
   false,     /* partial_inplace  */
1216
   0x00003fff,    /* src_mask  */
1217
   0x00003fff,    /* dst_mask  */
1218
   false),    /* pcrel_offset  */
1219
1220
  /* Relax hint for load/store sequence.  */
1221
  HOWTO2 (R_NDS32_17_FIXED_RELA,/* type  */
1222
   0,     /* rightshift  */
1223
   4,     /* size  */
1224
   32,      /* bitsize  */
1225
   false,     /* pc_relative  */
1226
   0,     /* bitpos  */
1227
   complain_overflow_dont,/* complain_on_overflow  */
1228
   nds32_elf_ignore_reloc,/* special_function  */
1229
   "R_NDS32_17_FIXED_RELA",/* name  */
1230
   false,     /* partial_inplace  */
1231
   0x0000ffff,    /* src_mask  */
1232
   0x0000ffff,    /* dst_mask  */
1233
   false),    /* pcrel_offset  */
1234
1235
  /* Relax hint for load/store sequence.  */
1236
  HOWTO2 (R_NDS32_25_FIXED_RELA,/* type  */
1237
   0,     /* rightshift  */
1238
   4,     /* size  */
1239
   32,      /* bitsize  */
1240
   false,     /* pc_relative  */
1241
   0,     /* bitpos  */
1242
   complain_overflow_dont,/* complain_on_overflow  */
1243
   nds32_elf_ignore_reloc,/* special_function  */
1244
   "R_NDS32_25_FIXED_RELA",/* name  */
1245
   false,     /* partial_inplace  */
1246
   0x00ffffff,    /* src_mask  */
1247
   0x00ffffff,    /* dst_mask  */
1248
   false),    /* pcrel_offset  */
1249
1250
  /* High 20 bits of PLT symbol offset relative to PC.  */
1251
  HOWTO2 (R_NDS32_PLTREL_HI20,  /* type  */
1252
   12,      /* rightshift  */
1253
   4,     /* size  */
1254
   20,      /* bitsize  */
1255
   false,     /* pc_relative  */
1256
   0,     /* bitpos  */
1257
   complain_overflow_dont,/* complain_on_overflow  */
1258
   bfd_elf_generic_reloc, /* special_function  */
1259
   "R_NDS32_PLTREL_HI20", /* name  */
1260
   false,     /* partial_inplace  */
1261
   0x000fffff,    /* src_mask  */
1262
   0x000fffff,    /* dst_mask  */
1263
   false),    /* pcrel_offset  */
1264
1265
  /* Low 12 bits of PLT symbol offset relative to PC.  */
1266
  HOWTO2 (R_NDS32_PLTREL_LO12,  /* type  */
1267
   0,     /* rightshift  */
1268
   4,     /* size  */
1269
   12,      /* bitsize  */
1270
   false,     /* pc_relative  */
1271
   0,     /* bitpos  */
1272
   complain_overflow_dont,/* complain_on_overflow  */
1273
   bfd_elf_generic_reloc, /* special_function  */
1274
   "R_NDS32_PLTREL_LO12", /* name  */
1275
   false,     /* partial_inplace  */
1276
   0x00000fff,    /* src_mask  */
1277
   0x00000fff,    /* dst_mask  */
1278
   false),    /* pcrel_offset  */
1279
1280
  /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
1281
  HOWTO2 (R_NDS32_PLT_GOTREL_HI20,  /* type  */
1282
   12,      /* rightshift  */
1283
   4,     /* size  */
1284
   20,      /* bitsize  */
1285
   false,     /* pc_relative  */
1286
   0,     /* bitpos  */
1287
   complain_overflow_dont,/* complain_on_overflow  */
1288
   bfd_elf_generic_reloc, /* special_function  */
1289
   "R_NDS32_PLT_GOTREL_HI20",/* name  */
1290
   false,     /* partial_inplace  */
1291
   0x000fffff,    /* src_mask  */
1292
   0x000fffff,    /* dst_mask  */
1293
   false),    /* pcrel_offset  */
1294
1295
  /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
1296
  HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type  */
1297
   0,     /* rightshift  */
1298
   4,     /* size  */
1299
   12,      /* bitsize  */
1300
   false,     /* pc_relative  */
1301
   0,     /* bitpos  */
1302
   complain_overflow_dont,/* complain_on_overflow  */
1303
   bfd_elf_generic_reloc, /* special_function  */
1304
   "R_NDS32_PLT_GOTREL_LO12",/* name  */
1305
   false,     /* partial_inplace  */
1306
   0x00000fff,    /* src_mask  */
1307
   0x00000fff,    /* dst_mask  */
1308
   false),    /* pcrel_offset  */
1309
1310
  /* Small data area 12 bits offset.  */
1311
  HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type  */
1312
   2,     /* rightshift  */
1313
   4,     /* size  */
1314
   12,      /* bitsize  */
1315
   false,     /* pc_relative  */
1316
   0,     /* bitpos  */
1317
   complain_overflow_signed,/* complain_on_overflow  */
1318
   bfd_elf_generic_reloc, /* special_function  */
1319
   "R_NDS32_SDA12S2_DP_RELA",/* name  */
1320
   false,     /* partial_inplace  */
1321
   0x00000fff,    /* src_mask  */
1322
   0x00000fff,    /* dst_mask  */
1323
   false),    /* pcrel_offset  */
1324
1325
  /* Small data area 12 bits offset.  */
1326
  HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type  */
1327
   2,     /* rightshift  */
1328
   4,     /* size  */
1329
   12,      /* bitsize  */
1330
   false,     /* pc_relative  */
1331
   0,     /* bitpos  */
1332
   complain_overflow_signed,/* complain_on_overflow  */
1333
   bfd_elf_generic_reloc, /* special_function  */
1334
   "R_NDS32_SDA12S2_SP_RELA",/* name  */
1335
   false,     /* partial_inplace  */
1336
   0x00000fff,    /* src_mask  */
1337
   0x00000fff,    /* dst_mask  */
1338
   false),    /* pcrel_offset  */
1339
  /* Lower 12 bits of address.  */
1340
1341
  HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type  */
1342
   2,     /* rightshift  */
1343
   4,     /* size  */
1344
   10,      /* bitsize  */
1345
   false,     /* pc_relative  */
1346
   0,     /* bitpos  */
1347
   complain_overflow_dont,/* complain_on_overflow  */
1348
   bfd_elf_generic_reloc, /* special_function  */
1349
   "R_NDS32_LO12S2_DP_RELA",/* name  */
1350
   false,     /* partial_inplace  */
1351
   0x000003ff,    /* src_mask  */
1352
   0x000003ff,    /* dst_mask  */
1353
   false),    /* pcrel_offset  */
1354
1355
  /* Lower 12 bits of address.  */
1356
  HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type  */
1357
   2,     /* rightshift  */
1358
   4,     /* size  */
1359
   10,      /* bitsize  */
1360
   false,     /* pc_relative  */
1361
   0,     /* bitpos  */
1362
   complain_overflow_dont,/* complain_on_overflow  */
1363
   bfd_elf_generic_reloc, /* special_function  */
1364
   "R_NDS32_LO12S2_SP_RELA",/* name  */
1365
   false,     /* partial_inplace  */
1366
   0x000003ff,    /* src_mask  */
1367
   0x000003ff,    /* dst_mask  */
1368
   false),    /* pcrel_offset  */
1369
1370
  /* Lower 12 bits of address.  Special identity for or case.  */
1371
  HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type  */
1372
   0,     /* rightshift  */
1373
   4,     /* size  */
1374
   12,      /* bitsize  */
1375
   false,     /* pc_relative  */
1376
   0,     /* bitpos  */
1377
   complain_overflow_dont,/* complain_on_overflow  */
1378
   bfd_elf_generic_reloc, /* special_function  */
1379
   "R_NDS32_LO12S0_ORI_RELA",/* name  */
1380
   false,     /* partial_inplace  */
1381
   0x00000fff,    /* src_mask  */
1382
   0x00000fff,    /* dst_mask  */
1383
   false),    /* pcrel_offset  */
1384
1385
  /* Small data area 19 bits offset.  */
1386
  HOWTO2 (R_NDS32_SDA16S3_RELA, /* type  */
1387
   3,     /* rightshift  */
1388
   4,     /* size  */
1389
   16,      /* bitsize  */
1390
   false,     /* pc_relative  */
1391
   0,     /* bitpos  */
1392
   complain_overflow_signed,/* complain_on_overflow  */
1393
   bfd_elf_generic_reloc, /* special_function  */
1394
   "R_NDS32_SDA16S3_RELA",/* name  */
1395
   false,     /* partial_inplace  */
1396
   0x0000ffff,    /* src_mask  */
1397
   0x0000ffff,    /* dst_mask  */
1398
   false),    /* pcrel_offset  */
1399
1400
  /* Small data area 15 bits offset.  */
1401
  HOWTO2 (R_NDS32_SDA17S2_RELA, /* type  */
1402
   2,     /* rightshift  */
1403
   4,     /* size  */
1404
   17,      /* bitsize  */
1405
   false,     /* pc_relative  */
1406
   0,     /* bitpos  */
1407
   complain_overflow_signed,/* complain_on_overflow  */
1408
   bfd_elf_generic_reloc, /* special_function  */
1409
   "R_NDS32_SDA17S2_RELA",/* name  */
1410
   false,     /* partial_inplace  */
1411
   0x0001ffff,    /* src_mask  */
1412
   0x0001ffff,    /* dst_mask  */
1413
   false),    /* pcrel_offset  */
1414
1415
  HOWTO2 (R_NDS32_SDA18S1_RELA, /* type  */
1416
   1,     /* rightshift  */
1417
   4,     /* size  */
1418
   18,      /* bitsize  */
1419
   false,     /* pc_relative  */
1420
   0,     /* bitpos  */
1421
   complain_overflow_signed,/* complain_on_overflow  */
1422
   bfd_elf_generic_reloc, /* special_function  */
1423
   "R_NDS32_SDA18S1_RELA",/* name  */
1424
   false,     /* partial_inplace  */
1425
   0x0003ffff,    /* src_mask  */
1426
   0x0003ffff,    /* dst_mask  */
1427
   false),    /* pcrel_offset  */
1428
1429
  HOWTO2 (R_NDS32_SDA19S0_RELA, /* type  */
1430
   0,     /* rightshift  */
1431
   4,     /* size  */
1432
   19,      /* bitsize  */
1433
   false,     /* pc_relative  */
1434
   0,     /* bitpos  */
1435
   complain_overflow_signed,/* complain_on_overflow  */
1436
   bfd_elf_generic_reloc, /* special_function  */
1437
   "R_NDS32_SDA19S0_RELA",/* name  */
1438
   false,     /* partial_inplace  */
1439
   0x0007ffff,    /* src_mask  */
1440
   0x0007ffff,    /* dst_mask  */
1441
   false),    /* pcrel_offset  */
1442
  HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type  */
1443
   0,     /* rightshift  */
1444
   1,     /* size  */
1445
   8,     /* bitsize  */
1446
   false,     /* pc_relative  */
1447
   0,     /* bitpos  */
1448
   complain_overflow_dont,/* complain_on_overflow  */
1449
   nds32_elf_ignore_reloc,/* special_function  */
1450
   "R_NDS32_DWARF2_OP1_RELA",/* name  */
1451
   false,     /* partial_inplace  */
1452
   0xff,      /* src_mask  */
1453
   0xff,      /* dst_mask  */
1454
   false),    /* pcrel_offset  */
1455
1456
  HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type  */
1457
   0,     /* rightshift  */
1458
   2,     /* size  */
1459
   16,      /* bitsize  */
1460
   false,     /* pc_relative  */
1461
   0,     /* bitpos  */
1462
   complain_overflow_dont,/* complain_on_overflow  */
1463
   nds32_elf_ignore_reloc,/* special_function  */
1464
   "R_NDS32_DWARF2_OP2_RELA",/* name  */
1465
   false,     /* partial_inplace  */
1466
   0xffff,    /* src_mask  */
1467
   0xffff,    /* dst_mask  */
1468
   false),    /* pcrel_offset  */
1469
1470
  HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type  */
1471
   0,     /* rightshift  */
1472
   4,     /* size  */
1473
   32,      /* bitsize  */
1474
   false,     /* pc_relative  */
1475
   0,     /* bitpos  */
1476
   complain_overflow_dont,/* complain_on_overflow  */
1477
   nds32_elf_ignore_reloc,/* special_function  */
1478
   "R_NDS32_DWARF2_LEB_RELA",/* name  */
1479
   false,     /* partial_inplace  */
1480
   0xffffffff,    /* src_mask  */
1481
   0xffffffff,    /* dst_mask  */
1482
   false),    /* pcrel_offset  */
1483
1484
  HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type  */
1485
   0,     /* rightshift  */
1486
   2,     /* size  */
1487
   16,      /* bitsize  */
1488
   false,     /* pc_relative  */
1489
   0,     /* bitpos  */
1490
   complain_overflow_dont,/* complain_on_overflow  */
1491
   nds32_elf_ignore_reloc,/* special_function  */
1492
   "R_NDS32_UPDATE_TA_RELA",/* name  */
1493
   false,     /* partial_inplace  */
1494
   0xffff,    /* src_mask  */
1495
   0xffff,    /* dst_mask  */
1496
   false),    /* pcrel_offset  */
1497
1498
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1499
     entry for the symbol.  */
1500
  HOWTO2 (R_NDS32_9_PLTREL, /* type  */
1501
   1,     /* rightshift  */
1502
   2,     /* size  */
1503
   8,     /* bitsize  */
1504
   true,      /* pc_relative  */
1505
   0,     /* bitpos  */
1506
   complain_overflow_signed,/* complain_on_overflow  */
1507
   bfd_elf_generic_reloc, /* special_function  */
1508
   "R_NDS32_9_PLTREL",  /* name  */
1509
   false,     /* partial_inplace  */
1510
   0xff,      /* src_mask  */
1511
   0xff,      /* dst_mask  */
1512
   true),     /* pcrel_offset  */
1513
1514
  /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
1515
  HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type  */
1516
   0,     /* rightshift  */
1517
   4,     /* size  */
1518
   20,      /* bitsize  */
1519
   false,     /* pc_relative  */
1520
   0,     /* bitpos  */
1521
   complain_overflow_dont,/* complain_on_overflow  */
1522
   bfd_elf_generic_reloc, /* special_function  */
1523
   "R_NDS32_PLT_GOTREL_LO20",/* name  */
1524
   false,     /* partial_inplace  */
1525
   0x000fffff,    /* src_mask  */
1526
   0x000fffff,    /* dst_mask  */
1527
   false),    /* pcrel_offset  */
1528
1529
  /* low 15 bits of PLT symbol offset relative to GOT (GP)  */
1530
  HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type  */
1531
   0,     /* rightshift  */
1532
   4,     /* size  */
1533
   15,      /* bitsize  */
1534
   false,     /* pc_relative  */
1535
   0,     /* bitpos  */
1536
   complain_overflow_dont,/* complain_on_overflow  */
1537
   bfd_elf_generic_reloc, /* special_function  */
1538
   "R_NDS32_PLT_GOTREL_LO15",/* name  */
1539
   false,     /* partial_inplace  */
1540
   0x00007fff,    /* src_mask  */
1541
   0x00007fff,    /* dst_mask  */
1542
   false),    /* pcrel_offset  */
1543
1544
  /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
1545
  HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type  */
1546
   0,     /* rightshift  */
1547
   4,     /* size  */
1548
   19,      /* bitsize  */
1549
   false,     /* pc_relative  */
1550
   0,     /* bitpos  */
1551
   complain_overflow_dont,/* complain_on_overflow  */
1552
   bfd_elf_generic_reloc, /* special_function  */
1553
   "R_NDS32_PLT_GOTREL_LO19",/* name  */
1554
   false,     /* partial_inplace  */
1555
   0x0007ffff,    /* src_mask  */
1556
   0x0007ffff,    /* dst_mask  */
1557
   false),    /* pcrel_offset  */
1558
1559
  HOWTO2 (R_NDS32_GOT_LO15, /* type  */
1560
   0,     /* rightshift  */
1561
   4,     /* size  */
1562
   15,      /* bitsize  */
1563
   false,     /* pc_relative  */
1564
   0,     /* bitpos  */
1565
   complain_overflow_dont,/* complain_on_overflow  */
1566
   bfd_elf_generic_reloc, /* special_function  */
1567
   "R_NDS32_GOT_LO15",  /* name  */
1568
   false,     /* partial_inplace  */
1569
   0x00007fff,    /* src_mask  */
1570
   0x00007fff,    /* dst_mask  */
1571
   false),    /* pcrel_offset  */
1572
1573
  HOWTO2 (R_NDS32_GOT_LO19, /* type  */
1574
   0,     /* rightshift  */
1575
   4,     /* size  */
1576
   19,      /* bitsize  */
1577
   false,     /* pc_relative  */
1578
   0,     /* bitpos  */
1579
   complain_overflow_dont,/* complain_on_overflow  */
1580
   bfd_elf_generic_reloc, /* special_function  */
1581
   "R_NDS32_GOT_LO19",  /* name  */
1582
   false,     /* partial_inplace  */
1583
   0x0007ffff,    /* src_mask  */
1584
   0x0007ffff,    /* dst_mask  */
1585
   false),    /* pcrel_offset  */
1586
1587
  HOWTO2 (R_NDS32_GOTOFF_LO15,  /* type  */
1588
   0,     /* rightshift  */
1589
   4,     /* size  */
1590
   15,      /* bitsize  */
1591
   false,     /* pc_relative  */
1592
   0,     /* bitpos  */
1593
   complain_overflow_dont,/* complain_on_overflow  */
1594
   bfd_elf_generic_reloc, /* special_function  */
1595
   "R_NDS32_GOTOFF_LO15", /* name  */
1596
   false,     /* partial_inplace  */
1597
   0x00007fff,    /* src_mask  */
1598
   0x00007fff,    /* dst_mask  */
1599
   false),    /* pcrel_offset  */
1600
1601
  HOWTO2 (R_NDS32_GOTOFF_LO19,  /* type  */
1602
   0,     /* rightshift  */
1603
   4,     /* size  */
1604
   19,      /* bitsize  */
1605
   false,     /* pc_relative  */
1606
   0,     /* bitpos  */
1607
   complain_overflow_dont,/* complain_on_overflow  */
1608
   bfd_elf_generic_reloc, /* special_function  */
1609
   "R_NDS32_GOTOFF_LO19", /* name  */
1610
   false,     /* partial_inplace  */
1611
   0x0007ffff,    /* src_mask  */
1612
   0x0007ffff,    /* dst_mask  */
1613
   false),    /* pcrel_offset  */
1614
1615
  /* GOT 15 bits offset.  */
1616
  HOWTO2 (R_NDS32_GOT15S2_RELA, /* type  */
1617
   2,     /* rightshift  */
1618
   4,     /* size  */
1619
   15,      /* bitsize  */
1620
   false,     /* pc_relative  */
1621
   0,     /* bitpos  */
1622
   complain_overflow_signed,/* complain_on_overflow  */
1623
   bfd_elf_generic_reloc, /* special_function  */
1624
   "R_NDS32_GOT15S2_RELA",/* name  */
1625
   false,     /* partial_inplace  */
1626
   0x00007fff,    /* src_mask  */
1627
   0x00007fff,    /* dst_mask  */
1628
   false),    /* pcrel_offset  */
1629
1630
  /* GOT 17 bits offset.  */
1631
  HOWTO2 (R_NDS32_GOT17S2_RELA, /* type  */
1632
   2,     /* rightshift  */
1633
   4,     /* size  */
1634
   17,      /* bitsize  */
1635
   false,     /* pc_relative  */
1636
   0,     /* bitpos  */
1637
   complain_overflow_signed,/* complain_on_overflow  */
1638
   bfd_elf_generic_reloc, /* special_function  */
1639
   "R_NDS32_GOT17S2_RELA",/* name  */
1640
   false,     /* partial_inplace  */
1641
   0x0001ffff,    /* src_mask  */
1642
   0x0001ffff,    /* dst_mask  */
1643
   false),    /* pcrel_offset  */
1644
1645
  /* A 5 bit address.  */
1646
  HOWTO2 (R_NDS32_5_RELA, /* type  */
1647
   0,     /* rightshift  */
1648
   2,     /* size  */
1649
   5,     /* bitsize  */
1650
   false,     /* pc_relative  */
1651
   0,     /* bitpos  */
1652
   complain_overflow_signed,/* complain_on_overflow  */
1653
   bfd_elf_generic_reloc, /* special_function  */
1654
   "R_NDS32_5_RELA",  /* name  */
1655
   false,     /* partial_inplace  */
1656
   0x1f,      /* src_mask  */
1657
   0x1f,      /* dst_mask  */
1658
   false),    /* pcrel_offset  */
1659
1660
  HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type  */
1661
   1,     /* rightshift  */
1662
   2,     /* size  */
1663
   9,     /* bitsize  */
1664
   true,      /* pc_relative  */
1665
   0,     /* bitpos  */
1666
   complain_overflow_unsigned,/* complain_on_overflow  */
1667
   bfd_elf_generic_reloc, /* special_function  */
1668
   "R_NDS32_10_UPCREL_RELA",/* name  */
1669
   false,     /* partial_inplace  */
1670
   0x1ff,     /* src_mask  */
1671
   0x1ff,     /* dst_mask  */
1672
   true),     /* pcrel_offset  */
1673
1674
  HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type  */
1675
   2,     /* rightshift  */
1676
   2,     /* size  */
1677
   7,     /* bitsize  */
1678
   false,     /* pc_relative  */
1679
   0,     /* bitpos  */
1680
   complain_overflow_unsigned,/* complain_on_overflow  */
1681
   bfd_elf_generic_reloc, /* special_function  */
1682
   "R_NDS32_SDA_FP7U2_RELA",/* name  */
1683
   false,     /* partial_inplace  */
1684
   0x0000007f,    /* src_mask  */
1685
   0x0000007f,    /* dst_mask  */
1686
   false),    /* pcrel_offset  */
1687
1688
  HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type  */
1689
   1,     /* rightshift  */
1690
   4,     /* size  */
1691
   8,     /* bitsize  */
1692
   true,      /* pc_relative  */
1693
   0,     /* bitpos  */
1694
   complain_overflow_signed,/* complain_on_overflow  */
1695
   bfd_elf_generic_reloc, /* special_function  */
1696
   "R_NDS32_WORD_9_PCREL_RELA",/* name  */
1697
   false,     /* partial_inplace  */
1698
   0xff,      /* src_mask  */
1699
   0xff,      /* dst_mask  */
1700
   true),     /* pcrel_offset  */
1701
1702
  HOWTO2 (R_NDS32_25_ABS_RELA,  /* type  */
1703
   1,     /* rightshift  */
1704
   4,     /* size  */
1705
   24,      /* bitsize  */
1706
   false,     /* pc_relative  */
1707
   0,     /* bitpos  */
1708
   complain_overflow_dont,/* complain_on_overflow  */
1709
   bfd_elf_generic_reloc, /* special_function  */
1710
   "R_NDS32_25_ABS_RELA", /* name  */
1711
   false,     /* partial_inplace  */
1712
   0xffffff,    /* src_mask  */
1713
   0xffffff,    /* dst_mask  */
1714
   false),    /* pcrel_offset  */
1715
1716
  /* A relative 17 bit relocation for ifc, right shifted by 1.  */
1717
  HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type  */
1718
   1,     /* rightshift  */
1719
   4,     /* size  */
1720
   16,      /* bitsize  */
1721
   true,      /* pc_relative  */
1722
   0,     /* bitpos  */
1723
   complain_overflow_signed,/* complain_on_overflow  */
1724
   bfd_elf_generic_reloc, /* special_function  */
1725
   "R_NDS32_17IFC_PCREL_RELA",/* name  */
1726
   false,     /* partial_inplace  */
1727
   0xffff,    /* src_mask  */
1728
   0xffff,    /* dst_mask  */
1729
   true),     /* pcrel_offset  */
1730
1731
  /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
1732
  HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type  */
1733
   1,     /* rightshift  */
1734
   2,     /* size  */
1735
   9,     /* bitsize  */
1736
   true,      /* pc_relative  */
1737
   0,     /* bitpos  */
1738
   complain_overflow_unsigned,/* complain_on_overflow  */
1739
   bfd_elf_generic_reloc, /* special_function  */
1740
   "R_NDS32_10IFCU_PCREL_RELA",/* name  */
1741
   false,     /* partial_inplace  */
1742
   0x1ff,     /* src_mask  */
1743
   0x1ff,     /* dst_mask  */
1744
   true),     /* pcrel_offset  */
1745
1746
  /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol.  */
1747
  HOWTO2 (R_NDS32_TLS_LE_HI20,  /* type  */
1748
   12,      /* rightshift  */
1749
   4,     /* size  */
1750
   20,      /* bitsize  */
1751
   false,     /* pc_relative  */
1752
   0,     /* bitpos  */
1753
   complain_overflow_dont,/* complain_on_overflow  */
1754
   bfd_elf_generic_reloc, /* special_function  */
1755
   "R_NDS32_TLS_LE_HI20", /* name  */
1756
   false,     /* partial_inplace  */
1757
   0x000fffff,    /* src_mask  */
1758
   0x000fffff,    /* dst_mask  */
1759
   false),    /* pcrel_offset  */
1760
1761
  HOWTO2 (R_NDS32_TLS_LE_LO12,  /* type  */
1762
   0,     /* rightshift  */
1763
   4,     /* size  */
1764
   12,      /* bitsize  */
1765
   false,     /* pc_relative  */
1766
   0,     /* bitpos  */
1767
   complain_overflow_dont,/* complain_on_overflow  */
1768
   bfd_elf_generic_reloc, /* special_function  */
1769
   "R_NDS32_TLS_LE_LO12", /* name  */
1770
   false,     /* partial_inplace  */
1771
   0x00000fff,    /* src_mask  */
1772
   0x00000fff,    /* dst_mask  */
1773
   false),    /* pcrel_offset  */
1774
1775
  /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol.  */
1776
  HOWTO2 (R_NDS32_TLS_IE_HI20,  /* type  */
1777
   12,      /* rightshift  */
1778
   4,     /* size  */
1779
   20,      /* bitsize  */
1780
   false,     /* pc_relative  */
1781
   0,     /* bitpos  */
1782
   complain_overflow_dont,/* complain_on_overflow  */
1783
   bfd_elf_generic_reloc, /* special_function  */
1784
   "R_NDS32_TLS_IE_HI20", /* name  */
1785
   false,     /* partial_inplace  */
1786
   0x000fffff,    /* src_mask  */
1787
   0x000fffff,    /* dst_mask  */
1788
   false),    /* pcrel_offset  */
1789
1790
  HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type  */
1791
   2,     /* rightshift  */
1792
   4,     /* size  */
1793
   10,      /* bitsize  */
1794
   false,     /* pc_relative  */
1795
   0,     /* bitpos  */
1796
   complain_overflow_dont,/* complain_on_overflow  */
1797
   bfd_elf_generic_reloc, /* special_function  */
1798
   "R_NDS32_TLS_IE_LO12S2",/* name  */
1799
   false,     /* partial_inplace  */
1800
   0x000003ff,    /* src_mask  */
1801
   0x000003ff,    /* dst_mask  */
1802
   false),    /* pcrel_offset  */
1803
1804
  /* TLS LE TP offset relocation  */
1805
  HOWTO2 (R_NDS32_TLS_TPOFF,  /* type  */
1806
   0,     /* rightshift  */
1807
   4,     /* size  */
1808
   32,      /* bitsize  */
1809
   false,     /* pc_relative  */
1810
   0,     /* bitpos  */
1811
   complain_overflow_bitfield,/* complain_on_overflow  */
1812
   bfd_elf_generic_reloc, /* special_function  */
1813
   "R_NDS32_TLS_TPOFF", /* name  */
1814
   false,     /* partial_inplace  */
1815
   0xffffffff,    /* src_mask  */
1816
   0xffffffff,    /* dst_mask  */
1817
   false),    /* pcrel_offset  */
1818
1819
  /* A 20 bit address.  */
1820
  HOWTO2 (R_NDS32_TLS_LE_20,  /* type  */
1821
   0,     /* rightshift  */
1822
   4,     /* size  */
1823
   20,      /* bitsize  */
1824
   false,     /* pc_relative  */
1825
   0,     /* bitpos  */
1826
   complain_overflow_signed,/* complain_on_overflow  */
1827
   bfd_elf_generic_reloc, /* special_function  */
1828
   "R_NDS32_TLS_LE_20", /* name  */
1829
   false,     /* partial_inplace  */
1830
   0xfffff,   /* src_mask  */
1831
   0xfffff,   /* dst_mask  */
1832
   false),    /* pcrel_offset  */
1833
1834
  HOWTO2 (R_NDS32_TLS_LE_15S0,  /* type  */
1835
   0,     /* rightshift  */
1836
   4,     /* size  */
1837
   15,      /* bitsize  */
1838
   false,     /* pc_relative  */
1839
   0,     /* bitpos  */
1840
   complain_overflow_signed,/* complain_on_overflow  */
1841
   bfd_elf_generic_reloc, /* special_function  */
1842
   "R_NDS32_TLS_LE_15S0", /* name  */
1843
   false,     /* partial_inplace  */
1844
   0x7fff,    /* src_mask  */
1845
   0x7fff,    /* dst_mask  */
1846
   false),    /* pcrel_offset  */
1847
1848
  HOWTO2 (R_NDS32_TLS_LE_15S1,  /* type  */
1849
   1,     /* rightshift  */
1850
   4,     /* size  */
1851
   15,      /* bitsize  */
1852
   false,     /* pc_relative  */
1853
   0,     /* bitpos  */
1854
   complain_overflow_signed,/* complain_on_overflow  */
1855
   bfd_elf_generic_reloc, /* special_function  */
1856
   "R_NDS32_TLS_LE_15S1", /* name  */
1857
   false,     /* partial_inplace  */
1858
   0x7fff,    /* src_mask  */
1859
   0x7fff,    /* dst_mask  */
1860
   false),    /* pcrel_offset  */
1861
1862
  HOWTO2 (R_NDS32_TLS_LE_15S2,  /* type  */
1863
   2,     /* rightshift  */
1864
   4,     /* size  */
1865
   15,      /* bitsize  */
1866
   false,     /* pc_relative  */
1867
   0,     /* bitpos  */
1868
   complain_overflow_signed,/* complain_on_overflow  */
1869
   bfd_elf_generic_reloc, /* special_function  */
1870
   "R_NDS32_TLS_LE_15S2", /* name  */
1871
   false,     /* partial_inplace  */
1872
   0x7fff,    /* src_mask  */
1873
   0x7fff,    /* dst_mask  */
1874
   false),    /* pcrel_offset  */
1875
1876
  /* Relax hint for unconditional call sequence  */
1877
  HOWTO2 (R_NDS32_LONGCALL4,  /* type  */
1878
   0,     /* rightshift  */
1879
   4,     /* size  */
1880
   32,      /* bitsize  */
1881
   false,     /* pc_relative  */
1882
   0,     /* bitpos  */
1883
   complain_overflow_dont,/* complain_on_overflow  */
1884
   nds32_elf_ignore_reloc,/* special_function  */
1885
   "R_NDS32_LONGCALL4", /* name  */
1886
   false,     /* partial_inplace  */
1887
   0xffffffff,    /* src_mask  */
1888
   0xffffffff,    /* dst_mask  */
1889
   false),    /* pcrel_offset  */
1890
1891
  /* Relax hint for conditional call sequence.  */
1892
  HOWTO2 (R_NDS32_LONGCALL5,  /* type  */
1893
   0,     /* rightshift  */
1894
   4,     /* size  */
1895
   32,      /* bitsize  */
1896
   false,     /* pc_relative  */
1897
   0,     /* bitpos  */
1898
   complain_overflow_dont,/* complain_on_overflow  */
1899
   nds32_elf_ignore_reloc,/* special_function  */
1900
   "R_NDS32_LONGCALL5", /* name  */
1901
   false,     /* partial_inplace  */
1902
   0xffffffff,    /* src_mask  */
1903
   0xffffffff,    /* dst_mask  */
1904
   false),    /* pcrel_offset  */
1905
1906
  /* Relax hint for conditional call sequence.  */
1907
  HOWTO2 (R_NDS32_LONGCALL6,  /* type  */
1908
   0,     /* rightshift  */
1909
   4,     /* size  */
1910
   32,      /* bitsize  */
1911
   false,     /* pc_relative  */
1912
   0,     /* bitpos  */
1913
   complain_overflow_dont,/* complain_on_overflow  */
1914
   nds32_elf_ignore_reloc,/* special_function  */
1915
   "R_NDS32_LONGCALL6", /* name  */
1916
   false,     /* partial_inplace  */
1917
   0xffffffff,    /* src_mask  */
1918
   0xffffffff,    /* dst_mask  */
1919
   false),    /* pcrel_offset  */
1920
1921
  /* Relax hint for unconditional branch sequence.  */
1922
  HOWTO2 (R_NDS32_LONGJUMP4,  /* type  */
1923
   0,     /* rightshift  */
1924
   4,     /* size  */
1925
   32,      /* bitsize  */
1926
   false,     /* pc_relative  */
1927
   0,     /* bitpos  */
1928
   complain_overflow_dont,/* complain_on_overflow  */
1929
   nds32_elf_ignore_reloc,/* special_function  */
1930
   "R_NDS32_LONGJUMP4", /* name  */
1931
   false,     /* partial_inplace  */
1932
   0xffffffff,    /* src_mask  */
1933
   0xffffffff,    /* dst_mask  */
1934
   false),    /* pcrel_offset  */
1935
1936
  /* Relax hint for conditional branch sequence.  */
1937
  HOWTO2 (R_NDS32_LONGJUMP5,  /* type  */
1938
   0,     /* rightshift  */
1939
   4,     /* size  */
1940
   32,      /* bitsize  */
1941
   false,     /* pc_relative  */
1942
   0,     /* bitpos  */
1943
   complain_overflow_dont,/* complain_on_overflow  */
1944
   nds32_elf_ignore_reloc,/* special_function  */
1945
   "R_NDS32_LONGJUMP5", /* name  */
1946
   false,     /* partial_inplace  */
1947
   0xffffffff,    /* src_mask  */
1948
   0xffffffff,    /* dst_mask  */
1949
   false),    /* pcrel_offset  */
1950
1951
  /* Relax hint for conditional branch sequence.  */
1952
  HOWTO2 (R_NDS32_LONGJUMP6,  /* type  */
1953
   0,     /* rightshift  */
1954
   4,     /* size  */
1955
   32,      /* bitsize  */
1956
   false,     /* pc_relative  */
1957
   0,     /* bitpos  */
1958
   complain_overflow_dont,/* complain_on_overflow  */
1959
   nds32_elf_ignore_reloc,/* special_function  */
1960
   "R_NDS32_LONGJUMP6", /* name  */
1961
   false,     /* partial_inplace  */
1962
   0xffffffff,    /* src_mask  */
1963
   0xffffffff,    /* dst_mask  */
1964
   false),    /* pcrel_offset  */
1965
1966
  /* Relax hint for conditional branch sequence.  */
1967
  HOWTO2 (R_NDS32_LONGJUMP7,  /* type  */
1968
   0,     /* rightshift  */
1969
   4,     /* size  */
1970
   32,      /* bitsize  */
1971
   false,     /* pc_relative  */
1972
   0,     /* bitpos  */
1973
   complain_overflow_dont,/* complain_on_overflow  */
1974
   nds32_elf_ignore_reloc,/* special_function  */
1975
   "R_NDS32_LONGJUMP7", /* name  */
1976
   false,     /* partial_inplace  */
1977
   0xffffffff,    /* src_mask  */
1978
   0xffffffff,    /* dst_mask  */
1979
   false),    /* pcrel_offset  */
1980
1981
  EMPTY_HOWTO (114),
1982
1983
  HOWTO2 (R_NDS32_TLS_IE_LO12,  /* type  */
1984
   0,     /* rightshift  */
1985
   4,     /* size  */
1986
   12,      /* bitsize  */
1987
   false,     /* pc_relative  */
1988
   0,     /* bitpos  */
1989
   complain_overflow_dont,/* complain_on_overflow  */
1990
   bfd_elf_generic_reloc, /* special_function  */
1991
   "R_NDS32_TLS_IE_LO12", /* name  */
1992
   false,     /* partial_inplace  */
1993
   0x00000fff,    /* src_mask  */
1994
   0x00000fff,    /* dst_mask  */
1995
   false),    /* pcrel_offset  */
1996
1997
  /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1998
     entry for the symbol.  */
1999
  HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type  */
2000
   12,      /* rightshift  */
2001
   4,     /* size  */
2002
   20,      /* bitsize  */
2003
   false,     /* pc_relative  */
2004
   0,     /* bitpos  */
2005
   complain_overflow_dont,/* complain_on_overflow  */
2006
   bfd_elf_generic_reloc, /* special_function  */
2007
   "R_NDS32_TLS_IEGP_HI20",/* name  */
2008
   false,     /* partial_inplace  */
2009
   0x000fffff,    /* src_mask  */
2010
   0x000fffff,    /* dst_mask  */
2011
   false),    /* pcrel_offset  */
2012
2013
  HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type  */
2014
   0,     /* rightshift  */
2015
   4,     /* size  */
2016
   12,      /* bitsize  */
2017
   false,     /* pc_relative  */
2018
   0,     /* bitpos  */
2019
   complain_overflow_dont,/* complain_on_overflow  */
2020
   bfd_elf_generic_reloc, /* special_function  */
2021
   "R_NDS32_TLS_IEGP_LO12",/* name  */
2022
   false,     /* partial_inplace  */
2023
   0x00000fff,    /* src_mask  */
2024
   0x00000fff,    /* dst_mask  */
2025
   false),    /* pcrel_offset  */
2026
2027
  HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type  */
2028
   2,     /* rightshift  */
2029
   4,     /* size  */
2030
   10,      /* bitsize  */
2031
   false,     /* pc_relative  */
2032
   0,     /* bitpos  */
2033
   complain_overflow_dont,/* complain_on_overflow  */
2034
   bfd_elf_generic_reloc, /* special_function  */
2035
   "R_NDS32_TLS_IEGP_LO12S2",/* name  */
2036
   false,     /* partial_inplace  */
2037
   0x000003ff,    /* src_mask  */
2038
   0x000003ff,    /* dst_mask  */
2039
   false),    /* pcrel_offset  */
2040
2041
  /* TLS description relocation  */
2042
  HOWTO2 (R_NDS32_TLS_DESC, /* type  */
2043
   12,      /* rightshift  */
2044
   4,     /* size  */
2045
   20,      /* bitsize  */
2046
   false,     /* pc_relative  */
2047
   0,     /* bitpos  */
2048
   complain_overflow_dont,/* complain_on_overflow  */
2049
   nds32_elf_hi20_reloc,  /* special_function  */
2050
   "R_NDS32_TLS_DESC_HI20",/* name  */
2051
   false,     /* partial_inplace  */
2052
   0x000fffff,    /* src_mask  */
2053
   0x000fffff,    /* dst_mask  */
2054
   false),    /* pcrel_offset  */
2055
2056
  /* TLS GD/LD description offset high part.  */
2057
  HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type  */
2058
   12,      /* rightshift  */
2059
   4,     /* size  */
2060
   20,      /* bitsize  */
2061
   false,     /* pc_relative  */
2062
   0,     /* bitpos  */
2063
   complain_overflow_dont,/* complain_on_overflow  */
2064
   nds32_elf_hi20_reloc,  /* special_function  */
2065
   "R_NDS32_TLS_DESC_HI20",/* name  */
2066
   false,     /* partial_inplace  */
2067
   0x000fffff,    /* src_mask  */
2068
   0x000fffff,    /* dst_mask  */
2069
   false),    /* pcrel_offset  */
2070
2071
  /* TLS GD/LD description offset low part.  */
2072
  HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type  */
2073
   0,     /* rightshift  */
2074
   4,     /* size  */
2075
   12,      /* bitsize  */
2076
   false,     /* pc_relative  */
2077
   0,     /* bitpos  */
2078
   complain_overflow_dont,/* complain_on_overflow  */
2079
   nds32_elf_lo12_reloc,  /* special_function  */
2080
   "R_NDS32_TLS_DESC_LO12",/* name  */
2081
   false,     /* partial_inplace  */
2082
   0x00000fff,    /* src_mask  */
2083
   0x00000fff,    /* dst_mask  */
2084
   false),    /* pcrel_offset  */
2085
2086
  /* TLS GD/LD description offset set (movi).  */
2087
  HOWTO2 (R_NDS32_TLS_DESC_20,  /* type  */
2088
   0,     /* rightshift  */
2089
   4,     /* size  */
2090
   20,      /* bitsize  */
2091
   false,     /* pc_relative  */
2092
   0,     /* bitpos  */
2093
   complain_overflow_signed,/* complain_on_overflow  */
2094
   bfd_elf_generic_reloc, /* special_function  */
2095
   "R_NDS32_TLS_DESC_20", /* name  */
2096
   false,     /* partial_inplace  */
2097
   0x000fffff,    /* src_mask  */
2098
   0x000fffff,    /* dst_mask  */
2099
   false),    /* pcrel_offset  */
2100
2101
  /* TLS GD/LD description offset set (lwi.gp).  */
2102
  HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type  */
2103
   2,     /* rightshift  */
2104
   4,     /* size  */
2105
   17,      /* bitsize  */
2106
   false,     /* pc_relative  */
2107
   0,     /* bitpos  */
2108
   complain_overflow_signed,/* complain_on_overflow  */
2109
   bfd_elf_generic_reloc, /* special_function  */
2110
   "R_NDS32_TLS_DESC_SDA17S2",/* name  */
2111
   false,     /* partial_inplace  */
2112
   0x0001ffff,    /* src_mask  */
2113
   0x0001ffff,    /* dst_mask  */
2114
   false),    /* pcrel_offset  */
2115
};
2116
2117
/* Relocations used for relaxation.  */
2118
#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2119
  [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2120
2121
static reloc_howto_type nds32_elf_relax_howto_table[] = {
2122
  HOWTO3 (R_NDS32_RELAX_ENTRY,  /* type  */
2123
   0,     /* rightshift  */
2124
   4,     /* size  */
2125
   32,      /* bitsize  */
2126
   false,     /* pc_relative  */
2127
   0,     /* bitpos  */
2128
   complain_overflow_dont,/* complain_on_overflow  */
2129
   nds32_elf_ignore_reloc,/* special_function  */
2130
   "R_NDS32_RELAX_ENTRY", /* name  */
2131
   false,     /* partial_inplace  */
2132
   0xffffffff,    /* src_mask  */
2133
   0xffffffff,    /* dst_mask  */
2134
   false),    /* pcrel_offset  */
2135
2136
  HOWTO3 (R_NDS32_GOT_SUFF, /* type  */
2137
   0,     /* rightshift  */
2138
   4,     /* size  */
2139
   32,      /* bitsize  */
2140
   false,     /* pc_relative  */
2141
   0,     /* bitpos  */
2142
   complain_overflow_dont,/* complain_on_overflow  */
2143
   nds32_elf_ignore_reloc,/* special_function  */
2144
   "R_NDS32_GOT_SUFF",  /* name  */
2145
   false,     /* partial_inplace  */
2146
   0xffffffff,    /* src_mask  */
2147
   0xffffffff,    /* dst_mask  */
2148
   false),    /* pcrel_offset  */
2149
2150
  HOWTO3 (R_NDS32_GOTOFF_SUFF,  /* type  */
2151
   0,     /* rightshift  */
2152
   4,     /* size  */
2153
   32,      /* bitsize  */
2154
   false,     /* pc_relative  */
2155
   0,     /* bitpos  */
2156
   complain_overflow_bitfield,/* complain_on_overflow  */
2157
   nds32_elf_ignore_reloc,/* special_function  */
2158
   "R_NDS32_GOTOFF_SUFF", /* name  */
2159
   false,     /* partial_inplace  */
2160
   0xffffffff,    /* src_mask  */
2161
   0xffffffff,    /* dst_mask  */
2162
   false),    /* pcrel_offset  */
2163
2164
  HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type  */
2165
   0,     /* rightshift  */
2166
   4,     /* size  */
2167
   32,      /* bitsize  */
2168
   false,     /* pc_relative  */
2169
   0,     /* bitpos  */
2170
   complain_overflow_dont,/* complain_on_overflow  */
2171
   nds32_elf_ignore_reloc,/* special_function  */
2172
   "R_NDS32_PLT_GOT_SUFF",/* name  */
2173
   false,     /* partial_inplace  */
2174
   0xffffffff,    /* src_mask  */
2175
   0xffffffff,    /* dst_mask  */
2176
   false),    /* pcrel_offset  */
2177
2178
  HOWTO3 (R_NDS32_MULCALL_SUFF, /* type  */
2179
   0,     /* rightshift  */
2180
   4,     /* size  */
2181
   32,      /* bitsize  */
2182
   false,     /* pc_relative  */
2183
   0,     /* bitpos  */
2184
   complain_overflow_dont,/* complain_on_overflow  */
2185
   nds32_elf_ignore_reloc,/* special_function  */
2186
   "R_NDS32_MULCALL_SUFF",/* name  */
2187
   false,     /* partial_inplace  */
2188
   0xffffffff,    /* src_mask  */
2189
   0xffffffff,    /* dst_mask  */
2190
   false),    /* pcrel_offset  */
2191
2192
  HOWTO3 (R_NDS32_PTR,    /* type  */
2193
   0,     /* rightshift  */
2194
   4,     /* size  */
2195
   32,      /* bitsize  */
2196
   false,     /* pc_relative  */
2197
   0,     /* bitpos  */
2198
   complain_overflow_dont,/* complain_on_overflow  */
2199
   nds32_elf_ignore_reloc,/* special_function  */
2200
   "R_NDS32_PTR",   /* name  */
2201
   false,     /* partial_inplace  */
2202
   0xffffffff,    /* src_mask  */
2203
   0xffffffff,    /* dst_mask  */
2204
   false),    /* pcrel_offset  */
2205
2206
  HOWTO3 (R_NDS32_PTR_COUNT,  /* type  */
2207
   0,     /* rightshift  */
2208
   4,     /* size  */
2209
   32,      /* bitsize  */
2210
   false,     /* pc_relative  */
2211
   0,     /* bitpos  */
2212
   complain_overflow_dont,/* complain_on_overflow  */
2213
   nds32_elf_ignore_reloc,/* special_function  */
2214
   "R_NDS32_PTR_COUNT", /* name  */
2215
   false,     /* partial_inplace  */
2216
   0xffffffff,    /* src_mask  */
2217
   0xffffffff,    /* dst_mask  */
2218
   false),    /* pcrel_offset  */
2219
2220
  HOWTO3 (R_NDS32_PTR_RESOLVED, /* type  */
2221
   0,     /* rightshift  */
2222
   4,     /* size  */
2223
   32,      /* bitsize  */
2224
   false,     /* pc_relative  */
2225
   0,     /* bitpos  */
2226
   complain_overflow_dont,/* complain_on_overflow  */
2227
   nds32_elf_ignore_reloc,/* special_function  */
2228
   "R_NDS32_PTR_RESOLVED",/* name  */
2229
   false,     /* partial_inplace  */
2230
   0xffffffff,    /* src_mask  */
2231
   0xffffffff,    /* dst_mask  */
2232
   false),    /* pcrel_offset  */
2233
2234
  HOWTO3 (R_NDS32_PLTBLOCK, /* type  */
2235
   0,     /* rightshift  */
2236
   4,     /* size  */
2237
   32,      /* bitsize  */
2238
   false,     /* pc_relative  */
2239
   0,     /* bitpos  */
2240
   complain_overflow_dont,/* complain_on_overflow  */
2241
   nds32_elf_ignore_reloc,/* special_function  */
2242
   "R_NDS32_PLTBLOCK",  /* name  */
2243
   false,     /* partial_inplace  */
2244
   0xffffffff,    /* src_mask  */
2245
   0xffffffff,    /* dst_mask  */
2246
   false),    /* pcrel_offset  */
2247
2248
  HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type  */
2249
   0,     /* rightshift  */
2250
   4,     /* size  */
2251
   32,      /* bitsize  */
2252
   false,     /* pc_relative  */
2253
   0,     /* bitpos  */
2254
   complain_overflow_dont,/* complain_on_overflow  */
2255
   nds32_elf_ignore_reloc,/* special_function  */
2256
   "R_NDS32_RELAX_REGION_BEGIN",/* name  */
2257
   false,     /* partial_inplace  */
2258
   0xffffffff,    /* src_mask  */
2259
   0xffffffff,    /* dst_mask  */
2260
   false),    /* pcrel_offset  */
2261
2262
  HOWTO3 (R_NDS32_RELAX_REGION_END,/* type  */
2263
   0,     /* rightshift  */
2264
   4,     /* size  */
2265
   32,      /* bitsize  */
2266
   false,     /* pc_relative  */
2267
   0,     /* bitpos  */
2268
   complain_overflow_dont,/* complain_on_overflow  */
2269
   nds32_elf_ignore_reloc,/* special_function  */
2270
   "R_NDS32_RELAX_REGION_END",/* name  */
2271
   false,     /* partial_inplace  */
2272
   0xffffffff,    /* src_mask  */
2273
   0xffffffff,    /* dst_mask  */
2274
   false),    /* pcrel_offset  */
2275
2276
  HOWTO3 (R_NDS32_MINUEND,  /* type  */
2277
   0,     /* rightshift  */
2278
   4,     /* size  */
2279
   32,      /* bitsize  */
2280
   false,     /* pc_relative  */
2281
   0,     /* bitpos  */
2282
   complain_overflow_dont,/* complain_on_overflow  */
2283
   nds32_elf_ignore_reloc,/* special_function  */
2284
   "R_NDS32_MINUEND", /* name  */
2285
   false,     /* partial_inplace  */
2286
   0xffffffff,    /* src_mask  */
2287
   0xffffffff,    /* dst_mask  */
2288
   false),    /* pcrel_offset  */
2289
2290
  HOWTO3 (R_NDS32_SUBTRAHEND, /* type  */
2291
   0,     /* rightshift  */
2292
   4,     /* size  */
2293
   32,      /* bitsize  */
2294
   false,     /* pc_relative  */
2295
   0,     /* bitpos  */
2296
   complain_overflow_dont,/* complain_on_overflow  */
2297
   nds32_elf_ignore_reloc,/* special_function  */
2298
   "R_NDS32_SUBTRAHEND",  /* name  */
2299
   false,     /* partial_inplace  */
2300
   0xffffffff,    /* src_mask  */
2301
   0xffffffff,    /* dst_mask  */
2302
   false),    /* pcrel_offset  */
2303
2304
  HOWTO3 (R_NDS32_DIFF8,  /* type  */
2305
   0,     /* rightshift  */
2306
   1,     /* size  */
2307
   8,     /* bitsize  */
2308
   false,     /* pc_relative  */
2309
   0,     /* bitpos  */
2310
   complain_overflow_dont,/* complain_on_overflow  */
2311
   nds32_elf_ignore_reloc,/* special_function  */
2312
   "R_NDS32_DIFF8", /* name  */
2313
   false,     /* partial_inplace  */
2314
   0x000000ff,    /* src_mask  */
2315
   0x000000ff,    /* dst_mask  */
2316
   false),    /* pcrel_offset  */
2317
2318
  HOWTO3 (R_NDS32_DIFF16, /* type  */
2319
   0,     /* rightshift  */
2320
   2,     /* size  */
2321
   16,      /* bitsize  */
2322
   false,     /* pc_relative  */
2323
   0,     /* bitpos  */
2324
   complain_overflow_dont,/* complain_on_overflow  */
2325
   nds32_elf_ignore_reloc,/* special_function  */
2326
   "R_NDS32_DIFF16",  /* name  */
2327
   false,     /* partial_inplace  */
2328
   0x0000ffff,    /* src_mask  */
2329
   0x0000ffff,    /* dst_mask  */
2330
   false),    /* pcrel_offset  */
2331
2332
  HOWTO3 (R_NDS32_DIFF32, /* type  */
2333
   0,     /* rightshift  */
2334
   4,     /* size  */
2335
   32,      /* bitsize  */
2336
   false,     /* pc_relative  */
2337
   0,     /* bitpos  */
2338
   complain_overflow_dont,/* complain_on_overflow  */
2339
   nds32_elf_ignore_reloc,/* special_function  */
2340
   "R_NDS32_DIFF32",  /* name  */
2341
   false,     /* partial_inplace  */
2342
   0xffffffff,    /* src_mask  */
2343
   0xffffffff,    /* dst_mask  */
2344
   false),    /* pcrel_offset  */
2345
2346
  HOWTO3 (R_NDS32_DIFF_ULEB128, /* type  */
2347
   0,     /* rightshift  */
2348
   1,     /* size  */
2349
   0,     /* bitsize  */
2350
   false,     /* pc_relative  */
2351
   0,     /* bitpos  */
2352
   complain_overflow_dont,/* complain_on_overflow  */
2353
   nds32_elf_ignore_reloc,/* special_function  */
2354
   "R_NDS32_DIFF_ULEB128",/* name  */
2355
   false,     /* partial_inplace  */
2356
   0xffffffff,    /* src_mask  */
2357
   0xffffffff,    /* dst_mask  */
2358
   false),    /* pcrel_offset  */
2359
2360
  HOWTO3 (R_NDS32_DATA,   /* type  */
2361
   0,     /* rightshift  */
2362
   4,     /* size  */
2363
   32,      /* bitsize  */
2364
   false,     /* pc_relative  */
2365
   0,     /* bitpos  */
2366
   complain_overflow_dont,/* complain_on_overflow  */
2367
   nds32_elf_ignore_reloc,/* special_function  */
2368
   "R_NDS32_DATA",  /* name  */
2369
   false,     /* partial_inplace  */
2370
   0xffffffff,    /* src_mask  */
2371
   0xffffffff,    /* dst_mask  */
2372
   false),    /* pcrel_offset  */
2373
2374
  HOWTO3 (R_NDS32_TRAN,   /* type  */
2375
   0,     /* rightshift  */
2376
   4,     /* size  */
2377
   32,      /* bitsize  */
2378
   false,     /* pc_relative  */
2379
   0,     /* bitpos  */
2380
   complain_overflow_dont,/* complain_on_overflow  */
2381
   nds32_elf_ignore_reloc,/* special_function  */
2382
   "R_NDS32_TRAN",  /* name  */
2383
   false,     /* partial_inplace  */
2384
   0xffffffff,    /* src_mask  */
2385
   0xffffffff,    /* dst_mask  */
2386
   false),    /* pcrel_offset  */
2387
2388
  HOWTO3 (R_NDS32_TLS_LE_ADD, /* type  */
2389
   0,     /* rightshift  */
2390
   4,     /* size  */
2391
   32,      /* bitsize  */
2392
   false,     /* pc_relative  */
2393
   0,     /* bitpos  */
2394
   complain_overflow_dont,/* complain_on_overflow  */
2395
   nds32_elf_ignore_reloc,/* special_function  */
2396
   "R_NDS32_TLS_LE_ADD",  /* name  */
2397
   false,     /* partial_inplace  */
2398
   0xffffffff,    /* src_mask  */
2399
   0xffffffff,    /* dst_mask  */
2400
   false),    /* pcrel_offset  */
2401
2402
  HOWTO3 (R_NDS32_TLS_LE_LS,  /* type  */
2403
   0,     /* rightshift  */
2404
   4,     /* size  */
2405
   32,      /* bitsize  */
2406
   false,     /* pc_relative  */
2407
   0,     /* bitpos  */
2408
   complain_overflow_dont,/* complain_on_overflow  */
2409
   nds32_elf_ignore_reloc,/* special_function  */
2410
   "R_NDS32_TLS_LE_LS", /* name  */
2411
   false,     /* partial_inplace  */
2412
   0xffffffff,    /* src_mask  */
2413
   0xffffffff,    /* dst_mask  */
2414
   false),    /* pcrel_offset  */
2415
2416
  HOWTO3 (R_NDS32_EMPTY,  /* type  */
2417
   0,     /* rightshift  */
2418
   4,     /* size  */
2419
   32,      /* bitsize  */
2420
   false,     /* pc_relative  */
2421
   0,     /* bitpos  */
2422
   complain_overflow_dont,/* complain_on_overflow  */
2423
   nds32_elf_ignore_reloc,/* special_function  */
2424
   "R_NDS32_EMPTY", /* name  */
2425
   false,     /* partial_inplace  */
2426
   0xffffffff,    /* src_mask  */
2427
   0xffffffff,    /* dst_mask  */
2428
   false),    /* pcrel_offset  */
2429
2430
  /* TLS GD/LD description address base addition.  */
2431
  HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type  */
2432
   0,     /* rightshift  */
2433
   4,     /* size  */
2434
   32,      /* bitsize  */
2435
   false,     /* pc_relative  */
2436
   0,     /* bitpos  */
2437
   complain_overflow_dont,/* complain_on_overflow  */
2438
   nds32_elf_ignore_reloc,/* special_function  */
2439
   "R_NDS32_TLS_DESC_ADD",/* name  */
2440
   false,     /* partial_inplace  */
2441
   0xffffffff,    /* src_mask  */
2442
   0xffffffff,    /* dst_mask  */
2443
   false),    /* pcrel_offset  */
2444
2445
  /* TLS GD/LD description function load.  */
2446
  HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type  */
2447
   0,     /* rightshift  */
2448
   4,     /* size  */
2449
   32,      /* bitsize  */
2450
   false,     /* pc_relative  */
2451
   0,     /* bitpos  */
2452
   complain_overflow_dont,/* complain_on_overflow  */
2453
   nds32_elf_ignore_reloc,/* special_function  */
2454
   "R_NDS32_TLS_DESC_FUNC",/* name  */
2455
   false,     /* partial_inplace  */
2456
   0xffffffff,    /* src_mask  */
2457
   0xffffffff,    /* dst_mask  */
2458
   false),    /* pcrel_offset  */
2459
2460
  /* TLS DESC resolve function call.  */
2461
  HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type  */
2462
   0,     /* rightshift  */
2463
   4,     /* size  */
2464
   32,      /* bitsize  */
2465
   false,     /* pc_relative  */
2466
   0,     /* bitpos  */
2467
   complain_overflow_dont,/* complain_on_overflow  */
2468
   nds32_elf_ignore_reloc,/* special_function  */
2469
   "R_NDS32_TLS_DESC_CALL",/* name  */
2470
   false,     /* partial_inplace  */
2471
   0xffffffff,    /* src_mask  */
2472
   0xffffffff,    /* dst_mask  */
2473
   false),    /* pcrel_offset  */
2474
2475
  /* TLS DESC variable access.  */
2476
  HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type  */
2477
   0,     /* rightshift  */
2478
   4,     /* size  */
2479
   32,      /* bitsize  */
2480
   false,     /* pc_relative  */
2481
   0,     /* bitpos  */
2482
   complain_overflow_dont,/* complain_on_overflow  */
2483
   nds32_elf_ignore_reloc,/* special_function  */
2484
   "R_NDS32_TLS_DESC_MEM",/* name  */
2485
   false,     /* partial_inplace  */
2486
   0xffffffff,    /* src_mask  */
2487
   0xffffffff,    /* dst_mask  */
2488
   false),    /* pcrel_offset  */
2489
2490
  /* TLS GD/LD description mark (@tlsdec).  */
2491
  HOWTO3 (R_NDS32_RELAX_REMOVE, /* type  */
2492
   0,     /* rightshift  */
2493
   4,     /* size  */
2494
   32,      /* bitsize  */
2495
   false,     /* pc_relative  */
2496
   0,     /* bitpos  */
2497
   complain_overflow_dont,/* complain_on_overflow  */
2498
   nds32_elf_ignore_reloc,/* special_function  */
2499
   "R_NDS32_REMOVE",  /* name  */
2500
   false,     /* partial_inplace  */
2501
   0xffffffff,    /* src_mask  */
2502
   0xffffffff,    /* dst_mask  */
2503
   false),    /* pcrel_offset  */
2504
2505
  /* TLS GD/LD description mark (@tlsdec).  */
2506
  HOWTO3 (R_NDS32_RELAX_GROUP,  /* type  */
2507
   0,     /* rightshift  */
2508
   4,     /* size  */
2509
   32,      /* bitsize  */
2510
   false,     /* pc_relative  */
2511
   0,     /* bitpos  */
2512
   complain_overflow_dont,/* complain_on_overflow  */
2513
   nds32_elf_ignore_reloc,/* special_function  */
2514
   "R_NDS32_GROUP", /* name  */
2515
   false,     /* partial_inplace  */
2516
   0xffffffff,    /* src_mask  */
2517
   0xffffffff,    /* dst_mask  */
2518
   false),    /* pcrel_offset  */
2519
2520
  HOWTO3 (R_NDS32_TLS_IEGP_LW,  /* type  */
2521
   0,     /* rightshift  */
2522
   4,     /* size  */
2523
   32,      /* bitsize  */
2524
   false,     /* pc_relative  */
2525
   0,     /* bitpos  */
2526
   complain_overflow_dont,/* complain_on_overflow  */
2527
   nds32_elf_ignore_reloc,/* special_function  */
2528
   "R_NDS32_TLS_IEGP_LW", /* name  */
2529
   false,     /* partial_inplace  */
2530
   0xffffffff,    /* src_mask  */
2531
   0xffffffff,    /* dst_mask  */
2532
   false),    /* pcrel_offset  */
2533
2534
  /* LA and FLSI relaxation.  */
2535
  HOWTO3 (R_NDS32_LSI,    /* type  */
2536
   0,     /* rightshift  */
2537
   4,     /* size  */
2538
   32,      /* bitsize  */
2539
   false,     /* pc_relative  */
2540
   0,     /* bitpos  */
2541
   complain_overflow_dont,/* complain_on_overflow  */
2542
   nds32_elf_ignore_reloc,/* special_function  */
2543
   "R_NDS32_LSI",   /* name  */
2544
   false,     /* partial_inplace  */
2545
   0xffffffff,    /* src_mask  */
2546
   0xffffffff,    /* dst_mask  */
2547
   false),
2548
};
2549
2550
static unsigned long dl_tlsdesc_lazy_trampoline[] =
2551
{
2552
  0x46200000,     /* sethi $r2,#0x0      */
2553
  0x58210000,     /* ori $r2,$r2,#0x0    */
2554
  0x40217400,     /* add $r2,$r2,$gp     */
2555
  0x04210000,     /* lwi $r2,[$r2+#0x0]  */
2556
  0x46300000,     /* sethi $r3,#0x0      */
2557
  0x58318000,     /* ori $r3,$r3,#0x0    */
2558
  0x4031f400,     /* add $r3,$r3,$gp     */
2559
  0x4a000800,     /* jr $r2              */
2560
};
2561
2562
static void
2563
nds32_put_trampoline (void *contents, const unsigned long *template,
2564
          unsigned count)
2565
0
{
2566
0
  unsigned ix;
2567
2568
0
  for (ix = 0; ix != count; ix++)
2569
0
    {
2570
0
      unsigned long insn = template[ix];
2571
0
      bfd_putb32 (insn, (char *) contents + ix * 4);
2572
0
    }
2573
0
}
2574
2575
/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2576
   This prototype is the same as qsort ().  */
2577
2578
static void
2579
nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2580
          int (*compar) (const void *lhs, const void *rhs))
2581
0
{
2582
0
  char *ptr = (char *) base;
2583
0
  int i, j;
2584
0
  char tmp[sizeof (Elf_Internal_Rela)];
2585
2586
0
  BFD_ASSERT (size <= sizeof (tmp));
2587
2588
  /* If i is less than j, i is inserted before j.
2589
2590
     |---- j ----- i --------------|
2591
      \    / \      /
2592
   sorted   unsorted
2593
   */
2594
2595
0
  for (i = 1; i < (int) nmemb; i++)
2596
0
    {
2597
0
      for (j = (i - 1); j >= 0; j--)
2598
0
  if (compar (ptr + i * size, ptr + j * size) >= 0)
2599
0
    break;
2600
2601
0
      j++;
2602
2603
0
      if (i == j)
2604
0
  continue; /* i is in order.  */
2605
2606
0
      memcpy (tmp, ptr + i * size, size);
2607
0
      memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2608
0
      memcpy (ptr + j * size, tmp, size);
2609
0
    }
2610
0
}
2611
2612
/* Sort relocation by r_offset.
2613
2614
   We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2615
   algorithm.  Relocations at the same r_offset must keep their order.
2616
   For example, RELAX_ENTRY must be the very first relocation entry.
2617
2618
   Currently, this function implements insertion-sort.
2619
2620
   FIXME: If we already sort them in assembler, why bother sort them
2621
    here again?  */
2622
2623
static int
2624
compar_reloc (const void *lhs, const void *rhs)
2625
0
{
2626
0
  const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2627
0
  const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2628
2629
0
  if (l->r_offset > r->r_offset)
2630
0
    return 1;
2631
0
  else if (l->r_offset == r->r_offset)
2632
0
    return 0;
2633
0
  else
2634
0
    return -1;
2635
0
}
2636
2637
/* Functions listed below are only used for old relocs.
2638
     nds32_elf_9_pcrel_reloc
2639
     nds32_elf_do_9_pcrel_reloc
2640
     nds32_elf_hi20_reloc
2641
     nds32_elf_relocate_hi20
2642
     nds32_elf_lo12_reloc
2643
     nds32_elf_sda15_reloc
2644
     nds32_elf_generic_reloc.  */
2645
2646
/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
2647
2648
static bfd_reloc_status_type
2649
nds32_elf_9_pcrel_reloc (bfd *       abfd,
2650
       arelent *   reloc_entry,
2651
       asymbol *   symbol,
2652
       void *      data,
2653
       asection *  input_section,
2654
       bfd *       output_bfd,
2655
       char **     error_message ATTRIBUTE_UNUSED)
2656
3
{
2657
  /* This part is from bfd_elf_generic_reloc.  */
2658
3
  if (output_bfd != (bfd *) NULL
2659
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
2660
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2661
0
    {
2662
0
      reloc_entry->address += input_section->output_offset;
2663
0
      return bfd_reloc_ok;
2664
0
    }
2665
2666
3
  if (output_bfd != NULL)
2667
0
    {
2668
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
2669
0
      return bfd_reloc_continue;
2670
0
    }
2671
2672
3
  return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2673
3
             input_section,
2674
3
             data, reloc_entry->address,
2675
3
             symbol->section,
2676
3
             (symbol->value
2677
3
              + symbol->section->output_section->vma
2678
3
              + symbol->section->output_offset),
2679
3
             reloc_entry->addend);
2680
3
}
2681
2682
/* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
2683
0
#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2684
2685
static bfd_reloc_status_type
2686
nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
2687
          reloc_howto_type *  howto,
2688
          asection *          input_section,
2689
          bfd_byte *          data,
2690
          bfd_vma             offset,
2691
          asection *          symbol_section ATTRIBUTE_UNUSED,
2692
          bfd_vma             symbol_value,
2693
          bfd_vma             addend)
2694
3
{
2695
3
  bfd_vma relocation;
2696
3
  unsigned short x;
2697
3
  bfd_reloc_status_type status;
2698
2699
  /* Sanity check the address (offset in section).  */
2700
3
  bfd_vma octet = offset * OCTETS_PER_BYTE (abfd, input_section);
2701
3
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octet))
2702
1
    return bfd_reloc_outofrange;
2703
2704
2
  relocation = symbol_value + addend;
2705
  /* Make it pc relative.  */
2706
2
  relocation -= (input_section->output_section->vma
2707
2
     + input_section->output_offset);
2708
  /* These jumps mask off the lower two bits of the current address
2709
     before doing pcrel calculations.  */
2710
2
  relocation -= (offset & -(bfd_vma) 2);
2711
2712
2
  if (relocation + ACCURATE_8BIT_S1 >= 2 * ACCURATE_8BIT_S1)
2713
1
    status = bfd_reloc_overflow;
2714
1
  else
2715
1
    status = bfd_reloc_ok;
2716
2717
2
  x = bfd_getb16 (data + offset);
2718
2719
2
  relocation >>= howto->rightshift;
2720
2
  relocation <<= howto->bitpos;
2721
2
  x = (x & ~howto->dst_mask)
2722
2
      | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2723
2724
2
  bfd_putb16 ((bfd_vma) x, data + offset);
2725
2726
2
  return status;
2727
3
}
2728
2729
/* Handle the R_NDS32_HI20_[SU]LO relocs.
2730
   HI20_SLO is for the add3 and load/store with displacement instructions.
2731
   HI20 is for the or3 instruction.
2732
   For R_NDS32_HI20_SLO, the lower 12 bits are sign extended when added to
2733
   the high 20 bits so if the lower 12 bits are negative (bit 11 set) then
2734
   we must add one to the high 20 bits (which will get subtracted off when
2735
   the low 12 bits are added).
2736
   These relocs have to be done in combination with an R_NDS32_LO12 reloc
2737
   because there is a carry from the LO12 to the HI20.  Here we just save
2738
   the information we need; we do the actual relocation when we see the LO12.
2739
   This code is copied from the elf32-mips.c.  We also support an arbitrary
2740
   number of HI20 relocs to be associated with a single LO12 reloc.  The
2741
   assembler sorts the relocs to ensure each HI20 immediately precedes its
2742
   LO12.  However if there are multiple copies, the assembler may not find
2743
   the real LO12 so it picks the first one it finds.  */
2744
2745
struct nds32_hi20
2746
{
2747
  struct nds32_hi20 *next;
2748
  bfd_byte *addr;
2749
  bfd_vma addend;
2750
};
2751
2752
struct _nds32_elf_section_data
2753
{
2754
  struct bfd_elf_section_data elf;
2755
  struct nds32_hi20 *nds32_hi20_list;
2756
};
2757
2758
#define nds32_elf_section_data(sec) \
2759
10.4k
  ((struct _nds32_elf_section_data *) elf_section_data (sec))
2760
2761
static bool
2762
nds32_elf_new_section_hook (bfd *abfd, asection *sec)
2763
31.1k
{
2764
31.1k
  struct _nds32_elf_section_data *sdata;
2765
2766
31.1k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
2767
31.1k
  if (sdata == NULL)
2768
0
    return false;
2769
31.1k
  sec->used_by_bfd = sdata;
2770
2771
31.1k
  return _bfd_elf_new_section_hook (abfd, sec);
2772
31.1k
}
2773
2774
static void
2775
nds32_elf_free_hi20_list (asection *sec)
2776
10.4k
{
2777
10.4k
  struct _nds32_elf_section_data *sdata = nds32_elf_section_data (sec);
2778
10.4k
  while (sdata->nds32_hi20_list != NULL)
2779
2
    {
2780
2
      struct nds32_hi20 *hi = sdata->nds32_hi20_list;
2781
2
      sdata->nds32_hi20_list = hi->next;
2782
2
      free (hi);
2783
2
    }
2784
10.4k
}
2785
2786
static bool
2787
nds32_elf_free_cached_info (bfd *abfd)
2788
170k
{
2789
170k
  if (bfd_get_format (abfd) == bfd_object
2790
170k
      || bfd_get_format (abfd) == bfd_core)
2791
647
    {
2792
11.1k
      for (asection *sec = abfd->sections; sec; sec = sec->next)
2793
10.4k
  nds32_elf_free_hi20_list (sec);
2794
647
    }
2795
170k
  return _bfd_elf_free_cached_info (abfd);
2796
170k
}
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
4
{
2807
4
  bfd_reloc_status_type ret;
2808
4
  bfd_vma relocation;
2809
4
  struct nds32_hi20 *n;
2810
4
  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
4
  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
4
  bfd_vma octet = (reloc_entry->address
2824
4
       * OCTETS_PER_BYTE (abfd, input_section));
2825
4
  if (!bfd_reloc_offset_in_range (reloc_entry->howto,
2826
4
          abfd, input_section, octet))
2827
2
    return bfd_reloc_outofrange;
2828
2829
2
  ret = bfd_reloc_ok;
2830
2
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2831
0
    ret = bfd_reloc_undefined;
2832
2833
2
  if (bfd_is_com_section (symbol->section))
2834
0
    relocation = 0;
2835
2
  else
2836
2
    relocation = symbol->value;
2837
2838
2
  relocation += symbol->section->output_section->vma;
2839
2
  relocation += symbol->section->output_offset;
2840
2
  relocation += reloc_entry->addend;
2841
2842
  /* Save the information, and let LO12 do the actual relocation.  */
2843
2
  n = bfd_malloc (sizeof (*n));
2844
2
  if (n == NULL)
2845
0
    return bfd_reloc_outofrange;
2846
2
  sdata = nds32_elf_section_data (input_section);
2847
2848
2
  n->addr = (bfd_byte *) data + reloc_entry->address;
2849
2
  n->addend = relocation;
2850
2
  n->next = sdata->nds32_hi20_list;
2851
2
  sdata->nds32_hi20_list = n;
2852
2853
2
  if (output_bfd != (bfd *) NULL)
2854
0
    reloc_entry->address += input_section->output_offset;
2855
2856
2
  return ret;
2857
2
}
2858
2859
/* Handle an NDS32 ELF HI20 reloc.  */
2860
2861
static void
2862
nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2863
       int type ATTRIBUTE_UNUSED,
2864
       Elf_Internal_Rela *relhi,
2865
       Elf_Internal_Rela *rello,
2866
       bfd_byte *contents,
2867
       bfd_vma addend)
2868
0
{
2869
0
  unsigned long insn;
2870
0
  bfd_vma addlo;
2871
2872
0
  insn = bfd_getb32 (contents + relhi->r_offset);
2873
2874
0
  addlo = bfd_getb32 (contents + rello->r_offset);
2875
0
  addlo &= 0xfff;
2876
2877
0
  addend += ((insn & 0xfffff) << 20) + addlo;
2878
2879
0
  insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2880
0
  bfd_putb32 (insn, contents + relhi->r_offset);
2881
0
}
2882
2883
/* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
2884
   inplace relocation; this function exists in order to do the
2885
   R_NDS32_HI20_[SU]LO relocation described above.  */
2886
2887
static bfd_reloc_status_type
2888
nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2889
          void *data, asection *input_section, bfd *output_bfd,
2890
          char **error_message)
2891
6
{
2892
6
  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
6
  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
6
  bfd_vma octet = (reloc_entry->address
2905
6
       * OCTETS_PER_BYTE (input_bfd, input_section));
2906
6
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, input_bfd, input_section,
2907
6
          octet))
2908
2
    return bfd_reloc_outofrange;
2909
2910
4
  sdata = nds32_elf_section_data (input_section);
2911
4
  if (sdata->nds32_hi20_list != NULL)
2912
0
    {
2913
0
      struct nds32_hi20 *l;
2914
2915
0
      l = sdata->nds32_hi20_list;
2916
0
      while (l != NULL)
2917
0
  {
2918
0
    unsigned long insn;
2919
0
    unsigned long val;
2920
0
    unsigned long vallo;
2921
0
    struct nds32_hi20 *next;
2922
2923
    /* Do the HI20 relocation.  Note that we actually don't need
2924
       to know anything about the LO12 itself, except where to
2925
       find the low 12 bits of the addend needed by the LO12.  */
2926
0
    insn = bfd_getb32 (l->addr);
2927
0
    vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2928
0
    vallo &= 0xfff;
2929
0
    switch (reloc_entry->howto->type)
2930
0
      {
2931
0
      case R_NDS32_LO12S3:
2932
0
        vallo <<= 3;
2933
0
        break;
2934
2935
0
      case R_NDS32_LO12S2:
2936
0
        vallo <<= 2;
2937
0
        break;
2938
2939
0
      case R_NDS32_LO12S1:
2940
0
        vallo <<= 1;
2941
0
        break;
2942
2943
0
      case R_NDS32_LO12S0:
2944
0
        vallo <<= 0;
2945
0
        break;
2946
0
      }
2947
2948
0
    val = ((insn & 0xfffff) << 12) + vallo;
2949
0
    val += l->addend;
2950
2951
0
    insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2952
0
    bfd_putb32 ((bfd_vma) insn, l->addr);
2953
2954
0
    next = l->next;
2955
0
    free (l);
2956
0
    l = next;
2957
0
  }
2958
2959
0
      sdata->nds32_hi20_list = NULL;
2960
0
    }
2961
2962
  /* Now do the LO12 reloc in the usual way.
2963
     ??? It would be nice to call bfd_elf_generic_reloc here,
2964
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2965
     pass the handling back to bfd_install_relocation which will install
2966
     a section relative addend which is wrong.  */
2967
4
  return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2968
4
          input_section, output_bfd, error_message);
2969
4
}
2970
2971
/* Do generic partial_inplace relocation.
2972
   This is a local replacement for bfd_elf_generic_reloc.  */
2973
2974
static bfd_reloc_status_type
2975
nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2976
       asymbol *symbol, void *data, asection *input_section,
2977
       bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2978
8
{
2979
8
  bfd_reloc_status_type ret;
2980
8
  bfd_vma relocation;
2981
8
  bfd_byte *inplace_address;
2982
2983
  /* This part is from bfd_elf_generic_reloc.
2984
     If we're relocating, and this an external symbol, we don't want
2985
     to change anything.  */
2986
8
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2987
0
      && reloc_entry->addend == 0)
2988
0
    {
2989
0
      reloc_entry->address += input_section->output_offset;
2990
0
      return bfd_reloc_ok;
2991
0
    }
2992
2993
  /* Now do the reloc in the usual way.
2994
     ??? It would be nice to call bfd_elf_generic_reloc here,
2995
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2996
     pass the handling back to bfd_install_relocation which will install
2997
     a section relative addend which is wrong.  */
2998
2999
  /* Sanity check the address (offset in section).  */
3000
8
  bfd_vma octet = (reloc_entry->address
3001
8
       * OCTETS_PER_BYTE (input_bfd, input_section));
3002
8
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, input_bfd, input_section,
3003
8
          octet))
3004
0
    return bfd_reloc_outofrange;
3005
3006
8
  ret = bfd_reloc_ok;
3007
8
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
3008
0
    ret = bfd_reloc_undefined;
3009
3010
8
  if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
3011
0
    relocation = 0;
3012
8
  else
3013
8
    relocation = symbol->value;
3014
3015
  /* Only do this for a final link.  */
3016
8
  if (output_bfd == (bfd *) NULL)
3017
8
    {
3018
8
      relocation += symbol->section->output_section->vma;
3019
8
      relocation += symbol->section->output_offset;
3020
8
    }
3021
3022
8
  relocation += reloc_entry->addend;
3023
8
  switch (reloc_entry->howto->type)
3024
8
    {
3025
1
    case R_NDS32_LO12S3:
3026
1
      relocation >>= 3;
3027
1
      break;
3028
3029
0
    case R_NDS32_LO12S2:
3030
0
      relocation >>= 2;
3031
0
      break;
3032
3033
0
    case R_NDS32_LO12S1:
3034
0
      relocation >>= 1;
3035
0
      break;
3036
3037
3
    case R_NDS32_LO12S0:
3038
7
    default:
3039
7
      relocation >>= 0;
3040
7
      break;
3041
8
    }
3042
3043
8
  inplace_address = (bfd_byte *) data + reloc_entry->address;
3044
3045
8
#define DOIT(x)           \
3046
8
  x = ((x & ~reloc_entry->howto->dst_mask) |    \
3047
8
  (((x & reloc_entry->howto->src_mask) +  relocation) & \
3048
8
  reloc_entry->howto->dst_mask))
3049
3050
8
  switch (bfd_get_reloc_size (reloc_entry->howto))
3051
8
    {
3052
2
    case 2:
3053
2
      {
3054
2
  short x = bfd_getb16 (inplace_address);
3055
3056
2
  DOIT (x);
3057
2
  bfd_putb16 ((bfd_vma) x, inplace_address);
3058
2
      }
3059
2
      break;
3060
6
    case 4:
3061
6
      {
3062
6
  unsigned long x = bfd_getb32 (inplace_address);
3063
3064
6
  DOIT (x);
3065
6
  bfd_putb32 ((bfd_vma) x, inplace_address);
3066
6
      }
3067
6
      break;
3068
0
    default:
3069
0
      BFD_ASSERT (0);
3070
8
    }
3071
3072
8
  if (output_bfd != (bfd *) NULL)
3073
0
    reloc_entry->address += input_section->output_offset;
3074
3075
8
  return ret;
3076
8
}
3077
3078
/* Handle the R_NDS32_SDA15 reloc.
3079
   This reloc is used to compute the address of objects in the small data area
3080
   and to perform loads and stores from that area.
3081
   The lower 15 bits are sign extended and added to the register specified
3082
   in the instruction, which is assumed to point to _SDA_BASE_.
3083
3084
   Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
3085
   the access size, this must be taken care of.  */
3086
3087
static bfd_reloc_status_type
3088
nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3089
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
3090
           asection *input_section, bfd *output_bfd,
3091
           char **error_message ATTRIBUTE_UNUSED)
3092
0
{
3093
  /* This part is from bfd_elf_generic_reloc.  */
3094
0
  if (output_bfd != (bfd *) NULL
3095
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3096
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3097
0
    {
3098
0
      reloc_entry->address += input_section->output_offset;
3099
0
      return bfd_reloc_ok;
3100
0
    }
3101
3102
0
  if (output_bfd != NULL)
3103
0
    {
3104
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
3105
0
      return bfd_reloc_continue;
3106
0
    }
3107
3108
  /* FIXME: not sure what to do here yet.  But then again, the linker
3109
     may never call us.  */
3110
0
  abort ();
3111
0
}
3112
3113
/* nds32_elf_ignore_reloc is the special function for
3114
   relocation types which don't need to be relocated
3115
   like relaxation relocation types.
3116
   This function simply return bfd_reloc_ok when it is
3117
   invoked.  */
3118
3119
static bfd_reloc_status_type
3120
nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3121
      asymbol *symbol ATTRIBUTE_UNUSED,
3122
      void *data ATTRIBUTE_UNUSED, asection *input_section,
3123
      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3124
8
{
3125
8
  if (output_bfd != NULL)
3126
0
    reloc_entry->address += input_section->output_offset;
3127
3128
8
  return bfd_reloc_ok;
3129
8
}
3130

3131
3132
/* Map BFD reloc types to NDS32 ELF reloc types.  */
3133
3134
struct nds32_reloc_map_entry
3135
{
3136
  bfd_reloc_code_real_type bfd_reloc_val;
3137
  unsigned char elf_reloc_val;
3138
};
3139
3140
static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3141
{
3142
  {BFD_RELOC_NONE, R_NDS32_NONE},
3143
  {BFD_RELOC_16, R_NDS32_16_RELA},
3144
  {BFD_RELOC_32, R_NDS32_32_RELA},
3145
  {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3146
  {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3147
3148
  {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3149
  {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3150
  {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3151
  {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3152
  {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3153
  {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3154
  {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3155
  {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3156
  {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3157
  {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3158
  {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3159
  {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3160
  {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3161
  {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3162
  {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3163
  {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3164
  {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3165
  {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3166
  {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3167
  {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3168
  {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3169
  {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3170
  {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3171
  {BFD_RELOC_COPY, R_NDS32_COPY},
3172
  {BFD_RELOC_GLOB_DAT, R_NDS32_GLOB_DAT},
3173
  {BFD_RELOC_JMP_SLOT, R_NDS32_JMP_SLOT},
3174
  {BFD_RELOC_RELATIVE, R_NDS32_RELATIVE},
3175
  {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3176
  {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3177
  {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3178
  {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3179
  {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3180
  {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3181
  {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3182
  {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3183
  {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3184
  {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3185
  {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3186
  {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3187
  {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3188
  {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3189
  {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3190
  {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3191
  {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3192
  {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3193
  {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3194
  {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3195
  {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3196
  {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3197
  {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3198
  {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3199
  {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3200
  {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3201
  {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3202
  {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3203
  {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3204
  {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3205
  {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3206
  {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3207
  {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3208
  {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3209
  {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3210
  {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3211
  {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3212
  {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3213
  {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3214
  {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3215
  {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3216
  {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3217
  {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3218
  {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3219
  {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3220
  {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3221
  {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3222
  {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3223
  {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3224
  {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3225
  {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3226
  {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3227
  {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3228
  {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3229
  {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3230
  {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3231
  {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3232
  {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3233
  {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3234
  {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3235
  {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3236
  {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3237
  {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3238
  {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3239
  {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3240
  {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3241
  {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3242
  {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3243
  {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3244
  {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3245
  {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3246
  {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3247
  {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3248
  {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3249
  {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3250
  /* Not sure.  */
3251
  {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3252
  /* Missing: BFD_RELOC_NDS32_GOTTPOFF.  */
3253
  {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3254
  {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3255
  {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3256
  {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3257
  {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3258
  {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3259
  {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3260
  {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3261
  {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3262
  {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3263
  {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3264
  {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3265
  {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3266
  {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3267
  {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3268
  {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3269
  {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3270
  {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3271
  {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3272
  {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3273
  {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3274
  {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3275
  {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3276
  {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3277
  {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3278
  {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3279
  {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3280
};
3281
3282
/* Patch tag.  */
3283
3284
static reloc_howto_type *
3285
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3286
         const char *r_name)
3287
0
{
3288
0
  unsigned int i;
3289
3290
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3291
0
    if (nds32_elf_howto_table[i].name != NULL
3292
0
  && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3293
0
      return &nds32_elf_howto_table[i];
3294
3295
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3296
0
    if (nds32_elf_relax_howto_table[i].name != NULL
3297
0
  && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3298
0
      return &nds32_elf_relax_howto_table[i];
3299
3300
0
  return NULL;
3301
0
}
3302
3303
static reloc_howto_type *
3304
bfd_elf32_bfd_reloc_type_table_lookup (unsigned int code)
3305
1.51k
{
3306
1.51k
  if (code < R_NDS32_RELAX_ENTRY)
3307
1.45k
    {
3308
1.45k
      if (code < ARRAY_SIZE (nds32_elf_howto_table))
3309
1.44k
  return &nds32_elf_howto_table[code];
3310
1.45k
    }
3311
67
  else
3312
67
    {
3313
67
      if (code - R_NDS32_RELAX_ENTRY < ARRAY_SIZE (nds32_elf_relax_howto_table))
3314
60
  return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3315
67
    }
3316
12
  return NULL;
3317
1.51k
}
3318
3319
static reloc_howto_type *
3320
bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3321
         bfd_reloc_code_real_type code)
3322
0
{
3323
0
  unsigned int i;
3324
3325
0
  for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3326
0
    {
3327
0
      if (nds32_reloc_map[i].bfd_reloc_val == code)
3328
0
  return bfd_elf32_bfd_reloc_type_table_lookup
3329
0
    (nds32_reloc_map[i].elf_reloc_val);
3330
0
    }
3331
3332
0
  return NULL;
3333
0
}
3334
3335
/* Set the howto pointer for an NDS32 ELF reloc.  */
3336
3337
static bool
3338
nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3339
       Elf_Internal_Rela *dst)
3340
91
{
3341
91
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3342
3343
91
  cache_ptr->howto = NULL;
3344
91
  if (r_type <= R_NDS32_GNU_VTENTRY)
3345
81
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3346
91
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3347
10
    {
3348
      /* xgettext:c-format */
3349
10
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3350
10
        abfd, r_type);
3351
10
      bfd_set_error (bfd_error_bad_value);
3352
10
      return false;
3353
10
    }
3354
81
  return true;
3355
91
}
3356
3357
static bool
3358
nds32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3359
         Elf_Internal_Rela *dst)
3360
1.46k
{
3361
1.46k
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3362
3363
1.46k
  cache_ptr->howto = NULL;
3364
1.46k
  if (r_type == R_NDS32_NONE
3365
371
      || r_type > R_NDS32_GNU_VTENTRY)
3366
1.43k
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3367
1.46k
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3368
38
    {
3369
      /* xgettext:c-format */
3370
38
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3371
38
        abfd, r_type);
3372
38
      bfd_set_error (bfd_error_bad_value);
3373
38
      return false;
3374
38
    }
3375
1.42k
  return true;
3376
1.46k
}
3377
3378
/* Support for core dump NOTE sections.
3379
   Reference to include/linux/elfcore.h in Linux.  */
3380
3381
static bool
3382
nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3383
0
{
3384
0
  int offset;
3385
0
  size_t size;
3386
3387
0
  switch (note->descsz)
3388
0
    {
3389
0
    case 0x114:
3390
      /* Linux/NDS32 32-bit, ABI1.  */
3391
3392
      /* pr_cursig */
3393
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3394
3395
      /* pr_pid */
3396
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3397
3398
      /* pr_reg */
3399
0
      offset = 72;
3400
0
      size = 200;
3401
0
      break;
3402
3403
0
    case 0xfc:
3404
      /* Linux/NDS32 32-bit.  */
3405
3406
      /* pr_cursig */
3407
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3408
3409
      /* pr_pid */
3410
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3411
3412
      /* pr_reg */
3413
0
      offset = 72;
3414
0
      size = 176;
3415
0
      break;
3416
3417
0
    default:
3418
0
      return false;
3419
0
    }
3420
3421
  /* Make a ".reg" section.  */
3422
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3423
0
            size, note->descpos + offset);
3424
0
}
3425
3426
static bool
3427
nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3428
0
{
3429
0
  switch (note->descsz)
3430
0
    {
3431
0
    case 124:
3432
      /* Linux/NDS32.  */
3433
3434
      /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
3435
0
      elf_tdata (abfd)->core->program =
3436
0
  _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3437
0
      elf_tdata (abfd)->core->command =
3438
0
  _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3439
0
      break;
3440
3441
0
    default:
3442
0
      return false;
3443
0
    }
3444
3445
  /* Note that for some reason, a spurious space is tacked
3446
     onto the end of the args in some (at least one anyway)
3447
     implementations, so strip it off if it exists.  */
3448
0
  {
3449
0
    char *command = elf_tdata (abfd)->core->command;
3450
0
    int n = strlen (command);
3451
3452
0
    if (0 < n && command[n - 1] == ' ')
3453
0
      command[n - 1] = '\0';
3454
0
  }
3455
3456
0
  return true;
3457
0
}
3458
3459
/* Hook called by the linker routine which adds symbols from an object
3460
   file.  We must handle the special NDS32 section numbers here.
3461
   We also keep watching for whether we need to create the sdata special
3462
   linker sections.  */
3463
3464
static bool
3465
nds32_elf_add_symbol_hook (bfd *abfd,
3466
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
3467
         Elf_Internal_Sym *sym,
3468
         const char **namep ATTRIBUTE_UNUSED,
3469
         flagword *flagsp ATTRIBUTE_UNUSED,
3470
         asection **secp, bfd_vma *valp)
3471
0
{
3472
0
  switch (sym->st_shndx)
3473
0
    {
3474
0
    case SHN_COMMON:
3475
      /* Common symbols less than the GP size are automatically
3476
   treated as SHN_MIPS_SCOMMON symbols.  */
3477
0
      if (sym->st_size > elf_gp_size (abfd)
3478
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3479
0
  break;
3480
3481
      /* st_value is the alignment constraint.
3482
   That might be its actual size if it is an array or structure.  */
3483
0
      switch (sym->st_value)
3484
0
  {
3485
0
  case 1:
3486
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3487
0
    break;
3488
0
  case 2:
3489
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3490
0
    break;
3491
0
  case 4:
3492
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3493
0
    break;
3494
0
  case 8:
3495
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3496
0
    break;
3497
0
  default:
3498
0
    return true;
3499
0
  }
3500
3501
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
3502
0
      *valp = sym->st_size;
3503
0
      break;
3504
0
    }
3505
3506
0
  return true;
3507
0
}
3508
3509
/* This function can figure out the best location for a base register to access
3510
   data relative to this base register
3511
   INPUT:
3512
   sda_d0: size of first DOUBLE WORD data section
3513
   sda_w0: size of first WORD data section
3514
   sda_h0: size of first HALF WORD data section
3515
   sda_b : size of BYTE data section
3516
   sda_hi: size of second HALF WORD data section
3517
   sda_w1: size of second WORD data section
3518
   sda_d1: size of second DOUBLE WORD data section
3519
   OUTPUT:
3520
   offset (always positive) from the beginning of sda_d0 if OK
3521
   a negative error value if fail
3522
   NOTE:
3523
   these 7 sections have to be located back to back if exist
3524
   a pass in 0 value for non-existing section   */
3525
3526
/* Due to the interpretation of simm15 field of load/store depending on
3527
   data accessing size, the organization of base register relative data shall
3528
   like the following figure
3529
   -------------------------------------------
3530
   |  DOUBLE WORD sized data (range +/- 128K)
3531
   -------------------------------------------
3532
   |  WORD sized data (range +/- 64K)
3533
   -------------------------------------------
3534
   |  HALF WORD sized data (range +/- 32K)
3535
   -------------------------------------------
3536
   |  BYTE sized data (range +/- 16K)
3537
   -------------------------------------------
3538
   |  HALF WORD sized data (range +/- 32K)
3539
   -------------------------------------------
3540
   |  WORD sized data (range +/- 64K)
3541
   -------------------------------------------
3542
   |  DOUBLE WORD sized data (range +/- 128K)
3543
   -------------------------------------------
3544
   Its base register shall be set to access these data freely.  */
3545
3546
/* We have to figure out the SDA_BASE value, so that we can adjust the
3547
   symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3548
   BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3549
   target data.  We don't need to adjust the symbol value for an
3550
   external symbol if we are producing relocatable output.  */
3551
3552
static asection *sda_rela_sec = NULL;
3553
3554
#define SDA_SECTION_NUM 10
3555
3556
static bfd_reloc_status_type
3557
nds32_elf_final_sda_base (bfd *output_bfd,
3558
        struct bfd_link_info *info,
3559
        bfd_vma *psb,
3560
        bool add_symbol)
3561
0
{
3562
0
  int relax_fp_as_gp;
3563
0
  struct elf_nds32_link_hash_table *table;
3564
0
  struct bfd_link_hash_entry *h, *h2;
3565
0
  long unsigned int total = 0;
3566
0
  asection *first = NULL, *final = NULL, *temp;
3567
0
  bfd_vma sda_base = 0;
3568
3569
0
  h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
3570
0
  if (!h || (h->type != bfd_link_hash_defined
3571
0
       && h->type != bfd_link_hash_defweak))
3572
0
    {
3573
      /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3574
   4 byte-aligned.  Therefore, it has to set the first section ".data"
3575
   4 byte-aligned.  */
3576
0
      static const char sec_name[SDA_SECTION_NUM][10] =
3577
0
  {
3578
0
    ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3579
0
    ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3580
0
  };
3581
0
      size_t i = 0;
3582
3583
0
      if (output_bfd->sections == NULL)
3584
0
  {
3585
0
    *psb = elf_gp (output_bfd);
3586
0
    return bfd_reloc_ok;
3587
0
  }
3588
3589
      /* Get the first and final section.  */
3590
0
      while (i < ARRAY_SIZE (sec_name))
3591
0
  {
3592
0
    temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3593
0
    if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3594
0
      first = temp;
3595
0
    if (temp && (temp->size != 0 || temp->rawsize != 0))
3596
0
      final = temp;
3597
3598
    /* Summarize the sections in order to check if joining .bss.  */
3599
0
    if (temp && temp->size != 0)
3600
0
      total += temp->size;
3601
0
    else if (temp && temp->rawsize != 0)
3602
0
      total += temp->rawsize;
3603
3604
0
    i++;
3605
0
  }
3606
3607
      /* Check .bss size.  */
3608
0
      temp = bfd_get_section_by_name (output_bfd, ".bss");
3609
0
      if (temp)
3610
0
  {
3611
0
    if (temp->size != 0)
3612
0
      total += temp->size;
3613
0
    else if (temp->rawsize != 0)
3614
0
      total += temp->rawsize;
3615
3616
0
    if (total < 0x80000)
3617
0
      {
3618
0
        if (!first && (temp->size != 0 || temp->rawsize != 0))
3619
0
    first = temp;
3620
0
        if ((temp->size != 0 || temp->rawsize != 0))
3621
0
    final = temp;
3622
0
      }
3623
0
  }
3624
3625
0
      if (first && final)
3626
0
  {
3627
    /* The middle of data region.  */
3628
0
    sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3629
3630
    /* Find the section sda_base located.  */
3631
0
    i = 0;
3632
0
    while (i < ARRAY_SIZE (sec_name))
3633
0
      {
3634
0
        final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3635
0
        if (final && (final->size != 0 || final->rawsize != 0)
3636
0
      && sda_base >= final->vma)
3637
0
    {
3638
0
      first = final;
3639
0
      i++;
3640
0
    }
3641
0
        else
3642
0
    break;
3643
0
      }
3644
0
  }
3645
0
      else
3646
0
  {
3647
    /* If there is not any default data section in output bfd, try to find
3648
       the first data section.  If no data section be found, just simplily
3649
       choose the first output section.  */
3650
0
    temp = output_bfd->sections;
3651
0
    while (temp)
3652
0
      {
3653
0
        if (temp->flags & SEC_ALLOC
3654
0
      && (((temp->flags & SEC_DATA)
3655
0
           && ((temp->flags & SEC_READONLY) == 0))
3656
0
          || (temp->flags & SEC_LOAD) == 0)
3657
0
      && (temp->size != 0 || temp->rawsize != 0))
3658
0
    {
3659
0
      if (!first)
3660
0
        first = temp;
3661
0
      final = temp;
3662
0
    }
3663
0
        temp = temp->next;
3664
0
      }
3665
3666
    /* There is no data or bss section.  */
3667
0
    if (!first || (first->size == 0 && first->rawsize == 0))
3668
0
      {
3669
0
        first = output_bfd->sections;
3670
0
        while (first && first->size == 0 && first->rawsize == 0)
3671
0
    first = first->next;
3672
0
      }
3673
3674
    /* There is no concrete section.  */
3675
0
    if (!first)
3676
0
      {
3677
0
        *psb = elf_gp (output_bfd);
3678
0
        return bfd_reloc_ok;
3679
0
      }
3680
3681
0
    if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3682
0
      sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3683
0
    else
3684
0
      sda_base = first->vma + 0x2000;
3685
0
  }
3686
3687
0
      sda_base -= first->vma;
3688
0
      sda_base = sda_base & (~7);
3689
3690
0
      if (!_bfd_generic_link_add_one_symbol
3691
0
    (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3692
0
     (bfd_vma) sda_base, (const char *) NULL, false,
3693
0
     get_elf_backend_data (output_bfd)->collect, &h))
3694
0
  return false;
3695
3696
0
      sda_rela_sec = first;
3697
0
    }
3698
3699
  /* Set _FP_BASE_ to _SDA_BASE_.  */
3700
0
  table = nds32_elf_hash_table (info);
3701
0
  relax_fp_as_gp = table->relax_fp_as_gp;
3702
0
  h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, false, false, false);
3703
  /* _SDA_BASE_ is difined in linker script.  */
3704
0
  if (!first)
3705
0
    {
3706
0
      first = h->u.def.section;
3707
0
      sda_base = h->u.def.value;
3708
0
    }
3709
3710
0
  if (relax_fp_as_gp && h2
3711
0
      && (h2->type == bfd_link_hash_undefweak
3712
0
    || h2->type == bfd_link_hash_undefined))
3713
0
    {
3714
      /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3715
   And set FP equal to SDA_BASE to do relaxation for
3716
   la $fp, _FP_BASE_.  */
3717
0
      if (!_bfd_generic_link_add_one_symbol
3718
0
    (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3719
0
     first, sda_base, (const char *) NULL,
3720
0
     false, get_elf_backend_data (output_bfd)->collect, &h2))
3721
0
  return false;
3722
0
    }
3723
3724
0
  if (add_symbol)
3725
0
    {
3726
0
      if (h)
3727
0
  {
3728
    /* Now set gp.  */
3729
0
    elf_gp (output_bfd) = (h->u.def.value
3730
0
         + h->u.def.section->output_section->vma
3731
0
         + h->u.def.section->output_offset);
3732
0
  }
3733
0
      else
3734
0
  {
3735
0
    _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3736
0
    return bfd_reloc_dangerous;
3737
0
  }
3738
0
    }
3739
3740
0
  *psb = h->u.def.value
3741
0
    + h->u.def.section->output_section->vma
3742
0
    + h->u.def.section->output_offset;
3743
0
  return bfd_reloc_ok;
3744
0
}
3745

3746
3747
/* Return size of a PLT entry.  */
3748
#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3749
3750
/* Create an entry in an nds32 ELF linker hash table.  */
3751
3752
static struct bfd_hash_entry *
3753
nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3754
           struct bfd_hash_table *table,
3755
           const char *string)
3756
0
{
3757
0
  struct elf_nds32_link_hash_entry *ret;
3758
3759
0
  ret = (struct elf_nds32_link_hash_entry *) entry;
3760
3761
  /* Allocate the structure if it has not already been allocated by a
3762
     subclass.  */
3763
0
  if (ret == NULL)
3764
0
    ret = (struct elf_nds32_link_hash_entry *)
3765
0
       bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3766
3767
0
  if (ret == NULL)
3768
0
    return (struct bfd_hash_entry *) ret;
3769
3770
  /* Call the allocation method of the superclass.  */
3771
0
  ret = (struct elf_nds32_link_hash_entry *)
3772
0
    _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3773
3774
0
  if (ret != NULL)
3775
0
    {
3776
0
      struct elf_nds32_link_hash_entry *eh;
3777
3778
0
      eh = (struct elf_nds32_link_hash_entry *) ret;
3779
0
      eh->tls_type = GOT_UNKNOWN;
3780
0
      eh->offset_to_gp = 0;
3781
0
    }
3782
3783
0
  return (struct bfd_hash_entry *) ret;
3784
0
}
3785
3786
/* Create an nds32 ELF linker hash table.  */
3787
3788
static struct bfd_link_hash_table *
3789
nds32_elf_link_hash_table_create (bfd *abfd)
3790
0
{
3791
0
  struct elf_nds32_link_hash_table *ret;
3792
3793
0
  size_t amt = sizeof (struct elf_nds32_link_hash_table);
3794
3795
0
  ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3796
0
  if (ret == NULL)
3797
0
    return NULL;
3798
3799
  /* Patch tag.  */
3800
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3801
0
              nds32_elf_link_hash_newfunc,
3802
0
              sizeof (struct elf_nds32_link_hash_entry)))
3803
0
    {
3804
0
      free (ret);
3805
0
      return NULL;
3806
0
    }
3807
3808
0
  ret->sym_ld_script = NULL;
3809
3810
0
  return &ret->root.root;
3811
0
}
3812
3813
/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3814
   shortcuts to them in our hash table.  */
3815
3816
static bool
3817
create_got_section (bfd *dynobj, struct bfd_link_info *info)
3818
0
{
3819
0
  struct elf_link_hash_table *ehtab;
3820
3821
0
  if (!_bfd_elf_create_got_section (dynobj, info))
3822
0
    return false;
3823
3824
0
  ehtab = elf_hash_table (info);
3825
0
  ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3826
0
  ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3827
0
  if (!ehtab->sgot || !ehtab->sgotplt)
3828
0
    abort ();
3829
3830
  /* _bfd_elf_create_got_section will create it for us.  */
3831
0
  ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3832
0
  if (ehtab->srelgot == NULL
3833
0
      || !bfd_set_section_flags (ehtab->srelgot,
3834
0
         (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3835
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED
3836
0
          | SEC_READONLY))
3837
0
      || !bfd_set_section_alignment (ehtab->srelgot, 2))
3838
0
    return false;
3839
3840
0
  return true;
3841
0
}
3842
3843
/* Create dynamic sections when linking against a dynamic object.  */
3844
3845
static bool
3846
nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3847
0
{
3848
0
  struct elf_link_hash_table *ehtab;
3849
0
  struct elf_nds32_link_hash_table *htab;
3850
0
  flagword flags, pltflags;
3851
0
  register asection *s;
3852
0
  elf_backend_data *bed;
3853
0
  int ptralign = 2;   /* 32-bit  */
3854
0
  const char *secname;
3855
0
  char *relname;
3856
0
  flagword secflags;
3857
0
  asection *sec;
3858
3859
0
  bed = get_elf_backend_data (abfd);
3860
0
  ehtab = elf_hash_table (info);
3861
0
  htab = nds32_elf_hash_table (info);
3862
3863
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3864
     .rel[a].bss sections.  */
3865
3866
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3867
0
     | SEC_LINKER_CREATED);
3868
3869
0
  pltflags = flags;
3870
0
  pltflags |= SEC_CODE;
3871
0
  if (bed->plt_not_loaded)
3872
0
    pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3873
0
  if (bed->plt_readonly)
3874
0
    pltflags |= SEC_READONLY;
3875
3876
0
  s = bfd_make_section (abfd, ".plt");
3877
0
  ehtab->splt = s;
3878
0
  if (s == NULL
3879
0
      || !bfd_set_section_flags (s, pltflags)
3880
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
3881
0
    return false;
3882
3883
0
  if (bed->want_plt_sym)
3884
0
    {
3885
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3886
   .plt section.  */
3887
0
      struct bfd_link_hash_entry *bh = NULL;
3888
0
      struct elf_link_hash_entry *h;
3889
3890
0
      if (!(_bfd_generic_link_add_one_symbol
3891
0
      (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3892
0
       (bfd_vma) 0, (const char *) NULL, false,
3893
0
       get_elf_backend_data (abfd)->collect, &bh)))
3894
0
  return false;
3895
3896
0
      h = (struct elf_link_hash_entry *) bh;
3897
0
      h->def_regular = 1;
3898
0
      h->type = STT_OBJECT;
3899
3900
0
      if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3901
0
  return false;
3902
0
    }
3903
3904
0
  s = bfd_make_section (abfd,
3905
0
      bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3906
0
  ehtab->srelplt = s;
3907
0
  if (s == NULL
3908
0
      || !bfd_set_section_flags (s, flags | SEC_READONLY)
3909
0
      || !bfd_set_section_alignment (s, ptralign))
3910
0
    return false;
3911
3912
0
  if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3913
0
    return false;
3914
3915
0
  for (sec = abfd->sections; sec; sec = sec->next)
3916
0
    {
3917
0
      secflags = bfd_section_flags (sec);
3918
0
      if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3919
0
    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3920
0
  continue;
3921
0
      secname = bfd_section_name (sec);
3922
0
      relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3923
0
      strcpy (relname, ".rela");
3924
0
      strcat (relname, secname);
3925
0
      if (bfd_get_section_by_name (abfd, secname))
3926
0
  continue;
3927
0
      s = bfd_make_section (abfd, relname);
3928
0
      if (s == NULL
3929
0
    || !bfd_set_section_flags (s, flags | SEC_READONLY)
3930
0
    || !bfd_set_section_alignment (s, ptralign))
3931
0
  return false;
3932
0
    }
3933
3934
0
  if (bed->want_dynbss)
3935
0
    {
3936
      /* The .dynbss section is a place to put symbols which are defined
3937
   by dynamic objects, are referenced by regular objects, and are
3938
   not functions.  We must allocate space for them in the process
3939
   image and use a R_*_COPY reloc to tell the dynamic linker to
3940
   initialize them at run time.  The linker script puts the .dynbss
3941
   section into the .bss section of the final image.  */
3942
0
      s = bfd_make_section (abfd, ".dynbss");
3943
0
      htab->root.sdynbss = s;
3944
0
      if (s == NULL
3945
0
    || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3946
0
  return false;
3947
      /* The .rel[a].bss section holds copy relocs.  This section is not
3948
   normally needed.  We need to create it here, though, so that the
3949
   linker will map it to an output section.  We can't just create it
3950
   only if we need it, because we will not know whether we need it
3951
   until we have seen all the input files, and the first time the
3952
   main linker code calls BFD after examining all the input files
3953
   (size_dynamic_sections) the input sections have already been
3954
   mapped to the output sections.  If the section turns out not to
3955
   be needed, we can discard it later.  We will never need this
3956
   section when generating a shared object, since they do not use
3957
   copy relocs.  */
3958
0
      if (!bfd_link_pic (info))
3959
0
  {
3960
0
    s = bfd_make_section (abfd, (bed->default_use_rela_p
3961
0
               ? ".rela.bss" : ".rel.bss"));
3962
0
    htab->root.srelbss = s;
3963
0
    if (s == NULL
3964
0
        || !bfd_set_section_flags (s, flags | SEC_READONLY)
3965
0
        || !bfd_set_section_alignment (s, ptralign))
3966
0
      return false;
3967
0
  }
3968
0
    }
3969
3970
0
  return true;
3971
0
}
3972
3973
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3974
static void
3975
nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3976
        struct elf_link_hash_entry *dir,
3977
        struct elf_link_hash_entry *ind)
3978
0
{
3979
0
  struct elf_nds32_link_hash_entry *edir, *eind;
3980
3981
0
  edir = (struct elf_nds32_link_hash_entry *) dir;
3982
0
  eind = (struct elf_nds32_link_hash_entry *) ind;
3983
3984
0
  if (ind->root.type == bfd_link_hash_indirect)
3985
0
    {
3986
0
      if (dir->got.refcount <= 0)
3987
0
  {
3988
0
    edir->tls_type = eind->tls_type;
3989
0
    eind->tls_type = GOT_UNKNOWN;
3990
0
  }
3991
0
    }
3992
3993
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3994
0
}
3995

3996
/* Adjust a symbol defined by a dynamic object and referenced by a
3997
   regular object.  The current definition is in some section of the
3998
   dynamic object, but we're not including those sections.  We have to
3999
   change the definition to something the rest of the link can
4000
   understand.  */
4001
4002
static bool
4003
nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4004
         struct elf_link_hash_entry *h)
4005
0
{
4006
0
  struct elf_nds32_link_hash_table *htab;
4007
0
  bfd *dynobj;
4008
0
  asection *s;
4009
0
  unsigned int power_of_two;
4010
4011
0
  dynobj = elf_hash_table (info)->dynobj;
4012
4013
  /* Make sure we know what is going on here.  */
4014
0
  BFD_ASSERT (dynobj != NULL
4015
0
        && (h->needs_plt
4016
0
      || h->is_weakalias
4017
0
      || (h->def_dynamic && h->ref_regular && !h->def_regular)));
4018
4019
4020
  /* If this is a function, put it in the procedure linkage table.  We
4021
     will fill in the contents of the procedure linkage table later,
4022
     when we know the address of the .got section.  */
4023
0
  if (h->type == STT_FUNC || h->needs_plt)
4024
0
    {
4025
0
      if (!bfd_link_pic (info)
4026
0
    && !h->def_dynamic
4027
0
    && !h->ref_dynamic
4028
0
    && h->root.type != bfd_link_hash_undefweak
4029
0
    && h->root.type != bfd_link_hash_undefined)
4030
0
  {
4031
    /* This case can occur if we saw a PLT reloc in an input
4032
       file, but the symbol was never referred to by a dynamic
4033
       object.  In such a case, we don't actually need to build
4034
       a procedure linkage table, and we can just do a PCREL
4035
       reloc instead.  */
4036
0
    h->plt.offset = (bfd_vma) - 1;
4037
0
    h->needs_plt = 0;
4038
0
  }
4039
4040
0
      return true;
4041
0
    }
4042
0
  else
4043
0
    h->plt.offset = (bfd_vma) - 1;
4044
4045
  /* If this is a weak symbol, and there is a real definition, the
4046
     processor independent code will have arranged for us to see the
4047
     real definition first, and we can just use the same value.  */
4048
0
  if (h->is_weakalias)
4049
0
    {
4050
0
      struct elf_link_hash_entry *def = weakdef (h);
4051
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4052
0
      h->root.u.def.section = def->root.u.def.section;
4053
0
      h->root.u.def.value = def->root.u.def.value;
4054
0
      return true;
4055
0
    }
4056
4057
  /* This is a reference to a symbol defined by a dynamic object which
4058
     is not a function.  */
4059
4060
  /* If we are creating a shared library, we must presume that the
4061
     only references to the symbol are via the global offset table.
4062
     For such cases we need not do anything here; the relocations will
4063
     be handled correctly by relocate_section.  */
4064
0
  if (bfd_link_pic (info))
4065
0
    return true;
4066
4067
  /* If there are no references to this symbol that do not use the
4068
     GOT, we don't need to generate a copy reloc.  */
4069
0
  if (!h->non_got_ref)
4070
0
    return true;
4071
4072
  /* If -z nocopyreloc was given, we won't generate them either.  */
4073
0
  if (0 && info->nocopyreloc)
4074
0
    {
4075
0
      h->non_got_ref = 0;
4076
0
      return true;
4077
0
    }
4078
4079
  /* If we don't find any dynamic relocs in read-only sections, then
4080
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4081
0
  if (!_bfd_elf_readonly_dynrelocs (h))
4082
0
    {
4083
0
      h->non_got_ref = 0;
4084
0
      return true;
4085
0
    }
4086
4087
  /* We must allocate the symbol in our .dynbss section, which will
4088
     become part of the .bss section of the executable.  There will be
4089
     an entry for this symbol in the .dynsym section.  The dynamic
4090
     object will contain position independent code, so all references
4091
     from the dynamic object to this symbol will go through the global
4092
     offset table.  The dynamic linker will use the .dynsym entry to
4093
     determine the address it must put in the global offset table, so
4094
     both the dynamic object and the regular object will refer to the
4095
     same memory location for the variable.  */
4096
4097
0
  htab = nds32_elf_hash_table (info);
4098
0
  s = htab->root.sdynbss;
4099
0
  BFD_ASSERT (s != NULL);
4100
4101
  /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4102
     to copy the initial value out of the dynamic object and into the
4103
     runtime process image.  We need to remember the offset into the
4104
     .rela.bss section we are going to use.  */
4105
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4106
0
    {
4107
0
      asection *srel;
4108
4109
0
      srel = htab->root.srelbss;
4110
0
      BFD_ASSERT (srel != NULL);
4111
0
      srel->size += sizeof (Elf32_External_Rela);
4112
0
      h->needs_copy = 1;
4113
0
    }
4114
4115
  /* We need to figure out the alignment required for this symbol.  I
4116
     have no idea how ELF linkers handle this.  */
4117
0
  power_of_two = bfd_log2 (h->size);
4118
0
  if (power_of_two > 3)
4119
0
    power_of_two = 3;
4120
4121
  /* Apply the required alignment.  */
4122
0
  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4123
0
  if (!bfd_link_align_section (s, power_of_two))
4124
0
    return false;
4125
4126
  /* Define the symbol as being at this point in the section.  */
4127
0
  h->root.u.def.section = s;
4128
0
  h->root.u.def.value = s->size;
4129
4130
  /* Increment the section size to make room for the symbol.  */
4131
0
  s->size += h->size;
4132
4133
0
  return true;
4134
0
}
4135
4136
/* Allocate space in .plt, .got and associated reloc sections for
4137
   dynamic relocs.  */
4138
4139
static bool
4140
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4141
0
{
4142
0
  struct bfd_link_info *info;
4143
0
  struct elf_link_hash_table *ehtab;
4144
0
  struct elf_nds32_link_hash_table *htab;
4145
0
  struct elf_dyn_relocs *p;
4146
4147
0
  if (h->root.type == bfd_link_hash_indirect)
4148
0
    return true;
4149
4150
  /* When warning symbols are created, they **replace** the "real"
4151
     entry in the hash table, thus we never get to see the real
4152
     symbol in a hash traversal. So look at it now.  */
4153
0
  if (h->root.type == bfd_link_hash_warning)
4154
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4155
4156
0
  info = (struct bfd_link_info *) inf;
4157
0
  ehtab = elf_hash_table (info);
4158
0
  htab = nds32_elf_hash_table (info);
4159
0
  if (htab == NULL)
4160
0
    return false;
4161
4162
0
  if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4163
0
      && h->plt.refcount > 0
4164
0
      && !(bfd_link_pie (info) && h->def_regular))
4165
0
    {
4166
      /* Make sure this symbol is output as a dynamic symbol.
4167
   Undefined weak syms won't yet be marked as dynamic.  */
4168
0
      if (h->dynindx == -1 && !h->forced_local)
4169
0
  {
4170
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4171
0
      return false;
4172
0
  }
4173
4174
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4175
0
  {
4176
0
    asection *s = ehtab->splt;
4177
4178
    /* If this is the first .plt entry, make room for the special
4179
       first entry.  */
4180
0
    if (s->size == 0)
4181
0
      s->size += PLT_ENTRY_SIZE;
4182
4183
0
    h->plt.offset = s->size;
4184
4185
    /* If this symbol is not defined in a regular file, and we are
4186
       not generating a shared library, then set the symbol to this
4187
       location in the .plt.  This is required to make function
4188
       pointers compare as equal between the normal executable and
4189
       the shared library.  */
4190
0
    if (!bfd_link_pic (info) && !h->def_regular)
4191
0
      {
4192
0
        h->root.u.def.section = s;
4193
0
        h->root.u.def.value = h->plt.offset;
4194
0
      }
4195
4196
    /* Make room for this entry.  */
4197
0
    s->size += PLT_ENTRY_SIZE;
4198
4199
    /* We also need to make an entry in the .got.plt section, which
4200
       will be placed in the .got section by the linker script.  */
4201
0
    ehtab->sgotplt->size += 4;
4202
4203
    /* We also need to make an entry in the .rel.plt section.  */
4204
0
    ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4205
0
    if (htab->tls_desc_trampoline)
4206
0
      htab->next_tls_desc_index++;
4207
0
  }
4208
0
      else
4209
0
  {
4210
0
    h->plt.offset = (bfd_vma) - 1;
4211
0
    h->needs_plt = 0;
4212
0
  }
4213
0
    }
4214
0
  else
4215
0
    {
4216
0
      h->plt.offset = (bfd_vma) - 1;
4217
0
      h->needs_plt = 0;
4218
0
    }
4219
4220
0
  if (h->got.refcount > 0)
4221
0
    {
4222
0
      asection *sgot;
4223
0
      bool dyn;
4224
0
      int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4225
4226
      /* Make sure this symbol is output as a dynamic symbol.
4227
   Undefined weak syms won't yet be marked as dynamic.  */
4228
0
      if (h->dynindx == -1 && !h->forced_local)
4229
0
  {
4230
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4231
0
      return false;
4232
0
  }
4233
4234
0
      sgot = elf_hash_table (info)->sgot;
4235
0
      h->got.offset = sgot->size;
4236
4237
0
      if (tls_type == GOT_UNKNOWN)
4238
0
  abort ();
4239
4240
      /* Non-TLS symbols, and TLS_IE need one GOT slot.  */
4241
0
      if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4242
0
  sgot->size += 4;
4243
0
      else
4244
0
  {
4245
    /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots.  */
4246
0
    if (tls_type & GOT_TLS_DESC)
4247
0
      sgot->size += 8;
4248
0
  }
4249
4250
0
      dyn = htab->root.dynamic_sections_created;
4251
4252
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4253
0
  {
4254
0
    if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4255
0
      {
4256
        /* TLS_DESC with trampoline needs a relocation slot
4257
     within .rela.plt.  */
4258
0
        htab->num_tls_desc++;
4259
0
        ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4260
0
        htab->tls_trampoline = -1;
4261
0
      }
4262
0
    else
4263
0
      {
4264
        /* other relocations, including TLS_DESC without trampoline, need
4265
     a relocation slot within .rela.got.  */
4266
0
        ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4267
0
      }
4268
0
  }
4269
0
    }
4270
0
  else
4271
0
    h->got.offset = (bfd_vma)-1;
4272
4273
0
  if (h->dyn_relocs == NULL)
4274
0
    return true;
4275
4276
  /* In the shared -Bsymbolic case, discard space allocated for
4277
     dynamic pc-relative relocs against symbols which turn out to be
4278
     defined in regular objects.  For the normal shared case, discard
4279
     space for pc-relative relocs that have become local due to symbol
4280
     visibility changes.  */
4281
4282
0
  if (bfd_link_pic (info))
4283
0
    {
4284
0
      if (h->def_regular && (h->forced_local || info->symbolic))
4285
0
  {
4286
0
    struct elf_dyn_relocs **pp;
4287
4288
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4289
0
      {
4290
0
        p->count -= p->pc_count;
4291
0
        p->pc_count = 0;
4292
0
        if (p->count == 0)
4293
0
    *pp = p->next;
4294
0
        else
4295
0
    pp = &p->next;
4296
0
      }
4297
0
  }
4298
0
    }
4299
0
  else
4300
0
    {
4301
      /* For the non-shared case, discard space for relocs against
4302
   symbols which turn out to need copy relocs or are not dynamic.  */
4303
4304
0
      if (!h->non_got_ref
4305
0
    && ((h->def_dynamic
4306
0
         && !h->def_regular)
4307
0
        || (htab->root.dynamic_sections_created
4308
0
      && (h->root.type == bfd_link_hash_undefweak
4309
0
          || h->root.type == bfd_link_hash_undefined))))
4310
0
  {
4311
    /* Make sure this symbol is output as a dynamic symbol.
4312
       Undefined weak syms won't yet be marked as dynamic.  */
4313
0
    if (h->dynindx == -1 && !h->forced_local)
4314
0
      {
4315
0
        if (!bfd_elf_link_record_dynamic_symbol (info, h))
4316
0
    return false;
4317
0
      }
4318
4319
    /* If that succeeded, we know we'll be keeping all the
4320
       relocs.  */
4321
0
    if (h->dynindx != -1)
4322
0
      goto keep;
4323
0
  }
4324
4325
0
      h->dyn_relocs = NULL;
4326
4327
0
    keep:;
4328
0
    }
4329
4330
  /* Finally, allocate space.  */
4331
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
4332
0
    {
4333
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4334
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4335
0
    }
4336
4337
0
  return true;
4338
0
}
4339
4340
/* Add relocation REL to the end of relocation section SRELOC.  */
4341
4342
static void
4343
elf32_nds32_add_dynreloc (bfd *output_bfd,
4344
        struct bfd_link_info *info ATTRIBUTE_UNUSED,
4345
        asection *sreloc, Elf_Internal_Rela *rel)
4346
0
{
4347
0
  bfd_byte *loc;
4348
0
  if (sreloc == NULL)
4349
0
    abort ();
4350
4351
0
  loc = sreloc->contents;
4352
0
  loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4353
0
  if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4354
0
    abort ();
4355
4356
0
  bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4357
0
}
4358
4359
/* Set the sizes of the dynamic sections.  */
4360
4361
static bool
4362
nds32_elf_late_size_sections (struct bfd_link_info *info)
4363
0
{
4364
0
  struct elf_nds32_link_hash_table *htab;
4365
0
  bfd *dynobj;
4366
0
  asection *s;
4367
0
  bool relocs;
4368
0
  bfd *ibfd;
4369
4370
0
  htab = nds32_elf_hash_table (info);
4371
0
  if (htab == NULL)
4372
0
    return false;
4373
4374
0
  dynobj = elf_hash_table (info)->dynobj;
4375
0
  if (dynobj == NULL)
4376
0
    return true;
4377
4378
0
  if (elf_hash_table (info)->dynamic_sections_created)
4379
0
    {
4380
      /* Set the contents of the .interp section to the interpreter.  */
4381
0
      if (bfd_link_executable (info) && !info->nointerp)
4382
0
  {
4383
0
    s = elf_hash_table (info)->interp;
4384
0
    BFD_ASSERT (s != NULL);
4385
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4386
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4387
0
    s->alloced = 1;
4388
0
  }
4389
0
    }
4390
4391
  /* Set up .got offsets for local syms, and space for local dynamic
4392
     relocs.  */
4393
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4394
0
    {
4395
0
      bfd_signed_vma *local_got;
4396
0
      bfd_signed_vma *end_local_got;
4397
0
      bfd_size_type locsymcount;
4398
0
      Elf_Internal_Shdr *symtab_hdr;
4399
0
      asection *sgot;
4400
0
      char *local_tls_type;
4401
0
      bfd_vma *local_tlsdesc_gotent;
4402
4403
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4404
0
  continue;
4405
4406
0
      for (s = ibfd->sections; s != NULL; s = s->next)
4407
0
  {
4408
0
    struct elf_dyn_relocs *p;
4409
4410
0
    for (p = ((struct elf_dyn_relocs *)
4411
0
        elf_section_data (s)->local_dynrel);
4412
0
         p != NULL; p = p->next)
4413
0
      {
4414
0
        if (!bfd_is_abs_section (p->sec)
4415
0
      && bfd_is_abs_section (p->sec->output_section))
4416
0
    {
4417
      /* Input section has been discarded, either because
4418
         it is a copy of a linkonce section or due to
4419
         linker script /DISCARD/, so we'll be discarding
4420
         the relocs too.  */
4421
0
    }
4422
0
        else if (p->count != 0)
4423
0
    {
4424
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4425
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4426
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4427
0
        info->flags |= DF_TEXTREL;
4428
0
    }
4429
0
      }
4430
0
  }
4431
4432
0
      local_got = elf_local_got_refcounts (ibfd);
4433
0
      if (!local_got)
4434
0
  continue;
4435
4436
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
4437
0
      locsymcount = symtab_hdr->sh_info;
4438
0
      end_local_got = local_got + locsymcount;
4439
0
      sgot = elf_hash_table (info)->sgot;
4440
0
      local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4441
0
      local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4442
0
      for (; local_got < end_local_got;
4443
0
     ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
4444
0
  {
4445
0
    if (*local_got > 0)
4446
0
      {
4447
0
        int num_of_got_entry_needed = 0;
4448
0
        *local_got = sgot->size;
4449
0
        *local_tlsdesc_gotent = sgot->size;
4450
4451
        /* TLS_NORMAL, and TLS_IE need one slot in .got.  */
4452
0
        if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4453
0
    num_of_got_entry_needed = 1;
4454
        /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT.  */
4455
0
        else if (*local_tls_type & GOT_TLS_DESC)
4456
0
    num_of_got_entry_needed = 2;
4457
4458
0
        sgot->size += (num_of_got_entry_needed << 2);
4459
4460
        /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4461
     others need a slot in .rela.got.  */
4462
0
        if (*local_tls_type == GOT_TLS_DESC)
4463
0
    {
4464
0
      if (bfd_link_pic (info))
4465
0
        {
4466
0
          if (htab->tls_desc_trampoline)
4467
0
      {
4468
0
        htab->num_tls_desc++;
4469
0
        htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4470
0
        htab->tls_trampoline = -1;
4471
0
      }
4472
0
          else
4473
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4474
0
        }
4475
0
      else
4476
0
        {
4477
          /* TLS_DESC -> TLS_LE  */
4478
0
        }
4479
0
    }
4480
0
        else
4481
0
    {
4482
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4483
0
    }
4484
0
      }
4485
0
    else
4486
0
      {
4487
0
        *local_got = (bfd_vma) -1;
4488
0
        *local_tlsdesc_gotent = (bfd_vma) -1;
4489
0
      }
4490
0
  }
4491
0
    }
4492
4493
  /* Allocate global sym .plt and .got entries, and space for global
4494
     sym dynamic relocs.  */
4495
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4496
4497
  /* For every jump slot reserved in the sgotplt, reloc_count is
4498
     incremented.  However, when we reserve space for TLS descriptors,
4499
     it's not incremented, so in order to compute the space reserved
4500
     for them, it suffices to multiply the reloc count by the jump
4501
     slot size.  */
4502
0
  if (htab->tls_desc_trampoline && htab->root.srelplt)
4503
0
    htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4504
4505
0
  if (htab->tls_trampoline)
4506
0
    {
4507
0
      htab->tls_trampoline = htab->root.splt->size;
4508
4509
      /* If we're not using lazy TLS relocations, don't generate the
4510
   PLT and GOT entries they require.  */
4511
0
      if ((info->flags & DF_BIND_NOW))
4512
0
  htab->root.tlsdesc_plt = 0;
4513
0
      else
4514
0
  {
4515
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
4516
0
    htab->root.sgot->size += 4;
4517
4518
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
4519
0
    htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4520
0
  }
4521
0
    }
4522
4523
  /* We now have determined the sizes of the various dynamic sections.
4524
     Allocate memory for them.  */
4525
  /* The check_relocs and adjust_dynamic_symbol entry points have
4526
     determined the sizes of the various dynamic sections.  Allocate
4527
     memory for them.  */
4528
0
  relocs = false;
4529
0
  for (s = dynobj->sections; s != NULL; s = s->next)
4530
0
    {
4531
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
4532
0
  continue;
4533
4534
0
      if (s == htab->root.splt)
4535
0
  {
4536
    /* Strip this section if we don't need it; see the
4537
       comment below.  */
4538
0
    ;
4539
0
  }
4540
0
      else if (s == elf_hash_table (info)->sgot)
4541
0
  {
4542
0
    got_size += s->size;
4543
0
  }
4544
0
      else if (s == elf_hash_table (info)->sgotplt)
4545
0
  {
4546
0
    got_size += s->size;
4547
0
  }
4548
0
      else if (startswith (bfd_section_name (s), ".rela"))
4549
0
  {
4550
0
    if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4551
0
      relocs = true;
4552
4553
    /* We use the reloc_count field as a counter if we need
4554
       to copy relocs into the output file.  */
4555
0
    s->reloc_count = 0;
4556
0
  }
4557
0
      else
4558
0
  {
4559
    /* It's not one of our sections, so don't allocate space.  */
4560
0
    continue;
4561
0
  }
4562
4563
0
      if (s->size == 0)
4564
0
  {
4565
    /* If we don't need this section, strip it from the
4566
       output file.  This is mostly to handle .rela.bss and
4567
       .rela.plt.  We must create both sections in
4568
       create_dynamic_sections, because they must be created
4569
       before the linker maps input sections to output
4570
       sections.  The linker does that before
4571
       adjust_dynamic_symbol is called, and it is that
4572
       function which decides whether anything needs to go
4573
       into these sections.  */
4574
0
    s->flags |= SEC_EXCLUDE;
4575
0
    continue;
4576
0
  }
4577
4578
      /* Allocate memory for the section contents.  We use bfd_zalloc
4579
   here in case unused entries are not reclaimed before the
4580
   section's contents are written out.  This should not happen,
4581
   but this way if it does, we get a R_NDS32_NONE reloc instead
4582
   of garbage.  */
4583
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4584
0
      if (s->contents == NULL)
4585
0
  return false;
4586
0
      s->alloced = 1;
4587
0
    }
4588
4589
0
  return _bfd_elf_add_dynamic_tags (info, relocs);
4590
0
}
4591
4592
static bfd_reloc_status_type
4593
nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4594
       bfd_vma relocation, bfd_byte *location)
4595
0
{
4596
0
  int size;
4597
0
  bfd_vma x = 0;
4598
0
  bfd_reloc_status_type flag;
4599
0
  unsigned int rightshift = howto->rightshift;
4600
0
  unsigned int bitpos = howto->bitpos;
4601
4602
0
  if (howto->negate)
4603
0
    relocation = -relocation;
4604
4605
  /* Get the value we are going to relocate.  */
4606
0
  size = bfd_get_reloc_size (howto);
4607
0
  switch (size)
4608
0
    {
4609
0
    default:
4610
0
      abort ();
4611
0
      break;
4612
0
    case 0:
4613
0
      return bfd_reloc_ok;
4614
0
    case 2:
4615
0
      x = bfd_getb16 (location);
4616
0
      break;
4617
0
    case 4:
4618
0
      x = bfd_getb32 (location);
4619
0
      break;
4620
0
    }
4621
4622
  /* Check for overflow.  FIXME: We may drop bits during the addition
4623
     which we don't check for.  We must either check at every single
4624
     operation, which would be tedious, or we must do the computations
4625
     in a type larger than bfd_vma, which would be inefficient.  */
4626
0
  flag = bfd_reloc_ok;
4627
0
  if (howto->complain_on_overflow != complain_overflow_dont)
4628
0
    {
4629
0
      bfd_vma addrmask, fieldmask, signmask, ss;
4630
0
      bfd_vma a, b, sum;
4631
4632
      /* Get the values to be added together.  For signed and unsigned
4633
   relocations, we assume that all values should be truncated to
4634
   the size of an address.  For bitfields, all the bits matter.
4635
   See also bfd_check_overflow.  */
4636
0
      fieldmask = N_ONES (howto->bitsize);
4637
0
      signmask = ~fieldmask;
4638
0
      addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4639
0
      a = (relocation & addrmask) >> rightshift;
4640
0
      b = (x & howto->src_mask & addrmask) >> bitpos;
4641
4642
0
      switch (howto->complain_on_overflow)
4643
0
  {
4644
0
  case complain_overflow_signed:
4645
    /* If any sign bits are set, all sign bits must be set.
4646
       That is, A must be a valid negative address after
4647
       shifting.  */
4648
0
    signmask = ~(fieldmask >> 1);
4649
    /* Fall through.  */
4650
4651
0
  case complain_overflow_bitfield:
4652
    /* Much like the signed check, but for a field one bit
4653
       wider.  We allow a bitfield to represent numbers in the
4654
       range -2**n to 2**n-1, where n is the number of bits in the
4655
       field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4656
       can't overflow, which is exactly what we want.  */
4657
0
    ss = a & signmask;
4658
0
    if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4659
0
      flag = bfd_reloc_overflow;
4660
4661
    /* We only need this next bit of code if the sign bit of B
4662
       is below the sign bit of A.  This would only happen if
4663
       SRC_MASK had fewer bits than BITSIZE.  Note that if
4664
       SRC_MASK has more bits than BITSIZE, we can get into
4665
       trouble; we would need to verify that B is in range, as
4666
       we do for A above.  */
4667
0
    ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4668
0
    ss >>= bitpos;
4669
4670
    /* Set all the bits above the sign bit.  */
4671
0
    b = (b ^ ss) - ss;
4672
4673
    /* Now we can do the addition.  */
4674
0
    sum = a + b;
4675
4676
    /* See if the result has the correct sign.  Bits above the
4677
       sign bit are junk now; ignore them.  If the sum is
4678
       positive, make sure we did not have all negative inputs;
4679
       if the sum is negative, make sure we did not have all
4680
       positive inputs.  The test below looks only at the sign
4681
       bits, and it really just
4682
       SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4683
4684
       We mask with addrmask here to explicitly allow an address
4685
       wrap-around.  The Linux kernel relies on it, and it is
4686
       the only way to write assembler code which can run when
4687
       loaded at a location 0x80000000 away from the location at
4688
       which it is linked.  */
4689
0
    if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4690
0
      flag = bfd_reloc_overflow;
4691
4692
0
    break;
4693
4694
0
  case complain_overflow_unsigned:
4695
    /* Checking for an unsigned overflow is relatively easy:
4696
       trim the addresses and add, and trim the result as well.
4697
       Overflow is normally indicated when the result does not
4698
       fit in the field.  However, we also need to consider the
4699
       case when, e.g., fieldmask is 0x7fffffff or smaller, an
4700
       input is 0x80000000, and bfd_vma is only 32 bits; then we
4701
       will get sum == 0, but there is an overflow, since the
4702
       inputs did not fit in the field.  Instead of doing a
4703
       separate test, we can check for this by or-ing in the
4704
       operands when testing for the sum overflowing its final
4705
       field.  */
4706
0
    sum = (a + b) & addrmask;
4707
0
    if ((a | b | sum) & signmask)
4708
0
      flag = bfd_reloc_overflow;
4709
0
    break;
4710
4711
0
  default:
4712
0
    abort ();
4713
0
  }
4714
0
    }
4715
4716
  /* Put RELOCATION in the right bits.  */
4717
0
  relocation >>= (bfd_vma) rightshift;
4718
0
  relocation <<= (bfd_vma) bitpos;
4719
4720
  /* Add RELOCATION to the right bits of X.  */
4721
  /* FIXME : 090616
4722
     Because the relaxation may generate duplicate relocation at one address,
4723
     an addition to immediate in the instruction may cause the relocation added
4724
     several times.
4725
     This bug should be fixed in assembler, but a check is also needed here.  */
4726
0
  if (howto->partial_inplace)
4727
0
    x = ((x & ~howto->dst_mask)
4728
0
   | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4729
0
  else
4730
0
    x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4731
4732
4733
  /* Put the relocated value back in the object file.  */
4734
0
  switch (size)
4735
0
    {
4736
0
    default:
4737
0
    case 0:
4738
0
    case 1:
4739
0
    case 8:
4740
0
      abort ();
4741
0
      break;
4742
0
    case 2:
4743
0
      bfd_putb16 (x, location);
4744
0
      break;
4745
0
    case 4:
4746
0
      bfd_putb32 (x, location);
4747
0
      break;
4748
0
    }
4749
4750
0
  return flag;
4751
0
}
4752
4753
static bfd_reloc_status_type
4754
nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4755
             asection *input_section, bfd_byte *contents,
4756
             bfd_vma address, bfd_vma value, bfd_vma addend)
4757
0
{
4758
0
  bfd_vma relocation;
4759
4760
  /* Sanity check the address.  */
4761
0
  bfd_vma octet = address * OCTETS_PER_BYTE (input_bfd, input_section);
4762
0
  if (!bfd_reloc_offset_in_range (howto, input_bfd, input_section, octet))
4763
0
    return bfd_reloc_outofrange;
4764
4765
  /* This function assumes that we are dealing with a basic relocation
4766
     against a symbol.  We want to compute the value of the symbol to
4767
     relocate to.  This is just VALUE, the value of the symbol, plus
4768
     ADDEND, any addend associated with the reloc.  */
4769
0
  relocation = value + addend;
4770
4771
  /* If the relocation is PC relative, we want to set RELOCATION to
4772
     the distance between the symbol (currently in RELOCATION) and the
4773
     location we are relocating.  If pcrel_offset is FALSE we do not
4774
     need to subtract out the offset of the location within the
4775
     section (which is just ADDRESS).  */
4776
0
  if (howto->pc_relative)
4777
0
    {
4778
0
      relocation -= (input_section->output_section->vma
4779
0
         + input_section->output_offset);
4780
0
      if (howto->pcrel_offset)
4781
0
  relocation -= address;
4782
0
    }
4783
4784
0
  return nds32_relocate_contents (howto, input_bfd, relocation,
4785
0
          contents + address);
4786
0
}
4787
4788
static int
4789
nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4790
            const char *name,
4791
            Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4792
            asection *input_sec,
4793
            struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4794
0
{
4795
0
  const char *source;
4796
0
  FILE *sym_ld_script = NULL;
4797
0
  struct elf_nds32_link_hash_table *table;
4798
4799
0
  table = nds32_elf_hash_table (info);
4800
0
  sym_ld_script = table->sym_ld_script;
4801
0
  if (!sym_ld_script)
4802
0
    return true;
4803
4804
0
  if (!h || !name || *name == '\0')
4805
0
    return true;
4806
4807
0
  if (input_sec->flags & SEC_EXCLUDE)
4808
0
    return true;
4809
4810
0
  if (!check_start_export_sym)
4811
0
    {
4812
0
      fprintf (sym_ld_script, "SECTIONS\n{\n");
4813
0
      check_start_export_sym = 1;
4814
0
    }
4815
4816
0
  if (h->root.type == bfd_link_hash_defined
4817
0
      || h->root.type == bfd_link_hash_defweak)
4818
0
    {
4819
0
      if (!h->root.u.def.section->output_section)
4820
0
  return true;
4821
4822
0
      if (bfd_is_const_section (input_sec))
4823
0
  source = input_sec->name;
4824
0
      else
4825
0
  source = bfd_get_filename (input_sec->owner);
4826
4827
0
      fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4828
0
         h->root.root.string,
4829
0
         (long) (h->root.u.def.value
4830
0
    + h->root.u.def.section->output_section->vma
4831
0
    + h->root.u.def.section->output_offset), source);
4832
0
    }
4833
4834
0
  return true;
4835
0
}
4836
4837
/* Relocate an NDS32/D ELF section.
4838
   There is some attempt to make this function usable for many architectures,
4839
   both for RELA and REL type relocs, if only to serve as a learning tool.
4840
4841
   The RELOCATE_SECTION function is called by the new ELF backend linker
4842
   to handle the relocations for a section.
4843
4844
   The relocs are always passed as Rela structures; if the section
4845
   actually uses Rel structures, the r_addend field will always be
4846
   zero.
4847
4848
   This function is responsible for adjust the section contents as
4849
   necessary, and (if using Rela relocs and generating a
4850
   relocatable output file) adjusting the reloc addend as
4851
   necessary.
4852
4853
   This function does not have to worry about setting the reloc
4854
   address or the reloc symbol index.
4855
4856
   LOCAL_SYMS is a pointer to the swapped in local symbols.
4857
4858
   LOCAL_SECTIONS is an array giving the section in the input file
4859
   corresponding to the st_shndx field of each local symbol.
4860
4861
   The global hash table entry for the global symbols can be found
4862
   via elf_sym_hashes (input_bfd).
4863
4864
   When generating relocatable output, this function must handle
4865
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4866
   going to be the section symbol corresponding to the output
4867
   section, which means that the addend must be adjusted
4868
   accordingly.  */
4869
4870
/* Return the base VMA address which should be subtracted from real addresses
4871
   when resolving @dtpoff relocation.
4872
   This is PT_TLS segment p_vaddr.  */
4873
4874
/* Return the relocation value for @tpoff relocation
4875
   if STT_TLS virtual address is ADDRESS.  */
4876
4877
/* Return the relocation value for @gottpoff relocation
4878
   if STT_TLS virtual address is ADDRESS.  */
4879
4880
static bfd_vma
4881
gottpoff (struct bfd_link_info *info, bfd_vma address)
4882
0
{
4883
0
  bfd_vma tp_base;
4884
0
  bfd_vma tp_offset;
4885
4886
  /* If tls_sec is NULL, we should have signalled an error already.  */
4887
0
  if (elf_hash_table (info)->tls_sec == NULL)
4888
0
    return 0;
4889
4890
0
  tp_base = elf_hash_table (info)->tls_sec->vma;
4891
0
  tp_offset = address - tp_base;
4892
4893
0
  return tp_offset;
4894
0
}
4895
4896
static bool
4897
patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4898
0
{
4899
  /* TLS_GD/TLS_LD model #1
4900
     46 00 00 00 sethi $r0,#0x0
4901
     58 00 00 00 ori $r0,$r0,#0x0
4902
     40 00 74 00 add $r0,$r0,$gp
4903
     04 10 00 00 lwi $r1,[$r0+#0x0]
4904
     4b e0 04 01 jral $lp,$r1  */
4905
4906
  /* TLS_GD/TLS_LD model #2
4907
     46 00 00 00 sethi $r0,#0x0
4908
     58 00 00 00 ori $r0,$r0,#0x0
4909
     38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4910
     40 00 74 00 add $r0,$r0,$gp
4911
     4b e0 04 01 jral $lp,$r1  */
4912
4913
  /* TLS_IE model (non-PIC)
4914
     46 00 00 00 sethi $r0,#0x0
4915
     04 00 00 00 lwi $r0,[$r0+#0x0]
4916
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4917
4918
  /* TLS_IE model (PIC)
4919
     46 00 00 00 sethi $r0,#0x0
4920
     58 00 00 00 ori $r0,$r0,#0x0
4921
     38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4922
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4923
4924
  /* TLS_GD_TO_IE model
4925
     46 00 00 00 sethi $r0,#0x0
4926
     58 00 00 00 ori $r0,$r0,#0x0
4927
     40 00 74 00 add $r0,$rM,$gp
4928
     04 00 00 01 lwi $r0,[$r0+#0x4]
4929
     40 00 64 00 add $r0,$r0,$r25  */
4930
4931
0
  bool rz = false;
4932
4933
0
  typedef struct
4934
0
    {
4935
0
      uint32_t opcode;
4936
0
      uint32_t mask;
4937
0
    } pat_t;
4938
4939
0
  uint32_t patch[3] =
4940
0
    {
4941
0
      0x40007400, /* add $r0,$rM,$gp     */
4942
0
      0x04000001, /* lwi $r0,[$r0+#0x4]  */
4943
0
      0x40006400, /* add $r0,$r0,$r25    */
4944
0
    };
4945
4946
0
  pat_t mode0[3] =
4947
0
    {
4948
0
  { 0x40000000, 0xfe0003ff },
4949
0
  { 0x04000000, 0xfe000000 },
4950
0
  { 0x4be00001, 0xffff83ff },
4951
0
    };
4952
4953
0
  pat_t mode1[3] =
4954
0
    {
4955
0
  { 0x38007402, 0xfe007fff },
4956
0
  { 0x40007400, 0xfe007fff },
4957
0
  { 0x4be00001, 0xffff83ff },
4958
0
    };
4959
4960
0
  unsigned char *p = contents + rel->r_offset;
4961
4962
0
  uint32_t insn;
4963
0
  uint32_t regidx = 0;
4964
0
  insn = bfd_getb32 (p);
4965
0
  if (INSN_SETHI == (0xfe0fffffu & insn))
4966
0
    {
4967
0
      regidx = 0x1f & (insn >> 20);
4968
0
      p += 4;
4969
0
    }
4970
4971
0
  insn = bfd_getb32 (p);
4972
0
  if (INSN_ORI == (0xfe007fffu & insn))
4973
0
    {
4974
0
      regidx = 0x1f & (insn >> 20);
4975
0
      p += 4;
4976
0
    }
4977
4978
0
  if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction.  */
4979
0
    {
4980
      /* already patched?  */
4981
0
      if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4982
0
    (patch[1] == bfd_getb32 (p + 4)))
4983
0
  rz = true;
4984
0
    }
4985
0
  else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4986
0
    {
4987
0
      if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4988
0
    (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4989
0
  {
4990
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4991
0
    bfd_putb32 (patch[1], p + 4);
4992
0
    bfd_putb32 (patch[2], p + 8);
4993
0
    rz = true;
4994
0
  }
4995
0
    }
4996
0
  else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4997
0
    {
4998
0
      if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4999
0
    (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
5000
0
  {
5001
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
5002
0
    bfd_putb32 (patch[1], p + 4);
5003
0
    bfd_putb32 (patch[2], p + 8);
5004
0
    rz = true;
5005
0
  }
5006
0
    }
5007
5008
0
  if (!rz)
5009
0
    {
5010
0
      printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
5011
0
        (int) rel->r_offset);
5012
0
      BFD_ASSERT(0); /* Unsupported pattern.  */
5013
0
    }
5014
5015
0
  return rz;
5016
0
}
5017
5018
static enum elf_nds32_tls_type
5019
get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5020
5021
static unsigned int
5022
ones32 (register unsigned int x)
5023
0
{
5024
  /* 32-bit recursive reduction using SWAR...
5025
     but first step is mapping 2-bit values
5026
     into sum of 2 1-bit values in sneaky way.  */
5027
0
  x -= ((x >> 1) & 0x55555555);
5028
0
  x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5029
0
  x = (((x >> 4) + x) & 0x0f0f0f0f);
5030
0
  x += (x >> 8);
5031
0
  x += (x >> 16);
5032
0
  return (x & 0x0000003f);
5033
0
}
5034
5035
#if !HAVE_FLS
5036
static unsigned int
5037
fls (register unsigned int x)
5038
0
{
5039
0
  return ffs (x & (-x));
5040
0
}
5041
#endif /* !HAVE_FLS */
5042
5043
#define nds32_elf_local_tlsdesc_gotent(bfd) \
5044
  (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5045
5046
static int
5047
nds32_elf_relocate_section (struct bfd_link_info * info,
5048
          bfd *      input_bfd,
5049
          asection *       input_section,
5050
          bfd_byte *       contents,
5051
          Elf_Internal_Rela *    relocs,
5052
          Elf_Internal_Sym *     local_syms,
5053
          asection **      local_sections)
5054
0
{
5055
0
  Elf_Internal_Shdr *symtab_hdr;
5056
0
  struct elf_link_hash_entry **sym_hashes;
5057
0
  Elf_Internal_Rela *rel, *relend;
5058
0
  bool ret = true;   /* Assume success.  */
5059
0
  int align = 0;
5060
0
  bfd_reloc_status_type r;
5061
0
  const char *errmsg = NULL;
5062
0
  bfd_vma gp;
5063
0
  struct elf_link_hash_table *ehtab;
5064
0
  struct elf_nds32_link_hash_table *htab;
5065
0
  bfd *dynobj;
5066
0
  bfd_vma *local_got_offsets;
5067
0
  asection *sgot, *splt, *sreloc;
5068
0
  bfd_vma high_address;
5069
0
  struct elf_nds32_link_hash_table *table;
5070
0
  int eliminate_gc_relocs;
5071
0
  bfd_vma fpbase_addr;
5072
5073
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
5074
0
  sym_hashes = elf_sym_hashes (input_bfd);
5075
0
  ehtab = elf_hash_table (info);
5076
0
  htab = nds32_elf_hash_table (info);
5077
0
  high_address = bfd_get_section_limit (input_bfd, input_section);
5078
5079
0
  dynobj = htab->root.dynobj;
5080
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
5081
5082
0
  sgot = ehtab->sgot;
5083
0
  splt = ehtab->splt;
5084
0
  sreloc = NULL;
5085
5086
0
  rel = relocs;
5087
0
  relend = relocs + input_section->reloc_count;
5088
5089
0
  table = nds32_elf_hash_table (info);
5090
0
  eliminate_gc_relocs = table->eliminate_gc_relocs;
5091
5092
  /* By this time, we can adjust the value of _SDA_BASE_.  */
5093
  /* Explain _SDA_BASE_  */
5094
0
  if ((!bfd_link_relocatable (info)))
5095
0
    {
5096
0
      is_SDA_BASE_set = 1;
5097
0
      r = nds32_elf_final_sda_base (info->output_bfd, info, &gp, true);
5098
0
      if (r != bfd_reloc_ok)
5099
0
  return false;
5100
0
    }
5101
5102
  /* Do TLS model conversion once at first.  */
5103
0
  nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5104
5105
  /* Use gp as fp to prevent truncated fit.  Because in relaxation time
5106
     the fp value is set as gp, and it has be reverted for instruction
5107
     setting fp.  */
5108
0
  fpbase_addr = elf_gp (info->output_bfd);
5109
5110
  /* Deal with (dynamic) relocations.  */
5111
0
  for (rel = relocs; rel < relend; rel++)
5112
0
    {
5113
0
      enum elf_nds32_reloc_type r_type;
5114
0
      reloc_howto_type *howto = NULL;
5115
0
      unsigned long r_symndx;
5116
0
      struct elf_link_hash_entry *h = NULL;
5117
0
      Elf_Internal_Sym *sym = NULL;
5118
0
      asection *sec;
5119
0
      bfd_vma relocation;
5120
0
      bfd_vma relocation_sym = 0xdeadbeef;
5121
0
      Elf_Internal_Rela *lorel;
5122
0
      bfd_vma off;
5123
5124
      /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5125
   ensure it's zero (we use REL relocs, not RELA).  Therefore this
5126
   should be assigning zero to `addend', but for clarity we use
5127
   `r_addend'.  */
5128
5129
0
      bfd_vma addend = rel->r_addend;
5130
0
      bfd_vma offset = rel->r_offset;
5131
5132
0
      r_type = ELF32_R_TYPE (rel->r_info);
5133
0
      if (r_type >= R_NDS32_max)
5134
0
  {
5135
    /* xgettext:c-format */
5136
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5137
0
            input_bfd, r_type);
5138
0
    bfd_set_error (bfd_error_bad_value);
5139
0
    ret = false;
5140
0
    continue;
5141
0
  }
5142
5143
0
      if (r_type == R_NDS32_GNU_VTENTRY
5144
0
    || r_type == R_NDS32_GNU_VTINHERIT
5145
0
    || r_type == R_NDS32_NONE
5146
0
    || r_type == R_NDS32_RELA_GNU_VTENTRY
5147
0
    || r_type == R_NDS32_RELA_GNU_VTINHERIT
5148
0
    || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5149
0
    || r_type == R_NDS32_DATA
5150
0
    || r_type == R_NDS32_TRAN)
5151
0
  continue;
5152
5153
      /* If we enter the fp-as-gp region.  Resolve the address
5154
   of best fp-base.  */
5155
0
      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5156
0
    && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5157
0
  {
5158
0
    int dist;
5159
5160
    /* Distance to relocation of best fp-base is encoded in R_SYM.  */
5161
0
    dist =  rel->r_addend >> 16;
5162
0
    fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5163
0
              local_syms, symtab_hdr);
5164
0
  }
5165
0
      else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5166
0
         && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5167
0
  {
5168
0
    fpbase_addr = elf_gp (info->output_bfd);
5169
0
  }
5170
5171
      /* Skip the relocations used for relaxation.  */
5172
      /* We have to update LONGCALL and LONGJUMP
5173
   relocations when generating the relocatable files.  */
5174
0
      if (!bfd_link_relocatable (info)
5175
0
    && (r_type >= R_NDS32_RELAX_ENTRY
5176
0
        || (r_type >= R_NDS32_LONGCALL4
5177
0
      && r_type <= R_NDS32_LONGJUMP7)))
5178
0
  continue;
5179
5180
0
      howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5181
0
      r_symndx = ELF32_R_SYM (rel->r_info);
5182
5183
      /* This is a final link.  */
5184
0
      sym = NULL;
5185
0
      sec = NULL;
5186
0
      h = NULL;
5187
5188
0
      if (r_symndx < symtab_hdr->sh_info)
5189
0
  {
5190
    /* Local symbol.  */
5191
0
    sym = local_syms + r_symndx;
5192
0
    sec = local_sections[r_symndx];
5193
5194
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
5195
0
            sym, &sec, rel);
5196
0
    addend = rel->r_addend;
5197
5198
    /* keep symbol location for static TLS_IE GOT entry  */
5199
0
    relocation_sym = relocation;
5200
0
    if (bfd_link_relocatable (info))
5201
0
      {
5202
        /* This is a relocatable link.  We don't have to change
5203
     anything, unless the reloc is against a section symbol,
5204
     in which case we have to adjust according to where the
5205
     section symbol winds up in the output section.  */
5206
0
        if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5207
0
    rel->r_addend += sec->output_offset + sym->st_value;
5208
5209
0
        continue;
5210
0
      }
5211
0
  }
5212
0
      else
5213
0
  {
5214
    /* External symbol.  */
5215
0
    if (bfd_link_relocatable (info))
5216
0
      continue;
5217
0
    bool warned, ignored, unresolved_reloc;
5218
0
    int symndx = r_symndx - symtab_hdr->sh_info;
5219
5220
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5221
0
           r_symndx, symtab_hdr, sym_hashes, h, sec,
5222
0
           relocation, unresolved_reloc, warned,
5223
0
           ignored);
5224
5225
    /* keep symbol location for static TLS_IE GOT entry  */
5226
0
    relocation_sym = relocation;
5227
5228
    /* la $fp, _FP_BASE_ is per-function (region).
5229
       Handle it specially.  */
5230
0
    switch ((int) r_type)
5231
0
      {
5232
0
      case R_NDS32_HI20_RELA:
5233
0
      case R_NDS32_LO12S0_RELA:
5234
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5235
0
        FP_BASE_NAME) == 0)
5236
0
    {
5237
0
      if (!bfd_link_pie (info))
5238
0
        {
5239
0
          _bfd_error_handler
5240
0
      ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5241
0
       input_bfd);
5242
0
        }
5243
0
      relocation = fpbase_addr;
5244
0
    }
5245
0
        break;
5246
0
      case R_NDS32_SDA19S0_RELA:
5247
0
      case R_NDS32_SDA15S0_RELA:
5248
0
      case R_NDS32_20_RELA:
5249
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5250
0
        FP_BASE_NAME) == 0)
5251
0
    {
5252
0
      relocation = fpbase_addr;
5253
0
      break;
5254
0
    }
5255
0
      }
5256
0
  }
5257
5258
      /* Sanity check the address.  */
5259
0
      if (offset > high_address)
5260
0
  {
5261
0
    r = bfd_reloc_outofrange;
5262
0
    goto check_reloc;
5263
0
  }
5264
5265
0
      if (r_type >= R_NDS32_RELAX_ENTRY)
5266
0
  continue;
5267
5268
0
      switch ((int) r_type)
5269
0
  {
5270
0
  case R_NDS32_GOTOFF:
5271
    /* Relocation is relative to the start of the global offset
5272
       table (for ld24 rx, #uimm24), e.g. access at label+addend
5273
5274
       ld24 rx. #label@GOTOFF + addend
5275
       sub  rx, r12.  */
5276
0
  case R_NDS32_GOTOFF_HI20:
5277
0
  case R_NDS32_GOTOFF_LO12:
5278
0
  case R_NDS32_GOTOFF_LO15:
5279
0
  case R_NDS32_GOTOFF_LO19:
5280
0
    BFD_ASSERT (sgot != NULL);
5281
5282
0
    relocation -= elf_gp (info->output_bfd);
5283
0
    break;
5284
5285
0
  case R_NDS32_9_PLTREL:
5286
0
  case R_NDS32_25_PLTREL:
5287
    /* Relocation is to the entry for this symbol in the
5288
       procedure linkage table.  */
5289
5290
    /* The native assembler will generate a 25_PLTREL reloc
5291
       for a local symbol if you assemble a call from one
5292
       section to another when using -K pic.  */
5293
0
    if (h == NULL)
5294
0
      break;
5295
5296
0
    if (h->forced_local)
5297
0
      break;
5298
5299
    /* We didn't make a PLT entry for this symbol.  This
5300
       happens when statically linking PIC code, or when
5301
       using -Bsymbolic.  */
5302
0
    if (h->plt.offset == (bfd_vma) - 1)
5303
0
      break;
5304
5305
0
    relocation = (splt->output_section->vma
5306
0
      + splt->output_offset + h->plt.offset);
5307
0
    break;
5308
5309
0
  case R_NDS32_PLT_GOTREL_HI20:
5310
0
  case R_NDS32_PLT_GOTREL_LO12:
5311
0
  case R_NDS32_PLT_GOTREL_LO15:
5312
0
  case R_NDS32_PLT_GOTREL_LO19:
5313
0
  case R_NDS32_PLT_GOTREL_LO20:
5314
0
    if (h == NULL
5315
0
        || h->forced_local
5316
0
        || h->plt.offset == (bfd_vma) -1
5317
0
        || (bfd_link_pie (info) && h->def_regular))
5318
0
      {
5319
        /* Maybe we should find better checking to optimize
5320
     PIE PLT relocations.  */
5321
        /* We didn't make a PLT entry for this symbol.  This
5322
     happens when statically linking PIC code, or when
5323
     using -Bsymbolic.  */
5324
0
        if (h)
5325
0
    h->plt.offset = (bfd_vma) -1;   /* Cancel PLT trampoline.  */
5326
0
        relocation -= elf_gp (info->output_bfd);
5327
0
        break;
5328
0
      }
5329
5330
0
    relocation = (splt->output_section->vma
5331
0
      + splt->output_offset + h->plt.offset);
5332
5333
0
    relocation -= elf_gp (info->output_bfd);
5334
0
    break;
5335
5336
0
  case R_NDS32_PLTREL_HI20:
5337
0
  case R_NDS32_PLTREL_LO12:
5338
5339
    /* Relocation is to the entry for this symbol in the
5340
       procedure linkage table.  */
5341
5342
    /* The native assembler will generate a 25_PLTREL reloc
5343
       for a local symbol if you assemble a call from one
5344
       section to another when using -K pic.  */
5345
0
    if (h == NULL)
5346
0
      break;
5347
5348
0
    if (h->forced_local)
5349
0
      break;
5350
5351
0
    if (h->plt.offset == (bfd_vma) - 1)
5352
      /* We didn't make a PLT entry for this symbol.  This
5353
         happens when statically linking PIC code, or when
5354
         using -Bsymbolic.  */
5355
0
      break;
5356
5357
0
    if (splt == NULL)
5358
0
      break;
5359
5360
0
    relocation = (splt->output_section->vma
5361
0
      + splt->output_offset
5362
0
      + h->plt.offset + 4)
5363
0
           - (input_section->output_section->vma
5364
0
        + input_section->output_offset
5365
0
        + rel->r_offset);
5366
5367
0
    break;
5368
5369
0
  case R_NDS32_GOTPC20:
5370
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5371
       ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
5372
0
    relocation = elf_gp (info->output_bfd);
5373
0
    break;
5374
5375
0
  case R_NDS32_GOTPC_HI20:
5376
0
  case R_NDS32_GOTPC_LO12:
5377
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5378
       bl .+4
5379
       seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5380
       or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5381
       or
5382
       bl .+4
5383
       seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5384
       add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)  */
5385
0
    relocation = elf_gp (info->output_bfd);
5386
0
    relocation -= (input_section->output_section->vma
5387
0
       + input_section->output_offset + rel->r_offset);
5388
0
    break;
5389
5390
0
  case R_NDS32_GOT20:
5391
    /* Fall through.  */
5392
0
  case R_NDS32_GOT_HI20:
5393
0
  case R_NDS32_GOT_LO12:
5394
0
  case R_NDS32_GOT_LO15:
5395
0
  case R_NDS32_GOT_LO19:
5396
    /* Relocation is to the entry for this symbol in the global
5397
       offset table.  */
5398
0
    BFD_ASSERT (sgot != NULL);
5399
5400
0
    if (h != NULL)
5401
0
      {
5402
        /* External symbol  */
5403
0
        bool dyn;
5404
5405
0
        off = h->got.offset;
5406
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5407
0
        dyn = htab->root.dynamic_sections_created;
5408
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5409
0
                bfd_link_pic (info),
5410
0
                h)
5411
0
      || (bfd_link_pic (info)
5412
0
          && (info->symbolic
5413
0
        || h->dynindx == -1
5414
0
        || h->forced_local) && h->def_regular))
5415
0
    {
5416
      /* This is actually a static link, or it is a
5417
         -Bsymbolic link and the symbol is defined
5418
         locally, or the symbol was forced to be local
5419
         because of a version file.  We must initialize
5420
         this entry in the global offset table.  Since the
5421
         offset must always be a multiple of 4, we use the
5422
         least significant bit to record whether we have
5423
         initialized it already.
5424
5425
         When doing a dynamic link, we create a .rela.got
5426
         relocation entry to initialize the value.  This
5427
         is done in the finish_dynamic_symbol routine.  */
5428
0
      if ((off & 1) != 0) /* clear LSB  */
5429
0
        off &= ~1;
5430
0
      else
5431
0
        {
5432
0
          bfd_put_32 (info->output_bfd, relocation,
5433
0
          sgot->contents + off);
5434
0
          h->got.offset |= 1;
5435
0
        }
5436
0
    }
5437
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5438
0
         - elf_gp (info->output_bfd);
5439
0
      }
5440
0
    else
5441
0
      {
5442
        /* Local symbol  */
5443
0
        bfd_byte *loc;
5444
5445
0
        BFD_ASSERT (local_got_offsets != NULL
5446
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5447
5448
0
        off = local_got_offsets[r_symndx];
5449
5450
        /* The offset must always be a multiple of 4.  We use
5451
     the least significant bit to record whether we have
5452
     already processed this entry.  */
5453
0
        if ((off & 1) != 0) /* clear LSB  */
5454
0
    off &= ~1;
5455
0
        else
5456
0
    {
5457
0
      bfd_put_32 (info->output_bfd, relocation,
5458
0
            sgot->contents + off);
5459
5460
0
      if (bfd_link_pic (info))
5461
0
        {
5462
0
          asection *srelgot;
5463
0
          Elf_Internal_Rela outrel;
5464
5465
          /* We need to generate a R_NDS32_RELATIVE reloc
5466
       for the dynamic linker.  */
5467
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5468
0
          BFD_ASSERT (srelgot != NULL);
5469
5470
0
          outrel.r_offset = (elf_gp (info->output_bfd)
5471
0
           + sgot->output_offset + off);
5472
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5473
0
          outrel.r_addend = relocation;
5474
0
          loc = srelgot->contents;
5475
0
          loc +=
5476
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5477
0
          bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5478
0
          ++srelgot->reloc_count;
5479
0
        }
5480
0
      local_got_offsets[r_symndx] |= 1;
5481
0
    }
5482
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5483
0
         - elf_gp (info->output_bfd);
5484
0
      }
5485
5486
0
    break;
5487
5488
0
  case R_NDS32_16_RELA:
5489
0
  case R_NDS32_20_RELA:
5490
0
  case R_NDS32_5_RELA:
5491
0
  case R_NDS32_32_RELA:
5492
0
  case R_NDS32_9_PCREL_RELA:
5493
0
  case R_NDS32_WORD_9_PCREL_RELA:
5494
0
  case R_NDS32_10_UPCREL_RELA:
5495
0
  case R_NDS32_15_PCREL_RELA:
5496
0
  case R_NDS32_17_PCREL_RELA:
5497
0
  case R_NDS32_25_PCREL_RELA:
5498
0
  case R_NDS32_HI20_RELA:
5499
0
  case R_NDS32_LO12S3_RELA:
5500
0
  case R_NDS32_LO12S2_RELA:
5501
0
  case R_NDS32_LO12S2_DP_RELA:
5502
0
  case R_NDS32_LO12S2_SP_RELA:
5503
0
  case R_NDS32_LO12S1_RELA:
5504
0
  case R_NDS32_LO12S0_RELA:
5505
0
  case R_NDS32_LO12S0_ORI_RELA:
5506
0
    if (bfd_link_pic (info) && r_symndx != 0
5507
0
        && (input_section->flags & SEC_ALLOC) != 0
5508
0
        && (eliminate_gc_relocs == 0
5509
0
      || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5510
0
        && ((r_type != R_NDS32_9_PCREL_RELA
5511
0
       && r_type != R_NDS32_WORD_9_PCREL_RELA
5512
0
       && r_type != R_NDS32_10_UPCREL_RELA
5513
0
       && r_type != R_NDS32_15_PCREL_RELA
5514
0
       && r_type != R_NDS32_17_PCREL_RELA
5515
0
       && r_type != R_NDS32_25_PCREL_RELA
5516
0
       && !(r_type == R_NDS32_32_RELA
5517
0
      && strcmp (input_section->name, ".eh_frame") == 0))
5518
0
      || (h != NULL && h->dynindx != -1
5519
0
          && (!info->symbolic || !h->def_regular))))
5520
0
      {
5521
0
        Elf_Internal_Rela outrel;
5522
0
        bool skip, relocate;
5523
0
        bfd_byte *loc;
5524
5525
        /* When generating a shared object, these relocations
5526
     are copied into the output file to be resolved at run
5527
     time.  */
5528
5529
0
        if (sreloc == NULL)
5530
0
    {
5531
0
      const char *name;
5532
5533
0
      name = bfd_elf_string_from_elf_section
5534
0
        (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5535
0
         elf_section_data (input_section)->rela.hdr->sh_name);
5536
0
      if (name == NULL)
5537
0
        return false;
5538
5539
0
      BFD_ASSERT (startswith (name, ".rela")
5540
0
            && strcmp (bfd_section_name (input_section),
5541
0
           name + 5) == 0);
5542
5543
0
      sreloc = bfd_get_section_by_name (dynobj, name);
5544
0
      BFD_ASSERT (sreloc != NULL);
5545
0
    }
5546
5547
0
        skip = false;
5548
0
        relocate = false;
5549
5550
0
        outrel.r_offset = _bfd_elf_section_offset (info->output_bfd,
5551
0
               info,
5552
0
               input_section,
5553
0
               rel->r_offset);
5554
0
        if (outrel.r_offset == (bfd_vma) - 1)
5555
0
    skip = true;
5556
0
        else if (outrel.r_offset == (bfd_vma) - 2)
5557
0
    skip = true, relocate = true;
5558
0
        outrel.r_offset += (input_section->output_section->vma
5559
0
          + input_section->output_offset);
5560
5561
0
        if (skip)
5562
0
    memset (&outrel, 0, sizeof outrel);
5563
0
        else if (r_type == R_NDS32_17_PCREL_RELA
5564
0
           || r_type == R_NDS32_15_PCREL_RELA
5565
0
           || r_type == R_NDS32_25_PCREL_RELA)
5566
0
    {
5567
0
      BFD_ASSERT (h != NULL && h->dynindx != -1);
5568
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5569
0
      outrel.r_addend = rel->r_addend;
5570
0
    }
5571
0
        else
5572
0
    {
5573
      /* h->dynindx may be -1 if this symbol was marked to
5574
         become local.  */
5575
0
      if (h == NULL
5576
0
          || ((info->symbolic || h->dynindx == -1)
5577
0
        && h->def_regular)
5578
0
          || (bfd_link_pie (info) && h->def_regular))
5579
0
        {
5580
0
          relocate = true;
5581
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5582
0
          outrel.r_addend = relocation + rel->r_addend;
5583
5584
0
          if (h)
5585
0
      {
5586
0
        h->plt.offset = (bfd_vma) -1;   /* cancel PLT trampoline.  */
5587
5588
0
        BFD_ASSERT (sgot != NULL);
5589
        /* If we did not allocate got entry for the symbol,
5590
           we can not fill the nonexistent got entry.  */
5591
0
        if (h->got.offset != (bfd_vma) -1
5592
0
            && (h->got.offset & 1) == 0)
5593
0
          {
5594
0
            bfd_put_32 (info->output_bfd, outrel.r_addend,
5595
0
            sgot->contents + h->got.offset);
5596
0
          }
5597
0
      }
5598
0
        }
5599
0
      else
5600
0
        {
5601
0
          if (h->dynindx == -1)
5602
0
      {
5603
0
        _bfd_error_handler
5604
0
          (_("%pB: relocation %s against `%s' can not be used when "
5605
0
             "making a shared object; recompile with -fPIC"),
5606
0
           input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5607
0
        bfd_set_error (bfd_error_bad_value);
5608
0
        return false;
5609
0
      }
5610
5611
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5612
0
          outrel.r_addend = rel->r_addend;
5613
0
        }
5614
0
    }
5615
5616
0
        loc = sreloc->contents;
5617
0
        loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5618
0
        bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5619
0
        ++sreloc->reloc_count;
5620
5621
        /* If this reloc is against an external symbol, we do
5622
     not want to fiddle with the addend.  Otherwise, we
5623
     need to include the symbol value so that it becomes
5624
     an addend for the dynamic reloc.  */
5625
0
        if (!relocate)
5626
0
    continue;
5627
0
      }
5628
0
    break;
5629
5630
0
  case R_NDS32_25_ABS_RELA:
5631
0
    if (bfd_link_pic (info))
5632
0
      {
5633
0
        _bfd_error_handler
5634
0
    (_("%pB: warning: %s unsupported in shared mode"),
5635
0
     input_bfd, "R_NDS32_25_ABS_RELA");
5636
0
        return false;
5637
0
      }
5638
0
    break;
5639
5640
0
  case R_NDS32_9_PCREL:
5641
0
    r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5642
0
            contents, offset,
5643
0
            sec, relocation, addend);
5644
0
    goto check_reloc;
5645
5646
0
  case R_NDS32_HI20:
5647
    /* We allow an arbitrary number of HI20 relocs before the
5648
       LO12 reloc.  This permits gcc to emit the HI and LO relocs
5649
       itself.  */
5650
0
    for (lorel = rel + 1;
5651
0
         (lorel < relend
5652
0
    && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5653
0
      continue;
5654
0
    if (lorel < relend
5655
0
        && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5656
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5657
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5658
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5659
0
      {
5660
0
        nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5661
0
               contents, relocation + addend);
5662
0
        r = bfd_reloc_ok;
5663
0
      }
5664
0
    else
5665
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5666
0
            contents, offset, relocation,
5667
0
            addend);
5668
0
    goto check_reloc;
5669
5670
0
  case R_NDS32_GOT17S2_RELA:
5671
0
  case R_NDS32_GOT15S2_RELA:
5672
0
    BFD_ASSERT (sgot != NULL);
5673
5674
0
    if (h != NULL)
5675
0
      {
5676
0
        bool dyn;
5677
5678
0
        off = h->got.offset;
5679
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5680
5681
0
        dyn = htab->root.dynamic_sections_created;
5682
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5683
0
      (dyn, bfd_link_pic (info), h)
5684
0
      || (bfd_link_pic (info)
5685
0
          && (info->symbolic
5686
0
        || h->dynindx == -1
5687
0
        || h->forced_local)
5688
0
          && h->def_regular))
5689
0
    {
5690
      /* This is actually a static link, or it is a
5691
         -Bsymbolic link and the symbol is defined
5692
         locally, or the symbol was forced to be local
5693
         because of a version file.  We must initialize
5694
         this entry in the global offset table.  Since the
5695
         offset must always be a multiple of 4, we use the
5696
         least significant bit to record whether we have
5697
         initialized it already.
5698
5699
         When doing a dynamic link, we create a .rela.got
5700
         relocation entry to initialize the value.  This
5701
         is done in the finish_dynamic_symbol routine.  */
5702
0
      if ((off & 1) != 0)
5703
0
        off &= ~1;
5704
0
      else
5705
0
        {
5706
0
          bfd_put_32 (info->output_bfd, relocation,
5707
0
          sgot->contents + off);
5708
0
          h->got.offset |= 1;
5709
0
        }
5710
0
    }
5711
0
      }
5712
0
    else
5713
0
      {
5714
0
        bfd_byte *loc;
5715
5716
0
        BFD_ASSERT (local_got_offsets != NULL
5717
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5718
5719
0
        off = local_got_offsets[r_symndx];
5720
5721
        /* The offset must always be a multiple of 4.  We use
5722
     the least significant bit to record whether we have
5723
     already processed this entry.  */
5724
0
        if ((off & 1) != 0)
5725
0
    off &= ~1;
5726
0
        else
5727
0
    {
5728
0
      bfd_put_32 (info->output_bfd, relocation,
5729
0
            sgot->contents + off);
5730
5731
0
      if (bfd_link_pic (info))
5732
0
        {
5733
0
          asection *srelgot;
5734
0
          Elf_Internal_Rela outrel;
5735
5736
          /* We need to generate a R_NDS32_RELATIVE reloc
5737
       for the dynamic linker.  */
5738
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5739
0
          BFD_ASSERT (srelgot != NULL);
5740
5741
0
          outrel.r_offset = (elf_gp (info->output_bfd)
5742
0
           + sgot->output_offset + off);
5743
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5744
0
          outrel.r_addend = relocation;
5745
0
          loc = srelgot->contents;
5746
0
          loc +=
5747
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5748
0
          bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5749
0
          ++srelgot->reloc_count;
5750
0
        }
5751
0
      local_got_offsets[r_symndx] |= 1;
5752
0
    }
5753
0
      }
5754
0
    relocation = sgot->output_section->vma + sgot->output_offset + off
5755
0
      - elf_gp (info->output_bfd);
5756
5757
0
    if (relocation & align)
5758
0
      {
5759
        /* Incorrect alignment.  */
5760
0
        _bfd_error_handler
5761
0
    (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5762
0
        ret = false;
5763
0
        r = bfd_reloc_dangerous;
5764
0
        goto check_reloc;
5765
0
      }
5766
0
    break;
5767
5768
0
  case R_NDS32_SDA16S3_RELA:
5769
0
  case R_NDS32_SDA15S3_RELA:
5770
0
  case R_NDS32_SDA15S3:
5771
0
    align = 0x7;
5772
0
    goto handle_sda;
5773
5774
0
  case R_NDS32_SDA17S2_RELA:
5775
0
  case R_NDS32_SDA15S2_RELA:
5776
0
  case R_NDS32_SDA12S2_SP_RELA:
5777
0
  case R_NDS32_SDA12S2_DP_RELA:
5778
0
  case R_NDS32_SDA15S2:
5779
0
  case R_NDS32_SDA_FP7U2_RELA:
5780
0
    align = 0x3;
5781
0
    goto handle_sda;
5782
5783
0
  case R_NDS32_SDA18S1_RELA:
5784
0
  case R_NDS32_SDA15S1_RELA:
5785
0
  case R_NDS32_SDA15S1:
5786
0
    align = 0x1;
5787
0
    goto handle_sda;
5788
5789
0
  case R_NDS32_SDA19S0_RELA:
5790
0
  case R_NDS32_SDA15S0_RELA:
5791
0
  case R_NDS32_SDA15S0:
5792
0
    align = 0x0;
5793
0
  handle_sda:
5794
0
    BFD_ASSERT (sec != NULL);
5795
5796
    /* If the symbol is in the abs section, the out_bfd will be null.
5797
       This happens when the relocation has a symbol@GOTOFF.  */
5798
0
    r = nds32_elf_final_sda_base (info->output_bfd, info, &gp, false);
5799
0
    if (r != bfd_reloc_ok)
5800
0
      {
5801
0
        _bfd_error_handler
5802
0
    (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5803
0
        ret = false;
5804
0
        goto check_reloc;
5805
0
      }
5806
5807
    /* At this point `relocation' contains the object's
5808
       address.  */
5809
0
    if (r_type == R_NDS32_SDA_FP7U2_RELA)
5810
0
      {
5811
0
        relocation -= fpbase_addr;
5812
0
      }
5813
0
    else
5814
0
      relocation -= gp;
5815
    /* Now it contains the offset from _SDA_BASE_.  */
5816
5817
    /* Make sure alignment is correct.  */
5818
5819
0
    if (relocation & align)
5820
0
      {
5821
        /* Incorrect alignment.  */
5822
0
        _bfd_error_handler
5823
    /* xgettext:c-format */
5824
0
    (_("%pB(%pA): warning: unaligned small data access"
5825
0
       " of type %d"),
5826
0
     input_bfd, input_section, r_type);
5827
0
        ret = false;
5828
0
        goto check_reloc;
5829
0
      }
5830
0
    break;
5831
5832
0
  case R_NDS32_17IFC_PCREL_RELA:
5833
0
  case R_NDS32_10IFCU_PCREL_RELA:
5834
    /* Do nothing.  */
5835
0
    break;
5836
5837
0
  case R_NDS32_TLS_LE_HI20:
5838
0
  case R_NDS32_TLS_LE_LO12:
5839
0
  case R_NDS32_TLS_LE_20:
5840
0
  case R_NDS32_TLS_LE_15S0:
5841
0
  case R_NDS32_TLS_LE_15S1:
5842
0
  case R_NDS32_TLS_LE_15S2:
5843
    /* We do not have garbage collection for got entries.
5844
       Therefore, IE to LE may have one empty entry, and DESC to
5845
       LE may have two.  */
5846
0
    if (elf_hash_table (info)->tls_sec != NULL)
5847
0
      relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5848
0
    break;
5849
5850
0
  case R_NDS32_TLS_IE_HI20:
5851
0
  case R_NDS32_TLS_IE_LO12S2:
5852
0
  case R_NDS32_TLS_DESC_HI20:
5853
0
  case R_NDS32_TLS_DESC_LO12:
5854
0
  case R_NDS32_TLS_IE_LO12:
5855
0
  case R_NDS32_TLS_IEGP_HI20:
5856
0
  case R_NDS32_TLS_IEGP_LO12:
5857
0
  case R_NDS32_TLS_IEGP_LO12S2:
5858
0
    {
5859
      /* Relocation is to the entry for this symbol in the global
5860
         offset table.  */
5861
0
      enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5862
0
      asection *srelgot;
5863
0
      Elf_Internal_Rela outrel;
5864
0
      bfd_byte *loc;
5865
0
      int indx = 0;
5866
5867
0
      eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5868
5869
0
      BFD_ASSERT (sgot != NULL);
5870
0
      if (h != NULL)
5871
0
        {
5872
0
    bool dyn;
5873
5874
0
    off = h->got.offset;
5875
0
    BFD_ASSERT (off != (bfd_vma) -1);
5876
0
    dyn = htab->root.dynamic_sections_created;
5877
0
    tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5878
0
    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5879
0
        && (!bfd_link_pic (info)
5880
0
      || !SYMBOL_REFERENCES_LOCAL (info, h)))
5881
0
      indx = h->dynindx;
5882
0
        }
5883
0
      else
5884
0
        {
5885
0
    BFD_ASSERT (local_got_offsets != NULL
5886
0
          && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5887
0
    off = local_got_offsets[r_symndx];
5888
0
    tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5889
0
        }
5890
5891
0
      relocation = sgot->output_section->vma + sgot->output_offset + off;
5892
5893
0
      if (1 < ones32 (tls_type))
5894
0
        {
5895
0
    eff_tls_type = 1 << (fls (tls_type) - 1);
5896
    /* TLS model shall be handled in nds32_elf_unify_tls_model ().  */
5897
5898
    /* TLS model X -> LE is not implement yet!
5899
       workaround here!  */
5900
0
    if (eff_tls_type == GOT_TLS_LE)
5901
0
      {
5902
0
        eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5903
0
      }
5904
0
        }
5905
5906
      /* The offset must always be a multiple of 4.  We use
5907
         the least significant bit to record whether we have
5908
         already processed this entry.  */
5909
0
      bool need_relocs = false;
5910
0
      srelgot = ehtab->srelgot;
5911
0
      if ((bfd_link_pic (info) || indx != 0)
5912
0
    && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5913
0
        || h->root.type != bfd_link_hash_undefweak))
5914
0
        {
5915
0
    need_relocs = true;
5916
0
    BFD_ASSERT (srelgot != NULL);
5917
0
        }
5918
5919
0
      if (off & 1)
5920
0
        {
5921
0
    off &= ~1;
5922
0
    relocation &= ~1;
5923
5924
0
    if (eff_tls_type & GOT_TLS_DESC)
5925
0
      {
5926
0
        relocation -= elf_gp (info->output_bfd);
5927
0
        if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5928
0
          {
5929
      /* TLS model shall be converted.  */
5930
0
      BFD_ASSERT(0);
5931
0
          }
5932
0
      }
5933
0
    else if (eff_tls_type & GOT_TLS_IEGP)
5934
0
      {
5935
0
        relocation -= elf_gp (info->output_bfd);
5936
0
      }
5937
0
        }
5938
0
      else
5939
0
        {
5940
0
    if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5941
0
      {
5942
        /* TLS model workaround shall be applied.  */
5943
0
        BFD_ASSERT(0);
5944
0
      }
5945
0
    else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5946
0
      {
5947
0
        if (eff_tls_type & GOT_TLS_IEGP)
5948
0
          relocation -= elf_gp(info->output_bfd);
5949
5950
0
        if (need_relocs)
5951
0
          {
5952
0
      if (indx == 0)
5953
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5954
0
      else
5955
0
        outrel.r_addend = 0;
5956
0
      outrel.r_offset = (sgot->output_section->vma
5957
0
             + sgot->output_offset + off);
5958
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5959
5960
0
      elf32_nds32_add_dynreloc (info->output_bfd, info,
5961
0
              srelgot, &outrel);
5962
0
          }
5963
0
        else
5964
0
          {
5965
0
      bfd_put_32 (info->output_bfd,
5966
0
            gottpoff (info, relocation_sym),
5967
0
            sgot->contents + off);
5968
0
          }
5969
0
      }
5970
0
    else if (eff_tls_type & GOT_TLS_DESC)
5971
0
      {
5972
0
        relocation -= elf_gp (info->output_bfd);
5973
0
        if (need_relocs)
5974
0
          {
5975
0
      if (indx == 0)
5976
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5977
0
      else
5978
0
        outrel.r_addend = 0;
5979
0
      outrel.r_offset = (sgot->output_section->vma
5980
0
             + sgot->output_offset + off);
5981
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5982
5983
0
      if (htab->tls_desc_trampoline)
5984
0
        {
5985
0
          asection *srelplt;
5986
0
          srelplt = ehtab->srelplt;
5987
0
          loc = srelplt->contents;
5988
0
          loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5989
0
          BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5990
0
          <= srelplt->contents + srelplt->size);
5991
5992
0
          bfd_elf32_swap_reloca_out (info->output_bfd,
5993
0
                   &outrel, loc);
5994
0
        }
5995
0
      else
5996
0
        {
5997
0
          loc = srelgot->contents;
5998
0
          loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5999
0
          bfd_elf32_swap_reloca_out (info->output_bfd,
6000
0
                   &outrel, loc);
6001
0
          ++srelgot->reloc_count;
6002
0
        }
6003
0
          }
6004
0
        else
6005
0
          {
6006
      /* feed me!  */
6007
0
      bfd_put_32 (info->output_bfd, 0xdeadbeef,
6008
0
            sgot->contents + off);
6009
0
      bfd_put_32 (info->output_bfd,
6010
0
            gottpoff (info, relocation_sym),
6011
0
            sgot->contents + off + 4);
6012
0
      patch_tls_desc_to_ie (contents, rel, input_bfd);
6013
0
      BFD_ASSERT(0);
6014
0
          }
6015
0
      }
6016
0
    else
6017
0
      {
6018
        /* TLS model workaround shall be applied.  */
6019
0
        BFD_ASSERT(0);
6020
0
      }
6021
6022
0
    if (h != NULL)
6023
0
      h->got.offset |= 1;
6024
0
    else
6025
0
      local_got_offsets[r_symndx] |= 1;
6026
0
        }
6027
0
    }
6028
0
  break;
6029
    /* DON'T fall through.  */
6030
6031
0
  default:
6032
    /* OLD_NDS32_RELOC.  */
6033
6034
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6035
0
          contents, offset, relocation, addend);
6036
0
    goto check_reloc;
6037
0
  }
6038
6039
0
      switch ((int) r_type)
6040
0
  {
6041
0
  case R_NDS32_20_RELA:
6042
0
  case R_NDS32_5_RELA:
6043
0
  case R_NDS32_9_PCREL_RELA:
6044
0
  case R_NDS32_WORD_9_PCREL_RELA:
6045
0
  case R_NDS32_10_UPCREL_RELA:
6046
0
  case R_NDS32_15_PCREL_RELA:
6047
0
  case R_NDS32_17_PCREL_RELA:
6048
0
  case R_NDS32_25_PCREL_RELA:
6049
0
  case R_NDS32_25_ABS_RELA:
6050
0
  case R_NDS32_HI20_RELA:
6051
0
  case R_NDS32_LO12S3_RELA:
6052
0
  case R_NDS32_LO12S2_RELA:
6053
0
  case R_NDS32_LO12S2_DP_RELA:
6054
0
  case R_NDS32_LO12S2_SP_RELA:
6055
0
  case R_NDS32_LO12S1_RELA:
6056
0
  case R_NDS32_LO12S0_RELA:
6057
0
  case R_NDS32_LO12S0_ORI_RELA:
6058
0
  case R_NDS32_SDA16S3_RELA:
6059
0
  case R_NDS32_SDA17S2_RELA:
6060
0
  case R_NDS32_SDA18S1_RELA:
6061
0
  case R_NDS32_SDA19S0_RELA:
6062
0
  case R_NDS32_SDA15S3_RELA:
6063
0
  case R_NDS32_SDA15S2_RELA:
6064
0
  case R_NDS32_SDA12S2_DP_RELA:
6065
0
  case R_NDS32_SDA12S2_SP_RELA:
6066
0
  case R_NDS32_SDA15S1_RELA:
6067
0
  case R_NDS32_SDA15S0_RELA:
6068
0
  case R_NDS32_SDA_FP7U2_RELA:
6069
0
  case R_NDS32_9_PLTREL:
6070
0
  case R_NDS32_25_PLTREL:
6071
0
  case R_NDS32_GOT20:
6072
0
  case R_NDS32_GOT_HI20:
6073
0
  case R_NDS32_GOT_LO12:
6074
0
  case R_NDS32_GOT_LO15:
6075
0
  case R_NDS32_GOT_LO19:
6076
0
  case R_NDS32_GOT15S2_RELA:
6077
0
  case R_NDS32_GOT17S2_RELA:
6078
0
  case R_NDS32_GOTPC20:
6079
0
  case R_NDS32_GOTPC_HI20:
6080
0
  case R_NDS32_GOTPC_LO12:
6081
0
  case R_NDS32_GOTOFF:
6082
0
  case R_NDS32_GOTOFF_HI20:
6083
0
  case R_NDS32_GOTOFF_LO12:
6084
0
  case R_NDS32_GOTOFF_LO15:
6085
0
  case R_NDS32_GOTOFF_LO19:
6086
0
  case R_NDS32_PLTREL_HI20:
6087
0
  case R_NDS32_PLTREL_LO12:
6088
0
  case R_NDS32_PLT_GOTREL_HI20:
6089
0
  case R_NDS32_PLT_GOTREL_LO12:
6090
0
  case R_NDS32_PLT_GOTREL_LO15:
6091
0
  case R_NDS32_PLT_GOTREL_LO19:
6092
0
  case R_NDS32_PLT_GOTREL_LO20:
6093
0
  case R_NDS32_17IFC_PCREL_RELA:
6094
0
  case R_NDS32_10IFCU_PCREL_RELA:
6095
0
  case R_NDS32_TLS_LE_HI20:
6096
0
  case R_NDS32_TLS_LE_LO12:
6097
0
  case R_NDS32_TLS_IE_HI20:
6098
0
  case R_NDS32_TLS_IE_LO12S2:
6099
0
  case R_NDS32_TLS_LE_20:
6100
0
  case R_NDS32_TLS_LE_15S0:
6101
0
  case R_NDS32_TLS_LE_15S1:
6102
0
  case R_NDS32_TLS_LE_15S2:
6103
0
  case R_NDS32_TLS_DESC_HI20:
6104
0
  case R_NDS32_TLS_DESC_LO12:
6105
0
  case R_NDS32_TLS_IE_LO12:
6106
0
  case R_NDS32_TLS_IEGP_HI20:
6107
0
  case R_NDS32_TLS_IEGP_LO12:
6108
0
  case R_NDS32_TLS_IEGP_LO12S2:
6109
    /* Instruction related relocs must handle endian properly.  */
6110
    /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
6111
0
    r = nds32_elf_final_link_relocate (howto, input_bfd,
6112
0
               input_section, contents,
6113
0
               rel->r_offset, relocation,
6114
0
               rel->r_addend);
6115
0
    break;
6116
6117
0
  default:
6118
    /* All other relocs can use default handler.  */
6119
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6120
0
          contents, rel->r_offset,
6121
0
          relocation, rel->r_addend);
6122
0
    break;
6123
0
  }
6124
6125
0
    check_reloc:
6126
6127
0
      if (r != bfd_reloc_ok)
6128
0
  {
6129
    /* FIXME: This should be generic enough to go in a utility.  */
6130
0
    const char *name;
6131
6132
0
    if (h != NULL)
6133
0
      name = h->root.root.string;
6134
0
    else
6135
0
      {
6136
0
        name = bfd_elf_string_from_elf_section
6137
0
          (input_bfd, symtab_hdr->sh_link, sym->st_name);
6138
0
        if (name == NULL || *name == '\0')
6139
0
    name = bfd_section_name (sec);
6140
0
      }
6141
6142
0
    if (errmsg != NULL)
6143
0
      goto common_error;
6144
6145
0
    switch (r)
6146
0
      {
6147
0
      case bfd_reloc_overflow:
6148
0
        (*info->callbacks->reloc_overflow)
6149
0
    (info, (h ? &h->root : NULL), name, howto->name,
6150
0
     (bfd_vma) 0, input_bfd, input_section, offset);
6151
0
        break;
6152
6153
0
      case bfd_reloc_undefined:
6154
0
        (*info->callbacks->undefined_symbol)
6155
0
    (info, name, input_bfd, input_section, offset, true);
6156
0
        break;
6157
6158
0
      case bfd_reloc_outofrange:
6159
0
        errmsg = _("internal error: out of range error");
6160
0
        goto common_error;
6161
6162
0
      case bfd_reloc_notsupported:
6163
0
        errmsg = _("internal error: unsupported relocation error");
6164
0
        goto common_error;
6165
6166
0
      case bfd_reloc_dangerous:
6167
0
        errmsg = _("internal error: dangerous error");
6168
0
        goto common_error;
6169
6170
0
      default:
6171
0
        errmsg = _("internal error: unknown error");
6172
        /* Fall through.  */
6173
6174
0
      common_error:
6175
0
        (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6176
0
             input_section, offset);
6177
0
        break;
6178
0
      }
6179
0
  }
6180
0
    }
6181
6182
  /* Resotre header size to avoid overflow load.  */
6183
0
  if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6184
0
    symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6185
6186
0
  return ret;
6187
0
}
6188
6189
/* Finish up dynamic symbol handling.  We set the contents of various
6190
   dynamic sections here.  */
6191
6192
static bool
6193
nds32_elf_finish_dynamic_symbol (struct bfd_link_info *info,
6194
         struct elf_link_hash_entry *h,
6195
         Elf_Internal_Sym *sym)
6196
0
{
6197
0
  struct elf_link_hash_table *ehtab;
6198
0
  struct elf_nds32_link_hash_entry *hent;
6199
0
  bfd_byte *loc;
6200
6201
0
  ehtab = elf_hash_table (info);
6202
0
  hent = (struct elf_nds32_link_hash_entry *) h;
6203
6204
0
  if (h->plt.offset != (bfd_vma) - 1)
6205
0
    {
6206
0
      asection *splt;
6207
0
      asection *sgot;
6208
0
      asection *srela;
6209
6210
0
      bfd_vma plt_index;
6211
0
      bfd_vma got_offset;
6212
0
      bfd_vma local_plt_offset;
6213
0
      Elf_Internal_Rela rela;
6214
6215
      /* This symbol has an entry in the procedure linkage table.  Set
6216
   it up.  */
6217
6218
0
      BFD_ASSERT (h->dynindx != -1);
6219
6220
0
      splt = ehtab->splt;
6221
0
      sgot = ehtab->sgotplt;
6222
0
      srela = ehtab->srelplt;
6223
0
      BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6224
6225
      /* Get the index in the procedure linkage table which
6226
   corresponds to this symbol.  This is the index of this symbol
6227
   in all the symbols for which we are making plt entries.  The
6228
   first entry in the procedure linkage table is reserved.  */
6229
0
      plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6230
6231
      /* Get the offset into the .got table of the entry that
6232
   corresponds to this function.  Each .got entry is 4 bytes.
6233
   The first three are reserved.  */
6234
0
      got_offset = (plt_index + 3) * 4;
6235
6236
      /* Fill in the entry in the procedure linkage table.  */
6237
0
      if (!bfd_link_pic (info))
6238
0
  {
6239
0
    unsigned long insn;
6240
6241
0
    insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6242
0
              + sgot->output_offset + got_offset) >> 12)
6243
0
            & 0xfffff);
6244
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6245
6246
0
    insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6247
0
              + sgot->output_offset + got_offset) & 0x0fff)
6248
0
            >> 2);
6249
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6250
6251
0
    insn = PLT_ENTRY_WORD2;
6252
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6253
6254
0
    insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6255
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6256
6257
0
    insn = PLT_ENTRY_WORD4
6258
0
     + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6259
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6260
0
    local_plt_offset = 12;
6261
0
  }
6262
0
      else
6263
0
  {
6264
    /* sda_base must be set at this time.  */
6265
0
    unsigned long insn;
6266
0
    long offset;
6267
6268
0
    offset = sgot->output_section->vma + sgot->output_offset + got_offset
6269
0
       - elf_gp (info->output_bfd);
6270
0
    insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6271
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6272
6273
0
    insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6274
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6275
6276
0
    insn = PLT_PIC_ENTRY_WORD2;
6277
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6278
6279
0
    insn = PLT_PIC_ENTRY_WORD3;
6280
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6281
6282
0
    insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6283
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6284
6285
0
    insn = PLT_PIC_ENTRY_WORD5
6286
0
      + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6287
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6288
6289
0
    local_plt_offset = 16;
6290
0
  }
6291
6292
      /* Fill in the entry in the global offset table,
6293
   so it will fall through to the next instruction for the first time.  */
6294
0
      bfd_put_32 (info->output_bfd,
6295
0
      (splt->output_section->vma + splt->output_offset
6296
0
       + h->plt.offset + local_plt_offset),
6297
0
      sgot->contents + got_offset);
6298
6299
      /* Fill in the entry in the .rela.plt section.  */
6300
0
      rela.r_offset = (sgot->output_section->vma
6301
0
           + sgot->output_offset + got_offset);
6302
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6303
0
      rela.r_addend = 0;
6304
0
      loc = srela->contents;
6305
0
      loc += plt_index * sizeof (Elf32_External_Rela);
6306
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6307
6308
0
      if (!h->def_regular)
6309
0
  {
6310
    /* Mark the symbol as undefined, rather than as defined in
6311
       the .plt section.  Leave the value alone.  */
6312
0
    sym->st_shndx = SHN_UNDEF;
6313
0
    if (!h->ref_regular_nonweak)
6314
0
      sym->st_value = 0;
6315
0
  }
6316
0
    }
6317
6318
0
  if (h->got.offset != (bfd_vma) - 1
6319
0
      && hent->tls_type == GOT_NORMAL)
6320
0
    {
6321
0
      asection *sgot;
6322
0
      asection *srelagot;
6323
0
      Elf_Internal_Rela rela;
6324
6325
      /* This symbol has an entry in the global offset table.
6326
   Set it up.  */
6327
6328
0
      sgot = ehtab->sgot;
6329
0
      srelagot = ehtab->srelgot;
6330
0
      BFD_ASSERT (sgot != NULL && srelagot != NULL);
6331
6332
0
      rela.r_offset = (sgot->output_section->vma
6333
0
           + sgot->output_offset + (h->got.offset & ~1));
6334
6335
      /* If this is a -Bsymbolic link, and the symbol is defined
6336
   locally, we just want to emit a RELATIVE reloc.  Likewise if
6337
   the symbol was forced to be local because of a version file.
6338
   The entry in the global offset table will already have been
6339
   initialized in the relocate_section function.  */
6340
0
      if ((bfd_link_pic (info)
6341
0
     && (info->symbolic || h->dynindx == -1 || h->forced_local)
6342
0
     && h->def_regular)
6343
0
    || (bfd_link_pie (info) && h->def_regular))
6344
0
  {
6345
0
    rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6346
0
    rela.r_addend = (h->root.u.def.value
6347
0
         + h->root.u.def.section->output_section->vma
6348
0
         + h->root.u.def.section->output_offset);
6349
6350
0
    if ((h->got.offset & 1) == 0)
6351
0
      {
6352
0
        bfd_put_32 (info->output_bfd, rela.r_addend,
6353
0
        sgot->contents + h->got.offset);
6354
0
      }
6355
0
  }
6356
0
      else
6357
0
  {
6358
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
6359
0
    bfd_put_32 (info->output_bfd, (bfd_vma) 0,
6360
0
          sgot->contents + h->got.offset);
6361
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6362
0
    rela.r_addend = 0;
6363
0
  }
6364
6365
0
      loc = srelagot->contents;
6366
0
      loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6367
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6368
0
      ++srelagot->reloc_count;
6369
0
      BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6370
0
    }
6371
6372
0
  if (h->needs_copy)
6373
0
    {
6374
0
      asection *s;
6375
0
      Elf_Internal_Rela rela;
6376
6377
      /* This symbols needs a copy reloc.  Set it up.  */
6378
6379
0
      BFD_ASSERT (h->dynindx != -1
6380
0
      && (h->root.type == bfd_link_hash_defined
6381
0
          || h->root.type == bfd_link_hash_defweak));
6382
6383
0
      s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6384
0
      BFD_ASSERT (s != NULL);
6385
6386
0
      rela.r_offset = (h->root.u.def.value
6387
0
           + h->root.u.def.section->output_section->vma
6388
0
           + h->root.u.def.section->output_offset);
6389
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6390
0
      rela.r_addend = 0;
6391
0
      loc = s->contents;
6392
0
      loc += s->reloc_count * sizeof (Elf32_External_Rela);
6393
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6394
0
      ++s->reloc_count;
6395
0
    }
6396
6397
  /* Mark some specially defined symbols as absolute.  */
6398
0
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6399
0
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6400
0
    sym->st_shndx = SHN_ABS;
6401
6402
0
  return true;
6403
0
}
6404
6405
6406
/* Finish up the dynamic sections.  */
6407
6408
static bool
6409
nds32_elf_finish_dynamic_sections (struct bfd_link_info *info,
6410
           bfd_byte *buf ATTRIBUTE_UNUSED)
6411
0
{
6412
0
  bfd *dynobj;
6413
0
  asection *sdyn;
6414
0
  asection *sgotplt;
6415
0
  struct elf_link_hash_table *ehtab;
6416
0
  struct elf_nds32_link_hash_table *htab;
6417
6418
0
  ehtab = elf_hash_table (info);
6419
0
  htab = nds32_elf_hash_table (info);
6420
0
  if (htab == NULL)
6421
0
    return false;
6422
6423
0
  dynobj = elf_hash_table (info)->dynobj;
6424
6425
0
  sgotplt = ehtab->sgotplt;
6426
  /* A broken linker script might have discarded the dynamic sections.
6427
     Catch this here so that we do not seg-fault later on.  */
6428
0
  if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6429
0
    return false;
6430
0
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6431
6432
0
  if (elf_hash_table (info)->dynamic_sections_created)
6433
0
    {
6434
0
      asection *splt;
6435
0
      Elf32_External_Dyn *dyncon, *dynconend;
6436
6437
0
      BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6438
6439
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
6440
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6441
6442
0
      for (; dyncon < dynconend; dyncon++)
6443
0
  {
6444
0
    Elf_Internal_Dyn dyn;
6445
0
    asection *s;
6446
6447
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6448
6449
0
    switch (dyn.d_tag)
6450
0
      {
6451
0
      default:
6452
0
        break;
6453
6454
0
      case DT_PLTGOT:
6455
        /* name = ".got";  */
6456
0
        s = ehtab->sgot;
6457
0
        goto get_vma;
6458
0
      case DT_JMPREL:
6459
0
        s = ehtab->srelplt;
6460
0
      get_vma:
6461
0
        BFD_ASSERT (s != NULL);
6462
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6463
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6464
0
        break;
6465
6466
0
      case DT_PLTRELSZ:
6467
0
        s = ehtab->srelplt;
6468
0
        BFD_ASSERT (s != NULL);
6469
0
        dyn.d_un.d_val = s->size;
6470
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6471
0
        break;
6472
6473
0
      case DT_RELASZ:
6474
        /* My reading of the SVR4 ABI indicates that the
6475
     procedure linkage table relocs (DT_JMPREL) should be
6476
     included in the overall relocs (DT_RELA).  This is
6477
     what Solaris does.  However, UnixWare can not handle
6478
     that case.  Therefore, we override the DT_RELASZ entry
6479
     here to make it not include the JMPREL relocs.  Since
6480
     the linker script arranges for .rela.plt to follow all
6481
     other relocation sections, we don't have to worry
6482
     about changing the DT_RELA entry.  */
6483
0
        if (ehtab->srelplt != NULL)
6484
0
    {
6485
0
      s = ehtab->srelplt;
6486
0
      dyn.d_un.d_val -= s->size;
6487
0
    }
6488
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6489
0
        break;
6490
6491
0
      case DT_TLSDESC_PLT:
6492
0
        s = htab->root.splt;
6493
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6494
0
        + htab->root.tlsdesc_plt);
6495
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6496
0
        break;
6497
6498
0
      case DT_TLSDESC_GOT:
6499
0
        s = htab->root.sgot;
6500
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6501
0
        + htab->root.tlsdesc_got);
6502
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6503
0
        break;
6504
0
      }
6505
0
  }
6506
6507
      /* Fill in the first entry in the procedure linkage table.  */
6508
0
      splt = ehtab->splt;
6509
0
      if (splt && splt->size > 0)
6510
0
  {
6511
0
    if (bfd_link_pic (info))
6512
0
      {
6513
0
        unsigned long insn;
6514
0
        long offset;
6515
6516
0
        offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6517
0
    - elf_gp (info->output_bfd);
6518
0
        insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6519
0
        bfd_putb32 (insn, splt->contents);
6520
6521
        /* here has a typo?  */
6522
0
        insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6523
0
        bfd_putb32 (insn, splt->contents + 4);
6524
6525
0
        insn = PLT0_PIC_ENTRY_WORD2;
6526
0
        bfd_putb32 (insn, splt->contents + 8);
6527
6528
0
        insn = PLT0_PIC_ENTRY_WORD3;
6529
0
        bfd_putb32 (insn, splt->contents + 12);
6530
6531
0
        insn = PLT0_PIC_ENTRY_WORD4;
6532
0
        bfd_putb32 (insn, splt->contents + 16);
6533
6534
0
        insn = PLT0_PIC_ENTRY_WORD5;
6535
0
        bfd_putb32 (insn, splt->contents + 20);
6536
0
      }
6537
0
    else
6538
0
      {
6539
0
        unsigned long insn;
6540
0
        unsigned long addr;
6541
6542
        /* addr = .got + 4 */
6543
0
        addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6544
0
        insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6545
0
        bfd_putb32 (insn, splt->contents);
6546
6547
0
        insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6548
0
        bfd_putb32 (insn, splt->contents + 4);
6549
6550
0
        insn = PLT0_ENTRY_WORD2;
6551
0
        bfd_putb32 (insn, splt->contents + 8);
6552
6553
0
        insn = PLT0_ENTRY_WORD3;
6554
0
        bfd_putb32 (insn, splt->contents + 12);
6555
6556
0
        insn = PLT0_ENTRY_WORD4;
6557
0
        bfd_putb32 (insn, splt->contents + 16);
6558
0
      }
6559
6560
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6561
0
      PLT_ENTRY_SIZE;
6562
0
  }
6563
6564
0
      if (htab->root.tlsdesc_plt)
6565
0
  {
6566
    /* Calculate addresses.  */
6567
0
    asection *sgot = sgot = ehtab->sgot;
6568
0
    bfd_vma pltgot = sgotplt->output_section->vma
6569
0
      + sgotplt->output_offset;
6570
0
    bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6571
0
      + htab->root.tlsdesc_got;
6572
6573
    /* Get GP offset.  */
6574
0
    pltgot -= elf_gp (info->output_bfd) - 4; /* PLTGOT[1]  */
6575
0
    tlsdesc_got -= elf_gp (info->output_bfd);
6576
6577
    /* Do relocation.  */
6578
0
    dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6579
0
    dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6580
0
    dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6581
0
    dl_tlsdesc_lazy_trampoline[5] +=  0xfff & pltgot;
6582
6583
    /* Insert .plt.  */
6584
0
    nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
6585
0
        dl_tlsdesc_lazy_trampoline,
6586
0
        ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6587
0
  }
6588
0
    }
6589
6590
  /* Fill in the first three entries in the global offset table.  */
6591
0
  if (sgotplt && sgotplt->size > 0)
6592
0
    {
6593
0
      if (sdyn == NULL)
6594
0
  bfd_put_32 (info->output_bfd, 0, sgotplt->contents);
6595
0
      else
6596
0
  bfd_put_32 (info->output_bfd,
6597
0
        sdyn->output_section->vma + sdyn->output_offset,
6598
0
        sgotplt->contents);
6599
0
      bfd_put_32 (info->output_bfd, 0, sgotplt->contents + 4);
6600
0
      bfd_put_32 (info->output_bfd, 0, sgotplt->contents + 8);
6601
6602
0
      elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6603
0
    }
6604
6605
0
  return true;
6606
0
}
6607

6608
6609
/* Set the right machine number.  */
6610
6611
static bool
6612
nds32_elf_object_p (bfd *abfd)
6613
1.68k
{
6614
1.68k
  static unsigned int cur_arch = 0;
6615
6616
1.68k
  if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6617
1.64k
    {
6618
      /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
6619
1.64k
      cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6620
1.64k
    }
6621
6622
1.68k
  switch (cur_arch)
6623
1.68k
    {
6624
1.57k
    default:
6625
1.57k
    case E_N1_ARCH:
6626
1.57k
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6627
1.57k
      break;
6628
12
    case E_N1H_ARCH:
6629
12
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6630
12
      break;
6631
84
    case E_NDS_ARCH_STAR_V2_0:
6632
84
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6633
84
      break;
6634
11
    case E_NDS_ARCH_STAR_V3_0:
6635
11
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6636
11
      break;
6637
6
    case E_NDS_ARCH_STAR_V3_M:
6638
6
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6639
6
      break;
6640
1.68k
    }
6641
6642
1.68k
  return true;
6643
1.68k
}
6644
6645
/* Store the machine number in the flags field.  */
6646
6647
static bool
6648
nds32_elf_final_write_processing (bfd *abfd)
6649
3
{
6650
3
  unsigned long val;
6651
3
  static unsigned int cur_mach = 0;
6652
6653
3
  if (bfd_mach_n1 != bfd_get_mach (abfd))
6654
0
    {
6655
0
      cur_mach = bfd_get_mach (abfd);
6656
0
    }
6657
6658
3
  switch (cur_mach)
6659
3
    {
6660
0
    case bfd_mach_n1:
6661
      /* Only happen when object is empty, since the case is abandon.  */
6662
0
      val = E_N1_ARCH;
6663
0
      val |= E_NDS_ABI_AABI;
6664
0
      val |= E_NDS32_ELF_VER_1_4;
6665
0
      break;
6666
0
    case bfd_mach_n1h:
6667
0
      val = E_N1H_ARCH;
6668
0
      break;
6669
0
    case bfd_mach_n1h_v2:
6670
0
      val = E_NDS_ARCH_STAR_V2_0;
6671
0
      break;
6672
0
    case bfd_mach_n1h_v3:
6673
0
      val = E_NDS_ARCH_STAR_V3_0;
6674
0
      break;
6675
0
    case bfd_mach_n1h_v3m:
6676
0
      val = E_NDS_ARCH_STAR_V3_M;
6677
0
      break;
6678
3
    default:
6679
3
      val = 0;
6680
3
      break;
6681
3
    }
6682
6683
3
  elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6684
3
  elf_elfheader (abfd)->e_flags |= val;
6685
3
  return _bfd_elf_final_write_processing (abfd);
6686
3
}
6687
6688
/* Function to keep NDS32 specific file flags.  */
6689
6690
static bool
6691
nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6692
0
{
6693
0
  BFD_ASSERT (!elf_flags_init (abfd)
6694
0
        || elf_elfheader (abfd)->e_flags == flags);
6695
6696
0
  elf_elfheader (abfd)->e_flags = flags;
6697
0
  elf_flags_init (abfd) = true;
6698
0
  return true;
6699
0
}
6700
6701
static unsigned int
6702
convert_e_flags (unsigned int e_flags, unsigned int arch)
6703
0
{
6704
0
  if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6705
0
    {
6706
      /* From 0.9 to 1.0.  */
6707
0
      e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6708
6709
      /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6710
0
      e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6711
0
      if (arch == E_NDS_ARCH_STAR_V1_0)
6712
0
  {
6713
    /* Done.  */
6714
0
    return e_flags;
6715
0
  }
6716
0
    }
6717
6718
  /* From 1.0 to 2.0.  */
6719
0
  e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6720
6721
  /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
6722
0
  e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6723
6724
  /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6725
0
  e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6726
0
  return e_flags;
6727
0
}
6728
6729
static bool
6730
nds32_check_vec_size (bfd *ibfd)
6731
0
{
6732
0
  static unsigned int nds32_vec_size = 0;
6733
6734
0
  asection *sec_t = NULL;
6735
0
  bfd_byte *contents = NULL;
6736
6737
0
  sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6738
6739
0
  if (sec_t && sec_t->size >= 4)
6740
0
    {
6741
      /* Get vec_size in file.  */
6742
0
      unsigned int flag_t;
6743
6744
0
      nds32_get_section_contents (ibfd, sec_t, &contents, true);
6745
0
      flag_t = bfd_get_32 (ibfd, contents);
6746
6747
      /* The value could only be 4 or 16.  */
6748
6749
0
      if (!nds32_vec_size)
6750
  /* Set if not set yet.  */
6751
0
  nds32_vec_size = (flag_t & 0x3);
6752
0
      else if (nds32_vec_size != (flag_t & 0x3))
6753
0
  {
6754
0
    _bfd_error_handler
6755
      /* xgettext:c-format */
6756
0
      (_("%pB: ISR vector size mismatch"
6757
0
         " with previous modules, previous %u-byte, current %u-byte"),
6758
0
       ibfd,
6759
0
       nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6760
0
       (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6761
0
    return false;
6762
0
  }
6763
0
      else
6764
  /* Only keep the first vec_size section.  */
6765
0
  sec_t->flags |= SEC_EXCLUDE;
6766
0
    }
6767
6768
0
  return true;
6769
0
}
6770
6771
/* Merge backend specific data from an object file to the output
6772
   object file when linking.  */
6773
6774
static bool
6775
nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6776
0
{
6777
0
  bfd *obfd = info->output_bfd;
6778
0
  flagword out_flags;
6779
0
  flagword in_flags;
6780
0
  flagword out_16regs;
6781
0
  flagword in_no_mac;
6782
0
  flagword out_no_mac;
6783
0
  flagword in_16regs;
6784
0
  flagword out_version;
6785
0
  flagword in_version;
6786
0
  flagword out_fpu_config;
6787
0
  flagword in_fpu_config;
6788
6789
  /* FIXME: What should be checked when linking shared libraries?  */
6790
0
  if ((ibfd->flags & DYNAMIC) != 0)
6791
0
    return true;
6792
6793
  /* TODO: Revise to use object-attributes instead.  */
6794
0
  if (!nds32_check_vec_size (ibfd))
6795
0
    return false;
6796
6797
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
6798
0
    return true;
6799
6800
0
  if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6801
0
    {
6802
0
      _bfd_error_handler
6803
0
  (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6804
6805
0
      bfd_set_error (bfd_error_bad_value);
6806
0
      return false;
6807
0
    }
6808
6809
  /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6810
     treat as generic one without checking and merging.  */
6811
0
  if (elf_elfheader (ibfd)->e_flags)
6812
0
    {
6813
0
      in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6814
0
      if (in_version == E_NDS32_ELF_VER_1_2)
6815
0
  {
6816
0
    _bfd_error_handler
6817
0
      (_("%pB: warning: older version of object file encountered, "
6818
0
         "please recompile with current tool chain"), ibfd);
6819
0
  }
6820
6821
      /* We may need to merge V1 and V2 arch object files to V2.  */
6822
0
      if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6823
0
    != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6824
0
  {
6825
    /* Need to convert version.  */
6826
0
    if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6827
0
        == E_NDS_ARCH_STAR_RESERVED)
6828
0
      {
6829
0
        elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6830
0
      }
6831
0
    else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6832
0
       == E_NDS_ARCH_STAR_V3_M
6833
0
       && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6834
0
       == E_NDS_ARCH_STAR_V3_0)
6835
0
      {
6836
0
        elf_elfheader (ibfd)->e_flags =
6837
0
    (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6838
0
    | E_NDS_ARCH_STAR_V3_0;
6839
0
      }
6840
0
    else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6841
0
       == E_NDS_ARCH_STAR_V0_9
6842
0
       || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6843
0
       > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6844
0
      {
6845
0
        elf_elfheader (obfd)->e_flags =
6846
0
    convert_e_flags (elf_elfheader (obfd)->e_flags,
6847
0
         (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6848
0
      }
6849
0
    else
6850
0
      {
6851
0
        elf_elfheader (ibfd)->e_flags =
6852
0
    convert_e_flags (elf_elfheader (ibfd)->e_flags,
6853
0
         (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6854
0
      }
6855
0
  }
6856
6857
      /* Extract some flags.  */
6858
0
      in_flags = elf_elfheader (ibfd)->e_flags
6859
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6860
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6861
6862
      /* The following flags need special treatment.  */
6863
0
      in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6864
0
      in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6865
0
      in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6866
6867
      /* Extract some flags.  */
6868
0
      out_flags = elf_elfheader (obfd)->e_flags
6869
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6870
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6871
6872
      /* The following flags need special treatment.  */
6873
0
      out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6874
0
      out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6875
0
      out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6876
0
      out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6877
0
      if (!elf_flags_init (obfd))
6878
0
  {
6879
    /* If the input is the default architecture then do not
6880
       bother setting the flags for the output architecture,
6881
       instead allow future merges to do this.  If no future
6882
       merges ever set these flags then they will retain their
6883
       unitialised values, which surprise surprise, correspond
6884
       to the default values.  */
6885
0
    if (bfd_get_arch_info (ibfd)->the_default)
6886
0
      return true;
6887
6888
0
    elf_flags_init (obfd) = true;
6889
0
    elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6890
6891
0
    if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6892
0
        && bfd_get_arch_info (obfd)->the_default)
6893
0
      {
6894
0
        return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6895
0
          bfd_get_mach (ibfd));
6896
0
      }
6897
6898
0
    return true;
6899
0
  }
6900
6901
      /* Check flag compatibility.  */
6902
0
      if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6903
0
  {
6904
0
    _bfd_error_handler
6905
0
      (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6906
0
    bfd_set_error (bfd_error_bad_value);
6907
0
    return false;
6908
0
  }
6909
6910
0
      if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6911
0
  {
6912
0
    if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6913
0
      {
6914
0
        _bfd_error_handler
6915
0
    (_("%pB: error: instruction set mismatch with previous modules"),
6916
0
     ibfd);
6917
6918
0
        bfd_set_error (bfd_error_bad_value);
6919
0
        return false;
6920
0
      }
6921
0
  }
6922
6923
      /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6924
   and perf ext1 and DIV are mergerd to perf ext1.  */
6925
0
      if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6926
0
  {
6927
0
    elf_elfheader (obfd)->e_flags =
6928
0
      (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6929
0
      | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6930
0
      | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6931
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6932
0
      | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6933
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6934
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6935
0
      | ((in_version > out_version) ? out_version : in_version);
6936
0
  }
6937
0
      else
6938
0
  {
6939
0
    if (in_version != out_version)
6940
0
      _bfd_error_handler
6941
        /* xgettext:c-format */
6942
0
        (_("%pB: warning: incompatible elf-versions %s and %s"),
6943
0
         ibfd, nds32_elfver_strtab[out_version],
6944
0
         nds32_elfver_strtab[in_version]);
6945
6946
0
    elf_elfheader (obfd)->e_flags = in_flags | out_flags
6947
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6948
0
      | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6949
0
      | (in_version > out_version ?  out_version : in_version);
6950
0
  }
6951
0
    }
6952
6953
0
  return true;
6954
0
}
6955
6956
/* Display the flags field.  */
6957
6958
static bool
6959
nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6960
377
{
6961
377
  FILE *file = (FILE *) ptr;
6962
6963
377
  BFD_ASSERT (abfd != NULL && ptr != NULL);
6964
6965
377
  _bfd_elf_print_private_bfd_data (abfd, ptr);
6966
6967
377
  fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6968
6969
377
  switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6970
377
    {
6971
377
    default:
6972
377
    case E_N1_ARCH:
6973
377
      fprintf (file, _(": n1 instructions"));
6974
377
      break;
6975
0
    case E_N1H_ARCH:
6976
0
      fprintf (file, _(": n1h instructions"));
6977
0
      break;
6978
377
    }
6979
6980
377
  fputc ('\n', file);
6981
6982
377
  return true;
6983
377
}
6984
6985
static unsigned int
6986
nds32_elf_action_discarded (asection *sec)
6987
0
{
6988
6989
0
  if (startswith (sec->name, ".gcc_except_table"))
6990
0
    return 0;
6991
6992
0
  return _bfd_elf_default_action_discarded (sec);
6993
0
}
6994
6995
static asection *
6996
nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6997
      struct elf_reloc_cookie *cookie,
6998
      struct elf_link_hash_entry *h,
6999
      unsigned int symndx)
7000
0
{
7001
0
  if (h != NULL)
7002
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
7003
0
      {
7004
0
      case R_NDS32_GNU_VTINHERIT:
7005
0
      case R_NDS32_GNU_VTENTRY:
7006
0
      case R_NDS32_RELA_GNU_VTINHERIT:
7007
0
      case R_NDS32_RELA_GNU_VTENTRY:
7008
0
  return NULL;
7009
0
      }
7010
7011
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
7012
0
}
7013
7014
static enum elf_nds32_tls_type
7015
get_tls_type (enum elf_nds32_reloc_type r_type,
7016
        struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7017
0
{
7018
0
  enum elf_nds32_tls_type tls_type;
7019
7020
0
  switch (r_type)
7021
0
    {
7022
0
    case R_NDS32_TLS_LE_HI20:
7023
0
    case R_NDS32_TLS_LE_LO12:
7024
0
      tls_type = GOT_TLS_LE;
7025
0
      break;
7026
0
    case R_NDS32_TLS_IE_HI20:
7027
0
    case R_NDS32_TLS_IE_LO12S2:
7028
0
    case R_NDS32_TLS_IE_LO12:
7029
0
      tls_type = GOT_TLS_IE;
7030
0
      break;
7031
0
    case R_NDS32_TLS_IEGP_HI20:
7032
0
    case R_NDS32_TLS_IEGP_LO12:
7033
0
    case R_NDS32_TLS_IEGP_LO12S2:
7034
0
      tls_type = GOT_TLS_IEGP;
7035
0
      break;
7036
0
    case R_NDS32_TLS_DESC_HI20:
7037
0
    case R_NDS32_TLS_DESC_LO12:
7038
0
    case R_NDS32_TLS_DESC_ADD:
7039
0
    case R_NDS32_TLS_DESC_FUNC:
7040
0
    case R_NDS32_TLS_DESC_CALL:
7041
0
      tls_type = GOT_TLS_DESC;
7042
0
      break;
7043
0
    default:
7044
0
      tls_type = GOT_NORMAL;
7045
0
      break;
7046
0
    }
7047
7048
0
  return tls_type;
7049
0
}
7050
7051
/* Ensure that we have allocated bookkeeping structures for ABFD's local
7052
   symbols.  */
7053
7054
static bool
7055
elf32_nds32_allocate_local_sym_info (bfd *abfd)
7056
0
{
7057
0
  if (elf_local_got_refcounts (abfd) == NULL)
7058
0
    {
7059
0
      bfd_size_type num_syms;
7060
0
      bfd_size_type size;
7061
0
      char *data;
7062
7063
0
      num_syms = elf_symtab_hdr (abfd).sh_info;
7064
      /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7065
   gp_offset.  The details can refer to struct elf_nds32_obj_tdata.  */
7066
0
      size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7067
0
       + sizeof (bfd_vma) + sizeof (int)
7068
0
       + sizeof (bool) + sizeof (bfd_vma));
7069
0
      data = bfd_zalloc (abfd, size);
7070
0
      if (data == NULL)
7071
0
  return false;
7072
7073
0
      elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7074
0
      data += num_syms * sizeof (bfd_signed_vma);
7075
7076
0
      elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7077
0
      data += num_syms * sizeof (char);
7078
7079
0
      elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7080
0
      data += num_syms * sizeof (bfd_vma);
7081
7082
0
      elf32_nds32_local_gp_offset (abfd) = (int *) data;
7083
0
      data += num_syms * sizeof (int);
7084
0
    }
7085
7086
0
  return true;
7087
0
}
7088
7089
/* Look through the relocs for a section during the first phase.
7090
   Since we don't do .gots or .plts, we just need to consider the
7091
   virtual table relocs for gc.  */
7092
7093
static bool
7094
nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7095
      asection *sec, const Elf_Internal_Rela *relocs)
7096
0
{
7097
0
  Elf_Internal_Shdr *symtab_hdr;
7098
0
  struct elf_link_hash_entry **sym_hashes;
7099
0
  const Elf_Internal_Rela *rel;
7100
0
  const Elf_Internal_Rela *rel_end;
7101
0
  struct elf_link_hash_table *ehtab;
7102
0
  struct elf_nds32_link_hash_table *htab;
7103
0
  bfd *dynobj;
7104
0
  asection *sreloc = NULL;
7105
7106
  /* No need for relocation if relocatable already.  */
7107
0
  if (bfd_link_relocatable (info))
7108
0
    {
7109
0
      elf32_nds32_check_relax_group (abfd, sec);
7110
0
      return true;
7111
0
    }
7112
7113
0
  symtab_hdr = &elf_symtab_hdr (abfd);
7114
0
  sym_hashes = elf_sym_hashes (abfd);
7115
7116
0
  ehtab = elf_hash_table (info);
7117
0
  htab = nds32_elf_hash_table (info);
7118
0
  dynobj = htab->root.dynobj;
7119
7120
0
  rel_end = relocs + sec->reloc_count;
7121
0
  for (rel = relocs; rel < rel_end; rel++)
7122
0
    {
7123
0
      enum elf_nds32_reloc_type r_type;
7124
0
      struct elf_link_hash_entry *h;
7125
0
      unsigned long r_symndx;
7126
0
      enum elf_nds32_tls_type tls_type, old_tls_type;
7127
7128
0
      r_symndx = ELF32_R_SYM (rel->r_info);
7129
0
      r_type = ELF32_R_TYPE (rel->r_info);
7130
0
      if (r_symndx < symtab_hdr->sh_info)
7131
0
  h = NULL;
7132
0
      else
7133
0
  {
7134
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7135
0
    while (h->root.type == bfd_link_hash_indirect
7136
0
     || h->root.type == bfd_link_hash_warning)
7137
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
7138
0
  }
7139
7140
      /* Create .got section if necessary.
7141
   Some relocs require a global offset table.  We create
7142
   got section here, since these relocation need a got section
7143
   and if it is not created yet.  */
7144
0
      if (ehtab->sgot == NULL)
7145
0
  {
7146
0
    switch (r_type)
7147
0
      {
7148
0
      case R_NDS32_GOT_HI20:
7149
0
      case R_NDS32_GOT_LO12:
7150
0
      case R_NDS32_GOT_LO15:
7151
0
      case R_NDS32_GOT_LO19:
7152
0
      case R_NDS32_GOT17S2_RELA:
7153
0
      case R_NDS32_GOT15S2_RELA:
7154
0
      case R_NDS32_GOTOFF:
7155
0
      case R_NDS32_GOTOFF_HI20:
7156
0
      case R_NDS32_GOTOFF_LO12:
7157
0
      case R_NDS32_GOTOFF_LO15:
7158
0
      case R_NDS32_GOTOFF_LO19:
7159
0
      case R_NDS32_GOTPC20:
7160
0
      case R_NDS32_GOTPC_HI20:
7161
0
      case R_NDS32_GOTPC_LO12:
7162
0
      case R_NDS32_GOT20:
7163
0
      case R_NDS32_TLS_IE_HI20:
7164
0
      case R_NDS32_TLS_IE_LO12:
7165
0
      case R_NDS32_TLS_IE_LO12S2:
7166
0
      case R_NDS32_TLS_IEGP_HI20:
7167
0
      case R_NDS32_TLS_IEGP_LO12:
7168
0
      case R_NDS32_TLS_IEGP_LO12S2:
7169
0
      case R_NDS32_TLS_DESC_HI20:
7170
0
      case R_NDS32_TLS_DESC_LO12:
7171
0
        if (dynobj == NULL)
7172
0
    {
7173
0
      dynobj = _bfd_elf_link_dynobj (info);
7174
0
      if (dynobj == NULL)
7175
0
        return false;
7176
0
    }
7177
0
        if (!create_got_section (dynobj, info))
7178
0
    return false;
7179
0
        break;
7180
7181
0
      default:
7182
0
        break;
7183
0
      }
7184
0
  }
7185
7186
      /* Check relocation type.  */
7187
0
      switch ((int) r_type)
7188
0
  {
7189
0
  case R_NDS32_GOT_HI20:
7190
0
  case R_NDS32_GOT_LO12:
7191
0
  case R_NDS32_GOT_LO15:
7192
0
  case R_NDS32_GOT_LO19:
7193
0
  case R_NDS32_GOT20:
7194
0
  case R_NDS32_TLS_LE_HI20:
7195
0
  case R_NDS32_TLS_LE_LO12:
7196
0
  case R_NDS32_TLS_IE_HI20:
7197
0
  case R_NDS32_TLS_IE_LO12:
7198
0
  case R_NDS32_TLS_IE_LO12S2:
7199
0
  case R_NDS32_TLS_IEGP_HI20:
7200
0
  case R_NDS32_TLS_IEGP_LO12:
7201
0
  case R_NDS32_TLS_IEGP_LO12S2:
7202
0
  case R_NDS32_TLS_DESC_HI20:
7203
0
  case R_NDS32_TLS_DESC_LO12:
7204
0
    tls_type = get_tls_type (r_type, h);
7205
0
    if (h)
7206
0
      {
7207
0
        if (tls_type != GOT_TLS_LE)
7208
0
    h->got.refcount += 1;
7209
0
        old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7210
0
      }
7211
0
    else
7212
0
      {
7213
        /* This is a global offset table entry for a local symbol.  */
7214
0
        if (!elf32_nds32_allocate_local_sym_info (abfd))
7215
0
    return false;
7216
7217
0
        BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7218
0
        if (tls_type != GOT_TLS_LE)
7219
0
    elf_local_got_refcounts (abfd)[r_symndx] += 1;
7220
0
        old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7221
0
      }
7222
7223
    /* We would already have issued an error message if there
7224
       is a TLS/non-TLS mismatch, based on the symbol
7225
       type.  So just combine any TLS types needed.  */
7226
0
    if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7227
0
        && tls_type != GOT_NORMAL)
7228
0
      tls_type |= old_tls_type;
7229
7230
    /* DESC to IE/IEGP if link to executable.  */
7231
0
    if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7232
0
        && (bfd_link_executable (info)))
7233
0
      tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7234
7235
0
    if (old_tls_type != tls_type)
7236
0
      {
7237
0
        if (h != NULL)
7238
0
    elf32_nds32_hash_entry (h)->tls_type = tls_type;
7239
0
        else
7240
0
    elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7241
0
      }
7242
0
    break;
7243
0
  case R_NDS32_9_PLTREL:
7244
0
  case R_NDS32_25_PLTREL:
7245
0
  case R_NDS32_PLTREL_HI20:
7246
0
  case R_NDS32_PLTREL_LO12:
7247
0
  case R_NDS32_PLT_GOTREL_HI20:
7248
0
  case R_NDS32_PLT_GOTREL_LO12:
7249
0
  case R_NDS32_PLT_GOTREL_LO15:
7250
0
  case R_NDS32_PLT_GOTREL_LO19:
7251
0
  case R_NDS32_PLT_GOTREL_LO20:
7252
7253
    /* This symbol requires a procedure linkage table entry.  We
7254
       actually build the entry in adjust_dynamic_symbol,
7255
       because this might be a case of linking PIC code without
7256
       linking in any dynamic objects, in which case we don't
7257
       need to generate a procedure linkage table after all.  */
7258
7259
    /* If this is a local symbol, we resolve it directly without
7260
       creating a procedure linkage table entry.  */
7261
0
    if (h == NULL)
7262
0
      continue;
7263
7264
0
    if (h->forced_local
7265
0
        || (bfd_link_pie (info) && h->def_regular))
7266
0
      break;
7267
7268
0
    elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7269
0
    h->needs_plt = 1;
7270
0
    h->plt.refcount += 1;
7271
0
    break;
7272
7273
0
  case R_NDS32_16_RELA:
7274
0
  case R_NDS32_20_RELA:
7275
0
  case R_NDS32_5_RELA:
7276
0
  case R_NDS32_32_RELA:
7277
0
  case R_NDS32_HI20_RELA:
7278
0
  case R_NDS32_LO12S3_RELA:
7279
0
  case R_NDS32_LO12S2_RELA:
7280
0
  case R_NDS32_LO12S2_DP_RELA:
7281
0
  case R_NDS32_LO12S2_SP_RELA:
7282
0
  case R_NDS32_LO12S1_RELA:
7283
0
  case R_NDS32_LO12S0_RELA:
7284
0
  case R_NDS32_LO12S0_ORI_RELA:
7285
0
  case R_NDS32_SDA16S3_RELA:
7286
0
  case R_NDS32_SDA17S2_RELA:
7287
0
  case R_NDS32_SDA18S1_RELA:
7288
0
  case R_NDS32_SDA19S0_RELA:
7289
0
  case R_NDS32_SDA15S3_RELA:
7290
0
  case R_NDS32_SDA15S2_RELA:
7291
0
  case R_NDS32_SDA12S2_DP_RELA:
7292
0
  case R_NDS32_SDA12S2_SP_RELA:
7293
0
  case R_NDS32_SDA15S1_RELA:
7294
0
  case R_NDS32_SDA15S0_RELA:
7295
0
  case R_NDS32_SDA_FP7U2_RELA:
7296
0
  case R_NDS32_15_PCREL_RELA:
7297
0
  case R_NDS32_17_PCREL_RELA:
7298
0
  case R_NDS32_25_PCREL_RELA:
7299
7300
0
    if (h != NULL && !bfd_link_pic (info))
7301
0
      {
7302
0
        h->non_got_ref = 1;
7303
0
        h->plt.refcount += 1;
7304
0
      }
7305
7306
    /* If we are creating a shared library, and this is a reloc against
7307
       a global symbol, or a non PC relative reloc against a local
7308
       symbol, then we need to copy the reloc into the shared library.
7309
       However, if we are linking with -Bsymbolic, we do not need to
7310
       copy a reloc against a global symbol which is defined in an
7311
       object we are including in the link (i.e., DEF_REGULAR is set).
7312
       At this point we have not seen all the input files, so it is
7313
       possible that DEF_REGULAR is not set now but will be set later
7314
       (it is never cleared).  We account for that possibility below by
7315
       storing information in the dyn_relocs field of the hash table
7316
       entry.  A similar situation occurs when creating shared libraries
7317
       and symbol visibility changes render the symbol local.
7318
7319
       If on the other hand, we are creating an executable, we may need
7320
       to keep relocations for symbols satisfied by a dynamic library
7321
       if we manage to avoid copy relocs for the symbol.  */
7322
0
    if ((bfd_link_pic (info)
7323
0
         && (sec->flags & SEC_ALLOC) != 0
7324
0
         && ((r_type != R_NDS32_25_PCREL_RELA
7325
0
        && r_type != R_NDS32_15_PCREL_RELA
7326
0
        && r_type != R_NDS32_17_PCREL_RELA
7327
0
        && !(r_type == R_NDS32_32_RELA
7328
0
       && strcmp (sec->name, ".eh_frame") == 0))
7329
0
       || (h != NULL
7330
0
           && (!info->symbolic
7331
0
         || h->root.type == bfd_link_hash_defweak
7332
0
         || !h->def_regular))))
7333
0
        || (!bfd_link_pic (info)
7334
0
      && (sec->flags & SEC_ALLOC) != 0
7335
0
      && h != NULL
7336
0
      && (h->root.type == bfd_link_hash_defweak
7337
0
          || !h->def_regular)))
7338
0
      {
7339
0
        struct elf_dyn_relocs *p;
7340
0
        struct elf_dyn_relocs **head;
7341
7342
0
        if (dynobj == NULL)
7343
0
    {
7344
0
      dynobj = _bfd_elf_link_dynobj (info);
7345
0
      if (dynobj == NULL)
7346
0
        return false;
7347
0
    }
7348
7349
        /* When creating a shared object, we must copy these
7350
     relocs into the output file.  We create a reloc
7351
     section in dynobj and make room for the reloc.  */
7352
0
        if (sreloc == NULL)
7353
0
    {
7354
0
      const char *name;
7355
7356
0
      name = bfd_elf_string_from_elf_section
7357
0
        (abfd, elf_elfheader (abfd)->e_shstrndx,
7358
0
         elf_section_data (sec)->rela.hdr->sh_name);
7359
0
      if (name == NULL)
7360
0
        return false;
7361
7362
0
      BFD_ASSERT (startswith (name, ".rela")
7363
0
            && strcmp (bfd_section_name (sec),
7364
0
           name + 5) == 0);
7365
7366
0
      sreloc = bfd_get_section_by_name (dynobj, name);
7367
0
      if (sreloc == NULL)
7368
0
        {
7369
0
          flagword flags;
7370
7371
0
          sreloc = bfd_make_section (dynobj, name);
7372
0
          flags = (SEC_HAS_CONTENTS | SEC_READONLY
7373
0
             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7374
0
          if ((sec->flags & SEC_ALLOC) != 0)
7375
0
      flags |= SEC_ALLOC | SEC_LOAD;
7376
0
          if (sreloc == NULL
7377
0
        || !bfd_set_section_flags (sreloc, flags)
7378
0
        || !bfd_set_section_alignment (sreloc, 2))
7379
0
      return false;
7380
7381
0
          elf_section_type (sreloc) = SHT_RELA;
7382
0
        }
7383
0
      elf_section_data (sec)->sreloc = sreloc;
7384
0
    }
7385
7386
        /* If this is a global symbol, we count the number of
7387
     relocations we need for this symbol.  */
7388
0
        if (h != NULL)
7389
0
    head = &h->dyn_relocs;
7390
0
        else
7391
0
    {
7392
0
      asection *s;
7393
0
      void *vpp;
7394
7395
0
      Elf_Internal_Sym *isym;
7396
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
7397
0
            abfd, r_symndx);
7398
0
      if (isym == NULL)
7399
0
        return false;
7400
7401
      /* Track dynamic relocs needed for local syms too.  */
7402
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7403
0
      if (s == NULL)
7404
0
        return false;
7405
7406
0
      vpp = &elf_section_data (s)->local_dynrel;
7407
0
      head = (struct elf_dyn_relocs **) vpp;
7408
0
    }
7409
7410
0
        p = *head;
7411
0
        if (p == NULL || p->sec != sec)
7412
0
    {
7413
0
      size_t amt = sizeof (*p);
7414
0
      p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7415
0
      if (p == NULL)
7416
0
        return false;
7417
0
      p->next = *head;
7418
0
      *head = p;
7419
0
      p->sec = sec;
7420
0
      p->count = 0;
7421
0
      p->pc_count = 0;
7422
0
    }
7423
7424
0
        p->count += 1;
7425
7426
        /* Since eh_frame is readonly, R_NDS32_32_RELA
7427
     reloc for eh_frame will cause shared library has
7428
     TEXTREL entry in the dynamic section. This lead glibc
7429
     testsuites to failure (bug-13092) and cause kernel fail
7430
     (bug-11819).  I think the best solution is to replace
7431
     absolute reloc with pc relative reloc in the eh_frame.
7432
     To do that, we need to support the following issues:
7433
7434
     === For GCC ===
7435
     * gcc/config/nds32/nds32.h: Define
7436
     ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7437
     and DW_EH_PE_sdata4 into DWARF exception header when
7438
     option have '-fpic'.
7439
7440
     === For binutils ===
7441
     * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7442
     * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7443
     may break our nds DIFF mechanism, therefore, we
7444
     must disable all linker relaxations to ensure
7445
     correctness.
7446
     * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7447
     R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7448
     do the necessary modification.
7449
7450
     Unfortunately, it still have some problems for nds32
7451
     to support pc relative reloc in the eh_frame. So I use
7452
     another solution to fix this issue.
7453
7454
     However, I find that ld always emit TEXTREL marker for
7455
     R_NDS32_NONE relocs in rel.dyn. These none relocs are
7456
     correspond to R_NDS32_32_RELA for .eh_frame section.
7457
     It means that we always reserve redundant entries of rel.dyn
7458
     for these relocs which actually do nothing in dynamic linker.
7459
7460
     Therefore, we regard these relocs as pc relative relocs
7461
     here and increase the pc_count.  */
7462
0
        if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7463
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7464
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7465
0
      || (r_type == R_NDS32_32_RELA
7466
0
          && strcmp (sec->name, ".eh_frame") == 0))
7467
0
    p->pc_count += 1;
7468
0
      }
7469
0
    break;
7470
7471
    /* This relocation describes the C++ object vtable hierarchy.
7472
       Reconstruct it for later use during GC.  */
7473
0
  case R_NDS32_RELA_GNU_VTINHERIT:
7474
0
  case R_NDS32_GNU_VTINHERIT:
7475
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7476
0
      return false;
7477
0
    break;
7478
7479
    /* This relocation describes which C++ vtable entries are actually
7480
       used.  Record for later use during GC.  */
7481
0
  case R_NDS32_GNU_VTENTRY:
7482
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7483
0
      return false;
7484
0
    break;
7485
0
  case R_NDS32_RELA_GNU_VTENTRY:
7486
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7487
0
      return false;
7488
0
    break;
7489
0
  }
7490
0
    }
7491
7492
0
  return true;
7493
0
}
7494
7495
/* Write VAL in uleb128 format to P, returning a pointer to the
7496
   following byte.
7497
   This code is copied from elf-attr.c.  */
7498
7499
static bfd_byte *
7500
write_uleb128 (bfd_byte *p, unsigned int val)
7501
0
{
7502
0
  bfd_byte c;
7503
0
  do
7504
0
    {
7505
0
      c = val & 0x7f;
7506
0
      val >>= 7;
7507
0
      if (val)
7508
0
  c |= 0x80;
7509
0
      *(p++) = c;
7510
0
    }
7511
0
  while (val);
7512
0
  return p;
7513
0
}
7514
7515
static bfd_signed_vma
7516
calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7517
      Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7518
0
{
7519
0
  bfd_signed_vma foff;
7520
0
  bfd_vma symval, addend;
7521
0
  asection *sym_sec;
7522
7523
  /* Get the value of the symbol referred to by the reloc.  */
7524
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7525
0
    {
7526
0
      Elf_Internal_Sym *isym;
7527
7528
      /* A local symbol.  */
7529
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
7530
7531
0
      if (isym->st_shndx == SHN_UNDEF)
7532
0
  sym_sec = bfd_und_section_ptr;
7533
0
      else if (isym->st_shndx == SHN_ABS)
7534
0
  sym_sec = bfd_abs_section_ptr;
7535
0
      else if (isym->st_shndx == SHN_COMMON)
7536
0
  sym_sec = bfd_com_section_ptr;
7537
0
      else
7538
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7539
0
      symval = isym->st_value + sym_sec->output_section->vma
7540
0
         + sym_sec->output_offset;
7541
0
    }
7542
0
  else
7543
0
    {
7544
0
      unsigned long indx;
7545
0
      struct elf_link_hash_entry *h;
7546
7547
      /* An external symbol.  */
7548
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7549
0
      h = elf_sym_hashes (abfd)[indx];
7550
0
      BFD_ASSERT (h != NULL);
7551
7552
0
      if (h->root.type != bfd_link_hash_defined
7553
0
    && h->root.type != bfd_link_hash_defweak)
7554
  /* This appears to be a reference to an undefined
7555
     symbol.  Just ignore it--it will be caught by the
7556
     regular reloc processing.  */
7557
0
  return 0;
7558
7559
0
      if (h->root.u.def.section->flags & SEC_MERGE)
7560
0
  {
7561
0
    sym_sec = h->root.u.def.section;
7562
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
7563
0
                 h->root.u.def.value);
7564
0
    symval = symval + sym_sec->output_section->vma
7565
0
       + sym_sec->output_offset;
7566
0
  }
7567
0
      else
7568
0
  symval = (h->root.u.def.value
7569
0
      + h->root.u.def.section->output_section->vma
7570
0
      + h->root.u.def.section->output_offset);
7571
0
    }
7572
7573
0
  addend = irel->r_addend;
7574
7575
0
  foff = (symval + addend
7576
0
    - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7577
0
  return foff;
7578
0
}
7579

7580
7581
/* Convert a 32-bit instruction to 16-bit one.
7582
   INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7583
   instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
7584
   type of INSN16.  Return 1 if successful.  */
7585
7586
static int
7587
nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7588
           int *pinsn_type)
7589
0
{
7590
0
  uint16_t insn16 = 0;
7591
0
  int insn_type = 0;
7592
0
  unsigned long mach = bfd_get_mach (abfd);
7593
7594
0
  if (N32_SH5 (insn) != 0)
7595
0
    return 0;
7596
7597
0
  switch (N32_SUB5 (insn))
7598
0
    {
7599
0
    case N32_ALU1_ADD_SLLI:
7600
0
    case N32_ALU1_ADD_SRLI:
7601
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7602
0
  {
7603
0
    insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7604
0
        N32_RB5 (insn));
7605
0
    insn_type = NDS32_INSN_ADD333;
7606
0
  }
7607
0
      else if (N32_IS_RT4 (insn))
7608
0
  {
7609
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7610
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7611
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7612
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7613
0
    insn_type = NDS32_INSN_ADD45;
7614
0
  }
7615
0
      break;
7616
7617
0
    case N32_ALU1_SUB_SLLI:
7618
0
    case N32_ALU1_SUB_SRLI:
7619
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7620
0
  {
7621
0
    insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7622
0
        N32_RB5 (insn));
7623
0
    insn_type = NDS32_INSN_SUB333;
7624
0
  }
7625
0
      else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7626
0
  {
7627
0
    insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7628
0
    insn_type = NDS32_INSN_SUB45;
7629
0
  }
7630
0
      break;
7631
7632
0
    case N32_ALU1_AND_SLLI:
7633
0
    case N32_ALU1_AND_SRLI:
7634
      /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
7635
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7636
0
    && N32_IS_RB3 (insn))
7637
0
  {
7638
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7639
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7640
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7641
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7642
0
    if (insn16)
7643
0
      insn_type = NDS32_INSN_AND33;
7644
0
  }
7645
0
      break;
7646
7647
0
    case N32_ALU1_XOR_SLLI:
7648
0
    case N32_ALU1_XOR_SRLI:
7649
      /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
7650
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7651
0
    && N32_IS_RB3 (insn))
7652
0
  {
7653
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7654
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7655
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7656
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7657
0
    if (insn16)
7658
0
      insn_type = NDS32_INSN_XOR33;
7659
0
  }
7660
0
      break;
7661
7662
0
    case N32_ALU1_OR_SLLI:
7663
0
    case N32_ALU1_OR_SRLI:
7664
      /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
7665
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7666
0
    && N32_IS_RB3 (insn))
7667
0
  {
7668
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7669
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7670
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7671
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7672
0
    if (insn16)
7673
0
      insn_type = NDS32_INSN_OR33;
7674
0
  }
7675
0
      break;
7676
0
    case N32_ALU1_NOR:
7677
      /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
7678
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7679
0
    && N32_RA5 (insn) == N32_RB5 (insn))
7680
0
  {
7681
0
    insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7682
0
    insn_type = NDS32_INSN_NOT33;
7683
0
  }
7684
0
      break;
7685
0
    case N32_ALU1_SRAI:
7686
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7687
0
  {
7688
0
    insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7689
0
    insn_type = NDS32_INSN_SRAI45;
7690
0
  }
7691
0
      break;
7692
7693
0
    case N32_ALU1_SRLI:
7694
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7695
0
  {
7696
0
    insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7697
0
    insn_type = NDS32_INSN_SRLI45;
7698
0
  }
7699
0
      break;
7700
7701
0
    case N32_ALU1_SLLI:
7702
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7703
0
  {
7704
0
    insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7705
0
        N32_UB5 (insn));
7706
0
    insn_type = NDS32_INSN_SLLI333;
7707
0
  }
7708
0
      break;
7709
7710
0
    case N32_ALU1_ZEH:
7711
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7712
0
  {
7713
0
    insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7714
0
    insn_type = NDS32_INSN_ZEH33;
7715
0
  }
7716
0
      break;
7717
7718
0
    case N32_ALU1_SEB:
7719
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7720
0
  {
7721
0
    insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7722
0
    insn_type = NDS32_INSN_SEB33;
7723
0
  }
7724
0
      break;
7725
7726
0
    case N32_ALU1_SEH:
7727
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7728
0
  {
7729
0
    insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7730
0
    insn_type = NDS32_INSN_SEH33;
7731
0
  }
7732
0
      break;
7733
7734
0
    case N32_ALU1_SLT:
7735
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn))
7736
0
  {
7737
    /* Implicit r15.  */
7738
0
    insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7739
0
    insn_type = NDS32_INSN_SLT45;
7740
0
  }
7741
0
      break;
7742
7743
0
    case N32_ALU1_SLTS:
7744
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn))
7745
0
  {
7746
    /* Implicit r15.  */
7747
0
    insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7748
0
    insn_type = NDS32_INSN_SLTS45;
7749
0
  }
7750
0
      break;
7751
0
    }
7752
7753
0
  if ((insn16 & 0x8000) == 0)
7754
0
    return 0;
7755
7756
0
  if (pinsn16)
7757
0
    *pinsn16 = insn16;
7758
0
  if (pinsn_type)
7759
0
    *pinsn_type = insn_type;
7760
0
  return 1;
7761
0
}
7762
7763
static int
7764
nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7765
           int *pinsn_type)
7766
0
{
7767
0
  uint16_t insn16 = 0;
7768
0
  int insn_type;
7769
0
  unsigned long mach = bfd_get_mach (abfd);
7770
7771
  /* TODO: bset, bclr, btgl, btst.  */
7772
0
  if (__GF (insn, 6, 4) != 0)
7773
0
    return 0;
7774
7775
0
  switch (N32_IMMU (insn, 6))
7776
0
    {
7777
0
    case N32_ALU2_MUL:
7778
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7779
0
    && N32_IS_RB3 (insn))
7780
0
  {
7781
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7782
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7783
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7784
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7785
0
    if (insn16)
7786
0
      insn_type = NDS32_INSN_MUL33;
7787
0
  }
7788
0
    }
7789
7790
0
  if ((insn16 & 0x8000) == 0)
7791
0
    return 0;
7792
7793
0
  if (pinsn16)
7794
0
    *pinsn16 = insn16;
7795
0
  if (pinsn_type)
7796
0
    *pinsn_type = insn_type;
7797
0
  return 1;
7798
0
}
7799
7800
int
7801
bfd_elf_nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7802
        int *pinsn_type)
7803
0
{
7804
0
  int op6;
7805
0
  uint16_t insn16 = 0;
7806
0
  int insn_type = 0;
7807
0
  unsigned long mach = bfd_get_mach (abfd);
7808
7809
  /* Decode 32-bit instruction.  */
7810
0
  if (insn & 0x80000000)
7811
0
    {
7812
      /* Not 32-bit insn.  */
7813
0
      return 0;
7814
0
    }
7815
7816
0
  op6 = N32_OP6 (insn);
7817
7818
  /* Convert it to 16-bit instruction.  */
7819
0
  switch (op6)
7820
0
    {
7821
0
    case N32_OP6_MOVI:
7822
0
      if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7823
0
  {
7824
0
    insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7825
0
    insn_type = NDS32_INSN_MOVI55;
7826
0
  }
7827
0
      else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7828
0
         && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7829
0
  {
7830
0
    insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7831
0
             N32_IMM20S (insn) - 16);
7832
0
    insn_type = NDS32_INSN_MOVPI45;
7833
0
  }
7834
0
      break;
7835
7836
0
    case N32_OP6_ADDI:
7837
0
      if (N32_IMM15S (insn) == 0)
7838
0
  {
7839
    /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7840
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7841
0
    if (mach <= MACH_V2
7842
0
        || N32_RT5 (insn) != N32_REG_SP || N32_RA5 (insn) != N32_REG_SP)
7843
0
      {
7844
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7845
0
        insn_type = NDS32_INSN_MOV55;
7846
0
      }
7847
0
  }
7848
0
      else if (N32_IMM15S (insn) > 0)
7849
0
  {
7850
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7851
0
      {
7852
0
        insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7853
0
            N32_IMM15S (insn));
7854
0
        insn_type = NDS32_INSN_ADDI333;
7855
0
      }
7856
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7857
0
       && N32_IMM15S (insn) < 32)
7858
0
      {
7859
0
        insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7860
0
        insn_type = NDS32_INSN_ADDI45;
7861
0
      }
7862
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == N32_REG_SP
7863
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7864
0
       && N32_IMM15S (insn) < 512)
7865
0
      {
7866
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7867
0
        insn_type = NDS32_INSN_ADDI10_SP;
7868
0
      }
7869
0
    else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7870
0
       && N32_RA5 (insn) == N32_REG_SP && N32_IMM15S (insn) < 256
7871
0
       && (N32_IMM15S (insn) % 4 == 0))
7872
0
      {
7873
0
        insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7874
0
           N32_IMM15S (insn) >> 2);
7875
0
        insn_type = NDS32_INSN_ADDRI36_SP;
7876
0
      }
7877
0
  }
7878
0
      else
7879
0
  {
7880
    /* Less than 0.  */
7881
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7882
0
      {
7883
0
        insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7884
0
            0 - N32_IMM15S (insn));
7885
0
        insn_type = NDS32_INSN_SUBI333;
7886
0
      }
7887
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7888
0
       && N32_IMM15S (insn) > -32)
7889
0
      {
7890
0
        insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7891
0
           0 - N32_IMM15S (insn));
7892
0
        insn_type = NDS32_INSN_SUBI45;
7893
0
      }
7894
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == N32_REG_SP
7895
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7896
0
       && N32_IMM15S (insn) >= -512)
7897
0
      {
7898
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7899
0
        insn_type = NDS32_INSN_ADDI10_SP;
7900
0
      }
7901
0
  }
7902
0
      break;
7903
7904
0
    case N32_OP6_ORI:
7905
0
      if (N32_IMM15S (insn) == 0)
7906
0
  {
7907
    /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7908
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7909
0
    if (mach <= MACH_V2
7910
0
        || N32_RT5 (insn) != N32_REG_SP || N32_RA5 (insn) != N32_REG_SP)
7911
0
      {
7912
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7913
0
        insn_type = NDS32_INSN_MOV55;
7914
0
      }
7915
0
  }
7916
0
      break;
7917
7918
0
    case N32_OP6_SUBRI:
7919
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7920
0
    && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7921
0
  {
7922
0
    insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7923
0
    insn_type = NDS32_INSN_NEG33;
7924
0
  }
7925
0
      break;
7926
7927
0
    case N32_OP6_ANDI:
7928
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7929
0
  {
7930
0
    if (N32_IMM15U (insn) == 1)
7931
0
      {
7932
0
        insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7933
0
        insn_type = NDS32_INSN_XLSB33;
7934
0
      }
7935
0
    else if (N32_IMM15U (insn) == 0x7ff)
7936
0
      {
7937
0
        insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7938
0
        insn_type = NDS32_INSN_X11B33;
7939
0
      }
7940
0
    else if (N32_IMM15U (insn) == 0xff)
7941
0
      {
7942
0
        insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7943
0
        insn_type = NDS32_INSN_ZEB33;
7944
0
      }
7945
0
    else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7946
0
       && N32_IMM15U (insn) < 256)
7947
0
      {
7948
0
        int imm15u = N32_IMM15U (insn);
7949
7950
0
        if (__builtin_popcount (imm15u) == 1)
7951
0
    {
7952
      /* BMSKI33 */
7953
0
      int imm3u = __builtin_ctz (imm15u);
7954
7955
0
      insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7956
0
      insn_type = NDS32_INSN_BMSKI33;
7957
0
    }
7958
0
        else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7959
0
    {
7960
      /* FEXTI33 */
7961
0
      int imm3u = __builtin_ctz (imm15u + 1) - 1;
7962
7963
0
      insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7964
0
      insn_type = NDS32_INSN_FEXTI33;
7965
0
    }
7966
0
      }
7967
0
  }
7968
0
      break;
7969
7970
0
    case N32_OP6_SLTI:
7971
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn)
7972
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7973
0
  {
7974
0
    insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7975
0
    insn_type = NDS32_INSN_SLTI45;
7976
0
  }
7977
0
      break;
7978
7979
0
    case N32_OP6_SLTSI:
7980
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn)
7981
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7982
0
  {
7983
0
    insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7984
0
    insn_type = NDS32_INSN_SLTSI45;
7985
0
  }
7986
0
      break;
7987
7988
0
    case N32_OP6_LWI:
7989
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7990
0
  {
7991
0
    insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7992
0
    insn_type = NDS32_INSN_LWI450;
7993
0
  }
7994
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7995
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
7996
0
  {
7997
0
    insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7998
0
        N32_IMM15S (insn));
7999
0
    insn_type = NDS32_INSN_LWI333;
8000
0
  }
8001
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_FP
8002
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8003
0
  {
8004
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8005
0
    insn_type = NDS32_INSN_LWI37;
8006
0
  }
8007
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn)
8008
0
         && N32_RA5 (insn) == N32_REG_SP
8009
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8010
0
  {
8011
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8012
0
    insn_type = NDS32_INSN_LWI37_SP;
8013
0
  }
8014
0
      else if (mach >= MACH_V2 && N32_IS_RT4 (insn)
8015
0
         && N32_RA5 (insn) == N32_REG_R8
8016
0
         && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8017
0
  {
8018
0
    insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8019
0
             N32_IMM15S (insn) + 32);
8020
0
    insn_type = NDS32_INSN_LWI45_FE;
8021
0
  }
8022
0
      break;
8023
8024
0
    case N32_OP6_SWI:
8025
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8026
0
  {
8027
0
    insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8028
0
    insn_type = NDS32_INSN_SWI450;
8029
0
  }
8030
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8031
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
8032
0
  {
8033
0
    insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8034
0
        N32_IMM15S (insn));
8035
0
    insn_type = NDS32_INSN_SWI333;
8036
0
  }
8037
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_FP
8038
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8039
0
  {
8040
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8041
0
    insn_type = NDS32_INSN_SWI37;
8042
0
  }
8043
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn)
8044
0
         && N32_RA5 (insn) == N32_REG_SP
8045
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8046
0
  {
8047
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8048
0
    insn_type = NDS32_INSN_SWI37_SP;
8049
0
  }
8050
0
      break;
8051
8052
0
    case N32_OP6_LWI_BI:
8053
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8054
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8055
0
  {
8056
0
    insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8057
0
        N32_IMM15S (insn));
8058
0
    insn_type = NDS32_INSN_LWI333_BI;
8059
0
  }
8060
0
      break;
8061
8062
0
    case N32_OP6_SWI_BI:
8063
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8064
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8065
0
  {
8066
0
    insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8067
0
        N32_IMM15S (insn));
8068
0
    insn_type = NDS32_INSN_SWI333_BI;
8069
0
  }
8070
0
      break;
8071
8072
0
    case N32_OP6_LHI:
8073
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8074
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8075
0
  {
8076
0
    insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8077
0
        N32_IMM15S (insn));
8078
0
    insn_type = NDS32_INSN_LHI333;
8079
0
  }
8080
0
      break;
8081
8082
0
    case N32_OP6_SHI:
8083
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8084
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8085
0
  {
8086
0
    insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8087
0
        N32_IMM15S (insn));
8088
0
    insn_type = NDS32_INSN_SHI333;
8089
0
  }
8090
0
      break;
8091
8092
0
    case N32_OP6_LBI:
8093
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8094
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8095
0
  {
8096
0
    insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8097
0
        N32_IMM15S (insn));
8098
0
    insn_type = NDS32_INSN_LBI333;
8099
0
  }
8100
0
      break;
8101
8102
0
    case N32_OP6_SBI:
8103
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8104
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8105
0
  {
8106
0
    insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8107
0
        N32_IMM15S (insn));
8108
0
    insn_type = NDS32_INSN_SBI333;
8109
0
  }
8110
0
      break;
8111
8112
0
    case N32_OP6_ALU1:
8113
0
      return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8114
8115
0
    case N32_OP6_ALU2:
8116
0
      return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8117
8118
0
    case N32_OP6_BR1:
8119
0
      if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8120
0
  goto done;
8121
8122
0
      if ((insn & N32_BIT (14)) == 0)
8123
0
  {
8124
    /* N32_BR1_BEQ */
8125
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_R5
8126
0
        && N32_RT5 (insn) != N32_REG_R5)
8127
0
      insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8128
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == N32_REG_R5
8129
0
       && N32_RA5 (insn) != N32_REG_R5)
8130
0
      insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8131
0
    insn_type = NDS32_INSN_BEQS38;
8132
0
    break;
8133
0
  }
8134
0
      else
8135
0
  {
8136
    /* N32_BR1_BNE */
8137
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_R5
8138
0
        && N32_RT5 (insn) != N32_REG_R5)
8139
0
      insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8140
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == N32_REG_R5
8141
0
       && N32_RA5 (insn) != N32_REG_R5)
8142
0
      insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8143
0
    insn_type = NDS32_INSN_BNES38;
8144
0
    break;
8145
0
  }
8146
0
      break;
8147
8148
0
    case N32_OP6_BR2:
8149
0
      switch (N32_BR2_SUB (insn))
8150
0
  {
8151
0
  case N32_BR2_BEQZ:
8152
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8153
0
      {
8154
0
        insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8155
0
        insn_type = NDS32_INSN_BEQZ38;
8156
0
      }
8157
0
    else if (N32_RT5 (insn) == N32_REG_R15
8158
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8159
0
      {
8160
0
        insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8161
0
        insn_type = NDS32_INSN_BEQZS8;
8162
0
      }
8163
0
    break;
8164
8165
0
  case N32_BR2_BNEZ:
8166
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8167
0
      {
8168
0
        insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8169
0
        insn_type = NDS32_INSN_BNEZ38;
8170
0
      }
8171
0
    else if (N32_RT5 (insn) == N32_REG_R15
8172
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8173
0
      {
8174
0
        insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8175
0
        insn_type = NDS32_INSN_BNEZS8;
8176
0
      }
8177
0
    break;
8178
8179
0
  case N32_BR2_SOP0:
8180
0
    if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8181
0
      {
8182
0
        insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8183
0
        insn_type = NDS32_INSN_IFCALL9;
8184
0
      }
8185
0
    break;
8186
0
  }
8187
0
      break;
8188
8189
0
    case N32_OP6_JI:
8190
0
      if ((insn & N32_BIT (24)) == 0)
8191
0
  {
8192
    /* N32_JI_J */
8193
0
    if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8194
0
      {
8195
0
        insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8196
0
        insn_type = NDS32_INSN_J8;
8197
0
      }
8198
0
  }
8199
0
      break;
8200
8201
0
    case N32_OP6_JREG:
8202
0
      if (__GF (insn, 8, 2) != 0)
8203
0
  goto done;
8204
8205
0
      switch (N32_IMMU (insn, 5))
8206
0
  {
8207
0
  case N32_JREG_JR:
8208
0
    if (N32_JREG_HINT (insn) == 0)
8209
0
      {
8210
        /* jr */
8211
0
        insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8212
0
        insn_type = NDS32_INSN_JR5;
8213
0
      }
8214
0
    else if (N32_JREG_HINT (insn) == 1)
8215
0
      {
8216
        /* ret */
8217
0
        insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8218
0
        insn_type = NDS32_INSN_RET5;
8219
0
      }
8220
0
    else if (N32_JREG_HINT (insn) == 3)
8221
0
      {
8222
        /* ifret = mov55 $sp, $sp */
8223
0
        insn16 = N16_TYPE55 (MOV55, N32_REG_SP, N32_REG_SP);
8224
0
        insn_type = NDS32_INSN_IFRET;
8225
0
      }
8226
0
    break;
8227
8228
0
  case N32_JREG_JRAL:
8229
    /* It's convertible when return rt5 is $lp and address
8230
       translation is kept.  */
8231
0
    if (N32_RT5 (insn) == N32_REG_LP && N32_JREG_HINT (insn) == 0)
8232
0
      {
8233
0
        insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8234
0
        insn_type = NDS32_INSN_JRAL5;
8235
0
      }
8236
0
    break;
8237
0
  }
8238
0
      break;
8239
8240
0
    case N32_OP6_MISC:
8241
0
      if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8242
0
  {
8243
    /* For v3, swid above 31 are used for ex9.it.  */
8244
0
    insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8245
0
    insn_type = NDS32_INSN_BREAK16;
8246
0
  }
8247
0
      break;
8248
8249
0
    default:
8250
      /* This instruction has no 16-bit variant.  */
8251
0
      goto done;
8252
0
    }
8253
8254
0
 done:
8255
  /* Bit-15 of insn16 should be set for a valid instruction.  */
8256
0
  if ((insn16 & 0x8000) == 0)
8257
0
    return 0;
8258
8259
0
  if (pinsn16)
8260
0
    *pinsn16 = insn16;
8261
0
  if (pinsn_type)
8262
0
    *pinsn_type = insn_type;
8263
0
  return 1;
8264
0
}
8265
8266
static int
8267
special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8268
        Elf_Internal_Rela *reloc)
8269
0
{
8270
0
  uint16_t insn16 = 0;
8271
8272
0
  if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8273
0
      || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8274
0
    return 0;
8275
8276
0
  if (!N32_IS_RT3 (insn))
8277
0
    return 0;
8278
8279
0
  switch (N32_OP6 (insn))
8280
0
    {
8281
0
    case N32_OP6_LWI:
8282
0
      if (N32_RA5 (insn) == N32_REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8283
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8284
0
      break;
8285
0
    case N32_OP6_SWI:
8286
0
      if (N32_RA5 (insn) == N32_REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8287
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8288
0
      break;
8289
0
    case N32_OP6_HWGP:
8290
0
      if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8291
0
  break;
8292
8293
0
      if (__GF (insn, 17, 3) == 6)
8294
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8295
0
      else if (__GF (insn, 17, 3) == 7)
8296
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8297
0
      break;
8298
0
    }
8299
8300
0
  if ((insn16 & 0x8000) == 0)
8301
0
    return 0;
8302
8303
0
  *pinsn16 = insn16;
8304
0
  return 1;
8305
0
}
8306
8307
/* Convert a 16-bit instruction to 32-bit one.
8308
   INSN16 it the input and PINSN it the point to output.
8309
   Return non-zero on successful.  Otherwise 0 is returned.  */
8310
8311
int
8312
bfd_elf_nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8313
0
{
8314
0
  uint32_t insn = 0xffffffff;
8315
0
  unsigned long mach = bfd_get_mach (abfd);
8316
8317
  /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
8318
8319
0
  switch (__GF (insn16, 9, 6))
8320
0
    {
8321
0
    case 0x4:     /* add45 */
8322
0
      insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8323
0
           N16_RA5 (insn16));
8324
0
      goto done;
8325
0
    case 0x5:     /* sub45 */
8326
0
      insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8327
0
           N16_RA5 (insn16));
8328
0
      goto done;
8329
0
    case 0x6:     /* addi45 */
8330
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8331
0
      N16_IMM5U (insn16));
8332
0
      goto done;
8333
0
    case 0x7:     /* subi45 */
8334
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8335
0
      -N16_IMM5U (insn16));
8336
0
      goto done;
8337
0
    case 0x8:     /* srai45 */
8338
0
      insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8339
0
           N16_IMM5U (insn16));
8340
0
      goto done;
8341
0
    case 0x9:     /* srli45 */
8342
0
      insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8343
0
           N16_IMM5U (insn16));
8344
0
      goto done;
8345
0
    case 0xa:     /* slli333 */
8346
0
      insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8347
0
           N16_IMM3U (insn16));
8348
0
      goto done;
8349
0
    case 0xc:     /* add333 */
8350
0
      insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8351
0
           N16_RB3 (insn16));
8352
0
      goto done;
8353
0
    case 0xd:     /* sub333 */
8354
0
      insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8355
0
           N16_RB3 (insn16));
8356
0
      goto done;
8357
0
    case 0xe:     /* addi333 */
8358
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8359
0
      N16_IMM3U (insn16));
8360
0
      goto done;
8361
0
    case 0xf:     /* subi333 */
8362
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8363
0
      -N16_IMM3U (insn16));
8364
0
      goto done;
8365
0
    case 0x10:      /* lwi333 */
8366
0
      insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8367
0
      N16_IMM3U (insn16));
8368
0
      goto done;
8369
0
    case 0x12:      /* lhi333 */
8370
0
      insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8371
0
      N16_IMM3U (insn16));
8372
0
      goto done;
8373
0
    case 0x13:      /* lbi333 */
8374
0
      insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8375
0
      N16_IMM3U (insn16));
8376
0
      goto done;
8377
0
    case 0x11:      /* lwi333.bi */
8378
0
      insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8379
0
      N16_IMM3U (insn16));
8380
0
      goto done;
8381
0
    case 0x14:      /* swi333 */
8382
0
      insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8383
0
      N16_IMM3U (insn16));
8384
0
      goto done;
8385
0
    case 0x16:      /* shi333 */
8386
0
      insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8387
0
      N16_IMM3U (insn16));
8388
0
      goto done;
8389
0
    case 0x17:      /* sbi333 */
8390
0
      insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8391
0
      N16_IMM3U (insn16));
8392
0
      goto done;
8393
0
    case 0x15:      /* swi333.bi */
8394
0
      insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8395
0
      N16_IMM3U (insn16));
8396
0
      goto done;
8397
0
    case 0x18:      /* addri36.sp */
8398
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N32_REG_SP,
8399
0
      N16_IMM6U (insn16) << 2);
8400
0
      goto done;
8401
0
    case 0x19:      /* lwi45.fe */
8402
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N32_REG_R8,
8403
0
      (N16_IMM5U (insn16) - 32));
8404
0
      goto done;
8405
0
    case 0x1a:      /* lwi450 */
8406
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8407
0
      goto done;
8408
0
    case 0x1b:      /* swi450 */
8409
0
      insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8410
0
      goto done;
8411
8412
      /* These are r15 implied instructions.  */
8413
0
    case 0x30:      /* slts45 */
8414
0
      insn = N32_ALU1 (SLTS, N32_REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8415
0
      goto done;
8416
0
    case 0x31:      /* slt45 */
8417
0
      insn = N32_ALU1 (SLT, N32_REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8418
0
      goto done;
8419
0
    case 0x32:      /* sltsi45 */
8420
0
      insn = N32_TYPE2 (SLTSI, N32_REG_TA, N16_RT4 (insn16),
8421
0
      N16_IMM5U (insn16));
8422
0
      goto done;
8423
0
    case 0x33:      /* slti45 */
8424
0
      insn = N32_TYPE2 (SLTI, N32_REG_TA, N16_RT4 (insn16),
8425
0
      N16_IMM5U (insn16));
8426
0
      goto done;
8427
0
    case 0x34:      /* beqzs8, bnezs8 */
8428
0
      if (insn16 & N32_BIT (8))
8429
0
  insn = N32_BR2 (BNEZ, N32_REG_TA, N16_IMM8S (insn16));
8430
0
      else
8431
0
  insn = N32_BR2 (BEQZ, N32_REG_TA, N16_IMM8S (insn16));
8432
0
      goto done;
8433
8434
0
    case 0x35:      /* break16, ex9.it */
8435
      /* Only consider range of v3 break16.  */
8436
0
      insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8437
0
      goto done;
8438
8439
0
    case 0x3c:      /* ifcall9 */
8440
0
      insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8441
0
      goto done;
8442
0
    case 0x3d:      /* movpi45 */
8443
0
      insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8444
0
      goto done;
8445
8446
0
    case 0x3f:      /* MISC33 */
8447
0
      switch (insn16 & 0x7)
8448
0
  {
8449
0
  case 2:     /* neg33 */
8450
0
    insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8451
0
    break;
8452
0
  case 3:     /* not33 */
8453
0
    insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8454
0
         N16_RA3 (insn16));
8455
0
    break;
8456
0
  case 4:     /* mul33 */
8457
0
    insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8458
0
         N16_RA3 (insn16));
8459
0
    break;
8460
0
  case 5:     /* xor33 */
8461
0
    insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8462
0
         N16_RA3 (insn16));
8463
0
    break;
8464
0
  case 6:     /* and33 */
8465
0
    insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8466
0
         N16_RA3 (insn16));
8467
0
    break;
8468
0
  case 7:     /* or33 */
8469
0
    insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8470
0
         N16_RA3 (insn16));
8471
0
    break;
8472
0
  }
8473
0
      goto done;
8474
8475
0
    case 0xb:
8476
0
      switch (insn16 & 0x7)
8477
0
  {
8478
0
  case 0:     /* zeb33 */
8479
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8480
0
    break;
8481
0
  case 1:     /* zeh33 */
8482
0
    insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8483
0
    break;
8484
0
  case 2:     /* seb33 */
8485
0
    insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8486
0
    break;
8487
0
  case 3:     /* seh33 */
8488
0
    insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8489
0
    break;
8490
0
  case 4:     /* xlsb33 */
8491
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8492
0
    break;
8493
0
  case 5:     /* x11b33 */
8494
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8495
0
    break;
8496
0
  case 6:     /* bmski33 */
8497
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8498
0
          1 << __GF (insn16, 3, 3));
8499
0
    break;
8500
0
  case 7:     /* fexti33 */
8501
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8502
0
          (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8503
0
    break;
8504
0
  }
8505
0
      goto done;
8506
0
    }
8507
8508
0
  switch (__GF (insn16, 10, 5))
8509
0
    {
8510
0
    case 0x0:     /* mov55 or ifret16 */
8511
0
      if (mach >= MACH_V3 && N16_RT5 (insn16) == N32_REG_SP
8512
0
    && N16_RT5 (insn16) == N16_RA5 (insn16))
8513
0
  insn = N32_JREG (JR, 0, 0, 0, 3);
8514
0
      else
8515
0
  insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8516
0
      goto done;
8517
0
    case 0x1:     /* movi55 */
8518
0
      insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8519
0
      goto done;
8520
0
    case 0x1b:      /* addi10s (V2) */
8521
0
      insn = N32_TYPE2 (ADDI, N32_REG_SP, N32_REG_SP, N16_IMM10S (insn16));
8522
0
      goto done;
8523
0
    }
8524
8525
0
  switch (__GF (insn16, 11, 4))
8526
0
    {
8527
0
    case 0x7:     /* lwi37.fp/swi37.fp */
8528
0
      if (insn16 & N32_BIT (7)) /* swi37.fp */
8529
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), N32_REG_FP,
8530
0
        N16_IMM7U (insn16));
8531
0
      else      /* lwi37.fp */
8532
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), N32_REG_FP,
8533
0
        N16_IMM7U (insn16));
8534
0
      goto done;
8535
0
    case 0x8:     /* beqz38 */
8536
0
      insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8537
0
      goto done;
8538
0
    case 0x9:     /* bnez38 */
8539
0
      insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8540
0
      goto done;
8541
0
    case 0xa:     /* beqs38/j8, implied r5 */
8542
0
      if (N16_RT38 (insn16) == 5)
8543
0
  insn = N32_JI (J, N16_IMM8S (insn16));
8544
0
      else
8545
0
  insn = N32_BR1 (BEQ, N16_RT38 (insn16), N32_REG_R5,
8546
0
      N16_IMM8S (insn16));
8547
0
      goto done;
8548
0
    case 0xb:     /* bnes38 and others.  */
8549
0
      if (N16_RT38 (insn16) == 5)
8550
0
  {
8551
0
    switch (__GF (insn16, 5, 3))
8552
0
      {
8553
0
      case 0:   /* jr5 */
8554
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8555
0
        break;
8556
0
      case 4:   /* ret5 */
8557
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8558
0
        break;
8559
0
      case 1:   /* jral5 */
8560
0
        insn = N32_JREG (JRAL, N32_REG_LP, N16_RA5 (insn16), 0, 0);
8561
0
        break;
8562
0
      case 2:   /* ex9.it imm5 */
8563
        /* ex9.it had no 32-bit variantl.  */
8564
0
        break;
8565
0
      case 5:   /* add5.pc */
8566
        /* add5.pc had no 32-bit variantl.  */
8567
0
        break;
8568
0
      }
8569
0
  }
8570
0
      else      /* bnes38 */
8571
0
  insn = N32_BR1 (BNE, N16_RT38 (insn16), N32_REG_R5,
8572
0
      N16_IMM8S (insn16));
8573
0
      goto done;
8574
0
    case 0xe:     /* lwi37/swi37 */
8575
0
      if (insn16 & (1 << 7)) /* swi37.sp */
8576
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), N32_REG_SP,
8577
0
        N16_IMM7U (insn16));
8578
0
      else      /* lwi37.sp */
8579
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), N32_REG_SP,
8580
0
        N16_IMM7U (insn16));
8581
0
      goto done;
8582
0
    }
8583
8584
0
 done:
8585
0
  if (insn & 0x80000000)
8586
0
    return 0;
8587
8588
0
  if (pinsn)
8589
0
    *pinsn = insn;
8590
0
  return 1;
8591
0
}
8592

8593
8594
static bool
8595
is_sda_access_insn (unsigned long insn)
8596
0
{
8597
0
  switch (N32_OP6 (insn))
8598
0
    {
8599
0
    case N32_OP6_LWI:
8600
0
    case N32_OP6_LHI:
8601
0
    case N32_OP6_LHSI:
8602
0
    case N32_OP6_LBI:
8603
0
    case N32_OP6_LBSI:
8604
0
    case N32_OP6_SWI:
8605
0
    case N32_OP6_SHI:
8606
0
    case N32_OP6_SBI:
8607
0
    case N32_OP6_LWC:
8608
0
    case N32_OP6_LDC:
8609
0
    case N32_OP6_SWC:
8610
0
    case N32_OP6_SDC:
8611
0
      return true;
8612
0
    default:
8613
0
      ;
8614
0
    }
8615
0
  return false;
8616
0
}
8617
8618
static unsigned long
8619
turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8620
0
{
8621
0
  uint32_t oinsn = 0;
8622
8623
0
  switch (type)
8624
0
    {
8625
0
    case R_NDS32_GOT_LO12:
8626
0
    case R_NDS32_GOTOFF_LO12:
8627
0
    case R_NDS32_PLTREL_LO12:
8628
0
    case R_NDS32_PLT_GOTREL_LO12:
8629
0
    case R_NDS32_LO12S0_RELA:
8630
0
      switch (N32_OP6 (insn))
8631
0
  {
8632
0
  case N32_OP6_LBI:
8633
    /* lbi.gp */
8634
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8635
0
    break;
8636
0
  case N32_OP6_LBSI:
8637
    /* lbsi.gp */
8638
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8639
0
    break;
8640
0
  case N32_OP6_SBI:
8641
    /* sbi.gp */
8642
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8643
0
    break;
8644
0
  case N32_OP6_ORI:
8645
    /* addi.gp */
8646
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8647
0
    break;
8648
0
  }
8649
0
      break;
8650
8651
0
    case R_NDS32_LO12S1_RELA:
8652
0
      switch (N32_OP6 (insn))
8653
0
  {
8654
0
  case N32_OP6_LHI:
8655
    /* lhi.gp */
8656
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8657
0
    break;
8658
0
  case N32_OP6_LHSI:
8659
    /* lhsi.gp */
8660
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8661
0
    break;
8662
0
  case N32_OP6_SHI:
8663
    /* shi.gp */
8664
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8665
0
    break;
8666
0
  }
8667
0
      break;
8668
8669
0
    case R_NDS32_LO12S2_RELA:
8670
0
      switch (N32_OP6 (insn))
8671
0
  {
8672
0
  case N32_OP6_LWI:
8673
    /* lwi.gp */
8674
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8675
0
    break;
8676
0
  case N32_OP6_SWI:
8677
    /* swi.gp */
8678
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8679
0
    break;
8680
0
  }
8681
0
      break;
8682
8683
0
    case R_NDS32_LO12S2_DP_RELA:
8684
0
    case R_NDS32_LO12S2_SP_RELA:
8685
0
      oinsn = (insn & 0x7ff07000) | (N32_REG_GP << 15);
8686
0
      break;
8687
0
    }
8688
8689
0
  if (oinsn)
8690
0
    *pinsn = oinsn;
8691
8692
0
  return oinsn != 0;
8693
0
}
8694
8695
/* Linker hasn't found the correct merge section for non-section symbol
8696
   in relax time, this work is left to the function elf_link_input_bfd().
8697
   So for non-section symbol, _bfd_merged_section_offset is also needed
8698
   to find the correct symbol address.  */
8699
8700
static bfd_vma
8701
nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8702
        asection **psec, Elf_Internal_Rela *rel)
8703
0
{
8704
0
  asection *sec = *psec;
8705
0
  bfd_vma relocation;
8706
8707
0
  relocation = (sec->output_section->vma
8708
0
    + sec->output_offset + sym->st_value);
8709
0
  if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8710
0
    {
8711
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8712
0
  rel->r_addend =
8713
0
    _bfd_merged_section_offset (abfd, psec,
8714
0
              sym->st_value + rel->r_addend);
8715
0
      else
8716
0
  rel->r_addend =
8717
0
    _bfd_merged_section_offset (abfd, psec,
8718
0
              sym->st_value) + rel->r_addend;
8719
8720
0
      if (sec != *psec)
8721
0
  {
8722
    /* If we have changed the section, and our original section is
8723
       marked with SEC_EXCLUDE, it means that the original
8724
       SEC_MERGE section has been completely subsumed in some
8725
       other SEC_MERGE section.  In this case, we need to leave
8726
       some info around for --emit-relocs.  */
8727
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
8728
0
      sec->kept_section = *psec;
8729
0
    sec = *psec;
8730
0
  }
8731
0
      rel->r_addend -= relocation;
8732
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
8733
0
    }
8734
0
  return relocation;
8735
0
}
8736
8737
static bfd_vma
8738
calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8739
        Elf_Internal_Sym *isymbuf,
8740
        Elf_Internal_Shdr *symtab_hdr)
8741
0
{
8742
0
  bfd_signed_vma foff;
8743
0
  bfd_vma symval, addend;
8744
0
  Elf_Internal_Rela irel_fn;
8745
0
  Elf_Internal_Sym *isym;
8746
0
  asection *sym_sec;
8747
8748
  /* Get the value of the symbol referred to by the reloc.  */
8749
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8750
0
    {
8751
      /* A local symbol.  */
8752
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
8753
8754
0
      if (isym->st_shndx == SHN_UNDEF)
8755
0
  sym_sec = bfd_und_section_ptr;
8756
0
      else if (isym->st_shndx == SHN_ABS)
8757
0
  sym_sec = bfd_abs_section_ptr;
8758
0
      else if (isym->st_shndx == SHN_COMMON)
8759
0
  sym_sec = bfd_com_section_ptr;
8760
0
      else
8761
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8762
0
      memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8763
0
      symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8764
0
      addend = irel_fn.r_addend;
8765
0
    }
8766
0
  else
8767
0
    {
8768
0
      unsigned long indx;
8769
0
      struct elf_link_hash_entry *h;
8770
8771
      /* An external symbol.  */
8772
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8773
0
      h = elf_sym_hashes (abfd)[indx];
8774
0
      BFD_ASSERT (h != NULL);
8775
8776
0
      while (h->root.type == bfd_link_hash_indirect
8777
0
       || h->root.type == bfd_link_hash_warning)
8778
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
8779
8780
0
      if (h->root.type != bfd_link_hash_defined
8781
0
    && h->root.type != bfd_link_hash_defweak)
8782
  /* This appears to be a reference to an undefined
8783
     symbol.  Just ignore it--it will be caught by the
8784
     regular reloc processing.  */
8785
0
  return 0;
8786
8787
0
      if (h->root.u.def.section->flags & SEC_MERGE)
8788
0
  {
8789
0
    sym_sec = h->root.u.def.section;
8790
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
8791
0
                 h->root.u.def.value);
8792
0
    symval = symval + sym_sec->output_section->vma
8793
0
       + sym_sec->output_offset;
8794
0
  }
8795
0
      else
8796
0
  symval = (h->root.u.def.value
8797
0
      + h->root.u.def.section->output_section->vma
8798
0
      + h->root.u.def.section->output_offset);
8799
0
      addend = irel->r_addend;
8800
0
    }
8801
8802
0
  foff = symval + addend;
8803
8804
0
  return foff;
8805
0
}
8806
8807
static int
8808
is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8809
        asection *sec, Elf_Internal_Rela *rel)
8810
0
{
8811
0
  bfd_byte *contents;
8812
0
  unsigned short insn16;
8813
8814
0
  if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8815
0
    return false;
8816
0
  contents = elf_section_data (sec)->this_hdr.contents;
8817
0
  insn16 = bfd_getb16 (contents + rel->r_offset);
8818
0
  if (insn16 == NDS32_NOP16)
8819
0
    return true;
8820
0
  return false;
8821
0
}
8822
8823
/* It checks whether the instruction could be converted to
8824
   16-bit form and returns the converted one.
8825
8826
   `internal_relocs' is supposed to be sorted.  */
8827
8828
static int
8829
is_convert_32_to_16 (bfd *abfd, asection *sec,
8830
         Elf_Internal_Rela *reloc,
8831
         Elf_Internal_Rela *internal_relocs,
8832
         Elf_Internal_Rela *irelend,
8833
         uint16_t *insn16)
8834
0
{
8835
0
#define NORMAL_32_TO_16 (1 << 0)
8836
0
#define SPECIAL_32_TO_16 (1 << 1)
8837
0
  bfd_byte *contents = NULL;
8838
0
  bfd_signed_vma off;
8839
0
  bfd_vma mem_addr;
8840
0
  uint32_t insn = 0;
8841
0
  Elf_Internal_Rela *pc_rel;
8842
0
  Elf_Internal_Shdr *symtab_hdr;
8843
0
  Elf_Internal_Sym *isymbuf = NULL;
8844
0
  int convert_type;
8845
0
  bfd_vma offset;
8846
8847
0
  if (reloc->r_offset + 4 > sec->size)
8848
0
    return false;
8849
8850
0
  offset = reloc->r_offset;
8851
8852
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true))
8853
0
    return false;
8854
0
  insn = bfd_getb32 (contents + offset);
8855
8856
0
  if (bfd_elf_nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8857
0
    convert_type = NORMAL_32_TO_16;
8858
0
  else if (special_convert_32_to_16 (insn, insn16, reloc))
8859
0
    convert_type = SPECIAL_32_TO_16;
8860
0
  else
8861
0
    return false;
8862
8863
0
  symtab_hdr = &elf_symtab_hdr (abfd);
8864
0
  if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8865
0
    return false;
8866
8867
  /* Find the first relocation of the same relocation-type,
8868
     so we iteratie them forward.  */
8869
0
  pc_rel = reloc;
8870
0
  while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8871
0
    pc_rel--;
8872
8873
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8874
0
    {
8875
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8876
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8877
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8878
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8879
0
  {
8880
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8881
0
    if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8882
0
        || off == 0)
8883
0
      return false;
8884
0
    break;
8885
0
  }
8886
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8887
0
  {
8888
    /* movi => movi55  */
8889
0
    mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8890
0
                 symtab_hdr);
8891
    /* mem_addr is unsigned, but the value should
8892
       be between [-16, 15].  */
8893
0
    if ((mem_addr + 0x10) >> 5)
8894
0
      return false;
8895
0
    break;
8896
0
  }
8897
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8898
0
         || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8899
0
  {
8900
    /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8901
       because it can be relaxed to addi for TLS_LE_ADD.  */
8902
0
    return false;
8903
0
  }
8904
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8905
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8906
0
         && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8907
0
         && convert_type == SPECIAL_32_TO_16)
8908
0
  {
8909
    /* fp-as-gp
8910
       We've selected a best fp-base for this access, so we can
8911
       always resolve it anyway.  Do nothing.  */
8912
0
    break;
8913
0
  }
8914
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8915
0
    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8916
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8917
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8918
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8919
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8920
0
  {
8921
    /* Prevent unresolved addi instruction translate
8922
       to addi45 or addi333.  */
8923
0
    return false;
8924
0
  }
8925
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8926
0
  {
8927
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8928
0
    if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8929
0
      return false;
8930
0
    break;
8931
0
  }
8932
0
    }
8933
8934
0
  return true;
8935
0
}
8936
8937
static void
8938
nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8939
        Elf_Internal_Rela *reloc,
8940
        Elf_Internal_Rela *internal_relocs,
8941
        Elf_Internal_Rela *irelend,
8942
        unsigned short insn16)
8943
0
{
8944
0
  Elf_Internal_Rela *pc_rel;
8945
0
  bfd_vma offset;
8946
8947
0
  offset = reloc->r_offset;
8948
0
  bfd_putb16 (insn16, contents + offset);
8949
  /* Find the first relocation of the same relocation-type,
8950
     so we iteratie them forward.  */
8951
0
  pc_rel = reloc;
8952
0
  while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8953
0
    pc_rel--;
8954
8955
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8956
0
    {
8957
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8958
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8959
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8960
0
  {
8961
0
    pc_rel->r_info =
8962
0
      ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8963
0
  }
8964
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8965
0
  pc_rel->r_info =
8966
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8967
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8968
0
  pc_rel->r_info =
8969
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8970
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8971
0
         || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8972
0
  pc_rel->r_info =
8973
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8974
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8975
0
  pc_rel->r_info =
8976
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8977
0
    }
8978
0
}
8979
8980
/* Find a relocation of type specified by `reloc_type'
8981
   of the same r_offset with reloc.
8982
   If not found, return irelend.
8983
8984
   Assuming relocations are sorted by r_offset,
8985
   we find the relocation from `reloc' backward untill relocs,
8986
   or find it from `reloc' forward untill irelend.  */
8987
8988
static Elf_Internal_Rela *
8989
find_relocs_at_address (Elf_Internal_Rela *reloc,
8990
      Elf_Internal_Rela *relocs,
8991
      Elf_Internal_Rela *irelend,
8992
      enum elf_nds32_reloc_type reloc_type)
8993
0
{
8994
0
  Elf_Internal_Rela *rel_t;
8995
8996
  /* Find backward.  */
8997
0
  for (rel_t = reloc;
8998
0
       rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8999
0
       rel_t--)
9000
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9001
0
      return rel_t;
9002
9003
  /* We didn't find it backward.  Try find it forward.  */
9004
0
  for (rel_t = reloc;
9005
0
       rel_t < irelend && rel_t->r_offset == reloc->r_offset;
9006
0
       rel_t++)
9007
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9008
0
      return rel_t;
9009
9010
0
  return irelend;
9011
0
}
9012
9013
/* Find a relocation of specified type and offset.
9014
   `reloc' is just a refence point to find a relocation at specified offset.
9015
   If not found, return irelend.
9016
9017
   Assuming relocations are sorted by r_offset,
9018
   we find the relocation from `reloc' backward untill relocs,
9019
   or find it from `reloc' forward untill irelend.  */
9020
9021
static Elf_Internal_Rela *
9022
find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9023
           Elf_Internal_Rela *relocs,
9024
           Elf_Internal_Rela *irelend,
9025
           enum elf_nds32_reloc_type reloc_type,
9026
           bfd_vma offset_p)
9027
0
{
9028
0
  Elf_Internal_Rela *rel_t = NULL;
9029
9030
  /* First, we try to find a relocation of offset `offset_p',
9031
     and then we use find_relocs_at_address to find specific type.  */
9032
9033
0
  if (reloc->r_offset > offset_p)
9034
0
    {
9035
      /* Find backward.  */
9036
0
      for (rel_t = reloc;
9037
0
     rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9038
0
  /* Do nothing.  */;
9039
0
    }
9040
0
  else if (reloc->r_offset < offset_p)
9041
0
    {
9042
      /* Find forward.  */
9043
0
      for (rel_t = reloc;
9044
0
     rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9045
0
  /* Do nothing.  */;
9046
0
    }
9047
0
  else
9048
0
    rel_t = reloc;
9049
9050
  /* Not found?  */
9051
0
  if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9052
0
    return irelend;
9053
9054
0
  return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9055
0
}
9056
9057
typedef struct nds32_elf_blank nds32_elf_blank_t;
9058
struct nds32_elf_blank
9059
{
9060
  /* Where the blank begins.  */
9061
  bfd_vma offset;
9062
  /* The size of the blank.  */
9063
  bfd_vma size;
9064
  /* The accumulative size before this blank.  */
9065
  bfd_vma total_size;
9066
  nds32_elf_blank_t *next;
9067
  nds32_elf_blank_t *prev;
9068
};
9069
9070
static nds32_elf_blank_t *blank_free_list = NULL;
9071
9072
static nds32_elf_blank_t *
9073
create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9074
0
{
9075
0
  nds32_elf_blank_t *blank_t;
9076
9077
0
  if (blank_free_list)
9078
0
    {
9079
0
      blank_t = blank_free_list;
9080
0
      blank_free_list = blank_free_list->next;
9081
0
    }
9082
0
  else
9083
0
    blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9084
9085
0
  if (blank_t == NULL)
9086
0
    return NULL;
9087
9088
0
  blank_t->offset = offset_p;
9089
0
  blank_t->size = size_p;
9090
0
  blank_t->total_size = 0;
9091
0
  blank_t->next = NULL;
9092
0
  blank_t->prev = NULL;
9093
9094
0
  return blank_t;
9095
0
}
9096
9097
static void
9098
remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9099
0
{
9100
0
  if (blank_free_list)
9101
0
    {
9102
0
      blank_free_list->prev = blank_p;
9103
0
      blank_p->next = blank_free_list;
9104
0
    }
9105
0
  else
9106
0
    blank_p->next = NULL;
9107
9108
0
  blank_p->prev = NULL;
9109
0
  blank_free_list = blank_p;
9110
0
}
9111
9112
static void
9113
clean_nds32_elf_blank (void)
9114
0
{
9115
0
  nds32_elf_blank_t *blank_t;
9116
9117
0
  while (blank_free_list)
9118
0
    {
9119
0
      blank_t = blank_free_list;
9120
0
      blank_free_list = blank_free_list->next;
9121
0
      free (blank_t);
9122
0
    }
9123
0
}
9124
9125
static nds32_elf_blank_t *
9126
search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9127
0
{
9128
0
  nds32_elf_blank_t *blank_t;
9129
9130
0
  if (!blank_p)
9131
0
    return NULL;
9132
0
  blank_t = blank_p;
9133
9134
0
  while (blank_t && addr < blank_t->offset)
9135
0
    blank_t = blank_t->prev;
9136
0
  while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9137
0
    blank_t = blank_t->next;
9138
9139
0
  return blank_t;
9140
0
}
9141
9142
static bfd_vma
9143
get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9144
         int overwrite)
9145
0
{
9146
0
  nds32_elf_blank_t *blank_t;
9147
9148
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9149
0
  if (!blank_t)
9150
0
    return 0;
9151
9152
0
  if (overwrite)
9153
0
    *blank_p = blank_t;
9154
9155
0
  if (addr < blank_t->offset + blank_t->size)
9156
0
    return blank_t->total_size + (addr - blank_t->offset);
9157
0
  else
9158
0
    return blank_t->total_size + blank_t->size;
9159
0
}
9160
9161
static bool
9162
insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9163
0
{
9164
0
  nds32_elf_blank_t *blank_t, *blank_t2;
9165
9166
0
  if (!*blank_p)
9167
0
    {
9168
0
      *blank_p = create_nds32_elf_blank (addr, len);
9169
0
      return *blank_p != NULL;
9170
0
    }
9171
9172
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9173
9174
0
  if (blank_t == NULL)
9175
0
    {
9176
0
      blank_t = create_nds32_elf_blank (addr, len);
9177
0
      if (!blank_t)
9178
0
  return false;
9179
0
      while ((*blank_p)->prev != NULL)
9180
0
  *blank_p = (*blank_p)->prev;
9181
0
      blank_t->next = *blank_p;
9182
0
      (*blank_p)->prev = blank_t;
9183
0
      (*blank_p) = blank_t;
9184
0
      return true;
9185
0
    }
9186
9187
0
  if (addr < blank_t->offset + blank_t->size)
9188
0
    {
9189
      /* Extend the origin blank.  */
9190
0
      if (addr + len > blank_t->offset + blank_t->size)
9191
0
  blank_t->size = addr + len - blank_t->offset;
9192
0
    }
9193
0
  else
9194
0
    {
9195
0
      blank_t2 = create_nds32_elf_blank (addr, len);
9196
0
      if (!blank_t2)
9197
0
  return false;
9198
0
      if (blank_t->next)
9199
0
  {
9200
0
    blank_t->next->prev = blank_t2;
9201
0
    blank_t2->next = blank_t->next;
9202
0
  }
9203
0
      blank_t2->prev = blank_t;
9204
0
      blank_t->next = blank_t2;
9205
0
      *blank_p = blank_t2;
9206
0
    }
9207
9208
0
  return true;
9209
0
}
9210
9211
static bool
9212
insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9213
             bfd_vma len)
9214
0
{
9215
0
  nds32_elf_blank_t *blank_t;
9216
9217
0
  if (!insert_nds32_elf_blank (blank_p, addr, len))
9218
0
    return false;
9219
9220
0
  blank_t = *blank_p;
9221
9222
0
  if (!blank_t->prev)
9223
0
    {
9224
0
      blank_t->total_size = 0;
9225
0
      blank_t = blank_t->next;
9226
0
    }
9227
9228
0
  while (blank_t)
9229
0
    {
9230
0
      blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9231
0
      blank_t = blank_t->next;
9232
0
    }
9233
9234
0
  return true;
9235
0
}
9236
9237
static void
9238
calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9239
0
{
9240
0
  nds32_elf_blank_t *blank_t;
9241
0
  bfd_vma total_size = 0;
9242
9243
0
  if (!blank_p)
9244
0
    return;
9245
9246
0
  blank_t = blank_p;
9247
0
  while (blank_t->prev)
9248
0
    blank_t = blank_t->prev;
9249
0
  while (blank_t)
9250
0
    {
9251
0
      blank_t->total_size = total_size;
9252
0
      total_size += blank_t->size;
9253
0
      blank_t = blank_t->next;
9254
0
    }
9255
0
}
9256
9257
static bool
9258
nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9259
             nds32_elf_blank_t *blank_p)
9260
0
{
9261
0
  Elf_Internal_Shdr *symtab_hdr;  /* Symbol table header of this bfd.  */
9262
0
  Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd.  */
9263
0
  Elf_Internal_Sym *isymend;    /* Symbol entry iterator.  */
9264
0
  unsigned int sec_shndx;   /* The section the be relaxed.  */
9265
0
  bfd_byte *contents;     /* Contents data of iterating section.  */
9266
0
  Elf_Internal_Rela *internal_relocs;
9267
0
  Elf_Internal_Rela *irel;
9268
0
  Elf_Internal_Rela *irelend;
9269
0
  struct elf_link_hash_entry **sym_hashes;
9270
0
  struct elf_link_hash_entry **end_hashes;
9271
0
  unsigned int symcount;
9272
0
  asection *sect;
9273
0
  nds32_elf_blank_t *blank_t;
9274
0
  nds32_elf_blank_t *blank_t2;
9275
0
  nds32_elf_blank_t *blank_head;
9276
9277
0
  blank_head = blank_t = blank_p;
9278
0
  while (blank_head->prev != NULL)
9279
0
    blank_head = blank_head->prev;
9280
0
  while (blank_t->next != NULL)
9281
0
    blank_t = blank_t->next;
9282
9283
0
  if (blank_t->offset + blank_t->size <= sec->size)
9284
0
    {
9285
0
      blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9286
0
      blank_t->next->prev = blank_t;
9287
0
    }
9288
0
  if (blank_head->offset > 0)
9289
0
    {
9290
0
      blank_head->prev = create_nds32_elf_blank (0, 0);
9291
0
      blank_head->prev->next = blank_head;
9292
0
      blank_head = blank_head->prev;
9293
0
    }
9294
9295
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9296
9297
  /* The deletion must stop at the next ALIGN reloc for an alignment
9298
     power larger than the number of bytes we are deleting.  */
9299
9300
0
  symtab_hdr = &elf_symtab_hdr (abfd);
9301
0
  if (!nds32_get_local_syms (abfd, sec, &isym))
9302
0
    return false;
9303
9304
0
  if (isym == NULL)
9305
0
    {
9306
0
      isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9307
0
           symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9308
0
      symtab_hdr->contents = (bfd_byte *) isym;
9309
0
    }
9310
9311
0
  if (isym == NULL || symtab_hdr->sh_info == 0)
9312
0
    return false;
9313
9314
0
  blank_t = blank_head;
9315
0
  calc_nds32_blank_total (blank_head);
9316
9317
0
  for (sect = abfd->sections; sect != NULL; sect = sect->next)
9318
0
    {
9319
      /* Adjust all the relocs.  */
9320
9321
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
9322
0
      internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9323
0
               true /* keep_memory */);
9324
0
      irelend = internal_relocs + sect->reloc_count;
9325
9326
0
      blank_t = blank_head;
9327
0
      blank_t2 = blank_head;
9328
9329
0
      if (!(sect->flags & SEC_RELOC))
9330
0
  continue;
9331
9332
0
      contents = NULL;
9333
0
      nds32_get_section_contents (abfd, sect, &contents, true);
9334
9335
0
      for (irel = internal_relocs; irel < irelend; irel++)
9336
0
  {
9337
0
    bfd_vma raddr;
9338
9339
0
    if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9340
0
        && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9341
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9342
0
      {
9343
0
        unsigned long val = 0;
9344
0
        unsigned long mask;
9345
0
        long before, between;
9346
0
        long offset = 0;
9347
9348
0
        switch (ELF32_R_TYPE (irel->r_info))
9349
0
    {
9350
0
    case R_NDS32_DIFF8:
9351
0
      offset = bfd_get_8 (abfd, contents + irel->r_offset);
9352
0
      break;
9353
0
    case R_NDS32_DIFF16:
9354
0
      offset = bfd_get_16 (abfd, contents + irel->r_offset);
9355
0
      break;
9356
0
    case R_NDS32_DIFF32:
9357
0
      val = bfd_get_32 (abfd, contents + irel->r_offset);
9358
      /* Get the signed bit and mask for the high part.  The
9359
         gcc will alarm when right shift 32-bit since the
9360
         type size of long may be 32-bit.  */
9361
0
      mask = 0 - (val >> 31);
9362
0
      if (mask)
9363
0
        offset = (val | (mask - 0xffffffff));
9364
0
      else
9365
0
        offset = val;
9366
0
      break;
9367
0
    default:
9368
0
      BFD_ASSERT (0);
9369
0
    }
9370
9371
        /*      DIFF value
9372
    0      |encoded in location|
9373
    |------------|-------------------|---------
9374
          sym+off(addend)
9375
    -- before ---| *****************
9376
    --------------------- between ---|
9377
9378
    We only care how much data are relax between DIFF,
9379
    marked as ***.  */
9380
9381
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9382
0
        between = get_nds32_elf_blank_total (&blank_t,
9383
0
               irel->r_addend + offset, 0);
9384
0
        if (between == before)
9385
0
    goto done_adjust_diff;
9386
9387
0
        switch (ELF32_R_TYPE (irel->r_info))
9388
0
    {
9389
0
    case R_NDS32_DIFF8:
9390
0
      bfd_put_8 (abfd, offset - (between - before),
9391
0
           contents + irel->r_offset);
9392
0
      break;
9393
0
    case R_NDS32_DIFF16:
9394
0
      bfd_put_16 (abfd, offset - (between - before),
9395
0
            contents + irel->r_offset);
9396
0
      break;
9397
0
    case R_NDS32_DIFF32:
9398
0
      bfd_put_32 (abfd, offset - (between - before),
9399
0
            contents + irel->r_offset);
9400
0
      break;
9401
0
    }
9402
0
      }
9403
0
    else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9404
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9405
0
      {
9406
0
        bfd_vma val;
9407
0
        unsigned long before, between;
9408
0
        bfd_byte *endp, *p;
9409
9410
0
        p = contents + irel->r_offset;
9411
0
        endp = contents + sec->size;
9412
0
        val = _bfd_safe_read_leb128 (abfd, &p, false, endp);
9413
9414
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9415
0
        between = get_nds32_elf_blank_total (&blank_t,
9416
0
               irel->r_addend + val, 0);
9417
0
        if (between == before)
9418
0
    goto done_adjust_diff;
9419
9420
0
        endp = p - 1;
9421
0
        p = contents + irel->r_offset;
9422
0
        memset (p, 0x80, endp - p);
9423
0
        *endp = 0;
9424
0
        p = write_uleb128 (p, val - (between - before)) - 1;
9425
0
        if (p < endp)
9426
0
    *p |= 0x80;
9427
0
      }
9428
0
  done_adjust_diff:
9429
9430
0
    if (sec == sect)
9431
0
      {
9432
0
        raddr = irel->r_offset;
9433
0
        irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9434
0
                 irel->r_offset, 1);
9435
9436
0
        if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9437
0
    continue;
9438
0
        if (blank_t2 && blank_t2->next
9439
0
      && (blank_t2->offset > raddr
9440
0
          || blank_t2->next->offset <= raddr))
9441
0
    _bfd_error_handler
9442
0
      (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9443
0
       abfd);
9444
9445
        /* Mark reloc in deleted portion as NONE.
9446
     For some relocs like R_NDS32_LABEL that doesn't modify the
9447
     content in the section.  R_NDS32_LABEL doesn't belong to the
9448
     instruction in the section, so we should preserve it.  */
9449
0
        if (raddr >= blank_t2->offset
9450
0
      && raddr < blank_t2->offset + blank_t2->size
9451
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9452
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9453
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9454
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9455
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9456
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9457
0
    {
9458
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9459
0
                 R_NDS32_NONE);
9460
0
      continue;
9461
0
    }
9462
0
      }
9463
9464
0
    if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9465
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9466
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9467
0
      continue;
9468
9469
0
    if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9470
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9471
0
        && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9472
0
      {
9473
0
        if (irel->r_addend <= sec->size)
9474
0
    irel->r_addend -=
9475
0
      get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9476
0
      }
9477
0
  }
9478
0
    }
9479
9480
  /* Adjust the local symbols defined in this section.  */
9481
0
  blank_t = blank_head;
9482
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9483
0
    {
9484
0
      if (isym->st_shndx == sec_shndx)
9485
0
  {
9486
0
    if (isym->st_value <= sec->size)
9487
0
      {
9488
0
        bfd_vma ahead;
9489
0
        bfd_vma orig_addr = isym->st_value;
9490
9491
0
        ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9492
0
        isym->st_value -= ahead;
9493
9494
        /* Adjust function size.  */
9495
0
        if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9496
0
      && isym->st_size > 0)
9497
0
    isym->st_size -=
9498
0
      get_nds32_elf_blank_total
9499
0
      (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9500
0
      }
9501
0
  }
9502
0
    }
9503
9504
  /* Now adjust the global symbols defined in this section.  */
9505
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9506
0
        - symtab_hdr->sh_info);
9507
0
  sym_hashes = elf_sym_hashes (abfd);
9508
0
  end_hashes = sym_hashes + symcount;
9509
0
  blank_t = blank_head;
9510
0
  for (; sym_hashes < end_hashes; sym_hashes++)
9511
0
    {
9512
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
9513
9514
0
      if ((sym_hash->root.type == bfd_link_hash_defined
9515
0
     || sym_hash->root.type == bfd_link_hash_defweak)
9516
0
    && sym_hash->root.u.def.section == sec)
9517
0
  {
9518
0
    if (sym_hash->root.u.def.value <= sec->size)
9519
0
      {
9520
0
        bfd_vma ahead;
9521
0
        bfd_vma orig_addr = sym_hash->root.u.def.value;
9522
9523
0
        ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9524
0
        sym_hash->root.u.def.value -= ahead;
9525
9526
        /* Adjust function size.  */
9527
0
        if (sym_hash->type == STT_FUNC)
9528
0
    sym_hash->size -=
9529
0
      get_nds32_elf_blank_total
9530
0
      (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9531
9532
0
      }
9533
0
  }
9534
0
    }
9535
9536
0
  contents = elf_section_data (sec)->this_hdr.contents;
9537
0
  blank_t = blank_head;
9538
0
  while (blank_t->next)
9539
0
    {
9540
      /* Actually delete the bytes.  */
9541
9542
      /* If current blank is the last blank overlap with current section,
9543
   go to finish process.  */
9544
0
      if (sec->size <= (blank_t->next->offset))
9545
0
  break;
9546
9547
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9548
0
         contents + blank_t->offset + blank_t->size,
9549
0
         blank_t->next->offset - (blank_t->offset + blank_t->size));
9550
9551
0
      blank_t = blank_t->next;
9552
0
    }
9553
9554
0
  if (sec->size > (blank_t->offset + blank_t->size))
9555
0
    {
9556
      /* There are remaining code between blank and section boundary.
9557
   Move the remaining code to appropriate location.  */
9558
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9559
0
         contents + blank_t->offset + blank_t->size,
9560
0
         sec->size - (blank_t->offset + blank_t->size));
9561
0
      sec->size -= blank_t->total_size + blank_t->size;
9562
0
    }
9563
0
  else
9564
    /* This blank is not entirely included in the section,
9565
       reduce the section size by only part of the blank size.  */
9566
0
    sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9567
9568
0
  while (blank_head)
9569
0
    {
9570
0
      blank_t = blank_head;
9571
0
      blank_head = blank_head->next;
9572
0
      remove_nds32_elf_blank (blank_t);
9573
0
    }
9574
9575
0
  return true;
9576
0
}
9577
9578
/* Get the contents of a section.  */
9579
9580
static int
9581
nds32_get_section_contents (bfd *abfd, asection *sec,
9582
          bfd_byte **contents_p, bool cache)
9583
50
{
9584
  /* Get the section contents.  */
9585
50
  if (elf_section_data (sec)->this_hdr.contents != NULL)
9586
0
    *contents_p = elf_section_data (sec)->this_hdr.contents;
9587
50
  else
9588
50
    {
9589
50
      if (!bfd_get_full_section_contents (abfd, sec, contents_p))
9590
2
  return false;
9591
48
      if (cache)
9592
0
  elf_section_data (sec)->this_hdr.contents = *contents_p;
9593
48
    }
9594
9595
48
  return true;
9596
50
}
9597
9598
/* Get the contents of the internal symbol of abfd.  */
9599
9600
static int
9601
nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9602
          Elf_Internal_Sym **isymbuf_p)
9603
0
{
9604
0
  Elf_Internal_Shdr *symtab_hdr;
9605
0
  symtab_hdr = &elf_symtab_hdr (abfd);
9606
9607
  /* Read this BFD's local symbols if we haven't done so already.  */
9608
0
  if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9609
0
    {
9610
0
      *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9611
0
      if (*isymbuf_p == NULL)
9612
0
  {
9613
0
    *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9614
0
               symtab_hdr->sh_info, 0,
9615
0
               NULL, NULL, NULL);
9616
0
    if (*isymbuf_p == NULL)
9617
0
      return false;
9618
0
  }
9619
0
    }
9620
0
  symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9621
9622
0
  return true;
9623
0
}
9624
9625
/* Range of small data.  */
9626
static bfd_vma sdata_range[2][2];
9627
static bfd_vma const sdata_init_range[2] =
9628
{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
9629
9630
static int
9631
nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9632
         bfd_byte *contents, bfd_vma addr)
9633
0
{
9634
0
  unsigned long insn = bfd_getb32 (contents + addr);
9635
9636
0
  if (insn & 0x80000000)
9637
0
    return 2;
9638
9639
0
  return 4;
9640
0
}
9641
9642
/* Set the gp relax range.  We have to measure the safe range
9643
   to do gp relaxation.  */
9644
9645
static void
9646
relax_range_measurement (bfd *abfd, struct bfd_link_info *link_info)
9647
0
{
9648
0
  asection *sec_f, *sec_b;
9649
  /* For upper bound.   */
9650
0
  bfd_vma maxpgsz;
9651
0
  bfd_vma align;
9652
0
  static int decide_relax_range = 0;
9653
0
  int i;
9654
0
  int range_number = ARRAY_SIZE (sdata_init_range);
9655
9656
0
  if (decide_relax_range)
9657
0
    return;
9658
0
  decide_relax_range = 1;
9659
9660
0
  if (sda_rela_sec == NULL)
9661
0
    {
9662
      /* Since there is no data sections, we assume the range is page size.  */
9663
0
      for (i = 0; i < range_number; i++)
9664
0
  {
9665
0
    sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9666
0
    sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9667
0
  }
9668
0
      return;
9669
0
    }
9670
9671
  /* Get the biggest alignment power after the gp located section.  */
9672
0
  sec_f = sda_rela_sec->output_section;
9673
0
  sec_b = sec_f->next;
9674
0
  align = 0;
9675
0
  while (sec_b != NULL)
9676
0
    {
9677
0
      if ((unsigned)(1 << sec_b->alignment_power) > align)
9678
0
  align = (1 << sec_b->alignment_power);
9679
0
      sec_b = sec_b->next;
9680
0
    }
9681
9682
0
  if (link_info != NULL)
9683
0
    maxpgsz = link_info->maxpagesize;
9684
0
  else
9685
0
    maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9686
  /* I guess we can not determine the section before
9687
     gp located section, so we assume the align is max page size.  */
9688
0
  for (i = 0; i < range_number; i++)
9689
0
    {
9690
0
      sdata_range[i][1] = sdata_init_range[i] - align;
9691
0
      BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9692
0
      sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9693
0
      BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9694
0
    }
9695
0
}
9696
9697
/* These are macros used to check flags encoded in r_addend.
9698
   They are only used by nds32_elf_relax_section ().  */
9699
0
#define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
9700
0
#define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
9701
#define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
9702
0
#define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
9703
9704
static const char * unrecognized_reloc_msg =
9705
  /* xgettext:c-format */
9706
  N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9707
9708
/* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
9709
9710
static bool
9711
nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9712
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9713
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9714
         Elf_Internal_Shdr *symtab_hdr)
9715
0
{
9716
  /* There are 3 variations for LONGCALL1
9717
     case 4-4-2; 16-bit on, optimize off or optimize for space
9718
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9719
     ori   ta, ta, lo12(symbol) ; LO12S0
9720
     jral5 ta     ;
9721
9722
     case 4-4-4; 16-bit off, optimize don't care
9723
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9724
     ori   ta, ta, lo12(symbol) ; LO12S0
9725
     jral  ta     ;
9726
9727
     case 4-4-4; 16-bit on, optimize for speed
9728
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9729
     ori   ta, ta, lo12(symbol) ; LO12S0
9730
     jral  ta     ;
9731
     Check code for -mlong-calls output.  */
9732
9733
  /* Get the reloc for the address from which the register is
9734
     being loaded.  This reloc will tell us which function is
9735
     actually being called.  */
9736
9737
0
  bfd_vma laddr;
9738
0
  int seq_len;  /* Original length of instruction sequence.  */
9739
0
  uint32_t insn;
9740
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9741
0
  bfd_signed_vma foff;
9742
0
  uint16_t insn16;
9743
9744
0
  irelend = internal_relocs + sec->reloc_count;
9745
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9746
0
  laddr = irel->r_offset;
9747
0
  *insn_len = seq_len;
9748
9749
0
  hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9750
0
             R_NDS32_HI20_RELA, laddr);
9751
0
  lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9752
0
             R_NDS32_LO12S0_ORI_RELA,
9753
0
             laddr + 4);
9754
9755
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9756
0
    {
9757
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9758
0
        (uint64_t) irel->r_offset);
9759
0
      return false;
9760
0
    }
9761
9762
  /* Get the value of the symbol referred to by the reloc.  */
9763
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9764
9765
  /* This condition only happened when symbol is undefined.  */
9766
0
  if (foff == 0
9767
0
      || foff < -CONSERVATIVE_24BIT_S1
9768
0
      || foff >= CONSERVATIVE_24BIT_S1)
9769
0
    return false;
9770
9771
  /* Relax to: jal symbol; 25_PCREL.  */
9772
  /* For simplicity of coding, we are going to modify the section
9773
     contents, the section relocs, and the BFD symbol table.  We
9774
     must tell the rest of the code not to free up this
9775
     information.  It would be possible to instead create a table
9776
     of changes which have to be made, as is done in coff-mips.c;
9777
     that would be more work, but would require less memory when
9778
     the linker is run.  */
9779
9780
  /* Replace the long call with a jal.  */
9781
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9782
0
             R_NDS32_25_PCREL_RELA);
9783
0
  irel->r_addend = hi_irelfn->r_addend;
9784
9785
  /* We don't resolve this here but resolve it in relocate_section.  */
9786
0
  insn = INSN_JAL;
9787
0
  bfd_putb32 (insn, contents + irel->r_offset);
9788
9789
0
  hi_irelfn->r_info =
9790
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9791
0
  lo_irelfn->r_info =
9792
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9793
0
  *insn_len = 4;
9794
9795
0
  if (seq_len & 0x2)
9796
0
    {
9797
0
      insn16 = NDS32_NOP16;
9798
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9799
0
      lo_irelfn->r_info =
9800
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9801
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9802
0
      *insn_len += 2;
9803
0
    }
9804
0
  return true;
9805
0
}
9806
9807
0
#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9808
/* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
9809
9810
static bool
9811
nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9812
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9813
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9814
         Elf_Internal_Shdr *symtab_hdr)
9815
0
{
9816
  /* bltz  rt, .L1   ; LONGCALL2
9817
     jal   symbol   ; 25_PCREL
9818
     .L1: */
9819
9820
  /* Get the reloc for the address from which the register is
9821
     being loaded.  This reloc will tell us which function is
9822
     actually being called.  */
9823
9824
0
  bfd_vma laddr;
9825
0
  uint32_t insn;
9826
0
  Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9827
0
  bfd_signed_vma foff;
9828
9829
0
  irelend = internal_relocs + sec->reloc_count;
9830
0
  laddr = irel->r_offset;
9831
0
  i1_irelfn =
9832
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9833
0
         R_NDS32_25_PCREL_RELA, laddr + 4);
9834
9835
0
  if (i1_irelfn == irelend)
9836
0
    {
9837
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9838
0
        (uint64_t) irel->r_offset);
9839
0
      return false;
9840
0
    }
9841
9842
0
  insn = bfd_getb32 (contents + laddr);
9843
9844
  /* Get the value of the symbol referred to by the reloc.  */
9845
0
  foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9846
9847
0
  if (foff == 0
9848
0
      || foff < -CONSERVATIVE_16BIT_S1
9849
0
      || foff >= CONSERVATIVE_16BIT_S1)
9850
0
    return false;
9851
9852
  /* Relax to bgezal   rt, label ; 17_PCREL
9853
     or   bltzal   rt, label ; 17_PCREL */
9854
9855
  /* Convert to complimentary conditional call.  */
9856
0
  insn = CONVERT_CONDITION_CALL (insn);
9857
9858
  /* For simplicity of coding, we are going to modify the section
9859
     contents, the section relocs, and the BFD symbol table.  We
9860
     must tell the rest of the code not to free up this
9861
     information.  It would be possible to instead create a table
9862
     of changes which have to be made, as is done in coff-mips.c;
9863
     that would be more work, but would require less memory when
9864
     the linker is run.  */
9865
9866
  /* Clean unnessary relocations.  */
9867
0
  i1_irelfn->r_info =
9868
0
    ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9869
0
  cond_irelfn =
9870
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9871
0
         R_NDS32_17_PCREL_RELA, laddr);
9872
0
  if (cond_irelfn != irelend)
9873
0
    cond_irelfn->r_info =
9874
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9875
9876
  /* Replace the long call with a bgezal.  */
9877
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9878
0
             R_NDS32_17_PCREL_RELA);
9879
0
  irel->r_addend = i1_irelfn->r_addend;
9880
9881
0
  bfd_putb32 (insn, contents + irel->r_offset);
9882
9883
0
  *insn_len = 4;
9884
0
  return true;
9885
0
}
9886
9887
/* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9888
9889
static bool
9890
nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9891
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9892
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9893
         Elf_Internal_Shdr *symtab_hdr)
9894
0
{
9895
  /* There are 3 variations for LONGCALL3
9896
     case 4-4-4-2; 16-bit on, optimize off or optimize for space
9897
     bltz  rt,   $1      ; LONGCALL3
9898
     sethi ta,   hi20(symbol)    ; HI20
9899
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9900
     jral5 ta        ;
9901
     $1
9902
9903
     case 4-4-4-4; 16-bit off, optimize don't care
9904
     bltz  rt,   $1      ; LONGCALL3
9905
     sethi ta,   hi20(symbol)    ; HI20
9906
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9907
     jral  ta        ;
9908
     $1
9909
9910
     case 4-4-4-4; 16-bit on, optimize for speed
9911
     bltz  rt,   $1      ; LONGCALL3
9912
     sethi ta,   hi20(symbol)    ; HI20
9913
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9914
     jral  ta        ;
9915
     $1 */
9916
9917
  /* Get the reloc for the address from which the register is
9918
     being loaded.  This reloc will tell us which function is
9919
     actually being called.  */
9920
9921
0
  bfd_vma laddr;
9922
0
  int seq_len;  /* Original length of instruction sequence.  */
9923
0
  uint32_t insn;
9924
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9925
0
  bfd_signed_vma foff;
9926
0
  uint16_t insn16;
9927
9928
0
  irelend = internal_relocs + sec->reloc_count;
9929
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9930
0
  laddr = irel->r_offset;
9931
0
  *insn_len = seq_len;
9932
9933
0
  hi_irelfn =
9934
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9935
0
         R_NDS32_HI20_RELA, laddr + 4);
9936
0
  lo_irelfn =
9937
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9938
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9939
9940
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9941
0
    {
9942
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9943
0
        (uint64_t) irel->r_offset);
9944
0
      return false;
9945
0
    }
9946
9947
  /* Get the value of the symbol referred to by the reloc.  */
9948
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9949
9950
0
  if (foff == 0
9951
0
      || foff < -CONSERVATIVE_24BIT_S1
9952
0
      || foff >= CONSERVATIVE_24BIT_S1)
9953
0
    return false;
9954
9955
0
  insn = bfd_getb32 (contents + laddr);
9956
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9957
0
    {
9958
      /* Relax to  bgezal   rt, label ; 17_PCREL
9959
   or    bltzal   rt, label ; 17_PCREL */
9960
9961
      /* Convert to complimentary conditional call.  */
9962
0
      insn = CONVERT_CONDITION_CALL (insn);
9963
0
      bfd_putb32 (insn, contents + irel->r_offset);
9964
9965
0
      *insn_len = 4;
9966
0
      irel->r_info =
9967
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9968
0
      hi_irelfn->r_info =
9969
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9970
0
      lo_irelfn->r_info =
9971
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9972
9973
0
      cond_irelfn =
9974
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
9975
0
             R_NDS32_17_PCREL_RELA, laddr);
9976
0
      if (cond_irelfn != irelend)
9977
0
  {
9978
0
    cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9979
0
                R_NDS32_17_PCREL_RELA);
9980
0
    cond_irelfn->r_addend = hi_irelfn->r_addend;
9981
0
  }
9982
9983
0
      if (seq_len & 0x2)
9984
0
  {
9985
0
    insn16 = NDS32_NOP16;
9986
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9987
0
    hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9988
0
              R_NDS32_INSN16);
9989
0
    hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9990
0
    insn_len += 2;
9991
0
  }
9992
0
    }
9993
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9994
0
    {
9995
      /* Relax to the following instruction sequence
9996
   bltz  rt,   $1 ; LONGCALL2
9997
   jal   symbol   ; 25_PCREL
9998
   $1 */
9999
0
      *insn_len = 8;
10000
0
      insn = INSN_JAL;
10001
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10002
10003
0
      hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10004
0
          R_NDS32_25_PCREL_RELA);
10005
0
      irel->r_info =
10006
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
10007
10008
0
      lo_irelfn->r_info =
10009
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10010
10011
0
      if (seq_len & 0x2)
10012
0
  {
10013
0
    insn16 = NDS32_NOP16;
10014
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10015
0
    lo_irelfn->r_info =
10016
0
      ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10017
0
    lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10018
0
    insn_len += 2;
10019
0
  }
10020
0
    }
10021
0
  return true;
10022
0
}
10023
10024
/* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
10025
10026
static bool
10027
nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10028
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10029
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10030
         Elf_Internal_Shdr *symtab_hdr)
10031
0
{
10032
  /* There are 3 variations for LONGJUMP1
10033
     case 4-4-2; 16-bit bit on, optimize off or optimize for space
10034
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10035
     ori   ta, ta, lo12(symbol)  ; LO12S0
10036
     jr5   ta      ;
10037
10038
     case 4-4-4; 16-bit off, optimize don't care
10039
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10040
     ori   ta, ta, lo12(symbol)  ; LO12S0
10041
     jr    ta      ;
10042
10043
     case 4-4-4; 16-bit on, optimize for speed
10044
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10045
     ori   ta, ta, lo12(symbol)  ; LO12S0
10046
     jr    ta      ;  */
10047
10048
  /* Get the reloc for the address from which the register is
10049
     being loaded.  This reloc will tell us which function is
10050
     actually being called.  */
10051
10052
0
  bfd_vma laddr;
10053
0
  int seq_len;  /* Original length of instruction sequence.  */
10054
0
  int insn16_on;  /* 16-bit on/off.  */
10055
0
  uint32_t insn;
10056
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10057
0
  bfd_signed_vma foff;
10058
0
  uint16_t insn16;
10059
0
  unsigned long reloc;
10060
10061
0
  irelend = internal_relocs + sec->reloc_count;
10062
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10063
0
  laddr = irel->r_offset;
10064
0
  *insn_len = seq_len;
10065
0
  insn16_on = IS_16BIT_ON (irel->r_addend);
10066
10067
0
  hi_irelfn =
10068
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10069
0
         R_NDS32_HI20_RELA, laddr);
10070
0
  lo_irelfn =
10071
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10072
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10073
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
10074
0
    {
10075
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10076
0
        (uint64_t) irel->r_offset);
10077
0
      return false;
10078
0
    }
10079
10080
  /* Get the value of the symbol referred to by the reloc.  */
10081
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10082
10083
0
  if (foff == 0
10084
0
      || foff >= CONSERVATIVE_24BIT_S1
10085
0
      || foff < -CONSERVATIVE_24BIT_S1)
10086
0
    return false;
10087
10088
0
  if (insn16_on
10089
0
      && foff >= -ACCURATE_8BIT_S1
10090
0
      && foff < ACCURATE_8BIT_S1
10091
0
      && (seq_len & 0x2))
10092
0
    {
10093
      /* j8 label */
10094
      /* 16-bit on, but not optimized for speed.  */
10095
0
      reloc = R_NDS32_9_PCREL_RELA;
10096
0
      insn16 = INSN_J8;
10097
0
      bfd_putb16 (insn16, contents + irel->r_offset);
10098
0
      *insn_len = 2;
10099
0
      irel->r_info =
10100
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10101
0
    }
10102
0
  else
10103
0
    {
10104
      /* j     label */
10105
0
      reloc = R_NDS32_25_PCREL_RELA;
10106
0
      insn = INSN_J;
10107
0
      bfd_putb32 (insn, contents + irel->r_offset);
10108
0
      *insn_len = 4;
10109
0
      irel->r_info =
10110
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10111
0
      irel->r_addend = 0;
10112
0
    }
10113
10114
0
  hi_irelfn->r_info =
10115
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10116
0
  lo_irelfn->r_info =
10117
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10118
10119
0
  if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10120
0
    {
10121
0
      insn16 = NDS32_NOP16;
10122
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10123
0
      lo_irelfn->r_info =
10124
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10125
0
          R_NDS32_INSN16);
10126
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10127
0
      *insn_len += 2;
10128
0
    }
10129
0
  return true;
10130
0
}
10131
10132
/* Revert condition branch.  This function does not check if the input
10133
   instruction is condition branch or not.  */
10134
10135
static void
10136
nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10137
         uint16_t *re_insn16, uint32_t *re_insn)
10138
0
{
10139
0
  uint32_t comp_insn = 0;
10140
0
  uint16_t comp_insn16 = 0;
10141
10142
0
  if (insn)
10143
0
    {
10144
0
      if (N32_OP6 (insn) == N32_OP6_BR1)
10145
0
  {
10146
    /* beqs label.  */
10147
0
    comp_insn = (insn ^ 0x4000) & 0xffffc000;
10148
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_R5)
10149
0
      {
10150
        /* Insn can be contracted to 16-bit implied r5.  */
10151
0
        comp_insn16 =
10152
0
    (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10153
0
        comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10154
0
      }
10155
0
  }
10156
0
      else if (N32_OP6 (insn) == N32_OP6_BR3)
10157
0
  {
10158
    /* bnec $ta, imm11, label.  */
10159
0
    comp_insn = (insn ^ 0x80000) & 0xffffff00;
10160
0
  }
10161
0
      else
10162
0
  {
10163
0
    comp_insn = (insn ^ 0x10000) & 0xffffc000;
10164
0
    if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10165
0
        || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10166
0
      {
10167
0
        if (N32_IS_RT3 (insn))
10168
0
    {
10169
      /* Insn can be contracted to 16-bit.  */
10170
0
      comp_insn16 =
10171
0
        (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10172
0
      comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10173
0
    }
10174
0
        else if (N32_RT5 (insn) == N32_REG_R15)
10175
0
    {
10176
      /* Insn can be contracted to 16-bit.  */
10177
0
      comp_insn16 =
10178
0
        (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10179
0
    }
10180
0
      }
10181
0
  }
10182
0
    }
10183
0
  else
10184
0
    {
10185
0
      switch ((insn16 & 0xf000) >> 12)
10186
0
  {
10187
0
  case 0xc:
10188
    /* beqz38 or bnez38 */
10189
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10190
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10191
0
    comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10192
0
    break;
10193
10194
0
  case 0xd:
10195
    /* beqs38 or bnes38 */
10196
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10197
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10198
0
    comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10199
0
      | (N32_REG_R5 << 15);
10200
0
    break;
10201
10202
0
  case 0xe:
10203
    /* beqzS8 or bnezS8 */
10204
0
    comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10205
0
    comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10206
0
    comp_insn |= N32_REG_R15 << 20;
10207
0
    break;
10208
10209
0
  default:
10210
0
    break;
10211
0
  }
10212
0
    }
10213
0
  if (comp_insn && re_insn)
10214
0
    *re_insn = comp_insn;
10215
0
  if (comp_insn16 && re_insn16)
10216
0
    *re_insn16 = comp_insn16;
10217
0
}
10218
10219
/* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
10220
10221
static bool
10222
nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10223
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10224
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10225
         Elf_Internal_Shdr *symtab_hdr)
10226
0
{
10227
  /* There are 3 variations for LONGJUMP2
10228
     case 2-4;  1st insn convertible, 16-bit on,
10229
     optimize off or optimize for space
10230
     bnes38  rt, ra, $1 ; LONGJUMP2
10231
     j       label      ; 25_PCREL
10232
     $1:
10233
10234
     case 4-4; 1st insn not convertible
10235
     bne  rt, ra, $1 ; LONGJUMP2
10236
     j    label      ; 25_PCREL
10237
     $1:
10238
10239
     case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10240
     bne  rt, ra, $1 ; LONGJUMP2
10241
     j    label      ; 25_PCREL
10242
     $1: */
10243
10244
  /* Get the reloc for the address from which the register is
10245
     being loaded.  This reloc will tell us which function is
10246
     actually being called.  */
10247
10248
0
  bfd_vma laddr;
10249
0
  int seq_len;  /* Original length of instruction sequence.  */
10250
0
  Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10251
0
  int first_size;
10252
0
  unsigned int i;
10253
0
  bfd_signed_vma foff;
10254
0
  uint32_t insn, re_insn = 0;
10255
0
  uint16_t insn16, re_insn16 = 0;
10256
0
  unsigned long reloc, cond_reloc;
10257
10258
0
  enum elf_nds32_reloc_type checked_types[] =
10259
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10260
10261
0
  irelend = internal_relocs + sec->reloc_count;
10262
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10263
0
  laddr = irel->r_offset;
10264
0
  *insn_len = seq_len;
10265
0
  first_size = (seq_len == 6) ? 2 : 4;
10266
10267
0
  i2_irelfn =
10268
0
    find_relocs_at_address_addr (irel, internal_relocs,
10269
0
         irelend, R_NDS32_25_PCREL_RELA,
10270
0
         laddr + first_size);
10271
10272
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10273
0
    {
10274
0
      cond_irelfn =
10275
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10276
0
             checked_types[i], laddr);
10277
0
      if (cond_irelfn != irelend)
10278
0
  break;
10279
0
    }
10280
10281
0
  if (i2_irelfn == irelend || cond_irelfn == irelend)
10282
0
    {
10283
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10284
0
        (uint64_t) irel->r_offset);
10285
0
      return false;
10286
0
    }
10287
10288
  /* Get the value of the symbol referred to by the reloc.  */
10289
0
  foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10290
0
  if (foff == 0
10291
0
      || foff < -CONSERVATIVE_16BIT_S1
10292
0
      || foff >= CONSERVATIVE_16BIT_S1)
10293
0
    return false;
10294
10295
  /* Get the all corresponding instructions.  */
10296
0
  if (first_size == 4)
10297
0
    {
10298
0
      insn = bfd_getb32 (contents + laddr);
10299
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10300
0
    }
10301
0
  else
10302
0
    {
10303
0
      insn16 = bfd_getb16 (contents + laddr);
10304
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10305
0
    }
10306
10307
0
  if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10308
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10309
0
    {
10310
0
      if (first_size == 4)
10311
0
  {
10312
    /* Don't convert it to 16-bit now, keep this as relaxable for
10313
       ``label reloc; INSN16''.  */
10314
10315
    /* Save comp_insn32 to buffer.  */
10316
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10317
0
    *insn_len = 4;
10318
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10319
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10320
0
    cond_reloc = R_NDS32_INSN16;
10321
0
  }
10322
0
      else
10323
0
  {
10324
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10325
0
    *insn_len = 2;
10326
0
    reloc = R_NDS32_9_PCREL_RELA;
10327
0
    cond_reloc = R_NDS32_NONE;
10328
0
  }
10329
0
    }
10330
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10331
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10332
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10333
0
    {
10334
      /* beqs     label    ; 15_PCREL */
10335
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10336
0
      *insn_len = 4;
10337
0
      reloc = R_NDS32_15_PCREL_RELA;
10338
0
      cond_reloc = R_NDS32_NONE;
10339
0
    }
10340
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10341
0
     && foff >= -CONSERVATIVE_16BIT_S1
10342
0
     && foff < CONSERVATIVE_16BIT_S1)
10343
0
    {
10344
      /* beqz     label ; 17_PCREL */
10345
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10346
0
      *insn_len = 4;
10347
0
      reloc = R_NDS32_17_PCREL_RELA;
10348
0
      cond_reloc = R_NDS32_NONE;
10349
0
    }
10350
0
  else
10351
0
    return false;
10352
10353
  /* Set all relocations.  */
10354
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10355
0
  irel->r_addend = i2_irelfn->r_addend;
10356
10357
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10358
0
              cond_reloc);
10359
0
  cond_irelfn->r_addend = 0;
10360
10361
0
  if ((seq_len ^ *insn_len ) & 0x2)
10362
0
    {
10363
0
      insn16 = NDS32_NOP16;
10364
0
      bfd_putb16 (insn16, contents + irel->r_offset + 4);
10365
0
      i2_irelfn->r_offset = 4;
10366
0
      i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10367
0
          R_NDS32_INSN16);
10368
0
      i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10369
0
      *insn_len += 2;
10370
0
    }
10371
0
  else
10372
0
    i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10373
0
              R_NDS32_NONE);
10374
0
  return true;
10375
0
}
10376
10377
/* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
10378
10379
static bool
10380
nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10381
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10382
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10383
         Elf_Internal_Shdr *symtab_hdr)
10384
0
{
10385
  /* There are 5 variations for LONGJUMP3
10386
     case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10387
     optimize off or optimize for space
10388
     bnes38   rt, ra, $1      ; LONGJUMP3
10389
     sethi    ta, hi20(symbol)      ; HI20
10390
     ori      ta, ta, lo12(symbol)  ; LO12S0
10391
     jr5      ta        ;
10392
     $1:          ;
10393
10394
     case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10395
     bnes38   rt, ra, $1     ; LONGJUMP3
10396
     sethi    ta, hi20(symbol)     ; HI20
10397
     ori      ta, ta, lo12(symbol) ; LO12S0
10398
     jr5      ta       ;
10399
     $1:         ; LABEL
10400
10401
     case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10402
     optimize off or optimize for space
10403
     bne   rt, ra, $1   ; LONGJUMP3
10404
     sethi ta, hi20(symbol) ; HI20
10405
     ori   ta, ta, lo12(symbol) ; LO12S0
10406
     jr5   ta     ;
10407
     $1:      ;
10408
10409
     case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10410
     16-bit off if no INSN16
10411
     bne   rt, ra, $1   ; LONGJUMP3
10412
     sethi ta, hi20(symbol) ; HI20
10413
     ori   ta, ta, lo12(symbol) ; LO12S0
10414
     jr    ta     ;
10415
     $1:      ;
10416
10417
     case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10418
     16-bit off if no INSN16
10419
     bne   rt, ra, $1   ; LONGJUMP3
10420
     sethi ta, hi20(symbol) ; HI20
10421
     ori   ta, ta, lo12(symbol) ; LO12S0
10422
     jr    ta     ;
10423
     $1:      ; LABEL */
10424
10425
  /* Get the reloc for the address from which the register is
10426
     being loaded.  This reloc will tell us which function is
10427
     actually being called.  */
10428
0
  enum elf_nds32_reloc_type checked_types[] =
10429
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10430
10431
0
  int reloc_off = 0, cond_removed = 0, convertible;
10432
0
  bfd_vma laddr;
10433
0
  int seq_len;  /* Original length of instruction sequence.  */
10434
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10435
0
  int first_size;
10436
0
  unsigned int i;
10437
0
  bfd_signed_vma foff;
10438
0
  uint32_t insn, re_insn = 0;
10439
0
  uint16_t insn16, re_insn16 = 0;
10440
0
  unsigned long reloc, cond_reloc;
10441
10442
0
  irelend = internal_relocs + sec->reloc_count;
10443
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10444
0
  laddr = irel->r_offset;
10445
0
  *insn_len = seq_len;
10446
10447
0
  convertible = IS_1ST_CONVERT (irel->r_addend);
10448
10449
0
  if (convertible)
10450
0
    first_size = 2;
10451
0
  else
10452
0
    first_size = 4;
10453
10454
  /* Get all needed relocations.  */
10455
0
  hi_irelfn =
10456
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10457
0
         R_NDS32_HI20_RELA, laddr + first_size);
10458
0
  lo_irelfn =
10459
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10460
0
         R_NDS32_LO12S0_ORI_RELA,
10461
0
         laddr + first_size + 4);
10462
10463
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10464
0
    {
10465
0
      cond_irelfn =
10466
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10467
0
             checked_types[i], laddr);
10468
0
      if (cond_irelfn != irelend)
10469
0
  break;
10470
0
    }
10471
10472
0
  if (hi_irelfn == irelend
10473
0
      || lo_irelfn == irelend
10474
0
      || cond_irelfn == irelend)
10475
0
    {
10476
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10477
0
        (uint64_t) irel->r_offset);
10478
0
      return false;
10479
0
    }
10480
10481
  /* Get the value of the symbol referred to by the reloc.  */
10482
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10483
10484
0
  if (foff == 0
10485
0
      || foff < -CONSERVATIVE_24BIT_S1
10486
0
      || foff >= CONSERVATIVE_24BIT_S1)
10487
0
    return false;
10488
10489
  /* Get the all corresponding instructions.  */
10490
0
  if (first_size == 4)
10491
0
    {
10492
0
      insn = bfd_getb32 (contents + laddr);
10493
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10494
0
    }
10495
0
  else
10496
0
    {
10497
0
      insn16 = bfd_getb16 (contents + laddr);
10498
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10499
0
    }
10500
10501
  /* For simplicity of coding, we are going to modify the section
10502
     contents, the section relocs, and the BFD symbol table.  We
10503
     must tell the rest of the code not to free up this
10504
     information.  It would be possible to instead create a table
10505
     of changes which have to be made, as is done in coff-mips.c;
10506
     that would be more work, but would require less memory when
10507
     the linker is run.  */
10508
10509
0
  if (re_insn16
10510
0
      && foff >= -ACCURATE_8BIT_S1 - first_size
10511
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10512
0
    {
10513
0
      if (!(seq_len & 0x2))
10514
0
  {
10515
    /* Don't convert it to 16-bit now, keep this as relaxable
10516
       for ``label reloc; INSN1a''6.  */
10517
    /* Save comp_insn32 to buffer.  */
10518
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10519
0
    *insn_len = 4;
10520
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10521
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10522
0
    cond_reloc = R_NDS32_INSN16;
10523
0
  }
10524
0
      else
10525
0
  {
10526
    /* Not optimize for speed; convert sequence to 16-bit.  */
10527
    /* Save comp_insn16 to buffer.  */
10528
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10529
0
    *insn_len = 2;
10530
0
    reloc = R_NDS32_9_PCREL_RELA;
10531
0
    cond_reloc = R_NDS32_NONE;
10532
0
  }
10533
0
      cond_removed = 1;
10534
0
    }
10535
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10536
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10537
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10538
0
    {
10539
      /* beqs     label    ; 15_PCREL */
10540
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10541
0
      *insn_len = 4;
10542
0
      reloc = R_NDS32_15_PCREL_RELA;
10543
0
      cond_reloc = R_NDS32_NONE;
10544
0
      cond_removed = 1;
10545
0
    }
10546
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10547
0
     && foff >= -CONSERVATIVE_16BIT_S1
10548
0
     && foff < CONSERVATIVE_16BIT_S1)
10549
0
    {
10550
      /* beqz     label ; 17_PCREL */
10551
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10552
0
      *insn_len = 4;
10553
0
      reloc = R_NDS32_17_PCREL_RELA;
10554
0
      cond_reloc = R_NDS32_NONE;
10555
0
      cond_removed = 1;
10556
0
    }
10557
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10558
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10559
0
    {
10560
      /* Relax to one of the following 3 variations
10561
10562
   case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10563
   for space
10564
   bnes38  rt, $1 ; LONGJUMP2
10565
   j       label  ; 25_PCREL
10566
   $1
10567
10568
   case 4-4; 1st insn not convertible, others don't care
10569
   bne   rt, ra, $1 ; LONGJUMP2
10570
   j     label      ; 25_PCREL
10571
   $1
10572
10573
   case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10574
   bne   rt, ra, $1 ; LONGJUMP2
10575
   j     label      ; 25_PCREL
10576
   $1 */
10577
10578
      /* Offset for first instruction.  */
10579
10580
      /* Use j label as second instruction.  */
10581
0
      *insn_len = 4 + first_size;
10582
0
      insn = INSN_J;
10583
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10584
0
      reloc = R_NDS32_LONGJUMP2;
10585
0
      cond_reloc = R_NDS32_25_PLTREL;
10586
0
    }
10587
0
    else
10588
0
      return false;
10589
10590
0
    if (cond_removed == 1)
10591
0
      {
10592
  /* Set all relocations.  */
10593
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10594
0
  irel->r_addend = hi_irelfn->r_addend;
10595
10596
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10597
0
              cond_reloc);
10598
0
  cond_irelfn->r_addend = 0;
10599
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10600
0
            R_NDS32_NONE);
10601
0
      }
10602
0
    else
10603
0
      {
10604
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10605
0
  irel->r_addend = irel->r_addend;
10606
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10607
0
            cond_reloc);
10608
0
      }
10609
10610
0
  if ((seq_len ^ *insn_len ) & 0x2)
10611
0
    {
10612
0
      insn16 = NDS32_NOP16;
10613
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10614
0
      lo_irelfn->r_offset = *insn_len;
10615
0
      lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10616
0
          R_NDS32_INSN16);
10617
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10618
0
      *insn_len += 2;
10619
0
    }
10620
0
  else
10621
0
    lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10622
0
              R_NDS32_NONE);
10623
0
  return true;
10624
0
}
10625
10626
/* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
10627
10628
static bool
10629
nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10630
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10631
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10632
         Elf_Internal_Shdr *symtab_hdr)
10633
0
{
10634
  /* The pattern for LONGCALL4.  Support for function cse.
10635
     sethi ta, hi20(symbol) ; LONGCALL4/HI20
10636
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10637
     jral  ta     ; PTR_RES/EMPTY/INSN16  */
10638
10639
0
  bfd_vma laddr;
10640
0
  uint32_t insn;
10641
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10642
0
  Elf_Internal_Rela *irelend;
10643
0
  bfd_signed_vma foff;
10644
10645
0
  irelend = internal_relocs + sec->reloc_count;
10646
0
  laddr = irel->r_offset;
10647
10648
  /* Get the reloc for the address from which the register is
10649
     being loaded.  This reloc will tell us which function is
10650
     actually being called.  */
10651
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10652
0
           R_NDS32_HI20_RELA, laddr);
10653
10654
0
  if (hi_irel == irelend)
10655
0
    {
10656
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10657
0
        (uint64_t) irel->r_offset);
10658
0
      return false;
10659
0
    }
10660
10661
  /* Get the value of the symbol referred to by the reloc.  */
10662
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10663
10664
  /* This condition only happened when symbol is undefined.  */
10665
0
  if (foff == 0
10666
0
      || foff < -CONSERVATIVE_24BIT_S1
10667
0
      || foff >= CONSERVATIVE_24BIT_S1)
10668
0
    return false;
10669
10670
  /* Relax to: jal symbol; 25_PCREL.  */
10671
  /* For simplicity of coding, we are going to modify the section
10672
     contents, the section relocs, and the BFD symbol table.  We
10673
     must tell the rest of the code not to free up this
10674
     information.  It would be possible to instead create a table
10675
     of changes which have to be made, as is done in coff-mips.c;
10676
     that would be more work, but would require less memory when
10677
     the linker is run.  */
10678
10679
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10680
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10681
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10682
0
            R_NDS32_EMPTY, irel->r_addend);
10683
10684
0
  if (ptr_irel == irelend || em_irel == irelend)
10685
0
    {
10686
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10687
0
        (uint64_t) irel->r_offset);
10688
0
      return false;
10689
0
    }
10690
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10691
0
  insn = bfd_getb32 (contents + irel->r_addend);
10692
0
  if (insn & 0x80000000)
10693
0
    return false;
10694
10695
  /* Replace the long call with a jal.  */
10696
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10697
0
          R_NDS32_25_PCREL_RELA);
10698
0
  ptr_irel->r_addend = 1;
10699
10700
  /* We don't resolve this here but resolve it in relocate_section.  */
10701
0
  insn = INSN_JAL;
10702
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10703
10704
0
  irel->r_info =
10705
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10706
10707
  /* If there is function cse, HI20 can not remove now.  */
10708
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10709
0
             R_NDS32_LONGCALL4, laddr);
10710
0
  if (call_irel == irelend)
10711
0
    {
10712
0
      *insn_len = 0;
10713
0
      hi_irel->r_info =
10714
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10715
0
    }
10716
10717
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10718
0
            R_NDS32_INSN16, irel->r_addend);
10719
0
  if (insn_irel != irelend)
10720
0
    insn_irel->r_info =
10721
0
      ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10722
10723
0
  return true;
10724
0
}
10725
10726
/* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
10727
10728
static bool
10729
nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10730
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10731
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10732
         Elf_Internal_Shdr *symtab_hdr)
10733
0
{
10734
  /* The pattern for LONGCALL5.
10735
     bltz  rt, .L1  ; LONGCALL5/17_PCREL
10736
     jal   symbol ; 25_PCREL
10737
     .L1:  */
10738
10739
0
  bfd_vma laddr;
10740
0
  uint32_t insn;
10741
0
  Elf_Internal_Rela *cond_irel, *irelend;
10742
0
  bfd_signed_vma foff;
10743
10744
0
  irelend = internal_relocs + sec->reloc_count;
10745
0
  laddr = irel->r_offset;
10746
0
  insn = bfd_getb32 (contents + laddr);
10747
10748
  /* Get the reloc for the address from which the register is
10749
     being loaded.  This reloc will tell us which function is
10750
     actually being called.  */
10751
0
  cond_irel =
10752
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10753
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
10754
0
  if (cond_irel == irelend)
10755
0
    {
10756
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10757
0
        (uint64_t) irel->r_offset);
10758
0
      return false;
10759
0
    }
10760
10761
  /* Get the value of the symbol referred to by the reloc.  */
10762
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10763
10764
0
  if (foff == 0
10765
0
      || foff < -CONSERVATIVE_16BIT_S1
10766
0
      || foff >= CONSERVATIVE_16BIT_S1)
10767
0
    return false;
10768
10769
  /* Relax to bgezal   rt, label ; 17_PCREL
10770
     or   bltzal   rt, label ; 17_PCREL.  */
10771
10772
  /* Convert to complimentary conditional call.  */
10773
0
  insn = CONVERT_CONDITION_CALL (insn);
10774
10775
  /* For simplicity of coding, we are going to modify the section
10776
     contents, the section relocs, and the BFD symbol table.  We
10777
     must tell the rest of the code not to free up this
10778
     information.  It would be possible to instead create a table
10779
     of changes which have to be made, as is done in coff-mips.c;
10780
     that would be more work, but would require less memory when
10781
     the linker is run.  */
10782
10783
  /* Modify relocation and contents.  */
10784
0
  cond_irel->r_info =
10785
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10786
10787
  /* Replace the long call with a bgezal.  */
10788
0
  bfd_putb32 (insn, contents + cond_irel->r_offset);
10789
0
  *insn_len = 0;
10790
10791
  /* Clean unnessary relocations.  */
10792
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10793
10794
0
  cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10795
0
             R_NDS32_17_PCREL_RELA, laddr);
10796
0
  cond_irel->r_info =
10797
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10798
10799
0
  return true;
10800
0
}
10801
10802
/* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
10803
10804
static bool
10805
nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10806
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10807
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10808
         Elf_Internal_Shdr *symtab_hdr)
10809
0
{
10810
  /* The pattern for LONGCALL6.
10811
     bltz  rt,   .L1      ; LONGCALL6/17_PCREL
10812
     sethi ta,   hi20(symbol)   ; HI20/PTR
10813
     ori   ta, ta,  lo12(symbol)  ; LO12S0_ORI/PTR
10814
     jral  ta       ; PTR_RES/EMPTY/INSN16
10815
     .L1  */
10816
10817
0
  bfd_vma laddr;
10818
0
  uint32_t insn;
10819
0
  Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10820
0
  bfd_signed_vma foff;
10821
10822
0
  irelend = internal_relocs + sec->reloc_count;
10823
0
  laddr = irel->r_offset;
10824
10825
  /* Get the reloc for the address from which the register is
10826
     being loaded.  This reloc will tell us which function is
10827
     actually being called.  */
10828
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10829
0
           R_NDS32_EMPTY, irel->r_addend);
10830
10831
0
  if (em_irel == irelend)
10832
0
    {
10833
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10834
0
        (uint64_t) irel->r_offset);
10835
0
      return false;
10836
0
    }
10837
10838
  /* Get the value of the symbol referred to by the reloc.  */
10839
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10840
10841
0
  if (foff == 0
10842
0
      || foff < -CONSERVATIVE_24BIT_S1
10843
0
      || foff >= CONSERVATIVE_24BIT_S1)
10844
0
    return false;
10845
10846
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10847
0
  insn = bfd_getb32 (contents + irel->r_addend);
10848
0
  if (insn & 0x80000000)
10849
0
    return false;
10850
10851
0
  insn = bfd_getb32 (contents + laddr);
10852
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10853
0
    {
10854
      /* Relax to  bgezal   rt, label ; 17_PCREL
10855
   or    bltzal   rt, label ; 17_PCREL.  */
10856
10857
      /* Convert to complimentary conditional call.  */
10858
0
      *insn_len = 0;
10859
0
      insn = CONVERT_CONDITION_CALL (insn);
10860
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10861
10862
0
      em_irel->r_info =
10863
0
  ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10864
10865
      /* Set resolved relocation.  */
10866
0
      cond_irel =
10867
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10868
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10869
0
      if (cond_irel == irelend)
10870
0
  {
10871
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10872
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10873
0
    return false;
10874
0
  }
10875
0
      cond_irel->r_addend = 1;
10876
10877
      /* Clear relocations.  */
10878
10879
0
      irel->r_info =
10880
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10881
10882
0
      cond_irel =
10883
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10884
0
             R_NDS32_17_PCREL_RELA, laddr);
10885
0
      if (cond_irel != irelend)
10886
0
  cond_irel->r_info =
10887
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10888
10889
0
      cond_irel =
10890
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10891
0
             R_NDS32_INSN16, irel->r_addend);
10892
0
      if (cond_irel != irelend)
10893
0
  cond_irel->r_info =
10894
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10895
10896
0
    }
10897
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10898
0
    {
10899
      /* Relax to the following instruction sequence
10900
   bltz  rt, .L1  ; LONGCALL2/17_PCREL
10901
   jal   symbol ; 25_PCREL/PTR_RES
10902
   .L1  */
10903
0
      *insn_len = 4;
10904
      /* Convert instruction.  */
10905
0
      insn = INSN_JAL;
10906
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10907
10908
      /* Convert relocations.  */
10909
0
      em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10910
0
              R_NDS32_25_PCREL_RELA);
10911
0
      irel->r_info =
10912
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10913
10914
      /* Set resolved relocation.  */
10915
0
      cond_irel =
10916
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10917
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10918
0
      if (cond_irel == irelend)
10919
0
  {
10920
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10921
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10922
0
    return false;
10923
0
  }
10924
0
      cond_irel->r_addend = 1;
10925
10926
0
      cond_irel =
10927
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10928
0
             R_NDS32_INSN16, irel->r_addend);
10929
0
      if (cond_irel != irelend)
10930
0
  cond_irel->r_info =
10931
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10932
0
    }
10933
0
  return true;
10934
0
}
10935
10936
/* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10937
10938
static bool
10939
nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10940
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10941
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10942
         Elf_Internal_Shdr *symtab_hdr)
10943
0
{
10944
  /* The pattern for LONGJUMP4.
10945
     sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10946
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10947
     jr    ta     ; PTR_RES/INSN16/EMPTY  */
10948
10949
0
  bfd_vma laddr;
10950
0
  int seq_len;  /* Original length of instruction sequence.  */
10951
0
  uint32_t insn;
10952
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10953
0
  bfd_signed_vma foff;
10954
10955
0
  irelend = internal_relocs + sec->reloc_count;
10956
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10957
0
  laddr = irel->r_offset;
10958
0
  *insn_len = seq_len;
10959
10960
  /* Get the reloc for the address from which the register is
10961
     being loaded.  This reloc will tell us which function is
10962
     actually being called.  */
10963
10964
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10965
0
           R_NDS32_HI20_RELA, laddr);
10966
10967
0
  if (hi_irel == irelend)
10968
0
    {
10969
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10970
0
        (uint64_t) irel->r_offset);
10971
0
      return false;
10972
0
    }
10973
10974
  /* Get the value of the symbol referred to by the reloc.  */
10975
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10976
10977
0
  if (foff == 0
10978
0
      || foff >= CONSERVATIVE_24BIT_S1
10979
0
      || foff < -CONSERVATIVE_24BIT_S1)
10980
0
    return false;
10981
10982
  /* Convert it to "j label", it may be converted to j8 in the final
10983
     pass of relaxation.  Therefore, we do not consider this currently.  */
10984
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10985
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10986
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10987
0
           R_NDS32_EMPTY, irel->r_addend);
10988
10989
0
  if (ptr_irel == irelend || em_irel == irelend)
10990
0
    {
10991
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10992
0
        (uint64_t) irel->r_offset);
10993
0
      return false;
10994
0
    }
10995
10996
0
  em_irel->r_info =
10997
0
    ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10998
0
  ptr_irel->r_addend = 1;
10999
11000
  /* Write instruction.  */
11001
0
  insn = INSN_J;
11002
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
11003
11004
  /* Clear relocations.  */
11005
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11006
11007
  /* If there is function cse, HI20 can not remove now.  */
11008
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11009
0
             R_NDS32_LONGJUMP4, laddr);
11010
0
  if (call_irel == irelend)
11011
0
    {
11012
0
      *insn_len = 0;
11013
0
      hi_irel->r_info =
11014
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11015
0
    }
11016
11017
0
  return true;
11018
0
}
11019
11020
/* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
11021
11022
static bool
11023
nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11024
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11025
         int *seq_len, bfd_byte *contents,
11026
         Elf_Internal_Sym *isymbuf,
11027
         Elf_Internal_Shdr *symtab_hdr)
11028
0
{
11029
  /* There are 2 variations for LONGJUMP5
11030
     case 2-4;  1st insn convertible, 16-bit on.
11031
     bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11032
     j       label    ; 25_PCREL/INSN16
11033
     $1:
11034
11035
     case 4-4; 1st insn not convertible
11036
     bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11037
     j    label   ; 25_PCREL/INSN16
11038
     .L1:  */
11039
11040
0
  bfd_vma laddr;
11041
0
  Elf_Internal_Rela *cond_irel,  *irelend;
11042
0
  unsigned int i;
11043
0
  bfd_signed_vma foff;
11044
0
  uint32_t insn, re_insn = 0;
11045
0
  uint16_t insn16, re_insn16 = 0;
11046
0
  unsigned long reloc;
11047
11048
0
  enum elf_nds32_reloc_type checked_types[] =
11049
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11050
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11051
11052
0
  irelend = internal_relocs + sec->reloc_count;
11053
0
  laddr = irel->r_offset;
11054
11055
  /* Get the reloc for the address from which the register is
11056
     being loaded.  This reloc will tell us which function is
11057
     actually being called.  */
11058
11059
0
  cond_irel =
11060
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11061
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
11062
0
  if (cond_irel == irelend)
11063
0
    {
11064
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11065
0
        (uint64_t) irel->r_offset);
11066
0
      return false;
11067
0
    }
11068
11069
  /* Get the value of the symbol referred to by the reloc.  */
11070
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11071
11072
0
  if (foff == 0
11073
0
      || foff < -CONSERVATIVE_16BIT_S1
11074
0
      || foff >= CONSERVATIVE_16BIT_S1)
11075
0
    return false;
11076
11077
  /* Get the all corresponding instructions.  */
11078
0
  insn = bfd_getb32 (contents + laddr);
11079
  /* Check instruction size.  */
11080
0
  if (insn & 0x80000000)
11081
0
    {
11082
0
      *seq_len = 0;
11083
0
      insn16 = insn >> 16;
11084
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11085
0
    }
11086
0
  else
11087
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11088
11089
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11090
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11091
0
    {
11092
      /* beqs label ; 15_PCREL.  */
11093
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11094
0
      reloc = R_NDS32_15_PCREL_RELA;
11095
0
    }
11096
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11097
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11098
0
    {
11099
      /* beqz label ; 17_PCREL.  */
11100
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11101
0
      reloc = R_NDS32_17_PCREL_RELA;
11102
0
    }
11103
0
  else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11104
0
     && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11105
0
    {
11106
      /* beqc label ; 9_PCREL.  */
11107
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11108
0
      reloc = R_NDS32_WORD_9_PCREL_RELA;
11109
0
    }
11110
0
  else
11111
0
    return false;
11112
11113
  /* Set all relocations.  */
11114
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11115
11116
  /* Clean relocations.  */
11117
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11118
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11119
0
    {
11120
0
      cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11121
0
                 checked_types[i], laddr);
11122
0
      if (cond_irel != irelend)
11123
0
  {
11124
0
    if (*seq_len == 0
11125
0
        && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11126
0
      {
11127
        /* If the branch instruction is 2 byte, it cannot remove
11128
     directly.  Only convert it to nop16 and remove it after
11129
     checking alignment issue.  */
11130
0
        insn16 = NDS32_NOP16;
11131
0
        bfd_putb16 (insn16, contents + laddr);
11132
0
        cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11133
0
      }
11134
0
    else
11135
0
      cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11136
0
                R_NDS32_NONE);
11137
0
  }
11138
0
    }
11139
0
  *insn_len = 0;
11140
11141
0
  return true;
11142
0
}
11143
11144
/* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
11145
11146
static bool
11147
nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11148
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11149
         int *seq_len, bfd_byte *contents,
11150
         Elf_Internal_Sym *isymbuf,
11151
         Elf_Internal_Shdr *symtab_hdr)
11152
0
{
11153
  /* There are 5 variations for LONGJUMP6
11154
     case : 2-4-4-4; 1st insn convertible, 16-bit on.
11155
     bnes38   rt, ra, .L1   ; LONGJUMP6/15_PCREL/INSN16
11156
     sethi    ta, hi20(symbol)    ; HI20/PTR
11157
     ori      ta, ta, lo12(symbol)  ; LO12S0_ORI/PTR
11158
     jr       ta      ; PTR_RES/INSN16/EMPTY
11159
     .L1:
11160
11161
     case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11162
     bne   rt, ra, .L1    ; LONGJUMP6/15_PCREL/INSN16
11163
     sethi ta, hi20(symbol) ; HI20/PTR
11164
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11165
     jr    ta     ; PTR_RES/INSN16/EMPTY
11166
     .L1:  */
11167
11168
0
  enum elf_nds32_reloc_type checked_types[] =
11169
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11170
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11171
11172
0
  int reloc_off = 0, cond_removed = 0;
11173
0
  bfd_vma laddr;
11174
0
  Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11175
0
  unsigned int i;
11176
0
  bfd_signed_vma foff;
11177
0
  uint32_t insn, re_insn = 0;
11178
0
  uint16_t insn16, re_insn16 = 0;
11179
0
  unsigned long reloc;
11180
11181
0
  irelend = internal_relocs + sec->reloc_count;
11182
0
  laddr = irel->r_offset;
11183
11184
  /* Get the reloc for the address from which the register is
11185
     being loaded.  This reloc will tell us which function is
11186
     actually being called.  */
11187
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11188
0
           R_NDS32_EMPTY, irel->r_addend);
11189
11190
0
  if (em_irel == irelend)
11191
0
    {
11192
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11193
0
        (uint64_t) irel->r_offset);
11194
0
      return false;
11195
0
    }
11196
11197
  /* Get the value of the symbol referred to by the reloc.  */
11198
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11199
11200
0
  if (foff == 0
11201
0
      || foff < -CONSERVATIVE_24BIT_S1
11202
0
      || foff >= CONSERVATIVE_24BIT_S1)
11203
0
    return false;
11204
11205
0
  insn = bfd_getb32 (contents + laddr);
11206
  /* Check instruction size.  */
11207
0
  if (insn & 0x80000000)
11208
0
    {
11209
0
      *seq_len = 0;
11210
0
      insn16 = insn >> 16;
11211
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11212
0
    }
11213
0
  else
11214
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11215
11216
  /* For simplicity of coding, we are going to modify the section
11217
     contents, the section relocs, and the BFD symbol table.  We
11218
     must tell the rest of the code not to free up this
11219
     information.  It would be possible to instead create a table
11220
     of changes which have to be made, as is done in coff-mips.c;
11221
     that would be more work, but would require less memory when
11222
     the linker is run.  */
11223
11224
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11225
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11226
0
    {
11227
      /* beqs     label    ; 15_PCREL.  */
11228
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11229
0
      reloc = R_NDS32_15_PCREL_RELA;
11230
0
      cond_removed = 1;
11231
0
    }
11232
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11233
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11234
0
    {
11235
      /* beqz     label ; 17_PCREL.  */
11236
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11237
0
      reloc = R_NDS32_17_PCREL_RELA;
11238
0
      cond_removed = 1;
11239
0
    }
11240
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11241
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11242
0
    {
11243
      /* Relax to one of the following 2 variations
11244
11245
   case 2-4;  1st insn convertible, 16-bit on.
11246
   bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11247
   j       label    ; 25_PCREL/INSN16
11248
   $1:
11249
11250
   case 4-4; 1st insn not convertible
11251
   bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11252
   j    label   ; 25_PCREL/INSN16
11253
   .L1:  */
11254
11255
      /* Use j label as second instruction.  */
11256
0
      insn = INSN_J;
11257
0
      reloc = R_NDS32_25_PCREL_RELA;
11258
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
11259
0
    }
11260
0
  else
11261
0
    return false;
11262
11263
  /* Set all relocations.  */
11264
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11265
11266
0
  cond_irel =
11267
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11268
0
         R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11269
0
  cond_irel->r_addend = 1;
11270
11271
  /* Use INSN16 of first branch instruction to distinguish if keeping
11272
     INSN16 of final instruction or not.  */
11273
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11274
0
             R_NDS32_INSN16, irel->r_offset);
11275
0
  if (insn_irel == irelend)
11276
0
    {
11277
      /* Clean the final INSN16.  */
11278
0
      insn_irel =
11279
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
11280
0
             R_NDS32_INSN16, em_irel->r_offset);
11281
0
      insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11282
0
          R_NDS32_NONE);
11283
0
    }
11284
11285
0
  if (cond_removed == 1)
11286
0
    {
11287
0
      *insn_len = 0;
11288
11289
      /* Clear relocations.  */
11290
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11291
11292
0
      for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11293
0
  {
11294
0
    cond_irel =
11295
0
      find_relocs_at_address_addr (irel, internal_relocs, irelend,
11296
0
           checked_types[i], laddr);
11297
0
    if (cond_irel != irelend)
11298
0
      {
11299
0
        if (*seq_len == 0
11300
0
      && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11301
0
    {
11302
      /* If the branch instruction is 2 byte, it cannot remove
11303
         directly.  Only convert it to nop16 and remove it after
11304
         checking alignment issue.  */
11305
0
      insn16 = NDS32_NOP16;
11306
0
      bfd_putb16 (insn16, contents + laddr);
11307
0
      cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11308
0
    }
11309
0
        else
11310
0
    cond_irel->r_info =
11311
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11312
0
      }
11313
0
  }
11314
0
    }
11315
0
  else
11316
0
    {
11317
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11318
0
           R_NDS32_LONGJUMP5);
11319
0
    }
11320
11321
0
  return true;
11322
0
}
11323
11324
/* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
11325
11326
static bool
11327
nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11328
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11329
         int *seq_len, bfd_byte *contents,
11330
         Elf_Internal_Sym *isymbuf,
11331
         Elf_Internal_Shdr *symtab_hdr)
11332
0
{
11333
  /* There are 2 variations for LONGJUMP5
11334
     case 2-4;  1st insn convertible, 16-bit on.
11335
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11336
     beq     rt, ta, label  ; 15_PCREL
11337
11338
     case 4-4; 1st insn not convertible
11339
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11340
     beq     rt, ta, label  ; 15_PCREL  */
11341
11342
0
  bfd_vma laddr;
11343
0
  Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
11344
0
  bfd_signed_vma foff;
11345
0
  uint32_t insn, re_insn = 0;
11346
0
  uint16_t insn16;
11347
0
  uint32_t imm11;
11348
11349
0
  irelend = internal_relocs + sec->reloc_count;
11350
0
  laddr = irel->r_offset;
11351
11352
  /* Get the reloc for the address from which the register is
11353
     being loaded.  This reloc will tell us which function is
11354
     actually being called.  */
11355
11356
0
  cond_irel =
11357
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11358
0
         R_NDS32_15_PCREL_RELA, irel->r_addend);
11359
0
  if (cond_irel == irelend)
11360
0
    {
11361
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11362
0
        (uint64_t) irel->r_offset);
11363
0
      return false;
11364
0
    }
11365
11366
  /* Get the value of the symbol referred to by the reloc.  */
11367
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11368
11369
0
  if (foff == 0
11370
0
      || foff < -CONSERVATIVE_8BIT_S1
11371
0
      || foff >= CONSERVATIVE_8BIT_S1)
11372
0
    return false;
11373
11374
  /* Get the first instruction for its size.  */
11375
0
  insn = bfd_getb32 (contents + laddr);
11376
0
  if (insn & 0x80000000)
11377
0
    {
11378
0
      *seq_len = 0;
11379
      /* Get the immediate from movi55.  */
11380
0
      imm11 = N16_IMM5S (insn >> 16);
11381
0
    }
11382
0
  else
11383
0
    {
11384
      /* Get the immediate from movi.  */
11385
0
      imm11 = N32_IMM20S (insn);
11386
0
    }
11387
11388
  /* Get the branch instruction.  */
11389
0
  insn = bfd_getb32 (contents + irel->r_addend);
11390
  /* Convert instruction to BR3.  */
11391
0
  if ((insn >> 14) & 0x1)
11392
0
    re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11393
0
  else
11394
0
    re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11395
11396
0
  bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11397
11398
  /* Set all relocations.  */
11399
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11400
0
            R_NDS32_WORD_9_PCREL_RELA);
11401
11402
  /* Clean relocations.  */
11403
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11404
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11405
0
             R_NDS32_INSN16, irel->r_offset);
11406
0
  if (insn_irel != irelend)
11407
0
    {
11408
0
      if (*seq_len == 0)
11409
0
  {
11410
    /* If the first insntruction is 16bit, convert it to nop16.  */
11411
0
    insn16 = NDS32_NOP16;
11412
0
    bfd_putb16 (insn16, contents + laddr);
11413
0
    insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11414
0
  }
11415
0
      else
11416
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11417
0
            R_NDS32_NONE);
11418
0
    }
11419
0
  *insn_len = 0;
11420
11421
0
  return true;
11422
0
}
11423
11424
/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11425
   for each relax round. But the gp may changed dramatically and then cause
11426
   the truncated to fit errors for the the converted gp instructions.
11427
   Therefore, we must reserve the minimum but safe enough size to prevent it.  */
11428
11429
static bool
11430
nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11431
           Elf_Internal_Rela *irel, bool *again,
11432
           bool init,
11433
           struct elf_nds32_link_hash_table *table,
11434
           Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11435
11436
0
{
11437
0
  int offset_to_gp;
11438
0
  static bool sec_pass = false;
11439
0
  static asection *first_sec = NULL, *sym_sec;
11440
  /* Record the number of instructions which may be removed.  */
11441
0
  static int count = 0, record_count;
11442
0
  Elf_Internal_Sym *isym;
11443
0
  struct elf_link_hash_entry *h = NULL;
11444
0
  int indx;
11445
0
  unsigned long r_symndx;
11446
0
  bfd *abfd = sec->owner;
11447
0
  static bfd_vma record_sda = 0;
11448
0
  int sda_offset = 0;
11449
11450
  /* Force doing relaxation when hyper-relax is high.  */
11451
0
  if (table->hyper_relax == 2)
11452
0
    return true;
11453
11454
  /* Do not relax the load/store patterns for the first
11455
     relax round.  */
11456
0
  if (init)
11457
0
    {
11458
0
      if (!first_sec)
11459
0
  first_sec = sec;
11460
0
      else if (first_sec == sec)
11461
0
  {
11462
0
    record_count = count;
11463
0
    count = 0;
11464
0
    sec_pass = true;
11465
0
  }
11466
11467
0
      if (!sec_pass)
11468
0
  *again = true;
11469
11470
0
      return true;
11471
0
    }
11472
11473
  /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11474
     DATA_SEGMENT_ALIGN size in the linker script may make it
11475
     get even bigger.  */
11476
0
  if (record_sda == 0)
11477
0
    record_sda = local_sda;
11478
0
  else if (local_sda > record_sda)
11479
0
    sda_offset = local_sda - record_sda;
11480
11481
  /* Assume the instruction will be removed in the best case.  */
11482
0
  count++;
11483
11484
  /* We record the offset to gp for each symbol, and then check
11485
     if it is changed dramatically after relaxing.
11486
     (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11487
     (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx].  */
11488
0
  r_symndx = ELF32_R_SYM (irel->r_info);
11489
0
  if (r_symndx >= symtab_hdr->sh_info)
11490
0
    {
11491
      /* Global symbols.  */
11492
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11493
0
      h = elf_sym_hashes (abfd)[indx];
11494
0
      sym_sec = h->root.u.def.section;
11495
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags)
11496
0
    || bfd_is_abs_section (sym_sec))
11497
0
  {
11498
    /* Forbid doing relaxation when hyper-relax is low.  */
11499
0
    if (table->hyper_relax == 0)
11500
0
      return false;
11501
11502
0
    offset_to_gp = *access_addr - local_sda;
11503
0
    if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11504
0
      elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11505
0
    else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11506
0
       < abs (offset_to_gp) - sda_offset)
11507
0
      {
11508
        /* This may cause the error, so we reserve the
11509
     safe enough size for relaxing.  */
11510
0
        if (*access_addr >= local_sda)
11511
0
    *access_addr += (record_count * 4);
11512
0
        else
11513
0
    *access_addr -= (record_count * 4);
11514
0
      }
11515
0
    return sec_pass;
11516
0
  }
11517
0
    }
11518
0
  else
11519
0
    {
11520
      /* Local symbols.  */
11521
0
      if (!elf32_nds32_allocate_local_sym_info (abfd))
11522
0
  return false;
11523
0
      isym = isymbuf + r_symndx;
11524
11525
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11526
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags))
11527
0
  {
11528
    /* Forbid doing relaxation when hyper-relax is low.  */
11529
0
    if (table->hyper_relax == 0)
11530
0
      return false;
11531
11532
0
    offset_to_gp = *access_addr - local_sda;
11533
0
    if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11534
0
      elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11535
0
    else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11536
0
       < abs (offset_to_gp) - sda_offset)
11537
0
      {
11538
        /* This may cause the error, so we reserve the
11539
     safe enough size for relaxing.  */
11540
0
        if (*access_addr >= local_sda)
11541
0
    *access_addr += (record_count * 4);
11542
0
        else
11543
0
    *access_addr -= (record_count * 4);
11544
0
      }
11545
0
    return sec_pass;
11546
0
  }
11547
0
    }
11548
11549
0
  return true;
11550
0
}
11551
11552
0
#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11553
11554
/* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
11555
11556
static bool
11557
nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11558
         asection *sec, Elf_Internal_Rela *irel,
11559
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11560
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11561
         Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11562
         struct elf_nds32_link_hash_table *table)
11563
0
{
11564
0
  int eliminate_sethi = 0, range_type;
11565
0
  unsigned int i;
11566
0
  bfd_vma local_sda, laddr;
11567
0
  int seq_len;  /* Original length of instruction sequence.  */
11568
0
  uint32_t insn;
11569
0
  Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11570
0
  bfd_vma access_addr = 0;
11571
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11572
0
  struct elf_link_hash_entry *h = NULL;
11573
0
  int indx;
11574
0
  enum elf_nds32_reloc_type checked_types[] =
11575
0
    { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11576
0
      R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11577
0
      R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11578
0
      R_NDS32_TLS_LE_HI20
11579
0
    };
11580
11581
0
  irelend = internal_relocs + sec->reloc_count;
11582
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
11583
0
  laddr = irel->r_offset;
11584
0
  *insn_len = seq_len;
11585
11586
  /* Get the high part relocation.  */
11587
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11588
0
    {
11589
0
      hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11590
0
                 checked_types[i], laddr);
11591
0
      if (hi_irelfn != irelend)
11592
0
  break;
11593
0
    }
11594
11595
0
  if (hi_irelfn == irelend)
11596
0
    {
11597
      /* Not R_NDS32_HI20_RELA.  */
11598
0
      if (i != 0)
11599
0
  _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11600
0
          (uint64_t) irel->r_offset);
11601
0
      return false;
11602
0
    }
11603
11604
0
  range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11605
0
  nds32_elf_final_sda_base (sec->output_section->owner,
11606
0
          link_info, &local_sda, false);
11607
11608
0
  switch (ELF32_R_TYPE (hi_irelfn->r_info))
11609
0
    {
11610
0
    case R_NDS32_HI20_RELA:
11611
0
      insn = bfd_getb32 (contents + laddr);
11612
0
      access_addr =
11613
0
  calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11614
11615
0
      if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11616
0
  {
11617
0
    indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11618
0
    h = elf_sym_hashes (abfd)[indx];
11619
0
  }
11620
11621
      /* Try movi.  */
11622
0
      if (range_type == NDS32_LOADSTORE_IMM
11623
0
    && access_addr < CONSERVATIVE_20BIT
11624
0
    && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11625
0
  {
11626
0
    eliminate_sethi = 1;
11627
0
    break;
11628
0
  }
11629
11630
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11631
0
  {
11632
0
    eliminate_sethi = 1;
11633
0
    break;
11634
0
  }
11635
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11636
0
               NULL, false, table, isymbuf, symtab_hdr))
11637
0
  return false;
11638
11639
0
      if (!load_store_relax)
11640
0
  return false;
11641
11642
      /* Case for set gp register.  */
11643
0
      if (N32_RT5 (insn) == N32_REG_GP)
11644
0
  return false;
11645
11646
0
      if (range_type == NDS32_LOADSTORE_FLOAT_S
11647
0
    || range_type == NDS32_LOADSTORE_FLOAT_D)
11648
0
  {
11649
0
    range_l = sdata_range[0][0];
11650
0
    range_h = sdata_range[0][1];
11651
0
  }
11652
0
      else
11653
0
  {
11654
0
    range_l = sdata_range[1][0];
11655
0
    range_h = sdata_range[1][1];
11656
0
  }
11657
0
      break;
11658
11659
0
    default:
11660
0
      return false;
11661
0
    }
11662
11663
  /* Delete sethi instruction.  */
11664
0
  if (eliminate_sethi == 1
11665
0
      || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11666
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11667
0
    {
11668
0
      hi_irelfn->r_info =
11669
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11670
0
      irel->r_info =
11671
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11672
0
      *insn_len = 0;
11673
0
      return true;
11674
0
    }
11675
11676
0
  return false;
11677
0
}
11678
11679
/* Relax LO12 relocation for nds32_elf_relax_section.  */
11680
11681
static void
11682
nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11683
          asection *sec, Elf_Internal_Rela *irel,
11684
          Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11685
          Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11686
          struct elf_nds32_link_hash_table *table)
11687
0
{
11688
0
  uint32_t insn;
11689
0
  bfd_vma local_sda, laddr;
11690
0
  unsigned long reloc;
11691
0
  bfd_vma access_addr;
11692
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11693
0
  Elf_Internal_Rela *irelfn = NULL, *irelend;
11694
0
  struct elf_link_hash_entry *h = NULL;
11695
0
  int indx;
11696
11697
  /* For SDA base relative relaxation.  */
11698
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11699
0
          &local_sda, false);
11700
11701
0
  irelend = internal_relocs + sec->reloc_count;
11702
0
  laddr = irel->r_offset;
11703
0
  insn = bfd_getb32 (contents + laddr);
11704
11705
0
  if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11706
0
    return;
11707
11708
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11709
11710
0
  if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11711
0
    {
11712
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11713
0
      h = elf_sym_hashes (abfd)[indx];
11714
0
    }
11715
11716
  /* Try movi.  */
11717
0
  if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11718
0
      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11719
0
    {
11720
0
      reloc = R_NDS32_20_RELA;
11721
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11722
0
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11723
0
      bfd_putb32 (insn, contents + laddr);
11724
0
    }
11725
0
  else
11726
0
    {
11727
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11728
0
  {
11729
    /* Fall through.  */
11730
0
  }
11731
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11732
0
               false, table, isymbuf, symtab_hdr))
11733
0
  return;
11734
11735
0
      range_l = sdata_range[1][0];
11736
0
      range_h = sdata_range[1][1];
11737
0
      switch (ELF32_R_TYPE (irel->r_info))
11738
0
  {
11739
0
  case R_NDS32_LO12S0_RELA:
11740
0
    reloc = R_NDS32_SDA19S0_RELA;
11741
0
    break;
11742
0
  case R_NDS32_LO12S1_RELA:
11743
0
    reloc = R_NDS32_SDA18S1_RELA;
11744
0
    break;
11745
0
  case R_NDS32_LO12S2_RELA:
11746
0
    reloc = R_NDS32_SDA17S2_RELA;
11747
0
    break;
11748
0
  case R_NDS32_LO12S2_DP_RELA:
11749
0
    range_l = sdata_range[0][0];
11750
0
    range_h = sdata_range[0][1];
11751
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11752
0
    break;
11753
0
  case R_NDS32_LO12S2_SP_RELA:
11754
0
    range_l = sdata_range[0][0];
11755
0
    range_h = sdata_range[0][1];
11756
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11757
0
    break;
11758
0
  default:
11759
0
    return;
11760
0
  }
11761
11762
      /* There are range_h and range_l because linker has to promise
11763
   all sections move cross one page together.  */
11764
0
      if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11765
0
    || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11766
0
    || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11767
0
  {
11768
0
    if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == N32_REG_GP)
11769
0
      {
11770
        /* Maybe we should add R_NDS32_INSN16 reloc type here
11771
     or manually do some optimization.  sethi can't be
11772
     eliminated when updating $gp so the relative ori
11773
     needs to be preserved.  */
11774
0
        return;
11775
0
      }
11776
0
    if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11777
0
          &insn))
11778
0
      return;
11779
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11780
0
    bfd_putb32 (insn, contents + laddr);
11781
11782
0
    irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11783
0
             R_NDS32_INSN16);
11784
    /* SDA17 must keep INSN16 for converting fp_as_gp.  */
11785
0
    if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11786
0
      irelfn->r_info =
11787
0
        ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11788
11789
0
  }
11790
0
    }
11791
0
  return;
11792
0
}
11793
11794
/* Relax PTR relocation for nds32_elf_relax_section.  */
11795
11796
static bool
11797
nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11798
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11799
         int *seq_len, bfd_byte *contents)
11800
0
{
11801
0
  Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11802
11803
0
  irelend = internal_relocs + sec->reloc_count;
11804
11805
0
  re_irel =
11806
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11807
0
         R_NDS32_PTR_RESOLVED, irel->r_addend);
11808
11809
0
  if (re_irel == irelend)
11810
0
    {
11811
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11812
0
        (uint64_t) irel->r_offset);
11813
0
      return false;
11814
0
    }
11815
11816
0
  if (re_irel->r_addend != 1)
11817
0
    return false;
11818
11819
  /* Pointed target is relaxed and no longer needs this void *,
11820
     change the type to NONE.  */
11821
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11822
11823
  /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11824
     not exist, it means only count 1 and remove it directly.  */
11825
  /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11826
0
  count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11827
0
               R_NDS32_PTR_COUNT);
11828
0
  ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11829
0
             R_NDS32_PTR);
11830
0
  if (count_irel != irelend)
11831
0
    {
11832
0
      if (--count_irel->r_addend > 0)
11833
0
  return false;
11834
0
    }
11835
11836
0
  if (ptr_irel != irelend)
11837
0
    return false;
11838
11839
  /* If the PTR_COUNT is already 0, remove current instruction.  */
11840
0
  *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11841
0
  *insn_len = 0;
11842
0
  return true;
11843
0
}
11844
11845
/* Relax LWC relocation for nds32_elf_relax_section.  */
11846
11847
static void
11848
nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11849
          asection *sec, Elf_Internal_Rela *irel,
11850
          Elf_Internal_Rela *internal_relocs,
11851
          bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11852
          Elf_Internal_Shdr *symtab_hdr, bool *again)
11853
0
{
11854
  /* Pattern:
11855
     sethi    ra, hi20(symbol)      ; HI20/LOADSTORE
11856
     ori      ra, ra, lo12(symbol)  ; LO12S0/PTR/PTR/.../INSN16
11857
     flsi     fsa, [ra + offset1]   ; LSI/PTR_RESOLVED/INSN16
11858
     flsi     fsb, [ra + offset2]   ; LSI/PTR_RESOLVED/INSN16
11859
     ...  */
11860
11861
0
  uint32_t insn;
11862
0
  bfd_vma local_sda, laddr;
11863
0
  unsigned long reloc;
11864
0
  bfd_vma access_addr, flsi_offset;
11865
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11866
0
  Elf_Internal_Rela *irelend, *re_irel;
11867
0
  unsigned int opcode;
11868
11869
0
  irelend = internal_relocs + sec->reloc_count;
11870
0
  laddr = irel->r_offset;
11871
0
  insn = bfd_getb32 (contents + laddr);
11872
11873
0
  if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11874
0
    return;
11875
11876
  /* Can not do relaxation for bi format.  */
11877
0
  if ((insn & 0x1000))
11878
0
    return;
11879
11880
  /* Only deal with flsi, fssi, fldi, fsdi, so far.  */
11881
0
  opcode = N32_OP6 (insn);
11882
0
  if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11883
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11884
0
  else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11885
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11886
0
  else
11887
0
    return;
11888
11889
0
  re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11890
0
            R_NDS32_PTR_RESOLVED);
11891
0
  if (re_irel == irelend)
11892
0
    {
11893
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11894
0
        (uint64_t) irel->r_offset);
11895
0
      return;
11896
0
    }
11897
11898
  /* For SDA base relative relaxation.  */
11899
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11900
0
          &local_sda, false);
11901
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11902
0
  flsi_offset = (insn & 0xfff) << 2;
11903
0
  access_addr += flsi_offset;
11904
0
  range_l = sdata_range[0][0];
11905
0
  range_h = sdata_range[0][1];
11906
11907
0
  if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11908
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11909
0
    {
11910
      /* Turn flsi instruction into sda access format.  */
11911
0
      insn = (insn & 0x7ff07000) | (N32_REG_GP << 15);
11912
11913
      /* Add relocation type to flsi.  */
11914
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11915
0
      irel->r_addend += flsi_offset;
11916
0
      bfd_putb32 (insn, contents + re_irel->r_offset);
11917
11918
0
      re_irel->r_addend |= 1;
11919
0
      *again = true;
11920
0
    }
11921
0
}
11922
11923
static bool
11924
nds32_relax_adjust_label (bfd *abfd, asection *sec,
11925
        Elf_Internal_Rela *internal_relocs,
11926
        bfd_byte *contents,
11927
        nds32_elf_blank_t **relax_blank_list,
11928
        int optimize, int opt_size)
11929
0
{
11930
  /* This code block is used to adjust 4-byte alignment by relax a pair
11931
     of instruction a time.
11932
11933
     It recognizes three types of relocations.
11934
     1. R_NDS32_LABEL - a alignment.
11935
     2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11936
     3. is_16bit_NOP () - remove a 16-bit instruction.  */
11937
11938
  /* TODO: It seems currently implementation only support 4-byte alignment.
11939
     We should handle any-alignment.  */
11940
11941
0
  Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11942
0
  Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11943
0
  Elf_Internal_Rela rel_temp;
11944
0
  Elf_Internal_Rela *irelend;
11945
0
  bfd_vma address;
11946
0
  uint16_t insn16;
11947
11948
  /* Checking for branch relaxation relies on the relocations to
11949
     be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11950
0
  nds32_insertion_sort (internal_relocs, sec->reloc_count,
11951
0
      sizeof (Elf_Internal_Rela), compar_reloc);
11952
11953
0
  irelend = internal_relocs + sec->reloc_count;
11954
11955
  /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11956
  /* FIXME: Can we generate the right order in assembler?
11957
     So we don't have to swapping them here.  */
11958
11959
0
  for (label_rel = internal_relocs, insn_rel = internal_relocs;
11960
0
       label_rel < irelend; label_rel++)
11961
0
    {
11962
0
      if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11963
0
  continue;
11964
11965
      /* Find the first reloc has the same offset with label_rel.  */
11966
0
      while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11967
0
  insn_rel++;
11968
11969
0
      for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11970
0
     insn_rel++)
11971
  /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11972
     address.  */
11973
0
  if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11974
0
    break;
11975
11976
0
      if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11977
0
    && insn_rel < label_rel)
11978
0
  {
11979
    /* Swap the two reloc if the R_NDS32_INSN16 is
11980
       before R_NDS32_LABEL.  */
11981
0
    memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11982
0
    memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11983
0
    memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11984
0
  }
11985
0
    }
11986
11987
0
  label_rel = NULL;
11988
0
  insn_rel = NULL;
11989
  /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11990
     or higher, remove other R_NDS32_LABEL with lower alignment.
11991
     If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11992
     then the R_NDS32_LABEL sequence is broke.  */
11993
0
  for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11994
0
    {
11995
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11996
0
  {
11997
0
    if (label_rel == NULL)
11998
0
      {
11999
0
        if (tmp_rel->r_addend < 2)
12000
0
    label_rel = tmp_rel;
12001
0
        continue;
12002
0
      }
12003
0
    else if (tmp_rel->r_addend > 1)
12004
0
      {
12005
        /* Remove all LABEL relocation from label_rel to tmp_rel
12006
     including relocations with same offset as tmp_rel.  */
12007
0
        for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
12008
0
    {
12009
0
      if (tmp2_rel->r_offset == tmp_rel->r_offset)
12010
0
        break;
12011
12012
0
      if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12013
0
          && tmp2_rel->r_addend < 2)
12014
0
        tmp2_rel->r_info =
12015
0
          ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12016
0
            R_NDS32_NONE);
12017
0
    }
12018
0
        label_rel = NULL;
12019
0
      }
12020
0
  }
12021
0
      else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12022
0
  {
12023
    /* A new INSN16 which can be converted, so clear label_rel.  */
12024
0
    if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12025
0
           irelend, &insn16)
12026
0
        || is_16bit_NOP (abfd, sec, tmp_rel))
12027
0
      label_rel = NULL;
12028
0
  }
12029
0
    }
12030
12031
0
  label_rel = NULL;
12032
0
  insn_rel = NULL;
12033
  /* Optimized for speed and nothing has not been relaxed.
12034
     It's time to align labels.
12035
     We may convert a 16-bit instruction right before a label to
12036
     32-bit, in order to align the label if necessary
12037
     all reloc entries has been sorted by r_offset.  */
12038
0
  for (irel = internal_relocs;
12039
0
       irel < irelend && irel->r_offset < sec->size; irel++)
12040
0
    {
12041
0
      if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12042
0
    && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12043
0
  continue;
12044
12045
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12046
0
  {
12047
    /* A new INSN16 found, resize the old one.  */
12048
0
    if (is_convert_32_to_16
12049
0
        (abfd, sec, irel, internal_relocs, irelend, &insn16)
12050
0
        || is_16bit_NOP (abfd, sec, irel))
12051
0
      {
12052
0
        if (insn_rel)
12053
0
    {
12054
      /* Previous INSN16 reloc exists, reduce its
12055
         size to 16-bit.  */
12056
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12057
0
             irelend, &insn16))
12058
0
        {
12059
0
          nds32_elf_write_16 (abfd, contents, insn_rel,
12060
0
            internal_relocs, irelend, insn16);
12061
12062
0
          if (!insert_nds32_elf_blank_recalc_total
12063
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12064
0
      return false;
12065
0
        }
12066
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12067
0
        {
12068
0
          if (!insert_nds32_elf_blank_recalc_total
12069
0
        (relax_blank_list, insn_rel->r_offset, 2))
12070
0
      return false;
12071
0
        }
12072
0
      insn_rel->r_info =
12073
0
        ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12074
0
    }
12075
        /* Save the new one for later use.  */
12076
0
        insn_rel = irel;
12077
0
      }
12078
0
    else
12079
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12080
0
           R_NDS32_NONE);
12081
0
  }
12082
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12083
0
  {
12084
    /* Search for label.  */
12085
0
    int force_relax = 0;
12086
12087
    /* Label on 16-bit instruction or optimization
12088
       needless, just reset this reloc.  */
12089
0
    insn16 = bfd_getb16 (contents + irel->r_offset);
12090
0
    if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12091
0
      {
12092
0
        irel->r_info =
12093
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12094
0
        continue;
12095
0
      }
12096
12097
0
    address =
12098
0
      irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12099
0
              irel->r_offset, 1);
12100
12101
0
    if (!insn_rel)
12102
0
      {
12103
        /* Check if there is case which can not be aligned.  */
12104
0
        if (irel->r_addend == 2 && address & 0x2)
12105
0
    return false;
12106
0
        continue;
12107
0
      }
12108
12109
    /* Try to align this label.  */
12110
12111
0
    if ((irel->r_addend & 0x1f) < 2)
12112
0
      {
12113
        /* Check if there is a INSN16 at the same address.
12114
     Label_rel always seats before insn_rel after
12115
     our sort.  */
12116
12117
        /* Search for INSN16 at LABEL location.  If INSN16 is at
12118
     same location and this LABEL alignment is lower than 2,
12119
     the INSN16 can be converted to 2-byte.  */
12120
0
        for (tmp_rel = irel;
12121
0
       tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12122
0
       tmp_rel++)
12123
0
    {
12124
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12125
0
          && (is_convert_32_to_16
12126
0
        (abfd, sec, tmp_rel, internal_relocs,
12127
0
         irelend, &insn16)
12128
0
        || is_16bit_NOP (abfd, sec, tmp_rel)))
12129
0
        {
12130
0
          force_relax = 1;
12131
0
          break;
12132
0
        }
12133
0
    }
12134
0
      }
12135
12136
0
    if (force_relax || irel->r_addend == 1 || address & 0x2)
12137
0
      {
12138
        /* Label not aligned.  */
12139
        /* Previous reloc exists, reduce its size to 16-bit.  */
12140
0
        if (is_convert_32_to_16 (abfd, sec, insn_rel,
12141
0
               internal_relocs, irelend, &insn16))
12142
0
    {
12143
0
      nds32_elf_write_16 (abfd, contents, insn_rel,
12144
0
              internal_relocs, irelend, insn16);
12145
12146
0
      if (!insert_nds32_elf_blank_recalc_total
12147
0
          (relax_blank_list, insn_rel->r_offset + 2, 2))
12148
0
        return false;
12149
0
    }
12150
0
        else if (is_16bit_NOP (abfd, sec, insn_rel))
12151
0
    {
12152
0
      if (!insert_nds32_elf_blank_recalc_total
12153
0
          (relax_blank_list, insn_rel->r_offset, 2))
12154
0
        return false;
12155
0
    }
12156
12157
0
      }
12158
    /* INSN16 reloc is used.  */
12159
0
    insn_rel = NULL;
12160
0
  }
12161
0
    }
12162
12163
0
  address =
12164
0
    sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12165
0
  if (insn_rel && (address & 0x2 || opt_size))
12166
0
    {
12167
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12168
0
             irelend, &insn16))
12169
0
  {
12170
0
    nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12171
0
            irelend, insn16);
12172
0
    if (!insert_nds32_elf_blank_recalc_total
12173
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12174
0
      return false;
12175
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12176
0
             R_NDS32_NONE);
12177
0
  }
12178
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12179
0
  {
12180
0
    if (!insert_nds32_elf_blank_recalc_total
12181
0
        (relax_blank_list, insn_rel->r_offset, 2))
12182
0
      return false;
12183
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12184
0
             R_NDS32_NONE);
12185
0
  }
12186
0
    }
12187
0
  insn_rel = NULL;
12188
0
  return true;
12189
0
}
12190
12191
static bool
12192
nds32_elf_relax_section (bfd *abfd, asection *sec,
12193
       struct bfd_link_info *link_info, bool *again)
12194
0
{
12195
0
  nds32_elf_blank_t *relax_blank_list = NULL;
12196
0
  Elf_Internal_Shdr *symtab_hdr;
12197
0
  Elf_Internal_Rela *internal_relocs;
12198
0
  Elf_Internal_Rela *irel;
12199
0
  Elf_Internal_Rela *irelend;
12200
0
  Elf_Internal_Sym *isymbuf = NULL;
12201
0
  bfd_byte *contents = NULL;
12202
0
  bool result = true;
12203
0
  int optimize = 0;
12204
0
  int opt_size = 0;
12205
0
  uint32_t insn;
12206
0
  uint16_t insn16;
12207
12208
  /* Target dependnet option.  */
12209
0
  struct elf_nds32_link_hash_table *table;
12210
0
  int load_store_relax;
12211
12212
0
  relax_blank_list = NULL;
12213
12214
0
  *again = false;
12215
12216
  /* Nothing to do for
12217
   * relocatable link or
12218
   * non-relocatable section or
12219
   * non-code section or
12220
   * empty content or
12221
   * no reloc entry.  */
12222
0
  if (bfd_link_relocatable (link_info)
12223
0
      || (sec->flags & SEC_RELOC) == 0
12224
0
      || (sec->flags & SEC_EXCLUDE) != 0
12225
0
      || (sec->flags & SEC_CODE) == 0
12226
0
      || sec->size == 0
12227
0
      || sec->reloc_count == 0)
12228
0
    return true;
12229
12230
  /* 09.12.11 Workaround.  */
12231
  /*  We have to adjust align for R_NDS32_LABEL if needed.
12232
      The adjust approach only can fix 2-byte align once.  */
12233
0
  if (sec->alignment_power > 2)
12234
0
    return true;
12235
12236
  /* Do TLS model conversion once at first.  */
12237
0
  nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12238
12239
  /* The optimization type to do.  */
12240
12241
0
  table = nds32_elf_hash_table (link_info);
12242
12243
  /* Save the first section for abs symbol relaxation.
12244
     This is used for checking gp relaxation in the
12245
     nds32_elf_relax_loadstore and nds32_elf_relax_lo12.  */
12246
0
  nds32_elf_relax_guard (NULL, 0, sec, NULL, again, true,
12247
0
       table, NULL, NULL);
12248
12249
  /* The begining of general relaxation.  */
12250
12251
0
  if (is_SDA_BASE_set == 0)
12252
0
    {
12253
0
      bfd_vma gp;
12254
0
      is_SDA_BASE_set = 1;
12255
0
      nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12256
0
        &gp, false);
12257
0
      relax_range_measurement (abfd, link_info);
12258
0
    }
12259
12260
0
  symtab_hdr = &elf_symtab_hdr (abfd);
12261
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
12262
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12263
0
                 true /* keep_memory */);
12264
0
  if (internal_relocs == NULL)
12265
0
    goto error_return;
12266
12267
0
  irelend = internal_relocs + sec->reloc_count;
12268
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
12269
0
         irelend, R_NDS32_RELAX_ENTRY);
12270
12271
0
  if (irel == irelend)
12272
0
    return true;
12273
12274
0
  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12275
0
    {
12276
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12277
0
  return true;
12278
12279
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12280
0
  optimize = 1;
12281
12282
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12283
0
  opt_size = 1;
12284
0
    }
12285
12286
0
  load_store_relax = table->load_store_relax;
12287
12288
  /* Get symbol table and section content.  */
12289
0
  contents = NULL;
12290
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12291
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12292
0
    goto error_return;
12293
12294
  /* Do relax loop only when finalize is not done.
12295
     Take care of relaxable relocs except INSN16.  */
12296
0
  for (irel = internal_relocs; irel < irelend; irel++)
12297
0
    {
12298
0
      int seq_len;    /* Original length of instruction sequence.  */
12299
0
      int insn_len = 0;   /* Final length of instruction sequence.  */
12300
0
      bool removed;
12301
12302
0
      insn = 0;
12303
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12304
0
    && (irel->r_addend & 0x1f) >= 2)
12305
0
  optimize = 1;
12306
12307
      /* Relocation Types
12308
   R_NDS32_LONGCALL1  53
12309
   R_NDS32_LONGCALL2  54
12310
   R_NDS32_LONGCALL3  55
12311
   R_NDS32_LONGJUMP1  56
12312
   R_NDS32_LONGJUMP2  57
12313
   R_NDS32_LONGJUMP3  58
12314
   R_NDS32_LOADSTORE  59  */
12315
0
      if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12316
0
    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12317
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
12318
12319
      /* Relocation Types
12320
   R_NDS32_LONGCALL4  107
12321
   R_NDS32_LONGCALL5  108
12322
   R_NDS32_LONGCALL6  109
12323
   R_NDS32_LONGJUMP4  110
12324
   R_NDS32_LONGJUMP5  111
12325
   R_NDS32_LONGJUMP6  112
12326
   R_NDS32_LONGJUMP7  113  */
12327
0
      else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12328
0
         && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12329
0
  seq_len = 4;
12330
12331
  /* Relocation Types
12332
   R_NDS32_LO12S0_RELA    30
12333
   R_NDS32_LO12S1_RELA    29
12334
   R_NDS32_LO12S2_RELA    28
12335
   R_NDS32_LO12S2_SP_RELA   71
12336
   R_NDS32_LO12S2_DP_RELA   70
12337
   R_NDS32_GOT_LO12   46
12338
   R_NDS32_GOTOFF_LO12    50
12339
   R_NDS32_PLTREL_LO12    65
12340
   R_NDS32_PLT_GOTREL_LO12  67
12341
   R_NDS32_17IFC_PCREL_RELA 96
12342
   R_NDS32_GOT_SUFF   193
12343
   R_NDS32_GOTOFF_SUFF    194
12344
   R_NDS32_PLT_GOT_SUFF   195
12345
   R_NDS32_MULCALL_SUFF   196
12346
   R_NDS32_PTR      197  */
12347
0
      else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12348
0
    && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12349
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12350
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12351
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12352
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12353
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12354
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12355
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12356
0
         || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12357
0
       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12358
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12359
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12360
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12361
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12362
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12363
0
  seq_len = 0;
12364
0
      else
12365
0
  continue;
12366
12367
0
      insn_len = seq_len;
12368
0
      removed = false;
12369
12370
0
      switch (ELF32_R_TYPE (irel->r_info))
12371
0
  {
12372
0
  case R_NDS32_LONGCALL1:
12373
0
    removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12374
0
                 &insn_len, contents, isymbuf,
12375
0
                 symtab_hdr);
12376
0
    break;
12377
0
  case R_NDS32_LONGCALL2:
12378
0
    removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12379
0
                 &insn_len, contents, isymbuf,
12380
0
                 symtab_hdr);
12381
0
    break;
12382
0
  case R_NDS32_LONGCALL3:
12383
0
    removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12384
0
                 &insn_len, contents, isymbuf,
12385
0
                 symtab_hdr);
12386
0
    break;
12387
0
  case R_NDS32_LONGJUMP1:
12388
0
    removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12389
0
                 &insn_len, contents, isymbuf,
12390
0
                 symtab_hdr);
12391
0
    break;
12392
0
  case R_NDS32_LONGJUMP2:
12393
0
    removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12394
0
                 &insn_len, contents, isymbuf,
12395
0
                 symtab_hdr);
12396
0
    break;
12397
0
  case R_NDS32_LONGJUMP3:
12398
0
    removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12399
0
                 &insn_len, contents, isymbuf,
12400
0
                 symtab_hdr);
12401
0
    break;
12402
0
  case R_NDS32_LONGCALL4:
12403
0
    removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12404
0
                 &insn_len, contents, isymbuf,
12405
0
                 symtab_hdr);
12406
0
    break;
12407
0
  case R_NDS32_LONGCALL5:
12408
0
    removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12409
0
                 &insn_len, contents, isymbuf,
12410
0
                 symtab_hdr);
12411
0
    break;
12412
0
  case R_NDS32_LONGCALL6:
12413
0
    removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12414
0
                 &insn_len, contents, isymbuf,
12415
0
                 symtab_hdr);
12416
0
    break;
12417
0
  case R_NDS32_LONGJUMP4:
12418
0
    removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12419
0
                 &insn_len, contents, isymbuf,
12420
0
                 symtab_hdr);
12421
0
    break;
12422
0
  case R_NDS32_LONGJUMP5:
12423
0
    removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12424
0
                 &insn_len, &seq_len, contents,
12425
0
                 isymbuf, symtab_hdr);
12426
0
    break;
12427
0
  case R_NDS32_LONGJUMP6:
12428
0
    removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12429
0
                 &insn_len, &seq_len, contents,
12430
0
                 isymbuf, symtab_hdr);
12431
0
    break;
12432
0
  case R_NDS32_LONGJUMP7:
12433
0
    removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12434
0
                 &insn_len, &seq_len, contents,
12435
0
                 isymbuf, symtab_hdr);
12436
0
    break;
12437
0
  case R_NDS32_LOADSTORE:
12438
0
    removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12439
0
                 internal_relocs, &insn_len,
12440
0
                 contents, isymbuf, symtab_hdr,
12441
0
                 load_store_relax, table);
12442
0
    break;
12443
0
  case R_NDS32_LO12S0_RELA:
12444
0
  case R_NDS32_LO12S1_RELA:
12445
0
  case R_NDS32_LO12S2_RELA:
12446
0
  case R_NDS32_LO12S2_DP_RELA:
12447
0
  case R_NDS32_LO12S2_SP_RELA:
12448
    /* Relax for low part.  */
12449
0
    nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12450
0
        contents, isymbuf, symtab_hdr, table);
12451
12452
    /* It is impossible to delete blank, so just continue.  */
12453
0
    continue;
12454
0
  case R_NDS32_PTR:
12455
0
    removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12456
0
           &insn_len, &seq_len, contents);
12457
0
    break;
12458
0
  case R_NDS32_LSI:
12459
0
    nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12460
0
        contents, isymbuf, symtab_hdr, again);
12461
0
    continue;
12462
0
  case R_NDS32_GOT_LO12:
12463
0
  case R_NDS32_GOTOFF_LO12:
12464
0
  case R_NDS32_PLTREL_LO12:
12465
0
  case R_NDS32_PLT_GOTREL_LO12:
12466
0
  case R_NDS32_GOTPC_LO12:
12467
0
  case R_NDS32_TLS_LE_LO12:
12468
0
  case R_NDS32_TLS_LE_ADD:
12469
0
  case R_NDS32_TLS_LE_LS:
12470
0
  case R_NDS32_PLT_GOT_SUFF:
12471
0
  case R_NDS32_GOT_SUFF:
12472
0
  case R_NDS32_GOTOFF_SUFF:
12473
0
    continue;
12474
0
  default:
12475
0
    continue;
12476
0
  }
12477
12478
0
      if (removed && seq_len - insn_len > 0)
12479
0
  {
12480
0
    if (!insert_nds32_elf_blank
12481
0
        (&relax_blank_list, irel->r_offset + insn_len,
12482
0
         seq_len - insn_len))
12483
0
      goto error_return;
12484
0
    *again = true;
12485
0
  }
12486
0
    }
12487
12488
0
  calc_nds32_blank_total (relax_blank_list);
12489
12490
0
  if (table->relax_fp_as_gp)
12491
0
    {
12492
0
      if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12493
0
         irelend, isymbuf))
12494
0
  goto error_return;
12495
12496
0
      if (!*again)
12497
0
  {
12498
0
    if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12499
0
                 irelend))
12500
0
      goto error_return;
12501
0
  }
12502
0
    }
12503
12504
0
  if (!*again)
12505
0
    {
12506
0
      if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12507
0
             &relax_blank_list, optimize, opt_size))
12508
0
  goto error_return;
12509
0
    }
12510
12511
  /* It doesn't matter optimize_for_space_no_align anymore.
12512
       If object file is assembled with flag '-Os',
12513
       the we don't adjust jump-destination on 4-byte boundary.  */
12514
12515
0
  if (relax_blank_list)
12516
0
    {
12517
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12518
0
      relax_blank_list = NULL;
12519
0
    }
12520
12521
0
  if (!*again)
12522
0
    {
12523
      /* Closing the section, so we don't relax it anymore.  */
12524
0
      bfd_vma sec_size_align;
12525
0
      Elf_Internal_Rela *tmp_rel;
12526
12527
      /* Pad to alignment boundary.  Only handle current section alignment.  */
12528
0
      sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12529
0
           & ((-1U) << sec->alignment_power);
12530
0
      if ((sec_size_align - sec->size) & 0x2)
12531
0
  {
12532
0
    insn16 = NDS32_NOP16;
12533
0
    bfd_putb16 (insn16, contents + sec->size);
12534
0
    sec->size += 2;
12535
0
  }
12536
12537
0
      while (sec_size_align != sec->size)
12538
0
  {
12539
0
    insn = NDS32_NOP32;
12540
0
    bfd_putb32 (insn, contents + sec->size);
12541
0
    sec->size += 4;
12542
0
  }
12543
12544
0
      tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12545
0
          irelend, R_NDS32_RELAX_ENTRY);
12546
0
      if (tmp_rel != irelend)
12547
0
  tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12548
12549
0
      clean_nds32_elf_blank ();
12550
0
    }
12551
12552
0
 finish:
12553
0
  if (elf_section_data (sec)->relocs != internal_relocs)
12554
0
    free (internal_relocs);
12555
12556
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
12557
0
    free (contents);
12558
12559
0
  if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12560
0
    free (isymbuf);
12561
12562
0
  return result;
12563
12564
0
 error_return:
12565
0
  result = false;
12566
0
  goto finish;
12567
0
}
12568
12569
static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12570
{
12571
  {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12572
  {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12573
  {NULL, 0, 0, 0, 0}
12574
};
12575
12576
static bool
12577
nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12578
3.54k
{
12579
3.54k
  const char *name = hdr->bfd_section->name;
12580
12581
3.54k
  if (startswith (name, ".sbss")
12582
3.50k
      || startswith (name, ".sdata"))
12583
93
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
12584
12585
3.54k
  return true;
12586
3.54k
}
12587
12588
static bool
12589
nds32_elf_output_arch_syms (struct bfd_link_info *info,
12590
          void *finfo ATTRIBUTE_UNUSED,
12591
          int (*func) (void *, const char *,
12592
           Elf_Internal_Sym *,
12593
           asection *,
12594
           struct elf_link_hash_entry *)
12595
          ATTRIBUTE_UNUSED)
12596
0
{
12597
0
  FILE *sym_ld_script = NULL;
12598
0
  struct elf_nds32_link_hash_table *table;
12599
12600
0
  table = nds32_elf_hash_table (info);
12601
0
  sym_ld_script = table->sym_ld_script;
12602
12603
0
  if (check_start_export_sym)
12604
0
    fprintf (sym_ld_script, "}\n");
12605
12606
0
  return true;
12607
0
}
12608
12609
static enum elf_reloc_type_class
12610
nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12611
          const asection *rel_sec ATTRIBUTE_UNUSED,
12612
          const Elf_Internal_Rela *rela)
12613
0
{
12614
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
12615
0
    {
12616
0
    case R_NDS32_RELATIVE:
12617
0
      return reloc_class_relative;
12618
0
    case R_NDS32_JMP_SLOT:
12619
0
      return reloc_class_plt;
12620
0
    case R_NDS32_COPY:
12621
0
      return reloc_class_copy;
12622
0
    default:
12623
0
      return reloc_class_normal;
12624
0
    }
12625
0
}
12626
12627
/* Put target dependent option into info hash table.  */
12628
void
12629
bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12630
           int relax_fp_as_gp,
12631
           int eliminate_gc_relocs,
12632
           FILE * sym_ld_script,
12633
           int hyper_relax,
12634
           int tls_desc_trampoline,
12635
           int load_store_relax)
12636
0
{
12637
0
  struct elf_nds32_link_hash_table *table;
12638
12639
0
  table = nds32_elf_hash_table (link_info);
12640
0
  if (table == NULL)
12641
0
    return;
12642
12643
0
  table->relax_fp_as_gp = relax_fp_as_gp;
12644
0
  table->eliminate_gc_relocs = eliminate_gc_relocs;
12645
0
  table->sym_ld_script = sym_ld_script;
12646
0
  table->hyper_relax = hyper_relax;
12647
0
  table->tls_desc_trampoline = tls_desc_trampoline;
12648
0
  table ->load_store_relax = load_store_relax;
12649
0
}
12650

12651
12652
/* These functions and data-structures are used for fp-as-gp
12653
   optimization.  */
12654
12655
0
#define FAG_THRESHOLD 3  /* At least 3 gp-access.  */
12656
/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12657
   the read-only section and read-write section.  */
12658
0
#define FAG_WINDOW  (508 - 32)
12659
12660
/* An nds32_fag represent a gp-relative access.
12661
   We find best fp-base by using a sliding window
12662
   to find a base address which can cover most gp-access.  */
12663
struct nds32_fag
12664
{
12665
  struct nds32_fag *next; /* NULL-teminated linked list.  */
12666
  bfd_vma addr;     /* The address of this fag.  */
12667
  Elf_Internal_Rela **relas;  /* The relocations associated with this fag.
12668
           It is used for applying FP7U2_FLAG.  */
12669
  int count;      /* How many times this address is referred.
12670
           There should be exactly `count' relocations
12671
           in relas.  */
12672
  int relas_capcity;    /* The buffer size of relas.
12673
           We use an array instead of linked-list,
12674
           and realloc is used to adjust buffer size.  */
12675
};
12676
12677
static void
12678
nds32_fag_init (struct nds32_fag *head)
12679
0
{
12680
0
  memset (head, 0, sizeof (struct nds32_fag));
12681
0
}
12682
12683
static void
12684
nds32_fag_verify (struct nds32_fag *head)
12685
0
{
12686
0
  struct nds32_fag *iter;
12687
0
  struct nds32_fag *prev;
12688
12689
0
  prev = NULL;
12690
0
  iter = head->next;
12691
0
  while (iter)
12692
0
    {
12693
0
      if (prev && prev->addr >= iter->addr)
12694
0
  puts ("Bug in fp-as-gp insertion.");
12695
0
      prev = iter;
12696
0
      iter = iter->next;
12697
0
    }
12698
0
}
12699
12700
/* Insert a fag in ascending order.
12701
   If a fag of the same address already exists,
12702
   they are chained by relas array.  */
12703
12704
static void
12705
nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12706
      Elf_Internal_Rela * rel)
12707
0
{
12708
0
  struct nds32_fag *iter;
12709
0
  struct nds32_fag *new_fag;
12710
0
  const int INIT_RELAS_CAP = 4;
12711
12712
0
  for (iter = head;
12713
0
       iter->next && iter->next->addr <= addr;
12714
0
       iter = iter->next)
12715
0
    /* Find somewhere to insert.  */ ;
12716
12717
  /* `iter' will be equal to `head' if the list is empty.  */
12718
0
  if (iter != head && iter->addr == addr)
12719
0
    {
12720
      /* The address exists in the list.
12721
   Insert `rel' into relocation list, relas.  */
12722
12723
      /* Check whether relas is big enough.  */
12724
0
      if (iter->count >= iter->relas_capcity)
12725
0
  {
12726
0
    iter->relas_capcity *= 2;
12727
0
    iter->relas = bfd_realloc
12728
0
      (iter->relas, iter->relas_capcity * sizeof (void *));
12729
0
  }
12730
0
      iter->relas[iter->count++] = rel;
12731
0
      return;
12732
0
    }
12733
12734
  /* This is a new address.  Create a fag node for it.  */
12735
0
  new_fag = bfd_malloc (sizeof (struct nds32_fag));
12736
0
  memset (new_fag, 0, sizeof (*new_fag));
12737
0
  new_fag->addr = addr;
12738
0
  new_fag->count = 1;
12739
0
  new_fag->next = iter->next;
12740
0
  new_fag->relas_capcity = INIT_RELAS_CAP;
12741
0
  new_fag->relas = (Elf_Internal_Rela **)
12742
0
    bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12743
0
  new_fag->relas[0] = rel;
12744
0
  iter->next = new_fag;
12745
12746
0
  nds32_fag_verify (head);
12747
0
}
12748
12749
static void
12750
nds32_fag_free_list (struct nds32_fag *head)
12751
0
{
12752
0
  struct nds32_fag *iter;
12753
12754
0
  iter = head->next;
12755
0
  while (iter)
12756
0
    {
12757
0
      struct nds32_fag *tmp = iter;
12758
0
      iter = iter->next;
12759
0
      free (tmp->relas);
12760
0
      tmp->relas = NULL;
12761
0
      free (tmp);
12762
0
    }
12763
0
}
12764
12765
/* Find the best fp-base address.
12766
   The relocation associated with that address is returned,
12767
   so we can track the symbol instead of a fixed address.
12768
12769
   When relaxation, the address of an datum may change,
12770
   because a text section is shrinked, so the data section
12771
   moves forward.  If the aligments of text and data section
12772
   are different, their distance may change too.
12773
   Therefore, tracking a fixed address is not appriate.  */
12774
12775
static int
12776
nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12777
0
{
12778
0
  struct nds32_fag *base; /* First fag in the window.  */
12779
0
  struct nds32_fag *last; /* First fag outside the window.  */
12780
0
  int accu = 0;     /* Usage accumulation.  */
12781
0
  struct nds32_fag *best; /* Best fag.  */
12782
0
  int baccu = 0;    /* Best accumulation.  */
12783
12784
  /* Use first fag for initial, and find the last fag in the window.
12785
12786
     In each iteration, we could simply subtract previous fag
12787
     and accumulate following fags which are inside the window,
12788
     untill we each the end.  */
12789
12790
0
  if (head->next == NULL)
12791
0
    {
12792
0
      *bestpp = NULL;
12793
0
      return 0;
12794
0
    }
12795
12796
  /* Initialize base.  */
12797
0
  base = head->next;
12798
0
  best = base;
12799
0
  for (last = base;
12800
0
       last && last->addr < base->addr + FAG_WINDOW;
12801
0
       last = last->next)
12802
0
    accu += last->count;
12803
12804
0
  baccu = accu;
12805
12806
  /* Record the best base in each iteration.  */
12807
0
  while (base->next)
12808
0
    {
12809
0
      accu -= base->count;
12810
0
      base = base->next;
12811
      /* Account fags in window.  */
12812
0
      for (/* Nothing.  */;
12813
0
     last && last->addr < base->addr + FAG_WINDOW;
12814
0
     last = last->next)
12815
0
  accu += last->count;
12816
12817
      /* A better fp-base?  */
12818
0
      if (accu > baccu)
12819
0
  {
12820
0
    best = base;
12821
0
    baccu = accu;
12822
0
  }
12823
0
    }
12824
12825
0
  if (bestpp)
12826
0
    *bestpp = best;
12827
0
  return baccu;
12828
0
}
12829
12830
/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12831
   so we can convert it fo fp-relative access later.
12832
   `best_fag' is the best fp-base.  Only those inside the window
12833
   of best_fag is applied the flag.  */
12834
12835
static bool
12836
nds32_fag_mark_relax (struct bfd_link_info *link_info,
12837
          asection *sec, struct nds32_fag *best_fag,
12838
          Elf_Internal_Rela *internal_relocs,
12839
          Elf_Internal_Rela *irelend)
12840
0
{
12841
0
  struct nds32_fag *ifag;
12842
0
  bfd_vma best_fpbase, gp;
12843
0
  bfd *output_bfd;
12844
12845
0
  output_bfd = sec->output_section->owner;
12846
0
  nds32_elf_final_sda_base (output_bfd, link_info, &gp, false);
12847
0
  best_fpbase = best_fag->addr;
12848
12849
0
  if (best_fpbase > gp + sdata_range[1][1]
12850
0
      || best_fpbase < gp - sdata_range[1][0])
12851
0
    return false;
12852
12853
  /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12854
     so we know they can be converted to lwi37.fp.   */
12855
0
  for (ifag = best_fag;
12856
0
       ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12857
0
    {
12858
0
      int i;
12859
12860
0
      for (i = 0; i < ifag->count; i++)
12861
0
  {
12862
0
    Elf_Internal_Rela *insn16_rel;
12863
0
    Elf_Internal_Rela *fag_rel;
12864
12865
0
    fag_rel = ifag->relas[i];
12866
12867
    /* Only if this is within the WINDOWS, FP7U2_FLAG
12868
       is applied.  */
12869
12870
0
    insn16_rel = find_relocs_at_address
12871
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12872
12873
0
    if (insn16_rel != irelend)
12874
0
      insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12875
0
  }
12876
0
    }
12877
0
  return true;
12878
0
}
12879
12880
/* Reset INSN16 to clean fp as gp.  */
12881
12882
static void
12883
nds32_fag_unmark_relax (struct nds32_fag *fag,
12884
      Elf_Internal_Rela *internal_relocs,
12885
      Elf_Internal_Rela *irelend)
12886
0
{
12887
0
  struct nds32_fag *ifag;
12888
0
  int i;
12889
0
  Elf_Internal_Rela *insn16_rel;
12890
0
  Elf_Internal_Rela *fag_rel;
12891
12892
0
  for (ifag = fag; ifag; ifag = ifag->next)
12893
0
    {
12894
0
      for (i = 0; i < ifag->count; i++)
12895
0
  {
12896
0
    fag_rel = ifag->relas[i];
12897
12898
    /* Restore the INSN16 relocation.  */
12899
0
    insn16_rel = find_relocs_at_address
12900
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12901
12902
0
    if (insn16_rel != irelend)
12903
0
      insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12904
0
  }
12905
0
    }
12906
0
}
12907
12908
/* This is the main function of fp-as-gp optimization.
12909
   It should be called by relax_section.  */
12910
12911
static bool
12912
nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12913
          bfd *abfd, asection *sec,
12914
          Elf_Internal_Rela *internal_relocs,
12915
          Elf_Internal_Rela *irelend,
12916
          Elf_Internal_Sym *isymbuf)
12917
0
{
12918
0
  Elf_Internal_Rela *begin_rel = NULL;
12919
0
  Elf_Internal_Rela *irel;
12920
0
  struct nds32_fag fag_head;
12921
0
  Elf_Internal_Shdr *symtab_hdr;
12922
0
  bfd_byte *contents;
12923
0
  bool ifc_inside = false;
12924
12925
  /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12926
12927
  /* Per-function fp-base selection.
12928
     1. Create a list for all the gp-relative access.
12929
     2. Base on those gp-relative address,
12930
  find a fp-base which can cover most access.
12931
     3. Use the fp-base for fp-as-gp relaxation.
12932
12933
     NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12934
     we should
12935
     1. delete the `la $fp, _FP_BASE_' instruction and
12936
     2. not convert lwi.gp to lwi37.fp.
12937
12938
     To delete the _FP_BASE_ instruction, we simply apply
12939
     R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12940
12941
     To suppress the conversion, we simply NOT to apply
12942
     R_NDS32_INSN16_FP7U2_FLAG flag.  */
12943
12944
0
  symtab_hdr = &elf_symtab_hdr (abfd);
12945
12946
0
  contents = NULL;
12947
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12948
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12949
0
    return false;
12950
12951
  /* Check whether it is worth for fp-as-gp optimization,
12952
     i.e., at least 3 gp-load.
12953
12954
     Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12955
     apply this optimization.  */
12956
12957
0
  for (irel = internal_relocs; irel < irelend; irel++)
12958
0
    {
12959
      /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12960
   One we enter the begin of the region, we track all the LW/ST
12961
   instructions, so when we leave the region, we try to find
12962
   the best fp-base address for those LW/ST instructions.  */
12963
12964
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12965
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12966
0
  {
12967
    /* Begin of the region.  */
12968
0
    if (begin_rel)
12969
      /* xgettext:c-format */
12970
0
      _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12971
12972
0
    begin_rel = irel;
12973
0
    nds32_fag_init (&fag_head);
12974
0
    ifc_inside = false;
12975
0
  }
12976
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12977
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12978
0
  {
12979
0
    int accu;
12980
0
    struct nds32_fag *best_fag, *tmp_fag;
12981
0
    int dist;
12982
12983
    /* End of the region.
12984
       Check whether it is worth to do fp-as-gp.  */
12985
12986
0
    if (begin_rel == NULL)
12987
0
      {
12988
        /* xgettext:c-format */
12989
0
        _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12990
0
          abfd, sec);
12991
0
        continue;
12992
0
      }
12993
12994
0
    accu = nds32_fag_find_base (&fag_head, &best_fag);
12995
12996
    /* Clean FP7U2_FLAG because they may set ever.  */
12997
0
    tmp_fag = fag_head.next;
12998
0
    nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12999
13000
    /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
13001
0
    if (accu < FAG_THRESHOLD
13002
0
        || !nds32_fag_mark_relax (link_info, sec, best_fag,
13003
0
          internal_relocs, irelend))
13004
0
      {
13005
        /* Not worth to do fp-as-gp.  */
13006
0
        begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13007
0
        begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13008
0
        irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13009
0
        irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13010
0
        nds32_fag_free_list (&fag_head);
13011
0
        begin_rel = NULL;
13012
0
        continue;
13013
0
      }
13014
13015
    /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13016
       so we use it to record the distance to the reloction of best
13017
       fp-base.  */
13018
0
    dist = best_fag->relas[0] - begin_rel;
13019
0
    BFD_ASSERT (dist > 0 && dist < 0xffffff);
13020
    /* Use high 16 bits of addend to record the _FP_BASE_ matched
13021
       relocation.  And get the base value when relocating.  */
13022
0
    begin_rel->r_addend &= (0x1 << 16) - 1;
13023
0
    begin_rel->r_addend |= dist << 16;
13024
13025
0
    nds32_fag_free_list (&fag_head);
13026
0
    begin_rel = NULL;
13027
0
  }
13028
13029
0
      if (begin_rel == NULL || ifc_inside)
13030
  /* Skip if we are not in the region of fp-as-gp.  */
13031
0
  continue;
13032
13033
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13034
0
    || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13035
0
  {
13036
0
    bfd_vma addr;
13037
0
    uint32_t insn;
13038
13039
    /* A gp-relative access is found.  Insert it to the fag-list.  */
13040
13041
    /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
13042
0
    insn = bfd_getb32 (contents + irel->r_offset);
13043
0
    if (!N32_IS_RT3 (insn))
13044
0
      continue;
13045
13046
0
    addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13047
0
    nds32_fag_insert (&fag_head, addr, irel);
13048
0
  }
13049
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13050
0
  {
13051
0
    begin_rel = NULL;
13052
0
  }
13053
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13054
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13055
0
  {
13056
    /* Suppress fp as gp when encounter ifc.  */
13057
0
    ifc_inside = true;
13058
0
  }
13059
0
    }
13060
13061
0
  return true;
13062
0
}
13063
13064
/* Remove unused `la $fp, _FD_BASE_' instruction.  */
13065
13066
static bool
13067
nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13068
        Elf_Internal_Rela *internal_relocs,
13069
        Elf_Internal_Rela *irelend)
13070
0
{
13071
0
  Elf_Internal_Rela *irel;
13072
0
  Elf_Internal_Shdr *symtab_hdr;
13073
0
  bfd_byte *contents = NULL;
13074
0
  nds32_elf_blank_t *relax_blank_list = NULL;
13075
0
  bool result = true;
13076
0
  bool unused_region = false;
13077
13078
  /*
13079
     NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13080
       R_NDS32_17IFC_PCREL_RELA
13081
       R_NDS32_10IFCU_PCREL_RELA.  */
13082
13083
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13084
0
  nds32_get_section_contents (abfd, sec, &contents, true);
13085
13086
0
  for (irel = internal_relocs; irel < irelend; irel++)
13087
0
    {
13088
      /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13089
   we marked to in previous pass.
13090
   DO NOT scan relocations again, since we've alreadly decided it
13091
   and set the flag.  */
13092
0
      const char *syname;
13093
0
      int syndx;
13094
0
      uint32_t insn;
13095
13096
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13097
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13098
0
  unused_region = true;
13099
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13100
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13101
0
  unused_region = false;
13102
13103
      /* We're not in the region.  */
13104
0
      if (!unused_region)
13105
0
  continue;
13106
13107
      /* _FP_BASE_ must be a GLOBAL symbol.  */
13108
0
      syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13109
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13110
0
  continue;
13111
13112
      /* The symbol name must be _FP_BASE_.  */
13113
0
      syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13114
0
      if (strcmp (syname, FP_BASE_NAME) != 0)
13115
0
  continue;
13116
13117
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13118
0
  {
13119
    /* addi.gp  $fp, -256  */
13120
0
    insn = bfd_getb32 (contents + irel->r_offset);
13121
0
    if (insn != INSN_ADDIGP_TO_FP)
13122
0
      continue;
13123
0
  }
13124
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13125
0
  {
13126
    /* addi  $fp, $gp, -256  */
13127
0
    insn = bfd_getb32 (contents + irel->r_offset);
13128
0
    if (insn != INSN_ADDI_GP_TO_FP)
13129
0
      continue;
13130
0
  }
13131
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13132
0
  {
13133
    /* movi  $fp, FP_BASE  */
13134
0
    insn = bfd_getb32 (contents + irel->r_offset);
13135
0
    if (insn != INSN_MOVI_TO_FP)
13136
0
      continue;
13137
0
  }
13138
0
      else
13139
0
  continue;
13140
13141
      /* We got here because a FP_BASE instruction is found.  */
13142
0
      if (!insert_nds32_elf_blank_recalc_total
13143
0
    (&relax_blank_list, irel->r_offset, 4))
13144
0
  goto error_return;
13145
0
    }
13146
13147
0
 finish:
13148
0
  if (relax_blank_list)
13149
0
    {
13150
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13151
0
      relax_blank_list = NULL;
13152
0
    }
13153
0
  return result;
13154
13155
0
 error_return:
13156
0
  result = false;
13157
0
  goto finish;
13158
0
}
13159
13160
/* This is a version of bfd_generic_get_relocated_section_contents.
13161
   We need this variety because relaxation will modify the dwarf
13162
   infomation.  When there is undefined symbol reference error mesage,
13163
   linker need to dump line number where the symbol be used.  However
13164
   the address is be relaxed, it can not get the original dwarf contents.
13165
   The variety only modify function call for reading in the section.  */
13166
13167
static bfd_byte *
13168
nds32_elf_get_relocated_section_contents
13169
  (bfd *abfd,
13170
   struct bfd_link_info *link_info,
13171
   const struct bfd_link_order *link_order,
13172
   bfd_byte *data,
13173
   bool relocatable,
13174
   asymbol **symbols)
13175
51
{
13176
51
  bfd *input_bfd = link_order->u.indirect.section->owner;
13177
51
  asection *input_section = link_order->u.indirect.section;
13178
51
  long reloc_size;
13179
51
  arelent **reloc_vector;
13180
51
  long reloc_count;
13181
13182
51
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13183
51
  if (reloc_size < 0)
13184
1
    return NULL;
13185
13186
  /* Read in the section.  */
13187
50
  bfd_byte *orig_data = data;
13188
50
  if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
13189
2
    return NULL;
13190
13191
48
  if (reloc_size == 0)
13192
0
    return data;
13193
13194
48
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13195
48
  if (reloc_vector == NULL)
13196
0
    goto error_return;
13197
13198
48
  reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13199
48
          reloc_vector, symbols);
13200
48
  if (reloc_count < 0)
13201
10
    goto error_return;
13202
13203
38
  if (reloc_count > 0)
13204
30
    {
13205
30
      arelent **parent;
13206
279
      for (parent = reloc_vector; *parent != NULL; parent++)
13207
273
  {
13208
273
    char *error_message = NULL;
13209
273
    asymbol *symbol;
13210
273
    bfd_reloc_status_type r;
13211
13212
273
    symbol = *(*parent)->sym_ptr_ptr;
13213
273
    if (symbol->section && discarded_section (symbol->section))
13214
0
      {
13215
0
        bfd_vma off;
13216
0
        static reloc_howto_type none_howto
13217
0
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13218
0
       "unused", false, 0, 0, false);
13219
13220
0
        off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13221
0
                input_section);
13222
0
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13223
0
           input_section, data, off);
13224
0
        (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13225
0
        (*parent)->addend = 0;
13226
0
        (*parent)->howto = &none_howto;
13227
0
        r = bfd_reloc_ok;
13228
0
      }
13229
273
    else
13230
273
      r = bfd_perform_relocation (input_bfd, *parent, data,
13231
273
          input_section,
13232
273
          relocatable ? abfd : NULL,
13233
273
          &error_message);
13234
13235
273
    if (relocatable)
13236
0
      {
13237
0
        asection *os = input_section->output_section;
13238
13239
        /* A partial link, so keep the relocs.  */
13240
0
        os->orelocation[os->reloc_count] = *parent;
13241
0
        os->reloc_count++;
13242
0
      }
13243
13244
273
    if (r != bfd_reloc_ok)
13245
38
      {
13246
38
        _bfd_link_reloc_status_error (abfd, link_info, input_section,
13247
38
              *parent, error_message, r);
13248
38
        if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
13249
24
    goto error_return;
13250
38
      }
13251
273
  }
13252
30
    }
13253
13254
14
  free (reloc_vector);
13255
14
  return data;
13256
13257
34
 error_return:
13258
34
  free (reloc_vector);
13259
34
  if (orig_data == NULL)
13260
20
    free (data);
13261
34
  return NULL;
13262
38
}
13263
13264
/* Check target symbol.  */
13265
13266
static bool
13267
nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13268
1.25k
{
13269
1.25k
  if (!sym || !sym->name || sym->name[0] != '$')
13270
1.22k
    return false;
13271
35
  return true;
13272
1.25k
}
13273
13274
/* nds32 find maybe function sym.  Ignore target special symbol
13275
   first, and then go the general function.  */
13276
13277
static bfd_size_type
13278
nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13279
            bfd_vma *code_off)
13280
944
{
13281
944
  if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13282
27
    return 0;
13283
13284
917
  return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13285
944
}
13286

13287
13288
/* Do TLS model conversion.  */
13289
13290
typedef struct relax_group_list_t
13291
{
13292
  Elf_Internal_Rela *relo;
13293
  struct relax_group_list_t *next;
13294
  struct relax_group_list_t *next_sibling;
13295
  int id;
13296
} relax_group_list_t;
13297
13298
int
13299
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13300
13301
int
13302
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13303
13304
void
13305
dump_chain (relax_group_list_t *pHead);
13306
13307
int
13308
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13309
0
{
13310
0
  relax_group_list_t *pNext = pHead;
13311
13312
  /* Find place.  */
13313
0
  while (pNext->next)
13314
0
    {
13315
0
      if (pNext->next->id > (int) pElem->r_addend)
13316
0
  break;
13317
13318
0
      pNext = pNext->next;
13319
0
    }
13320
13321
  /* Insert node.  */
13322
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13323
0
  if (!pNew)
13324
0
    return false;
13325
13326
0
  relax_group_list_t *tmp = pNext->next;
13327
0
  pNext->next = pNew;
13328
13329
0
  pNew->id = pElem->r_addend;
13330
0
  pNew->relo = pElem;
13331
0
  pNew->next = tmp;
13332
0
  pNew->next_sibling = NULL;
13333
13334
0
  return true;
13335
0
}
13336
13337
int
13338
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13339
0
{
13340
0
  relax_group_list_t *pNext = pNode;
13341
13342
  /* Find place.  */
13343
0
  while (pNext->next_sibling)
13344
0
    {
13345
0
      pNext = pNext->next_sibling;
13346
0
    }
13347
13348
  /* Insert node.  */
13349
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13350
0
  if (!pNew)
13351
0
    return false;
13352
13353
0
  relax_group_list_t *tmp = pNext->next_sibling;
13354
0
  pNext->next_sibling = pNew;
13355
13356
0
  pNew->id = -1;
13357
0
  pNew->relo = pElem;
13358
0
  pNew->next = NULL;
13359
0
  pNew->next_sibling = tmp;
13360
13361
0
  return true;
13362
0
}
13363
13364
void
13365
dump_chain (relax_group_list_t *pHead)
13366
0
{
13367
0
  relax_group_list_t *pNext = pHead->next;
13368
0
  while (pNext)
13369
0
    {
13370
0
      printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13371
0
      relax_group_list_t *pNextSib = pNext->next_sibling;
13372
0
      while (pNextSib)
13373
0
  {
13374
0
    printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13375
0
    pNextSib = pNextSib->next_sibling;
13376
0
  }
13377
0
      pNext = pNext->next;
13378
0
      printf("\n");
13379
0
    }
13380
0
}
13381
13382
/* Check R_NDS32_RELAX_GROUP of each section.
13383
   There might be multiple sections in one object file.  */
13384
13385
int
13386
elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13387
0
{
13388
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13389
0
    elf32_nds32_relax_group_ptr (abfd);
13390
13391
0
  int min_id = relax_group_ptr->min_id;
13392
0
  int max_id = relax_group_ptr->max_id;
13393
13394
0
  Elf_Internal_Rela *rel;
13395
0
  Elf_Internal_Rela *relend;
13396
0
  Elf_Internal_Rela *relocs;
13397
0
  enum elf_nds32_reloc_type rtype;
13398
13399
0
  do
13400
0
    {
13401
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13402
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13403
0
            true /* keep_memory  */);
13404
0
      if (relocs == NULL)
13405
0
  break;
13406
13407
      /* Check R_NDS32_RELAX_GROUP.  */
13408
0
      relend = relocs + asec->reloc_count;
13409
0
      for (rel = relocs; rel < relend; rel++)
13410
0
  {
13411
0
    int id;
13412
0
    rtype = ELF32_R_TYPE (rel->r_info);
13413
0
    if (rtype != R_NDS32_RELAX_GROUP)
13414
0
      continue;
13415
13416
0
    id = rel->r_addend;
13417
0
    if (id < min_id)
13418
0
      min_id = id;
13419
0
    else if (id > max_id)
13420
0
      max_id = id;
13421
0
  }
13422
0
    }
13423
0
  while (false);
13424
13425
0
  if (elf_section_data (asec)->relocs != relocs)
13426
0
    free (relocs);
13427
13428
0
  if ((min_id != relax_group_ptr->min_id)
13429
0
      || (max_id != relax_group_ptr->max_id))
13430
0
    {
13431
0
      relax_group_ptr->count = max_id - min_id + 1;
13432
0
      BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13433
0
      relax_group_ptr->min_id = min_id;
13434
0
      BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13435
0
      relax_group_ptr->max_id = max_id;
13436
0
    }
13437
13438
0
  return relax_group_ptr->count;
13439
0
}
13440
13441
/* Reorder RELAX_GROUP ID when command line option '-r' is applied.  */
13442
static struct section_id_list_t *relax_group_section_id_list = NULL;
13443
13444
struct section_id_list_t *
13445
elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13446
0
{
13447
0
  struct section_id_list_t *result = NULL;
13448
0
  struct section_id_list_t *lst = *lst_ptr;
13449
13450
0
  if (NULL == lst)
13451
0
    {
13452
0
      result = (struct section_id_list_t *) calloc
13453
0
  (1, sizeof (struct section_id_list_t));
13454
0
      BFD_ASSERT (result); /* Feed me.  */
13455
0
      result->id = id;
13456
0
      *lst_ptr = result;
13457
0
    }
13458
0
  else
13459
0
    {
13460
0
      struct section_id_list_t *cur = lst;
13461
0
      struct section_id_list_t *prv = NULL;
13462
0
      struct section_id_list_t *sec = NULL;
13463
13464
0
      while (cur)
13465
0
  {
13466
0
    if (cur->id < id)
13467
0
      {
13468
0
        prv = cur;
13469
0
        cur = cur->next;
13470
0
        continue;
13471
0
      }
13472
13473
0
    if (cur->id > id)
13474
0
      {
13475
0
        cur = NULL; /* To insert after prv.  */
13476
0
        sec = cur;  /* In case prv == NULL.  */
13477
0
      }
13478
13479
0
    break;
13480
0
  }
13481
13482
0
      if (NULL == cur)
13483
0
  {
13484
    /* Insert after prv.  */
13485
0
    result = (struct section_id_list_t *) calloc
13486
0
      (1, sizeof (struct section_id_list_t));
13487
0
    BFD_ASSERT (result); /* Feed me.  */
13488
0
    result->id = id;
13489
0
    if (NULL != prv)
13490
0
      {
13491
0
        result->next = prv->next;
13492
0
        prv->next = result;
13493
0
      }
13494
0
    else
13495
0
      {
13496
0
        *lst_ptr = result;
13497
0
        result->next = sec;
13498
0
      }
13499
0
  }
13500
0
    }
13501
13502
0
  return result;
13503
0
}
13504
13505
int
13506
elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13507
0
{
13508
0
  static int next_relax_group_bias = 0;
13509
13510
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13511
0
    elf32_nds32_relax_group_ptr (abfd);
13512
13513
0
  bool result = true;
13514
0
  Elf_Internal_Rela *rel;
13515
0
  Elf_Internal_Rela *relend;
13516
0
  Elf_Internal_Rela *relocs = NULL;
13517
0
  enum elf_nds32_reloc_type rtype;
13518
0
  struct section_id_list_t *node = NULL;
13519
13520
0
  do
13521
0
    {
13522
0
      if (0 == relax_group_ptr->count)
13523
0
  break;
13524
13525
      /* Check if this section has been handled.  */
13526
0
      node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13527
0
      if (NULL == node)
13528
0
  break; /* Hit, the section id has handled.  */
13529
13530
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13531
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13532
0
            true /* keep_memory  */);
13533
0
      if (relocs == NULL)
13534
0
  {
13535
0
    BFD_ASSERT (0); /* feed me */
13536
0
    break;
13537
0
  }
13538
13539
      /* Allocate group id bias for this bfd!  */
13540
0
      if (0 == relax_group_ptr->init)
13541
0
  {
13542
0
    relax_group_ptr->bias = next_relax_group_bias;
13543
0
    next_relax_group_bias += relax_group_ptr->count;
13544
0
    relax_group_ptr->init = 1;
13545
0
  }
13546
13547
      /* Reorder relax group groups.  */
13548
0
      relend = relocs + asec->reloc_count;
13549
0
      for (rel = relocs; rel < relend; rel++)
13550
0
  {
13551
0
    rtype = ELF32_R_TYPE(rel->r_info);
13552
0
    if (rtype != R_NDS32_RELAX_GROUP)
13553
0
      continue;
13554
13555
    /* Change it.  */
13556
0
    rel->r_addend += relax_group_ptr->bias;
13557
0
  }
13558
0
    }
13559
0
  while (false);
13560
13561
0
  if (elf_section_data (asec)->relocs != relocs)
13562
0
    free (relocs);
13563
13564
0
  return result;
13565
0
}
13566
13567
int
13568
nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13569
         struct bfd_link_info *lnkinfo)
13570
0
{
13571
0
  bool result = true;
13572
0
  Elf_Internal_Rela *irel;
13573
0
  Elf_Internal_Rela *irelend;
13574
0
  Elf_Internal_Rela *internal_relocs;
13575
0
  unsigned long r_symndx;
13576
0
  enum elf_nds32_reloc_type r_type;
13577
13578
0
  Elf_Internal_Sym *local_syms = NULL;
13579
0
  bfd_byte *contents = NULL;
13580
13581
0
  relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13582
13583
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (inbfd);
13584
0
  struct elf_link_hash_entry **sym_hashes;
13585
0
  sym_hashes = elf_sym_hashes (inbfd);
13586
13587
  /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
13588
0
  if (bfd_link_relocatable (lnkinfo))
13589
0
    {
13590
0
      elf32_nds32_unify_relax_group (inbfd, insec);
13591
0
      return result;
13592
0
    }
13593
13594
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13595
0
  internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13596
0
                 true /* keep_memory  */);
13597
0
  if (internal_relocs == NULL)
13598
0
    goto error_return;
13599
13600
0
  irelend = internal_relocs + insec->reloc_count;
13601
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
13602
0
         irelend, R_NDS32_RELAX_ENTRY);
13603
0
  if (irel == irelend)
13604
0
    goto finish;
13605
13606
  /* Chain/remove groups.  */
13607
0
  for (irel = internal_relocs; irel < irelend; irel++)
13608
0
    {
13609
0
      r_symndx = ELF32_R_SYM (irel->r_info);
13610
0
      r_type = ELF32_R_TYPE (irel->r_info);
13611
0
      if (r_type != R_NDS32_RELAX_GROUP)
13612
0
  continue;
13613
13614
      /* Remove it.  */
13615
0
      irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13616
      /* Chain it now.  */
13617
0
      if (!list_insert (&chain, irel))
13618
0
  goto error_return;
13619
0
    }
13620
13621
  /* Collect group relocations.  */
13622
  /* Presume relocations are sorted.  */
13623
0
  relax_group_list_t *pNext = chain.next;
13624
0
  while (pNext)
13625
0
    {
13626
0
      for (irel = internal_relocs; irel < irelend; irel++)
13627
0
  {
13628
0
    if (irel->r_offset == pNext->relo->r_offset)
13629
0
      {
13630
        /* Ignore Non-TLS relocation types.  */
13631
0
        r_type = ELF32_R_TYPE (irel->r_info);
13632
0
        if ((R_NDS32_TLS_LE_HI20 > r_type)
13633
0
      || (R_NDS32_RELAX_ENTRY == r_type))
13634
0
    continue;
13635
13636
0
        if (!list_insert_sibling (pNext, irel))
13637
0
    goto error_return;
13638
0
      }
13639
0
    else if (irel->r_offset > pNext->relo->r_offset)
13640
0
      {
13641
0
        pNext = pNext->next;
13642
0
        if (!pNext)
13643
0
    break;
13644
13645
0
        bfd_vma current_offset = pNext->relo->r_offset;
13646
0
        if (irel->r_offset > current_offset)
13647
0
    irel = internal_relocs; /* restart from head */
13648
0
        else
13649
0
    --irel; /* Check current irel again.  */
13650
0
        continue;
13651
0
      }
13652
0
    else
13653
0
      {
13654
        /* This shouldn't be reached.  */
13655
0
      }
13656
0
  }
13657
0
      if (pNext)
13658
0
  pNext = pNext->next;
13659
0
    }
13660
13661
#ifdef DUBUG_VERBOSE
13662
  dump_chain(&chain);
13663
#endif
13664
13665
  /* Get symbol table and section content.  */
13666
0
  if (incontents)
13667
0
    contents = incontents;
13668
0
  else if (!nds32_get_section_contents (inbfd, insec, &contents, true)
13669
0
     || !nds32_get_local_syms (inbfd, insec, &local_syms))
13670
0
    goto error_return;
13671
13672
0
  char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13673
13674
  /* Convert TLS model each group if necessary.  */
13675
0
  pNext = chain.next;
13676
13677
0
  int cur_grp_id = -1;
13678
0
  int sethi_rt = -1;
13679
0
  int add_rt = -1;
13680
0
  enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13681
13682
0
  tls_type = org_tls_type = eff_tls_type = 0;
13683
13684
0
  while (pNext)
13685
0
    {
13686
0
      relax_group_list_t *pNextSig = pNext->next_sibling;
13687
0
      while (pNextSig)
13688
0
  {
13689
0
    struct elf_link_hash_entry *h = NULL;
13690
13691
0
    irel = pNextSig->relo;
13692
0
    r_symndx = ELF32_R_SYM(irel->r_info);
13693
0
    r_type = ELF32_R_TYPE(irel->r_info);
13694
13695
0
    if (pNext->id != cur_grp_id)
13696
0
      {
13697
0
        cur_grp_id = pNext->id;
13698
0
        org_tls_type = get_tls_type (r_type, NULL);
13699
0
        if (r_symndx >= symtab_hdr->sh_info)
13700
0
    {
13701
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13702
0
      while (h->root.type == bfd_link_hash_indirect
13703
0
       || h->root.type == bfd_link_hash_warning)
13704
0
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
13705
0
      tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13706
0
    }
13707
0
        else
13708
0
    {
13709
0
      tls_type = local_got_tls_type
13710
0
        ? local_got_tls_type[r_symndx]
13711
0
        : GOT_NORMAL;
13712
0
    }
13713
13714
0
        eff_tls_type = 1 << (fls (tls_type) - 1);
13715
0
        sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13716
0
      }
13717
13718
0
    if (eff_tls_type != org_tls_type)
13719
0
      {
13720
0
        switch (org_tls_type)
13721
0
    {
13722
      /* DESC to IEGP/IE/LE.  */
13723
0
    case GOT_TLS_DESC:
13724
0
      switch (eff_tls_type)
13725
0
        {
13726
0
        case GOT_TLS_IE:
13727
0
          switch (r_type)
13728
0
      {
13729
0
      case R_NDS32_TLS_DESC_HI20:
13730
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13731
0
                  R_NDS32_TLS_IE_HI20);
13732
0
        break;
13733
0
      case R_NDS32_TLS_DESC_LO12:
13734
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13735
0
                  R_NDS32_TLS_IE_LO12);
13736
0
        break;
13737
0
      case R_NDS32_TLS_DESC_ADD:
13738
0
        {
13739
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13740
0
          add_rt = N32_RT5 (insn);
13741
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13742
0
          bfd_putb32 (insn, contents + irel->r_offset);
13743
13744
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13745
0
        }
13746
0
        break;
13747
0
      case R_NDS32_TLS_DESC_FUNC:
13748
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13749
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13750
0
                  R_NDS32_RELAX_REMOVE);
13751
0
        break;
13752
0
      case R_NDS32_TLS_DESC_CALL:
13753
0
        {
13754
0
          uint32_t insn = N32_ALU1(ADD, N32_REG_R0, add_rt,
13755
0
                 N32_REG_TP);
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_LOADSTORE:
13762
0
      case R_NDS32_PTR:
13763
0
      case R_NDS32_PTR_RESOLVED:
13764
0
      case R_NDS32_NONE:
13765
0
      case R_NDS32_LABEL:
13766
0
        break;
13767
0
      default:
13768
0
        BFD_ASSERT(0);
13769
0
        break;
13770
0
      }
13771
0
          break;
13772
0
        case GOT_TLS_IEGP:
13773
0
          switch (r_type)
13774
0
      {
13775
0
      case R_NDS32_TLS_DESC_HI20:
13776
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13777
0
                  R_NDS32_TLS_IEGP_HI20);
13778
0
        break;
13779
0
      case R_NDS32_TLS_DESC_LO12:
13780
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13781
0
                  R_NDS32_TLS_IEGP_LO12);
13782
0
        break;
13783
0
      case R_NDS32_TLS_DESC_ADD:
13784
0
        {
13785
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13786
0
          add_rt = N32_RT5 (insn);
13787
0
          insn = N32_MEM(LW, add_rt, sethi_rt,
13788
0
             N32_REG_GP, 0);
13789
0
          bfd_putb32 (insn, contents + irel->r_offset);
13790
13791
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13792
0
        }
13793
0
        break;
13794
0
      case R_NDS32_TLS_DESC_FUNC:
13795
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13796
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13797
0
                  R_NDS32_RELAX_REMOVE);
13798
0
        break;
13799
0
      case R_NDS32_TLS_DESC_CALL:
13800
0
        {
13801
0
          uint32_t insn = N32_ALU1(ADD, N32_REG_R0, add_rt,
13802
0
                 N32_REG_TP);
13803
0
          bfd_putb32 (insn, contents + irel->r_offset);
13804
13805
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13806
0
        }
13807
0
        break;
13808
0
      case R_NDS32_LOADSTORE:
13809
0
      case R_NDS32_PTR:
13810
0
      case R_NDS32_PTR_RESOLVED:
13811
0
      case R_NDS32_NONE:
13812
0
      case R_NDS32_LABEL:
13813
0
        break;
13814
0
      default:
13815
0
        BFD_ASSERT(0);
13816
0
        break;
13817
0
      }
13818
0
          break;
13819
0
        case GOT_TLS_LE:
13820
0
          switch (r_type)
13821
0
      {
13822
0
      case R_NDS32_TLS_DESC_HI20:
13823
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13824
0
        break;
13825
0
      case R_NDS32_TLS_DESC_LO12:
13826
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13827
0
        break;
13828
0
      case R_NDS32_TLS_DESC_ADD:
13829
0
        {
13830
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13831
13832
0
          add_rt = N32_RT5 (insn);
13833
0
          insn = N32_ALU1 (ADD, N32_REG_R0, sethi_rt,
13834
0
               N32_REG_TP);
13835
0
          bfd_putb32 (insn, contents + irel->r_offset);
13836
13837
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13838
0
        }
13839
0
        break;
13840
0
      case R_NDS32_TLS_DESC_FUNC:
13841
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13842
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13843
0
        break;
13844
0
      case R_NDS32_TLS_DESC_CALL:
13845
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13846
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13847
0
        break;
13848
0
      case R_NDS32_LOADSTORE:
13849
0
      case R_NDS32_PTR:
13850
0
      case R_NDS32_PTR_RESOLVED:
13851
0
      case R_NDS32_NONE:
13852
0
      case R_NDS32_LABEL:
13853
0
        break;
13854
0
      default:
13855
0
        BFD_ASSERT(0);
13856
0
        break;
13857
0
      }
13858
0
          break;
13859
0
        default:
13860
0
          break;
13861
0
        }
13862
0
      break;
13863
      /* IEGP to IE/LE.  */
13864
0
    case GOT_TLS_IEGP:
13865
0
      switch (eff_tls_type)
13866
0
        {
13867
0
        case GOT_TLS_IE:
13868
0
          switch (r_type)
13869
0
      {
13870
0
      case R_NDS32_TLS_IEGP_HI20:
13871
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13872
0
                  R_NDS32_TLS_IE_HI20);
13873
0
        break;
13874
0
      case R_NDS32_TLS_IEGP_LO12:
13875
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13876
0
                  R_NDS32_TLS_IE_LO12);
13877
0
        break;
13878
0
      case R_NDS32_PTR_RESOLVED:
13879
0
        {
13880
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13881
13882
0
          add_rt = N32_RT5 (insn);
13883
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13884
0
          bfd_putb32 (insn, contents + irel->r_offset);
13885
0
        }
13886
0
        break;
13887
0
      case R_NDS32_TLS_IEGP_LW:
13888
0
        break;
13889
0
      case R_NDS32_LOADSTORE:
13890
0
      case R_NDS32_PTR:
13891
0
      case R_NDS32_NONE:
13892
0
      case R_NDS32_LABEL:
13893
0
        break;
13894
0
      default:
13895
0
        BFD_ASSERT(0);
13896
0
        break;
13897
0
      }
13898
0
          break;
13899
0
        case GOT_TLS_LE:
13900
0
          switch (r_type)
13901
0
      {
13902
0
      case R_NDS32_TLS_IEGP_HI20:
13903
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13904
0
        break;
13905
0
      case R_NDS32_TLS_IEGP_LO12:
13906
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13907
0
        break;
13908
0
      case R_NDS32_TLS_IEGP_LW:
13909
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13910
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13911
0
        break;
13912
0
      case R_NDS32_LOADSTORE:
13913
0
      case R_NDS32_PTR:
13914
0
      case R_NDS32_NONE:
13915
0
      case R_NDS32_LABEL:
13916
0
      case R_NDS32_PTR_RESOLVED:
13917
0
        break;
13918
0
      default:
13919
0
        BFD_ASSERT(0);
13920
0
        break;
13921
0
      }
13922
0
          break;
13923
0
        default:
13924
0
          break;
13925
0
        }
13926
0
      break;
13927
      /* IE to LE. */
13928
0
    case GOT_TLS_IE:
13929
0
      switch (eff_tls_type)
13930
0
        {
13931
0
        case GOT_TLS_LE:
13932
0
          switch (r_type)
13933
0
      {
13934
0
      case R_NDS32_TLS_IE_HI20:
13935
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13936
0
        break;
13937
0
      case R_NDS32_TLS_IE_LO12S2:
13938
0
        {
13939
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13940
13941
0
          add_rt = N32_RT5 (insn);
13942
0
          insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13943
0
          bfd_putb32 (insn, contents + irel->r_offset);
13944
13945
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13946
0
        }
13947
0
        break;
13948
0
      case R_NDS32_LOADSTORE:
13949
0
      case R_NDS32_PTR:
13950
0
      case R_NDS32_NONE:
13951
0
      case R_NDS32_LABEL:
13952
0
        break;
13953
0
      default:
13954
0
        BFD_ASSERT(0);
13955
0
        break;
13956
0
      }
13957
0
          break;
13958
0
        default:
13959
0
          break;
13960
0
        }
13961
0
      break;
13962
0
    default:
13963
0
      break;
13964
0
    }
13965
0
      }
13966
0
    pNextSig = pNextSig->next_sibling;
13967
0
  }
13968
13969
0
#if 1
13970
0
      pNext = pNext->next;
13971
#else
13972
      while (pNext)
13973
  {
13974
    if (pNext->id != cur_grp_id)
13975
      break;
13976
    pNext = pNext->next;
13977
  }
13978
#endif
13979
0
    }
13980
13981
0
 finish:
13982
0
  if (incontents)
13983
0
    contents = NULL;
13984
13985
0
  if (elf_section_data (insec)->relocs != internal_relocs)
13986
0
    free (internal_relocs);
13987
13988
0
  if (elf_section_data (insec)->this_hdr.contents != contents)
13989
0
    free (contents);
13990
13991
0
  if (symtab_hdr->contents != (bfd_byte *) local_syms)
13992
0
    free (local_syms);
13993
13994
0
  if (chain.next)
13995
0
    {
13996
0
      pNext = chain.next;
13997
0
      relax_group_list_t *pDel;
13998
0
      while (pNext)
13999
0
  {
14000
0
    pDel = pNext;
14001
0
    pNext = pNext->next;
14002
0
    free (pDel);
14003
0
  }
14004
0
    }
14005
14006
0
  return result;
14007
14008
0
 error_return:
14009
  result = false;
14010
0
  goto finish;
14011
0
}
14012
14013
/* End TLS model conversion.  */
14014
14015
#define ELF_ARCH        bfd_arch_nds32
14016
#define ELF_MACHINE_CODE      EM_NDS32
14017
#define ELF_MAXPAGESIZE       0x1000
14018
#define ELF_TARGET_ID       NDS32_ELF_DATA
14019
14020
#define TARGET_BIG_SYM        nds32_elf32_be_vec
14021
#define TARGET_BIG_NAME       "elf32-nds32be"
14022
#define TARGET_LITTLE_SYM     nds32_elf32_le_vec
14023
#define TARGET_LITTLE_NAME      "elf32-nds32le"
14024
14025
#define elf_info_to_howto     nds32_info_to_howto
14026
#define elf_info_to_howto_rel     nds32_info_to_howto_rel
14027
14028
#define bfd_elf32_bfd_link_hash_table_create  nds32_elf_link_hash_table_create
14029
#define bfd_elf32_bfd_merge_private_bfd_data  nds32_elf_merge_private_bfd_data
14030
#define bfd_elf32_bfd_print_private_bfd_data  nds32_elf_print_private_bfd_data
14031
#define bfd_elf32_bfd_relax_section   nds32_elf_relax_section
14032
#define bfd_elf32_bfd_set_private_flags   nds32_elf_set_private_flags
14033
14034
#define bfd_elf32_mkobject      nds32_elf_mkobject
14035
#define bfd_elf32_new_section_hook    nds32_elf_new_section_hook
14036
#define bfd_elf32_bfd_free_cached_info    nds32_elf_free_cached_info
14037
#define elf_backend_action_discarded    nds32_elf_action_discarded
14038
#define elf_backend_add_symbol_hook   nds32_elf_add_symbol_hook
14039
#define elf_backend_check_relocs    nds32_elf_check_relocs
14040
#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14041
#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14042
#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14043
#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14044
#define elf_backend_late_size_sections    nds32_elf_late_size_sections
14045
#define elf_backend_relocate_section    nds32_elf_relocate_section
14046
#define elf_backend_gc_mark_hook    nds32_elf_gc_mark_hook
14047
#define elf_backend_grok_prstatus   nds32_elf_grok_prstatus
14048
#define elf_backend_grok_psinfo     nds32_elf_grok_psinfo
14049
#define elf_backend_reloc_type_class    nds32_elf_reloc_type_class
14050
#define elf_backend_copy_indirect_symbol  nds32_elf_copy_indirect_symbol
14051
#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14052
#define elf_backend_output_arch_syms    nds32_elf_output_arch_syms
14053
#define elf_backend_object_p      nds32_elf_object_p
14054
#define elf_backend_final_write_processing  nds32_elf_final_write_processing
14055
#define elf_backend_special_sections    nds32_elf_special_sections
14056
#define elf_backend_section_flags   nds32_elf_section_flags
14057
#define bfd_elf32_bfd_get_relocated_section_contents \
14058
        nds32_elf_get_relocated_section_contents
14059
#define bfd_elf32_bfd_is_target_special_symbol  nds32_elf_is_target_special_symbol
14060
#define elf_backend_maybe_function_sym    nds32_elf_maybe_function_sym
14061
14062
#define elf_backend_can_gc_sections   1
14063
#define elf_backend_can_refcount    1
14064
#define elf_backend_want_got_plt    1
14065
#define elf_backend_plt_readonly    1
14066
#define elf_backend_want_plt_sym    0
14067
#define elf_backend_got_header_size   12
14068
#define elf_backend_may_use_rel_p   1
14069
#define elf_backend_default_use_rela_p    1
14070
#define elf_backend_may_use_rela_p    1
14071
#define elf_backend_dtrel_excludes_plt    0
14072
14073
#include "elf32-target.h"
14074
14075
#undef ELF_MAXPAGESIZE
14076
#define ELF_MAXPAGESIZE       0x2000
14077
14078
#undef  TARGET_BIG_SYM
14079
#define TARGET_BIG_SYM        nds32_elf32_linux_be_vec
14080
#undef  TARGET_BIG_NAME
14081
#define TARGET_BIG_NAME       "elf32-nds32be-linux"
14082
#undef  TARGET_LITTLE_SYM
14083
#define TARGET_LITTLE_SYM     nds32_elf32_linux_le_vec
14084
#undef  TARGET_LITTLE_NAME
14085
#define TARGET_LITTLE_NAME      "elf32-nds32le-linux"
14086
#undef  elf32_bed
14087
#define elf32_bed       elf32_nds32_lin_bed
14088
14089
#include "elf32-target.h"