Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-nds32.c
Line
Count
Source
1
/* NDS32-specific support for 32-bit ELF.
2
   Copyright (C) 2012-2026 Free Software Foundation, Inc.
3
   Contributed by Andes Technology Corporation.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
21
22
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#include "elf-bfd.h"
28
#include "libiberty.h"
29
#include "elf/nds32.h"
30
#include "opcode/nds32.h"
31
#include "elf32-nds32.h"
32
#include "opcode/cgen.h"
33
#include "../opcodes/nds32-opc.h"
34
35
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
36
0
#define OCTETS_PER_BYTE(ABFD, SEC) 1
37
38
/* Relocation HOWTO functions.  */
39
static bfd_reloc_status_type nds32_elf_ignore_reloc
40
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41
static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43
static bfd_reloc_status_type nds32_elf_hi20_reloc
44
  (bfd *, arelent *, asymbol *, void *,
45
   asection *, bfd *, char **);
46
static bfd_reloc_status_type nds32_elf_lo12_reloc
47
  (bfd *, arelent *, asymbol *, void *,
48
   asection *, bfd *, char **);
49
static bfd_reloc_status_type nds32_elf_generic_reloc
50
  (bfd *, arelent *, asymbol *, void *,
51
   asection *, bfd *, char **);
52
static bfd_reloc_status_type nds32_elf_sda15_reloc
53
  (bfd *, arelent *, asymbol *, void *,
54
   asection *, bfd *, char **);
55
56
/* Helper functions for HOWTO.  */
57
static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
58
  (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
59
   asection *, bfd_vma, bfd_vma);
60
61
/* Nds32 helper functions.  */
62
static bfd_vma calculate_memory_address
63
  (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
64
static int nds32_get_section_contents (bfd *, asection *,
65
               bfd_byte **, bool);
66
static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67
         Elf_Internal_Sym **);
68
static bool  nds32_relax_fp_as_gp
69
  (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
70
   Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
71
   Elf_Internal_Sym *isymbuf);
72
static bool nds32_fag_remove_unused_fpbase
73
  (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74
   Elf_Internal_Rela *irelend);
75
76
enum
77
{
78
  MACH_V1 = bfd_mach_n1h,
79
  MACH_V2 = bfd_mach_n1h_v2,
80
  MACH_V3 = bfd_mach_n1h_v3,
81
  MACH_V3M = bfd_mach_n1h_v3m
82
};
83
84
#define MIN(a, b) ((a) > (b) ? (b) : (a))
85
#define MAX(a, b) ((a) > (b) ? (a) : (b))
86
87
/* The name of the dynamic interpreter.  This is put in the .interp
88
   section.  */
89
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
90
91
0
#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92
0
          && (flags) & SEC_LOAD \
93
0
          && (flags) & SEC_READONLY)
94
95
/* The nop opcode we use.  */
96
0
#define NDS32_NOP32 0x40000009
97
0
#define NDS32_NOP16 0x9200
98
99
/* The size in bytes of an entry in the procedure linkage table.  */
100
0
#define PLT_ENTRY_SIZE 24
101
#define PLT_HEADER_SIZE 24
102
103
/* The first entry in a procedure linkage table are reserved,
104
   and the initial contents are unimportant (we zero them out).
105
   Subsequent entries look like this.  */
106
0
#define PLT0_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(.got+4)    */
107
0
#define PLT0_ENTRY_WORD1  0x58f78000    /* ori     r15, r25, LO12(.got+4) */
108
0
#define PLT0_ENTRY_WORD2  0x05178000    /* lwi     r17, [r15+0]     */
109
0
#define PLT0_ENTRY_WORD3  0x04f78001    /* lwi     r15, [r15+4]     */
110
0
#define PLT0_ENTRY_WORD4  0x4a003c00    /* jr    r15        */
111
112
/* $ta is change to $r15 (from $r25).  */
113
0
#define PLT0_PIC_ENTRY_WORD0  0x46f00000  /* sethi   r15, HI20(got[1]@GOT)  */
114
0
#define PLT0_PIC_ENTRY_WORD1  0x58f78000  /* ori     r15, r15, LO12(got[1]@GOT) */
115
0
#define PLT0_PIC_ENTRY_WORD2  0x40f7f400  /* add     r15, gp, r15     */
116
0
#define PLT0_PIC_ENTRY_WORD3  0x05178000  /* lwi     r17, [r15+0]     */
117
0
#define PLT0_PIC_ENTRY_WORD4  0x04f78001  /* lwi     r15, [r15+4]     */
118
0
#define PLT0_PIC_ENTRY_WORD5  0x4a003c00  /* jr    r15        */
119
120
0
#define PLT_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(&got[n+3])      */
121
0
#define PLT_ENTRY_WORD1  0x04f78000    /* lwi     r15, r15, LO12(&got[n+3]) */
122
0
#define PLT_ENTRY_WORD2  0x4a003c00    /* jr    r15           */
123
0
#define PLT_ENTRY_WORD3  0x45000000    /* movi    r16, sizeof(RELA) * n     */
124
0
#define PLT_ENTRY_WORD4  0x48000000    /* j    .plt0.         */
125
126
0
#define PLT_PIC_ENTRY_WORD0  0x46f00000    /* sethi  r15, HI20(got[n+3]@GOT)    */
127
0
#define PLT_PIC_ENTRY_WORD1  0x58f78000    /* ori    r15, r15,    LO12(got[n+3]@GOT) */
128
0
#define PLT_PIC_ENTRY_WORD2  0x38febc02    /* lw   r15, [gp+r15]        */
129
0
#define PLT_PIC_ENTRY_WORD3  0x4a003c00    /* jr   r15          */
130
0
#define PLT_PIC_ENTRY_WORD4  0x45000000    /* movi   r16, sizeof(RELA) * n      */
131
0
#define PLT_PIC_ENTRY_WORD5  0x48000000    /* j    .plt0          */
132
133
/* These are macros used to get the relocation accurate value.  */
134
0
#define ACCURATE_8BIT_S1  (0x100)
135
0
#define ACCURATE_U9BIT_S1 (0x400)
136
#define ACCURATE_12BIT_S1 (0x2000)
137
0
#define ACCURATE_14BIT_S1 (0x4000)
138
#define ACCURATE_19BIT    (0x40000)
139
140
/* These are macros used to get the relocation conservative value.  */
141
0
#define CONSERVATIVE_8BIT_S1  (0x100 - 4)
142
0
#define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143
0
#define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144
0
#define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
145
/* These must be more conservative because the address may be in
146
   different segment.  */
147
#define CONSERVATIVE_15BIT  (0x4000 - 0x1000)
148
#define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
149
#define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
150
#define CONSERVATIVE_19BIT  (0x40000 - 0x1000)
151
0
#define CONSERVATIVE_20BIT  (0x80000 - 0x1000)
152
153
/* Size of small data/bss sections, used to calculate SDA_BASE.  */
154
static long got_size = 0;
155
static int is_SDA_BASE_set = 0;
156
157
/* Convert ELF-VER in eflags to string for debugging purpose.  */
158
static const char *const nds32_elfver_strtab[] =
159
{
160
  "ELF-1.2",
161
  "ELF-1.3",
162
  "ELF-1.4",
163
};
164
165
/* The nds32 linker needs to keep track of the number of relocs that it
166
   decides to copy in check_relocs for each symbol.  This is so that
167
   it can discard PC relative relocs if it doesn't need them when
168
   linking with -Bsymbolic.  We store the information in a field
169
   extending the regular ELF linker hash table.  */
170
171
/* This structure keeps track of the number of PC relative relocs we
172
   have copied for a given symbol.  */
173
174
struct elf_nds32_pcrel_relocs_copied
175
{
176
  /* Next section.  */
177
  struct elf_nds32_pcrel_relocs_copied *next;
178
  /* A section in dynobj.  */
179
  asection *section;
180
  /* Number of relocs copied in this section.  */
181
  bfd_size_type count;
182
};
183
184
enum elf_nds32_tls_type
185
{
186
  GOT_UNKNOWN = (0),
187
  GOT_NORMAL = (1 << 0),
188
  GOT_TLS_LE = (1 << 1),
189
  GOT_TLS_IE = (1 << 2),
190
  GOT_TLS_IEGP = (1 << 3),
191
  GOT_TLS_LD = (1 << 4),
192
  GOT_TLS_GD = (1 << 5),
193
  GOT_TLS_DESC = (1 << 6),
194
};
195
196
/* Nds32 ELF linker hash entry.  */
197
198
struct elf_nds32_link_hash_entry
199
{
200
  struct elf_link_hash_entry root;
201
202
  /* For checking relocation type.  */
203
  enum elf_nds32_tls_type tls_type;
204
205
  int offset_to_gp;
206
};
207
208
/* Get the nds32 ELF linker hash table from a link_info structure.  */
209
210
0
#define FP_BASE_NAME "_FP_BASE_"
211
static int check_start_export_sym = 0;
212
213
/* The offset for executable tls relaxation.  */
214
0
#define TP_OFFSET 0x0
215
216
typedef struct
217
{
218
  int min_id;
219
  int max_id;
220
  int count;
221
  int bias;
222
  int init;
223
} elf32_nds32_relax_group_t;
224
225
struct elf_nds32_obj_tdata
226
{
227
  struct elf_obj_tdata root;
228
229
  /* tls_type for each local got entry.  */
230
  char *local_got_tls_type;
231
232
  /* GOTPLT entries for TLS descriptors.  */
233
  bfd_vma *local_tlsdesc_gotent;
234
235
  /* for R_NDS32_RELAX_GROUP handling.  */
236
  elf32_nds32_relax_group_t relax_group;
237
238
  unsigned int hdr_size;
239
  int* offset_to_gp;
240
};
241
242
#define elf_nds32_tdata(bfd) \
243
0
  ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
244
245
#define elf32_nds32_local_got_tls_type(bfd) \
246
0
  (elf_nds32_tdata (bfd)->local_got_tls_type)
247
248
#define elf32_nds32_local_gp_offset(bfd) \
249
0
  (elf_nds32_tdata (bfd)->offset_to_gp)
250
251
0
#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
252
253
#define elf32_nds32_relax_group_ptr(bfd) \
254
0
  &(elf_nds32_tdata (bfd)->relax_group)
255
256
static bool
257
nds32_elf_mkobject (bfd *abfd)
258
57.6k
{
259
57.6k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata));
260
57.6k
}
261
262
/* Relocations used for relocation.  */
263
/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
264
   initialize array nds32_elf_howto_table in any order. The benefit
265
   is that we can add any new relocations with any numbers and don't
266
   need to fill the gap by lots of EMPTY_HOWTO. */
267
#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
268
  [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
269
270
static reloc_howto_type nds32_elf_howto_table[] =
271
{
272
  /* This reloc does nothing.  */
273
  HOWTO2 (R_NDS32_NONE,   /* type  */
274
   0,     /* rightshift  */
275
   4,     /* size  */
276
   32,      /* bitsize  */
277
   false,     /* pc_relative  */
278
   0,     /* bitpos  */
279
   complain_overflow_bitfield,/* complain_on_overflow  */
280
   bfd_elf_generic_reloc, /* special_function  */
281
   "R_NDS32_NONE",  /* name  */
282
   false,     /* partial_inplace  */
283
   0,     /* src_mask  */
284
   0,     /* dst_mask  */
285
   false),    /* pcrel_offset  */
286
287
  /* A 16 bit absolute relocation.  */
288
  HOWTO2 (R_NDS32_16,   /* type  */
289
   0,     /* rightshift  */
290
   2,     /* size  */
291
   16,      /* bitsize  */
292
   false,     /* pc_relative  */
293
   0,     /* bitpos  */
294
   complain_overflow_bitfield,/* complain_on_overflow  */
295
   nds32_elf_generic_reloc,/* special_function  */
296
   "R_NDS32_16",    /* name  */
297
   false,     /* partial_inplace  */
298
   0xffff,    /* src_mask  */
299
   0xffff,    /* dst_mask  */
300
   false),    /* pcrel_offset  */
301
302
  /* A 32 bit absolute relocation.  */
303
  HOWTO2 (R_NDS32_32,   /* type  */
304
   0,     /* rightshift  */
305
   4,     /* size  */
306
   32,      /* bitsize  */
307
   false,     /* pc_relative  */
308
   0,     /* bitpos  */
309
   complain_overflow_bitfield,/* complain_on_overflow  */
310
   nds32_elf_generic_reloc,/* special_function  */
311
   "R_NDS32_32",    /* name  */
312
   false,     /* partial_inplace  */
313
   0xffffffff,    /* src_mask  */
314
   0xffffffff,    /* dst_mask  */
315
   false),    /* pcrel_offset  */
316
317
  /* A 20 bit address.  */
318
  HOWTO2 (R_NDS32_20,   /* type  */
319
   0,     /* rightshift  */
320
   4,     /* size  */
321
   20,      /* bitsize  */
322
   false,     /* pc_relative  */
323
   0,     /* bitpos  */
324
   complain_overflow_unsigned,/* complain_on_overflow  */
325
   nds32_elf_generic_reloc,/* special_function  */
326
   "R_NDS32_20",    /* name  */
327
   false,     /* partial_inplace  */
328
   0xfffff,   /* src_mask  */
329
   0xfffff,   /* dst_mask  */
330
   false),    /* pcrel_offset  */
331
332
  /* An PC Relative 9-bit relocation, shifted by 2.
333
     This reloc is complicated because relocations are relative to pc & -4.
334
     i.e. branches in the right insn slot use the address of the left insn
335
     slot for pc.  */
336
  /* It's not clear whether this should have partial_inplace set or not.
337
     Branch relaxing in the assembler can store the addend in the insn,
338
     and if bfd_install_relocation gets called the addend may get added
339
     again.  */
340
  HOWTO2 (R_NDS32_9_PCREL,  /* type  */
341
   1,     /* rightshift  */
342
   2,     /* size  */
343
   8,     /* bitsize  */
344
   true,      /* pc_relative  */
345
   0,     /* bitpos  */
346
   complain_overflow_signed,/* complain_on_overflow  */
347
   nds32_elf_9_pcrel_reloc,/* special_function  */
348
   "R_NDS32_9_PCREL", /* name  */
349
   false,     /* partial_inplace  */
350
   0xff,      /* src_mask  */
351
   0xff,      /* dst_mask  */
352
   true),     /* pcrel_offset  */
353
354
  /* A relative 15 bit relocation, right shifted by 1.  */
355
  HOWTO2 (R_NDS32_15_PCREL, /* type  */
356
   1,     /* rightshift  */
357
   4,     /* size  */
358
   14,      /* bitsize  */
359
   true,      /* pc_relative  */
360
   0,     /* bitpos  */
361
   complain_overflow_signed,/* complain_on_overflow  */
362
   bfd_elf_generic_reloc, /* special_function  */
363
   "R_NDS32_15_PCREL",  /* name  */
364
   false,     /* partial_inplace  */
365
   0x3fff,    /* src_mask  */
366
   0x3fff,    /* dst_mask  */
367
   true),     /* pcrel_offset  */
368
369
  /* A relative 17 bit relocation, right shifted by 1.  */
370
  HOWTO2 (R_NDS32_17_PCREL, /* type  */
371
   1,     /* rightshift  */
372
   4,     /* size  */
373
   16,      /* bitsize  */
374
   true,      /* pc_relative  */
375
   0,     /* bitpos  */
376
   complain_overflow_signed,/* complain_on_overflow  */
377
   bfd_elf_generic_reloc, /* special_function  */
378
   "R_NDS32_17_PCREL",  /* name  */
379
   false,     /* partial_inplace  */
380
   0xffff,    /* src_mask  */
381
   0xffff,    /* dst_mask  */
382
   true),     /* pcrel_offset  */
383
384
  /* A relative 25 bit relocation, right shifted by 1.  */
385
  /* It's not clear whether this should have partial_inplace set or not.
386
     Branch relaxing in the assembler can store the addend in the insn,
387
     and if bfd_install_relocation gets called the addend may get added
388
     again.  */
389
  HOWTO2 (R_NDS32_25_PCREL, /* type  */
390
   1,     /* rightshift  */
391
   4,     /* size  */
392
   24,      /* bitsize  */
393
   true,      /* pc_relative  */
394
   0,     /* bitpos  */
395
   complain_overflow_signed,/* complain_on_overflow  */
396
   bfd_elf_generic_reloc, /* special_function  */
397
   "R_NDS32_25_PCREL",  /* name  */
398
   false,     /* partial_inplace  */
399
   0xffffff,    /* src_mask  */
400
   0xffffff,    /* dst_mask  */
401
   true),     /* pcrel_offset  */
402
403
  /* High 20 bits of address when lower 12 is or'd in.  */
404
  HOWTO2 (R_NDS32_HI20,   /* type  */
405
   12,      /* rightshift  */
406
   4,     /* size  */
407
   20,      /* bitsize  */
408
   false,     /* pc_relative  */
409
   0,     /* bitpos  */
410
   complain_overflow_dont,/* complain_on_overflow  */
411
   nds32_elf_hi20_reloc,  /* special_function  */
412
   "R_NDS32_HI20",  /* name  */
413
   false,     /* partial_inplace  */
414
   0x000fffff,    /* src_mask  */
415
   0x000fffff,    /* dst_mask  */
416
   false),    /* pcrel_offset  */
417
418
  /* Lower 12 bits of address.  */
419
  HOWTO2 (R_NDS32_LO12S3, /* type  */
420
   3,     /* rightshift  */
421
   4,     /* size  */
422
   9,     /* bitsize  */
423
   false,     /* pc_relative  */
424
   0,     /* bitpos  */
425
   complain_overflow_dont,/* complain_on_overflow  */
426
   nds32_elf_lo12_reloc,  /* special_function  */
427
   "R_NDS32_LO12S3",  /* name  */
428
   false,     /* partial_inplace  */
429
   0x000001ff,    /* src_mask  */
430
   0x000001ff,    /* dst_mask  */
431
   false),    /* pcrel_offset  */
432
433
  /* Lower 12 bits of address.  */
434
  HOWTO2 (R_NDS32_LO12S2, /* type  */
435
   2,     /* rightshift  */
436
   4,     /* size  */
437
   10,      /* bitsize  */
438
   false,     /* pc_relative  */
439
   0,     /* bitpos  */
440
   complain_overflow_dont,/* complain_on_overflow  */
441
   nds32_elf_lo12_reloc,  /* special_function  */
442
   "R_NDS32_LO12S2",  /* name  */
443
   false,     /* partial_inplace  */
444
   0x000003ff,    /* src_mask  */
445
   0x000003ff,    /* dst_mask  */
446
   false),    /* pcrel_offset  */
447
448
  /* Lower 12 bits of address.  */
449
  HOWTO2 (R_NDS32_LO12S1, /* type  */
450
   1,     /* rightshift  */
451
   4,     /* size  */
452
   11,      /* bitsize  */
453
   false,     /* pc_relative  */
454
   0,     /* bitpos  */
455
   complain_overflow_dont,/* complain_on_overflow  */
456
   nds32_elf_lo12_reloc,  /* special_function  */
457
   "R_NDS32_LO12S1",  /* name  */
458
   false,     /* partial_inplace  */
459
   0x000007ff,    /* src_mask  */
460
   0x000007ff,    /* dst_mask  */
461
   false),    /* pcrel_offset  */
462
463
  /* Lower 12 bits of address.  */
464
  HOWTO2 (R_NDS32_LO12S0, /* type  */
465
   0,     /* rightshift  */
466
   4,     /* size  */
467
   12,      /* bitsize  */
468
   false,     /* pc_relative  */
469
   0,     /* bitpos  */
470
   complain_overflow_dont,/* complain_on_overflow  */
471
   nds32_elf_lo12_reloc,  /* special_function  */
472
   "R_NDS32_LO12S0",  /* name  */
473
   false,     /* partial_inplace  */
474
   0x00000fff,    /* src_mask  */
475
   0x00000fff,    /* dst_mask  */
476
   false),    /* pcrel_offset  */
477
478
  /* Small data area 15 bits offset.  */
479
  HOWTO2 (R_NDS32_SDA15S3,  /* type  */
480
   3,     /* rightshift  */
481
   4,     /* size  */
482
   15,      /* bitsize  */
483
   false,     /* pc_relative  */
484
   0,     /* bitpos  */
485
   complain_overflow_signed,/* complain_on_overflow  */
486
   nds32_elf_sda15_reloc, /* special_function  */
487
   "R_NDS32_SDA15S3", /* name  */
488
   false,     /* partial_inplace  */
489
   0x00007fff,    /* src_mask  */
490
   0x00007fff,    /* dst_mask  */
491
   false),    /* pcrel_offset  */
492
493
  /* Small data area 15 bits offset.  */
494
  HOWTO2 (R_NDS32_SDA15S2,  /* type  */
495
   2,     /* rightshift  */
496
   4,     /* size  */
497
   15,      /* bitsize  */
498
   false,     /* pc_relative  */
499
   0,     /* bitpos  */
500
   complain_overflow_signed,/* complain_on_overflow  */
501
   nds32_elf_sda15_reloc, /* special_function  */
502
   "R_NDS32_SDA15S2", /* name  */
503
   false,     /* partial_inplace  */
504
   0x00007fff,    /* src_mask  */
505
   0x00007fff,    /* dst_mask  */
506
   false),    /* pcrel_offset  */
507
508
  /* Small data area 15 bits offset.  */
509
  HOWTO2 (R_NDS32_SDA15S1,  /* type  */
510
   1,     /* rightshift  */
511
   4,     /* size  */
512
   15,      /* bitsize  */
513
   false,     /* pc_relative  */
514
   0,     /* bitpos  */
515
   complain_overflow_signed,/* complain_on_overflow  */
516
   nds32_elf_sda15_reloc, /* special_function  */
517
   "R_NDS32_SDA15S1", /* name  */
518
   false,     /* partial_inplace  */
519
   0x00007fff,    /* src_mask  */
520
   0x00007fff,    /* dst_mask  */
521
   false),    /* pcrel_offset  */
522
523
  /* Small data area 15 bits offset.  */
524
  HOWTO2 (R_NDS32_SDA15S0,  /* type  */
525
   0,     /* rightshift  */
526
   4,     /* size  */
527
   15,      /* bitsize  */
528
   false,     /* pc_relative  */
529
   0,     /* bitpos  */
530
   complain_overflow_signed,/* complain_on_overflow  */
531
   nds32_elf_sda15_reloc, /* special_function  */
532
   "R_NDS32_SDA15S0", /* name  */
533
   false,     /* partial_inplace  */
534
   0x00007fff,    /* src_mask  */
535
   0x00007fff,    /* dst_mask  */
536
   false),    /* pcrel_offset  */
537
538
  /* GNU extension to record C++ vtable hierarchy  */
539
  HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type  */
540
   0,     /* rightshift  */
541
   4,     /* size  */
542
   0,     /* bitsize  */
543
   false,     /* pc_relative  */
544
   0,     /* bitpos  */
545
   complain_overflow_dont,/* complain_on_overflow  */
546
   NULL,      /* special_function  */
547
   "R_NDS32_GNU_VTINHERIT",/* name  */
548
   false,     /* partial_inplace  */
549
   0,     /* src_mask  */
550
   0,     /* dst_mask  */
551
   false),    /* pcrel_offset  */
552
553
  /* GNU extension to record C++ vtable member usage  */
554
  HOWTO2 (R_NDS32_GNU_VTENTRY,  /* type  */
555
   0,     /* rightshift  */
556
   4,     /* size  */
557
   0,     /* bitsize  */
558
   false,     /* pc_relative  */
559
   0,     /* bitpos  */
560
   complain_overflow_dont,/* complain_on_overflow  */
561
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
562
   "R_NDS32_GNU_VTENTRY", /* name  */
563
   false,     /* partial_inplace  */
564
   0,     /* src_mask  */
565
   0,     /* dst_mask  */
566
   false),    /* pcrel_offset  */
567
568
  /* A 16 bit absolute relocation.  */
569
  HOWTO2 (R_NDS32_16_RELA,  /* type  */
570
   0,     /* rightshift  */
571
   2,     /* size  */
572
   16,      /* bitsize  */
573
   false,     /* pc_relative  */
574
   0,     /* bitpos  */
575
   complain_overflow_bitfield,/* complain_on_overflow  */
576
   bfd_elf_generic_reloc, /* special_function  */
577
   "R_NDS32_16_RELA", /* name  */
578
   false,     /* partial_inplace  */
579
   0xffff,    /* src_mask  */
580
   0xffff,    /* dst_mask  */
581
   false),    /* pcrel_offset  */
582
583
  /* A 32 bit absolute relocation.  */
584
  HOWTO2 (R_NDS32_32_RELA,  /* type  */
585
   0,     /* rightshift  */
586
   4,     /* size  */
587
   32,      /* bitsize  */
588
   false,     /* pc_relative  */
589
   0,     /* bitpos  */
590
   complain_overflow_bitfield,/* complain_on_overflow  */
591
   bfd_elf_generic_reloc, /* special_function  */
592
   "R_NDS32_32_RELA", /* name  */
593
   false,     /* partial_inplace  */
594
   0xffffffff,    /* src_mask  */
595
   0xffffffff,    /* dst_mask  */
596
   false),    /* pcrel_offset  */
597
598
  /* A 20 bit address.  */
599
  HOWTO2 (R_NDS32_20_RELA,  /* type  */
600
   0,     /* rightshift  */
601
   4,     /* size  */
602
   20,      /* bitsize  */
603
   false,     /* pc_relative  */
604
   0,     /* bitpos  */
605
   complain_overflow_signed,/* complain_on_overflow  */
606
   bfd_elf_generic_reloc, /* special_function  */
607
   "R_NDS32_20_RELA", /* name  */
608
   false,     /* partial_inplace  */
609
   0xfffff,   /* src_mask  */
610
   0xfffff,   /* dst_mask  */
611
   false),    /* pcrel_offset  */
612
613
  HOWTO2 (R_NDS32_9_PCREL_RELA, /* type  */
614
   1,     /* rightshift  */
615
   2,     /* size  */
616
   8,     /* bitsize  */
617
   true,      /* pc_relative  */
618
   0,     /* bitpos  */
619
   complain_overflow_signed,/* complain_on_overflow  */
620
   bfd_elf_generic_reloc, /* special_function  */
621
   "R_NDS32_9_PCREL_RELA",/* name  */
622
   false,     /* partial_inplace  */
623
   0xff,      /* src_mask  */
624
   0xff,      /* dst_mask  */
625
   true),     /* pcrel_offset  */
626
627
  /* A relative 15 bit relocation, right shifted by 1.  */
628
  HOWTO2 (R_NDS32_15_PCREL_RELA,/* type  */
629
   1,     /* rightshift  */
630
   4,     /* size  */
631
   14,      /* bitsize  */
632
   true,      /* pc_relative  */
633
   0,     /* bitpos  */
634
   complain_overflow_signed,/* complain_on_overflow  */
635
   bfd_elf_generic_reloc, /* special_function  */
636
   "R_NDS32_15_PCREL_RELA",/* name  */
637
   false,     /* partial_inplace  */
638
   0x3fff,    /* src_mask  */
639
   0x3fff,    /* dst_mask  */
640
   true),     /* pcrel_offset  */
641
642
  /* A relative 17 bit relocation, right shifted by 1.  */
643
  HOWTO2 (R_NDS32_17_PCREL_RELA,/* type  */
644
   1,     /* rightshift  */
645
   4,     /* size  */
646
   16,      /* bitsize  */
647
   true,      /* pc_relative  */
648
   0,     /* bitpos  */
649
   complain_overflow_signed,/* complain_on_overflow  */
650
   bfd_elf_generic_reloc, /* special_function  */
651
   "R_NDS32_17_PCREL_RELA",/* name  */
652
   false,     /* partial_inplace  */
653
   0xffff,    /* src_mask  */
654
   0xffff,    /* dst_mask  */
655
   true),     /* pcrel_offset  */
656
657
  /* A relative 25 bit relocation, right shifted by 2.  */
658
  HOWTO2 (R_NDS32_25_PCREL_RELA,/* type  */
659
   1,     /* rightshift  */
660
   4,     /* size  */
661
   24,      /* bitsize  */
662
   true,      /* pc_relative  */
663
   0,     /* bitpos  */
664
   complain_overflow_signed,/* complain_on_overflow  */
665
   bfd_elf_generic_reloc, /* special_function  */
666
   "R_NDS32_25_PCREL_RELA",/* name  */
667
   false,     /* partial_inplace  */
668
   0xffffff,    /* src_mask  */
669
   0xffffff,    /* dst_mask  */
670
   true),     /* pcrel_offset  */
671
672
  /* High 20 bits of address when lower 16 is or'd in.  */
673
  HOWTO2 (R_NDS32_HI20_RELA,  /* type  */
674
   12,      /* rightshift  */
675
   4,     /* size  */
676
   20,      /* bitsize  */
677
   false,     /* pc_relative  */
678
   0,     /* bitpos  */
679
   complain_overflow_dont,/* complain_on_overflow  */
680
   bfd_elf_generic_reloc, /* special_function  */
681
   "R_NDS32_HI20_RELA", /* name  */
682
   false,     /* partial_inplace  */
683
   0x000fffff,    /* src_mask  */
684
   0x000fffff,    /* dst_mask  */
685
   false),    /* pcrel_offset  */
686
687
  /* Lower 12 bits of address.  */
688
  HOWTO2 (R_NDS32_LO12S3_RELA,  /* type  */
689
   3,     /* rightshift  */
690
   4,     /* size  */
691
   9,     /* bitsize  */
692
   false,     /* pc_relative  */
693
   0,     /* bitpos  */
694
   complain_overflow_dont,/* complain_on_overflow  */
695
   bfd_elf_generic_reloc, /* special_function  */
696
   "R_NDS32_LO12S3_RELA", /* name  */
697
   false,     /* partial_inplace  */
698
   0x000001ff,    /* src_mask  */
699
   0x000001ff,    /* dst_mask  */
700
   false),    /* pcrel_offset  */
701
702
  /* Lower 12 bits of address.  */
703
  HOWTO2 (R_NDS32_LO12S2_RELA,  /* type  */
704
   2,     /* rightshift  */
705
   4,     /* size  */
706
   10,      /* bitsize  */
707
   false,     /* pc_relative  */
708
   0,     /* bitpos  */
709
   complain_overflow_dont,/* complain_on_overflow  */
710
   bfd_elf_generic_reloc, /* special_function  */
711
   "R_NDS32_LO12S2_RELA", /* name  */
712
   false,     /* partial_inplace  */
713
   0x000003ff,    /* src_mask  */
714
   0x000003ff,    /* dst_mask  */
715
   false),    /* pcrel_offset  */
716
717
  /* Lower 12 bits of address.  */
718
  HOWTO2 (R_NDS32_LO12S1_RELA,  /* type  */
719
   1,     /* rightshift  */
720
   4,     /* size  */
721
   11,      /* bitsize  */
722
   false,     /* pc_relative  */
723
   0,     /* bitpos  */
724
   complain_overflow_dont,/* complain_on_overflow  */
725
   bfd_elf_generic_reloc, /* special_function  */
726
   "R_NDS32_LO12S1_RELA", /* name  */
727
   false,     /* partial_inplace  */
728
   0x000007ff,    /* src_mask  */
729
   0x000007ff,    /* dst_mask  */
730
   false),    /* pcrel_offset  */
731
732
  /* Lower 12 bits of address.  */
733
  HOWTO2 (R_NDS32_LO12S0_RELA,  /* type  */
734
   0,     /* rightshift  */
735
   4,     /* size  */
736
   12,      /* bitsize  */
737
   false,     /* pc_relative  */
738
   0,     /* bitpos  */
739
   complain_overflow_dont,/* complain_on_overflow  */
740
   bfd_elf_generic_reloc, /* special_function  */
741
   "R_NDS32_LO12S0_RELA", /* name  */
742
   false,     /* partial_inplace  */
743
   0x00000fff,    /* src_mask  */
744
   0x00000fff,    /* dst_mask  */
745
   false),    /* pcrel_offset  */
746
747
  /* Small data area 15 bits offset.  */
748
  HOWTO2 (R_NDS32_SDA15S3_RELA, /* type  */
749
   3,     /* rightshift  */
750
   4,     /* size  */
751
   15,      /* bitsize  */
752
   false,     /* pc_relative  */
753
   0,     /* bitpos  */
754
   complain_overflow_signed,/* complain_on_overflow  */
755
   bfd_elf_generic_reloc, /* special_function  */
756
   "R_NDS32_SDA15S3_RELA",/* name  */
757
   false,     /* partial_inplace  */
758
   0x00007fff,    /* src_mask  */
759
   0x00007fff,    /* dst_mask  */
760
   false),    /* pcrel_offset  */
761
762
  /* Small data area 15 bits offset.  */
763
  HOWTO2 (R_NDS32_SDA15S2_RELA, /* type  */
764
   2,     /* rightshift  */
765
   4,     /* size  */
766
   15,      /* bitsize  */
767
   false,     /* pc_relative  */
768
   0,     /* bitpos  */
769
   complain_overflow_signed,/* complain_on_overflow  */
770
   bfd_elf_generic_reloc, /* special_function  */
771
   "R_NDS32_SDA15S2_RELA",/* name  */
772
   false,     /* partial_inplace  */
773
   0x00007fff,    /* src_mask  */
774
   0x00007fff,    /* dst_mask  */
775
   false),    /* pcrel_offset  */
776
777
  HOWTO2 (R_NDS32_SDA15S1_RELA, /* type  */
778
   1,     /* rightshift  */
779
   4,     /* size  */
780
   15,      /* bitsize  */
781
   false,     /* pc_relative  */
782
   0,     /* bitpos  */
783
   complain_overflow_signed,/* complain_on_overflow  */
784
   bfd_elf_generic_reloc, /* special_function  */
785
   "R_NDS32_SDA15S1_RELA",/* name  */
786
   false,     /* partial_inplace  */
787
   0x00007fff,    /* src_mask  */
788
   0x00007fff,    /* dst_mask  */
789
   false),    /* pcrel_offset  */
790
791
  HOWTO2 (R_NDS32_SDA15S0_RELA, /* type  */
792
   0,     /* rightshift  */
793
   4,     /* size  */
794
   15,      /* bitsize  */
795
   false,     /* pc_relative  */
796
   0,     /* bitpos  */
797
   complain_overflow_signed,/* complain_on_overflow  */
798
   bfd_elf_generic_reloc, /* special_function  */
799
   "R_NDS32_SDA15S0_RELA",/* name  */
800
   false,     /* partial_inplace  */
801
   0x00007fff,    /* src_mask  */
802
   0x00007fff,    /* dst_mask  */
803
   false),    /* pcrel_offset  */
804
805
  /* GNU extension to record C++ vtable hierarchy  */
806
  HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type  */
807
   0,     /* rightshift  */
808
   4,     /* size  */
809
   0,     /* bitsize  */
810
   false,     /* pc_relative  */
811
   0,     /* bitpos  */
812
   complain_overflow_dont,/* complain_on_overflow  */
813
   NULL,      /* special_function  */
814
   "R_NDS32_RELA_GNU_VTINHERIT",/* name  */
815
   false,     /* partial_inplace  */
816
   0,     /* src_mask  */
817
   0,     /* dst_mask  */
818
   false),    /* pcrel_offset  */
819
820
  /* GNU extension to record C++ vtable member usage  */
821
  HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type  */
822
   0,     /* rightshift  */
823
   4,     /* size  */
824
   0,     /* bitsize  */
825
   false,     /* pc_relative  */
826
   0,     /* bitpos  */
827
   complain_overflow_dont,/* complain_on_overflow  */
828
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
829
   "R_NDS32_RELA_GNU_VTENTRY",/* name  */
830
   false,     /* partial_inplace  */
831
   0,     /* src_mask  */
832
   0,     /* dst_mask  */
833
   false),    /* pcrel_offset  */
834
835
  /* Like R_NDS32_20, but referring to the GOT table entry for
836
     the symbol.  */
837
  HOWTO2 (R_NDS32_GOT20,  /* type  */
838
   0,     /* rightshift  */
839
   4,     /* size  */
840
   20,      /* bitsize  */
841
   false,     /* pc_relative  */
842
   0,     /* bitpos  */
843
   complain_overflow_signed,/* complain_on_overflow  */
844
   bfd_elf_generic_reloc, /* special_function  */
845
   "R_NDS32_GOT20", /* name  */
846
   false,     /* partial_inplace  */
847
   0xfffff,   /* src_mask  */
848
   0xfffff,   /* dst_mask  */
849
   false),    /* pcrel_offset  */
850
851
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
852
     entry for the symbol.  */
853
  HOWTO2 (R_NDS32_25_PLTREL,  /* type  */
854
   1,     /* rightshift  */
855
   4,     /* size  */
856
   24,      /* bitsize  */
857
   true,      /* pc_relative  */
858
   0,     /* bitpos  */
859
   complain_overflow_signed,/* complain_on_overflow  */
860
   bfd_elf_generic_reloc, /* special_function  */
861
   "R_NDS32_25_PLTREL", /* name  */
862
   false,     /* partial_inplace  */
863
   0xffffff,    /* src_mask  */
864
   0xffffff,    /* dst_mask  */
865
   true),     /* pcrel_offset  */
866
867
  /* This is used only by the dynamic linker.  The symbol should exist
868
     both in the object being run and in some shared library.  The
869
     dynamic linker copies the data addressed by the symbol from the
870
     shared library into the object, because the object being
871
     run has to have the data at some particular address.  */
872
  HOWTO2 (R_NDS32_COPY,   /* type  */
873
   0,     /* rightshift  */
874
   4,     /* size  */
875
   32,      /* bitsize  */
876
   false,     /* pc_relative  */
877
   0,     /* bitpos  */
878
   complain_overflow_bitfield,/* complain_on_overflow  */
879
   bfd_elf_generic_reloc, /* special_function  */
880
   "R_NDS32_COPY",  /* name  */
881
   false,     /* partial_inplace  */
882
   0xffffffff,    /* src_mask  */
883
   0xffffffff,    /* dst_mask  */
884
   false),    /* pcrel_offset  */
885
886
  /* Like R_NDS32_20, but used when setting global offset table
887
     entries.  */
888
  HOWTO2 (R_NDS32_GLOB_DAT, /* type  */
889
   0,     /* rightshift  */
890
   4,     /* size  */
891
   32,      /* bitsize  */
892
   false,     /* pc_relative  */
893
   0,     /* bitpos  */
894
   complain_overflow_bitfield,/* complain_on_overflow  */
895
   bfd_elf_generic_reloc, /* special_function  */
896
   "R_NDS32_GLOB_DAT",  /* name  */
897
   false,     /* partial_inplace  */
898
   0xffffffff,    /* src_mask  */
899
   0xffffffff,    /* dst_mask  */
900
   false),    /* pcrel_offset  */
901
902
  /* Marks a procedure linkage table entry for a symbol.  */
903
  HOWTO2 (R_NDS32_JMP_SLOT, /* type  */
904
   0,     /* rightshift  */
905
   4,     /* size  */
906
   32,      /* bitsize  */
907
   false,     /* pc_relative  */
908
   0,     /* bitpos  */
909
   complain_overflow_bitfield,/* complain_on_overflow  */
910
   bfd_elf_generic_reloc, /* special_function  */
911
   "R_NDS32_JMP_SLOT",  /* name  */
912
   false,     /* partial_inplace  */
913
   0xffffffff,    /* src_mask  */
914
   0xffffffff,    /* dst_mask  */
915
   false),    /* pcrel_offset  */
916
917
  /* Used only by the dynamic linker.  When the object is run, this
918
     longword is set to the load address of the object, plus the
919
     addend.  */
920
  HOWTO2 (R_NDS32_RELATIVE, /* type  */
921
   0,     /* rightshift  */
922
   4,     /* size  */
923
   32,      /* bitsize  */
924
   false,     /* pc_relative  */
925
   0,     /* bitpos  */
926
   complain_overflow_bitfield,/* complain_on_overflow  */
927
   bfd_elf_generic_reloc, /* special_function  */
928
   "R_NDS32_RELATIVE",  /* name  */
929
   false,     /* partial_inplace  */
930
   0xffffffff,    /* src_mask  */
931
   0xffffffff,    /* dst_mask  */
932
   false),    /* pcrel_offset  */
933
934
  HOWTO2 (R_NDS32_GOTOFF, /* type  */
935
   0,     /* rightshift  */
936
   4,     /* size  */
937
   20,      /* bitsize  */
938
   false,     /* pc_relative  */
939
   0,     /* bitpos  */
940
   complain_overflow_signed,/* complain_on_overflow  */
941
   bfd_elf_generic_reloc, /* special_function  */
942
   "R_NDS32_GOTOFF",  /* name  */
943
   false,     /* partial_inplace  */
944
   0xfffff,   /* src_mask  */
945
   0xfffff,   /* dst_mask  */
946
   false),    /* pcrel_offset  */
947
948
  /* An PC Relative 20-bit relocation used when setting PIC offset
949
     table register.  */
950
  HOWTO2 (R_NDS32_GOTPC20,  /* type  */
951
   0,     /* rightshift  */
952
   4,     /* size  */
953
   20,      /* bitsize  */
954
   true,      /* pc_relative  */
955
   0,     /* bitpos  */
956
   complain_overflow_signed,/* complain_on_overflow  */
957
   bfd_elf_generic_reloc, /* special_function  */
958
   "R_NDS32_GOTPC20", /* name  */
959
   false,     /* partial_inplace  */
960
   0xfffff,   /* src_mask  */
961
   0xfffff,   /* dst_mask  */
962
   true),     /* pcrel_offset  */
963
964
  /* Like R_NDS32_HI20, but referring to the GOT table entry for
965
     the symbol.  */
966
  HOWTO2 (R_NDS32_GOT_HI20, /* type  */
967
   12,      /* rightshift  */
968
   4,     /* size  */
969
   20,      /* bitsize  */
970
   false,     /* pc_relative  */
971
   0,     /* bitpos  */
972
   complain_overflow_dont,/* complain_on_overflow  */
973
   bfd_elf_generic_reloc, /* special_function  */
974
   "R_NDS32_GOT_HI20",  /* name  */
975
   false,     /* partial_inplace  */
976
   0x000fffff,    /* src_mask  */
977
   0x000fffff,    /* dst_mask  */
978
   false),    /* pcrel_offset  */
979
980
  HOWTO2 (R_NDS32_GOT_LO12, /* type  */
981
   0,     /* rightshift  */
982
   4,     /* size  */
983
   12,      /* bitsize  */
984
   false,     /* pc_relative  */
985
   0,     /* bitpos  */
986
   complain_overflow_dont,/* complain_on_overflow  */
987
   bfd_elf_generic_reloc, /* special_function  */
988
   "R_NDS32_GOT_LO12",  /* name  */
989
   false,     /* partial_inplace  */
990
   0x00000fff,    /* src_mask  */
991
   0x00000fff,    /* dst_mask  */
992
   false),    /* pcrel_offset  */
993
994
  /* An PC Relative relocation used when setting PIC offset table register.
995
     Like R_NDS32_HI20, but referring to the GOT table entry for
996
     the symbol.  */
997
  HOWTO2 (R_NDS32_GOTPC_HI20, /* type  */
998
   12,      /* rightshift  */
999
   4,     /* size  */
1000
   20,      /* bitsize  */
1001
   false,     /* pc_relative  */
1002
   0,     /* bitpos  */
1003
   complain_overflow_dont,/* complain_on_overflow  */
1004
   bfd_elf_generic_reloc, /* special_function  */
1005
   "R_NDS32_GOTPC_HI20",  /* name  */
1006
   false,     /* partial_inplace  */
1007
   0x000fffff,    /* src_mask  */
1008
   0x000fffff,    /* dst_mask  */
1009
   true),     /* pcrel_offset  */
1010
1011
  HOWTO2 (R_NDS32_GOTPC_LO12, /* type  */
1012
   0,     /* rightshift  */
1013
   4,     /* size  */
1014
   12,      /* bitsize  */
1015
   false,     /* pc_relative  */
1016
   0,     /* bitpos  */
1017
   complain_overflow_dont,/* complain_on_overflow  */
1018
   bfd_elf_generic_reloc, /* special_function  */
1019
   "R_NDS32_GOTPC_LO12",  /* name  */
1020
   false,     /* partial_inplace  */
1021
   0x00000fff,    /* src_mask  */
1022
   0x00000fff,    /* dst_mask  */
1023
   true),     /* pcrel_offset  */
1024
1025
  HOWTO2 (R_NDS32_GOTOFF_HI20,  /* type  */
1026
   12,      /* rightshift  */
1027
   4,     /* size  */
1028
   20,      /* bitsize  */
1029
   false,     /* pc_relative  */
1030
   0,     /* bitpos  */
1031
   complain_overflow_dont,/* complain_on_overflow  */
1032
   bfd_elf_generic_reloc, /* special_function  */
1033
   "R_NDS32_GOTOFF_HI20", /* name  */
1034
   false,     /* partial_inplace  */
1035
   0x000fffff,    /* src_mask  */
1036
   0x000fffff,    /* dst_mask  */
1037
   false),    /* pcrel_offset  */
1038
1039
  HOWTO2 (R_NDS32_GOTOFF_LO12,  /* type  */
1040
   0,     /* rightshift  */
1041
   4,     /* size  */
1042
   12,      /* bitsize  */
1043
   false,     /* pc_relative  */
1044
   0,     /* bitpos  */
1045
   complain_overflow_dont,/* complain_on_overflow  */
1046
   bfd_elf_generic_reloc, /* special_function  */
1047
   "R_NDS32_GOTOFF_LO12", /* name  */
1048
   false,     /* partial_inplace  */
1049
   0x00000fff,    /* src_mask  */
1050
   0x00000fff,    /* dst_mask  */
1051
   false),    /* pcrel_offset  */
1052
1053
  /* Alignment hint for relaxable instruction.  This is used with
1054
     R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
1055
     in order to make next label aligned on word boundary.  */
1056
  HOWTO2 (R_NDS32_INSN16, /* type  */
1057
   0,     /* rightshift  */
1058
   4,     /* size  */
1059
   32,      /* bitsize  */
1060
   false,     /* pc_relative  */
1061
   0,     /* bitpos  */
1062
   complain_overflow_dont,/* complain_on_overflow  */
1063
   nds32_elf_ignore_reloc,/* special_function  */
1064
   "R_NDS32_INSN16",  /* name  */
1065
   false,     /* partial_inplace  */
1066
   0x00000fff,    /* src_mask  */
1067
   0x00000fff,    /* dst_mask  */
1068
   false),    /* pcrel_offset  */
1069
1070
  /* Alignment hint for label.  */
1071
  HOWTO2 (R_NDS32_LABEL,  /* type  */
1072
   0,     /* rightshift  */
1073
   4,     /* size  */
1074
   32,      /* bitsize  */
1075
   false,     /* pc_relative  */
1076
   0,     /* bitpos  */
1077
   complain_overflow_dont,/* complain_on_overflow  */
1078
   nds32_elf_ignore_reloc,/* special_function  */
1079
   "R_NDS32_LABEL", /* name  */
1080
   false,     /* partial_inplace  */
1081
   0xffffffff,    /* src_mask  */
1082
   0xffffffff,    /* dst_mask  */
1083
   false),    /* pcrel_offset  */
1084
1085
  /* Relax hint for unconditional call sequence  */
1086
  HOWTO2 (R_NDS32_LONGCALL1,  /* type  */
1087
   0,     /* rightshift  */
1088
   4,     /* size  */
1089
   32,      /* bitsize  */
1090
   false,     /* pc_relative  */
1091
   0,     /* bitpos  */
1092
   complain_overflow_dont,/* complain_on_overflow  */
1093
   nds32_elf_ignore_reloc,/* special_function  */
1094
   "R_NDS32_LONGCALL1", /* name  */
1095
   false,     /* partial_inplace  */
1096
   0xffffffff,    /* src_mask  */
1097
   0xffffffff,    /* dst_mask  */
1098
   false),    /* pcrel_offset  */
1099
1100
  /* Relax hint for conditional call sequence.  */
1101
  HOWTO2 (R_NDS32_LONGCALL2,  /* type  */
1102
   0,     /* rightshift  */
1103
   4,     /* size  */
1104
   32,      /* bitsize  */
1105
   false,     /* pc_relative  */
1106
   0,     /* bitpos  */
1107
   complain_overflow_dont,/* complain_on_overflow  */
1108
   nds32_elf_ignore_reloc,/* special_function  */
1109
   "R_NDS32_LONGCALL2", /* name  */
1110
   false,     /* partial_inplace  */
1111
   0xffffffff,    /* src_mask  */
1112
   0xffffffff,    /* dst_mask  */
1113
   false),    /* pcrel_offset  */
1114
1115
  /* Relax hint for conditional call sequence.  */
1116
  HOWTO2 (R_NDS32_LONGCALL3,  /* type  */
1117
   0,     /* rightshift  */
1118
   4,     /* size  */
1119
   32,      /* bitsize  */
1120
   false,     /* pc_relative  */
1121
   0,     /* bitpos  */
1122
   complain_overflow_dont,/* complain_on_overflow  */
1123
   nds32_elf_ignore_reloc,/* special_function  */
1124
   "R_NDS32_LONGCALL3", /* name  */
1125
   false,     /* partial_inplace  */
1126
   0xffffffff,    /* src_mask  */
1127
   0xffffffff,    /* dst_mask  */
1128
   false),    /* pcrel_offset  */
1129
1130
  /* Relax hint for unconditional branch sequence.  */
1131
  HOWTO2 (R_NDS32_LONGJUMP1,  /* type  */
1132
   0,     /* rightshift  */
1133
   4,     /* size  */
1134
   32,      /* bitsize  */
1135
   false,     /* pc_relative  */
1136
   0,     /* bitpos  */
1137
   complain_overflow_dont,/* complain_on_overflow  */
1138
   nds32_elf_ignore_reloc,/* special_function  */
1139
   "R_NDS32_LONGJUMP1", /* name  */
1140
   false,     /* partial_inplace  */
1141
   0xffffffff,    /* src_mask  */
1142
   0xffffffff,    /* dst_mask  */
1143
   false),    /* pcrel_offset  */
1144
1145
  /* Relax hint for conditional branch sequence.  */
1146
  HOWTO2 (R_NDS32_LONGJUMP2,  /* type  */
1147
   0,     /* rightshift  */
1148
   4,     /* size  */
1149
   32,      /* bitsize  */
1150
   false,     /* pc_relative  */
1151
   0,     /* bitpos  */
1152
   complain_overflow_dont,/* complain_on_overflow  */
1153
   nds32_elf_ignore_reloc,/* special_function  */
1154
   "R_NDS32_LONGJUMP2", /* name  */
1155
   false,     /* partial_inplace  */
1156
   0xffffffff,    /* src_mask  */
1157
   0xffffffff,    /* dst_mask  */
1158
   false),    /* pcrel_offset  */
1159
1160
  /* Relax hint for conditional branch sequence.  */
1161
  HOWTO2 (R_NDS32_LONGJUMP3,  /* type  */
1162
   0,     /* rightshift  */
1163
   4,     /* size  */
1164
   32,      /* bitsize  */
1165
   false,     /* pc_relative  */
1166
   0,     /* bitpos  */
1167
   complain_overflow_dont,/* complain_on_overflow  */
1168
   nds32_elf_ignore_reloc,/* special_function  */
1169
   "R_NDS32_LONGJUMP3", /* name  */
1170
   false,     /* partial_inplace  */
1171
   0xffffffff,    /* src_mask  */
1172
   0xffffffff,    /* dst_mask  */
1173
   false),    /* pcrel_offset  */
1174
1175
  /* Relax hint for load/store sequence.   */
1176
  HOWTO2 (R_NDS32_LOADSTORE,  /* type  */
1177
   0,     /* rightshift  */
1178
   4,     /* size  */
1179
   32,      /* bitsize  */
1180
   false,     /* pc_relative  */
1181
   0,     /* bitpos  */
1182
   complain_overflow_dont,/* complain_on_overflow  */
1183
   nds32_elf_ignore_reloc,/* special_function  */
1184
   "R_NDS32_LOADSTORE", /* name  */
1185
   false,     /* partial_inplace  */
1186
   0xffffffff,    /* src_mask  */
1187
   0xffffffff,    /* dst_mask  */
1188
   false),    /* pcrel_offset  */
1189
1190
  /* Relax hint for load/store sequence.  */
1191
  HOWTO2 (R_NDS32_9_FIXED_RELA, /* type  */
1192
   0,     /* rightshift  */
1193
   2,     /* size  */
1194
   16,      /* bitsize  */
1195
   false,     /* pc_relative  */
1196
   0,     /* bitpos  */
1197
   complain_overflow_dont,/* complain_on_overflow  */
1198
   nds32_elf_ignore_reloc,/* special_function  */
1199
   "R_NDS32_9_FIXED_RELA",/* name  */
1200
   false,     /* partial_inplace  */
1201
   0x000000ff,    /* src_mask  */
1202
   0x000000ff,    /* dst_mask  */
1203
   false),    /* pcrel_offset  */
1204
1205
  /* Relax hint for load/store sequence.  */
1206
  HOWTO2 (R_NDS32_15_FIXED_RELA,/* type  */
1207
   0,     /* rightshift  */
1208
   4,     /* size  */
1209
   32,      /* bitsize  */
1210
   false,     /* pc_relative  */
1211
   0,     /* bitpos  */
1212
   complain_overflow_dont,/* complain_on_overflow  */
1213
   nds32_elf_ignore_reloc,/* special_function  */
1214
   "R_NDS32_15_FIXED_RELA",/* name  */
1215
   false,     /* partial_inplace  */
1216
   0x00003fff,    /* src_mask  */
1217
   0x00003fff,    /* dst_mask  */
1218
   false),    /* pcrel_offset  */
1219
1220
  /* Relax hint for load/store sequence.  */
1221
  HOWTO2 (R_NDS32_17_FIXED_RELA,/* type  */
1222
   0,     /* rightshift  */
1223
   4,     /* size  */
1224
   32,      /* bitsize  */
1225
   false,     /* pc_relative  */
1226
   0,     /* bitpos  */
1227
   complain_overflow_dont,/* complain_on_overflow  */
1228
   nds32_elf_ignore_reloc,/* special_function  */
1229
   "R_NDS32_17_FIXED_RELA",/* name  */
1230
   false,     /* partial_inplace  */
1231
   0x0000ffff,    /* src_mask  */
1232
   0x0000ffff,    /* dst_mask  */
1233
   false),    /* pcrel_offset  */
1234
1235
  /* Relax hint for load/store sequence.  */
1236
  HOWTO2 (R_NDS32_25_FIXED_RELA,/* type  */
1237
   0,     /* rightshift  */
1238
   4,     /* size  */
1239
   32,      /* bitsize  */
1240
   false,     /* pc_relative  */
1241
   0,     /* bitpos  */
1242
   complain_overflow_dont,/* complain_on_overflow  */
1243
   nds32_elf_ignore_reloc,/* special_function  */
1244
   "R_NDS32_25_FIXED_RELA",/* name  */
1245
   false,     /* partial_inplace  */
1246
   0x00ffffff,    /* src_mask  */
1247
   0x00ffffff,    /* dst_mask  */
1248
   false),    /* pcrel_offset  */
1249
1250
  /* High 20 bits of PLT symbol offset relative to PC.  */
1251
  HOWTO2 (R_NDS32_PLTREL_HI20,  /* type  */
1252
   12,      /* rightshift  */
1253
   4,     /* size  */
1254
   20,      /* bitsize  */
1255
   false,     /* pc_relative  */
1256
   0,     /* bitpos  */
1257
   complain_overflow_dont,/* complain_on_overflow  */
1258
   bfd_elf_generic_reloc, /* special_function  */
1259
   "R_NDS32_PLTREL_HI20", /* name  */
1260
   false,     /* partial_inplace  */
1261
   0x000fffff,    /* src_mask  */
1262
   0x000fffff,    /* dst_mask  */
1263
   false),    /* pcrel_offset  */
1264
1265
  /* Low 12 bits of PLT symbol offset relative to PC.  */
1266
  HOWTO2 (R_NDS32_PLTREL_LO12,  /* type  */
1267
   0,     /* rightshift  */
1268
   4,     /* size  */
1269
   12,      /* bitsize  */
1270
   false,     /* pc_relative  */
1271
   0,     /* bitpos  */
1272
   complain_overflow_dont,/* complain_on_overflow  */
1273
   bfd_elf_generic_reloc, /* special_function  */
1274
   "R_NDS32_PLTREL_LO12", /* name  */
1275
   false,     /* partial_inplace  */
1276
   0x00000fff,    /* src_mask  */
1277
   0x00000fff,    /* dst_mask  */
1278
   false),    /* pcrel_offset  */
1279
1280
  /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
1281
  HOWTO2 (R_NDS32_PLT_GOTREL_HI20,  /* type  */
1282
   12,      /* rightshift  */
1283
   4,     /* size  */
1284
   20,      /* bitsize  */
1285
   false,     /* pc_relative  */
1286
   0,     /* bitpos  */
1287
   complain_overflow_dont,/* complain_on_overflow  */
1288
   bfd_elf_generic_reloc, /* special_function  */
1289
   "R_NDS32_PLT_GOTREL_HI20",/* name  */
1290
   false,     /* partial_inplace  */
1291
   0x000fffff,    /* src_mask  */
1292
   0x000fffff,    /* dst_mask  */
1293
   false),    /* pcrel_offset  */
1294
1295
  /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
1296
  HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type  */
1297
   0,     /* rightshift  */
1298
   4,     /* size  */
1299
   12,      /* bitsize  */
1300
   false,     /* pc_relative  */
1301
   0,     /* bitpos  */
1302
   complain_overflow_dont,/* complain_on_overflow  */
1303
   bfd_elf_generic_reloc, /* special_function  */
1304
   "R_NDS32_PLT_GOTREL_LO12",/* name  */
1305
   false,     /* partial_inplace  */
1306
   0x00000fff,    /* src_mask  */
1307
   0x00000fff,    /* dst_mask  */
1308
   false),    /* pcrel_offset  */
1309
1310
  /* Small data area 12 bits offset.  */
1311
  HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type  */
1312
   2,     /* rightshift  */
1313
   4,     /* size  */
1314
   12,      /* bitsize  */
1315
   false,     /* pc_relative  */
1316
   0,     /* bitpos  */
1317
   complain_overflow_signed,/* complain_on_overflow  */
1318
   bfd_elf_generic_reloc, /* special_function  */
1319
   "R_NDS32_SDA12S2_DP_RELA",/* name  */
1320
   false,     /* partial_inplace  */
1321
   0x00000fff,    /* src_mask  */
1322
   0x00000fff,    /* dst_mask  */
1323
   false),    /* pcrel_offset  */
1324
1325
  /* Small data area 12 bits offset.  */
1326
  HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type  */
1327
   2,     /* rightshift  */
1328
   4,     /* size  */
1329
   12,      /* bitsize  */
1330
   false,     /* pc_relative  */
1331
   0,     /* bitpos  */
1332
   complain_overflow_signed,/* complain_on_overflow  */
1333
   bfd_elf_generic_reloc, /* special_function  */
1334
   "R_NDS32_SDA12S2_SP_RELA",/* name  */
1335
   false,     /* partial_inplace  */
1336
   0x00000fff,    /* src_mask  */
1337
   0x00000fff,    /* dst_mask  */
1338
   false),    /* pcrel_offset  */
1339
  /* Lower 12 bits of address.  */
1340
1341
  HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type  */
1342
   2,     /* rightshift  */
1343
   4,     /* size  */
1344
   10,      /* bitsize  */
1345
   false,     /* pc_relative  */
1346
   0,     /* bitpos  */
1347
   complain_overflow_dont,/* complain_on_overflow  */
1348
   bfd_elf_generic_reloc, /* special_function  */
1349
   "R_NDS32_LO12S2_DP_RELA",/* name  */
1350
   false,     /* partial_inplace  */
1351
   0x000003ff,    /* src_mask  */
1352
   0x000003ff,    /* dst_mask  */
1353
   false),    /* pcrel_offset  */
1354
1355
  /* Lower 12 bits of address.  */
1356
  HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type  */
1357
   2,     /* rightshift  */
1358
   4,     /* size  */
1359
   10,      /* bitsize  */
1360
   false,     /* pc_relative  */
1361
   0,     /* bitpos  */
1362
   complain_overflow_dont,/* complain_on_overflow  */
1363
   bfd_elf_generic_reloc, /* special_function  */
1364
   "R_NDS32_LO12S2_SP_RELA",/* name  */
1365
   false,     /* partial_inplace  */
1366
   0x000003ff,    /* src_mask  */
1367
   0x000003ff,    /* dst_mask  */
1368
   false),    /* pcrel_offset  */
1369
1370
  /* Lower 12 bits of address.  Special identity for or case.  */
1371
  HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type  */
1372
   0,     /* rightshift  */
1373
   4,     /* size  */
1374
   12,      /* bitsize  */
1375
   false,     /* pc_relative  */
1376
   0,     /* bitpos  */
1377
   complain_overflow_dont,/* complain_on_overflow  */
1378
   bfd_elf_generic_reloc, /* special_function  */
1379
   "R_NDS32_LO12S0_ORI_RELA",/* name  */
1380
   false,     /* partial_inplace  */
1381
   0x00000fff,    /* src_mask  */
1382
   0x00000fff,    /* dst_mask  */
1383
   false),    /* pcrel_offset  */
1384
1385
  /* Small data area 19 bits offset.  */
1386
  HOWTO2 (R_NDS32_SDA16S3_RELA, /* type  */
1387
   3,     /* rightshift  */
1388
   4,     /* size  */
1389
   16,      /* bitsize  */
1390
   false,     /* pc_relative  */
1391
   0,     /* bitpos  */
1392
   complain_overflow_signed,/* complain_on_overflow  */
1393
   bfd_elf_generic_reloc, /* special_function  */
1394
   "R_NDS32_SDA16S3_RELA",/* name  */
1395
   false,     /* partial_inplace  */
1396
   0x0000ffff,    /* src_mask  */
1397
   0x0000ffff,    /* dst_mask  */
1398
   false),    /* pcrel_offset  */
1399
1400
  /* Small data area 15 bits offset.  */
1401
  HOWTO2 (R_NDS32_SDA17S2_RELA, /* type  */
1402
   2,     /* rightshift  */
1403
   4,     /* size  */
1404
   17,      /* bitsize  */
1405
   false,     /* pc_relative  */
1406
   0,     /* bitpos  */
1407
   complain_overflow_signed,/* complain_on_overflow  */
1408
   bfd_elf_generic_reloc, /* special_function  */
1409
   "R_NDS32_SDA17S2_RELA",/* name  */
1410
   false,     /* partial_inplace  */
1411
   0x0001ffff,    /* src_mask  */
1412
   0x0001ffff,    /* dst_mask  */
1413
   false),    /* pcrel_offset  */
1414
1415
  HOWTO2 (R_NDS32_SDA18S1_RELA, /* type  */
1416
   1,     /* rightshift  */
1417
   4,     /* size  */
1418
   18,      /* bitsize  */
1419
   false,     /* pc_relative  */
1420
   0,     /* bitpos  */
1421
   complain_overflow_signed,/* complain_on_overflow  */
1422
   bfd_elf_generic_reloc, /* special_function  */
1423
   "R_NDS32_SDA18S1_RELA",/* name  */
1424
   false,     /* partial_inplace  */
1425
   0x0003ffff,    /* src_mask  */
1426
   0x0003ffff,    /* dst_mask  */
1427
   false),    /* pcrel_offset  */
1428
1429
  HOWTO2 (R_NDS32_SDA19S0_RELA, /* type  */
1430
   0,     /* rightshift  */
1431
   4,     /* size  */
1432
   19,      /* bitsize  */
1433
   false,     /* pc_relative  */
1434
   0,     /* bitpos  */
1435
   complain_overflow_signed,/* complain_on_overflow  */
1436
   bfd_elf_generic_reloc, /* special_function  */
1437
   "R_NDS32_SDA19S0_RELA",/* name  */
1438
   false,     /* partial_inplace  */
1439
   0x0007ffff,    /* src_mask  */
1440
   0x0007ffff,    /* dst_mask  */
1441
   false),    /* pcrel_offset  */
1442
  HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type  */
1443
   0,     /* rightshift  */
1444
   1,     /* size  */
1445
   8,     /* bitsize  */
1446
   false,     /* pc_relative  */
1447
   0,     /* bitpos  */
1448
   complain_overflow_dont,/* complain_on_overflow  */
1449
   nds32_elf_ignore_reloc,/* special_function  */
1450
   "R_NDS32_DWARF2_OP1_RELA",/* name  */
1451
   false,     /* partial_inplace  */
1452
   0xff,      /* src_mask  */
1453
   0xff,      /* dst_mask  */
1454
   false),    /* pcrel_offset  */
1455
1456
  HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type  */
1457
   0,     /* rightshift  */
1458
   2,     /* size  */
1459
   16,      /* bitsize  */
1460
   false,     /* pc_relative  */
1461
   0,     /* bitpos  */
1462
   complain_overflow_dont,/* complain_on_overflow  */
1463
   nds32_elf_ignore_reloc,/* special_function  */
1464
   "R_NDS32_DWARF2_OP2_RELA",/* name  */
1465
   false,     /* partial_inplace  */
1466
   0xffff,    /* src_mask  */
1467
   0xffff,    /* dst_mask  */
1468
   false),    /* pcrel_offset  */
1469
1470
  HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type  */
1471
   0,     /* rightshift  */
1472
   4,     /* size  */
1473
   32,      /* bitsize  */
1474
   false,     /* pc_relative  */
1475
   0,     /* bitpos  */
1476
   complain_overflow_dont,/* complain_on_overflow  */
1477
   nds32_elf_ignore_reloc,/* special_function  */
1478
   "R_NDS32_DWARF2_LEB_RELA",/* name  */
1479
   false,     /* partial_inplace  */
1480
   0xffffffff,    /* src_mask  */
1481
   0xffffffff,    /* dst_mask  */
1482
   false),    /* pcrel_offset  */
1483
1484
  HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type  */
1485
   0,     /* rightshift  */
1486
   2,     /* size  */
1487
   16,      /* bitsize  */
1488
   false,     /* pc_relative  */
1489
   0,     /* bitpos  */
1490
   complain_overflow_dont,/* complain_on_overflow  */
1491
   nds32_elf_ignore_reloc,/* special_function  */
1492
   "R_NDS32_UPDATE_TA_RELA",/* name  */
1493
   false,     /* partial_inplace  */
1494
   0xffff,    /* src_mask  */
1495
   0xffff,    /* dst_mask  */
1496
   false),    /* pcrel_offset  */
1497
1498
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1499
     entry for the symbol.  */
1500
  HOWTO2 (R_NDS32_9_PLTREL, /* type  */
1501
   1,     /* rightshift  */
1502
   2,     /* size  */
1503
   8,     /* bitsize  */
1504
   true,      /* pc_relative  */
1505
   0,     /* bitpos  */
1506
   complain_overflow_signed,/* complain_on_overflow  */
1507
   bfd_elf_generic_reloc, /* special_function  */
1508
   "R_NDS32_9_PLTREL",  /* name  */
1509
   false,     /* partial_inplace  */
1510
   0xff,      /* src_mask  */
1511
   0xff,      /* dst_mask  */
1512
   true),     /* pcrel_offset  */
1513
1514
  /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
1515
  HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type  */
1516
   0,     /* rightshift  */
1517
   4,     /* size  */
1518
   20,      /* bitsize  */
1519
   false,     /* pc_relative  */
1520
   0,     /* bitpos  */
1521
   complain_overflow_dont,/* complain_on_overflow  */
1522
   bfd_elf_generic_reloc, /* special_function  */
1523
   "R_NDS32_PLT_GOTREL_LO20",/* name  */
1524
   false,     /* partial_inplace  */
1525
   0x000fffff,    /* src_mask  */
1526
   0x000fffff,    /* dst_mask  */
1527
   false),    /* pcrel_offset  */
1528
1529
  /* low 15 bits of PLT symbol offset relative to GOT (GP)  */
1530
  HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type  */
1531
   0,     /* rightshift  */
1532
   4,     /* size  */
1533
   15,      /* bitsize  */
1534
   false,     /* pc_relative  */
1535
   0,     /* bitpos  */
1536
   complain_overflow_dont,/* complain_on_overflow  */
1537
   bfd_elf_generic_reloc, /* special_function  */
1538
   "R_NDS32_PLT_GOTREL_LO15",/* name  */
1539
   false,     /* partial_inplace  */
1540
   0x00007fff,    /* src_mask  */
1541
   0x00007fff,    /* dst_mask  */
1542
   false),    /* pcrel_offset  */
1543
1544
  /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
1545
  HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type  */
1546
   0,     /* rightshift  */
1547
   4,     /* size  */
1548
   19,      /* bitsize  */
1549
   false,     /* pc_relative  */
1550
   0,     /* bitpos  */
1551
   complain_overflow_dont,/* complain_on_overflow  */
1552
   bfd_elf_generic_reloc, /* special_function  */
1553
   "R_NDS32_PLT_GOTREL_LO19",/* name  */
1554
   false,     /* partial_inplace  */
1555
   0x0007ffff,    /* src_mask  */
1556
   0x0007ffff,    /* dst_mask  */
1557
   false),    /* pcrel_offset  */
1558
1559
  HOWTO2 (R_NDS32_GOT_LO15, /* type  */
1560
   0,     /* rightshift  */
1561
   4,     /* size  */
1562
   15,      /* bitsize  */
1563
   false,     /* pc_relative  */
1564
   0,     /* bitpos  */
1565
   complain_overflow_dont,/* complain_on_overflow  */
1566
   bfd_elf_generic_reloc, /* special_function  */
1567
   "R_NDS32_GOT_LO15",  /* name  */
1568
   false,     /* partial_inplace  */
1569
   0x00007fff,    /* src_mask  */
1570
   0x00007fff,    /* dst_mask  */
1571
   false),    /* pcrel_offset  */
1572
1573
  HOWTO2 (R_NDS32_GOT_LO19, /* type  */
1574
   0,     /* rightshift  */
1575
   4,     /* size  */
1576
   19,      /* bitsize  */
1577
   false,     /* pc_relative  */
1578
   0,     /* bitpos  */
1579
   complain_overflow_dont,/* complain_on_overflow  */
1580
   bfd_elf_generic_reloc, /* special_function  */
1581
   "R_NDS32_GOT_LO19",  /* name  */
1582
   false,     /* partial_inplace  */
1583
   0x0007ffff,    /* src_mask  */
1584
   0x0007ffff,    /* dst_mask  */
1585
   false),    /* pcrel_offset  */
1586
1587
  HOWTO2 (R_NDS32_GOTOFF_LO15,  /* type  */
1588
   0,     /* rightshift  */
1589
   4,     /* size  */
1590
   15,      /* bitsize  */
1591
   false,     /* pc_relative  */
1592
   0,     /* bitpos  */
1593
   complain_overflow_dont,/* complain_on_overflow  */
1594
   bfd_elf_generic_reloc, /* special_function  */
1595
   "R_NDS32_GOTOFF_LO15", /* name  */
1596
   false,     /* partial_inplace  */
1597
   0x00007fff,    /* src_mask  */
1598
   0x00007fff,    /* dst_mask  */
1599
   false),    /* pcrel_offset  */
1600
1601
  HOWTO2 (R_NDS32_GOTOFF_LO19,  /* type  */
1602
   0,     /* rightshift  */
1603
   4,     /* size  */
1604
   19,      /* bitsize  */
1605
   false,     /* pc_relative  */
1606
   0,     /* bitpos  */
1607
   complain_overflow_dont,/* complain_on_overflow  */
1608
   bfd_elf_generic_reloc, /* special_function  */
1609
   "R_NDS32_GOTOFF_LO19", /* name  */
1610
   false,     /* partial_inplace  */
1611
   0x0007ffff,    /* src_mask  */
1612
   0x0007ffff,    /* dst_mask  */
1613
   false),    /* pcrel_offset  */
1614
1615
  /* GOT 15 bits offset.  */
1616
  HOWTO2 (R_NDS32_GOT15S2_RELA, /* type  */
1617
   2,     /* rightshift  */
1618
   4,     /* size  */
1619
   15,      /* bitsize  */
1620
   false,     /* pc_relative  */
1621
   0,     /* bitpos  */
1622
   complain_overflow_signed,/* complain_on_overflow  */
1623
   bfd_elf_generic_reloc, /* special_function  */
1624
   "R_NDS32_GOT15S2_RELA",/* name  */
1625
   false,     /* partial_inplace  */
1626
   0x00007fff,    /* src_mask  */
1627
   0x00007fff,    /* dst_mask  */
1628
   false),    /* pcrel_offset  */
1629
1630
  /* GOT 17 bits offset.  */
1631
  HOWTO2 (R_NDS32_GOT17S2_RELA, /* type  */
1632
   2,     /* rightshift  */
1633
   4,     /* size  */
1634
   17,      /* bitsize  */
1635
   false,     /* pc_relative  */
1636
   0,     /* bitpos  */
1637
   complain_overflow_signed,/* complain_on_overflow  */
1638
   bfd_elf_generic_reloc, /* special_function  */
1639
   "R_NDS32_GOT17S2_RELA",/* name  */
1640
   false,     /* partial_inplace  */
1641
   0x0001ffff,    /* src_mask  */
1642
   0x0001ffff,    /* dst_mask  */
1643
   false),    /* pcrel_offset  */
1644
1645
  /* A 5 bit address.  */
1646
  HOWTO2 (R_NDS32_5_RELA, /* type  */
1647
   0,     /* rightshift  */
1648
   2,     /* size  */
1649
   5,     /* bitsize  */
1650
   false,     /* pc_relative  */
1651
   0,     /* bitpos  */
1652
   complain_overflow_signed,/* complain_on_overflow  */
1653
   bfd_elf_generic_reloc, /* special_function  */
1654
   "R_NDS32_5_RELA",  /* name  */
1655
   false,     /* partial_inplace  */
1656
   0x1f,      /* src_mask  */
1657
   0x1f,      /* dst_mask  */
1658
   false),    /* pcrel_offset  */
1659
1660
  HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type  */
1661
   1,     /* rightshift  */
1662
   2,     /* size  */
1663
   9,     /* bitsize  */
1664
   true,      /* pc_relative  */
1665
   0,     /* bitpos  */
1666
   complain_overflow_unsigned,/* complain_on_overflow  */
1667
   bfd_elf_generic_reloc, /* special_function  */
1668
   "R_NDS32_10_UPCREL_RELA",/* name  */
1669
   false,     /* partial_inplace  */
1670
   0x1ff,     /* src_mask  */
1671
   0x1ff,     /* dst_mask  */
1672
   true),     /* pcrel_offset  */
1673
1674
  HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type  */
1675
   2,     /* rightshift  */
1676
   2,     /* size  */
1677
   7,     /* bitsize  */
1678
   false,     /* pc_relative  */
1679
   0,     /* bitpos  */
1680
   complain_overflow_unsigned,/* complain_on_overflow  */
1681
   bfd_elf_generic_reloc, /* special_function  */
1682
   "R_NDS32_SDA_FP7U2_RELA",/* name  */
1683
   false,     /* partial_inplace  */
1684
   0x0000007f,    /* src_mask  */
1685
   0x0000007f,    /* dst_mask  */
1686
   false),    /* pcrel_offset  */
1687
1688
  HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type  */
1689
   1,     /* rightshift  */
1690
   4,     /* size  */
1691
   8,     /* bitsize  */
1692
   true,      /* pc_relative  */
1693
   0,     /* bitpos  */
1694
   complain_overflow_signed,/* complain_on_overflow  */
1695
   bfd_elf_generic_reloc, /* special_function  */
1696
   "R_NDS32_WORD_9_PCREL_RELA",/* name  */
1697
   false,     /* partial_inplace  */
1698
   0xff,      /* src_mask  */
1699
   0xff,      /* dst_mask  */
1700
   true),     /* pcrel_offset  */
1701
1702
  HOWTO2 (R_NDS32_25_ABS_RELA,  /* type  */
1703
   1,     /* rightshift  */
1704
   4,     /* size  */
1705
   24,      /* bitsize  */
1706
   false,     /* pc_relative  */
1707
   0,     /* bitpos  */
1708
   complain_overflow_dont,/* complain_on_overflow  */
1709
   bfd_elf_generic_reloc, /* special_function  */
1710
   "R_NDS32_25_ABS_RELA", /* name  */
1711
   false,     /* partial_inplace  */
1712
   0xffffff,    /* src_mask  */
1713
   0xffffff,    /* dst_mask  */
1714
   false),    /* pcrel_offset  */
1715
1716
  /* A relative 17 bit relocation for ifc, right shifted by 1.  */
1717
  HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type  */
1718
   1,     /* rightshift  */
1719
   4,     /* size  */
1720
   16,      /* bitsize  */
1721
   true,      /* pc_relative  */
1722
   0,     /* bitpos  */
1723
   complain_overflow_signed,/* complain_on_overflow  */
1724
   bfd_elf_generic_reloc, /* special_function  */
1725
   "R_NDS32_17IFC_PCREL_RELA",/* name  */
1726
   false,     /* partial_inplace  */
1727
   0xffff,    /* src_mask  */
1728
   0xffff,    /* dst_mask  */
1729
   true),     /* pcrel_offset  */
1730
1731
  /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
1732
  HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type  */
1733
   1,     /* rightshift  */
1734
   2,     /* size  */
1735
   9,     /* bitsize  */
1736
   true,      /* pc_relative  */
1737
   0,     /* bitpos  */
1738
   complain_overflow_unsigned,/* complain_on_overflow  */
1739
   bfd_elf_generic_reloc, /* special_function  */
1740
   "R_NDS32_10IFCU_PCREL_RELA",/* name  */
1741
   false,     /* partial_inplace  */
1742
   0x1ff,     /* src_mask  */
1743
   0x1ff,     /* dst_mask  */
1744
   true),     /* pcrel_offset  */
1745
1746
  /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol.  */
1747
  HOWTO2 (R_NDS32_TLS_LE_HI20,  /* type  */
1748
   12,      /* rightshift  */
1749
   4,     /* size  */
1750
   20,      /* bitsize  */
1751
   false,     /* pc_relative  */
1752
   0,     /* bitpos  */
1753
   complain_overflow_dont,/* complain_on_overflow  */
1754
   bfd_elf_generic_reloc, /* special_function  */
1755
   "R_NDS32_TLS_LE_HI20", /* name  */
1756
   false,     /* partial_inplace  */
1757
   0x000fffff,    /* src_mask  */
1758
   0x000fffff,    /* dst_mask  */
1759
   false),    /* pcrel_offset  */
1760
1761
  HOWTO2 (R_NDS32_TLS_LE_LO12,  /* type  */
1762
   0,     /* rightshift  */
1763
   4,     /* size  */
1764
   12,      /* bitsize  */
1765
   false,     /* pc_relative  */
1766
   0,     /* bitpos  */
1767
   complain_overflow_dont,/* complain_on_overflow  */
1768
   bfd_elf_generic_reloc, /* special_function  */
1769
   "R_NDS32_TLS_LE_LO12", /* name  */
1770
   false,     /* partial_inplace  */
1771
   0x00000fff,    /* src_mask  */
1772
   0x00000fff,    /* dst_mask  */
1773
   false),    /* pcrel_offset  */
1774
1775
  /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol.  */
1776
  HOWTO2 (R_NDS32_TLS_IE_HI20,  /* type  */
1777
   12,      /* rightshift  */
1778
   4,     /* size  */
1779
   20,      /* bitsize  */
1780
   false,     /* pc_relative  */
1781
   0,     /* bitpos  */
1782
   complain_overflow_dont,/* complain_on_overflow  */
1783
   bfd_elf_generic_reloc, /* special_function  */
1784
   "R_NDS32_TLS_IE_HI20", /* name  */
1785
   false,     /* partial_inplace  */
1786
   0x000fffff,    /* src_mask  */
1787
   0x000fffff,    /* dst_mask  */
1788
   false),    /* pcrel_offset  */
1789
1790
  HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type  */
1791
   2,     /* rightshift  */
1792
   4,     /* size  */
1793
   10,      /* bitsize  */
1794
   false,     /* pc_relative  */
1795
   0,     /* bitpos  */
1796
   complain_overflow_dont,/* complain_on_overflow  */
1797
   bfd_elf_generic_reloc, /* special_function  */
1798
   "R_NDS32_TLS_IE_LO12S2",/* name  */
1799
   false,     /* partial_inplace  */
1800
   0x000003ff,    /* src_mask  */
1801
   0x000003ff,    /* dst_mask  */
1802
   false),    /* pcrel_offset  */
1803
1804
  /* TLS LE TP offset relocation  */
1805
  HOWTO2 (R_NDS32_TLS_TPOFF,  /* type  */
1806
   0,     /* rightshift  */
1807
   4,     /* size  */
1808
   32,      /* bitsize  */
1809
   false,     /* pc_relative  */
1810
   0,     /* bitpos  */
1811
   complain_overflow_bitfield,/* complain_on_overflow  */
1812
   bfd_elf_generic_reloc, /* special_function  */
1813
   "R_NDS32_TLS_TPOFF", /* name  */
1814
   false,     /* partial_inplace  */
1815
   0xffffffff,    /* src_mask  */
1816
   0xffffffff,    /* dst_mask  */
1817
   false),    /* pcrel_offset  */
1818
1819
  /* A 20 bit address.  */
1820
  HOWTO2 (R_NDS32_TLS_LE_20,  /* type  */
1821
   0,     /* rightshift  */
1822
   4,     /* size  */
1823
   20,      /* bitsize  */
1824
   false,     /* pc_relative  */
1825
   0,     /* bitpos  */
1826
   complain_overflow_signed,/* complain_on_overflow  */
1827
   bfd_elf_generic_reloc, /* special_function  */
1828
   "R_NDS32_TLS_LE_20", /* name  */
1829
   false,     /* partial_inplace  */
1830
   0xfffff,   /* src_mask  */
1831
   0xfffff,   /* dst_mask  */
1832
   false),    /* pcrel_offset  */
1833
1834
  HOWTO2 (R_NDS32_TLS_LE_15S0,  /* type  */
1835
   0,     /* rightshift  */
1836
   4,     /* size  */
1837
   15,      /* bitsize  */
1838
   false,     /* pc_relative  */
1839
   0,     /* bitpos  */
1840
   complain_overflow_signed,/* complain_on_overflow  */
1841
   bfd_elf_generic_reloc, /* special_function  */
1842
   "R_NDS32_TLS_LE_15S0", /* name  */
1843
   false,     /* partial_inplace  */
1844
   0x7fff,    /* src_mask  */
1845
   0x7fff,    /* dst_mask  */
1846
   false),    /* pcrel_offset  */
1847
1848
  HOWTO2 (R_NDS32_TLS_LE_15S1,  /* type  */
1849
   1,     /* rightshift  */
1850
   4,     /* size  */
1851
   15,      /* bitsize  */
1852
   false,     /* pc_relative  */
1853
   0,     /* bitpos  */
1854
   complain_overflow_signed,/* complain_on_overflow  */
1855
   bfd_elf_generic_reloc, /* special_function  */
1856
   "R_NDS32_TLS_LE_15S1", /* name  */
1857
   false,     /* partial_inplace  */
1858
   0x7fff,    /* src_mask  */
1859
   0x7fff,    /* dst_mask  */
1860
   false),    /* pcrel_offset  */
1861
1862
  HOWTO2 (R_NDS32_TLS_LE_15S2,  /* type  */
1863
   2,     /* rightshift  */
1864
   4,     /* size  */
1865
   15,      /* bitsize  */
1866
   false,     /* pc_relative  */
1867
   0,     /* bitpos  */
1868
   complain_overflow_signed,/* complain_on_overflow  */
1869
   bfd_elf_generic_reloc, /* special_function  */
1870
   "R_NDS32_TLS_LE_15S2", /* name  */
1871
   false,     /* partial_inplace  */
1872
   0x7fff,    /* src_mask  */
1873
   0x7fff,    /* dst_mask  */
1874
   false),    /* pcrel_offset  */
1875
1876
  /* Relax hint for unconditional call sequence  */
1877
  HOWTO2 (R_NDS32_LONGCALL4,  /* type  */
1878
   0,     /* rightshift  */
1879
   4,     /* size  */
1880
   32,      /* bitsize  */
1881
   false,     /* pc_relative  */
1882
   0,     /* bitpos  */
1883
   complain_overflow_dont,/* complain_on_overflow  */
1884
   nds32_elf_ignore_reloc,/* special_function  */
1885
   "R_NDS32_LONGCALL4", /* name  */
1886
   false,     /* partial_inplace  */
1887
   0xffffffff,    /* src_mask  */
1888
   0xffffffff,    /* dst_mask  */
1889
   false),    /* pcrel_offset  */
1890
1891
  /* Relax hint for conditional call sequence.  */
1892
  HOWTO2 (R_NDS32_LONGCALL5,  /* type  */
1893
   0,     /* rightshift  */
1894
   4,     /* size  */
1895
   32,      /* bitsize  */
1896
   false,     /* pc_relative  */
1897
   0,     /* bitpos  */
1898
   complain_overflow_dont,/* complain_on_overflow  */
1899
   nds32_elf_ignore_reloc,/* special_function  */
1900
   "R_NDS32_LONGCALL5", /* name  */
1901
   false,     /* partial_inplace  */
1902
   0xffffffff,    /* src_mask  */
1903
   0xffffffff,    /* dst_mask  */
1904
   false),    /* pcrel_offset  */
1905
1906
  /* Relax hint for conditional call sequence.  */
1907
  HOWTO2 (R_NDS32_LONGCALL6,  /* type  */
1908
   0,     /* rightshift  */
1909
   4,     /* size  */
1910
   32,      /* bitsize  */
1911
   false,     /* pc_relative  */
1912
   0,     /* bitpos  */
1913
   complain_overflow_dont,/* complain_on_overflow  */
1914
   nds32_elf_ignore_reloc,/* special_function  */
1915
   "R_NDS32_LONGCALL6", /* name  */
1916
   false,     /* partial_inplace  */
1917
   0xffffffff,    /* src_mask  */
1918
   0xffffffff,    /* dst_mask  */
1919
   false),    /* pcrel_offset  */
1920
1921
  /* Relax hint for unconditional branch sequence.  */
1922
  HOWTO2 (R_NDS32_LONGJUMP4,  /* type  */
1923
   0,     /* rightshift  */
1924
   4,     /* size  */
1925
   32,      /* bitsize  */
1926
   false,     /* pc_relative  */
1927
   0,     /* bitpos  */
1928
   complain_overflow_dont,/* complain_on_overflow  */
1929
   nds32_elf_ignore_reloc,/* special_function  */
1930
   "R_NDS32_LONGJUMP4", /* name  */
1931
   false,     /* partial_inplace  */
1932
   0xffffffff,    /* src_mask  */
1933
   0xffffffff,    /* dst_mask  */
1934
   false),    /* pcrel_offset  */
1935
1936
  /* Relax hint for conditional branch sequence.  */
1937
  HOWTO2 (R_NDS32_LONGJUMP5,  /* type  */
1938
   0,     /* rightshift  */
1939
   4,     /* size  */
1940
   32,      /* bitsize  */
1941
   false,     /* pc_relative  */
1942
   0,     /* bitpos  */
1943
   complain_overflow_dont,/* complain_on_overflow  */
1944
   nds32_elf_ignore_reloc,/* special_function  */
1945
   "R_NDS32_LONGJUMP5", /* name  */
1946
   false,     /* partial_inplace  */
1947
   0xffffffff,    /* src_mask  */
1948
   0xffffffff,    /* dst_mask  */
1949
   false),    /* pcrel_offset  */
1950
1951
  /* Relax hint for conditional branch sequence.  */
1952
  HOWTO2 (R_NDS32_LONGJUMP6,  /* type  */
1953
   0,     /* rightshift  */
1954
   4,     /* size  */
1955
   32,      /* bitsize  */
1956
   false,     /* pc_relative  */
1957
   0,     /* bitpos  */
1958
   complain_overflow_dont,/* complain_on_overflow  */
1959
   nds32_elf_ignore_reloc,/* special_function  */
1960
   "R_NDS32_LONGJUMP6", /* name  */
1961
   false,     /* partial_inplace  */
1962
   0xffffffff,    /* src_mask  */
1963
   0xffffffff,    /* dst_mask  */
1964
   false),    /* pcrel_offset  */
1965
1966
  /* Relax hint for conditional branch sequence.  */
1967
  HOWTO2 (R_NDS32_LONGJUMP7,  /* type  */
1968
   0,     /* rightshift  */
1969
   4,     /* size  */
1970
   32,      /* bitsize  */
1971
   false,     /* pc_relative  */
1972
   0,     /* bitpos  */
1973
   complain_overflow_dont,/* complain_on_overflow  */
1974
   nds32_elf_ignore_reloc,/* special_function  */
1975
   "R_NDS32_LONGJUMP7", /* name  */
1976
   false,     /* partial_inplace  */
1977
   0xffffffff,    /* src_mask  */
1978
   0xffffffff,    /* dst_mask  */
1979
   false),    /* pcrel_offset  */
1980
1981
  EMPTY_HOWTO (114),
1982
1983
  HOWTO2 (R_NDS32_TLS_IE_LO12,  /* type  */
1984
   0,     /* rightshift  */
1985
   4,     /* size  */
1986
   12,      /* bitsize  */
1987
   false,     /* pc_relative  */
1988
   0,     /* bitpos  */
1989
   complain_overflow_dont,/* complain_on_overflow  */
1990
   bfd_elf_generic_reloc, /* special_function  */
1991
   "R_NDS32_TLS_IE_LO12", /* name  */
1992
   false,     /* partial_inplace  */
1993
   0x00000fff,    /* src_mask  */
1994
   0x00000fff,    /* dst_mask  */
1995
   false),    /* pcrel_offset  */
1996
1997
  /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1998
     entry for the symbol.  */
1999
  HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type  */
2000
   12,      /* rightshift  */
2001
   4,     /* size  */
2002
   20,      /* bitsize  */
2003
   false,     /* pc_relative  */
2004
   0,     /* bitpos  */
2005
   complain_overflow_dont,/* complain_on_overflow  */
2006
   bfd_elf_generic_reloc, /* special_function  */
2007
   "R_NDS32_TLS_IEGP_HI20",/* name  */
2008
   false,     /* partial_inplace  */
2009
   0x000fffff,    /* src_mask  */
2010
   0x000fffff,    /* dst_mask  */
2011
   false),    /* pcrel_offset  */
2012
2013
  HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type  */
2014
   0,     /* rightshift  */
2015
   4,     /* size  */
2016
   12,      /* bitsize  */
2017
   false,     /* pc_relative  */
2018
   0,     /* bitpos  */
2019
   complain_overflow_dont,/* complain_on_overflow  */
2020
   bfd_elf_generic_reloc, /* special_function  */
2021
   "R_NDS32_TLS_IEGP_LO12",/* name  */
2022
   false,     /* partial_inplace  */
2023
   0x00000fff,    /* src_mask  */
2024
   0x00000fff,    /* dst_mask  */
2025
   false),    /* pcrel_offset  */
2026
2027
  HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type  */
2028
   2,     /* rightshift  */
2029
   4,     /* size  */
2030
   10,      /* bitsize  */
2031
   false,     /* pc_relative  */
2032
   0,     /* bitpos  */
2033
   complain_overflow_dont,/* complain_on_overflow  */
2034
   bfd_elf_generic_reloc, /* special_function  */
2035
   "R_NDS32_TLS_IEGP_LO12S2",/* name  */
2036
   false,     /* partial_inplace  */
2037
   0x000003ff,    /* src_mask  */
2038
   0x000003ff,    /* dst_mask  */
2039
   false),    /* pcrel_offset  */
2040
2041
  /* TLS description relocation  */
2042
  HOWTO2 (R_NDS32_TLS_DESC, /* type  */
2043
   12,      /* rightshift  */
2044
   4,     /* size  */
2045
   20,      /* bitsize  */
2046
   false,     /* pc_relative  */
2047
   0,     /* bitpos  */
2048
   complain_overflow_dont,/* complain_on_overflow  */
2049
   nds32_elf_hi20_reloc,  /* special_function  */
2050
   "R_NDS32_TLS_DESC_HI20",/* name  */
2051
   false,     /* partial_inplace  */
2052
   0x000fffff,    /* src_mask  */
2053
   0x000fffff,    /* dst_mask  */
2054
   false),    /* pcrel_offset  */
2055
2056
  /* TLS GD/LD description offset high part.  */
2057
  HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type  */
2058
   12,      /* rightshift  */
2059
   4,     /* size  */
2060
   20,      /* bitsize  */
2061
   false,     /* pc_relative  */
2062
   0,     /* bitpos  */
2063
   complain_overflow_dont,/* complain_on_overflow  */
2064
   nds32_elf_hi20_reloc,  /* special_function  */
2065
   "R_NDS32_TLS_DESC_HI20",/* name  */
2066
   false,     /* partial_inplace  */
2067
   0x000fffff,    /* src_mask  */
2068
   0x000fffff,    /* dst_mask  */
2069
   false),    /* pcrel_offset  */
2070
2071
  /* TLS GD/LD description offset low part.  */
2072
  HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type  */
2073
   0,     /* rightshift  */
2074
   4,     /* size  */
2075
   12,      /* bitsize  */
2076
   false,     /* pc_relative  */
2077
   0,     /* bitpos  */
2078
   complain_overflow_dont,/* complain_on_overflow  */
2079
   nds32_elf_lo12_reloc,  /* special_function  */
2080
   "R_NDS32_TLS_DESC_LO12",/* name  */
2081
   false,     /* partial_inplace  */
2082
   0x00000fff,    /* src_mask  */
2083
   0x00000fff,    /* dst_mask  */
2084
   false),    /* pcrel_offset  */
2085
2086
  /* TLS GD/LD description offset set (movi).  */
2087
  HOWTO2 (R_NDS32_TLS_DESC_20,  /* type  */
2088
   0,     /* rightshift  */
2089
   4,     /* size  */
2090
   20,      /* bitsize  */
2091
   false,     /* pc_relative  */
2092
   0,     /* bitpos  */
2093
   complain_overflow_signed,/* complain_on_overflow  */
2094
   bfd_elf_generic_reloc, /* special_function  */
2095
   "R_NDS32_TLS_DESC_20", /* name  */
2096
   false,     /* partial_inplace  */
2097
   0x000fffff,    /* src_mask  */
2098
   0x000fffff,    /* dst_mask  */
2099
   false),    /* pcrel_offset  */
2100
2101
  /* TLS GD/LD description offset set (lwi.gp).  */
2102
  HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type  */
2103
   2,     /* rightshift  */
2104
   4,     /* size  */
2105
   17,      /* bitsize  */
2106
   false,     /* pc_relative  */
2107
   0,     /* bitpos  */
2108
   complain_overflow_signed,/* complain_on_overflow  */
2109
   bfd_elf_generic_reloc, /* special_function  */
2110
   "R_NDS32_TLS_DESC_SDA17S2",/* name  */
2111
   false,     /* partial_inplace  */
2112
   0x0001ffff,    /* src_mask  */
2113
   0x0001ffff,    /* dst_mask  */
2114
   false),    /* pcrel_offset  */
2115
};
2116
2117
/* Relocations used for relaxation.  */
2118
#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2119
  [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2120
2121
static reloc_howto_type nds32_elf_relax_howto_table[] = {
2122
  HOWTO3 (R_NDS32_RELAX_ENTRY,  /* type  */
2123
   0,     /* rightshift  */
2124
   4,     /* size  */
2125
   32,      /* bitsize  */
2126
   false,     /* pc_relative  */
2127
   0,     /* bitpos  */
2128
   complain_overflow_dont,/* complain_on_overflow  */
2129
   nds32_elf_ignore_reloc,/* special_function  */
2130
   "R_NDS32_RELAX_ENTRY", /* name  */
2131
   false,     /* partial_inplace  */
2132
   0xffffffff,    /* src_mask  */
2133
   0xffffffff,    /* dst_mask  */
2134
   false),    /* pcrel_offset  */
2135
2136
  HOWTO3 (R_NDS32_GOT_SUFF, /* type  */
2137
   0,     /* rightshift  */
2138
   4,     /* size  */
2139
   32,      /* bitsize  */
2140
   false,     /* pc_relative  */
2141
   0,     /* bitpos  */
2142
   complain_overflow_dont,/* complain_on_overflow  */
2143
   nds32_elf_ignore_reloc,/* special_function  */
2144
   "R_NDS32_GOT_SUFF",  /* name  */
2145
   false,     /* partial_inplace  */
2146
   0xffffffff,    /* src_mask  */
2147
   0xffffffff,    /* dst_mask  */
2148
   false),    /* pcrel_offset  */
2149
2150
  HOWTO3 (R_NDS32_GOTOFF_SUFF,  /* type  */
2151
   0,     /* rightshift  */
2152
   4,     /* size  */
2153
   32,      /* bitsize  */
2154
   false,     /* pc_relative  */
2155
   0,     /* bitpos  */
2156
   complain_overflow_bitfield,/* complain_on_overflow  */
2157
   nds32_elf_ignore_reloc,/* special_function  */
2158
   "R_NDS32_GOTOFF_SUFF", /* name  */
2159
   false,     /* partial_inplace  */
2160
   0xffffffff,    /* src_mask  */
2161
   0xffffffff,    /* dst_mask  */
2162
   false),    /* pcrel_offset  */
2163
2164
  HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type  */
2165
   0,     /* rightshift  */
2166
   4,     /* size  */
2167
   32,      /* bitsize  */
2168
   false,     /* pc_relative  */
2169
   0,     /* bitpos  */
2170
   complain_overflow_dont,/* complain_on_overflow  */
2171
   nds32_elf_ignore_reloc,/* special_function  */
2172
   "R_NDS32_PLT_GOT_SUFF",/* name  */
2173
   false,     /* partial_inplace  */
2174
   0xffffffff,    /* src_mask  */
2175
   0xffffffff,    /* dst_mask  */
2176
   false),    /* pcrel_offset  */
2177
2178
  HOWTO3 (R_NDS32_MULCALL_SUFF, /* type  */
2179
   0,     /* rightshift  */
2180
   4,     /* size  */
2181
   32,      /* bitsize  */
2182
   false,     /* pc_relative  */
2183
   0,     /* bitpos  */
2184
   complain_overflow_dont,/* complain_on_overflow  */
2185
   nds32_elf_ignore_reloc,/* special_function  */
2186
   "R_NDS32_MULCALL_SUFF",/* name  */
2187
   false,     /* partial_inplace  */
2188
   0xffffffff,    /* src_mask  */
2189
   0xffffffff,    /* dst_mask  */
2190
   false),    /* pcrel_offset  */
2191
2192
  HOWTO3 (R_NDS32_PTR,    /* type  */
2193
   0,     /* rightshift  */
2194
   4,     /* size  */
2195
   32,      /* bitsize  */
2196
   false,     /* pc_relative  */
2197
   0,     /* bitpos  */
2198
   complain_overflow_dont,/* complain_on_overflow  */
2199
   nds32_elf_ignore_reloc,/* special_function  */
2200
   "R_NDS32_PTR",   /* name  */
2201
   false,     /* partial_inplace  */
2202
   0xffffffff,    /* src_mask  */
2203
   0xffffffff,    /* dst_mask  */
2204
   false),    /* pcrel_offset  */
2205
2206
  HOWTO3 (R_NDS32_PTR_COUNT,  /* type  */
2207
   0,     /* rightshift  */
2208
   4,     /* size  */
2209
   32,      /* bitsize  */
2210
   false,     /* pc_relative  */
2211
   0,     /* bitpos  */
2212
   complain_overflow_dont,/* complain_on_overflow  */
2213
   nds32_elf_ignore_reloc,/* special_function  */
2214
   "R_NDS32_PTR_COUNT", /* name  */
2215
   false,     /* partial_inplace  */
2216
   0xffffffff,    /* src_mask  */
2217
   0xffffffff,    /* dst_mask  */
2218
   false),    /* pcrel_offset  */
2219
2220
  HOWTO3 (R_NDS32_PTR_RESOLVED, /* type  */
2221
   0,     /* rightshift  */
2222
   4,     /* size  */
2223
   32,      /* bitsize  */
2224
   false,     /* pc_relative  */
2225
   0,     /* bitpos  */
2226
   complain_overflow_dont,/* complain_on_overflow  */
2227
   nds32_elf_ignore_reloc,/* special_function  */
2228
   "R_NDS32_PTR_RESOLVED",/* name  */
2229
   false,     /* partial_inplace  */
2230
   0xffffffff,    /* src_mask  */
2231
   0xffffffff,    /* dst_mask  */
2232
   false),    /* pcrel_offset  */
2233
2234
  HOWTO3 (R_NDS32_PLTBLOCK, /* type  */
2235
   0,     /* rightshift  */
2236
   4,     /* size  */
2237
   32,      /* bitsize  */
2238
   false,     /* pc_relative  */
2239
   0,     /* bitpos  */
2240
   complain_overflow_dont,/* complain_on_overflow  */
2241
   nds32_elf_ignore_reloc,/* special_function  */
2242
   "R_NDS32_PLTBLOCK",  /* name  */
2243
   false,     /* partial_inplace  */
2244
   0xffffffff,    /* src_mask  */
2245
   0xffffffff,    /* dst_mask  */
2246
   false),    /* pcrel_offset  */
2247
2248
  HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type  */
2249
   0,     /* rightshift  */
2250
   4,     /* size  */
2251
   32,      /* bitsize  */
2252
   false,     /* pc_relative  */
2253
   0,     /* bitpos  */
2254
   complain_overflow_dont,/* complain_on_overflow  */
2255
   nds32_elf_ignore_reloc,/* special_function  */
2256
   "R_NDS32_RELAX_REGION_BEGIN",/* name  */
2257
   false,     /* partial_inplace  */
2258
   0xffffffff,    /* src_mask  */
2259
   0xffffffff,    /* dst_mask  */
2260
   false),    /* pcrel_offset  */
2261
2262
  HOWTO3 (R_NDS32_RELAX_REGION_END,/* type  */
2263
   0,     /* rightshift  */
2264
   4,     /* size  */
2265
   32,      /* bitsize  */
2266
   false,     /* pc_relative  */
2267
   0,     /* bitpos  */
2268
   complain_overflow_dont,/* complain_on_overflow  */
2269
   nds32_elf_ignore_reloc,/* special_function  */
2270
   "R_NDS32_RELAX_REGION_END",/* name  */
2271
   false,     /* partial_inplace  */
2272
   0xffffffff,    /* src_mask  */
2273
   0xffffffff,    /* dst_mask  */
2274
   false),    /* pcrel_offset  */
2275
2276
  HOWTO3 (R_NDS32_MINUEND,  /* type  */
2277
   0,     /* rightshift  */
2278
   4,     /* size  */
2279
   32,      /* bitsize  */
2280
   false,     /* pc_relative  */
2281
   0,     /* bitpos  */
2282
   complain_overflow_dont,/* complain_on_overflow  */
2283
   nds32_elf_ignore_reloc,/* special_function  */
2284
   "R_NDS32_MINUEND", /* name  */
2285
   false,     /* partial_inplace  */
2286
   0xffffffff,    /* src_mask  */
2287
   0xffffffff,    /* dst_mask  */
2288
   false),    /* pcrel_offset  */
2289
2290
  HOWTO3 (R_NDS32_SUBTRAHEND, /* type  */
2291
   0,     /* rightshift  */
2292
   4,     /* size  */
2293
   32,      /* bitsize  */
2294
   false,     /* pc_relative  */
2295
   0,     /* bitpos  */
2296
   complain_overflow_dont,/* complain_on_overflow  */
2297
   nds32_elf_ignore_reloc,/* special_function  */
2298
   "R_NDS32_SUBTRAHEND",  /* name  */
2299
   false,     /* partial_inplace  */
2300
   0xffffffff,    /* src_mask  */
2301
   0xffffffff,    /* dst_mask  */
2302
   false),    /* pcrel_offset  */
2303
2304
  HOWTO3 (R_NDS32_DIFF8,  /* type  */
2305
   0,     /* rightshift  */
2306
   1,     /* size  */
2307
   8,     /* bitsize  */
2308
   false,     /* pc_relative  */
2309
   0,     /* bitpos  */
2310
   complain_overflow_dont,/* complain_on_overflow  */
2311
   nds32_elf_ignore_reloc,/* special_function  */
2312
   "R_NDS32_DIFF8", /* name  */
2313
   false,     /* partial_inplace  */
2314
   0x000000ff,    /* src_mask  */
2315
   0x000000ff,    /* dst_mask  */
2316
   false),    /* pcrel_offset  */
2317
2318
  HOWTO3 (R_NDS32_DIFF16, /* type  */
2319
   0,     /* rightshift  */
2320
   2,     /* size  */
2321
   16,      /* bitsize  */
2322
   false,     /* pc_relative  */
2323
   0,     /* bitpos  */
2324
   complain_overflow_dont,/* complain_on_overflow  */
2325
   nds32_elf_ignore_reloc,/* special_function  */
2326
   "R_NDS32_DIFF16",  /* name  */
2327
   false,     /* partial_inplace  */
2328
   0x0000ffff,    /* src_mask  */
2329
   0x0000ffff,    /* dst_mask  */
2330
   false),    /* pcrel_offset  */
2331
2332
  HOWTO3 (R_NDS32_DIFF32, /* type  */
2333
   0,     /* rightshift  */
2334
   4,     /* size  */
2335
   32,      /* bitsize  */
2336
   false,     /* pc_relative  */
2337
   0,     /* bitpos  */
2338
   complain_overflow_dont,/* complain_on_overflow  */
2339
   nds32_elf_ignore_reloc,/* special_function  */
2340
   "R_NDS32_DIFF32",  /* name  */
2341
   false,     /* partial_inplace  */
2342
   0xffffffff,    /* src_mask  */
2343
   0xffffffff,    /* dst_mask  */
2344
   false),    /* pcrel_offset  */
2345
2346
  HOWTO3 (R_NDS32_DIFF_ULEB128, /* type  */
2347
   0,     /* rightshift  */
2348
   1,     /* size  */
2349
   0,     /* bitsize  */
2350
   false,     /* pc_relative  */
2351
   0,     /* bitpos  */
2352
   complain_overflow_dont,/* complain_on_overflow  */
2353
   nds32_elf_ignore_reloc,/* special_function  */
2354
   "R_NDS32_DIFF_ULEB128",/* name  */
2355
   false,     /* partial_inplace  */
2356
   0xffffffff,    /* src_mask  */
2357
   0xffffffff,    /* dst_mask  */
2358
   false),    /* pcrel_offset  */
2359
2360
  HOWTO3 (R_NDS32_DATA,   /* type  */
2361
   0,     /* rightshift  */
2362
   4,     /* size  */
2363
   32,      /* bitsize  */
2364
   false,     /* pc_relative  */
2365
   0,     /* bitpos  */
2366
   complain_overflow_dont,/* complain_on_overflow  */
2367
   nds32_elf_ignore_reloc,/* special_function  */
2368
   "R_NDS32_DATA",  /* name  */
2369
   false,     /* partial_inplace  */
2370
   0xffffffff,    /* src_mask  */
2371
   0xffffffff,    /* dst_mask  */
2372
   false),    /* pcrel_offset  */
2373
2374
  HOWTO3 (R_NDS32_TRAN,   /* type  */
2375
   0,     /* rightshift  */
2376
   4,     /* size  */
2377
   32,      /* bitsize  */
2378
   false,     /* pc_relative  */
2379
   0,     /* bitpos  */
2380
   complain_overflow_dont,/* complain_on_overflow  */
2381
   nds32_elf_ignore_reloc,/* special_function  */
2382
   "R_NDS32_TRAN",  /* name  */
2383
   false,     /* partial_inplace  */
2384
   0xffffffff,    /* src_mask  */
2385
   0xffffffff,    /* dst_mask  */
2386
   false),    /* pcrel_offset  */
2387
2388
  HOWTO3 (R_NDS32_TLS_LE_ADD, /* type  */
2389
   0,     /* rightshift  */
2390
   4,     /* size  */
2391
   32,      /* bitsize  */
2392
   false,     /* pc_relative  */
2393
   0,     /* bitpos  */
2394
   complain_overflow_dont,/* complain_on_overflow  */
2395
   nds32_elf_ignore_reloc,/* special_function  */
2396
   "R_NDS32_TLS_LE_ADD",  /* name  */
2397
   false,     /* partial_inplace  */
2398
   0xffffffff,    /* src_mask  */
2399
   0xffffffff,    /* dst_mask  */
2400
   false),    /* pcrel_offset  */
2401
2402
  HOWTO3 (R_NDS32_TLS_LE_LS,  /* type  */
2403
   0,     /* rightshift  */
2404
   4,     /* size  */
2405
   32,      /* bitsize  */
2406
   false,     /* pc_relative  */
2407
   0,     /* bitpos  */
2408
   complain_overflow_dont,/* complain_on_overflow  */
2409
   nds32_elf_ignore_reloc,/* special_function  */
2410
   "R_NDS32_TLS_LE_LS", /* name  */
2411
   false,     /* partial_inplace  */
2412
   0xffffffff,    /* src_mask  */
2413
   0xffffffff,    /* dst_mask  */
2414
   false),    /* pcrel_offset  */
2415
2416
  HOWTO3 (R_NDS32_EMPTY,  /* type  */
2417
   0,     /* rightshift  */
2418
   4,     /* size  */
2419
   32,      /* bitsize  */
2420
   false,     /* pc_relative  */
2421
   0,     /* bitpos  */
2422
   complain_overflow_dont,/* complain_on_overflow  */
2423
   nds32_elf_ignore_reloc,/* special_function  */
2424
   "R_NDS32_EMPTY", /* name  */
2425
   false,     /* partial_inplace  */
2426
   0xffffffff,    /* src_mask  */
2427
   0xffffffff,    /* dst_mask  */
2428
   false),    /* pcrel_offset  */
2429
2430
  /* TLS GD/LD description address base addition.  */
2431
  HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type  */
2432
   0,     /* rightshift  */
2433
   4,     /* size  */
2434
   32,      /* bitsize  */
2435
   false,     /* pc_relative  */
2436
   0,     /* bitpos  */
2437
   complain_overflow_dont,/* complain_on_overflow  */
2438
   nds32_elf_ignore_reloc,/* special_function  */
2439
   "R_NDS32_TLS_DESC_ADD",/* name  */
2440
   false,     /* partial_inplace  */
2441
   0xffffffff,    /* src_mask  */
2442
   0xffffffff,    /* dst_mask  */
2443
   false),    /* pcrel_offset  */
2444
2445
  /* TLS GD/LD description function load.  */
2446
  HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type  */
2447
   0,     /* rightshift  */
2448
   4,     /* size  */
2449
   32,      /* bitsize  */
2450
   false,     /* pc_relative  */
2451
   0,     /* bitpos  */
2452
   complain_overflow_dont,/* complain_on_overflow  */
2453
   nds32_elf_ignore_reloc,/* special_function  */
2454
   "R_NDS32_TLS_DESC_FUNC",/* name  */
2455
   false,     /* partial_inplace  */
2456
   0xffffffff,    /* src_mask  */
2457
   0xffffffff,    /* dst_mask  */
2458
   false),    /* pcrel_offset  */
2459
2460
  /* TLS DESC resolve function call.  */
2461
  HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type  */
2462
   0,     /* rightshift  */
2463
   4,     /* size  */
2464
   32,      /* bitsize  */
2465
   false,     /* pc_relative  */
2466
   0,     /* bitpos  */
2467
   complain_overflow_dont,/* complain_on_overflow  */
2468
   nds32_elf_ignore_reloc,/* special_function  */
2469
   "R_NDS32_TLS_DESC_CALL",/* name  */
2470
   false,     /* partial_inplace  */
2471
   0xffffffff,    /* src_mask  */
2472
   0xffffffff,    /* dst_mask  */
2473
   false),    /* pcrel_offset  */
2474
2475
  /* TLS DESC variable access.  */
2476
  HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type  */
2477
   0,     /* rightshift  */
2478
   4,     /* size  */
2479
   32,      /* bitsize  */
2480
   false,     /* pc_relative  */
2481
   0,     /* bitpos  */
2482
   complain_overflow_dont,/* complain_on_overflow  */
2483
   nds32_elf_ignore_reloc,/* special_function  */
2484
   "R_NDS32_TLS_DESC_MEM",/* name  */
2485
   false,     /* partial_inplace  */
2486
   0xffffffff,    /* src_mask  */
2487
   0xffffffff,    /* dst_mask  */
2488
   false),    /* pcrel_offset  */
2489
2490
  /* TLS GD/LD description mark (@tlsdec).  */
2491
  HOWTO3 (R_NDS32_RELAX_REMOVE, /* type  */
2492
   0,     /* rightshift  */
2493
   4,     /* size  */
2494
   32,      /* bitsize  */
2495
   false,     /* pc_relative  */
2496
   0,     /* bitpos  */
2497
   complain_overflow_dont,/* complain_on_overflow  */
2498
   nds32_elf_ignore_reloc,/* special_function  */
2499
   "R_NDS32_REMOVE",  /* name  */
2500
   false,     /* partial_inplace  */
2501
   0xffffffff,    /* src_mask  */
2502
   0xffffffff,    /* dst_mask  */
2503
   false),    /* pcrel_offset  */
2504
2505
  /* TLS GD/LD description mark (@tlsdec).  */
2506
  HOWTO3 (R_NDS32_RELAX_GROUP,  /* type  */
2507
   0,     /* rightshift  */
2508
   4,     /* size  */
2509
   32,      /* bitsize  */
2510
   false,     /* pc_relative  */
2511
   0,     /* bitpos  */
2512
   complain_overflow_dont,/* complain_on_overflow  */
2513
   nds32_elf_ignore_reloc,/* special_function  */
2514
   "R_NDS32_GROUP", /* name  */
2515
   false,     /* partial_inplace  */
2516
   0xffffffff,    /* src_mask  */
2517
   0xffffffff,    /* dst_mask  */
2518
   false),    /* pcrel_offset  */
2519
2520
  HOWTO3 (R_NDS32_TLS_IEGP_LW,  /* type  */
2521
   0,     /* rightshift  */
2522
   4,     /* size  */
2523
   32,      /* bitsize  */
2524
   false,     /* pc_relative  */
2525
   0,     /* bitpos  */
2526
   complain_overflow_dont,/* complain_on_overflow  */
2527
   nds32_elf_ignore_reloc,/* special_function  */
2528
   "R_NDS32_TLS_IEGP_LW", /* name  */
2529
   false,     /* partial_inplace  */
2530
   0xffffffff,    /* src_mask  */
2531
   0xffffffff,    /* dst_mask  */
2532
   false),    /* pcrel_offset  */
2533
2534
  /* LA and FLSI relaxation.  */
2535
  HOWTO3 (R_NDS32_LSI,    /* type  */
2536
   0,     /* rightshift  */
2537
   4,     /* size  */
2538
   32,      /* bitsize  */
2539
   false,     /* pc_relative  */
2540
   0,     /* bitpos  */
2541
   complain_overflow_dont,/* complain_on_overflow  */
2542
   nds32_elf_ignore_reloc,/* special_function  */
2543
   "R_NDS32_LSI",   /* name  */
2544
   false,     /* partial_inplace  */
2545
   0xffffffff,    /* src_mask  */
2546
   0xffffffff,    /* dst_mask  */
2547
   false),
2548
};
2549
2550
static unsigned long dl_tlsdesc_lazy_trampoline[] =
2551
{
2552
  0x46200000,     /* sethi $r2,#0x0      */
2553
  0x58210000,     /* ori $r2,$r2,#0x0    */
2554
  0x40217400,     /* add $r2,$r2,$gp     */
2555
  0x04210000,     /* lwi $r2,[$r2+#0x0]  */
2556
  0x46300000,     /* sethi $r3,#0x0      */
2557
  0x58318000,     /* ori $r3,$r3,#0x0    */
2558
  0x4031f400,     /* add $r3,$r3,$gp     */
2559
  0x4a000800,     /* jr $r2              */
2560
};
2561
2562
static void
2563
nds32_put_trampoline (void *contents, const unsigned long *template,
2564
          unsigned count)
2565
0
{
2566
0
  unsigned ix;
2567
2568
0
  for (ix = 0; ix != count; ix++)
2569
0
    {
2570
0
      unsigned long insn = template[ix];
2571
0
      bfd_putb32 (insn, (char *) contents + ix * 4);
2572
0
    }
2573
0
}
2574
2575
/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2576
   This prototype is the same as qsort ().  */
2577
2578
static void
2579
nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2580
          int (*compar) (const void *lhs, const void *rhs))
2581
0
{
2582
0
  char *ptr = (char *) base;
2583
0
  int i, j;
2584
0
  char tmp[sizeof (Elf_Internal_Rela)];
2585
2586
0
  BFD_ASSERT (size <= sizeof (tmp));
2587
2588
  /* If i is less than j, i is inserted before j.
2589
2590
     |---- j ----- i --------------|
2591
      \    / \      /
2592
   sorted   unsorted
2593
   */
2594
2595
0
  for (i = 1; i < (int) nmemb; i++)
2596
0
    {
2597
0
      for (j = (i - 1); j >= 0; j--)
2598
0
  if (compar (ptr + i * size, ptr + j * size) >= 0)
2599
0
    break;
2600
2601
0
      j++;
2602
2603
0
      if (i == j)
2604
0
  continue; /* i is in order.  */
2605
2606
0
      memcpy (tmp, ptr + i * size, size);
2607
0
      memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2608
0
      memcpy (ptr + j * size, tmp, size);
2609
0
    }
2610
0
}
2611
2612
/* Sort relocation by r_offset.
2613
2614
   We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2615
   algorithm.  Relocations at the same r_offset must keep their order.
2616
   For example, RELAX_ENTRY must be the very first relocation entry.
2617
2618
   Currently, this function implements insertion-sort.
2619
2620
   FIXME: If we already sort them in assembler, why bother sort them
2621
    here again?  */
2622
2623
static int
2624
compar_reloc (const void *lhs, const void *rhs)
2625
0
{
2626
0
  const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2627
0
  const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2628
2629
0
  if (l->r_offset > r->r_offset)
2630
0
    return 1;
2631
0
  else if (l->r_offset == r->r_offset)
2632
0
    return 0;
2633
0
  else
2634
0
    return -1;
2635
0
}
2636
2637
/* Functions listed below are only used for old relocs.
2638
     nds32_elf_9_pcrel_reloc
2639
     nds32_elf_do_9_pcrel_reloc
2640
     nds32_elf_hi20_reloc
2641
     nds32_elf_relocate_hi20
2642
     nds32_elf_lo12_reloc
2643
     nds32_elf_sda15_reloc
2644
     nds32_elf_generic_reloc.  */
2645
2646
/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
2647
2648
static bfd_reloc_status_type
2649
nds32_elf_9_pcrel_reloc (bfd *       abfd,
2650
       arelent *   reloc_entry,
2651
       asymbol *   symbol,
2652
       void *      data,
2653
       asection *  input_section,
2654
       bfd *       output_bfd,
2655
       char **     error_message ATTRIBUTE_UNUSED)
2656
0
{
2657
  /* This part is from bfd_elf_generic_reloc.  */
2658
0
  if (output_bfd != (bfd *) NULL
2659
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
2660
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2661
0
    {
2662
0
      reloc_entry->address += input_section->output_offset;
2663
0
      return bfd_reloc_ok;
2664
0
    }
2665
2666
0
  if (output_bfd != NULL)
2667
0
    {
2668
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
2669
0
      return bfd_reloc_continue;
2670
0
    }
2671
2672
0
  return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2673
0
             input_section,
2674
0
             data, reloc_entry->address,
2675
0
             symbol->section,
2676
0
             (symbol->value
2677
0
              + symbol->section->output_section->vma
2678
0
              + symbol->section->output_offset),
2679
0
             reloc_entry->addend);
2680
0
}
2681
2682
/* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
2683
0
#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2684
2685
static bfd_reloc_status_type
2686
nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
2687
          reloc_howto_type *  howto,
2688
          asection *          input_section,
2689
          bfd_byte *          data,
2690
          bfd_vma             offset,
2691
          asection *          symbol_section ATTRIBUTE_UNUSED,
2692
          bfd_vma             symbol_value,
2693
          bfd_vma             addend)
2694
0
{
2695
0
  bfd_vma relocation;
2696
0
  unsigned short x;
2697
0
  bfd_reloc_status_type status;
2698
2699
  /* Sanity check the address (offset in section).  */
2700
0
  bfd_vma octet = offset * bfd_octets_per_byte (abfd, input_section);
2701
0
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octet))
2702
0
    return bfd_reloc_outofrange;
2703
2704
0
  relocation = symbol_value + addend;
2705
  /* Make it pc relative.  */
2706
0
  relocation -= (input_section->output_section->vma
2707
0
     + input_section->output_offset);
2708
  /* These jumps mask off the lower two bits of the current address
2709
     before doing pcrel calculations.  */
2710
0
  relocation -= (offset & -(bfd_vma) 2);
2711
2712
0
  if (relocation + ACCURATE_8BIT_S1 >= 2 * ACCURATE_8BIT_S1)
2713
0
    status = bfd_reloc_overflow;
2714
0
  else
2715
0
    status = bfd_reloc_ok;
2716
2717
0
  x = bfd_getb16 (data + offset);
2718
2719
0
  relocation >>= howto->rightshift;
2720
0
  relocation <<= howto->bitpos;
2721
0
  x = (x & ~howto->dst_mask)
2722
0
      | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2723
2724
0
  bfd_putb16 ((bfd_vma) x, data + offset);
2725
2726
0
  return status;
2727
0
}
2728
2729
/* Handle the R_NDS32_HI20_[SU]LO relocs.
2730
   HI20_SLO is for the add3 and load/store with displacement instructions.
2731
   HI20 is for the or3 instruction.
2732
   For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2733
   the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2734
   we must add one to the high 16 bytes (which will get subtracted off when
2735
   the low 16 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
static struct nds32_hi20 *nds32_hi20_list;
2753
2754
static bfd_reloc_status_type
2755
nds32_elf_hi20_reloc (bfd *abfd,
2756
          arelent *reloc_entry,
2757
          asymbol *symbol,
2758
          void *data,
2759
          asection *input_section,
2760
          bfd *output_bfd,
2761
          char **error_message ATTRIBUTE_UNUSED)
2762
0
{
2763
0
  bfd_reloc_status_type ret;
2764
0
  bfd_vma relocation;
2765
0
  struct nds32_hi20 *n;
2766
2767
  /* This part is from bfd_elf_generic_reloc.
2768
     If we're relocating, and this an external symbol, we don't want
2769
     to change anything.  */
2770
0
  if (output_bfd != (bfd *) NULL
2771
0
      && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2772
0
    {
2773
0
      reloc_entry->address += input_section->output_offset;
2774
0
      return bfd_reloc_ok;
2775
0
    }
2776
2777
  /* Sanity check the address (offset in section).  */
2778
0
  bfd_vma octet = (reloc_entry->address
2779
0
       * bfd_octets_per_byte (abfd, input_section));
2780
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto,
2781
0
          abfd, input_section, octet))
2782
0
    return bfd_reloc_outofrange;
2783
2784
0
  ret = bfd_reloc_ok;
2785
0
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2786
0
    ret = bfd_reloc_undefined;
2787
2788
0
  if (bfd_is_com_section (symbol->section))
2789
0
    relocation = 0;
2790
0
  else
2791
0
    relocation = symbol->value;
2792
2793
0
  relocation += symbol->section->output_section->vma;
2794
0
  relocation += symbol->section->output_offset;
2795
0
  relocation += reloc_entry->addend;
2796
2797
  /* Save the information, and let LO12 do the actual relocation.  */
2798
0
  n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2799
0
  if (n == NULL)
2800
0
    return bfd_reloc_outofrange;
2801
2802
0
  n->addr = (bfd_byte *) data + reloc_entry->address;
2803
0
  n->addend = relocation;
2804
0
  n->next = nds32_hi20_list;
2805
0
  nds32_hi20_list = n;
2806
2807
0
  if (output_bfd != (bfd *) NULL)
2808
0
    reloc_entry->address += input_section->output_offset;
2809
2810
0
  return ret;
2811
0
}
2812
2813
/* Handle an NDS32 ELF HI20 reloc.  */
2814
2815
static void
2816
nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2817
       int type ATTRIBUTE_UNUSED,
2818
       Elf_Internal_Rela *relhi,
2819
       Elf_Internal_Rela *rello,
2820
       bfd_byte *contents,
2821
       bfd_vma addend)
2822
0
{
2823
0
  unsigned long insn;
2824
0
  bfd_vma addlo;
2825
2826
0
  insn = bfd_getb32 (contents + relhi->r_offset);
2827
2828
0
  addlo = bfd_getb32 (contents + rello->r_offset);
2829
0
  addlo &= 0xfff;
2830
2831
0
  addend += ((insn & 0xfffff) << 20) + addlo;
2832
2833
0
  insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2834
0
  bfd_putb32 (insn, contents + relhi->r_offset);
2835
0
}
2836
2837
/* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
2838
   inplace relocation; this function exists in order to do the
2839
   R_NDS32_HI20_[SU]LO relocation described above.  */
2840
2841
static bfd_reloc_status_type
2842
nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2843
          void *data, asection *input_section, bfd *output_bfd,
2844
          char **error_message)
2845
1
{
2846
  /* This part is from bfd_elf_generic_reloc.
2847
     If we're relocating, and this an external symbol, we don't want
2848
     to change anything.  */
2849
1
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2850
0
      && reloc_entry->addend == 0)
2851
0
    {
2852
0
      reloc_entry->address += input_section->output_offset;
2853
0
      return bfd_reloc_ok;
2854
0
    }
2855
2856
1
  if (nds32_hi20_list != NULL)
2857
0
    {
2858
0
      struct nds32_hi20 *l;
2859
2860
0
      l = nds32_hi20_list;
2861
0
      while (l != NULL)
2862
0
  {
2863
0
    unsigned long insn;
2864
0
    unsigned long val;
2865
0
    unsigned long vallo;
2866
0
    struct nds32_hi20 *next;
2867
2868
    /* Do the HI20 relocation.  Note that we actually don't need
2869
       to know anything about the LO12 itself, except where to
2870
       find the low 12 bits of the addend needed by the LO12.  */
2871
0
    insn = bfd_getb32 (l->addr);
2872
0
    vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2873
0
    vallo &= 0xfff;
2874
0
    switch (reloc_entry->howto->type)
2875
0
      {
2876
0
      case R_NDS32_LO12S3:
2877
0
        vallo <<= 3;
2878
0
        break;
2879
2880
0
      case R_NDS32_LO12S2:
2881
0
        vallo <<= 2;
2882
0
        break;
2883
2884
0
      case R_NDS32_LO12S1:
2885
0
        vallo <<= 1;
2886
0
        break;
2887
2888
0
      case R_NDS32_LO12S0:
2889
0
        vallo <<= 0;
2890
0
        break;
2891
0
      }
2892
2893
0
    val = ((insn & 0xfffff) << 12) + vallo;
2894
0
    val += l->addend;
2895
2896
0
    insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2897
0
    bfd_putb32 ((bfd_vma) insn, l->addr);
2898
2899
0
    next = l->next;
2900
0
    free (l);
2901
0
    l = next;
2902
0
  }
2903
2904
0
      nds32_hi20_list = NULL;
2905
0
    }
2906
2907
  /* Now do the LO12 reloc in the usual way.
2908
     ??? It would be nice to call bfd_elf_generic_reloc here,
2909
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2910
     pass the handling back to bfd_install_relocation which will install
2911
     a section relative addend which is wrong.  */
2912
1
  return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2913
1
          input_section, output_bfd, error_message);
2914
1
}
2915
2916
/* Do generic partial_inplace relocation.
2917
   This is a local replacement for bfd_elf_generic_reloc.  */
2918
2919
static bfd_reloc_status_type
2920
nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2921
       asymbol *symbol, void *data, asection *input_section,
2922
       bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2923
1
{
2924
1
  bfd_reloc_status_type ret;
2925
1
  bfd_vma relocation;
2926
1
  bfd_byte *inplace_address;
2927
2928
  /* This part is from bfd_elf_generic_reloc.
2929
     If we're relocating, and this an external symbol, we don't want
2930
     to change anything.  */
2931
1
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2932
0
      && reloc_entry->addend == 0)
2933
0
    {
2934
0
      reloc_entry->address += input_section->output_offset;
2935
0
      return bfd_reloc_ok;
2936
0
    }
2937
2938
  /* Now do the reloc in the usual way.
2939
     ??? It would be nice to call bfd_elf_generic_reloc here,
2940
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2941
     pass the handling back to bfd_install_relocation which will install
2942
     a section relative addend which is wrong.  */
2943
2944
  /* Sanity check the address (offset in section).  */
2945
1
  bfd_vma octet = (reloc_entry->address
2946
1
       * bfd_octets_per_byte (input_bfd, input_section));
2947
1
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, input_bfd, input_section,
2948
1
          octet))
2949
1
    return bfd_reloc_outofrange;
2950
2951
0
  ret = bfd_reloc_ok;
2952
0
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2953
0
    ret = bfd_reloc_undefined;
2954
2955
0
  if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2956
0
    relocation = 0;
2957
0
  else
2958
0
    relocation = symbol->value;
2959
2960
  /* Only do this for a final link.  */
2961
0
  if (output_bfd == (bfd *) NULL)
2962
0
    {
2963
0
      relocation += symbol->section->output_section->vma;
2964
0
      relocation += symbol->section->output_offset;
2965
0
    }
2966
2967
0
  relocation += reloc_entry->addend;
2968
0
  switch (reloc_entry->howto->type)
2969
0
    {
2970
0
    case R_NDS32_LO12S3:
2971
0
      relocation >>= 3;
2972
0
      break;
2973
2974
0
    case R_NDS32_LO12S2:
2975
0
      relocation >>= 2;
2976
0
      break;
2977
2978
0
    case R_NDS32_LO12S1:
2979
0
      relocation >>= 1;
2980
0
      break;
2981
2982
0
    case R_NDS32_LO12S0:
2983
0
    default:
2984
0
      relocation >>= 0;
2985
0
      break;
2986
0
    }
2987
2988
0
  inplace_address = (bfd_byte *) data + reloc_entry->address;
2989
2990
0
#define DOIT(x)           \
2991
0
  x = ((x & ~reloc_entry->howto->dst_mask) |    \
2992
0
  (((x & reloc_entry->howto->src_mask) +  relocation) & \
2993
0
  reloc_entry->howto->dst_mask))
2994
2995
0
  switch (bfd_get_reloc_size (reloc_entry->howto))
2996
0
    {
2997
0
    case 2:
2998
0
      {
2999
0
  short x = bfd_getb16 (inplace_address);
3000
3001
0
  DOIT (x);
3002
0
  bfd_putb16 ((bfd_vma) x, inplace_address);
3003
0
      }
3004
0
      break;
3005
0
    case 4:
3006
0
      {
3007
0
  unsigned long x = bfd_getb32 (inplace_address);
3008
3009
0
  DOIT (x);
3010
0
  bfd_putb32 ((bfd_vma) x, inplace_address);
3011
0
      }
3012
0
      break;
3013
0
    default:
3014
0
      BFD_ASSERT (0);
3015
0
    }
3016
3017
0
  if (output_bfd != (bfd *) NULL)
3018
0
    reloc_entry->address += input_section->output_offset;
3019
3020
0
  return ret;
3021
0
}
3022
3023
/* Handle the R_NDS32_SDA15 reloc.
3024
   This reloc is used to compute the address of objects in the small data area
3025
   and to perform loads and stores from that area.
3026
   The lower 15 bits are sign extended and added to the register specified
3027
   in the instruction, which is assumed to point to _SDA_BASE_.
3028
3029
   Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
3030
   the access size, this must be taken care of.  */
3031
3032
static bfd_reloc_status_type
3033
nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3034
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
3035
           asection *input_section, bfd *output_bfd,
3036
           char **error_message ATTRIBUTE_UNUSED)
3037
0
{
3038
  /* This part is from bfd_elf_generic_reloc.  */
3039
0
  if (output_bfd != (bfd *) NULL
3040
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3041
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3042
0
    {
3043
0
      reloc_entry->address += input_section->output_offset;
3044
0
      return bfd_reloc_ok;
3045
0
    }
3046
3047
0
  if (output_bfd != NULL)
3048
0
    {
3049
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
3050
0
      return bfd_reloc_continue;
3051
0
    }
3052
3053
  /* FIXME: not sure what to do here yet.  But then again, the linker
3054
     may never call us.  */
3055
0
  abort ();
3056
0
}
3057
3058
/* nds32_elf_ignore_reloc is the special function for
3059
   relocation types which don't need to be relocated
3060
   like relaxation relocation types.
3061
   This function simply return bfd_reloc_ok when it is
3062
   invoked.  */
3063
3064
static bfd_reloc_status_type
3065
nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3066
      asymbol *symbol ATTRIBUTE_UNUSED,
3067
      void *data ATTRIBUTE_UNUSED, asection *input_section,
3068
      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3069
0
{
3070
0
  if (output_bfd != NULL)
3071
0
    reloc_entry->address += input_section->output_offset;
3072
3073
0
  return bfd_reloc_ok;
3074
0
}
3075

3076
3077
/* Map BFD reloc types to NDS32 ELF reloc types.  */
3078
3079
struct nds32_reloc_map_entry
3080
{
3081
  bfd_reloc_code_real_type bfd_reloc_val;
3082
  unsigned char elf_reloc_val;
3083
};
3084
3085
static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3086
{
3087
  {BFD_RELOC_NONE, R_NDS32_NONE},
3088
  {BFD_RELOC_16, R_NDS32_16_RELA},
3089
  {BFD_RELOC_32, R_NDS32_32_RELA},
3090
  {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3091
  {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3092
3093
  {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3094
  {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3095
  {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3096
  {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3097
  {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3098
  {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3099
  {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3100
  {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3101
  {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3102
  {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3103
  {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3104
  {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3105
  {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3106
  {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3107
  {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3108
  {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3109
  {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3110
  {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3111
  {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3112
  {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3113
  {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3114
  {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3115
  {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3116
  {BFD_RELOC_COPY, R_NDS32_COPY},
3117
  {BFD_RELOC_GLOB_DAT, R_NDS32_GLOB_DAT},
3118
  {BFD_RELOC_JMP_SLOT, R_NDS32_JMP_SLOT},
3119
  {BFD_RELOC_RELATIVE, R_NDS32_RELATIVE},
3120
  {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3121
  {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3122
  {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3123
  {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3124
  {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3125
  {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3126
  {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3127
  {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3128
  {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3129
  {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3130
  {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3131
  {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3132
  {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3133
  {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3134
  {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3135
  {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3136
  {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3137
  {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3138
  {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3139
  {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3140
  {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3141
  {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3142
  {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3143
  {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3144
  {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3145
  {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3146
  {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3147
  {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3148
  {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3149
  {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3150
  {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3151
  {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3152
  {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3153
  {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3154
  {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3155
  {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3156
  {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3157
  {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3158
  {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3159
  {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3160
  {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3161
  {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3162
  {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3163
  {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3164
  {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3165
  {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3166
  {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3167
  {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3168
  {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3169
  {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3170
  {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3171
  {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3172
  {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3173
  {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3174
  {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3175
  {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3176
  {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3177
  {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3178
  {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3179
  {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3180
  {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3181
  {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3182
  {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3183
  {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3184
  {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3185
  {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3186
  {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3187
  {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3188
  {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3189
  {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3190
  {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3191
  {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3192
  {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3193
  {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3194
  {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3195
  /* Not sure.  */
3196
  {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3197
  /* Missing: BFD_RELOC_NDS32_GOTTPOFF.  */
3198
  {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3199
  {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3200
  {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3201
  {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3202
  {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3203
  {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3204
  {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3205
  {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3206
  {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3207
  {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3208
  {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3209
  {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3210
  {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3211
  {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3212
  {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3213
  {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3214
  {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3215
  {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3216
  {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3217
  {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3218
  {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3219
  {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3220
  {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3221
  {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3222
  {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3223
  {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3224
  {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3225
};
3226
3227
/* Patch tag.  */
3228
3229
static reloc_howto_type *
3230
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3231
         const char *r_name)
3232
0
{
3233
0
  unsigned int i;
3234
3235
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3236
0
    if (nds32_elf_howto_table[i].name != NULL
3237
0
  && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3238
0
      return &nds32_elf_howto_table[i];
3239
3240
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3241
0
    if (nds32_elf_relax_howto_table[i].name != NULL
3242
0
  && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3243
0
      return &nds32_elf_relax_howto_table[i];
3244
3245
0
  return NULL;
3246
0
}
3247
3248
static reloc_howto_type *
3249
bfd_elf32_bfd_reloc_type_table_lookup (unsigned int code)
3250
30
{
3251
30
  if (code < R_NDS32_RELAX_ENTRY)
3252
30
    {
3253
30
      if (code < ARRAY_SIZE (nds32_elf_howto_table))
3254
30
  return &nds32_elf_howto_table[code];
3255
30
    }
3256
0
  else
3257
0
    {
3258
0
      if (code - R_NDS32_RELAX_ENTRY < ARRAY_SIZE (nds32_elf_relax_howto_table))
3259
0
  return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3260
0
    }
3261
0
  return NULL;
3262
30
}
3263
3264
static reloc_howto_type *
3265
bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3266
         bfd_reloc_code_real_type code)
3267
0
{
3268
0
  unsigned int i;
3269
3270
0
  for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3271
0
    {
3272
0
      if (nds32_reloc_map[i].bfd_reloc_val == code)
3273
0
  return bfd_elf32_bfd_reloc_type_table_lookup
3274
0
    (nds32_reloc_map[i].elf_reloc_val);
3275
0
    }
3276
3277
0
  return NULL;
3278
0
}
3279
3280
/* Set the howto pointer for an NDS32 ELF reloc.  */
3281
3282
static bool
3283
nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3284
       Elf_Internal_Rela *dst)
3285
34
{
3286
34
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3287
3288
34
  cache_ptr->howto = NULL;
3289
34
  if (r_type <= R_NDS32_GNU_VTENTRY)
3290
30
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3291
34
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3292
4
    {
3293
      /* xgettext:c-format */
3294
4
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3295
4
        abfd, r_type);
3296
4
      bfd_set_error (bfd_error_bad_value);
3297
4
      return false;
3298
4
    }
3299
30
  return true;
3300
34
}
3301
3302
static bool
3303
nds32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3304
         Elf_Internal_Rela *dst)
3305
0
{
3306
0
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3307
3308
0
  cache_ptr->howto = NULL;
3309
0
  if (r_type == R_NDS32_NONE
3310
0
      || r_type > R_NDS32_GNU_VTENTRY)
3311
0
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3312
0
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3313
0
    {
3314
      /* xgettext:c-format */
3315
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3316
0
        abfd, r_type);
3317
0
      bfd_set_error (bfd_error_bad_value);
3318
0
      return false;
3319
0
    }
3320
0
  return true;
3321
0
}
3322
3323
/* Support for core dump NOTE sections.
3324
   Reference to include/linux/elfcore.h in Linux.  */
3325
3326
static bool
3327
nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3328
0
{
3329
0
  int offset;
3330
0
  size_t size;
3331
3332
0
  switch (note->descsz)
3333
0
    {
3334
0
    case 0x114:
3335
      /* Linux/NDS32 32-bit, ABI1.  */
3336
3337
      /* pr_cursig */
3338
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3339
3340
      /* pr_pid */
3341
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3342
3343
      /* pr_reg */
3344
0
      offset = 72;
3345
0
      size = 200;
3346
0
      break;
3347
3348
0
    case 0xfc:
3349
      /* Linux/NDS32 32-bit.  */
3350
3351
      /* pr_cursig */
3352
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3353
3354
      /* pr_pid */
3355
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3356
3357
      /* pr_reg */
3358
0
      offset = 72;
3359
0
      size = 176;
3360
0
      break;
3361
3362
0
    default:
3363
0
      return false;
3364
0
    }
3365
3366
  /* Make a ".reg" section.  */
3367
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3368
0
            size, note->descpos + offset);
3369
0
}
3370
3371
static bool
3372
nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3373
0
{
3374
0
  switch (note->descsz)
3375
0
    {
3376
0
    case 124:
3377
      /* Linux/NDS32.  */
3378
3379
      /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
3380
0
      elf_tdata (abfd)->core->program =
3381
0
  _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3382
0
      elf_tdata (abfd)->core->command =
3383
0
  _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3384
0
      break;
3385
3386
0
    default:
3387
0
      return false;
3388
0
    }
3389
3390
  /* Note that for some reason, a spurious space is tacked
3391
     onto the end of the args in some (at least one anyway)
3392
     implementations, so strip it off if it exists.  */
3393
0
  {
3394
0
    char *command = elf_tdata (abfd)->core->command;
3395
0
    int n = strlen (command);
3396
3397
0
    if (0 < n && command[n - 1] == ' ')
3398
0
      command[n - 1] = '\0';
3399
0
  }
3400
3401
0
  return true;
3402
0
}
3403
3404
/* Hook called by the linker routine which adds symbols from an object
3405
   file.  We must handle the special NDS32 section numbers here.
3406
   We also keep watching for whether we need to create the sdata special
3407
   linker sections.  */
3408
3409
static bool
3410
nds32_elf_add_symbol_hook (bfd *abfd,
3411
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
3412
         Elf_Internal_Sym *sym,
3413
         const char **namep ATTRIBUTE_UNUSED,
3414
         flagword *flagsp ATTRIBUTE_UNUSED,
3415
         asection **secp, bfd_vma *valp)
3416
0
{
3417
0
  switch (sym->st_shndx)
3418
0
    {
3419
0
    case SHN_COMMON:
3420
      /* Common symbols less than the GP size are automatically
3421
   treated as SHN_MIPS_SCOMMON symbols.  */
3422
0
      if (sym->st_size > elf_gp_size (abfd)
3423
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3424
0
  break;
3425
3426
      /* st_value is the alignment constraint.
3427
   That might be its actual size if it is an array or structure.  */
3428
0
      switch (sym->st_value)
3429
0
  {
3430
0
  case 1:
3431
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3432
0
    break;
3433
0
  case 2:
3434
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3435
0
    break;
3436
0
  case 4:
3437
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3438
0
    break;
3439
0
  case 8:
3440
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3441
0
    break;
3442
0
  default:
3443
0
    return true;
3444
0
  }
3445
3446
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
3447
0
      *valp = sym->st_size;
3448
0
      break;
3449
0
    }
3450
3451
0
  return true;
3452
0
}
3453
3454
/* This function can figure out the best location for a base register to access
3455
   data relative to this base register
3456
   INPUT:
3457
   sda_d0: size of first DOUBLE WORD data section
3458
   sda_w0: size of first WORD data section
3459
   sda_h0: size of first HALF WORD data section
3460
   sda_b : size of BYTE data section
3461
   sda_hi: size of second HALF WORD data section
3462
   sda_w1: size of second WORD data section
3463
   sda_d1: size of second DOUBLE WORD data section
3464
   OUTPUT:
3465
   offset (always positive) from the beginning of sda_d0 if OK
3466
   a negative error value if fail
3467
   NOTE:
3468
   these 7 sections have to be located back to back if exist
3469
   a pass in 0 value for non-existing section   */
3470
3471
/* Due to the interpretation of simm15 field of load/store depending on
3472
   data accessing size, the organization of base register relative data shall
3473
   like the following figure
3474
   -------------------------------------------
3475
   |  DOUBLE WORD sized data (range +/- 128K)
3476
   -------------------------------------------
3477
   |  WORD sized data (range +/- 64K)
3478
   -------------------------------------------
3479
   |  HALF WORD sized data (range +/- 32K)
3480
   -------------------------------------------
3481
   |  BYTE sized data (range +/- 16K)
3482
   -------------------------------------------
3483
   |  HALF WORD sized data (range +/- 32K)
3484
   -------------------------------------------
3485
   |  WORD sized data (range +/- 64K)
3486
   -------------------------------------------
3487
   |  DOUBLE WORD sized data (range +/- 128K)
3488
   -------------------------------------------
3489
   Its base register shall be set to access these data freely.  */
3490
3491
/* We have to figure out the SDA_BASE value, so that we can adjust the
3492
   symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3493
   BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3494
   target data.  We don't need to adjust the symbol value for an
3495
   external symbol if we are producing relocatable output.  */
3496
3497
static asection *sda_rela_sec = NULL;
3498
3499
#define SDA_SECTION_NUM 10
3500
3501
static bfd_reloc_status_type
3502
nds32_elf_final_sda_base (bfd *output_bfd,
3503
        struct bfd_link_info *info,
3504
        bfd_vma *psb,
3505
        bool add_symbol)
3506
0
{
3507
0
  int relax_fp_as_gp;
3508
0
  struct elf_nds32_link_hash_table *table;
3509
0
  struct bfd_link_hash_entry *h, *h2;
3510
0
  long unsigned int total = 0;
3511
0
  asection *first = NULL, *final = NULL, *temp;
3512
0
  bfd_vma sda_base = 0;
3513
3514
0
  h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
3515
0
  if (!h || (h->type != bfd_link_hash_defined
3516
0
       && h->type != bfd_link_hash_defweak))
3517
0
    {
3518
      /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3519
   4 byte-aligned.  Therefore, it has to set the first section ".data"
3520
   4 byte-aligned.  */
3521
0
      static const char sec_name[SDA_SECTION_NUM][10] =
3522
0
  {
3523
0
    ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3524
0
    ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3525
0
  };
3526
0
      size_t i = 0;
3527
3528
0
      if (output_bfd->sections == NULL)
3529
0
  {
3530
0
    *psb = elf_gp (output_bfd);
3531
0
    return bfd_reloc_ok;
3532
0
  }
3533
3534
      /* Get the first and final section.  */
3535
0
      while (i < ARRAY_SIZE (sec_name))
3536
0
  {
3537
0
    temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3538
0
    if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3539
0
      first = temp;
3540
0
    if (temp && (temp->size != 0 || temp->rawsize != 0))
3541
0
      final = temp;
3542
3543
    /* Summarize the sections in order to check if joining .bss.  */
3544
0
    if (temp && temp->size != 0)
3545
0
      total += temp->size;
3546
0
    else if (temp && temp->rawsize != 0)
3547
0
      total += temp->rawsize;
3548
3549
0
    i++;
3550
0
  }
3551
3552
      /* Check .bss size.  */
3553
0
      temp = bfd_get_section_by_name (output_bfd, ".bss");
3554
0
      if (temp)
3555
0
  {
3556
0
    if (temp->size != 0)
3557
0
      total += temp->size;
3558
0
    else if (temp->rawsize != 0)
3559
0
      total += temp->rawsize;
3560
3561
0
    if (total < 0x80000)
3562
0
      {
3563
0
        if (!first && (temp->size != 0 || temp->rawsize != 0))
3564
0
    first = temp;
3565
0
        if ((temp->size != 0 || temp->rawsize != 0))
3566
0
    final = temp;
3567
0
      }
3568
0
  }
3569
3570
0
      if (first && final)
3571
0
  {
3572
    /* The middle of data region.  */
3573
0
    sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3574
3575
    /* Find the section sda_base located.  */
3576
0
    i = 0;
3577
0
    while (i < ARRAY_SIZE (sec_name))
3578
0
      {
3579
0
        final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3580
0
        if (final && (final->size != 0 || final->rawsize != 0)
3581
0
      && sda_base >= final->vma)
3582
0
    {
3583
0
      first = final;
3584
0
      i++;
3585
0
    }
3586
0
        else
3587
0
    break;
3588
0
      }
3589
0
  }
3590
0
      else
3591
0
  {
3592
    /* If there is not any default data section in output bfd, try to find
3593
       the first data section.  If no data section be found, just simplily
3594
       choose the first output section.  */
3595
0
    temp = output_bfd->sections;
3596
0
    while (temp)
3597
0
      {
3598
0
        if (temp->flags & SEC_ALLOC
3599
0
      && (((temp->flags & SEC_DATA)
3600
0
           && ((temp->flags & SEC_READONLY) == 0))
3601
0
          || (temp->flags & SEC_LOAD) == 0)
3602
0
      && (temp->size != 0 || temp->rawsize != 0))
3603
0
    {
3604
0
      if (!first)
3605
0
        first = temp;
3606
0
      final = temp;
3607
0
    }
3608
0
        temp = temp->next;
3609
0
      }
3610
3611
    /* There is no data or bss section.  */
3612
0
    if (!first || (first->size == 0 && first->rawsize == 0))
3613
0
      {
3614
0
        first = output_bfd->sections;
3615
0
        while (first && first->size == 0 && first->rawsize == 0)
3616
0
    first = first->next;
3617
0
      }
3618
3619
    /* There is no concrete section.  */
3620
0
    if (!first)
3621
0
      {
3622
0
        *psb = elf_gp (output_bfd);
3623
0
        return bfd_reloc_ok;
3624
0
      }
3625
3626
0
    if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3627
0
      sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3628
0
    else
3629
0
      sda_base = first->vma + 0x2000;
3630
0
  }
3631
3632
0
      sda_base -= first->vma;
3633
0
      sda_base = sda_base & (~7);
3634
3635
0
      if (!_bfd_generic_link_add_one_symbol
3636
0
    (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3637
0
     (bfd_vma) sda_base, (const char *) NULL, false,
3638
0
     get_elf_backend_data (output_bfd)->collect, &h))
3639
0
  return false;
3640
3641
0
      sda_rela_sec = first;
3642
0
    }
3643
3644
  /* Set _FP_BASE_ to _SDA_BASE_.  */
3645
0
  table = nds32_elf_hash_table (info);
3646
0
  relax_fp_as_gp = table->relax_fp_as_gp;
3647
0
  h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, false, false, false);
3648
  /* _SDA_BASE_ is difined in linker script.  */
3649
0
  if (!first)
3650
0
    {
3651
0
      first = h->u.def.section;
3652
0
      sda_base = h->u.def.value;
3653
0
    }
3654
3655
0
  if (relax_fp_as_gp && h2
3656
0
      && (h2->type == bfd_link_hash_undefweak
3657
0
    || h2->type == bfd_link_hash_undefined))
3658
0
    {
3659
      /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3660
   And set FP equal to SDA_BASE to do relaxation for
3661
   la $fp, _FP_BASE_.  */
3662
0
      if (!_bfd_generic_link_add_one_symbol
3663
0
    (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3664
0
     first, sda_base, (const char *) NULL,
3665
0
     false, get_elf_backend_data (output_bfd)->collect, &h2))
3666
0
  return false;
3667
0
    }
3668
3669
0
  if (add_symbol)
3670
0
    {
3671
0
      if (h)
3672
0
  {
3673
    /* Now set gp.  */
3674
0
    elf_gp (output_bfd) = (h->u.def.value
3675
0
         + h->u.def.section->output_section->vma
3676
0
         + h->u.def.section->output_offset);
3677
0
  }
3678
0
      else
3679
0
  {
3680
0
    _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3681
0
    return bfd_reloc_dangerous;
3682
0
  }
3683
0
    }
3684
3685
0
  *psb = h->u.def.value
3686
0
    + h->u.def.section->output_section->vma
3687
0
    + h->u.def.section->output_offset;
3688
0
  return bfd_reloc_ok;
3689
0
}
3690

3691
3692
/* Return size of a PLT entry.  */
3693
#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3694
3695
/* Create an entry in an nds32 ELF linker hash table.  */
3696
3697
static struct bfd_hash_entry *
3698
nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3699
           struct bfd_hash_table *table,
3700
           const char *string)
3701
0
{
3702
0
  struct elf_nds32_link_hash_entry *ret;
3703
3704
0
  ret = (struct elf_nds32_link_hash_entry *) entry;
3705
3706
  /* Allocate the structure if it has not already been allocated by a
3707
     subclass.  */
3708
0
  if (ret == NULL)
3709
0
    ret = (struct elf_nds32_link_hash_entry *)
3710
0
       bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3711
3712
0
  if (ret == NULL)
3713
0
    return (struct bfd_hash_entry *) ret;
3714
3715
  /* Call the allocation method of the superclass.  */
3716
0
  ret = (struct elf_nds32_link_hash_entry *)
3717
0
    _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3718
3719
0
  if (ret != NULL)
3720
0
    {
3721
0
      struct elf_nds32_link_hash_entry *eh;
3722
3723
0
      eh = (struct elf_nds32_link_hash_entry *) ret;
3724
0
      eh->tls_type = GOT_UNKNOWN;
3725
0
      eh->offset_to_gp = 0;
3726
0
    }
3727
3728
0
  return (struct bfd_hash_entry *) ret;
3729
0
}
3730
3731
/* Create an nds32 ELF linker hash table.  */
3732
3733
static struct bfd_link_hash_table *
3734
nds32_elf_link_hash_table_create (bfd *abfd)
3735
0
{
3736
0
  struct elf_nds32_link_hash_table *ret;
3737
3738
0
  size_t amt = sizeof (struct elf_nds32_link_hash_table);
3739
3740
0
  ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3741
0
  if (ret == NULL)
3742
0
    return NULL;
3743
3744
  /* Patch tag.  */
3745
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3746
0
              nds32_elf_link_hash_newfunc,
3747
0
              sizeof (struct elf_nds32_link_hash_entry)))
3748
0
    {
3749
0
      free (ret);
3750
0
      return NULL;
3751
0
    }
3752
3753
0
  ret->sym_ld_script = NULL;
3754
3755
0
  return &ret->root.root;
3756
0
}
3757
3758
/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3759
   shortcuts to them in our hash table.  */
3760
3761
static bool
3762
create_got_section (bfd *dynobj, struct bfd_link_info *info)
3763
0
{
3764
0
  struct elf_link_hash_table *ehtab;
3765
3766
0
  if (!_bfd_elf_create_got_section (dynobj, info))
3767
0
    return false;
3768
3769
0
  ehtab = elf_hash_table (info);
3770
0
  ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3771
0
  ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3772
0
  if (!ehtab->sgot || !ehtab->sgotplt)
3773
0
    abort ();
3774
3775
  /* _bfd_elf_create_got_section will create it for us.  */
3776
0
  ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3777
0
  if (ehtab->srelgot == NULL
3778
0
      || !bfd_set_section_flags (ehtab->srelgot,
3779
0
         (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3780
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED
3781
0
          | SEC_READONLY))
3782
0
      || !bfd_set_section_alignment (ehtab->srelgot, 2))
3783
0
    return false;
3784
3785
0
  return true;
3786
0
}
3787
3788
/* Create dynamic sections when linking against a dynamic object.  */
3789
3790
static bool
3791
nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3792
0
{
3793
0
  struct elf_link_hash_table *ehtab;
3794
0
  struct elf_nds32_link_hash_table *htab;
3795
0
  flagword flags, pltflags;
3796
0
  register asection *s;
3797
0
  elf_backend_data *bed;
3798
0
  int ptralign = 2;   /* 32-bit  */
3799
0
  const char *secname;
3800
0
  char *relname;
3801
0
  flagword secflags;
3802
0
  asection *sec;
3803
3804
0
  bed = get_elf_backend_data (abfd);
3805
0
  ehtab = elf_hash_table (info);
3806
0
  htab = nds32_elf_hash_table (info);
3807
3808
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3809
     .rel[a].bss sections.  */
3810
3811
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3812
0
     | SEC_LINKER_CREATED);
3813
3814
0
  pltflags = flags;
3815
0
  pltflags |= SEC_CODE;
3816
0
  if (bed->plt_not_loaded)
3817
0
    pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3818
0
  if (bed->plt_readonly)
3819
0
    pltflags |= SEC_READONLY;
3820
3821
0
  s = bfd_make_section (abfd, ".plt");
3822
0
  ehtab->splt = s;
3823
0
  if (s == NULL
3824
0
      || !bfd_set_section_flags (s, pltflags)
3825
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
3826
0
    return false;
3827
3828
0
  if (bed->want_plt_sym)
3829
0
    {
3830
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3831
   .plt section.  */
3832
0
      struct bfd_link_hash_entry *bh = NULL;
3833
0
      struct elf_link_hash_entry *h;
3834
3835
0
      if (!(_bfd_generic_link_add_one_symbol
3836
0
      (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3837
0
       (bfd_vma) 0, (const char *) NULL, false,
3838
0
       get_elf_backend_data (abfd)->collect, &bh)))
3839
0
  return false;
3840
3841
0
      h = (struct elf_link_hash_entry *) bh;
3842
0
      h->def_regular = 1;
3843
0
      h->type = STT_OBJECT;
3844
3845
0
      if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3846
0
  return false;
3847
0
    }
3848
3849
0
  s = bfd_make_section (abfd,
3850
0
      bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3851
0
  ehtab->srelplt = s;
3852
0
  if (s == NULL
3853
0
      || !bfd_set_section_flags (s, flags | SEC_READONLY)
3854
0
      || !bfd_set_section_alignment (s, ptralign))
3855
0
    return false;
3856
3857
0
  if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3858
0
    return false;
3859
3860
0
  for (sec = abfd->sections; sec; sec = sec->next)
3861
0
    {
3862
0
      secflags = bfd_section_flags (sec);
3863
0
      if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3864
0
    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3865
0
  continue;
3866
0
      secname = bfd_section_name (sec);
3867
0
      relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3868
0
      strcpy (relname, ".rela");
3869
0
      strcat (relname, secname);
3870
0
      if (bfd_get_section_by_name (abfd, secname))
3871
0
  continue;
3872
0
      s = bfd_make_section (abfd, relname);
3873
0
      if (s == NULL
3874
0
    || !bfd_set_section_flags (s, flags | SEC_READONLY)
3875
0
    || !bfd_set_section_alignment (s, ptralign))
3876
0
  return false;
3877
0
    }
3878
3879
0
  if (bed->want_dynbss)
3880
0
    {
3881
      /* The .dynbss section is a place to put symbols which are defined
3882
   by dynamic objects, are referenced by regular objects, and are
3883
   not functions.  We must allocate space for them in the process
3884
   image and use a R_*_COPY reloc to tell the dynamic linker to
3885
   initialize them at run time.  The linker script puts the .dynbss
3886
   section into the .bss section of the final image.  */
3887
0
      s = bfd_make_section (abfd, ".dynbss");
3888
0
      htab->root.sdynbss = s;
3889
0
      if (s == NULL
3890
0
    || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3891
0
  return false;
3892
      /* The .rel[a].bss section holds copy relocs.  This section is not
3893
   normally needed.  We need to create it here, though, so that the
3894
   linker will map it to an output section.  We can't just create it
3895
   only if we need it, because we will not know whether we need it
3896
   until we have seen all the input files, and the first time the
3897
   main linker code calls BFD after examining all the input files
3898
   (size_dynamic_sections) the input sections have already been
3899
   mapped to the output sections.  If the section turns out not to
3900
   be needed, we can discard it later.  We will never need this
3901
   section when generating a shared object, since they do not use
3902
   copy relocs.  */
3903
0
      if (!bfd_link_pic (info))
3904
0
  {
3905
0
    s = bfd_make_section (abfd, (bed->default_use_rela_p
3906
0
               ? ".rela.bss" : ".rel.bss"));
3907
0
    htab->root.srelbss = s;
3908
0
    if (s == NULL
3909
0
        || !bfd_set_section_flags (s, flags | SEC_READONLY)
3910
0
        || !bfd_set_section_alignment (s, ptralign))
3911
0
      return false;
3912
0
  }
3913
0
    }
3914
3915
0
  return true;
3916
0
}
3917
3918
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3919
static void
3920
nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3921
        struct elf_link_hash_entry *dir,
3922
        struct elf_link_hash_entry *ind)
3923
0
{
3924
0
  struct elf_nds32_link_hash_entry *edir, *eind;
3925
3926
0
  edir = (struct elf_nds32_link_hash_entry *) dir;
3927
0
  eind = (struct elf_nds32_link_hash_entry *) ind;
3928
3929
0
  if (ind->root.type == bfd_link_hash_indirect)
3930
0
    {
3931
0
      if (dir->got.refcount <= 0)
3932
0
  {
3933
0
    edir->tls_type = eind->tls_type;
3934
0
    eind->tls_type = GOT_UNKNOWN;
3935
0
  }
3936
0
    }
3937
3938
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3939
0
}
3940

3941
/* Adjust a symbol defined by a dynamic object and referenced by a
3942
   regular object.  The current definition is in some section of the
3943
   dynamic object, but we're not including those sections.  We have to
3944
   change the definition to something the rest of the link can
3945
   understand.  */
3946
3947
static bool
3948
nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3949
         struct elf_link_hash_entry *h)
3950
0
{
3951
0
  struct elf_nds32_link_hash_table *htab;
3952
0
  bfd *dynobj;
3953
0
  asection *s;
3954
0
  unsigned int power_of_two;
3955
3956
0
  dynobj = elf_hash_table (info)->dynobj;
3957
3958
  /* Make sure we know what is going on here.  */
3959
0
  BFD_ASSERT (dynobj != NULL
3960
0
        && (h->needs_plt
3961
0
      || h->is_weakalias
3962
0
      || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3963
3964
3965
  /* If this is a function, put it in the procedure linkage table.  We
3966
     will fill in the contents of the procedure linkage table later,
3967
     when we know the address of the .got section.  */
3968
0
  if (h->type == STT_FUNC || h->needs_plt)
3969
0
    {
3970
0
      if (!bfd_link_pic (info)
3971
0
    && !h->def_dynamic
3972
0
    && !h->ref_dynamic
3973
0
    && h->root.type != bfd_link_hash_undefweak
3974
0
    && h->root.type != bfd_link_hash_undefined)
3975
0
  {
3976
    /* This case can occur if we saw a PLT reloc in an input
3977
       file, but the symbol was never referred to by a dynamic
3978
       object.  In such a case, we don't actually need to build
3979
       a procedure linkage table, and we can just do a PCREL
3980
       reloc instead.  */
3981
0
    h->plt.offset = (bfd_vma) - 1;
3982
0
    h->needs_plt = 0;
3983
0
  }
3984
3985
0
      return true;
3986
0
    }
3987
0
  else
3988
0
    h->plt.offset = (bfd_vma) - 1;
3989
3990
  /* If this is a weak symbol, and there is a real definition, the
3991
     processor independent code will have arranged for us to see the
3992
     real definition first, and we can just use the same value.  */
3993
0
  if (h->is_weakalias)
3994
0
    {
3995
0
      struct elf_link_hash_entry *def = weakdef (h);
3996
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3997
0
      h->root.u.def.section = def->root.u.def.section;
3998
0
      h->root.u.def.value = def->root.u.def.value;
3999
0
      return true;
4000
0
    }
4001
4002
  /* This is a reference to a symbol defined by a dynamic object which
4003
     is not a function.  */
4004
4005
  /* If we are creating a shared library, we must presume that the
4006
     only references to the symbol are via the global offset table.
4007
     For such cases we need not do anything here; the relocations will
4008
     be handled correctly by relocate_section.  */
4009
0
  if (bfd_link_pic (info))
4010
0
    return true;
4011
4012
  /* If there are no references to this symbol that do not use the
4013
     GOT, we don't need to generate a copy reloc.  */
4014
0
  if (!h->non_got_ref)
4015
0
    return true;
4016
4017
  /* If -z nocopyreloc was given, we won't generate them either.  */
4018
0
  if (0 && info->nocopyreloc)
4019
0
    {
4020
0
      h->non_got_ref = 0;
4021
0
      return true;
4022
0
    }
4023
4024
  /* If we don't find any dynamic relocs in read-only sections, then
4025
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4026
0
  if (!_bfd_elf_readonly_dynrelocs (h))
4027
0
    {
4028
0
      h->non_got_ref = 0;
4029
0
      return true;
4030
0
    }
4031
4032
  /* We must allocate the symbol in our .dynbss section, which will
4033
     become part of the .bss section of the executable.  There will be
4034
     an entry for this symbol in the .dynsym section.  The dynamic
4035
     object will contain position independent code, so all references
4036
     from the dynamic object to this symbol will go through the global
4037
     offset table.  The dynamic linker will use the .dynsym entry to
4038
     determine the address it must put in the global offset table, so
4039
     both the dynamic object and the regular object will refer to the
4040
     same memory location for the variable.  */
4041
4042
0
  htab = nds32_elf_hash_table (info);
4043
0
  s = htab->root.sdynbss;
4044
0
  BFD_ASSERT (s != NULL);
4045
4046
  /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4047
     to copy the initial value out of the dynamic object and into the
4048
     runtime process image.  We need to remember the offset into the
4049
     .rela.bss section we are going to use.  */
4050
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4051
0
    {
4052
0
      asection *srel;
4053
4054
0
      srel = htab->root.srelbss;
4055
0
      BFD_ASSERT (srel != NULL);
4056
0
      srel->size += sizeof (Elf32_External_Rela);
4057
0
      h->needs_copy = 1;
4058
0
    }
4059
4060
  /* We need to figure out the alignment required for this symbol.  I
4061
     have no idea how ELF linkers handle this.  */
4062
0
  power_of_two = bfd_log2 (h->size);
4063
0
  if (power_of_two > 3)
4064
0
    power_of_two = 3;
4065
4066
  /* Apply the required alignment.  */
4067
0
  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4068
0
  if (!bfd_link_align_section (s, power_of_two))
4069
0
    return false;
4070
4071
  /* Define the symbol as being at this point in the section.  */
4072
0
  h->root.u.def.section = s;
4073
0
  h->root.u.def.value = s->size;
4074
4075
  /* Increment the section size to make room for the symbol.  */
4076
0
  s->size += h->size;
4077
4078
0
  return true;
4079
0
}
4080
4081
/* Allocate space in .plt, .got and associated reloc sections for
4082
   dynamic relocs.  */
4083
4084
static bool
4085
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4086
0
{
4087
0
  struct bfd_link_info *info;
4088
0
  struct elf_link_hash_table *ehtab;
4089
0
  struct elf_nds32_link_hash_table *htab;
4090
0
  struct elf_dyn_relocs *p;
4091
4092
0
  if (h->root.type == bfd_link_hash_indirect)
4093
0
    return true;
4094
4095
  /* When warning symbols are created, they **replace** the "real"
4096
     entry in the hash table, thus we never get to see the real
4097
     symbol in a hash traversal. So look at it now.  */
4098
0
  if (h->root.type == bfd_link_hash_warning)
4099
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4100
4101
0
  info = (struct bfd_link_info *) inf;
4102
0
  ehtab = elf_hash_table (info);
4103
0
  htab = nds32_elf_hash_table (info);
4104
0
  if (htab == NULL)
4105
0
    return false;
4106
4107
0
  if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4108
0
      && h->plt.refcount > 0
4109
0
      && !(bfd_link_pie (info) && h->def_regular))
4110
0
    {
4111
      /* Make sure this symbol is output as a dynamic symbol.
4112
   Undefined weak syms won't yet be marked as dynamic.  */
4113
0
      if (h->dynindx == -1 && !h->forced_local)
4114
0
  {
4115
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4116
0
      return false;
4117
0
  }
4118
4119
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4120
0
  {
4121
0
    asection *s = ehtab->splt;
4122
4123
    /* If this is the first .plt entry, make room for the special
4124
       first entry.  */
4125
0
    if (s->size == 0)
4126
0
      s->size += PLT_ENTRY_SIZE;
4127
4128
0
    h->plt.offset = s->size;
4129
4130
    /* If this symbol is not defined in a regular file, and we are
4131
       not generating a shared library, then set the symbol to this
4132
       location in the .plt.  This is required to make function
4133
       pointers compare as equal between the normal executable and
4134
       the shared library.  */
4135
0
    if (!bfd_link_pic (info) && !h->def_regular)
4136
0
      {
4137
0
        h->root.u.def.section = s;
4138
0
        h->root.u.def.value = h->plt.offset;
4139
0
      }
4140
4141
    /* Make room for this entry.  */
4142
0
    s->size += PLT_ENTRY_SIZE;
4143
4144
    /* We also need to make an entry in the .got.plt section, which
4145
       will be placed in the .got section by the linker script.  */
4146
0
    ehtab->sgotplt->size += 4;
4147
4148
    /* We also need to make an entry in the .rel.plt section.  */
4149
0
    ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4150
0
    if (htab->tls_desc_trampoline)
4151
0
      htab->next_tls_desc_index++;
4152
0
  }
4153
0
      else
4154
0
  {
4155
0
    h->plt.offset = (bfd_vma) - 1;
4156
0
    h->needs_plt = 0;
4157
0
  }
4158
0
    }
4159
0
  else
4160
0
    {
4161
0
      h->plt.offset = (bfd_vma) - 1;
4162
0
      h->needs_plt = 0;
4163
0
    }
4164
4165
0
  if (h->got.refcount > 0)
4166
0
    {
4167
0
      asection *sgot;
4168
0
      bool dyn;
4169
0
      int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4170
4171
      /* Make sure this symbol is output as a dynamic symbol.
4172
   Undefined weak syms won't yet be marked as dynamic.  */
4173
0
      if (h->dynindx == -1 && !h->forced_local)
4174
0
  {
4175
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4176
0
      return false;
4177
0
  }
4178
4179
0
      sgot = elf_hash_table (info)->sgot;
4180
0
      h->got.offset = sgot->size;
4181
4182
0
      if (tls_type == GOT_UNKNOWN)
4183
0
  abort ();
4184
4185
      /* Non-TLS symbols, and TLS_IE need one GOT slot.  */
4186
0
      if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4187
0
  sgot->size += 4;
4188
0
      else
4189
0
  {
4190
    /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots.  */
4191
0
    if (tls_type & GOT_TLS_DESC)
4192
0
      sgot->size += 8;
4193
0
  }
4194
4195
0
      dyn = htab->root.dynamic_sections_created;
4196
4197
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4198
0
  {
4199
0
    if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4200
0
      {
4201
        /* TLS_DESC with trampoline needs a relocation slot
4202
     within .rela.plt.  */
4203
0
        htab->num_tls_desc++;
4204
0
        ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4205
0
        htab->tls_trampoline = -1;
4206
0
      }
4207
0
    else
4208
0
      {
4209
        /* other relocations, including TLS_DESC without trampoline, need
4210
     a relocation slot within .rela.got.  */
4211
0
        ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4212
0
      }
4213
0
  }
4214
0
    }
4215
0
  else
4216
0
    h->got.offset = (bfd_vma)-1;
4217
4218
0
  if (h->dyn_relocs == NULL)
4219
0
    return true;
4220
4221
  /* In the shared -Bsymbolic case, discard space allocated for
4222
     dynamic pc-relative relocs against symbols which turn out to be
4223
     defined in regular objects.  For the normal shared case, discard
4224
     space for pc-relative relocs that have become local due to symbol
4225
     visibility changes.  */
4226
4227
0
  if (bfd_link_pic (info))
4228
0
    {
4229
0
      if (h->def_regular && (h->forced_local || info->symbolic))
4230
0
  {
4231
0
    struct elf_dyn_relocs **pp;
4232
4233
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4234
0
      {
4235
0
        p->count -= p->pc_count;
4236
0
        p->pc_count = 0;
4237
0
        if (p->count == 0)
4238
0
    *pp = p->next;
4239
0
        else
4240
0
    pp = &p->next;
4241
0
      }
4242
0
  }
4243
0
    }
4244
0
  else
4245
0
    {
4246
      /* For the non-shared case, discard space for relocs against
4247
   symbols which turn out to need copy relocs or are not dynamic.  */
4248
4249
0
      if (!h->non_got_ref
4250
0
    && ((h->def_dynamic
4251
0
         && !h->def_regular)
4252
0
        || (htab->root.dynamic_sections_created
4253
0
      && (h->root.type == bfd_link_hash_undefweak
4254
0
          || h->root.type == bfd_link_hash_undefined))))
4255
0
  {
4256
    /* Make sure this symbol is output as a dynamic symbol.
4257
       Undefined weak syms won't yet be marked as dynamic.  */
4258
0
    if (h->dynindx == -1 && !h->forced_local)
4259
0
      {
4260
0
        if (!bfd_elf_link_record_dynamic_symbol (info, h))
4261
0
    return false;
4262
0
      }
4263
4264
    /* If that succeeded, we know we'll be keeping all the
4265
       relocs.  */
4266
0
    if (h->dynindx != -1)
4267
0
      goto keep;
4268
0
  }
4269
4270
0
      h->dyn_relocs = NULL;
4271
4272
0
    keep:;
4273
0
    }
4274
4275
  /* Finally, allocate space.  */
4276
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
4277
0
    {
4278
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4279
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4280
0
    }
4281
4282
0
  return true;
4283
0
}
4284
4285
/* Add relocation REL to the end of relocation section SRELOC.  */
4286
4287
static void
4288
elf32_nds32_add_dynreloc (bfd *output_bfd,
4289
        struct bfd_link_info *info ATTRIBUTE_UNUSED,
4290
        asection *sreloc, Elf_Internal_Rela *rel)
4291
0
{
4292
0
  bfd_byte *loc;
4293
0
  if (sreloc == NULL)
4294
0
    abort ();
4295
4296
0
  loc = sreloc->contents;
4297
0
  loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4298
0
  if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4299
0
    abort ();
4300
4301
0
  bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4302
0
}
4303
4304
/* Set the sizes of the dynamic sections.  */
4305
4306
static bool
4307
nds32_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4308
            struct bfd_link_info *info)
4309
0
{
4310
0
  struct elf_nds32_link_hash_table *htab;
4311
0
  bfd *dynobj;
4312
0
  asection *s;
4313
0
  bool relocs;
4314
0
  bfd *ibfd;
4315
4316
0
  htab = nds32_elf_hash_table (info);
4317
0
  if (htab == NULL)
4318
0
    return false;
4319
4320
0
  dynobj = elf_hash_table (info)->dynobj;
4321
0
  if (dynobj == NULL)
4322
0
    return true;
4323
4324
0
  if (elf_hash_table (info)->dynamic_sections_created)
4325
0
    {
4326
      /* Set the contents of the .interp section to the interpreter.  */
4327
0
      if (bfd_link_executable (info) && !info->nointerp)
4328
0
  {
4329
0
    s = elf_hash_table (info)->interp;
4330
0
    BFD_ASSERT (s != NULL);
4331
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4332
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4333
0
    s->alloced = 1;
4334
0
  }
4335
0
    }
4336
4337
  /* Set up .got offsets for local syms, and space for local dynamic
4338
     relocs.  */
4339
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4340
0
    {
4341
0
      bfd_signed_vma *local_got;
4342
0
      bfd_signed_vma *end_local_got;
4343
0
      bfd_size_type locsymcount;
4344
0
      Elf_Internal_Shdr *symtab_hdr;
4345
0
      asection *sgot;
4346
0
      char *local_tls_type;
4347
0
      bfd_vma *local_tlsdesc_gotent;
4348
4349
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4350
0
  continue;
4351
4352
0
      for (s = ibfd->sections; s != NULL; s = s->next)
4353
0
  {
4354
0
    struct elf_dyn_relocs *p;
4355
4356
0
    for (p = ((struct elf_dyn_relocs *)
4357
0
        elf_section_data (s)->local_dynrel);
4358
0
         p != NULL; p = p->next)
4359
0
      {
4360
0
        if (!bfd_is_abs_section (p->sec)
4361
0
      && bfd_is_abs_section (p->sec->output_section))
4362
0
    {
4363
      /* Input section has been discarded, either because
4364
         it is a copy of a linkonce section or due to
4365
         linker script /DISCARD/, so we'll be discarding
4366
         the relocs too.  */
4367
0
    }
4368
0
        else if (p->count != 0)
4369
0
    {
4370
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4371
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4372
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4373
0
        info->flags |= DF_TEXTREL;
4374
0
    }
4375
0
      }
4376
0
  }
4377
4378
0
      local_got = elf_local_got_refcounts (ibfd);
4379
0
      if (!local_got)
4380
0
  continue;
4381
4382
0
      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4383
0
      locsymcount = symtab_hdr->sh_info;
4384
0
      end_local_got = local_got + locsymcount;
4385
0
      sgot = elf_hash_table (info)->sgot;
4386
0
      local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4387
0
      local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4388
0
      for (; local_got < end_local_got;
4389
0
     ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
4390
0
  {
4391
0
    if (*local_got > 0)
4392
0
      {
4393
0
        int num_of_got_entry_needed = 0;
4394
0
        *local_got = sgot->size;
4395
0
        *local_tlsdesc_gotent = sgot->size;
4396
4397
        /* TLS_NORMAL, and TLS_IE need one slot in .got.  */
4398
0
        if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4399
0
    num_of_got_entry_needed = 1;
4400
        /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT.  */
4401
0
        else if (*local_tls_type & GOT_TLS_DESC)
4402
0
    num_of_got_entry_needed = 2;
4403
4404
0
        sgot->size += (num_of_got_entry_needed << 2);
4405
4406
        /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4407
     others need a slot in .rela.got.  */
4408
0
        if (*local_tls_type == GOT_TLS_DESC)
4409
0
    {
4410
0
      if (bfd_link_pic (info))
4411
0
        {
4412
0
          if (htab->tls_desc_trampoline)
4413
0
      {
4414
0
        htab->num_tls_desc++;
4415
0
        htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4416
0
        htab->tls_trampoline = -1;
4417
0
      }
4418
0
          else
4419
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4420
0
        }
4421
0
      else
4422
0
        {
4423
          /* TLS_DESC -> TLS_LE  */
4424
0
        }
4425
0
    }
4426
0
        else
4427
0
    {
4428
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4429
0
    }
4430
0
      }
4431
0
    else
4432
0
      {
4433
0
        *local_got = (bfd_vma) -1;
4434
0
        *local_tlsdesc_gotent = (bfd_vma) -1;
4435
0
      }
4436
0
  }
4437
0
    }
4438
4439
  /* Allocate global sym .plt and .got entries, and space for global
4440
     sym dynamic relocs.  */
4441
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4442
4443
  /* For every jump slot reserved in the sgotplt, reloc_count is
4444
     incremented.  However, when we reserve space for TLS descriptors,
4445
     it's not incremented, so in order to compute the space reserved
4446
     for them, it suffices to multiply the reloc count by the jump
4447
     slot size.  */
4448
0
  if (htab->tls_desc_trampoline && htab->root.srelplt)
4449
0
    htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4450
4451
0
  if (htab->tls_trampoline)
4452
0
    {
4453
0
      htab->tls_trampoline = htab->root.splt->size;
4454
4455
      /* If we're not using lazy TLS relocations, don't generate the
4456
   PLT and GOT entries they require.  */
4457
0
      if ((info->flags & DF_BIND_NOW))
4458
0
  htab->root.tlsdesc_plt = 0;
4459
0
      else
4460
0
  {
4461
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
4462
0
    htab->root.sgot->size += 4;
4463
4464
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
4465
0
    htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4466
0
  }
4467
0
    }
4468
4469
  /* We now have determined the sizes of the various dynamic sections.
4470
     Allocate memory for them.  */
4471
  /* The check_relocs and adjust_dynamic_symbol entry points have
4472
     determined the sizes of the various dynamic sections.  Allocate
4473
     memory for them.  */
4474
0
  relocs = false;
4475
0
  for (s = dynobj->sections; s != NULL; s = s->next)
4476
0
    {
4477
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
4478
0
  continue;
4479
4480
0
      if (s == htab->root.splt)
4481
0
  {
4482
    /* Strip this section if we don't need it; see the
4483
       comment below.  */
4484
0
    ;
4485
0
  }
4486
0
      else if (s == elf_hash_table (info)->sgot)
4487
0
  {
4488
0
    got_size += s->size;
4489
0
  }
4490
0
      else if (s == elf_hash_table (info)->sgotplt)
4491
0
  {
4492
0
    got_size += s->size;
4493
0
  }
4494
0
      else if (startswith (bfd_section_name (s), ".rela"))
4495
0
  {
4496
0
    if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4497
0
      relocs = true;
4498
4499
    /* We use the reloc_count field as a counter if we need
4500
       to copy relocs into the output file.  */
4501
0
    s->reloc_count = 0;
4502
0
  }
4503
0
      else
4504
0
  {
4505
    /* It's not one of our sections, so don't allocate space.  */
4506
0
    continue;
4507
0
  }
4508
4509
0
      if (s->size == 0)
4510
0
  {
4511
    /* If we don't need this section, strip it from the
4512
       output file.  This is mostly to handle .rela.bss and
4513
       .rela.plt.  We must create both sections in
4514
       create_dynamic_sections, because they must be created
4515
       before the linker maps input sections to output
4516
       sections.  The linker does that before
4517
       adjust_dynamic_symbol is called, and it is that
4518
       function which decides whether anything needs to go
4519
       into these sections.  */
4520
0
    s->flags |= SEC_EXCLUDE;
4521
0
    continue;
4522
0
  }
4523
4524
      /* Allocate memory for the section contents.  We use bfd_zalloc
4525
   here in case unused entries are not reclaimed before the
4526
   section's contents are written out.  This should not happen,
4527
   but this way if it does, we get a R_NDS32_NONE reloc instead
4528
   of garbage.  */
4529
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4530
0
      if (s->contents == NULL)
4531
0
  return false;
4532
0
      s->alloced = 1;
4533
0
    }
4534
4535
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
4536
0
}
4537
4538
static bfd_reloc_status_type
4539
nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4540
       bfd_vma relocation, bfd_byte *location)
4541
0
{
4542
0
  int size;
4543
0
  bfd_vma x = 0;
4544
0
  bfd_reloc_status_type flag;
4545
0
  unsigned int rightshift = howto->rightshift;
4546
0
  unsigned int bitpos = howto->bitpos;
4547
4548
0
  if (howto->negate)
4549
0
    relocation = -relocation;
4550
4551
  /* Get the value we are going to relocate.  */
4552
0
  size = bfd_get_reloc_size (howto);
4553
0
  switch (size)
4554
0
    {
4555
0
    default:
4556
0
      abort ();
4557
0
      break;
4558
0
    case 0:
4559
0
      return bfd_reloc_ok;
4560
0
    case 2:
4561
0
      x = bfd_getb16 (location);
4562
0
      break;
4563
0
    case 4:
4564
0
      x = bfd_getb32 (location);
4565
0
      break;
4566
0
    }
4567
4568
  /* Check for overflow.  FIXME: We may drop bits during the addition
4569
     which we don't check for.  We must either check at every single
4570
     operation, which would be tedious, or we must do the computations
4571
     in a type larger than bfd_vma, which would be inefficient.  */
4572
0
  flag = bfd_reloc_ok;
4573
0
  if (howto->complain_on_overflow != complain_overflow_dont)
4574
0
    {
4575
0
      bfd_vma addrmask, fieldmask, signmask, ss;
4576
0
      bfd_vma a, b, sum;
4577
4578
      /* Get the values to be added together.  For signed and unsigned
4579
   relocations, we assume that all values should be truncated to
4580
   the size of an address.  For bitfields, all the bits matter.
4581
   See also bfd_check_overflow.  */
4582
0
      fieldmask = N_ONES (howto->bitsize);
4583
0
      signmask = ~fieldmask;
4584
0
      addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4585
0
      a = (relocation & addrmask) >> rightshift;
4586
0
      b = (x & howto->src_mask & addrmask) >> bitpos;
4587
4588
0
      switch (howto->complain_on_overflow)
4589
0
  {
4590
0
  case complain_overflow_signed:
4591
    /* If any sign bits are set, all sign bits must be set.
4592
       That is, A must be a valid negative address after
4593
       shifting.  */
4594
0
    signmask = ~(fieldmask >> 1);
4595
    /* Fall through.  */
4596
4597
0
  case complain_overflow_bitfield:
4598
    /* Much like the signed check, but for a field one bit
4599
       wider.  We allow a bitfield to represent numbers in the
4600
       range -2**n to 2**n-1, where n is the number of bits in the
4601
       field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4602
       can't overflow, which is exactly what we want.  */
4603
0
    ss = a & signmask;
4604
0
    if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4605
0
      flag = bfd_reloc_overflow;
4606
4607
    /* We only need this next bit of code if the sign bit of B
4608
       is below the sign bit of A.  This would only happen if
4609
       SRC_MASK had fewer bits than BITSIZE.  Note that if
4610
       SRC_MASK has more bits than BITSIZE, we can get into
4611
       trouble; we would need to verify that B is in range, as
4612
       we do for A above.  */
4613
0
    ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4614
0
    ss >>= bitpos;
4615
4616
    /* Set all the bits above the sign bit.  */
4617
0
    b = (b ^ ss) - ss;
4618
4619
    /* Now we can do the addition.  */
4620
0
    sum = a + b;
4621
4622
    /* See if the result has the correct sign.  Bits above the
4623
       sign bit are junk now; ignore them.  If the sum is
4624
       positive, make sure we did not have all negative inputs;
4625
       if the sum is negative, make sure we did not have all
4626
       positive inputs.  The test below looks only at the sign
4627
       bits, and it really just
4628
       SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4629
4630
       We mask with addrmask here to explicitly allow an address
4631
       wrap-around.  The Linux kernel relies on it, and it is
4632
       the only way to write assembler code which can run when
4633
       loaded at a location 0x80000000 away from the location at
4634
       which it is linked.  */
4635
0
    if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4636
0
      flag = bfd_reloc_overflow;
4637
4638
0
    break;
4639
4640
0
  case complain_overflow_unsigned:
4641
    /* Checking for an unsigned overflow is relatively easy:
4642
       trim the addresses and add, and trim the result as well.
4643
       Overflow is normally indicated when the result does not
4644
       fit in the field.  However, we also need to consider the
4645
       case when, e.g., fieldmask is 0x7fffffff or smaller, an
4646
       input is 0x80000000, and bfd_vma is only 32 bits; then we
4647
       will get sum == 0, but there is an overflow, since the
4648
       inputs did not fit in the field.  Instead of doing a
4649
       separate test, we can check for this by or-ing in the
4650
       operands when testing for the sum overflowing its final
4651
       field.  */
4652
0
    sum = (a + b) & addrmask;
4653
0
    if ((a | b | sum) & signmask)
4654
0
      flag = bfd_reloc_overflow;
4655
0
    break;
4656
4657
0
  default:
4658
0
    abort ();
4659
0
  }
4660
0
    }
4661
4662
  /* Put RELOCATION in the right bits.  */
4663
0
  relocation >>= (bfd_vma) rightshift;
4664
0
  relocation <<= (bfd_vma) bitpos;
4665
4666
  /* Add RELOCATION to the right bits of X.  */
4667
  /* FIXME : 090616
4668
     Because the relaxation may generate duplicate relocation at one address,
4669
     an addition to immediate in the instruction may cause the relocation added
4670
     several times.
4671
     This bug should be fixed in assembler, but a check is also needed here.  */
4672
0
  if (howto->partial_inplace)
4673
0
    x = ((x & ~howto->dst_mask)
4674
0
   | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4675
0
  else
4676
0
    x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4677
4678
4679
  /* Put the relocated value back in the object file.  */
4680
0
  switch (size)
4681
0
    {
4682
0
    default:
4683
0
    case 0:
4684
0
    case 1:
4685
0
    case 8:
4686
0
      abort ();
4687
0
      break;
4688
0
    case 2:
4689
0
      bfd_putb16 (x, location);
4690
0
      break;
4691
0
    case 4:
4692
0
      bfd_putb32 (x, location);
4693
0
      break;
4694
0
    }
4695
4696
0
  return flag;
4697
0
}
4698
4699
static bfd_reloc_status_type
4700
nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4701
             asection *input_section, bfd_byte *contents,
4702
             bfd_vma address, bfd_vma value, bfd_vma addend)
4703
0
{
4704
0
  bfd_vma relocation;
4705
4706
  /* Sanity check the address.  */
4707
0
  bfd_vma octet = address * bfd_octets_per_byte (input_bfd, input_section);
4708
0
  if (!bfd_reloc_offset_in_range (howto, input_bfd, input_section, octet))
4709
0
    return bfd_reloc_outofrange;
4710
4711
  /* This function assumes that we are dealing with a basic relocation
4712
     against a symbol.  We want to compute the value of the symbol to
4713
     relocate to.  This is just VALUE, the value of the symbol, plus
4714
     ADDEND, any addend associated with the reloc.  */
4715
0
  relocation = value + addend;
4716
4717
  /* If the relocation is PC relative, we want to set RELOCATION to
4718
     the distance between the symbol (currently in RELOCATION) and the
4719
     location we are relocating.  If pcrel_offset is FALSE we do not
4720
     need to subtract out the offset of the location within the
4721
     section (which is just ADDRESS).  */
4722
0
  if (howto->pc_relative)
4723
0
    {
4724
0
      relocation -= (input_section->output_section->vma
4725
0
         + input_section->output_offset);
4726
0
      if (howto->pcrel_offset)
4727
0
  relocation -= address;
4728
0
    }
4729
4730
0
  return nds32_relocate_contents (howto, input_bfd, relocation,
4731
0
          contents + address);
4732
0
}
4733
4734
static int
4735
nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4736
            const char *name,
4737
            Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4738
            asection *input_sec,
4739
            struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4740
0
{
4741
0
  const char *source;
4742
0
  FILE *sym_ld_script = NULL;
4743
0
  struct elf_nds32_link_hash_table *table;
4744
4745
0
  table = nds32_elf_hash_table (info);
4746
0
  sym_ld_script = table->sym_ld_script;
4747
0
  if (!sym_ld_script)
4748
0
    return true;
4749
4750
0
  if (!h || !name || *name == '\0')
4751
0
    return true;
4752
4753
0
  if (input_sec->flags & SEC_EXCLUDE)
4754
0
    return true;
4755
4756
0
  if (!check_start_export_sym)
4757
0
    {
4758
0
      fprintf (sym_ld_script, "SECTIONS\n{\n");
4759
0
      check_start_export_sym = 1;
4760
0
    }
4761
4762
0
  if (h->root.type == bfd_link_hash_defined
4763
0
      || h->root.type == bfd_link_hash_defweak)
4764
0
    {
4765
0
      if (!h->root.u.def.section->output_section)
4766
0
  return true;
4767
4768
0
      if (bfd_is_const_section (input_sec))
4769
0
  source = input_sec->name;
4770
0
      else
4771
0
  source = bfd_get_filename (input_sec->owner);
4772
4773
0
      fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4774
0
         h->root.root.string,
4775
0
         (long) (h->root.u.def.value
4776
0
    + h->root.u.def.section->output_section->vma
4777
0
    + h->root.u.def.section->output_offset), source);
4778
0
    }
4779
4780
0
  return true;
4781
0
}
4782
4783
/* Relocate an NDS32/D ELF section.
4784
   There is some attempt to make this function usable for many architectures,
4785
   both for RELA and REL type relocs, if only to serve as a learning tool.
4786
4787
   The RELOCATE_SECTION function is called by the new ELF backend linker
4788
   to handle the relocations for a section.
4789
4790
   The relocs are always passed as Rela structures; if the section
4791
   actually uses Rel structures, the r_addend field will always be
4792
   zero.
4793
4794
   This function is responsible for adjust the section contents as
4795
   necessary, and (if using Rela relocs and generating a
4796
   relocatable output file) adjusting the reloc addend as
4797
   necessary.
4798
4799
   This function does not have to worry about setting the reloc
4800
   address or the reloc symbol index.
4801
4802
   LOCAL_SYMS is a pointer to the swapped in local symbols.
4803
4804
   LOCAL_SECTIONS is an array giving the section in the input file
4805
   corresponding to the st_shndx field of each local symbol.
4806
4807
   The global hash table entry for the global symbols can be found
4808
   via elf_sym_hashes (input_bfd).
4809
4810
   When generating relocatable output, this function must handle
4811
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4812
   going to be the section symbol corresponding to the output
4813
   section, which means that the addend must be adjusted
4814
   accordingly.  */
4815
4816
/* Return the base VMA address which should be subtracted from real addresses
4817
   when resolving @dtpoff relocation.
4818
   This is PT_TLS segment p_vaddr.  */
4819
4820
/* Return the relocation value for @tpoff relocation
4821
   if STT_TLS virtual address is ADDRESS.  */
4822
4823
/* Return the relocation value for @gottpoff relocation
4824
   if STT_TLS virtual address is ADDRESS.  */
4825
4826
static bfd_vma
4827
gottpoff (struct bfd_link_info *info, bfd_vma address)
4828
0
{
4829
0
  bfd_vma tp_base;
4830
0
  bfd_vma tp_offset;
4831
4832
  /* If tls_sec is NULL, we should have signalled an error already.  */
4833
0
  if (elf_hash_table (info)->tls_sec == NULL)
4834
0
    return 0;
4835
4836
0
  tp_base = elf_hash_table (info)->tls_sec->vma;
4837
0
  tp_offset = address - tp_base;
4838
4839
0
  return tp_offset;
4840
0
}
4841
4842
static bool
4843
patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4844
0
{
4845
  /* TLS_GD/TLS_LD model #1
4846
     46 00 00 00 sethi $r0,#0x0
4847
     58 00 00 00 ori $r0,$r0,#0x0
4848
     40 00 74 00 add $r0,$r0,$gp
4849
     04 10 00 00 lwi $r1,[$r0+#0x0]
4850
     4b e0 04 01 jral $lp,$r1  */
4851
4852
  /* TLS_GD/TLS_LD model #2
4853
     46 00 00 00 sethi $r0,#0x0
4854
     58 00 00 00 ori $r0,$r0,#0x0
4855
     38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4856
     40 00 74 00 add $r0,$r0,$gp
4857
     4b e0 04 01 jral $lp,$r1  */
4858
4859
  /* TLS_IE model (non-PIC)
4860
     46 00 00 00 sethi $r0,#0x0
4861
     04 00 00 00 lwi $r0,[$r0+#0x0]
4862
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4863
4864
  /* TLS_IE model (PIC)
4865
     46 00 00 00 sethi $r0,#0x0
4866
     58 00 00 00 ori $r0,$r0,#0x0
4867
     38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4868
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4869
4870
  /* TLS_GD_TO_IE model
4871
     46 00 00 00 sethi $r0,#0x0
4872
     58 00 00 00 ori $r0,$r0,#0x0
4873
     40 00 74 00 add $r0,$rM,$gp
4874
     04 00 00 01 lwi $r0,[$r0+#0x4]
4875
     40 00 64 00 add $r0,$r0,$r25  */
4876
4877
0
  bool rz = false;
4878
4879
0
  typedef struct
4880
0
    {
4881
0
      uint32_t opcode;
4882
0
      uint32_t mask;
4883
0
    } pat_t;
4884
4885
0
  uint32_t patch[3] =
4886
0
    {
4887
0
      0x40007400, /* add $r0,$rM,$gp     */
4888
0
      0x04000001, /* lwi $r0,[$r0+#0x4]  */
4889
0
      0x40006400, /* add $r0,$r0,$r25    */
4890
0
    };
4891
4892
0
  pat_t mode0[3] =
4893
0
    {
4894
0
  { 0x40000000, 0xfe0003ff },
4895
0
  { 0x04000000, 0xfe000000 },
4896
0
  { 0x4be00001, 0xffff83ff },
4897
0
    };
4898
4899
0
  pat_t mode1[3] =
4900
0
    {
4901
0
  { 0x38007402, 0xfe007fff },
4902
0
  { 0x40007400, 0xfe007fff },
4903
0
  { 0x4be00001, 0xffff83ff },
4904
0
    };
4905
4906
0
  unsigned char *p = contents + rel->r_offset;
4907
4908
0
  uint32_t insn;
4909
0
  uint32_t regidx = 0;
4910
0
  insn = bfd_getb32 (p);
4911
0
  if (INSN_SETHI == (0xfe0fffffu & insn))
4912
0
    {
4913
0
      regidx = 0x1f & (insn >> 20);
4914
0
      p += 4;
4915
0
    }
4916
4917
0
  insn = bfd_getb32 (p);
4918
0
  if (INSN_ORI == (0xfe007fffu & insn))
4919
0
    {
4920
0
      regidx = 0x1f & (insn >> 20);
4921
0
      p += 4;
4922
0
    }
4923
4924
0
  if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction.  */
4925
0
    {
4926
      /* already patched?  */
4927
0
      if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4928
0
    (patch[1] == bfd_getb32 (p + 4)))
4929
0
  rz = true;
4930
0
    }
4931
0
  else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4932
0
    {
4933
0
      if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4934
0
    (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4935
0
  {
4936
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4937
0
    bfd_putb32 (patch[1], p + 4);
4938
0
    bfd_putb32 (patch[2], p + 8);
4939
0
    rz = true;
4940
0
  }
4941
0
    }
4942
0
  else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4943
0
    {
4944
0
      if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4945
0
    (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
4946
0
  {
4947
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4948
0
    bfd_putb32 (patch[1], p + 4);
4949
0
    bfd_putb32 (patch[2], p + 8);
4950
0
    rz = true;
4951
0
  }
4952
0
    }
4953
4954
0
  if (!rz)
4955
0
    {
4956
0
      printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
4957
0
        (int) rel->r_offset);
4958
0
      BFD_ASSERT(0); /* Unsupported pattern.  */
4959
0
    }
4960
4961
0
  return rz;
4962
0
}
4963
4964
static enum elf_nds32_tls_type
4965
get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
4966
4967
static unsigned int
4968
ones32 (register unsigned int x)
4969
0
{
4970
  /* 32-bit recursive reduction using SWAR...
4971
     but first step is mapping 2-bit values
4972
     into sum of 2 1-bit values in sneaky way.  */
4973
0
  x -= ((x >> 1) & 0x55555555);
4974
0
  x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
4975
0
  x = (((x >> 4) + x) & 0x0f0f0f0f);
4976
0
  x += (x >> 8);
4977
0
  x += (x >> 16);
4978
0
  return (x & 0x0000003f);
4979
0
}
4980
4981
#if !HAVE_FLS
4982
static unsigned int
4983
fls (register unsigned int x)
4984
0
{
4985
0
  return ffs (x & (-x));
4986
0
}
4987
#endif /* !HAVE_FLS */
4988
4989
#define nds32_elf_local_tlsdesc_gotent(bfd) \
4990
  (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
4991
4992
static int
4993
nds32_elf_relocate_section (bfd *      output_bfd ATTRIBUTE_UNUSED,
4994
          struct bfd_link_info * info,
4995
          bfd *      input_bfd,
4996
          asection *       input_section,
4997
          bfd_byte *       contents,
4998
          Elf_Internal_Rela *    relocs,
4999
          Elf_Internal_Sym *     local_syms,
5000
          asection **      local_sections)
5001
0
{
5002
0
  Elf_Internal_Shdr *symtab_hdr;
5003
0
  struct elf_link_hash_entry **sym_hashes;
5004
0
  Elf_Internal_Rela *rel, *relend;
5005
0
  bool ret = true;   /* Assume success.  */
5006
0
  int align = 0;
5007
0
  bfd_reloc_status_type r;
5008
0
  const char *errmsg = NULL;
5009
0
  bfd_vma gp;
5010
0
  struct elf_link_hash_table *ehtab;
5011
0
  struct elf_nds32_link_hash_table *htab;
5012
0
  bfd *dynobj;
5013
0
  bfd_vma *local_got_offsets;
5014
0
  asection *sgot, *splt, *sreloc;
5015
0
  bfd_vma high_address;
5016
0
  struct elf_nds32_link_hash_table *table;
5017
0
  int eliminate_gc_relocs;
5018
0
  bfd_vma fpbase_addr;
5019
5020
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5021
0
  sym_hashes = elf_sym_hashes (input_bfd);
5022
0
  ehtab = elf_hash_table (info);
5023
0
  htab = nds32_elf_hash_table (info);
5024
0
  high_address = bfd_get_section_limit (input_bfd, input_section);
5025
5026
0
  dynobj = htab->root.dynobj;
5027
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
5028
5029
0
  sgot = ehtab->sgot;
5030
0
  splt = ehtab->splt;
5031
0
  sreloc = NULL;
5032
5033
0
  rel = relocs;
5034
0
  relend = relocs + input_section->reloc_count;
5035
5036
0
  table = nds32_elf_hash_table (info);
5037
0
  eliminate_gc_relocs = table->eliminate_gc_relocs;
5038
5039
  /* By this time, we can adjust the value of _SDA_BASE_.  */
5040
  /* Explain _SDA_BASE_  */
5041
0
  if ((!bfd_link_relocatable (info)))
5042
0
    {
5043
0
      is_SDA_BASE_set = 1;
5044
0
      r = nds32_elf_final_sda_base (output_bfd, info, &gp, true);
5045
0
      if (r != bfd_reloc_ok)
5046
0
  return false;
5047
0
    }
5048
5049
  /* Do TLS model conversion once at first.  */
5050
0
  nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5051
5052
  /* Use gp as fp to prevent truncated fit.  Because in relaxation time
5053
     the fp value is set as gp, and it has be reverted for instruction
5054
     setting fp.  */
5055
0
  fpbase_addr = elf_gp (output_bfd);
5056
5057
  /* Deal with (dynamic) relocations.  */
5058
0
  for (rel = relocs; rel < relend; rel++)
5059
0
    {
5060
0
      enum elf_nds32_reloc_type r_type;
5061
0
      reloc_howto_type *howto = NULL;
5062
0
      unsigned long r_symndx;
5063
0
      struct elf_link_hash_entry *h = NULL;
5064
0
      Elf_Internal_Sym *sym = NULL;
5065
0
      asection *sec;
5066
0
      bfd_vma relocation;
5067
0
      bfd_vma relocation_sym = 0xdeadbeef;
5068
0
      Elf_Internal_Rela *lorel;
5069
0
      bfd_vma off;
5070
5071
      /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5072
   ensure it's zero (we use REL relocs, not RELA).  Therefore this
5073
   should be assigning zero to `addend', but for clarity we use
5074
   `r_addend'.  */
5075
5076
0
      bfd_vma addend = rel->r_addend;
5077
0
      bfd_vma offset = rel->r_offset;
5078
5079
0
      r_type = ELF32_R_TYPE (rel->r_info);
5080
0
      if (r_type >= R_NDS32_max)
5081
0
  {
5082
    /* xgettext:c-format */
5083
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5084
0
            input_bfd, r_type);
5085
0
    bfd_set_error (bfd_error_bad_value);
5086
0
    ret = false;
5087
0
    continue;
5088
0
  }
5089
5090
0
      if (r_type == R_NDS32_GNU_VTENTRY
5091
0
    || r_type == R_NDS32_GNU_VTINHERIT
5092
0
    || r_type == R_NDS32_NONE
5093
0
    || r_type == R_NDS32_RELA_GNU_VTENTRY
5094
0
    || r_type == R_NDS32_RELA_GNU_VTINHERIT
5095
0
    || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5096
0
    || r_type == R_NDS32_DATA
5097
0
    || r_type == R_NDS32_TRAN)
5098
0
  continue;
5099
5100
      /* If we enter the fp-as-gp region.  Resolve the address
5101
   of best fp-base.  */
5102
0
      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5103
0
    && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5104
0
  {
5105
0
    int dist;
5106
5107
    /* Distance to relocation of best fp-base is encoded in R_SYM.  */
5108
0
    dist =  rel->r_addend >> 16;
5109
0
    fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5110
0
              local_syms, symtab_hdr);
5111
0
  }
5112
0
      else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5113
0
         && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5114
0
  {
5115
0
    fpbase_addr = elf_gp (output_bfd);
5116
0
  }
5117
5118
      /* Skip the relocations used for relaxation.  */
5119
      /* We have to update LONGCALL and LONGJUMP
5120
   relocations when generating the relocatable files.  */
5121
0
      if (!bfd_link_relocatable (info)
5122
0
    && (r_type >= R_NDS32_RELAX_ENTRY
5123
0
        || (r_type >= R_NDS32_LONGCALL4
5124
0
      && r_type <= R_NDS32_LONGJUMP7)))
5125
0
  continue;
5126
5127
0
      howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5128
0
      r_symndx = ELF32_R_SYM (rel->r_info);
5129
5130
      /* This is a final link.  */
5131
0
      sym = NULL;
5132
0
      sec = NULL;
5133
0
      h = NULL;
5134
5135
0
      if (r_symndx < symtab_hdr->sh_info)
5136
0
  {
5137
    /* Local symbol.  */
5138
0
    sym = local_syms + r_symndx;
5139
0
    sec = local_sections[r_symndx];
5140
5141
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5142
0
    addend = rel->r_addend;
5143
5144
    /* keep symbol location for static TLS_IE GOT entry  */
5145
0
    relocation_sym = relocation;
5146
0
    if (bfd_link_relocatable (info))
5147
0
      {
5148
        /* This is a relocatable link.  We don't have to change
5149
     anything, unless the reloc is against a section symbol,
5150
     in which case we have to adjust according to where the
5151
     section symbol winds up in the output section.  */
5152
0
        if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5153
0
    rel->r_addend += sec->output_offset + sym->st_value;
5154
5155
0
        continue;
5156
0
      }
5157
0
  }
5158
0
      else
5159
0
  {
5160
    /* External symbol.  */
5161
0
    if (bfd_link_relocatable (info))
5162
0
      continue;
5163
0
    bool warned, ignored, unresolved_reloc;
5164
0
    int symndx = r_symndx - symtab_hdr->sh_info;
5165
5166
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5167
0
           r_symndx, symtab_hdr, sym_hashes, h, sec,
5168
0
           relocation, unresolved_reloc, warned,
5169
0
           ignored);
5170
5171
    /* keep symbol location for static TLS_IE GOT entry  */
5172
0
    relocation_sym = relocation;
5173
5174
    /* la $fp, _FP_BASE_ is per-function (region).
5175
       Handle it specially.  */
5176
0
    switch ((int) r_type)
5177
0
      {
5178
0
      case R_NDS32_HI20_RELA:
5179
0
      case R_NDS32_LO12S0_RELA:
5180
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5181
0
        FP_BASE_NAME) == 0)
5182
0
    {
5183
0
      if (!bfd_link_pie (info))
5184
0
        {
5185
0
          _bfd_error_handler
5186
0
      ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5187
0
       input_bfd);
5188
0
        }
5189
0
      relocation = fpbase_addr;
5190
0
    }
5191
0
        break;
5192
0
      case R_NDS32_SDA19S0_RELA:
5193
0
      case R_NDS32_SDA15S0_RELA:
5194
0
      case R_NDS32_20_RELA:
5195
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5196
0
        FP_BASE_NAME) == 0)
5197
0
    {
5198
0
      relocation = fpbase_addr;
5199
0
      break;
5200
0
    }
5201
0
      }
5202
0
  }
5203
5204
      /* Sanity check the address.  */
5205
0
      if (offset > high_address)
5206
0
  {
5207
0
    r = bfd_reloc_outofrange;
5208
0
    goto check_reloc;
5209
0
  }
5210
5211
0
      if (r_type >= R_NDS32_RELAX_ENTRY)
5212
0
  continue;
5213
5214
0
      switch ((int) r_type)
5215
0
  {
5216
0
  case R_NDS32_GOTOFF:
5217
    /* Relocation is relative to the start of the global offset
5218
       table (for ld24 rx, #uimm24), e.g. access at label+addend
5219
5220
       ld24 rx. #label@GOTOFF + addend
5221
       sub  rx, r12.  */
5222
0
  case R_NDS32_GOTOFF_HI20:
5223
0
  case R_NDS32_GOTOFF_LO12:
5224
0
  case R_NDS32_GOTOFF_LO15:
5225
0
  case R_NDS32_GOTOFF_LO19:
5226
0
    BFD_ASSERT (sgot != NULL);
5227
5228
0
    relocation -= elf_gp (output_bfd);
5229
0
    break;
5230
5231
0
  case R_NDS32_9_PLTREL:
5232
0
  case R_NDS32_25_PLTREL:
5233
    /* Relocation is to the entry for this symbol in the
5234
       procedure linkage table.  */
5235
5236
    /* The native assembler will generate a 25_PLTREL reloc
5237
       for a local symbol if you assemble a call from one
5238
       section to another when using -K pic.  */
5239
0
    if (h == NULL)
5240
0
      break;
5241
5242
0
    if (h->forced_local)
5243
0
      break;
5244
5245
    /* We didn't make a PLT entry for this symbol.  This
5246
       happens when statically linking PIC code, or when
5247
       using -Bsymbolic.  */
5248
0
    if (h->plt.offset == (bfd_vma) - 1)
5249
0
      break;
5250
5251
0
    relocation = (splt->output_section->vma
5252
0
      + splt->output_offset + h->plt.offset);
5253
0
    break;
5254
5255
0
  case R_NDS32_PLT_GOTREL_HI20:
5256
0
  case R_NDS32_PLT_GOTREL_LO12:
5257
0
  case R_NDS32_PLT_GOTREL_LO15:
5258
0
  case R_NDS32_PLT_GOTREL_LO19:
5259
0
  case R_NDS32_PLT_GOTREL_LO20:
5260
0
    if (h == NULL
5261
0
        || h->forced_local
5262
0
        || h->plt.offset == (bfd_vma) -1
5263
0
        || (bfd_link_pie (info) && h->def_regular))
5264
0
      {
5265
        /* Maybe we should find better checking to optimize
5266
     PIE PLT relocations.  */
5267
        /* We didn't make a PLT entry for this symbol.  This
5268
     happens when statically linking PIC code, or when
5269
     using -Bsymbolic.  */
5270
0
        if (h)
5271
0
    h->plt.offset = (bfd_vma) -1;   /* Cancel PLT trampoline.  */
5272
0
        relocation -= elf_gp (output_bfd);
5273
0
        break;
5274
0
      }
5275
5276
0
    relocation = (splt->output_section->vma
5277
0
      + splt->output_offset + h->plt.offset);
5278
5279
0
    relocation -= elf_gp (output_bfd);
5280
0
    break;
5281
5282
0
  case R_NDS32_PLTREL_HI20:
5283
0
  case R_NDS32_PLTREL_LO12:
5284
5285
    /* Relocation is to the entry for this symbol in the
5286
       procedure linkage table.  */
5287
5288
    /* The native assembler will generate a 25_PLTREL reloc
5289
       for a local symbol if you assemble a call from one
5290
       section to another when using -K pic.  */
5291
0
    if (h == NULL)
5292
0
      break;
5293
5294
0
    if (h->forced_local)
5295
0
      break;
5296
5297
0
    if (h->plt.offset == (bfd_vma) - 1)
5298
      /* We didn't make a PLT entry for this symbol.  This
5299
         happens when statically linking PIC code, or when
5300
         using -Bsymbolic.  */
5301
0
      break;
5302
5303
0
    if (splt == NULL)
5304
0
      break;
5305
5306
0
    relocation = (splt->output_section->vma
5307
0
      + splt->output_offset
5308
0
      + h->plt.offset + 4)
5309
0
           - (input_section->output_section->vma
5310
0
        + input_section->output_offset
5311
0
        + rel->r_offset);
5312
5313
0
    break;
5314
5315
0
  case R_NDS32_GOTPC20:
5316
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5317
       ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
5318
0
    relocation = elf_gp (output_bfd);
5319
0
    break;
5320
5321
0
  case R_NDS32_GOTPC_HI20:
5322
0
  case R_NDS32_GOTPC_LO12:
5323
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5324
       bl .+4
5325
       seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5326
       or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5327
       or
5328
       bl .+4
5329
       seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5330
       add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)  */
5331
0
    relocation = elf_gp (output_bfd);
5332
0
    relocation -= (input_section->output_section->vma
5333
0
       + input_section->output_offset + rel->r_offset);
5334
0
    break;
5335
5336
0
  case R_NDS32_GOT20:
5337
    /* Fall through.  */
5338
0
  case R_NDS32_GOT_HI20:
5339
0
  case R_NDS32_GOT_LO12:
5340
0
  case R_NDS32_GOT_LO15:
5341
0
  case R_NDS32_GOT_LO19:
5342
    /* Relocation is to the entry for this symbol in the global
5343
       offset table.  */
5344
0
    BFD_ASSERT (sgot != NULL);
5345
5346
0
    if (h != NULL)
5347
0
      {
5348
        /* External symbol  */
5349
0
        bool dyn;
5350
5351
0
        off = h->got.offset;
5352
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5353
0
        dyn = htab->root.dynamic_sections_created;
5354
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5355
0
                bfd_link_pic (info),
5356
0
                h)
5357
0
      || (bfd_link_pic (info)
5358
0
          && (info->symbolic
5359
0
        || h->dynindx == -1
5360
0
        || h->forced_local) && h->def_regular))
5361
0
    {
5362
      /* This is actually a static link, or it is a
5363
         -Bsymbolic link and the symbol is defined
5364
         locally, or the symbol was forced to be local
5365
         because of a version file.  We must initialize
5366
         this entry in the global offset table.  Since the
5367
         offset must always be a multiple of 4, we use the
5368
         least significant bit to record whether we have
5369
         initialized it already.
5370
5371
         When doing a dynamic link, we create a .rela.got
5372
         relocation entry to initialize the value.  This
5373
         is done in the finish_dynamic_symbol routine.  */
5374
0
      if ((off & 1) != 0) /* clear LSB  */
5375
0
        off &= ~1;
5376
0
      else
5377
0
        {
5378
0
          bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5379
0
          h->got.offset |= 1;
5380
0
        }
5381
0
    }
5382
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5383
0
         - elf_gp (output_bfd);
5384
0
      }
5385
0
    else
5386
0
      {
5387
        /* Local symbol  */
5388
0
        bfd_byte *loc;
5389
5390
0
        BFD_ASSERT (local_got_offsets != NULL
5391
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5392
5393
0
        off = local_got_offsets[r_symndx];
5394
5395
        /* The offset must always be a multiple of 4.  We use
5396
     the least significant bit to record whether we have
5397
     already processed this entry.  */
5398
0
        if ((off & 1) != 0) /* clear LSB  */
5399
0
    off &= ~1;
5400
0
        else
5401
0
    {
5402
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5403
5404
0
      if (bfd_link_pic (info))
5405
0
        {
5406
0
          asection *srelgot;
5407
0
          Elf_Internal_Rela outrel;
5408
5409
          /* We need to generate a R_NDS32_RELATIVE reloc
5410
       for the dynamic linker.  */
5411
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5412
0
          BFD_ASSERT (srelgot != NULL);
5413
5414
0
          outrel.r_offset = (elf_gp (output_bfd)
5415
0
           + sgot->output_offset + off);
5416
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5417
0
          outrel.r_addend = relocation;
5418
0
          loc = srelgot->contents;
5419
0
          loc +=
5420
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5421
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5422
0
          ++srelgot->reloc_count;
5423
0
        }
5424
0
      local_got_offsets[r_symndx] |= 1;
5425
0
    }
5426
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5427
0
         - elf_gp (output_bfd);
5428
0
      }
5429
5430
0
    break;
5431
5432
0
  case R_NDS32_16_RELA:
5433
0
  case R_NDS32_20_RELA:
5434
0
  case R_NDS32_5_RELA:
5435
0
  case R_NDS32_32_RELA:
5436
0
  case R_NDS32_9_PCREL_RELA:
5437
0
  case R_NDS32_WORD_9_PCREL_RELA:
5438
0
  case R_NDS32_10_UPCREL_RELA:
5439
0
  case R_NDS32_15_PCREL_RELA:
5440
0
  case R_NDS32_17_PCREL_RELA:
5441
0
  case R_NDS32_25_PCREL_RELA:
5442
0
  case R_NDS32_HI20_RELA:
5443
0
  case R_NDS32_LO12S3_RELA:
5444
0
  case R_NDS32_LO12S2_RELA:
5445
0
  case R_NDS32_LO12S2_DP_RELA:
5446
0
  case R_NDS32_LO12S2_SP_RELA:
5447
0
  case R_NDS32_LO12S1_RELA:
5448
0
  case R_NDS32_LO12S0_RELA:
5449
0
  case R_NDS32_LO12S0_ORI_RELA:
5450
0
    if (bfd_link_pic (info) && r_symndx != 0
5451
0
        && (input_section->flags & SEC_ALLOC) != 0
5452
0
        && (eliminate_gc_relocs == 0
5453
0
      || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5454
0
        && ((r_type != R_NDS32_9_PCREL_RELA
5455
0
       && r_type != R_NDS32_WORD_9_PCREL_RELA
5456
0
       && r_type != R_NDS32_10_UPCREL_RELA
5457
0
       && r_type != R_NDS32_15_PCREL_RELA
5458
0
       && r_type != R_NDS32_17_PCREL_RELA
5459
0
       && r_type != R_NDS32_25_PCREL_RELA
5460
0
       && !(r_type == R_NDS32_32_RELA
5461
0
      && strcmp (input_section->name, ".eh_frame") == 0))
5462
0
      || (h != NULL && h->dynindx != -1
5463
0
          && (!info->symbolic || !h->def_regular))))
5464
0
      {
5465
0
        Elf_Internal_Rela outrel;
5466
0
        bool skip, relocate;
5467
0
        bfd_byte *loc;
5468
5469
        /* When generating a shared object, these relocations
5470
     are copied into the output file to be resolved at run
5471
     time.  */
5472
5473
0
        if (sreloc == NULL)
5474
0
    {
5475
0
      const char *name;
5476
5477
0
      name = bfd_elf_string_from_elf_section
5478
0
        (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5479
0
         elf_section_data (input_section)->rela.hdr->sh_name);
5480
0
      if (name == NULL)
5481
0
        return false;
5482
5483
0
      BFD_ASSERT (startswith (name, ".rela")
5484
0
            && strcmp (bfd_section_name (input_section),
5485
0
           name + 5) == 0);
5486
5487
0
      sreloc = bfd_get_section_by_name (dynobj, name);
5488
0
      BFD_ASSERT (sreloc != NULL);
5489
0
    }
5490
5491
0
        skip = false;
5492
0
        relocate = false;
5493
5494
0
        outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5495
0
               info,
5496
0
               input_section,
5497
0
               rel->r_offset);
5498
0
        if (outrel.r_offset == (bfd_vma) - 1)
5499
0
    skip = true;
5500
0
        else if (outrel.r_offset == (bfd_vma) - 2)
5501
0
    skip = true, relocate = true;
5502
0
        outrel.r_offset += (input_section->output_section->vma
5503
0
          + input_section->output_offset);
5504
5505
0
        if (skip)
5506
0
    memset (&outrel, 0, sizeof outrel);
5507
0
        else if (r_type == R_NDS32_17_PCREL_RELA
5508
0
           || r_type == R_NDS32_15_PCREL_RELA
5509
0
           || r_type == R_NDS32_25_PCREL_RELA)
5510
0
    {
5511
0
      BFD_ASSERT (h != NULL && h->dynindx != -1);
5512
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5513
0
      outrel.r_addend = rel->r_addend;
5514
0
    }
5515
0
        else
5516
0
    {
5517
      /* h->dynindx may be -1 if this symbol was marked to
5518
         become local.  */
5519
0
      if (h == NULL
5520
0
          || ((info->symbolic || h->dynindx == -1)
5521
0
        && h->def_regular)
5522
0
          || (bfd_link_pie (info) && h->def_regular))
5523
0
        {
5524
0
          relocate = true;
5525
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5526
0
          outrel.r_addend = relocation + rel->r_addend;
5527
5528
0
          if (h)
5529
0
      {
5530
0
        h->plt.offset = (bfd_vma) -1;   /* cancel PLT trampoline.  */
5531
5532
0
        BFD_ASSERT (sgot != NULL);
5533
        /* If we did not allocate got entry for the symbol,
5534
           we can not fill the nonexistent got entry.  */
5535
0
        if (h->got.offset != (bfd_vma) -1
5536
0
            && (h->got.offset & 1) == 0)
5537
0
          {
5538
0
            bfd_put_32 (output_bfd, outrel.r_addend,
5539
0
            sgot->contents + h->got.offset);
5540
0
          }
5541
0
      }
5542
0
        }
5543
0
      else
5544
0
        {
5545
0
          if (h->dynindx == -1)
5546
0
      {
5547
0
        _bfd_error_handler
5548
0
          (_("%pB: relocation %s against `%s' can not be used when "
5549
0
             "making a shared object; recompile with -fPIC"),
5550
0
           input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5551
0
        bfd_set_error (bfd_error_bad_value);
5552
0
        return false;
5553
0
      }
5554
5555
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5556
0
          outrel.r_addend = rel->r_addend;
5557
0
        }
5558
0
    }
5559
5560
0
        loc = sreloc->contents;
5561
0
        loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5562
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5563
0
        ++sreloc->reloc_count;
5564
5565
        /* If this reloc is against an external symbol, we do
5566
     not want to fiddle with the addend.  Otherwise, we
5567
     need to include the symbol value so that it becomes
5568
     an addend for the dynamic reloc.  */
5569
0
        if (!relocate)
5570
0
    continue;
5571
0
      }
5572
0
    break;
5573
5574
0
  case R_NDS32_25_ABS_RELA:
5575
0
    if (bfd_link_pic (info))
5576
0
      {
5577
0
        _bfd_error_handler
5578
0
    (_("%pB: warning: %s unsupported in shared mode"),
5579
0
     input_bfd, "R_NDS32_25_ABS_RELA");
5580
0
        return false;
5581
0
      }
5582
0
    break;
5583
5584
0
  case R_NDS32_9_PCREL:
5585
0
    r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5586
0
            contents, offset,
5587
0
            sec, relocation, addend);
5588
0
    goto check_reloc;
5589
5590
0
  case R_NDS32_HI20:
5591
    /* We allow an arbitrary number of HI20 relocs before the
5592
       LO12 reloc.  This permits gcc to emit the HI and LO relocs
5593
       itself.  */
5594
0
    for (lorel = rel + 1;
5595
0
         (lorel < relend
5596
0
    && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5597
0
      continue;
5598
0
    if (lorel < relend
5599
0
        && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5600
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5601
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5602
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5603
0
      {
5604
0
        nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5605
0
               contents, relocation + addend);
5606
0
        r = bfd_reloc_ok;
5607
0
      }
5608
0
    else
5609
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5610
0
            contents, offset, relocation,
5611
0
            addend);
5612
0
    goto check_reloc;
5613
5614
0
  case R_NDS32_GOT17S2_RELA:
5615
0
  case R_NDS32_GOT15S2_RELA:
5616
0
    BFD_ASSERT (sgot != NULL);
5617
5618
0
    if (h != NULL)
5619
0
      {
5620
0
        bool dyn;
5621
5622
0
        off = h->got.offset;
5623
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5624
5625
0
        dyn = htab->root.dynamic_sections_created;
5626
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5627
0
      (dyn, bfd_link_pic (info), h)
5628
0
      || (bfd_link_pic (info)
5629
0
          && (info->symbolic
5630
0
        || h->dynindx == -1
5631
0
        || h->forced_local)
5632
0
          && h->def_regular))
5633
0
    {
5634
      /* This is actually a static link, or it is a
5635
         -Bsymbolic link and the symbol is defined
5636
         locally, or the symbol was forced to be local
5637
         because of a version file.  We must initialize
5638
         this entry in the global offset table.  Since the
5639
         offset must always be a multiple of 4, we use the
5640
         least significant bit to record whether we have
5641
         initialized it already.
5642
5643
         When doing a dynamic link, we create a .rela.got
5644
         relocation entry to initialize the value.  This
5645
         is done in the finish_dynamic_symbol routine.  */
5646
0
      if ((off & 1) != 0)
5647
0
        off &= ~1;
5648
0
      else
5649
0
        {
5650
0
          bfd_put_32 (output_bfd, relocation,
5651
0
          sgot->contents + off);
5652
0
          h->got.offset |= 1;
5653
0
        }
5654
0
    }
5655
0
      }
5656
0
    else
5657
0
      {
5658
0
        bfd_byte *loc;
5659
5660
0
        BFD_ASSERT (local_got_offsets != NULL
5661
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5662
5663
0
        off = local_got_offsets[r_symndx];
5664
5665
        /* The offset must always be a multiple of 4.  We use
5666
     the least significant bit to record whether we have
5667
     already processed this entry.  */
5668
0
        if ((off & 1) != 0)
5669
0
    off &= ~1;
5670
0
        else
5671
0
    {
5672
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5673
5674
0
      if (bfd_link_pic (info))
5675
0
        {
5676
0
          asection *srelgot;
5677
0
          Elf_Internal_Rela outrel;
5678
5679
          /* We need to generate a R_NDS32_RELATIVE reloc
5680
       for the dynamic linker.  */
5681
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5682
0
          BFD_ASSERT (srelgot != NULL);
5683
5684
0
          outrel.r_offset = (elf_gp (output_bfd)
5685
0
           + sgot->output_offset + off);
5686
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5687
0
          outrel.r_addend = relocation;
5688
0
          loc = srelgot->contents;
5689
0
          loc +=
5690
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5691
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5692
0
          ++srelgot->reloc_count;
5693
0
        }
5694
0
      local_got_offsets[r_symndx] |= 1;
5695
0
    }
5696
0
      }
5697
0
    relocation = sgot->output_section->vma + sgot->output_offset + off
5698
0
      - elf_gp (output_bfd);
5699
5700
0
    if (relocation & align)
5701
0
      {
5702
        /* Incorrect alignment.  */
5703
0
        _bfd_error_handler
5704
0
    (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5705
0
        ret = false;
5706
0
        r = bfd_reloc_dangerous;
5707
0
        goto check_reloc;
5708
0
      }
5709
0
    break;
5710
5711
0
  case R_NDS32_SDA16S3_RELA:
5712
0
  case R_NDS32_SDA15S3_RELA:
5713
0
  case R_NDS32_SDA15S3:
5714
0
    align = 0x7;
5715
0
    goto handle_sda;
5716
5717
0
  case R_NDS32_SDA17S2_RELA:
5718
0
  case R_NDS32_SDA15S2_RELA:
5719
0
  case R_NDS32_SDA12S2_SP_RELA:
5720
0
  case R_NDS32_SDA12S2_DP_RELA:
5721
0
  case R_NDS32_SDA15S2:
5722
0
  case R_NDS32_SDA_FP7U2_RELA:
5723
0
    align = 0x3;
5724
0
    goto handle_sda;
5725
5726
0
  case R_NDS32_SDA18S1_RELA:
5727
0
  case R_NDS32_SDA15S1_RELA:
5728
0
  case R_NDS32_SDA15S1:
5729
0
    align = 0x1;
5730
0
    goto handle_sda;
5731
5732
0
  case R_NDS32_SDA19S0_RELA:
5733
0
  case R_NDS32_SDA15S0_RELA:
5734
0
  case R_NDS32_SDA15S0:
5735
0
    align = 0x0;
5736
0
  handle_sda:
5737
0
    BFD_ASSERT (sec != NULL);
5738
5739
    /* If the symbol is in the abs section, the out_bfd will be null.
5740
       This happens when the relocation has a symbol@GOTOFF.  */
5741
0
    r = nds32_elf_final_sda_base (output_bfd, info, &gp, false);
5742
0
    if (r != bfd_reloc_ok)
5743
0
      {
5744
0
        _bfd_error_handler
5745
0
    (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5746
0
        ret = false;
5747
0
        goto check_reloc;
5748
0
      }
5749
5750
    /* At this point `relocation' contains the object's
5751
       address.  */
5752
0
    if (r_type == R_NDS32_SDA_FP7U2_RELA)
5753
0
      {
5754
0
        relocation -= fpbase_addr;
5755
0
      }
5756
0
    else
5757
0
      relocation -= gp;
5758
    /* Now it contains the offset from _SDA_BASE_.  */
5759
5760
    /* Make sure alignment is correct.  */
5761
5762
0
    if (relocation & align)
5763
0
      {
5764
        /* Incorrect alignment.  */
5765
0
        _bfd_error_handler
5766
    /* xgettext:c-format */
5767
0
    (_("%pB(%pA): warning: unaligned small data access"
5768
0
       " of type %d"),
5769
0
     input_bfd, input_section, r_type);
5770
0
        ret = false;
5771
0
        goto check_reloc;
5772
0
      }
5773
0
    break;
5774
5775
0
  case R_NDS32_17IFC_PCREL_RELA:
5776
0
  case R_NDS32_10IFCU_PCREL_RELA:
5777
    /* Do nothing.  */
5778
0
    break;
5779
5780
0
  case R_NDS32_TLS_LE_HI20:
5781
0
  case R_NDS32_TLS_LE_LO12:
5782
0
  case R_NDS32_TLS_LE_20:
5783
0
  case R_NDS32_TLS_LE_15S0:
5784
0
  case R_NDS32_TLS_LE_15S1:
5785
0
  case R_NDS32_TLS_LE_15S2:
5786
    /* We do not have garbage collection for got entries.
5787
       Therefore, IE to LE may have one empty entry, and DESC to
5788
       LE may have two.  */
5789
0
    if (elf_hash_table (info)->tls_sec != NULL)
5790
0
      relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5791
0
    break;
5792
5793
0
  case R_NDS32_TLS_IE_HI20:
5794
0
  case R_NDS32_TLS_IE_LO12S2:
5795
0
  case R_NDS32_TLS_DESC_HI20:
5796
0
  case R_NDS32_TLS_DESC_LO12:
5797
0
  case R_NDS32_TLS_IE_LO12:
5798
0
  case R_NDS32_TLS_IEGP_HI20:
5799
0
  case R_NDS32_TLS_IEGP_LO12:
5800
0
  case R_NDS32_TLS_IEGP_LO12S2:
5801
0
    {
5802
      /* Relocation is to the entry for this symbol in the global
5803
         offset table.  */
5804
0
      enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5805
0
      asection *srelgot;
5806
0
      Elf_Internal_Rela outrel;
5807
0
      bfd_byte *loc;
5808
0
      int indx = 0;
5809
5810
0
      eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5811
5812
0
      BFD_ASSERT (sgot != NULL);
5813
0
      if (h != NULL)
5814
0
        {
5815
0
    bool dyn;
5816
5817
0
    off = h->got.offset;
5818
0
    BFD_ASSERT (off != (bfd_vma) -1);
5819
0
    dyn = htab->root.dynamic_sections_created;
5820
0
    tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5821
0
    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5822
0
        && (!bfd_link_pic (info)
5823
0
      || !SYMBOL_REFERENCES_LOCAL (info, h)))
5824
0
      indx = h->dynindx;
5825
0
        }
5826
0
      else
5827
0
        {
5828
0
    BFD_ASSERT (local_got_offsets != NULL
5829
0
          && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5830
0
    off = local_got_offsets[r_symndx];
5831
0
    tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5832
0
        }
5833
5834
0
      relocation = sgot->output_section->vma + sgot->output_offset + off;
5835
5836
0
      if (1 < ones32 (tls_type))
5837
0
        {
5838
0
    eff_tls_type = 1 << (fls (tls_type) - 1);
5839
    /* TLS model shall be handled in nds32_elf_unify_tls_model ().  */
5840
5841
    /* TLS model X -> LE is not implement yet!
5842
       workaround here!  */
5843
0
    if (eff_tls_type == GOT_TLS_LE)
5844
0
      {
5845
0
        eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5846
0
      }
5847
0
        }
5848
5849
      /* The offset must always be a multiple of 4.  We use
5850
         the least significant bit to record whether we have
5851
         already processed this entry.  */
5852
0
      bool need_relocs = false;
5853
0
      srelgot = ehtab->srelgot;
5854
0
      if ((bfd_link_pic (info) || indx != 0)
5855
0
    && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5856
0
        || h->root.type != bfd_link_hash_undefweak))
5857
0
        {
5858
0
    need_relocs = true;
5859
0
    BFD_ASSERT (srelgot != NULL);
5860
0
        }
5861
5862
0
      if (off & 1)
5863
0
        {
5864
0
    off &= ~1;
5865
0
    relocation &= ~1;
5866
5867
0
    if (eff_tls_type & GOT_TLS_DESC)
5868
0
      {
5869
0
        relocation -= elf_gp (output_bfd);
5870
0
        if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5871
0
          {
5872
      /* TLS model shall be converted.  */
5873
0
      BFD_ASSERT(0);
5874
0
          }
5875
0
      }
5876
0
    else if (eff_tls_type & GOT_TLS_IEGP)
5877
0
      {
5878
0
        relocation -= elf_gp (output_bfd);
5879
0
      }
5880
0
        }
5881
0
      else
5882
0
        {
5883
0
    if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5884
0
      {
5885
        /* TLS model workaround shall be applied.  */
5886
0
        BFD_ASSERT(0);
5887
0
      }
5888
0
    else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5889
0
      {
5890
0
        if (eff_tls_type & GOT_TLS_IEGP)
5891
0
          relocation -= elf_gp(output_bfd);
5892
5893
0
        if (need_relocs)
5894
0
          {
5895
0
      if (indx == 0)
5896
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5897
0
      else
5898
0
        outrel.r_addend = 0;
5899
0
      outrel.r_offset = (sgot->output_section->vma
5900
0
             + sgot->output_offset + off);
5901
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5902
5903
0
      elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5904
0
              &outrel);
5905
0
          }
5906
0
        else
5907
0
          {
5908
0
      bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5909
0
            sgot->contents + off);
5910
0
          }
5911
0
      }
5912
0
    else if (eff_tls_type & GOT_TLS_DESC)
5913
0
      {
5914
0
        relocation -= elf_gp (output_bfd);
5915
0
        if (need_relocs)
5916
0
          {
5917
0
      if (indx == 0)
5918
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5919
0
      else
5920
0
        outrel.r_addend = 0;
5921
0
      outrel.r_offset = (sgot->output_section->vma
5922
0
             + sgot->output_offset + off);
5923
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5924
5925
0
      if (htab->tls_desc_trampoline)
5926
0
        {
5927
0
          asection *srelplt;
5928
0
          srelplt = ehtab->srelplt;
5929
0
          loc = srelplt->contents;
5930
0
          loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5931
0
          BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5932
0
          <= srelplt->contents + srelplt->size);
5933
5934
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5935
0
        }
5936
0
      else
5937
0
        {
5938
0
          loc = srelgot->contents;
5939
0
          loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5940
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5941
0
          ++srelgot->reloc_count;
5942
0
        }
5943
0
          }
5944
0
        else
5945
0
          {
5946
      /* feed me!  */
5947
0
      bfd_put_32 (output_bfd, 0xdeadbeef,
5948
0
            sgot->contents + off);
5949
0
      bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5950
0
            sgot->contents + off + 4);
5951
0
      patch_tls_desc_to_ie (contents, rel, input_bfd);
5952
0
      BFD_ASSERT(0);
5953
0
          }
5954
0
      }
5955
0
    else
5956
0
      {
5957
        /* TLS model workaround shall be applied.  */
5958
0
        BFD_ASSERT(0);
5959
0
      }
5960
5961
0
    if (h != NULL)
5962
0
      h->got.offset |= 1;
5963
0
    else
5964
0
      local_got_offsets[r_symndx] |= 1;
5965
0
        }
5966
0
    }
5967
0
  break;
5968
    /* DON'T fall through.  */
5969
5970
0
  default:
5971
    /* OLD_NDS32_RELOC.  */
5972
5973
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5974
0
          contents, offset, relocation, addend);
5975
0
    goto check_reloc;
5976
0
  }
5977
5978
0
      switch ((int) r_type)
5979
0
  {
5980
0
  case R_NDS32_20_RELA:
5981
0
  case R_NDS32_5_RELA:
5982
0
  case R_NDS32_9_PCREL_RELA:
5983
0
  case R_NDS32_WORD_9_PCREL_RELA:
5984
0
  case R_NDS32_10_UPCREL_RELA:
5985
0
  case R_NDS32_15_PCREL_RELA:
5986
0
  case R_NDS32_17_PCREL_RELA:
5987
0
  case R_NDS32_25_PCREL_RELA:
5988
0
  case R_NDS32_25_ABS_RELA:
5989
0
  case R_NDS32_HI20_RELA:
5990
0
  case R_NDS32_LO12S3_RELA:
5991
0
  case R_NDS32_LO12S2_RELA:
5992
0
  case R_NDS32_LO12S2_DP_RELA:
5993
0
  case R_NDS32_LO12S2_SP_RELA:
5994
0
  case R_NDS32_LO12S1_RELA:
5995
0
  case R_NDS32_LO12S0_RELA:
5996
0
  case R_NDS32_LO12S0_ORI_RELA:
5997
0
  case R_NDS32_SDA16S3_RELA:
5998
0
  case R_NDS32_SDA17S2_RELA:
5999
0
  case R_NDS32_SDA18S1_RELA:
6000
0
  case R_NDS32_SDA19S0_RELA:
6001
0
  case R_NDS32_SDA15S3_RELA:
6002
0
  case R_NDS32_SDA15S2_RELA:
6003
0
  case R_NDS32_SDA12S2_DP_RELA:
6004
0
  case R_NDS32_SDA12S2_SP_RELA:
6005
0
  case R_NDS32_SDA15S1_RELA:
6006
0
  case R_NDS32_SDA15S0_RELA:
6007
0
  case R_NDS32_SDA_FP7U2_RELA:
6008
0
  case R_NDS32_9_PLTREL:
6009
0
  case R_NDS32_25_PLTREL:
6010
0
  case R_NDS32_GOT20:
6011
0
  case R_NDS32_GOT_HI20:
6012
0
  case R_NDS32_GOT_LO12:
6013
0
  case R_NDS32_GOT_LO15:
6014
0
  case R_NDS32_GOT_LO19:
6015
0
  case R_NDS32_GOT15S2_RELA:
6016
0
  case R_NDS32_GOT17S2_RELA:
6017
0
  case R_NDS32_GOTPC20:
6018
0
  case R_NDS32_GOTPC_HI20:
6019
0
  case R_NDS32_GOTPC_LO12:
6020
0
  case R_NDS32_GOTOFF:
6021
0
  case R_NDS32_GOTOFF_HI20:
6022
0
  case R_NDS32_GOTOFF_LO12:
6023
0
  case R_NDS32_GOTOFF_LO15:
6024
0
  case R_NDS32_GOTOFF_LO19:
6025
0
  case R_NDS32_PLTREL_HI20:
6026
0
  case R_NDS32_PLTREL_LO12:
6027
0
  case R_NDS32_PLT_GOTREL_HI20:
6028
0
  case R_NDS32_PLT_GOTREL_LO12:
6029
0
  case R_NDS32_PLT_GOTREL_LO15:
6030
0
  case R_NDS32_PLT_GOTREL_LO19:
6031
0
  case R_NDS32_PLT_GOTREL_LO20:
6032
0
  case R_NDS32_17IFC_PCREL_RELA:
6033
0
  case R_NDS32_10IFCU_PCREL_RELA:
6034
0
  case R_NDS32_TLS_LE_HI20:
6035
0
  case R_NDS32_TLS_LE_LO12:
6036
0
  case R_NDS32_TLS_IE_HI20:
6037
0
  case R_NDS32_TLS_IE_LO12S2:
6038
0
  case R_NDS32_TLS_LE_20:
6039
0
  case R_NDS32_TLS_LE_15S0:
6040
0
  case R_NDS32_TLS_LE_15S1:
6041
0
  case R_NDS32_TLS_LE_15S2:
6042
0
  case R_NDS32_TLS_DESC_HI20:
6043
0
  case R_NDS32_TLS_DESC_LO12:
6044
0
  case R_NDS32_TLS_IE_LO12:
6045
0
  case R_NDS32_TLS_IEGP_HI20:
6046
0
  case R_NDS32_TLS_IEGP_LO12:
6047
0
  case R_NDS32_TLS_IEGP_LO12S2:
6048
    /* Instruction related relocs must handle endian properly.  */
6049
    /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
6050
0
    r = nds32_elf_final_link_relocate (howto, input_bfd,
6051
0
               input_section, contents,
6052
0
               rel->r_offset, relocation,
6053
0
               rel->r_addend);
6054
0
    break;
6055
6056
0
  default:
6057
    /* All other relocs can use default handler.  */
6058
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6059
0
          contents, rel->r_offset,
6060
0
          relocation, rel->r_addend);
6061
0
    break;
6062
0
  }
6063
6064
0
    check_reloc:
6065
6066
0
      if (r != bfd_reloc_ok)
6067
0
  {
6068
    /* FIXME: This should be generic enough to go in a utility.  */
6069
0
    const char *name;
6070
6071
0
    if (h != NULL)
6072
0
      name = h->root.root.string;
6073
0
    else
6074
0
      {
6075
0
        name = bfd_elf_string_from_elf_section
6076
0
          (input_bfd, symtab_hdr->sh_link, sym->st_name);
6077
0
        if (name == NULL || *name == '\0')
6078
0
    name = bfd_section_name (sec);
6079
0
      }
6080
6081
0
    if (errmsg != NULL)
6082
0
      goto common_error;
6083
6084
0
    switch (r)
6085
0
      {
6086
0
      case bfd_reloc_overflow:
6087
0
        (*info->callbacks->reloc_overflow)
6088
0
    (info, (h ? &h->root : NULL), name, howto->name,
6089
0
     (bfd_vma) 0, input_bfd, input_section, offset);
6090
0
        break;
6091
6092
0
      case bfd_reloc_undefined:
6093
0
        (*info->callbacks->undefined_symbol)
6094
0
    (info, name, input_bfd, input_section, offset, true);
6095
0
        break;
6096
6097
0
      case bfd_reloc_outofrange:
6098
0
        errmsg = _("internal error: out of range error");
6099
0
        goto common_error;
6100
6101
0
      case bfd_reloc_notsupported:
6102
0
        errmsg = _("internal error: unsupported relocation error");
6103
0
        goto common_error;
6104
6105
0
      case bfd_reloc_dangerous:
6106
0
        errmsg = _("internal error: dangerous error");
6107
0
        goto common_error;
6108
6109
0
      default:
6110
0
        errmsg = _("internal error: unknown error");
6111
        /* Fall through.  */
6112
6113
0
      common_error:
6114
0
        (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6115
0
             input_section, offset);
6116
0
        break;
6117
0
      }
6118
0
  }
6119
0
    }
6120
6121
  /* Resotre header size to avoid overflow load.  */
6122
0
  if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6123
0
    symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6124
6125
0
  return ret;
6126
0
}
6127
6128
/* Finish up dynamic symbol handling.  We set the contents of various
6129
   dynamic sections here.  */
6130
6131
static bool
6132
nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6133
         struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6134
0
{
6135
0
  struct elf_link_hash_table *ehtab;
6136
0
  struct elf_nds32_link_hash_entry *hent;
6137
0
  bfd_byte *loc;
6138
6139
0
  ehtab = elf_hash_table (info);
6140
0
  hent = (struct elf_nds32_link_hash_entry *) h;
6141
6142
0
  if (h->plt.offset != (bfd_vma) - 1)
6143
0
    {
6144
0
      asection *splt;
6145
0
      asection *sgot;
6146
0
      asection *srela;
6147
6148
0
      bfd_vma plt_index;
6149
0
      bfd_vma got_offset;
6150
0
      bfd_vma local_plt_offset;
6151
0
      Elf_Internal_Rela rela;
6152
6153
      /* This symbol has an entry in the procedure linkage table.  Set
6154
   it up.  */
6155
6156
0
      BFD_ASSERT (h->dynindx != -1);
6157
6158
0
      splt = ehtab->splt;
6159
0
      sgot = ehtab->sgotplt;
6160
0
      srela = ehtab->srelplt;
6161
0
      BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6162
6163
      /* Get the index in the procedure linkage table which
6164
   corresponds to this symbol.  This is the index of this symbol
6165
   in all the symbols for which we are making plt entries.  The
6166
   first entry in the procedure linkage table is reserved.  */
6167
0
      plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6168
6169
      /* Get the offset into the .got table of the entry that
6170
   corresponds to this function.  Each .got entry is 4 bytes.
6171
   The first three are reserved.  */
6172
0
      got_offset = (plt_index + 3) * 4;
6173
6174
      /* Fill in the entry in the procedure linkage table.  */
6175
0
      if (!bfd_link_pic (info))
6176
0
  {
6177
0
    unsigned long insn;
6178
6179
0
    insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6180
0
              + sgot->output_offset + got_offset) >> 12)
6181
0
            & 0xfffff);
6182
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6183
6184
0
    insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6185
0
              + sgot->output_offset + got_offset) & 0x0fff)
6186
0
            >> 2);
6187
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6188
6189
0
    insn = PLT_ENTRY_WORD2;
6190
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6191
6192
0
    insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6193
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6194
6195
0
    insn = PLT_ENTRY_WORD4
6196
0
     + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6197
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6198
0
    local_plt_offset = 12;
6199
0
  }
6200
0
      else
6201
0
  {
6202
    /* sda_base must be set at this time.  */
6203
0
    unsigned long insn;
6204
0
    long offset;
6205
6206
0
    offset = sgot->output_section->vma + sgot->output_offset + got_offset
6207
0
       - elf_gp (output_bfd);
6208
0
    insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6209
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6210
6211
0
    insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6212
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6213
6214
0
    insn = PLT_PIC_ENTRY_WORD2;
6215
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6216
6217
0
    insn = PLT_PIC_ENTRY_WORD3;
6218
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6219
6220
0
    insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6221
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6222
6223
0
    insn = PLT_PIC_ENTRY_WORD5
6224
0
      + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6225
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6226
6227
0
    local_plt_offset = 16;
6228
0
  }
6229
6230
      /* Fill in the entry in the global offset table,
6231
   so it will fall through to the next instruction for the first time.  */
6232
0
      bfd_put_32 (output_bfd,
6233
0
      (splt->output_section->vma + splt->output_offset
6234
0
       + h->plt.offset + local_plt_offset),
6235
0
      sgot->contents + got_offset);
6236
6237
      /* Fill in the entry in the .rela.plt section.  */
6238
0
      rela.r_offset = (sgot->output_section->vma
6239
0
           + sgot->output_offset + got_offset);
6240
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6241
0
      rela.r_addend = 0;
6242
0
      loc = srela->contents;
6243
0
      loc += plt_index * sizeof (Elf32_External_Rela);
6244
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6245
6246
0
      if (!h->def_regular)
6247
0
  {
6248
    /* Mark the symbol as undefined, rather than as defined in
6249
       the .plt section.  Leave the value alone.  */
6250
0
    sym->st_shndx = SHN_UNDEF;
6251
0
    if (!h->ref_regular_nonweak)
6252
0
      sym->st_value = 0;
6253
0
  }
6254
0
    }
6255
6256
0
  if (h->got.offset != (bfd_vma) - 1
6257
0
      && hent->tls_type == GOT_NORMAL)
6258
0
    {
6259
0
      asection *sgot;
6260
0
      asection *srelagot;
6261
0
      Elf_Internal_Rela rela;
6262
6263
      /* This symbol has an entry in the global offset table.
6264
   Set it up.  */
6265
6266
0
      sgot = ehtab->sgot;
6267
0
      srelagot = ehtab->srelgot;
6268
0
      BFD_ASSERT (sgot != NULL && srelagot != NULL);
6269
6270
0
      rela.r_offset = (sgot->output_section->vma
6271
0
           + sgot->output_offset + (h->got.offset & ~1));
6272
6273
      /* If this is a -Bsymbolic link, and the symbol is defined
6274
   locally, we just want to emit a RELATIVE reloc.  Likewise if
6275
   the symbol was forced to be local because of a version file.
6276
   The entry in the global offset table will already have been
6277
   initialized in the relocate_section function.  */
6278
0
      if ((bfd_link_pic (info)
6279
0
     && (info->symbolic || h->dynindx == -1 || h->forced_local)
6280
0
     && h->def_regular)
6281
0
    || (bfd_link_pie (info) && h->def_regular))
6282
0
  {
6283
0
    rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6284
0
    rela.r_addend = (h->root.u.def.value
6285
0
         + h->root.u.def.section->output_section->vma
6286
0
         + h->root.u.def.section->output_offset);
6287
6288
0
    if ((h->got.offset & 1) == 0)
6289
0
      {
6290
0
        bfd_put_32 (output_bfd, rela.r_addend,
6291
0
        sgot->contents + h->got.offset);
6292
0
      }
6293
0
  }
6294
0
      else
6295
0
  {
6296
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
6297
0
    bfd_put_32 (output_bfd, (bfd_vma) 0,
6298
0
          sgot->contents + h->got.offset);
6299
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6300
0
    rela.r_addend = 0;
6301
0
  }
6302
6303
0
      loc = srelagot->contents;
6304
0
      loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6305
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6306
0
      ++srelagot->reloc_count;
6307
0
      BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6308
0
    }
6309
6310
0
  if (h->needs_copy)
6311
0
    {
6312
0
      asection *s;
6313
0
      Elf_Internal_Rela rela;
6314
6315
      /* This symbols needs a copy reloc.  Set it up.  */
6316
6317
0
      BFD_ASSERT (h->dynindx != -1
6318
0
      && (h->root.type == bfd_link_hash_defined
6319
0
          || h->root.type == bfd_link_hash_defweak));
6320
6321
0
      s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6322
0
      BFD_ASSERT (s != NULL);
6323
6324
0
      rela.r_offset = (h->root.u.def.value
6325
0
           + h->root.u.def.section->output_section->vma
6326
0
           + h->root.u.def.section->output_offset);
6327
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6328
0
      rela.r_addend = 0;
6329
0
      loc = s->contents;
6330
0
      loc += s->reloc_count * sizeof (Elf32_External_Rela);
6331
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6332
0
      ++s->reloc_count;
6333
0
    }
6334
6335
  /* Mark some specially defined symbols as absolute.  */
6336
0
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6337
0
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6338
0
    sym->st_shndx = SHN_ABS;
6339
6340
0
  return true;
6341
0
}
6342
6343
6344
/* Finish up the dynamic sections.  */
6345
6346
static bool
6347
nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info,
6348
           bfd_byte *buf ATTRIBUTE_UNUSED)
6349
0
{
6350
0
  bfd *dynobj;
6351
0
  asection *sdyn;
6352
0
  asection *sgotplt;
6353
0
  struct elf_link_hash_table *ehtab;
6354
0
  struct elf_nds32_link_hash_table *htab;
6355
6356
0
  ehtab = elf_hash_table (info);
6357
0
  htab = nds32_elf_hash_table (info);
6358
0
  if (htab == NULL)
6359
0
    return false;
6360
6361
0
  dynobj = elf_hash_table (info)->dynobj;
6362
6363
0
  sgotplt = ehtab->sgotplt;
6364
  /* A broken linker script might have discarded the dynamic sections.
6365
     Catch this here so that we do not seg-fault later on.  */
6366
0
  if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6367
0
    return false;
6368
0
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6369
6370
0
  if (elf_hash_table (info)->dynamic_sections_created)
6371
0
    {
6372
0
      asection *splt;
6373
0
      Elf32_External_Dyn *dyncon, *dynconend;
6374
6375
0
      BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6376
6377
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
6378
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6379
6380
0
      for (; dyncon < dynconend; dyncon++)
6381
0
  {
6382
0
    Elf_Internal_Dyn dyn;
6383
0
    asection *s;
6384
6385
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6386
6387
0
    switch (dyn.d_tag)
6388
0
      {
6389
0
      default:
6390
0
        break;
6391
6392
0
      case DT_PLTGOT:
6393
        /* name = ".got";  */
6394
0
        s = ehtab->sgot;
6395
0
        goto get_vma;
6396
0
      case DT_JMPREL:
6397
0
        s = ehtab->srelplt;
6398
0
      get_vma:
6399
0
        BFD_ASSERT (s != NULL);
6400
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6401
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6402
0
        break;
6403
6404
0
      case DT_PLTRELSZ:
6405
0
        s = ehtab->srelplt;
6406
0
        BFD_ASSERT (s != NULL);
6407
0
        dyn.d_un.d_val = s->size;
6408
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6409
0
        break;
6410
6411
0
      case DT_RELASZ:
6412
        /* My reading of the SVR4 ABI indicates that the
6413
     procedure linkage table relocs (DT_JMPREL) should be
6414
     included in the overall relocs (DT_RELA).  This is
6415
     what Solaris does.  However, UnixWare can not handle
6416
     that case.  Therefore, we override the DT_RELASZ entry
6417
     here to make it not include the JMPREL relocs.  Since
6418
     the linker script arranges for .rela.plt to follow all
6419
     other relocation sections, we don't have to worry
6420
     about changing the DT_RELA entry.  */
6421
0
        if (ehtab->srelplt != NULL)
6422
0
    {
6423
0
      s = ehtab->srelplt;
6424
0
      dyn.d_un.d_val -= s->size;
6425
0
    }
6426
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6427
0
        break;
6428
6429
0
      case DT_TLSDESC_PLT:
6430
0
        s = htab->root.splt;
6431
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6432
0
        + htab->root.tlsdesc_plt);
6433
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6434
0
        break;
6435
6436
0
      case DT_TLSDESC_GOT:
6437
0
        s = htab->root.sgot;
6438
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6439
0
        + htab->root.tlsdesc_got);
6440
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6441
0
        break;
6442
0
      }
6443
0
  }
6444
6445
      /* Fill in the first entry in the procedure linkage table.  */
6446
0
      splt = ehtab->splt;
6447
0
      if (splt && splt->size > 0)
6448
0
  {
6449
0
    if (bfd_link_pic (info))
6450
0
      {
6451
0
        unsigned long insn;
6452
0
        long offset;
6453
6454
0
        offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6455
0
    - elf_gp (output_bfd);
6456
0
        insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6457
0
        bfd_putb32 (insn, splt->contents);
6458
6459
        /* here has a typo?  */
6460
0
        insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6461
0
        bfd_putb32 (insn, splt->contents + 4);
6462
6463
0
        insn = PLT0_PIC_ENTRY_WORD2;
6464
0
        bfd_putb32 (insn, splt->contents + 8);
6465
6466
0
        insn = PLT0_PIC_ENTRY_WORD3;
6467
0
        bfd_putb32 (insn, splt->contents + 12);
6468
6469
0
        insn = PLT0_PIC_ENTRY_WORD4;
6470
0
        bfd_putb32 (insn, splt->contents + 16);
6471
6472
0
        insn = PLT0_PIC_ENTRY_WORD5;
6473
0
        bfd_putb32 (insn, splt->contents + 20);
6474
0
      }
6475
0
    else
6476
0
      {
6477
0
        unsigned long insn;
6478
0
        unsigned long addr;
6479
6480
        /* addr = .got + 4 */
6481
0
        addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6482
0
        insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6483
0
        bfd_putb32 (insn, splt->contents);
6484
6485
0
        insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6486
0
        bfd_putb32 (insn, splt->contents + 4);
6487
6488
0
        insn = PLT0_ENTRY_WORD2;
6489
0
        bfd_putb32 (insn, splt->contents + 8);
6490
6491
0
        insn = PLT0_ENTRY_WORD3;
6492
0
        bfd_putb32 (insn, splt->contents + 12);
6493
6494
0
        insn = PLT0_ENTRY_WORD4;
6495
0
        bfd_putb32 (insn, splt->contents + 16);
6496
0
      }
6497
6498
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6499
0
      PLT_ENTRY_SIZE;
6500
0
  }
6501
6502
0
      if (htab->root.tlsdesc_plt)
6503
0
  {
6504
    /* Calculate addresses.  */
6505
0
    asection *sgot = sgot = ehtab->sgot;
6506
0
    bfd_vma pltgot = sgotplt->output_section->vma
6507
0
      + sgotplt->output_offset;
6508
0
    bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6509
0
      + htab->root.tlsdesc_got;
6510
6511
    /* Get GP offset.  */
6512
0
    pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1]  */
6513
0
    tlsdesc_got -= elf_gp (output_bfd);
6514
6515
    /* Do relocation.  */
6516
0
    dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6517
0
    dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6518
0
    dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6519
0
    dl_tlsdesc_lazy_trampoline[5] +=  0xfff & pltgot;
6520
6521
    /* Insert .plt.  */
6522
0
    nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
6523
0
        dl_tlsdesc_lazy_trampoline,
6524
0
        ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6525
0
  }
6526
0
    }
6527
6528
  /* Fill in the first three entries in the global offset table.  */
6529
0
  if (sgotplt && sgotplt->size > 0)
6530
0
    {
6531
0
      if (sdyn == NULL)
6532
0
  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6533
0
      else
6534
0
  bfd_put_32 (output_bfd,
6535
0
        sdyn->output_section->vma + sdyn->output_offset,
6536
0
        sgotplt->contents);
6537
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6538
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6539
6540
0
      elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6541
0
    }
6542
6543
0
  return true;
6544
0
}
6545

6546
6547
/* Set the right machine number.  */
6548
6549
static bool
6550
nds32_elf_object_p (bfd *abfd)
6551
784
{
6552
784
  static unsigned int cur_arch = 0;
6553
6554
784
  if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6555
761
    {
6556
      /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
6557
761
      cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6558
761
    }
6559
6560
784
  switch (cur_arch)
6561
784
    {
6562
711
    default:
6563
711
    case E_N1_ARCH:
6564
711
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6565
711
      break;
6566
0
    case E_N1H_ARCH:
6567
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6568
0
      break;
6569
57
    case E_NDS_ARCH_STAR_V2_0:
6570
57
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6571
57
      break;
6572
16
    case E_NDS_ARCH_STAR_V3_0:
6573
16
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6574
16
      break;
6575
0
    case E_NDS_ARCH_STAR_V3_M:
6576
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6577
0
      break;
6578
784
    }
6579
6580
784
  return true;
6581
784
}
6582
6583
/* Store the machine number in the flags field.  */
6584
6585
static bool
6586
nds32_elf_final_write_processing (bfd *abfd)
6587
4
{
6588
4
  unsigned long val;
6589
4
  static unsigned int cur_mach = 0;
6590
6591
4
  if (bfd_mach_n1 != bfd_get_mach (abfd))
6592
1
    {
6593
1
      cur_mach = bfd_get_mach (abfd);
6594
1
    }
6595
6596
4
  switch (cur_mach)
6597
4
    {
6598
0
    case bfd_mach_n1:
6599
      /* Only happen when object is empty, since the case is abandon.  */
6600
0
      val = E_N1_ARCH;
6601
0
      val |= E_NDS_ABI_AABI;
6602
0
      val |= E_NDS32_ELF_VER_1_4;
6603
0
      break;
6604
0
    case bfd_mach_n1h:
6605
0
      val = E_N1H_ARCH;
6606
0
      break;
6607
0
    case bfd_mach_n1h_v2:
6608
0
      val = E_NDS_ARCH_STAR_V2_0;
6609
0
      break;
6610
2
    case bfd_mach_n1h_v3:
6611
2
      val = E_NDS_ARCH_STAR_V3_0;
6612
2
      break;
6613
0
    case bfd_mach_n1h_v3m:
6614
0
      val = E_NDS_ARCH_STAR_V3_M;
6615
0
      break;
6616
2
    default:
6617
2
      val = 0;
6618
2
      break;
6619
4
    }
6620
6621
4
  elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6622
4
  elf_elfheader (abfd)->e_flags |= val;
6623
4
  return _bfd_elf_final_write_processing (abfd);
6624
4
}
6625
6626
/* Function to keep NDS32 specific file flags.  */
6627
6628
static bool
6629
nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6630
0
{
6631
0
  BFD_ASSERT (!elf_flags_init (abfd)
6632
0
        || elf_elfheader (abfd)->e_flags == flags);
6633
6634
0
  elf_elfheader (abfd)->e_flags = flags;
6635
0
  elf_flags_init (abfd) = true;
6636
0
  return true;
6637
0
}
6638
6639
static unsigned int
6640
convert_e_flags (unsigned int e_flags, unsigned int arch)
6641
0
{
6642
0
  if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6643
0
    {
6644
      /* From 0.9 to 1.0.  */
6645
0
      e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6646
6647
      /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6648
0
      e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6649
0
      if (arch == E_NDS_ARCH_STAR_V1_0)
6650
0
  {
6651
    /* Done.  */
6652
0
    return e_flags;
6653
0
  }
6654
0
    }
6655
6656
  /* From 1.0 to 2.0.  */
6657
0
  e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6658
6659
  /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
6660
0
  e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6661
6662
  /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6663
0
  e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6664
0
  return e_flags;
6665
0
}
6666
6667
static bool
6668
nds32_check_vec_size (bfd *ibfd)
6669
0
{
6670
0
  static unsigned int nds32_vec_size = 0;
6671
6672
0
  asection *sec_t = NULL;
6673
0
  bfd_byte *contents = NULL;
6674
6675
0
  sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6676
6677
0
  if (sec_t && sec_t->size >= 4)
6678
0
    {
6679
      /* Get vec_size in file.  */
6680
0
      unsigned int flag_t;
6681
6682
0
      nds32_get_section_contents (ibfd, sec_t, &contents, true);
6683
0
      flag_t = bfd_get_32 (ibfd, contents);
6684
6685
      /* The value could only be 4 or 16.  */
6686
6687
0
      if (!nds32_vec_size)
6688
  /* Set if not set yet.  */
6689
0
  nds32_vec_size = (flag_t & 0x3);
6690
0
      else if (nds32_vec_size != (flag_t & 0x3))
6691
0
  {
6692
0
    _bfd_error_handler
6693
      /* xgettext:c-format */
6694
0
      (_("%pB: ISR vector size mismatch"
6695
0
         " with previous modules, previous %u-byte, current %u-byte"),
6696
0
       ibfd,
6697
0
       nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6698
0
       (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6699
0
    return false;
6700
0
  }
6701
0
      else
6702
  /* Only keep the first vec_size section.  */
6703
0
  sec_t->flags |= SEC_EXCLUDE;
6704
0
    }
6705
6706
0
  return true;
6707
0
}
6708
6709
/* Merge backend specific data from an object file to the output
6710
   object file when linking.  */
6711
6712
static bool
6713
nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6714
0
{
6715
0
  bfd *obfd = info->output_bfd;
6716
0
  flagword out_flags;
6717
0
  flagword in_flags;
6718
0
  flagword out_16regs;
6719
0
  flagword in_no_mac;
6720
0
  flagword out_no_mac;
6721
0
  flagword in_16regs;
6722
0
  flagword out_version;
6723
0
  flagword in_version;
6724
0
  flagword out_fpu_config;
6725
0
  flagword in_fpu_config;
6726
6727
  /* FIXME: What should be checked when linking shared libraries?  */
6728
0
  if ((ibfd->flags & DYNAMIC) != 0)
6729
0
    return true;
6730
6731
  /* TODO: Revise to use object-attributes instead.  */
6732
0
  if (!nds32_check_vec_size (ibfd))
6733
0
    return false;
6734
6735
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
6736
0
    return true;
6737
6738
0
  if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6739
0
    {
6740
0
      _bfd_error_handler
6741
0
  (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6742
6743
0
      bfd_set_error (bfd_error_bad_value);
6744
0
      return false;
6745
0
    }
6746
6747
  /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6748
     treat as generic one without checking and merging.  */
6749
0
  if (elf_elfheader (ibfd)->e_flags)
6750
0
    {
6751
0
      in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6752
0
      if (in_version == E_NDS32_ELF_VER_1_2)
6753
0
  {
6754
0
    _bfd_error_handler
6755
0
      (_("%pB: warning: older version of object file encountered, "
6756
0
         "please recompile with current tool chain"), ibfd);
6757
0
  }
6758
6759
      /* We may need to merge V1 and V2 arch object files to V2.  */
6760
0
      if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6761
0
    != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6762
0
  {
6763
    /* Need to convert version.  */
6764
0
    if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6765
0
        == E_NDS_ARCH_STAR_RESERVED)
6766
0
      {
6767
0
        elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6768
0
      }
6769
0
    else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6770
0
       == E_NDS_ARCH_STAR_V3_M
6771
0
       && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6772
0
       == E_NDS_ARCH_STAR_V3_0)
6773
0
      {
6774
0
        elf_elfheader (ibfd)->e_flags =
6775
0
    (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6776
0
    | E_NDS_ARCH_STAR_V3_0;
6777
0
      }
6778
0
    else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6779
0
       == E_NDS_ARCH_STAR_V0_9
6780
0
       || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6781
0
       > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6782
0
      {
6783
0
        elf_elfheader (obfd)->e_flags =
6784
0
    convert_e_flags (elf_elfheader (obfd)->e_flags,
6785
0
         (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6786
0
      }
6787
0
    else
6788
0
      {
6789
0
        elf_elfheader (ibfd)->e_flags =
6790
0
    convert_e_flags (elf_elfheader (ibfd)->e_flags,
6791
0
         (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6792
0
      }
6793
0
  }
6794
6795
      /* Extract some flags.  */
6796
0
      in_flags = elf_elfheader (ibfd)->e_flags
6797
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6798
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6799
6800
      /* The following flags need special treatment.  */
6801
0
      in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6802
0
      in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6803
0
      in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6804
6805
      /* Extract some flags.  */
6806
0
      out_flags = elf_elfheader (obfd)->e_flags
6807
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6808
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6809
6810
      /* The following flags need special treatment.  */
6811
0
      out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6812
0
      out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6813
0
      out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6814
0
      out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6815
0
      if (!elf_flags_init (obfd))
6816
0
  {
6817
    /* If the input is the default architecture then do not
6818
       bother setting the flags for the output architecture,
6819
       instead allow future merges to do this.  If no future
6820
       merges ever set these flags then they will retain their
6821
       unitialised values, which surprise surprise, correspond
6822
       to the default values.  */
6823
0
    if (bfd_get_arch_info (ibfd)->the_default)
6824
0
      return true;
6825
6826
0
    elf_flags_init (obfd) = true;
6827
0
    elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6828
6829
0
    if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6830
0
        && bfd_get_arch_info (obfd)->the_default)
6831
0
      {
6832
0
        return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6833
0
          bfd_get_mach (ibfd));
6834
0
      }
6835
6836
0
    return true;
6837
0
  }
6838
6839
      /* Check flag compatibility.  */
6840
0
      if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6841
0
  {
6842
0
    _bfd_error_handler
6843
0
      (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6844
0
    bfd_set_error (bfd_error_bad_value);
6845
0
    return false;
6846
0
  }
6847
6848
0
      if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6849
0
  {
6850
0
    if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6851
0
      {
6852
0
        _bfd_error_handler
6853
0
    (_("%pB: error: instruction set mismatch with previous modules"),
6854
0
     ibfd);
6855
6856
0
        bfd_set_error (bfd_error_bad_value);
6857
0
        return false;
6858
0
      }
6859
0
  }
6860
6861
      /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6862
   and perf ext1 and DIV are mergerd to perf ext1.  */
6863
0
      if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6864
0
  {
6865
0
    elf_elfheader (obfd)->e_flags =
6866
0
      (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6867
0
      | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6868
0
      | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6869
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6870
0
      | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6871
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6872
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6873
0
      | ((in_version > out_version) ? out_version : in_version);
6874
0
  }
6875
0
      else
6876
0
  {
6877
0
    if (in_version != out_version)
6878
0
      _bfd_error_handler
6879
        /* xgettext:c-format */
6880
0
        (_("%pB: warning: incompatible elf-versions %s and %s"),
6881
0
         ibfd, nds32_elfver_strtab[out_version],
6882
0
         nds32_elfver_strtab[in_version]);
6883
6884
0
    elf_elfheader (obfd)->e_flags = in_flags | out_flags
6885
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6886
0
      | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6887
0
      | (in_version > out_version ?  out_version : in_version);
6888
0
  }
6889
0
    }
6890
6891
0
  return true;
6892
0
}
6893
6894
/* Display the flags field.  */
6895
6896
static bool
6897
nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6898
206
{
6899
206
  FILE *file = (FILE *) ptr;
6900
6901
206
  BFD_ASSERT (abfd != NULL && ptr != NULL);
6902
6903
206
  _bfd_elf_print_private_bfd_data (abfd, ptr);
6904
6905
206
  fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6906
6907
206
  switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6908
206
    {
6909
206
    default:
6910
206
    case E_N1_ARCH:
6911
206
      fprintf (file, _(": n1 instructions"));
6912
206
      break;
6913
0
    case E_N1H_ARCH:
6914
0
      fprintf (file, _(": n1h instructions"));
6915
0
      break;
6916
206
    }
6917
6918
206
  fputc ('\n', file);
6919
6920
206
  return true;
6921
206
}
6922
6923
static unsigned int
6924
nds32_elf_action_discarded (asection *sec)
6925
0
{
6926
6927
0
  if (startswith (sec->name, ".gcc_except_table"))
6928
0
    return 0;
6929
6930
0
  return _bfd_elf_default_action_discarded (sec);
6931
0
}
6932
6933
static asection *
6934
nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6935
      struct elf_reloc_cookie *cookie,
6936
      struct elf_link_hash_entry *h,
6937
      unsigned int symndx)
6938
0
{
6939
0
  if (h != NULL)
6940
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
6941
0
      {
6942
0
      case R_NDS32_GNU_VTINHERIT:
6943
0
      case R_NDS32_GNU_VTENTRY:
6944
0
      case R_NDS32_RELA_GNU_VTINHERIT:
6945
0
      case R_NDS32_RELA_GNU_VTENTRY:
6946
0
  return NULL;
6947
0
      }
6948
6949
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
6950
0
}
6951
6952
static enum elf_nds32_tls_type
6953
get_tls_type (enum elf_nds32_reloc_type r_type,
6954
        struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6955
0
{
6956
0
  enum elf_nds32_tls_type tls_type;
6957
6958
0
  switch (r_type)
6959
0
    {
6960
0
    case R_NDS32_TLS_LE_HI20:
6961
0
    case R_NDS32_TLS_LE_LO12:
6962
0
      tls_type = GOT_TLS_LE;
6963
0
      break;
6964
0
    case R_NDS32_TLS_IE_HI20:
6965
0
    case R_NDS32_TLS_IE_LO12S2:
6966
0
    case R_NDS32_TLS_IE_LO12:
6967
0
      tls_type = GOT_TLS_IE;
6968
0
      break;
6969
0
    case R_NDS32_TLS_IEGP_HI20:
6970
0
    case R_NDS32_TLS_IEGP_LO12:
6971
0
    case R_NDS32_TLS_IEGP_LO12S2:
6972
0
      tls_type = GOT_TLS_IEGP;
6973
0
      break;
6974
0
    case R_NDS32_TLS_DESC_HI20:
6975
0
    case R_NDS32_TLS_DESC_LO12:
6976
0
    case R_NDS32_TLS_DESC_ADD:
6977
0
    case R_NDS32_TLS_DESC_FUNC:
6978
0
    case R_NDS32_TLS_DESC_CALL:
6979
0
      tls_type = GOT_TLS_DESC;
6980
0
      break;
6981
0
    default:
6982
0
      tls_type = GOT_NORMAL;
6983
0
      break;
6984
0
    }
6985
6986
0
  return tls_type;
6987
0
}
6988
6989
/* Ensure that we have allocated bookkeeping structures for ABFD's local
6990
   symbols.  */
6991
6992
static bool
6993
elf32_nds32_allocate_local_sym_info (bfd *abfd)
6994
0
{
6995
0
  if (elf_local_got_refcounts (abfd) == NULL)
6996
0
    {
6997
0
      bfd_size_type num_syms;
6998
0
      bfd_size_type size;
6999
0
      char *data;
7000
7001
0
      num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
7002
      /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7003
   gp_offset.  The details can refer to struct elf_nds32_obj_tdata.  */
7004
0
      size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7005
0
       + sizeof (bfd_vma) + sizeof (int)
7006
0
       + sizeof (bool) + sizeof (bfd_vma));
7007
0
      data = bfd_zalloc (abfd, size);
7008
0
      if (data == NULL)
7009
0
  return false;
7010
7011
0
      elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7012
0
      data += num_syms * sizeof (bfd_signed_vma);
7013
7014
0
      elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7015
0
      data += num_syms * sizeof (char);
7016
7017
0
      elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7018
0
      data += num_syms * sizeof (bfd_vma);
7019
7020
0
      elf32_nds32_local_gp_offset (abfd) = (int *) data;
7021
0
      data += num_syms * sizeof (int);
7022
0
    }
7023
7024
0
  return true;
7025
0
}
7026
7027
/* Look through the relocs for a section during the first phase.
7028
   Since we don't do .gots or .plts, we just need to consider the
7029
   virtual table relocs for gc.  */
7030
7031
static bool
7032
nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7033
      asection *sec, const Elf_Internal_Rela *relocs)
7034
0
{
7035
0
  Elf_Internal_Shdr *symtab_hdr;
7036
0
  struct elf_link_hash_entry **sym_hashes;
7037
0
  const Elf_Internal_Rela *rel;
7038
0
  const Elf_Internal_Rela *rel_end;
7039
0
  struct elf_link_hash_table *ehtab;
7040
0
  struct elf_nds32_link_hash_table *htab;
7041
0
  bfd *dynobj;
7042
0
  asection *sreloc = NULL;
7043
7044
  /* No need for relocation if relocatable already.  */
7045
0
  if (bfd_link_relocatable (info))
7046
0
    {
7047
0
      elf32_nds32_check_relax_group (abfd, sec);
7048
0
      return true;
7049
0
    }
7050
7051
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7052
0
  sym_hashes = elf_sym_hashes (abfd);
7053
7054
0
  ehtab = elf_hash_table (info);
7055
0
  htab = nds32_elf_hash_table (info);
7056
0
  dynobj = htab->root.dynobj;
7057
7058
0
  rel_end = relocs + sec->reloc_count;
7059
0
  for (rel = relocs; rel < rel_end; rel++)
7060
0
    {
7061
0
      enum elf_nds32_reloc_type r_type;
7062
0
      struct elf_link_hash_entry *h;
7063
0
      unsigned long r_symndx;
7064
0
      enum elf_nds32_tls_type tls_type, old_tls_type;
7065
7066
0
      r_symndx = ELF32_R_SYM (rel->r_info);
7067
0
      r_type = ELF32_R_TYPE (rel->r_info);
7068
0
      if (r_symndx < symtab_hdr->sh_info)
7069
0
  h = NULL;
7070
0
      else
7071
0
  {
7072
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7073
0
    while (h->root.type == bfd_link_hash_indirect
7074
0
     || h->root.type == bfd_link_hash_warning)
7075
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
7076
0
  }
7077
7078
      /* Create .got section if necessary.
7079
   Some relocs require a global offset table.  We create
7080
   got section here, since these relocation need a got section
7081
   and if it is not created yet.  */
7082
0
      if (ehtab->sgot == NULL)
7083
0
  {
7084
0
    switch (r_type)
7085
0
      {
7086
0
      case R_NDS32_GOT_HI20:
7087
0
      case R_NDS32_GOT_LO12:
7088
0
      case R_NDS32_GOT_LO15:
7089
0
      case R_NDS32_GOT_LO19:
7090
0
      case R_NDS32_GOT17S2_RELA:
7091
0
      case R_NDS32_GOT15S2_RELA:
7092
0
      case R_NDS32_GOTOFF:
7093
0
      case R_NDS32_GOTOFF_HI20:
7094
0
      case R_NDS32_GOTOFF_LO12:
7095
0
      case R_NDS32_GOTOFF_LO15:
7096
0
      case R_NDS32_GOTOFF_LO19:
7097
0
      case R_NDS32_GOTPC20:
7098
0
      case R_NDS32_GOTPC_HI20:
7099
0
      case R_NDS32_GOTPC_LO12:
7100
0
      case R_NDS32_GOT20:
7101
0
      case R_NDS32_TLS_IE_HI20:
7102
0
      case R_NDS32_TLS_IE_LO12:
7103
0
      case R_NDS32_TLS_IE_LO12S2:
7104
0
      case R_NDS32_TLS_IEGP_HI20:
7105
0
      case R_NDS32_TLS_IEGP_LO12:
7106
0
      case R_NDS32_TLS_IEGP_LO12S2:
7107
0
      case R_NDS32_TLS_DESC_HI20:
7108
0
      case R_NDS32_TLS_DESC_LO12:
7109
0
        if (dynobj == NULL)
7110
0
    htab->root.dynobj = dynobj = abfd;
7111
0
        if (!create_got_section (dynobj, info))
7112
0
    return false;
7113
0
        break;
7114
7115
0
      default:
7116
0
        break;
7117
0
      }
7118
0
  }
7119
7120
      /* Check relocation type.  */
7121
0
      switch ((int) r_type)
7122
0
  {
7123
0
  case R_NDS32_GOT_HI20:
7124
0
  case R_NDS32_GOT_LO12:
7125
0
  case R_NDS32_GOT_LO15:
7126
0
  case R_NDS32_GOT_LO19:
7127
0
  case R_NDS32_GOT20:
7128
0
  case R_NDS32_TLS_LE_HI20:
7129
0
  case R_NDS32_TLS_LE_LO12:
7130
0
  case R_NDS32_TLS_IE_HI20:
7131
0
  case R_NDS32_TLS_IE_LO12:
7132
0
  case R_NDS32_TLS_IE_LO12S2:
7133
0
  case R_NDS32_TLS_IEGP_HI20:
7134
0
  case R_NDS32_TLS_IEGP_LO12:
7135
0
  case R_NDS32_TLS_IEGP_LO12S2:
7136
0
  case R_NDS32_TLS_DESC_HI20:
7137
0
  case R_NDS32_TLS_DESC_LO12:
7138
0
    tls_type = get_tls_type (r_type, h);
7139
0
    if (h)
7140
0
      {
7141
0
        if (tls_type != GOT_TLS_LE)
7142
0
    h->got.refcount += 1;
7143
0
        old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7144
0
      }
7145
0
    else
7146
0
      {
7147
        /* This is a global offset table entry for a local symbol.  */
7148
0
        if (!elf32_nds32_allocate_local_sym_info (abfd))
7149
0
    return false;
7150
7151
0
        BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7152
0
        if (tls_type != GOT_TLS_LE)
7153
0
    elf_local_got_refcounts (abfd)[r_symndx] += 1;
7154
0
        old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7155
0
      }
7156
7157
    /* We would already have issued an error message if there
7158
       is a TLS/non-TLS mismatch, based on the symbol
7159
       type.  So just combine any TLS types needed.  */
7160
0
    if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7161
0
        && tls_type != GOT_NORMAL)
7162
0
      tls_type |= old_tls_type;
7163
7164
    /* DESC to IE/IEGP if link to executable.  */
7165
0
    if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7166
0
        && (bfd_link_executable (info)))
7167
0
      tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7168
7169
0
    if (old_tls_type != tls_type)
7170
0
      {
7171
0
        if (h != NULL)
7172
0
    elf32_nds32_hash_entry (h)->tls_type = tls_type;
7173
0
        else
7174
0
    elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7175
0
      }
7176
0
    break;
7177
0
  case R_NDS32_9_PLTREL:
7178
0
  case R_NDS32_25_PLTREL:
7179
0
  case R_NDS32_PLTREL_HI20:
7180
0
  case R_NDS32_PLTREL_LO12:
7181
0
  case R_NDS32_PLT_GOTREL_HI20:
7182
0
  case R_NDS32_PLT_GOTREL_LO12:
7183
0
  case R_NDS32_PLT_GOTREL_LO15:
7184
0
  case R_NDS32_PLT_GOTREL_LO19:
7185
0
  case R_NDS32_PLT_GOTREL_LO20:
7186
7187
    /* This symbol requires a procedure linkage table entry.  We
7188
       actually build the entry in adjust_dynamic_symbol,
7189
       because this might be a case of linking PIC code without
7190
       linking in any dynamic objects, in which case we don't
7191
       need to generate a procedure linkage table after all.  */
7192
7193
    /* If this is a local symbol, we resolve it directly without
7194
       creating a procedure linkage table entry.  */
7195
0
    if (h == NULL)
7196
0
      continue;
7197
7198
0
    if (h->forced_local
7199
0
        || (bfd_link_pie (info) && h->def_regular))
7200
0
      break;
7201
7202
0
    elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7203
0
    h->needs_plt = 1;
7204
0
    h->plt.refcount += 1;
7205
0
    break;
7206
7207
0
  case R_NDS32_16_RELA:
7208
0
  case R_NDS32_20_RELA:
7209
0
  case R_NDS32_5_RELA:
7210
0
  case R_NDS32_32_RELA:
7211
0
  case R_NDS32_HI20_RELA:
7212
0
  case R_NDS32_LO12S3_RELA:
7213
0
  case R_NDS32_LO12S2_RELA:
7214
0
  case R_NDS32_LO12S2_DP_RELA:
7215
0
  case R_NDS32_LO12S2_SP_RELA:
7216
0
  case R_NDS32_LO12S1_RELA:
7217
0
  case R_NDS32_LO12S0_RELA:
7218
0
  case R_NDS32_LO12S0_ORI_RELA:
7219
0
  case R_NDS32_SDA16S3_RELA:
7220
0
  case R_NDS32_SDA17S2_RELA:
7221
0
  case R_NDS32_SDA18S1_RELA:
7222
0
  case R_NDS32_SDA19S0_RELA:
7223
0
  case R_NDS32_SDA15S3_RELA:
7224
0
  case R_NDS32_SDA15S2_RELA:
7225
0
  case R_NDS32_SDA12S2_DP_RELA:
7226
0
  case R_NDS32_SDA12S2_SP_RELA:
7227
0
  case R_NDS32_SDA15S1_RELA:
7228
0
  case R_NDS32_SDA15S0_RELA:
7229
0
  case R_NDS32_SDA_FP7U2_RELA:
7230
0
  case R_NDS32_15_PCREL_RELA:
7231
0
  case R_NDS32_17_PCREL_RELA:
7232
0
  case R_NDS32_25_PCREL_RELA:
7233
7234
0
    if (h != NULL && !bfd_link_pic (info))
7235
0
      {
7236
0
        h->non_got_ref = 1;
7237
0
        h->plt.refcount += 1;
7238
0
      }
7239
7240
    /* If we are creating a shared library, and this is a reloc against
7241
       a global symbol, or a non PC relative reloc against a local
7242
       symbol, then we need to copy the reloc into the shared library.
7243
       However, if we are linking with -Bsymbolic, we do not need to
7244
       copy a reloc against a global symbol which is defined in an
7245
       object we are including in the link (i.e., DEF_REGULAR is set).
7246
       At this point we have not seen all the input files, so it is
7247
       possible that DEF_REGULAR is not set now but will be set later
7248
       (it is never cleared).  We account for that possibility below by
7249
       storing information in the dyn_relocs field of the hash table
7250
       entry.  A similar situation occurs when creating shared libraries
7251
       and symbol visibility changes render the symbol local.
7252
7253
       If on the other hand, we are creating an executable, we may need
7254
       to keep relocations for symbols satisfied by a dynamic library
7255
       if we manage to avoid copy relocs for the symbol.  */
7256
0
    if ((bfd_link_pic (info)
7257
0
         && (sec->flags & SEC_ALLOC) != 0
7258
0
         && ((r_type != R_NDS32_25_PCREL_RELA
7259
0
        && r_type != R_NDS32_15_PCREL_RELA
7260
0
        && r_type != R_NDS32_17_PCREL_RELA
7261
0
        && !(r_type == R_NDS32_32_RELA
7262
0
       && strcmp (sec->name, ".eh_frame") == 0))
7263
0
       || (h != NULL
7264
0
           && (!info->symbolic
7265
0
         || h->root.type == bfd_link_hash_defweak
7266
0
         || !h->def_regular))))
7267
0
        || (!bfd_link_pic (info)
7268
0
      && (sec->flags & SEC_ALLOC) != 0
7269
0
      && h != NULL
7270
0
      && (h->root.type == bfd_link_hash_defweak
7271
0
          || !h->def_regular)))
7272
0
      {
7273
0
        struct elf_dyn_relocs *p;
7274
0
        struct elf_dyn_relocs **head;
7275
7276
0
        if (dynobj == NULL)
7277
0
    htab->root.dynobj = dynobj = abfd;
7278
7279
        /* When creating a shared object, we must copy these
7280
     relocs into the output file.  We create a reloc
7281
     section in dynobj and make room for the reloc.  */
7282
0
        if (sreloc == NULL)
7283
0
    {
7284
0
      const char *name;
7285
7286
0
      name = bfd_elf_string_from_elf_section
7287
0
        (abfd, elf_elfheader (abfd)->e_shstrndx,
7288
0
         elf_section_data (sec)->rela.hdr->sh_name);
7289
0
      if (name == NULL)
7290
0
        return false;
7291
7292
0
      BFD_ASSERT (startswith (name, ".rela")
7293
0
            && strcmp (bfd_section_name (sec),
7294
0
           name + 5) == 0);
7295
7296
0
      sreloc = bfd_get_section_by_name (dynobj, name);
7297
0
      if (sreloc == NULL)
7298
0
        {
7299
0
          flagword flags;
7300
7301
0
          sreloc = bfd_make_section (dynobj, name);
7302
0
          flags = (SEC_HAS_CONTENTS | SEC_READONLY
7303
0
             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7304
0
          if ((sec->flags & SEC_ALLOC) != 0)
7305
0
      flags |= SEC_ALLOC | SEC_LOAD;
7306
0
          if (sreloc == NULL
7307
0
        || !bfd_set_section_flags (sreloc, flags)
7308
0
        || !bfd_set_section_alignment (sreloc, 2))
7309
0
      return false;
7310
7311
0
          elf_section_type (sreloc) = SHT_RELA;
7312
0
        }
7313
0
      elf_section_data (sec)->sreloc = sreloc;
7314
0
    }
7315
7316
        /* If this is a global symbol, we count the number of
7317
     relocations we need for this symbol.  */
7318
0
        if (h != NULL)
7319
0
    head = &h->dyn_relocs;
7320
0
        else
7321
0
    {
7322
0
      asection *s;
7323
0
      void *vpp;
7324
7325
0
      Elf_Internal_Sym *isym;
7326
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
7327
0
            abfd, r_symndx);
7328
0
      if (isym == NULL)
7329
0
        return false;
7330
7331
      /* Track dynamic relocs needed for local syms too.  */
7332
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7333
0
      if (s == NULL)
7334
0
        return false;
7335
7336
0
      vpp = &elf_section_data (s)->local_dynrel;
7337
0
      head = (struct elf_dyn_relocs **) vpp;
7338
0
    }
7339
7340
0
        p = *head;
7341
0
        if (p == NULL || p->sec != sec)
7342
0
    {
7343
0
      size_t amt = sizeof (*p);
7344
0
      p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7345
0
      if (p == NULL)
7346
0
        return false;
7347
0
      p->next = *head;
7348
0
      *head = p;
7349
0
      p->sec = sec;
7350
0
      p->count = 0;
7351
0
      p->pc_count = 0;
7352
0
    }
7353
7354
0
        p->count += 1;
7355
7356
        /* Since eh_frame is readonly, R_NDS32_32_RELA
7357
     reloc for eh_frame will cause shared library has
7358
     TEXTREL entry in the dynamic section. This lead glibc
7359
     testsuites to failure (bug-13092) and cause kernel fail
7360
     (bug-11819).  I think the best solution is to replace
7361
     absolute reloc with pc relative reloc in the eh_frame.
7362
     To do that, we need to support the following issues:
7363
7364
     === For GCC ===
7365
     * gcc/config/nds32/nds32.h: Define
7366
     ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7367
     and DW_EH_PE_sdata4 into DWARF exception header when
7368
     option have '-fpic'.
7369
7370
     === For binutils ===
7371
     * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7372
     * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7373
     may break our nds DIFF mechanism, therefore, we
7374
     must disable all linker relaxations to ensure
7375
     correctness.
7376
     * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7377
     R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7378
     do the necessary modification.
7379
7380
     Unfortunately, it still have some problems for nds32
7381
     to support pc relative reloc in the eh_frame. So I use
7382
     another solution to fix this issue.
7383
7384
     However, I find that ld always emit TEXTREL marker for
7385
     R_NDS32_NONE relocs in rel.dyn. These none relocs are
7386
     correspond to R_NDS32_32_RELA for .eh_frame section.
7387
     It means that we always reserve redundant entries of rel.dyn
7388
     for these relocs which actually do nothing in dynamic linker.
7389
7390
     Therefore, we regard these relocs as pc relative relocs
7391
     here and increase the pc_count.  */
7392
0
        if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7393
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7394
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7395
0
      || (r_type == R_NDS32_32_RELA
7396
0
          && strcmp (sec->name, ".eh_frame") == 0))
7397
0
    p->pc_count += 1;
7398
0
      }
7399
0
    break;
7400
7401
    /* This relocation describes the C++ object vtable hierarchy.
7402
       Reconstruct it for later use during GC.  */
7403
0
  case R_NDS32_RELA_GNU_VTINHERIT:
7404
0
  case R_NDS32_GNU_VTINHERIT:
7405
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7406
0
      return false;
7407
0
    break;
7408
7409
    /* This relocation describes which C++ vtable entries are actually
7410
       used.  Record for later use during GC.  */
7411
0
  case R_NDS32_GNU_VTENTRY:
7412
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7413
0
      return false;
7414
0
    break;
7415
0
  case R_NDS32_RELA_GNU_VTENTRY:
7416
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7417
0
      return false;
7418
0
    break;
7419
0
  }
7420
0
    }
7421
7422
0
  return true;
7423
0
}
7424
7425
/* Write VAL in uleb128 format to P, returning a pointer to the
7426
   following byte.
7427
   This code is copied from elf-attr.c.  */
7428
7429
static bfd_byte *
7430
write_uleb128 (bfd_byte *p, unsigned int val)
7431
0
{
7432
0
  bfd_byte c;
7433
0
  do
7434
0
    {
7435
0
      c = val & 0x7f;
7436
0
      val >>= 7;
7437
0
      if (val)
7438
0
  c |= 0x80;
7439
0
      *(p++) = c;
7440
0
    }
7441
0
  while (val);
7442
0
  return p;
7443
0
}
7444
7445
static bfd_signed_vma
7446
calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7447
      Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7448
0
{
7449
0
  bfd_signed_vma foff;
7450
0
  bfd_vma symval, addend;
7451
0
  asection *sym_sec;
7452
7453
  /* Get the value of the symbol referred to by the reloc.  */
7454
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7455
0
    {
7456
0
      Elf_Internal_Sym *isym;
7457
7458
      /* A local symbol.  */
7459
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
7460
7461
0
      if (isym->st_shndx == SHN_UNDEF)
7462
0
  sym_sec = bfd_und_section_ptr;
7463
0
      else if (isym->st_shndx == SHN_ABS)
7464
0
  sym_sec = bfd_abs_section_ptr;
7465
0
      else if (isym->st_shndx == SHN_COMMON)
7466
0
  sym_sec = bfd_com_section_ptr;
7467
0
      else
7468
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7469
0
      symval = isym->st_value + sym_sec->output_section->vma
7470
0
         + sym_sec->output_offset;
7471
0
    }
7472
0
  else
7473
0
    {
7474
0
      unsigned long indx;
7475
0
      struct elf_link_hash_entry *h;
7476
7477
      /* An external symbol.  */
7478
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7479
0
      h = elf_sym_hashes (abfd)[indx];
7480
0
      BFD_ASSERT (h != NULL);
7481
7482
0
      if (h->root.type != bfd_link_hash_defined
7483
0
    && h->root.type != bfd_link_hash_defweak)
7484
  /* This appears to be a reference to an undefined
7485
     symbol.  Just ignore it--it will be caught by the
7486
     regular reloc processing.  */
7487
0
  return 0;
7488
7489
0
      if (h->root.u.def.section->flags & SEC_MERGE)
7490
0
  {
7491
0
    sym_sec = h->root.u.def.section;
7492
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
7493
0
                 h->root.u.def.value);
7494
0
    symval = symval + sym_sec->output_section->vma
7495
0
       + sym_sec->output_offset;
7496
0
  }
7497
0
      else
7498
0
  symval = (h->root.u.def.value
7499
0
      + h->root.u.def.section->output_section->vma
7500
0
      + h->root.u.def.section->output_offset);
7501
0
    }
7502
7503
0
  addend = irel->r_addend;
7504
7505
0
  foff = (symval + addend
7506
0
    - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7507
0
  return foff;
7508
0
}
7509

7510
7511
/* Convert a 32-bit instruction to 16-bit one.
7512
   INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7513
   instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
7514
   type of INSN16.  Return 1 if successful.  */
7515
7516
static int
7517
nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7518
           int *pinsn_type)
7519
0
{
7520
0
  uint16_t insn16 = 0;
7521
0
  int insn_type = 0;
7522
0
  unsigned long mach = bfd_get_mach (abfd);
7523
7524
0
  if (N32_SH5 (insn) != 0)
7525
0
    return 0;
7526
7527
0
  switch (N32_SUB5 (insn))
7528
0
    {
7529
0
    case N32_ALU1_ADD_SLLI:
7530
0
    case N32_ALU1_ADD_SRLI:
7531
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7532
0
  {
7533
0
    insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7534
0
        N32_RB5 (insn));
7535
0
    insn_type = NDS32_INSN_ADD333;
7536
0
  }
7537
0
      else if (N32_IS_RT4 (insn))
7538
0
  {
7539
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7540
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7541
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7542
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7543
0
    insn_type = NDS32_INSN_ADD45;
7544
0
  }
7545
0
      break;
7546
7547
0
    case N32_ALU1_SUB_SLLI:
7548
0
    case N32_ALU1_SUB_SRLI:
7549
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7550
0
  {
7551
0
    insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7552
0
        N32_RB5 (insn));
7553
0
    insn_type = NDS32_INSN_SUB333;
7554
0
  }
7555
0
      else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7556
0
  {
7557
0
    insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7558
0
    insn_type = NDS32_INSN_SUB45;
7559
0
  }
7560
0
      break;
7561
7562
0
    case N32_ALU1_AND_SLLI:
7563
0
    case N32_ALU1_AND_SRLI:
7564
      /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
7565
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7566
0
    && N32_IS_RB3 (insn))
7567
0
  {
7568
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7569
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7570
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7571
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7572
0
    if (insn16)
7573
0
      insn_type = NDS32_INSN_AND33;
7574
0
  }
7575
0
      break;
7576
7577
0
    case N32_ALU1_XOR_SLLI:
7578
0
    case N32_ALU1_XOR_SRLI:
7579
      /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
7580
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7581
0
    && N32_IS_RB3 (insn))
7582
0
  {
7583
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7584
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7585
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7586
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7587
0
    if (insn16)
7588
0
      insn_type = NDS32_INSN_XOR33;
7589
0
  }
7590
0
      break;
7591
7592
0
    case N32_ALU1_OR_SLLI:
7593
0
    case N32_ALU1_OR_SRLI:
7594
      /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
7595
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7596
0
    && N32_IS_RB3 (insn))
7597
0
  {
7598
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7599
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7600
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7601
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7602
0
    if (insn16)
7603
0
      insn_type = NDS32_INSN_OR33;
7604
0
  }
7605
0
      break;
7606
0
    case N32_ALU1_NOR:
7607
      /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
7608
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7609
0
    && N32_RA5 (insn) == N32_RB5 (insn))
7610
0
  {
7611
0
    insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7612
0
    insn_type = NDS32_INSN_NOT33;
7613
0
  }
7614
0
      break;
7615
0
    case N32_ALU1_SRAI:
7616
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7617
0
  {
7618
0
    insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7619
0
    insn_type = NDS32_INSN_SRAI45;
7620
0
  }
7621
0
      break;
7622
7623
0
    case N32_ALU1_SRLI:
7624
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7625
0
  {
7626
0
    insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7627
0
    insn_type = NDS32_INSN_SRLI45;
7628
0
  }
7629
0
      break;
7630
7631
0
    case N32_ALU1_SLLI:
7632
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7633
0
  {
7634
0
    insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7635
0
        N32_UB5 (insn));
7636
0
    insn_type = NDS32_INSN_SLLI333;
7637
0
  }
7638
0
      break;
7639
7640
0
    case N32_ALU1_ZEH:
7641
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7642
0
  {
7643
0
    insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7644
0
    insn_type = NDS32_INSN_ZEH33;
7645
0
  }
7646
0
      break;
7647
7648
0
    case N32_ALU1_SEB:
7649
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7650
0
  {
7651
0
    insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7652
0
    insn_type = NDS32_INSN_SEB33;
7653
0
  }
7654
0
      break;
7655
7656
0
    case N32_ALU1_SEH:
7657
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7658
0
  {
7659
0
    insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7660
0
    insn_type = NDS32_INSN_SEH33;
7661
0
  }
7662
0
      break;
7663
7664
0
    case N32_ALU1_SLT:
7665
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7666
0
  {
7667
    /* Implicit r15.  */
7668
0
    insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7669
0
    insn_type = NDS32_INSN_SLT45;
7670
0
  }
7671
0
      break;
7672
7673
0
    case N32_ALU1_SLTS:
7674
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7675
0
  {
7676
    /* Implicit r15.  */
7677
0
    insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7678
0
    insn_type = NDS32_INSN_SLTS45;
7679
0
  }
7680
0
      break;
7681
0
    }
7682
7683
0
  if ((insn16 & 0x8000) == 0)
7684
0
    return 0;
7685
7686
0
  if (pinsn16)
7687
0
    *pinsn16 = insn16;
7688
0
  if (pinsn_type)
7689
0
    *pinsn_type = insn_type;
7690
0
  return 1;
7691
0
}
7692
7693
static int
7694
nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7695
           int *pinsn_type)
7696
0
{
7697
0
  uint16_t insn16 = 0;
7698
0
  int insn_type;
7699
0
  unsigned long mach = bfd_get_mach (abfd);
7700
7701
  /* TODO: bset, bclr, btgl, btst.  */
7702
0
  if (__GF (insn, 6, 4) != 0)
7703
0
    return 0;
7704
7705
0
  switch (N32_IMMU (insn, 6))
7706
0
    {
7707
0
    case N32_ALU2_MUL:
7708
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7709
0
    && N32_IS_RB3 (insn))
7710
0
  {
7711
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7712
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7713
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7714
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7715
0
    if (insn16)
7716
0
      insn_type = NDS32_INSN_MUL33;
7717
0
  }
7718
0
    }
7719
7720
0
  if ((insn16 & 0x8000) == 0)
7721
0
    return 0;
7722
7723
0
  if (pinsn16)
7724
0
    *pinsn16 = insn16;
7725
0
  if (pinsn_type)
7726
0
    *pinsn_type = insn_type;
7727
0
  return 1;
7728
0
}
7729
7730
int
7731
bfd_elf_nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7732
        int *pinsn_type)
7733
0
{
7734
0
  int op6;
7735
0
  uint16_t insn16 = 0;
7736
0
  int insn_type = 0;
7737
0
  unsigned long mach = bfd_get_mach (abfd);
7738
7739
  /* Decode 32-bit instruction.  */
7740
0
  if (insn & 0x80000000)
7741
0
    {
7742
      /* Not 32-bit insn.  */
7743
0
      return 0;
7744
0
    }
7745
7746
0
  op6 = N32_OP6 (insn);
7747
7748
  /* Convert it to 16-bit instruction.  */
7749
0
  switch (op6)
7750
0
    {
7751
0
    case N32_OP6_MOVI:
7752
0
      if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7753
0
  {
7754
0
    insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7755
0
    insn_type = NDS32_INSN_MOVI55;
7756
0
  }
7757
0
      else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7758
0
         && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7759
0
  {
7760
0
    insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7761
0
             N32_IMM20S (insn) - 16);
7762
0
    insn_type = NDS32_INSN_MOVPI45;
7763
0
  }
7764
0
      break;
7765
7766
0
    case N32_OP6_ADDI:
7767
0
      if (N32_IMM15S (insn) == 0)
7768
0
  {
7769
    /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7770
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7771
0
    if (mach <= MACH_V2
7772
0
        || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7773
0
      {
7774
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7775
0
        insn_type = NDS32_INSN_MOV55;
7776
0
      }
7777
0
  }
7778
0
      else if (N32_IMM15S (insn) > 0)
7779
0
  {
7780
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7781
0
      {
7782
0
        insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7783
0
            N32_IMM15S (insn));
7784
0
        insn_type = NDS32_INSN_ADDI333;
7785
0
      }
7786
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7787
0
       && N32_IMM15S (insn) < 32)
7788
0
      {
7789
0
        insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7790
0
        insn_type = NDS32_INSN_ADDI45;
7791
0
      }
7792
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7793
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7794
0
       && N32_IMM15S (insn) < 512)
7795
0
      {
7796
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7797
0
        insn_type = NDS32_INSN_ADDI10_SP;
7798
0
      }
7799
0
    else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7800
0
       && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7801
0
       && (N32_IMM15S (insn) % 4 == 0))
7802
0
      {
7803
0
        insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7804
0
           N32_IMM15S (insn) >> 2);
7805
0
        insn_type = NDS32_INSN_ADDRI36_SP;
7806
0
      }
7807
0
  }
7808
0
      else
7809
0
  {
7810
    /* Less than 0.  */
7811
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7812
0
      {
7813
0
        insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7814
0
            0 - N32_IMM15S (insn));
7815
0
        insn_type = NDS32_INSN_SUBI333;
7816
0
      }
7817
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7818
0
       && N32_IMM15S (insn) > -32)
7819
0
      {
7820
0
        insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7821
0
           0 - N32_IMM15S (insn));
7822
0
        insn_type = NDS32_INSN_SUBI45;
7823
0
      }
7824
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7825
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7826
0
       && N32_IMM15S (insn) >= -512)
7827
0
      {
7828
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7829
0
        insn_type = NDS32_INSN_ADDI10_SP;
7830
0
      }
7831
0
  }
7832
0
      break;
7833
7834
0
    case N32_OP6_ORI:
7835
0
      if (N32_IMM15S (insn) == 0)
7836
0
  {
7837
    /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7838
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7839
0
    if (mach <= MACH_V2
7840
0
        || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7841
0
      {
7842
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7843
0
        insn_type = NDS32_INSN_MOV55;
7844
0
      }
7845
0
  }
7846
0
      break;
7847
7848
0
    case N32_OP6_SUBRI:
7849
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7850
0
    && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7851
0
  {
7852
0
    insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7853
0
    insn_type = NDS32_INSN_NEG33;
7854
0
  }
7855
0
      break;
7856
7857
0
    case N32_OP6_ANDI:
7858
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7859
0
  {
7860
0
    if (N32_IMM15U (insn) == 1)
7861
0
      {
7862
0
        insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7863
0
        insn_type = NDS32_INSN_XLSB33;
7864
0
      }
7865
0
    else if (N32_IMM15U (insn) == 0x7ff)
7866
0
      {
7867
0
        insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7868
0
        insn_type = NDS32_INSN_X11B33;
7869
0
      }
7870
0
    else if (N32_IMM15U (insn) == 0xff)
7871
0
      {
7872
0
        insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7873
0
        insn_type = NDS32_INSN_ZEB33;
7874
0
      }
7875
0
    else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7876
0
       && N32_IMM15U (insn) < 256)
7877
0
      {
7878
0
        int imm15u = N32_IMM15U (insn);
7879
7880
0
        if (__builtin_popcount (imm15u) == 1)
7881
0
    {
7882
      /* BMSKI33 */
7883
0
      int imm3u = __builtin_ctz (imm15u);
7884
7885
0
      insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7886
0
      insn_type = NDS32_INSN_BMSKI33;
7887
0
    }
7888
0
        else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7889
0
    {
7890
      /* FEXTI33 */
7891
0
      int imm3u = __builtin_ctz (imm15u + 1) - 1;
7892
7893
0
      insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7894
0
      insn_type = NDS32_INSN_FEXTI33;
7895
0
    }
7896
0
      }
7897
0
  }
7898
0
      break;
7899
7900
0
    case N32_OP6_SLTI:
7901
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7902
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7903
0
  {
7904
0
    insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7905
0
    insn_type = NDS32_INSN_SLTI45;
7906
0
  }
7907
0
      break;
7908
7909
0
    case N32_OP6_SLTSI:
7910
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7911
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7912
0
  {
7913
0
    insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7914
0
    insn_type = NDS32_INSN_SLTSI45;
7915
0
  }
7916
0
      break;
7917
7918
0
    case N32_OP6_LWI:
7919
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7920
0
  {
7921
0
    insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7922
0
    insn_type = NDS32_INSN_LWI450;
7923
0
  }
7924
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7925
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
7926
0
  {
7927
0
    insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7928
0
        N32_IMM15S (insn));
7929
0
    insn_type = NDS32_INSN_LWI333;
7930
0
  }
7931
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7932
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7933
0
  {
7934
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7935
0
    insn_type = NDS32_INSN_LWI37;
7936
0
  }
7937
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7938
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7939
0
  {
7940
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7941
0
    insn_type = NDS32_INSN_LWI37_SP;
7942
0
  }
7943
0
      else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7944
0
         && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7945
0
  {
7946
0
    insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7947
0
             N32_IMM15S (insn) + 32);
7948
0
    insn_type = NDS32_INSN_LWI45_FE;
7949
0
  }
7950
0
      break;
7951
7952
0
    case N32_OP6_SWI:
7953
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7954
0
  {
7955
0
    insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7956
0
    insn_type = NDS32_INSN_SWI450;
7957
0
  }
7958
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7959
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
7960
0
  {
7961
0
    insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7962
0
        N32_IMM15S (insn));
7963
0
    insn_type = NDS32_INSN_SWI333;
7964
0
  }
7965
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7966
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7967
0
  {
7968
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7969
0
    insn_type = NDS32_INSN_SWI37;
7970
0
  }
7971
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7972
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7973
0
  {
7974
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7975
0
    insn_type = NDS32_INSN_SWI37_SP;
7976
0
  }
7977
0
      break;
7978
7979
0
    case N32_OP6_LWI_BI:
7980
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7981
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
7982
0
  {
7983
0
    insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7984
0
        N32_IMM15S (insn));
7985
0
    insn_type = NDS32_INSN_LWI333_BI;
7986
0
  }
7987
0
      break;
7988
7989
0
    case N32_OP6_SWI_BI:
7990
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7991
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
7992
0
  {
7993
0
    insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7994
0
        N32_IMM15S (insn));
7995
0
    insn_type = NDS32_INSN_SWI333_BI;
7996
0
  }
7997
0
      break;
7998
7999
0
    case N32_OP6_LHI:
8000
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8001
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8002
0
  {
8003
0
    insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8004
0
        N32_IMM15S (insn));
8005
0
    insn_type = NDS32_INSN_LHI333;
8006
0
  }
8007
0
      break;
8008
8009
0
    case N32_OP6_SHI:
8010
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8011
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8012
0
  {
8013
0
    insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8014
0
        N32_IMM15S (insn));
8015
0
    insn_type = NDS32_INSN_SHI333;
8016
0
  }
8017
0
      break;
8018
8019
0
    case N32_OP6_LBI:
8020
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8021
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8022
0
  {
8023
0
    insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8024
0
        N32_IMM15S (insn));
8025
0
    insn_type = NDS32_INSN_LBI333;
8026
0
  }
8027
0
      break;
8028
8029
0
    case N32_OP6_SBI:
8030
0
      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 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8034
0
        N32_IMM15S (insn));
8035
0
    insn_type = NDS32_INSN_SBI333;
8036
0
  }
8037
0
      break;
8038
8039
0
    case N32_OP6_ALU1:
8040
0
      return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8041
8042
0
    case N32_OP6_ALU2:
8043
0
      return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8044
8045
0
    case N32_OP6_BR1:
8046
0
      if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8047
0
  goto done;
8048
8049
0
      if ((insn & N32_BIT (14)) == 0)
8050
0
  {
8051
    /* N32_BR1_BEQ */
8052
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8053
0
        && N32_RT5 (insn) != REG_R5)
8054
0
      insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8055
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8056
0
       && N32_RA5 (insn) != REG_R5)
8057
0
      insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8058
0
    insn_type = NDS32_INSN_BEQS38;
8059
0
    break;
8060
0
  }
8061
0
      else
8062
0
  {
8063
    /* N32_BR1_BNE */
8064
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8065
0
        && N32_RT5 (insn) != REG_R5)
8066
0
      insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8067
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8068
0
       && N32_RA5 (insn) != REG_R5)
8069
0
      insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8070
0
    insn_type = NDS32_INSN_BNES38;
8071
0
    break;
8072
0
  }
8073
0
      break;
8074
8075
0
    case N32_OP6_BR2:
8076
0
      switch (N32_BR2_SUB (insn))
8077
0
  {
8078
0
  case N32_BR2_BEQZ:
8079
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8080
0
      {
8081
0
        insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8082
0
        insn_type = NDS32_INSN_BEQZ38;
8083
0
      }
8084
0
    else if (N32_RT5 (insn) == REG_R15
8085
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8086
0
      {
8087
0
        insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8088
0
        insn_type = NDS32_INSN_BEQZS8;
8089
0
      }
8090
0
    break;
8091
8092
0
  case N32_BR2_BNEZ:
8093
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8094
0
      {
8095
0
        insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8096
0
        insn_type = NDS32_INSN_BNEZ38;
8097
0
      }
8098
0
    else if (N32_RT5 (insn) == REG_R15
8099
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8100
0
      {
8101
0
        insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8102
0
        insn_type = NDS32_INSN_BNEZS8;
8103
0
      }
8104
0
    break;
8105
8106
0
  case N32_BR2_SOP0:
8107
0
    if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8108
0
      {
8109
0
        insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8110
0
        insn_type = NDS32_INSN_IFCALL9;
8111
0
      }
8112
0
    break;
8113
0
  }
8114
0
      break;
8115
8116
0
    case N32_OP6_JI:
8117
0
      if ((insn & N32_BIT (24)) == 0)
8118
0
  {
8119
    /* N32_JI_J */
8120
0
    if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8121
0
      {
8122
0
        insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8123
0
        insn_type = NDS32_INSN_J8;
8124
0
      }
8125
0
  }
8126
0
      break;
8127
8128
0
    case N32_OP6_JREG:
8129
0
      if (__GF (insn, 8, 2) != 0)
8130
0
  goto done;
8131
8132
0
      switch (N32_IMMU (insn, 5))
8133
0
  {
8134
0
  case N32_JREG_JR:
8135
0
    if (N32_JREG_HINT (insn) == 0)
8136
0
      {
8137
        /* jr */
8138
0
        insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8139
0
        insn_type = NDS32_INSN_JR5;
8140
0
      }
8141
0
    else if (N32_JREG_HINT (insn) == 1)
8142
0
      {
8143
        /* ret */
8144
0
        insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8145
0
        insn_type = NDS32_INSN_RET5;
8146
0
      }
8147
0
    else if (N32_JREG_HINT (insn) == 3)
8148
0
      {
8149
        /* ifret = mov55 $sp, $sp */
8150
0
        insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8151
0
        insn_type = NDS32_INSN_IFRET;
8152
0
      }
8153
0
    break;
8154
8155
0
  case N32_JREG_JRAL:
8156
    /* It's convertible when return rt5 is $lp and address
8157
       translation is kept.  */
8158
0
    if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8159
0
      {
8160
0
        insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8161
0
        insn_type = NDS32_INSN_JRAL5;
8162
0
      }
8163
0
    break;
8164
0
  }
8165
0
      break;
8166
8167
0
    case N32_OP6_MISC:
8168
0
      if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8169
0
  {
8170
    /* For v3, swid above 31 are used for ex9.it.  */
8171
0
    insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8172
0
    insn_type = NDS32_INSN_BREAK16;
8173
0
  }
8174
0
      break;
8175
8176
0
    default:
8177
      /* This instruction has no 16-bit variant.  */
8178
0
      goto done;
8179
0
    }
8180
8181
0
 done:
8182
  /* Bit-15 of insn16 should be set for a valid instruction.  */
8183
0
  if ((insn16 & 0x8000) == 0)
8184
0
    return 0;
8185
8186
0
  if (pinsn16)
8187
0
    *pinsn16 = insn16;
8188
0
  if (pinsn_type)
8189
0
    *pinsn_type = insn_type;
8190
0
  return 1;
8191
0
}
8192
8193
static int
8194
special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8195
        Elf_Internal_Rela *reloc)
8196
0
{
8197
0
  uint16_t insn16 = 0;
8198
8199
0
  if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8200
0
      || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8201
0
    return 0;
8202
8203
0
  if (!N32_IS_RT3 (insn))
8204
0
    return 0;
8205
8206
0
  switch (N32_OP6 (insn))
8207
0
    {
8208
0
    case N32_OP6_LWI:
8209
0
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8210
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8211
0
      break;
8212
0
    case N32_OP6_SWI:
8213
0
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8214
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8215
0
      break;
8216
0
    case N32_OP6_HWGP:
8217
0
      if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8218
0
  break;
8219
8220
0
      if (__GF (insn, 17, 3) == 6)
8221
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8222
0
      else if (__GF (insn, 17, 3) == 7)
8223
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8224
0
      break;
8225
0
    }
8226
8227
0
  if ((insn16 & 0x8000) == 0)
8228
0
    return 0;
8229
8230
0
  *pinsn16 = insn16;
8231
0
  return 1;
8232
0
}
8233
8234
/* Convert a 16-bit instruction to 32-bit one.
8235
   INSN16 it the input and PINSN it the point to output.
8236
   Return non-zero on successful.  Otherwise 0 is returned.  */
8237
8238
int
8239
bfd_elf_nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8240
0
{
8241
0
  uint32_t insn = 0xffffffff;
8242
0
  unsigned long mach = bfd_get_mach (abfd);
8243
8244
  /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
8245
8246
0
  switch (__GF (insn16, 9, 6))
8247
0
    {
8248
0
    case 0x4:     /* add45 */
8249
0
      insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8250
0
           N16_RA5 (insn16));
8251
0
      goto done;
8252
0
    case 0x5:     /* sub45 */
8253
0
      insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8254
0
           N16_RA5 (insn16));
8255
0
      goto done;
8256
0
    case 0x6:     /* addi45 */
8257
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8258
0
      N16_IMM5U (insn16));
8259
0
      goto done;
8260
0
    case 0x7:     /* subi45 */
8261
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8262
0
      -N16_IMM5U (insn16));
8263
0
      goto done;
8264
0
    case 0x8:     /* srai45 */
8265
0
      insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8266
0
           N16_IMM5U (insn16));
8267
0
      goto done;
8268
0
    case 0x9:     /* srli45 */
8269
0
      insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8270
0
           N16_IMM5U (insn16));
8271
0
      goto done;
8272
0
    case 0xa:     /* slli333 */
8273
0
      insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8274
0
           N16_IMM3U (insn16));
8275
0
      goto done;
8276
0
    case 0xc:     /* add333 */
8277
0
      insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8278
0
           N16_RB3 (insn16));
8279
0
      goto done;
8280
0
    case 0xd:     /* sub333 */
8281
0
      insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8282
0
           N16_RB3 (insn16));
8283
0
      goto done;
8284
0
    case 0xe:     /* addi333 */
8285
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8286
0
      N16_IMM3U (insn16));
8287
0
      goto done;
8288
0
    case 0xf:     /* subi333 */
8289
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8290
0
      -N16_IMM3U (insn16));
8291
0
      goto done;
8292
0
    case 0x10:      /* lwi333 */
8293
0
      insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8294
0
      N16_IMM3U (insn16));
8295
0
      goto done;
8296
0
    case 0x12:      /* lhi333 */
8297
0
      insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8298
0
      N16_IMM3U (insn16));
8299
0
      goto done;
8300
0
    case 0x13:      /* lbi333 */
8301
0
      insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8302
0
      N16_IMM3U (insn16));
8303
0
      goto done;
8304
0
    case 0x11:      /* lwi333.bi */
8305
0
      insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8306
0
      N16_IMM3U (insn16));
8307
0
      goto done;
8308
0
    case 0x14:      /* swi333 */
8309
0
      insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8310
0
      N16_IMM3U (insn16));
8311
0
      goto done;
8312
0
    case 0x16:      /* shi333 */
8313
0
      insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8314
0
      N16_IMM3U (insn16));
8315
0
      goto done;
8316
0
    case 0x17:      /* sbi333 */
8317
0
      insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8318
0
      N16_IMM3U (insn16));
8319
0
      goto done;
8320
0
    case 0x15:      /* swi333.bi */
8321
0
      insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8322
0
      N16_IMM3U (insn16));
8323
0
      goto done;
8324
0
    case 0x18:      /* addri36.sp */
8325
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8326
0
      N16_IMM6U (insn16) << 2);
8327
0
      goto done;
8328
0
    case 0x19:      /* lwi45.fe */
8329
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8330
0
      (N16_IMM5U (insn16) - 32));
8331
0
      goto done;
8332
0
    case 0x1a:      /* lwi450 */
8333
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8334
0
      goto done;
8335
0
    case 0x1b:      /* swi450 */
8336
0
      insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8337
0
      goto done;
8338
8339
      /* These are r15 implied instructions.  */
8340
0
    case 0x30:      /* slts45 */
8341
0
      insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8342
0
      goto done;
8343
0
    case 0x31:      /* slt45 */
8344
0
      insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8345
0
      goto done;
8346
0
    case 0x32:      /* sltsi45 */
8347
0
      insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8348
0
      goto done;
8349
0
    case 0x33:      /* slti45 */
8350
0
      insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8351
0
      goto done;
8352
0
    case 0x34:      /* beqzs8, bnezs8 */
8353
0
      if (insn16 & N32_BIT (8))
8354
0
  insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8355
0
      else
8356
0
  insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8357
0
      goto done;
8358
8359
0
    case 0x35:      /* break16, ex9.it */
8360
      /* Only consider range of v3 break16.  */
8361
0
      insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8362
0
      goto done;
8363
8364
0
    case 0x3c:      /* ifcall9 */
8365
0
      insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8366
0
      goto done;
8367
0
    case 0x3d:      /* movpi45 */
8368
0
      insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8369
0
      goto done;
8370
8371
0
    case 0x3f:      /* MISC33 */
8372
0
      switch (insn16 & 0x7)
8373
0
  {
8374
0
  case 2:     /* neg33 */
8375
0
    insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8376
0
    break;
8377
0
  case 3:     /* not33 */
8378
0
    insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8379
0
         N16_RA3 (insn16));
8380
0
    break;
8381
0
  case 4:     /* mul33 */
8382
0
    insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8383
0
         N16_RA3 (insn16));
8384
0
    break;
8385
0
  case 5:     /* xor33 */
8386
0
    insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8387
0
         N16_RA3 (insn16));
8388
0
    break;
8389
0
  case 6:     /* and33 */
8390
0
    insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8391
0
         N16_RA3 (insn16));
8392
0
    break;
8393
0
  case 7:     /* or33 */
8394
0
    insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8395
0
         N16_RA3 (insn16));
8396
0
    break;
8397
0
  }
8398
0
      goto done;
8399
8400
0
    case 0xb:
8401
0
      switch (insn16 & 0x7)
8402
0
  {
8403
0
  case 0:     /* zeb33 */
8404
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8405
0
    break;
8406
0
  case 1:     /* zeh33 */
8407
0
    insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8408
0
    break;
8409
0
  case 2:     /* seb33 */
8410
0
    insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8411
0
    break;
8412
0
  case 3:     /* seh33 */
8413
0
    insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8414
0
    break;
8415
0
  case 4:     /* xlsb33 */
8416
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8417
0
    break;
8418
0
  case 5:     /* x11b33 */
8419
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8420
0
    break;
8421
0
  case 6:     /* bmski33 */
8422
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8423
0
          1 << __GF (insn16, 3, 3));
8424
0
    break;
8425
0
  case 7:     /* fexti33 */
8426
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8427
0
          (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8428
0
    break;
8429
0
  }
8430
0
      goto done;
8431
0
    }
8432
8433
0
  switch (__GF (insn16, 10, 5))
8434
0
    {
8435
0
    case 0x0:     /* mov55 or ifret16 */
8436
0
      if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8437
0
    && N16_RT5 (insn16) == N16_RA5 (insn16))
8438
0
  insn = N32_JREG (JR, 0, 0, 0, 3);
8439
0
      else
8440
0
  insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8441
0
      goto done;
8442
0
    case 0x1:     /* movi55 */
8443
0
      insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8444
0
      goto done;
8445
0
    case 0x1b:      /* addi10s (V2) */
8446
0
      insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8447
0
      goto done;
8448
0
    }
8449
8450
0
  switch (__GF (insn16, 11, 4))
8451
0
    {
8452
0
    case 0x7:     /* lwi37.fp/swi37.fp */
8453
0
      if (insn16 & N32_BIT (7)) /* swi37.fp */
8454
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8455
0
      else      /* lwi37.fp */
8456
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8457
0
      goto done;
8458
0
    case 0x8:     /* beqz38 */
8459
0
      insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8460
0
      goto done;
8461
0
    case 0x9:     /* bnez38 */
8462
0
      insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8463
0
      goto done;
8464
0
    case 0xa:     /* beqs38/j8, implied r5 */
8465
0
      if (N16_RT38 (insn16) == 5)
8466
0
  insn = N32_JI (J, N16_IMM8S (insn16));
8467
0
      else
8468
0
  insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8469
0
      goto done;
8470
0
    case 0xb:     /* bnes38 and others.  */
8471
0
      if (N16_RT38 (insn16) == 5)
8472
0
  {
8473
0
    switch (__GF (insn16, 5, 3))
8474
0
      {
8475
0
      case 0:   /* jr5 */
8476
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8477
0
        break;
8478
0
      case 4:   /* ret5 */
8479
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8480
0
        break;
8481
0
      case 1:   /* jral5 */
8482
0
        insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8483
0
        break;
8484
0
      case 2:   /* ex9.it imm5 */
8485
        /* ex9.it had no 32-bit variantl.  */
8486
0
        break;
8487
0
      case 5:   /* add5.pc */
8488
        /* add5.pc had no 32-bit variantl.  */
8489
0
        break;
8490
0
      }
8491
0
  }
8492
0
      else      /* bnes38 */
8493
0
  insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8494
0
      goto done;
8495
0
    case 0xe:     /* lwi37/swi37 */
8496
0
      if (insn16 & (1 << 7)) /* swi37.sp */
8497
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8498
0
      else      /* lwi37.sp */
8499
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8500
0
      goto done;
8501
0
    }
8502
8503
0
 done:
8504
0
  if (insn & 0x80000000)
8505
0
    return 0;
8506
8507
0
  if (pinsn)
8508
0
    *pinsn = insn;
8509
0
  return 1;
8510
0
}
8511

8512
8513
static bool
8514
is_sda_access_insn (unsigned long insn)
8515
0
{
8516
0
  switch (N32_OP6 (insn))
8517
0
    {
8518
0
    case N32_OP6_LWI:
8519
0
    case N32_OP6_LHI:
8520
0
    case N32_OP6_LHSI:
8521
0
    case N32_OP6_LBI:
8522
0
    case N32_OP6_LBSI:
8523
0
    case N32_OP6_SWI:
8524
0
    case N32_OP6_SHI:
8525
0
    case N32_OP6_SBI:
8526
0
    case N32_OP6_LWC:
8527
0
    case N32_OP6_LDC:
8528
0
    case N32_OP6_SWC:
8529
0
    case N32_OP6_SDC:
8530
0
      return true;
8531
0
    default:
8532
0
      ;
8533
0
    }
8534
0
  return false;
8535
0
}
8536
8537
static unsigned long
8538
turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8539
0
{
8540
0
  uint32_t oinsn = 0;
8541
8542
0
  switch (type)
8543
0
    {
8544
0
    case R_NDS32_GOT_LO12:
8545
0
    case R_NDS32_GOTOFF_LO12:
8546
0
    case R_NDS32_PLTREL_LO12:
8547
0
    case R_NDS32_PLT_GOTREL_LO12:
8548
0
    case R_NDS32_LO12S0_RELA:
8549
0
      switch (N32_OP6 (insn))
8550
0
  {
8551
0
  case N32_OP6_LBI:
8552
    /* lbi.gp */
8553
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8554
0
    break;
8555
0
  case N32_OP6_LBSI:
8556
    /* lbsi.gp */
8557
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8558
0
    break;
8559
0
  case N32_OP6_SBI:
8560
    /* sbi.gp */
8561
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8562
0
    break;
8563
0
  case N32_OP6_ORI:
8564
    /* addi.gp */
8565
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8566
0
    break;
8567
0
  }
8568
0
      break;
8569
8570
0
    case R_NDS32_LO12S1_RELA:
8571
0
      switch (N32_OP6 (insn))
8572
0
  {
8573
0
  case N32_OP6_LHI:
8574
    /* lhi.gp */
8575
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8576
0
    break;
8577
0
  case N32_OP6_LHSI:
8578
    /* lhsi.gp */
8579
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8580
0
    break;
8581
0
  case N32_OP6_SHI:
8582
    /* shi.gp */
8583
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8584
0
    break;
8585
0
  }
8586
0
      break;
8587
8588
0
    case R_NDS32_LO12S2_RELA:
8589
0
      switch (N32_OP6 (insn))
8590
0
  {
8591
0
  case N32_OP6_LWI:
8592
    /* lwi.gp */
8593
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8594
0
    break;
8595
0
  case N32_OP6_SWI:
8596
    /* swi.gp */
8597
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8598
0
    break;
8599
0
  }
8600
0
      break;
8601
8602
0
    case R_NDS32_LO12S2_DP_RELA:
8603
0
    case R_NDS32_LO12S2_SP_RELA:
8604
0
      oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8605
0
      break;
8606
0
    }
8607
8608
0
  if (oinsn)
8609
0
    *pinsn = oinsn;
8610
8611
0
  return oinsn != 0;
8612
0
}
8613
8614
/* Linker hasn't found the correct merge section for non-section symbol
8615
   in relax time, this work is left to the function elf_link_input_bfd().
8616
   So for non-section symbol, _bfd_merged_section_offset is also needed
8617
   to find the correct symbol address.  */
8618
8619
static bfd_vma
8620
nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8621
        asection **psec, Elf_Internal_Rela *rel)
8622
0
{
8623
0
  asection *sec = *psec;
8624
0
  bfd_vma relocation;
8625
8626
0
  relocation = (sec->output_section->vma
8627
0
    + sec->output_offset + sym->st_value);
8628
0
  if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8629
0
    {
8630
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8631
0
  rel->r_addend =
8632
0
    _bfd_merged_section_offset (abfd, psec,
8633
0
              sym->st_value + rel->r_addend);
8634
0
      else
8635
0
  rel->r_addend =
8636
0
    _bfd_merged_section_offset (abfd, psec,
8637
0
              sym->st_value) + rel->r_addend;
8638
8639
0
      if (sec != *psec)
8640
0
  {
8641
    /* If we have changed the section, and our original section is
8642
       marked with SEC_EXCLUDE, it means that the original
8643
       SEC_MERGE section has been completely subsumed in some
8644
       other SEC_MERGE section.  In this case, we need to leave
8645
       some info around for --emit-relocs.  */
8646
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
8647
0
      sec->kept_section = *psec;
8648
0
    sec = *psec;
8649
0
  }
8650
0
      rel->r_addend -= relocation;
8651
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
8652
0
    }
8653
0
  return relocation;
8654
0
}
8655
8656
static bfd_vma
8657
calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8658
        Elf_Internal_Sym *isymbuf,
8659
        Elf_Internal_Shdr *symtab_hdr)
8660
0
{
8661
0
  bfd_signed_vma foff;
8662
0
  bfd_vma symval, addend;
8663
0
  Elf_Internal_Rela irel_fn;
8664
0
  Elf_Internal_Sym *isym;
8665
0
  asection *sym_sec;
8666
8667
  /* Get the value of the symbol referred to by the reloc.  */
8668
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8669
0
    {
8670
      /* A local symbol.  */
8671
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
8672
8673
0
      if (isym->st_shndx == SHN_UNDEF)
8674
0
  sym_sec = bfd_und_section_ptr;
8675
0
      else if (isym->st_shndx == SHN_ABS)
8676
0
  sym_sec = bfd_abs_section_ptr;
8677
0
      else if (isym->st_shndx == SHN_COMMON)
8678
0
  sym_sec = bfd_com_section_ptr;
8679
0
      else
8680
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8681
0
      memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8682
0
      symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8683
0
      addend = irel_fn.r_addend;
8684
0
    }
8685
0
  else
8686
0
    {
8687
0
      unsigned long indx;
8688
0
      struct elf_link_hash_entry *h;
8689
8690
      /* An external symbol.  */
8691
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8692
0
      h = elf_sym_hashes (abfd)[indx];
8693
0
      BFD_ASSERT (h != NULL);
8694
8695
0
      while (h->root.type == bfd_link_hash_indirect
8696
0
       || h->root.type == bfd_link_hash_warning)
8697
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
8698
8699
0
      if (h->root.type != bfd_link_hash_defined
8700
0
    && h->root.type != bfd_link_hash_defweak)
8701
  /* This appears to be a reference to an undefined
8702
     symbol.  Just ignore it--it will be caught by the
8703
     regular reloc processing.  */
8704
0
  return 0;
8705
8706
0
      if (h->root.u.def.section->flags & SEC_MERGE)
8707
0
  {
8708
0
    sym_sec = h->root.u.def.section;
8709
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
8710
0
                 h->root.u.def.value);
8711
0
    symval = symval + sym_sec->output_section->vma
8712
0
       + sym_sec->output_offset;
8713
0
  }
8714
0
      else
8715
0
  symval = (h->root.u.def.value
8716
0
      + h->root.u.def.section->output_section->vma
8717
0
      + h->root.u.def.section->output_offset);
8718
0
      addend = irel->r_addend;
8719
0
    }
8720
8721
0
  foff = symval + addend;
8722
8723
0
  return foff;
8724
0
}
8725
8726
static int
8727
is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8728
        asection *sec, Elf_Internal_Rela *rel)
8729
0
{
8730
0
  bfd_byte *contents;
8731
0
  unsigned short insn16;
8732
8733
0
  if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8734
0
    return false;
8735
0
  contents = elf_section_data (sec)->this_hdr.contents;
8736
0
  insn16 = bfd_getb16 (contents + rel->r_offset);
8737
0
  if (insn16 == NDS32_NOP16)
8738
0
    return true;
8739
0
  return false;
8740
0
}
8741
8742
/* It checks whether the instruction could be converted to
8743
   16-bit form and returns the converted one.
8744
8745
   `internal_relocs' is supposed to be sorted.  */
8746
8747
static int
8748
is_convert_32_to_16 (bfd *abfd, asection *sec,
8749
         Elf_Internal_Rela *reloc,
8750
         Elf_Internal_Rela *internal_relocs,
8751
         Elf_Internal_Rela *irelend,
8752
         uint16_t *insn16)
8753
0
{
8754
0
#define NORMAL_32_TO_16 (1 << 0)
8755
0
#define SPECIAL_32_TO_16 (1 << 1)
8756
0
  bfd_byte *contents = NULL;
8757
0
  bfd_signed_vma off;
8758
0
  bfd_vma mem_addr;
8759
0
  uint32_t insn = 0;
8760
0
  Elf_Internal_Rela *pc_rel;
8761
0
  Elf_Internal_Shdr *symtab_hdr;
8762
0
  Elf_Internal_Sym *isymbuf = NULL;
8763
0
  int convert_type;
8764
0
  bfd_vma offset;
8765
8766
0
  if (reloc->r_offset + 4 > sec->size)
8767
0
    return false;
8768
8769
0
  offset = reloc->r_offset;
8770
8771
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true))
8772
0
    return false;
8773
0
  insn = bfd_getb32 (contents + offset);
8774
8775
0
  if (bfd_elf_nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8776
0
    convert_type = NORMAL_32_TO_16;
8777
0
  else if (special_convert_32_to_16 (insn, insn16, reloc))
8778
0
    convert_type = SPECIAL_32_TO_16;
8779
0
  else
8780
0
    return false;
8781
8782
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8783
0
  if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8784
0
    return false;
8785
8786
  /* Find the first relocation of the same relocation-type,
8787
     so we iteratie them forward.  */
8788
0
  pc_rel = reloc;
8789
0
  while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8790
0
    pc_rel--;
8791
8792
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8793
0
    {
8794
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8795
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8796
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8797
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8798
0
  {
8799
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8800
0
    if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8801
0
        || off == 0)
8802
0
      return false;
8803
0
    break;
8804
0
  }
8805
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8806
0
  {
8807
    /* movi => movi55  */
8808
0
    mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8809
0
                 symtab_hdr);
8810
    /* mem_addr is unsigned, but the value should
8811
       be between [-16, 15].  */
8812
0
    if ((mem_addr + 0x10) >> 5)
8813
0
      return false;
8814
0
    break;
8815
0
  }
8816
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8817
0
         || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8818
0
  {
8819
    /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8820
       because it can be relaxed to addi for TLS_LE_ADD.  */
8821
0
    return false;
8822
0
  }
8823
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8824
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8825
0
         && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8826
0
         && convert_type == SPECIAL_32_TO_16)
8827
0
  {
8828
    /* fp-as-gp
8829
       We've selected a best fp-base for this access, so we can
8830
       always resolve it anyway.  Do nothing.  */
8831
0
    break;
8832
0
  }
8833
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8834
0
    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8835
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8836
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8837
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8838
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8839
0
  {
8840
    /* Prevent unresolved addi instruction translate
8841
       to addi45 or addi333.  */
8842
0
    return false;
8843
0
  }
8844
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8845
0
  {
8846
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8847
0
    if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8848
0
      return false;
8849
0
    break;
8850
0
  }
8851
0
    }
8852
8853
0
  return true;
8854
0
}
8855
8856
static void
8857
nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8858
        Elf_Internal_Rela *reloc,
8859
        Elf_Internal_Rela *internal_relocs,
8860
        Elf_Internal_Rela *irelend,
8861
        unsigned short insn16)
8862
0
{
8863
0
  Elf_Internal_Rela *pc_rel;
8864
0
  bfd_vma offset;
8865
8866
0
  offset = reloc->r_offset;
8867
0
  bfd_putb16 (insn16, contents + offset);
8868
  /* Find the first relocation of the same relocation-type,
8869
     so we iteratie them forward.  */
8870
0
  pc_rel = reloc;
8871
0
  while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8872
0
    pc_rel--;
8873
8874
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8875
0
    {
8876
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8877
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8878
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8879
0
  {
8880
0
    pc_rel->r_info =
8881
0
      ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8882
0
  }
8883
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8884
0
  pc_rel->r_info =
8885
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8886
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8887
0
  pc_rel->r_info =
8888
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8889
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8890
0
         || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8891
0
  pc_rel->r_info =
8892
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8893
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8894
0
  pc_rel->r_info =
8895
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8896
0
    }
8897
0
}
8898
8899
/* Find a relocation of type specified by `reloc_type'
8900
   of the same r_offset with reloc.
8901
   If not found, return irelend.
8902
8903
   Assuming relocations are sorted by r_offset,
8904
   we find the relocation from `reloc' backward untill relocs,
8905
   or find it from `reloc' forward untill irelend.  */
8906
8907
static Elf_Internal_Rela *
8908
find_relocs_at_address (Elf_Internal_Rela *reloc,
8909
      Elf_Internal_Rela *relocs,
8910
      Elf_Internal_Rela *irelend,
8911
      enum elf_nds32_reloc_type reloc_type)
8912
0
{
8913
0
  Elf_Internal_Rela *rel_t;
8914
8915
  /* Find backward.  */
8916
0
  for (rel_t = reloc;
8917
0
       rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8918
0
       rel_t--)
8919
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8920
0
      return rel_t;
8921
8922
  /* We didn't find it backward.  Try find it forward.  */
8923
0
  for (rel_t = reloc;
8924
0
       rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8925
0
       rel_t++)
8926
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8927
0
      return rel_t;
8928
8929
0
  return irelend;
8930
0
}
8931
8932
/* Find a relocation of specified type and offset.
8933
   `reloc' is just a refence point to find a relocation at specified offset.
8934
   If not found, return irelend.
8935
8936
   Assuming relocations are sorted by r_offset,
8937
   we find the relocation from `reloc' backward untill relocs,
8938
   or find it from `reloc' forward untill irelend.  */
8939
8940
static Elf_Internal_Rela *
8941
find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8942
           Elf_Internal_Rela *relocs,
8943
           Elf_Internal_Rela *irelend,
8944
           enum elf_nds32_reloc_type reloc_type,
8945
           bfd_vma offset_p)
8946
0
{
8947
0
  Elf_Internal_Rela *rel_t = NULL;
8948
8949
  /* First, we try to find a relocation of offset `offset_p',
8950
     and then we use find_relocs_at_address to find specific type.  */
8951
8952
0
  if (reloc->r_offset > offset_p)
8953
0
    {
8954
      /* Find backward.  */
8955
0
      for (rel_t = reloc;
8956
0
     rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8957
0
  /* Do nothing.  */;
8958
0
    }
8959
0
  else if (reloc->r_offset < offset_p)
8960
0
    {
8961
      /* Find forward.  */
8962
0
      for (rel_t = reloc;
8963
0
     rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8964
0
  /* Do nothing.  */;
8965
0
    }
8966
0
  else
8967
0
    rel_t = reloc;
8968
8969
  /* Not found?  */
8970
0
  if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8971
0
    return irelend;
8972
8973
0
  return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8974
0
}
8975
8976
typedef struct nds32_elf_blank nds32_elf_blank_t;
8977
struct nds32_elf_blank
8978
{
8979
  /* Where the blank begins.  */
8980
  bfd_vma offset;
8981
  /* The size of the blank.  */
8982
  bfd_vma size;
8983
  /* The accumulative size before this blank.  */
8984
  bfd_vma total_size;
8985
  nds32_elf_blank_t *next;
8986
  nds32_elf_blank_t *prev;
8987
};
8988
8989
static nds32_elf_blank_t *blank_free_list = NULL;
8990
8991
static nds32_elf_blank_t *
8992
create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8993
0
{
8994
0
  nds32_elf_blank_t *blank_t;
8995
8996
0
  if (blank_free_list)
8997
0
    {
8998
0
      blank_t = blank_free_list;
8999
0
      blank_free_list = blank_free_list->next;
9000
0
    }
9001
0
  else
9002
0
    blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9003
9004
0
  if (blank_t == NULL)
9005
0
    return NULL;
9006
9007
0
  blank_t->offset = offset_p;
9008
0
  blank_t->size = size_p;
9009
0
  blank_t->total_size = 0;
9010
0
  blank_t->next = NULL;
9011
0
  blank_t->prev = NULL;
9012
9013
0
  return blank_t;
9014
0
}
9015
9016
static void
9017
remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9018
0
{
9019
0
  if (blank_free_list)
9020
0
    {
9021
0
      blank_free_list->prev = blank_p;
9022
0
      blank_p->next = blank_free_list;
9023
0
    }
9024
0
  else
9025
0
    blank_p->next = NULL;
9026
9027
0
  blank_p->prev = NULL;
9028
0
  blank_free_list = blank_p;
9029
0
}
9030
9031
static void
9032
clean_nds32_elf_blank (void)
9033
0
{
9034
0
  nds32_elf_blank_t *blank_t;
9035
9036
0
  while (blank_free_list)
9037
0
    {
9038
0
      blank_t = blank_free_list;
9039
0
      blank_free_list = blank_free_list->next;
9040
0
      free (blank_t);
9041
0
    }
9042
0
}
9043
9044
static nds32_elf_blank_t *
9045
search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9046
0
{
9047
0
  nds32_elf_blank_t *blank_t;
9048
9049
0
  if (!blank_p)
9050
0
    return NULL;
9051
0
  blank_t = blank_p;
9052
9053
0
  while (blank_t && addr < blank_t->offset)
9054
0
    blank_t = blank_t->prev;
9055
0
  while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9056
0
    blank_t = blank_t->next;
9057
9058
0
  return blank_t;
9059
0
}
9060
9061
static bfd_vma
9062
get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9063
         int overwrite)
9064
0
{
9065
0
  nds32_elf_blank_t *blank_t;
9066
9067
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9068
0
  if (!blank_t)
9069
0
    return 0;
9070
9071
0
  if (overwrite)
9072
0
    *blank_p = blank_t;
9073
9074
0
  if (addr < blank_t->offset + blank_t->size)
9075
0
    return blank_t->total_size + (addr - blank_t->offset);
9076
0
  else
9077
0
    return blank_t->total_size + blank_t->size;
9078
0
}
9079
9080
static bool
9081
insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9082
0
{
9083
0
  nds32_elf_blank_t *blank_t, *blank_t2;
9084
9085
0
  if (!*blank_p)
9086
0
    {
9087
0
      *blank_p = create_nds32_elf_blank (addr, len);
9088
0
      return *blank_p != NULL;
9089
0
    }
9090
9091
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9092
9093
0
  if (blank_t == NULL)
9094
0
    {
9095
0
      blank_t = create_nds32_elf_blank (addr, len);
9096
0
      if (!blank_t)
9097
0
  return false;
9098
0
      while ((*blank_p)->prev != NULL)
9099
0
  *blank_p = (*blank_p)->prev;
9100
0
      blank_t->next = *blank_p;
9101
0
      (*blank_p)->prev = blank_t;
9102
0
      (*blank_p) = blank_t;
9103
0
      return true;
9104
0
    }
9105
9106
0
  if (addr < blank_t->offset + blank_t->size)
9107
0
    {
9108
      /* Extend the origin blank.  */
9109
0
      if (addr + len > blank_t->offset + blank_t->size)
9110
0
  blank_t->size = addr + len - blank_t->offset;
9111
0
    }
9112
0
  else
9113
0
    {
9114
0
      blank_t2 = create_nds32_elf_blank (addr, len);
9115
0
      if (!blank_t2)
9116
0
  return false;
9117
0
      if (blank_t->next)
9118
0
  {
9119
0
    blank_t->next->prev = blank_t2;
9120
0
    blank_t2->next = blank_t->next;
9121
0
  }
9122
0
      blank_t2->prev = blank_t;
9123
0
      blank_t->next = blank_t2;
9124
0
      *blank_p = blank_t2;
9125
0
    }
9126
9127
0
  return true;
9128
0
}
9129
9130
static bool
9131
insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9132
             bfd_vma len)
9133
0
{
9134
0
  nds32_elf_blank_t *blank_t;
9135
9136
0
  if (!insert_nds32_elf_blank (blank_p, addr, len))
9137
0
    return false;
9138
9139
0
  blank_t = *blank_p;
9140
9141
0
  if (!blank_t->prev)
9142
0
    {
9143
0
      blank_t->total_size = 0;
9144
0
      blank_t = blank_t->next;
9145
0
    }
9146
9147
0
  while (blank_t)
9148
0
    {
9149
0
      blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9150
0
      blank_t = blank_t->next;
9151
0
    }
9152
9153
0
  return true;
9154
0
}
9155
9156
static void
9157
calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9158
0
{
9159
0
  nds32_elf_blank_t *blank_t;
9160
0
  bfd_vma total_size = 0;
9161
9162
0
  if (!blank_p)
9163
0
    return;
9164
9165
0
  blank_t = blank_p;
9166
0
  while (blank_t->prev)
9167
0
    blank_t = blank_t->prev;
9168
0
  while (blank_t)
9169
0
    {
9170
0
      blank_t->total_size = total_size;
9171
0
      total_size += blank_t->size;
9172
0
      blank_t = blank_t->next;
9173
0
    }
9174
0
}
9175
9176
static bool
9177
nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9178
             nds32_elf_blank_t *blank_p)
9179
0
{
9180
0
  Elf_Internal_Shdr *symtab_hdr;  /* Symbol table header of this bfd.  */
9181
0
  Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd.  */
9182
0
  Elf_Internal_Sym *isymend;    /* Symbol entry iterator.  */
9183
0
  unsigned int sec_shndx;   /* The section the be relaxed.  */
9184
0
  bfd_byte *contents;     /* Contents data of iterating section.  */
9185
0
  Elf_Internal_Rela *internal_relocs;
9186
0
  Elf_Internal_Rela *irel;
9187
0
  Elf_Internal_Rela *irelend;
9188
0
  struct elf_link_hash_entry **sym_hashes;
9189
0
  struct elf_link_hash_entry **end_hashes;
9190
0
  unsigned int symcount;
9191
0
  asection *sect;
9192
0
  nds32_elf_blank_t *blank_t;
9193
0
  nds32_elf_blank_t *blank_t2;
9194
0
  nds32_elf_blank_t *blank_head;
9195
9196
0
  blank_head = blank_t = blank_p;
9197
0
  while (blank_head->prev != NULL)
9198
0
    blank_head = blank_head->prev;
9199
0
  while (blank_t->next != NULL)
9200
0
    blank_t = blank_t->next;
9201
9202
0
  if (blank_t->offset + blank_t->size <= sec->size)
9203
0
    {
9204
0
      blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9205
0
      blank_t->next->prev = blank_t;
9206
0
    }
9207
0
  if (blank_head->offset > 0)
9208
0
    {
9209
0
      blank_head->prev = create_nds32_elf_blank (0, 0);
9210
0
      blank_head->prev->next = blank_head;
9211
0
      blank_head = blank_head->prev;
9212
0
    }
9213
9214
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9215
9216
  /* The deletion must stop at the next ALIGN reloc for an alignment
9217
     power larger than the number of bytes we are deleting.  */
9218
9219
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9220
0
  if (!nds32_get_local_syms (abfd, sec, &isym))
9221
0
    return false;
9222
9223
0
  if (isym == NULL)
9224
0
    {
9225
0
      isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9226
0
           symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9227
0
      symtab_hdr->contents = (bfd_byte *) isym;
9228
0
    }
9229
9230
0
  if (isym == NULL || symtab_hdr->sh_info == 0)
9231
0
    return false;
9232
9233
0
  blank_t = blank_head;
9234
0
  calc_nds32_blank_total (blank_head);
9235
9236
0
  for (sect = abfd->sections; sect != NULL; sect = sect->next)
9237
0
    {
9238
      /* Adjust all the relocs.  */
9239
9240
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
9241
0
      internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9242
0
               true /* keep_memory */);
9243
0
      irelend = internal_relocs + sect->reloc_count;
9244
9245
0
      blank_t = blank_head;
9246
0
      blank_t2 = blank_head;
9247
9248
0
      if (!(sect->flags & SEC_RELOC))
9249
0
  continue;
9250
9251
0
      contents = NULL;
9252
0
      nds32_get_section_contents (abfd, sect, &contents, true);
9253
9254
0
      for (irel = internal_relocs; irel < irelend; irel++)
9255
0
  {
9256
0
    bfd_vma raddr;
9257
9258
0
    if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9259
0
        && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9260
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9261
0
      {
9262
0
        unsigned long val = 0;
9263
0
        unsigned long mask;
9264
0
        long before, between;
9265
0
        long offset = 0;
9266
9267
0
        switch (ELF32_R_TYPE (irel->r_info))
9268
0
    {
9269
0
    case R_NDS32_DIFF8:
9270
0
      offset = bfd_get_8 (abfd, contents + irel->r_offset);
9271
0
      break;
9272
0
    case R_NDS32_DIFF16:
9273
0
      offset = bfd_get_16 (abfd, contents + irel->r_offset);
9274
0
      break;
9275
0
    case R_NDS32_DIFF32:
9276
0
      val = bfd_get_32 (abfd, contents + irel->r_offset);
9277
      /* Get the signed bit and mask for the high part.  The
9278
         gcc will alarm when right shift 32-bit since the
9279
         type size of long may be 32-bit.  */
9280
0
      mask = 0 - (val >> 31);
9281
0
      if (mask)
9282
0
        offset = (val | (mask - 0xffffffff));
9283
0
      else
9284
0
        offset = val;
9285
0
      break;
9286
0
    default:
9287
0
      BFD_ASSERT (0);
9288
0
    }
9289
9290
        /*      DIFF value
9291
    0      |encoded in location|
9292
    |------------|-------------------|---------
9293
          sym+off(addend)
9294
    -- before ---| *****************
9295
    --------------------- between ---|
9296
9297
    We only care how much data are relax between DIFF,
9298
    marked as ***.  */
9299
9300
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9301
0
        between = get_nds32_elf_blank_total (&blank_t,
9302
0
               irel->r_addend + offset, 0);
9303
0
        if (between == before)
9304
0
    goto done_adjust_diff;
9305
9306
0
        switch (ELF32_R_TYPE (irel->r_info))
9307
0
    {
9308
0
    case R_NDS32_DIFF8:
9309
0
      bfd_put_8 (abfd, offset - (between - before),
9310
0
           contents + irel->r_offset);
9311
0
      break;
9312
0
    case R_NDS32_DIFF16:
9313
0
      bfd_put_16 (abfd, offset - (between - before),
9314
0
            contents + irel->r_offset);
9315
0
      break;
9316
0
    case R_NDS32_DIFF32:
9317
0
      bfd_put_32 (abfd, offset - (between - before),
9318
0
            contents + irel->r_offset);
9319
0
      break;
9320
0
    }
9321
0
      }
9322
0
    else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9323
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9324
0
      {
9325
0
        bfd_vma val = 0;
9326
0
        unsigned int len = 0;
9327
0
        unsigned long before, between;
9328
0
        bfd_byte *endp, *p;
9329
9330
0
        val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9331
0
                 &len);
9332
9333
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9334
0
        between = get_nds32_elf_blank_total (&blank_t,
9335
0
               irel->r_addend + val, 0);
9336
0
        if (between == before)
9337
0
    goto done_adjust_diff;
9338
9339
0
        p = contents + irel->r_offset;
9340
0
        endp = p + len -1;
9341
0
        memset (p, 0x80, len);
9342
0
        *(endp) = 0;
9343
0
        p = write_uleb128 (p, val - (between - before)) - 1;
9344
0
        if (p < endp)
9345
0
    *p |= 0x80;
9346
0
      }
9347
0
  done_adjust_diff:
9348
9349
0
    if (sec == sect)
9350
0
      {
9351
0
        raddr = irel->r_offset;
9352
0
        irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9353
0
                 irel->r_offset, 1);
9354
9355
0
        if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9356
0
    continue;
9357
0
        if (blank_t2 && blank_t2->next
9358
0
      && (blank_t2->offset > raddr
9359
0
          || blank_t2->next->offset <= raddr))
9360
0
    _bfd_error_handler
9361
0
      (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9362
0
       abfd);
9363
9364
        /* Mark reloc in deleted portion as NONE.
9365
     For some relocs like R_NDS32_LABEL that doesn't modify the
9366
     content in the section.  R_NDS32_LABEL doesn't belong to the
9367
     instruction in the section, so we should preserve it.  */
9368
0
        if (raddr >= blank_t2->offset
9369
0
      && raddr < blank_t2->offset + blank_t2->size
9370
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9371
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9372
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9373
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9374
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9375
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9376
0
    {
9377
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9378
0
                 R_NDS32_NONE);
9379
0
      continue;
9380
0
    }
9381
0
      }
9382
9383
0
    if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9384
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9385
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9386
0
      continue;
9387
9388
0
    if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9389
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9390
0
        && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9391
0
      {
9392
0
        if (irel->r_addend <= sec->size)
9393
0
    irel->r_addend -=
9394
0
      get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9395
0
      }
9396
0
  }
9397
0
    }
9398
9399
  /* Adjust the local symbols defined in this section.  */
9400
0
  blank_t = blank_head;
9401
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9402
0
    {
9403
0
      if (isym->st_shndx == sec_shndx)
9404
0
  {
9405
0
    if (isym->st_value <= sec->size)
9406
0
      {
9407
0
        bfd_vma ahead;
9408
0
        bfd_vma orig_addr = isym->st_value;
9409
9410
0
        ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9411
0
        isym->st_value -= ahead;
9412
9413
        /* Adjust function size.  */
9414
0
        if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9415
0
      && isym->st_size > 0)
9416
0
    isym->st_size -=
9417
0
      get_nds32_elf_blank_total
9418
0
      (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9419
0
      }
9420
0
  }
9421
0
    }
9422
9423
  /* Now adjust the global symbols defined in this section.  */
9424
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9425
0
        - symtab_hdr->sh_info);
9426
0
  sym_hashes = elf_sym_hashes (abfd);
9427
0
  end_hashes = sym_hashes + symcount;
9428
0
  blank_t = blank_head;
9429
0
  for (; sym_hashes < end_hashes; sym_hashes++)
9430
0
    {
9431
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
9432
9433
0
      if ((sym_hash->root.type == bfd_link_hash_defined
9434
0
     || sym_hash->root.type == bfd_link_hash_defweak)
9435
0
    && sym_hash->root.u.def.section == sec)
9436
0
  {
9437
0
    if (sym_hash->root.u.def.value <= sec->size)
9438
0
      {
9439
0
        bfd_vma ahead;
9440
0
        bfd_vma orig_addr = sym_hash->root.u.def.value;
9441
9442
0
        ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9443
0
        sym_hash->root.u.def.value -= ahead;
9444
9445
        /* Adjust function size.  */
9446
0
        if (sym_hash->type == STT_FUNC)
9447
0
    sym_hash->size -=
9448
0
      get_nds32_elf_blank_total
9449
0
      (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9450
9451
0
      }
9452
0
  }
9453
0
    }
9454
9455
0
  contents = elf_section_data (sec)->this_hdr.contents;
9456
0
  blank_t = blank_head;
9457
0
  while (blank_t->next)
9458
0
    {
9459
      /* Actually delete the bytes.  */
9460
9461
      /* If current blank is the last blank overlap with current section,
9462
   go to finish process.  */
9463
0
      if (sec->size <= (blank_t->next->offset))
9464
0
  break;
9465
9466
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9467
0
         contents + blank_t->offset + blank_t->size,
9468
0
         blank_t->next->offset - (blank_t->offset + blank_t->size));
9469
9470
0
      blank_t = blank_t->next;
9471
0
    }
9472
9473
0
  if (sec->size > (blank_t->offset + blank_t->size))
9474
0
    {
9475
      /* There are remaining code between blank and section boundary.
9476
   Move the remaining code to appropriate location.  */
9477
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9478
0
         contents + blank_t->offset + blank_t->size,
9479
0
         sec->size - (blank_t->offset + blank_t->size));
9480
0
      sec->size -= blank_t->total_size + blank_t->size;
9481
0
    }
9482
0
  else
9483
    /* This blank is not entirely included in the section,
9484
       reduce the section size by only part of the blank size.  */
9485
0
    sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9486
9487
0
  while (blank_head)
9488
0
    {
9489
0
      blank_t = blank_head;
9490
0
      blank_head = blank_head->next;
9491
0
      remove_nds32_elf_blank (blank_t);
9492
0
    }
9493
9494
0
  return true;
9495
0
}
9496
9497
/* Get the contents of a section.  */
9498
9499
static int
9500
nds32_get_section_contents (bfd *abfd, asection *sec,
9501
          bfd_byte **contents_p, bool cache)
9502
11
{
9503
  /* Get the section contents.  */
9504
11
  if (elf_section_data (sec)->this_hdr.contents != NULL)
9505
0
    *contents_p = elf_section_data (sec)->this_hdr.contents;
9506
11
  else
9507
11
    {
9508
11
      if (!bfd_get_full_section_contents (abfd, sec, contents_p))
9509
0
  return false;
9510
11
      if (cache)
9511
0
  elf_section_data (sec)->this_hdr.contents = *contents_p;
9512
11
    }
9513
9514
11
  return true;
9515
11
}
9516
9517
/* Get the contents of the internal symbol of abfd.  */
9518
9519
static int
9520
nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9521
          Elf_Internal_Sym **isymbuf_p)
9522
0
{
9523
0
  Elf_Internal_Shdr *symtab_hdr;
9524
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9525
9526
  /* Read this BFD's local symbols if we haven't done so already.  */
9527
0
  if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9528
0
    {
9529
0
      *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9530
0
      if (*isymbuf_p == NULL)
9531
0
  {
9532
0
    *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9533
0
               symtab_hdr->sh_info, 0,
9534
0
               NULL, NULL, NULL);
9535
0
    if (*isymbuf_p == NULL)
9536
0
      return false;
9537
0
  }
9538
0
    }
9539
0
  symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9540
9541
0
  return true;
9542
0
}
9543
9544
/* Range of small data.  */
9545
static bfd_vma sdata_range[2][2];
9546
static bfd_vma const sdata_init_range[2] =
9547
{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
9548
9549
static int
9550
nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9551
         bfd_byte *contents, bfd_vma addr)
9552
0
{
9553
0
  unsigned long insn = bfd_getb32 (contents + addr);
9554
9555
0
  if (insn & 0x80000000)
9556
0
    return 2;
9557
9558
0
  return 4;
9559
0
}
9560
9561
/* Set the gp relax range.  We have to measure the safe range
9562
   to do gp relaxation.  */
9563
9564
static void
9565
relax_range_measurement (bfd *abfd, struct bfd_link_info *link_info)
9566
0
{
9567
0
  asection *sec_f, *sec_b;
9568
  /* For upper bound.   */
9569
0
  bfd_vma maxpgsz;
9570
0
  bfd_vma align;
9571
0
  static int decide_relax_range = 0;
9572
0
  int i;
9573
0
  int range_number = ARRAY_SIZE (sdata_init_range);
9574
9575
0
  if (decide_relax_range)
9576
0
    return;
9577
0
  decide_relax_range = 1;
9578
9579
0
  if (sda_rela_sec == NULL)
9580
0
    {
9581
      /* Since there is no data sections, we assume the range is page size.  */
9582
0
      for (i = 0; i < range_number; i++)
9583
0
  {
9584
0
    sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9585
0
    sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9586
0
  }
9587
0
      return;
9588
0
    }
9589
9590
  /* Get the biggest alignment power after the gp located section.  */
9591
0
  sec_f = sda_rela_sec->output_section;
9592
0
  sec_b = sec_f->next;
9593
0
  align = 0;
9594
0
  while (sec_b != NULL)
9595
0
    {
9596
0
      if ((unsigned)(1 << sec_b->alignment_power) > align)
9597
0
  align = (1 << sec_b->alignment_power);
9598
0
      sec_b = sec_b->next;
9599
0
    }
9600
9601
0
  if (link_info != NULL)
9602
0
    maxpgsz = link_info->maxpagesize;
9603
0
  else
9604
0
    maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9605
  /* I guess we can not determine the section before
9606
     gp located section, so we assume the align is max page size.  */
9607
0
  for (i = 0; i < range_number; i++)
9608
0
    {
9609
0
      sdata_range[i][1] = sdata_init_range[i] - align;
9610
0
      BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9611
0
      sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9612
0
      BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9613
0
    }
9614
0
}
9615
9616
/* These are macros used to check flags encoded in r_addend.
9617
   They are only used by nds32_elf_relax_section ().  */
9618
0
#define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
9619
0
#define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
9620
#define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
9621
0
#define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
9622
9623
static const char * unrecognized_reloc_msg =
9624
  /* xgettext:c-format */
9625
  N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9626
9627
/* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
9628
9629
static bool
9630
nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9631
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9632
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9633
         Elf_Internal_Shdr *symtab_hdr)
9634
0
{
9635
  /* There are 3 variations for LONGCALL1
9636
     case 4-4-2; 16-bit on, optimize off or optimize for space
9637
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9638
     ori   ta, ta, lo12(symbol) ; LO12S0
9639
     jral5 ta     ;
9640
9641
     case 4-4-4; 16-bit off, optimize don't care
9642
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9643
     ori   ta, ta, lo12(symbol) ; LO12S0
9644
     jral  ta     ;
9645
9646
     case 4-4-4; 16-bit on, optimize for speed
9647
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9648
     ori   ta, ta, lo12(symbol) ; LO12S0
9649
     jral  ta     ;
9650
     Check code for -mlong-calls output.  */
9651
9652
  /* Get the reloc for the address from which the register is
9653
     being loaded.  This reloc will tell us which function is
9654
     actually being called.  */
9655
9656
0
  bfd_vma laddr;
9657
0
  int seq_len;  /* Original length of instruction sequence.  */
9658
0
  uint32_t insn;
9659
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9660
0
  bfd_signed_vma foff;
9661
0
  uint16_t insn16;
9662
9663
0
  irelend = internal_relocs + sec->reloc_count;
9664
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9665
0
  laddr = irel->r_offset;
9666
0
  *insn_len = seq_len;
9667
9668
0
  hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9669
0
             R_NDS32_HI20_RELA, laddr);
9670
0
  lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9671
0
             R_NDS32_LO12S0_ORI_RELA,
9672
0
             laddr + 4);
9673
9674
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9675
0
    {
9676
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9677
0
        (uint64_t) irel->r_offset);
9678
0
      return false;
9679
0
    }
9680
9681
  /* Get the value of the symbol referred to by the reloc.  */
9682
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9683
9684
  /* This condition only happened when symbol is undefined.  */
9685
0
  if (foff == 0
9686
0
      || foff < -CONSERVATIVE_24BIT_S1
9687
0
      || foff >= CONSERVATIVE_24BIT_S1)
9688
0
    return false;
9689
9690
  /* Relax to: jal symbol; 25_PCREL.  */
9691
  /* For simplicity of coding, we are going to modify the section
9692
     contents, the section relocs, and the BFD symbol table.  We
9693
     must tell the rest of the code not to free up this
9694
     information.  It would be possible to instead create a table
9695
     of changes which have to be made, as is done in coff-mips.c;
9696
     that would be more work, but would require less memory when
9697
     the linker is run.  */
9698
9699
  /* Replace the long call with a jal.  */
9700
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9701
0
             R_NDS32_25_PCREL_RELA);
9702
0
  irel->r_addend = hi_irelfn->r_addend;
9703
9704
  /* We don't resolve this here but resolve it in relocate_section.  */
9705
0
  insn = INSN_JAL;
9706
0
  bfd_putb32 (insn, contents + irel->r_offset);
9707
9708
0
  hi_irelfn->r_info =
9709
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9710
0
  lo_irelfn->r_info =
9711
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9712
0
  *insn_len = 4;
9713
9714
0
  if (seq_len & 0x2)
9715
0
    {
9716
0
      insn16 = NDS32_NOP16;
9717
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9718
0
      lo_irelfn->r_info =
9719
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9720
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9721
0
      *insn_len += 2;
9722
0
    }
9723
0
  return true;
9724
0
}
9725
9726
0
#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9727
/* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
9728
9729
static bool
9730
nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9731
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9732
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9733
         Elf_Internal_Shdr *symtab_hdr)
9734
0
{
9735
  /* bltz  rt, .L1   ; LONGCALL2
9736
     jal   symbol   ; 25_PCREL
9737
     .L1: */
9738
9739
  /* Get the reloc for the address from which the register is
9740
     being loaded.  This reloc will tell us which function is
9741
     actually being called.  */
9742
9743
0
  bfd_vma laddr;
9744
0
  uint32_t insn;
9745
0
  Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9746
0
  bfd_signed_vma foff;
9747
9748
0
  irelend = internal_relocs + sec->reloc_count;
9749
0
  laddr = irel->r_offset;
9750
0
  i1_irelfn =
9751
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9752
0
         R_NDS32_25_PCREL_RELA, laddr + 4);
9753
9754
0
  if (i1_irelfn == irelend)
9755
0
    {
9756
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9757
0
        (uint64_t) irel->r_offset);
9758
0
      return false;
9759
0
    }
9760
9761
0
  insn = bfd_getb32 (contents + laddr);
9762
9763
  /* Get the value of the symbol referred to by the reloc.  */
9764
0
  foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9765
9766
0
  if (foff == 0
9767
0
      || foff < -CONSERVATIVE_16BIT_S1
9768
0
      || foff >= CONSERVATIVE_16BIT_S1)
9769
0
    return false;
9770
9771
  /* Relax to bgezal   rt, label ; 17_PCREL
9772
     or   bltzal   rt, label ; 17_PCREL */
9773
9774
  /* Convert to complimentary conditional call.  */
9775
0
  insn = CONVERT_CONDITION_CALL (insn);
9776
9777
  /* For simplicity of coding, we are going to modify the section
9778
     contents, the section relocs, and the BFD symbol table.  We
9779
     must tell the rest of the code not to free up this
9780
     information.  It would be possible to instead create a table
9781
     of changes which have to be made, as is done in coff-mips.c;
9782
     that would be more work, but would require less memory when
9783
     the linker is run.  */
9784
9785
  /* Clean unnessary relocations.  */
9786
0
  i1_irelfn->r_info =
9787
0
    ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9788
0
  cond_irelfn =
9789
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9790
0
         R_NDS32_17_PCREL_RELA, laddr);
9791
0
  if (cond_irelfn != irelend)
9792
0
    cond_irelfn->r_info =
9793
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9794
9795
  /* Replace the long call with a bgezal.  */
9796
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9797
0
             R_NDS32_17_PCREL_RELA);
9798
0
  irel->r_addend = i1_irelfn->r_addend;
9799
9800
0
  bfd_putb32 (insn, contents + irel->r_offset);
9801
9802
0
  *insn_len = 4;
9803
0
  return true;
9804
0
}
9805
9806
/* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9807
9808
static bool
9809
nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9810
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9811
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9812
         Elf_Internal_Shdr *symtab_hdr)
9813
0
{
9814
  /* There are 3 variations for LONGCALL3
9815
     case 4-4-4-2; 16-bit on, optimize off or optimize for space
9816
     bltz  rt,   $1      ; LONGCALL3
9817
     sethi ta,   hi20(symbol)    ; HI20
9818
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9819
     jral5 ta        ;
9820
     $1
9821
9822
     case 4-4-4-4; 16-bit off, optimize don't care
9823
     bltz  rt,   $1      ; LONGCALL3
9824
     sethi ta,   hi20(symbol)    ; HI20
9825
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9826
     jral  ta        ;
9827
     $1
9828
9829
     case 4-4-4-4; 16-bit on, optimize for speed
9830
     bltz  rt,   $1      ; LONGCALL3
9831
     sethi ta,   hi20(symbol)    ; HI20
9832
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9833
     jral  ta        ;
9834
     $1 */
9835
9836
  /* Get the reloc for the address from which the register is
9837
     being loaded.  This reloc will tell us which function is
9838
     actually being called.  */
9839
9840
0
  bfd_vma laddr;
9841
0
  int seq_len;  /* Original length of instruction sequence.  */
9842
0
  uint32_t insn;
9843
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9844
0
  bfd_signed_vma foff;
9845
0
  uint16_t insn16;
9846
9847
0
  irelend = internal_relocs + sec->reloc_count;
9848
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9849
0
  laddr = irel->r_offset;
9850
0
  *insn_len = seq_len;
9851
9852
0
  hi_irelfn =
9853
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9854
0
         R_NDS32_HI20_RELA, laddr + 4);
9855
0
  lo_irelfn =
9856
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9857
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9858
9859
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9860
0
    {
9861
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9862
0
        (uint64_t) irel->r_offset);
9863
0
      return false;
9864
0
    }
9865
9866
  /* Get the value of the symbol referred to by the reloc.  */
9867
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9868
9869
0
  if (foff == 0
9870
0
      || foff < -CONSERVATIVE_24BIT_S1
9871
0
      || foff >= CONSERVATIVE_24BIT_S1)
9872
0
    return false;
9873
9874
0
  insn = bfd_getb32 (contents + laddr);
9875
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9876
0
    {
9877
      /* Relax to  bgezal   rt, label ; 17_PCREL
9878
   or    bltzal   rt, label ; 17_PCREL */
9879
9880
      /* Convert to complimentary conditional call.  */
9881
0
      insn = CONVERT_CONDITION_CALL (insn);
9882
0
      bfd_putb32 (insn, contents + irel->r_offset);
9883
9884
0
      *insn_len = 4;
9885
0
      irel->r_info =
9886
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9887
0
      hi_irelfn->r_info =
9888
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9889
0
      lo_irelfn->r_info =
9890
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9891
9892
0
      cond_irelfn =
9893
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
9894
0
             R_NDS32_17_PCREL_RELA, laddr);
9895
0
      if (cond_irelfn != irelend)
9896
0
  {
9897
0
    cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9898
0
                R_NDS32_17_PCREL_RELA);
9899
0
    cond_irelfn->r_addend = hi_irelfn->r_addend;
9900
0
  }
9901
9902
0
      if (seq_len & 0x2)
9903
0
  {
9904
0
    insn16 = NDS32_NOP16;
9905
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9906
0
    hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9907
0
              R_NDS32_INSN16);
9908
0
    hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9909
0
    insn_len += 2;
9910
0
  }
9911
0
    }
9912
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9913
0
    {
9914
      /* Relax to the following instruction sequence
9915
   bltz  rt,   $1 ; LONGCALL2
9916
   jal   symbol   ; 25_PCREL
9917
   $1 */
9918
0
      *insn_len = 8;
9919
0
      insn = INSN_JAL;
9920
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9921
9922
0
      hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9923
0
          R_NDS32_25_PCREL_RELA);
9924
0
      irel->r_info =
9925
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9926
9927
0
      lo_irelfn->r_info =
9928
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9929
9930
0
      if (seq_len & 0x2)
9931
0
  {
9932
0
    insn16 = NDS32_NOP16;
9933
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9934
0
    lo_irelfn->r_info =
9935
0
      ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9936
0
    lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9937
0
    insn_len += 2;
9938
0
  }
9939
0
    }
9940
0
  return true;
9941
0
}
9942
9943
/* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
9944
9945
static bool
9946
nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9947
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9948
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9949
         Elf_Internal_Shdr *symtab_hdr)
9950
0
{
9951
  /* There are 3 variations for LONGJUMP1
9952
     case 4-4-2; 16-bit bit on, optimize off or optimize for space
9953
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
9954
     ori   ta, ta, lo12(symbol)  ; LO12S0
9955
     jr5   ta      ;
9956
9957
     case 4-4-4; 16-bit off, optimize don't care
9958
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
9959
     ori   ta, ta, lo12(symbol)  ; LO12S0
9960
     jr    ta      ;
9961
9962
     case 4-4-4; 16-bit on, optimize for speed
9963
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
9964
     ori   ta, ta, lo12(symbol)  ; LO12S0
9965
     jr    ta      ;  */
9966
9967
  /* Get the reloc for the address from which the register is
9968
     being loaded.  This reloc will tell us which function is
9969
     actually being called.  */
9970
9971
0
  bfd_vma laddr;
9972
0
  int seq_len;  /* Original length of instruction sequence.  */
9973
0
  int insn16_on;  /* 16-bit on/off.  */
9974
0
  uint32_t insn;
9975
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9976
0
  bfd_signed_vma foff;
9977
0
  uint16_t insn16;
9978
0
  unsigned long reloc;
9979
9980
0
  irelend = internal_relocs + sec->reloc_count;
9981
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9982
0
  laddr = irel->r_offset;
9983
0
  *insn_len = seq_len;
9984
0
  insn16_on = IS_16BIT_ON (irel->r_addend);
9985
9986
0
  hi_irelfn =
9987
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9988
0
         R_NDS32_HI20_RELA, laddr);
9989
0
  lo_irelfn =
9990
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9991
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9992
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9993
0
    {
9994
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
9995
0
        (uint64_t) irel->r_offset);
9996
0
      return false;
9997
0
    }
9998
9999
  /* Get the value of the symbol referred to by the reloc.  */
10000
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10001
10002
0
  if (foff == 0
10003
0
      || foff >= CONSERVATIVE_24BIT_S1
10004
0
      || foff < -CONSERVATIVE_24BIT_S1)
10005
0
    return false;
10006
10007
0
  if (insn16_on
10008
0
      && foff >= -ACCURATE_8BIT_S1
10009
0
      && foff < ACCURATE_8BIT_S1
10010
0
      && (seq_len & 0x2))
10011
0
    {
10012
      /* j8 label */
10013
      /* 16-bit on, but not optimized for speed.  */
10014
0
      reloc = R_NDS32_9_PCREL_RELA;
10015
0
      insn16 = INSN_J8;
10016
0
      bfd_putb16 (insn16, contents + irel->r_offset);
10017
0
      *insn_len = 2;
10018
0
      irel->r_info =
10019
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10020
0
    }
10021
0
  else
10022
0
    {
10023
      /* j     label */
10024
0
      reloc = R_NDS32_25_PCREL_RELA;
10025
0
      insn = INSN_J;
10026
0
      bfd_putb32 (insn, contents + irel->r_offset);
10027
0
      *insn_len = 4;
10028
0
      irel->r_info =
10029
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10030
0
      irel->r_addend = 0;
10031
0
    }
10032
10033
0
  hi_irelfn->r_info =
10034
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10035
0
  lo_irelfn->r_info =
10036
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10037
10038
0
  if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10039
0
    {
10040
0
      insn16 = NDS32_NOP16;
10041
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10042
0
      lo_irelfn->r_info =
10043
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10044
0
          R_NDS32_INSN16);
10045
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10046
0
      *insn_len += 2;
10047
0
    }
10048
0
  return true;
10049
0
}
10050
10051
/* Revert condition branch.  This function does not check if the input
10052
   instruction is condition branch or not.  */
10053
10054
static void
10055
nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10056
         uint16_t *re_insn16, uint32_t *re_insn)
10057
0
{
10058
0
  uint32_t comp_insn = 0;
10059
0
  uint16_t comp_insn16 = 0;
10060
10061
0
  if (insn)
10062
0
    {
10063
0
      if (N32_OP6 (insn) == N32_OP6_BR1)
10064
0
  {
10065
    /* beqs label.  */
10066
0
    comp_insn = (insn ^ 0x4000) & 0xffffc000;
10067
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10068
0
      {
10069
        /* Insn can be contracted to 16-bit implied r5.  */
10070
0
        comp_insn16 =
10071
0
    (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10072
0
        comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10073
0
      }
10074
0
  }
10075
0
      else if (N32_OP6 (insn) == N32_OP6_BR3)
10076
0
  {
10077
    /* bnec $ta, imm11, label.  */
10078
0
    comp_insn = (insn ^ 0x80000) & 0xffffff00;
10079
0
  }
10080
0
      else
10081
0
  {
10082
0
    comp_insn = (insn ^ 0x10000) & 0xffffc000;
10083
0
    if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10084
0
        || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10085
0
      {
10086
0
        if (N32_IS_RT3 (insn))
10087
0
    {
10088
      /* Insn can be contracted to 16-bit.  */
10089
0
      comp_insn16 =
10090
0
        (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10091
0
      comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10092
0
    }
10093
0
        else if (N32_RT5 (insn) == REG_R15)
10094
0
    {
10095
      /* Insn can be contracted to 16-bit.  */
10096
0
      comp_insn16 =
10097
0
        (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10098
0
    }
10099
0
      }
10100
0
  }
10101
0
    }
10102
0
  else
10103
0
    {
10104
0
      switch ((insn16 & 0xf000) >> 12)
10105
0
  {
10106
0
  case 0xc:
10107
    /* beqz38 or bnez38 */
10108
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10109
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10110
0
    comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10111
0
    break;
10112
10113
0
  case 0xd:
10114
    /* beqs38 or bnes38 */
10115
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10116
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10117
0
    comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10118
0
      | (REG_R5 << 15);
10119
0
    break;
10120
10121
0
  case 0xe:
10122
    /* beqzS8 or bnezS8 */
10123
0
    comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10124
0
    comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10125
0
    comp_insn |= REG_R15 << 20;
10126
0
    break;
10127
10128
0
  default:
10129
0
    break;
10130
0
  }
10131
0
    }
10132
0
  if (comp_insn && re_insn)
10133
0
    *re_insn = comp_insn;
10134
0
  if (comp_insn16 && re_insn16)
10135
0
    *re_insn16 = comp_insn16;
10136
0
}
10137
10138
/* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
10139
10140
static bool
10141
nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10142
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10143
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10144
         Elf_Internal_Shdr *symtab_hdr)
10145
0
{
10146
  /* There are 3 variations for LONGJUMP2
10147
     case 2-4;  1st insn convertible, 16-bit on,
10148
     optimize off or optimize for space
10149
     bnes38  rt, ra, $1 ; LONGJUMP2
10150
     j       label      ; 25_PCREL
10151
     $1:
10152
10153
     case 4-4; 1st insn not convertible
10154
     bne  rt, ra, $1 ; LONGJUMP2
10155
     j    label      ; 25_PCREL
10156
     $1:
10157
10158
     case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10159
     bne  rt, ra, $1 ; LONGJUMP2
10160
     j    label      ; 25_PCREL
10161
     $1: */
10162
10163
  /* Get the reloc for the address from which the register is
10164
     being loaded.  This reloc will tell us which function is
10165
     actually being called.  */
10166
10167
0
  bfd_vma laddr;
10168
0
  int seq_len;  /* Original length of instruction sequence.  */
10169
0
  Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10170
0
  int first_size;
10171
0
  unsigned int i;
10172
0
  bfd_signed_vma foff;
10173
0
  uint32_t insn, re_insn = 0;
10174
0
  uint16_t insn16, re_insn16 = 0;
10175
0
  unsigned long reloc, cond_reloc;
10176
10177
0
  enum elf_nds32_reloc_type checked_types[] =
10178
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10179
10180
0
  irelend = internal_relocs + sec->reloc_count;
10181
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10182
0
  laddr = irel->r_offset;
10183
0
  *insn_len = seq_len;
10184
0
  first_size = (seq_len == 6) ? 2 : 4;
10185
10186
0
  i2_irelfn =
10187
0
    find_relocs_at_address_addr (irel, internal_relocs,
10188
0
         irelend, R_NDS32_25_PCREL_RELA,
10189
0
         laddr + first_size);
10190
10191
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10192
0
    {
10193
0
      cond_irelfn =
10194
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10195
0
             checked_types[i], laddr);
10196
0
      if (cond_irelfn != irelend)
10197
0
  break;
10198
0
    }
10199
10200
0
  if (i2_irelfn == irelend || cond_irelfn == irelend)
10201
0
    {
10202
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10203
0
        (uint64_t) irel->r_offset);
10204
0
      return false;
10205
0
    }
10206
10207
  /* Get the value of the symbol referred to by the reloc.  */
10208
0
  foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10209
0
  if (foff == 0
10210
0
      || foff < -CONSERVATIVE_16BIT_S1
10211
0
      || foff >= CONSERVATIVE_16BIT_S1)
10212
0
    return false;
10213
10214
  /* Get the all corresponding instructions.  */
10215
0
  if (first_size == 4)
10216
0
    {
10217
0
      insn = bfd_getb32 (contents + laddr);
10218
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10219
0
    }
10220
0
  else
10221
0
    {
10222
0
      insn16 = bfd_getb16 (contents + laddr);
10223
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10224
0
    }
10225
10226
0
  if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10227
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10228
0
    {
10229
0
      if (first_size == 4)
10230
0
  {
10231
    /* Don't convert it to 16-bit now, keep this as relaxable for
10232
       ``label reloc; INSN16''.  */
10233
10234
    /* Save comp_insn32 to buffer.  */
10235
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10236
0
    *insn_len = 4;
10237
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10238
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10239
0
    cond_reloc = R_NDS32_INSN16;
10240
0
  }
10241
0
      else
10242
0
  {
10243
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10244
0
    *insn_len = 2;
10245
0
    reloc = R_NDS32_9_PCREL_RELA;
10246
0
    cond_reloc = R_NDS32_NONE;
10247
0
  }
10248
0
    }
10249
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10250
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10251
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10252
0
    {
10253
      /* beqs     label    ; 15_PCREL */
10254
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10255
0
      *insn_len = 4;
10256
0
      reloc = R_NDS32_15_PCREL_RELA;
10257
0
      cond_reloc = R_NDS32_NONE;
10258
0
    }
10259
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10260
0
     && foff >= -CONSERVATIVE_16BIT_S1
10261
0
     && foff < CONSERVATIVE_16BIT_S1)
10262
0
    {
10263
      /* beqz     label ; 17_PCREL */
10264
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10265
0
      *insn_len = 4;
10266
0
      reloc = R_NDS32_17_PCREL_RELA;
10267
0
      cond_reloc = R_NDS32_NONE;
10268
0
    }
10269
0
  else
10270
0
    return false;
10271
10272
  /* Set all relocations.  */
10273
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10274
0
  irel->r_addend = i2_irelfn->r_addend;
10275
10276
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10277
0
              cond_reloc);
10278
0
  cond_irelfn->r_addend = 0;
10279
10280
0
  if ((seq_len ^ *insn_len ) & 0x2)
10281
0
    {
10282
0
      insn16 = NDS32_NOP16;
10283
0
      bfd_putb16 (insn16, contents + irel->r_offset + 4);
10284
0
      i2_irelfn->r_offset = 4;
10285
0
      i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10286
0
          R_NDS32_INSN16);
10287
0
      i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10288
0
      *insn_len += 2;
10289
0
    }
10290
0
  else
10291
0
    i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10292
0
              R_NDS32_NONE);
10293
0
  return true;
10294
0
}
10295
10296
/* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
10297
10298
static bool
10299
nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10300
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10301
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10302
         Elf_Internal_Shdr *symtab_hdr)
10303
0
{
10304
  /* There are 5 variations for LONGJUMP3
10305
     case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10306
     optimize off or optimize for space
10307
     bnes38   rt, ra, $1      ; LONGJUMP3
10308
     sethi    ta, hi20(symbol)      ; HI20
10309
     ori      ta, ta, lo12(symbol)  ; LO12S0
10310
     jr5      ta        ;
10311
     $1:          ;
10312
10313
     case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10314
     bnes38   rt, ra, $1     ; LONGJUMP3
10315
     sethi    ta, hi20(symbol)     ; HI20
10316
     ori      ta, ta, lo12(symbol) ; LO12S0
10317
     jr5      ta       ;
10318
     $1:         ; LABEL
10319
10320
     case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10321
     optimize off or optimize for space
10322
     bne   rt, ra, $1   ; LONGJUMP3
10323
     sethi ta, hi20(symbol) ; HI20
10324
     ori   ta, ta, lo12(symbol) ; LO12S0
10325
     jr5   ta     ;
10326
     $1:      ;
10327
10328
     case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10329
     16-bit off if no INSN16
10330
     bne   rt, ra, $1   ; LONGJUMP3
10331
     sethi ta, hi20(symbol) ; HI20
10332
     ori   ta, ta, lo12(symbol) ; LO12S0
10333
     jr    ta     ;
10334
     $1:      ;
10335
10336
     case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10337
     16-bit off if no INSN16
10338
     bne   rt, ra, $1   ; LONGJUMP3
10339
     sethi ta, hi20(symbol) ; HI20
10340
     ori   ta, ta, lo12(symbol) ; LO12S0
10341
     jr    ta     ;
10342
     $1:      ; LABEL */
10343
10344
  /* Get the reloc for the address from which the register is
10345
     being loaded.  This reloc will tell us which function is
10346
     actually being called.  */
10347
0
  enum elf_nds32_reloc_type checked_types[] =
10348
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10349
10350
0
  int reloc_off = 0, cond_removed = 0, convertible;
10351
0
  bfd_vma laddr;
10352
0
  int seq_len;  /* Original length of instruction sequence.  */
10353
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10354
0
  int first_size;
10355
0
  unsigned int i;
10356
0
  bfd_signed_vma foff;
10357
0
  uint32_t insn, re_insn = 0;
10358
0
  uint16_t insn16, re_insn16 = 0;
10359
0
  unsigned long reloc, cond_reloc;
10360
10361
0
  irelend = internal_relocs + sec->reloc_count;
10362
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10363
0
  laddr = irel->r_offset;
10364
0
  *insn_len = seq_len;
10365
10366
0
  convertible = IS_1ST_CONVERT (irel->r_addend);
10367
10368
0
  if (convertible)
10369
0
    first_size = 2;
10370
0
  else
10371
0
    first_size = 4;
10372
10373
  /* Get all needed relocations.  */
10374
0
  hi_irelfn =
10375
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10376
0
         R_NDS32_HI20_RELA, laddr + first_size);
10377
0
  lo_irelfn =
10378
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10379
0
         R_NDS32_LO12S0_ORI_RELA,
10380
0
         laddr + first_size + 4);
10381
10382
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10383
0
    {
10384
0
      cond_irelfn =
10385
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10386
0
             checked_types[i], laddr);
10387
0
      if (cond_irelfn != irelend)
10388
0
  break;
10389
0
    }
10390
10391
0
  if (hi_irelfn == irelend
10392
0
      || lo_irelfn == irelend
10393
0
      || cond_irelfn == irelend)
10394
0
    {
10395
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10396
0
        (uint64_t) irel->r_offset);
10397
0
      return false;
10398
0
    }
10399
10400
  /* Get the value of the symbol referred to by the reloc.  */
10401
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10402
10403
0
  if (foff == 0
10404
0
      || foff < -CONSERVATIVE_24BIT_S1
10405
0
      || foff >= CONSERVATIVE_24BIT_S1)
10406
0
    return false;
10407
10408
  /* Get the all corresponding instructions.  */
10409
0
  if (first_size == 4)
10410
0
    {
10411
0
      insn = bfd_getb32 (contents + laddr);
10412
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10413
0
    }
10414
0
  else
10415
0
    {
10416
0
      insn16 = bfd_getb16 (contents + laddr);
10417
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10418
0
    }
10419
10420
  /* For simplicity of coding, we are going to modify the section
10421
     contents, the section relocs, and the BFD symbol table.  We
10422
     must tell the rest of the code not to free up this
10423
     information.  It would be possible to instead create a table
10424
     of changes which have to be made, as is done in coff-mips.c;
10425
     that would be more work, but would require less memory when
10426
     the linker is run.  */
10427
10428
0
  if (re_insn16
10429
0
      && foff >= -ACCURATE_8BIT_S1 - first_size
10430
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10431
0
    {
10432
0
      if (!(seq_len & 0x2))
10433
0
  {
10434
    /* Don't convert it to 16-bit now, keep this as relaxable
10435
       for ``label reloc; INSN1a''6.  */
10436
    /* Save comp_insn32 to buffer.  */
10437
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10438
0
    *insn_len = 4;
10439
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10440
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10441
0
    cond_reloc = R_NDS32_INSN16;
10442
0
  }
10443
0
      else
10444
0
  {
10445
    /* Not optimize for speed; convert sequence to 16-bit.  */
10446
    /* Save comp_insn16 to buffer.  */
10447
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10448
0
    *insn_len = 2;
10449
0
    reloc = R_NDS32_9_PCREL_RELA;
10450
0
    cond_reloc = R_NDS32_NONE;
10451
0
  }
10452
0
      cond_removed = 1;
10453
0
    }
10454
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10455
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10456
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10457
0
    {
10458
      /* beqs     label    ; 15_PCREL */
10459
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10460
0
      *insn_len = 4;
10461
0
      reloc = R_NDS32_15_PCREL_RELA;
10462
0
      cond_reloc = R_NDS32_NONE;
10463
0
      cond_removed = 1;
10464
0
    }
10465
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10466
0
     && foff >= -CONSERVATIVE_16BIT_S1
10467
0
     && foff < CONSERVATIVE_16BIT_S1)
10468
0
    {
10469
      /* beqz     label ; 17_PCREL */
10470
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10471
0
      *insn_len = 4;
10472
0
      reloc = R_NDS32_17_PCREL_RELA;
10473
0
      cond_reloc = R_NDS32_NONE;
10474
0
      cond_removed = 1;
10475
0
    }
10476
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10477
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10478
0
    {
10479
      /* Relax to one of the following 3 variations
10480
10481
   case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10482
   for space
10483
   bnes38  rt, $1 ; LONGJUMP2
10484
   j       label  ; 25_PCREL
10485
   $1
10486
10487
   case 4-4; 1st insn not convertible, others don't care
10488
   bne   rt, ra, $1 ; LONGJUMP2
10489
   j     label      ; 25_PCREL
10490
   $1
10491
10492
   case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10493
   bne   rt, ra, $1 ; LONGJUMP2
10494
   j     label      ; 25_PCREL
10495
   $1 */
10496
10497
      /* Offset for first instruction.  */
10498
10499
      /* Use j label as second instruction.  */
10500
0
      *insn_len = 4 + first_size;
10501
0
      insn = INSN_J;
10502
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10503
0
      reloc = R_NDS32_LONGJUMP2;
10504
0
      cond_reloc = R_NDS32_25_PLTREL;
10505
0
    }
10506
0
    else
10507
0
      return false;
10508
10509
0
    if (cond_removed == 1)
10510
0
      {
10511
  /* Set all relocations.  */
10512
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10513
0
  irel->r_addend = hi_irelfn->r_addend;
10514
10515
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10516
0
              cond_reloc);
10517
0
  cond_irelfn->r_addend = 0;
10518
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10519
0
            R_NDS32_NONE);
10520
0
      }
10521
0
    else
10522
0
      {
10523
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10524
0
  irel->r_addend = irel->r_addend;
10525
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10526
0
            cond_reloc);
10527
0
      }
10528
10529
0
  if ((seq_len ^ *insn_len ) & 0x2)
10530
0
    {
10531
0
      insn16 = NDS32_NOP16;
10532
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10533
0
      lo_irelfn->r_offset = *insn_len;
10534
0
      lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10535
0
          R_NDS32_INSN16);
10536
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10537
0
      *insn_len += 2;
10538
0
    }
10539
0
  else
10540
0
    lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10541
0
              R_NDS32_NONE);
10542
0
  return true;
10543
0
}
10544
10545
/* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
10546
10547
static bool
10548
nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10549
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10550
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10551
         Elf_Internal_Shdr *symtab_hdr)
10552
0
{
10553
  /* The pattern for LONGCALL4.  Support for function cse.
10554
     sethi ta, hi20(symbol) ; LONGCALL4/HI20
10555
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10556
     jral  ta     ; PTR_RES/EMPTY/INSN16  */
10557
10558
0
  bfd_vma laddr;
10559
0
  uint32_t insn;
10560
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10561
0
  Elf_Internal_Rela *irelend;
10562
0
  bfd_signed_vma foff;
10563
10564
0
  irelend = internal_relocs + sec->reloc_count;
10565
0
  laddr = irel->r_offset;
10566
10567
  /* Get the reloc for the address from which the register is
10568
     being loaded.  This reloc will tell us which function is
10569
     actually being called.  */
10570
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10571
0
           R_NDS32_HI20_RELA, laddr);
10572
10573
0
  if (hi_irel == irelend)
10574
0
    {
10575
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10576
0
        (uint64_t) irel->r_offset);
10577
0
      return false;
10578
0
    }
10579
10580
  /* Get the value of the symbol referred to by the reloc.  */
10581
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10582
10583
  /* This condition only happened when symbol is undefined.  */
10584
0
  if (foff == 0
10585
0
      || foff < -CONSERVATIVE_24BIT_S1
10586
0
      || foff >= CONSERVATIVE_24BIT_S1)
10587
0
    return false;
10588
10589
  /* Relax to: jal symbol; 25_PCREL.  */
10590
  /* For simplicity of coding, we are going to modify the section
10591
     contents, the section relocs, and the BFD symbol table.  We
10592
     must tell the rest of the code not to free up this
10593
     information.  It would be possible to instead create a table
10594
     of changes which have to be made, as is done in coff-mips.c;
10595
     that would be more work, but would require less memory when
10596
     the linker is run.  */
10597
10598
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10599
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10600
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10601
0
            R_NDS32_EMPTY, irel->r_addend);
10602
10603
0
  if (ptr_irel == irelend || em_irel == irelend)
10604
0
    {
10605
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10606
0
        (uint64_t) irel->r_offset);
10607
0
      return false;
10608
0
    }
10609
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10610
0
  insn = bfd_getb32 (contents + irel->r_addend);
10611
0
  if (insn & 0x80000000)
10612
0
    return false;
10613
10614
  /* Replace the long call with a jal.  */
10615
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10616
0
          R_NDS32_25_PCREL_RELA);
10617
0
  ptr_irel->r_addend = 1;
10618
10619
  /* We don't resolve this here but resolve it in relocate_section.  */
10620
0
  insn = INSN_JAL;
10621
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10622
10623
0
  irel->r_info =
10624
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10625
10626
  /* If there is function cse, HI20 can not remove now.  */
10627
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10628
0
             R_NDS32_LONGCALL4, laddr);
10629
0
  if (call_irel == irelend)
10630
0
    {
10631
0
      *insn_len = 0;
10632
0
      hi_irel->r_info =
10633
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10634
0
    }
10635
10636
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10637
0
            R_NDS32_INSN16, irel->r_addend);
10638
0
  if (insn_irel != irelend)
10639
0
    insn_irel->r_info =
10640
0
      ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10641
10642
0
  return true;
10643
0
}
10644
10645
/* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
10646
10647
static bool
10648
nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10649
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10650
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10651
         Elf_Internal_Shdr *symtab_hdr)
10652
0
{
10653
  /* The pattern for LONGCALL5.
10654
     bltz  rt, .L1  ; LONGCALL5/17_PCREL
10655
     jal   symbol ; 25_PCREL
10656
     .L1:  */
10657
10658
0
  bfd_vma laddr;
10659
0
  uint32_t insn;
10660
0
  Elf_Internal_Rela *cond_irel, *irelend;
10661
0
  bfd_signed_vma foff;
10662
10663
0
  irelend = internal_relocs + sec->reloc_count;
10664
0
  laddr = irel->r_offset;
10665
0
  insn = bfd_getb32 (contents + laddr);
10666
10667
  /* Get the reloc for the address from which the register is
10668
     being loaded.  This reloc will tell us which function is
10669
     actually being called.  */
10670
0
  cond_irel =
10671
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10672
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
10673
0
  if (cond_irel == irelend)
10674
0
    {
10675
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10676
0
        (uint64_t) irel->r_offset);
10677
0
      return false;
10678
0
    }
10679
10680
  /* Get the value of the symbol referred to by the reloc.  */
10681
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10682
10683
0
  if (foff == 0
10684
0
      || foff < -CONSERVATIVE_16BIT_S1
10685
0
      || foff >= CONSERVATIVE_16BIT_S1)
10686
0
    return false;
10687
10688
  /* Relax to bgezal   rt, label ; 17_PCREL
10689
     or   bltzal   rt, label ; 17_PCREL.  */
10690
10691
  /* Convert to complimentary conditional call.  */
10692
0
  insn = CONVERT_CONDITION_CALL (insn);
10693
10694
  /* For simplicity of coding, we are going to modify the section
10695
     contents, the section relocs, and the BFD symbol table.  We
10696
     must tell the rest of the code not to free up this
10697
     information.  It would be possible to instead create a table
10698
     of changes which have to be made, as is done in coff-mips.c;
10699
     that would be more work, but would require less memory when
10700
     the linker is run.  */
10701
10702
  /* Modify relocation and contents.  */
10703
0
  cond_irel->r_info =
10704
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10705
10706
  /* Replace the long call with a bgezal.  */
10707
0
  bfd_putb32 (insn, contents + cond_irel->r_offset);
10708
0
  *insn_len = 0;
10709
10710
  /* Clean unnessary relocations.  */
10711
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10712
10713
0
  cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10714
0
             R_NDS32_17_PCREL_RELA, laddr);
10715
0
  cond_irel->r_info =
10716
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10717
10718
0
  return true;
10719
0
}
10720
10721
/* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
10722
10723
static bool
10724
nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10725
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10726
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10727
         Elf_Internal_Shdr *symtab_hdr)
10728
0
{
10729
  /* The pattern for LONGCALL6.
10730
     bltz  rt,   .L1      ; LONGCALL6/17_PCREL
10731
     sethi ta,   hi20(symbol)   ; HI20/PTR
10732
     ori   ta, ta,  lo12(symbol)  ; LO12S0_ORI/PTR
10733
     jral  ta       ; PTR_RES/EMPTY/INSN16
10734
     .L1  */
10735
10736
0
  bfd_vma laddr;
10737
0
  uint32_t insn;
10738
0
  Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10739
0
  bfd_signed_vma foff;
10740
10741
0
  irelend = internal_relocs + sec->reloc_count;
10742
0
  laddr = irel->r_offset;
10743
10744
  /* Get the reloc for the address from which the register is
10745
     being loaded.  This reloc will tell us which function is
10746
     actually being called.  */
10747
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10748
0
           R_NDS32_EMPTY, irel->r_addend);
10749
10750
0
  if (em_irel == irelend)
10751
0
    {
10752
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10753
0
        (uint64_t) irel->r_offset);
10754
0
      return false;
10755
0
    }
10756
10757
  /* Get the value of the symbol referred to by the reloc.  */
10758
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10759
10760
0
  if (foff == 0
10761
0
      || foff < -CONSERVATIVE_24BIT_S1
10762
0
      || foff >= CONSERVATIVE_24BIT_S1)
10763
0
    return false;
10764
10765
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10766
0
  insn = bfd_getb32 (contents + irel->r_addend);
10767
0
  if (insn & 0x80000000)
10768
0
    return false;
10769
10770
0
  insn = bfd_getb32 (contents + laddr);
10771
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10772
0
    {
10773
      /* Relax to  bgezal   rt, label ; 17_PCREL
10774
   or    bltzal   rt, label ; 17_PCREL.  */
10775
10776
      /* Convert to complimentary conditional call.  */
10777
0
      *insn_len = 0;
10778
0
      insn = CONVERT_CONDITION_CALL (insn);
10779
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10780
10781
0
      em_irel->r_info =
10782
0
  ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10783
10784
      /* Set resolved relocation.  */
10785
0
      cond_irel =
10786
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10787
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10788
0
      if (cond_irel == irelend)
10789
0
  {
10790
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10791
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10792
0
    return false;
10793
0
  }
10794
0
      cond_irel->r_addend = 1;
10795
10796
      /* Clear relocations.  */
10797
10798
0
      irel->r_info =
10799
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10800
10801
0
      cond_irel =
10802
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10803
0
             R_NDS32_17_PCREL_RELA, laddr);
10804
0
      if (cond_irel != irelend)
10805
0
  cond_irel->r_info =
10806
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10807
10808
0
      cond_irel =
10809
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10810
0
             R_NDS32_INSN16, irel->r_addend);
10811
0
      if (cond_irel != irelend)
10812
0
  cond_irel->r_info =
10813
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10814
10815
0
    }
10816
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10817
0
    {
10818
      /* Relax to the following instruction sequence
10819
   bltz  rt, .L1  ; LONGCALL2/17_PCREL
10820
   jal   symbol ; 25_PCREL/PTR_RES
10821
   .L1  */
10822
0
      *insn_len = 4;
10823
      /* Convert instruction.  */
10824
0
      insn = INSN_JAL;
10825
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10826
10827
      /* Convert relocations.  */
10828
0
      em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10829
0
              R_NDS32_25_PCREL_RELA);
10830
0
      irel->r_info =
10831
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10832
10833
      /* Set resolved relocation.  */
10834
0
      cond_irel =
10835
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10836
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10837
0
      if (cond_irel == irelend)
10838
0
  {
10839
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10840
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10841
0
    return false;
10842
0
  }
10843
0
      cond_irel->r_addend = 1;
10844
10845
0
      cond_irel =
10846
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10847
0
             R_NDS32_INSN16, irel->r_addend);
10848
0
      if (cond_irel != irelend)
10849
0
  cond_irel->r_info =
10850
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10851
0
    }
10852
0
  return true;
10853
0
}
10854
10855
/* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10856
10857
static bool
10858
nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10859
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10860
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10861
         Elf_Internal_Shdr *symtab_hdr)
10862
0
{
10863
  /* The pattern for LONGJUMP4.
10864
     sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10865
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10866
     jr    ta     ; PTR_RES/INSN16/EMPTY  */
10867
10868
0
  bfd_vma laddr;
10869
0
  int seq_len;  /* Original length of instruction sequence.  */
10870
0
  uint32_t insn;
10871
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10872
0
  bfd_signed_vma foff;
10873
10874
0
  irelend = internal_relocs + sec->reloc_count;
10875
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10876
0
  laddr = irel->r_offset;
10877
0
  *insn_len = seq_len;
10878
10879
  /* Get the reloc for the address from which the register is
10880
     being loaded.  This reloc will tell us which function is
10881
     actually being called.  */
10882
10883
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10884
0
           R_NDS32_HI20_RELA, laddr);
10885
10886
0
  if (hi_irel == irelend)
10887
0
    {
10888
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10889
0
        (uint64_t) irel->r_offset);
10890
0
      return false;
10891
0
    }
10892
10893
  /* Get the value of the symbol referred to by the reloc.  */
10894
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10895
10896
0
  if (foff == 0
10897
0
      || foff >= CONSERVATIVE_24BIT_S1
10898
0
      || foff < -CONSERVATIVE_24BIT_S1)
10899
0
    return false;
10900
10901
  /* Convert it to "j label", it may be converted to j8 in the final
10902
     pass of relaxation.  Therefore, we do not consider this currently.  */
10903
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10904
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10905
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10906
0
           R_NDS32_EMPTY, irel->r_addend);
10907
10908
0
  if (ptr_irel == irelend || em_irel == irelend)
10909
0
    {
10910
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10911
0
        (uint64_t) irel->r_offset);
10912
0
      return false;
10913
0
    }
10914
10915
0
  em_irel->r_info =
10916
0
    ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10917
0
  ptr_irel->r_addend = 1;
10918
10919
  /* Write instruction.  */
10920
0
  insn = INSN_J;
10921
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10922
10923
  /* Clear relocations.  */
10924
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10925
10926
  /* If there is function cse, HI20 can not remove now.  */
10927
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10928
0
             R_NDS32_LONGJUMP4, laddr);
10929
0
  if (call_irel == irelend)
10930
0
    {
10931
0
      *insn_len = 0;
10932
0
      hi_irel->r_info =
10933
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10934
0
    }
10935
10936
0
  return true;
10937
0
}
10938
10939
/* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
10940
10941
static bool
10942
nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10943
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10944
         int *seq_len, bfd_byte *contents,
10945
         Elf_Internal_Sym *isymbuf,
10946
         Elf_Internal_Shdr *symtab_hdr)
10947
0
{
10948
  /* There are 2 variations for LONGJUMP5
10949
     case 2-4;  1st insn convertible, 16-bit on.
10950
     bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
10951
     j       label    ; 25_PCREL/INSN16
10952
     $1:
10953
10954
     case 4-4; 1st insn not convertible
10955
     bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10956
     j    label   ; 25_PCREL/INSN16
10957
     .L1:  */
10958
10959
0
  bfd_vma laddr;
10960
0
  Elf_Internal_Rela *cond_irel,  *irelend;
10961
0
  unsigned int i;
10962
0
  bfd_signed_vma foff;
10963
0
  uint32_t insn, re_insn = 0;
10964
0
  uint16_t insn16, re_insn16 = 0;
10965
0
  unsigned long reloc;
10966
10967
0
  enum elf_nds32_reloc_type checked_types[] =
10968
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10969
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10970
10971
0
  irelend = internal_relocs + sec->reloc_count;
10972
0
  laddr = irel->r_offset;
10973
10974
  /* Get the reloc for the address from which the register is
10975
     being loaded.  This reloc will tell us which function is
10976
     actually being called.  */
10977
10978
0
  cond_irel =
10979
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10980
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
10981
0
  if (cond_irel == irelend)
10982
0
    {
10983
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10984
0
        (uint64_t) irel->r_offset);
10985
0
      return false;
10986
0
    }
10987
10988
  /* Get the value of the symbol referred to by the reloc.  */
10989
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10990
10991
0
  if (foff == 0
10992
0
      || foff < -CONSERVATIVE_16BIT_S1
10993
0
      || foff >= CONSERVATIVE_16BIT_S1)
10994
0
    return false;
10995
10996
  /* Get the all corresponding instructions.  */
10997
0
  insn = bfd_getb32 (contents + laddr);
10998
  /* Check instruction size.  */
10999
0
  if (insn & 0x80000000)
11000
0
    {
11001
0
      *seq_len = 0;
11002
0
      insn16 = insn >> 16;
11003
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11004
0
    }
11005
0
  else
11006
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11007
11008
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11009
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11010
0
    {
11011
      /* beqs label ; 15_PCREL.  */
11012
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11013
0
      reloc = R_NDS32_15_PCREL_RELA;
11014
0
    }
11015
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11016
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11017
0
    {
11018
      /* beqz label ; 17_PCREL.  */
11019
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11020
0
      reloc = R_NDS32_17_PCREL_RELA;
11021
0
    }
11022
0
  else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11023
0
     && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11024
0
    {
11025
      /* beqc label ; 9_PCREL.  */
11026
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11027
0
      reloc = R_NDS32_WORD_9_PCREL_RELA;
11028
0
    }
11029
0
  else
11030
0
    return false;
11031
11032
  /* Set all relocations.  */
11033
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11034
11035
  /* Clean relocations.  */
11036
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11037
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11038
0
    {
11039
0
      cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11040
0
                 checked_types[i], laddr);
11041
0
      if (cond_irel != irelend)
11042
0
  {
11043
0
    if (*seq_len == 0
11044
0
        && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11045
0
      {
11046
        /* If the branch instruction is 2 byte, it cannot remove
11047
     directly.  Only convert it to nop16 and remove it after
11048
     checking alignment issue.  */
11049
0
        insn16 = NDS32_NOP16;
11050
0
        bfd_putb16 (insn16, contents + laddr);
11051
0
        cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11052
0
      }
11053
0
    else
11054
0
      cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11055
0
                R_NDS32_NONE);
11056
0
  }
11057
0
    }
11058
0
  *insn_len = 0;
11059
11060
0
  return true;
11061
0
}
11062
11063
/* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
11064
11065
static bool
11066
nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11067
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11068
         int *seq_len, bfd_byte *contents,
11069
         Elf_Internal_Sym *isymbuf,
11070
         Elf_Internal_Shdr *symtab_hdr)
11071
0
{
11072
  /* There are 5 variations for LONGJUMP6
11073
     case : 2-4-4-4; 1st insn convertible, 16-bit on.
11074
     bnes38   rt, ra, .L1   ; LONGJUMP6/15_PCREL/INSN16
11075
     sethi    ta, hi20(symbol)    ; HI20/PTR
11076
     ori      ta, ta, lo12(symbol)  ; LO12S0_ORI/PTR
11077
     jr       ta      ; PTR_RES/INSN16/EMPTY
11078
     .L1:
11079
11080
     case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11081
     bne   rt, ra, .L1    ; LONGJUMP6/15_PCREL/INSN16
11082
     sethi ta, hi20(symbol) ; HI20/PTR
11083
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11084
     jr    ta     ; PTR_RES/INSN16/EMPTY
11085
     .L1:  */
11086
11087
0
  enum elf_nds32_reloc_type checked_types[] =
11088
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11089
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11090
11091
0
  int reloc_off = 0, cond_removed = 0;
11092
0
  bfd_vma laddr;
11093
0
  Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11094
0
  unsigned int i;
11095
0
  bfd_signed_vma foff;
11096
0
  uint32_t insn, re_insn = 0;
11097
0
  uint16_t insn16, re_insn16 = 0;
11098
0
  unsigned long reloc;
11099
11100
0
  irelend = internal_relocs + sec->reloc_count;
11101
0
  laddr = irel->r_offset;
11102
11103
  /* Get the reloc for the address from which the register is
11104
     being loaded.  This reloc will tell us which function is
11105
     actually being called.  */
11106
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11107
0
           R_NDS32_EMPTY, irel->r_addend);
11108
11109
0
  if (em_irel == irelend)
11110
0
    {
11111
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11112
0
        (uint64_t) irel->r_offset);
11113
0
      return false;
11114
0
    }
11115
11116
  /* Get the value of the symbol referred to by the reloc.  */
11117
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11118
11119
0
  if (foff == 0
11120
0
      || foff < -CONSERVATIVE_24BIT_S1
11121
0
      || foff >= CONSERVATIVE_24BIT_S1)
11122
0
    return false;
11123
11124
0
  insn = bfd_getb32 (contents + laddr);
11125
  /* Check instruction size.  */
11126
0
  if (insn & 0x80000000)
11127
0
    {
11128
0
      *seq_len = 0;
11129
0
      insn16 = insn >> 16;
11130
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11131
0
    }
11132
0
  else
11133
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11134
11135
  /* For simplicity of coding, we are going to modify the section
11136
     contents, the section relocs, and the BFD symbol table.  We
11137
     must tell the rest of the code not to free up this
11138
     information.  It would be possible to instead create a table
11139
     of changes which have to be made, as is done in coff-mips.c;
11140
     that would be more work, but would require less memory when
11141
     the linker is run.  */
11142
11143
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11144
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11145
0
    {
11146
      /* beqs     label    ; 15_PCREL.  */
11147
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11148
0
      reloc = R_NDS32_15_PCREL_RELA;
11149
0
      cond_removed = 1;
11150
0
    }
11151
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11152
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11153
0
    {
11154
      /* beqz     label ; 17_PCREL.  */
11155
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11156
0
      reloc = R_NDS32_17_PCREL_RELA;
11157
0
      cond_removed = 1;
11158
0
    }
11159
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11160
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11161
0
    {
11162
      /* Relax to one of the following 2 variations
11163
11164
   case 2-4;  1st insn convertible, 16-bit on.
11165
   bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11166
   j       label    ; 25_PCREL/INSN16
11167
   $1:
11168
11169
   case 4-4; 1st insn not convertible
11170
   bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11171
   j    label   ; 25_PCREL/INSN16
11172
   .L1:  */
11173
11174
      /* Use j label as second instruction.  */
11175
0
      insn = INSN_J;
11176
0
      reloc = R_NDS32_25_PCREL_RELA;
11177
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
11178
0
    }
11179
0
  else
11180
0
    return false;
11181
11182
  /* Set all relocations.  */
11183
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11184
11185
0
  cond_irel =
11186
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11187
0
         R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11188
0
  cond_irel->r_addend = 1;
11189
11190
  /* Use INSN16 of first branch instruction to distinguish if keeping
11191
     INSN16 of final instruction or not.  */
11192
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11193
0
             R_NDS32_INSN16, irel->r_offset);
11194
0
  if (insn_irel == irelend)
11195
0
    {
11196
      /* Clean the final INSN16.  */
11197
0
      insn_irel =
11198
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
11199
0
             R_NDS32_INSN16, em_irel->r_offset);
11200
0
      insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11201
0
          R_NDS32_NONE);
11202
0
    }
11203
11204
0
  if (cond_removed == 1)
11205
0
    {
11206
0
      *insn_len = 0;
11207
11208
      /* Clear relocations.  */
11209
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11210
11211
0
      for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11212
0
  {
11213
0
    cond_irel =
11214
0
      find_relocs_at_address_addr (irel, internal_relocs, irelend,
11215
0
           checked_types[i], laddr);
11216
0
    if (cond_irel != irelend)
11217
0
      {
11218
0
        if (*seq_len == 0
11219
0
      && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11220
0
    {
11221
      /* If the branch instruction is 2 byte, it cannot remove
11222
         directly.  Only convert it to nop16 and remove it after
11223
         checking alignment issue.  */
11224
0
      insn16 = NDS32_NOP16;
11225
0
      bfd_putb16 (insn16, contents + laddr);
11226
0
      cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11227
0
    }
11228
0
        else
11229
0
    cond_irel->r_info =
11230
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11231
0
      }
11232
0
  }
11233
0
    }
11234
0
  else
11235
0
    {
11236
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11237
0
           R_NDS32_LONGJUMP5);
11238
0
    }
11239
11240
0
  return true;
11241
0
}
11242
11243
/* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
11244
11245
static bool
11246
nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11247
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11248
         int *seq_len, bfd_byte *contents,
11249
         Elf_Internal_Sym *isymbuf,
11250
         Elf_Internal_Shdr *symtab_hdr)
11251
0
{
11252
  /* There are 2 variations for LONGJUMP5
11253
     case 2-4;  1st insn convertible, 16-bit on.
11254
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11255
     beq     rt, ta, label  ; 15_PCREL
11256
11257
     case 4-4; 1st insn not convertible
11258
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11259
     beq     rt, ta, label  ; 15_PCREL  */
11260
11261
0
  bfd_vma laddr;
11262
0
  Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
11263
0
  bfd_signed_vma foff;
11264
0
  uint32_t insn, re_insn = 0;
11265
0
  uint16_t insn16;
11266
0
  uint32_t imm11;
11267
11268
0
  irelend = internal_relocs + sec->reloc_count;
11269
0
  laddr = irel->r_offset;
11270
11271
  /* Get the reloc for the address from which the register is
11272
     being loaded.  This reloc will tell us which function is
11273
     actually being called.  */
11274
11275
0
  cond_irel =
11276
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11277
0
         R_NDS32_15_PCREL_RELA, irel->r_addend);
11278
0
  if (cond_irel == irelend)
11279
0
    {
11280
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11281
0
        (uint64_t) irel->r_offset);
11282
0
      return false;
11283
0
    }
11284
11285
  /* Get the value of the symbol referred to by the reloc.  */
11286
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11287
11288
0
  if (foff == 0
11289
0
      || foff < -CONSERVATIVE_8BIT_S1
11290
0
      || foff >= CONSERVATIVE_8BIT_S1)
11291
0
    return false;
11292
11293
  /* Get the first instruction for its size.  */
11294
0
  insn = bfd_getb32 (contents + laddr);
11295
0
  if (insn & 0x80000000)
11296
0
    {
11297
0
      *seq_len = 0;
11298
      /* Get the immediate from movi55.  */
11299
0
      imm11 = N16_IMM5S (insn >> 16);
11300
0
    }
11301
0
  else
11302
0
    {
11303
      /* Get the immediate from movi.  */
11304
0
      imm11 = N32_IMM20S (insn);
11305
0
    }
11306
11307
  /* Get the branch instruction.  */
11308
0
  insn = bfd_getb32 (contents + irel->r_addend);
11309
  /* Convert instruction to BR3.  */
11310
0
  if ((insn >> 14) & 0x1)
11311
0
    re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11312
0
  else
11313
0
    re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11314
11315
0
  bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11316
11317
  /* Set all relocations.  */
11318
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11319
0
            R_NDS32_WORD_9_PCREL_RELA);
11320
11321
  /* Clean relocations.  */
11322
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11323
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11324
0
             R_NDS32_INSN16, irel->r_offset);
11325
0
  if (insn_irel != irelend)
11326
0
    {
11327
0
      if (*seq_len == 0)
11328
0
  {
11329
    /* If the first insntruction is 16bit, convert it to nop16.  */
11330
0
    insn16 = NDS32_NOP16;
11331
0
    bfd_putb16 (insn16, contents + laddr);
11332
0
    insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11333
0
  }
11334
0
      else
11335
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11336
0
            R_NDS32_NONE);
11337
0
    }
11338
0
  *insn_len = 0;
11339
11340
0
  return true;
11341
0
}
11342
11343
/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11344
   for each relax round. But the gp may changed dramatically and then cause
11345
   the truncated to fit errors for the the converted gp instructions.
11346
   Therefore, we must reserve the minimum but safe enough size to prevent it.  */
11347
11348
static bool
11349
nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11350
           Elf_Internal_Rela *irel, bool *again,
11351
           bool init,
11352
           struct elf_nds32_link_hash_table *table,
11353
           Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11354
11355
0
{
11356
0
  int offset_to_gp;
11357
0
  static bool sec_pass = false;
11358
0
  static asection *first_sec = NULL, *sym_sec;
11359
  /* Record the number of instructions which may be removed.  */
11360
0
  static int count = 0, record_count;
11361
0
  Elf_Internal_Sym *isym;
11362
0
  struct elf_link_hash_entry *h = NULL;
11363
0
  int indx;
11364
0
  unsigned long r_symndx;
11365
0
  bfd *abfd = sec->owner;
11366
0
  static bfd_vma record_sda = 0;
11367
0
  int sda_offset = 0;
11368
11369
  /* Force doing relaxation when hyper-relax is high.  */
11370
0
  if (table->hyper_relax == 2)
11371
0
    return true;
11372
11373
  /* Do not relax the load/store patterns for the first
11374
     relax round.  */
11375
0
  if (init)
11376
0
    {
11377
0
      if (!first_sec)
11378
0
  first_sec = sec;
11379
0
      else if (first_sec == sec)
11380
0
  {
11381
0
    record_count = count;
11382
0
    count = 0;
11383
0
    sec_pass = true;
11384
0
  }
11385
11386
0
      if (!sec_pass)
11387
0
  *again = true;
11388
11389
0
      return true;
11390
0
    }
11391
11392
  /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11393
     DATA_SEGMENT_ALIGN size in the linker script may make it
11394
     get even bigger.  */
11395
0
  if (record_sda == 0)
11396
0
    record_sda = local_sda;
11397
0
  else if (local_sda > record_sda)
11398
0
    sda_offset = local_sda - record_sda;
11399
11400
  /* Assume the instruction will be removed in the best case.  */
11401
0
  count++;
11402
11403
  /* We record the offset to gp for each symbol, and then check
11404
     if it is changed dramatically after relaxing.
11405
     (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11406
     (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx].  */
11407
0
  r_symndx = ELF32_R_SYM (irel->r_info);
11408
0
  if (r_symndx >= symtab_hdr->sh_info)
11409
0
    {
11410
      /* Global symbols.  */
11411
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11412
0
      h = elf_sym_hashes (abfd)[indx];
11413
0
      sym_sec = h->root.u.def.section;
11414
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags)
11415
0
    || bfd_is_abs_section (sym_sec))
11416
0
  {
11417
    /* Forbid doing relaxation when hyper-relax is low.  */
11418
0
    if (table->hyper_relax == 0)
11419
0
      return false;
11420
11421
0
    offset_to_gp = *access_addr - local_sda;
11422
0
    if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11423
0
      elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11424
0
    else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11425
0
       < abs (offset_to_gp) - sda_offset)
11426
0
      {
11427
        /* This may cause the error, so we reserve the
11428
     safe enough size for relaxing.  */
11429
0
        if (*access_addr >= local_sda)
11430
0
    *access_addr += (record_count * 4);
11431
0
        else
11432
0
    *access_addr -= (record_count * 4);
11433
0
      }
11434
0
    return sec_pass;
11435
0
  }
11436
0
    }
11437
0
  else
11438
0
    {
11439
      /* Local symbols.  */
11440
0
      if (!elf32_nds32_allocate_local_sym_info (abfd))
11441
0
  return false;
11442
0
      isym = isymbuf + r_symndx;
11443
11444
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11445
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags))
11446
0
  {
11447
    /* Forbid doing relaxation when hyper-relax is low.  */
11448
0
    if (table->hyper_relax == 0)
11449
0
      return false;
11450
11451
0
    offset_to_gp = *access_addr - local_sda;
11452
0
    if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11453
0
      elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11454
0
    else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11455
0
       < abs (offset_to_gp) - sda_offset)
11456
0
      {
11457
        /* This may cause the error, so we reserve the
11458
     safe enough size for relaxing.  */
11459
0
        if (*access_addr >= local_sda)
11460
0
    *access_addr += (record_count * 4);
11461
0
        else
11462
0
    *access_addr -= (record_count * 4);
11463
0
      }
11464
0
    return sec_pass;
11465
0
  }
11466
0
    }
11467
11468
0
  return true;
11469
0
}
11470
11471
0
#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11472
11473
/* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
11474
11475
static bool
11476
nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11477
         asection *sec, Elf_Internal_Rela *irel,
11478
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11479
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11480
         Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11481
         struct elf_nds32_link_hash_table *table)
11482
0
{
11483
0
  int eliminate_sethi = 0, range_type;
11484
0
  unsigned int i;
11485
0
  bfd_vma local_sda, laddr;
11486
0
  int seq_len;  /* Original length of instruction sequence.  */
11487
0
  uint32_t insn;
11488
0
  Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11489
0
  bfd_vma access_addr = 0;
11490
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11491
0
  struct elf_link_hash_entry *h = NULL;
11492
0
  int indx;
11493
0
  enum elf_nds32_reloc_type checked_types[] =
11494
0
    { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11495
0
      R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11496
0
      R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11497
0
      R_NDS32_TLS_LE_HI20
11498
0
    };
11499
11500
0
  irelend = internal_relocs + sec->reloc_count;
11501
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
11502
0
  laddr = irel->r_offset;
11503
0
  *insn_len = seq_len;
11504
11505
  /* Get the high part relocation.  */
11506
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11507
0
    {
11508
0
      hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11509
0
                 checked_types[i], laddr);
11510
0
      if (hi_irelfn != irelend)
11511
0
  break;
11512
0
    }
11513
11514
0
  if (hi_irelfn == irelend)
11515
0
    {
11516
      /* Not R_NDS32_HI20_RELA.  */
11517
0
      if (i != 0)
11518
0
  _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11519
0
          (uint64_t) irel->r_offset);
11520
0
      return false;
11521
0
    }
11522
11523
0
  range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11524
0
  nds32_elf_final_sda_base (sec->output_section->owner,
11525
0
          link_info, &local_sda, false);
11526
11527
0
  switch (ELF32_R_TYPE (hi_irelfn->r_info))
11528
0
    {
11529
0
    case R_NDS32_HI20_RELA:
11530
0
      insn = bfd_getb32 (contents + laddr);
11531
0
      access_addr =
11532
0
  calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11533
11534
0
      if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11535
0
  {
11536
0
    indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11537
0
    h = elf_sym_hashes (abfd)[indx];
11538
0
  }
11539
11540
      /* Try movi.  */
11541
0
      if (range_type == NDS32_LOADSTORE_IMM
11542
0
    && access_addr < CONSERVATIVE_20BIT
11543
0
    && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11544
0
  {
11545
0
    eliminate_sethi = 1;
11546
0
    break;
11547
0
  }
11548
11549
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11550
0
  {
11551
0
    eliminate_sethi = 1;
11552
0
    break;
11553
0
  }
11554
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11555
0
               NULL, false, table, isymbuf, symtab_hdr))
11556
0
  return false;
11557
11558
0
      if (!load_store_relax)
11559
0
  return false;
11560
11561
      /* Case for set gp register.  */
11562
0
      if (N32_RT5 (insn) == REG_GP)
11563
0
  return false;
11564
11565
0
      if (range_type == NDS32_LOADSTORE_FLOAT_S
11566
0
    || range_type == NDS32_LOADSTORE_FLOAT_D)
11567
0
  {
11568
0
    range_l = sdata_range[0][0];
11569
0
    range_h = sdata_range[0][1];
11570
0
  }
11571
0
      else
11572
0
  {
11573
0
    range_l = sdata_range[1][0];
11574
0
    range_h = sdata_range[1][1];
11575
0
  }
11576
0
      break;
11577
11578
0
    default:
11579
0
      return false;
11580
0
    }
11581
11582
  /* Delete sethi instruction.  */
11583
0
  if (eliminate_sethi == 1
11584
0
      || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11585
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11586
0
    {
11587
0
      hi_irelfn->r_info =
11588
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11589
0
      irel->r_info =
11590
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11591
0
      *insn_len = 0;
11592
0
      return true;
11593
0
    }
11594
11595
0
  return false;
11596
0
}
11597
11598
/* Relax LO12 relocation for nds32_elf_relax_section.  */
11599
11600
static void
11601
nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11602
          asection *sec, Elf_Internal_Rela *irel,
11603
          Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11604
          Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11605
          struct elf_nds32_link_hash_table *table)
11606
0
{
11607
0
  uint32_t insn;
11608
0
  bfd_vma local_sda, laddr;
11609
0
  unsigned long reloc;
11610
0
  bfd_vma access_addr;
11611
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11612
0
  Elf_Internal_Rela *irelfn = NULL, *irelend;
11613
0
  struct elf_link_hash_entry *h = NULL;
11614
0
  int indx;
11615
11616
  /* For SDA base relative relaxation.  */
11617
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11618
0
          &local_sda, false);
11619
11620
0
  irelend = internal_relocs + sec->reloc_count;
11621
0
  laddr = irel->r_offset;
11622
0
  insn = bfd_getb32 (contents + laddr);
11623
11624
0
  if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11625
0
    return;
11626
11627
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11628
11629
0
  if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11630
0
    {
11631
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11632
0
      h = elf_sym_hashes (abfd)[indx];
11633
0
    }
11634
11635
  /* Try movi.  */
11636
0
  if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11637
0
      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11638
0
    {
11639
0
      reloc = R_NDS32_20_RELA;
11640
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11641
0
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11642
0
      bfd_putb32 (insn, contents + laddr);
11643
0
    }
11644
0
  else
11645
0
    {
11646
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11647
0
  {
11648
    /* Fall through.  */
11649
0
  }
11650
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11651
0
               false, table, isymbuf, symtab_hdr))
11652
0
  return;
11653
11654
0
      range_l = sdata_range[1][0];
11655
0
      range_h = sdata_range[1][1];
11656
0
      switch (ELF32_R_TYPE (irel->r_info))
11657
0
  {
11658
0
  case R_NDS32_LO12S0_RELA:
11659
0
    reloc = R_NDS32_SDA19S0_RELA;
11660
0
    break;
11661
0
  case R_NDS32_LO12S1_RELA:
11662
0
    reloc = R_NDS32_SDA18S1_RELA;
11663
0
    break;
11664
0
  case R_NDS32_LO12S2_RELA:
11665
0
    reloc = R_NDS32_SDA17S2_RELA;
11666
0
    break;
11667
0
  case R_NDS32_LO12S2_DP_RELA:
11668
0
    range_l = sdata_range[0][0];
11669
0
    range_h = sdata_range[0][1];
11670
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11671
0
    break;
11672
0
  case R_NDS32_LO12S2_SP_RELA:
11673
0
    range_l = sdata_range[0][0];
11674
0
    range_h = sdata_range[0][1];
11675
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11676
0
    break;
11677
0
  default:
11678
0
    return;
11679
0
  }
11680
11681
      /* There are range_h and range_l because linker has to promise
11682
   all sections move cross one page together.  */
11683
0
      if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11684
0
    || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11685
0
    || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11686
0
  {
11687
0
    if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11688
0
      {
11689
        /* Maybe we should add R_NDS32_INSN16 reloc type here
11690
     or manually do some optimization.  sethi can't be
11691
     eliminated when updating $gp so the relative ori
11692
     needs to be preserved.  */
11693
0
        return;
11694
0
      }
11695
0
    if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11696
0
          &insn))
11697
0
      return;
11698
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11699
0
    bfd_putb32 (insn, contents + laddr);
11700
11701
0
    irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11702
0
             R_NDS32_INSN16);
11703
    /* SDA17 must keep INSN16 for converting fp_as_gp.  */
11704
0
    if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11705
0
      irelfn->r_info =
11706
0
        ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11707
11708
0
  }
11709
0
    }
11710
0
  return;
11711
0
}
11712
11713
/* Relax PTR relocation for nds32_elf_relax_section.  */
11714
11715
static bool
11716
nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11717
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11718
         int *seq_len, bfd_byte *contents)
11719
0
{
11720
0
  Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11721
11722
0
  irelend = internal_relocs + sec->reloc_count;
11723
11724
0
  re_irel =
11725
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11726
0
         R_NDS32_PTR_RESOLVED, irel->r_addend);
11727
11728
0
  if (re_irel == irelend)
11729
0
    {
11730
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11731
0
        (uint64_t) irel->r_offset);
11732
0
      return false;
11733
0
    }
11734
11735
0
  if (re_irel->r_addend != 1)
11736
0
    return false;
11737
11738
  /* Pointed target is relaxed and no longer needs this void *,
11739
     change the type to NONE.  */
11740
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11741
11742
  /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11743
     not exist, it means only count 1 and remove it directly.  */
11744
  /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11745
0
  count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11746
0
               R_NDS32_PTR_COUNT);
11747
0
  ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11748
0
             R_NDS32_PTR);
11749
0
  if (count_irel != irelend)
11750
0
    {
11751
0
      if (--count_irel->r_addend > 0)
11752
0
  return false;
11753
0
    }
11754
11755
0
  if (ptr_irel != irelend)
11756
0
    return false;
11757
11758
  /* If the PTR_COUNT is already 0, remove current instruction.  */
11759
0
  *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11760
0
  *insn_len = 0;
11761
0
  return true;
11762
0
}
11763
11764
/* Relax LWC relocation for nds32_elf_relax_section.  */
11765
11766
static void
11767
nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11768
          asection *sec, Elf_Internal_Rela *irel,
11769
          Elf_Internal_Rela *internal_relocs,
11770
          bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11771
          Elf_Internal_Shdr *symtab_hdr, bool *again)
11772
0
{
11773
  /* Pattern:
11774
     sethi    ra, hi20(symbol)      ; HI20/LOADSTORE
11775
     ori      ra, ra, lo12(symbol)  ; LO12S0/PTR/PTR/.../INSN16
11776
     flsi     fsa, [ra + offset1]   ; LSI/PTR_RESOLVED/INSN16
11777
     flsi     fsb, [ra + offset2]   ; LSI/PTR_RESOLVED/INSN16
11778
     ...  */
11779
11780
0
  uint32_t insn;
11781
0
  bfd_vma local_sda, laddr;
11782
0
  unsigned long reloc;
11783
0
  bfd_vma access_addr, flsi_offset;
11784
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11785
0
  Elf_Internal_Rela *irelend, *re_irel;
11786
0
  unsigned int opcode;
11787
11788
0
  irelend = internal_relocs + sec->reloc_count;
11789
0
  laddr = irel->r_offset;
11790
0
  insn = bfd_getb32 (contents + laddr);
11791
11792
0
  if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11793
0
    return;
11794
11795
  /* Can not do relaxation for bi format.  */
11796
0
  if ((insn & 0x1000))
11797
0
    return;
11798
11799
  /* Only deal with flsi, fssi, fldi, fsdi, so far.  */
11800
0
  opcode = N32_OP6 (insn);
11801
0
  if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11802
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11803
0
  else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11804
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11805
0
  else
11806
0
    return;
11807
11808
0
  re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11809
0
            R_NDS32_PTR_RESOLVED);
11810
0
  if (re_irel == irelend)
11811
0
    {
11812
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11813
0
        (uint64_t) irel->r_offset);
11814
0
      return;
11815
0
    }
11816
11817
  /* For SDA base relative relaxation.  */
11818
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11819
0
          &local_sda, false);
11820
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11821
0
  flsi_offset = (insn & 0xfff) << 2;
11822
0
  access_addr += flsi_offset;
11823
0
  range_l = sdata_range[0][0];
11824
0
  range_h = sdata_range[0][1];
11825
11826
0
  if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11827
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11828
0
    {
11829
      /* Turn flsi instruction into sda access format.  */
11830
0
      insn = (insn & 0x7ff07000) | (REG_GP << 15);
11831
11832
      /* Add relocation type to flsi.  */
11833
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11834
0
      irel->r_addend += flsi_offset;
11835
0
      bfd_putb32 (insn, contents + re_irel->r_offset);
11836
11837
0
      re_irel->r_addend |= 1;
11838
0
      *again = true;
11839
0
    }
11840
0
}
11841
11842
static bool
11843
nds32_relax_adjust_label (bfd *abfd, asection *sec,
11844
        Elf_Internal_Rela *internal_relocs,
11845
        bfd_byte *contents,
11846
        nds32_elf_blank_t **relax_blank_list,
11847
        int optimize, int opt_size)
11848
0
{
11849
  /* This code block is used to adjust 4-byte alignment by relax a pair
11850
     of instruction a time.
11851
11852
     It recognizes three types of relocations.
11853
     1. R_NDS32_LABEL - a alignment.
11854
     2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11855
     3. is_16bit_NOP () - remove a 16-bit instruction.  */
11856
11857
  /* TODO: It seems currently implementation only support 4-byte alignment.
11858
     We should handle any-alignment.  */
11859
11860
0
  Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11861
0
  Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11862
0
  Elf_Internal_Rela rel_temp;
11863
0
  Elf_Internal_Rela *irelend;
11864
0
  bfd_vma address;
11865
0
  uint16_t insn16;
11866
11867
  /* Checking for branch relaxation relies on the relocations to
11868
     be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11869
0
  nds32_insertion_sort (internal_relocs, sec->reloc_count,
11870
0
      sizeof (Elf_Internal_Rela), compar_reloc);
11871
11872
0
  irelend = internal_relocs + sec->reloc_count;
11873
11874
  /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11875
  /* FIXME: Can we generate the right order in assembler?
11876
     So we don't have to swapping them here.  */
11877
11878
0
  for (label_rel = internal_relocs, insn_rel = internal_relocs;
11879
0
       label_rel < irelend; label_rel++)
11880
0
    {
11881
0
      if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11882
0
  continue;
11883
11884
      /* Find the first reloc has the same offset with label_rel.  */
11885
0
      while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11886
0
  insn_rel++;
11887
11888
0
      for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11889
0
     insn_rel++)
11890
  /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11891
     address.  */
11892
0
  if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11893
0
    break;
11894
11895
0
      if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11896
0
    && insn_rel < label_rel)
11897
0
  {
11898
    /* Swap the two reloc if the R_NDS32_INSN16 is
11899
       before R_NDS32_LABEL.  */
11900
0
    memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11901
0
    memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11902
0
    memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11903
0
  }
11904
0
    }
11905
11906
0
  label_rel = NULL;
11907
0
  insn_rel = NULL;
11908
  /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11909
     or higher, remove other R_NDS32_LABEL with lower alignment.
11910
     If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11911
     then the R_NDS32_LABEL sequence is broke.  */
11912
0
  for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11913
0
    {
11914
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11915
0
  {
11916
0
    if (label_rel == NULL)
11917
0
      {
11918
0
        if (tmp_rel->r_addend < 2)
11919
0
    label_rel = tmp_rel;
11920
0
        continue;
11921
0
      }
11922
0
    else if (tmp_rel->r_addend > 1)
11923
0
      {
11924
        /* Remove all LABEL relocation from label_rel to tmp_rel
11925
     including relocations with same offset as tmp_rel.  */
11926
0
        for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
11927
0
    {
11928
0
      if (tmp2_rel->r_offset == tmp_rel->r_offset)
11929
0
        break;
11930
11931
0
      if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11932
0
          && tmp2_rel->r_addend < 2)
11933
0
        tmp2_rel->r_info =
11934
0
          ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11935
0
            R_NDS32_NONE);
11936
0
    }
11937
0
        label_rel = NULL;
11938
0
      }
11939
0
  }
11940
0
      else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11941
0
  {
11942
    /* A new INSN16 which can be converted, so clear label_rel.  */
11943
0
    if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11944
0
           irelend, &insn16)
11945
0
        || is_16bit_NOP (abfd, sec, tmp_rel))
11946
0
      label_rel = NULL;
11947
0
  }
11948
0
    }
11949
11950
0
  label_rel = NULL;
11951
0
  insn_rel = NULL;
11952
  /* Optimized for speed and nothing has not been relaxed.
11953
     It's time to align labels.
11954
     We may convert a 16-bit instruction right before a label to
11955
     32-bit, in order to align the label if necessary
11956
     all reloc entries has been sorted by r_offset.  */
11957
0
  for (irel = internal_relocs;
11958
0
       irel < irelend && irel->r_offset < sec->size; irel++)
11959
0
    {
11960
0
      if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11961
0
    && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11962
0
  continue;
11963
11964
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11965
0
  {
11966
    /* A new INSN16 found, resize the old one.  */
11967
0
    if (is_convert_32_to_16
11968
0
        (abfd, sec, irel, internal_relocs, irelend, &insn16)
11969
0
        || is_16bit_NOP (abfd, sec, irel))
11970
0
      {
11971
0
        if (insn_rel)
11972
0
    {
11973
      /* Previous INSN16 reloc exists, reduce its
11974
         size to 16-bit.  */
11975
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11976
0
             irelend, &insn16))
11977
0
        {
11978
0
          nds32_elf_write_16 (abfd, contents, insn_rel,
11979
0
            internal_relocs, irelend, insn16);
11980
11981
0
          if (!insert_nds32_elf_blank_recalc_total
11982
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
11983
0
      return false;
11984
0
        }
11985
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
11986
0
        {
11987
0
          if (!insert_nds32_elf_blank_recalc_total
11988
0
        (relax_blank_list, insn_rel->r_offset, 2))
11989
0
      return false;
11990
0
        }
11991
0
      insn_rel->r_info =
11992
0
        ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11993
0
    }
11994
        /* Save the new one for later use.  */
11995
0
        insn_rel = irel;
11996
0
      }
11997
0
    else
11998
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11999
0
           R_NDS32_NONE);
12000
0
  }
12001
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12002
0
  {
12003
    /* Search for label.  */
12004
0
    int force_relax = 0;
12005
12006
    /* Label on 16-bit instruction or optimization
12007
       needless, just reset this reloc.  */
12008
0
    insn16 = bfd_getb16 (contents + irel->r_offset);
12009
0
    if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12010
0
      {
12011
0
        irel->r_info =
12012
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12013
0
        continue;
12014
0
      }
12015
12016
0
    address =
12017
0
      irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12018
0
              irel->r_offset, 1);
12019
12020
0
    if (!insn_rel)
12021
0
      {
12022
        /* Check if there is case which can not be aligned.  */
12023
0
        if (irel->r_addend == 2 && address & 0x2)
12024
0
    return false;
12025
0
        continue;
12026
0
      }
12027
12028
    /* Try to align this label.  */
12029
12030
0
    if ((irel->r_addend & 0x1f) < 2)
12031
0
      {
12032
        /* Check if there is a INSN16 at the same address.
12033
     Label_rel always seats before insn_rel after
12034
     our sort.  */
12035
12036
        /* Search for INSN16 at LABEL location.  If INSN16 is at
12037
     same location and this LABEL alignment is lower than 2,
12038
     the INSN16 can be converted to 2-byte.  */
12039
0
        for (tmp_rel = irel;
12040
0
       tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12041
0
       tmp_rel++)
12042
0
    {
12043
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12044
0
          && (is_convert_32_to_16
12045
0
        (abfd, sec, tmp_rel, internal_relocs,
12046
0
         irelend, &insn16)
12047
0
        || is_16bit_NOP (abfd, sec, tmp_rel)))
12048
0
        {
12049
0
          force_relax = 1;
12050
0
          break;
12051
0
        }
12052
0
    }
12053
0
      }
12054
12055
0
    if (force_relax || irel->r_addend == 1 || address & 0x2)
12056
0
      {
12057
        /* Label not aligned.  */
12058
        /* Previous reloc exists, reduce its size to 16-bit.  */
12059
0
        if (is_convert_32_to_16 (abfd, sec, insn_rel,
12060
0
               internal_relocs, irelend, &insn16))
12061
0
    {
12062
0
      nds32_elf_write_16 (abfd, contents, insn_rel,
12063
0
              internal_relocs, irelend, insn16);
12064
12065
0
      if (!insert_nds32_elf_blank_recalc_total
12066
0
          (relax_blank_list, insn_rel->r_offset + 2, 2))
12067
0
        return false;
12068
0
    }
12069
0
        else if (is_16bit_NOP (abfd, sec, insn_rel))
12070
0
    {
12071
0
      if (!insert_nds32_elf_blank_recalc_total
12072
0
          (relax_blank_list, insn_rel->r_offset, 2))
12073
0
        return false;
12074
0
    }
12075
12076
0
      }
12077
    /* INSN16 reloc is used.  */
12078
0
    insn_rel = NULL;
12079
0
  }
12080
0
    }
12081
12082
0
  address =
12083
0
    sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12084
0
  if (insn_rel && (address & 0x2 || opt_size))
12085
0
    {
12086
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12087
0
             irelend, &insn16))
12088
0
  {
12089
0
    nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12090
0
            irelend, insn16);
12091
0
    if (!insert_nds32_elf_blank_recalc_total
12092
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12093
0
      return false;
12094
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12095
0
             R_NDS32_NONE);
12096
0
  }
12097
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12098
0
  {
12099
0
    if (!insert_nds32_elf_blank_recalc_total
12100
0
        (relax_blank_list, insn_rel->r_offset, 2))
12101
0
      return false;
12102
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12103
0
             R_NDS32_NONE);
12104
0
  }
12105
0
    }
12106
0
  insn_rel = NULL;
12107
0
  return true;
12108
0
}
12109
12110
static bool
12111
nds32_elf_relax_section (bfd *abfd, asection *sec,
12112
       struct bfd_link_info *link_info, bool *again)
12113
0
{
12114
0
  nds32_elf_blank_t *relax_blank_list = NULL;
12115
0
  Elf_Internal_Shdr *symtab_hdr;
12116
0
  Elf_Internal_Rela *internal_relocs;
12117
0
  Elf_Internal_Rela *irel;
12118
0
  Elf_Internal_Rela *irelend;
12119
0
  Elf_Internal_Sym *isymbuf = NULL;
12120
0
  bfd_byte *contents = NULL;
12121
0
  bool result = true;
12122
0
  int optimize = 0;
12123
0
  int opt_size = 0;
12124
0
  uint32_t insn;
12125
0
  uint16_t insn16;
12126
12127
  /* Target dependnet option.  */
12128
0
  struct elf_nds32_link_hash_table *table;
12129
0
  int load_store_relax;
12130
12131
0
  relax_blank_list = NULL;
12132
12133
0
  *again = false;
12134
12135
  /* Nothing to do for
12136
   * relocatable link or
12137
   * non-relocatable section or
12138
   * non-code section or
12139
   * empty content or
12140
   * no reloc entry.  */
12141
0
  if (bfd_link_relocatable (link_info)
12142
0
      || (sec->flags & SEC_RELOC) == 0
12143
0
      || (sec->flags & SEC_EXCLUDE) != 0
12144
0
      || (sec->flags & SEC_CODE) == 0
12145
0
      || sec->size == 0
12146
0
      || sec->reloc_count == 0)
12147
0
    return true;
12148
12149
  /* 09.12.11 Workaround.  */
12150
  /*  We have to adjust align for R_NDS32_LABEL if needed.
12151
      The adjust approach only can fix 2-byte align once.  */
12152
0
  if (sec->alignment_power > 2)
12153
0
    return true;
12154
12155
  /* Do TLS model conversion once at first.  */
12156
0
  nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12157
12158
  /* The optimization type to do.  */
12159
12160
0
  table = nds32_elf_hash_table (link_info);
12161
12162
  /* Save the first section for abs symbol relaxation.
12163
     This is used for checking gp relaxation in the
12164
     nds32_elf_relax_loadstore and nds32_elf_relax_lo12.  */
12165
0
  nds32_elf_relax_guard (NULL, 0, sec, NULL, again, true,
12166
0
       table, NULL, NULL);
12167
12168
  /* The begining of general relaxation.  */
12169
12170
0
  if (is_SDA_BASE_set == 0)
12171
0
    {
12172
0
      bfd_vma gp;
12173
0
      is_SDA_BASE_set = 1;
12174
0
      nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12175
0
        &gp, false);
12176
0
      relax_range_measurement (abfd, link_info);
12177
0
    }
12178
12179
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12180
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
12181
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12182
0
                 true /* keep_memory */);
12183
0
  if (internal_relocs == NULL)
12184
0
    goto error_return;
12185
12186
0
  irelend = internal_relocs + sec->reloc_count;
12187
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
12188
0
         irelend, R_NDS32_RELAX_ENTRY);
12189
12190
0
  if (irel == irelend)
12191
0
    return true;
12192
12193
0
  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12194
0
    {
12195
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12196
0
  return true;
12197
12198
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12199
0
  optimize = 1;
12200
12201
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12202
0
  opt_size = 1;
12203
0
    }
12204
12205
0
  load_store_relax = table->load_store_relax;
12206
12207
  /* Get symbol table and section content.  */
12208
0
  contents = NULL;
12209
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12210
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12211
0
    goto error_return;
12212
12213
  /* Do relax loop only when finalize is not done.
12214
     Take care of relaxable relocs except INSN16.  */
12215
0
  for (irel = internal_relocs; irel < irelend; irel++)
12216
0
    {
12217
0
      int seq_len;    /* Original length of instruction sequence.  */
12218
0
      int insn_len = 0;   /* Final length of instruction sequence.  */
12219
0
      bool removed;
12220
12221
0
      insn = 0;
12222
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12223
0
    && (irel->r_addend & 0x1f) >= 2)
12224
0
  optimize = 1;
12225
12226
      /* Relocation Types
12227
   R_NDS32_LONGCALL1  53
12228
   R_NDS32_LONGCALL2  54
12229
   R_NDS32_LONGCALL3  55
12230
   R_NDS32_LONGJUMP1  56
12231
   R_NDS32_LONGJUMP2  57
12232
   R_NDS32_LONGJUMP3  58
12233
   R_NDS32_LOADSTORE  59  */
12234
0
      if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12235
0
    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12236
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
12237
12238
      /* Relocation Types
12239
   R_NDS32_LONGCALL4  107
12240
   R_NDS32_LONGCALL5  108
12241
   R_NDS32_LONGCALL6  109
12242
   R_NDS32_LONGJUMP4  110
12243
   R_NDS32_LONGJUMP5  111
12244
   R_NDS32_LONGJUMP6  112
12245
   R_NDS32_LONGJUMP7  113  */
12246
0
      else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12247
0
         && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12248
0
  seq_len = 4;
12249
12250
  /* Relocation Types
12251
   R_NDS32_LO12S0_RELA    30
12252
   R_NDS32_LO12S1_RELA    29
12253
   R_NDS32_LO12S2_RELA    28
12254
   R_NDS32_LO12S2_SP_RELA   71
12255
   R_NDS32_LO12S2_DP_RELA   70
12256
   R_NDS32_GOT_LO12   46
12257
   R_NDS32_GOTOFF_LO12    50
12258
   R_NDS32_PLTREL_LO12    65
12259
   R_NDS32_PLT_GOTREL_LO12  67
12260
   R_NDS32_17IFC_PCREL_RELA 96
12261
   R_NDS32_GOT_SUFF   193
12262
   R_NDS32_GOTOFF_SUFF    194
12263
   R_NDS32_PLT_GOT_SUFF   195
12264
   R_NDS32_MULCALL_SUFF   196
12265
   R_NDS32_PTR      197  */
12266
0
      else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12267
0
    && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12268
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12269
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12270
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12271
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12272
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12273
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12274
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12275
0
         || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12276
0
       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12277
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12278
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12279
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12280
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12281
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12282
0
  seq_len = 0;
12283
0
      else
12284
0
  continue;
12285
12286
0
      insn_len = seq_len;
12287
0
      removed = false;
12288
12289
0
      switch (ELF32_R_TYPE (irel->r_info))
12290
0
  {
12291
0
  case R_NDS32_LONGCALL1:
12292
0
    removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12293
0
                 &insn_len, contents, isymbuf,
12294
0
                 symtab_hdr);
12295
0
    break;
12296
0
  case R_NDS32_LONGCALL2:
12297
0
    removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12298
0
                 &insn_len, contents, isymbuf,
12299
0
                 symtab_hdr);
12300
0
    break;
12301
0
  case R_NDS32_LONGCALL3:
12302
0
    removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12303
0
                 &insn_len, contents, isymbuf,
12304
0
                 symtab_hdr);
12305
0
    break;
12306
0
  case R_NDS32_LONGJUMP1:
12307
0
    removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12308
0
                 &insn_len, contents, isymbuf,
12309
0
                 symtab_hdr);
12310
0
    break;
12311
0
  case R_NDS32_LONGJUMP2:
12312
0
    removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12313
0
                 &insn_len, contents, isymbuf,
12314
0
                 symtab_hdr);
12315
0
    break;
12316
0
  case R_NDS32_LONGJUMP3:
12317
0
    removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12318
0
                 &insn_len, contents, isymbuf,
12319
0
                 symtab_hdr);
12320
0
    break;
12321
0
  case R_NDS32_LONGCALL4:
12322
0
    removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12323
0
                 &insn_len, contents, isymbuf,
12324
0
                 symtab_hdr);
12325
0
    break;
12326
0
  case R_NDS32_LONGCALL5:
12327
0
    removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12328
0
                 &insn_len, contents, isymbuf,
12329
0
                 symtab_hdr);
12330
0
    break;
12331
0
  case R_NDS32_LONGCALL6:
12332
0
    removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12333
0
                 &insn_len, contents, isymbuf,
12334
0
                 symtab_hdr);
12335
0
    break;
12336
0
  case R_NDS32_LONGJUMP4:
12337
0
    removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12338
0
                 &insn_len, contents, isymbuf,
12339
0
                 symtab_hdr);
12340
0
    break;
12341
0
  case R_NDS32_LONGJUMP5:
12342
0
    removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12343
0
                 &insn_len, &seq_len, contents,
12344
0
                 isymbuf, symtab_hdr);
12345
0
    break;
12346
0
  case R_NDS32_LONGJUMP6:
12347
0
    removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12348
0
                 &insn_len, &seq_len, contents,
12349
0
                 isymbuf, symtab_hdr);
12350
0
    break;
12351
0
  case R_NDS32_LONGJUMP7:
12352
0
    removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12353
0
                 &insn_len, &seq_len, contents,
12354
0
                 isymbuf, symtab_hdr);
12355
0
    break;
12356
0
  case R_NDS32_LOADSTORE:
12357
0
    removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12358
0
                 internal_relocs, &insn_len,
12359
0
                 contents, isymbuf, symtab_hdr,
12360
0
                 load_store_relax, table);
12361
0
    break;
12362
0
  case R_NDS32_LO12S0_RELA:
12363
0
  case R_NDS32_LO12S1_RELA:
12364
0
  case R_NDS32_LO12S2_RELA:
12365
0
  case R_NDS32_LO12S2_DP_RELA:
12366
0
  case R_NDS32_LO12S2_SP_RELA:
12367
    /* Relax for low part.  */
12368
0
    nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12369
0
        contents, isymbuf, symtab_hdr, table);
12370
12371
    /* It is impossible to delete blank, so just continue.  */
12372
0
    continue;
12373
0
  case R_NDS32_PTR:
12374
0
    removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12375
0
           &insn_len, &seq_len, contents);
12376
0
    break;
12377
0
  case R_NDS32_LSI:
12378
0
    nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12379
0
        contents, isymbuf, symtab_hdr, again);
12380
0
    continue;
12381
0
  case R_NDS32_GOT_LO12:
12382
0
  case R_NDS32_GOTOFF_LO12:
12383
0
  case R_NDS32_PLTREL_LO12:
12384
0
  case R_NDS32_PLT_GOTREL_LO12:
12385
0
  case R_NDS32_GOTPC_LO12:
12386
0
  case R_NDS32_TLS_LE_LO12:
12387
0
  case R_NDS32_TLS_LE_ADD:
12388
0
  case R_NDS32_TLS_LE_LS:
12389
0
  case R_NDS32_PLT_GOT_SUFF:
12390
0
  case R_NDS32_GOT_SUFF:
12391
0
  case R_NDS32_GOTOFF_SUFF:
12392
0
    continue;
12393
0
  default:
12394
0
    continue;
12395
0
  }
12396
12397
0
      if (removed && seq_len - insn_len > 0)
12398
0
  {
12399
0
    if (!insert_nds32_elf_blank
12400
0
        (&relax_blank_list, irel->r_offset + insn_len,
12401
0
         seq_len - insn_len))
12402
0
      goto error_return;
12403
0
    *again = true;
12404
0
  }
12405
0
    }
12406
12407
0
  calc_nds32_blank_total (relax_blank_list);
12408
12409
0
  if (table->relax_fp_as_gp)
12410
0
    {
12411
0
      if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12412
0
         irelend, isymbuf))
12413
0
  goto error_return;
12414
12415
0
      if (!*again)
12416
0
  {
12417
0
    if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12418
0
                 irelend))
12419
0
      goto error_return;
12420
0
  }
12421
0
    }
12422
12423
0
  if (!*again)
12424
0
    {
12425
0
      if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12426
0
             &relax_blank_list, optimize, opt_size))
12427
0
  goto error_return;
12428
0
    }
12429
12430
  /* It doesn't matter optimize_for_space_no_align anymore.
12431
       If object file is assembled with flag '-Os',
12432
       the we don't adjust jump-destination on 4-byte boundary.  */
12433
12434
0
  if (relax_blank_list)
12435
0
    {
12436
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12437
0
      relax_blank_list = NULL;
12438
0
    }
12439
12440
0
  if (!*again)
12441
0
    {
12442
      /* Closing the section, so we don't relax it anymore.  */
12443
0
      bfd_vma sec_size_align;
12444
0
      Elf_Internal_Rela *tmp_rel;
12445
12446
      /* Pad to alignment boundary.  Only handle current section alignment.  */
12447
0
      sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12448
0
           & ((-1U) << sec->alignment_power);
12449
0
      if ((sec_size_align - sec->size) & 0x2)
12450
0
  {
12451
0
    insn16 = NDS32_NOP16;
12452
0
    bfd_putb16 (insn16, contents + sec->size);
12453
0
    sec->size += 2;
12454
0
  }
12455
12456
0
      while (sec_size_align != sec->size)
12457
0
  {
12458
0
    insn = NDS32_NOP32;
12459
0
    bfd_putb32 (insn, contents + sec->size);
12460
0
    sec->size += 4;
12461
0
  }
12462
12463
0
      tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12464
0
          irelend, R_NDS32_RELAX_ENTRY);
12465
0
      if (tmp_rel != irelend)
12466
0
  tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12467
12468
0
      clean_nds32_elf_blank ();
12469
0
    }
12470
12471
0
 finish:
12472
0
  if (elf_section_data (sec)->relocs != internal_relocs)
12473
0
    free (internal_relocs);
12474
12475
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
12476
0
    free (contents);
12477
12478
0
  if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12479
0
    free (isymbuf);
12480
12481
0
  return result;
12482
12483
0
 error_return:
12484
0
  result = false;
12485
0
  goto finish;
12486
0
}
12487
12488
static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12489
{
12490
  {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12491
  {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12492
  {NULL, 0, 0, 0, 0}
12493
};
12494
12495
static bool
12496
nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12497
1.87k
{
12498
1.87k
  const char *name = hdr->bfd_section->name;
12499
12500
1.87k
  if (startswith (name, ".sbss")
12501
1.84k
      || startswith (name, ".sdata"))
12502
77
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
12503
12504
1.87k
  return true;
12505
1.87k
}
12506
12507
static bool
12508
nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12509
          struct bfd_link_info *info,
12510
          void *finfo ATTRIBUTE_UNUSED,
12511
          int (*func) (void *, const char *,
12512
           Elf_Internal_Sym *,
12513
           asection *,
12514
           struct elf_link_hash_entry *)
12515
          ATTRIBUTE_UNUSED)
12516
0
{
12517
0
  FILE *sym_ld_script = NULL;
12518
0
  struct elf_nds32_link_hash_table *table;
12519
12520
0
  table = nds32_elf_hash_table (info);
12521
0
  sym_ld_script = table->sym_ld_script;
12522
12523
0
  if (check_start_export_sym)
12524
0
    fprintf (sym_ld_script, "}\n");
12525
12526
0
  return true;
12527
0
}
12528
12529
static enum elf_reloc_type_class
12530
nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12531
          const asection *rel_sec ATTRIBUTE_UNUSED,
12532
          const Elf_Internal_Rela *rela)
12533
0
{
12534
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
12535
0
    {
12536
0
    case R_NDS32_RELATIVE:
12537
0
      return reloc_class_relative;
12538
0
    case R_NDS32_JMP_SLOT:
12539
0
      return reloc_class_plt;
12540
0
    case R_NDS32_COPY:
12541
0
      return reloc_class_copy;
12542
0
    default:
12543
0
      return reloc_class_normal;
12544
0
    }
12545
0
}
12546
12547
/* Put target dependent option into info hash table.  */
12548
void
12549
bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12550
           int relax_fp_as_gp,
12551
           int eliminate_gc_relocs,
12552
           FILE * sym_ld_script,
12553
           int hyper_relax,
12554
           int tls_desc_trampoline,
12555
           int load_store_relax)
12556
0
{
12557
0
  struct elf_nds32_link_hash_table *table;
12558
12559
0
  table = nds32_elf_hash_table (link_info);
12560
0
  if (table == NULL)
12561
0
    return;
12562
12563
0
  table->relax_fp_as_gp = relax_fp_as_gp;
12564
0
  table->eliminate_gc_relocs = eliminate_gc_relocs;
12565
0
  table->sym_ld_script = sym_ld_script;
12566
0
  table->hyper_relax = hyper_relax;
12567
0
  table->tls_desc_trampoline = tls_desc_trampoline;
12568
0
  table ->load_store_relax = load_store_relax;
12569
0
}
12570

12571
12572
/* These functions and data-structures are used for fp-as-gp
12573
   optimization.  */
12574
12575
0
#define FAG_THRESHOLD 3  /* At least 3 gp-access.  */
12576
/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12577
   the read-only section and read-write section.  */
12578
0
#define FAG_WINDOW  (508 - 32)
12579
12580
/* An nds32_fag represent a gp-relative access.
12581
   We find best fp-base by using a sliding window
12582
   to find a base address which can cover most gp-access.  */
12583
struct nds32_fag
12584
{
12585
  struct nds32_fag *next; /* NULL-teminated linked list.  */
12586
  bfd_vma addr;     /* The address of this fag.  */
12587
  Elf_Internal_Rela **relas;  /* The relocations associated with this fag.
12588
           It is used for applying FP7U2_FLAG.  */
12589
  int count;      /* How many times this address is referred.
12590
           There should be exactly `count' relocations
12591
           in relas.  */
12592
  int relas_capcity;    /* The buffer size of relas.
12593
           We use an array instead of linked-list,
12594
           and realloc is used to adjust buffer size.  */
12595
};
12596
12597
static void
12598
nds32_fag_init (struct nds32_fag *head)
12599
0
{
12600
0
  memset (head, 0, sizeof (struct nds32_fag));
12601
0
}
12602
12603
static void
12604
nds32_fag_verify (struct nds32_fag *head)
12605
0
{
12606
0
  struct nds32_fag *iter;
12607
0
  struct nds32_fag *prev;
12608
12609
0
  prev = NULL;
12610
0
  iter = head->next;
12611
0
  while (iter)
12612
0
    {
12613
0
      if (prev && prev->addr >= iter->addr)
12614
0
  puts ("Bug in fp-as-gp insertion.");
12615
0
      prev = iter;
12616
0
      iter = iter->next;
12617
0
    }
12618
0
}
12619
12620
/* Insert a fag in ascending order.
12621
   If a fag of the same address already exists,
12622
   they are chained by relas array.  */
12623
12624
static void
12625
nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12626
      Elf_Internal_Rela * rel)
12627
0
{
12628
0
  struct nds32_fag *iter;
12629
0
  struct nds32_fag *new_fag;
12630
0
  const int INIT_RELAS_CAP = 4;
12631
12632
0
  for (iter = head;
12633
0
       iter->next && iter->next->addr <= addr;
12634
0
       iter = iter->next)
12635
0
    /* Find somewhere to insert.  */ ;
12636
12637
  /* `iter' will be equal to `head' if the list is empty.  */
12638
0
  if (iter != head && iter->addr == addr)
12639
0
    {
12640
      /* The address exists in the list.
12641
   Insert `rel' into relocation list, relas.  */
12642
12643
      /* Check whether relas is big enough.  */
12644
0
      if (iter->count >= iter->relas_capcity)
12645
0
  {
12646
0
    iter->relas_capcity *= 2;
12647
0
    iter->relas = bfd_realloc
12648
0
      (iter->relas, iter->relas_capcity * sizeof (void *));
12649
0
  }
12650
0
      iter->relas[iter->count++] = rel;
12651
0
      return;
12652
0
    }
12653
12654
  /* This is a new address.  Create a fag node for it.  */
12655
0
  new_fag = bfd_malloc (sizeof (struct nds32_fag));
12656
0
  memset (new_fag, 0, sizeof (*new_fag));
12657
0
  new_fag->addr = addr;
12658
0
  new_fag->count = 1;
12659
0
  new_fag->next = iter->next;
12660
0
  new_fag->relas_capcity = INIT_RELAS_CAP;
12661
0
  new_fag->relas = (Elf_Internal_Rela **)
12662
0
    bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12663
0
  new_fag->relas[0] = rel;
12664
0
  iter->next = new_fag;
12665
12666
0
  nds32_fag_verify (head);
12667
0
}
12668
12669
static void
12670
nds32_fag_free_list (struct nds32_fag *head)
12671
0
{
12672
0
  struct nds32_fag *iter;
12673
12674
0
  iter = head->next;
12675
0
  while (iter)
12676
0
    {
12677
0
      struct nds32_fag *tmp = iter;
12678
0
      iter = iter->next;
12679
0
      free (tmp->relas);
12680
0
      tmp->relas = NULL;
12681
0
      free (tmp);
12682
0
    }
12683
0
}
12684
12685
/* Find the best fp-base address.
12686
   The relocation associated with that address is returned,
12687
   so we can track the symbol instead of a fixed address.
12688
12689
   When relaxation, the address of an datum may change,
12690
   because a text section is shrinked, so the data section
12691
   moves forward.  If the aligments of text and data section
12692
   are different, their distance may change too.
12693
   Therefore, tracking a fixed address is not appriate.  */
12694
12695
static int
12696
nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12697
0
{
12698
0
  struct nds32_fag *base; /* First fag in the window.  */
12699
0
  struct nds32_fag *last; /* First fag outside the window.  */
12700
0
  int accu = 0;     /* Usage accumulation.  */
12701
0
  struct nds32_fag *best; /* Best fag.  */
12702
0
  int baccu = 0;    /* Best accumulation.  */
12703
12704
  /* Use first fag for initial, and find the last fag in the window.
12705
12706
     In each iteration, we could simply subtract previous fag
12707
     and accumulate following fags which are inside the window,
12708
     untill we each the end.  */
12709
12710
0
  if (head->next == NULL)
12711
0
    {
12712
0
      *bestpp = NULL;
12713
0
      return 0;
12714
0
    }
12715
12716
  /* Initialize base.  */
12717
0
  base = head->next;
12718
0
  best = base;
12719
0
  for (last = base;
12720
0
       last && last->addr < base->addr + FAG_WINDOW;
12721
0
       last = last->next)
12722
0
    accu += last->count;
12723
12724
0
  baccu = accu;
12725
12726
  /* Record the best base in each iteration.  */
12727
0
  while (base->next)
12728
0
    {
12729
0
      accu -= base->count;
12730
0
      base = base->next;
12731
      /* Account fags in window.  */
12732
0
      for (/* Nothing.  */;
12733
0
     last && last->addr < base->addr + FAG_WINDOW;
12734
0
     last = last->next)
12735
0
  accu += last->count;
12736
12737
      /* A better fp-base?  */
12738
0
      if (accu > baccu)
12739
0
  {
12740
0
    best = base;
12741
0
    baccu = accu;
12742
0
  }
12743
0
    }
12744
12745
0
  if (bestpp)
12746
0
    *bestpp = best;
12747
0
  return baccu;
12748
0
}
12749
12750
/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12751
   so we can convert it fo fp-relative access later.
12752
   `best_fag' is the best fp-base.  Only those inside the window
12753
   of best_fag is applied the flag.  */
12754
12755
static bool
12756
nds32_fag_mark_relax (struct bfd_link_info *link_info,
12757
          asection *sec, struct nds32_fag *best_fag,
12758
          Elf_Internal_Rela *internal_relocs,
12759
          Elf_Internal_Rela *irelend)
12760
0
{
12761
0
  struct nds32_fag *ifag;
12762
0
  bfd_vma best_fpbase, gp;
12763
0
  bfd *output_bfd;
12764
12765
0
  output_bfd = sec->output_section->owner;
12766
0
  nds32_elf_final_sda_base (output_bfd, link_info, &gp, false);
12767
0
  best_fpbase = best_fag->addr;
12768
12769
0
  if (best_fpbase > gp + sdata_range[1][1]
12770
0
      || best_fpbase < gp - sdata_range[1][0])
12771
0
    return false;
12772
12773
  /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12774
     so we know they can be converted to lwi37.fp.   */
12775
0
  for (ifag = best_fag;
12776
0
       ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12777
0
    {
12778
0
      int i;
12779
12780
0
      for (i = 0; i < ifag->count; i++)
12781
0
  {
12782
0
    Elf_Internal_Rela *insn16_rel;
12783
0
    Elf_Internal_Rela *fag_rel;
12784
12785
0
    fag_rel = ifag->relas[i];
12786
12787
    /* Only if this is within the WINDOWS, FP7U2_FLAG
12788
       is applied.  */
12789
12790
0
    insn16_rel = find_relocs_at_address
12791
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12792
12793
0
    if (insn16_rel != irelend)
12794
0
      insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12795
0
  }
12796
0
    }
12797
0
  return true;
12798
0
}
12799
12800
/* Reset INSN16 to clean fp as gp.  */
12801
12802
static void
12803
nds32_fag_unmark_relax (struct nds32_fag *fag,
12804
      Elf_Internal_Rela *internal_relocs,
12805
      Elf_Internal_Rela *irelend)
12806
0
{
12807
0
  struct nds32_fag *ifag;
12808
0
  int i;
12809
0
  Elf_Internal_Rela *insn16_rel;
12810
0
  Elf_Internal_Rela *fag_rel;
12811
12812
0
  for (ifag = fag; ifag; ifag = ifag->next)
12813
0
    {
12814
0
      for (i = 0; i < ifag->count; i++)
12815
0
  {
12816
0
    fag_rel = ifag->relas[i];
12817
12818
    /* Restore the INSN16 relocation.  */
12819
0
    insn16_rel = find_relocs_at_address
12820
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12821
12822
0
    if (insn16_rel != irelend)
12823
0
      insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12824
0
  }
12825
0
    }
12826
0
}
12827
12828
/* This is the main function of fp-as-gp optimization.
12829
   It should be called by relax_section.  */
12830
12831
static bool
12832
nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12833
          bfd *abfd, asection *sec,
12834
          Elf_Internal_Rela *internal_relocs,
12835
          Elf_Internal_Rela *irelend,
12836
          Elf_Internal_Sym *isymbuf)
12837
0
{
12838
0
  Elf_Internal_Rela *begin_rel = NULL;
12839
0
  Elf_Internal_Rela *irel;
12840
0
  struct nds32_fag fag_head;
12841
0
  Elf_Internal_Shdr *symtab_hdr;
12842
0
  bfd_byte *contents;
12843
0
  bool ifc_inside = false;
12844
12845
  /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12846
12847
  /* Per-function fp-base selection.
12848
     1. Create a list for all the gp-relative access.
12849
     2. Base on those gp-relative address,
12850
  find a fp-base which can cover most access.
12851
     3. Use the fp-base for fp-as-gp relaxation.
12852
12853
     NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12854
     we should
12855
     1. delete the `la $fp, _FP_BASE_' instruction and
12856
     2. not convert lwi.gp to lwi37.fp.
12857
12858
     To delete the _FP_BASE_ instruction, we simply apply
12859
     R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12860
12861
     To suppress the conversion, we simply NOT to apply
12862
     R_NDS32_INSN16_FP7U2_FLAG flag.  */
12863
12864
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12865
12866
0
  contents = NULL;
12867
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12868
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12869
0
    return false;
12870
12871
  /* Check whether it is worth for fp-as-gp optimization,
12872
     i.e., at least 3 gp-load.
12873
12874
     Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12875
     apply this optimization.  */
12876
12877
0
  for (irel = internal_relocs; irel < irelend; irel++)
12878
0
    {
12879
      /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12880
   One we enter the begin of the region, we track all the LW/ST
12881
   instructions, so when we leave the region, we try to find
12882
   the best fp-base address for those LW/ST instructions.  */
12883
12884
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12885
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12886
0
  {
12887
    /* Begin of the region.  */
12888
0
    if (begin_rel)
12889
      /* xgettext:c-format */
12890
0
      _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12891
12892
0
    begin_rel = irel;
12893
0
    nds32_fag_init (&fag_head);
12894
0
    ifc_inside = false;
12895
0
  }
12896
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12897
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12898
0
  {
12899
0
    int accu;
12900
0
    struct nds32_fag *best_fag, *tmp_fag;
12901
0
    int dist;
12902
12903
    /* End of the region.
12904
       Check whether it is worth to do fp-as-gp.  */
12905
12906
0
    if (begin_rel == NULL)
12907
0
      {
12908
        /* xgettext:c-format */
12909
0
        _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12910
0
          abfd, sec);
12911
0
        continue;
12912
0
      }
12913
12914
0
    accu = nds32_fag_find_base (&fag_head, &best_fag);
12915
12916
    /* Clean FP7U2_FLAG because they may set ever.  */
12917
0
    tmp_fag = fag_head.next;
12918
0
    nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12919
12920
    /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
12921
0
    if (accu < FAG_THRESHOLD
12922
0
        || !nds32_fag_mark_relax (link_info, sec, best_fag,
12923
0
          internal_relocs, irelend))
12924
0
      {
12925
        /* Not worth to do fp-as-gp.  */
12926
0
        begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12927
0
        begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12928
0
        irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12929
0
        irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12930
0
        nds32_fag_free_list (&fag_head);
12931
0
        begin_rel = NULL;
12932
0
        continue;
12933
0
      }
12934
12935
    /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12936
       so we use it to record the distance to the reloction of best
12937
       fp-base.  */
12938
0
    dist = best_fag->relas[0] - begin_rel;
12939
0
    BFD_ASSERT (dist > 0 && dist < 0xffffff);
12940
    /* Use high 16 bits of addend to record the _FP_BASE_ matched
12941
       relocation.  And get the base value when relocating.  */
12942
0
    begin_rel->r_addend &= (0x1 << 16) - 1;
12943
0
    begin_rel->r_addend |= dist << 16;
12944
12945
0
    nds32_fag_free_list (&fag_head);
12946
0
    begin_rel = NULL;
12947
0
  }
12948
12949
0
      if (begin_rel == NULL || ifc_inside)
12950
  /* Skip if we are not in the region of fp-as-gp.  */
12951
0
  continue;
12952
12953
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12954
0
    || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12955
0
  {
12956
0
    bfd_vma addr;
12957
0
    uint32_t insn;
12958
12959
    /* A gp-relative access is found.  Insert it to the fag-list.  */
12960
12961
    /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
12962
0
    insn = bfd_getb32 (contents + irel->r_offset);
12963
0
    if (!N32_IS_RT3 (insn))
12964
0
      continue;
12965
12966
0
    addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12967
0
    nds32_fag_insert (&fag_head, addr, irel);
12968
0
  }
12969
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12970
0
  {
12971
0
    begin_rel = NULL;
12972
0
  }
12973
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12974
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12975
0
  {
12976
    /* Suppress fp as gp when encounter ifc.  */
12977
0
    ifc_inside = true;
12978
0
  }
12979
0
    }
12980
12981
0
  return true;
12982
0
}
12983
12984
/* Remove unused `la $fp, _FD_BASE_' instruction.  */
12985
12986
static bool
12987
nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12988
        Elf_Internal_Rela *internal_relocs,
12989
        Elf_Internal_Rela *irelend)
12990
0
{
12991
0
  Elf_Internal_Rela *irel;
12992
0
  Elf_Internal_Shdr *symtab_hdr;
12993
0
  bfd_byte *contents = NULL;
12994
0
  nds32_elf_blank_t *relax_blank_list = NULL;
12995
0
  bool result = true;
12996
0
  bool unused_region = false;
12997
12998
  /*
12999
     NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13000
       R_NDS32_17IFC_PCREL_RELA
13001
       R_NDS32_10IFCU_PCREL_RELA.  */
13002
13003
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13004
0
  nds32_get_section_contents (abfd, sec, &contents, true);
13005
13006
0
  for (irel = internal_relocs; irel < irelend; irel++)
13007
0
    {
13008
      /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13009
   we marked to in previous pass.
13010
   DO NOT scan relocations again, since we've alreadly decided it
13011
   and set the flag.  */
13012
0
      const char *syname;
13013
0
      int syndx;
13014
0
      uint32_t insn;
13015
13016
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13017
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13018
0
  unused_region = true;
13019
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13020
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13021
0
  unused_region = false;
13022
13023
      /* We're not in the region.  */
13024
0
      if (!unused_region)
13025
0
  continue;
13026
13027
      /* _FP_BASE_ must be a GLOBAL symbol.  */
13028
0
      syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13029
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13030
0
  continue;
13031
13032
      /* The symbol name must be _FP_BASE_.  */
13033
0
      syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13034
0
      if (strcmp (syname, FP_BASE_NAME) != 0)
13035
0
  continue;
13036
13037
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13038
0
  {
13039
    /* addi.gp  $fp, -256  */
13040
0
    insn = bfd_getb32 (contents + irel->r_offset);
13041
0
    if (insn != INSN_ADDIGP_TO_FP)
13042
0
      continue;
13043
0
  }
13044
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13045
0
  {
13046
    /* addi  $fp, $gp, -256  */
13047
0
    insn = bfd_getb32 (contents + irel->r_offset);
13048
0
    if (insn != INSN_ADDI_GP_TO_FP)
13049
0
      continue;
13050
0
  }
13051
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13052
0
  {
13053
    /* movi  $fp, FP_BASE  */
13054
0
    insn = bfd_getb32 (contents + irel->r_offset);
13055
0
    if (insn != INSN_MOVI_TO_FP)
13056
0
      continue;
13057
0
  }
13058
0
      else
13059
0
  continue;
13060
13061
      /* We got here because a FP_BASE instruction is found.  */
13062
0
      if (!insert_nds32_elf_blank_recalc_total
13063
0
    (&relax_blank_list, irel->r_offset, 4))
13064
0
  goto error_return;
13065
0
    }
13066
13067
0
 finish:
13068
0
  if (relax_blank_list)
13069
0
    {
13070
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13071
0
      relax_blank_list = NULL;
13072
0
    }
13073
0
  return result;
13074
13075
0
 error_return:
13076
0
  result = false;
13077
0
  goto finish;
13078
0
}
13079
13080
/* This is a version of bfd_generic_get_relocated_section_contents.
13081
   We need this variety because relaxation will modify the dwarf
13082
   infomation.  When there is undefined symbol reference error mesage,
13083
   linker need to dump line number where the symbol be used.  However
13084
   the address is be relaxed, it can not get the original dwarf contents.
13085
   The variety only modify function call for reading in the section.  */
13086
13087
static bfd_byte *
13088
nds32_elf_get_relocated_section_contents (bfd *abfd,
13089
            struct bfd_link_info *link_info,
13090
            struct bfd_link_order *link_order,
13091
            bfd_byte *data,
13092
            bool relocatable,
13093
            asymbol **symbols)
13094
12
{
13095
12
  bfd *input_bfd = link_order->u.indirect.section->owner;
13096
12
  asection *input_section = link_order->u.indirect.section;
13097
12
  long reloc_size;
13098
12
  arelent **reloc_vector;
13099
12
  long reloc_count;
13100
13101
12
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13102
12
  if (reloc_size < 0)
13103
1
    return NULL;
13104
13105
  /* Read in the section.  */
13106
11
  bfd_byte *orig_data = data;
13107
11
  if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
13108
0
    return NULL;
13109
13110
11
  if (reloc_size == 0)
13111
0
    return data;
13112
13113
11
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13114
11
  if (reloc_vector == NULL)
13115
0
    goto error_return;
13116
13117
11
  reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13118
11
          reloc_vector, symbols);
13119
11
  if (reloc_count < 0)
13120
5
    goto error_return;
13121
13122
6
  if (reloc_count > 0)
13123
6
    {
13124
6
      arelent **parent;
13125
16
      for (parent = reloc_vector; *parent != NULL; parent++)
13126
15
  {
13127
15
    char *error_message = NULL;
13128
15
    asymbol *symbol;
13129
15
    bfd_reloc_status_type r;
13130
13131
15
    symbol = *(*parent)->sym_ptr_ptr;
13132
15
    if (symbol->section && discarded_section (symbol->section))
13133
0
      {
13134
0
        bfd_vma off;
13135
0
        static reloc_howto_type none_howto
13136
0
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13137
0
       "unused", false, 0, 0, false);
13138
13139
0
        off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13140
0
                input_section);
13141
0
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13142
0
           input_section, data, off);
13143
0
        (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13144
0
        (*parent)->addend = 0;
13145
0
        (*parent)->howto = &none_howto;
13146
0
        r = bfd_reloc_ok;
13147
0
      }
13148
15
    else
13149
15
      r = bfd_perform_relocation (input_bfd, *parent, data,
13150
15
          input_section,
13151
15
          relocatable ? abfd : NULL,
13152
15
          &error_message);
13153
13154
15
    if (relocatable)
13155
0
      {
13156
0
        asection *os = input_section->output_section;
13157
13158
        /* A partial link, so keep the relocs.  */
13159
0
        os->orelocation[os->reloc_count] = *parent;
13160
0
        os->reloc_count++;
13161
0
      }
13162
13163
15
    if (r != bfd_reloc_ok)
13164
5
      {
13165
5
        _bfd_link_reloc_status_error (abfd, link_info, input_section,
13166
5
              *parent, error_message, r);
13167
5
        if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
13168
5
    goto error_return;
13169
5
      }
13170
15
  }
13171
6
    }
13172
13173
1
  free (reloc_vector);
13174
1
  return data;
13175
13176
10
 error_return:
13177
10
  free (reloc_vector);
13178
10
  if (orig_data == NULL)
13179
0
    free (data);
13180
10
  return NULL;
13181
6
}
13182
13183
/* Check target symbol.  */
13184
13185
static bool
13186
nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13187
221
{
13188
221
  if (!sym || !sym->name || sym->name[0] != '$')
13189
221
    return false;
13190
0
  return true;
13191
221
}
13192
13193
/* nds32 find maybe function sym.  Ignore target special symbol
13194
   first, and then go the general function.  */
13195
13196
static bfd_size_type
13197
nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13198
            bfd_vma *code_off)
13199
221
{
13200
221
  if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13201
0
    return 0;
13202
13203
221
  return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13204
221
}
13205

13206
13207
/* Do TLS model conversion.  */
13208
13209
typedef struct relax_group_list_t
13210
{
13211
  Elf_Internal_Rela *relo;
13212
  struct relax_group_list_t *next;
13213
  struct relax_group_list_t *next_sibling;
13214
  int id;
13215
} relax_group_list_t;
13216
13217
int
13218
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13219
13220
int
13221
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13222
13223
void
13224
dump_chain (relax_group_list_t *pHead);
13225
13226
int
13227
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13228
0
{
13229
0
  relax_group_list_t *pNext = pHead;
13230
13231
  /* Find place.  */
13232
0
  while (pNext->next)
13233
0
    {
13234
0
      if (pNext->next->id > (int) pElem->r_addend)
13235
0
  break;
13236
13237
0
      pNext = pNext->next;
13238
0
    }
13239
13240
  /* Insert node.  */
13241
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13242
0
  if (!pNew)
13243
0
    return false;
13244
13245
0
  relax_group_list_t *tmp = pNext->next;
13246
0
  pNext->next = pNew;
13247
13248
0
  pNew->id = pElem->r_addend;
13249
0
  pNew->relo = pElem;
13250
0
  pNew->next = tmp;
13251
0
  pNew->next_sibling = NULL;
13252
13253
0
  return true;
13254
0
}
13255
13256
int
13257
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13258
0
{
13259
0
  relax_group_list_t *pNext = pNode;
13260
13261
  /* Find place.  */
13262
0
  while (pNext->next_sibling)
13263
0
    {
13264
0
      pNext = pNext->next_sibling;
13265
0
    }
13266
13267
  /* Insert node.  */
13268
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13269
0
  if (!pNew)
13270
0
    return false;
13271
13272
0
  relax_group_list_t *tmp = pNext->next_sibling;
13273
0
  pNext->next_sibling = pNew;
13274
13275
0
  pNew->id = -1;
13276
0
  pNew->relo = pElem;
13277
0
  pNew->next = NULL;
13278
0
  pNew->next_sibling = tmp;
13279
13280
0
  return true;
13281
0
}
13282
13283
void
13284
dump_chain (relax_group_list_t *pHead)
13285
0
{
13286
0
  relax_group_list_t *pNext = pHead->next;
13287
0
  while (pNext)
13288
0
    {
13289
0
      printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13290
0
      relax_group_list_t *pNextSib = pNext->next_sibling;
13291
0
      while (pNextSib)
13292
0
  {
13293
0
    printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13294
0
    pNextSib = pNextSib->next_sibling;
13295
0
  }
13296
0
      pNext = pNext->next;
13297
0
      printf("\n");
13298
0
    }
13299
0
}
13300
13301
/* Check R_NDS32_RELAX_GROUP of each section.
13302
   There might be multiple sections in one object file.  */
13303
13304
int
13305
elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13306
0
{
13307
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13308
0
    elf32_nds32_relax_group_ptr (abfd);
13309
13310
0
  int min_id = relax_group_ptr->min_id;
13311
0
  int max_id = relax_group_ptr->max_id;
13312
13313
0
  Elf_Internal_Rela *rel;
13314
0
  Elf_Internal_Rela *relend;
13315
0
  Elf_Internal_Rela *relocs;
13316
0
  enum elf_nds32_reloc_type rtype;
13317
13318
0
  do
13319
0
    {
13320
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13321
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13322
0
            true /* keep_memory  */);
13323
0
      if (relocs == NULL)
13324
0
  break;
13325
13326
      /* Check R_NDS32_RELAX_GROUP.  */
13327
0
      relend = relocs + asec->reloc_count;
13328
0
      for (rel = relocs; rel < relend; rel++)
13329
0
  {
13330
0
    int id;
13331
0
    rtype = ELF32_R_TYPE (rel->r_info);
13332
0
    if (rtype != R_NDS32_RELAX_GROUP)
13333
0
      continue;
13334
13335
0
    id = rel->r_addend;
13336
0
    if (id < min_id)
13337
0
      min_id = id;
13338
0
    else if (id > max_id)
13339
0
      max_id = id;
13340
0
  }
13341
0
    }
13342
0
  while (false);
13343
13344
0
  if (elf_section_data (asec)->relocs != relocs)
13345
0
    free (relocs);
13346
13347
0
  if ((min_id != relax_group_ptr->min_id)
13348
0
      || (max_id != relax_group_ptr->max_id))
13349
0
    {
13350
0
      relax_group_ptr->count = max_id - min_id + 1;
13351
0
      BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13352
0
      relax_group_ptr->min_id = min_id;
13353
0
      BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13354
0
      relax_group_ptr->max_id = max_id;
13355
0
    }
13356
13357
0
  return relax_group_ptr->count;
13358
0
}
13359
13360
/* Reorder RELAX_GROUP ID when command line option '-r' is applied.  */
13361
static struct section_id_list_t *relax_group_section_id_list = NULL;
13362
13363
struct section_id_list_t *
13364
elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13365
0
{
13366
0
  struct section_id_list_t *result = NULL;
13367
0
  struct section_id_list_t *lst = *lst_ptr;
13368
13369
0
  if (NULL == lst)
13370
0
    {
13371
0
      result = (struct section_id_list_t *) calloc
13372
0
  (1, sizeof (struct section_id_list_t));
13373
0
      BFD_ASSERT (result); /* Feed me.  */
13374
0
      result->id = id;
13375
0
      *lst_ptr = result;
13376
0
    }
13377
0
  else
13378
0
    {
13379
0
      struct section_id_list_t *cur = lst;
13380
0
      struct section_id_list_t *prv = NULL;
13381
0
      struct section_id_list_t *sec = NULL;
13382
13383
0
      while (cur)
13384
0
  {
13385
0
    if (cur->id < id)
13386
0
      {
13387
0
        prv = cur;
13388
0
        cur = cur->next;
13389
0
        continue;
13390
0
      }
13391
13392
0
    if (cur->id > id)
13393
0
      {
13394
0
        cur = NULL; /* To insert after prv.  */
13395
0
        sec = cur;  /* In case prv == NULL.  */
13396
0
      }
13397
13398
0
    break;
13399
0
  }
13400
13401
0
      if (NULL == cur)
13402
0
  {
13403
    /* Insert after prv.  */
13404
0
    result = (struct section_id_list_t *) calloc
13405
0
      (1, sizeof (struct section_id_list_t));
13406
0
    BFD_ASSERT (result); /* Feed me.  */
13407
0
    result->id = id;
13408
0
    if (NULL != prv)
13409
0
      {
13410
0
        result->next = prv->next;
13411
0
        prv->next = result;
13412
0
      }
13413
0
    else
13414
0
      {
13415
0
        *lst_ptr = result;
13416
0
        result->next = sec;
13417
0
      }
13418
0
  }
13419
0
    }
13420
13421
0
  return result;
13422
0
}
13423
13424
int
13425
elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13426
0
{
13427
0
  static int next_relax_group_bias = 0;
13428
13429
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13430
0
    elf32_nds32_relax_group_ptr (abfd);
13431
13432
0
  bool result = true;
13433
0
  Elf_Internal_Rela *rel;
13434
0
  Elf_Internal_Rela *relend;
13435
0
  Elf_Internal_Rela *relocs = NULL;
13436
0
  enum elf_nds32_reloc_type rtype;
13437
0
  struct section_id_list_t *node = NULL;
13438
13439
0
  do
13440
0
    {
13441
0
      if (0 == relax_group_ptr->count)
13442
0
  break;
13443
13444
      /* Check if this section has been handled.  */
13445
0
      node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13446
0
      if (NULL == node)
13447
0
  break; /* Hit, the section id has handled.  */
13448
13449
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13450
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13451
0
            true /* keep_memory  */);
13452
0
      if (relocs == NULL)
13453
0
  {
13454
0
    BFD_ASSERT (0); /* feed me */
13455
0
    break;
13456
0
  }
13457
13458
      /* Allocate group id bias for this bfd!  */
13459
0
      if (0 == relax_group_ptr->init)
13460
0
  {
13461
0
    relax_group_ptr->bias = next_relax_group_bias;
13462
0
    next_relax_group_bias += relax_group_ptr->count;
13463
0
    relax_group_ptr->init = 1;
13464
0
  }
13465
13466
      /* Reorder relax group groups.  */
13467
0
      relend = relocs + asec->reloc_count;
13468
0
      for (rel = relocs; rel < relend; rel++)
13469
0
  {
13470
0
    rtype = ELF32_R_TYPE(rel->r_info);
13471
0
    if (rtype != R_NDS32_RELAX_GROUP)
13472
0
      continue;
13473
13474
    /* Change it.  */
13475
0
    rel->r_addend += relax_group_ptr->bias;
13476
0
  }
13477
0
    }
13478
0
  while (false);
13479
13480
0
  if (elf_section_data (asec)->relocs != relocs)
13481
0
    free (relocs);
13482
13483
0
  return result;
13484
0
}
13485
13486
int
13487
nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13488
         struct bfd_link_info *lnkinfo)
13489
0
{
13490
0
  bool result = true;
13491
0
  Elf_Internal_Rela *irel;
13492
0
  Elf_Internal_Rela *irelend;
13493
0
  Elf_Internal_Rela *internal_relocs;
13494
0
  unsigned long r_symndx;
13495
0
  enum elf_nds32_reloc_type r_type;
13496
13497
0
  Elf_Internal_Sym *local_syms = NULL;
13498
0
  bfd_byte *contents = NULL;
13499
13500
0
  relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13501
13502
0
  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13503
0
  struct elf_link_hash_entry **sym_hashes;
13504
0
  sym_hashes = elf_sym_hashes (inbfd);
13505
13506
  /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
13507
0
  if (bfd_link_relocatable (lnkinfo))
13508
0
    {
13509
0
      elf32_nds32_unify_relax_group (inbfd, insec);
13510
0
      return result;
13511
0
    }
13512
13513
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13514
0
  internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13515
0
                 true /* keep_memory  */);
13516
0
  if (internal_relocs == NULL)
13517
0
    goto error_return;
13518
13519
0
  irelend = internal_relocs + insec->reloc_count;
13520
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
13521
0
         irelend, R_NDS32_RELAX_ENTRY);
13522
0
  if (irel == irelend)
13523
0
    goto finish;
13524
13525
  /* Chain/remove groups.  */
13526
0
  for (irel = internal_relocs; irel < irelend; irel++)
13527
0
    {
13528
0
      r_symndx = ELF32_R_SYM (irel->r_info);
13529
0
      r_type = ELF32_R_TYPE (irel->r_info);
13530
0
      if (r_type != R_NDS32_RELAX_GROUP)
13531
0
  continue;
13532
13533
      /* Remove it.  */
13534
0
      irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13535
      /* Chain it now.  */
13536
0
      if (!list_insert (&chain, irel))
13537
0
  goto error_return;
13538
0
    }
13539
13540
  /* Collect group relocations.  */
13541
  /* Presume relocations are sorted.  */
13542
0
  relax_group_list_t *pNext = chain.next;
13543
0
  while (pNext)
13544
0
    {
13545
0
      for (irel = internal_relocs; irel < irelend; irel++)
13546
0
  {
13547
0
    if (irel->r_offset == pNext->relo->r_offset)
13548
0
      {
13549
        /* Ignore Non-TLS relocation types.  */
13550
0
        r_type = ELF32_R_TYPE (irel->r_info);
13551
0
        if ((R_NDS32_TLS_LE_HI20 > r_type)
13552
0
      || (R_NDS32_RELAX_ENTRY == r_type))
13553
0
    continue;
13554
13555
0
        if (!list_insert_sibling (pNext, irel))
13556
0
    goto error_return;
13557
0
      }
13558
0
    else if (irel->r_offset > pNext->relo->r_offset)
13559
0
      {
13560
0
        pNext = pNext->next;
13561
0
        if (!pNext)
13562
0
    break;
13563
13564
0
        bfd_vma current_offset = pNext->relo->r_offset;
13565
0
        if (irel->r_offset > current_offset)
13566
0
    irel = internal_relocs; /* restart from head */
13567
0
        else
13568
0
    --irel; /* Check current irel again.  */
13569
0
        continue;
13570
0
      }
13571
0
    else
13572
0
      {
13573
        /* This shouldn't be reached.  */
13574
0
      }
13575
0
  }
13576
0
      if (pNext)
13577
0
  pNext = pNext->next;
13578
0
    }
13579
13580
#ifdef DUBUG_VERBOSE
13581
  dump_chain(&chain);
13582
#endif
13583
13584
  /* Get symbol table and section content.  */
13585
0
  if (incontents)
13586
0
    contents = incontents;
13587
0
  else if (!nds32_get_section_contents (inbfd, insec, &contents, true)
13588
0
     || !nds32_get_local_syms (inbfd, insec, &local_syms))
13589
0
    goto error_return;
13590
13591
0
  char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13592
13593
  /* Convert TLS model each group if necessary.  */
13594
0
  pNext = chain.next;
13595
13596
0
  int cur_grp_id = -1;
13597
0
  int sethi_rt = -1;
13598
0
  int add_rt = -1;
13599
0
  enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13600
13601
0
  tls_type = org_tls_type = eff_tls_type = 0;
13602
13603
0
  while (pNext)
13604
0
    {
13605
0
      relax_group_list_t *pNextSig = pNext->next_sibling;
13606
0
      while (pNextSig)
13607
0
  {
13608
0
    struct elf_link_hash_entry *h = NULL;
13609
13610
0
    irel = pNextSig->relo;
13611
0
    r_symndx = ELF32_R_SYM(irel->r_info);
13612
0
    r_type = ELF32_R_TYPE(irel->r_info);
13613
13614
0
    if (pNext->id != cur_grp_id)
13615
0
      {
13616
0
        cur_grp_id = pNext->id;
13617
0
        org_tls_type = get_tls_type (r_type, NULL);
13618
0
        if (r_symndx >= symtab_hdr->sh_info)
13619
0
    {
13620
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13621
0
      while (h->root.type == bfd_link_hash_indirect
13622
0
       || h->root.type == bfd_link_hash_warning)
13623
0
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
13624
0
      tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13625
0
    }
13626
0
        else
13627
0
    {
13628
0
      tls_type = local_got_tls_type
13629
0
        ? local_got_tls_type[r_symndx]
13630
0
        : GOT_NORMAL;
13631
0
    }
13632
13633
0
        eff_tls_type = 1 << (fls (tls_type) - 1);
13634
0
        sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13635
0
      }
13636
13637
0
    if (eff_tls_type != org_tls_type)
13638
0
      {
13639
0
        switch (org_tls_type)
13640
0
    {
13641
      /* DESC to IEGP/IE/LE.  */
13642
0
    case GOT_TLS_DESC:
13643
0
      switch (eff_tls_type)
13644
0
        {
13645
0
        case GOT_TLS_IE:
13646
0
          switch (r_type)
13647
0
      {
13648
0
      case R_NDS32_TLS_DESC_HI20:
13649
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13650
0
                  R_NDS32_TLS_IE_HI20);
13651
0
        break;
13652
0
      case R_NDS32_TLS_DESC_LO12:
13653
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13654
0
                  R_NDS32_TLS_IE_LO12);
13655
0
        break;
13656
0
      case R_NDS32_TLS_DESC_ADD:
13657
0
        {
13658
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13659
0
          add_rt = N32_RT5 (insn);
13660
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13661
0
          bfd_putb32 (insn, contents + irel->r_offset);
13662
13663
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13664
0
        }
13665
0
        break;
13666
0
      case R_NDS32_TLS_DESC_FUNC:
13667
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13668
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13669
0
                  R_NDS32_RELAX_REMOVE);
13670
0
        break;
13671
0
      case R_NDS32_TLS_DESC_CALL:
13672
0
        {
13673
0
          uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13674
0
                 REG_TP);
13675
0
          bfd_putb32 (insn, contents + irel->r_offset);
13676
13677
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13678
0
        }
13679
0
        break;
13680
0
      case R_NDS32_LOADSTORE:
13681
0
      case R_NDS32_PTR:
13682
0
      case R_NDS32_PTR_RESOLVED:
13683
0
      case R_NDS32_NONE:
13684
0
      case R_NDS32_LABEL:
13685
0
        break;
13686
0
      default:
13687
0
        BFD_ASSERT(0);
13688
0
        break;
13689
0
      }
13690
0
          break;
13691
0
        case GOT_TLS_IEGP:
13692
0
          switch (r_type)
13693
0
      {
13694
0
      case R_NDS32_TLS_DESC_HI20:
13695
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13696
0
                  R_NDS32_TLS_IEGP_HI20);
13697
0
        break;
13698
0
      case R_NDS32_TLS_DESC_LO12:
13699
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13700
0
                  R_NDS32_TLS_IEGP_LO12);
13701
0
        break;
13702
0
      case R_NDS32_TLS_DESC_ADD:
13703
0
        {
13704
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13705
0
          add_rt = N32_RT5 (insn);
13706
0
          insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13707
0
          bfd_putb32 (insn, contents + irel->r_offset);
13708
13709
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13710
0
        }
13711
0
        break;
13712
0
      case R_NDS32_TLS_DESC_FUNC:
13713
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13714
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13715
0
                  R_NDS32_RELAX_REMOVE);
13716
0
        break;
13717
0
      case R_NDS32_TLS_DESC_CALL:
13718
0
        {
13719
0
          uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13720
0
                 REG_TP);
13721
0
          bfd_putb32 (insn, contents + irel->r_offset);
13722
13723
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13724
0
        }
13725
0
        break;
13726
0
      case R_NDS32_LOADSTORE:
13727
0
      case R_NDS32_PTR:
13728
0
      case R_NDS32_PTR_RESOLVED:
13729
0
      case R_NDS32_NONE:
13730
0
      case R_NDS32_LABEL:
13731
0
        break;
13732
0
      default:
13733
0
        BFD_ASSERT(0);
13734
0
        break;
13735
0
      }
13736
0
          break;
13737
0
        case GOT_TLS_LE:
13738
0
          switch (r_type)
13739
0
      {
13740
0
      case R_NDS32_TLS_DESC_HI20:
13741
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13742
0
        break;
13743
0
      case R_NDS32_TLS_DESC_LO12:
13744
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13745
0
        break;
13746
0
      case R_NDS32_TLS_DESC_ADD:
13747
0
        {
13748
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13749
13750
0
          add_rt = N32_RT5 (insn);
13751
0
          insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13752
0
          bfd_putb32 (insn, contents + irel->r_offset);
13753
13754
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13755
0
        }
13756
0
        break;
13757
0
      case R_NDS32_TLS_DESC_FUNC:
13758
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13759
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13760
0
        break;
13761
0
      case R_NDS32_TLS_DESC_CALL:
13762
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13763
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13764
0
        break;
13765
0
      case R_NDS32_LOADSTORE:
13766
0
      case R_NDS32_PTR:
13767
0
      case R_NDS32_PTR_RESOLVED:
13768
0
      case R_NDS32_NONE:
13769
0
      case R_NDS32_LABEL:
13770
0
        break;
13771
0
      default:
13772
0
        BFD_ASSERT(0);
13773
0
        break;
13774
0
      }
13775
0
          break;
13776
0
        default:
13777
0
          break;
13778
0
        }
13779
0
      break;
13780
      /* IEGP to IE/LE.  */
13781
0
    case GOT_TLS_IEGP:
13782
0
      switch (eff_tls_type)
13783
0
        {
13784
0
        case GOT_TLS_IE:
13785
0
          switch (r_type)
13786
0
      {
13787
0
      case R_NDS32_TLS_IEGP_HI20:
13788
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13789
0
                  R_NDS32_TLS_IE_HI20);
13790
0
        break;
13791
0
      case R_NDS32_TLS_IEGP_LO12:
13792
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13793
0
                  R_NDS32_TLS_IE_LO12);
13794
0
        break;
13795
0
      case R_NDS32_PTR_RESOLVED:
13796
0
        {
13797
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13798
13799
0
          add_rt = N32_RT5 (insn);
13800
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13801
0
          bfd_putb32 (insn, contents + irel->r_offset);
13802
0
        }
13803
0
        break;
13804
0
      case R_NDS32_TLS_IEGP_LW:
13805
0
        break;
13806
0
      case R_NDS32_LOADSTORE:
13807
0
      case R_NDS32_PTR:
13808
0
      case R_NDS32_NONE:
13809
0
      case R_NDS32_LABEL:
13810
0
        break;
13811
0
      default:
13812
0
        BFD_ASSERT(0);
13813
0
        break;
13814
0
      }
13815
0
          break;
13816
0
        case GOT_TLS_LE:
13817
0
          switch (r_type)
13818
0
      {
13819
0
      case R_NDS32_TLS_IEGP_HI20:
13820
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13821
0
        break;
13822
0
      case R_NDS32_TLS_IEGP_LO12:
13823
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13824
0
        break;
13825
0
      case R_NDS32_TLS_IEGP_LW:
13826
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13827
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13828
0
        break;
13829
0
      case R_NDS32_LOADSTORE:
13830
0
      case R_NDS32_PTR:
13831
0
      case R_NDS32_NONE:
13832
0
      case R_NDS32_LABEL:
13833
0
      case R_NDS32_PTR_RESOLVED:
13834
0
        break;
13835
0
      default:
13836
0
        BFD_ASSERT(0);
13837
0
        break;
13838
0
      }
13839
0
          break;
13840
0
        default:
13841
0
          break;
13842
0
        }
13843
0
      break;
13844
      /* IE to LE. */
13845
0
    case GOT_TLS_IE:
13846
0
      switch (eff_tls_type)
13847
0
        {
13848
0
        case GOT_TLS_LE:
13849
0
          switch (r_type)
13850
0
      {
13851
0
      case R_NDS32_TLS_IE_HI20:
13852
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13853
0
        break;
13854
0
      case R_NDS32_TLS_IE_LO12S2:
13855
0
        {
13856
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13857
13858
0
          add_rt = N32_RT5 (insn);
13859
0
          insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13860
0
          bfd_putb32 (insn, contents + irel->r_offset);
13861
13862
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13863
0
        }
13864
0
        break;
13865
0
      case R_NDS32_LOADSTORE:
13866
0
      case R_NDS32_PTR:
13867
0
      case R_NDS32_NONE:
13868
0
      case R_NDS32_LABEL:
13869
0
        break;
13870
0
      default:
13871
0
        BFD_ASSERT(0);
13872
0
        break;
13873
0
      }
13874
0
          break;
13875
0
        default:
13876
0
          break;
13877
0
        }
13878
0
      break;
13879
0
    default:
13880
0
      break;
13881
0
    }
13882
0
      }
13883
0
    pNextSig = pNextSig->next_sibling;
13884
0
  }
13885
13886
0
#if 1
13887
0
      pNext = pNext->next;
13888
#else
13889
      while (pNext)
13890
  {
13891
    if (pNext->id != cur_grp_id)
13892
      break;
13893
    pNext = pNext->next;
13894
  }
13895
#endif
13896
0
    }
13897
13898
0
 finish:
13899
0
  if (incontents)
13900
0
    contents = NULL;
13901
13902
0
  if (elf_section_data (insec)->relocs != internal_relocs)
13903
0
    free (internal_relocs);
13904
13905
0
  if (elf_section_data (insec)->this_hdr.contents != contents)
13906
0
    free (contents);
13907
13908
0
  if (symtab_hdr->contents != (bfd_byte *) local_syms)
13909
0
    free (local_syms);
13910
13911
0
  if (chain.next)
13912
0
    {
13913
0
      pNext = chain.next;
13914
0
      relax_group_list_t *pDel;
13915
0
      while (pNext)
13916
0
  {
13917
0
    pDel = pNext;
13918
0
    pNext = pNext->next;
13919
0
    free (pDel);
13920
0
  }
13921
0
    }
13922
13923
0
  return result;
13924
13925
0
 error_return:
13926
  result = false;
13927
0
  goto finish;
13928
0
}
13929
13930
/* End TLS model conversion.  */
13931
13932
#define ELF_ARCH        bfd_arch_nds32
13933
#define ELF_MACHINE_CODE      EM_NDS32
13934
#define ELF_MAXPAGESIZE       0x1000
13935
#define ELF_TARGET_ID       NDS32_ELF_DATA
13936
13937
#define TARGET_BIG_SYM        nds32_elf32_be_vec
13938
#define TARGET_BIG_NAME       "elf32-nds32be"
13939
#define TARGET_LITTLE_SYM     nds32_elf32_le_vec
13940
#define TARGET_LITTLE_NAME      "elf32-nds32le"
13941
13942
#define elf_info_to_howto     nds32_info_to_howto
13943
#define elf_info_to_howto_rel     nds32_info_to_howto_rel
13944
13945
#define bfd_elf32_bfd_link_hash_table_create  nds32_elf_link_hash_table_create
13946
#define bfd_elf32_bfd_merge_private_bfd_data  nds32_elf_merge_private_bfd_data
13947
#define bfd_elf32_bfd_print_private_bfd_data  nds32_elf_print_private_bfd_data
13948
#define bfd_elf32_bfd_relax_section   nds32_elf_relax_section
13949
#define bfd_elf32_bfd_set_private_flags   nds32_elf_set_private_flags
13950
13951
#define bfd_elf32_mkobject      nds32_elf_mkobject
13952
#define elf_backend_action_discarded    nds32_elf_action_discarded
13953
#define elf_backend_add_symbol_hook   nds32_elf_add_symbol_hook
13954
#define elf_backend_check_relocs    nds32_elf_check_relocs
13955
#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
13956
#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
13957
#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
13958
#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
13959
#define elf_backend_late_size_sections    nds32_elf_late_size_sections
13960
#define elf_backend_relocate_section    nds32_elf_relocate_section
13961
#define elf_backend_gc_mark_hook    nds32_elf_gc_mark_hook
13962
#define elf_backend_grok_prstatus   nds32_elf_grok_prstatus
13963
#define elf_backend_grok_psinfo     nds32_elf_grok_psinfo
13964
#define elf_backend_reloc_type_class    nds32_elf_reloc_type_class
13965
#define elf_backend_copy_indirect_symbol  nds32_elf_copy_indirect_symbol
13966
#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
13967
#define elf_backend_output_arch_syms    nds32_elf_output_arch_syms
13968
#define elf_backend_object_p      nds32_elf_object_p
13969
#define elf_backend_final_write_processing  nds32_elf_final_write_processing
13970
#define elf_backend_special_sections    nds32_elf_special_sections
13971
#define elf_backend_section_flags   nds32_elf_section_flags
13972
#define bfd_elf32_bfd_get_relocated_section_contents \
13973
        nds32_elf_get_relocated_section_contents
13974
#define bfd_elf32_bfd_is_target_special_symbol  nds32_elf_is_target_special_symbol
13975
#define elf_backend_maybe_function_sym    nds32_elf_maybe_function_sym
13976
13977
#define elf_backend_can_gc_sections   1
13978
#define elf_backend_can_refcount    1
13979
#define elf_backend_want_got_plt    1
13980
#define elf_backend_plt_readonly    1
13981
#define elf_backend_want_plt_sym    0
13982
#define elf_backend_got_header_size   12
13983
#define elf_backend_may_use_rel_p   1
13984
#define elf_backend_default_use_rela_p    1
13985
#define elf_backend_may_use_rela_p    1
13986
#define elf_backend_dtrel_excludes_plt    0
13987
13988
#include "elf32-target.h"
13989
13990
#undef ELF_MAXPAGESIZE
13991
#define ELF_MAXPAGESIZE       0x2000
13992
13993
#undef  TARGET_BIG_SYM
13994
#define TARGET_BIG_SYM        nds32_elf32_linux_be_vec
13995
#undef  TARGET_BIG_NAME
13996
#define TARGET_BIG_NAME       "elf32-nds32be-linux"
13997
#undef  TARGET_LITTLE_SYM
13998
#define TARGET_LITTLE_SYM     nds32_elf32_linux_le_vec
13999
#undef  TARGET_LITTLE_NAME
14000
#define TARGET_LITTLE_NAME      "elf32-nds32le-linux"
14001
#undef  elf32_bed
14002
#define elf32_bed       elf32_nds32_lin_bed
14003
14004
#include "elf32-target.h"