Coverage Report

Created: 2025-06-24 06:45

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